PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/Avc/SDLMain.m

http://github.com/mbebenita/Broadway
Objective C | 383 lines | 242 code | 79 blank | 62 comment | 24 complexity | 6682801bdcf031660e38c87210b889f7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /* SDLMain.m - main entry point for our Cocoa-ized SDL app
  2. Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
  3. Non-NIB-Code & other changes: Max Horn <max@quendi.de>
  4. Feel free to customize this file to suit your needs
  5. */
  6. #include <SDL/SDL.h>
  7. #include "SDLMain.h"
  8. #include <sys/param.h> /* for MAXPATHLEN */
  9. #include <unistd.h>
  10. /* For some reaon, Apple removed setAppleMenu from the headers in 10.4,
  11. but the method still is there and works. To avoid warnings, we declare
  12. it ourselves here. */
  13. @interface NSApplication(SDL_Missing_Methods)
  14. - (void)setAppleMenu:(NSMenu *)menu;
  15. @end
  16. /* Use this flag to determine whether we use SDLMain.nib or not */
  17. #define SDL_USE_NIB_FILE 0
  18. /* Use this flag to determine whether we use CPS (docking) or not */
  19. #define SDL_USE_CPS 1
  20. #ifdef SDL_USE_CPS
  21. /* Portions of CPS.h */
  22. typedef struct CPSProcessSerNum
  23. {
  24. UInt32 lo;
  25. UInt32 hi;
  26. } CPSProcessSerNum;
  27. extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn);
  28. extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
  29. extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn);
  30. #endif /* SDL_USE_CPS */
  31. static int gArgc;
  32. static char **gArgv;
  33. static BOOL gFinderLaunch;
  34. static BOOL gCalledAppMainline = FALSE;
  35. static NSString *getApplicationName(void)
  36. {
  37. const NSDictionary *dict;
  38. NSString *appName = 0;
  39. /* Determine the application name */
  40. dict = (const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
  41. if (dict)
  42. appName = [dict objectForKey: @"CFBundleName"];
  43. if (![appName length])
  44. appName = [[NSProcessInfo processInfo] processName];
  45. return appName;
  46. }
  47. #if SDL_USE_NIB_FILE
  48. /* A helper category for NSString */
  49. @interface NSString (ReplaceSubString)
  50. - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
  51. @end
  52. #endif
  53. @interface SDLApplication : NSApplication
  54. @end
  55. @implementation SDLApplication
  56. /* Invoked from the Quit menu item */
  57. - (void)terminate:(id)sender
  58. {
  59. /* Post a SDL_QUIT event */
  60. SDL_Event event;
  61. event.type = SDL_QUIT;
  62. SDL_PushEvent(&event);
  63. }
  64. @end
  65. /* The main class of the application, the application's delegate */
  66. @implementation SDLMain
  67. /* Set the working directory to the .app's parent directory */
  68. - (void) setupWorkingDirectory:(BOOL)shouldChdir
  69. {
  70. if (shouldChdir)
  71. {
  72. char parentdir[MAXPATHLEN];
  73. CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
  74. CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
  75. if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN)) {
  76. chdir(parentdir); /* chdir to the binary app's parent */
  77. }
  78. CFRelease(url);
  79. CFRelease(url2);
  80. }
  81. }
  82. #if SDL_USE_NIB_FILE
  83. /* Fix menu to contain the real app name instead of "SDL App" */
  84. - (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
  85. {
  86. NSRange aRange;
  87. NSEnumerator *enumerator;
  88. NSMenuItem *menuItem;
  89. aRange = [[aMenu title] rangeOfString:@"SDL App"];
  90. if (aRange.length != 0)
  91. [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
  92. enumerator = [[aMenu itemArray] objectEnumerator];
  93. while ((menuItem = [enumerator nextObject]))
  94. {
  95. aRange = [[menuItem title] rangeOfString:@"SDL App"];
  96. if (aRange.length != 0)
  97. [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
  98. if ([menuItem hasSubmenu])
  99. [self fixMenu:[menuItem submenu] withAppName:appName];
  100. }
  101. [ aMenu sizeToFit ];
  102. }
  103. #else
  104. static void setApplicationMenu(void)
  105. {
  106. /* warning: this code is very odd */
  107. NSMenu *appleMenu;
  108. NSMenuItem *menuItem;
  109. NSString *title;
  110. NSString *appName;
  111. appName = getApplicationName();
  112. appleMenu = [[NSMenu alloc] initWithTitle:@""];
  113. /* Add menu items */
  114. title = [@"About " stringByAppendingString:appName];
  115. [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
  116. [appleMenu addItem:[NSMenuItem separatorItem]];
  117. title = [@"Hide " stringByAppendingString:appName];
  118. [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
  119. menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
  120. [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
  121. [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
  122. [appleMenu addItem:[NSMenuItem separatorItem]];
  123. title = [@"Quit " stringByAppendingString:appName];
  124. [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
  125. /* Put menu into the menubar */
  126. menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
  127. [menuItem setSubmenu:appleMenu];
  128. [[NSApp mainMenu] addItem:menuItem];
  129. /* Tell the application object that this is now the application menu */
  130. [NSApp setAppleMenu:appleMenu];
  131. /* Finally give up our references to the objects */
  132. [appleMenu release];
  133. [menuItem release];
  134. }
  135. /* Create a window menu */
  136. static void setupWindowMenu(void)
  137. {
  138. NSMenu *windowMenu;
  139. NSMenuItem *windowMenuItem;
  140. NSMenuItem *menuItem;
  141. windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
  142. /* "Minimize" item */
  143. menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
  144. [windowMenu addItem:menuItem];
  145. [menuItem release];
  146. /* Put menu into the menubar */
  147. windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
  148. [windowMenuItem setSubmenu:windowMenu];
  149. [[NSApp mainMenu] addItem:windowMenuItem];
  150. /* Tell the application object that this is now the window menu */
  151. [NSApp setWindowsMenu:windowMenu];
  152. /* Finally give up our references to the objects */
  153. [windowMenu release];
  154. [windowMenuItem release];
  155. }
  156. /* Replacement for NSApplicationMain */
  157. static void CustomApplicationMain (int argc, char **argv)
  158. {
  159. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  160. SDLMain *sdlMain;
  161. /* Ensure the application object is initialised */
  162. [SDLApplication sharedApplication];
  163. #ifdef SDL_USE_CPS
  164. {
  165. CPSProcessSerNum PSN;
  166. /* Tell the dock about us */
  167. if (!CPSGetCurrentProcess(&PSN))
  168. if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
  169. if (!CPSSetFrontProcess(&PSN))
  170. [SDLApplication sharedApplication];
  171. }
  172. #endif /* SDL_USE_CPS */
  173. /* Set up the menubar */
  174. [NSApp setMainMenu:[[NSMenu alloc] init]];
  175. setApplicationMenu();
  176. setupWindowMenu();
  177. /* Create SDLMain and make it the app delegate */
  178. sdlMain = [[SDLMain alloc] init];
  179. [NSApp setDelegate:sdlMain];
  180. /* Start the main event loop */
  181. [NSApp run];
  182. [sdlMain release];
  183. [pool release];
  184. }
  185. #endif
  186. /*
  187. * Catch document open requests...this lets us notice files when the app
  188. * was launched by double-clicking a document, or when a document was
  189. * dragged/dropped on the app's icon. You need to have a
  190. * CFBundleDocumentsType section in your Info.plist to get this message,
  191. * apparently.
  192. *
  193. * Files are added to gArgv, so to the app, they'll look like command line
  194. * arguments. Previously, apps launched from the finder had nothing but
  195. * an argv[0].
  196. *
  197. * This message may be received multiple times to open several docs on launch.
  198. *
  199. * This message is ignored once the app's mainline has been called.
  200. */
  201. - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
  202. {
  203. const char *temparg;
  204. size_t arglen;
  205. char *arg;
  206. char **newargv;
  207. if (!gFinderLaunch) /* MacOS is passing command line args. */
  208. return FALSE;
  209. if (gCalledAppMainline) /* app has started, ignore this document. */
  210. return FALSE;
  211. temparg = [filename UTF8String];
  212. arglen = SDL_strlen(temparg) + 1;
  213. arg = (char *) SDL_malloc(arglen);
  214. if (arg == NULL)
  215. return FALSE;
  216. newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
  217. if (newargv == NULL)
  218. {
  219. SDL_free(arg);
  220. return FALSE;
  221. }
  222. gArgv = newargv;
  223. SDL_strlcpy(arg, temparg, arglen);
  224. gArgv[gArgc++] = arg;
  225. gArgv[gArgc] = NULL;
  226. return TRUE;
  227. }
  228. /* Called when the internal event loop has just started running */
  229. - (void) applicationDidFinishLaunching: (NSNotification *) note
  230. {
  231. int status;
  232. /* Set the working directory to the .app's parent directory */
  233. [self setupWorkingDirectory:gFinderLaunch];
  234. #if SDL_USE_NIB_FILE
  235. /* Set the main menu to contain the real app name instead of "SDL App" */
  236. [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
  237. #endif
  238. /* Hand off to main application code */
  239. gCalledAppMainline = TRUE;
  240. status = SDL_main (gArgc, gArgv);
  241. /* We're done, thank you for playing */
  242. exit(status);
  243. }
  244. @end
  245. @implementation NSString (ReplaceSubString)
  246. - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
  247. {
  248. unsigned int bufferSize;
  249. unsigned int selfLen = [self length];
  250. unsigned int aStringLen = [aString length];
  251. unichar *buffer;
  252. NSRange localRange;
  253. NSString *result;
  254. bufferSize = selfLen + aStringLen - aRange.length;
  255. buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar));
  256. /* Get first part into buffer */
  257. localRange.location = 0;
  258. localRange.length = aRange.location;
  259. [self getCharacters:buffer range:localRange];
  260. /* Get middle part into buffer */
  261. localRange.location = 0;
  262. localRange.length = aStringLen;
  263. [aString getCharacters:(buffer+aRange.location) range:localRange];
  264. /* Get last part into buffer */
  265. localRange.location = aRange.location + aRange.length;
  266. localRange.length = selfLen - localRange.location;
  267. [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
  268. /* Build output string */
  269. result = [NSString stringWithCharacters:buffer length:bufferSize];
  270. NSDeallocateMemoryPages(buffer, bufferSize);
  271. return result;
  272. }
  273. @end
  274. #ifdef main
  275. # undef main
  276. #endif
  277. /* Main entry point to executable - should *not* be SDL_main! */
  278. int main (int argc, char **argv)
  279. {
  280. /* Copy the arguments into a global variable */
  281. /* This is passed if we are launched by double-clicking */
  282. if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
  283. gArgv = (char **) SDL_malloc(sizeof (char *) * 2);
  284. gArgv[0] = argv[0];
  285. gArgv[1] = NULL;
  286. gArgc = 1;
  287. gFinderLaunch = YES;
  288. } else {
  289. int i;
  290. gArgc = argc;
  291. gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1));
  292. for (i = 0; i <= argc; i++)
  293. gArgv[i] = argv[i];
  294. gFinderLaunch = NO;
  295. }
  296. #if SDL_USE_NIB_FILE
  297. [SDLApplication poseAsClass:[NSApplication class]];
  298. NSApplicationMain (argc, argv);
  299. #else
  300. CustomApplicationMain (argc, argv);
  301. #endif
  302. return 0;
  303. }