ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
(Generate patch)

Comparing BasiliskII/src/SDL/video_sdl.cpp (file contents):
Revision 1.1 by gbeauche, 2004-06-23T13:47:20Z vs.
Revision 1.22 by gbeauche, 2005-11-29T23:20:31Z

# Line 1 | Line 1
1   /*
2   *  video_sdl.cpp - Video/graphics emulation, SDL specific stuff
3   *
4 < *  Basilisk II (C) 1997-2004 Christian Bauer
4 > *  Basilisk II (C) 1997-2005 Christian Bauer
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 21 | Line 21
21   /*
22   *  NOTES:
23   *    The Ctrl key works like a qualifier for special actions:
24 < *      Ctrl-Tab = suspend DGA mode
24 > *      Ctrl-Tab = suspend DGA mode (TODO)
25   *      Ctrl-Esc = emergency quit
26   *      Ctrl-F1 = mount floppy
27   *      Ctrl-F5 = grab mouse (in windowed mode)
28   *
29   *  FIXMEs and TODOs:
30 + *  - Ctr-Tab for suspend/resume but how? SDL does not support that for non-Linux
31   *  - Ctrl-Fn doesn't generate SDL_KEYDOWN events (SDL bug?)
32   *  - Mouse acceleration, there is no API in SDL yet for that
33   *  - Force relative mode in Grab mode even if SDL provides absolute coordinates?
34   *  - Fullscreen mode
35   *  - Gamma tables support is likely to be broken here
36 + *  - Events processing is bound to the general emulation thread as SDL requires
37 + *    to PumpEvents() within the same thread as the one that called SetVideoMode().
38 + *    Besides, there can't seem to be a way to call SetVideoMode() from a child thread.
39 + *  - Refresh performance is still slow. Use SDL_CreateRGBSurface()?
40 + *  - Backport hw cursor acceleration to Basilisk II?
41 + *  - Factor out code
42   */
43  
44   #include "sysdeps.h"
# Line 40 | Line 47
47   #include <SDL_mutex.h>
48   #include <SDL_thread.h>
49   #include <errno.h>
50 + #include <vector>
51  
52   #include "cpu_emulation.h"
53   #include "main.h"
# Line 48 | Line 56
56   #include "prefs.h"
57   #include "user_strings.h"
58   #include "video.h"
59 + #include "video_defs.h"
60   #include "video_blit.h"
61 + #include "vm_alloc.h"
62  
63   #define DEBUG 0
64   #include "debug.h"
65  
66  
67   // Supported video modes
68 < static vector<video_mode> VideoModes;
68 > using std::vector;
69 > static vector<VIDEO_MODE> VideoModes;
70  
71   // Display types
72 + #ifdef SHEEPSHAVER
73   enum {
74 <        DISPLAY_WINDOW, // windowed display
75 <        DISPLAY_SCREEN  // fullscreen display
74 >        DISPLAY_WINDOW = DIS_WINDOW,                                    // windowed display
75 >        DISPLAY_SCREEN = DIS_SCREEN                                             // fullscreen display
76   };
77 + extern int display_type;                                                        // See enum above
78 + #else
79 + enum {
80 +        DISPLAY_WINDOW,                                                                 // windowed display
81 +        DISPLAY_SCREEN                                                                  // fullscreen display
82 + };
83 + static int display_type = DISPLAY_WINDOW;                       // See enum above
84 + #endif
85  
86   // Constants
87 + #ifdef WIN32
88 + const char KEYCODE_FILE_NAME[] = "BasiliskII_keycodes";
89 + #else
90   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
91 + #endif
92  
93  
94   // Global variables
# Line 72 | Line 96 | static int32 frame_skip;                                                       // Prefs
96   static int16 mouse_wheel_mode;
97   static int16 mouse_wheel_lines;
98  
75 static int display_type = DISPLAY_WINDOW;                       // See enum above
99   static uint8 *the_buffer = NULL;                                        // Mac frame buffer (where MacOS draws into)
100   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer (for refreshed modes)
101   static uint32 the_buffer_size;                                          // Size of allocated the_buffer
102  
103   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
104 + #ifndef USE_CPU_EMUL_SERVICES
105   static volatile bool redraw_thread_cancel;                      // Flag: Cancel Redraw thread
106   static SDL_Thread *redraw_thread = NULL;                        // Redraw thread
107 + #ifdef SHEEPSHAVER
108 + static volatile bool thread_stop_req = false;
109 + static volatile bool thread_stop_ack = false;           // Acknowledge for thread_stop_req
110 + #endif
111 + #endif
112  
113   #ifdef ENABLE_VOSF
114 < static bool use_vosf = true;                                            // Flag: VOSF enabled
114 > static bool use_vosf = false;                                           // Flag: VOSF enabled
115   #else
116   static const bool use_vosf = false;                                     // VOSF not possible
117   #endif
# Line 100 | Line 129 | static int keycode_table[256];                                         // X
129  
130   // SDL variables
131   static int screen_depth;                                                        // Depth of current screen
132 + static SDL_Cursor *sdl_cursor;                                          // Copy of Mac cursor
133 + static volatile bool cursor_changed = false;            // Flag: cursor changed, redraw_func must update the cursor
134   static SDL_Color sdl_palette[256];                                      // Color palette to be used as CLUT and gamma table
135   static bool sdl_palette_changed = false;                        // Flag: Palette changed, redraw thread must set new colors
136 + static const int sdl_eventmask = SDL_MOUSEBUTTONDOWNMASK | SDL_MOUSEBUTTONUPMASK | SDL_MOUSEMOTIONMASK | SDL_KEYUPMASK | SDL_KEYDOWNMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK;
137 +
138 + // Mutex to protect SDL events
139 + static SDL_mutex *sdl_events_lock = NULL;
140 + #define LOCK_EVENTS SDL_LockMutex(sdl_events_lock)
141 + #define UNLOCK_EVENTS SDL_UnlockMutex(sdl_events_lock)
142  
143   // Mutex to protect palette
144   static SDL_mutex *sdl_palette_lock = NULL;
# Line 126 | Line 163 | extern void SysMountFirstFloppy(void);
163  
164  
165   /*
166 + *  SDL surface locking glue
167 + */
168 +
169 + #ifdef ENABLE_VOSF
170 + #define SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE) do {                               \
171 +        if ((SURFACE)->flags & (SDL_HWSURFACE | SDL_FULLSCREEN))        \
172 +                the_host_buffer = (uint8 *)(SURFACE)->pixels;                   \
173 + } while (0)
174 + #else
175 + #define SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE)
176 + #endif
177 +
178 + #define SDL_VIDEO_LOCK_SURFACE(SURFACE) do {    \
179 +        if (SDL_MUSTLOCK(SURFACE)) {                            \
180 +                SDL_LockSurface(SURFACE);                               \
181 +                SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE);   \
182 +        }                                                                                       \
183 + } while (0)
184 +
185 + #define SDL_VIDEO_UNLOCK_SURFACE(SURFACE) do {  \
186 +        if (SDL_MUSTLOCK(SURFACE))                                      \
187 +                SDL_UnlockSurface(SURFACE);                             \
188 + } while (0)
189 +
190 +
191 + /*
192 + *  Framebuffer allocation routines
193 + */
194 +
195 + static void *vm_acquire_framebuffer(uint32 size)
196 + {
197 +        return vm_acquire(size);
198 + }
199 +
200 + static inline void vm_release_framebuffer(void *fb, uint32 size)
201 + {
202 +        vm_release(fb, size);
203 + }
204 +
205 +
206 + /*
207 + *  SheepShaver glue
208 + */
209 +
210 + #ifdef SHEEPSHAVER
211 + // Color depth modes type
212 + typedef int video_depth;
213 +
214 + // 1, 2, 4 and 8 bit depths use a color palette
215 + static inline bool IsDirectMode(VIDEO_MODE const & mode)
216 + {
217 +        return IsDirectMode(mode.viAppleMode);
218 + }
219 +
220 + // Abstract base class representing one (possibly virtual) monitor
221 + // ("monitor" = rectangular display with a contiguous frame buffer)
222 + class monitor_desc {
223 + public:
224 +        monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) {}
225 +        virtual ~monitor_desc() {}
226 +
227 +        // Get current Mac frame buffer base address
228 +        uint32 get_mac_frame_base(void) const {return screen_base;}
229 +
230 +        // Set Mac frame buffer base address (called from switch_to_mode())
231 +        void set_mac_frame_base(uint32 base) {screen_base = base;}
232 +
233 +        // Get current video mode
234 +        const VIDEO_MODE &get_current_mode(void) const {return VModes[cur_mode];}
235 +
236 +        // Called by the video driver to switch the video mode on this display
237 +        // (must call set_mac_frame_base())
238 +        virtual void switch_to_current_mode(void) = 0;
239 +
240 +        // Called by the video driver to set the color palette (in indexed modes)
241 +        // or the gamma table (in direct modes)
242 +        virtual void set_palette(uint8 *pal, int num) = 0;
243 + };
244 +
245 + // Vector of pointers to available monitor descriptions, filled by VideoInit()
246 + static vector<monitor_desc *> VideoMonitors;
247 +
248 + // Find Apple mode matching best specified dimensions
249 + static int find_apple_resolution(int xsize, int ysize)
250 + {
251 +        int apple_id;
252 +        if (xsize < 800)
253 +                apple_id = APPLE_640x480;
254 +        else if (xsize < 1024)
255 +                apple_id = APPLE_800x600;
256 +        else if (xsize < 1152)
257 +                apple_id = APPLE_1024x768;
258 +        else if (xsize < 1280) {
259 +                if (ysize < 900)
260 +                        apple_id = APPLE_1152x768;
261 +                else
262 +                        apple_id = APPLE_1152x900;
263 +        }
264 +        else if (xsize < 1600)
265 +                apple_id = APPLE_1280x1024;
266 +        else
267 +                apple_id = APPLE_1600x1200;
268 +        return apple_id;
269 + }
270 +
271 + // Set parameters to specified Apple mode
272 + static void set_apple_resolution(int apple_id, int &xsize, int &ysize)
273 + {
274 +        switch (apple_id) {
275 +        case APPLE_640x480:
276 +                xsize = 640;
277 +                ysize = 480;
278 +                break;
279 +        case APPLE_800x600:
280 +                xsize = 800;
281 +                ysize = 600;
282 +                break;
283 +        case APPLE_1024x768:
284 +                xsize = 1024;
285 +                ysize = 768;
286 +                break;
287 +        case APPLE_1152x768:
288 +                xsize = 1152;
289 +                ysize = 768;
290 +                break;
291 +        case APPLE_1152x900:
292 +                xsize = 1152;
293 +                ysize = 900;
294 +                break;
295 +        case APPLE_1280x1024:
296 +                xsize = 1280;
297 +                ysize = 1024;
298 +                break;
299 +        case APPLE_1600x1200:
300 +                xsize = 1600;
301 +                ysize = 1200;
302 +                break;
303 +        default:
304 +                abort();
305 +        }
306 + }
307 +
308 + // Match Apple mode matching best specified dimensions
309 + static int match_apple_resolution(int &xsize, int &ysize)
310 + {
311 +        int apple_id = find_apple_resolution(xsize, ysize);
312 +        set_apple_resolution(apple_id, xsize, ysize);
313 +        return apple_id;
314 + }
315 +
316 + // Display error alert
317 + static void ErrorAlert(int error)
318 + {
319 +        ErrorAlert(GetString(error));
320 + }
321 +
322 + // Display warning alert
323 + static void WarningAlert(int warning)
324 + {
325 +        WarningAlert(GetString(warning));
326 + }
327 + #endif
328 +
329 +
330 + /*
331   *  monitor_desc subclass for SDL display
332   */
333  
334   class SDL_monitor_desc : public monitor_desc {
335   public:
336 <        SDL_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
336 >        SDL_monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
337          ~SDL_monitor_desc() {}
338  
339          virtual void switch_to_current_mode(void);
# Line 146 | Line 348 | public:
348   *  Utility functions
349   */
350  
351 + // Find palette size for given color depth
352 + static int palette_size(int mode)
353 + {
354 +        switch (mode) {
355 +        case VIDEO_DEPTH_1BIT: return 2;
356 +        case VIDEO_DEPTH_2BIT: return 4;
357 +        case VIDEO_DEPTH_4BIT: return 16;
358 +        case VIDEO_DEPTH_8BIT: return 256;
359 +        case VIDEO_DEPTH_16BIT: return 32;
360 +        case VIDEO_DEPTH_32BIT: return 256;
361 +        default: return 0;
362 +        }
363 + }
364 +
365 + // Return bytes per pixel for requested depth
366 + static inline int bytes_per_pixel(int depth)
367 + {
368 +        int bpp;
369 +        switch (depth) {
370 +        case 8:
371 +                bpp = 1;
372 +                break;
373 +        case 15: case 16:
374 +                bpp = 2;
375 +                break;
376 +        case 24: case 32:
377 +                bpp = 4;
378 +                break;
379 +        default:
380 +                abort();
381 +        }
382 +        return bpp;
383 + }
384 +
385   // Map video_mode depth ID to numerical depth value
386 < static int sdl_depth_of_video_depth(int video_depth)
386 > static int mac_depth_of_video_depth(int video_depth)
387   {
388          int depth = -1;
389          switch (video_depth) {
390 <        case VDEPTH_1BIT:
390 >        case VIDEO_DEPTH_1BIT:
391                  depth = 1;
392                  break;
393 <        case VDEPTH_2BIT:
393 >        case VIDEO_DEPTH_2BIT:
394                  depth = 2;
395                  break;
396 <        case VDEPTH_4BIT:
396 >        case VIDEO_DEPTH_4BIT:
397                  depth = 4;
398                  break;
399 <        case VDEPTH_8BIT:
399 >        case VIDEO_DEPTH_8BIT:
400                  depth = 8;
401                  break;
402 <        case VDEPTH_16BIT:
402 >        case VIDEO_DEPTH_16BIT:
403                  depth = 16;
404                  break;
405 <        case VDEPTH_32BIT:
405 >        case VIDEO_DEPTH_32BIT:
406                  depth = 32;
407                  break;
408          default:
# Line 175 | Line 411 | static int sdl_depth_of_video_depth(int
411          return depth;
412   }
413  
414 + // Map video_mode depth ID to SDL screen depth
415 + static int sdl_depth_of_video_depth(int video_depth)
416 + {
417 +        return (video_depth <= VIDEO_DEPTH_8BIT) ? 8 : mac_depth_of_video_depth(video_depth);
418 + }
419 +
420 + // Check wether specified mode is available
421 + static bool has_mode(int type, int width, int height)
422 + {
423 + #ifdef SHEEPSHAVER
424 +        // Filter out Classic resolutiosn
425 +        if (width == 512 && height == 384)
426 +                return false;
427 +
428 +        // "screen" prefs items always succeeds
429 +        if (PrefsFindString("screen"))
430 +                return true;
431 +
432 +        // Read window & screen modes prefs
433 +        static uint32 window_modes = 0;
434 +        static uint32 screen_modes = 0;
435 +        if (window_modes == 0 || screen_modes == 0) {
436 +                window_modes = PrefsFindInt32("windowmodes");
437 +                screen_modes = PrefsFindInt32("screenmodes");
438 +                if (window_modes == 0 || screen_modes == 0)
439 +                        window_modes |= 3;                      // Allow at least 640x480 and 800x600 window modes
440 +        }
441 +
442 +        int test_modes;
443 +        switch (type) {
444 +        case DISPLAY_WINDOW:
445 +                test_modes = window_modes;
446 +                break;
447 +        case DISPLAY_SCREEN:
448 +                test_modes = screen_modes;
449 +                break;
450 +        default:
451 +                test_modes = 0;
452 +                break;
453 +        }
454 +
455 +        int apple_mask;
456 +        switch (find_apple_resolution(width, height)) {
457 +        case APPLE_640x480:             apple_mask = 0x01; break;
458 +        case APPLE_800x600:             apple_mask = 0x02; break;
459 +        case APPLE_1024x768:    apple_mask = 0x04; break;
460 +        case APPLE_1152x768:    apple_mask = 0x40; break;
461 +        case APPLE_1152x900:    apple_mask = 0x08; break;
462 +        case APPLE_1280x1024:   apple_mask = 0x10; break;
463 +        case APPLE_1600x1200:   apple_mask = 0x20; break;
464 +        default:                                apple_mask = 0x00; break;
465 +        }
466 +        return (test_modes & apple_mask);
467 + #endif
468 +        return true;
469 + }
470 +
471   // Add mode to list of supported modes
472 < static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth)
472 > static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
473   {
474 <        video_mode mode;
475 <        mode.x = width;
476 <        mode.y = height;
477 <        mode.resolution_id = resolution_id;
478 <        mode.bytes_per_row = bytes_per_row;
479 <        mode.depth = depth;
474 >        // Filter out unsupported modes
475 >        if (!has_mode(type, width, height))
476 >                return;
477 >
478 >        // Fill in VideoMode entry
479 >        VIDEO_MODE mode;
480 > #ifdef SHEEPSHAVER
481 >        // Recalculate dimensions to fit Apple modes
482 >        resolution_id = match_apple_resolution(width, height);
483 >        mode.viType = type;
484 > #endif
485 >        VIDEO_MODE_X = width;
486 >        VIDEO_MODE_Y = height;
487 >        VIDEO_MODE_RESOLUTION = resolution_id;
488 >        VIDEO_MODE_ROW_BYTES = bytes_per_row;
489 >        VIDEO_MODE_DEPTH = (video_depth)depth;
490          VideoModes.push_back(mode);
491   }
492  
493   // Add standard list of windowed modes for given color depth
494 < static void add_window_modes(video_depth depth)
494 > static void add_window_modes(int depth)
495   {
496 <        add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth);
497 <        add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth);
498 <        add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth);
499 <        add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth), depth);
500 <        add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth);
501 <        add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth);
502 <        add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth);
496 >        video_depth vdepth = (video_depth)depth;
497 >        add_mode(DISPLAY_WINDOW, 512, 384, 0x80, TrivialBytesPerRow(512, vdepth), depth);
498 >        add_mode(DISPLAY_WINDOW, 640, 480, 0x81, TrivialBytesPerRow(640, vdepth), depth);
499 >        add_mode(DISPLAY_WINDOW, 800, 600, 0x82, TrivialBytesPerRow(800, vdepth), depth);
500 >        add_mode(DISPLAY_WINDOW, 1024, 768, 0x83, TrivialBytesPerRow(1024, vdepth), depth);
501 >        add_mode(DISPLAY_WINDOW, 1152, 870, 0x84, TrivialBytesPerRow(1152, vdepth), depth);
502 >        add_mode(DISPLAY_WINDOW, 1280, 1024, 0x85, TrivialBytesPerRow(1280, vdepth), depth);
503 >        add_mode(DISPLAY_WINDOW, 1600, 1200, 0x86, TrivialBytesPerRow(1600, vdepth), depth);
504   }
505  
506   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
507 < static void set_mac_frame_buffer(SDL_monitor_desc &monitor, video_depth depth, bool native_byte_order)
507 > static void set_mac_frame_buffer(SDL_monitor_desc &monitor, int depth, bool native_byte_order)
508   {
509   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
510          int layout = FLAYOUT_DIRECT;
511 <        if (depth == VDEPTH_16BIT)
511 >        if (depth == VIDEO_DEPTH_16BIT)
512                  layout = (screen_depth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
513 <        else if (depth == VDEPTH_32BIT)
513 >        else if (depth == VIDEO_DEPTH_32BIT)
514                  layout = (screen_depth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
515          if (native_byte_order)
516                  MacFrameLayout = layout;
# Line 215 | Line 519 | static void set_mac_frame_buffer(SDL_mon
519          monitor.set_mac_frame_base(MacFrameBaseMac);
520  
521          // Set variables used by UAE memory banking
522 <        const video_mode &mode = monitor.get_current_mode();
522 >        const VIDEO_MODE &mode = monitor.get_current_mode();
523          MacFrameBaseHost = the_buffer;
524 <        MacFrameSize = mode.bytes_per_row * mode.y;
524 >        MacFrameSize = VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y;
525          InitFrameBufferMapping();
526   #else
527          monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
# Line 238 | Line 542 | static void set_window_name(int name)
542   // Set mouse grab mode
543   static SDL_GrabMode set_grab_mode(SDL_GrabMode mode)
544   {
545 <        const SDL_VideoInfo *vi =SDL_GetVideoInfo();
545 >        const SDL_VideoInfo *vi = SDL_GetVideoInfo();
546          return (vi && vi->wm_available ? SDL_WM_GrabInput(mode) : SDL_GRAB_OFF);
547   }
548  
# Line 266 | Line 570 | public:
570  
571   public:
572          SDL_monitor_desc &monitor; // Associated video monitor
573 <        const video_mode &mode;    // Video mode handled by the driver
573 >        const VIDEO_MODE &mode;    // Video mode handled by the driver
574  
575          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
576          SDL_Surface *s; // The surface we draw into
# Line 297 | Line 601 | private:
601          int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode)
602   };
603  
604 + class driver_fullscreen : public driver_base {
605 + public:
606 +        driver_fullscreen(SDL_monitor_desc &monitor);
607 +        ~driver_fullscreen();
608 + };
609 +
610   static driver_base *drv = NULL; // Pointer to currently used driver object
611  
612   #ifdef ENABLE_VOSF
# Line 318 | Line 628 | driver_base::~driver_base()
628          if (s)
629                  SDL_FreeSurface(s);
630  
631 +        // the_buffer shall always be mapped through vm_acquire_framebuffer()
632 +        if (the_buffer != VM_MAP_FAILED) {
633 +                D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
634 +                vm_release_framebuffer(the_buffer, the_buffer_size);
635 +                the_buffer = NULL;
636 +        }
637 +
638          // Free frame buffer(s)
639          if (!use_vosf) {
323                if (the_buffer) {
324                        free(the_buffer);
325                        the_buffer = NULL;
326                }
640                  if (the_buffer_copy) {
641                          free(the_buffer_copy);
642                          the_buffer_copy = NULL;
# Line 331 | Line 644 | driver_base::~driver_base()
644          }
645   #ifdef ENABLE_VOSF
646          else {
334                // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
335                if (the_buffer != VM_MAP_FAILED) {
336                        D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
337                        vm_release(the_buffer, the_buffer_size);
338                        the_buffer = NULL;
339                }
647                  if (the_host_buffer) {
648                          D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
649                          free(the_host_buffer);
# Line 347 | Line 654 | driver_base::~driver_base()
654                          free(the_buffer_copy);
655                          the_buffer_copy = NULL;
656                  }
657 +
658 +                // Deinitialize VOSF
659 +                video_vosf_exit();
660          }
661   #endif
662   }
# Line 354 | Line 664 | driver_base::~driver_base()
664   // Palette has changed
665   void driver_base::update_palette(void)
666   {
667 <        const video_mode &mode = monitor.get_current_mode();
667 >        const VIDEO_MODE &mode = monitor.get_current_mode();
668  
669 <        if (mode.depth <= VDEPTH_8BIT)
669 >        if ((int)VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT)
670                  SDL_SetPalette(s, SDL_PHYSPAL, sdl_palette, 0, 256);
671   }
672  
# Line 379 | Line 689 | void driver_base::restore_mouse_accel(vo
689   driver_window::driver_window(SDL_monitor_desc &m)
690          : driver_base(m), mouse_grabbed(false)
691   {
692 <        int width = mode.x, height = mode.y;
692 >        int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
693          int aligned_width = (width + 15) & ~15;
694          int aligned_height = (height + 15) & ~15;
695  
# Line 387 | Line 697 | driver_window::driver_window(SDL_monitor
697          ADBSetRelMouseMode(mouse_grabbed);
698  
699          // Create surface
700 <        int depth = (mode.depth <= VDEPTH_8BIT ? 8 : screen_depth);
700 >        int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
701          if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL)
702                  return;
703  
# Line 396 | Line 706 | driver_window::driver_window(SDL_monitor
706          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
707          the_host_buffer = (uint8 *)s->pixels;
708          the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
709 <        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
709 >        the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
710          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
711          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
712 +
713 +        // Check whether we can initialize the VOSF subsystem and it's profitable
714 +        if (!video_vosf_init(m)) {
715 +                WarningAlert(STR_VOSF_INIT_ERR);
716 +                use_vosf = false;
717 +        }
718 +        else if (!video_vosf_profitable()) {
719 +                video_vosf_exit();
720 +                printf("VOSF acceleration is not profitable on this platform, disabling it\n");
721 +                use_vosf = false;
722 +        }
723 +        if (!use_vosf) {
724 +                free(the_buffer_copy);
725 +                vm_release(the_buffer, the_buffer_size);
726 +                the_host_buffer = NULL;
727 +        }
728 + #endif
729 +        if (!use_vosf) {
730 +                // Allocate memory for frame buffer
731 +                the_buffer_size = (aligned_height + 2) * s->pitch;
732 +                the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
733 +                the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
734 +                D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
735 +        }
736 +        
737 + #ifdef SHEEPSHAVER
738 +        // Create cursor
739 +        if ((sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, 0, 0)) != NULL) {
740 +                SDL_SetCursor(sdl_cursor);
741 +                cursor_changed = false;
742 +        }
743   #else
744 <        // Allocate memory for frame buffer
745 <        the_buffer_size = (aligned_height + 2) * s->pitch;
405 <        the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
406 <        the_buffer = (uint8 *)calloc(1, the_buffer_size);
407 <        D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
744 >        // Hide cursor
745 >        SDL_ShowCursor(0);
746   #endif
747  
748          // Set window name/class
749          set_window_name(STR_WINDOW_TITLE);
750  
413        // Hide cursor
414        SDL_ShowCursor(0);
415
751          // Init blitting routines
752          SDL_PixelFormat *f = s->format;
753          VisualFormat visualFormat;
# Line 420 | Line 755 | driver_window::driver_window(SDL_monitor
755          visualFormat.Rmask = f->Rmask;
756          visualFormat.Gmask = f->Gmask;
757          visualFormat.Bmask = f->Bmask;
758 <        Screen_blitter_init(visualFormat, true, sdl_depth_of_video_depth(mode.depth));
758 >        Screen_blitter_init(visualFormat, true, mac_depth_of_video_depth(VIDEO_MODE_DEPTH));
759  
760          // Load gray ramp to 8->16/32 expand map
761          if (!IsDirectMode(mode))
# Line 428 | Line 763 | driver_window::driver_window(SDL_monitor
763                          ExpandMap[i] = SDL_MapRGB(f, i, i, i);
764  
765          // Set frame buffer base
766 <        set_mac_frame_buffer(monitor, mode.depth, true);
766 >        set_mac_frame_buffer(monitor, VIDEO_MODE_DEPTH, true);
767  
768          // Everything went well
769          init_ok = true;
# Line 487 | Line 822 | void driver_window::mouse_moved(int x, i
822          ADBMouseMoved(x, y);
823   }
824  
825 +
826 + /*
827 + *  Full-screen display driver
828 + */
829 +
830 + // Open display
831 + driver_fullscreen::driver_fullscreen(SDL_monitor_desc &m)
832 +        : driver_base(m)
833 + {
834 +        int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
835 +        int aligned_width = (width + 15) & ~15;
836 +        int aligned_height = (height + 15) & ~15;
837 +
838 +        // Set absolute mouse mode
839 +        ADBSetRelMouseMode(false);
840 +
841 +        // Create surface
842 +        int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
843 +        if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE | SDL_FULLSCREEN)) == NULL)
844 +                return;
845 +
846 + #ifdef ENABLE_VOSF
847 +        use_vosf = true;
848 +        // Allocate memory for frame buffer (SIZE is extended to page-boundary)
849 +        the_host_buffer = (uint8 *)s->pixels;
850 +        the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
851 +        the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
852 +        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
853 +        D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
854 +
855 +        // Check whether we can initialize the VOSF subsystem and it's profitable
856 +        if (!video_vosf_init(m)) {
857 +                WarningAlert(STR_VOSF_INIT_ERR);
858 +                use_vosf = false;
859 +        }
860 +        else if (!video_vosf_profitable()) {
861 +                video_vosf_exit();
862 +                printf("VOSF acceleration is not profitable on this platform, disabling it\n");
863 +                use_vosf = false;
864 +        }
865 +        if (!use_vosf) {
866 +                free(the_buffer_copy);
867 +                vm_release(the_buffer, the_buffer_size);
868 +                the_host_buffer = NULL;
869 +        }
870 + #endif
871 +        if (!use_vosf) {
872 +                // Allocate memory for frame buffer
873 +                the_buffer_size = (aligned_height + 2) * s->pitch;
874 +                the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
875 +                the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
876 +                D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
877 +        }
878 +        
879 +        // Hide cursor
880 +        SDL_ShowCursor(0);
881 +
882 +        // Init blitting routines
883 +        SDL_PixelFormat *f = s->format;
884 +        VisualFormat visualFormat;
885 +        visualFormat.depth = depth;
886 +        visualFormat.Rmask = f->Rmask;
887 +        visualFormat.Gmask = f->Gmask;
888 +        visualFormat.Bmask = f->Bmask;
889 +        Screen_blitter_init(visualFormat, true, mac_depth_of_video_depth(VIDEO_MODE_DEPTH));
890 +
891 +        // Load gray ramp to 8->16/32 expand map
892 +        if (!IsDirectMode(mode))
893 +                for (int i=0; i<256; i++)
894 +                        ExpandMap[i] = SDL_MapRGB(f, i, i, i);
895 +
896 +        // Set frame buffer base
897 +        set_mac_frame_buffer(monitor, VIDEO_MODE_DEPTH, true);
898 +
899 +        // Everything went well
900 +        init_ok = true;
901 + }
902 +
903 + // Close display
904 + driver_fullscreen::~driver_fullscreen()
905 + {
906 + #ifdef ENABLE_VOSF
907 +        if (use_vosf)
908 +                the_host_buffer = NULL; // don't free() in driver_base dtor
909 + #endif
910 +        if (s)
911 +                SDL_FreeSurface(s);
912 +
913 +        // Show cursor
914 +        SDL_ShowCursor(1);
915 + }
916 +
917 +
918   /*
919   *  Initialization
920   */
# Line 518 | Line 946 | static void keycode_init(void)
946                  SDL_VideoDriverName(video_driver, sizeof(video_driver));
947                  bool video_driver_found = false;
948                  char line[256];
949 +                int n_keys = 0;
950                  while (fgets(line, sizeof(line) - 1, f)) {
951                          // Read line
952                          int len = strlen(line);
# Line 530 | Line 959 | static void keycode_init(void)
959                                  continue;
960  
961                          if (video_driver_found) {
962 <                                // Skip aliases
963 <                                static const char alias_str[] = "alias";
964 <                                if (strncmp(line, alias_str, sizeof(alias_str) - 1) == 0)
962 >                                // Skip aliases as long as we have read keycodes yet
963 >                                // Otherwise, it's another mapping and we have to stop
964 >                                static const char sdl_str[] = "sdl";
965 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0 && n_keys == 0)
966                                          continue;
967  
968                                  // Read keycode
969                                  int x_code, mac_code;
970                                  if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
971 <                                        keycode_table[x_code & 0xff] = mac_code;
971 >                                        keycode_table[x_code & 0xff] = mac_code, n_keys++;
972                                  else
973                                          break;
974                          } else {
975                                  // Search for SDL video driver string
976 <                                static const char alias_sdl_str[] = "alias SDL";
977 <                                if (strncmp(line, alias_sdl_str, sizeof(alias_sdl_str) - 1) == 0) {
978 <                                        char *p = line + sizeof(alias_sdl_str);
976 >                                static const char sdl_str[] = "sdl";
977 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0) {
978 >                                        char *p = line + sizeof(sdl_str);
979                                          if (strstr(video_driver, p) == video_driver)
980                                                  video_driver_found = true;
981                                  }
# Line 563 | Line 993 | static void keycode_init(void)
993                          WarningAlert(str);
994                          return;
995                  }
996 +
997 +                D(bug("Using SDL/%s keycodes table, %d key mappings\n", video_driver, n_keys));
998          }
999   }
1000  
# Line 570 | Line 1002 | static void keycode_init(void)
1002   bool SDL_monitor_desc::video_open(void)
1003   {
1004          D(bug("video_open()\n"));
1005 <        const video_mode &mode = get_current_mode();
1005 >        const VIDEO_MODE &mode = get_current_mode();
1006 > #if DEBUG
1007 >        D(bug("Current video mode:\n"));
1008 >        D(bug(" %dx%d (ID %02x), %d bpp\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << (VIDEO_MODE_DEPTH & 0x0f)));
1009 > #endif
1010  
1011          // Create display driver object of requested type
1012          switch (display_type) {
1013          case DISPLAY_WINDOW:
1014                  drv = new(std::nothrow) driver_window(*this);
1015                  break;
1016 +        case DISPLAY_SCREEN:
1017 +                drv = new(std::nothrow) driver_fullscreen(*this);
1018 +                break;
1019          }
1020          if (drv == NULL)
1021                  return false;
# Line 586 | Line 1025 | bool SDL_monitor_desc::video_open(void)
1025                  return false;
1026          }
1027  
589 #ifdef ENABLE_VOSF
590        if (use_vosf) {
591                // Initialize the VOSF system
592                if (!video_vosf_init(*this)) {
593                        ErrorAlert(STR_VOSF_INIT_ERR);
594                return false;
595                }
596        }
597 #endif
598        
1028          // Initialize VideoRefresh function
1029          VideoRefreshInit();
1030  
# Line 603 | Line 1032 | bool SDL_monitor_desc::video_open(void)
1032          LOCK_FRAME_BUFFER;
1033  
1034          // Start redraw/input thread
1035 + #ifndef USE_CPU_EMUL_SERVICES
1036          redraw_thread_cancel = false;
1037 <        redraw_thread_active = (SDL_CreateThread(redraw_func, NULL) != NULL);
1037 >        redraw_thread_active = ((redraw_thread = SDL_CreateThread(redraw_func, NULL)) != NULL);
1038          if (!redraw_thread_active) {
1039                  printf("FATAL: cannot create redraw thread\n");
1040                  return false;
1041          }
1042 + #else
1043 +        redraw_thread_active = true;
1044 + #endif
1045          return true;
1046   }
1047  
1048 + #ifdef SHEEPSHAVER
1049 + bool VideoInit(void)
1050 + {
1051 +        const bool classic = false;
1052 + #else
1053   bool VideoInit(bool classic)
1054   {
1055 + #endif
1056          classic_mode = classic;
1057  
1058   #ifdef ENABLE_VOSF
# Line 623 | Line 1062 | bool VideoInit(bool classic)
1062   #endif
1063  
1064          // Create Mutexes
1065 +        if ((sdl_events_lock = SDL_CreateMutex()) == NULL)
1066 +                return false;
1067          if ((sdl_palette_lock = SDL_CreateMutex()) == NULL)
1068                  return false;
1069          if ((frame_buffer_lock = SDL_CreateMutex()) == NULL)
# Line 637 | Line 1078 | bool VideoInit(bool classic)
1078          mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1079  
1080          // Get screen mode from preferences
1081 <        const char *mode_str;
1081 >        const char *mode_str = NULL;
1082          if (classic_mode)
1083                  mode_str = "win/512/342";
1084          else
1085                  mode_str = PrefsFindString("screen");
1086  
1087          // Determine display type and default dimensions
1088 <        int default_width = 512, default_height = 384;
1088 >        int default_width, default_height;
1089 >        if (classic) {
1090 >                default_width = 512;
1091 >                default_height = 384;
1092 >        }
1093 >        else {
1094 >                default_width = 640;
1095 >                default_height = 480;
1096 >        }
1097          display_type = DISPLAY_WINDOW;
1098          if (mode_str) {
1099                  if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
1100                          display_type = DISPLAY_WINDOW;
1101 +                else if (sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1102 +                        display_type = DISPLAY_SCREEN;
1103          }
1104          int max_width = 640, max_height = 480;
1105 <        if (display_type == DISPLAY_SCREEN) {
1106 <                SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
1107 <                if (modes && modes != (SDL_Rect **)-1) {
1108 <                        max_width = modes[0]->w;
1109 <                        max_height = modes[0]->h;
1105 >        SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
1106 >        if (modes && modes != (SDL_Rect **)-1) {
1107 >                // It turns out that on some implementations, and contrary to the documentation,
1108 >                // the returned list is not sorted from largest to smallest (e.g. Windows)
1109 >                for (int i = 0; modes[i] != NULL; i++) {
1110 >                        const int w = modes[i]->w;
1111 >                        const int h = modes[i]->h;
1112 >                        if (w > max_width && h > max_height) {
1113 >                                max_width = w;
1114 >                                max_height = h;
1115 >                        }
1116                  }
1117 +                if (default_width > max_width)
1118 +                        default_width = max_width;
1119 +                if (default_height > max_height)
1120 +                        default_height = max_height;
1121          }
1122          if (default_width <= 0)
1123                  default_width = max_width;
# Line 665 | Line 1126 | bool VideoInit(bool classic)
1126  
1127          // Mac screen depth follows X depth
1128          screen_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
1129 <        video_depth default_depth;
1129 >        int default_depth;
1130          switch (screen_depth) {
1131          case 8:
1132 <                default_depth = VDEPTH_8BIT;
1132 >                default_depth = VIDEO_DEPTH_8BIT;
1133                  break;
1134          case 15: case 16:
1135 <                default_depth = VDEPTH_16BIT;
1135 >                default_depth = VIDEO_DEPTH_16BIT;
1136                  break;
1137          case 24: case 32:
1138 <                default_depth = VDEPTH_32BIT;
1138 >                default_depth = VIDEO_DEPTH_32BIT;
1139                  break;
1140          default:
1141 <                default_depth =  VDEPTH_1BIT;
1141 >                default_depth =  VIDEO_DEPTH_1BIT;
1142                  break;
1143          }
1144  
1145          // Construct list of supported modes
1146          if (display_type == DISPLAY_WINDOW) {
1147                  if (classic)
1148 <                        add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
1148 >                        add_mode(display_type, 512, 342, 0x80, 64, VIDEO_DEPTH_1BIT);
1149                  else {
1150 <                        for (int d = VDEPTH_1BIT; d <= default_depth; d++) {
1151 <                                int bpp = (d <= VDEPTH_8BIT ? 8 : sdl_depth_of_video_depth(d));
1150 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1151 >                                int bpp = sdl_depth_of_video_depth(d);
1152                                  if (SDL_VideoModeOK(max_width, max_height, bpp, SDL_HWSURFACE))
1153                                          add_window_modes(video_depth(d));
1154                          }
1155                  }
1156 <        } else
1157 <                add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
1156 >        } else if (display_type == DISPLAY_SCREEN) {
1157 >                struct {
1158 >                        int w;
1159 >                        int h;
1160 >                        int resolution_id;
1161 >                }
1162 >                video_modes[] = {
1163 >                        {   -1,   -1, 0x80 },
1164 >                        {  640,  480, 0x81 },
1165 >                        {  800,  600, 0x82 },
1166 >                        { 1024,  768, 0x83 },
1167 >                        { 1152,  870, 0x84 },
1168 >                        { 1280, 1024, 0x85 },
1169 >                        { 1600, 1200, 0x86 },
1170 >                        { 0, }
1171 >                };
1172 >                video_modes[0].w = default_width;
1173 >                video_modes[0].h = default_height;
1174 >
1175 >                for (int i = 0; video_modes[i].w != 0; i++) {
1176 >                        const int w = video_modes[i].w;
1177 >                        const int h = video_modes[i].h;
1178 >                        if (i > 0 && (w >= default_width || h >= default_height))
1179 >                                continue;
1180 > #ifdef ENABLE_VOSF
1181 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1182 >                                int bpp = sdl_depth_of_video_depth(d);
1183 >                                if (SDL_VideoModeOK(w, h, bpp, SDL_HWSURFACE | SDL_FULLSCREEN))
1184 >                                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)d), d);
1185 >                        }
1186 > #else
1187 >                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)default_depth), default_depth);
1188 > #endif
1189 >                }
1190 >        }
1191 >
1192          if (VideoModes.empty()) {
1193                  ErrorAlert(STR_NO_XVISUAL_ERR);
1194                  return false;
# Line 701 | Line 1196 | bool VideoInit(bool classic)
1196  
1197          // Find requested default mode with specified dimensions
1198          uint32 default_id;
1199 <        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1199 >        std::vector<VIDEO_MODE>::const_iterator i, end = VideoModes.end();
1200          for (i = VideoModes.begin(); i != end; ++i) {
1201 <                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1202 <                        default_id = i->resolution_id;
1201 >                const VIDEO_MODE & mode = (*i);
1202 >                if (VIDEO_MODE_X == default_width && VIDEO_MODE_Y == default_height && VIDEO_MODE_DEPTH == default_depth) {
1203 >                        default_id = VIDEO_MODE_RESOLUTION;
1204 > #ifdef SHEEPSHAVER
1205 >                        std::vector<VIDEO_MODE>::const_iterator begin = VideoModes.begin();
1206 >                        cur_mode = distance(begin, i);
1207 > #endif
1208                          break;
1209                  }
1210          }
1211          if (i == end) { // not found, use first available mode
1212 <                default_depth = VideoModes[0].depth;
1213 <                default_id = VideoModes[0].resolution_id;
1212 >                const VIDEO_MODE & mode = VideoModes[0];
1213 >                default_depth = VIDEO_MODE_DEPTH;
1214 >                default_id = VIDEO_MODE_RESOLUTION;
1215 > #ifdef SHEEPSHAVER
1216 >                cur_mode = 0;
1217 > #endif
1218          }
1219  
1220 + #ifdef SHEEPSHAVER
1221 +        for (int i = 0; i < VideoModes.size(); i++)
1222 +                VModes[i] = VideoModes[i];
1223 +        VideoInfo *p = &VModes[VideoModes.size()];
1224 +        p->viType = DIS_INVALID;        // End marker
1225 +        p->viRowBytes = 0;
1226 +        p->viXsize = p->viYsize = 0;
1227 +        p->viAppleMode = 0;
1228 +        p->viAppleID = 0;
1229 + #endif
1230 +
1231   #if DEBUG
1232          D(bug("Available video modes:\n"));
1233          for (i = VideoModes.begin(); i != end; ++i) {
1234 <                int bits = 1 << i->depth;
1234 >                const VIDEO_MODE & mode = (*i);
1235 >                int bits = 1 << VIDEO_MODE_DEPTH;
1236                  if (bits == 16)
1237                          bits = 15;
1238                  else if (bits == 32)
1239                          bits = 24;
1240 <                D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1240 >                D(bug(" %dx%d (ID %02x), %d colors\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << bits));
1241          }
1242   #endif
1243  
1244          // Create SDL_monitor_desc for this (the only) display
1245 <        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, default_depth, default_id);
1245 >        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, (video_depth)default_depth, default_id);
1246          VideoMonitors.push_back(monitor);
1247  
1248          // Open display
# Line 744 | Line 1260 | void SDL_monitor_desc::video_close(void)
1260          D(bug("video_close()\n"));
1261  
1262          // Stop redraw thread
1263 + #ifndef USE_CPU_EMUL_SERVICES
1264          if (redraw_thread_active) {
1265                  redraw_thread_cancel = true;
1266 < //              SDL_WaitThread(redraw_thread, NULL); doesn't work
750 <                while (redraw_thread_cancel);
1266 >                SDL_WaitThread(redraw_thread, NULL);
1267          }
1268 + #endif
1269          redraw_thread_active = false;
1270  
1271          // Unlock frame buffer
1272          UNLOCK_FRAME_BUFFER;
1273          D(bug(" frame buffer unlocked\n"));
1274  
758 #ifdef ENABLE_VOSF
759        if (use_vosf) {
760                // Deinitialize VOSF
761                video_vosf_exit();
762        }
763 #endif
764
1275          // Close display
1276          delete drv;
1277          drv = NULL;
# Line 779 | Line 1289 | void VideoExit(void)
1289                  SDL_DestroyMutex(frame_buffer_lock);
1290          if (sdl_palette_lock)
1291                  SDL_DestroyMutex(sdl_palette_lock);
1292 +        if (sdl_events_lock)
1293 +                SDL_DestroyMutex(sdl_events_lock);
1294   }
1295  
1296  
# Line 797 | Line 1309 | void VideoQuitFullScreen(void)
1309   *  Mac VBL interrupt
1310   */
1311  
1312 + /*
1313 + *  Execute video VBL routine
1314 + */
1315 +
1316 + #ifdef SHEEPSHAVER
1317 + void VideoVBL(void)
1318 + {
1319 +        // Emergency quit requested? Then quit
1320 +        if (emerg_quit)
1321 +                QuitEmulator();
1322 +
1323 +        // Temporarily give up frame buffer lock (this is the point where
1324 +        // we are suspended when the user presses Ctrl-Tab)
1325 +        UNLOCK_FRAME_BUFFER;
1326 +        LOCK_FRAME_BUFFER;
1327 +
1328 +        // Execute video VBL
1329 +        if (private_data != NULL && private_data->interruptsEnabled)
1330 +                VSLDoInterruptService(private_data->vslServiceID);
1331 + }
1332 + #else
1333   void VideoInterrupt(void)
1334   {
1335 +        // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
1336 +        SDL_PumpEvents();
1337 +
1338          // Emergency quit requested? Then quit
1339          if (emerg_quit)
1340                  QuitEmulator();
# Line 808 | Line 1344 | void VideoInterrupt(void)
1344          UNLOCK_FRAME_BUFFER;
1345          LOCK_FRAME_BUFFER;
1346   }
1347 + #endif
1348  
1349  
1350   /*
1351   *  Set palette
1352   */
1353  
1354 + #ifdef SHEEPSHAVER
1355 + void video_set_palette(void)
1356 + {
1357 +        monitor_desc * monitor = VideoMonitors[0];
1358 +        int n_colors = palette_size(monitor->get_current_mode().viAppleMode);
1359 +        uint8 pal[256 * 3];
1360 +        for (int c = 0; c < n_colors; c++) {
1361 +                pal[c*3 + 0] = mac_pal[c].red;
1362 +                pal[c*3 + 1] = mac_pal[c].green;
1363 +                pal[c*3 + 2] = mac_pal[c].blue;
1364 +        }
1365 +        monitor->set_palette(pal, n_colors);
1366 + }
1367 + #endif
1368 +
1369   void SDL_monitor_desc::set_palette(uint8 *pal, int num_in)
1370   {
1371 <        const video_mode &mode = get_current_mode();
1371 >        const VIDEO_MODE &mode = get_current_mode();
1372  
1373          // FIXME: how can we handle the gamma ramp?
1374 <        if (mode.depth > VDEPTH_8BIT)
1374 >        if ((int)VIDEO_MODE_DEPTH > VIDEO_DEPTH_8BIT)
1375                  return;
1376  
1377          LOCK_PALETTE;
# Line 844 | Line 1396 | void SDL_monitor_desc::set_palette(uint8
1396                  }
1397  
1398   #ifdef ENABLE_VOSF
1399 <                // We have to redraw everything because the interpretation of pixel values changed
1400 <                LOCK_VOSF;
1401 <                PFLAG_SET_ALL;
1402 <                UNLOCK_VOSF;
1403 <                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1399 >                if (use_vosf) {
1400 >                        // We have to redraw everything because the interpretation of pixel values changed
1401 >                        LOCK_VOSF;
1402 >                        PFLAG_SET_ALL;
1403 >                        UNLOCK_VOSF;
1404 >                        memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1405 >                }
1406   #endif
1407          }
1408  
# Line 863 | Line 1417 | void SDL_monitor_desc::set_palette(uint8
1417   *  Switch video mode
1418   */
1419  
1420 + #ifdef SHEEPSHAVER
1421 + int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1422 + {
1423 +        /* return if no mode change */
1424 +        if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1425 +            (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
1426 +
1427 +        /* first find video mode in table */
1428 +        for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
1429 +                if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
1430 +                    (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
1431 +                        csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
1432 +                        csSave->saveData = ReadMacInt32(ParamPtr + csData);
1433 +                        csSave->savePage = ReadMacInt16(ParamPtr + csPage);
1434 +
1435 +                        // Disable interrupts and pause redraw thread
1436 +                        DisableInterrupt();
1437 +                        thread_stop_ack = false;
1438 +                        thread_stop_req = true;
1439 +                        while (!thread_stop_ack) ;
1440 +
1441 +                        cur_mode = i;
1442 +                        monitor_desc *monitor = VideoMonitors[0];
1443 +                        monitor->switch_to_current_mode();
1444 +
1445 +                        WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
1446 +                        csSave->saveBaseAddr=screen_base;
1447 +                        csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
1448 +                        csSave->saveMode=VModes[cur_mode].viAppleMode;
1449 +
1450 +                        // Enable interrupts and resume redraw thread
1451 +                        thread_stop_req = false;
1452 +                        EnableInterrupt();
1453 +                        return noErr;
1454 +                }
1455 +        }
1456 +        return paramErr;
1457 + }
1458 + #endif
1459 +
1460   void SDL_monitor_desc::switch_to_current_mode(void)
1461   {
1462          // Close and reopen display
1463 +        LOCK_EVENTS;
1464          video_close();
1465          video_open();
1466 +        UNLOCK_EVENTS;
1467  
1468          if (drv == NULL) {
1469                  ErrorAlert(STR_OPEN_WINDOW_ERR);
# Line 877 | Line 1473 | void SDL_monitor_desc::switch_to_current
1473  
1474  
1475   /*
1476 < *  Translate key event to Mac keycode, returns -1 if no keycode was found
881 < *  and -2 if the key was recognized as a hotkey
1476 > *  Can we set the MacOS cursor image into the window?
1477   */
1478  
1479 + #ifdef SHEEPSHAVER
1480 + bool video_can_change_cursor(void)
1481 + {
1482 +        return (display_type == DISPLAY_WINDOW);
1483 + }
1484 + #endif
1485 +
1486 +
1487 + /*
1488 + *  Set cursor image for window
1489 + */
1490 +
1491 + #ifdef SHEEPSHAVER
1492 + void video_set_cursor(void)
1493 + {
1494 +        cursor_changed = true;
1495 + }
1496 + #endif
1497 +
1498 +
1499 + /*
1500 + *  Keyboard-related utilify functions
1501 + */
1502 +
1503 + static bool is_modifier_key(SDL_KeyboardEvent const & e)
1504 + {
1505 +        switch (e.keysym.sym) {
1506 +        case SDLK_NUMLOCK:
1507 +        case SDLK_CAPSLOCK:
1508 +        case SDLK_SCROLLOCK:
1509 +        case SDLK_RSHIFT:
1510 +        case SDLK_LSHIFT:
1511 +        case SDLK_RCTRL:
1512 +        case SDLK_LCTRL:
1513 +        case SDLK_RALT:
1514 +        case SDLK_LALT:
1515 +        case SDLK_RMETA:
1516 +        case SDLK_LMETA:
1517 +        case SDLK_LSUPER:
1518 +        case SDLK_RSUPER:
1519 +        case SDLK_MODE:
1520 +        case SDLK_COMPOSE:
1521 +                return true;
1522 +        }
1523 +        return false;
1524 + }
1525 +
1526   static bool is_ctrl_down(SDL_keysym const & ks)
1527   {
1528          return ctrl_down || (ks.mod & KMOD_CTRL);
1529   }
1530  
1531 +
1532 + /*
1533 + *  Translate key event to Mac keycode, returns -1 if no keycode was found
1534 + *  and -2 if the key was recognized as a hotkey
1535 + */
1536 +
1537   static int kc_decode(SDL_keysym const & ks, bool key_down)
1538   {
1539          switch (ks.sym) {
# Line 918 | Line 1566 | static int kc_decode(SDL_keysym const &
1566  
1567          case SDLK_1: case SDLK_EXCLAIM: return 0x12;
1568          case SDLK_2: case SDLK_AT: return 0x13;
1569 < //      case SDLK_3: case SDLK_numbersign: return 0x14;
1569 >        case SDLK_3: case SDLK_HASH: return 0x14;
1570          case SDLK_4: case SDLK_DOLLAR: return 0x15;
1571 < //      case SDLK_5: case SDLK_percent: return 0x17;
1571 >        case SDLK_5: return 0x17;
1572          case SDLK_6: return 0x16;
1573          case SDLK_7: return 0x1a;
1574          case SDLK_8: return 0x1c;
1575          case SDLK_9: return 0x19;
1576          case SDLK_0: return 0x1d;
1577  
1578 < //      case SDLK_BACKQUOTE: case SDLK_asciitilde: return 0x0a;
1578 >        case SDLK_BACKQUOTE: return 0x0a;
1579          case SDLK_MINUS: case SDLK_UNDERSCORE: return 0x1b;
1580          case SDLK_EQUALS: case SDLK_PLUS: return 0x18;
1581 < //      case SDLK_bracketleft: case SDLK_braceleft: return 0x21;
1582 < //      case SDLK_bracketright: case SDLK_braceright: return 0x1e;
1583 < //      case SDLK_BACKSLASH: case SDLK_bar: return 0x2a;
1581 >        case SDLK_LEFTBRACKET: return 0x21;
1582 >        case SDLK_RIGHTBRACKET: return 0x1e;
1583 >        case SDLK_BACKSLASH: return 0x2a;
1584          case SDLK_SEMICOLON: case SDLK_COLON: return 0x29;
1585 < //      case SDLK_apostrophe: case SDLK_QUOTEDBL: return 0x27;
1585 >        case SDLK_QUOTE: case SDLK_QUOTEDBL: return 0x27;
1586          case SDLK_COMMA: case SDLK_LESS: return 0x2b;
1587          case SDLK_PERIOD: case SDLK_GREATER: return 0x2f;
1588          case SDLK_SLASH: case SDLK_QUESTION: return 0x2c;
# Line 955 | Line 1603 | static int kc_decode(SDL_keysym const &
1603          case SDLK_RCTRL: return 0x36;
1604          case SDLK_LSHIFT: return 0x38;
1605          case SDLK_RSHIFT: return 0x38;
1606 + #if (defined(__APPLE__) && defined(__MACH__))
1607 +        case SDLK_LALT: return 0x3a;
1608 +        case SDLK_RALT: return 0x3a;
1609 +        case SDLK_LMETA: return 0x37;
1610 +        case SDLK_RMETA: return 0x37;
1611 + #else
1612          case SDLK_LALT: return 0x37;
1613          case SDLK_RALT: return 0x37;
1614          case SDLK_LMETA: return 0x3a;
1615          case SDLK_RMETA: return 0x3a;
1616 + #endif
1617 +        case SDLK_LSUPER: return 0x3a; // "Windows" key
1618 +        case SDLK_RSUPER: return 0x3a;
1619          case SDLK_MENU: return 0x32;
1620          case SDLK_CAPSLOCK: return 0x39;
1621          case SDLK_NUMLOCK: return 0x47;
# Line 1021 | Line 1678 | static int event2keycode(SDL_KeyboardEve
1678  
1679   static void handle_events(void)
1680   {
1681 <        SDL_Event event;
1682 <        while (SDL_PollEvent(&event)) {
1683 <                switch (event.type) {
1681 >        SDL_Event events[10];
1682 >        const int n_max_events = sizeof(events) / sizeof(events[0]);
1683 >        int n_events;
1684 >
1685 >        while ((n_events = SDL_PeepEvents(events, n_max_events, SDL_GETEVENT, sdl_eventmask)) > 0) {
1686 >                for (int i = 0; i < n_events; i++) {
1687 >                        SDL_Event const & event = events[i];
1688 >                        switch (event.type) {
1689  
1690                          // Mouse button
1691                          case SDL_MOUSEBUTTONDOWN: {
# Line 1060 | Line 1722 | static void handle_events(void)
1722                          // Keyboard
1723                          case SDL_KEYDOWN: {
1724                                  int code = -1;
1725 <                                if (use_keycodes) {
1725 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1726                                          if (event2keycode(event.key, true) != -2)       // This is called to process the hotkeys
1727                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1728                                  } else
# Line 1088 | Line 1750 | static void handle_events(void)
1750                          }
1751                          case SDL_KEYUP: {
1752                                  int code = -1;
1753 <                                if (use_keycodes) {
1753 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1754                                          if (event2keycode(event.key, false) != -2)      // This is called to process the hotkeys
1755                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1756                                  } else
1757                                          code = event2keycode(event.key, false);
1758 <                                if (code >= 0 && code != 0x39) {        // Don't propagate Caps Lock releases
1759 <                                        ADBKeyUp(code);
1758 >                                if (code >= 0) {
1759 >                                        if (code == 0x39) {     // Caps Lock released
1760 >                                                if (caps_on) {
1761 >                                                        ADBKeyUp(code);
1762 >                                                        caps_on = false;
1763 >                                                } else {
1764 >                                                        ADBKeyDown(code);
1765 >                                                        caps_on = true;
1766 >                                                }
1767 >                                        } else
1768 >                                                ADBKeyUp(code);
1769                                          if (code == 0x36)
1770                                                  ctrl_down = false;
1771                                  }
# Line 1104 | Line 1775 | static void handle_events(void)
1775                          // Hidden parts exposed, force complete refresh of window
1776                          case SDL_VIDEOEXPOSE:
1777                                  if (display_type == DISPLAY_WINDOW) {
1778 <                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
1778 >                                        const VIDEO_MODE &mode = VideoMonitors[0]->get_current_mode();
1779   #ifdef ENABLE_VOSF
1780                                          if (use_vosf) {                 // VOSF refresh
1781                                                  LOCK_VOSF;
1782                                                  PFLAG_SET_ALL;
1783                                                  UNLOCK_VOSF;
1784 <                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1784 >                                                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1785                                          }
1786                                          else
1787   #endif
1788 <                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1788 >                                                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1789                                  }
1790                                  break;
1791  
# Line 1123 | Line 1794 | static void handle_events(void)
1794                                  ADBKeyDown(0x7f);       // Power key
1795                                  ADBKeyUp(0x7f);
1796                                  break;
1797 +                        }
1798                  }
1799          }
1800   }
# Line 1137 | Line 1809 | static void update_display_static(driver
1809   {
1810          // Incremental update code
1811          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1812 <        const video_mode &mode = drv->mode;
1813 <        int bytes_per_row = mode.bytes_per_row;
1812 >        const VIDEO_MODE &mode = drv->mode;
1813 >        int bytes_per_row = VIDEO_MODE_ROW_BYTES;
1814          uint8 *p, *p2;
1815  
1816          // Check for first line from top and first line from bottom that have changed
1817          y1 = 0;
1818 <        for (j=0; j<mode.y; j++) {
1818 >        for (j=0; j<VIDEO_MODE_Y; j++) {
1819                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1820                          y1 = j;
1821                          break;
1822                  }
1823          }
1824          y2 = y1 - 1;
1825 <        for (j=mode.y-1; j>=y1; j--) {
1825 >        for (j=VIDEO_MODE_Y-1; j>=y1; j--) {
1826                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1827                          y2 = j;
1828                          break;
# Line 1160 | Line 1832 | static void update_display_static(driver
1832  
1833          // Check for first column from left and first column from right that have changed
1834          if (high) {
1835 <                if (mode.depth < VDEPTH_8BIT) {
1835 >                if ((int)VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
1836                          const int src_bytes_per_row = bytes_per_row;
1837                          const int dst_bytes_per_row = drv->s->pitch;
1838 <                        const int pixels_per_byte = mode.x / src_bytes_per_row;
1838 >                        const int pixels_per_byte = VIDEO_MODE_X / src_bytes_per_row;
1839  
1840 <                        x1 = mode.x / pixels_per_byte;
1840 >                        x1 = VIDEO_MODE_X / pixels_per_byte;
1841                          for (j = y1; j <= y2; j++) {
1842                                  p = &the_buffer[j * bytes_per_row];
1843                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1183 | Line 1855 | static void update_display_static(driver
1855                                  p2 = &the_buffer_copy[j * bytes_per_row];
1856                                  p += bytes_per_row;
1857                                  p2 += bytes_per_row;
1858 <                                for (i = (mode.x / pixels_per_byte); i > x2; i--) {
1858 >                                for (i = (VIDEO_MODE_X / pixels_per_byte); i > x2; i--) {
1859                                          p--; p2--;
1860                                          if (*p != *p2) {
1861                                                  x2 = i;
# Line 1221 | Line 1893 | static void update_display_static(driver
1893                          }
1894  
1895                  } else {
1896 <                        const int bytes_per_pixel = mode.bytes_per_row / mode.x;
1896 >                        const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
1897  
1898 <                        x1 = mode.x;
1898 >                        x1 = VIDEO_MODE_X;
1899                          for (j=y1; j<=y2; j++) {
1900                                  p = &the_buffer[j * bytes_per_row];
1901                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1241 | Line 1913 | static void update_display_static(driver
1913                                  p2 = &the_buffer_copy[j * bytes_per_row];
1914                                  p += bytes_per_row;
1915                                  p2 += bytes_per_row;
1916 <                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1916 >                                for (i=VIDEO_MODE_X*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1917                                          p--;
1918                                          p2--;
1919                                          if (*p != *p2) {
# Line 1337 | Line 2009 | static void video_refresh_dga_vosf(void)
2009                  tick_counter = 0;
2010                  if (mainBuffer.dirty) {
2011                          LOCK_VOSF;
2012 <                        update_display_dga_vosf();
2012 >                        update_display_dga_vosf(static_cast<driver_fullscreen *>(drv));
2013                          UNLOCK_VOSF;
2014                  }
2015          }
# Line 1401 | Line 2073 | static void VideoRefreshInit(void)
2073          }
2074   }
2075  
2076 + static inline void do_video_refresh(void)
2077 + {
2078 +        // Handle SDL events
2079 +        handle_events();
2080 +
2081 +        // Update display
2082 +        video_refresh();
2083 +
2084 + #ifdef SHEEPSHAVER
2085 +        // Set new cursor image if it was changed
2086 +        if (cursor_changed && sdl_cursor) {
2087 +                cursor_changed = false;
2088 +                LOCK_EVENTS;
2089 +                SDL_FreeCursor(sdl_cursor);
2090 +                sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
2091 +                if (sdl_cursor)
2092 +                        SDL_SetCursor(sdl_cursor);
2093 +                UNLOCK_EVENTS;
2094 +        }
2095 + #endif
2096 +
2097 +        // Set new palette if it was changed
2098 +        handle_palette_changes();
2099 + }
2100 +
2101 + // This function is called on non-threaded platforms from a timer interrupt
2102 + void VideoRefresh(void)
2103 + {
2104 +        // We need to check redraw_thread_active to inhibit refreshed during
2105 +        // mode changes on non-threaded platforms
2106 +        if (!redraw_thread_active)
2107 +                return;
2108 +
2109 +        // Process pending events and update display
2110 +        do_video_refresh();
2111 + }
2112 +
2113 + const int VIDEO_REFRESH_HZ = 60;
2114 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2115 +
2116 + #ifndef USE_CPU_EMUL_SERVICES
2117   static int redraw_func(void *arg)
2118   {
2119          uint64 start = GetTicks_usec();
2120          int64 ticks = 0;
2121 +        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2122  
2123          while (!redraw_thread_cancel) {
2124  
2125                  // Wait
2126 <                Delay_usec(16667);
2127 <
2128 <                // Handle SDL events
2129 <                handle_events();
2130 <
2131 <                // Refresh display
1418 <                video_refresh();
2126 >                next += VIDEO_REFRESH_DELAY;
2127 >                int64 delay = next - GetTicks_usec();
2128 >                if (delay > 0)
2129 >                        Delay_usec(delay);
2130 >                else if (delay < -VIDEO_REFRESH_DELAY)
2131 >                        next = GetTicks_usec();
2132                  ticks++;
2133  
2134 <                // Set new palette if it was changed
2135 <                handle_palette_changes();
2134 > #ifdef SHEEPSHAVER
2135 >                // Pause if requested (during video mode switches)
2136 >                if (thread_stop_req) {
2137 >                        thread_stop_ack = true;
2138 >                        continue;
2139 >                }
2140 > #endif
2141 >
2142 >                // Process pending events and update display
2143 >                do_video_refresh();
2144          }
2145  
2146          uint64 end = GetTicks_usec();
2147          D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1427        redraw_thread_cancel = false;
2148          return 0;
2149   }
2150 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines