| 1 |
#import "allegroAppDelegate.h" |
| 2 |
#import "EAGLView.h" |
| 3 |
|
| 4 |
#import <UIKit/UIKit.h> |
| 5 |
#include <pthread.h> |
| 6 |
|
| 7 |
#include "allegro5/allegro_opengl.h" |
| 8 |
#include "allegro5/allegro_iphone.h" |
| 9 |
#include "allegro5/internal/aintern_opengl.h" |
| 10 |
|
| 11 |
ALLEGRO_DEBUG_CHANNEL("iphone") |
| 12 |
|
| 13 |
void _al_iphone_run_user_main(void); |
| 14 |
|
| 15 |
static allegroAppDelegate *global_delegate; |
| 16 |
static UIApplication *app = NULL; |
| 17 |
static volatile bool waiting_for_program_halt = false; |
| 18 |
static bool disconnect_wait = false; |
| 19 |
static UIScreen *airplay_screen = NULL; |
| 20 |
static bool airplay_connected = false; |
| 21 |
|
| 22 |
ALLEGRO_MUTEX *_al_iphone_display_hotplug_mutex = NULL; |
| 23 |
|
| 24 |
/* Screen handling */ |
| 25 |
@interface iphone_screen : NSObject |
| 26 |
{ |
| 27 |
@public |
| 28 |
int adapter; |
| 29 |
UIScreen *screen; |
| 30 |
UIWindow *window; |
| 31 |
ViewController *vc; |
| 32 |
EAGLView *view; |
| 33 |
ALLEGRO_DISPLAY *display; |
| 34 |
} |
| 35 |
@end |
| 36 |
|
| 37 |
@implementation iphone_screen |
| 38 |
@end |
| 39 |
|
| 40 |
void _al_iphone_disconnect(ALLEGRO_DISPLAY *display) |
| 41 |
{ |
| 42 |
(void)display; |
| 43 |
|
| 44 |
disconnect_wait = false; |
| 45 |
} |
| 46 |
|
| 47 |
static NSMutableArray *iphone_screens; |
| 48 |
|
| 49 |
static int iphone_get_adapter(ALLEGRO_DISPLAY *display) |
| 50 |
{ |
| 51 |
ALLEGRO_DISPLAY_IPHONE *d = (ALLEGRO_DISPLAY_IPHONE *)display; |
| 52 |
return d->extra->adapter; |
| 53 |
} |
| 54 |
|
| 55 |
static iphone_screen *iphone_get_screen_by_adapter(int adapter) |
| 56 |
{ |
| 57 |
al_lock_mutex(_al_iphone_display_hotplug_mutex); |
| 58 |
|
| 59 |
int num = [iphone_screens count]; |
| 60 |
int i; |
| 61 |
iphone_screen *ret = NULL; |
| 62 |
|
| 63 |
for (i = 0; i < num; i++) { |
| 64 |
iphone_screen *scr = [iphone_screens objectAtIndex:i]; |
| 65 |
if (scr->adapter == adapter) { |
| 66 |
ret = scr; |
| 67 |
break; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
al_unlock_mutex(_al_iphone_display_hotplug_mutex); |
| 72 |
return ret; |
| 73 |
} |
| 74 |
|
| 75 |
static iphone_screen *iphone_get_screen(ALLEGRO_DISPLAY *display) |
| 76 |
{ |
| 77 |
int adapter = iphone_get_adapter(display); |
| 78 |
return iphone_get_screen_by_adapter(adapter); |
| 79 |
} |
| 80 |
|
| 81 |
bool _al_iphone_is_display_connected(ALLEGRO_DISPLAY *display) |
| 82 |
{ |
| 83 |
iphone_screen *scr = iphone_get_screen(display); |
| 84 |
return scr && (scr->display == display); |
| 85 |
} |
| 86 |
|
| 87 |
static void iphone_remove_screen(UIScreen *screen) |
| 88 |
{ |
| 89 |
al_lock_mutex(_al_iphone_display_hotplug_mutex); |
| 90 |
|
| 91 |
int num_screens = [iphone_screens count]; |
| 92 |
int i; |
| 93 |
|
| 94 |
for (i = 1; i < num_screens; i++) { |
| 95 |
iphone_screen *scr = iphone_get_screen_by_adapter(i); |
| 96 |
if (scr->screen == screen) { |
| 97 |
[iphone_screens removeObjectAtIndex:i]; |
| 98 |
[scr release]; |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
al_unlock_mutex(_al_iphone_display_hotplug_mutex); |
| 104 |
} |
| 105 |
|
| 106 |
void _al_iphone_destroy_screen(ALLEGRO_DISPLAY *display) |
| 107 |
{ |
| 108 |
ALLEGRO_DISPLAY_IPHONE *d = (ALLEGRO_DISPLAY_IPHONE *)display; |
| 109 |
|
| 110 |
if (d->extra->adapter == 0) { |
| 111 |
global_delegate->main_display = NULL; |
| 112 |
} |
| 113 |
|
| 114 |
al_free(d->extra); |
| 115 |
} |
| 116 |
|
| 117 |
// create iphone_screen for all currently attached screens |
| 118 |
static void iphone_create_screens(void) |
| 119 |
{ |
| 120 |
iphone_screens = [[NSMutableArray alloc] init]; |
| 121 |
|
| 122 |
_al_iphone_display_hotplug_mutex = al_create_mutex_recursive(); |
| 123 |
|
| 124 |
if ([UIScreen respondsToSelector:NSSelectorFromString(@"screens")]) { |
| 125 |
int num_screens; |
| 126 |
int i; |
| 127 |
|
| 128 |
num_screens = [[UIScreen screens] count];; |
| 129 |
for (i = 0; i < num_screens && i < 2; i++) { |
| 130 |
if (i == 1) { |
| 131 |
airplay_screen = [[UIScreen screens] objectAtIndex:i]; |
| 132 |
continue; |
| 133 |
} |
| 134 |
[global_delegate add_screen:[[UIScreen screens] objectAtIndex:i]]; |
| 135 |
} |
| 136 |
} |
| 137 |
else { |
| 138 |
[global_delegate add_screen:[UIScreen mainScreen]]; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/* Function: al_iphone_get_window |
| 143 |
*/ |
| 144 |
UIWindow *al_iphone_get_window(ALLEGRO_DISPLAY *display) |
| 145 |
{ |
| 146 |
al_lock_mutex(_al_iphone_display_hotplug_mutex); |
| 147 |
iphone_screen *scr = iphone_get_screen(display); |
| 148 |
if (scr == NULL) { |
| 149 |
al_unlock_mutex(_al_iphone_display_hotplug_mutex); |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
al_unlock_mutex(_al_iphone_display_hotplug_mutex); |
| 153 |
return scr->window; |
| 154 |
} |
| 155 |
|
| 156 |
/* Function: al_iphone_get_view |
| 157 |
*/ |
| 158 |
UIView *al_iphone_get_view(ALLEGRO_DISPLAY *display) |
| 159 |
{ |
| 160 |
iphone_screen *scr = iphone_get_screen(display); |
| 161 |
if (scr == NULL) { |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
return scr->vc.view; |
| 165 |
} |
| 166 |
|
| 167 |
static int iphone_orientation_to_allegro(UIDeviceOrientation orientation) |
| 168 |
{ |
| 169 |
switch (orientation) { |
| 170 |
case UIDeviceOrientationPortrait: |
| 171 |
return ALLEGRO_DISPLAY_ORIENTATION_0_DEGREES; |
| 172 |
|
| 173 |
case UIDeviceOrientationPortraitUpsideDown: |
| 174 |
return ALLEGRO_DISPLAY_ORIENTATION_180_DEGREES; |
| 175 |
|
| 176 |
case UIDeviceOrientationLandscapeRight: |
| 177 |
return ALLEGRO_DISPLAY_ORIENTATION_90_DEGREES; |
| 178 |
|
| 179 |
case UIDeviceOrientationLandscapeLeft: |
| 180 |
return ALLEGRO_DISPLAY_ORIENTATION_270_DEGREES; |
| 181 |
|
| 182 |
case UIDeviceOrientationFaceUp: |
| 183 |
return ALLEGRO_DISPLAY_ORIENTATION_FACE_UP; |
| 184 |
|
| 185 |
case UIDeviceOrientationFaceDown: |
| 186 |
return ALLEGRO_DISPLAY_ORIENTATION_FACE_DOWN; |
| 187 |
|
| 188 |
default: |
| 189 |
return ALLEGRO_DISPLAY_ORIENTATION_UNKNOWN; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
static void iphone_send_orientation_event(ALLEGRO_DISPLAY* display, int orientation) |
| 194 |
{ |
| 195 |
_al_event_source_lock(&display->es); |
| 196 |
if (_al_event_source_needs_to_generate_event(&display->es)) { |
| 197 |
ALLEGRO_EVENT event; |
| 198 |
event.display.type = ALLEGRO_EVENT_DISPLAY_ORIENTATION; |
| 199 |
event.display.timestamp = al_get_time(); |
| 200 |
event.display.source = display; |
| 201 |
event.display.orientation = orientation; |
| 202 |
_al_event_source_emit_event(&display->es, &event); |
| 203 |
} |
| 204 |
_al_event_source_unlock(&display->es); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
void _al_iphone_acknowledge_drawing_halt(ALLEGRO_DISPLAY *display) |
| 209 |
{ |
| 210 |
(void)display; |
| 211 |
waiting_for_program_halt = false; |
| 212 |
} |
| 213 |
|
| 214 |
/* Function: al_iphone_set_statusbar_orientation |
| 215 |
*/ |
| 216 |
void al_iphone_set_statusbar_orientation(int o) |
| 217 |
{ |
| 218 |
UIInterfaceOrientation orientation = UIInterfaceOrientationPortrait; |
| 219 |
|
| 220 |
if (o == ALLEGRO_IPHONE_STATUSBAR_ORIENTATION_PORTRAIT) |
| 221 |
orientation = UIInterfaceOrientationPortrait; |
| 222 |
else if (o == ALLEGRO_IPHONE_STATUSBAR_ORIENTATION_PORTRAIT_UPSIDE_DOWN) |
| 223 |
orientation = UIInterfaceOrientationPortraitUpsideDown; |
| 224 |
else if (o == ALLEGRO_IPHONE_STATUSBAR_ORIENTATION_LANDSCAPE_RIGHT) |
| 225 |
orientation = UIInterfaceOrientationLandscapeRight; |
| 226 |
else if (o == ALLEGRO_IPHONE_STATUSBAR_ORIENTATION_LANDSCAPE_LEFT) |
| 227 |
orientation = UIInterfaceOrientationLandscapeLeft; |
| 228 |
|
| 229 |
[app setStatusBarOrientation:orientation animated:NO]; |
| 230 |
} |
| 231 |
|
| 232 |
bool _al_iphone_add_view(ALLEGRO_DISPLAY *display) |
| 233 |
{ |
| 234 |
ALLEGRO_DISPLAY_IPHONE *d = (ALLEGRO_DISPLAY_IPHONE *)display; |
| 235 |
|
| 236 |
d->extra = al_calloc(1, sizeof(ALLEGRO_DISPLAY_IPHONE_EXTRA)); |
| 237 |
int adapter = al_get_new_display_adapter(); |
| 238 |
if (adapter < 0) |
| 239 |
adapter = 0; |
| 240 |
d->extra->adapter = adapter; |
| 241 |
|
| 242 |
/* This is the same as |
| 243 |
* [global_delegate.view add_view]; |
| 244 |
* except it will run in the main thread. |
| 245 |
*/ |
| 246 |
[global_delegate performSelectorOnMainThread: @selector(add_view:) |
| 247 |
withObject: [NSValue valueWithPointer:display] |
| 248 |
waitUntilDone: YES]; |
| 249 |
|
| 250 |
if (d->extra->failed) { |
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
/* There are two ways to get orientation information under ios - but they seem |
| 255 |
* to be mutually exclusive (just my experience, the documentation never says |
| 256 |
* they are). |
| 257 |
* One method has 6 orientations including face up and face down and is |
| 258 |
* independent of the screen orientations, just directly using the accelerometer |
| 259 |
* to determine how the device is positioned relative to gravity. The other |
| 260 |
* method has 4 orientations and simply tells how the view controller thinks |
| 261 |
* the user interface should be rotated. |
| 262 |
* |
| 263 |
* Supporting both at the same time is a) slightly confusing since we'd need |
| 264 |
* two sets of query functions and events and b) does not seem to work due to |
| 265 |
* the mutual exclusivity. |
| 266 |
* |
| 267 |
* Now normally using just the 4-orientation way would appear to be the best |
| 268 |
* choice as you can always use the accelerometer anyway to get the actual |
| 269 |
* 3d orientation and otherwise are likely more concerned with the orientation |
| 270 |
* things are being displayed at right now (instead of just seeing FACE_UP which |
| 271 |
* would not tell you). |
| 272 |
* |
| 273 |
* But to stay compatible with how things worked at first we still support the |
| 274 |
* 6-orientations way as well - but only if the display has a sole supported |
| 275 |
* orientation of ALLEGRO_DISPLAY_ORIENTATION_0_DEGREES. |
| 276 |
*/ |
| 277 |
ALLEGRO_EXTRA_DISPLAY_SETTINGS *options = &display->extra_settings; |
| 278 |
int supported = options->settings[ALLEGRO_SUPPORTED_ORIENTATIONS]; |
| 279 |
if (supported == ALLEGRO_DISPLAY_ORIENTATION_0_DEGREES) { |
| 280 |
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; |
| 281 |
} |
| 282 |
|
| 283 |
return true; |
| 284 |
} |
| 285 |
|
| 286 |
void _al_iphone_make_view_current(ALLEGRO_DISPLAY *display) |
| 287 |
{ |
| 288 |
iphone_screen *scr = iphone_get_screen(display); |
| 289 |
if (scr) |
| 290 |
[scr->view make_current]; |
| 291 |
} |
| 292 |
|
| 293 |
void _al_iphone_recreate_framebuffer(ALLEGRO_DISPLAY *display) |
| 294 |
{ |
| 295 |
iphone_screen *scr = iphone_get_screen(display); |
| 296 |
if (scr) { |
| 297 |
EAGLView *view = scr->view; |
| 298 |
[view destroyFramebuffer]; |
| 299 |
[view createFramebuffer]; |
| 300 |
display->w = view.backingWidth; |
| 301 |
display->h = view.backingHeight; |
| 302 |
_al_ogl_resize_backbuffer(display->ogl_extras->backbuffer, display->w, display->h); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
void _al_iphone_flip_view(ALLEGRO_DISPLAY *display) |
| 307 |
{ |
| 308 |
iphone_screen *scr = iphone_get_screen(display); |
| 309 |
if (scr) |
| 310 |
[scr->view flip]; |
| 311 |
} |
| 312 |
|
| 313 |
void _al_iphone_reset_framebuffer(ALLEGRO_DISPLAY *display) |
| 314 |
{ |
| 315 |
iphone_screen *scr = iphone_get_screen(display); |
| 316 |
if (scr) |
| 317 |
[scr->view reset_framebuffer]; |
| 318 |
} |
| 319 |
|
| 320 |
/* Use a frequency to start receiving events at the freuqency, 0 to shut off |
| 321 |
* the accelerometer (according to Apple, it drains a bit of battery while on). |
| 322 |
*/ |
| 323 |
void _al_iphone_accelerometer_control(int frequency) |
| 324 |
{ |
| 325 |
if (frequency) { |
| 326 |
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / frequency)]; |
| 327 |
[[UIAccelerometer sharedAccelerometer] setDelegate:global_delegate]; |
| 328 |
} |
| 329 |
else { |
| 330 |
[[UIAccelerometer sharedAccelerometer] setDelegate:nil]; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
int _al_iphone_get_num_video_adapters(void) |
| 335 |
{ |
| 336 |
if (![UIScreen respondsToSelector:NSSelectorFromString(@"screens")]) { |
| 337 |
return 1; |
| 338 |
} |
| 339 |
return [[UIScreen screens] count]; |
| 340 |
} |
| 341 |
|
| 342 |
void _al_iphone_get_screen_size(int adapter, int *w, int *h) |
| 343 |
{ |
| 344 |
iphone_screen *scr = iphone_get_screen_by_adapter(adapter); |
| 345 |
UIScreen *screen = scr->screen; |
| 346 |
|
| 347 |
if (NULL != screen) { |
| 348 |
|
| 349 |
CGRect bounds = [screen bounds]; |
| 350 |
CGFloat scale = 1.0f; |
| 351 |
|
| 352 |
if (adapter == 0 && [screen respondsToSelector:NSSelectorFromString(@"scale")]) { |
| 353 |
scale = [screen scale]; |
| 354 |
} |
| 355 |
|
| 356 |
*w = (int)(bounds.size.width * scale); |
| 357 |
*h = (int)(bounds.size.height * scale); |
| 358 |
} |
| 359 |
else { |
| 360 |
|
| 361 |
ASSERT("You should never see this message, unless Apple changed their policy and allows for removing screens from iDevices." && false); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
int _al_iphone_get_orientation(ALLEGRO_DISPLAY *display) |
| 366 |
{ |
| 367 |
if (display) { |
| 368 |
ALLEGRO_EXTRA_DISPLAY_SETTINGS *options = &display->extra_settings; |
| 369 |
int supported = options->settings[ALLEGRO_SUPPORTED_ORIENTATIONS]; |
| 370 |
if (supported != ALLEGRO_DISPLAY_ORIENTATION_0_DEGREES) { |
| 371 |
iphone_screen *scr = iphone_get_screen(display); |
| 372 |
UIInterfaceOrientation o = scr->vc.interfaceOrientation; |
| 373 |
UIDeviceOrientation od = (int)o; /* They are compatible. */ |
| 374 |
return iphone_orientation_to_allegro(od); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
UIDevice* device = [UIDevice currentDevice]; |
| 379 |
|
| 380 |
if (NULL != device) |
| 381 |
return iphone_orientation_to_allegro([device orientation]); |
| 382 |
else |
| 383 |
return ALLEGRO_DISPLAY_ORIENTATION_UNKNOWN; |
| 384 |
} |
| 385 |
|
| 386 |
@implementation allegroAppDelegate |
| 387 |
|
| 388 |
+ (void)run:(int)argc:(char **)argv { |
| 389 |
UIApplicationMain(argc, argv, nil, @"allegroAppDelegate"); |
| 390 |
} |
| 391 |
|
| 392 |
- (void)orientation_change:(NSNotification *)notification |
| 393 |
{ |
| 394 |
(void)notification; |
| 395 |
|
| 396 |
int orientation = _al_iphone_get_orientation(NULL); |
| 397 |
|
| 398 |
if (main_display == NULL) |
| 399 |
return; |
| 400 |
|
| 401 |
iphone_send_orientation_event(main_display, orientation); |
| 402 |
} |
| 403 |
|
| 404 |
- (void)applicationDidFinishLaunching:(UIApplication *)application { |
| 405 |
ALLEGRO_INFO("App launched.\n"); |
| 406 |
|
| 407 |
global_delegate = self; |
| 408 |
app = application; |
| 409 |
|
| 410 |
iphone_create_screens(); |
| 411 |
|
| 412 |
// Register for device orientation notifications |
| 413 |
// Note: The notifications won't be generated unless they are enabled, which |
| 414 |
// we do elsewhere. |
| 415 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientation_change:) |
| 416 |
name:UIDeviceOrientationDidChangeNotification object:nil]; |
| 417 |
|
| 418 |
// Register for screen connect/disconnect notifications |
| 419 |
[self setupScreenConnectionNotificationHandlers]; |
| 420 |
|
| 421 |
_al_iphone_run_user_main(); |
| 422 |
} |
| 423 |
|
| 424 |
/* This may never get called on iOS 4 */ |
| 425 |
- (void)applicationWillTerminate:(UIApplication *)application { |
| 426 |
(void)application; |
| 427 |
ALLEGRO_EVENT event; |
| 428 |
ALLEGRO_DISPLAY *d = main_display; |
| 429 |
ALLEGRO_SYSTEM_IPHONE *iphone = (void *)al_get_system_driver(); |
| 430 |
iphone->wants_shutdown = true; |
| 431 |
|
| 432 |
ALLEGRO_INFO("Terminating.\n"); |
| 433 |
|
| 434 |
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; |
| 435 |
|
| 436 |
_al_event_source_lock(&d->es); |
| 437 |
|
| 438 |
if (_al_event_source_needs_to_generate_event(&d->es)) { |
| 439 |
event.display.type = ALLEGRO_EVENT_DISPLAY_CLOSE; |
| 440 |
event.display.timestamp = al_get_time(); |
| 441 |
_al_event_source_emit_event(&d->es, &event); |
| 442 |
} |
| 443 |
_al_event_source_unlock(&d->es); |
| 444 |
|
| 445 |
/* When this method returns, the application terminates - so lets wait a bit |
| 446 |
* so the user app can shutdown properly, e.g. to save state as is usually |
| 447 |
* required by iphone apps. |
| 448 |
*/ |
| 449 |
_al_iphone_await_termination(); |
| 450 |
} |
| 451 |
|
| 452 |
- (void)applicationWillResignActive:(UIApplication *)application { |
| 453 |
ALLEGRO_DISPLAY *d = main_display; |
| 454 |
ALLEGRO_EVENT event; |
| 455 |
|
| 456 |
(void)application; |
| 457 |
|
| 458 |
ALLEGRO_INFO("Application becoming inactive.\n"); |
| 459 |
|
| 460 |
_al_event_source_lock(&d->es); |
| 461 |
if (_al_event_source_needs_to_generate_event(&d->es)) { |
| 462 |
event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_OUT; |
| 463 |
event.display.timestamp = al_current_time(); |
| 464 |
_al_event_source_emit_event(&d->es, &event); |
| 465 |
} |
| 466 |
_al_event_source_unlock(&d->es); |
| 467 |
} |
| 468 |
|
| 469 |
- (void)applicationDidBecomeActive:(UIApplication *)application { |
| 470 |
ALLEGRO_DISPLAY *d = main_display; |
| 471 |
ALLEGRO_EVENT event; |
| 472 |
|
| 473 |
(void)application; |
| 474 |
|
| 475 |
ALLEGRO_INFO("Application becoming active.\n"); |
| 476 |
|
| 477 |
if (!d) |
| 478 |
return; |
| 479 |
|
| 480 |
_al_event_source_lock(&d->es); |
| 481 |
if (_al_event_source_needs_to_generate_event(&d->es)) { |
| 482 |
event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_IN; |
| 483 |
event.display.timestamp = al_current_time(); |
| 484 |
_al_event_source_emit_event(&d->es, &event); |
| 485 |
} |
| 486 |
_al_event_source_unlock(&d->es); |
| 487 |
} |
| 488 |
|
| 489 |
- (void)applicationDidEnterBackground:(UIApplication *)application { |
| 490 |
ALLEGRO_DISPLAY *d = main_display; |
| 491 |
ALLEGRO_EVENT event; |
| 492 |
|
| 493 |
(void)application; |
| 494 |
|
| 495 |
ALLEGRO_INFO("Application entering background.\n"); |
| 496 |
|
| 497 |
waiting_for_program_halt = true; |
| 498 |
|
| 499 |
_al_event_source_lock(&d->es); |
| 500 |
if (_al_event_source_needs_to_generate_event(&d->es)) { |
| 501 |
event.display.type = ALLEGRO_EVENT_DISPLAY_HALT_DRAWING; |
| 502 |
event.display.timestamp = al_current_time(); |
| 503 |
_al_event_source_emit_event(&d->es, &event); |
| 504 |
} |
| 505 |
_al_event_source_unlock(&d->es); |
| 506 |
|
| 507 |
while (waiting_for_program_halt) { |
| 508 |
// do nothing, this should be quick |
| 509 |
al_rest(0.001); |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
- (void)applicationWillEnterForeground:(UIApplication *)application { |
| 514 |
ALLEGRO_DISPLAY *d = main_display; |
| 515 |
ALLEGRO_EVENT event; |
| 516 |
|
| 517 |
(void)application; |
| 518 |
|
| 519 |
ALLEGRO_INFO("Application coming back to foreground.\n"); |
| 520 |
|
| 521 |
_al_event_source_lock(&d->es); |
| 522 |
if (_al_event_source_needs_to_generate_event(&d->es)) { |
| 523 |
event.display.type = ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING; |
| 524 |
event.display.timestamp = al_current_time(); |
| 525 |
_al_event_source_emit_event(&d->es, &event); |
| 526 |
} |
| 527 |
_al_event_source_unlock(&d->es); |
| 528 |
} |
| 529 |
|
| 530 |
/* Note: This must be called from the main thread. Ask Apple why - but I tried |
| 531 |
* it and otherwise things simply don't work (the screen just stays black). |
| 532 |
*/ |
| 533 |
- (void)add_view:(NSValue *)value { |
| 534 |
ALLEGRO_DISPLAY *d = [value pointerValue]; |
| 535 |
ALLEGRO_DISPLAY_IPHONE *disp = (ALLEGRO_DISPLAY_IPHONE *)d; |
| 536 |
|
| 537 |
int adapter = iphone_get_adapter(d); |
| 538 |
if (adapter == 0) { |
| 539 |
main_display = d; |
| 540 |
} |
| 541 |
|
| 542 |
iphone_screen *scr = iphone_get_screen_by_adapter(adapter); |
| 543 |
|
| 544 |
scr->display = d; |
| 545 |
|
| 546 |
ViewController *vc = scr->vc; |
| 547 |
vc->display = d; |
| 548 |
EAGLView *view = (EAGLView *)vc.view; |
| 549 |
[view set_allegro_display:d]; |
| 550 |
|
| 551 |
[scr->window addSubview:view]; |
| 552 |
|
| 553 |
if (adapter == 0) { |
| 554 |
[view becomeFirstResponder]; |
| 555 |
[scr->window makeKeyAndVisible]; |
| 556 |
} |
| 557 |
else { |
| 558 |
scr->window.hidden = NO; |
| 559 |
} |
| 560 |
|
| 561 |
disp->extra->failed = false; |
| 562 |
disp->extra->vc = vc; |
| 563 |
disp->extra->window = scr->window; |
| 564 |
} |
| 565 |
|
| 566 |
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration |
| 567 |
{ |
| 568 |
(void)accelerometer; |
| 569 |
_al_iphone_generate_joystick_event(acceleration.x, acceleration.y, acceleration.z); |
| 570 |
} |
| 571 |
|
| 572 |
- (void)setupScreenConnectionNotificationHandlers |
| 573 |
{ |
| 574 |
/* Screen connect/disconnect notifications were added in iOS 3.2 */ |
| 575 |
NSString *reqSysVer = @"3.2"; |
| 576 |
NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; |
| 577 |
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending); |
| 578 |
|
| 579 |
if (osVersionSupported) { |
| 580 |
NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
| 581 |
|
| 582 |
[center addObserver:self selector:@selector(handleScreenConnectNotification:) |
| 583 |
name:UIScreenDidConnectNotification object:nil]; |
| 584 |
[center addObserver:self selector:@selector(handleScreenDisconnectNotification:) |
| 585 |
name:UIScreenDidDisconnectNotification object:nil]; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
- (void)handleScreenConnectNotification:(NSNotification*)aNotification |
| 590 |
{ |
| 591 |
airplay_screen = [aNotification object]; |
| 592 |
|
| 593 |
ALLEGRO_DISPLAY *display = main_display; |
| 594 |
|
| 595 |
if (!display) return; |
| 596 |
|
| 597 |
_al_event_source_lock(&display->es); |
| 598 |
if (_al_event_source_needs_to_generate_event(&display->es)) { |
| 599 |
ALLEGRO_EVENT event; |
| 600 |
event.display.type = ALLEGRO_EVENT_DISPLAY_CONNECTED; |
| 601 |
event.display.timestamp = al_get_time(); |
| 602 |
event.display.source = display; |
| 603 |
_al_event_source_emit_event(&display->es, &event); |
| 604 |
} |
| 605 |
_al_event_source_unlock(&display->es); |
| 606 |
} |
| 607 |
|
| 608 |
- (void)handleScreenDisconnectNotification:(NSNotification*)aNotification |
| 609 |
{ |
| 610 |
ALLEGRO_DISPLAY *display = main_display; |
| 611 |
ALLEGRO_DISPLAY_IPHONE *idisplay = (ALLEGRO_DISPLAY_IPHONE *)display; |
| 612 |
ALLEGRO_DISPLAY_IPHONE_EXTRA *extra; |
| 613 |
|
| 614 |
if (!display) return; |
| 615 |
|
| 616 |
extra = idisplay->extra; |
| 617 |
extra->disconnected = true; |
| 618 |
|
| 619 |
disconnect_wait = true; |
| 620 |
|
| 621 |
_al_event_source_lock(&display->es); |
| 622 |
if (_al_event_source_needs_to_generate_event(&display->es)) { |
| 623 |
ALLEGRO_EVENT event; |
| 624 |
event.display.type = ALLEGRO_EVENT_DISPLAY_DISCONNECTED; |
| 625 |
event.display.timestamp = al_get_time(); |
| 626 |
event.display.source = display; |
| 627 |
_al_event_source_emit_event(&display->es, &event); |
| 628 |
} |
| 629 |
_al_event_source_unlock(&display->es); |
| 630 |
|
| 631 |
if (airplay_connected) { |
| 632 |
// wait for user to destroy display |
| 633 |
while (disconnect_wait) |
| 634 |
al_rest(0.001); |
| 635 |
|
| 636 |
iphone_remove_screen([aNotification object]); |
| 637 |
airplay_connected = false; |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
- (void)dealloc { |
| 642 |
[super dealloc]; |
| 643 |
} |
| 644 |
|
| 645 |
- (void)add_screen:(UIScreen *)screen |
| 646 |
{ |
| 647 |
int i; |
| 648 |
|
| 649 |
if (screen == [UIScreen mainScreen]) { |
| 650 |
i = 0; |
| 651 |
} |
| 652 |
else { |
| 653 |
for (i = 0; i < (int)[[UIScreen screens] count]; i++) { |
| 654 |
if ([[UIScreen screens] objectAtIndex:i] == screen) |
| 655 |
break; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
if (i != 0) { |
| 660 |
if ([screen respondsToSelector:NSSelectorFromString(@"availableModes")]) { |
| 661 |
NSArray *a = [screen availableModes]; |
| 662 |
int j; |
| 663 |
UIScreenMode *largest = NULL; |
| 664 |
for (j = 0; j < (int)[a count]; j++) { |
| 665 |
UIScreenMode *m = [a objectAtIndex:j]; |
| 666 |
float w = m.size.width; |
| 667 |
float h = m.size.height; |
| 668 |
if ((largest == NULL) || (w+h > largest.size.width+largest.size.height)) { |
| 669 |
largest = m; |
| 670 |
} |
| 671 |
} |
| 672 |
if (largest) { |
| 673 |
screen.currentMode = largest; |
| 674 |
} |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
iphone_screen *scr = iphone_get_screen_by_adapter(i); |
| 679 |
bool add = scr == NULL; |
| 680 |
|
| 681 |
UIWindow *window = [[UIWindow alloc] initWithFrame:[screen bounds]]; |
| 682 |
if ([window respondsToSelector:NSSelectorFromString(@"screen")]) { |
| 683 |
window.screen = screen; |
| 684 |
} |
| 685 |
|
| 686 |
ViewController *vc = [[ViewController alloc] init]; |
| 687 |
vc->adapter = i; |
| 688 |
[vc create_view]; |
| 689 |
// Doesn't work on iOS < 4. |
| 690 |
NSString *reqSysVer = @"4.0"; |
| 691 |
NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; |
| 692 |
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending); |
| 693 |
if (osVersionSupported) { |
| 694 |
window.rootViewController = vc; |
| 695 |
} |
| 696 |
|
| 697 |
if (add) |
| 698 |
scr = [[iphone_screen alloc] init]; |
| 699 |
|
| 700 |
scr->adapter = i; |
| 701 |
scr->screen = screen; |
| 702 |
scr->window = window; |
| 703 |
scr->vc = vc; |
| 704 |
scr->view = (EAGLView *)vc.view; |
| 705 |
scr->display = NULL; |
| 706 |
|
| 707 |
if (add) { |
| 708 |
al_lock_mutex(_al_iphone_display_hotplug_mutex); |
| 709 |
[iphone_screens addObject:scr]; |
| 710 |
al_unlock_mutex(_al_iphone_display_hotplug_mutex); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
@end |
| 715 |
|
| 716 |
void _al_iphone_connect_airplay(void) |
| 717 |
{ |
| 718 |
[global_delegate performSelectorOnMainThread: @selector(add_screen:) withObject:airplay_screen waitUntilDone:TRUE]; |
| 719 |
airplay_connected = true; |
| 720 |
} |