-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathvideo_macosx.mm
More file actions
1037 lines (840 loc) · 25.1 KB
/
video_macosx.mm
File metadata and controls
1037 lines (840 loc) · 25.1 KB
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
/*
* $Id$
*
* video_macosx.mm - Interface between Basilisk II and Cocoa windowing.
* Based on video_x.cpp
*
* Basilisk II (C) 1997-2008 Christian Bauer
*
* 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 of the License, 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "sysdeps.h"
#ifdef HAVE_PTHREADS
# include <pthread.h>
#endif
#include <adb.h>
#include <cpu_emulation.h>
#include <main.h>
#include "macos_util_macosx.h"
#include <prefs.h>
#include <user_strings.h>
#include "video_macosx.h"
#define DEBUG 0
#define VERBOSE 0
#include "debug.h"
#ifdef NSBITMAP
#import <AppKit/NSBitmapImageRep.h>
#endif
#import <Foundation/NSString.h> // Needed for NSLog(@"")
#import "misc_macosx.h" // WarningSheet() prototype
// Global variables
uint8 display_type = DISPLAY_WINDOW, // These are used by PrefsEditor
frame_skip;
uint16 init_width = MIN_WIDTH, // as well as this code
init_height = MIN_HEIGHT,
init_depth = 32;
EmulatorView *output = nil; // Set by [EmulatorView init]
NSWindow *the_win = nil; // Set by [Emulator awakeFromNib]
static BOOL singleDisplay = YES;
/*
* Utility functions
*/
static uint8
bits_from_depth(const video_depth depth)
{
int bits = 1 << depth;
// if (bits == 16)
// bits = 15;
// else if (bits == 32)
// bits = 24;
return bits;
}
static const char *
colours_from_depth(const video_depth depth)
{
switch ( depth )
{
case VDEPTH_1BIT : return "Monochrome";
case VDEPTH_2BIT : return "4 colours";
case VDEPTH_4BIT : return "16 colours";
case VDEPTH_8BIT : return "256 colours";
case VDEPTH_16BIT: return "Thousands of colours";
case VDEPTH_32BIT: return "Millions of colours";
}
return "illegal colour depth";
}
static const char *
colours_from_depth(const uint16 depth)
{
return colours_from_depth(DepthModeForPixelDepth(depth) );
}
bool
parse_screen_prefs(const char *mode_str)
{
if ( ! mode_str )
{
// No screen pref was found. Supply a default:
mode_str = "win/512/384";
}
if (sscanf(mode_str, "win/%hd/%hd/%hd",
&init_width, &init_height, &init_depth) == 3)
display_type = DISPLAY_WINDOW;
else if (sscanf(mode_str, "win/%hd/%hd", &init_width, &init_height) == 2)
display_type = DISPLAY_WINDOW;
else if (strcmp(mode_str, "full") == 0)
display_type = DISPLAY_SCREEN;
else if (sscanf(mode_str, "full/%hd/%hd/%hd",
&init_width, &init_height, &init_depth) == 3)
display_type = DISPLAY_SCREEN;
else if (sscanf(mode_str, "full/%hd/%hd", &init_width, &init_height) == 2)
display_type = DISPLAY_SCREEN;
else if (sscanf(mode_str, "opengl/%hd/%hd/%hd",
&init_width, &init_height, &init_depth) == 3)
display_type = DISPLAY_OPENGL;
else if (sscanf(mode_str, "opengl/%hd/%hd", &init_width, &init_height) == 2)
display_type = DISPLAY_OPENGL;
else return false;
return true;
}
// Supported video modes
static vector<video_mode> VideoModes;
// Add mode to list of supported modes
static void
add_mode(const uint16 width, const uint16 height,
const uint32 resolution_id, const uint32 bytes_per_row,
const uint32 user_data,
const video_depth depth)
{
vector<video_mode>::const_iterator i,
end = VideoModes.end();
for (i = VideoModes.begin(); i != end; ++i)
if ( i->x == width && i->y == height &&
i->bytes_per_row == bytes_per_row && i->depth == depth )
{
D(NSLog(@"Duplicate mode (%hdx%hdx%ld, ID %02x, new ID %02x)\n",
width, height, depth, i->resolution_id, resolution_id));
return;
}
video_mode mode;
mode.x = width;
mode.y = height;
mode.resolution_id = resolution_id;
mode.bytes_per_row = bytes_per_row;
mode.user_data = user_data;
mode.depth = depth;
D(bug("Added video mode: w=%d h=%d d=%d(%d bits)\n",
width, height, depth, bits_from_depth(depth) ));
VideoModes.push_back(mode);
}
// Add standard list of windowed modes for given color depth
static void add_standard_modes(const video_depth depth)
{
D(bug("add_standard_modes: depth=%d(%d bits)\n",
depth, bits_from_depth(depth) ));
add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), 0, depth);
add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), 0, depth);
add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), 0, depth);
add_mode(832, 624, 0x83, TrivialBytesPerRow(832, depth), 0, depth);
add_mode(1024, 768, 0x84, TrivialBytesPerRow(1024, depth), 0, depth);
add_mode(1152, 768, 0x85, TrivialBytesPerRow(1152, depth), 0, depth);
add_mode(1152, 870, 0x86, TrivialBytesPerRow(1152, depth), 0, depth);
add_mode(1280, 1024, 0x87, TrivialBytesPerRow(1280, depth), 0, depth);
add_mode(1600, 1200, 0x88, TrivialBytesPerRow(1600, depth), 0, depth);
}
// Helper function to get a 32bit int from a dictionary
static int32 getCFint32 (CFDictionaryRef dict, CFStringRef key)
{
CFNumberRef ref = (CFNumberRef) CFDictionaryGetValue(dict, key);
if ( ref )
{
int32 val;
if ( CFNumberGetValue(ref, kCFNumberSInt32Type, &val) )
return val;
else
NSLog(@"getCFint32() - Failed to get the value %@", key);
}
else
NSLog(@"getCFint32() - Failed to get a 32bit int for %@", key);
return 0;
}
// Nasty hack. Under 10.1, CGDisplayAvailableModes() does not provide bytes per row,
// and the emulator doesn't like setting the bytes per row after the screen,
// so we use a lot of magic numbers here.
// This will probably fail on some video hardware.
// I have tested on my G4 PowerBook 400 and G3 PowerBook Series 292
static int
CGBytesPerRow(const uint16 width, const video_depth depth)
{
if ( depth == VDEPTH_8BIT )
switch ( width )
{
case 640:
case 720: return 768;
case 800:
case 896: return 1024;
case 1152: return 1280;
}
if ( width == 720 && depth == VDEPTH_16BIT) return 1536;
if ( width == 720 && depth == VDEPTH_32BIT) return 3072;
if ( width == 800 && depth == VDEPTH_16BIT) return 1792;
if ( width == 800 && depth == VDEPTH_32BIT) return 3328;
return TrivialBytesPerRow(width, depth);
}
static bool add_CGDirectDisplay_modes()
{
#define kMaxDisplays 8
CGDirectDisplayID displays[kMaxDisplays];
CGDisplayErr err;
CGDisplayCount n;
int32 oldRes = 0,
res_id = 0x80;
err = CGGetActiveDisplayList(kMaxDisplays, displays, &n);
if ( err != CGDisplayNoErr )
n = 1, displays[n] = kCGDirectMainDisplay;
if ( n > 1 )
singleDisplay = NO;
for ( CGDisplayCount dc = 0; dc < n; ++dc )
{
CGDirectDisplayID d = displays[dc];
CFArrayRef m = CGDisplayAvailableModes(d);
if ( ! m ) // Store the current display mode
add_mode(CGDisplayPixelsWide(d),
CGDisplayPixelsHigh(d),
res_id++, CGDisplayBytesPerRow(d),
(const uint32) d,
DepthModeForPixelDepth(CGDisplayBitsPerPixel(d)));
else
{
CFIndex nModes = CFArrayGetCount(m);
for ( CFIndex mc = 0; mc < nModes; ++mc )
{
CFDictionaryRef modeSpec = (CFDictionaryRef)
CFArrayGetValueAtIndex(m, mc);
int32 bpp = getCFint32(modeSpec, kCGDisplayBitsPerPixel);
int32 height = getCFint32(modeSpec, kCGDisplayHeight);
int32 width = getCFint32(modeSpec, kCGDisplayWidth);
#ifdef MAC_OS_X_VERSION_10_2
int32 bytes = getCFint32(modeSpec, kCGDisplayBytesPerRow);
#else
int32 bytes = 0;
#endif
video_depth depth = DepthModeForPixelDepth(bpp);
if ( ! bpp || ! height || ! width )
{
NSLog(@"Could not get details of mode %d, display %d",
mc, dc);
return false;
}
#if VERBOSE
else
NSLog(@"Display %ld, spec = %@", d, modeSpec);
#endif
if ( ! bytes )
{
NSLog(@"Could not get bytes per row, guessing");
bytes = CGBytesPerRow(width, depth);
}
if ( ! oldRes )
oldRes = width * height;
else
if ( oldRes != width * height )
{
oldRes = width * height;
++res_id;
}
add_mode(width, height, res_id, bytes, (const uint32) d, depth);
}
}
}
return true;
}
#ifdef CG_USE_ALPHA
// memset() by long instead of byte
static void memsetl (long *buffer, long pattern, size_t length)
{
long *buf = (long *) buffer,
*end = buf + length/4;
while ( ++buf < end )
*buf = pattern;
}
// Sets the alpha channel in a image to full on, except for the corners
static void mask_buffer (void *buffer, size_t width, size_t size)
{
long *bufl = (long *) buffer;
char *bufc = (char *) buffer;
memsetl(bufl, 0xFF000000, size);
// Round upper-left corner
*bufl = 0, *bufc+4 = 0; // XXXXX
bufc += width, *bufc++ = 0, *bufc++ = 0, *bufc++ = 0; // XXX
bufc += width, *bufc++ = 0, *bufc = 0; // XX
bufc += width, *bufc = 0; // X
bufc += width, *bufc = 0; // X
NSLog(@"Masked buffer");
}
#endif
// monitor_desc subclass for Mac OS X displays
class OSX_monitor : public monitor_desc
{
public:
OSX_monitor(const vector<video_mode> &available_modes,
video_depth default_depth,
uint32 default_id);
virtual void set_palette(uint8 *pal, int num);
virtual void switch_to_current_mode(void);
void set_mac_frame_buffer(const video_mode mode);
void video_close(void);
bool video_open (const video_mode &mode);
private:
bool init_opengl(const video_mode &mode);
bool init_screen( video_mode &mode);
bool init_window(const video_mode &mode);
CGColorSpaceRef colourSpace;
uint8 *colourTable;
CGImageRef imageRef;
CGDataProviderRef provider;
short x, y, bpp, depth, bpr;
void *the_buffer;
// These record changes we made in setting full screen mode,
// so that we can set the display back as it was again.
CGDirectDisplayID theDisplay;
CFDictionaryRef originalMode,
newMode;
};
OSX_monitor :: OSX_monitor (const vector<video_mode> &available_modes,
video_depth default_depth,
uint32 default_id)
: monitor_desc (available_modes, default_depth, default_id)
{
colourSpace = nil;
colourTable = (uint8 *) malloc(256 * 3);
imageRef = nil;
provider = nil;
newMode = originalMode = nil;
the_buffer = NULL;
theDisplay = nil;
};
// Should also have a destructor which does
// free(colourTable);
// Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
void
OSX_monitor::set_mac_frame_buffer(const video_mode mode)
{
#if !DIRECT_ADDRESSING
set_mac_frame_base(MacFrameBaseMac);
// Set variables used by UAE memory banking
MacFrameLayout = FLAYOUT_DIRECT;
MacFrameBaseHost = (uint8 *) the_buffer;
MacFrameSize = mode.bytes_per_row * mode.y;
InitFrameBufferMapping();
#else
set_mac_frame_base((unsigned int)Host2MacAddr((uint8 *)the_buffer));
#endif
D(bug("mac_frame_base = %08x\n", get_mac_frame_base()));
}
static void
resizeWinBy(const short deltaX, const short deltaY)
{
NSRect rect = [the_win frame];
D(bug("resizeWinBy(%d,%d) - ", deltaX, deltaY));
D(bug("old x=%g, y=%g", rect.size.width, rect.size.height));
rect.size.width += deltaX;
rect.size.height += deltaY;
D(bug(", new x=%g, y=%g\n", rect.size.width, rect.size.height));
[the_win setFrame: rect display: YES animate: YES];
[the_win center];
rect = [the_win frame];
}
void resizeWinTo(const uint16 newWidth, const uint16 newHeight)
{
int deltaX = newWidth - [output width],
deltaY = newHeight - [output height];
D(bug("resizeWinTo(%d,%d)\n", newWidth, newHeight));
if ( deltaX || deltaY )
resizeWinBy(deltaX, deltaY);
}
// Open window
bool
OSX_monitor::init_window(const video_mode &mode)
{
D(bug("init_window: depth=%d(%d bits)\n",
mode.depth, bits_from_depth(mode.depth) ));
// Set absolute mouse mode
ADBSetRelMouseMode(false);
// Is the window open?
if ( ! the_win )
{
ErrorAlert(STR_OPEN_WINDOW_ERR);
return false;
}
resizeWinTo(mode.x, mode.y);
// Create frame buffer ("height + 2" for safety)
int the_buffer_size = mode.bytes_per_row * (mode.y + 2);
the_buffer = calloc(the_buffer_size, 1);
if ( ! the_buffer )
{
NSLog(@"calloc(%d) failed", the_buffer_size);
ErrorAlert(STR_NO_MEM_ERR);
return false;
}
D(bug("the_buffer = %p\n", the_buffer));
unsigned char *offsetBuffer = (unsigned char *) the_buffer;
offsetBuffer += 1; // OS X NSBitmaps are RGBA, but Basilisk generates ARGB
switch ( mode.depth )
{
case VDEPTH_1BIT: bpp = 1; break;
case VDEPTH_2BIT: bpp = 2; break;
case VDEPTH_4BIT: bpp = 4; break;
case VDEPTH_8BIT: bpp = 8; break;
case VDEPTH_16BIT: bpp = 5; break;
case VDEPTH_32BIT: bpp = 8; break;
}
x = mode.x, y = mode.y, depth = bits_from_depth(mode.depth), bpr = mode.bytes_per_row;
colourSpace = CGColorSpaceCreateDeviceRGB();
if ( mode.depth < VDEPTH_16BIT )
{
CGColorSpaceRef oldColourSpace = colourSpace;
colourSpace = CGColorSpaceCreateIndexed(colourSpace, 255, colourTable);
CGColorSpaceRelease(oldColourSpace);
}
if ( ! colourSpace )
{
ErrorAlert("No valid colour space");
return false;
}
provider = CGDataProviderCreateWithData(NULL, the_buffer,
the_buffer_size, NULL);
if ( ! provider )
{
ErrorAlert("Could not create CGDataProvider from buffer data");
return false;
}
imageRef = CGImageCreate(x, y, bpp, depth, bpr, colourSpace,
#ifdef CG_USE_ALPHA
kCGImageAlphaPremultipliedFirst,
#else
kCGImageAlphaNoneSkipFirst,
#endif
provider,
NULL, // colourMap translation table
NO, // shouldInterpolate colours?
kCGRenderingIntentDefault);
if ( ! imageRef )
{
ErrorAlert("Could not create CGImage from CGDataProvider");
return false;
}
[output readyToDraw: imageRef
bitmap: offsetBuffer
imageWidth: x
imageHeight: y];
#ifdef CG_USE_ALPHA
mask_buffer(the_buffer, x, the_buffer_size);
/* Create an image mask with this call? */
//CG_EXTERN CGImageRef
//CGImageMaskCreate(size_t width, size_t height, size_t bitsPerComponent,
// size_t bitsPerPixel, size_t bytesPerRow,
// CGDataProviderRef provider, const float decode[], bool shouldInterpolate);
#endif
return true;
}
#import <AppKit/NSEvent.h>
#import <Carbon/Carbon.h>
#import "NNThread.h"
bool
OSX_monitor::init_screen(video_mode &mode)
{
// Set absolute mouse mode
ADBSetRelMouseMode(false);
// Display stored by add_CGDirectDisplay_modes()
theDisplay = (CGDirectDisplayID) mode.user_data;
originalMode = CGDisplayCurrentMode(theDisplay);
if ( ! originalMode )
{
ErrorSheet(@"Could not get current mode of display", the_win);
return false;
}
D(NSLog(@"About to call CGDisplayBestModeForParameters()"));
newMode = CGDisplayBestModeForParameters(theDisplay,
bits_from_depth(mode.depth),
mode.x, mode.y, NULL);
if ( ! newMode )
{
ErrorSheet(@"Could not find a matching screen mode", the_win);
return false;
}
// This sometimes takes ages to return after the window is genied,
// so for now we leave it onscreen
// [the_win miniaturize: nil];
D(NSLog(@"About to call CGDisplayCapture()"));
if ( CGDisplayCapture(theDisplay) != CGDisplayNoErr )
{
// [the_win deminiaturize: nil];
ErrorSheet(@"Could not capture display", the_win);
return false;
}
D(NSLog(@"About to call CGDisplaySwitchToMode()"));
if ( CGDisplaySwitchToMode(theDisplay, newMode) != CGDisplayNoErr )
{
CGDisplayRelease(theDisplay);
// [the_win deminiaturize: nil];
ErrorSheet(@"Could not switch to matching screen mode", the_win);
return false;
}
the_buffer = CGDisplayBaseAddress(theDisplay);
if ( ! the_buffer )
{
CGDisplaySwitchToMode(theDisplay, originalMode);
CGDisplayRelease(theDisplay);
// [the_win deminiaturize: nil];
ErrorSheet(@"Could not get base address of screen", the_win);
return false;
}
if ( mode.bytes_per_row != CGDisplayBytesPerRow(theDisplay) )
{
D(bug("Bytes per row (%d) doesn't match current (%ld)\n",
mode.bytes_per_row, CGDisplayBytesPerRow(theDisplay)));
mode.bytes_per_row = CGDisplayBytesPerRow(theDisplay);
}
[NSMenu setMenuBarVisible:NO];
if ( singleDisplay )
{
CGDisplayHideCursor(theDisplay);
[output startedFullScreen: theDisplay];
// Send emulated mouse to current location
[output fullscreenMouseMove];
}
else
{
// Should set up something to hide the cursor when it enters theDisplay?
}
return true;
}
bool
OSX_monitor::init_opengl(const video_mode &mode)
{
ErrorAlert("Sorry. OpenGL mode is not implemented yet");
return false;
}
/*
* Initialization
*/
static bool
monitor_init(const video_mode &init_mode)
{
OSX_monitor *monitor;
BOOL success;
monitor = new OSX_monitor(VideoModes, init_mode.depth,
init_mode.resolution_id);
success = monitor->video_open(init_mode);
if ( success )
{
monitor->set_mac_frame_buffer(init_mode);
VideoMonitors.push_back(monitor);
return YES;
}
return NO;
}
bool VideoInit(bool classic)
{
// Read frame skip prefs
frame_skip = PrefsFindInt32("frameskip");
if (frame_skip == 0)
frame_skip = 1;
// Get screen mode from preferences
const char *mode_str;
if (classic)
mode_str = "win/512/342";
else
mode_str = PrefsFindString("screen");
// Determine display_type and init_width, height & depth
parse_screen_prefs(mode_str);
// Construct list of supported modes
if (classic)
add_mode(512, 342, 0x80, 64, 0, VDEPTH_1BIT);
else
switch ( display_type )
{
case DISPLAY_SCREEN:
if ( ! add_CGDirectDisplay_modes() )
{
ErrorAlert("Unable to get list of displays for full screen mode");
return false;
}
break;
case DISPLAY_OPENGL:
// Same as window depths and sizes?
case DISPLAY_WINDOW:
add_standard_modes(VDEPTH_1BIT);
add_standard_modes(VDEPTH_2BIT);
add_standard_modes(VDEPTH_4BIT);
add_standard_modes(VDEPTH_8BIT);
add_standard_modes(VDEPTH_16BIT);
add_standard_modes(VDEPTH_32BIT);
break;
}
// video_init_depth_list(); Now done in monitor_desc constructor?
#if DEBUG
bug("Available video modes:\n");
vector<video_mode>::const_iterator i, end = VideoModes.end();
for (i = VideoModes.begin(); i != end; ++i)
bug(" %dx%d (ID %02x), %s\n", i->x, i->y, i->resolution_id,
colours_from_depth(i->depth));
#endif
D(bug("VideoInit: width=%hd height=%hd depth=%d\n",
init_width, init_height, init_depth));
// Find requested default mode and open display
if (VideoModes.size() > 0)
{
// Find mode with specified dimensions
std::vector<video_mode>::const_iterator i, end = VideoModes.end();
for (i = VideoModes.begin(); i != end; ++i)
{
D(bug("VideoInit: w=%d h=%d d=%d\n",
i->x, i->y, bits_from_depth(i->depth)));
if (i->x == init_width && i->y == init_height
&& bits_from_depth(i->depth) == init_depth)
return monitor_init(*i);
}
}
char str[150];
sprintf(str, "Cannot open selected video mode\r(%hd x %hd, %s).\r%s",
init_width, init_height,
colours_from_depth(init_depth), "Using lowest resolution");
WarningAlert(str);
return monitor_init(VideoModes[0]);
}
// Open display for specified mode
bool
OSX_monitor::video_open(const video_mode &mode)
{
D(bug("video_open: width=%d height=%d depth=%d bytes_per_row=%d\n",
mode.x, mode.y, bits_from_depth(mode.depth), mode.bytes_per_row));
// Open display
switch ( display_type )
{
case DISPLAY_WINDOW: return init_window(mode);
case DISPLAY_SCREEN: return init_screen((video_mode &)mode);
case DISPLAY_OPENGL: return init_opengl(mode);
}
return false;
}
void
OSX_monitor::video_close()
{
D(bug("video_close()\n"));
switch ( display_type ) {
case DISPLAY_WINDOW:
// Stop redraw thread
[output disableDrawing];
// Free frame buffer stuff
CGImageRelease(imageRef);
CGColorSpaceRelease(colourSpace);
CGDataProviderRelease(provider);
free(the_buffer);
break;
case DISPLAY_SCREEN:
if ( theDisplay && originalMode )
{
if ( singleDisplay )
CGDisplayShowCursor(theDisplay);
[NSMenu setMenuBarVisible:YES];
CGDisplaySwitchToMode(theDisplay, originalMode);
CGDisplayRelease(theDisplay);
//[the_win deminiaturize: nil];
}
break;
case DISPLAY_OPENGL:
break;
}
}
/*
* Deinitialization
*/
void VideoExit(void)
{
// Close displays
vector<monitor_desc *>::iterator i, end;
end = VideoMonitors.end();
for (i = VideoMonitors.begin(); i != end; ++i)
dynamic_cast<OSX_monitor *>(*i)->video_close();
VideoMonitors.clear();
VideoModes.clear();
}
/*
* Set palette
*/
void
OSX_monitor::set_palette(uint8 *pal, int num)
{
if ( [output isFullScreen] && CGDisplayCanSetPalette(theDisplay)
&& ! IsDirectMode(get_current_mode()) )
{
CGDirectPaletteRef CGpal;
CGDisplayErr err;
CGpal = CGPaletteCreateWithByteSamples((CGDeviceByteColor *)pal, num);
err = CGDisplaySetPalette(theDisplay, CGpal);
if ( err != noErr )
NSLog(@"Failed to set palette, error = %d", err);
CGPaletteRelease(CGpal);
}
if ( display_type != DISPLAY_WINDOW )
return;
// To change the palette, we have to regenerate
// the CGImageRef with the new colour space.
CGImageRef oldImageRef = imageRef;
CGColorSpaceRef oldColourSpace = colourSpace;
colourSpace = CGColorSpaceCreateDeviceRGB();
if ( depth < 16 )
{
CGColorSpaceRef tempColourSpace = colourSpace;
colourSpace = CGColorSpaceCreateIndexed(colourSpace, 255, pal);
CGColorSpaceRelease(tempColourSpace);
}
if ( ! colourSpace )
{
ErrorAlert("No valid colour space");
return;
}
imageRef = CGImageCreate(x, y, bpp, depth, bpr, colourSpace,
#ifdef CG_USE_ALPHA
kCGImageAlphaPremultipliedFirst,
#else
kCGImageAlphaNoneSkipFirst,
#endif
provider,
NULL, // colourMap translation table
NO, // shouldInterpolate colours?
kCGRenderingIntentDefault);
if ( ! imageRef )
{
ErrorAlert("Could not create CGImage from CGDataProvider");
return;
}
unsigned char *offsetBuffer = (unsigned char *) the_buffer;
offsetBuffer += 1; // OS X NSBitmaps are RGBA, but Basilisk generates ARGB
[output readyToDraw: imageRef
bitmap: offsetBuffer
imageWidth: x
imageHeight: y];
CGColorSpaceRelease(oldColourSpace);
CGImageRelease(oldImageRef);
}
/*
* Switch video mode
*/
void
OSX_monitor::switch_to_current_mode(void)
{
video_mode mode = get_current_mode();
const char *failure = NULL;
D(bug("switch_to_current_mode(): width=%d height=%d depth=%d bytes_per_row=%d\n", mode.x, mode.y, bits_from_depth(mode.depth), mode.bytes_per_row));
if ( display_type == DISPLAY_SCREEN && originalMode )
{
D(NSLog(@"About to call CGDisplayBestModeForParameters()"));
newMode = CGDisplayBestModeForParameters(theDisplay,
bits_from_depth(mode.depth),
mode.x, mode.y, NULL);
if ( ! newMode )
failure = "Could not find a matching screen mode";
else
{
D(NSLog(@"About to call CGDisplaySwitchToMode()"));
if ( CGDisplaySwitchToMode(theDisplay, newMode) != CGDisplayNoErr )
failure = "Could not switch to matching screen mode";
}
// For mouse event processing: update screen height
[output startedFullScreen: theDisplay];
if ( ! failure &&
mode.bytes_per_row != CGDisplayBytesPerRow(theDisplay) )
{
D(bug("Bytes per row (%d) doesn't match current (%ld)\n",
mode.bytes_per_row, CGDisplayBytesPerRow(theDisplay)));
mode.bytes_per_row = CGDisplayBytesPerRow(theDisplay);
}
if ( ! failure &&
! ( the_buffer = CGDisplayBaseAddress(theDisplay) ) )
failure = "Could not get base address of screen";
}
// Clean up the old CGImageRef stuff
else if ( display_type == DISPLAY_WINDOW && imageRef )
{
CGImageRef oldImageRef = imageRef;
CGColorSpaceRef oldColourSpace = colourSpace;
CGDataProviderRef oldProvider = provider;
void *oldBuffer = the_buffer;
if ( video_open(mode) )
{
CGImageRelease(oldImageRef);
CGColorSpaceRelease(oldColourSpace);
CGDataProviderRelease(oldProvider);
free(oldBuffer);
}
else
failure = "Could not video_open() requested mode";
}
else if ( ! video_open(mode) )
failure = "Could not video_open() requested mode";
if ( ! failure && display_type == DISPLAY_SCREEN )
{
// Whenever we change screen resolution, the MacOS mouse starts
// up in the top left corner. Send real mouse to that location
// if ( CGDisplayMoveCursorToPoint(theDisplay, CGPointMake(15,15))
// == CGDisplayNoErr )
// {
//
[output fullscreenMouseMove];
// }
// else
// failure = "Could move (jump) cursor on screen";
}
if ( failure )
{
NSLog(@"In switch_to_current_mode():");
NSLog(@"%s.", failure);
video_close();
if ( display_type == DISPLAY_SCREEN )
ErrorAlert("Cannot switch screen to selected video mode");
else
ErrorAlert(STR_OPEN_WINDOW_ERR);
QuitEmulator();
}
else