/
osxgl.m
2641 lines (2397 loc) · 88.1 KB
/
osxgl.m
1
2
3
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
44
45
46
47
48
49
50
51
52
53
54
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ______ ___ ___
* /\ _ \ /\_ \ /\_ \
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
* /\____/
* \_/__/
*
* MacOS X OpenGL gfx driver
*
* By Peter Hull.
*
* See readme.txt for copyright information.
*/
#include "allegro5/allegro.h"
#include "allegro5/allegro_opengl.h"
#include "allegro5/internal/aintern.h"
#include "allegro5/internal/aintern_bitmap.h"
#include "allegro5/internal/aintern_system.h"
#include "allegro5/internal/aintern_keyboard.h"
#include "allegro5/internal/aintern_opengl.h"
#include "allegro5/internal/aintern_osxclipboard.h"
#include "allegro5/platform/aintosx.h"
#include "./osxgl.h"
#include "allegro5/allegro_osx.h"
#ifndef ALLEGRO_MACOSX
#error something is wrong with the makefile
#endif
#import <Cocoa/Cocoa.h>
#import <OpenGL/OpenGL.h>
ALLEGRO_DEBUG_CHANNEL("MacOSX")
/* Many Cocoa methods can only be called from the main thread, but Allegro runs the
user's code on a separate thread or threads. It relies on `dispatch_sync` or
`[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]` to make sure the
operation happens on the main thread. Comments on functions in this file indicate their
thread requirements, if any.
#define EXTRA_THREAD_CHECKS to catch any issues during development (don't leave it defined though.) */
#undef EXTRA_THREAD_CHECKS
#ifdef EXTRA_THREAD_CHECKS
#define ASSERT_MAIN_THREAD() NSCAssert([NSThread isMainThread], @"Must be run on main thread")
#define ASSERT_USER_THREAD() NSCAssert(![NSThread isMainThread], @"Must be run on user thread")
#else
#define ASSERT_MAIN_THREAD()
#define ASSERT_USER_THREAD()
#endif
/* This constant isn't available on OS X < 10.7, define
* it here so the library can be built on < 10.7 (10.6
* tested so far.)
*/
#if (MAC_OS_X_VERSION_MAX_ALLOWED < 1070 && !defined(NSWindowCollectionBehaviorFullScreenPrimary))
enum {
NSWindowCollectionBehaviorFullScreenPrimary = (1 << 7)
};
#endif
/* Defines */
#define MINIMUM_WIDTH 48
#define MINIMUM_HEIGHT 48
/* Unsigned integer; data type only avaliable for OS X >= 10.5 */
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
typedef unsigned int NSUInteger;
#endif
/* Module Variables */
static BOOL _osx_mouse_installed = NO, _osx_keyboard_installed = NO;
static NSPoint last_window_pos;
static unsigned int next_display_group = 1;
/* The parameters are passed to initialiseDisplay manually, as it runs
* in a separate thread which renders TLS values incorrect.
*/
typedef struct OSX_DISPLAY_PARAMS {
ALLEGRO_DISPLAY_OSX_WIN* dpy;
int new_window_pos_x;
int new_window_pos_y;
int new_display_adapter;
/* A copy of the new window title. */
char* new_window_title;
} _OSX_DISPLAY_PARAMS;
/* Dictionary to map Allegro's DISPLAY_OPTIONS to OS X
* PixelFormatAttributes.
* The first column is Allegro's name, the second column is the OS X
* PixelFormatAttribute (or 0), the third column indicates whether we
* need an extra parameter or not (eg, colour depth).
*/
static const unsigned int allegro_to_osx_settings[][3] = {
{ ALLEGRO_RED_SIZE, 0, 0}, // Not supported per component
{ ALLEGRO_GREEN_SIZE, 0, 0}, // Not supported per component
{ ALLEGRO_BLUE_SIZE, 0, 0}, // Not supported per component
{ ALLEGRO_ALPHA_SIZE, NSOpenGLPFAAlphaSize, 1},
{ ALLEGRO_RED_SHIFT, 0, 0}, // Not available
{ ALLEGRO_GREEN_SHIFT, 0, 0}, // Not available
{ ALLEGRO_BLUE_SHIFT, 0, 0}, // Not available
{ ALLEGRO_ALPHA_SHIFT, 0, 0}, // Not available
{ ALLEGRO_ACC_RED_SIZE, NSOpenGLPFAAccumSize, 1}, // Correct?
{ ALLEGRO_ACC_GREEN_SIZE, NSOpenGLPFAAccumSize, 1}, // Correct?
{ ALLEGRO_ACC_BLUE_SIZE, NSOpenGLPFAAccumSize, 1}, // Correct?
{ ALLEGRO_ACC_ALPHA_SIZE, NSOpenGLPFAAccumSize, 1}, // Correct?
{ ALLEGRO_STEREO, NSOpenGLPFAStereo, 0},
{ ALLEGRO_AUX_BUFFERS, NSOpenGLPFAAuxBuffers, 1},
{ ALLEGRO_COLOR_SIZE, NSOpenGLPFAColorSize, 1},
{ ALLEGRO_DEPTH_SIZE, NSOpenGLPFADepthSize, 1},
{ ALLEGRO_STENCIL_SIZE, NSOpenGLPFAStencilSize, 1},
{ ALLEGRO_SAMPLE_BUFFERS, NSOpenGLPFASampleBuffers, 1},
{ ALLEGRO_SAMPLES, NSOpenGLPFASamples, 1},
//{ ALLEGRO_RENDER_METHOD, NSOpenGLPFAAccelerated, 0}, handled separately
{ ALLEGRO_FLOAT_COLOR, NSOpenGLPFAColorFloat, 0},
{ ALLEGRO_FLOAT_DEPTH, 0, 0},
//{ ALLEGRO_SINGLE_BUFFER , 0, 0}, handled separately
{ ALLEGRO_SWAP_METHOD, 0, 0},
{ ALLEGRO_COMPATIBLE_DISPLAY, 0, 0},
{ ALLEGRO_DISPLAY_OPTIONS_COUNT, 0, 0}
};
static const int number_of_settings =
sizeof(allegro_to_osx_settings)/sizeof(*allegro_to_osx_settings);
static const char *allegro_pixel_format_names[] = {
"ALLEGRO_RED_SIZE",
"ALLEGRO_GREEN_SIZE",
"ALLEGRO_BLUE_SIZE",
"ALLEGRO_ALPHA_SIZE",
"ALLEGRO_RED_SHIFT",
"ALLEGRO_GREEN_SHIFT",
"ALLEGRO_BLUE_SHIFT",
"ALLEGRO_ALPHA_SHIFT",
"ALLEGRO_ACC_RED_SIZE",
"ALLEGRO_ACC_GREEN_SIZE",
"ALLEGRO_ACC_BLUE_SIZE",
"ALLEGRO_ACC_ALPHA_SIZE",
"ALLEGRO_STEREO",
"ALLEGRO_AUX_BUFFERS",
"ALLEGRO_COLOR_SIZE",
"ALLEGRO_DEPTH_SIZE",
"ALLEGRO_STENCIL_SIZE",
"ALLEGRO_SAMPLE_BUFFERS",
"ALLEGRO_SAMPLES",
"ALLEGRO_RENDER_METHOD",
"ALLEGRO_FLOAT_COLOR",
"ALLEGRO_FLOAT_DEPTH",
"ALLEGRO_SINGLE_BUFFER",
"ALLEGRO_SWAP_METHOD",
"ALLEGRO_COMPATIBLE_DISPLAY",
"ALLEGRO_DISPLAY_OPTIONS_COUNT"
};
/* Module functions */
ALLEGRO_DISPLAY_INTERFACE* _al_osx_get_display_driver(void);
ALLEGRO_DISPLAY_INTERFACE* _al_osx_get_display_driver_win(void);
ALLEGRO_DISPLAY_INTERFACE* _al_osx_get_display_driver_fs(void);
static NSOpenGLContext* osx_create_shareable_context(NSOpenGLPixelFormat* fmt, unsigned int* group);
static bool set_display_flag(ALLEGRO_DISPLAY *display, int flag, bool onoff);
static bool resize_display_win(ALLEGRO_DISPLAY *d, int w, int h);
static bool resize_display_win_main_thread(ALLEGRO_DISPLAY *d, int w, int h);
static void clear_to_black(NSOpenGLContext *context)
{
/* Clear and flush (for double buffering) */
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
[context flushBuffer];
glClear(GL_COLOR_BUFFER_BIT);
}
static NSTrackingArea *create_tracking_area(NSView *view)
{
NSTrackingAreaOptions options =
NSTrackingMouseEnteredAndExited |
NSTrackingMouseMoved |
NSTrackingCursorUpdate |
NSTrackingActiveAlways;
return [[NSTrackingArea alloc] initWithRect:[view bounds] options:options owner:view userInfo:nil];
}
/* _al_osx_change_cursor:
* Actually change the current cursor. This can be called fom any thread
* but ensures that the change is only called from the main thread.
*/
static void _al_osx_change_cursor(ALLEGRO_DISPLAY_OSX_WIN *dpy, NSCursor* cursor)
{
NSCursor* old = dpy->cursor;
dpy->cursor = [cursor retain];
[old release];
if (dpy->show_cursor)
[cursor performSelectorOnMainThread: @selector(set) withObject: nil waitUntilDone: NO];
}
/* _al_osx_keyboard_was_installed:
* Called by the keyboard driver when the driver is installed or uninstalled.
* Set the variable so we can decide to pass events or not.
*/
void _al_osx_keyboard_was_installed(BOOL install) {
_osx_keyboard_installed = install;
}
/* The main additions to this view are event-handling functions */
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
@interface ALOpenGLView : NSOpenGLView <NSWindowDelegate>
#else
@interface ALOpenGLView : NSOpenGLView
#endif
{
/* This is passed onto the event functions so we know where the event came from */
ALLEGRO_DISPLAY* dpy_ptr;
}
-(void)setAllegroDisplay: (ALLEGRO_DISPLAY*) ptr;
-(ALLEGRO_DISPLAY*) allegroDisplay;
-(void) reshape;
-(BOOL) acceptsFirstResponder;
-(void) keyDown:(NSEvent*) event;
-(void) keyUp:(NSEvent*) event;
-(void) flagsChanged:(NSEvent*) event;
-(void) mouseDown: (NSEvent*) evt;
-(void) mouseUp: (NSEvent*) evt;
-(void) mouseDragged: (NSEvent*) evt;
-(void) rightMouseDown: (NSEvent*) evt;
-(void) rightMouseUp: (NSEvent*) evt;
-(void) rightMouseDragged: (NSEvent*) evt;
-(void) otherMouseDown: (NSEvent*) evt;
-(void) otherMouseUp: (NSEvent*) evt;
-(void) otherMouseDragged: (NSEvent*) evt;
-(void) mouseMoved: (NSEvent*) evt;
-(void) cursorUpdate: (NSEvent*) evt;
-(void) scrollWheel: (NSEvent*) evt;
-(void) viewDidMoveToWindow;
-(void) viewWillMoveToWindow: (NSWindow*) newWindow;
-(void) mouseEntered: (NSEvent*) evt;
-(void) mouseExited: (NSEvent*) evt;
-(void) viewWillStartLiveResize;
-(void) viewDidEndLiveResize;
/* Window delegate methods */
-(void) windowDidBecomeMain:(NSNotification*) notification;
-(void) windowDidResignMain:(NSNotification*) notification;
-(void) windowDidResize:(NSNotification*) notification;
-(void) enterFullScreenWindowMode;
-(void) exitFullScreenWindowMode;
-(void) finishExitingFullScreenWindowMode;
-(void) maximize;
-(NSRect) windowWillUseStandardFrame:
(NSWindow *) window
defaultFrame: (NSRect) newFrame;
@end
@implementation ALWindow
@synthesize display;
-(BOOL) canBecomeKeyWindow
{
return YES;
}
// main thread only
-(void) zoom:(id)sender
{
self.display->flags ^= ALLEGRO_MAXIMIZED;
[super zoom:sender];
}
@end
/* _al_osx_mouse_was_installed:
* Called by the mouse driver when the driver is installed or uninstalled.
* Set the variable so we can decide to pass events or not, and notify all
* existing displays that they need to set up their tracking areas.
* User thread only
*/
void _al_osx_mouse_was_installed(BOOL install) {
ASSERT_USER_THREAD();
if (_osx_mouse_installed == install) {
// done it already
return;
}
_osx_mouse_installed = install;
_AL_VECTOR* dpys = &al_get_system_driver()->displays;
dispatch_sync(dispatch_get_main_queue(), ^{
unsigned int i;
for (i = 0; i < _al_vector_size(dpys); ++i) {
ALLEGRO_DISPLAY_OSX_WIN* dpy = *(ALLEGRO_DISPLAY_OSX_WIN**) _al_vector_ref(dpys, i);
NSWindow* window = dpy->win;
if (window) {
[window setAcceptsMouseMovedEvents: _osx_mouse_installed];
}
}
});
}
@implementation ALOpenGLView
-(void) prepareOpenGL
{
[super prepareOpenGL];
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
if ([self respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) {
[self setWantsBestResolutionOpenGLSurface:YES];
}
#endif
}
/* setDisplay:
* Set the display this view is associated with
*/
-(void) setAllegroDisplay: (ALLEGRO_DISPLAY*) ptr
{
dpy_ptr = ptr;
}
/* display
* return the display this view is associated with
*/
-(ALLEGRO_DISPLAY*) allegroDisplay
{
return dpy_ptr;
}
/* reshape
* Called when the view changes size */
- (void) reshape
{
[super reshape];
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
if (dpy->tracking) {
[self removeTrackingArea: dpy->tracking];
dpy->tracking = create_tracking_area(self);
[self addTrackingArea: dpy->tracking];
}
}
/* acceptsFirstResponder
* Overridden to return YES, so that
* this view will receive events
*/
-(BOOL) acceptsFirstResponder
{
return YES;
}
/* Keyboard event handler */
-(void) keyDown:(NSEvent*) event
{
if (_osx_keyboard_installed)
_al_osx_keyboard_handler(true, event, dpy_ptr);
}
-(void) keyUp:(NSEvent*) event
{
if (_osx_keyboard_installed)
_al_osx_keyboard_handler(false, event, dpy_ptr);
}
-(void) flagsChanged:(NSEvent*) event
{
if (_osx_keyboard_installed) {
_al_osx_keyboard_modifiers([event modifierFlags], dpy_ptr);
}
}
/* Mouse handling
*/
-(void) mouseDown: (NSEvent*) evt
{
if (_osx_mouse_installed)
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
-(void) mouseUp: (NSEvent*) evt
{
if (_osx_mouse_installed)
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
-(void) mouseDragged: (NSEvent*) evt
{
if (_osx_mouse_installed)
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
-(void) rightMouseDown: (NSEvent*) evt
{
if (_osx_mouse_installed)
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
-(void) rightMouseUp: (NSEvent*) evt
{
if (_osx_mouse_installed)
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
-(void) rightMouseDragged: (NSEvent*) evt
{
if (_osx_mouse_installed)
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
-(void) otherMouseDown: (NSEvent*) evt
{
if (_osx_mouse_installed)
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
-(void) otherMouseUp: (NSEvent*) evt
{
if (_osx_mouse_installed)
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
-(void) otherMouseDragged: (NSEvent*) evt
{
if (_osx_mouse_installed)
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
-(void) mouseMoved: (NSEvent*) evt
{
if (_osx_mouse_installed)
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
-(void) cursorUpdate: (NSEvent*) evt
{
if (_osx_mouse_installed) {
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
_al_osx_change_cursor(dpy, dpy->cursor);
}
}
-(void) scrollWheel: (NSEvent*) evt
{
if (_osx_mouse_installed)
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
/* Cursor handling */
- (void) viewDidMoveToWindow {
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
if (dpy->tracking) {
[self removeTrackingArea: dpy->tracking];
}
dpy->tracking = create_tracking_area(self);
[self addTrackingArea: dpy->tracking];
}
- (void) viewWillMoveToWindow: (NSWindow*) newWindow {
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
(void)newWindow;
if (([self window] != nil) && (dpy->tracking != 0)) {
[self removeTrackingArea:dpy->tracking];
dpy->tracking = 0;
}
}
-(void) mouseEntered: (NSEvent*) evt
{
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
ALLEGRO_EVENT_SOURCE* src = &([self allegroDisplay]->es);
if (dpy->show_cursor) {
[dpy->cursor set];
}
else {
[NSCursor hide];
}
_al_event_source_lock(src);
_al_osx_switch_mouse_focus(dpy_ptr, true);
_al_event_source_unlock(src);
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
-(void) mouseExited: (NSEvent*) evt
{
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
ALLEGRO_EVENT_SOURCE* src = &([self allegroDisplay]->es);
if (!dpy->show_cursor) {
[NSCursor unhide];
}
_al_event_source_lock(src);
_al_osx_switch_mouse_focus(dpy_ptr, false);
_al_event_source_unlock(src);
_al_osx_mouse_generate_event(evt, dpy_ptr);
}
/* windowShouldClose:
* Veto the close and post a message
*/
- (BOOL)windowShouldClose:(id)sender
{
(void)sender;
ALLEGRO_EVENT_SOURCE* src = &([self allegroDisplay]->es);
_al_event_source_lock(src);
ALLEGRO_EVENT evt;
evt.type = ALLEGRO_EVENT_DISPLAY_CLOSE;
_al_event_source_emit_event(src, &evt);
_al_event_source_unlock(src);
return NO;
}
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1074
-(void) viewDidChangeBackingProperties
{
[super viewDidChangeBackingProperties];
if (!(al_get_display_flags(dpy_ptr) & ALLEGRO_RESIZABLE) &&
dpy_ptr->ogl_extras) {
resize_display_win_main_thread(dpy_ptr, al_get_display_width(dpy_ptr), al_get_display_height(dpy_ptr));
}
else {
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
NSWindow *window = dpy->win;
NSRect rc = [window frame];
NSRect content = [window contentRectForFrameRect: rc];
content = [self convertRectToBacking: content];
ALLEGRO_EVENT_SOURCE *es = &dpy->parent.es;
_al_event_source_lock(es);
if (_al_event_source_needs_to_generate_event(es)) {
ALLEGRO_EVENT event;
event.display.type = ALLEGRO_EVENT_DISPLAY_RESIZE;
event.display.timestamp = al_get_time();
event.display.width = NSWidth(content);
event.display.height = NSHeight(content);
_al_event_source_emit_event(es, &event);
ALLEGRO_INFO("Window finished resizing %d x %d\n", event.display.width, event.display.height);
}
_al_event_source_unlock(es);
}
}
#endif
-(void) viewWillStartLiveResize
{
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
ALLEGRO_EVENT_SOURCE *es = &dpy->parent.es;
if (dpy->send_halt_events) {
al_lock_mutex(dpy->halt_mutex);
dpy->halt_event_acknowledged = false;
al_unlock_mutex(dpy->halt_mutex);
_al_event_source_lock(es);
if (_al_event_source_needs_to_generate_event(es)) {
ALLEGRO_EVENT event;
event.display.type = ALLEGRO_EVENT_DISPLAY_HALT_DRAWING;
event.display.timestamp = al_get_time();
_al_event_source_emit_event(es, &event);
}
_al_event_source_unlock(es);
al_lock_mutex(dpy->halt_mutex);
while (!dpy->halt_event_acknowledged) {
al_wait_cond(dpy->halt_cond, dpy->halt_mutex);
}
al_unlock_mutex(dpy->halt_mutex);
}
}
-(void) viewDidEndLiveResize
{
[super viewDidEndLiveResize];
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
NSWindow *window = dpy->win;
NSRect rc = [window frame];
NSRect content = [window contentRectForFrameRect: rc];
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
content = [self convertRectToBacking: content];
#endif
ALLEGRO_EVENT_SOURCE *es = &dpy->parent.es;
_al_event_source_lock(es);
if (_al_event_source_needs_to_generate_event(es)) {
ALLEGRO_EVENT event;
event.display.type = ALLEGRO_EVENT_DISPLAY_RESIZE;
event.display.timestamp = al_get_time();
event.display.width = NSWidth(content);
event.display.height = NSHeight(content);
_al_event_source_emit_event(es, &event);
ALLEGRO_INFO("Window finished resizing %d x %d\n", event.display.width, event.display.height);
if (dpy->send_halt_events) {
event.display.type = ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING;
_al_event_source_emit_event(es, &event);
}
}
_al_event_source_unlock(es);
dpy->old_w = NSWidth(content);
dpy->old_h = NSWidth(content);
}
/* Window switch in/out */
-(void) windowDidBecomeMain:(NSNotification*) notification
{
(void)notification;
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
ALLEGRO_EVENT_SOURCE* src = &([self allegroDisplay]->es);
_al_event_source_lock(src);
ALLEGRO_EVENT evt;
evt.type = ALLEGRO_EVENT_DISPLAY_SWITCH_IN;
_al_event_source_emit_event(src, &evt);
_al_osx_switch_keyboard_focus(dpy_ptr, true);
_al_event_source_unlock(src);
_al_osx_change_cursor(dpy, dpy->cursor);
}
-(void) windowDidResignMain:(NSNotification*) notification
{
(void)notification;
ALLEGRO_EVENT_SOURCE* src = &([self allegroDisplay]->es);
_al_event_source_lock(src);
ALLEGRO_EVENT evt;
evt.type = ALLEGRO_EVENT_DISPLAY_SWITCH_OUT;
_al_event_source_emit_event(src, &evt);
_al_osx_switch_keyboard_focus(dpy_ptr, false);
_al_event_source_unlock(src);
}
-(void) windowDidResize:(NSNotification*) notification
{
(void)notification;
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
NSWindow *window = dpy->win;
NSRect rc = [window frame];
NSRect content = [window contentRectForFrameRect: rc];
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
content = [self convertRectToBacking: content];
#endif
ALLEGRO_EVENT_SOURCE *es = &dpy->parent.es;
/* Restore max. constraints when the window has been un-maximized.
* Note: isZoomed will return false in FullScreen mode.
*/
if (dpy_ptr->use_constraints &&
!(dpy_ptr->flags & ALLEGRO_FULLSCREEN_WINDOW) && ![window isZoomed])
{
float scale_factor = 1.0;
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
if ([window respondsToSelector:@selector(backingScaleFactor)]) {
scale_factor = [window backingScaleFactor];
}
#endif
NSSize max_size;
max_size.width = (dpy_ptr->max_w > 0) ? dpy_ptr->max_w / scale_factor : FLT_MAX;
max_size.height = (dpy_ptr->max_h > 0) ? dpy_ptr->max_h / scale_factor : FLT_MAX;
[window setContentMaxSize: max_size];
}
_al_event_source_lock(es);
if (_al_event_source_needs_to_generate_event(es)) {
ALLEGRO_EVENT event;
event.display.type = ALLEGRO_EVENT_DISPLAY_RESIZE;
event.display.timestamp = al_get_time();
event.display.width = NSWidth(content);
event.display.height = NSHeight(content);
_al_event_source_emit_event(es, &event);
ALLEGRO_INFO("Window was resized %d x %d\n", event.display.width, event.display.height);
}
_al_event_source_unlock(es);
}
-(void)windowWillEnterFullScreen:(NSNotification *)notification
{
(void)notification;
ALLEGRO_DISPLAY *display = dpy_ptr;
display->flags |= ALLEGRO_FULLSCREEN_WINDOW;
}
-(void)windowWillExitFullScreen:(NSNotification *)notification
{
(void)notification;
ALLEGRO_DISPLAY *display = dpy_ptr;
display->flags &= ~ALLEGRO_FULLSCREEN_WINDOW;
}
-(void) enterFullScreenWindowMode
{
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
ALLEGRO_DISPLAY_OSX_WIN *dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
int flags = NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar;
[dict setObject:[NSNumber numberWithInt: flags] forKey:NSFullScreenModeApplicationPresentationOptions];
/* HACK? For some reason, we need to disable the chrome. If we don't, the fullscreen window
will be created with space left over for it. Are we creating a fullscreen window in the wrong way? */
[dpy->win setStyleMask: [dpy->win styleMask] & ~NSWindowStyleMaskTitled];
[dpy->view enterFullScreenMode: [dpy->win screen] withOptions: dict];
[dict release];
#endif
}
/* Toggles maximize state. In OSX 10.10 this is the same as double clicking the title bar. */
-(void) maximize
{
ALLEGRO_DISPLAY_OSX_WIN *dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
[dpy->win performZoom: nil];
}
/* Called by NSWindow's zoom: method while determining the frame
* a window may be zoomed to.
* We use it to update max. constraints values when the window changes
* its maximized state.
*/
-(NSRect) windowWillUseStandardFrame:
(NSWindow *) window
defaultFrame: (NSRect) newFrame
{
NSSize max_size;
if (dpy_ptr->use_constraints) {
if (dpy_ptr->flags & ALLEGRO_MAXIMIZED) {
max_size.width = FLT_MAX;
max_size.height = FLT_MAX;
newFrame.size.width = max_size.width;
newFrame.size.height = max_size.height;
}
else {
float scale_factor = 1.0;
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
if ([window respondsToSelector:@selector(backingScaleFactor)]) {
scale_factor = [window backingScaleFactor];
}
#endif
max_size.width = (dpy_ptr->max_w > 0) ? dpy_ptr->max_w / scale_factor : FLT_MAX;
max_size.height = (dpy_ptr->max_h > 0) ? dpy_ptr->max_h / scale_factor : FLT_MAX;
}
[window setContentMaxSize: max_size];
}
return newFrame;
}
-(void) exitFullScreenWindowMode
{
ALLEGRO_DISPLAY_OSX_WIN *dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
/* Going from a fullscreen window to a smaller window, the mouse may end up outside
* the window when it was inside before (not possible the other way.) This causes a
* crash. To avoid it, remove the tracking area and add it back after exiting fullscreen.
* (my theory)
*/
[dpy->win orderOut: dpy->view];
[self exitFullScreenModeWithOptions: nil];
/* Restore the title bar disabled in enterFullScreenWindowMode. */
if (!(dpy_ptr->flags & ALLEGRO_FRAMELESS)) {
[dpy->win setStyleMask: [dpy->win styleMask] | NSWindowStyleMaskTitled];
}
}
-(void) finishExitingFullScreenWindowMode
{
ALLEGRO_DISPLAY_OSX_WIN *dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
[dpy->win center];
[dpy->win makeKeyAndOrderFront: dpy->view];
[[self window] makeFirstResponder: self];
}
/* End of ALOpenGLView implementation */
@end
/* set_current_display:
* Set the current windowed display to be current.
*/
static bool set_current_display(ALLEGRO_DISPLAY* d) {
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) d;
if (dpy->ctx != nil) {
[dpy->ctx makeCurrentContext];
}
_al_ogl_set_extensions(d->ogl_extras->extension_api);
return true;
}
/* Helper to set up GL state as we want it. */
static void setup_gl(ALLEGRO_DISPLAY *d)
{
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) d;
[dpy->ctx performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:YES];
_al_ogl_setup_gl(d);
}
/* Fills the array of NSOpenGLPixelFormatAttributes, which has a specfied
* maximum size, with the options appropriate for the display.
*/
static void osx_set_opengl_pixelformat_attributes(ALLEGRO_DISPLAY_OSX_WIN *dpy)
{
int i, n;
bool want_double_buffer;
NSOpenGLPixelFormatAttribute *a;
ALLEGRO_EXTRA_DISPLAY_SETTINGS *extras;
/* The following combination of flags indicates that multi-sampling is
* requested.
*/
const int sample_flags = (1<<ALLEGRO_SAMPLES)|(1<<ALLEGRO_SAMPLE_BUFFERS);
/* Get extra display settings */
extras = _al_get_new_display_settings();
/* Begin by setting all elements in the array to 0 */
memset(dpy->attributes, 0, AL_OSX_NUM_PFA*sizeof(*dpy->attributes));
/* We normally want a double buffer */
want_double_buffer = true;
/* Begin with the first attribute */
a = dpy->attributes;
/* First, check the display flags, so we know if we want a fullscreen
* mode or a windowed mode.
*/
if (dpy->parent.flags & ALLEGRO_FULLSCREEN) {
*a = NSOpenGLPFAFullScreen; a++;
// Take over the screen.
*a = NSOpenGLPFAScreenMask; a++;
*a = CGDisplayIDToOpenGLDisplayMask(dpy->display_id); a++;
} else {
*a = NSOpenGLPFAWindow; a++;
}
/* Find the requested colour depth */
if (extras)
dpy->depth = extras->settings[ALLEGRO_COLOR_SIZE];
if (!dpy->depth) { /* Use default */
NSScreen *screen;
int adapter = al_get_new_display_adapter();
if ((adapter >= 0) && (adapter < al_get_num_video_adapters())) {
screen = [[NSScreen screens] objectAtIndex: adapter];
} else {
screen = [NSScreen mainScreen];
}
dpy->depth = NSBitsPerPixelFromDepth([screen depth]);
if (dpy->depth == 24)
dpy->depth = 32;
if (dpy->depth == 15)
dpy->depth = 16;
ALLEGRO_DEBUG("Using default colour depth %d\n", dpy->depth);
}
*a = NSOpenGLPFAColorSize; a++;
*a = dpy->depth; a++;
/* Say we don't need an exact match for the depth of the colour buffer.
* FIXME: right now, this is set whenever nothing is required or
* something is suggested. Probably need finer control over this...
*/
if (!extras || !(extras->required) || extras->suggested) {
*a = NSOpenGLPFAClosestPolicy; a++;
}
/* Should we set double buffering? If it's not required we don't care
* and go with the default.
*/
if (extras && (extras->required & (1 << ALLEGRO_SINGLE_BUFFER)) &&
extras->settings[ALLEGRO_SINGLE_BUFFER]) {
want_double_buffer = false;
dpy->single_buffer = true;
}
if (want_double_buffer) {
*a = NSOpenGLPFADoubleBuffer; a++;
}
/* Detect if multi-sampling is requested */
/* Or "NSOpenGLPFASupersample" ? */
if (extras && (extras->required & sample_flags) &&
(extras->settings[ALLEGRO_SAMPLES]||extras->settings[ALLEGRO_SAMPLE_BUFFERS])) {
*a = NSOpenGLPFAMultisample; a++;
}
/* Now go through all other options, if set */
for (n = 0; n < number_of_settings; n++) {
i = allegro_to_osx_settings[n][0];
if (allegro_to_osx_settings[n][1] && extras &&
((extras->required & (1 << i)) || (extras->suggested & (1 << i)))) {
/* Need to distinguish between boolean attributes and settings that
* require a value.
*/
if (allegro_to_osx_settings[n][2]) { /* Value */
/* We must make sure the value is non-zero because the list are
* building is 0-terminated.
*/
if (extras->settings[i]) {
*a = allegro_to_osx_settings[n][1]; a++;
*a = extras->settings[i]; a++;
ALLEGRO_DEBUG("Passing pixel format attribute %s = %d\n", allegro_pixel_format_names[n], extras->settings[i]);
}
} else if (extras->settings[i]) { /* Boolean, just turn this on */
*a = allegro_to_osx_settings[n][1]; a++;
ALLEGRO_DEBUG("Passing pixel format attribute %s = %d\n", allegro_pixel_format_names[n], extras->settings[i]);
}
}
}
/* Accelerated is always preferred, so we only set this for required not
* for suggested.
*/
if (extras->required & ALLEGRO_RENDER_METHOD) {
*a++ = NSOpenGLPFAAccelerated;
}
}
/* Set the extra_settings[] array in the display, to report which options
* we have selected.
*/
static void osx_get_opengl_pixelformat_attributes(ALLEGRO_DISPLAY_OSX_WIN *dpy)
{
int n;
if (!dpy)
return;
/* Clear list of settings (none selected) */
memset(&dpy->parent.extra_settings, 0, sizeof(dpy->parent.extra_settings));
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
/* Get the pixel format associated with the OpenGL context.
* We use the Carbon API rather than the Cocoa API because that way we
* can use the same code in Windowed mode as in fullscreen mode (we
* don't have an NSOpenGLView in fullscreen mode).
*/
CGLContextObj ctx = [dpy->ctx CGLContextObj];
CGLPixelFormatObj pixel_format = CGLGetPixelFormat(ctx);
GLint screen_id;
CGLGetVirtualScreen(ctx, &screen_id);
ALLEGRO_DEBUG("Screen has ID %d\n", (int)screen_id);
for (n = 0; n < number_of_settings; n++) {
/* Go through the list of options and relist the ones that we have
* set to Allegro's option list.
*/
CGLPixelFormatAttribute attrib = allegro_to_osx_settings[n][1];
/* Skip options that don't exist on OS X */
if (attrib == 0)
continue;
/* Get value for this attribute */
GLint value;
CGLDescribePixelFormat(pixel_format, screen_id, attrib, &value);
int al_setting = allegro_to_osx_settings[n][0];
if (allegro_to_osx_settings[n][2] == 0) /* Boolean attribute */
value = 1;
dpy->parent.extra_settings.settings[al_setting] = value;
ALLEGRO_DEBUG("Pixel format attribute %s set to %d\n", allegro_pixel_format_names[n], value);
}
#else
/* CGLGetPixelFormat does not exist on Tiger, so we need to do something
* else.
* FIXME: right now, we just return the settings that were chosen by the
* user. To be correct, we should query the pixelformat corresponding to
* the display, using the NSOpenGLView pixelFormat method and the
* NSOpenGLPixelFormat getValues:forAttribute:forVirtualScreen: method,
* for which we need to know the logical screen number that the display
* is on. That doesn't work for fullscreen modes though...
*/
NSOpenGLPixelFormatAttribute *attributes = dpy->attributes;
for (n = 0; n < number_of_settings; n++) {
/* Go through the list of options and relist the ones that we have
* set to Allegro's option list.
*/
NSOpenGLPixelFormatAttribute *a = dpy->attributes;
while (*a) {
if (*a == allegro_to_osx_settings[n][1]) {
int al_setting = allegro_to_osx_settings[n][0];
int value = 1;
if (allegro_to_osx_settings[n][2])
value = a[1];
dpy->parent.extra_settings.settings[al_setting] = value;
ALLEGRO_DEBUG("Setting pixel format attribute %d to %d\n", al_setting, value);
}
/* Advance to next option */
if (allegro_to_osx_settings[n][2]) /* Has a parameter in the list */
a++;
a++;
}
}
#endif
dpy->parent.extra_settings.settings[ALLEGRO_COMPATIBLE_DISPLAY] = 1;
// Fill in the missing colour format options, as best we can
ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds = &dpy->parent.extra_settings;
if (eds->settings[ALLEGRO_COLOR_SIZE] == 0) {
eds->settings[ALLEGRO_COLOR_SIZE] = 32;
eds->settings[ALLEGRO_RED_SIZE] = 8;
eds->settings[ALLEGRO_GREEN_SIZE] = 8;
eds->settings[ALLEGRO_BLUE_SIZE] = 8;
eds->settings[ALLEGRO_ALPHA_SIZE] = 8;
eds->settings[ALLEGRO_RED_SHIFT] = 0;
eds->settings[ALLEGRO_GREEN_SHIFT] = 8;
eds->settings[ALLEGRO_BLUE_SHIFT] = 16;
eds->settings[ALLEGRO_ALPHA_SHIFT] = 24;
} else {
int size = eds->settings[ALLEGRO_ALPHA_SIZE];
if (!size) {
switch (eds->settings[ALLEGRO_COLOR_SIZE]) {
case 32:
size = 8;
break;
case 16:
size = 5;
break;
case 8:
size = 8;
break;
}
}
if (!eds->settings[ALLEGRO_RED_SIZE])
eds->settings[ALLEGRO_RED_SIZE] = size;