Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
209 lines (169 loc) · 6.35 KB

SDL_cocoaevents.m

File metadata and controls

209 lines (169 loc) · 6.35 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Dec 8, 2008
Dec 8, 2008
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
May 7, 2009
May 7, 2009
23
#include "SDL_timer.h"
24
25
#include "SDL_cocoavideo.h"
Sep 1, 2008
Sep 1, 2008
26
#include "../../events/SDL_events_c.h"
May 7, 2009
May 7, 2009
28
29
30
31
32
33
34
35
/* setAppleMenu disappeared from the headers in 10.4 */
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
@interface NSApplication(NSAppleMenu)
- (void)setAppleMenu:(NSMenu *)menu;
@end
#endif
Jul 25, 2006
Jul 25, 2006
36
37
@implementation NSApplication(SDL)
- (void)setRunning
38
39
40
41
42
{
_running = 1;
}
@end
Jul 25, 2006
Jul 25, 2006
43
44
45
46
47
48
49
50
51
52
53
54
@interface SDLAppDelegate : NSObject
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
@end
@implementation SDLAppDelegate : NSObject
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
SDL_SendQuit();
return NSTerminateCancel;
}
@end
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
static NSString *
GetApplicationName(void)
{
NSDictionary *dict;
NSString *appName = 0;
/* Determine the application name */
dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
if (dict)
appName = [dict objectForKey: @"CFBundleName"];
if (![appName length])
appName = [[NSProcessInfo processInfo] processName];
return appName;
}
static void
CreateApplicationMenus(void)
{
NSString *appName;
NSString *title;
NSMenu *appleMenu;
NSMenu *windowMenu;
NSMenuItem *menuItem;
/* Create the main menu bar */
[NSApp setMainMenu:[[NSMenu alloc] init]];
/* Create the application menu */
appName = GetApplicationName();
appleMenu = [[NSMenu alloc] initWithTitle:@""];
/* Add menu items */
title = [@"About " stringByAppendingString:appName];
[appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
[appleMenu addItem:[NSMenuItem separatorItem]];
title = [@"Hide " stringByAppendingString:appName];
Jul 30, 2006
Jul 30, 2006
95
[appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@/*"h"*/""];
Jul 30, 2006
Jul 30, 2006
97
menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@/*"h"*/""];
98
99
100
101
102
103
104
[menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
[appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
[appleMenu addItem:[NSMenuItem separatorItem]];
title = [@"Quit " stringByAppendingString:appName];
Jul 30, 2006
Jul 30, 2006
105
[appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@/*"q"*/""];
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* 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"];
/* "Minimize" item */
Jul 30, 2006
Jul 30, 2006
122
menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@/*"m"*/""];
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
[windowMenu addItem:menuItem];
[menuItem release];
/* Put menu into the menubar */
menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
[menuItem setSubmenu:windowMenu];
[[NSApp mainMenu] addItem:menuItem];
[menuItem release];
/* Tell the application object that this is now the window menu */
[NSApp setWindowsMenu:windowMenu];
[windowMenu release];
}
void
Cocoa_RegisterApp(void)
{
ProcessSerialNumber psn;
NSAutoreleasePool *pool;
if (!GetCurrentProcess(&psn)) {
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetFrontProcess(&psn);
}
pool = [[NSAutoreleasePool alloc] init];
if (NSApp == nil) {
Jul 25, 2006
Jul 25, 2006
150
[NSApplication sharedApplication];
151
152
153
154
155
156
if ([NSApp mainMenu] == nil) {
CreateApplicationMenus();
}
[NSApp finishLaunching];
}
Jul 25, 2006
Jul 25, 2006
157
158
159
if ([NSApp delegate] == nil) {
[NSApp setDelegate:[[SDLAppDelegate alloc] init]];
}
Jul 25, 2006
Jul 25, 2006
160
[NSApp setRunning];
161
162
163
164
165
166
167
168
[pool release];
}
void
Cocoa_PumpEvents(_THIS)
{
NSAutoreleasePool *pool;
Jan 12, 2009
Jan 12, 2009
169
/* Update activity every 30 seconds to prevent screensaver */
May 7, 2009
May 7, 2009
170
171
/* FIXME: This define isn't available with 64-bit Mac OS X? */
#ifdef UsrActivity
Jan 12, 2009
Jan 12, 2009
172
173
174
175
176
177
178
179
180
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;
}
}
May 7, 2009
May 7, 2009
181
#endif
Jan 12, 2009
Jan 12, 2009
182
183
pool = [[NSAutoreleasePool alloc] init];
Jul 23, 2006
Jul 23, 2006
184
while ([NSApp isRunning]) {
185
186
187
188
NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
if ( event == nil ) {
break;
}
Jul 30, 2006
Jul 30, 2006
189
190
191
192
193
switch ([event type]) {
case NSKeyDown:
case NSKeyUp:
case NSFlagsChanged:
Cocoa_HandleKeyEvent(_this, event);
Jul 30, 2006
Jul 30, 2006
194
195
/* Fall through to pass event to NSApp; er, nevermind... */
/* FIXME: Find a way to stop the beeping, using delegate */
Sep 19, 2009
Sep 19, 2009
196
197
198
199
/* Add to support system-wide keyboard shortcuts like CMD+Space */
if (([event modifierFlags] & NSCommandKeyMask) || [event type] == NSFlagsChanged)
[NSApp sendEvent: event];
Jul 30, 2006
Jul 30, 2006
200
break;
Jul 30, 2006
Jul 30, 2006
201
202
203
204
default:
[NSApp sendEvent:event];
break;
}
205
206
207
208
209
}
[pool release];
}
/* vi: set ts=4 sw=4 expandtab: */