| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* MacOS X system driver. |
| 12 |
* |
| 13 |
* By Angelo Mottola. |
| 14 |
* |
| 15 |
* See readme.txt for copyright information. |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
#include "allegro5/allegro.h" |
| 20 |
#include "allegro5/platform/aintosx.h" |
| 21 |
#include "allegro5/internal/aintern_osxclipboard.h" |
| 22 |
#include <sys/stat.h> |
| 23 |
|
| 24 |
#ifndef ALLEGRO_MACOSX |
| 25 |
#error something is wrong with the makefile |
| 26 |
#endif |
| 27 |
|
| 28 |
#import <CoreFoundation/CoreFoundation.h> |
| 29 |
#include <mach/mach_port.h> |
| 30 |
#include <mach-o/dyld.h> |
| 31 |
#include <servers/bootstrap.h> |
| 32 |
|
| 33 |
ALLEGRO_DEBUG_CHANNEL("MacOSX") |
| 34 |
|
| 35 |
/* These are used to warn the dock about the application */ |
| 36 |
struct CPSProcessSerNum |
| 37 |
{ |
| 38 |
UInt32 lo; |
| 39 |
UInt32 hi; |
| 40 |
}; |
| 41 |
extern OSErr CPSGetCurrentProcess(struct CPSProcessSerNum *psn); |
| 42 |
extern OSErr CPSEnableForegroundOperation(struct CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5); |
| 43 |
extern OSErr CPSSetFrontProcess(struct CPSProcessSerNum *psn); |
| 44 |
|
| 45 |
|
| 46 |
static ALLEGRO_SYSTEM* osx_sys_init(int flags); |
| 47 |
ALLEGRO_SYSTEM_INTERFACE *_al_system_osx_driver(void); |
| 48 |
static void osx_sys_exit(void); |
| 49 |
|
| 50 |
|
| 51 |
/* Global variables */ |
| 52 |
NSBundle *_al_osx_bundle = NULL; |
| 53 |
static _AL_VECTOR osx_display_modes; |
| 54 |
static ALLEGRO_SYSTEM osx_system; |
| 55 |
|
| 56 |
/* _al_osx_tell_dock: |
| 57 |
* Tell the dock about us; promote us from a console app to a graphical app |
| 58 |
* with dock icon and menu |
| 59 |
*/ |
| 60 |
void _al_osx_tell_dock(void) |
| 61 |
{ |
| 62 |
if (_al_osx_bundle != NULL) { |
| 63 |
/* If in a bundle, the dock will recognise us automatically */ |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
ProcessSerialNumber psn = { 0, kCurrentProcess }; |
| 68 |
TransformProcessType(&psn, kProcessTransformToForegroundApplication); |
| 69 |
[[NSApplication sharedApplication] performSelectorOnMainThread: @selector(activateIgnoringOtherApps:) |
| 70 |
withObject: [NSNumber numberWithBool:YES] |
| 71 |
waitUntilDone: YES]; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
/* _al_osx_bootstrap_ok: |
| 77 |
* Check if the current bootstrap context is privilege. If it's not, we can't |
| 78 |
* use NSApplication, and instead have to go directly to main. |
| 79 |
* Returns 1 if ok, 0 if not. |
| 80 |
*/ |
| 81 |
#ifdef OSX_BOOTSTRAP_DETECTION |
| 82 |
int _al_osx_bootstrap_ok(void) |
| 83 |
{ |
| 84 |
static int _ok = -1; |
| 85 |
mach_port_t bp; |
| 86 |
kern_return_t ret; |
| 87 |
CFMachPortRef cfport; |
| 88 |
|
| 89 |
/* If have tested once, just return that answer */ |
| 90 |
if (_ok >= 0) |
| 91 |
return _ok; |
| 92 |
cfport = CFMachPortCreate(NULL, NULL, NULL, NULL); |
| 93 |
task_get_bootstrap_port(mach_task_self(), &bp); |
| 94 |
ret = bootstrap_register(bp, "bootstrap-ok-test", CFMachPortGetPort(cfport)); |
| 95 |
CFRelease(cfport); |
| 96 |
_ok = (ret == KERN_SUCCESS) ? 1 : 0; |
| 97 |
return _ok; |
| 98 |
} |
| 99 |
#endif |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
/* osx_sys_init: |
| 104 |
* Initalizes the MacOS X system driver. |
| 105 |
*/ |
| 106 |
static ALLEGRO_SYSTEM* osx_sys_init(int flags) |
| 107 |
{ |
| 108 |
(void)flags; |
| 109 |
|
| 110 |
#ifdef OSX_BOOTSTRAP_DETECTION |
| 111 |
/* If we're in the 'dead bootstrap' environment, the Mac driver won't work. */ |
| 112 |
if (!_al_osx_bootstrap_ok()) { |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
#endif |
| 116 |
/* Initialise the vt and display list */ |
| 117 |
osx_system.vt = _al_system_osx_driver(); |
| 118 |
_al_vector_init(&osx_system.displays, sizeof(ALLEGRO_DISPLAY*)); |
| 119 |
|
| 120 |
// `_al_osx_tell_dock` is better done after creating a display. Support old behavior for compatability. |
| 121 |
bool do_osx_tell_dock = true; |
| 122 |
const char* osx_tell_dock_outside_bundle_value = |
| 123 |
al_get_config_value(al_get_system_config(), "compatibility", "osx_tell_dock_outside_bundle"); |
| 124 |
if (osx_tell_dock_outside_bundle_value && strcmp(osx_tell_dock_outside_bundle_value, "false") == 0) |
| 125 |
do_osx_tell_dock = false; |
| 126 |
|
| 127 |
if (do_osx_tell_dock) { |
| 128 |
/* If in a bundle, the dock will recognise us automatically */ |
| 129 |
_al_osx_tell_dock(); |
| 130 |
} |
| 131 |
|
| 132 |
/* Mark the beginning of time. */ |
| 133 |
_al_unix_init_time(); |
| 134 |
|
| 135 |
_al_vector_init(&osx_display_modes, sizeof(ALLEGRO_DISPLAY_MODE)); |
| 136 |
|
| 137 |
ALLEGRO_DEBUG("system driver initialised.\n"); |
| 138 |
return &osx_system; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
/* osx_sys_exit: |
| 144 |
* Shuts down the system driver. |
| 145 |
*/ |
| 146 |
static void osx_sys_exit(void) |
| 147 |
{ |
| 148 |
_al_vector_free(&osx_display_modes); |
| 149 |
ALLEGRO_DEBUG("system driver shutdown.\n"); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
/* |
| 155 |
* _al_osx_get_num_display_modes: |
| 156 |
* Gets the number of available display modes |
| 157 |
*/ |
| 158 |
static int _al_osx_get_num_display_modes(void) |
| 159 |
{ |
| 160 |
ALLEGRO_EXTRA_DISPLAY_SETTINGS *extras = _al_get_new_display_settings(); |
| 161 |
ALLEGRO_EXTRA_DISPLAY_SETTINGS temp; |
| 162 |
int refresh_rate = al_get_new_display_refresh_rate(); |
| 163 |
int adapter = al_get_new_display_adapter(); |
| 164 |
int depth = 0; |
| 165 |
CGDirectDisplayID display; |
| 166 |
CFArrayRef modes; |
| 167 |
CFIndex i; |
| 168 |
|
| 169 |
if (extras) |
| 170 |
depth = extras->settings[ALLEGRO_COLOR_SIZE]; |
| 171 |
memset(&temp, 0, sizeof(ALLEGRO_EXTRA_DISPLAY_SETTINGS)); |
| 172 |
|
| 173 |
display = CGMainDisplayID(); |
| 174 |
/* Get display ID for the requested display */ |
| 175 |
if (adapter > 0) { |
| 176 |
NSScreen *screen = [[NSScreen screens] objectAtIndex: adapter]; |
| 177 |
NSDictionary *dict = [screen deviceDescription]; |
| 178 |
NSNumber *display_id = [dict valueForKey: @"NSScreenNumber"]; |
| 179 |
|
| 180 |
/* FIXME (how?): in 64 bit-mode, this generates a warning, because |
| 181 |
* CGDirectDisplayID is apparently 32 bit whereas a pointer is 64 |
| 182 |
* bit. Considering that a CGDirectDisplayID is supposed to be a |
| 183 |
* pointer as well (according to the documentation available on-line) |
| 184 |
* it is not quite clear what the correct way to do this would be. |
| 185 |
*/ |
| 186 |
display = (CGDirectDisplayID) [display_id pointerValue]; |
| 187 |
} |
| 188 |
|
| 189 |
_al_vector_free(&osx_display_modes); |
| 190 |
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 |
| 191 |
/* Note: modes is owned by OSX and must not be released */ |
| 192 |
modes = CGDisplayAvailableModes(display); |
| 193 |
ALLEGRO_INFO("detected %d display modes.\n", (int)CFArrayGetCount(modes)); |
| 194 |
for (i = 0; i < CFArrayGetCount(modes); i++) { |
| 195 |
ALLEGRO_DISPLAY_MODE *mode; |
| 196 |
CFDictionaryRef dict = (CFDictionaryRef)CFArrayGetValueAtIndex(modes, i); |
| 197 |
CFNumberRef number; |
| 198 |
int value, samples; |
| 199 |
|
| 200 |
number = CFDictionaryGetValue(dict, kCGDisplayBitsPerPixel); |
| 201 |
CFNumberGetValue(number, kCFNumberIntType, &value); |
| 202 |
ALLEGRO_INFO("Mode %d has colour depth %d.\n", (int)i, value); |
| 203 |
if (depth && value != depth) { |
| 204 |
ALLEGRO_WARN("Skipping mode %d (requested colour depth %d).\n", (int)i, depth); |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
number = CFDictionaryGetValue(dict, kCGDisplayRefreshRate); |
| 209 |
CFNumberGetValue(number, kCFNumberIntType, &value); |
| 210 |
ALLEGRO_INFO("Mode %d has colour depth %d.\n", (int)i, value); |
| 211 |
if (refresh_rate && value != refresh_rate) { |
| 212 |
ALLEGRO_WARN("Skipping mode %d (requested refresh rate %d).\n", (int)i, refresh_rate); |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
mode = (ALLEGRO_DISPLAY_MODE *)_al_vector_alloc_back(&osx_display_modes); |
| 217 |
number = CFDictionaryGetValue(dict, kCGDisplayWidth); |
| 218 |
CFNumberGetValue(number, kCFNumberIntType, &mode->width); |
| 219 |
number = CFDictionaryGetValue(dict, kCGDisplayHeight); |
| 220 |
CFNumberGetValue(number, kCFNumberIntType, &mode->height); |
| 221 |
number = CFDictionaryGetValue(dict, kCGDisplayRefreshRate); |
| 222 |
CFNumberGetValue(number, kCFNumberIntType, &mode->refresh_rate); |
| 223 |
ALLEGRO_INFO("Mode %d is %dx%d@%dHz\n", (int)i, mode->width, mode->height, mode->refresh_rate); |
| 224 |
|
| 225 |
number = CFDictionaryGetValue(dict, kCGDisplayBitsPerPixel); |
| 226 |
CFNumberGetValue(number, kCFNumberIntType, &temp.settings[ALLEGRO_COLOR_SIZE]); |
| 227 |
number = CFDictionaryGetValue(dict, kCGDisplaySamplesPerPixel); |
| 228 |
CFNumberGetValue(number, kCFNumberIntType, &samples); |
| 229 |
number = CFDictionaryGetValue(dict, kCGDisplayBitsPerSample); |
| 230 |
CFNumberGetValue(number, kCFNumberIntType, &value); |
| 231 |
ALLEGRO_INFO("Mode %d has %d bits per pixel, %d samples per pixel and %d bits per sample\n", |
| 232 |
(int)i, temp.settings[ALLEGRO_COLOR_SIZE], samples, value); |
| 233 |
if (samples >= 3) { |
| 234 |
temp.settings[ALLEGRO_RED_SIZE] = value; |
| 235 |
temp.settings[ALLEGRO_GREEN_SIZE] = value; |
| 236 |
temp.settings[ALLEGRO_BLUE_SIZE] = value; |
| 237 |
if (samples == 4) |
| 238 |
temp.settings[ALLEGRO_ALPHA_SIZE] = value; |
| 239 |
} |
| 240 |
_al_fill_display_settings(&temp); |
| 241 |
mode->format = _al_deduce_color_format(&temp); |
| 242 |
} |
| 243 |
#else |
| 244 |
modes = CGDisplayCopyAllDisplayModes(display, NULL); |
| 245 |
ALLEGRO_INFO("detected %d display modes.\n", (int)CFArrayGetCount(modes)); |
| 246 |
for (i = 0; i < CFArrayGetCount(modes); i++) { |
| 247 |
ALLEGRO_DISPLAY_MODE *amode; |
| 248 |
CGDisplayModeRef mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(modes, i); |
| 249 |
CFStringRef pixel_encoding = CGDisplayModeCopyPixelEncoding(mode); |
| 250 |
|
| 251 |
int bpp = 0, mode_refresh_rate, samples = 0, value = 0; |
| 252 |
|
| 253 |
/* Determine pixel format. Whever thought this was a better idea than |
| 254 |
* having query functions for each of these properties should be |
| 255 |
* spoken to very harshly in a very severe voice. |
| 256 |
*/ |
| 257 |
|
| 258 |
if (CFStringCompare(pixel_encoding, CFSTR(kIO16BitFloatPixels), 1) == 0) { |
| 259 |
bpp = 64; |
| 260 |
samples = 3; |
| 261 |
value = 16; |
| 262 |
} |
| 263 |
|
| 264 |
if (CFStringCompare(pixel_encoding, CFSTR(kIO32BitFloatPixels), 1) == 0) { |
| 265 |
bpp = 128; |
| 266 |
samples = 3; |
| 267 |
value = 32; |
| 268 |
} |
| 269 |
|
| 270 |
if (CFStringCompare(pixel_encoding, CFSTR(kIO64BitDirectPixels), 1) == 0) { |
| 271 |
bpp = 64; |
| 272 |
samples = 3; |
| 273 |
value = 16; |
| 274 |
} |
| 275 |
|
| 276 |
if (CFStringCompare(pixel_encoding, CFSTR(kIO30BitDirectPixels), 1) == 0) { |
| 277 |
bpp = 32; |
| 278 |
samples = 3; |
| 279 |
value = 10; |
| 280 |
} |
| 281 |
|
| 282 |
if (CFStringCompare(pixel_encoding, CFSTR(IO32BitDirectPixels), 1) == 0) { |
| 283 |
bpp = 32; |
| 284 |
samples = 3; |
| 285 |
value = 8; |
| 286 |
} |
| 287 |
|
| 288 |
if (CFStringCompare(pixel_encoding, CFSTR(IO16BitDirectPixels), 1) == 0) { |
| 289 |
bpp = 16; |
| 290 |
samples = 3; |
| 291 |
value = 5; |
| 292 |
} |
| 293 |
|
| 294 |
if (CFStringCompare(pixel_encoding, CFSTR(IO8BitIndexedPixels), 1) == 0) { |
| 295 |
bpp = 8; |
| 296 |
samples = 1; |
| 297 |
value = 8; |
| 298 |
} |
| 299 |
CFRelease(pixel_encoding); |
| 300 |
|
| 301 |
/* Check if this mode is ok in terms of depth and refresh rate */ |
| 302 |
ALLEGRO_INFO("Mode %d has colour depth %d.\n", (int)i, bpp); |
| 303 |
if (depth && bpp != depth) { |
| 304 |
ALLEGRO_WARN("Skipping mode %d (requested colour depth %d).\n", (int)i, depth); |
| 305 |
continue; |
| 306 |
} |
| 307 |
|
| 308 |
mode_refresh_rate = CGDisplayModeGetRefreshRate(mode); |
| 309 |
ALLEGRO_INFO("Mode %d has a refresh rate of %d.\n", (int)i, mode_refresh_rate); |
| 310 |
if (refresh_rate && mode_refresh_rate != refresh_rate) { |
| 311 |
ALLEGRO_WARN("Skipping mode %d (requested refresh rate %d).\n", (int)i, refresh_rate); |
| 312 |
continue; |
| 313 |
} |
| 314 |
|
| 315 |
/* Yes, it's fine */ |
| 316 |
amode = (ALLEGRO_DISPLAY_MODE *)_al_vector_alloc_back(&osx_display_modes); |
| 317 |
amode->width = CGDisplayModeGetWidth(mode); |
| 318 |
amode->height = CGDisplayModeGetHeight(mode); |
| 319 |
amode->refresh_rate = mode_refresh_rate; |
| 320 |
ALLEGRO_INFO("Mode %d is %dx%d@%dHz\n", (int)i, amode->width, amode->height, amode->refresh_rate); |
| 321 |
|
| 322 |
temp.settings[ALLEGRO_COLOR_SIZE] = bpp; |
| 323 |
ALLEGRO_INFO("Mode %d has %d bits per pixel, %d samples per pixel and %d bits per sample\n", |
| 324 |
(int)i, temp.settings[ALLEGRO_COLOR_SIZE], samples, value); |
| 325 |
if (samples >= 3) { |
| 326 |
temp.settings[ALLEGRO_RED_SIZE] = value; |
| 327 |
temp.settings[ALLEGRO_GREEN_SIZE] = value; |
| 328 |
temp.settings[ALLEGRO_BLUE_SIZE] = value; |
| 329 |
if (samples == 4) |
| 330 |
temp.settings[ALLEGRO_ALPHA_SIZE] = value; |
| 331 |
} |
| 332 |
_al_fill_display_settings(&temp); |
| 333 |
amode->format = _al_deduce_color_format(&temp); |
| 334 |
} |
| 335 |
CFRelease(modes); |
| 336 |
#endif |
| 337 |
return _al_vector_size(&osx_display_modes); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
/* |
| 343 |
* _al_osx_get_num_display_modes: |
| 344 |
* Gets the number of available display modes |
| 345 |
*/ |
| 346 |
static ALLEGRO_DISPLAY_MODE *_al_osx_get_display_mode(int index, ALLEGRO_DISPLAY_MODE *mode) |
| 347 |
{ |
| 348 |
if ((unsigned)index >= _al_vector_size(&osx_display_modes)) |
| 349 |
return NULL; |
| 350 |
memcpy(mode, _al_vector_ref(&osx_display_modes, index), sizeof(ALLEGRO_DISPLAY_MODE)); |
| 351 |
return mode; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
/* osx_get_num_video_adapters: |
| 357 |
* Return the number of video adapters i.e displays |
| 358 |
*/ |
| 359 |
static int osx_get_num_video_adapters(void) |
| 360 |
{ |
| 361 |
NSArray *screen_list; |
| 362 |
int num = 0; |
| 363 |
|
| 364 |
screen_list = [NSScreen screens]; |
| 365 |
if (screen_list) |
| 366 |
num = [screen_list count]; |
| 367 |
|
| 368 |
ALLEGRO_INFO("Detected %d displays\n", num); |
| 369 |
return num; |
| 370 |
} |
| 371 |
|
| 372 |
int _al_osx_get_primary_screen_y(void) |
| 373 |
{ |
| 374 |
int count = osx_get_num_video_adapters(); |
| 375 |
if (count == 0) { |
| 376 |
return 0; |
| 377 |
} |
| 378 |
NSArray *screen_list = [NSScreen screens]; |
| 379 |
NSScreen *primary_screen = [screen_list objectAtIndex: 0]; |
| 380 |
NSRect primary_rc = [primary_screen frame]; |
| 381 |
return (int) (primary_rc.origin.y + primary_rc.size.height); |
| 382 |
} |
| 383 |
|
| 384 |
float _al_osx_get_global_scale_factor(void) |
| 385 |
{ |
| 386 |
int count = osx_get_num_video_adapters(); |
| 387 |
if (count == 0) { |
| 388 |
return 1.; |
| 389 |
} |
| 390 |
float global_scale_factor = 1.; |
| 391 |
NSArray *screen_list = [NSScreen screens]; |
| 392 |
for (int i = 0; i < count; i++) { |
| 393 |
NSScreen *screen = [screen_list objectAtIndex: i]; |
| 394 |
float scale_factor = 1.0; |
| 395 |
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 |
| 396 |
if ([screen respondsToSelector:@selector(backingScaleFactor)]) { |
| 397 |
scale_factor = [screen backingScaleFactor]; |
| 398 |
} |
| 399 |
#endif |
| 400 |
if (scale_factor > global_scale_factor) { |
| 401 |
global_scale_factor = scale_factor; |
| 402 |
} |
| 403 |
} |
| 404 |
return global_scale_factor; |
| 405 |
} |
| 406 |
|
| 407 |
/* osx_get_monitor_info: |
| 408 |
* Return the details of one monitor |
| 409 |
*/ |
| 410 |
static bool osx_get_monitor_info(int adapter, ALLEGRO_MONITOR_INFO* info) |
| 411 |
{ |
| 412 |
int count = osx_get_num_video_adapters(); |
| 413 |
int primary_y = _al_osx_get_primary_screen_y(); |
| 414 |
float global_scale_factor = _al_osx_get_global_scale_factor(); |
| 415 |
if (adapter < count) { |
| 416 |
NSScreen *screen = [[NSScreen screens] objectAtIndex: adapter]; |
| 417 |
NSRect rc = [screen frame]; |
| 418 |
float scale_factor = 1.0; |
| 419 |
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 |
| 420 |
if ([screen respondsToSelector:@selector(backingScaleFactor)]) { |
| 421 |
scale_factor = [screen backingScaleFactor]; |
| 422 |
} |
| 423 |
#endif |
| 424 |
/* |
| 425 |
* The goal is to use top-left origin, y-down coordinate system |
| 426 |
* where units are individual pixels. We need to make sure the monitors |
| 427 |
* do not overlap, as otherwise we can't implement `al_set_window_position`. |
| 428 |
* We do this by creating an extended 2D plane which uses a global scale |
| 429 |
* factor with slots for the monitors. The slots are big enough |
| 430 |
* to fit monitors with the largest scale factor, but otherwise contain |
| 431 |
* empty space. When calling `al_get/set_window_position` we |
| 432 |
* transform from this extended plane to the visual coordinates used by the OS. |
| 433 |
* |
| 434 |
* OSX uses bottom-left, y-up coordinate system, so we need to flip things around. |
| 435 |
*/ |
| 436 |
info->x1 = (int) rc.origin.x * global_scale_factor; |
| 437 |
info->x2 = (int) (rc.origin.x * global_scale_factor + rc.size.width * scale_factor); |
| 438 |
info->y1 = (int) (global_scale_factor * (primary_y - (rc.size.height + rc.origin.y))); |
| 439 |
info->y2 = (int) (info->y1 + scale_factor * rc.size.height); |
| 440 |
ALLEGRO_INFO("Display %d has coordinates (%d, %d) - (%d, %d)\n", |
| 441 |
adapter, info->x1, info->y1, info->x2, info->y2); |
| 442 |
return true; |
| 443 |
} |
| 444 |
else { |
| 445 |
return false; |
| 446 |
} |
| 447 |
/* |
| 448 |
CGDisplayCount count; |
| 449 |
// Assume no more than 16 monitors connected |
| 450 |
static const int max_displays = 16; |
| 451 |
CGDirectDisplayID displays[max_displays]; |
| 452 |
CGError err = CGGetActiveDisplayList(max_displays, displays, &count); |
| 453 |
if (err == kCGErrorSuccess && adapter >= 0 && adapter < (int) count) { |
| 454 |
CGRect rc = CGDisplayBounds(displays[adapter]); |
| 455 |
info->x1 = (int) rc.origin.x; |
| 456 |
info->x2 = (int) (rc.origin.x + rc.size.width); |
| 457 |
info->y1 = (int) rc.origin.y; |
| 458 |
info->y2 = (int) (rc.origin.y + rc.size.height); |
| 459 |
ALLEGRO_INFO("Display %d has coordinates (%d, %d) - (%d, %d)\n", |
| 460 |
adapter, info->x1, info->y1, info->x2, info->y2); |
| 461 |
return true; |
| 462 |
} |
| 463 |
else { |
| 464 |
return false; |
| 465 |
} |
| 466 |
*/ |
| 467 |
} |
| 468 |
|
| 469 |
/* osx_get_monitor_dpi: |
| 470 |
* Return the dots per inch value of one monitor |
| 471 |
*/ |
| 472 |
static int osx_get_monitor_dpi(int adapter) |
| 473 |
{ |
| 474 |
int count = osx_get_num_video_adapters(); |
| 475 |
if (adapter < count) { |
| 476 |
NSScreen *screen = [[NSScreen screens] objectAtIndex: adapter]; |
| 477 |
NSRect rc = [screen frame]; |
| 478 |
rc = [screen convertRectToBacking: rc]; |
| 479 |
|
| 480 |
NSDictionary *description = [screen deviceDescription]; |
| 481 |
CGSize size = CGDisplayScreenSize([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]); |
| 482 |
float dpi_hori = rc.size.width / (_AL_INCHES_PER_MM * size.width); |
| 483 |
float dpi_vert = rc.size.height / (_AL_INCHES_PER_MM * size.height); |
| 484 |
|
| 485 |
return sqrt(dpi_hori * dpi_vert); |
| 486 |
} |
| 487 |
else { |
| 488 |
return 0; |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
/* osx_inhibit_screensaver: |
| 493 |
* Stops the screen dimming/screen saver activation if inhibit is true |
| 494 |
* otherwise re-enable normal behaviour. The last call takes force (i.e |
| 495 |
* it does not count the calls to inhibit/uninhibit. |
| 496 |
* Always returns true |
| 497 |
*/ |
| 498 |
static bool osx_inhibit_screensaver(bool inhibit) |
| 499 |
{ |
| 500 |
// Send a message to the App's delegate always on the main thread |
| 501 |
NSObject* delegate = [NSApp delegate]; |
| 502 |
[delegate performSelectorOnMainThread: @selector(setInhibitScreenSaver:) |
| 503 |
withObject: [NSNumber numberWithBool:inhibit ? YES : NO] |
| 504 |
waitUntilDone: NO]; |
| 505 |
ALLEGRO_INFO("Stop screensaver\n"); |
| 506 |
return true; |
| 507 |
} |
| 508 |
|
| 509 |
/* NSImageFromAllegroBitmap: |
| 510 |
* Create an NSImage from an Allegro bitmap |
| 511 |
* This could definitely be speeded up if necessary. |
| 512 |
*/ |
| 513 |
NSImage* NSImageFromAllegroBitmap(ALLEGRO_BITMAP* bmp) |
| 514 |
{ |
| 515 |
int w = al_get_bitmap_width(bmp); |
| 516 |
int h = al_get_bitmap_height(bmp); |
| 517 |
NSImage* img = [[NSImage alloc] initWithSize: NSMakeSize((float) w, (float) h)]; |
| 518 |
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL // Allocate memory yourself |
| 519 |
pixelsWide:w |
| 520 |
pixelsHigh:h |
| 521 |
bitsPerSample: 8 |
| 522 |
samplesPerPixel: 4 |
| 523 |
hasAlpha:YES |
| 524 |
isPlanar:NO |
| 525 |
colorSpaceName:NSDeviceRGBColorSpace |
| 526 |
bytesPerRow: 0 // Calculate yourself |
| 527 |
bitsPerPixel:0 ];// Calculate yourself |
| 528 |
al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY); |
| 529 |
int x, y; |
| 530 |
for (y = 0; y<h; ++y) { |
| 531 |
for (x = 0; x<w; ++x) { |
| 532 |
ALLEGRO_COLOR c = al_get_pixel(bmp, x, y); |
| 533 |
unsigned char* ptr = [rep bitmapData] + y * [rep bytesPerRow] + x * ([rep bitsPerPixel]/8); |
| 534 |
al_unmap_rgba(c, ptr, ptr+1, ptr+2, ptr+3); |
| 535 |
// NSImage should be premultiplied alpha |
| 536 |
ptr[0] *= c.a; |
| 537 |
ptr[1] *= c.a; |
| 538 |
ptr[2] *= c.a; |
| 539 |
} |
| 540 |
} |
| 541 |
al_unlock_bitmap(bmp); |
| 542 |
[img addRepresentation:rep]; |
| 543 |
[rep release]; |
| 544 |
return img; |
| 545 |
} |
| 546 |
|
| 547 |
/* This works as long as there is only one screen */ |
| 548 |
/* Not clear from docs how mouseLocation works if > 1 */ |
| 549 |
static bool osx_get_cursor_position(int *x, int *y) |
| 550 |
{ |
| 551 |
NSPoint p = [NSEvent mouseLocation]; |
| 552 |
NSRect r = [[NSScreen mainScreen] frame]; |
| 553 |
*x = p.x; |
| 554 |
*y = r.size.height - p.y; |
| 555 |
return true; |
| 556 |
} |
| 557 |
|
| 558 |
static void osx_thread_init(ALLEGRO_THREAD *thread) |
| 559 |
{ |
| 560 |
} |
| 561 |
|
| 562 |
static void osx_thread_exit(ALLEGRO_THREAD *thread) |
| 563 |
{ |
| 564 |
} |
| 565 |
|
| 566 |
/* Internal function to get a reference to this driver. */ |
| 567 |
ALLEGRO_SYSTEM_INTERFACE *_al_system_osx_driver(void) |
| 568 |
{ |
| 569 |
static ALLEGRO_SYSTEM_INTERFACE* vt = NULL; |
| 570 |
if (vt == NULL) { |
| 571 |
vt = al_malloc(sizeof(*vt)); |
| 572 |
memset(vt, 0, sizeof(*vt)); |
| 573 |
vt->id = ALLEGRO_SYSTEM_ID_MACOSX; |
| 574 |
vt->initialize = osx_sys_init; |
| 575 |
vt->get_display_driver = _al_osx_get_display_driver; |
| 576 |
vt->get_keyboard_driver = _al_osx_get_keyboard_driver; |
| 577 |
vt->get_mouse_driver = _al_osx_get_mouse_driver; |
| 578 |
vt->get_joystick_driver = _al_osx_get_joystick_driver; |
| 579 |
vt->get_num_display_modes = _al_osx_get_num_display_modes; |
| 580 |
vt->get_display_mode = _al_osx_get_display_mode; |
| 581 |
vt->shutdown_system = osx_sys_exit; |
| 582 |
vt->get_num_video_adapters = osx_get_num_video_adapters; |
| 583 |
vt->get_monitor_info = osx_get_monitor_info; |
| 584 |
vt->get_monitor_dpi = osx_get_monitor_dpi; |
| 585 |
vt->create_mouse_cursor = _al_osx_create_mouse_cursor; |
| 586 |
vt->destroy_mouse_cursor = _al_osx_destroy_mouse_cursor; |
| 587 |
vt->get_cursor_position = osx_get_cursor_position; |
| 588 |
vt->get_path = _al_osx_get_path; |
| 589 |
vt->inhibit_screensaver = osx_inhibit_screensaver; |
| 590 |
vt->thread_init = osx_thread_init; |
| 591 |
vt->thread_exit = osx_thread_exit; |
| 592 |
vt->get_time = _al_unix_get_time; |
| 593 |
vt->rest = _al_unix_rest; |
| 594 |
vt->init_timeout = _al_unix_init_timeout; |
| 595 |
|
| 596 |
}; |
| 597 |
|
| 598 |
return vt; |
| 599 |
} |
| 600 |
|
| 601 |
/* This is a function each platform must define to register all available |
| 602 |
* system drivers. |
| 603 |
*/ |
| 604 |
void _al_register_system_interfaces() |
| 605 |
{ |
| 606 |
ALLEGRO_SYSTEM_INTERFACE **add; |
| 607 |
|
| 608 |
add = _al_vector_alloc_back(&_al_system_interfaces); |
| 609 |
*add = _al_system_osx_driver(); |
| 610 |
} |
| 611 |
|
| 612 |
/* Implementation of get_path */ |
| 613 |
ALLEGRO_PATH *_al_osx_get_path(int id) |
| 614 |
{ |
| 615 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 616 |
NSString* ans = nil; |
| 617 |
NSArray* paths = nil; |
| 618 |
NSString *org_name = [[NSString alloc] initWithUTF8String: al_get_org_name()]; |
| 619 |
NSString *app_name = [[NSString alloc] initWithUTF8String: al_get_app_name()]; |
| 620 |
ALLEGRO_PATH *path = NULL; |
| 621 |
|
| 622 |
switch (id) { |
| 623 |
case ALLEGRO_RESOURCES_PATH: |
| 624 |
if (_al_osx_bundle) { |
| 625 |
ans = [_al_osx_bundle resourcePath]; |
| 626 |
path = al_create_path_for_directory([ans UTF8String]); |
| 627 |
} else { |
| 628 |
/* Otherwise, return the executable pathname */ |
| 629 |
path = _al_osx_get_path(ALLEGRO_EXENAME_PATH); |
| 630 |
al_set_path_filename(path, NULL); |
| 631 |
} |
| 632 |
break; |
| 633 |
case ALLEGRO_TEMP_PATH: |
| 634 |
ans = NSTemporaryDirectory(); |
| 635 |
path = al_create_path_for_directory([ans UTF8String]); |
| 636 |
break; |
| 637 |
case ALLEGRO_USER_DATA_PATH: |
| 638 |
paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, |
| 639 |
NSUserDomainMask, |
| 640 |
YES); |
| 641 |
if ([paths count] > 0) |
| 642 |
ans = [paths objectAtIndex: 0]; |
| 643 |
if (ans != nil) { |
| 644 |
/* Append program name */ |
| 645 |
ans = [[ans stringByAppendingPathComponent: org_name] |
| 646 |
stringByAppendingPathComponent: app_name]; |
| 647 |
} |
| 648 |
path = al_create_path_for_directory([ans UTF8String]); |
| 649 |
break; |
| 650 |
case ALLEGRO_USER_HOME_PATH: |
| 651 |
ans = NSHomeDirectory(); |
| 652 |
path = al_create_path_for_directory([ans UTF8String]); |
| 653 |
break; |
| 654 |
case ALLEGRO_USER_DOCUMENTS_PATH: |
| 655 |
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, |
| 656 |
NSUserDomainMask, |
| 657 |
YES); |
| 658 |
if ([paths count] > 0) |
| 659 |
ans = [paths objectAtIndex: 0]; |
| 660 |
path = al_create_path_for_directory([ans UTF8String]); |
| 661 |
break; |
| 662 |
case ALLEGRO_EXENAME_PATH: { |
| 663 |
char exepath[PATH_MAX]; |
| 664 |
uint32_t size = sizeof(exepath); |
| 665 |
if (_NSGetExecutablePath(exepath, &size) == 0) |
| 666 |
ans = [NSString stringWithUTF8String: exepath]; |
| 667 |
|
| 668 |
path = al_create_path([ans UTF8String]); |
| 669 |
break; |
| 670 |
} |
| 671 |
case ALLEGRO_USER_SETTINGS_PATH: |
| 672 |
paths = |
| 673 |
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, |
| 674 |
NSUserDomainMask, |
| 675 |
YES); |
| 676 |
if ([paths count] > 0) |
| 677 |
ans = [paths objectAtIndex: 0]; |
| 678 |
if (ans != nil) { |
| 679 |
/* Append program name */ |
| 680 |
ans = [[ans stringByAppendingPathComponent: org_name] |
| 681 |
stringByAppendingPathComponent: app_name]; |
| 682 |
} |
| 683 |
path = al_create_path_for_directory([ans UTF8String]); |
| 684 |
break; |
| 685 |
default: |
| 686 |
break; |
| 687 |
} |
| 688 |
[org_name release]; |
| 689 |
[app_name release]; |
| 690 |
[pool drain]; |
| 691 |
|
| 692 |
return path; |
| 693 |
} |
| 694 |
|
| 695 |
/* _al_osx_post_quit |
| 696 |
* called when the user clicks the quit menu or cmd-Q. |
| 697 |
* Currently just sends a window close event to all windows. |
| 698 |
* This is a bit unsatisfactory |
| 699 |
*/ |
| 700 |
void _al_osx_post_quit(void) |
| 701 |
{ |
| 702 |
unsigned int i; |
| 703 |
_AL_VECTOR* dpys = &al_get_system_driver()->displays; |
| 704 |
// Iterate through all existing displays |
| 705 |
for (i = 0; i < _al_vector_size(dpys); ++i) { |
| 706 |
ALLEGRO_DISPLAY* dpy = *(ALLEGRO_DISPLAY**) _al_vector_ref(dpys, i); |
| 707 |
ALLEGRO_EVENT_SOURCE* src = &(dpy->es); |
| 708 |
_al_event_source_lock(src); |
| 709 |
ALLEGRO_EVENT evt; |
| 710 |
evt.type = ALLEGRO_EVENT_DISPLAY_CLOSE; |
| 711 |
// Send event |
| 712 |
_al_event_source_emit_event(src, &evt); |
| 713 |
_al_event_source_unlock(src); |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
/* Local variables: */ |
| 718 |
/* c-basic-offset: 3 */ |
| 719 |
/* indent-tabs-mode: nil */ |
| 720 |
/* End: */ |