summaryrefslogtreecommitdiff
path: root/Source/Core/MacUpdater/MacUI.mm
blob: b52da3182fe3e1d8825c5387a0b844a694c87d1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2019 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "MacUpdater/ViewController.h"

#include "UpdaterCommon/Platform.h"
#include "UpdaterCommon/UI.h"
#include "UpdaterCommon/UpdaterCommon.h"

#include <Cocoa/Cocoa.h>
#include <unistd.h>

#include <functional>

// When we call from the main thread, we are not allowed to use
// dispatch_sync(dispatch_get_main_queue() as it will cause crashes) To prevent this check if we're
// already on the main thread first
void run_on_main(std::function<void()> fnc)
{
  if (![NSThread isMainThread])
  {
    dispatch_sync(dispatch_get_main_queue(), ^{
      fnc();
    });
  }
  else
  {
    fnc();
  }
}

NSWindow* GetWindow()
{
  return [[[NSApplication sharedApplication] windows] objectAtIndex:0];
}

ViewController* GetView()
{
  return (ViewController*)GetWindow().contentViewController;
}

void UI::Error(const std::string& text)
{
  run_on_main([&] {
    NSAlert* alert = [[[NSAlert alloc] init] autorelease];

    [alert setMessageText:@"Fatal error"];
    [alert setInformativeText:[NSString stringWithCString:text.c_str()
                                                 encoding:NSUTF8StringEncoding]];
    [alert setAlertStyle:NSAlertStyleCritical];

    [alert beginSheetModalForWindow:GetWindow()
                  completionHandler:^(NSModalResponse) {
                    [NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0];
                  }];
  });
}

void UI::SetVisible(bool visible)
{
  run_on_main([&] {
    if (visible)
    {
      [NSApp unhide:nil];
      [NSApp activateIgnoringOtherApps:YES];
    }
    else
    {
      [NSApp hide:nil];
    }
  });
}

void UI::SetDescription(const std::string& text)
{
  run_on_main([&] {
    [GetView() SetDescription:[NSString stringWithCString:text.c_str()
                                                 encoding:NSUTF8StringEncoding]];
  });
}

void UI::SetTotalMarquee(bool marquee)
{
  run_on_main([marquee] { [GetView() SetTotalMarquee:marquee]; });
}

void UI::SetCurrentMarquee(bool marquee)
{
  run_on_main([&] { [GetView() SetCurrentMarquee:marquee]; });
}

void UI::ResetTotalProgress()
{
  run_on_main([] { SetTotalProgress(0, 1); });
}

void UI::ResetCurrentProgress()
{
  run_on_main([] { SetCurrentProgress(0, 1); });
}

void UI::SetCurrentProgress(int current, int total)
{
  run_on_main([&] { [GetView() SetCurrentProgress:(double)current total:(double)total]; });
}

void UI::SetTotalProgress(int current, int total)
{
  run_on_main([&] { [GetView() SetTotalProgress:(double)current total:(double)total]; });
}

void UI::Sleep(int seconds)
{
  [NSThread sleepForTimeInterval:static_cast<float>(seconds)];
}

void UI::WaitForPID(u32 pid)
{
  for (int res = kill(pid, 0); res == 0 || (res < 0 && errno == EPERM); res = kill(pid, 0))
  {
    UI::Sleep(1);
  }
}

void UI::LaunchApplication(std::string path)
{
  [[NSWorkspace sharedWorkspace]
      launchApplication:[NSString stringWithCString:path.c_str()
                                           encoding:[NSString defaultCStringEncoding]]];
}

void UI::Stop()
{
  run_on_main([] { [NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0]; });
}

// Stub. Only needed on Windows
void UI::Init()
{
}

// test-updater.py only works on Windows.
bool UI::IsTestMode()
{
  return false;
}

bool Platform::VersionCheck(const std::vector<TodoList::UpdateOp>& to_update,
                            const std::string& install_base_path, const std::string& temp_dir)
{
  const auto op_it = std::ranges::find(to_update, "Dolphin.app/Contents/Info.plist",
                                       &TodoList::UpdateOp::filename);
  if (op_it == to_update.cend())
    return true;

  const auto op = *op_it;
  std::string plist_path = temp_dir + "/" + HexEncode(op.new_hash.data(), op.new_hash.size());

  NSData* data = [NSData dataWithContentsOfFile:[NSString stringWithCString:plist_path.c_str()]];
  if (!data)
  {
    LogToFile("Failed to read %s, skipping platform version check.\n", plist_path.c_str());
    return true;
  }

  NSError* error = nil;
  NSDictionary* info_dict =
      [NSPropertyListSerialization propertyListWithData:data
                                                options:NSPropertyListImmutable
                                                 format:nil
                                                  error:&error];
  if (error)
  {
    LogToFile("Failed to parse %s, skipping platform version check.\n", plist_path.c_str());
    return true;
  }
  NSString* min_version_str = info_dict[@"LSMinimumSystemVersion"];
  if (!min_version_str)
  {
    LogToFile("LSMinimumSystemVersion key missing, skipping platform version check.\n");
    return true;
  }

  NSArray* components = [min_version_str componentsSeparatedByString:@"."];
  NSOperatingSystemVersion next_version{
      [components[0] integerValue], [components[1] integerValue], [components[2] integerValue]};

  LogToFile("Platform version check: next_version=%ld.%ld.%ld\n", (long)next_version.majorVersion,
            (long)next_version.minorVersion, (long)next_version.patchVersion);

  if (![[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:next_version])
  {
    UI::Error("Please update macOS in order to update Dolphin.");
    return false;
  }
  return true;
}