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.23 by gbeauche, 2006-03-28T07:01:19Z vs.
Revision 1.42 by asvitkine, 2011-12-28T22:15:10Z

# 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  
69 + #if (defined(__APPLE__) && defined(__MACH__))
70 + extern "C" {
71 +        void NSAutoReleasePool_wrap(void (*fn)(void));
72 + }
73 + #endif
74  
75   // Supported video modes
76   using std::vector;
# Line 133 | Line 141 | static SDL_Cursor *sdl_cursor;                                         // C
141   static volatile bool cursor_changed = false;            // Flag: cursor changed, redraw_func must update the cursor
142   static SDL_Color sdl_palette[256];                                      // Color palette to be used as CLUT and gamma table
143   static bool sdl_palette_changed = false;                        // Flag: Palette changed, redraw thread must set new colors
144 < static const int sdl_eventmask = SDL_MOUSEBUTTONDOWNMASK | SDL_MOUSEBUTTONUPMASK | SDL_MOUSEMOTIONMASK | SDL_KEYUPMASK | SDL_KEYDOWNMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK;
144 > static const int sdl_eventmask = SDL_MOUSEEVENTMASK | SDL_KEYEVENTMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK | SDL_ACTIVEEVENTMASK;
145  
146   // Mutex to protect SDL events
147   static SDL_mutex *sdl_events_lock = NULL;
# Line 194 | Line 202 | extern void SysMountFirstFloppy(void);
202  
203   static void *vm_acquire_framebuffer(uint32 size)
204   {
205 <        return vm_acquire(size);
205 >        // always try to reallocate framebuffer at the same address
206 >        static void *fb = VM_MAP_FAILED;
207 >        if (fb != VM_MAP_FAILED) {
208 >                if (vm_acquire_fixed(fb, size) < 0) {
209 > #ifndef SHEEPSHAVER
210 >                        printf("FATAL: Could not reallocate framebuffer at previous address\n");
211 > #endif
212 >                        fb = VM_MAP_FAILED;
213 >                }
214 >        }
215 >        if (fb == VM_MAP_FAILED)
216 >                fb = vm_acquire(size, VM_MAP_DEFAULT | VM_MAP_32BIT);
217 >        return fb;
218   }
219  
220   static inline void vm_release_framebuffer(void *fb, uint32 size)
# Line 286 | Line 306 | static vector<monitor_desc *> VideoMonit
306   // Find Apple mode matching best specified dimensions
307   static int find_apple_resolution(int xsize, int ysize)
308   {
309 <        int apple_id;
310 <        if (xsize < 800)
311 <                apple_id = APPLE_640x480;
312 <        else if (xsize < 1024)
313 <                apple_id = APPLE_800x600;
314 <        else if (xsize < 1152)
315 <                apple_id = APPLE_1024x768;
316 <        else if (xsize < 1280) {
317 <                if (ysize < 900)
318 <                        apple_id = APPLE_1152x768;
319 <                else
320 <                        apple_id = APPLE_1152x900;
321 <        }
322 <        else if (xsize < 1600)
323 <                apple_id = APPLE_1280x1024;
304 <        else
305 <                apple_id = APPLE_1600x1200;
306 <        return apple_id;
307 < }
308 <
309 < // Set parameters to specified Apple mode
310 < static void set_apple_resolution(int apple_id, int &xsize, int &ysize)
311 < {
312 <        switch (apple_id) {
313 <        case APPLE_640x480:
314 <                xsize = 640;
315 <                ysize = 480;
316 <                break;
317 <        case APPLE_800x600:
318 <                xsize = 800;
319 <                ysize = 600;
320 <                break;
321 <        case APPLE_1024x768:
322 <                xsize = 1024;
323 <                ysize = 768;
324 <                break;
325 <        case APPLE_1152x768:
326 <                xsize = 1152;
327 <                ysize = 768;
328 <                break;
329 <        case APPLE_1152x900:
330 <                xsize = 1152;
331 <                ysize = 900;
332 <                break;
333 <        case APPLE_1280x1024:
334 <                xsize = 1280;
335 <                ysize = 1024;
336 <                break;
337 <        case APPLE_1600x1200:
338 <                xsize = 1600;
339 <                ysize = 1200;
340 <                break;
341 <        default:
342 <                abort();
343 <        }
344 < }
345 <
346 < // Match Apple mode matching best specified dimensions
347 < static int match_apple_resolution(int &xsize, int &ysize)
348 < {
349 <        int apple_id = find_apple_resolution(xsize, ysize);
350 <        set_apple_resolution(apple_id, xsize, ysize);
351 <        return apple_id;
309 >        if (xsize == 640 && ysize == 480)
310 >                return APPLE_640x480;
311 >        if (xsize == 800 && ysize == 600)
312 >                return APPLE_800x600;
313 >        if (xsize == 1024 && ysize == 768)
314 >                return APPLE_1024x768;
315 >        if (xsize == 1152 && ysize == 768)
316 >                return APPLE_1152x768;
317 >        if (xsize == 1152 && ysize == 900)
318 >                return APPLE_1152x900;
319 >        if (xsize == 1280 && ysize == 1024)
320 >                return APPLE_1280x1024;
321 >        if (xsize == 1600 && ysize == 1200)
322 >                return APPLE_1600x1200;
323 >        return APPLE_CUSTOM;
324   }
325  
326   // Display error alert
# Line 455 | Line 427 | static int sdl_depth_of_video_depth(int
427          return (video_depth <= VIDEO_DEPTH_8BIT) ? 8 : mac_depth_of_video_depth(video_depth);
428   }
429  
430 + // Get screen dimensions
431 + static void sdl_display_dimensions(int &width, int &height)
432 + {
433 +        static int max_width, max_height;
434 +        if (max_width == 0 && max_height == 0) {
435 +                max_width = 640 ; max_height = 480;
436 +                SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
437 +                if (modes && modes != (SDL_Rect **)-1) {
438 +                        // It turns out that on some implementations, and contrary to the documentation,
439 +                        // the returned list is not sorted from largest to smallest (e.g. Windows)
440 +                        for (int i = 0; modes[i] != NULL; i++) {
441 +                                const int w = modes[i]->w;
442 +                                const int h = modes[i]->h;
443 +                                if (w > max_width && h > max_height) {
444 +                                        max_width = w;
445 +                                        max_height = h;
446 +                                }
447 +                        }
448 +                }
449 +        }
450 +        width = max_width;
451 +        height = max_height;
452 + }
453 +
454 + static inline int sdl_display_width(void)
455 + {
456 +        int width, height;
457 +        sdl_display_dimensions(width, height);
458 +        return width;
459 + }
460 +
461 + static inline int sdl_display_height(void)
462 + {
463 +        int width, height;
464 +        sdl_display_dimensions(width, height);
465 +        return height;
466 + }
467 +
468   // Check wether specified mode is available
469 < static bool has_mode(int type, int width, int height)
469 > static bool has_mode(int type, int width, int height, int depth)
470   {
471   #ifdef SHEEPSHAVER
472 <        // Filter out Classic resolutiosn
472 >        // Filter out Classic resolutions
473          if (width == 512 && height == 384)
474                  return false;
475 + #endif
476  
477 <        // "screen" prefs items always succeeds
478 <        if (PrefsFindString("screen"))
479 <                return true;
469 <
470 <        // Read window & screen modes prefs
471 <        static uint32 window_modes = 0;
472 <        static uint32 screen_modes = 0;
473 <        if (window_modes == 0 || screen_modes == 0) {
474 <                window_modes = PrefsFindInt32("windowmodes");
475 <                screen_modes = PrefsFindInt32("screenmodes");
476 <                if (window_modes == 0 || screen_modes == 0)
477 <                        window_modes |= 3;                      // Allow at least 640x480 and 800x600 window modes
478 <        }
479 <
480 <        int test_modes;
481 <        switch (type) {
482 <        case DISPLAY_WINDOW:
483 <                test_modes = window_modes;
484 <                break;
485 <        case DISPLAY_SCREEN:
486 <                test_modes = screen_modes;
487 <                break;
488 <        default:
489 <                test_modes = 0;
490 <                break;
491 <        }
477 >        // Filter out out-of-bounds resolutions
478 >        if (width > sdl_display_width() || height > sdl_display_height())
479 >                return false;
480  
481 <        int apple_mask;
482 <        switch (find_apple_resolution(width, height)) {
483 <        case APPLE_640x480:             apple_mask = 0x01; break;
484 <        case APPLE_800x600:             apple_mask = 0x02; break;
497 <        case APPLE_1024x768:    apple_mask = 0x04; break;
498 <        case APPLE_1152x768:    apple_mask = 0x40; break;
499 <        case APPLE_1152x900:    apple_mask = 0x08; break;
500 <        case APPLE_1280x1024:   apple_mask = 0x10; break;
501 <        case APPLE_1600x1200:   apple_mask = 0x20; break;
502 <        default:                                apple_mask = 0x00; break;
503 <        }
504 <        return (test_modes & apple_mask);
505 < #endif
506 <        return true;
481 >        // Rely on SDL capabilities
482 >        return SDL_VideoModeOK(width, height,
483 >                                                   sdl_depth_of_video_depth(depth),
484 >                                                   SDL_HWSURFACE | (type == DISPLAY_SCREEN ? SDL_FULLSCREEN : 0));
485   }
486  
487   // Add mode to list of supported modes
488   static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
489   {
490          // Filter out unsupported modes
491 <        if (!has_mode(type, width, height))
491 >        if (!has_mode(type, width, height, depth))
492                  return;
493  
494          // Fill in VideoMode entry
495          VIDEO_MODE mode;
496   #ifdef SHEEPSHAVER
497 <        // Recalculate dimensions to fit Apple modes
520 <        resolution_id = match_apple_resolution(width, height);
497 >        resolution_id = find_apple_resolution(width, height);
498          mode.viType = type;
499   #endif
500          VIDEO_MODE_X = width;
# Line 528 | Line 505 | static void add_mode(int type, int width
505          VideoModes.push_back(mode);
506   }
507  
531 // Add standard list of windowed modes for given color depth
532 static void add_window_modes(int depth)
533 {
534        video_depth vdepth = (video_depth)depth;
535        add_mode(DISPLAY_WINDOW, 512, 384, 0x80, TrivialBytesPerRow(512, vdepth), depth);
536        add_mode(DISPLAY_WINDOW, 640, 480, 0x81, TrivialBytesPerRow(640, vdepth), depth);
537        add_mode(DISPLAY_WINDOW, 800, 600, 0x82, TrivialBytesPerRow(800, vdepth), depth);
538        add_mode(DISPLAY_WINDOW, 1024, 768, 0x83, TrivialBytesPerRow(1024, vdepth), depth);
539        add_mode(DISPLAY_WINDOW, 1152, 870, 0x84, TrivialBytesPerRow(1152, vdepth), depth);
540        add_mode(DISPLAY_WINDOW, 1280, 1024, 0x85, TrivialBytesPerRow(1280, vdepth), depth);
541        add_mode(DISPLAY_WINDOW, 1600, 1200, 0x86, TrivialBytesPerRow(1600, vdepth), depth);
542 }
543
508   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
509   static void set_mac_frame_buffer(SDL_monitor_desc &monitor, int depth, bool native_byte_order)
510   {
# Line 584 | Line 548 | static SDL_GrabMode set_grab_mode(SDL_Gr
548          return (vi && vi->wm_available ? SDL_WM_GrabInput(mode) : SDL_GRAB_OFF);
549   }
550  
551 + // Migrate preferences items (XXX to be handled in MigratePrefs())
552 + static void migrate_screen_prefs(void)
553 + {
554 + #ifdef SHEEPSHAVER
555 +        // Look-up priorities are: "screen", "screenmodes", "windowmodes".
556 +        if (PrefsFindString("screen"))
557 +                return;
558 +
559 +        uint32 window_modes = PrefsFindInt32("windowmodes");
560 +        uint32 screen_modes = PrefsFindInt32("screenmodes");
561 +        int width = 0, height = 0;
562 +        if (screen_modes) {
563 +                static const struct {
564 +                        int id;
565 +                        int width;
566 +                        int height;
567 +                }
568 +                modes[] = {
569 +                        {  1,    640,    480 },
570 +                        {  2,    800,    600 },
571 +                        {  4,   1024,    768 },
572 +                        { 64,   1152,    768 },
573 +                        {  8,   1152,    900 },
574 +                        { 16,   1280,   1024 },
575 +                        { 32,   1600,   1200 },
576 +                        { 0, }
577 +                };
578 +                for (int i = 0; modes[i].id != 0; i++) {
579 +                        if (screen_modes & modes[i].id) {
580 +                                if (width < modes[i].width && height < modes[i].height) {
581 +                                        width = modes[i].width;
582 +                                        height = modes[i].height;
583 +                                }
584 +                        }
585 +                }
586 +        } else {
587 +                if (window_modes & 1)
588 +                        width = 640, height = 480;
589 +                if (window_modes & 2)
590 +                        width = 800, height = 600;
591 +        }
592 +        if (width && height) {
593 +                char str[32];
594 +                sprintf(str, "%s/%d/%d", screen_modes ? "dga" : "win", width, height);
595 +                PrefsReplaceString("screen", str);
596 +        }
597 + #endif
598 + }
599 +
600  
601   /*
602   *  Display "driver" classes
# Line 617 | Line 630 | public:
630   class driver_window;
631   static void update_display_window_vosf(driver_window *drv);
632   static void update_display_dynamic(int ticker, driver_window *drv);
633 < static void update_display_static(driver_window *drv);
633 > static void update_display_static(driver_base *drv);
634  
635   class driver_window : public driver_base {
636          friend void update_display_window_vosf(driver_window *drv);
637          friend void update_display_dynamic(int ticker, driver_window *drv);
638 <        friend void update_display_static(driver_window *drv);
638 >        friend void update_display_static(driver_base *drv);
639  
640   public:
641          driver_window(SDL_monitor_desc &monitor);
# Line 723 | Line 736 | void driver_base::restore_mouse_accel(vo
736   *  Windowed display driver
737   */
738  
739 + static bool SDL_display_opened = false;
740 +
741   // Open display
742   driver_window::driver_window(SDL_monitor_desc &m)
743          : driver_base(m), mouse_grabbed(false)
# Line 734 | Line 749 | driver_window::driver_window(SDL_monitor
749          // Set absolute mouse mode
750          ADBSetRelMouseMode(mouse_grabbed);
751  
752 +        // This is ugly:
753 +        // If we're switching resolutions (ie, not setting it for the first time),
754 +        // there's a bug in SDL where the SDL_Surface created will not be properly
755 +        // setup. The solution is to SDL_QuitSubSystem(SDL_INIT_VIDEO) before calling
756 +        // SDL_SetVideoMode for the second time (SDL_SetVideoMode will call SDL_Init()
757 +        // and all will be well). Without this, the video becomes corrupted (at least
758 +        // on Mac OS X), after the resolution switch.
759 +        if (SDL_display_opened)
760 +                SDL_QuitSubSystem(SDL_INIT_VIDEO);
761 +
762          // Create surface
763          int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
764          if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL)
765                  return;
766  
767 +        SDL_display_opened = true;
768 +
769   #ifdef ENABLE_VOSF
770          use_vosf = true;
771          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
# Line 1123 | Line 1150 | bool VideoInit(bool classic)
1150          mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1151  
1152          // Get screen mode from preferences
1153 +        migrate_screen_prefs();
1154          const char *mode_str = NULL;
1155          if (classic_mode)
1156                  mode_str = "win/512/342";
# Line 1146 | Line 1174 | bool VideoInit(bool classic)
1174                  else if (sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1175                          display_type = DISPLAY_SCREEN;
1176          }
1149        int max_width = 640, max_height = 480;
1150        SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
1151        if (modes && modes != (SDL_Rect **)-1) {
1152                // It turns out that on some implementations, and contrary to the documentation,
1153                // the returned list is not sorted from largest to smallest (e.g. Windows)
1154                for (int i = 0; modes[i] != NULL; i++) {
1155                        const int w = modes[i]->w;
1156                        const int h = modes[i]->h;
1157                        if (w > max_width && h > max_height) {
1158                                max_width = w;
1159                                max_height = h;
1160                        }
1161                }
1162                if (default_width > max_width)
1163                        default_width = max_width;
1164                if (default_height > max_height)
1165                        default_height = max_height;
1166        }
1177          if (default_width <= 0)
1178 <                default_width = max_width;
1178 >                default_width = sdl_display_width();
1179 >        else if (default_width > sdl_display_width())
1180 >                default_width = sdl_display_width();
1181          if (default_height <= 0)
1182 <                default_height = max_height;
1182 >                default_height = sdl_display_height();
1183 >        else if (default_height > sdl_display_height())
1184 >                default_height = sdl_display_height();
1185  
1186          // Mac screen depth follows X depth
1187          screen_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
# Line 1187 | Line 1201 | bool VideoInit(bool classic)
1201                  break;
1202          }
1203  
1204 +        // Initialize list of video modes to try
1205 +        struct {
1206 +                int w;
1207 +                int h;
1208 +                int resolution_id;
1209 +        }
1210 +        video_modes[] = {
1211 +                {   -1,   -1, 0x80 },
1212 +                {  512,  384, 0x80 },
1213 +                {  640,  480, 0x81 },
1214 +                {  800,  600, 0x82 },
1215 +                { 1024,  768, 0x83 },
1216 +                { 1152,  870, 0x84 },
1217 +                { 1280, 1024, 0x85 },
1218 +                { 1600, 1200, 0x86 },
1219 +                { 0, }
1220 +        };
1221 +        video_modes[0].w = default_width;
1222 +        video_modes[0].h = default_height;
1223 +
1224          // Construct list of supported modes
1225          if (display_type == DISPLAY_WINDOW) {
1226                  if (classic)
1227                          add_mode(display_type, 512, 342, 0x80, 64, VIDEO_DEPTH_1BIT);
1228                  else {
1229 <                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1230 <                                int bpp = sdl_depth_of_video_depth(d);
1231 <                                if (SDL_VideoModeOK(max_width, max_height, bpp, SDL_HWSURFACE))
1232 <                                        add_window_modes(video_depth(d));
1229 >                        for (int i = 0; video_modes[i].w != 0; i++) {
1230 >                                const int w = video_modes[i].w;
1231 >                                const int h = video_modes[i].h;
1232 >                                if (i > 0 && (w >= default_width || h >= default_height))
1233 >                                        continue;
1234 >                                for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++)
1235 >                                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)d), d);
1236                          }
1237                  }
1238          } else if (display_type == DISPLAY_SCREEN) {
1202                struct {
1203                        int w;
1204                        int h;
1205                        int resolution_id;
1206                }
1207                video_modes[] = {
1208                        {   -1,   -1, 0x80 },
1209                        {  640,  480, 0x81 },
1210                        {  800,  600, 0x82 },
1211                        { 1024,  768, 0x83 },
1212                        { 1152,  870, 0x84 },
1213                        { 1280, 1024, 0x85 },
1214                        { 1600, 1200, 0x86 },
1215                        { 0, }
1216                };
1217                video_modes[0].w = default_width;
1218                video_modes[0].h = default_height;
1219
1239                  for (int i = 0; video_modes[i].w != 0; i++) {
1240                          const int w = video_modes[i].w;
1241                          const int h = video_modes[i].h;
1242                          if (i > 0 && (w >= default_width || h >= default_height))
1243                                  continue;
1244 < #ifdef ENABLE_VOSF
1245 <                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1246 <                                int bpp = sdl_depth_of_video_depth(d);
1247 <                                if (SDL_VideoModeOK(w, h, bpp, SDL_HWSURFACE | SDL_FULLSCREEN))
1229 <                                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)d), d);
1230 <                        }
1231 < #else
1232 <                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)default_depth), default_depth);
1233 < #endif
1244 >                        if (w == 512 && h == 384)
1245 >                                continue;
1246 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++)
1247 >                                add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)d), d);
1248                  }
1249          }
1250  
# Line 1530 | Line 1544 | void SDL_monitor_desc::switch_to_current
1544   #ifdef SHEEPSHAVER
1545   bool video_can_change_cursor(void)
1546   {
1547 <        return (display_type == DISPLAY_WINDOW);
1547 >        static char driver[] = "Quartz?";
1548 >        static int quartzok = -1;
1549 >
1550 >        if (display_type != DISPLAY_WINDOW)
1551 >                return false;
1552 >
1553 >        if (quartzok < 0) {
1554 >                if (SDL_VideoDriverName(driver, sizeof driver) == NULL || strncmp(driver, "Quartz", sizeof driver))
1555 >                        quartzok = true;
1556 >                else {
1557 >                        // Quartz driver bug prevents cursor changing in SDL 1.2.11 and later
1558 >                        const SDL_version *vp = SDL_Linked_Version();
1559 >                        quartzok = SDL_VERSIONNUM(vp->major, vp->minor, vp->patch) <= SDL_VERSIONNUM(1, 2, 10);
1560 >                }
1561 >        }
1562 >
1563 >        return quartzok;
1564   }
1565   #endif
1566  
# Line 1741 | Line 1771 | static void handle_events(void)
1771                          // Mouse button
1772                          case SDL_MOUSEBUTTONDOWN: {
1773                                  unsigned int button = event.button.button;
1774 <                                if (button < 4)
1775 <                                        ADBMouseDown(button - 1);
1774 >                                if (button == SDL_BUTTON_LEFT)
1775 >                                        ADBMouseDown(0);
1776 >                                else if (button == SDL_BUTTON_RIGHT)
1777 >                                        ADBMouseDown(1);
1778 >                                else if (button == SDL_BUTTON_MIDDLE)
1779 >                                        ADBMouseDown(2);
1780                                  else if (button < 6) {  // Wheel mouse
1781                                          if (mouse_wheel_mode == 0) {
1782                                                  int key = (button == 5) ? 0x79 : 0x74;  // Page up/down
# Line 1760 | Line 1794 | static void handle_events(void)
1794                          }
1795                          case SDL_MOUSEBUTTONUP: {
1796                                  unsigned int button = event.button.button;
1797 <                                if (button < 4)
1798 <                                        ADBMouseUp(button - 1);
1797 >                                if (button == SDL_BUTTON_LEFT)
1798 >                                        ADBMouseUp(0);
1799 >                                else if (button == SDL_BUTTON_RIGHT)
1800 >                                        ADBMouseUp(1);
1801 >                                else if (button == SDL_BUTTON_MIDDLE)
1802 >                                        ADBMouseUp(2);
1803                                  break;
1804                          }
1805  
# Line 1845 | Line 1883 | static void handle_events(void)
1883                                  ADBKeyDown(0x7f);       // Power key
1884                                  ADBKeyUp(0x7f);
1885                                  break;
1886 +
1887 +                        // Application activate/deactivate; consume the event but otherwise ignore it
1888 +                        case SDL_ACTIVEEVENT:
1889 +                                break;
1890                          }
1891                  }
1892          }
# Line 1856 | Line 1898 | static void handle_events(void)
1898   */
1899  
1900   // Static display update (fixed frame rate, but incremental)
1901 < static void update_display_static(driver_window *drv)
1901 > static void update_display_static(driver_base *drv)
1902   {
1903          // Incremental update code
1904          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
# Line 1945 | Line 1987 | static void update_display_static(driver
1987  
1988                  } else {
1989                          const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
1990 +                        const int dst_bytes_per_row = drv->s->pitch;
1991  
1992                          x1 = VIDEO_MODE_X;
1993                          for (j=y1; j<=y2; j++) {
# Line 1985 | Line 2028 | static void update_display_static(driver
2028                                  // Blit to screen surface
2029                                  for (j=y1; j<=y2; j++) {
2030                                          i = j * bytes_per_row + x1 * bytes_per_pixel;
2031 +                                        int dst_i = j * dst_bytes_per_row + x1 * bytes_per_pixel;
2032                                          memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
2033 <                                        Screen_blit((uint8 *)drv->s->pixels + i, the_buffer + i, bytes_per_pixel * wide);
2033 >                                        Screen_blit((uint8 *)drv->s->pixels + dst_i, the_buffer + i, bytes_per_pixel * wide);
2034                                  }
2035  
2036                                  // Unlock surface, if required
# Line 2000 | Line 2044 | static void update_display_static(driver
2044          }
2045   }
2046  
2047 + // Static display update (fixed frame rate, bounding boxes based)
2048 + // XXX use NQD bounding boxes to help detect dirty areas?
2049 + static void update_display_static_bbox(driver_base *drv)
2050 + {
2051 +        const VIDEO_MODE &mode = drv->mode;
2052 +
2053 +        // Allocate bounding boxes for SDL_UpdateRects()
2054 +        const int N_PIXELS = 64;
2055 +        const int n_x_boxes = (VIDEO_MODE_X + N_PIXELS - 1) / N_PIXELS;
2056 +        const int n_y_boxes = (VIDEO_MODE_Y + N_PIXELS - 1) / N_PIXELS;
2057 +        SDL_Rect *boxes = (SDL_Rect *)alloca(sizeof(SDL_Rect) * n_x_boxes * n_y_boxes);
2058 +        int nr_boxes = 0;
2059 +
2060 +        // Lock surface, if required
2061 +        if (SDL_MUSTLOCK(drv->s))
2062 +                SDL_LockSurface(drv->s);
2063 +
2064 +        // Update the surface from Mac screen
2065 +        const int bytes_per_row = VIDEO_MODE_ROW_BYTES;
2066 +        const int bytes_per_pixel = bytes_per_row / VIDEO_MODE_X;
2067 +        const int dst_bytes_per_row = drv->s->pitch;
2068 +        int x, y;
2069 +        for (y = 0; y < VIDEO_MODE_Y; y += N_PIXELS) {
2070 +                int h = N_PIXELS;
2071 +                if (h > VIDEO_MODE_Y - y)
2072 +                        h = VIDEO_MODE_Y - y;
2073 +                for (x = 0; x < VIDEO_MODE_X; x += N_PIXELS) {
2074 +                        int w = N_PIXELS;
2075 +                        if (w > VIDEO_MODE_X - x)
2076 +                                w = VIDEO_MODE_X - x;
2077 +                        const int xs = w * bytes_per_pixel;
2078 +                        const int xb = x * bytes_per_pixel;
2079 +                        bool dirty = false;
2080 +                        for (int j = y; j < (y + h); j++) {
2081 +                                const int yb = j * bytes_per_row;
2082 +                                const int dst_yb = j * dst_bytes_per_row;
2083 +                                if (memcmp(&the_buffer[yb + xb], &the_buffer_copy[yb + xb], xs) != 0) {
2084 +                                        memcpy(&the_buffer_copy[yb + xb], &the_buffer[yb + xb], xs);
2085 +                                        Screen_blit((uint8 *)drv->s->pixels + dst_yb + xb, the_buffer + yb + xb, xs);
2086 +                                        dirty = true;
2087 +                                }
2088 +                        }
2089 +                        if (dirty) {
2090 +                                boxes[nr_boxes].x = x;
2091 +                                boxes[nr_boxes].y = y;
2092 +                                boxes[nr_boxes].w = w;
2093 +                                boxes[nr_boxes].h = h;
2094 +                                nr_boxes++;
2095 +                        }
2096 +                }
2097 +        }
2098 +
2099 +        // Unlock surface, if required
2100 +        if (SDL_MUSTLOCK(drv->s))
2101 +                SDL_UnlockSurface(drv->s);
2102 +
2103 +        // Refresh display
2104 +        if (nr_boxes)
2105 +                SDL_UpdateRects(drv->s, nr_boxes, boxes);
2106 + }
2107 +
2108  
2109   // We suggest the compiler to inline the next two functions so that it
2110   // may specialise the code according to the current screen depth and
# Line 2041 | Line 2146 | static inline void handle_palette_change
2146          UNLOCK_PALETTE;
2147   }
2148  
2149 + static void video_refresh_window_static(void);
2150 +
2151   static void video_refresh_dga(void)
2152   {
2153          // Quit DGA mode if requested
2154          possibly_quit_dga_mode();
2155 +        video_refresh_window_static();
2156   }
2157  
2158   #ifdef ENABLE_VOSF
# Line 2094 | Line 2202 | static void video_refresh_window_static(
2202          static int tick_counter = 0;
2203          if (++tick_counter >= frame_skip) {
2204                  tick_counter = 0;
2205 <                update_display_static(static_cast<driver_window *>(drv));
2205 >                const VIDEO_MODE &mode = drv->mode;
2206 >                if ((int)VIDEO_MODE_DEPTH >= VIDEO_DEPTH_8BIT)
2207 >                        update_display_static_bbox(drv);
2208 >                else
2209 >                        update_display_static(drv);
2210          }
2211   }
2212  
# Line 2130 | Line 2242 | static inline void do_video_refresh(void
2242          handle_events();
2243  
2244          // Update display
2245 + #if (defined(__APPLE__) && defined(__MACH__))
2246 +        // SDL expects an auto-release pool to be present.
2247 +        NSAutoReleasePool_wrap(video_refresh);
2248 + #else
2249          video_refresh();
2250 + #endif
2251  
2252   #ifdef SHEEPSHAVER
2253          // Set new cursor image if it was changed
# Line 2139 | Line 2256 | static inline void do_video_refresh(void
2256                  LOCK_EVENTS;
2257                  SDL_FreeCursor(sdl_cursor);
2258                  sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
2259 <                if (sdl_cursor)
2259 >                if (sdl_cursor) {
2260 >                        SDL_ShowCursor(private_data == NULL || private_data->cursorVisible);
2261                          SDL_SetCursor(sdl_cursor);
2262 + #ifdef WIN32
2263 +                        // XXX Windows apparently needs an extra mouse event to
2264 +                        // make the new cursor image visible
2265 +                        int visible = SDL_ShowCursor(-1);
2266 +                        if (visible) {
2267 +                                int x, y;
2268 +                                SDL_GetMouseState(&x, &y);
2269 +                                SDL_WarpMouse(x, y);
2270 +                        }
2271 + #endif
2272 +                }
2273                  UNLOCK_EVENTS;
2274          }
2275   #endif
# Line 2199 | Line 2328 | static int redraw_func(void *arg)
2328          return 0;
2329   }
2330   #endif
2331 +
2332 +
2333 + /*
2334 + *  Record dirty area from NQD
2335 + */
2336 +
2337 + #ifdef SHEEPSHAVER
2338 + void video_set_dirty_area(int x, int y, int w, int h)
2339 + {
2340 + #ifdef ENABLE_VOSF
2341 +        const VIDEO_MODE &mode = drv->mode;
2342 +        const int screen_width = VIDEO_MODE_X;
2343 +        const int screen_height = VIDEO_MODE_Y;
2344 +        const int bytes_per_row = VIDEO_MODE_ROW_BYTES;
2345 +
2346 +        if (use_vosf) {
2347 +                vosf_set_dirty_area(x, y, w, h, screen_width, screen_height, bytes_per_row);
2348 +                return;
2349 +        }
2350 + #endif
2351 +
2352 +        // XXX handle dirty bounding boxes for non-VOSF modes
2353 + }
2354 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines