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.21 by gbeauche, 2005-11-21T23:38:46Z vs.
Revision 1.46 by asvitkine, 2012-01-14T15:45:18Z

# Line 1 | Line 1
1   /*
2   *  video_sdl.cpp - Video/graphics emulation, SDL specific stuff
3   *
4 < *  Basilisk II (C) 1997-2005 Christian Bauer
4 > *  Basilisk II (C) 1997-2008 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 27 | Line 27
27   *      Ctrl-F5 = grab mouse (in windowed mode)
28   *
29   *  FIXMEs and TODOs:
30 + *  - Windows requires an extra mouse event to update the actual cursor image?
31   *  - Ctr-Tab for suspend/resume but how? SDL does not support that for non-Linux
32   *  - Ctrl-Fn doesn't generate SDL_KEYDOWN events (SDL bug?)
33   *  - Mouse acceleration, there is no API in SDL yet for that
34   *  - 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()?
39   *  - Backport hw cursor acceleration to Basilisk II?
40   *  - Factor out code
41   */
# Line 49 | Line 48
48   #include <errno.h>
49   #include <vector>
50  
51 + #ifdef WIN32
52 + #include <malloc.h> /* alloca() */
53 + #endif
54 +
55   #include "cpu_emulation.h"
56   #include "main.h"
57   #include "adb.h"
# Line 63 | Line 66
66   #define DEBUG 0
67   #include "debug.h"
68  
66
69   // Supported video modes
70   using std::vector;
71   static vector<VIDEO_MODE> VideoModes;
# Line 133 | Line 135 | static SDL_Cursor *sdl_cursor;                                         // C
135   static volatile bool cursor_changed = false;            // Flag: cursor changed, redraw_func must update the cursor
136   static SDL_Color sdl_palette[256];                                      // Color palette to be used as CLUT and gamma table
137   static bool sdl_palette_changed = false;                        // Flag: Palette changed, redraw thread must set new colors
138 < static const int sdl_eventmask = SDL_MOUSEBUTTONDOWNMASK | SDL_MOUSEBUTTONUPMASK | SDL_MOUSEMOTIONMASK | SDL_KEYUPMASK | SDL_KEYDOWNMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK;
138 > static const int sdl_eventmask = SDL_MOUSEEVENTMASK | SDL_KEYEVENTMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK | SDL_ACTIVEEVENTMASK;
139 >
140 > // Mutex to protect SDL events
141 > static SDL_mutex *sdl_events_lock = NULL;
142 > #define LOCK_EVENTS SDL_LockMutex(sdl_events_lock)
143 > #define UNLOCK_EVENTS SDL_UnlockMutex(sdl_events_lock)
144  
145   // Mutex to protect palette
146   static SDL_mutex *sdl_palette_lock = NULL;
# Line 189 | Line 196 | extern void SysMountFirstFloppy(void);
196  
197   static void *vm_acquire_framebuffer(uint32 size)
198   {
199 <        return vm_acquire(size);
199 >        // always try to reallocate framebuffer at the same address
200 >        static void *fb = VM_MAP_FAILED;
201 >        if (fb != VM_MAP_FAILED) {
202 >                if (vm_acquire_fixed(fb, size) < 0) {
203 > #ifndef SHEEPSHAVER
204 >                        printf("FATAL: Could not reallocate framebuffer at previous address\n");
205 > #endif
206 >                        fb = VM_MAP_FAILED;
207 >                }
208 >        }
209 >        if (fb == VM_MAP_FAILED)
210 >                fb = vm_acquire(size, VM_MAP_DEFAULT | VM_MAP_32BIT);
211 >        return fb;
212   }
213  
214   static inline void vm_release_framebuffer(void *fb, uint32 size)
# Line 199 | Line 218 | static inline void vm_release_framebuffe
218  
219  
220   /*
221 + *  Windows message handler
222 + */
223 +
224 + #ifdef WIN32
225 + #include <dbt.h>
226 + static WNDPROC sdl_window_proc = NULL;                          // Window proc used by SDL
227 +
228 + extern void SysMediaArrived(void);
229 + extern void SysMediaRemoved(void);
230 + extern HWND GetMainWindowHandle(void);
231 +
232 + static LRESULT CALLBACK windows_message_handler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
233 + {
234 +        switch (msg) {
235 +        case WM_DEVICECHANGE:
236 +                if (wParam == DBT_DEVICEREMOVECOMPLETE) {
237 +                        DEV_BROADCAST_HDR *p = (DEV_BROADCAST_HDR *)lParam;
238 +                        if (p->dbch_devicetype == DBT_DEVTYP_VOLUME)
239 +                                SysMediaRemoved();
240 +                }
241 +                else if (wParam == DBT_DEVICEARRIVAL) {
242 +                        DEV_BROADCAST_HDR *p = (DEV_BROADCAST_HDR *)lParam;
243 +                        if (p->dbch_devicetype == DBT_DEVTYP_VOLUME)
244 +                                SysMediaArrived();
245 +                }
246 +                return 0;
247 +
248 +        default:
249 +                if (sdl_window_proc)
250 +                        return CallWindowProc(sdl_window_proc, hwnd, msg, wParam, lParam);
251 +        }
252 +
253 +        return DefWindowProc(hwnd, msg, wParam, lParam);
254 + }
255 + #endif
256 +
257 +
258 + /*
259   *  SheepShaver glue
260   */
261  
# Line 243 | Line 300 | static vector<monitor_desc *> VideoMonit
300   // Find Apple mode matching best specified dimensions
301   static int find_apple_resolution(int xsize, int ysize)
302   {
303 <        int apple_id;
304 <        if (xsize < 800)
305 <                apple_id = APPLE_640x480;
306 <        else if (xsize < 1024)
307 <                apple_id = APPLE_800x600;
308 <        else if (xsize < 1152)
309 <                apple_id = APPLE_1024x768;
310 <        else if (xsize < 1280) {
311 <                if (ysize < 900)
312 <                        apple_id = APPLE_1152x768;
313 <                else
314 <                        apple_id = APPLE_1152x900;
315 <        }
316 <        else if (xsize < 1600)
317 <                apple_id = APPLE_1280x1024;
261 <        else
262 <                apple_id = APPLE_1600x1200;
263 <        return apple_id;
264 < }
265 <
266 < // Set parameters to specified Apple mode
267 < static void set_apple_resolution(int apple_id, int &xsize, int &ysize)
268 < {
269 <        switch (apple_id) {
270 <        case APPLE_640x480:
271 <                xsize = 640;
272 <                ysize = 480;
273 <                break;
274 <        case APPLE_800x600:
275 <                xsize = 800;
276 <                ysize = 600;
277 <                break;
278 <        case APPLE_1024x768:
279 <                xsize = 1024;
280 <                ysize = 768;
281 <                break;
282 <        case APPLE_1152x768:
283 <                xsize = 1152;
284 <                ysize = 768;
285 <                break;
286 <        case APPLE_1152x900:
287 <                xsize = 1152;
288 <                ysize = 900;
289 <                break;
290 <        case APPLE_1280x1024:
291 <                xsize = 1280;
292 <                ysize = 1024;
293 <                break;
294 <        case APPLE_1600x1200:
295 <                xsize = 1600;
296 <                ysize = 1200;
297 <                break;
298 <        default:
299 <                abort();
300 <        }
301 < }
302 <
303 < // Match Apple mode matching best specified dimensions
304 < static int match_apple_resolution(int &xsize, int &ysize)
305 < {
306 <        int apple_id = find_apple_resolution(xsize, ysize);
307 <        set_apple_resolution(apple_id, xsize, ysize);
308 <        return apple_id;
303 >        if (xsize == 640 && ysize == 480)
304 >                return APPLE_640x480;
305 >        if (xsize == 800 && ysize == 600)
306 >                return APPLE_800x600;
307 >        if (xsize == 1024 && ysize == 768)
308 >                return APPLE_1024x768;
309 >        if (xsize == 1152 && ysize == 768)
310 >                return APPLE_1152x768;
311 >        if (xsize == 1152 && ysize == 900)
312 >                return APPLE_1152x900;
313 >        if (xsize == 1280 && ysize == 1024)
314 >                return APPLE_1280x1024;
315 >        if (xsize == 1600 && ysize == 1200)
316 >                return APPLE_1600x1200;
317 >        return APPLE_CUSTOM;
318   }
319  
320   // Display error alert
# Line 412 | Line 421 | static int sdl_depth_of_video_depth(int
421          return (video_depth <= VIDEO_DEPTH_8BIT) ? 8 : mac_depth_of_video_depth(video_depth);
422   }
423  
424 + // Get screen dimensions
425 + static void sdl_display_dimensions(int &width, int &height)
426 + {
427 +        static int max_width, max_height;
428 +        if (max_width == 0 && max_height == 0) {
429 +                max_width = 640 ; max_height = 480;
430 +                SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
431 +                if (modes && modes != (SDL_Rect **)-1) {
432 +                        // It turns out that on some implementations, and contrary to the documentation,
433 +                        // the returned list is not sorted from largest to smallest (e.g. Windows)
434 +                        for (int i = 0; modes[i] != NULL; i++) {
435 +                                const int w = modes[i]->w;
436 +                                const int h = modes[i]->h;
437 +                                if (w > max_width && h > max_height) {
438 +                                        max_width = w;
439 +                                        max_height = h;
440 +                                }
441 +                        }
442 +                }
443 +        }
444 +        width = max_width;
445 +        height = max_height;
446 + }
447 +
448 + static inline int sdl_display_width(void)
449 + {
450 +        int width, height;
451 +        sdl_display_dimensions(width, height);
452 +        return width;
453 + }
454 +
455 + static inline int sdl_display_height(void)
456 + {
457 +        int width, height;
458 +        sdl_display_dimensions(width, height);
459 +        return height;
460 + }
461 +
462   // Check wether specified mode is available
463 < static bool has_mode(int type, int width, int height)
463 > static bool has_mode(int type, int width, int height, int depth)
464   {
465   #ifdef SHEEPSHAVER
466 <        // Filter out Classic resolutiosn
466 >        // Filter out Classic resolutions
467          if (width == 512 && height == 384)
468                  return false;
469 + #endif
470  
471 <        // "screen" prefs items always succeeds
472 <        if (PrefsFindString("screen"))
473 <                return true;
426 <
427 <        // Read window & screen modes prefs
428 <        static uint32 window_modes = 0;
429 <        static uint32 screen_modes = 0;
430 <        if (window_modes == 0 || screen_modes == 0) {
431 <                window_modes = PrefsFindInt32("windowmodes");
432 <                screen_modes = PrefsFindInt32("screenmodes");
433 <                if (window_modes == 0 || screen_modes == 0)
434 <                        window_modes |= 3;                      // Allow at least 640x480 and 800x600 window modes
435 <        }
436 <
437 <        int test_modes;
438 <        switch (type) {
439 <        case DISPLAY_WINDOW:
440 <                test_modes = window_modes;
441 <                break;
442 <        case DISPLAY_SCREEN:
443 <                test_modes = screen_modes;
444 <                break;
445 <        default:
446 <                test_modes = 0;
447 <                break;
448 <        }
471 >        // Filter out out-of-bounds resolutions
472 >        if (width > sdl_display_width() || height > sdl_display_height())
473 >                return false;
474  
475 <        int apple_mask;
476 <        switch (find_apple_resolution(width, height)) {
477 <        case APPLE_640x480:             apple_mask = 0x01; break;
478 <        case APPLE_800x600:             apple_mask = 0x02; break;
454 <        case APPLE_1024x768:    apple_mask = 0x04; break;
455 <        case APPLE_1152x768:    apple_mask = 0x40; break;
456 <        case APPLE_1152x900:    apple_mask = 0x08; break;
457 <        case APPLE_1280x1024:   apple_mask = 0x10; break;
458 <        case APPLE_1600x1200:   apple_mask = 0x20; break;
459 <        default:                                apple_mask = 0x00; break;
460 <        }
461 <        return (test_modes & apple_mask);
462 < #endif
463 <        return true;
475 >        // Rely on SDL capabilities
476 >        return SDL_VideoModeOK(width, height,
477 >                                                   sdl_depth_of_video_depth(depth),
478 >                                                   SDL_HWSURFACE | (type == DISPLAY_SCREEN ? SDL_FULLSCREEN : 0));
479   }
480  
481   // Add mode to list of supported modes
482   static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
483   {
484          // Filter out unsupported modes
485 <        if (!has_mode(type, width, height))
485 >        if (!has_mode(type, width, height, depth))
486                  return;
487  
488          // Fill in VideoMode entry
489          VIDEO_MODE mode;
490   #ifdef SHEEPSHAVER
491 <        // Recalculate dimensions to fit Apple modes
477 <        resolution_id = match_apple_resolution(width, height);
491 >        resolution_id = find_apple_resolution(width, height);
492          mode.viType = type;
493   #endif
494          VIDEO_MODE_X = width;
# Line 485 | Line 499 | static void add_mode(int type, int width
499          VideoModes.push_back(mode);
500   }
501  
488 // Add standard list of windowed modes for given color depth
489 static void add_window_modes(int depth)
490 {
491        video_depth vdepth = (video_depth)depth;
492        add_mode(DISPLAY_WINDOW, 512, 384, 0x80, TrivialBytesPerRow(512, vdepth), depth);
493        add_mode(DISPLAY_WINDOW, 640, 480, 0x81, TrivialBytesPerRow(640, vdepth), depth);
494        add_mode(DISPLAY_WINDOW, 800, 600, 0x82, TrivialBytesPerRow(800, vdepth), depth);
495        add_mode(DISPLAY_WINDOW, 1024, 768, 0x83, TrivialBytesPerRow(1024, vdepth), depth);
496        add_mode(DISPLAY_WINDOW, 1152, 870, 0x84, TrivialBytesPerRow(1152, vdepth), depth);
497        add_mode(DISPLAY_WINDOW, 1280, 1024, 0x85, TrivialBytesPerRow(1280, vdepth), depth);
498        add_mode(DISPLAY_WINDOW, 1600, 1200, 0x86, TrivialBytesPerRow(1600, vdepth), depth);
499 }
500
502   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
503   static void set_mac_frame_buffer(SDL_monitor_desc &monitor, int depth, bool native_byte_order)
504   {
# Line 541 | Line 542 | static SDL_GrabMode set_grab_mode(SDL_Gr
542          return (vi && vi->wm_available ? SDL_WM_GrabInput(mode) : SDL_GRAB_OFF);
543   }
544  
545 + // Migrate preferences items (XXX to be handled in MigratePrefs())
546 + static void migrate_screen_prefs(void)
547 + {
548 + #ifdef SHEEPSHAVER
549 +        // Look-up priorities are: "screen", "screenmodes", "windowmodes".
550 +        if (PrefsFindString("screen"))
551 +                return;
552 +
553 +        uint32 window_modes = PrefsFindInt32("windowmodes");
554 +        uint32 screen_modes = PrefsFindInt32("screenmodes");
555 +        int width = 0, height = 0;
556 +        if (screen_modes) {
557 +                static const struct {
558 +                        int id;
559 +                        int width;
560 +                        int height;
561 +                }
562 +                modes[] = {
563 +                        {  1,    640,    480 },
564 +                        {  2,    800,    600 },
565 +                        {  4,   1024,    768 },
566 +                        { 64,   1152,    768 },
567 +                        {  8,   1152,    900 },
568 +                        { 16,   1280,   1024 },
569 +                        { 32,   1600,   1200 },
570 +                        { 0, }
571 +                };
572 +                for (int i = 0; modes[i].id != 0; i++) {
573 +                        if (screen_modes & modes[i].id) {
574 +                                if (width < modes[i].width && height < modes[i].height) {
575 +                                        width = modes[i].width;
576 +                                        height = modes[i].height;
577 +                                }
578 +                        }
579 +                }
580 +        } else {
581 +                if (window_modes & 1)
582 +                        width = 640, height = 480;
583 +                if (window_modes & 2)
584 +                        width = 800, height = 600;
585 +        }
586 +        if (width && height) {
587 +                char str[32];
588 +                sprintf(str, "%s/%d/%d", screen_modes ? "dga" : "win", width, height);
589 +                PrefsReplaceString("screen", str);
590 +        }
591 + #endif
592 + }
593 +
594  
595   /*
596   *  Display "driver" classes
# Line 572 | Line 622 | public:
622   };
623  
624   class driver_window;
625 + #ifdef ENABLE_VOSF
626   static void update_display_window_vosf(driver_window *drv);
627 < static void update_display_dynamic(int ticker, driver_window *drv);
628 < static void update_display_static(driver_window *drv);
627 > #endif
628 > static void update_display_static(driver_base *drv);
629  
630   class driver_window : public driver_base {
631 + #ifdef ENABLE_VOSF
632          friend void update_display_window_vosf(driver_window *drv);
633 <        friend void update_display_dynamic(int ticker, driver_window *drv);
634 <        friend void update_display_static(driver_window *drv);
633 > #endif
634 >        friend void update_display_static(driver_base *drv);
635  
636   public:
637          driver_window(SDL_monitor_desc &monitor);
# Line 680 | Line 732 | void driver_base::restore_mouse_accel(vo
732   *  Windowed display driver
733   */
734  
735 + static bool SDL_display_opened = false;
736 +
737   // Open display
738   driver_window::driver_window(SDL_monitor_desc &m)
739          : driver_base(m), mouse_grabbed(false)
740   {
741          int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
688        int aligned_width = (width + 15) & ~15;
742          int aligned_height = (height + 15) & ~15;
743  
744          // Set absolute mouse mode
745          ADBSetRelMouseMode(mouse_grabbed);
746  
747 +        // This is ugly:
748 +        // If we're switching resolutions (ie, not setting it for the first time),
749 +        // there's a bug in SDL where the SDL_Surface created will not be properly
750 +        // setup. The solution is to SDL_QuitSubSystem(SDL_INIT_VIDEO) before calling
751 +        // SDL_SetVideoMode for the second time (SDL_SetVideoMode will call SDL_Init()
752 +        // and all will be well). Without this, the video becomes corrupted (at least
753 +        // on Mac OS X), after the resolution switch.
754 +        if (SDL_display_opened)
755 +                SDL_QuitSubSystem(SDL_INIT_VIDEO);
756 +
757          // Create surface
758          int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
759          if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL)
760                  return;
761  
762 +        SDL_display_opened = true;
763 +
764   #ifdef ENABLE_VOSF
765          use_vosf = true;
766          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
# Line 827 | Line 892 | driver_fullscreen::driver_fullscreen(SDL
892          : driver_base(m)
893   {
894          int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
830        int aligned_width = (width + 15) & ~15;
895          int aligned_height = (height + 15) & ~15;
896  
897          // Set absolute mouse mode
# Line 997 | Line 1061 | static void keycode_init(void)
1061   bool SDL_monitor_desc::video_open(void)
1062   {
1063          D(bug("video_open()\n"));
1000        const VIDEO_MODE &mode = get_current_mode();
1064   #if DEBUG
1065 +        const VIDEO_MODE &mode = get_current_mode();
1066          D(bug("Current video mode:\n"));
1067          D(bug(" %dx%d (ID %02x), %d bpp\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << (VIDEO_MODE_DEPTH & 0x0f)));
1068   #endif
# Line 1020 | Line 1084 | bool SDL_monitor_desc::video_open(void)
1084                  return false;
1085          }
1086  
1087 + #ifdef WIN32
1088 +        // Chain in a new message handler for WM_DEVICECHANGE
1089 +        HWND the_window = GetMainWindowHandle();
1090 +        sdl_window_proc = (WNDPROC)GetWindowLongPtr(the_window, GWLP_WNDPROC);
1091 +        SetWindowLongPtr(the_window, GWLP_WNDPROC, (LONG_PTR)windows_message_handler);
1092 + #endif
1093 +
1094          // Initialize VideoRefresh function
1095          VideoRefreshInit();
1096  
# Line 1057 | Line 1128 | bool VideoInit(bool classic)
1128   #endif
1129  
1130          // Create Mutexes
1131 +        if ((sdl_events_lock = SDL_CreateMutex()) == NULL)
1132 +                return false;
1133          if ((sdl_palette_lock = SDL_CreateMutex()) == NULL)
1134                  return false;
1135          if ((frame_buffer_lock = SDL_CreateMutex()) == NULL)
# Line 1071 | Line 1144 | bool VideoInit(bool classic)
1144          mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1145  
1146          // Get screen mode from preferences
1147 +        migrate_screen_prefs();
1148          const char *mode_str = NULL;
1149          if (classic_mode)
1150                  mode_str = "win/512/342";
# Line 1094 | Line 1168 | bool VideoInit(bool classic)
1168                  else if (sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1169                          display_type = DISPLAY_SCREEN;
1170          }
1097        int max_width = 640, max_height = 480;
1098        SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
1099        if (modes && modes != (SDL_Rect **)-1) {
1100                // It turns out that on some implementations, and contrary to the documentation,
1101                // the returned list is not sorted from largest to smallest (e.g. Windows)
1102                for (int i = 0; modes[i] != NULL; i++) {
1103                        const int w = modes[i]->w;
1104                        const int h = modes[i]->h;
1105                        if (w > max_width && h > max_height) {
1106                                max_width = w;
1107                                max_height = h;
1108                        }
1109                }
1110                if (default_width > max_width)
1111                        default_width = max_width;
1112                if (default_height > max_height)
1113                        default_height = max_height;
1114        }
1171          if (default_width <= 0)
1172 <                default_width = max_width;
1172 >                default_width = sdl_display_width();
1173 >        else if (default_width > sdl_display_width())
1174 >                default_width = sdl_display_width();
1175          if (default_height <= 0)
1176 <                default_height = max_height;
1176 >                default_height = sdl_display_height();
1177 >        else if (default_height > sdl_display_height())
1178 >                default_height = sdl_display_height();
1179  
1180          // Mac screen depth follows X depth
1181          screen_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
# Line 1135 | Line 1195 | bool VideoInit(bool classic)
1195                  break;
1196          }
1197  
1198 +        // Initialize list of video modes to try
1199 +        struct {
1200 +                int w;
1201 +                int h;
1202 +                int resolution_id;
1203 +        }
1204 +        video_modes[] = {
1205 +                {   -1,   -1, 0x80 },
1206 +                {  512,  384, 0x80 },
1207 +                {  640,  480, 0x81 },
1208 +                {  800,  600, 0x82 },
1209 +                { 1024,  768, 0x83 },
1210 +                { 1152,  870, 0x84 },
1211 +                { 1280, 1024, 0x85 },
1212 +                { 1600, 1200, 0x86 },
1213 +                { 0, }
1214 +        };
1215 +        video_modes[0].w = default_width;
1216 +        video_modes[0].h = default_height;
1217 +
1218          // Construct list of supported modes
1219          if (display_type == DISPLAY_WINDOW) {
1220                  if (classic)
1221                          add_mode(display_type, 512, 342, 0x80, 64, VIDEO_DEPTH_1BIT);
1222                  else {
1223 <                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1224 <                                int bpp = sdl_depth_of_video_depth(d);
1225 <                                if (SDL_VideoModeOK(max_width, max_height, bpp, SDL_HWSURFACE))
1226 <                                        add_window_modes(video_depth(d));
1223 >                        for (int i = 0; video_modes[i].w != 0; i++) {
1224 >                                const int w = video_modes[i].w;
1225 >                                const int h = video_modes[i].h;
1226 >                                if (i > 0 && (w >= default_width || h >= default_height))
1227 >                                        continue;
1228 >                                for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++)
1229 >                                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)d), d);
1230                          }
1231                  }
1232          } else if (display_type == DISPLAY_SCREEN) {
1150                struct {
1151                        int w;
1152                        int h;
1153                        int resolution_id;
1154                }
1155                video_modes[] = {
1156                        {   -1,   -1, 0x80 },
1157                        {  640,  480, 0x81 },
1158                        {  800,  600, 0x82 },
1159                        { 1024,  768, 0x83 },
1160                        { 1152,  870, 0x84 },
1161                        { 1280, 1024, 0x85 },
1162                        { 1600, 1200, 0x86 },
1163                        { 0, }
1164                };
1165                video_modes[0].w = default_width;
1166                video_modes[0].h = default_height;
1167
1233                  for (int i = 0; video_modes[i].w != 0; i++) {
1234                          const int w = video_modes[i].w;
1235                          const int h = video_modes[i].h;
1236                          if (i > 0 && (w >= default_width || h >= default_height))
1237                                  continue;
1238 < #ifdef ENABLE_VOSF
1239 <                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1240 <                                int bpp = sdl_depth_of_video_depth(d);
1241 <                                if (SDL_VideoModeOK(w, h, bpp, SDL_HWSURFACE | SDL_FULLSCREEN))
1177 <                                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)d), d);
1178 <                        }
1179 < #else
1180 <                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)default_depth), default_depth);
1181 < #endif
1238 >                        if (w == 512 && h == 384)
1239 >                                continue;
1240 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++)
1241 >                                add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)d), d);
1242                  }
1243          }
1244  
# Line 1252 | Line 1312 | void SDL_monitor_desc::video_close(void)
1312   {
1313          D(bug("video_close()\n"));
1314  
1315 + #ifdef WIN32
1316 +        // Remove message handler for WM_DEVICECHANGE
1317 +        HWND the_window = GetMainWindowHandle();
1318 +        SetWindowLongPtr(the_window, GWLP_WNDPROC, (LONG_PTR)sdl_window_proc);
1319 + #endif
1320 +
1321          // Stop redraw thread
1322   #ifndef USE_CPU_EMUL_SERVICES
1323          if (redraw_thread_active) {
# Line 1282 | Line 1348 | void VideoExit(void)
1348                  SDL_DestroyMutex(frame_buffer_lock);
1349          if (sdl_palette_lock)
1350                  SDL_DestroyMutex(sdl_palette_lock);
1351 +        if (sdl_events_lock)
1352 +                SDL_DestroyMutex(sdl_events_lock);
1353   }
1354  
1355  
# Line 1451 | Line 1519 | int16 video_mode_change(VidLocals *csSav
1519   void SDL_monitor_desc::switch_to_current_mode(void)
1520   {
1521          // Close and reopen display
1522 +        LOCK_EVENTS;
1523          video_close();
1524          video_open();
1525 +        UNLOCK_EVENTS;
1526  
1527          if (drv == NULL) {
1528                  ErrorAlert(STR_OPEN_WINDOW_ERR);
# Line 1468 | Line 1538 | void SDL_monitor_desc::switch_to_current
1538   #ifdef SHEEPSHAVER
1539   bool video_can_change_cursor(void)
1540   {
1541 <        return (display_type == DISPLAY_WINDOW);
1541 >        static char driver[] = "Quartz?";
1542 >        static int quartzok = -1;
1543 >
1544 >        if (display_type != DISPLAY_WINDOW)
1545 >                return false;
1546 >
1547 >        if (quartzok < 0) {
1548 >                if (SDL_VideoDriverName(driver, sizeof driver) == NULL || strncmp(driver, "Quartz", sizeof driver))
1549 >                        quartzok = true;
1550 >                else {
1551 >                        // Quartz driver bug prevents cursor changing in SDL 1.2.11 to 1.2.14.
1552 >                        const SDL_version *vp = SDL_Linked_Version();
1553 >                        int version = SDL_VERSIONNUM(vp->major, vp->minor, vp->patch);
1554 >                        quartzok = (version <= SDL_VERSIONNUM(1, 2, 10) || version >= SDL_VERSIONNUM(1, 2, 15));
1555 >                }
1556 >        }
1557 >
1558 >        return quartzok;
1559   }
1560   #endif
1561  
# Line 1679 | Line 1766 | static void handle_events(void)
1766                          // Mouse button
1767                          case SDL_MOUSEBUTTONDOWN: {
1768                                  unsigned int button = event.button.button;
1769 <                                if (button < 4)
1770 <                                        ADBMouseDown(button - 1);
1769 >                                if (button == SDL_BUTTON_LEFT)
1770 >                                        ADBMouseDown(0);
1771 >                                else if (button == SDL_BUTTON_RIGHT)
1772 >                                        ADBMouseDown(1);
1773 >                                else if (button == SDL_BUTTON_MIDDLE)
1774 >                                        ADBMouseDown(2);
1775                                  else if (button < 6) {  // Wheel mouse
1776                                          if (mouse_wheel_mode == 0) {
1777                                                  int key = (button == 5) ? 0x79 : 0x74;  // Page up/down
# Line 1698 | Line 1789 | static void handle_events(void)
1789                          }
1790                          case SDL_MOUSEBUTTONUP: {
1791                                  unsigned int button = event.button.button;
1792 <                                if (button < 4)
1793 <                                        ADBMouseUp(button - 1);
1792 >                                if (button == SDL_BUTTON_LEFT)
1793 >                                        ADBMouseUp(0);
1794 >                                else if (button == SDL_BUTTON_RIGHT)
1795 >                                        ADBMouseUp(1);
1796 >                                else if (button == SDL_BUTTON_MIDDLE)
1797 >                                        ADBMouseUp(2);
1798                                  break;
1799                          }
1800  
# Line 1783 | Line 1878 | static void handle_events(void)
1878                                  ADBKeyDown(0x7f);       // Power key
1879                                  ADBKeyUp(0x7f);
1880                                  break;
1881 +
1882 +                        // Application activate/deactivate; consume the event but otherwise ignore it
1883 +                        case SDL_ACTIVEEVENT:
1884 +                                break;
1885                          }
1886                  }
1887          }
# Line 1794 | Line 1893 | static void handle_events(void)
1893   */
1894  
1895   // Static display update (fixed frame rate, but incremental)
1896 < static void update_display_static(driver_window *drv)
1896 > static void update_display_static(driver_base *drv)
1897   {
1898          // Incremental update code
1899          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
# Line 1883 | Line 1982 | static void update_display_static(driver
1982  
1983                  } else {
1984                          const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
1985 +                        const int dst_bytes_per_row = drv->s->pitch;
1986  
1987                          x1 = VIDEO_MODE_X;
1988                          for (j=y1; j<=y2; j++) {
# Line 1923 | Line 2023 | static void update_display_static(driver
2023                                  // Blit to screen surface
2024                                  for (j=y1; j<=y2; j++) {
2025                                          i = j * bytes_per_row + x1 * bytes_per_pixel;
2026 +                                        int dst_i = j * dst_bytes_per_row + x1 * bytes_per_pixel;
2027                                          memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
2028 <                                        Screen_blit((uint8 *)drv->s->pixels + i, the_buffer + i, bytes_per_pixel * wide);
2028 >                                        Screen_blit((uint8 *)drv->s->pixels + dst_i, the_buffer + i, bytes_per_pixel * wide);
2029                                  }
2030  
2031                                  // Unlock surface, if required
# Line 1938 | Line 2039 | static void update_display_static(driver
2039          }
2040   }
2041  
2042 + // Static display update (fixed frame rate, bounding boxes based)
2043 + // XXX use NQD bounding boxes to help detect dirty areas?
2044 + static void update_display_static_bbox(driver_base *drv)
2045 + {
2046 +        const VIDEO_MODE &mode = drv->mode;
2047 +
2048 +        // Allocate bounding boxes for SDL_UpdateRects()
2049 +        const int N_PIXELS = 64;
2050 +        const int n_x_boxes = (VIDEO_MODE_X + N_PIXELS - 1) / N_PIXELS;
2051 +        const int n_y_boxes = (VIDEO_MODE_Y + N_PIXELS - 1) / N_PIXELS;
2052 +        SDL_Rect *boxes = (SDL_Rect *)alloca(sizeof(SDL_Rect) * n_x_boxes * n_y_boxes);
2053 +        int nr_boxes = 0;
2054 +
2055 +        // Lock surface, if required
2056 +        if (SDL_MUSTLOCK(drv->s))
2057 +                SDL_LockSurface(drv->s);
2058 +
2059 +        // Update the surface from Mac screen
2060 +        const int bytes_per_row = VIDEO_MODE_ROW_BYTES;
2061 +        const int bytes_per_pixel = bytes_per_row / VIDEO_MODE_X;
2062 +        const int dst_bytes_per_row = drv->s->pitch;
2063 +        int x, y;
2064 +        for (y = 0; y < VIDEO_MODE_Y; y += N_PIXELS) {
2065 +                int h = N_PIXELS;
2066 +                if (h > VIDEO_MODE_Y - y)
2067 +                        h = VIDEO_MODE_Y - y;
2068 +                for (x = 0; x < VIDEO_MODE_X; x += N_PIXELS) {
2069 +                        int w = N_PIXELS;
2070 +                        if (w > VIDEO_MODE_X - x)
2071 +                                w = VIDEO_MODE_X - x;
2072 +                        const int xs = w * bytes_per_pixel;
2073 +                        const int xb = x * bytes_per_pixel;
2074 +                        bool dirty = false;
2075 +                        for (int j = y; j < (y + h); j++) {
2076 +                                const int yb = j * bytes_per_row;
2077 +                                const int dst_yb = j * dst_bytes_per_row;
2078 +                                if (memcmp(&the_buffer[yb + xb], &the_buffer_copy[yb + xb], xs) != 0) {
2079 +                                        memcpy(&the_buffer_copy[yb + xb], &the_buffer[yb + xb], xs);
2080 +                                        Screen_blit((uint8 *)drv->s->pixels + dst_yb + xb, the_buffer + yb + xb, xs);
2081 +                                        dirty = true;
2082 +                                }
2083 +                        }
2084 +                        if (dirty) {
2085 +                                boxes[nr_boxes].x = x;
2086 +                                boxes[nr_boxes].y = y;
2087 +                                boxes[nr_boxes].w = w;
2088 +                                boxes[nr_boxes].h = h;
2089 +                                nr_boxes++;
2090 +                        }
2091 +                }
2092 +        }
2093 +
2094 +        // Unlock surface, if required
2095 +        if (SDL_MUSTLOCK(drv->s))
2096 +                SDL_UnlockSurface(drv->s);
2097 +
2098 +        // Refresh display
2099 +        if (nr_boxes)
2100 +                SDL_UpdateRects(drv->s, nr_boxes, boxes);
2101 + }
2102 +
2103  
2104   // We suggest the compiler to inline the next two functions so that it
2105   // may specialise the code according to the current screen depth and
# Line 1979 | Line 2141 | static inline void handle_palette_change
2141          UNLOCK_PALETTE;
2142   }
2143  
2144 + static void video_refresh_window_static(void);
2145 +
2146   static void video_refresh_dga(void)
2147   {
2148          // Quit DGA mode if requested
2149          possibly_quit_dga_mode();
2150 +        video_refresh_window_static();
2151   }
2152  
2153   #ifdef ENABLE_VOSF
# Line 2032 | Line 2197 | static void video_refresh_window_static(
2197          static int tick_counter = 0;
2198          if (++tick_counter >= frame_skip) {
2199                  tick_counter = 0;
2200 <                update_display_static(static_cast<driver_window *>(drv));
2200 >                const VIDEO_MODE &mode = drv->mode;
2201 >                if ((int)VIDEO_MODE_DEPTH >= VIDEO_DEPTH_8BIT)
2202 >                        update_display_static_bbox(drv);
2203 >                else
2204 >                        update_display_static(drv);
2205          }
2206   }
2207  
# Line 2074 | Line 2243 | static inline void do_video_refresh(void
2243          // Set new cursor image if it was changed
2244          if (cursor_changed && sdl_cursor) {
2245                  cursor_changed = false;
2246 +                LOCK_EVENTS;
2247                  SDL_FreeCursor(sdl_cursor);
2248                  sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
2249 <                if (sdl_cursor)
2249 >                if (sdl_cursor) {
2250 >                        SDL_ShowCursor(private_data == NULL || private_data->cursorVisible);
2251                          SDL_SetCursor(sdl_cursor);
2252 + #ifdef WIN32
2253 +                        // XXX Windows apparently needs an extra mouse event to
2254 +                        // make the new cursor image visible
2255 +                        int visible = SDL_ShowCursor(-1);
2256 +                        if (visible) {
2257 +                                int x, y;
2258 +                                SDL_GetMouseState(&x, &y);
2259 +                                SDL_WarpMouse(x, y);
2260 +                        }
2261 + #endif
2262 +                }
2263 +                UNLOCK_EVENTS;
2264          }
2265   #endif
2266  
# Line 2135 | Line 2318 | static int redraw_func(void *arg)
2318          return 0;
2319   }
2320   #endif
2321 +
2322 +
2323 + /*
2324 + *  Record dirty area from NQD
2325 + */
2326 +
2327 + #ifdef SHEEPSHAVER
2328 + void video_set_dirty_area(int x, int y, int w, int h)
2329 + {
2330 + #ifdef ENABLE_VOSF
2331 +        const VIDEO_MODE &mode = drv->mode;
2332 +        const int screen_width = VIDEO_MODE_X;
2333 +        const int screen_height = VIDEO_MODE_Y;
2334 +        const int bytes_per_row = VIDEO_MODE_ROW_BYTES;
2335 +
2336 +        if (use_vosf) {
2337 +                vosf_set_dirty_area(x, y, w, h, screen_width, screen_height, bytes_per_row);
2338 +                return;
2339 +        }
2340 + #endif
2341 +
2342 +        // XXX handle dirty bounding boxes for non-VOSF modes
2343 + }
2344 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines