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