Debian Codegraph @

Source/Core/MacUpdater/MacUI.mm

raw
1 // Copyright 2019 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "MacUpdater/ViewController.h"
5
6 #include "UpdaterCommon/Platform.h"
7 #include "UpdaterCommon/UI.h"
8 #include "UpdaterCommon/UpdaterCommon.h"
9
10 #include <Cocoa/Cocoa.h>
11 #include <unistd.h>
12
13 #include <functional>
14
15 // When we call from the main thread, we are not allowed to use
16 // dispatch_sync(dispatch_get_main_queue() as it will cause crashes) To prevent this check if we're
17 // already on the main thread first
18 void run_on_main(std::function<void()> fnc)
19 {
20 if (![NSThread isMainThread])
21 {
22 dispatch_sync(dispatch_get_main_queue(), ^{
23 fnc();
24 });
25 }
26 else
27 {
28 fnc();
29 }
30 }
31
32 NSWindow* GetWindow()
33 {
34 return [[[NSApplication sharedApplication] windows] objectAtIndex:0];
35 }
36
37 ViewController* GetView()
38 {
39 return (ViewController*)GetWindow().contentViewController;
40 }
41
42 void UI::Error(const std::string& text)
43 {
44 run_on_main([&] {
45 NSAlert* alert = [[[NSAlert alloc] init] autorelease];
46
47 [alert setMessageText:@"Fatal error"];
48 [alert setInformativeText:[NSString stringWithCString:text.c_str()
49 encoding:NSUTF8StringEncoding]];
50 [alert setAlertStyle:NSAlertStyleCritical];
51
52 [alert beginSheetModalForWindow:GetWindow()
53 completionHandler:^(NSModalResponse) {
54 [NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0];
55 }];
56 });
57 }
58
59 void UI::SetVisible(bool visible)
60 {
61 run_on_main([&] {
62 if (visible)
63 {
64 [NSApp unhide:nil];
65 [NSApp activateIgnoringOtherApps:YES];
66 }
67 else
68 {
69 [NSApp hide:nil];
70 }
71 });
72 }
73
74 void UI::SetDescription(const std::string& text)
75 {
76 run_on_main([&] {
77 [GetView() SetDescription:[NSString stringWithCString:text.c_str()
78 encoding:NSUTF8StringEncoding]];
79 });
80 }
81
82 void UI::SetTotalMarquee(bool marquee)
83 {
84 run_on_main([marquee] { [GetView() SetTotalMarquee:marquee]; });
85 }
86
87 void UI::SetCurrentMarquee(bool marquee)
88 {
89 run_on_main([&] { [GetView() SetCurrentMarquee:marquee]; });
90 }
91
92 void UI::ResetTotalProgress()
93 {
94 run_on_main([] { SetTotalProgress(0, 1); });
95 }
96
97 void UI::ResetCurrentProgress()
98 {
99 run_on_main([] { SetCurrentProgress(0, 1); });
100 }
101
102 void UI::SetCurrentProgress(int current, int total)
103 {
104 run_on_main([&] { [GetView() SetCurrentProgress:(double)current total:(double)total]; });
105 }
106
107 void UI::SetTotalProgress(int current, int total)
108 {
109 run_on_main([&] { [GetView() SetTotalProgress:(double)current total:(double)total]; });
110 }
111
112 void UI::Sleep(int seconds)
113 {
114 [NSThread sleepForTimeInterval:static_cast<float>(seconds)];
115 }
116
117 void UI::WaitForPID(u32 pid)
118 {
119 for (int res = kill(pid, 0); res == 0 || (res < 0 && errno == EPERM); res = kill(pid, 0))
120 {
121 UI::Sleep(1);
122 }
123 }
124
125 void UI::LaunchApplication(std::string path)
126 {
127 [[NSWorkspace sharedWorkspace]
128 launchApplication:[NSString stringWithCString:path.c_str()
129 encoding:[NSString defaultCStringEncoding]]];
130 }
131
132 void UI::Stop()
133 {
134 run_on_main([] { [NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0]; });
135 }
136
137 // Stub. Only needed on Windows
138 void UI::Init()
139 {
140 }
141
142 // test-updater.py only works on Windows.
143 bool UI::IsTestMode()
144 {
145 return false;
146 }
147
148 bool Platform::VersionCheck(const std::vector<TodoList::UpdateOp>& to_update,
149 const std::string& install_base_path, const std::string& temp_dir)
150 {
151 const auto op_it = std::ranges::find(to_update, "Dolphin.app/Contents/Info.plist",
152 &TodoList::UpdateOp::filename);
153 if (op_it == to_update.cend())
154 return true;
155
156 const auto op = *op_it;
157 std::string plist_path = temp_dir + "/" + HexEncode(op.new_hash.data(), op.new_hash.size());
158
159 NSData* data = [NSData dataWithContentsOfFile:[NSString stringWithCString:plist_path.c_str()]];
160 if (!data)
161 {
162 LogToFile("Failed to read %s, skipping platform version check.\n", plist_path.c_str());
163 return true;
164 }
165
166 NSError* error = nil;
167 NSDictionary* info_dict =
168 [NSPropertyListSerialization propertyListWithData:data
169 options:NSPropertyListImmutable
170 format:nil
171 error:&error];
172 if (error)
173 {
174 LogToFile("Failed to parse %s, skipping platform version check.\n", plist_path.c_str());
175 return true;
176 }
177 NSString* min_version_str = info_dict[@"LSMinimumSystemVersion"];
178 if (!min_version_str)
179 {
180 LogToFile("LSMinimumSystemVersion key missing, skipping platform version check.\n");
181 return true;
182 }
183
184 NSArray* components = [min_version_str componentsSeparatedByString:@"."];
185 NSOperatingSystemVersion next_version{
186 [components[0] integerValue], [components[1] integerValue], [components[2] integerValue]};
187
188 LogToFile("Platform version check: next_version=%ld.%ld.%ld\n", (long)next_version.majorVersion,
189 (long)next_version.minorVersion, (long)next_version.patchVersion);
190
191 if (![[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:next_version])
192 {
193 UI::Error("Please update macOS in order to update Dolphin.");
194 return false;
195 }
196 return true;
197 }