| 1 |
// Copyright 2023 Dolphin Emulator Project |
| 2 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
|
| 4 |
#include "DolphinNoGUI/Platform.h" |
| 5 |
|
| 6 |
#include "Common/MsgHandler.h" |
| 7 |
#include "Core/Config/MainSettings.h" |
| 8 |
#include "Core/Core.h" |
| 9 |
#include "Core/State.h" |
| 10 |
#include "Core/System.h" |
| 11 |
#include "VideoCommon/EFBInterface.h" |
| 12 |
#include "VideoCommon/Present.h" |
| 13 |
|
| 14 |
#include <AppKit/AppKit.h> |
| 15 |
#include <Carbon/Carbon.h> |
| 16 |
#include <Foundation/Foundation.h> |
| 17 |
#include <array> |
| 18 |
#include <chrono> |
| 19 |
#include <climits> |
| 20 |
#include <cstdio> |
| 21 |
#include <cstring> |
| 22 |
#include <thread> |
| 23 |
|
| 24 |
@interface Application : NSApplication |
| 25 |
@property Platform* platform; |
| 26 |
- (void)shutdown; |
| 27 |
- (void)togglePause; |
| 28 |
- (void)saveScreenShot; |
| 29 |
- (void)loadLastSaved; |
| 30 |
- (void)undoLoadState; |
| 31 |
- (void)undoSaveState; |
| 32 |
- (void)loadState:(id)sender; |
| 33 |
- (void)saveState:(id)sender; |
| 34 |
@end |
| 35 |
|
| 36 |
@implementation Application |
| 37 |
- (void)shutdown; |
| 38 |
{ |
| 39 |
[self platform]->RequestShutdown(); |
| 40 |
[self stop:nil]; |
| 41 |
} |
| 42 |
|
| 43 |
- (void)togglePause |
| 44 |
{ |
| 45 |
auto& system = Core::System::GetInstance(); |
| 46 |
if (Core::GetState(system) == Core::State::Running) |
| 47 |
Core::SetState(system, Core::State::Paused); |
| 48 |
else |
| 49 |
Core::SetState(system, Core::State::Running); |
| 50 |
} |
| 51 |
|
| 52 |
- (void)saveScreenShot |
| 53 |
{ |
| 54 |
Core::SaveScreenShot(); |
| 55 |
} |
| 56 |
|
| 57 |
- (void)loadLastSaved |
| 58 |
{ |
| 59 |
State::LoadLastSaved(Core::System::GetInstance()); |
| 60 |
} |
| 61 |
|
| 62 |
- (void)undoLoadState |
| 63 |
{ |
| 64 |
State::UndoLoadState(Core::System::GetInstance()); |
| 65 |
} |
| 66 |
|
| 67 |
- (void)undoSaveState |
| 68 |
{ |
| 69 |
State::UndoSaveState(Core::System::GetInstance()); |
| 70 |
} |
| 71 |
|
| 72 |
- (void)loadState:(id)sender |
| 73 |
{ |
| 74 |
State::Load(Core::System::GetInstance(), [sender tag]); |
| 75 |
} |
| 76 |
|
| 77 |
- (void)saveState:(id)sender |
| 78 |
{ |
| 79 |
State::Save(Core::System::GetInstance(), [sender tag]); |
| 80 |
} |
| 81 |
@end |
| 82 |
|
| 83 |
@interface AppDelegate : NSObject <NSApplicationDelegate> |
| 84 |
|
| 85 |
@property(readonly) Platform* platform; |
| 86 |
|
| 87 |
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender; |
| 88 |
- (id)initWithPlatform:(Platform*)platform; |
| 89 |
@end |
| 90 |
|
| 91 |
@implementation AppDelegate |
| 92 |
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender |
| 93 |
{ |
| 94 |
return YES; |
| 95 |
} |
| 96 |
|
| 97 |
- (id)initWithPlatform:(Platform*)platform |
| 98 |
{ |
| 99 |
self = [super init]; |
| 100 |
if (self) |
| 101 |
{ |
| 102 |
_platform = platform; |
| 103 |
} |
| 104 |
return self; |
| 105 |
} |
| 106 |
@end |
| 107 |
|
| 108 |
@interface WindowDelegate : NSObject <NSWindowDelegate> |
| 109 |
|
| 110 |
- (void)windowDidResize:(NSNotification*)notification; |
| 111 |
@end |
| 112 |
|
| 113 |
@implementation WindowDelegate |
| 114 |
|
| 115 |
- (void)windowDidResize:(NSNotification*)notification |
| 116 |
{ |
| 117 |
if (g_presenter) |
| 118 |
g_presenter->ResizeSurface(); |
| 119 |
} |
| 120 |
@end |
| 121 |
|
| 122 |
namespace |
| 123 |
{ |
| 124 |
class PlatformMacOS : public Platform |
| 125 |
{ |
| 126 |
public: |
| 127 |
~PlatformMacOS() override; |
| 128 |
|
| 129 |
bool Init() override; |
| 130 |
void SetTitle(const std::string& title) override; |
| 131 |
void MainLoop() override; |
| 132 |
|
| 133 |
WindowSystemInfo GetWindowSystemInfo() const override; |
| 134 |
|
| 135 |
private: |
| 136 |
void ProcessEvents(); |
| 137 |
void UpdateWindowPosition(); |
| 138 |
void HandleSaveStates(NSUInteger key, NSUInteger flags); |
| 139 |
void SetupMenu(); |
| 140 |
|
| 141 |
NSRect m_window_rect; |
| 142 |
NSWindow* m_window; |
| 143 |
NSMenu* menuBar; |
| 144 |
AppDelegate* m_app_delegate; |
| 145 |
WindowDelegate* m_window_delegate; |
| 146 |
|
| 147 |
int m_window_x = Config::Get(Config::MAIN_RENDER_WINDOW_XPOS); |
| 148 |
int m_window_y = Config::Get(Config::MAIN_RENDER_WINDOW_YPOS); |
| 149 |
unsigned int m_window_width = Config::Get(Config::MAIN_RENDER_WINDOW_WIDTH); |
| 150 |
unsigned int m_window_height = Config::Get(Config::MAIN_RENDER_WINDOW_HEIGHT); |
| 151 |
bool m_window_fullscreen = Config::Get(Config::MAIN_FULLSCREEN); |
| 152 |
}; |
| 153 |
|
| 154 |
PlatformMacOS::~PlatformMacOS() |
| 155 |
{ |
| 156 |
[m_window close]; |
| 157 |
} |
| 158 |
|
| 159 |
bool PlatformMacOS::Init() |
| 160 |
{ |
| 161 |
[Application sharedApplication]; |
| 162 |
|
| 163 |
m_app_delegate = [[AppDelegate alloc] initWithPlatform:this]; |
| 164 |
[NSApp setDelegate:m_app_delegate]; |
| 165 |
|
| 166 |
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; |
| 167 |
[NSApp setPlatform:this]; |
| 168 |
[Application.sharedApplication finishLaunching]; |
| 169 |
|
| 170 |
unsigned long styleMask = |
| 171 |
NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable; |
| 172 |
|
| 173 |
m_window_rect = CGRectMake(m_window_x, m_window_y, m_window_width, m_window_height); |
| 174 |
m_window = [NSWindow alloc]; |
| 175 |
m_window = [m_window initWithContentRect:m_window_rect |
| 176 |
styleMask:styleMask |
| 177 |
backing:NSBackingStoreBuffered |
| 178 |
defer:NO]; |
| 179 |
m_window_delegate = [[WindowDelegate alloc] init]; |
| 180 |
[m_window setDelegate:m_window_delegate]; |
| 181 |
|
| 182 |
NSNotificationCenter* c = [NSNotificationCenter defaultCenter]; |
| 183 |
[c addObserver:NSApp |
| 184 |
selector:@selector(shutdown) |
| 185 |
name:NSWindowWillCloseNotification |
| 186 |
object:m_window]; |
| 187 |
|
| 188 |
if (m_window == nil) |
| 189 |
{ |
| 190 |
NSLog(@"Window is %@\n", m_window); |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never) |
| 195 |
[NSCursor hide]; |
| 196 |
|
| 197 |
if (Config::Get(Config::MAIN_FULLSCREEN)) |
| 198 |
{ |
| 199 |
m_window_fullscreen = true; |
| 200 |
[m_window toggleFullScreen:m_window]; |
| 201 |
} |
| 202 |
|
| 203 |
[m_window makeKeyAndOrderFront:NSApp]; |
| 204 |
[m_window makeMainWindow]; |
| 205 |
[NSApp activateIgnoringOtherApps:YES]; |
| 206 |
[m_window setTitle:@"Dolphin-emu-nogui"]; |
| 207 |
|
| 208 |
SetupMenu(); |
| 209 |
|
| 210 |
return true; |
| 211 |
} |
| 212 |
|
| 213 |
void PlatformMacOS::SetTitle(const std::string& title) |
| 214 |
{ |
| 215 |
@autoreleasepool |
| 216 |
{ |
| 217 |
NSWindow* window = m_window; |
| 218 |
NSString* str = [NSString stringWithUTF8String:title.c_str()]; |
| 219 |
dispatch_async(dispatch_get_main_queue(), ^{ |
| 220 |
[window setTitle:str]; |
| 221 |
}); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
void PlatformMacOS::MainLoop() |
| 226 |
{ |
| 227 |
while (IsRunning()) |
| 228 |
{ |
| 229 |
UpdateRunningFlag(); |
| 230 |
Core::HostDispatchJobs(Core::System::GetInstance()); |
| 231 |
ProcessEvents(); |
| 232 |
UpdateWindowPosition(); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
WindowSystemInfo PlatformMacOS::GetWindowSystemInfo() const |
| 237 |
{ |
| 238 |
@autoreleasepool |
| 239 |
{ |
| 240 |
WindowSystemInfo wsi; |
| 241 |
wsi.type = WindowSystemType::MacOS; |
| 242 |
wsi.render_window = (void*)CFBridgingRetain([m_window contentView]); |
| 243 |
wsi.render_surface = wsi.render_window; |
| 244 |
return wsi; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
void PlatformMacOS::ProcessEvents() |
| 249 |
{ |
| 250 |
@autoreleasepool |
| 251 |
{ |
| 252 |
NSDate* expiration = [NSDate dateWithTimeIntervalSinceNow:1]; |
| 253 |
NSEvent* event = [NSApp nextEventMatchingMask:NSEventMaskAny |
| 254 |
untilDate:expiration |
| 255 |
inMode:NSDefaultRunLoopMode |
| 256 |
dequeue:YES]; |
| 257 |
|
| 258 |
[NSApp sendEvent:event]; |
| 259 |
|
| 260 |
// Need to update if m_window becomes fullscreen |
| 261 |
m_window_fullscreen = [m_window styleMask] & NSWindowStyleMaskFullScreen; |
| 262 |
|
| 263 |
if ([m_window isMainWindow]) |
| 264 |
{ |
| 265 |
m_window_focus = true; |
| 266 |
if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never && |
| 267 |
Core::GetState(Core::System::GetInstance()) != Core::State::Paused) |
| 268 |
{ |
| 269 |
[NSCursor unhide]; |
| 270 |
} |
| 271 |
} |
| 272 |
else |
| 273 |
{ |
| 274 |
m_window_focus = false; |
| 275 |
if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never) |
| 276 |
[NSCursor hide]; |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
void PlatformMacOS::UpdateWindowPosition() |
| 282 |
{ |
| 283 |
if (m_window_fullscreen) |
| 284 |
return; |
| 285 |
|
| 286 |
NSRect win = [m_window frame]; |
| 287 |
m_window_x = win.origin.x; |
| 288 |
m_window_y = win.origin.y; |
| 289 |
m_window_width = win.size.width; |
| 290 |
m_window_height = win.size.height; |
| 291 |
} |
| 292 |
|
| 293 |
void PlatformMacOS::SetupMenu() |
| 294 |
{ |
| 295 |
@autoreleasepool |
| 296 |
{ |
| 297 |
menuBar = [NSMenu new]; |
| 298 |
|
| 299 |
NSMenu* appMenu = [NSMenu new]; |
| 300 |
NSMenu* stateMenu = [[NSMenu alloc] initWithTitle:@"States"]; |
| 301 |
NSMenu* loadStateMenu = [[NSMenu alloc] initWithTitle:@"Load"]; |
| 302 |
NSMenu* saveStateMenu = [[NSMenu alloc] initWithTitle:@"Save"]; |
| 303 |
NSMenu* miscMenu = [[NSMenu alloc] initWithTitle:@"Misc"]; |
| 304 |
|
| 305 |
NSMenuItem* appMenuItem = [NSMenuItem new]; |
| 306 |
NSMenuItem* miscMenuItem = [NSMenuItem new]; |
| 307 |
NSMenuItem* stateMenuItem = [NSMenuItem new]; |
| 308 |
NSMenuItem* loadStateItem = [[NSMenuItem alloc] initWithTitle:@"Load" |
| 309 |
action:nil |
| 310 |
keyEquivalent:@""]; |
| 311 |
NSMenuItem* saveStateItem = [[NSMenuItem alloc] initWithTitle:@"Save" |
| 312 |
action:nil |
| 313 |
keyEquivalent:@""]; |
| 314 |
[menuBar addItem:appMenuItem]; |
| 315 |
[menuBar addItem:stateMenuItem]; |
| 316 |
[menuBar addItem:miscMenuItem]; |
| 317 |
|
| 318 |
// Quit |
| 319 |
NSString* quitTitle = [@"Quit " stringByAppendingString:@"dolphin-emu-nogui"]; |
| 320 |
NSMenuItem* quitMenuItem = [[NSMenuItem alloc] initWithTitle:quitTitle |
| 321 |
action:@selector(shutdown) |
| 322 |
keyEquivalent:@"q"]; |
| 323 |
|
| 324 |
// Fullscreen |
| 325 |
NSString* fullScreenItemTitle = @"Toggle Fullscreen"; |
| 326 |
NSMenuItem* fullScreenItem = [[NSMenuItem alloc] initWithTitle:fullScreenItemTitle |
| 327 |
action:@selector(toggleFullScreen:) |
| 328 |
keyEquivalent:@"f"]; |
| 329 |
[fullScreenItem setKeyEquivalentModifierMask:NSEventModifierFlagFunction]; |
| 330 |
|
| 331 |
// Screenshot |
| 332 |
NSString* ScreenShotTitle = @"Take Screenshot"; |
| 333 |
unichar c = NSF9FunctionKey; |
| 334 |
NSString* f9 = [NSString stringWithCharacters:&c length:1]; |
| 335 |
NSMenuItem* ScreenShotItem = [[NSMenuItem alloc] initWithTitle:ScreenShotTitle |
| 336 |
action:@selector(saveScreenShot) |
| 337 |
keyEquivalent:f9]; |
| 338 |
[ScreenShotItem setKeyEquivalentModifierMask:NSEventModifierFlagFunction]; |
| 339 |
|
| 340 |
// Pause game |
| 341 |
NSString* pauseTitle = @"Toggle pause"; |
| 342 |
c = NSF10FunctionKey; |
| 343 |
NSString* f10 = [NSString stringWithCharacters:&c length:1]; |
| 344 |
NSMenuItem* pauseItem = [[NSMenuItem alloc] initWithTitle:pauseTitle |
| 345 |
action:@selector(togglePause) |
| 346 |
keyEquivalent:f10]; |
| 347 |
[pauseItem setKeyEquivalentModifierMask:NSEventModifierFlagFunction]; |
| 348 |
|
| 349 |
// Load last save |
| 350 |
NSString* loadLastTitle = @"Load Last Saved"; |
| 351 |
c = NSF11FunctionKey; |
| 352 |
NSString* f11 = [NSString stringWithCharacters:&c length:1]; |
| 353 |
NSMenuItem* loadLastItem = [[NSMenuItem alloc] initWithTitle:loadLastTitle |
| 354 |
action:@selector(loadLastSaved) |
| 355 |
keyEquivalent:f11]; |
| 356 |
[loadLastItem setKeyEquivalentModifierMask:NSEventModifierFlagFunction]; |
| 357 |
|
| 358 |
// Undo Load State |
| 359 |
NSString* undoLoadTitle = @"Undo Load"; |
| 360 |
c = NSF12FunctionKey; |
| 361 |
NSString* f12 = [NSString stringWithCharacters:&c length:1]; |
| 362 |
NSMenuItem* undoLoadItem = [[NSMenuItem alloc] initWithTitle:undoLoadTitle |
| 363 |
action:@selector(undoLoadState) |
| 364 |
keyEquivalent:f12]; |
| 365 |
[undoLoadItem setKeyEquivalentModifierMask:NSEventModifierFlagShift]; |
| 366 |
|
| 367 |
// Undo Save State |
| 368 |
NSString* undoSaveTitle = @"Undo Save"; |
| 369 |
NSMenuItem* undoSaveItem = [[NSMenuItem alloc] initWithTitle:undoSaveTitle |
| 370 |
action:@selector(undoSaveState) |
| 371 |
keyEquivalent:f12]; |
| 372 |
[undoSaveItem setKeyEquivalentModifierMask:NSEventModifierFlagFunction]; |
| 373 |
|
| 374 |
// Load and Save States |
| 375 |
for (unichar i = NSF1FunctionKey; i <= NSF8FunctionKey; i++) |
| 376 |
{ |
| 377 |
NSInteger stateNum = i - NSF1FunctionKey + 1; |
| 378 |
NSString* lstateTitle = [NSString stringWithFormat:@"Load State %ld", (long)stateNum]; |
| 379 |
c = i; |
| 380 |
NSString* t = [NSString stringWithCharacters:&c length:1]; |
| 381 |
NSMenuItem* lstateItem = [[NSMenuItem alloc] initWithTitle:lstateTitle |
| 382 |
action:@selector(loadState:) |
| 383 |
keyEquivalent:t]; |
| 384 |
[lstateItem setTag:stateNum]; |
| 385 |
[lstateItem setKeyEquivalentModifierMask:NSEventModifierFlagFunction]; |
| 386 |
[loadStateMenu addItem:lstateItem]; |
| 387 |
|
| 388 |
NSString* sstateTitle = [NSString stringWithFormat:@"Save State %ld", (long)stateNum]; |
| 389 |
c = i; |
| 390 |
NSMenuItem* sstateItem = [[NSMenuItem alloc] initWithTitle:sstateTitle |
| 391 |
action:@selector(saveState:) |
| 392 |
keyEquivalent:t]; |
| 393 |
[sstateItem setKeyEquivalentModifierMask:NSEventModifierFlagShift]; |
| 394 |
[sstateItem setTag:stateNum]; |
| 395 |
[saveStateMenu addItem:sstateItem]; |
| 396 |
} |
| 397 |
|
| 398 |
// App Main menu |
| 399 |
[appMenu addItem:quitMenuItem]; |
| 400 |
|
| 401 |
// State Menu |
| 402 |
[loadStateItem setSubmenu:loadStateMenu]; |
| 403 |
[saveStateItem setSubmenu:saveStateMenu]; |
| 404 |
|
| 405 |
[stateMenu addItem:loadLastItem]; |
| 406 |
[stateMenu addItem:undoLoadItem]; |
| 407 |
[stateMenu addItem:undoSaveItem]; |
| 408 |
[stateMenu addItem:loadStateItem]; |
| 409 |
[stateMenu addItem:saveStateItem]; |
| 410 |
|
| 411 |
// Misc Menu |
| 412 |
[miscMenu addItem:fullScreenItem]; |
| 413 |
[miscMenu addItem:ScreenShotItem]; |
| 414 |
[miscMenu addItem:pauseItem]; |
| 415 |
|
| 416 |
[appMenuItem setSubmenu:appMenu]; |
| 417 |
[stateMenuItem setSubmenu:stateMenu]; |
| 418 |
[miscMenuItem setSubmenu:miscMenu]; |
| 419 |
|
| 420 |
[NSApp setMainMenu:menuBar]; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
} // namespace |
| 425 |
|
| 426 |
std::unique_ptr<Platform> Platform::CreateMacOSPlatform() |
| 427 |
{ |
| 428 |
return std::make_unique<PlatformMacOS>(); |
| 429 |
} |