ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/video_macosx.mm
Revision: 1.2
Committed: 2002-04-05T12:15:34Z (22 years, 3 months ago) by nigel
Branch: MAIN
Changes since 1.1: +13 -6 lines
Log Message:
Modify getCFint32() to cope with values of zero
(it was assuming 0 was an error condition)

File Contents

# Content
1 /*
2 * $Id: video_macosx.mm,v 1.2 2002/03/16 10:00:19 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 #include "debug.h"
41
42
43
44 // Global variables
45 uint8 display_type = DISPLAY_WINDOW, // These are used by PrefsEditor
46 frame_skip;
47 uint16 init_width = MIN_WIDTH, // as well as this code
48 init_height = MIN_HEIGHT,
49 init_depth = 32,
50 screen_height = MIN_HEIGHT; // Used by processMouseMove
51
52 EmulatorView *output = nil; // Set by [EmulatorView init]
53 NSWindow *the_win = nil; // Set by [Emulator awakeFromNib]
54 static void *the_buffer = NULL;
55
56
57 #import <Foundation/NSString.h> // Needed for NSLog(@"")
58
59 #ifdef CGIMAGEREF
60 static CGImageRef imageRef = nil;
61 #endif
62
63 #ifdef NSBITMAP
64 #import <AppKit/NSBitmapImageRep.h>
65
66 static NSBitmapImageRep *bitmap = nil;
67 #endif
68
69 // These record changes we made in setting full screen mode
70 CGDirectDisplayID theDisplay = NULL;
71 CFDictionaryRef originalMode = NULL,
72 newMode = NULL;
73
74
75
76 // Prototypes
77
78 static void add_mode (const uint16 width, const uint16 height,
79 const uint32 resolution_id,
80 const uint32 bytes_per_row,
81 const video_depth depth);
82 static void add_standard_modes (const video_depth depth);
83
84 static bool video_open (const video_mode &mode);
85 static void video_close (void);
86
87
88
89 /*
90 * Utility functions
91 */
92
93 uint8 bits_from_depth(const video_depth depth)
94 {
95 int bits = 1 << depth;
96 // if (bits == 16)
97 // bits = 15;
98 // else if (bits == 32)
99 // bits = 24;
100 return bits;
101 }
102
103 char *
104 colours_from_depth(const video_depth depth)
105 {
106 switch ( depth )
107 {
108 case VDEPTH_1BIT : return "Monochrome";
109 case VDEPTH_2BIT : return "4 colours";
110 case VDEPTH_4BIT : return "16 colours";
111 case VDEPTH_8BIT : return "256 colours";
112 case VDEPTH_16BIT: return "Thousands of colours";
113 case VDEPTH_32BIT: return "Millions of colours";
114 }
115
116 return "illegal colour depth";
117 }
118
119 char *
120 colours_from_depth(const uint16 depth)
121 {
122 return colours_from_depth(DepthModeForPixelDepth(depth) );
123 }
124
125 bool
126 parse_screen_prefs(const char *mode_str)
127 {
128 if (sscanf(mode_str, "win/%hd/%hd/%hd",
129 &init_width, &init_height, &init_depth) == 3)
130 display_type = DISPLAY_WINDOW;
131 else if (sscanf(mode_str, "win/%hd/%hd", &init_width, &init_height) == 2)
132 display_type = DISPLAY_WINDOW;
133 else if (strcmp(mode_str, "full") == 0)
134 display_type = DISPLAY_SCREEN;
135 else if (sscanf(mode_str, "full/%hd/%hd/%hd",
136 &init_width, &init_height, &init_depth) == 3)
137 display_type = DISPLAY_SCREEN;
138 else if (sscanf(mode_str, "full/%hd/%hd", &init_width, &init_height) == 2)
139 display_type = DISPLAY_SCREEN;
140 else if (sscanf(mode_str, "opengl/%hd/%hd/%hd",
141 &init_width, &init_height, &init_depth) == 3)
142 display_type = DISPLAY_OPENGL;
143 else if (sscanf(mode_str, "opengl/%hd/%hd", &init_width, &init_height) == 2)
144 display_type = DISPLAY_OPENGL;
145 else return false;
146
147 return true;
148 }
149
150
151 // Add mode to list of supported modes
152 static void
153 add_mode(const uint16 width, const uint16 height,
154 const uint32 resolution_id, const uint32 bytes_per_row,
155 const video_depth depth)
156 {
157 video_mode mode;
158 mode.x = width;
159 mode.y = height;
160 mode.resolution_id = resolution_id;
161 mode.bytes_per_row = bytes_per_row;
162 mode.depth = depth;
163
164 D(bug("Added video mode: w=%ld h=%ld d=%ld(%d bits)\n",
165 width, height, depth, bits_from_depth(depth) ));
166
167 VideoModes.push_back(mode);
168 }
169
170 // Add standard list of windowed modes for given color depth
171 static void add_standard_modes(const video_depth depth)
172 {
173 D(bug("add_standard_modes: depth=%ld(%d bits)\n",
174 depth, bits_from_depth(depth) ));
175
176 add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth);
177 add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth);
178 add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth);
179 add_mode(832, 624, 0x83, TrivialBytesPerRow(832, depth), depth);
180 add_mode(1024, 768, 0x84, TrivialBytesPerRow(1024, depth), depth);
181 add_mode(1152, 768, 0x85, TrivialBytesPerRow(1152, depth), depth);
182 add_mode(1152, 870, 0x86, TrivialBytesPerRow(1152, depth), depth);
183 add_mode(1280, 1024, 0x87, TrivialBytesPerRow(1280, depth), depth);
184 add_mode(1600, 1200, 0x88, TrivialBytesPerRow(1600, depth), depth);
185 }
186
187 // Helper function to get a 32bit int from a dictionary
188 static int32 getCFint32 (CFDictionaryRef dict, CFStringRef key)
189 {
190 CFNumberRef ref = CFDictionaryGetValue(dict, key);
191
192 if ( ref )
193 {
194 int32 val;
195
196 if ( CFNumberGetValue(ref, kCFNumberSInt32Type, &val) )
197 return val;
198 else
199 NSLog(@"getCFint32() - Failed to get the value %@", key);
200 }
201 else
202 NSLog(@"getCFint32() - Failed to get a 32bit int for %@", key);
203
204 return 0;
205 }
206
207 static bool add_CGDirectDisplay_modes()
208 {
209 #define kMaxDisplays 8
210 CGDirectDisplayID displays[kMaxDisplays];
211 CGDisplayErr err;
212 CGDisplayCount n;
213 int32 oldRes = 0,
214 res_id = 0x80;
215
216
217 err = CGGetActiveDisplayList(kMaxDisplays, displays, &n);
218 if ( err != CGDisplayNoErr )
219 return false;
220
221 for ( CGDisplayCount dc = 0; dc < n; ++dc )
222 {
223 CGDirectDisplayID d = displays[dc];
224 CFArrayRef m = CGDisplayAvailableModes(d);
225
226 if ( m == NULL ) // Store the current display mode
227 add_mode(CGDisplayPixelsWide(d),
228 CGDisplayPixelsHigh(d),
229 res_id++, CGDisplayBytesPerRow(d),
230 DepthModeForPixelDepth(CGDisplayBitsPerPixel(d)));
231 else
232 {
233 CFIndex nModes = CFArrayGetCount(m);
234
235 for ( CFIndex mc = 0; mc < nModes; ++mc )
236 {
237 CFDictionaryRef modeSpec = CFArrayGetValueAtIndex(m, mc);
238
239 int32 bpp = getCFint32(modeSpec, kCGDisplayBitsPerPixel);
240 int32 height = getCFint32(modeSpec, kCGDisplayHeight);
241 int32 mode = getCFint32(modeSpec, kCGDisplayMode);
242 int32 width = getCFint32(modeSpec, kCGDisplayWidth);
243 video_depth depth = DepthModeForPixelDepth(bpp);
244
245 if ( ! bpp || ! height || ! width )
246 {
247 NSLog(@"Could not get details of mode %d, display %d",
248 mc, dc);
249 return false;
250 }
251 #if DEBUG
252 else
253 NSLog(@"Display %ld, spec = %@", d, modeSpec);
254 #endif
255
256 add_mode(width, height, res_id,
257 TrivialBytesPerRow(width, depth), depth);
258
259 if ( ! oldRes )
260 oldRes = width * height;
261 else
262 if ( oldRes != width * height )
263 {
264 oldRes = width * height;
265 ++res_id;
266 }
267 }
268 }
269 }
270
271 return true;
272 }
273
274 // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
275 static void set_mac_frame_buffer(video_depth depth)
276 {
277 #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
278 switch ( depth )
279 {
280 // case VDEPTH_15BIT:
281 case VDEPTH_16BIT: MacFrameLayout = FLAYOUT_HOST_555; break;
282 // case VDEPTH_24BIT:
283 case VDEPTH_32BIT: MacFrameLayout = FLAYOUT_HOST_888; break;
284 default : MacFrameLayout = FLAYOUT_DIRECT;
285 }
286 VideoMonitor.mac_frame_base = MacFrameBaseMac;
287
288 // Set variables used by UAE memory banking
289 MacFrameBaseHost = the_buffer;
290 MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
291 InitFrameBufferMapping();
292 #else
293 VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
294 #endif
295 D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base));
296 }
297
298 void resizeWinBy(const short deltaX, const short deltaY)
299 {
300 NSRect rect = [the_win frame];
301
302 D(bug("resizeWinBy(%d,%d) - ", deltaX, deltaY));
303 D(bug("old x=%g, y=%g", rect.size.width, rect.size.height));
304
305 rect.size.width += deltaX;
306 rect.size.height += deltaY;
307
308 D(bug(", new x=%g, y=%g\n", rect.size.width, rect.size.height));
309
310 [the_win setFrame: rect display: YES];
311 rect = [the_win frame];
312 }
313
314 void resizeWinTo(const uint16 newWidth, const uint16 newHeight)
315 {
316 int deltaX = newWidth - [output width],
317 deltaY = newHeight - [output height];
318
319 D(bug("resizeWinTo(%d,%d)\n", newWidth, newHeight));
320
321 if ( deltaX || deltaY )
322 resizeWinBy(deltaX, deltaY);
323 }
324
325 // Open window
326 static bool init_window(const video_mode &mode)
327 {
328 #ifdef CGIMAGEREF
329 CGColorSpaceRef colourSpace;
330 CGDataProviderRef provider;
331 #endif
332 short bitsPer, samplesPer; // How big is each Pixel?
333 int the_buffer_size;
334
335 D(bug("init_window: depth=%ld(%d bits)\n",
336 mode.depth, bits_from_depth(mode.depth) ));
337
338
339 // Set absolute mouse mode
340 ADBSetRelMouseMode(false);
341
342
343 // Open window
344 if (the_win == NULL)
345 {
346 ErrorAlert(STR_OPEN_WINDOW_ERR);
347 return false;
348 }
349 resizeWinTo(mode.x, mode.y);
350
351
352 // Create frame buffer ("height + 2" for safety)
353 the_buffer_size = mode.bytes_per_row * (mode.y + 2);
354 the_buffer = calloc(the_buffer_size, 1);
355 if (the_buffer == NULL)
356 {
357 NSLog(@"calloc(%d) failed", the_buffer_size);
358 ErrorAlert(STR_NO_MEM_ERR);
359 return false;
360 }
361 D(bug("the_buffer = %p\n", the_buffer));
362
363
364 if ( mode.depth == VDEPTH_1BIT )
365 bitsPer = 1;
366 else
367 bitsPer = 8;
368
369 if ( mode.depth == VDEPTH_32BIT )
370 samplesPer = 3;
371 else
372 samplesPer = 1;
373
374 #ifdef CGIMAGEREF
375 switch ( mode.depth )
376 {
377 //case VDEPTH_1BIT: colourSpace = CGColorSpaceCreateDeviceMono(); break
378 case VDEPTH_8BIT: colourSpace = CGColorSpaceCreateDeviceGray(); break;
379 case VDEPTH_32BIT: colourSpace = CGColorSpaceCreateDeviceRGB(); break;
380 default: colourSpace = NULL;
381 }
382
383 if ( ! colourSpace )
384 {
385 ErrorAlert("No valid colour space");
386 return false;
387 }
388
389 provider = CGDataProviderCreateWithData(NULL, the_buffer,
390 the_buffer_size, NULL);
391 if ( ! provider )
392 {
393 ErrorAlert("Could not create CGDataProvider from buffer data");
394 return false;
395 }
396 imageRef = CGImageCreate(mode.x,
397 mode.y,
398 bitsPer,
399 bits_from_depth(mode.depth),
400 mode.bytes_per_row,
401 colourSpace,
402 kCGImageAlphaNoneSkipFirst,
403 provider,
404 NULL, // colourMap
405 NO, // shouldInterpolate
406 kCGRenderingIntentDefault);
407 if ( ! imageRef )
408 {
409 ErrorAlert("Could not create CGImage from CGDataProvider");
410 return false;
411 }
412 CGDataProviderRelease(provider);
413 CGColorSpaceRelease(colourSpace);
414
415 [output readyToDraw: imageRef
416 imageWidth: mode.x
417 imageHeight: mode.y];
418 #else
419 unsigned char *offsetBuffer = the_buffer;
420 offsetBuffer += 1; // OS X NSBitmaps are RGBA, but Basilisk generates ARGB
421 #endif
422
423 #ifdef NSBITMAP
424 bitmap = [NSBitmapImageRep alloc];
425 bitmap = [bitmap initWithBitmapDataPlanes: (unsigned char **) &offsetBuffer
426 pixelsWide: mode.x
427 pixelsHigh: mode.y
428 bitsPerSample: bitsPer
429 samplesPerPixel: samplesPer
430 hasAlpha: NO
431 isPlanar: NO
432 colorSpaceName: NSCalibratedRGBColorSpace
433 bytesPerRow: mode.bytes_per_row
434 bitsPerPixel: bits_from_depth(mode.depth)];
435
436 if ( bitmap == nil )
437 {
438 ErrorAlert("Could not allocate an NSBitmapImageRep");
439 return false;
440 }
441
442 [output readyToDraw: bitmap
443 imageWidth: mode.x
444 imageHeight: mode.y];
445 #endif
446
447 #ifdef CGDRAWBITMAP
448 [output readyToDraw: offsetBuffer
449 width: mode.x
450 height: mode.y
451 bps: bitsPer
452 spp: samplesPer
453 bpp: bits_from_depth(mode.depth)
454 bpr: mode.bytes_per_row
455 isPlanar: NO
456 hasAlpha: NO];
457 #endif
458
459 // Set VideoMonitor
460 VideoMonitor.mode = mode;
461 set_mac_frame_buffer(mode.depth);
462
463 return true;
464 }
465
466 #import <AppKit/NSEvent.h>
467
468 // How do I include this file?
469 // #import <Carbon/HIToolbox/Menus.h>
470 extern "C" void HideMenuBar(),
471 ShowMenuBar();
472
473 static bool init_screen(const video_mode &mode)
474 {
475 // Set absolute mouse mode
476 ADBSetRelMouseMode(false);
477
478
479 theDisplay = kCGDirectMainDisplay; // For now
480 originalMode = CGDisplayCurrentMode(theDisplay);
481
482 newMode = CGDisplayBestModeForParameters(theDisplay,
483 bits_from_depth(mode.depth),
484 mode.x, mode.y, NULL);
485 if ( NULL == newMode )
486 {
487 ErrorAlert("Could not find a matching screen mode");
488 return false;
489 }
490
491 [the_win miniaturize: nil];
492 // [the_win setLevel: CGShieldingWindowLevel()];
493 the_win = nil;
494
495 HideMenuBar();
496
497 CGDisplayCapture(theDisplay);
498
499 if ( CGDisplaySwitchToMode(theDisplay, newMode) != CGDisplayNoErr )
500 {
501 ErrorAlert("Could not switch to matching screen mode");
502 return false;
503 }
504
505 CGDisplayHideCursor(theDisplay);
506
507 the_buffer = CGDisplayBaseAddress(theDisplay);
508 if ( the_buffer == NULL )
509 {
510 ErrorAlert("Could not get base address of screen");
511 return false;
512 }
513
514 screen_height = mode.y; // For mouse co-ordinate flipping
515
516 // Send emulated mouse to current location
517
518 NSPoint mouse = [NSEvent mouseLocation];
519 ADBMouseMoved((int)mouse.x, screen_height - (int)mouse.y);
520
521 // Set VideoMonitor
522 VideoMonitor.mode = mode;
523 set_mac_frame_buffer(mode.depth);
524
525 return true;
526 }
527
528
529 static bool init_opengl(const video_mode &mode)
530 {
531 ErrorAlert("Sorry. OpenGL mode is not implemented yet");
532 return false;
533 }
534
535 /*
536 * Initialization
537 */
538
539 bool VideoInit(bool classic)
540 {
541 // Read frame skip prefs
542 frame_skip = PrefsFindInt32("frameskip");
543 if (frame_skip == 0)
544 frame_skip = 1;
545
546 // Get screen mode from preferences
547 const char *mode_str;
548 if (classic)
549 mode_str = "win/512/342";
550 else
551 mode_str = PrefsFindString("screen");
552
553 // Determine display_type and init_width, height & depth
554 parse_screen_prefs(mode_str);
555
556 // Construct list of supported modes
557 if (classic)
558 add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
559 else
560 switch ( display_type )
561 {
562 case DISPLAY_SCREEN:
563 if ( ! add_CGDirectDisplay_modes() )
564 {
565 ErrorAlert("Unable to get list of displays for full screen mode");
566 return false;
567 }
568 break;
569 case DISPLAY_OPENGL:
570 // Same as window depths and sizes?
571 case DISPLAY_WINDOW:
572 //add_standard_modes(VDEPTH_1BIT);
573 //add_standard_modes(VDEPTH_8BIT);
574 //add_standard_modes(VDEPTH_16BIT);
575 add_standard_modes(VDEPTH_32BIT);
576 break;
577 }
578
579 video_init_depth_list();
580
581 #if DEBUG
582 bug("Available video modes:\n");
583 vector<video_mode>::const_iterator i, end = VideoModes.end();
584 for (i = VideoModes.begin(); i != end; ++i)
585 bug(" %dx%d (ID %02x), %s\n", i->x, i->y, i->resolution_id,
586 colours_from_depth(i->depth));
587 #endif
588
589 D(bug("VideoInit: width=%hd height=%hd depth=%ld\n",
590 init_width, init_height, init_depth));
591
592 // Find requested default mode and open display
593 if (VideoModes.size() > 0)
594 {
595 // Find mode with specified dimensions
596 std::vector<video_mode>::const_iterator i, end = VideoModes.end();
597 for (i = VideoModes.begin(); i != end; ++i)
598 {
599 D(bug("VideoInit: w=%ld h=%ld d=%ld\n",
600 i->x, i->y, bits_from_depth(i->depth)));
601 if (i->x == init_width && i->y == init_height
602 && bits_from_depth(i->depth) == init_depth)
603 return video_open(*i);
604 }
605 }
606
607 char str[150];
608 sprintf(str, "Cannot open selected video mode\r(%hd x %hd, %s).\r%s",
609 init_width, init_height,
610 colours_from_depth(init_depth), "Using lowest resolution");
611 WarningAlert(str);
612
613 return video_open(VideoModes[0]);
614 }
615
616
617 // Open display for specified mode
618 static bool video_open(const video_mode &mode)
619 {
620 D(bug("video_open: width=%ld height=%ld depth=%ld bytes_per_row=%ld\n",
621 mode.x, mode.y, bits_from_depth(mode.depth), mode.bytes_per_row));
622
623 // Open display
624 switch ( display_type ) {
625 case DISPLAY_WINDOW:
626 if ( ! init_window(mode) )
627 return false;
628 break;
629
630 case DISPLAY_SCREEN:
631 if ( ! init_screen(mode) )
632 return false;
633 break;
634
635 case DISPLAY_OPENGL:
636 if ( ! init_opengl(mode) )
637 return false;
638 break;
639 }
640
641 return true;
642 }
643
644
645 static void video_close()
646 {
647 D(bug("video_close()\n"));
648
649 switch ( display_type ) {
650 case DISPLAY_WINDOW:
651 // Stop redraw thread
652 [output disableDrawing];
653
654 // Free frame buffer stuff
655 #ifdef CGIMAGEREF
656 CGImageRelease(imageRef);
657 #endif
658 #ifdef NSBITMAP
659 [bitmap release];
660 #endif
661 free(the_buffer);
662
663 break;
664
665 case DISPLAY_SCREEN:
666 if ( theDisplay && originalMode )
667 {
668 //CGDisplayShowCursor(theDisplay);
669 CGDisplaySwitchToMode(theDisplay, originalMode);
670 CGDisplayRelease(theDisplay);
671 ShowMenuBar();
672 }
673 break;
674
675 case DISPLAY_OPENGL:
676 break;
677 }
678 }
679
680
681 /*
682 * Deinitialization
683 */
684
685 void VideoExit(void)
686 {
687 video_close();
688 }
689
690
691 /*
692 * Set palette
693 */
694
695 void video_set_palette(uint8 *pal, int num)
696 {
697 if ( FULLSCREEN && CGDisplayCanSetPalette(theDisplay)
698 && ! IsDirectMode(VideoMonitor.mode) )
699 {
700 CGDirectPaletteRef CGpal;
701 CGDisplayErr err;
702
703
704 CGpal = CGPaletteCreateWithByteSamples((CGDeviceByteColor *)pal, num);
705 err = CGDisplaySetPalette(theDisplay, CGpal);
706 if ( err != noErr )
707 NSLog(@"Failed to set palette, error = %d", err);
708 CGPaletteRelease(CGpal);
709 }
710 }
711
712
713 /*
714 * Switch video mode
715 */
716
717 void video_switch_to_mode(const video_mode &mode)
718 {
719 // Close and reopen display
720 video_close();
721 if (!video_open(mode))
722 {
723 if ( display_type == DISPLAY_SCREEN )
724 ErrorAlert("Cannot switch screen to selected video mode");
725 else
726 ErrorAlert(STR_OPEN_WINDOW_ERR);
727 QuitEmulator();
728 }
729 }
730
731 /*
732 * Close down full-screen mode
733 * (if bringing up error alerts is unsafe while in full-screen mode)
734 */
735
736 void VideoQuitFullScreen(void)
737 {
738 }
739
740
741 /*
742 * Mac VBL interrupt
743 */
744
745 void VideoInterrupt(void)
746 {
747 }
748
749
750 // This function is called on non-threaded platforms from a timer interrupt
751 void VideoRefresh(void)
752 {
753 }