| 1 |
|
| 2 |
#include "allegro5/allegro.h" |
| 3 |
#include "allegro5/internal/aintern.h" |
| 4 |
#include "allegro5/internal/aintern_events.h" |
| 5 |
#include "allegro5/internal/aintern_keyboard.h" |
| 6 |
#include "allegro5/platform/aintosx.h" |
| 7 |
|
| 8 |
#ifdef __LP64__ |
| 9 |
/* FIXME: the following prototype and enum definition appear to needed in |
| 10 |
* 64 bit mode on 10.5 (Leopard). Apple's documentation indicates that |
| 11 |
* "deprecated features" are not available in 64 bit, but |
| 12 |
* UpdateSystemActivity is not deprecated and the documentation likewise |
| 13 |
* suggests that all that should be required is to #include CoreServices.h |
| 14 |
* or Power.h. However, this does not appear to work... for now, this |
| 15 |
* should work ok. |
| 16 |
* On 10.6 (Snow Leopard) these defines cause a problem, so they are |
| 17 |
* disabled. |
| 18 |
*/ |
| 19 |
extern OSErr UpdateSystemActivity(uint8_t activity); |
| 20 |
|
| 21 |
#if 0 |
| 22 |
enum { |
| 23 |
OverallAct = 0, /* Delays idle sleep by small amount */ |
| 24 |
UsrActivity = 1, /* Delays idle sleep and dimming by timeout time */ |
| 25 |
NetActivity = 2, /* Delays idle sleep and power cycling by small amount */ |
| 26 |
HDActivity = 3, /* Delays hard drive spindown and idle sleep by small amount */ |
| 27 |
IdleActivity = 4 /* Delays idle sleep by timeout time */ |
| 28 |
}; |
| 29 |
#endif |
| 30 |
|
| 31 |
#endif |
| 32 |
|
| 33 |
extern NSBundle *_al_osx_bundle; |
| 34 |
|
| 35 |
/* For compatibility with the unix code */ |
| 36 |
static int __crt0_argc; |
| 37 |
static char **__crt0_argv; |
| 38 |
static int (*user_main)(int, char **); |
| 39 |
|
| 40 |
static char *arg0, *arg1 = NULL; |
| 41 |
|
| 42 |
static BOOL in_bundle(void) |
| 43 |
{ |
| 44 |
/* This comes from the ADC tips & tricks section: how to detect if the app |
| 45 |
* lives inside a bundle |
| 46 |
*/ |
| 47 |
FSRef processRef; |
| 48 |
ProcessSerialNumber psn = { 0, kCurrentProcess }; |
| 49 |
FSCatalogInfo processInfo; |
| 50 |
GetProcessBundleLocation(&psn, &processRef); |
| 51 |
FSGetCatalogInfo(&processRef, kFSCatInfoNodeFlags, &processInfo, NULL, NULL, NULL); |
| 52 |
if (processInfo.nodeFlags & kFSNodeIsDirectoryMask) |
| 53 |
return YES; |
| 54 |
else |
| 55 |
return NO; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
@interface AllegroAppDelegate : NSObject <NSApplicationDelegate> |
| 60 |
{ |
| 61 |
NSTimer* activity; |
| 62 |
} |
| 63 |
- (void) dealloc; |
| 64 |
- (BOOL)application: (NSApplication *)theApplication openFile: (NSString *)filename; |
| 65 |
- (void)applicationDidFinishLaunching: (NSNotification *)aNotification; |
| 66 |
- (void)applicationDidChangeScreenParameters: (NSNotification *)aNotification; |
| 67 |
+ (void)app_main: (id)arg; |
| 68 |
- (NSApplicationTerminateReply) applicationShouldTerminate: (id)sender; |
| 69 |
- (void) updateSystemActivity:(NSTimer*) timer; |
| 70 |
- (void) setInhibitScreenSaver: (NSNumber*) inhibit; |
| 71 |
@end |
| 72 |
|
| 73 |
@interface AllegroWindowDelegate : NSObject |
| 74 |
- (BOOL)windowShouldClose: (id)sender; |
| 75 |
- (void)windowDidDeminiaturize: (NSNotification *)aNotification; |
| 76 |
@end |
| 77 |
|
| 78 |
@implementation AllegroAppDelegate |
| 79 |
|
| 80 |
/* setInhibitScreenSaver: |
| 81 |
* If inhibit is YES, set up an infrequent timer to call |
| 82 |
* updateSystemActivity: to prevent the screen saver from activating |
| 83 |
* Must be called from the main thread (osx_inhibit_screensaver ensures |
| 84 |
* this) |
| 85 |
* Has no effect if inhibit is YES and the timer is already active |
| 86 |
* or if inhibit is NO and the timer is not active |
| 87 |
*/ |
| 88 |
-(void) setInhibitScreenSaver: (NSNumber *) inhibit |
| 89 |
{ |
| 90 |
if ([inhibit boolValue] == YES) { |
| 91 |
if (activity == nil) { |
| 92 |
// Schedule every 30 seconds |
| 93 |
activity = [NSTimer scheduledTimerWithTimeInterval:30.0 |
| 94 |
target:self |
| 95 |
selector:@selector(updateSystemActivity:) |
| 96 |
userInfo:nil |
| 97 |
repeats:YES]; |
| 98 |
[activity retain]; |
| 99 |
} |
| 100 |
// else already active |
| 101 |
} |
| 102 |
else { |
| 103 |
// OK to send message to nil if timer wasn't set. |
| 104 |
[activity invalidate]; |
| 105 |
[activity release]; |
| 106 |
activity = nil; |
| 107 |
} |
| 108 |
} |
| 109 |
/* updateSystemActivity: |
| 110 |
* called by a timer to inform the system that there is still activity and |
| 111 |
* therefore do not dim the screen/start the screensaver |
| 112 |
*/ |
| 113 |
-(void) updateSystemActivity: (NSTimer*) timer |
| 114 |
{ |
| 115 |
(void)timer; |
| 116 |
UpdateSystemActivity(UsrActivity); |
| 117 |
} |
| 118 |
|
| 119 |
-(void) dealloc |
| 120 |
{ |
| 121 |
[activity invalidate]; |
| 122 |
[activity release]; |
| 123 |
[super dealloc]; |
| 124 |
} |
| 125 |
- (BOOL)application: (NSApplication *)theApplication openFile: (NSString *)filename |
| 126 |
{ |
| 127 |
NSData* data = [filename dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; |
| 128 |
(void)theApplication; |
| 129 |
if (data != nil) { |
| 130 |
unsigned int len = 1 + [data length]; |
| 131 |
arg1 = al_malloc(len); |
| 132 |
memset(arg1, 0, len); |
| 133 |
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 110000 |
| 134 |
[data getBytes: arg1 length:len]; |
| 135 |
#else |
| 136 |
[data getBytes: arg1]; |
| 137 |
#endif |
| 138 |
return YES; |
| 139 |
} |
| 140 |
else { |
| 141 |
return NO; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
/* applicationDidFinishLaunching: |
| 148 |
* Called when the app is ready to run. |
| 149 |
*/ |
| 150 |
- (void)applicationDidFinishLaunching: (NSNotification *)aNotification |
| 151 |
{ |
| 152 |
NSString* exename, *resdir; |
| 153 |
NSFileManager* fm; |
| 154 |
BOOL isDir; |
| 155 |
|
| 156 |
(void)aNotification; |
| 157 |
|
| 158 |
if (in_bundle() == YES) |
| 159 |
{ |
| 160 |
/* In a bundle, so chdir to the containing directory, |
| 161 |
* or to the 'magic' resource directory if it exists. |
| 162 |
* (see the readme.osx file for more info) |
| 163 |
*/ |
| 164 |
_al_osx_bundle = [NSBundle mainBundle]; |
| 165 |
exename = [[_al_osx_bundle executablePath] lastPathComponent]; |
| 166 |
resdir = [[_al_osx_bundle resourcePath] stringByAppendingPathComponent: exename]; |
| 167 |
fm = [NSFileManager defaultManager]; |
| 168 |
if ([fm fileExistsAtPath: resdir isDirectory: &isDir] && isDir) { |
| 169 |
/* Yes, it exists inside the bundle */ |
| 170 |
[fm changeCurrentDirectoryPath: resdir]; |
| 171 |
} |
| 172 |
else { |
| 173 |
/* No, change to the 'standard' OSX resource directory if it exists*/ |
| 174 |
if ([fm fileExistsAtPath: [_al_osx_bundle resourcePath] isDirectory: &isDir] && isDir) |
| 175 |
{ |
| 176 |
[fm changeCurrentDirectoryPath: [_al_osx_bundle resourcePath]]; |
| 177 |
} |
| 178 |
/* It doesn't exist - this is unusual for a bundle. Don't chdir */ |
| 179 |
} |
| 180 |
arg0 = strdup([[_al_osx_bundle bundlePath] fileSystemRepresentation]); |
| 181 |
if (arg1) { |
| 182 |
static char *args[2]; |
| 183 |
args[0] = arg0; |
| 184 |
args[1] = arg1; |
| 185 |
__crt0_argv = args; |
| 186 |
__crt0_argc = 2; |
| 187 |
} |
| 188 |
else { |
| 189 |
__crt0_argv = &arg0; |
| 190 |
__crt0_argc = 1; |
| 191 |
} |
| 192 |
} |
| 193 |
/* else: not in a bundle so don't chdir */ |
| 194 |
|
| 195 |
[NSThread detachNewThreadSelector: @selector(app_main:) |
| 196 |
toTarget: [AllegroAppDelegate class] |
| 197 |
withObject: nil]; |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
/* applicationDidChangeScreenParameters: |
| 204 |
* Invoked when the screen did change resolution/color depth. |
| 205 |
*/ |
| 206 |
- (void)applicationDidChangeScreenParameters: (NSNotification *)aNotification |
| 207 |
{ |
| 208 |
/* no-op */ |
| 209 |
(void)aNotification; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
/* Call the user main() */ |
| 215 |
static void call_user_main(void) |
| 216 |
{ |
| 217 |
exit(user_main(__crt0_argc, __crt0_argv)); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
/* app_main: |
| 223 |
* Thread dedicated to the user program; real main() gets called here. |
| 224 |
*/ |
| 225 |
+ (void)app_main: (id)arg |
| 226 |
{ |
| 227 |
(void)arg; |
| 228 |
call_user_main(); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
/* applicationShouldTerminate: |
| 234 |
* Called upon Command-Q or "Quit" menu item selection. |
| 235 |
* Post a message but do not quit directly |
| 236 |
*/ |
| 237 |
- (NSApplicationTerminateReply) applicationShouldTerminate: (id)sender |
| 238 |
{ |
| 239 |
(void)sender; |
| 240 |
_al_osx_post_quit(); |
| 241 |
return NSTerminateCancel; |
| 242 |
} |
| 243 |
|
| 244 |
/* end of AllegroAppDelegate implementation */ |
| 245 |
@end |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
/* This prevents warnings that 'NSApplication might not |
| 250 |
* respond to setAppleMenu' on OS X 10.4+ |
| 251 |
*/ |
| 252 |
@interface NSApplication(AllegroOSX) |
| 253 |
- (void)setAppleMenu:(NSMenu *)menu; |
| 254 |
@end |
| 255 |
|
| 256 |
/* Helper macro to add entries to the menu */ |
| 257 |
#define add_menu(name, sel, eq) \ |
| 258 |
[menu addItem: [[[NSMenuItem alloc] \ |
| 259 |
initWithTitle: name \ |
| 260 |
action: @selector(sel) \ |
| 261 |
keyEquivalent: eq] autorelease]] \ |
| 262 |
|
| 263 |
int _al_osx_run_main(int argc, char **argv, |
| 264 |
int (*real_main)(int, char **)) |
| 265 |
{ |
| 266 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 267 |
AllegroAppDelegate *app_delegate = [[AllegroAppDelegate alloc] init]; |
| 268 |
NSMenu *menu; |
| 269 |
NSMenuItem *temp_item; |
| 270 |
|
| 271 |
user_main = real_main; |
| 272 |
__crt0_argc = argc; |
| 273 |
__crt0_argv = argv; |
| 274 |
|
| 275 |
#ifdef OSX_BOOTSTRAP_DETECTION |
| 276 |
if (!_al_osx_bootstrap_ok()) /* not safe to use NSApplication */ |
| 277 |
call_user_main(); |
| 278 |
#endif |
| 279 |
|
| 280 |
[NSApplication sharedApplication]; |
| 281 |
|
| 282 |
/* Load the main menu nib if possible */ |
| 283 |
if ((!in_bundle()) || ([NSBundle loadNibNamed: @"MainMenu" |
| 284 |
owner: NSApp] == NO)) |
| 285 |
{ |
| 286 |
/* Didn't load the nib; create a default menu programmatically */ |
| 287 |
NSString* title = nil; |
| 288 |
NSDictionary* app_dictionary = [[NSBundle mainBundle] infoDictionary]; |
| 289 |
if (app_dictionary) |
| 290 |
{ |
| 291 |
title = [app_dictionary objectForKey: @"CFBundleName"]; |
| 292 |
} |
| 293 |
if (title == nil) |
| 294 |
{ |
| 295 |
title = [[NSProcessInfo processInfo] processName]; |
| 296 |
} |
| 297 |
NSMenu* main_menu = [[NSMenu alloc] initWithTitle: @""]; |
| 298 |
[NSApp setMainMenu: main_menu]; |
| 299 |
|
| 300 |
/* Add application ("Apple") menu */ |
| 301 |
menu = [[NSMenu alloc] initWithTitle: @"Apple menu"]; |
| 302 |
temp_item = [[NSMenuItem alloc] |
| 303 |
initWithTitle: @"" |
| 304 |
action: NULL |
| 305 |
keyEquivalent: @""]; |
| 306 |
[main_menu addItem: temp_item]; |
| 307 |
[main_menu setSubmenu: menu forItem: temp_item]; |
| 308 |
[temp_item release]; |
| 309 |
add_menu([@"Hide " stringByAppendingString: title], hide:, @"h"); |
| 310 |
add_menu(@"Hide Others", hideOtherApplications:, @""); |
| 311 |
add_menu(@"Show All", unhideAllApplications:, @""); |
| 312 |
[menu addItem: [NSMenuItem separatorItem]]; |
| 313 |
add_menu([@"Quit " stringByAppendingString: title], terminate:, @"q"); |
| 314 |
[NSApp setAppleMenu: menu]; |
| 315 |
[menu release]; |
| 316 |
|
| 317 |
/* Add "Window" menu */ |
| 318 |
menu = [[NSMenu alloc] |
| 319 |
initWithTitle: @"Window"]; |
| 320 |
temp_item = [[NSMenuItem alloc] |
| 321 |
initWithTitle: @"" |
| 322 |
action: NULL |
| 323 |
keyEquivalent: @""]; |
| 324 |
[main_menu addItem: temp_item]; |
| 325 |
[main_menu setSubmenu: menu forItem: temp_item]; |
| 326 |
[temp_item release]; |
| 327 |
/* Add menu entries */ |
| 328 |
add_menu(@"Minimize", performMiniaturize:, @"M"); |
| 329 |
add_menu(@"Bring All to Front", arrangeInFront:, @""); |
| 330 |
[NSApp setWindowsMenu:menu]; |
| 331 |
[menu release]; |
| 332 |
|
| 333 |
[main_menu release]; |
| 334 |
} |
| 335 |
// setDelegate: doesn't retain the delegate here (a Cocoa convention) |
| 336 |
// therefore we don't release it. |
| 337 |
[NSApp setDelegate: app_delegate]; |
| 338 |
[pool drain]; |
| 339 |
[NSApp run]; |
| 340 |
/* Can never get here */ |
| 341 |
[app_delegate release]; |
| 342 |
return 0; |
| 343 |
} |