ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/video_macosx.mm
Revision: 1.9
Committed: 2003-03-25T01:46:45Z (21 years, 7 months ago) by nigel
Branch: MAIN
Changes since 1.8: +25 -1 lines
Log Message:
When in CGIMAGEREF windowed mode; cleanup
when changing modes, future mask generation code.

File Contents

# User Rev Content
1 nigel 1.1 /*
2 nigel 1.9 * $Id: video_macosx.mm,v 1.8 2003/03/21 06:29:28 nigel Exp $
3 nigel 1.1 *
4     * video_macosx.mm - Interface between Basilisk II and Cocoa windowing.
5     * Based on video_amiga.cpp and video_x.cpp
6     *
7 nigel 1.8 * Basilisk II (C) 1997-2003 Christian Bauer
8 nigel 1.1 *
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 nigel 1.6 #define VERBOSE 0
41 nigel 1.1 #include "debug.h"
42    
43 nigel 1.5 #ifdef NSBITMAP
44     #import <AppKit/NSBitmapImageRep.h>
45     #endif
46    
47 nigel 1.3 #import <Foundation/NSString.h> // Needed for NSLog(@"")
48     #import "misc_macosx.h" // WarningSheet() prototype
49    
50 nigel 1.1
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 nigel 1.4 init_depth = 32;
58 nigel 1.1
59     EmulatorView *output = nil; // Set by [EmulatorView init]
60     NSWindow *the_win = nil; // Set by [Emulator awakeFromNib]
61    
62 nigel 1.5 static BOOL singleDisplay = YES;
63 nigel 1.1
64     /*
65     * Utility functions
66     */
67    
68 nigel 1.4 static uint8
69     bits_from_depth(const video_depth depth)
70 nigel 1.1 {
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 nigel 1.4 static char *
80 nigel 1.1 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 nigel 1.4 static char *
96 nigel 1.1 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 nigel 1.5 // Supported video modes
127     static vector<video_mode> VideoModes;
128    
129 nigel 1.1
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 nigel 1.5 const uint32 user_data,
135 nigel 1.1 const video_depth depth)
136     {
137 nigel 1.3 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 nigel 1.1 video_mode mode;
150 nigel 1.3
151 nigel 1.1 mode.x = width;
152     mode.y = height;
153     mode.resolution_id = resolution_id;
154     mode.bytes_per_row = bytes_per_row;
155 nigel 1.5 mode.user_data = user_data;
156 nigel 1.1 mode.depth = depth;
157    
158 nigel 1.3 D(bug("Added video mode: w=%d h=%d d=%d(%d bits)\n",
159 nigel 1.1 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 nigel 1.3 D(bug("add_standard_modes: depth=%d(%d bits)\n",
168 nigel 1.1 depth, bits_from_depth(depth) ));
169    
170 nigel 1.5 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 nigel 1.1 }
180    
181     // Helper function to get a 32bit int from a dictionary
182     static int32 getCFint32 (CFDictionaryRef dict, CFStringRef key)
183     {
184 nigel 1.6 CFNumberRef ref = (CFNumberRef) CFDictionaryGetValue(dict, key);
185 nigel 1.1
186 nigel 1.2 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 nigel 1.1
198 nigel 1.2 return 0;
199 nigel 1.1 }
200    
201 nigel 1.6 // Nasty hack. Under 10.1, CGDisplayAvailableModes() does not provide bytes per row,
202 nigel 1.5 // 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 nigel 1.1 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 nigel 1.3 n = 1, displays[n] = kCGDirectMainDisplay;
241 nigel 1.1
242 nigel 1.4 if ( n > 1 )
243     singleDisplay = NO;
244    
245 nigel 1.1 for ( CGDisplayCount dc = 0; dc < n; ++dc )
246     {
247     CGDirectDisplayID d = displays[dc];
248     CFArrayRef m = CGDisplayAvailableModes(d);
249    
250 nigel 1.4 if ( ! m ) // Store the current display mode
251 nigel 1.1 add_mode(CGDisplayPixelsWide(d),
252     CGDisplayPixelsHigh(d),
253     res_id++, CGDisplayBytesPerRow(d),
254 nigel 1.5 (const uint32) d,
255 nigel 1.1 DepthModeForPixelDepth(CGDisplayBitsPerPixel(d)));
256     else
257     {
258     CFIndex nModes = CFArrayGetCount(m);
259    
260     for ( CFIndex mc = 0; mc < nModes; ++mc )
261     {
262 nigel 1.6 CFDictionaryRef modeSpec = (CFDictionaryRef)
263     CFArrayGetValueAtIndex(m, mc);
264 nigel 1.1
265     int32 bpp = getCFint32(modeSpec, kCGDisplayBitsPerPixel);
266     int32 height = getCFint32(modeSpec, kCGDisplayHeight);
267     int32 width = getCFint32(modeSpec, kCGDisplayWidth);
268 nigel 1.6 #ifdef MAC_OS_X_VERSION_10_2
269     int32 bytes = getCFint32(modeSpec, kCGDisplayBytesPerRow);
270     #else
271 nigel 1.7 int32 bytes = 0;
272 nigel 1.6 #endif
273 nigel 1.1 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 nigel 1.3 #if VERBOSE
282 nigel 1.1 else
283     NSLog(@"Display %ld, spec = %@", d, modeSpec);
284     #endif
285    
286 nigel 1.6 if ( ! bytes )
287     {
288     NSLog(@"Could not get bytes per row, guessing");
289     bytes = CGBytesPerRow(width, depth);
290     }
291    
292 nigel 1.1 if ( ! oldRes )
293     oldRes = width * height;
294     else
295     if ( oldRes != width * height )
296     {
297     oldRes = width * height;
298     ++res_id;
299     }
300 nigel 1.3
301 nigel 1.6 add_mode(width, height, res_id, bytes, (const uint32) d, depth);
302 nigel 1.1 }
303     }
304     }
305    
306     return true;
307     }
308    
309 nigel 1.6 #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 nigel 1.5
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 nigel 1.8 CGColorSpaceRef colourSpace;
370     uint8 *colourTable;
371 nigel 1.5 CGImageRef imageRef;
372 nigel 1.8 CGDataProviderRef provider;
373     short x, y, bpp, depth, bpr;
374 nigel 1.5 #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 nigel 1.8 colourSpace = nil;
396     colourTable = (uint8 *) malloc(256 * 3);
397 nigel 1.5 imageRef = nil;
398 nigel 1.8 provider = nil;
399 nigel 1.5 #endif
400     #ifdef NSBITMAP
401     bitmap = nil;
402     #endif
403     newMode = originalMode = nil;
404     the_buffer = NULL;
405     theDisplay = nil;
406     };
407    
408 nigel 1.8 // Should also have a destructor which does
409     //#ifdef CGIMAGEREF
410     // free(colourTable);
411     //#endif
412    
413 nigel 1.5
414 nigel 1.1 // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
415 nigel 1.5 void
416     OSX_monitor::set_mac_frame_buffer(const video_mode mode)
417 nigel 1.1 {
418     #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
419 nigel 1.5 switch ( mode.depth )
420 nigel 1.1 {
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 nigel 1.5 set_mac_frame_base(MacFrameBaseMac);
428 nigel 1.1
429     // Set variables used by UAE memory banking
430 nigel 1.6 MacFrameBaseHost = (uint8 *) the_buffer;
431 nigel 1.5 MacFrameSize = mode.bytes_per_row * mode.y;
432 nigel 1.1 InitFrameBufferMapping();
433     #else
434 nigel 1.6 set_mac_frame_base((unsigned int)Host2MacAddr((uint8 *)the_buffer));
435 nigel 1.1 #endif
436 nigel 1.5 D(bug("mac_frame_base = %08x\n", get_mac_frame_base()));
437 nigel 1.1 }
438    
439 nigel 1.4 static void
440     resizeWinBy(const short deltaX, const short deltaY)
441 nigel 1.1 {
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 nigel 1.5 bool
469     OSX_monitor::init_window(const video_mode &mode)
470 nigel 1.1 {
471 nigel 1.3 D(bug("init_window: depth=%d(%d bits)\n",
472 nigel 1.1 mode.depth, bits_from_depth(mode.depth) ));
473    
474    
475     // Set absolute mouse mode
476     ADBSetRelMouseMode(false);
477    
478    
479 nigel 1.8 // Is the window open?
480 nigel 1.4 if ( ! the_win )
481 nigel 1.1 {
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 nigel 1.8 int the_buffer_size = mode.bytes_per_row * (mode.y + 2);
490    
491 nigel 1.1 the_buffer = calloc(the_buffer_size, 1);
492 nigel 1.4 if ( ! the_buffer )
493 nigel 1.1 {
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 nigel 1.8 unsigned char *offsetBuffer = (unsigned char *) the_buffer;
502     offsetBuffer += 1; // OS X NSBitmaps are RGBA, but Basilisk generates ARGB
503 nigel 1.1
504     #ifdef CGIMAGEREF
505     switch ( mode.depth )
506     {
507 nigel 1.8 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 nigel 1.1 }
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 nigel 1.8
542     imageRef = CGImageCreate(x, y, bpp, depth, bpr, colourSpace,
543 nigel 1.6 #ifdef CG_USE_ALPHA
544     kCGImageAlphaPremultipliedFirst,
545     #else
546 nigel 1.1 kCGImageAlphaNoneSkipFirst,
547 nigel 1.6 #endif
548 nigel 1.1 provider,
549 nigel 1.6 NULL, // colourMap translation table
550     NO, // shouldInterpolate colours?
551 nigel 1.1 kCGRenderingIntentDefault);
552     if ( ! imageRef )
553     {
554     ErrorAlert("Could not create CGImage from CGDataProvider");
555     return false;
556     }
557    
558     [output readyToDraw: imageRef
559 nigel 1.8 bitmap: offsetBuffer
560     imageWidth: x
561     imageHeight: y];
562    
563 nigel 1.6
564     #ifdef CG_USE_ALPHA
565 nigel 1.8 mask_buffer(the_buffer, x, the_buffer_size);
566 nigel 1.9
567     /* Create an image mask with this call? */
568     //CG_EXTERN CGImageRef
569     //CGImageMaskCreate(size_t width, size_t height, size_t bitsPerComponent,
570     // size_t bitsPerPixel, size_t bytesPerRow,
571     // CGDataProviderRef provider, const float decode[], bool shouldInterpolate);
572 nigel 1.6 #endif
573 nigel 1.8
574     return true;
575     #endif
576    
577    
578     #ifndef CGIMAGEREF
579     short bitsPer, samplesPer; // How big is each Pixel?
580    
581     if ( mode.depth == VDEPTH_1BIT )
582     bitsPer = 1;
583     else
584     bitsPer = 8;
585    
586     if ( mode.depth == VDEPTH_32BIT )
587     samplesPer = 3;
588     else
589     samplesPer = 1;
590 nigel 1.1 #endif
591    
592 nigel 1.8
593 nigel 1.1 #ifdef NSBITMAP
594     bitmap = [NSBitmapImageRep alloc];
595     bitmap = [bitmap initWithBitmapDataPlanes: (unsigned char **) &offsetBuffer
596     pixelsWide: mode.x
597     pixelsHigh: mode.y
598     bitsPerSample: bitsPer
599     samplesPerPixel: samplesPer
600     hasAlpha: NO
601     isPlanar: NO
602     colorSpaceName: NSCalibratedRGBColorSpace
603     bytesPerRow: mode.bytes_per_row
604     bitsPerPixel: bits_from_depth(mode.depth)];
605    
606 nigel 1.4 if ( ! bitmap )
607 nigel 1.1 {
608     ErrorAlert("Could not allocate an NSBitmapImageRep");
609     return false;
610     }
611    
612     [output readyToDraw: bitmap
613     imageWidth: mode.x
614     imageHeight: mode.y];
615     #endif
616    
617     #ifdef CGDRAWBITMAP
618     [output readyToDraw: offsetBuffer
619     width: mode.x
620     height: mode.y
621     bps: bitsPer
622     spp: samplesPer
623     bpp: bits_from_depth(mode.depth)
624     bpr: mode.bytes_per_row
625     isPlanar: NO
626     hasAlpha: NO];
627     #endif
628    
629     return true;
630     }
631    
632     #import <AppKit/NSEvent.h>
633 nigel 1.3 #import <Carbon/Carbon.h>
634 nigel 1.5 #import "NNThread.h"
635 nigel 1.1
636 nigel 1.5 bool
637     OSX_monitor::init_screen(video_mode &mode)
638 nigel 1.1 {
639     // Set absolute mouse mode
640     ADBSetRelMouseMode(false);
641    
642 nigel 1.5 // Display stored by add_CGDirectDisplay_modes()
643     theDisplay = (CGDirectDisplayID) mode.user_data;
644 nigel 1.1
645     originalMode = CGDisplayCurrentMode(theDisplay);
646 nigel 1.4 if ( ! originalMode )
647 nigel 1.3 {
648     ErrorSheet(@"Could not get current mode of display", the_win);
649     return false;
650     }
651 nigel 1.1
652 nigel 1.3 D(NSLog(@"About to call CGDisplayBestModeForParameters()"));
653 nigel 1.1 newMode = CGDisplayBestModeForParameters(theDisplay,
654     bits_from_depth(mode.depth),
655     mode.x, mode.y, NULL);
656 nigel 1.4 if ( ! newMode )
657 nigel 1.1 {
658 nigel 1.3 ErrorSheet(@"Could not find a matching screen mode", the_win);
659 nigel 1.1 return false;
660     }
661    
662 nigel 1.3 // This sometimes takes ages to return after the window is genied,
663     // so for now we leave it onscreen
664     // [the_win miniaturize: nil];
665 nigel 1.1
666 nigel 1.3 D(NSLog(@"About to call CGDisplayCapture()"));
667     if ( CGDisplayCapture(theDisplay) != CGDisplayNoErr )
668     {
669     // [the_win deminiaturize: nil];
670     ErrorSheet(@"Could not capture display", the_win);
671     return false;
672     }
673 nigel 1.1
674 nigel 1.3 D(NSLog(@"About to call CGDisplaySwitchToMode()"));
675 nigel 1.1 if ( CGDisplaySwitchToMode(theDisplay, newMode) != CGDisplayNoErr )
676     {
677 nigel 1.4 CGDisplayRelease(theDisplay);
678 nigel 1.3 // [the_win deminiaturize: nil];
679     ErrorSheet(@"Could not switch to matching screen mode", the_win);
680 nigel 1.1 return false;
681     }
682    
683 nigel 1.5 the_buffer = CGDisplayBaseAddress(theDisplay);
684     if ( ! the_buffer )
685     {
686     CGDisplaySwitchToMode(theDisplay, originalMode);
687     CGDisplayRelease(theDisplay);
688     // [the_win deminiaturize: nil];
689     ErrorSheet(@"Could not get base address of screen", the_win);
690     return false;
691     }
692 nigel 1.6
693 nigel 1.3 if ( mode.bytes_per_row != CGDisplayBytesPerRow(theDisplay) )
694     {
695     D(bug("Bytes per row (%d) doesn't match current (%ld)\n",
696     mode.bytes_per_row, CGDisplayBytesPerRow(theDisplay)));
697     mode.bytes_per_row = CGDisplayBytesPerRow(theDisplay);
698     }
699    
700     HideMenuBar();
701 nigel 1.4
702     if ( singleDisplay )
703     {
704     CGDisplayHideCursor(theDisplay);
705    
706 nigel 1.7 [output startedFullScreen: theDisplay];
707 nigel 1.5
708     // Send emulated mouse to current location
709 nigel 1.7 [output fullscreenMouseMove];
710 nigel 1.4 }
711     else
712     {
713     // Should set up something to hide the cursor when it enters theDisplay?
714     }
715 nigel 1.1
716     return true;
717     }
718    
719 nigel 1.5
720     bool
721     OSX_monitor::init_opengl(const video_mode &mode)
722 nigel 1.1 {
723     ErrorAlert("Sorry. OpenGL mode is not implemented yet");
724     return false;
725     }
726    
727     /*
728     * Initialization
729     */
730 nigel 1.5 static bool
731     monitor_init(const video_mode &init_mode)
732     {
733     OSX_monitor *monitor;
734     BOOL success;
735    
736     monitor = new OSX_monitor(VideoModes, init_mode.depth,
737     init_mode.resolution_id);
738     success = monitor->video_open(init_mode);
739    
740     if ( success )
741     {
742     monitor->set_mac_frame_buffer(init_mode);
743     VideoMonitors.push_back(monitor);
744     return YES;
745     }
746    
747     return NO;
748     }
749 nigel 1.1
750     bool VideoInit(bool classic)
751     {
752     // Read frame skip prefs
753     frame_skip = PrefsFindInt32("frameskip");
754     if (frame_skip == 0)
755     frame_skip = 1;
756    
757     // Get screen mode from preferences
758     const char *mode_str;
759     if (classic)
760     mode_str = "win/512/342";
761     else
762     mode_str = PrefsFindString("screen");
763    
764     // Determine display_type and init_width, height & depth
765     parse_screen_prefs(mode_str);
766    
767     // Construct list of supported modes
768     if (classic)
769 nigel 1.5 add_mode(512, 342, 0x80, 64, 0, VDEPTH_1BIT);
770 nigel 1.1 else
771     switch ( display_type )
772     {
773     case DISPLAY_SCREEN:
774     if ( ! add_CGDirectDisplay_modes() )
775     {
776     ErrorAlert("Unable to get list of displays for full screen mode");
777     return false;
778     }
779     break;
780     case DISPLAY_OPENGL:
781     // Same as window depths and sizes?
782     case DISPLAY_WINDOW:
783 nigel 1.8 #ifdef CGIMAGEREF
784 nigel 1.7 add_standard_modes(VDEPTH_1BIT);
785 nigel 1.8 add_standard_modes(VDEPTH_2BIT);
786     add_standard_modes(VDEPTH_4BIT);
787 nigel 1.7 add_standard_modes(VDEPTH_8BIT);
788     add_standard_modes(VDEPTH_16BIT);
789     #endif
790 nigel 1.1 add_standard_modes(VDEPTH_32BIT);
791     break;
792     }
793    
794 nigel 1.5 // video_init_depth_list(); Now done in monitor_desc constructor?
795 nigel 1.1
796     #if DEBUG
797     bug("Available video modes:\n");
798     vector<video_mode>::const_iterator i, end = VideoModes.end();
799     for (i = VideoModes.begin(); i != end; ++i)
800     bug(" %dx%d (ID %02x), %s\n", i->x, i->y, i->resolution_id,
801     colours_from_depth(i->depth));
802     #endif
803    
804 nigel 1.3 D(bug("VideoInit: width=%hd height=%hd depth=%d\n",
805 nigel 1.1 init_width, init_height, init_depth));
806    
807     // Find requested default mode and open display
808     if (VideoModes.size() > 0)
809     {
810     // Find mode with specified dimensions
811     std::vector<video_mode>::const_iterator i, end = VideoModes.end();
812     for (i = VideoModes.begin(); i != end; ++i)
813     {
814 nigel 1.3 D(bug("VideoInit: w=%d h=%d d=%d\n",
815 nigel 1.1 i->x, i->y, bits_from_depth(i->depth)));
816     if (i->x == init_width && i->y == init_height
817     && bits_from_depth(i->depth) == init_depth)
818 nigel 1.5 return monitor_init(*i);
819 nigel 1.1 }
820     }
821    
822     char str[150];
823     sprintf(str, "Cannot open selected video mode\r(%hd x %hd, %s).\r%s",
824     init_width, init_height,
825     colours_from_depth(init_depth), "Using lowest resolution");
826     WarningAlert(str);
827    
828 nigel 1.5 return monitor_init(VideoModes[0]);
829 nigel 1.1 }
830    
831    
832     // Open display for specified mode
833 nigel 1.5 bool
834     OSX_monitor::video_open(const video_mode &mode)
835 nigel 1.1 {
836 nigel 1.3 D(bug("video_open: width=%d height=%d depth=%d bytes_per_row=%d\n",
837 nigel 1.1 mode.x, mode.y, bits_from_depth(mode.depth), mode.bytes_per_row));
838    
839     // Open display
840 nigel 1.3 switch ( display_type )
841     {
842     case DISPLAY_WINDOW: return init_window(mode);
843     case DISPLAY_SCREEN: return init_screen((video_mode &)mode);
844     case DISPLAY_OPENGL: return init_opengl(mode);
845 nigel 1.1 }
846    
847 nigel 1.3 return false;
848 nigel 1.1 }
849    
850    
851 nigel 1.5 void
852     OSX_monitor::video_close()
853 nigel 1.1 {
854     D(bug("video_close()\n"));
855    
856     switch ( display_type ) {
857     case DISPLAY_WINDOW:
858     // Stop redraw thread
859     [output disableDrawing];
860    
861     // Free frame buffer stuff
862     #ifdef CGIMAGEREF
863     CGImageRelease(imageRef);
864 nigel 1.8 CGColorSpaceRelease(colourSpace);
865     CGDataProviderRelease(provider);
866 nigel 1.1 #endif
867     #ifdef NSBITMAP
868     [bitmap release];
869     #endif
870     free(the_buffer);
871    
872     break;
873    
874     case DISPLAY_SCREEN:
875     if ( theDisplay && originalMode )
876     {
877 nigel 1.4 if ( singleDisplay )
878     CGDisplayShowCursor(theDisplay);
879 nigel 1.3 ShowMenuBar();
880 nigel 1.1 CGDisplaySwitchToMode(theDisplay, originalMode);
881     CGDisplayRelease(theDisplay);
882 nigel 1.3 //[the_win deminiaturize: nil];
883 nigel 1.1 }
884     break;
885    
886     case DISPLAY_OPENGL:
887     break;
888     }
889     }
890    
891    
892     /*
893     * Deinitialization
894     */
895    
896     void VideoExit(void)
897     {
898 nigel 1.5 // Close displays
899     vector<monitor_desc *>::iterator i, end;
900    
901     end = VideoMonitors.end();
902    
903     for (i = VideoMonitors.begin(); i != end; ++i)
904     dynamic_cast<OSX_monitor *>(*i)->video_close();
905 nigel 1.1 }
906    
907    
908     /*
909     * Set palette
910     */
911    
912 nigel 1.5 void
913     OSX_monitor::set_palette(uint8 *pal, int num)
914 nigel 1.1 {
915 nigel 1.3 if ( [output isFullScreen] && CGDisplayCanSetPalette(theDisplay)
916 nigel 1.5 && ! IsDirectMode(get_current_mode()) )
917 nigel 1.1 {
918     CGDirectPaletteRef CGpal;
919     CGDisplayErr err;
920    
921    
922     CGpal = CGPaletteCreateWithByteSamples((CGDeviceByteColor *)pal, num);
923     err = CGDisplaySetPalette(theDisplay, CGpal);
924     if ( err != noErr )
925     NSLog(@"Failed to set palette, error = %d", err);
926     CGPaletteRelease(CGpal);
927     }
928 nigel 1.8
929     #ifdef CGIMAGEREF
930     if ( display_type != DISPLAY_WINDOW )
931     return;
932    
933     // To change the palette, we have to regenerate
934     // the CGImageRef with the new colour space.
935    
936     CGImageRef oldImageRef = imageRef;
937     CGColorSpaceRef oldColourSpace = colourSpace;
938    
939     colourSpace = CGColorSpaceCreateDeviceRGB();
940    
941     if ( depth < 16 )
942     {
943     CGColorSpaceRef tempColourSpace = colourSpace;
944    
945     colourSpace = CGColorSpaceCreateIndexed(colourSpace, 255, pal);
946     CGColorSpaceRelease(tempColourSpace);
947     }
948    
949     if ( ! colourSpace )
950     {
951     ErrorAlert("No valid colour space");
952     return;
953     }
954    
955     imageRef = CGImageCreate(x, y, bpp, depth, bpr, colourSpace,
956     #ifdef CG_USE_ALPHA
957     kCGImageAlphaPremultipliedFirst,
958     #else
959     kCGImageAlphaNoneSkipFirst,
960     #endif
961     provider,
962     NULL, // colourMap translation table
963     NO, // shouldInterpolate colours?
964     kCGRenderingIntentDefault);
965     if ( ! imageRef )
966     {
967     ErrorAlert("Could not create CGImage from CGDataProvider");
968     return;
969     }
970    
971     unsigned char *offsetBuffer = (unsigned char *) the_buffer;
972     offsetBuffer += 1; // OS X NSBitmaps are RGBA, but Basilisk generates ARGB
973    
974     [output readyToDraw: imageRef
975     bitmap: offsetBuffer
976     imageWidth: x
977     imageHeight: y];
978    
979     CGColorSpaceRelease(oldColourSpace);
980     CGImageRelease(oldImageRef);
981     #endif
982 nigel 1.1 }
983    
984    
985     /*
986     * Switch video mode
987     */
988    
989 nigel 1.5 void
990     OSX_monitor::switch_to_current_mode(void)
991 nigel 1.1 {
992 nigel 1.5 video_mode mode = get_current_mode();
993 nigel 1.4 char *failure = NULL;
994    
995    
996     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));
997 nigel 1.5
998 nigel 1.4 if ( display_type == DISPLAY_SCREEN && originalMode )
999     {
1000     D(NSLog(@"About to call CGDisplayBestModeForParameters()"));
1001     newMode = CGDisplayBestModeForParameters(theDisplay,
1002     bits_from_depth(mode.depth),
1003     mode.x, mode.y, NULL);
1004     if ( ! newMode )
1005     failure = "Could not find a matching screen mode";
1006     else
1007     {
1008     D(NSLog(@"About to call CGDisplaySwitchToMode()"));
1009     if ( CGDisplaySwitchToMode(theDisplay, newMode) != CGDisplayNoErr )
1010     failure = "Could not switch to matching screen mode";
1011     }
1012    
1013 nigel 1.5 // For mouse event processing: update screen height
1014     [output startedFullScreen: theDisplay];
1015    
1016 nigel 1.4 if ( ! failure &&
1017     mode.bytes_per_row != CGDisplayBytesPerRow(theDisplay) )
1018     {
1019     D(bug("Bytes per row (%d) doesn't match current (%ld)\n",
1020     mode.bytes_per_row, CGDisplayBytesPerRow(theDisplay)));
1021 nigel 1.5 mode.bytes_per_row = CGDisplayBytesPerRow(theDisplay);
1022 nigel 1.4 }
1023    
1024     if ( ! failure &&
1025     ! ( the_buffer = CGDisplayBaseAddress(theDisplay) ) )
1026     failure = "Could not get base address of screen";
1027 nigel 1.7
1028 nigel 1.9 }
1029     // Clean up the old CGImageRef stuff
1030     else if ( display_type == DISPLAY_WINDOW && imageRef )
1031     {
1032     CGImageRef oldImageRef = imageRef;
1033     CGColorSpaceRef oldColourSpace = colourSpace;
1034     CGDataProviderRef oldProvider = provider;
1035     void *oldBuffer = the_buffer;
1036    
1037     if ( video_open(mode) )
1038     {
1039     CGImageRelease(oldImageRef);
1040     CGColorSpaceRelease(oldColourSpace);
1041     CGDataProviderRelease(oldProvider);
1042     free(oldBuffer);
1043     }
1044     else
1045     failure = "Could not video_open() requested mode";
1046 nigel 1.4 }
1047     else if ( ! video_open(mode) )
1048     failure = "Could not video_open() requested mode";
1049 nigel 1.7
1050     if ( ! failure && display_type == DISPLAY_SCREEN )
1051     {
1052     // Whenever we change screen resolution, the MacOS mouse starts
1053     // up in the top left corner. Send real mouse to that location
1054     // if ( CGDisplayMoveCursorToPoint(theDisplay, CGPointMake(15,15))
1055     // == CGDisplayNoErr )
1056     // {
1057     //
1058     [output fullscreenMouseMove];
1059     // }
1060     // else
1061     // failure = "Could move (jump) cursor on screen";
1062     }
1063 nigel 1.4
1064     if ( failure )
1065 nigel 1.1 {
1066 nigel 1.4 NSLog(@"In switch_to_current_mode():");
1067     NSLog(@"%s.", failure);
1068     video_close();
1069 nigel 1.1 if ( display_type == DISPLAY_SCREEN )
1070     ErrorAlert("Cannot switch screen to selected video mode");
1071     else
1072     ErrorAlert(STR_OPEN_WINDOW_ERR);
1073     QuitEmulator();
1074     }
1075 nigel 1.4 else
1076 nigel 1.5 set_mac_frame_buffer(mode);
1077 nigel 1.1 }
1078    
1079     /*
1080     * Close down full-screen mode
1081     * (if bringing up error alerts is unsafe while in full-screen mode)
1082     */
1083    
1084     void VideoQuitFullScreen(void)
1085     {
1086     }
1087    
1088    
1089     /*
1090     * Mac VBL interrupt
1091     */
1092    
1093     void VideoInterrupt(void)
1094     {
1095     }
1096    
1097    
1098     // This function is called on non-threaded platforms from a timer interrupt
1099     void VideoRefresh(void)
1100     {
1101     }