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