-
Notifications
You must be signed in to change notification settings - Fork 1
/
WinSystemOSX.mm
1556 lines (1317 loc) · 46.9 KB
/
WinSystemOSX.mm
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
/*
* Copyright (C) 2005-2012 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#if defined(TARGET_DARWIN_OSX)
//hack around problem with xbmc's typedef int BOOL
// and obj-c's typedef unsigned char BOOL
#define BOOL XBMC_BOOL
#include "WinSystemOSX.h"
#include "WinEventsOSX.h"
#include "Application.h"
#include "guilib/DispResource.h"
#include "guilib/GUIWindowManager.h"
#include "settings/Settings.h"
#include "settings/GUISettings.h"
#include "input/KeyboardStat.h"
#include "threads/SingleLock.h"
#include "utils/log.h"
#include "osx/XBMCHelper.h"
#include "utils/SystemInfo.h"
#include "osx/CocoaInterface.h"
#undef BOOL
#import <SDL/SDL_video.h>
#import <SDL/SDL_events.h>
#import <Cocoa/Cocoa.h>
#import <QuartzCore/QuartzCore.h>
#import <IOKit/pwr_mgt/IOPMLib.h>
#import <IOKit/graphics/IOGraphicsLib.h>
// turn off deprecated warning spew.
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
//------------------------------------------------------------------------------------------
// special object-c class for handling the inhibit display NSTimer callback.
@interface windowInhibitScreenSaverClass : NSObject
- (void) updateSystemActivity: (NSTimer*)timer;
@end
@implementation windowInhibitScreenSaverClass
-(void) updateSystemActivity: (NSTimer*)timer
{
UpdateSystemActivity(UsrActivity);
}
@end
//------------------------------------------------------------------------------------------
// special object-c class for handling the NSWindowDidMoveNotification callback.
@interface windowDidMoveNoteClass : NSObject
{
void *m_userdata;
}
+ initWith: (void*) userdata;
- (void) windowDidMoveNotification:(NSNotification*) note;
@end
@implementation windowDidMoveNoteClass
+ initWith: (void*) userdata;
{
windowDidMoveNoteClass *windowDidMove = [windowDidMoveNoteClass new];
windowDidMove->m_userdata = userdata;
return [windowDidMove autorelease];
}
- (void) windowDidMoveNotification:(NSNotification*) note;
{
CWinSystemOSX *winsys = (CWinSystemOSX*)m_userdata;
if (!winsys)
return;
NSOpenGLContext* context = [NSOpenGLContext currentContext];
if (context)
{
if ([context view])
{
NSPoint window_origin = [[[context view] window] frame].origin;
XBMC_Event newEvent;
memset(&newEvent, 0, sizeof(newEvent));
newEvent.type = XBMC_VIDEOMOVE;
newEvent.move.x = window_origin.x;
newEvent.move.y = window_origin.y;
g_application.OnEvent(newEvent);
}
}
}
@end
//------------------------------------------------------------------------------------------
// special object-c class for handling the NSWindowDidReSizeNotification callback.
@interface windowDidReSizeNoteClass : NSObject
{
void *m_userdata;
}
+ initWith: (void*) userdata;
- (void) windowDidReSizeNotification:(NSNotification*) note;
@end
@implementation windowDidReSizeNoteClass
+ initWith: (void*) userdata;
{
windowDidReSizeNoteClass *windowDidReSize = [windowDidReSizeNoteClass new];
windowDidReSize->m_userdata = userdata;
return [windowDidReSize autorelease];
}
- (void) windowDidReSizeNotification:(NSNotification*) note;
{
CWinSystemOSX *winsys = (CWinSystemOSX*)m_userdata;
if (!winsys)
return;
/* placeholder, do not uncomment or you will SDL recurse into death
NSOpenGLContext* context = [NSOpenGLContext currentContext];
if (context)
{
if ([context view])
{
NSSize view_size = [[context view] frame].size;
XBMC_Event newEvent;
memset(&newEvent, 0, sizeof(newEvent));
newEvent.type = XBMC_VIDEORESIZE;
newEvent.resize.w = view_size.width;
newEvent.resize.h = view_size.height;
if (newEvent.resize.w * newEvent.resize.h)
{
g_application.OnEvent(newEvent);
g_windowManager.MarkDirty();
}
}
}
*/
}
@end
//------------------------------------------------------------------------------------------
#define MAX_DISPLAYS 32
static NSWindow* blankingWindows[MAX_DISPLAYS];
void* CWinSystemOSX::m_lastOwnedContext = 0;
//------------------------------------------------------------------------------------------
CRect CGRectToCRect(CGRect cgrect)
{
CRect crect = CRect(
cgrect.origin.x,
cgrect.origin.y,
cgrect.origin.x + cgrect.size.width,
cgrect.origin.y + cgrect.size.height);
return crect;
}
//------------------------------------------------------------------------------------------
Boolean GetDictionaryBoolean(CFDictionaryRef theDict, const void* key)
{
// get a boolean from the dictionary
Boolean value = false;
CFBooleanRef boolRef;
boolRef = (CFBooleanRef)CFDictionaryGetValue(theDict, key);
if (boolRef != NULL)
value = CFBooleanGetValue(boolRef);
return value;
}
//------------------------------------------------------------------------------------------
long GetDictionaryLong(CFDictionaryRef theDict, const void* key)
{
// get a long from the dictionary
long value = 0;
CFNumberRef numRef;
numRef = (CFNumberRef)CFDictionaryGetValue(theDict, key);
if (numRef != NULL)
CFNumberGetValue(numRef, kCFNumberLongType, &value);
return value;
}
//------------------------------------------------------------------------------------------
int GetDictionaryInt(CFDictionaryRef theDict, const void* key)
{
// get a long from the dictionary
int value = 0;
CFNumberRef numRef;
numRef = (CFNumberRef)CFDictionaryGetValue(theDict, key);
if (numRef != NULL)
CFNumberGetValue(numRef, kCFNumberIntType, &value);
return value;
}
//------------------------------------------------------------------------------------------
float GetDictionaryFloat(CFDictionaryRef theDict, const void* key)
{
// get a long from the dictionary
int value = 0;
CFNumberRef numRef;
numRef = (CFNumberRef)CFDictionaryGetValue(theDict, key);
if (numRef != NULL)
CFNumberGetValue(numRef, kCFNumberFloatType, &value);
return value;
}
//------------------------------------------------------------------------------------------
double GetDictionaryDouble(CFDictionaryRef theDict, const void* key)
{
// get a long from the dictionary
double value = 0.0;
CFNumberRef numRef;
numRef = (CFNumberRef)CFDictionaryGetValue(theDict, key);
if (numRef != NULL)
CFNumberGetValue(numRef, kCFNumberDoubleType, &value);
return value;
}
//---------------------------------------------------------------------------------
void SetMenuBarVisible(bool visible)
{
if(visible)
{
[[NSApplication sharedApplication]
setPresentationOptions: NSApplicationPresentationDefault];
}
else
{
[[NSApplication sharedApplication]
setPresentationOptions: NSApplicationPresentationHideMenuBar |
NSApplicationPresentationHideDock];
}
}
//---------------------------------------------------------------------------------
CGDirectDisplayID GetDisplayID(int screen_index)
{
CGDirectDisplayID displayArray[MAX_DISPLAYS];
CGDisplayCount numDisplays;
// Get the list of displays.
CGGetActiveDisplayList(MAX_DISPLAYS, displayArray, &numDisplays);
return(displayArray[screen_index]);
}
CGDirectDisplayID GetDisplayIDFromScreen(NSScreen *screen)
{
NSDictionary* screenInfo = [screen deviceDescription];
NSNumber* screenID = [screenInfo objectForKey:@"NSScreenNumber"];
return (CGDirectDisplayID)[screenID longValue];
}
int GetDisplayIndex(CGDirectDisplayID display)
{
CGDirectDisplayID displayArray[MAX_DISPLAYS];
CGDisplayCount numDisplays;
// Get the list of displays.
CGGetActiveDisplayList(MAX_DISPLAYS, displayArray, &numDisplays);
while (numDisplays > 0)
{
if (display == displayArray[--numDisplays])
return numDisplays;
}
return -1;
}
void BlankOtherDisplays(int screen_index)
{
int i;
int numDisplays = [[NSScreen screens] count];
// zero out blankingWindows for debugging
for (i=0; i<MAX_DISPLAYS; i++)
{
blankingWindows[i] = 0;
}
// Blank.
for (i=0; i<numDisplays; i++)
{
if (i != screen_index)
{
// Get the size.
NSScreen* pScreen = [[NSScreen screens] objectAtIndex:i];
NSRect screenRect = [pScreen frame];
// Build a blanking window.
screenRect.origin = NSZeroPoint;
blankingWindows[i] = [[NSWindow alloc] initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO
screen:pScreen];
[blankingWindows[i] setBackgroundColor:[NSColor blackColor]];
[blankingWindows[i] setLevel:CGShieldingWindowLevel()];
[blankingWindows[i] makeKeyAndOrderFront:nil];
}
}
}
void UnblankDisplays(void)
{
int numDisplays = [[NSScreen screens] count];
int i = 0;
for (i=0; i<numDisplays; i++)
{
if (blankingWindows[i] != 0)
{
// Get rid of the blanking windows we created.
[blankingWindows[i] close];
if ([blankingWindows[i] isReleasedWhenClosed] == NO)
[blankingWindows[i] release];
blankingWindows[i] = 0;
}
}
}
CGDisplayFadeReservationToken DisplayFadeToBlack(bool fade)
{
// Fade to black to hide resolution-switching flicker and garbage.
CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken;
if (CGAcquireDisplayFadeReservation (5, &fade_token) == kCGErrorSuccess && fade)
CGDisplayFade(fade_token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE);
return(fade_token);
}
void DisplayFadeFromBlack(CGDisplayFadeReservationToken fade_token, bool fade)
{
if (fade_token != kCGDisplayFadeReservationInvalidToken)
{
if (fade)
CGDisplayFade(fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
CGReleaseDisplayFadeReservation(fade_token);
}
}
NSString* screenNameForDisplay(CGDirectDisplayID displayID)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString *screenName = nil;
NSDictionary *deviceInfo = (NSDictionary *)IODisplayCreateInfoDictionary(CGDisplayIOServicePort(displayID), kIODisplayOnlyPreferredName);
NSDictionary *localizedNames = [deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]];
if ([localizedNames count] > 0) {
screenName = [[localizedNames objectForKey:[[localizedNames allKeys] objectAtIndex:0]] retain];
}
[deviceInfo release];
[pool release];
return [screenName autorelease];
}
void ShowHideNSWindow(NSWindow *wind, bool show)
{
if (show)
[wind orderFront:nil];
else
[wind orderOut:nil];
}
static NSWindow *curtainWindow;
void fadeInDisplay(NSScreen *theScreen, double fadeTime)
{
int fadeSteps = 100;
double fadeInterval = (fadeTime / (double) fadeSteps);
if (curtainWindow != nil)
{
for (int step = 0; step < fadeSteps; step++)
{
double fade = 1.0 - (step * fadeInterval);
[curtainWindow setAlphaValue:fade];
NSDate *nextDate = [NSDate dateWithTimeIntervalSinceNow:fadeInterval];
[[NSRunLoop currentRunLoop] runUntilDate:nextDate];
}
}
[curtainWindow close];
curtainWindow = nil;
[NSCursor unhide];
}
void fadeOutDisplay(NSScreen *theScreen, double fadeTime)
{
int fadeSteps = 100;
double fadeInterval = (fadeTime / (double) fadeSteps);
[NSCursor hide];
curtainWindow = [[NSWindow alloc]
initWithContentRect:[theScreen frame]
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:YES
screen:theScreen];
[curtainWindow setAlphaValue:0.0];
[curtainWindow setBackgroundColor:[NSColor blackColor]];
[curtainWindow setLevel:NSScreenSaverWindowLevel];
[curtainWindow makeKeyAndOrderFront:nil];
[curtainWindow setFrame:[curtainWindow
frameRectForContentRect:[theScreen frame]]
display:YES
animate:NO];
for (int step = 0; step < fadeSteps; step++)
{
double fade = step * fadeInterval;
[curtainWindow setAlphaValue:fade];
NSDate *nextDate = [NSDate dateWithTimeIntervalSinceNow:fadeInterval];
[[NSRunLoop currentRunLoop] runUntilDate:nextDate];
}
}
// try to find mode that matches the desired size, refreshrate
// non interlaced, nonstretched, safe for hardware
CFDictionaryRef GetMode(int width, int height, double refreshrate, int screenIdx)
{
if ( screenIdx >= (signed)[[NSScreen screens] count])
return NULL;
Boolean stretched;
Boolean interlaced;
Boolean safeForHardware;
Boolean televisionoutput;
int w, h, bitsperpixel;
double rate;
RESOLUTION_INFO res;
CLog::Log(LOGDEBUG, "GetMode looking for suitable mode with %d x %d @ %f Hz on display %d\n", width, height, refreshrate, screenIdx);
CFArrayRef displayModes = CGDisplayAvailableModes(GetDisplayID(screenIdx));
if (NULL == displayModes)
{
CLog::Log(LOGERROR, "GetMode - no displaymodes found!");
return NULL;
}
for (int i=0; i < CFArrayGetCount(displayModes); ++i)
{
CFDictionaryRef displayMode = (CFDictionaryRef)CFArrayGetValueAtIndex(displayModes, i);
stretched = GetDictionaryBoolean(displayMode, kCGDisplayModeIsStretched);
interlaced = GetDictionaryBoolean(displayMode, kCGDisplayModeIsInterlaced);
bitsperpixel = GetDictionaryInt(displayMode, kCGDisplayBitsPerPixel);
safeForHardware = GetDictionaryBoolean(displayMode, kCGDisplayModeIsSafeForHardware);
televisionoutput = GetDictionaryBoolean(displayMode, kCGDisplayModeIsTelevisionOutput);
w = GetDictionaryInt(displayMode, kCGDisplayWidth);
h = GetDictionaryInt(displayMode, kCGDisplayHeight);
rate = GetDictionaryDouble(displayMode, kCGDisplayRefreshRate);
if ((bitsperpixel == 32) &&
(safeForHardware == YES) &&
(stretched == NO) &&
(interlaced == NO) &&
(w == width) &&
(h == height) &&
(rate == refreshrate || rate == 0))
{
CLog::Log(LOGDEBUG, "GetMode found a match!");
return displayMode;
}
}
CLog::Log(LOGERROR, "GetMode - no match found!");
return NULL;
}
//---------------------------------------------------------------------------------
static void DisplayReconfigured(CGDirectDisplayID display,
CGDisplayChangeSummaryFlags flags, void* userData)
{
CWinSystemOSX *winsys = (CWinSystemOSX*)userData;
if (!winsys)
return;
if (flags & kCGDisplaySetModeFlag || flags & kCGDisplayBeginConfigurationFlag)
{
// pre/post-reconfiguration changes
RESOLUTION res = g_graphicsContext.GetVideoResolution();
if (res == RES_INVALID)
return;
NSScreen* pScreen = nil;
unsigned int screenIdx = g_settings.m_ResInfo[res].iScreen;
if ( screenIdx < [[NSScreen screens] count] )
{
pScreen = [[NSScreen screens] objectAtIndex:screenIdx];
}
if (pScreen)
{
CGDirectDisplayID xbmc_display = GetDisplayIDFromScreen(pScreen);
if (xbmc_display == display)
{
// we only respond to changes on the display we are running on.
CLog::Log(LOGDEBUG, "CWinSystemOSX::DisplayReconfigured");
winsys->CheckDisplayChanging(flags);
}
}
}
}
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
CWinSystemOSX::CWinSystemOSX() : CWinSystemBase()
{
m_eWindowSystem = WINDOW_SYSTEM_OSX;
m_glContext = 0;
m_SDLSurface = NULL;
m_osx_events = NULL;
m_obscured = false;
m_obscured_timecheck = XbmcThreads::SystemClockMillis() + 1000;
m_use_system_screensaver = true;
// check runtime, we only allow this on 10.5+
m_can_display_switch = (floor(NSAppKitVersionNumber) >= 949);
}
CWinSystemOSX::~CWinSystemOSX()
{
};
bool CWinSystemOSX::InitWindowSystem()
{
SDL_EnableUNICODE(1);
// set repeat to 10ms to ensure repeat time < frame time
// so that hold times can be reliably detected
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, 10);
if (!CWinSystemBase::InitWindowSystem())
return false;
m_osx_events = new CWinEventsOSX();
if (m_can_display_switch)
CGDisplayRegisterReconfigurationCallback(DisplayReconfigured, (void*)this);
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
windowDidMoveNoteClass *windowDidMove;
windowDidMove = [windowDidMoveNoteClass initWith: this];
[center addObserver:windowDidMove
selector:@selector(windowDidMoveNotification:)
name:NSWindowDidMoveNotification object:nil];
m_windowDidMove = windowDidMove;
windowDidReSizeNoteClass *windowDidReSize;
windowDidReSize = [windowDidReSizeNoteClass initWith: this];
[center addObserver:windowDidReSize
selector:@selector(windowDidReSizeNotification:)
name:NSWindowDidResizeNotification object:nil];
m_windowDidReSize = windowDidReSize;
return true;
}
bool CWinSystemOSX::DestroyWindowSystem()
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:(windowDidMoveNoteClass*)m_windowDidMove name:NSWindowDidMoveNotification object:nil];
[center removeObserver:(windowDidReSizeNoteClass*)m_windowDidReSize name:NSWindowDidResizeNotification object:nil];
if (m_can_display_switch)
CGDisplayRemoveReconfigurationCallback(DisplayReconfigured, (void*)this);
delete m_osx_events;
m_osx_events = NULL;
UnblankDisplays();
if (m_glContext)
{
NSOpenGLContext* oldContext = (NSOpenGLContext*)m_glContext;
[oldContext release];
m_glContext = NULL;
}
return true;
}
bool CWinSystemOSX::CreateNewWindow(const CStdString& name, bool fullScreen, RESOLUTION_INFO& res, PHANDLE_EVENT_FUNC userFunction)
{
m_nWidth = res.iWidth;
m_nHeight = res.iHeight;
m_bFullScreen = fullScreen;
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Enable vertical sync to avoid any tearing.
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
m_SDLSurface = SDL_SetVideoMode(m_nWidth, m_nHeight, 0, SDL_OPENGL | SDL_RESIZABLE);
if (!m_SDLSurface)
return false;
// the context SDL creates isn't full screen compatible, so we create new one
// first, find the current contect and make sure a view is attached
NSOpenGLContext* cur_context = [NSOpenGLContext currentContext];
NSView* view = [cur_context view];
if (!view)
return false;
// if we are not starting up windowed, then hide the initial SDL window
// so we do not see it flash before the fade-out and switch to fullscreen.
if (g_guiSettings.m_LookAndFeelResolution != RES_WINDOW)
ShowHideNSWindow([view window], false);
// disassociate view from context
[cur_context clearDrawable];
// release the context
if (m_lastOwnedContext == cur_context)
{
[ NSOpenGLContext clearCurrentContext ];
[ cur_context clearDrawable ];
[ cur_context release ];
}
// create a new context
NSOpenGLContext* new_context = (NSOpenGLContext*)CreateWindowedContext(nil);
if (!new_context)
return false;
// associate with current view
[new_context setView:view];
[new_context makeCurrentContext];
// set the window title
NSString *string;
string = [ [ NSString alloc ] initWithUTF8String:"XBMC Media Center" ];
[ [ [new_context view] window] setTitle:string ];
[ string release ];
m_glContext = new_context;
m_lastOwnedContext = new_context;
m_bWindowCreated = true;
return true;
}
bool CWinSystemOSX::DestroyWindow()
{
return true;
}
extern "C" void SDL_SetWidthHeight(int w, int h);
bool CWinSystemOSX::ResizeWindow(int newWidth, int newHeight, int newLeft, int newTop)
{
if (!m_glContext)
return false;
NSOpenGLContext* context = [NSOpenGLContext currentContext];
NSView* view;
NSWindow* window;
view = [context view];
if (view && (newWidth > 0) && (newHeight > 0))
{
window = [view window];
if (window)
{
[window setContentSize:NSMakeSize(newWidth, newHeight)];
[window update];
[view setFrameSize:NSMakeSize(newWidth, newHeight)];
[context update];
}
}
// HACK: resize SDL's view manually so that mouse bounds are correctly updated.
// there are two parts to this, the internal SDL (current_video->screen) and
// the cocoa view ( handled in SetFullScreen).
SDL_SetWidthHeight(newWidth, newHeight);
[context makeCurrentContext];
m_nWidth = newWidth;
m_nHeight = newHeight;
m_glContext = context;
return true;
}
static bool needtoshowme = true;
bool CWinSystemOSX::SetFullScreen(bool fullScreen, RESOLUTION_INFO& res, bool blankOtherDisplays)
{
static NSWindow* windowedFullScreenwindow = NULL;
static NSScreen* last_window_screen = NULL;
static NSPoint last_window_origin;
static NSView* last_view = NULL;
static NSSize last_view_size;
static NSPoint last_view_origin;
bool was_fullscreen = m_bFullScreen;
static int lastDisplayNr = res.iScreen;
NSOpenGLContext* cur_context;
// Fade to black to hide resolution-switching flicker and garbage.
CGDisplayFadeReservationToken fade_token = DisplayFadeToBlack(needtoshowme);
// If we're already fullscreen then we must be moving to a different display.
// or if we are still on the same display - it might be only a refreshrate/resolution
// change request.
// Recurse to reset fullscreen mode and then continue.
if (was_fullscreen && fullScreen && lastDisplayNr != res.iScreen)
{
needtoshowme = false;
ShowHideNSWindow([last_view window], needtoshowme);
RESOLUTION_INFO& window = g_settings.m_ResInfo[RES_WINDOW];
CWinSystemOSX::SetFullScreen(false, window, blankOtherDisplays);
needtoshowme = true;
}
m_nWidth = res.iWidth;
m_nHeight = res.iHeight;
m_bFullScreen = fullScreen;
cur_context = [NSOpenGLContext currentContext];
//handle resolution/refreshrate switching early here
if (m_bFullScreen)
{
if (m_can_display_switch)
{
// send pre-configuration change now and do not
// wait for switch videomode callback. This gives just
// a little more advanced notice of the display pre-change.
if (g_guiSettings.GetInt("videoplayer.adjustrefreshrate") != ADJUST_REFRESHRATE_OFF)
CheckDisplayChanging(kCGDisplayBeginConfigurationFlag);
// switch videomode
SwitchToVideoMode(res.iWidth, res.iHeight, res.fRefreshRate, res.iScreen);
lastDisplayNr = res.iScreen;
}
}
//no context? done.
if (!cur_context)
{
DisplayFadeFromBlack(fade_token, needtoshowme);
return false;
}
if (windowedFullScreenwindow != NULL)
{
[windowedFullScreenwindow close];
if ([windowedFullScreenwindow isReleasedWhenClosed] == NO)
[windowedFullScreenwindow release];
windowedFullScreenwindow = NULL;
}
if (m_bFullScreen)
{
// FullScreen Mode
NSOpenGLContext* newContext = NULL;
// Save info about the windowed context so we can restore it when returning to windowed.
last_view = [cur_context view];
last_view_size = [last_view frame].size;
last_view_origin = [last_view frame].origin;
last_window_screen = [[last_view window] screen];
last_window_origin = [[last_view window] frame].origin;
if (g_guiSettings.GetBool("videoscreen.fakefullscreen"))
{
// This is Cocca Windowed FullScreen Mode
// Get the screen rect of our current display
NSScreen* pScreen = [[NSScreen screens] objectAtIndex:res.iScreen];
NSRect screenRect = [pScreen frame];
// remove frame origin offset of orginal display
screenRect.origin = NSZeroPoint;
// make a new window to act as the windowedFullScreen
windowedFullScreenwindow = [[NSWindow alloc] initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO
screen:pScreen];
[windowedFullScreenwindow setBackgroundColor:[NSColor blackColor]];
[windowedFullScreenwindow makeKeyAndOrderFront:nil];
// make our window the same level as the rest to enable cmd+tab switching
[windowedFullScreenwindow setLevel:NSNormalWindowLevel];
// this will make our window topmost and hide all system messages
//[windowedFullScreenwindow setLevel:CGShieldingWindowLevel()];
// ...and the original one beneath it and on the same screen.
[[last_view window] setLevel:NSNormalWindowLevel-1];
[[last_view window] setFrameOrigin:[pScreen frame].origin];
// expand the mouse bounds in SDL view to fullscreen
[ last_view setFrameOrigin:NSMakePoint(0.0, 0.0)];
[ last_view setFrameSize:NSMakeSize(m_nWidth, m_nHeight) ];
NSView* blankView = [[NSView alloc] init];
[windowedFullScreenwindow setContentView:blankView];
[windowedFullScreenwindow setContentSize:NSMakeSize(m_nWidth, m_nHeight)];
[windowedFullScreenwindow update];
[blankView setFrameSize:NSMakeSize(m_nWidth, m_nHeight)];
// Obtain windowed pixel format and create a new context.
newContext = (NSOpenGLContext*)CreateWindowedContext((void* )cur_context);
[newContext setView:blankView];
// Hide the menu bar.
if (GetDisplayID(res.iScreen) == kCGDirectMainDisplay)
SetMenuBarVisible(false);
// Blank other displays if requested.
if (blankOtherDisplays)
BlankOtherDisplays(res.iScreen);
}
else
{
// hide the window
[[last_view window] setFrameOrigin:[last_window_screen frame].origin];
// expand the mouse bounds in SDL view to fullscreen
[ last_view setFrameOrigin:NSMakePoint(0.0, 0.0)];
[ last_view setFrameSize:NSMakeSize(m_nWidth, m_nHeight) ];
// This is OpenGL FullScreen Mode
// create our new context (sharing with the current one)
newContext = (NSOpenGLContext*)CreateFullScreenContext(res.iScreen, (void*)cur_context);
if (!newContext)
return false;
// clear the current context
[NSOpenGLContext clearCurrentContext];
// set fullscreen
[newContext setFullScreen];
// Capture the display before going fullscreen.
if (blankOtherDisplays == true)
CGCaptureAllDisplays();
else
CGDisplayCapture(GetDisplayID(res.iScreen));
// If we don't hide menu bar, it will get events and interrupt the program.
if (GetDisplayID(res.iScreen) == kCGDirectMainDisplay)
SetMenuBarVisible(false);
}
// Hide the mouse.
[NSCursor hide];
// Release old context if we created it.
if (m_lastOwnedContext == cur_context)
{
[ NSOpenGLContext clearCurrentContext ];
[ cur_context clearDrawable ];
[ cur_context release ];
}
// activate context
[newContext makeCurrentContext];
m_lastOwnedContext = newContext;
}
else
{
// Windowed Mode
// exit fullscreen
[cur_context clearDrawable];
[NSCursor unhide];
// Show menubar.
if (GetDisplayID(res.iScreen) == kCGDirectMainDisplay)
SetMenuBarVisible(true);
if (g_guiSettings.GetBool("videoscreen.fakefullscreen"))
{
// restore the windowed window level
[[last_view window] setLevel:NSNormalWindowLevel];
// Get rid of the new window we created.
if (windowedFullScreenwindow != NULL)
{
[windowedFullScreenwindow close];
if ([windowedFullScreenwindow isReleasedWhenClosed] == NO)
[windowedFullScreenwindow release];
windowedFullScreenwindow = NULL;
}
// Unblank.
// Force the unblank when returning from fullscreen, we get called with blankOtherDisplays set false.
//if (blankOtherDisplays)
UnblankDisplays();
}
else
{
// release displays
CGReleaseAllDisplays();
}
// create our new context (sharing with the current one)
NSOpenGLContext* newContext = (NSOpenGLContext*)CreateWindowedContext((void* )cur_context);
if (!newContext)
return false;
// Assign view from old context, move back to original screen.
[newContext setView:last_view];
[[last_view window] setFrameOrigin:last_window_origin];
// return the mouse bounds in SDL view to prevous size
[ last_view setFrameSize:last_view_size ];
[ last_view setFrameOrigin:last_view_origin ];
// done with restoring windowed window, don't set last_view to NULL as we can lose it under dual displays.
//last_window_screen = NULL;
// Release the fullscreen context.
if (m_lastOwnedContext == cur_context)
{
[ NSOpenGLContext clearCurrentContext ];
[ cur_context clearDrawable ];
[ cur_context release ];
}
// Activate context.
[newContext makeCurrentContext];
m_lastOwnedContext = newContext;
}
DisplayFadeFromBlack(fade_token, needtoshowme);
ShowHideNSWindow([last_view window], needtoshowme);
// need to make sure SDL tracks any window size changes
ResizeWindow(m_nWidth, m_nHeight, -1, -1);
return true;
}
void CWinSystemOSX::UpdateResolutions()
{
CWinSystemBase::UpdateResolutions();
// Add desktop resolution
int w, h;
double fps;
// first screen goes into the current desktop mode
GetScreenResolution(&w, &h, &fps, 0);
UpdateDesktopResolution(g_settings.m_ResInfo[RES_DESKTOP], 0, w, h, fps);
// see resolution.h enum RESOLUTION for how the resolutions
// have to appear in the g_settings.m_ResInfo vector
// add the desktop resolutions of the other screens
for(int i = 1; i < GetNumScreens(); i++)
{
RESOLUTION_INFO res;
// get current resolution of screen i
GetScreenResolution(&w, &h, &fps, i);
UpdateDesktopResolution(res, i, w, h, fps);
g_settings.m_ResInfo.push_back(res);
}
if (m_can_display_switch)
{
// now just fill in the possible reolutions for the attached screens
// and push to the m_ResInfo vector
FillInVideoModes();
}
}
/*
void* Cocoa_GL_CreateContext(void* pixFmt, void* shareCtx)
{
if (!pixFmt)
return nil;
NSOpenGLContext* newContext = [[NSOpenGLContext alloc] initWithFormat:(NSOpenGLPixelFormat*)pixFmt
shareContext:(NSOpenGLContext*)shareCtx];
// snipit from SDL_cocoaopengl.m
//
// Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
// "You are blowing a couple of the internal OpenGL function caches. This
// appears to be happening in the VAO case. You can tell OpenGL to up
// the cache size by issuing the following calls right after you create
// the OpenGL context. The default cache size is 16." --ryan.
//
#ifndef GLI_ARRAY_FUNC_CACHE_MAX