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