SDL_cocoaevents.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_COCOA
  20. #include "SDL_timer.h"
  21. #include "SDL_cocoavideo.h"
  22. #include "../../events/SDL_events_c.h"
  23. #if !defined(UsrActivity) && defined(__LP64__) && !defined(__POWER__)
  24. /*
  25. * Workaround for a bug in the 10.5 SDK: By accident, OSService.h does
  26. * not include Power.h at all when compiling in 64bit mode. This has
  27. * been fixed in 10.6, but for 10.5, we manually define UsrActivity
  28. * to ensure compilation works.
  29. */
  30. #define UsrActivity 1
  31. #endif
  32. @interface SDLApplication : NSApplication
  33. - (void)terminate:(id)sender;
  34. @end
  35. @implementation SDLApplication
  36. // Override terminate to handle Quit and System Shutdown smoothly.
  37. - (void)terminate:(id)sender
  38. {
  39. SDL_SendQuit();
  40. }
  41. @end // SDLApplication
  42. /* setAppleMenu disappeared from the headers in 10.4 */
  43. @interface NSApplication(NSAppleMenu)
  44. - (void)setAppleMenu:(NSMenu *)menu;
  45. @end
  46. @interface SDLAppDelegate : NSObject {
  47. @public
  48. BOOL seenFirstActivate;
  49. }
  50. - (id)init;
  51. @end
  52. @implementation SDLAppDelegate : NSObject
  53. - (id)init
  54. {
  55. self = [super init];
  56. if (self) {
  57. seenFirstActivate = NO;
  58. [[NSNotificationCenter defaultCenter] addObserver:self
  59. selector:@selector(focusSomeWindow:)
  60. name:NSApplicationDidBecomeActiveNotification
  61. object:nil];
  62. }
  63. return self;
  64. }
  65. - (void)dealloc
  66. {
  67. [[NSNotificationCenter defaultCenter] removeObserver:self];
  68. [super dealloc];
  69. }
  70. - (void)focusSomeWindow:(NSNotification *)aNotification
  71. {
  72. /* HACK: Ignore the first call. The application gets a
  73. * applicationDidBecomeActive: a little bit after the first window is
  74. * created, and if we don't ignore it, a window that has been created with
  75. * SDL_WINDOW_MINIZED will ~immediately be restored.
  76. */
  77. if (!seenFirstActivate) {
  78. seenFirstActivate = YES;
  79. return;
  80. }
  81. SDL_VideoDevice *device = SDL_GetVideoDevice();
  82. if (device && device->windows)
  83. {
  84. SDL_Window *window = device->windows;
  85. int i;
  86. for (i = 0; i < device->num_displays; ++i)
  87. {
  88. SDL_Window *fullscreen_window = device->displays[i].fullscreen_window;
  89. if (fullscreen_window)
  90. {
  91. if (fullscreen_window->flags & SDL_WINDOW_MINIMIZED) {
  92. SDL_RestoreWindow(fullscreen_window);
  93. }
  94. return;
  95. }
  96. }
  97. if (window->flags & SDL_WINDOW_MINIMIZED) {
  98. SDL_RestoreWindow(window);
  99. } else {
  100. SDL_RaiseWindow(window);
  101. }
  102. }
  103. }
  104. - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
  105. {
  106. return (BOOL)SDL_SendDropFile([filename UTF8String]);
  107. }
  108. @end
  109. static SDLAppDelegate *appDelegate = nil;
  110. static NSString *
  111. GetApplicationName(void)
  112. {
  113. NSString *appName;
  114. /* Determine the application name */
  115. appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
  116. if (!appName)
  117. appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
  118. if (![appName length])
  119. appName = [[NSProcessInfo processInfo] processName];
  120. return appName;
  121. }
  122. static void
  123. CreateApplicationMenus(void)
  124. {
  125. NSString *appName;
  126. NSString *title;
  127. NSMenu *appleMenu;
  128. NSMenu *serviceMenu;
  129. NSMenu *windowMenu;
  130. NSMenu *viewMenu;
  131. NSMenuItem *menuItem;
  132. if (NSApp == nil) {
  133. return;
  134. }
  135. /* Create the main menu bar */
  136. [NSApp setMainMenu:[[NSMenu alloc] init]];
  137. /* Create the application menu */
  138. appName = GetApplicationName();
  139. appleMenu = [[NSMenu alloc] initWithTitle:@""];
  140. /* Add menu items */
  141. title = [@"About " stringByAppendingString:appName];
  142. [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
  143. [appleMenu addItem:[NSMenuItem separatorItem]];
  144. [appleMenu addItemWithTitle:@"Preferences…" action:nil keyEquivalent:@","];
  145. [appleMenu addItem:[NSMenuItem separatorItem]];
  146. serviceMenu = [[NSMenu alloc] initWithTitle:@""];
  147. menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
  148. [menuItem setSubmenu:serviceMenu];
  149. [NSApp setServicesMenu:serviceMenu];
  150. [serviceMenu release];
  151. [appleMenu addItem:[NSMenuItem separatorItem]];
  152. title = [@"Hide " stringByAppendingString:appName];
  153. [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
  154. menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
  155. [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
  156. [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
  157. [appleMenu addItem:[NSMenuItem separatorItem]];
  158. title = [@"Quit " stringByAppendingString:appName];
  159. [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
  160. /* Put menu into the menubar */
  161. menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
  162. [menuItem setSubmenu:appleMenu];
  163. [[NSApp mainMenu] addItem:menuItem];
  164. [menuItem release];
  165. /* Tell the application object that this is now the application menu */
  166. [NSApp setAppleMenu:appleMenu];
  167. [appleMenu release];
  168. /* Create the window menu */
  169. windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
  170. /* Add menu items */
  171. [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
  172. [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
  173. /* Put menu into the menubar */
  174. menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
  175. [menuItem setSubmenu:windowMenu];
  176. [[NSApp mainMenu] addItem:menuItem];
  177. [menuItem release];
  178. /* Tell the application object that this is now the window menu */
  179. [NSApp setWindowsMenu:windowMenu];
  180. [windowMenu release];
  181. /* Add the fullscreen view toggle menu option, if supported */
  182. if ([NSApp respondsToSelector:@selector(setPresentationOptions:)]) {
  183. /* Create the view menu */
  184. viewMenu = [[NSMenu alloc] initWithTitle:@"View"];
  185. /* Add menu items */
  186. menuItem = [viewMenu addItemWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
  187. [menuItem setKeyEquivalentModifierMask:NSControlKeyMask | NSCommandKeyMask];
  188. /* Put menu into the menubar */
  189. menuItem = [[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""];
  190. [menuItem setSubmenu:viewMenu];
  191. [[NSApp mainMenu] addItem:menuItem];
  192. [menuItem release];
  193. [viewMenu release];
  194. }
  195. }
  196. void
  197. Cocoa_RegisterApp(void)
  198. {
  199. /* This can get called more than once! Be careful what you initialize! */
  200. ProcessSerialNumber psn;
  201. NSAutoreleasePool *pool;
  202. if (!GetCurrentProcess(&psn)) {
  203. TransformProcessType(&psn, kProcessTransformToForegroundApplication);
  204. SetFrontProcess(&psn);
  205. }
  206. pool = [[NSAutoreleasePool alloc] init];
  207. if (NSApp == nil) {
  208. [SDLApplication sharedApplication];
  209. if ([NSApp mainMenu] == nil) {
  210. CreateApplicationMenus();
  211. }
  212. [NSApp finishLaunching];
  213. NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
  214. [NSNumber numberWithBool:NO], @"AppleMomentumScrollSupported",
  215. [NSNumber numberWithBool:NO], @"ApplePressAndHoldEnabled",
  216. nil];
  217. [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
  218. }
  219. if (NSApp && !appDelegate) {
  220. appDelegate = [[SDLAppDelegate alloc] init];
  221. /* If someone else has an app delegate, it means we can't turn a
  222. * termination into SDL_Quit, and we can't handle application:openFile:
  223. */
  224. if (![NSApp delegate]) {
  225. [NSApp setDelegate:appDelegate];
  226. } else {
  227. appDelegate->seenFirstActivate = YES;
  228. }
  229. }
  230. [pool release];
  231. }
  232. void
  233. Cocoa_PumpEvents(_THIS)
  234. {
  235. NSAutoreleasePool *pool;
  236. /* Update activity every 30 seconds to prevent screensaver */
  237. if (_this->suspend_screensaver) {
  238. SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
  239. Uint32 now = SDL_GetTicks();
  240. if (!data->screensaver_activity ||
  241. SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
  242. UpdateSystemActivity(UsrActivity);
  243. data->screensaver_activity = now;
  244. }
  245. }
  246. pool = [[NSAutoreleasePool alloc] init];
  247. for ( ; ; ) {
  248. NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
  249. if ( event == nil ) {
  250. break;
  251. }
  252. switch ([event type]) {
  253. case NSLeftMouseDown:
  254. case NSOtherMouseDown:
  255. case NSRightMouseDown:
  256. case NSLeftMouseUp:
  257. case NSOtherMouseUp:
  258. case NSRightMouseUp:
  259. case NSLeftMouseDragged:
  260. case NSRightMouseDragged:
  261. case NSOtherMouseDragged: /* usually middle mouse dragged */
  262. case NSMouseMoved:
  263. case NSScrollWheel:
  264. Cocoa_HandleMouseEvent(_this, event);
  265. break;
  266. case NSKeyDown:
  267. case NSKeyUp:
  268. case NSFlagsChanged:
  269. Cocoa_HandleKeyEvent(_this, event);
  270. break;
  271. default:
  272. break;
  273. }
  274. /* Pass through to NSApp to make sure everything stays in sync */
  275. [NSApp sendEvent:event];
  276. }
  277. [pool release];
  278. }
  279. #endif /* SDL_VIDEO_DRIVER_COCOA */
  280. /* vi: set ts=4 sw=4 expandtab: */