Skip to content

Commit

Permalink
macOS: Register file extensions for openFile (Drag ROM to App Icon)…
Browse files Browse the repository at this point in the history
…, update `setupWorkingDirectory`, rename `SDLMain` to `SDLApplicationDelegate` to avoid confusion with the function `SDL_main()` (#1287)

Register file extensions for openFile, and supports the following actions
    Drag ROM to Application icon to launch the app
    Drag ROM to the running app's dock icon
    Drag ROM to the running app's window (applicable to all SDL platform by SDL_DROPFILE)
Update setupWorkingDirectory
    macOS default cwd is a nonsense "/" for non-terminal application
Rename SDLMain to SDLApplicationDelegate to avoid confusion with the function SDL_main()
some code cleanup
  • Loading branch information
vkedwardli authored Nov 7, 2023
1 parent 9a7bf75 commit b92277a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 73 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1645,8 +1645,8 @@ if(NOT LIBRETRO)

target_sources(${PROJECT_NAME} PRIVATE
shell/apple/common/http_client.mm
shell/apple/emulator-osx/emulator-osx/SDLMain.h
shell/apple/emulator-osx/emulator-osx/SDLMain.mm
shell/apple/emulator-osx/emulator-osx/SDLApplicationDelegate.h
shell/apple/emulator-osx/emulator-osx/SDLApplicationDelegate.mm
shell/apple/emulator-osx/emulator-osx/osx-main.mm)
set(ASSETS shell/apple/emulator-osx/emulator-osx/Images.xcassets)
target_sources(${PROJECT_NAME} PRIVATE ${ASSETS})
Expand Down
4 changes: 4 additions & 0 deletions core/sdl/sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ void input_sdl_handle()
case SDL_JOYDEVICEREMOVED:
sdl_close_joystick((SDL_JoystickID)event.jdevice.which);
break;

case SDL_DROPFILE:
gui_start_game(event.drop.file);
break;
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions shell/apple/emulator-osx/MacOSXBundleInfo.plist.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>chd</string>
<string>gdi</string>
<string>cdi</string>
<string>cue</string>
<string>zip</string>
<string>7z</string>
</array>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
</dict>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#import <Cocoa/Cocoa.h>

@interface SDLMain : NSObject <NSApplicationDelegate>
@interface SDLApplicationDelegate : NSObject <NSApplicationDelegate>
@end

#endif /* _SDLMain_h_ */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
Feel free to customize this file to suit your needs
*/
#include <SDL.h>
#include "SDLMain.h"
#include "SDLApplicationDelegate.h"
#include "emulator.h"
#include <sys/param.h> /* for MAXPATHLEN */
#include <unistd.h>
#include "rend/gui.h"
Expand Down Expand Up @@ -39,11 +40,6 @@ - (void)setAppleMenu:(NSMenu *)menu;
}
#endif /* SDL_USE_CPS */

static int gArgc;
static char **gArgv;
static BOOL gFinderLaunch;
static BOOL gCalledAppMainline = FALSE;

static NSString *getApplicationName(void)
{
const NSDictionary *dict;
Expand Down Expand Up @@ -127,21 +123,14 @@ - (void)selectAllAction:(id)sender
@end

/* The main class of the application, the application's delegate */
@implementation SDLMain
@implementation SDLApplicationDelegate

/* Set the working directory to the .app's parent directory */
- (void) setupWorkingDirectory:(BOOL)shouldChdir
- (void) setupWorkingDirectory
{
if (shouldChdir)
if([[NSProcessInfo processInfo] environment][@"PWD"] == NULL && [[[NSFileManager defaultManager] currentDirectoryPath] isEqualToString:@"/"])
{
char parentdir[MAXPATHLEN];
CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN)) {
chdir(parentdir); /* chdir to the binary app's parent */
}
CFRelease(url);
CFRelease(url2);
chdir([[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] cStringUsingEncoding:NSUTF8StringEncoding]);
}
}

Expand Down Expand Up @@ -274,7 +263,7 @@ static void setupHelpMenu(void)
static void CustomApplicationMain (int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
SDLMain *sdlMain;
SDLApplicationDelegate *appDelegate;

/* Ensure the application object is initialised */
[NSApplication sharedApplication];
Expand All @@ -297,13 +286,13 @@ static void CustomApplicationMain (int argc, char **argv)
setupHelpMenu(); /* needed for help menu */

/* Create SDLMain and make it the app delegate */
sdlMain = [[SDLMain alloc] init];
[NSApp setDelegate:sdlMain];
appDelegate = [[SDLApplicationDelegate alloc] init];
[NSApp setDelegate:appDelegate];

/* Start the main event loop */
[NSApp run];

[sdlMain release];
[appDelegate release];
[pool release];
}

Expand Down Expand Up @@ -338,34 +327,10 @@ static bool dumpCallback(const char *dump_dir, const char *minidump_id, void *co
*/
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
const char *temparg;
size_t arglen;
char *arg;
char **newargv;

if (!gFinderLaunch) /* MacOS is passing command line args. */
return FALSE;

if (gCalledAppMainline) /* app has started, ignore this document. */
return FALSE;

temparg = [filename UTF8String];
arglen = SDL_strlen(temparg) + 1;
arg = (char *) SDL_malloc(arglen);
if (arg == NULL)
return FALSE;

newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
if (newargv == NULL)
{
SDL_free(arg);
return FALSE;
}
gArgv = newargv;

SDL_strlcpy(arg, temparg, arglen);
gArgv[gArgc++] = arg;
gArgv[gArgc] = NULL;
dispatch_async(dispatch_get_main_queue(), ^(){
gui_start_game([filename cStringUsingEncoding:NSUTF8StringEncoding]);
});

return TRUE;
}

Expand All @@ -381,11 +346,9 @@ - (void) applicationDidFinishLaunching: (NSNotification *) note
int status;

/* Set the working directory to the .app's parent directory */
[self setupWorkingDirectory:gFinderLaunch];
[self setupWorkingDirectory];

/* Hand off to main application code */
gCalledAppMainline = TRUE;
status = SDL_main (gArgc, gArgv);
status = SDL_main(NULL, NULL);

/* We're done, thank you for playing */
exit(status);
Expand Down Expand Up @@ -456,23 +419,6 @@ - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
/* Main entry point to executable - should *not* be SDL_main! */
int main (int argc, char **argv)
{
/* Copy the arguments into a global variable */
/* This is passed if we are launched by double-clicking */
if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
gArgv = (char **) SDL_malloc(sizeof (char *) * 2);
gArgv[0] = argv[0];
gArgv[1] = NULL;
gArgc = 1;
gFinderLaunch = YES;
} else {
int i;
gArgc = argc;
gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1));
for (i = 0; i <= argc; i++)
gArgv[i] = argv[i];
gFinderLaunch = NO;
}

if (getppid() != 1) {
/* Make LLDB ignore EXC_BAD_ACCESS for debugging */
task_set_exception_ports(mach_task_self(), EXC_MASK_BAD_ACCESS, MACH_PORT_NULL, EXCEPTION_DEFAULT, 0);
Expand Down

0 comments on commit b92277a

Please sign in to comment.