ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/video_macosx.mm
Revision: 1.6
Committed: 2002-12-19T10:40:40Z (21 years, 7 months ago) by nigel
Branch: MAIN
Changes since 1.5: +69 -13 lines
Log Message:
10.2 enhancements, eliminated some warnings, added alpha channel mask stuff

File Contents

# Content
1 /*
2 * $Id: video_macosx.mm,v 1.5 2002/07/02 09:47:57 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-2002 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 CGImageRef imageRef;
370 #endif
371 #ifdef NSBITMAP
372 NSBitmapImageRep *bitmap;
373 #endif
374 void *the_buffer;
375
376
377 // These record changes we made in setting full screen mode,
378 // so that we can set the display back as it was again.
379 CGDirectDisplayID theDisplay;
380 CFDictionaryRef originalMode,
381 newMode;
382 };
383
384
385 OSX_monitor :: OSX_monitor (const vector<video_mode> &available_modes,
386 video_depth default_depth,
387 uint32 default_id)
388 : monitor_desc (available_modes, default_depth, default_id)
389 {
390 #ifdef CGIMAGEREF
391 imageRef = nil;
392 #endif
393 #ifdef NSBITMAP
394 bitmap = nil;
395 #endif
396 newMode = originalMode = nil;
397 the_buffer = NULL;
398 theDisplay = nil;
399 };
400
401
402 // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
403 void
404 OSX_monitor::set_mac_frame_buffer(const video_mode mode)
405 {
406 #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
407 switch ( mode.depth )
408 {
409 // case VDEPTH_15BIT:
410 case VDEPTH_16BIT: MacFrameLayout = FLAYOUT_HOST_555; break;
411 // case VDEPTH_24BIT:
412 case VDEPTH_32BIT: MacFrameLayout = FLAYOUT_HOST_888; break;
413 default : MacFrameLayout = FLAYOUT_DIRECT;
414 }
415 set_mac_frame_base(MacFrameBaseMac);
416
417 // Set variables used by UAE memory banking
418 MacFrameBaseHost = (uint8 *) the_buffer;
419 MacFrameSize = mode.bytes_per_row * mode.y;
420 InitFrameBufferMapping();
421 #else
422 set_mac_frame_base((unsigned int)Host2MacAddr((uint8 *)the_buffer));
423 #endif
424 D(bug("mac_frame_base = %08x\n", get_mac_frame_base()));
425 }
426
427 static void
428 resizeWinBy(const short deltaX, const short deltaY)
429 {
430 NSRect rect = [the_win frame];
431
432 D(bug("resizeWinBy(%d,%d) - ", deltaX, deltaY));
433 D(bug("old x=%g, y=%g", rect.size.width, rect.size.height));
434
435 rect.size.width += deltaX;
436 rect.size.height += deltaY;
437
438 D(bug(", new x=%g, y=%g\n", rect.size.width, rect.size.height));
439
440 [the_win setFrame: rect display: YES];
441 rect = [the_win frame];
442 }
443
444 void resizeWinTo(const uint16 newWidth, const uint16 newHeight)
445 {
446 int deltaX = newWidth - [output width],
447 deltaY = newHeight - [output height];
448
449 D(bug("resizeWinTo(%d,%d)\n", newWidth, newHeight));
450
451 if ( deltaX || deltaY )
452 resizeWinBy(deltaX, deltaY);
453 }
454
455 // Open window
456 bool
457 OSX_monitor::init_window(const video_mode &mode)
458 {
459 #ifdef CGIMAGEREF
460 CGColorSpaceRef colourSpace;
461 CGDataProviderRef provider;
462 #endif
463 short bitsPer, samplesPer; // How big is each Pixel?
464 int the_buffer_size;
465
466 D(bug("init_window: depth=%d(%d bits)\n",
467 mode.depth, bits_from_depth(mode.depth) ));
468
469
470 // Set absolute mouse mode
471 ADBSetRelMouseMode(false);
472
473
474 // Open window
475 if ( ! the_win )
476 {
477 ErrorAlert(STR_OPEN_WINDOW_ERR);
478 return false;
479 }
480 resizeWinTo(mode.x, mode.y);
481
482
483 // Create frame buffer ("height + 2" for safety)
484 the_buffer_size = mode.bytes_per_row * (mode.y + 2);
485 the_buffer = calloc(the_buffer_size, 1);
486 if ( ! the_buffer )
487 {
488 NSLog(@"calloc(%d) failed", the_buffer_size);
489 ErrorAlert(STR_NO_MEM_ERR);
490 return false;
491 }
492 D(bug("the_buffer = %p\n", the_buffer));
493
494
495 if ( mode.depth == VDEPTH_1BIT )
496 bitsPer = 1;
497 else
498 bitsPer = 8;
499
500 if ( mode.depth == VDEPTH_32BIT )
501 samplesPer = 3;
502 else
503 samplesPer = 1;
504
505 #ifdef CGIMAGEREF
506 switch ( mode.depth )
507 {
508 //case VDEPTH_1BIT: colourSpace = CGColorSpaceCreateDeviceMono(); break
509 case VDEPTH_8BIT: colourSpace = CGColorSpaceCreateDeviceGray(); break;
510 case VDEPTH_32BIT: colourSpace = CGColorSpaceCreateDeviceRGB(); break;
511 default: colourSpace = NULL;
512 }
513
514 if ( ! colourSpace )
515 {
516 ErrorAlert("No valid colour space");
517 return false;
518 }
519
520 provider = CGDataProviderCreateWithData(NULL, the_buffer,
521 the_buffer_size, NULL);
522 if ( ! provider )
523 {
524 ErrorAlert("Could not create CGDataProvider from buffer data");
525 return false;
526 }
527 imageRef = CGImageCreate(mode.x,
528 mode.y,
529 bitsPer,
530 bits_from_depth(mode.depth),
531 mode.bytes_per_row,
532 colourSpace,
533 #ifdef CG_USE_ALPHA
534 kCGImageAlphaPremultipliedFirst,
535 #else
536 kCGImageAlphaNoneSkipFirst,
537 #endif
538 provider,
539 NULL, // colourMap translation table
540 NO, // shouldInterpolate colours?
541 kCGRenderingIntentDefault);
542 if ( ! imageRef )
543 {
544 ErrorAlert("Could not create CGImage from CGDataProvider");
545 return false;
546 }
547 CGDataProviderRelease(provider);
548 CGColorSpaceRelease(colourSpace);
549
550 [output readyToDraw: imageRef
551 imageWidth: mode.x
552 imageHeight: mode.y];
553
554 #ifdef CG_USE_ALPHA
555 mask_buffer(the_buffer, mode.x, the_buffer_size);
556 #endif
557 #else
558 unsigned char *offsetBuffer = (unsigned char *) the_buffer;
559 offsetBuffer += 1; // OS X NSBitmaps are RGBA, but Basilisk generates ARGB
560 #endif
561
562 #ifdef NSBITMAP
563 bitmap = [NSBitmapImageRep alloc];
564 bitmap = [bitmap initWithBitmapDataPlanes: (unsigned char **) &offsetBuffer
565 pixelsWide: mode.x
566 pixelsHigh: mode.y
567 bitsPerSample: bitsPer
568 samplesPerPixel: samplesPer
569 hasAlpha: NO
570 isPlanar: NO
571 colorSpaceName: NSCalibratedRGBColorSpace
572 bytesPerRow: mode.bytes_per_row
573 bitsPerPixel: bits_from_depth(mode.depth)];
574
575 if ( ! bitmap )
576 {
577 ErrorAlert("Could not allocate an NSBitmapImageRep");
578 return false;
579 }
580
581 [output readyToDraw: bitmap
582 imageWidth: mode.x
583 imageHeight: mode.y];
584 #endif
585
586 #ifdef CGDRAWBITMAP
587 [output readyToDraw: offsetBuffer
588 width: mode.x
589 height: mode.y
590 bps: bitsPer
591 spp: samplesPer
592 bpp: bits_from_depth(mode.depth)
593 bpr: mode.bytes_per_row
594 isPlanar: NO
595 hasAlpha: NO];
596 #endif
597
598 return true;
599 }
600
601 #import <AppKit/NSEvent.h>
602 #import <Carbon/Carbon.h>
603 #import "NNThread.h"
604
605 bool
606 OSX_monitor::init_screen(video_mode &mode)
607 {
608 // Set absolute mouse mode
609 ADBSetRelMouseMode(false);
610
611 // Display stored by add_CGDirectDisplay_modes()
612 theDisplay = (CGDirectDisplayID) mode.user_data;
613
614 originalMode = CGDisplayCurrentMode(theDisplay);
615 if ( ! originalMode )
616 {
617 ErrorSheet(@"Could not get current mode of display", the_win);
618 return false;
619 }
620
621 D(NSLog(@"About to call CGDisplayBestModeForParameters()"));
622 newMode = CGDisplayBestModeForParameters(theDisplay,
623 bits_from_depth(mode.depth),
624 mode.x, mode.y, NULL);
625 if ( ! newMode )
626 {
627 ErrorSheet(@"Could not find a matching screen mode", the_win);
628 return false;
629 }
630
631 // This sometimes takes ages to return after the window is genied,
632 // so for now we leave it onscreen
633 // [the_win miniaturize: nil];
634
635 D(NSLog(@"About to call CGDisplayCapture()"));
636 if ( CGDisplayCapture(theDisplay) != CGDisplayNoErr )
637 {
638 // [the_win deminiaturize: nil];
639 ErrorSheet(@"Could not capture display", the_win);
640 return false;
641 }
642
643 D(NSLog(@"About to call CGDisplaySwitchToMode()"));
644 if ( CGDisplaySwitchToMode(theDisplay, newMode) != CGDisplayNoErr )
645 {
646 CGDisplayRelease(theDisplay);
647 // [the_win deminiaturize: nil];
648 ErrorSheet(@"Could not switch to matching screen mode", the_win);
649 return false;
650 }
651
652 // For mouse event processing: update screen height
653 [output startedFullScreen: theDisplay];
654
655 the_buffer = CGDisplayBaseAddress(theDisplay);
656 if ( ! the_buffer )
657 {
658 CGDisplaySwitchToMode(theDisplay, originalMode);
659 CGDisplayRelease(theDisplay);
660 // [the_win deminiaturize: nil];
661 ErrorSheet(@"Could not get base address of screen", the_win);
662 return false;
663 }
664
665 D(NSLog(@"Starting full screen mode, height = %d",
666 CGDisplayPixelsHigh(theDisplay)));
667
668 [output startedFullScreen: theDisplay]; // For mouse event processing
669
670 if ( mode.bytes_per_row != CGDisplayBytesPerRow(theDisplay) )
671 {
672 D(bug("Bytes per row (%d) doesn't match current (%ld)\n",
673 mode.bytes_per_row, CGDisplayBytesPerRow(theDisplay)));
674 mode.bytes_per_row = CGDisplayBytesPerRow(theDisplay);
675 }
676
677 HideMenuBar();
678
679 if ( singleDisplay )
680 {
681 CGDisplayHideCursor(theDisplay);
682
683 // Send real mouse to emulated location
684 if ( CGDisplayMoveCursorToPoint(theDisplay, CGPointMake(15,15))
685 != CGDisplayNoErr )
686 {
687 video_close();
688 ErrorSheet(@"Could move (jump) cursor on screen", the_win);
689 return false;
690 }
691
692 // Send emulated mouse to current location
693 // [output performSelector: @selector(processMouseMove:)
694 // withObject: nil
695 // afterDelay: 7.0];
696 // NNTimer *moveMouse = [[NNTimer new] retain];
697 // [moveMouse perform: @selector(processMouseMove:)
698 // of: output
699 // after: 3
700 // units: NNseconds];
701 }
702 else
703 {
704 // Should set up something to hide the cursor when it enters theDisplay?
705 }
706
707 return true;
708 }
709
710
711 bool
712 OSX_monitor::init_opengl(const video_mode &mode)
713 {
714 ErrorAlert("Sorry. OpenGL mode is not implemented yet");
715 return false;
716 }
717
718 /*
719 * Initialization
720 */
721 static bool
722 monitor_init(const video_mode &init_mode)
723 {
724 OSX_monitor *monitor;
725 BOOL success;
726
727 monitor = new OSX_monitor(VideoModes, init_mode.depth,
728 init_mode.resolution_id);
729 success = monitor->video_open(init_mode);
730
731 if ( success )
732 {
733 monitor->set_mac_frame_buffer(init_mode);
734 VideoMonitors.push_back(monitor);
735 return YES;
736 }
737
738 return NO;
739 }
740
741 bool VideoInit(bool classic)
742 {
743 // Read frame skip prefs
744 frame_skip = PrefsFindInt32("frameskip");
745 if (frame_skip == 0)
746 frame_skip = 1;
747
748 // Get screen mode from preferences
749 const char *mode_str;
750 if (classic)
751 mode_str = "win/512/342";
752 else
753 mode_str = PrefsFindString("screen");
754
755 // Determine display_type and init_width, height & depth
756 parse_screen_prefs(mode_str);
757
758 // Construct list of supported modes
759 if (classic)
760 add_mode(512, 342, 0x80, 64, 0, VDEPTH_1BIT);
761 else
762 switch ( display_type )
763 {
764 case DISPLAY_SCREEN:
765 if ( ! add_CGDirectDisplay_modes() )
766 {
767 ErrorAlert("Unable to get list of displays for full screen mode");
768 return false;
769 }
770 break;
771 case DISPLAY_OPENGL:
772 // Same as window depths and sizes?
773 case DISPLAY_WINDOW:
774 //add_standard_modes(VDEPTH_1BIT);
775 //add_standard_modes(VDEPTH_8BIT);
776 //add_standard_modes(VDEPTH_16BIT);
777 add_standard_modes(VDEPTH_32BIT);
778 break;
779 }
780
781 // video_init_depth_list(); Now done in monitor_desc constructor?
782
783 #if DEBUG
784 bug("Available video modes:\n");
785 vector<video_mode>::const_iterator i, end = VideoModes.end();
786 for (i = VideoModes.begin(); i != end; ++i)
787 bug(" %dx%d (ID %02x), %s\n", i->x, i->y, i->resolution_id,
788 colours_from_depth(i->depth));
789 #endif
790
791 D(bug("VideoInit: width=%hd height=%hd depth=%d\n",
792 init_width, init_height, init_depth));
793
794 // Find requested default mode and open display
795 if (VideoModes.size() > 0)
796 {
797 // Find mode with specified dimensions
798 std::vector<video_mode>::const_iterator i, end = VideoModes.end();
799 for (i = VideoModes.begin(); i != end; ++i)
800 {
801 D(bug("VideoInit: w=%d h=%d d=%d\n",
802 i->x, i->y, bits_from_depth(i->depth)));
803 if (i->x == init_width && i->y == init_height
804 && bits_from_depth(i->depth) == init_depth)
805 return monitor_init(*i);
806 }
807 }
808
809 char str[150];
810 sprintf(str, "Cannot open selected video mode\r(%hd x %hd, %s).\r%s",
811 init_width, init_height,
812 colours_from_depth(init_depth), "Using lowest resolution");
813 WarningAlert(str);
814
815 return monitor_init(VideoModes[0]);
816 }
817
818
819 // Open display for specified mode
820 bool
821 OSX_monitor::video_open(const video_mode &mode)
822 {
823 D(bug("video_open: width=%d height=%d depth=%d bytes_per_row=%d\n",
824 mode.x, mode.y, bits_from_depth(mode.depth), mode.bytes_per_row));
825
826 // Open display
827 switch ( display_type )
828 {
829 case DISPLAY_WINDOW: return init_window(mode);
830 case DISPLAY_SCREEN: return init_screen((video_mode &)mode);
831 case DISPLAY_OPENGL: return init_opengl(mode);
832 }
833
834 return false;
835 }
836
837
838 void
839 OSX_monitor::video_close()
840 {
841 D(bug("video_close()\n"));
842
843 switch ( display_type ) {
844 case DISPLAY_WINDOW:
845 // Stop redraw thread
846 [output disableDrawing];
847
848 // Free frame buffer stuff
849 #ifdef CGIMAGEREF
850 CGImageRelease(imageRef);
851 #endif
852 #ifdef NSBITMAP
853 [bitmap release];
854 #endif
855 free(the_buffer);
856
857 break;
858
859 case DISPLAY_SCREEN:
860 if ( theDisplay && originalMode )
861 {
862 if ( singleDisplay )
863 CGDisplayShowCursor(theDisplay);
864 ShowMenuBar();
865 CGDisplaySwitchToMode(theDisplay, originalMode);
866 CGDisplayRelease(theDisplay);
867 //[the_win deminiaturize: nil];
868 }
869 break;
870
871 case DISPLAY_OPENGL:
872 break;
873 }
874 }
875
876
877 /*
878 * Deinitialization
879 */
880
881 void VideoExit(void)
882 {
883 // Close displays
884 vector<monitor_desc *>::iterator i, end;
885
886 end = VideoMonitors.end();
887
888 for (i = VideoMonitors.begin(); i != end; ++i)
889 dynamic_cast<OSX_monitor *>(*i)->video_close();
890 }
891
892
893 /*
894 * Set palette
895 */
896
897 void
898 OSX_monitor::set_palette(uint8 *pal, int num)
899 {
900 if ( [output isFullScreen] && CGDisplayCanSetPalette(theDisplay)
901 && ! IsDirectMode(get_current_mode()) )
902 {
903 CGDirectPaletteRef CGpal;
904 CGDisplayErr err;
905
906
907 CGpal = CGPaletteCreateWithByteSamples((CGDeviceByteColor *)pal, num);
908 err = CGDisplaySetPalette(theDisplay, CGpal);
909 if ( err != noErr )
910 NSLog(@"Failed to set palette, error = %d", err);
911 CGPaletteRelease(CGpal);
912 }
913 }
914
915
916 /*
917 * Switch video mode
918 */
919
920 void
921 OSX_monitor::switch_to_current_mode(void)
922 {
923 video_mode mode = get_current_mode();
924 char *failure = NULL;
925
926
927 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));
928
929 if ( display_type == DISPLAY_SCREEN && originalMode )
930 {
931 D(NSLog(@"About to call CGDisplayBestModeForParameters()"));
932 newMode = CGDisplayBestModeForParameters(theDisplay,
933 bits_from_depth(mode.depth),
934 mode.x, mode.y, NULL);
935 if ( ! newMode )
936 failure = "Could not find a matching screen mode";
937 else
938 {
939 D(NSLog(@"About to call CGDisplaySwitchToMode()"));
940 if ( CGDisplaySwitchToMode(theDisplay, newMode) != CGDisplayNoErr )
941 failure = "Could not switch to matching screen mode";
942 }
943
944 // For mouse event processing: update screen height
945 [output startedFullScreen: theDisplay];
946
947 if ( ! failure &&
948 mode.bytes_per_row != CGDisplayBytesPerRow(theDisplay) )
949 {
950 D(bug("Bytes per row (%d) doesn't match current (%ld)\n",
951 mode.bytes_per_row, CGDisplayBytesPerRow(theDisplay)));
952 mode.bytes_per_row = CGDisplayBytesPerRow(theDisplay);
953 }
954
955 if ( ! failure &&
956 ! ( the_buffer = CGDisplayBaseAddress(theDisplay) ) )
957 failure = "Could not get base address of screen";
958 else
959 // Send emulated mouse to current location
960 [output processMouseMove: nil];
961 }
962 else if ( ! video_open(mode) )
963 failure = "Could not video_open() requested mode";
964
965 if ( failure )
966 {
967 NSLog(@"In switch_to_current_mode():");
968 NSLog(@"%s.", failure);
969 video_close();
970 if ( display_type == DISPLAY_SCREEN )
971 ErrorAlert("Cannot switch screen to selected video mode");
972 else
973 ErrorAlert(STR_OPEN_WINDOW_ERR);
974 QuitEmulator();
975 }
976 else
977 set_mac_frame_buffer(mode);
978 }
979
980 /*
981 * Close down full-screen mode
982 * (if bringing up error alerts is unsafe while in full-screen mode)
983 */
984
985 void VideoQuitFullScreen(void)
986 {
987 }
988
989
990 /*
991 * Mac VBL interrupt
992 */
993
994 void VideoInterrupt(void)
995 {
996 }
997
998
999 // This function is called on non-threaded platforms from a timer interrupt
1000 void VideoRefresh(void)
1001 {
1002 }