ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/video_macosx.mm
Revision: 1.8
Committed: 2003-03-21T06:29:28Z (21 years, 4 months ago) by nigel
Branch: MAIN
CVS Tags: nigel-build-12
Changes since 1.7: +122 -39 lines
Log Message:
Now supports all bit depth when in windowed mode
(but only when using CGIMAGEREF drawing strategy)

File Contents

# Content
1 /*
2 * $Id: video_macosx.mm,v 1.7 2003/03/11 11:44:14 nigel Exp $
3 *
4 * video_macosx.mm - Interface between Basilisk II and Cocoa windowing.
5 * Based on video_amiga.cpp and video_x.cpp
6 *
7 * Basilisk II (C) 1997-2003 Christian Bauer
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24
25 #include "sysdeps.h"
26
27 #ifdef HAVE_PTHREADS
28 # include <pthread.h>
29 #endif
30
31 #include <adb.h>
32 #include <cpu_emulation.h>
33 #include <main.h>
34 #include "macos_util_macosx.h"
35 #include <prefs.h>
36 #include <user_strings.h>
37 #include "video_macosx.h"
38
39 #define DEBUG 0
40 #define VERBOSE 0
41 #include "debug.h"
42
43 #ifdef NSBITMAP
44 #import <AppKit/NSBitmapImageRep.h>
45 #endif
46
47 #import <Foundation/NSString.h> // Needed for NSLog(@"")
48 #import "misc_macosx.h" // WarningSheet() prototype
49
50
51
52 // Global variables
53 uint8 display_type = DISPLAY_WINDOW, // These are used by PrefsEditor
54 frame_skip;
55 uint16 init_width = MIN_WIDTH, // as well as this code
56 init_height = MIN_HEIGHT,
57 init_depth = 32;
58
59 EmulatorView *output = nil; // Set by [EmulatorView init]
60 NSWindow *the_win = nil; // Set by [Emulator awakeFromNib]
61
62 static BOOL singleDisplay = YES;
63
64 /*
65 * Utility functions
66 */
67
68 static uint8
69 bits_from_depth(const video_depth depth)
70 {
71 int bits = 1 << depth;
72 // if (bits == 16)
73 // bits = 15;
74 // else if (bits == 32)
75 // bits = 24;
76 return bits;
77 }
78
79 static char *
80 colours_from_depth(const video_depth depth)
81 {
82 switch ( depth )
83 {
84 case VDEPTH_1BIT : return "Monochrome";
85 case VDEPTH_2BIT : return "4 colours";
86 case VDEPTH_4BIT : return "16 colours";
87 case VDEPTH_8BIT : return "256 colours";
88 case VDEPTH_16BIT: return "Thousands of colours";
89 case VDEPTH_32BIT: return "Millions of colours";
90 }
91
92 return "illegal colour depth";
93 }
94
95 static char *
96 colours_from_depth(const uint16 depth)
97 {
98 return colours_from_depth(DepthModeForPixelDepth(depth) );
99 }
100
101 bool
102 parse_screen_prefs(const char *mode_str)
103 {
104 if (sscanf(mode_str, "win/%hd/%hd/%hd",
105 &init_width, &init_height, &init_depth) == 3)
106 display_type = DISPLAY_WINDOW;
107 else if (sscanf(mode_str, "win/%hd/%hd", &init_width, &init_height) == 2)
108 display_type = DISPLAY_WINDOW;
109 else if (strcmp(mode_str, "full") == 0)
110 display_type = DISPLAY_SCREEN;
111 else if (sscanf(mode_str, "full/%hd/%hd/%hd",
112 &init_width, &init_height, &init_depth) == 3)
113 display_type = DISPLAY_SCREEN;
114 else if (sscanf(mode_str, "full/%hd/%hd", &init_width, &init_height) == 2)
115 display_type = DISPLAY_SCREEN;
116 else if (sscanf(mode_str, "opengl/%hd/%hd/%hd",
117 &init_width, &init_height, &init_depth) == 3)
118 display_type = DISPLAY_OPENGL;
119 else if (sscanf(mode_str, "opengl/%hd/%hd", &init_width, &init_height) == 2)
120 display_type = DISPLAY_OPENGL;
121 else return false;
122
123 return true;
124 }
125
126 // Supported video modes
127 static vector<video_mode> VideoModes;
128
129
130 // Add mode to list of supported modes
131 static void
132 add_mode(const uint16 width, const uint16 height,
133 const uint32 resolution_id, const uint32 bytes_per_row,
134 const uint32 user_data,
135 const video_depth depth)
136 {
137 vector<video_mode>::const_iterator i,
138 end = VideoModes.end();
139
140 for (i = VideoModes.begin(); i != end; ++i)
141 if ( i->x == width && i->y == height &&
142 i->bytes_per_row == bytes_per_row && i->depth == depth )
143 {
144 D(NSLog(@"Duplicate mode (%hdx%hdx%ld, ID %02x, new ID %02x)\n",
145 width, height, depth, i->resolution_id, resolution_id));
146 return;
147 }
148
149 video_mode mode;
150
151 mode.x = width;
152 mode.y = height;
153 mode.resolution_id = resolution_id;
154 mode.bytes_per_row = bytes_per_row;
155 mode.user_data = user_data;
156 mode.depth = depth;
157
158 D(bug("Added video mode: w=%d h=%d d=%d(%d bits)\n",
159 width, height, depth, bits_from_depth(depth) ));
160
161 VideoModes.push_back(mode);
162 }
163
164 // Add standard list of windowed modes for given color depth
165 static void add_standard_modes(const video_depth depth)
166 {
167 D(bug("add_standard_modes: depth=%d(%d bits)\n",
168 depth, bits_from_depth(depth) ));
169
170 add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), 0, depth);
171 add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), 0, depth);
172 add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), 0, depth);
173 add_mode(832, 624, 0x83, TrivialBytesPerRow(832, depth), 0, depth);
174 add_mode(1024, 768, 0x84, TrivialBytesPerRow(1024, depth), 0, depth);
175 add_mode(1152, 768, 0x85, TrivialBytesPerRow(1152, depth), 0, depth);
176 add_mode(1152, 870, 0x86, TrivialBytesPerRow(1152, depth), 0, depth);
177 add_mode(1280, 1024, 0x87, TrivialBytesPerRow(1280, depth), 0, depth);
178 add_mode(1600, 1200, 0x88, TrivialBytesPerRow(1600, depth), 0, depth);
179 }
180
181 // Helper function to get a 32bit int from a dictionary
182 static int32 getCFint32 (CFDictionaryRef dict, CFStringRef key)
183 {
184 CFNumberRef ref = (CFNumberRef) CFDictionaryGetValue(dict, key);
185
186 if ( ref )
187 {
188 int32 val;
189
190 if ( CFNumberGetValue(ref, kCFNumberSInt32Type, &val) )
191 return val;
192 else
193 NSLog(@"getCFint32() - Failed to get the value %@", key);
194 }
195 else
196 NSLog(@"getCFint32() - Failed to get a 32bit int for %@", key);
197
198 return 0;
199 }
200
201 // Nasty hack. Under 10.1, CGDisplayAvailableModes() does not provide bytes per row,
202 // and the emulator doesn't like setting the bytes per row after the screen,
203 // so we use a lot of magic numbers here.
204 // This will probably fail on some video hardware.
205 // I have tested on my G4 PowerBook 400 and G3 PowerBook Series 292
206
207 static int
208 CGBytesPerRow(const uint16 width, const video_depth depth)
209 {
210 if ( depth == VDEPTH_8BIT )
211 switch ( width )
212 {
213 case 640:
214 case 720: return 768;
215 case 800:
216 case 896: return 1024;
217 case 1152: return 1280;
218 }
219
220 if ( width == 720 && depth == VDEPTH_16BIT) return 1536;
221 if ( width == 720 && depth == VDEPTH_32BIT) return 3072;
222 if ( width == 800 && depth == VDEPTH_16BIT) return 1792;
223 if ( width == 800 && depth == VDEPTH_32BIT) return 3328;
224
225 return TrivialBytesPerRow(width, depth);
226 }
227
228 static bool add_CGDirectDisplay_modes()
229 {
230 #define kMaxDisplays 8
231 CGDirectDisplayID displays[kMaxDisplays];
232 CGDisplayErr err;
233 CGDisplayCount n;
234 int32 oldRes = 0,
235 res_id = 0x80;
236
237
238 err = CGGetActiveDisplayList(kMaxDisplays, displays, &n);
239 if ( err != CGDisplayNoErr )
240 n = 1, displays[n] = kCGDirectMainDisplay;
241
242 if ( n > 1 )
243 singleDisplay = NO;
244
245 for ( CGDisplayCount dc = 0; dc < n; ++dc )
246 {
247 CGDirectDisplayID d = displays[dc];
248 CFArrayRef m = CGDisplayAvailableModes(d);
249
250 if ( ! m ) // Store the current display mode
251 add_mode(CGDisplayPixelsWide(d),
252 CGDisplayPixelsHigh(d),
253 res_id++, CGDisplayBytesPerRow(d),
254 (const uint32) d,
255 DepthModeForPixelDepth(CGDisplayBitsPerPixel(d)));
256 else
257 {
258 CFIndex nModes = CFArrayGetCount(m);
259
260 for ( CFIndex mc = 0; mc < nModes; ++mc )
261 {
262 CFDictionaryRef modeSpec = (CFDictionaryRef)
263 CFArrayGetValueAtIndex(m, mc);
264
265 int32 bpp = getCFint32(modeSpec, kCGDisplayBitsPerPixel);
266 int32 height = getCFint32(modeSpec, kCGDisplayHeight);
267 int32 width = getCFint32(modeSpec, kCGDisplayWidth);
268 #ifdef MAC_OS_X_VERSION_10_2
269 int32 bytes = getCFint32(modeSpec, kCGDisplayBytesPerRow);
270 #else
271 int32 bytes = 0;
272 #endif
273 video_depth depth = DepthModeForPixelDepth(bpp);
274
275 if ( ! bpp || ! height || ! width )
276 {
277 NSLog(@"Could not get details of mode %d, display %d",
278 mc, dc);
279 return false;
280 }
281 #if VERBOSE
282 else
283 NSLog(@"Display %ld, spec = %@", d, modeSpec);
284 #endif
285
286 if ( ! bytes )
287 {
288 NSLog(@"Could not get bytes per row, guessing");
289 bytes = CGBytesPerRow(width, depth);
290 }
291
292 if ( ! oldRes )
293 oldRes = width * height;
294 else
295 if ( oldRes != width * height )
296 {
297 oldRes = width * height;
298 ++res_id;
299 }
300
301 add_mode(width, height, res_id, bytes, (const uint32) d, depth);
302 }
303 }
304 }
305
306 return true;
307 }
308
309 #ifdef CG_USE_ALPHA
310 // memset() by long instead of byte
311
312 static void memsetl (long *buffer, long pattern, size_t length)
313 {
314 long *buf = (long *) buffer,
315 *end = buf + length/4;
316
317 while ( ++buf < end )
318 *buf = pattern;
319 }
320
321 // Sets the alpha channel in a image to full on, except for the corners
322
323 static void mask_buffer (void *buffer, size_t width, size_t size)
324 {
325 long *bufl = (long *) buffer;
326 char *bufc = (char *) buffer;
327
328
329 memsetl(bufl, 0xFF000000, size);
330
331
332 // Round upper-left corner
333 *bufl = 0, *bufc+4 = 0; // XXXXX
334 bufc += width, *bufc++ = 0, *bufc++ = 0, *bufc++ = 0; // XXX
335 bufc += width, *bufc++ = 0, *bufc = 0; // XX
336 bufc += width, *bufc = 0; // X
337 bufc += width, *bufc = 0; // X
338
339
340 NSLog(@"Masked buffer");
341 }
342 #endif
343
344 // monitor_desc subclass for Mac OS X displays
345
346 class OSX_monitor : public monitor_desc
347 {
348 public:
349 OSX_monitor(const vector<video_mode> &available_modes,
350 video_depth default_depth,
351 uint32 default_id);
352
353 virtual void set_palette(uint8 *pal, int num);
354 virtual void switch_to_current_mode(void);
355
356 void set_mac_frame_buffer(const video_mode mode);
357
358 void video_close(void);
359 bool video_open (const video_mode &mode);
360
361
362 private:
363 bool init_opengl(const video_mode &mode);
364 bool init_screen( video_mode &mode);
365 bool init_window(const video_mode &mode);
366
367
368 #ifdef CGIMAGEREF
369 CGColorSpaceRef colourSpace;
370 uint8 *colourTable;
371 CGImageRef imageRef;
372 CGDataProviderRef provider;
373 short x, y, bpp, depth, bpr;
374 #endif
375 #ifdef NSBITMAP
376 NSBitmapImageRep *bitmap;
377 #endif
378 void *the_buffer;
379
380
381 // These record changes we made in setting full screen mode,
382 // so that we can set the display back as it was again.
383 CGDirectDisplayID theDisplay;
384 CFDictionaryRef originalMode,
385 newMode;
386 };
387
388
389 OSX_monitor :: OSX_monitor (const vector<video_mode> &available_modes,
390 video_depth default_depth,
391 uint32 default_id)
392 : monitor_desc (available_modes, default_depth, default_id)
393 {
394 #ifdef CGIMAGEREF
395 colourSpace = nil;
396 colourTable = (uint8 *) malloc(256 * 3);
397 imageRef = nil;
398 provider = nil;
399 #endif
400 #ifdef NSBITMAP
401 bitmap = nil;
402 #endif
403 newMode = originalMode = nil;
404 the_buffer = NULL;
405 theDisplay = nil;
406 };
407
408 // Should also have a destructor which does
409 //#ifdef CGIMAGEREF
410 // free(colourTable);
411 //#endif
412
413
414 // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
415 void
416 OSX_monitor::set_mac_frame_buffer(const video_mode mode)
417 {
418 #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
419 switch ( mode.depth )
420 {
421 // case VDEPTH_15BIT:
422 case VDEPTH_16BIT: MacFrameLayout = FLAYOUT_HOST_555; break;
423 // case VDEPTH_24BIT:
424 case VDEPTH_32BIT: MacFrameLayout = FLAYOUT_HOST_888; break;
425 default : MacFrameLayout = FLAYOUT_DIRECT;
426 }
427 set_mac_frame_base(MacFrameBaseMac);
428
429 // Set variables used by UAE memory banking
430 MacFrameBaseHost = (uint8 *) the_buffer;
431 MacFrameSize = mode.bytes_per_row * mode.y;
432 InitFrameBufferMapping();
433 #else
434 set_mac_frame_base((unsigned int)Host2MacAddr((uint8 *)the_buffer));
435 #endif
436 D(bug("mac_frame_base = %08x\n", get_mac_frame_base()));
437 }
438
439 static void
440 resizeWinBy(const short deltaX, const short deltaY)
441 {
442 NSRect rect = [the_win frame];
443
444 D(bug("resizeWinBy(%d,%d) - ", deltaX, deltaY));
445 D(bug("old x=%g, y=%g", rect.size.width, rect.size.height));
446
447 rect.size.width += deltaX;
448 rect.size.height += deltaY;
449
450 D(bug(", new x=%g, y=%g\n", rect.size.width, rect.size.height));
451
452 [the_win setFrame: rect display: YES];
453 rect = [the_win frame];
454 }
455
456 void resizeWinTo(const uint16 newWidth, const uint16 newHeight)
457 {
458 int deltaX = newWidth - [output width],
459 deltaY = newHeight - [output height];
460
461 D(bug("resizeWinTo(%d,%d)\n", newWidth, newHeight));
462
463 if ( deltaX || deltaY )
464 resizeWinBy(deltaX, deltaY);
465 }
466
467 // Open window
468 bool
469 OSX_monitor::init_window(const video_mode &mode)
470 {
471 D(bug("init_window: depth=%d(%d bits)\n",
472 mode.depth, bits_from_depth(mode.depth) ));
473
474
475 // Set absolute mouse mode
476 ADBSetRelMouseMode(false);
477
478
479 // Is the window open?
480 if ( ! the_win )
481 {
482 ErrorAlert(STR_OPEN_WINDOW_ERR);
483 return false;
484 }
485 resizeWinTo(mode.x, mode.y);
486
487
488 // Create frame buffer ("height + 2" for safety)
489 int the_buffer_size = mode.bytes_per_row * (mode.y + 2);
490
491 the_buffer = calloc(the_buffer_size, 1);
492 if ( ! the_buffer )
493 {
494 NSLog(@"calloc(%d) failed", the_buffer_size);
495 ErrorAlert(STR_NO_MEM_ERR);
496 return false;
497 }
498 D(bug("the_buffer = %p\n", the_buffer));
499
500
501 unsigned char *offsetBuffer = (unsigned char *) the_buffer;
502 offsetBuffer += 1; // OS X NSBitmaps are RGBA, but Basilisk generates ARGB
503
504 #ifdef CGIMAGEREF
505 switch ( mode.depth )
506 {
507 case VDEPTH_1BIT: bpp = 1; break;
508 case VDEPTH_2BIT: bpp = 2; break;
509 case VDEPTH_4BIT: bpp = 4; break;
510 case VDEPTH_8BIT: bpp = 8; break;
511 case VDEPTH_16BIT: bpp = 5; break;
512 case VDEPTH_32BIT: bpp = 8; break;
513 }
514
515 x = mode.x, y = mode.y, depth = bits_from_depth(mode.depth), bpr = mode.bytes_per_row;
516
517 colourSpace = CGColorSpaceCreateDeviceRGB();
518
519 if ( mode.depth < VDEPTH_16BIT )
520 {
521 CGColorSpaceRef oldColourSpace = colourSpace;
522
523 colourSpace = CGColorSpaceCreateIndexed(colourSpace, 255, colourTable);
524
525 CGColorSpaceRelease(oldColourSpace);
526 }
527
528 if ( ! colourSpace )
529 {
530 ErrorAlert("No valid colour space");
531 return false;
532 }
533
534 provider = CGDataProviderCreateWithData(NULL, the_buffer,
535 the_buffer_size, NULL);
536 if ( ! provider )
537 {
538 ErrorAlert("Could not create CGDataProvider from buffer data");
539 return false;
540 }
541
542 imageRef = CGImageCreate(x, y, bpp, depth, bpr, colourSpace,
543 #ifdef CG_USE_ALPHA
544 kCGImageAlphaPremultipliedFirst,
545 #else
546 kCGImageAlphaNoneSkipFirst,
547 #endif
548 provider,
549 NULL, // colourMap translation table
550 NO, // shouldInterpolate colours?
551 kCGRenderingIntentDefault);
552 if ( ! imageRef )
553 {
554 ErrorAlert("Could not create CGImage from CGDataProvider");
555 return false;
556 }
557
558 [output readyToDraw: imageRef
559 bitmap: offsetBuffer
560 imageWidth: x
561 imageHeight: y];
562
563
564 #ifdef CG_USE_ALPHA
565 mask_buffer(the_buffer, x, the_buffer_size);
566 #endif
567
568 return true;
569 #endif
570
571
572 #ifndef CGIMAGEREF
573 short bitsPer, samplesPer; // How big is each Pixel?
574
575 if ( mode.depth == VDEPTH_1BIT )
576 bitsPer = 1;
577 else
578 bitsPer = 8;
579
580 if ( mode.depth == VDEPTH_32BIT )
581 samplesPer = 3;
582 else
583 samplesPer = 1;
584 #endif
585
586
587 #ifdef NSBITMAP
588 bitmap = [NSBitmapImageRep alloc];
589 bitmap = [bitmap initWithBitmapDataPlanes: (unsigned char **) &offsetBuffer
590 pixelsWide: mode.x
591 pixelsHigh: mode.y
592 bitsPerSample: bitsPer
593 samplesPerPixel: samplesPer
594 hasAlpha: NO
595 isPlanar: NO
596 colorSpaceName: NSCalibratedRGBColorSpace
597 bytesPerRow: mode.bytes_per_row
598 bitsPerPixel: bits_from_depth(mode.depth)];
599
600 if ( ! bitmap )
601 {
602 ErrorAlert("Could not allocate an NSBitmapImageRep");
603 return false;
604 }
605
606 [output readyToDraw: bitmap
607 imageWidth: mode.x
608 imageHeight: mode.y];
609 #endif
610
611 #ifdef CGDRAWBITMAP
612 [output readyToDraw: offsetBuffer
613 width: mode.x
614 height: mode.y
615 bps: bitsPer
616 spp: samplesPer
617 bpp: bits_from_depth(mode.depth)
618 bpr: mode.bytes_per_row
619 isPlanar: NO
620 hasAlpha: NO];
621 #endif
622
623 return true;
624 }
625
626 #import <AppKit/NSEvent.h>
627 #import <Carbon/Carbon.h>
628 #import "NNThread.h"
629
630 bool
631 OSX_monitor::init_screen(video_mode &mode)
632 {
633 // Set absolute mouse mode
634 ADBSetRelMouseMode(false);
635
636 // Display stored by add_CGDirectDisplay_modes()
637 theDisplay = (CGDirectDisplayID) mode.user_data;
638
639 originalMode = CGDisplayCurrentMode(theDisplay);
640 if ( ! originalMode )
641 {
642 ErrorSheet(@"Could not get current mode of display", the_win);
643 return false;
644 }
645
646 D(NSLog(@"About to call CGDisplayBestModeForParameters()"));
647 newMode = CGDisplayBestModeForParameters(theDisplay,
648 bits_from_depth(mode.depth),
649 mode.x, mode.y, NULL);
650 if ( ! newMode )
651 {
652 ErrorSheet(@"Could not find a matching screen mode", the_win);
653 return false;
654 }
655
656 // This sometimes takes ages to return after the window is genied,
657 // so for now we leave it onscreen
658 // [the_win miniaturize: nil];
659
660 D(NSLog(@"About to call CGDisplayCapture()"));
661 if ( CGDisplayCapture(theDisplay) != CGDisplayNoErr )
662 {
663 // [the_win deminiaturize: nil];
664 ErrorSheet(@"Could not capture display", the_win);
665 return false;
666 }
667
668 D(NSLog(@"About to call CGDisplaySwitchToMode()"));
669 if ( CGDisplaySwitchToMode(theDisplay, newMode) != CGDisplayNoErr )
670 {
671 CGDisplayRelease(theDisplay);
672 // [the_win deminiaturize: nil];
673 ErrorSheet(@"Could not switch to matching screen mode", the_win);
674 return false;
675 }
676
677 the_buffer = CGDisplayBaseAddress(theDisplay);
678 if ( ! the_buffer )
679 {
680 CGDisplaySwitchToMode(theDisplay, originalMode);
681 CGDisplayRelease(theDisplay);
682 // [the_win deminiaturize: nil];
683 ErrorSheet(@"Could not get base address of screen", the_win);
684 return false;
685 }
686
687 if ( mode.bytes_per_row != CGDisplayBytesPerRow(theDisplay) )
688 {
689 D(bug("Bytes per row (%d) doesn't match current (%ld)\n",
690 mode.bytes_per_row, CGDisplayBytesPerRow(theDisplay)));
691 mode.bytes_per_row = CGDisplayBytesPerRow(theDisplay);
692 }
693
694 HideMenuBar();
695
696 if ( singleDisplay )
697 {
698 CGDisplayHideCursor(theDisplay);
699
700 [output startedFullScreen: theDisplay];
701
702 // Send emulated mouse to current location
703 [output fullscreenMouseMove];
704 }
705 else
706 {
707 // Should set up something to hide the cursor when it enters theDisplay?
708 }
709
710 return true;
711 }
712
713
714 bool
715 OSX_monitor::init_opengl(const video_mode &mode)
716 {
717 ErrorAlert("Sorry. OpenGL mode is not implemented yet");
718 return false;
719 }
720
721 /*
722 * Initialization
723 */
724 static bool
725 monitor_init(const video_mode &init_mode)
726 {
727 OSX_monitor *monitor;
728 BOOL success;
729
730 monitor = new OSX_monitor(VideoModes, init_mode.depth,
731 init_mode.resolution_id);
732 success = monitor->video_open(init_mode);
733
734 if ( success )
735 {
736 monitor->set_mac_frame_buffer(init_mode);
737 VideoMonitors.push_back(monitor);
738 return YES;
739 }
740
741 return NO;
742 }
743
744 bool VideoInit(bool classic)
745 {
746 // Read frame skip prefs
747 frame_skip = PrefsFindInt32("frameskip");
748 if (frame_skip == 0)
749 frame_skip = 1;
750
751 // Get screen mode from preferences
752 const char *mode_str;
753 if (classic)
754 mode_str = "win/512/342";
755 else
756 mode_str = PrefsFindString("screen");
757
758 // Determine display_type and init_width, height & depth
759 parse_screen_prefs(mode_str);
760
761 // Construct list of supported modes
762 if (classic)
763 add_mode(512, 342, 0x80, 64, 0, VDEPTH_1BIT);
764 else
765 switch ( display_type )
766 {
767 case DISPLAY_SCREEN:
768 if ( ! add_CGDirectDisplay_modes() )
769 {
770 ErrorAlert("Unable to get list of displays for full screen mode");
771 return false;
772 }
773 break;
774 case DISPLAY_OPENGL:
775 // Same as window depths and sizes?
776 case DISPLAY_WINDOW:
777 #ifdef CGIMAGEREF
778 add_standard_modes(VDEPTH_1BIT);
779 add_standard_modes(VDEPTH_2BIT);
780 add_standard_modes(VDEPTH_4BIT);
781 add_standard_modes(VDEPTH_8BIT);
782 add_standard_modes(VDEPTH_16BIT);
783 #endif
784 add_standard_modes(VDEPTH_32BIT);
785 break;
786 }
787
788 // video_init_depth_list(); Now done in monitor_desc constructor?
789
790 #if DEBUG
791 bug("Available video modes:\n");
792 vector<video_mode>::const_iterator i, end = VideoModes.end();
793 for (i = VideoModes.begin(); i != end; ++i)
794 bug(" %dx%d (ID %02x), %s\n", i->x, i->y, i->resolution_id,
795 colours_from_depth(i->depth));
796 #endif
797
798 D(bug("VideoInit: width=%hd height=%hd depth=%d\n",
799 init_width, init_height, init_depth));
800
801 // Find requested default mode and open display
802 if (VideoModes.size() > 0)
803 {
804 // Find mode with specified dimensions
805 std::vector<video_mode>::const_iterator i, end = VideoModes.end();
806 for (i = VideoModes.begin(); i != end; ++i)
807 {
808 D(bug("VideoInit: w=%d h=%d d=%d\n",
809 i->x, i->y, bits_from_depth(i->depth)));
810 if (i->x == init_width && i->y == init_height
811 && bits_from_depth(i->depth) == init_depth)
812 return monitor_init(*i);
813 }
814 }
815
816 char str[150];
817 sprintf(str, "Cannot open selected video mode\r(%hd x %hd, %s).\r%s",
818 init_width, init_height,
819 colours_from_depth(init_depth), "Using lowest resolution");
820 WarningAlert(str);
821
822 return monitor_init(VideoModes[0]);
823 }
824
825
826 // Open display for specified mode
827 bool
828 OSX_monitor::video_open(const video_mode &mode)
829 {
830 D(bug("video_open: width=%d height=%d depth=%d bytes_per_row=%d\n",
831 mode.x, mode.y, bits_from_depth(mode.depth), mode.bytes_per_row));
832
833 // Open display
834 switch ( display_type )
835 {
836 case DISPLAY_WINDOW: return init_window(mode);
837 case DISPLAY_SCREEN: return init_screen((video_mode &)mode);
838 case DISPLAY_OPENGL: return init_opengl(mode);
839 }
840
841 return false;
842 }
843
844
845 void
846 OSX_monitor::video_close()
847 {
848 D(bug("video_close()\n"));
849
850 switch ( display_type ) {
851 case DISPLAY_WINDOW:
852 // Stop redraw thread
853 [output disableDrawing];
854
855 // Free frame buffer stuff
856 #ifdef CGIMAGEREF
857 CGImageRelease(imageRef);
858 CGColorSpaceRelease(colourSpace);
859 CGDataProviderRelease(provider);
860 #endif
861 #ifdef NSBITMAP
862 [bitmap release];
863 #endif
864 free(the_buffer);
865
866 break;
867
868 case DISPLAY_SCREEN:
869 if ( theDisplay && originalMode )
870 {
871 if ( singleDisplay )
872 CGDisplayShowCursor(theDisplay);
873 ShowMenuBar();
874 CGDisplaySwitchToMode(theDisplay, originalMode);
875 CGDisplayRelease(theDisplay);
876 //[the_win deminiaturize: nil];
877 }
878 break;
879
880 case DISPLAY_OPENGL:
881 break;
882 }
883 }
884
885
886 /*
887 * Deinitialization
888 */
889
890 void VideoExit(void)
891 {
892 // Close displays
893 vector<monitor_desc *>::iterator i, end;
894
895 end = VideoMonitors.end();
896
897 for (i = VideoMonitors.begin(); i != end; ++i)
898 dynamic_cast<OSX_monitor *>(*i)->video_close();
899 }
900
901
902 /*
903 * Set palette
904 */
905
906 void
907 OSX_monitor::set_palette(uint8 *pal, int num)
908 {
909 if ( [output isFullScreen] && CGDisplayCanSetPalette(theDisplay)
910 && ! IsDirectMode(get_current_mode()) )
911 {
912 CGDirectPaletteRef CGpal;
913 CGDisplayErr err;
914
915
916 CGpal = CGPaletteCreateWithByteSamples((CGDeviceByteColor *)pal, num);
917 err = CGDisplaySetPalette(theDisplay, CGpal);
918 if ( err != noErr )
919 NSLog(@"Failed to set palette, error = %d", err);
920 CGPaletteRelease(CGpal);
921 }
922
923 #ifdef CGIMAGEREF
924 if ( display_type != DISPLAY_WINDOW )
925 return;
926
927 // To change the palette, we have to regenerate
928 // the CGImageRef with the new colour space.
929
930 CGImageRef oldImageRef = imageRef;
931 CGColorSpaceRef oldColourSpace = colourSpace;
932
933 colourSpace = CGColorSpaceCreateDeviceRGB();
934
935 if ( depth < 16 )
936 {
937 CGColorSpaceRef tempColourSpace = colourSpace;
938
939 colourSpace = CGColorSpaceCreateIndexed(colourSpace, 255, pal);
940 CGColorSpaceRelease(tempColourSpace);
941 }
942
943 if ( ! colourSpace )
944 {
945 ErrorAlert("No valid colour space");
946 return;
947 }
948
949 imageRef = CGImageCreate(x, y, bpp, depth, bpr, colourSpace,
950 #ifdef CG_USE_ALPHA
951 kCGImageAlphaPremultipliedFirst,
952 #else
953 kCGImageAlphaNoneSkipFirst,
954 #endif
955 provider,
956 NULL, // colourMap translation table
957 NO, // shouldInterpolate colours?
958 kCGRenderingIntentDefault);
959 if ( ! imageRef )
960 {
961 ErrorAlert("Could not create CGImage from CGDataProvider");
962 return;
963 }
964
965 unsigned char *offsetBuffer = (unsigned char *) the_buffer;
966 offsetBuffer += 1; // OS X NSBitmaps are RGBA, but Basilisk generates ARGB
967
968 [output readyToDraw: imageRef
969 bitmap: offsetBuffer
970 imageWidth: x
971 imageHeight: y];
972
973 CGColorSpaceRelease(oldColourSpace);
974 CGImageRelease(oldImageRef);
975 #endif
976 }
977
978
979 /*
980 * Switch video mode
981 */
982
983 void
984 OSX_monitor::switch_to_current_mode(void)
985 {
986 video_mode mode = get_current_mode();
987 char *failure = NULL;
988
989
990 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));
991
992 if ( display_type == DISPLAY_SCREEN && originalMode )
993 {
994 D(NSLog(@"About to call CGDisplayBestModeForParameters()"));
995 newMode = CGDisplayBestModeForParameters(theDisplay,
996 bits_from_depth(mode.depth),
997 mode.x, mode.y, NULL);
998 if ( ! newMode )
999 failure = "Could not find a matching screen mode";
1000 else
1001 {
1002 D(NSLog(@"About to call CGDisplaySwitchToMode()"));
1003 if ( CGDisplaySwitchToMode(theDisplay, newMode) != CGDisplayNoErr )
1004 failure = "Could not switch to matching screen mode";
1005 }
1006
1007 // For mouse event processing: update screen height
1008 [output startedFullScreen: theDisplay];
1009
1010 if ( ! failure &&
1011 mode.bytes_per_row != CGDisplayBytesPerRow(theDisplay) )
1012 {
1013 D(bug("Bytes per row (%d) doesn't match current (%ld)\n",
1014 mode.bytes_per_row, CGDisplayBytesPerRow(theDisplay)));
1015 mode.bytes_per_row = CGDisplayBytesPerRow(theDisplay);
1016 }
1017
1018 if ( ! failure &&
1019 ! ( the_buffer = CGDisplayBaseAddress(theDisplay) ) )
1020 failure = "Could not get base address of screen";
1021
1022 }
1023 else if ( ! video_open(mode) )
1024 failure = "Could not video_open() requested mode";
1025
1026 if ( ! failure && display_type == DISPLAY_SCREEN )
1027 {
1028 // Whenever we change screen resolution, the MacOS mouse starts
1029 // up in the top left corner. Send real mouse to that location
1030 // if ( CGDisplayMoveCursorToPoint(theDisplay, CGPointMake(15,15))
1031 // == CGDisplayNoErr )
1032 // {
1033 //
1034 [output fullscreenMouseMove];
1035 // }
1036 // else
1037 // failure = "Could move (jump) cursor on screen";
1038 }
1039
1040 if ( failure )
1041 {
1042 NSLog(@"In switch_to_current_mode():");
1043 NSLog(@"%s.", failure);
1044 video_close();
1045 if ( display_type == DISPLAY_SCREEN )
1046 ErrorAlert("Cannot switch screen to selected video mode");
1047 else
1048 ErrorAlert(STR_OPEN_WINDOW_ERR);
1049 QuitEmulator();
1050 }
1051 else
1052 set_mac_frame_buffer(mode);
1053 }
1054
1055 /*
1056 * Close down full-screen mode
1057 * (if bringing up error alerts is unsafe while in full-screen mode)
1058 */
1059
1060 void VideoQuitFullScreen(void)
1061 {
1062 }
1063
1064
1065 /*
1066 * Mac VBL interrupt
1067 */
1068
1069 void VideoInterrupt(void)
1070 {
1071 }
1072
1073
1074 // This function is called on non-threaded platforms from a timer interrupt
1075 void VideoRefresh(void)
1076 {
1077 }