comparison src/main/macosx/SDLMain.m @ 1113:39408f59a0f7

In MacOSX, when drag'n'dropping a document on an SDL app, or double-clicking a document associated with the app, the document(s) are passed to SDL_main() as if they were command line arguments. Otherwise, the command line is always empty and there is no way for the app to recover this information.
author Ryan C. Gordon <icculus@icculus.org>
date Thu, 11 Aug 2005 00:56:16 +0000
parents d390f9bd6b1c
children a6011e1394d9
comparison
equal deleted inserted replaced
1112:6ded3dd929f5 1113:39408f59a0f7
30 #endif /* SDL_USE_CPS */ 30 #endif /* SDL_USE_CPS */
31 31
32 static int gArgc; 32 static int gArgc;
33 static char **gArgv; 33 static char **gArgv;
34 static BOOL gFinderLaunch; 34 static BOOL gFinderLaunch;
35 static BOOL gCalledAppMainline = FALSE;
35 36
36 static NSString *getApplicationName(void) 37 static NSString *getApplicationName(void)
37 { 38 {
38 NSDictionary *dict; 39 NSDictionary *dict;
39 NSString *appName = 0; 40 NSString *appName = 0;
224 [pool release]; 225 [pool release];
225 } 226 }
226 227
227 #endif 228 #endif
228 229
230
231 /*
232 * Catch document open requests...this lets us notice files when the app
233 * was launched by double-clicking a document, or when a document was
234 * dragged/dropped on the app's icon. You need to have a
235 * CFBundleDocumentsType section in your Info.plist to get this message,
236 * apparently.
237 *
238 * Files are added to gArgv, so to the app, they'll look like command line
239 * arguments. Previously, apps launched from the finder had nothing but
240 * an argv[0].
241 *
242 * This message may be received multiple times to open several docs on launch.
243 *
244 * This message is ignored once the app's mainline has been called.
245 */
246 - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
247 {
248 if (gCalledAppMainline) /* app has started, ignore this document. */
249 return FALSE;
250
251 unsigned buflen = [filename lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
252 char *arg = (char *) malloc(buflen);
253 if (arg == NULL)
254 return FALSE;
255
256 char **newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
257 if (newargv == NULL)
258 {
259 free(arg);
260 return FALSE;
261 }
262 gArgv = newargv;
263
264 BOOL rc = [filename getCString:arg maxLength:buflen encoding:NSUTF8StringEncoding];
265 if (!rc)
266 free(arg);
267 else
268 {
269 gArgv[gArgc++] = arg;
270 gArgv[gArgc] = NULL;
271 }
272 return rc;
273 }
274
275
229 /* Called when the internal event loop has just started running */ 276 /* Called when the internal event loop has just started running */
230 - (void) applicationDidFinishLaunching: (NSNotification *) note 277 - (void) applicationDidFinishLaunching: (NSNotification *) note
231 { 278 {
232 int status; 279 int status;
233 280
238 /* Set the main menu to contain the real app name instead of "SDL App" */ 285 /* Set the main menu to contain the real app name instead of "SDL App" */
239 [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()]; 286 [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
240 #endif 287 #endif
241 288
242 /* Hand off to main application code */ 289 /* Hand off to main application code */
290 gCalledAppMainline = TRUE;
243 status = SDL_main (gArgc, gArgv); 291 status = SDL_main (gArgc, gArgv);
244 292
245 /* We're done, thank you for playing */ 293 /* We're done, thank you for playing */
246 exit(status); 294 exit(status);
247 } 295 }
298 int main (int argc, char **argv) 346 int main (int argc, char **argv)
299 { 347 {
300 /* Copy the arguments into a global variable */ 348 /* Copy the arguments into a global variable */
301 /* This is passed if we are launched by double-clicking */ 349 /* This is passed if we are launched by double-clicking */
302 if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) { 350 if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
351 gArgv = (char **) malloc(sizeof (char *) * 2);
352 gArgv[0] = argv[0];
353 gArgv[1] = NULL;
303 gArgc = 1; 354 gArgc = 1;
304 gFinderLaunch = YES; 355 gFinderLaunch = YES;
305 } else { 356 } else {
357 int i;
306 gArgc = argc; 358 gArgc = argc;
359 gArgv = (char **) malloc(sizeof (char *) * (argc+1));
360 for (i = 0; i <= argc; i++)
361 gArgv[i] = argv[i];
307 gFinderLaunch = NO; 362 gFinderLaunch = NO;
308 } 363 }
309 gArgv = argv;
310 364
311 #if SDL_USE_NIB_FILE 365 #if SDL_USE_NIB_FILE
312 [SDLApplication poseAsClass:[NSApplication class]]; 366 [SDLApplication poseAsClass:[NSApplication class]];
313 NSApplicationMain (argc, argv); 367 NSApplicationMain (argc, argv);
314 #else 368 #else