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.7 by gbeauche, 2004-06-26T15:22:01Z 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?
# Line 37 | Line 38
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 < *  - Move generic Native QuickDraw acceleration routines to gfxaccel.cpp
41 > *  - Factor out code
42   */
43  
44   #include "sysdeps.h"
# Line 57 | Line 58
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"
# Line 82 | Line 84 | static int display_type = DISPLAY_WINDOW
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 95 | Line 101 | static uint8 *the_buffer_copy = NULL;
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 = false;                                           // Flag: VOSF enabled
# Line 123 | Line 135 | static SDL_Color sdl_palette[256];                                     /
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;
145   #define LOCK_PALETTE SDL_LockMutex(sdl_palette_lock)
# Line 146 | 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  
# Line 325 | Line 383 | static inline int bytes_per_pixel(int de
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) {
# Line 353 | 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   {
359        // FIXME: no fullscreen support yet
360        if (type == DISPLAY_SCREEN)
361                return false;
362
423   #ifdef SHEEPSHAVER
424          // Filter out Classic resolutiosn
425          if (width == 512 && height == 384)
426                  return false;
427  
428 <        // Read window modes prefs
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) {
# Line 375 | Line 439 | static bool has_mode(int type, int width
439                          window_modes |= 3;                      // Allow at least 640x480 and 800x600 window modes
440          }
441  
442 <        if (type == DISPLAY_WINDOW) {
443 <                int apple_mask, apple_id = find_apple_resolution(width, height);
444 <                switch (apple_id) {
445 <                case APPLE_640x480:             apple_mask = 0x01; break;
446 <                case APPLE_800x600:             apple_mask = 0x02; break;
447 <                case APPLE_1024x768:    apple_mask = 0x04; break;
448 <                case APPLE_1152x768:    apple_mask = 0x40; break;
449 <                case APPLE_1152x900:    apple_mask = 0x08; break;
450 <                case APPLE_1280x1024:   apple_mask = 0x10; break;
451 <                case APPLE_1600x1200:   apple_mask = 0x20; break;
452 <                default:                                apple_mask = 0x00; break;
389 <                }
390 <                return (window_modes & apple_mask);
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 < #else
455 <        return true;
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 false;
468 >        return true;
469   }
470  
471   // Add mode to list of supported modes
# Line 413 | Line 486 | static void add_mode(int type, int width
486          VIDEO_MODE_Y = height;
487          VIDEO_MODE_RESOLUTION = resolution_id;
488          VIDEO_MODE_ROW_BYTES = bytes_per_row;
489 <        VIDEO_MODE_DEPTH = depth;
489 >        VIDEO_MODE_DEPTH = (video_depth)depth;
490          VideoModes.push_back(mode);
491   }
492  
# Line 469 | 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 528 | 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 549 | 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) {
554                if (the_buffer) {
555                        free(the_buffer);
556                        the_buffer = NULL;
557                }
640                  if (the_buffer_copy) {
641                          free(the_buffer_copy);
642                          the_buffer_copy = NULL;
# Line 562 | Line 644 | driver_base::~driver_base()
644          }
645   #ifdef ENABLE_VOSF
646          else {
565                // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
566                if (the_buffer != VM_MAP_FAILED) {
567                        D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
568                        vm_release(the_buffer, the_buffer_size);
569                        the_buffer = NULL;
570                }
647                  if (the_host_buffer) {
648                          D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
649                          free(the_host_buffer);
# Line 578 | 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 587 | Line 666 | void driver_base::update_palette(void)
666   {
667          const VIDEO_MODE &mode = monitor.get_current_mode();
668  
669 <        if (VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT)
669 >        if ((int)VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT)
670                  SDL_SetPalette(s, SDL_PHYSPAL, sdl_palette, 0, 256);
671   }
672  
# Line 618 | Line 697 | driver_window::driver_window(SDL_monitor
697          ADBSetRelMouseMode(mouse_grabbed);
698  
699          // Create surface
700 <        int depth = (VIDEO_MODE_DEPTH <= VIDEO_DEPTH_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 627 | 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  
# Line 638 | Line 717 | driver_window::driver_window(SDL_monitor
717          }
718          else if (!video_vosf_profitable()) {
719                  video_vosf_exit();
720 <                // WarningAlert(STR_VOSF_NOT_PROFITABLE_WARN);
642 <                printf("VOSF acceleration is not profitable on this platform\n");
720 >                printf("VOSF acceleration is not profitable on this platform, disabling it\n");
721                  use_vosf = false;
722          }
723          if (!use_vosf) {
# Line 652 | Line 730 | driver_window::driver_window(SDL_monitor
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 *)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          
# Line 677 | 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(VIDEO_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 744 | 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 775 | 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 787 | 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 820 | 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 838 | Line 1013 | bool SDL_monitor_desc::video_open(void)
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 854 | 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 = ((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  
# Line 880 | 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 895 | Line 1079 | bool VideoInit(bool classic)
1079  
1080          // Get screen mode from preferences
1081          const char *mode_str = NULL;
898 #ifndef SHEEPSHAVER
1082          if (classic_mode)
1083                  mode_str = "win/512/342";
1084          else
1085                  mode_str = PrefsFindString("screen");
903 #endif
1086  
1087          // Determine display type and default dimensions
1088          int default_width, default_height;
# Line 916 | Line 1098 | bool VideoInit(bool classic)
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          SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
1106          if (modes && modes != (SDL_Rect **)-1) {
1107 <                max_width = modes[0]->w;
1108 <                max_height = modes[0]->h;
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)
# Line 956 | Line 1148 | bool VideoInit(bool classic)
1148                          add_mode(display_type, 512, 342, 0x80, 64, VIDEO_DEPTH_1BIT);
1149                  else {
1150                          for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1151 <                                int bpp = (d <= VIDEO_DEPTH_8BIT ? 8 : sdl_depth_of_video_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(display_type, default_width, default_height, 0x80, TrivialBytesPerRow(default_width, (video_depth)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 1034 | 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);
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  
1047 #ifdef ENABLE_VOSF
1048        if (use_vosf) {
1049                // Deinitialize VOSF
1050                video_vosf_exit();
1051        }
1052 #endif
1053
1275          // Close display
1276          delete drv;
1277          drv = NULL;
# Line 1068 | 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 1148 | Line 1371 | void SDL_monitor_desc::set_palette(uint8
1371          const VIDEO_MODE &mode = get_current_mode();
1372  
1373          // FIXME: how can we handle the gamma ramp?
1374 <        if (VIDEO_MODE_DEPTH > VIDEO_DEPTH_8BIT)
1374 >        if ((int)VIDEO_MODE_DEPTH > VIDEO_DEPTH_8BIT)
1375                  return;
1376  
1377          LOCK_PALETTE;
# Line 1209 | Line 1432 | int16 video_mode_change(VidLocals *csSav
1432                          csSave->saveData = ReadMacInt32(ParamPtr + csData);
1433                          csSave->savePage = ReadMacInt16(ParamPtr + csPage);
1434  
1435 <                        // Disable interrupts
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];
# Line 1221 | Line 1447 | int16 video_mode_change(VidLocals *csSav
1447                          csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
1448                          csSave->saveMode=VModes[cur_mode].viAppleMode;
1449  
1450 <                        // Enable interrupts
1450 >                        // Enable interrupts and resume redraw thread
1451 >                        thread_stop_req = false;
1452                          EnableInterrupt();
1453                          return noErr;
1454                  }
# Line 1233 | Line 1460 | int16 video_mode_change(VidLocals *csSav
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 1268 | Line 1497 | void video_set_cursor(void)
1497  
1498  
1499   /*
1500 < *  Install graphics acceleration
1500 > *  Keyboard-related utilify functions
1501   */
1502  
1503 < #ifdef SHEEPSHAVER
1275 < // Rectangle inversion
1276 < template< int bpp >
1277 < static inline void do_invrect(uint8 *dest, uint32 length)
1278 < {
1279 < #define INVERT_1(PTR, OFS) ((uint8  *)(PTR))[OFS] = ~((uint8  *)(PTR))[OFS]
1280 < #define INVERT_2(PTR, OFS) ((uint16 *)(PTR))[OFS] = ~((uint16 *)(PTR))[OFS]
1281 < #define INVERT_4(PTR, OFS) ((uint32 *)(PTR))[OFS] = ~((uint32 *)(PTR))[OFS]
1282 < #define INVERT_8(PTR, OFS) ((uint64 *)(PTR))[OFS] = ~((uint64 *)(PTR))[OFS]
1283 <
1284 < #ifndef UNALIGNED_PROFITABLE
1285 <        // Align on 16-bit boundaries
1286 <        if (bpp < 16 && (((uintptr)dest) & 1)) {
1287 <                INVERT_1(dest, 0);
1288 <                dest += 1; length -= 1;
1289 <        }
1290 <
1291 <        // Align on 32-bit boundaries
1292 <        if (bpp < 32 && (((uintptr)dest) & 2)) {
1293 <                INVERT_2(dest, 0);
1294 <                dest += 2; length -= 2;
1295 <        }
1296 < #endif
1297 <
1298 <        // Invert 8-byte words
1299 <        if (length >= 8) {
1300 <                const int r = (length / 8) % 8;
1301 <                dest += r * 8;
1302 <
1303 <                int n = ((length / 8) + 7) / 8;
1304 <                switch (r) {
1305 <                case 0: do {
1306 <                                dest += 64;
1307 <                                INVERT_8(dest, -8);
1308 <                case 7: INVERT_8(dest, -7);
1309 <                case 6: INVERT_8(dest, -6);
1310 <                case 5: INVERT_8(dest, -5);
1311 <                case 4: INVERT_8(dest, -4);
1312 <                case 3: INVERT_8(dest, -3);
1313 <                case 2: INVERT_8(dest, -2);
1314 <                case 1: INVERT_8(dest, -1);
1315 <                                } while (--n > 0);
1316 <                }
1317 <        }
1318 <
1319 <        // 32-bit cell to invert?
1320 <        if (length & 4) {
1321 <                INVERT_4(dest, 0);
1322 <                if (bpp <= 16)
1323 <                        dest += 4;
1324 <        }
1325 <
1326 <        // 16-bit cell to invert?
1327 <        if (bpp <= 16 && (length & 2)) {
1328 <                INVERT_2(dest, 0);
1329 <                if (bpp <= 8)
1330 <                        dest += 2;
1331 <        }
1332 <
1333 <        // 8-bit cell to invert?
1334 <        if (bpp <= 8 && (length & 1))
1335 <                INVERT_1(dest, 0);
1336 <
1337 < #undef INVERT_1
1338 < #undef INVERT_2
1339 < #undef INVERT_4
1340 < #undef INVERT_8
1341 < }
1342 <
1343 < void NQD_invrect(uint32 p)
1344 < {
1345 <        D(bug("accl_invrect %08x\n", p));
1346 <
1347 <        // Get inversion parameters
1348 <        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1349 <        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1350 <        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1351 <        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1352 <        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1353 <        D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1354 <
1355 <        //!!?? pen_mode == 14
1356 <
1357 <        // And perform the inversion
1358 <        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1359 <        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1360 <        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1361 <        width *= bpp;
1362 <        switch (bpp) {
1363 <        case 1:
1364 <                for (int i = 0; i < height; i++) {
1365 <                        do_invrect<8>(dest, width);
1366 <                        dest += dest_row_bytes;
1367 <                }
1368 <                break;
1369 <        case 2:
1370 <                for (int i = 0; i < height; i++) {
1371 <                        do_invrect<16>(dest, width);
1372 <                        dest += dest_row_bytes;
1373 <                }
1374 <                break;
1375 <        case 4:
1376 <                for (int i = 0; i < height; i++) {
1377 <                        do_invrect<32>(dest, width);
1378 <                        dest += dest_row_bytes;
1379 <                }
1380 <                break;
1381 <        }
1382 < }
1383 <
1384 < // Rectangle filling
1385 < template< int bpp >
1386 < static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1387 < {
1388 < #define FILL_1(PTR, OFS, VAL) ((uint8  *)(PTR))[OFS] = (VAL)
1389 < #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1390 < #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1391 < #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
1392 <
1393 < #ifndef UNALIGNED_PROFITABLE
1394 <        // Align on 16-bit boundaries
1395 <        if (bpp < 16 && (((uintptr)dest) & 1)) {
1396 <                FILL_1(dest, 0, color);
1397 <                dest += 1; length -= 1;
1398 <        }
1399 <
1400 <        // Align on 32-bit boundaries
1401 <        if (bpp < 32 && (((uintptr)dest) & 2)) {
1402 <                FILL_2(dest, 0, color);
1403 <                dest += 2; length -= 2;
1404 <        }
1405 < #endif
1406 <
1407 <        // Fill 8-byte words
1408 <        if (length >= 8) {
1409 <                const uint64 c = (((uint64)color) << 32) | color;
1410 <                const int r = (length / 8) % 8;
1411 <                dest += r * 8;
1412 <
1413 <                int n = ((length / 8) + 7) / 8;
1414 <                switch (r) {
1415 <                case 0: do {
1416 <                                dest += 64;
1417 <                                FILL_8(dest, -8, c);
1418 <                case 7: FILL_8(dest, -7, c);
1419 <                case 6: FILL_8(dest, -6, c);
1420 <                case 5: FILL_8(dest, -5, c);
1421 <                case 4: FILL_8(dest, -4, c);
1422 <                case 3: FILL_8(dest, -3, c);
1423 <                case 2: FILL_8(dest, -2, c);
1424 <                case 1: FILL_8(dest, -1, c);
1425 <                                } while (--n > 0);
1426 <                }
1427 <        }
1428 <
1429 <        // 32-bit cell to fill?
1430 <        if (length & 4) {
1431 <                FILL_4(dest, 0, color);
1432 <                if (bpp <= 16)
1433 <                        dest += 4;
1434 <        }
1435 <
1436 <        // 16-bit cell to fill?
1437 <        if (bpp <= 16 && (length & 2)) {
1438 <                FILL_2(dest, 0, color);
1439 <                if (bpp <= 8)
1440 <                        dest += 2;
1441 <        }
1442 <
1443 <        // 8-bit cell to fill?
1444 <        if (bpp <= 8 && (length & 1))
1445 <                FILL_1(dest, 0, color);
1446 <
1447 < #undef FILL_1
1448 < #undef FILL_2
1449 < #undef FILL_4
1450 < #undef FILL_8
1451 < }
1452 <
1453 < void NQD_fillrect(uint32 p)
1454 < {
1455 <        D(bug("accl_fillrect %08x\n", p));
1456 <
1457 <        // Get filling parameters
1458 <        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1459 <        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1460 <        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1461 <        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1462 <        uint32 color = htonl(ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen));
1463 <        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1464 <        D(bug(" width %d, height %d\n", width, height));
1465 <        D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1466 <
1467 <        // And perform the fill
1468 <        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1469 <        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1470 <        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1471 <        width *= bpp;
1472 <        switch (bpp) {
1473 <        case 1:
1474 <                for (int i = 0; i < height; i++) {
1475 <                        memset(dest, color, width);
1476 <                        dest += dest_row_bytes;
1477 <                }
1478 <                break;
1479 <        case 2:
1480 <                for (int i = 0; i < height; i++) {
1481 <                        do_fillrect<16>(dest, color, width);
1482 <                        dest += dest_row_bytes;
1483 <                }
1484 <                break;
1485 <        case 4:
1486 <                for (int i = 0; i < height; i++) {
1487 <                        do_fillrect<32>(dest, color, width);
1488 <                        dest += dest_row_bytes;
1489 <                }
1490 <                break;
1491 <        }
1492 < }
1493 <
1494 < bool NQD_fillrect_hook(uint32 p)
1503 > static bool is_modifier_key(SDL_KeyboardEvent const & e)
1504   {
1505 <        D(bug("accl_fillrect_hook %08x\n", p));
1506 <
1507 <        // Check if we can accelerate this fillrect
1508 <        if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1509 <                const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1510 <                if (transfer_mode == 8) {
1511 <                        // Fill
1512 <                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1513 <                        return true;
1514 <                }
1515 <                else if (transfer_mode == 10) {
1516 <                        // Invert
1517 <                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1518 <                        return true;
1519 <                }
1520 <        }
1512 <        return false;
1513 < }
1514 <
1515 < // Rectangle blitting
1516 < // TODO: optimize for VOSF and target pixmap == screen
1517 < void NQD_bitblt(uint32 p)
1518 < {
1519 <        D(bug("accl_bitblt %08x\n", p));
1520 <
1521 <        // Get blitting parameters
1522 <        int16 src_X  = (int16)ReadMacInt16(p + acclSrcRect + 2) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 2);
1523 <        int16 src_Y  = (int16)ReadMacInt16(p + acclSrcRect + 0) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 0);
1524 <        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1525 <        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1526 <        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1527 <        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1528 <        D(bug(" src addr %08x, dest addr %08x\n", ReadMacInt32(p + acclSrcBaseAddr), ReadMacInt32(p + acclDestBaseAddr)));
1529 <        D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1530 <        D(bug(" width %d, height %d\n", width, height));
1531 <
1532 <        // And perform the blit
1533 <        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1534 <        width *= bpp;
1535 <        if ((int32)ReadMacInt32(p + acclSrcRowBytes) > 0) {
1536 <                const int src_row_bytes = (int32)ReadMacInt32(p + acclSrcRowBytes);
1537 <                const int dst_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1538 <                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + (src_Y * src_row_bytes) + (src_X * bpp));
1539 <                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dst_row_bytes) + (dest_X * bpp));
1540 <                for (int i = 0; i < height; i++) {
1541 <                        memmove(dst, src, width);
1542 <                        src += src_row_bytes;
1543 <                        dst += dst_row_bytes;
1544 <                }
1545 <        }
1546 <        else {
1547 <                const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1548 <                const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1549 <                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp));
1550 <                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp));
1551 <                for (int i = height - 1; i >= 0; i--) {
1552 <                        memmove(dst, src, width);
1553 <                        src -= src_row_bytes;
1554 <                        dst -= dst_row_bytes;
1555 <                }
1556 <        }
1557 < }
1558 <
1559 < /*
1560 <  BitBlt transfer modes:
1561 <  0 : srcCopy
1562 <  1 : srcOr
1563 <  2 : srcXor
1564 <  3 : srcBic
1565 <  4 : notSrcCopy
1566 <  5 : notSrcOr
1567 <  6 : notSrcXor
1568 <  7 : notSrcBic
1569 <  32 : blend
1570 <  33 : addPin
1571 <  34 : addOver
1572 <  35 : subPin
1573 <  36 : transparent
1574 <  37 : adMax
1575 <  38 : subOver
1576 <  39 : adMin
1577 <  50 : hilite
1578 < */
1579 <
1580 < bool NQD_bitblt_hook(uint32 p)
1581 < {
1582 <        D(bug("accl_draw_hook %08x\n", p));
1583 <
1584 <        // Check if we can accelerate this bitblt
1585 <        if (ReadMacInt32(p + 0x018) + ReadMacInt32(p + 0x128) == 0 &&
1586 <                ReadMacInt32(p + 0x130) == 0 &&
1587 <                ReadMacInt32(p + acclSrcPixelSize) >= 8 &&
1588 <                ReadMacInt32(p + acclSrcPixelSize) == ReadMacInt32(p + acclDestPixelSize) &&
1589 <                (ReadMacInt32(p + acclSrcRowBytes) ^ ReadMacInt32(p + acclDestRowBytes)) >= 0 && // same sign?
1590 <                ReadMacInt32(p + acclTransferMode) == 0 &&                                                                               // srcCopy?
1591 <                ReadMacInt32(p + 0x15c) > 0) {
1592 <
1593 <                // Yes, set function pointer
1594 <                WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
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 < // Wait for graphics operation to finish
1601 < bool NQD_sync_hook(uint32 arg)
1602 < {
1603 <        D(bug("accl_sync_hook %08x\n", arg));
1604 <        return true;
1605 < }
1606 <
1607 < void VideoInstallAccel(void)
1526 > static bool is_ctrl_down(SDL_keysym const & ks)
1527   {
1528 <        // Install acceleration hooks
1610 <        if (PrefsFindBool("gfxaccel")) {
1611 <                D(bug("Video: Installing acceleration hooks\n"));
1612 <                uint32 base;
1613 <
1614 <                SheepVar bitblt_hook_info(sizeof(accl_hook_info));
1615 <                base = bitblt_hook_info.addr();
1616 <                WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK));
1617 <                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1618 <                WriteMacInt32(base + 8, ACCL_BITBLT);
1619 <                NQDMisc(6, bitblt_hook_info.ptr());
1620 <
1621 <                SheepVar fillrect_hook_info(sizeof(accl_hook_info));
1622 <                base = fillrect_hook_info.addr();
1623 <                WriteMacInt32(base + 0, NativeTVECT(NATIVE_FILLRECT_HOOK));
1624 <                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1625 <                WriteMacInt32(base + 8, ACCL_FILLRECT);
1626 <                NQDMisc(6, fillrect_hook_info.ptr());
1627 <        }
1528 >        return ctrl_down || (ks.mod & KMOD_CTRL);
1529   }
1629 #endif
1530  
1531  
1532   /*
# Line 1634 | Line 1534 | void VideoInstallAccel(void)
1534   *  and -2 if the key was recognized as a hotkey
1535   */
1536  
1637 static bool is_ctrl_down(SDL_keysym const & ks)
1638 {
1639        return ctrl_down || (ks.mod & KMOD_CTRL);
1640 }
1641
1537   static int kc_decode(SDL_keysym const & ks, bool key_down)
1538   {
1539          switch (ks.sym) {
# Line 1671 | 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 1708 | 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 1818 | 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 1846 | 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 1919 | 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 (VIDEO_MODE_DEPTH < VIDEO_DEPTH_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 = VIDEO_MODE_X / src_bytes_per_row;
# Line 2096 | 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 2160 | 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();
# Line 2180 | Line 2131 | static int redraw_func(void *arg)
2131                          next = GetTicks_usec();
2132                  ticks++;
2133  
2183                // Handle SDL events
2184                handle_events();
2185
2186                // Refresh display
2187                video_refresh();
2188
2134   #ifdef SHEEPSHAVER
2135 <                // Set new cursor image if it was changed
2136 <                if (cursor_changed && sdl_cursor) {
2137 <                        cursor_changed = false;
2138 <                        SDL_FreeCursor(sdl_cursor);
2194 <                        sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
2195 <                        if (sdl_cursor)
2196 <                                SDL_SetCursor(sdl_cursor);
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 <                // Set new palette if it was changed
2143 <                handle_palette_changes();
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)));
2148          return 0;
2149   }
2150 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines