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.4 by gbeauche, 2004-06-24T15:25:57Z vs.
Revision 1.20 by gbeauche, 2005-06-14T22:35:42Z

# 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 35 | Line 36
36   *  - Events processing is bound to the general emulation thread as SDL requires
37   *    to PumpEvents() within the same thread as the one that called SetVideoMode().
38   *    Besides, there can't seem to be a way to call SetVideoMode() from a child thread.
39 + *  - Refresh performance is still slow. Use SDL_CreateRGBSurface()?
40 + *  - Backport hw cursor acceleration to Basilisk II?
41 + *  - Factor out code
42   */
43  
44   #include "sysdeps.h"
# Line 54 | 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 79 | 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 92 | 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 = true;                                            // Flag: VOSF enabled
114 > static bool use_vosf = false;                                           // Flag: VOSF enabled
115   #else
116   static const bool use_vosf = false;                                     // VOSF not possible
117   #endif
# Line 114 | Line 129 | static int keycode_table[256];                                         // X
129  
130   // SDL variables
131   static int screen_depth;                                                        // Depth of current screen
132 + static SDL_Cursor *sdl_cursor;                                          // Copy of Mac cursor
133 + static volatile bool cursor_changed = false;            // Flag: cursor changed, redraw_func must update the cursor
134   static SDL_Color sdl_palette[256];                                      // Color palette to be used as CLUT and gamma table
135   static bool sdl_palette_changed = false;                        // Flag: Palette changed, redraw thread must set new colors
136   static const int sdl_eventmask = SDL_MOUSEBUTTONDOWNMASK | SDL_MOUSEBUTTONUPMASK | SDL_MOUSEMOTIONMASK | SDL_KEYUPMASK | SDL_KEYDOWNMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK;
# Line 141 | Line 158 | extern void SysMountFirstFloppy(void);
158  
159  
160   /*
161 + *  Framebuffer allocation routines
162 + */
163 +
164 + static void *vm_acquire_framebuffer(uint32 size)
165 + {
166 + #ifdef SHEEPSHAVER
167 + #ifdef DIRECT_ADDRESSING_HACK
168 +        const uint32 FRAME_BUFFER_BASE = 0x61000000;
169 +        uint8 *fb = Mac2HostAddr(FRAME_BUFFER_BASE);
170 +        if (vm_acquire_fixed(fb, size) < 0)
171 +                fb = VM_MAP_FAILED;
172 +        return fb;
173 + #endif
174 + #endif
175 +        return vm_acquire(size);
176 + }
177 +
178 + static inline void vm_release_framebuffer(void *fb, uint32 size)
179 + {
180 +        vm_release(fb, size);
181 + }
182 +
183 +
184 + /*
185   *  SheepShaver glue
186   */
187  
# Line 255 | Line 296 | static void ErrorAlert(int error)
296   {
297          ErrorAlert(GetString(error));
298   }
299 +
300 + // Display warning alert
301 + static void WarningAlert(int warning)
302 + {
303 +        WarningAlert(GetString(warning));
304 + }
305   #endif
306  
307  
# Line 314 | Line 361 | static inline int bytes_per_pixel(int de
361   }
362  
363   // Map video_mode depth ID to numerical depth value
364 < static int sdl_depth_of_video_depth(int video_depth)
364 > static int mac_depth_of_video_depth(int video_depth)
365   {
366          int depth = -1;
367          switch (video_depth) {
# Line 342 | Line 389 | static int sdl_depth_of_video_depth(int
389          return depth;
390   }
391  
392 < // Add mode to list of supported modes
393 < static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
392 > // Map video_mode depth ID to SDL screen depth
393 > static int sdl_depth_of_video_depth(int video_depth)
394 > {
395 >        return (video_depth <= VIDEO_DEPTH_8BIT) ? 8 : mac_depth_of_video_depth(video_depth);
396 > }
397 >
398 > // Check wether specified mode is available
399 > static bool has_mode(int type, int width, int height)
400   {
348        VIDEO_MODE mode;
401   #ifdef SHEEPSHAVER
402 <        // Don't add 512x384 modes
402 >        // Filter out Classic resolutiosn
403          if (width == 512 && height == 384)
404 +                return false;
405 +
406 +        // "screen" prefs items always succeeds
407 +        if (PrefsFindString("screen"))
408 +                return true;
409 +
410 +        // Read window & screen modes prefs
411 +        static uint32 window_modes = 0;
412 +        static uint32 screen_modes = 0;
413 +        if (window_modes == 0 || screen_modes == 0) {
414 +                window_modes = PrefsFindInt32("windowmodes");
415 +                screen_modes = PrefsFindInt32("screenmodes");
416 +                if (window_modes == 0 || screen_modes == 0)
417 +                        window_modes |= 3;                      // Allow at least 640x480 and 800x600 window modes
418 +        }
419 +
420 +        int test_modes;
421 +        switch (type) {
422 +        case DISPLAY_WINDOW:
423 +                test_modes = window_modes;
424 +                break;
425 +        case DISPLAY_SCREEN:
426 +                test_modes = screen_modes;
427 +                break;
428 +        default:
429 +                test_modes = 0;
430 +                break;
431 +        }
432 +
433 +        int apple_mask;
434 +        switch (find_apple_resolution(width, height)) {
435 +        case APPLE_640x480:             apple_mask = 0x01; break;
436 +        case APPLE_800x600:             apple_mask = 0x02; break;
437 +        case APPLE_1024x768:    apple_mask = 0x04; break;
438 +        case APPLE_1152x768:    apple_mask = 0x40; break;
439 +        case APPLE_1152x900:    apple_mask = 0x08; break;
440 +        case APPLE_1280x1024:   apple_mask = 0x10; break;
441 +        case APPLE_1600x1200:   apple_mask = 0x20; break;
442 +        default:                                apple_mask = 0x00; break;
443 +        }
444 +        return (test_modes & apple_mask);
445 + #endif
446 +        return true;
447 + }
448 +
449 + // Add mode to list of supported modes
450 + static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
451 + {
452 +        // Filter out unsupported modes
453 +        if (!has_mode(type, width, height))
454                  return;
455  
456 +        // Fill in VideoMode entry
457 +        VIDEO_MODE mode;
458 + #ifdef SHEEPSHAVER
459          // Recalculate dimensions to fit Apple modes
460          resolution_id = match_apple_resolution(width, height);
461          mode.viType = type;
# Line 359 | Line 464 | static void add_mode(int type, int width
464          VIDEO_MODE_Y = height;
465          VIDEO_MODE_RESOLUTION = resolution_id;
466          VIDEO_MODE_ROW_BYTES = bytes_per_row;
467 <        VIDEO_MODE_DEPTH = depth;
467 >        VIDEO_MODE_DEPTH = (video_depth)depth;
468          VideoModes.push_back(mode);
469   }
470  
# Line 415 | Line 520 | static void set_window_name(int name)
520   // Set mouse grab mode
521   static SDL_GrabMode set_grab_mode(SDL_GrabMode mode)
522   {
523 <        const SDL_VideoInfo *vi =SDL_GetVideoInfo();
523 >        const SDL_VideoInfo *vi = SDL_GetVideoInfo();
524          return (vi && vi->wm_available ? SDL_WM_GrabInput(mode) : SDL_GRAB_OFF);
525   }
526  
# Line 474 | Line 579 | private:
579          int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode)
580   };
581  
582 + class driver_fullscreen : public driver_base {
583 + public:
584 +        driver_fullscreen(SDL_monitor_desc &monitor);
585 +        ~driver_fullscreen();
586 + };
587 +
588   static driver_base *drv = NULL; // Pointer to currently used driver object
589  
590   #ifdef ENABLE_VOSF
# Line 495 | Line 606 | driver_base::~driver_base()
606          if (s)
607                  SDL_FreeSurface(s);
608  
609 +        // the_buffer shall always be mapped through vm_acquire_framebuffer()
610 +        if (the_buffer != VM_MAP_FAILED) {
611 +                D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
612 +                vm_release_framebuffer(the_buffer, the_buffer_size);
613 +                the_buffer = NULL;
614 +        }
615 +
616          // Free frame buffer(s)
617          if (!use_vosf) {
500                if (the_buffer) {
501                        free(the_buffer);
502                        the_buffer = NULL;
503                }
618                  if (the_buffer_copy) {
619                          free(the_buffer_copy);
620                          the_buffer_copy = NULL;
# Line 508 | Line 622 | driver_base::~driver_base()
622          }
623   #ifdef ENABLE_VOSF
624          else {
511                // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
512                if (the_buffer != VM_MAP_FAILED) {
513                        D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
514                        vm_release(the_buffer, the_buffer_size);
515                        the_buffer = NULL;
516                }
625                  if (the_host_buffer) {
626                          D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
627                          free(the_host_buffer);
# Line 524 | Line 632 | driver_base::~driver_base()
632                          free(the_buffer_copy);
633                          the_buffer_copy = NULL;
634                  }
635 +
636 +                // Deinitialize VOSF
637 +                video_vosf_exit();
638          }
639   #endif
640   }
# Line 533 | Line 644 | void driver_base::update_palette(void)
644   {
645          const VIDEO_MODE &mode = monitor.get_current_mode();
646  
647 <        if (VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT)
647 >        if ((int)VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT)
648                  SDL_SetPalette(s, SDL_PHYSPAL, sdl_palette, 0, 256);
649   }
650  
# Line 564 | Line 675 | driver_window::driver_window(SDL_monitor
675          ADBSetRelMouseMode(mouse_grabbed);
676  
677          // Create surface
678 <        int depth = (VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT ? 8 : screen_depth);
678 >        int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
679          if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL)
680                  return;
681  
# Line 573 | Line 684 | driver_window::driver_window(SDL_monitor
684          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
685          the_host_buffer = (uint8 *)s->pixels;
686          the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
687 <        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
687 >        the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
688          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
689          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
690 +
691 +        // Check whether we can initialize the VOSF subsystem and it's profitable
692 +        if (!video_vosf_init(m)) {
693 +                WarningAlert(STR_VOSF_INIT_ERR);
694 +                use_vosf = false;
695 +        }
696 +        else if (!video_vosf_profitable()) {
697 +                video_vosf_exit();
698 +                printf("VOSF acceleration is not profitable on this platform, disabling it\n");
699 +                use_vosf = false;
700 +        }
701 +        if (!use_vosf) {
702 +                free(the_buffer_copy);
703 +                vm_release(the_buffer, the_buffer_size);
704 +                the_host_buffer = NULL;
705 +        }
706 + #endif
707 +        if (!use_vosf) {
708 +                // Allocate memory for frame buffer
709 +                the_buffer_size = (aligned_height + 2) * s->pitch;
710 +                the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
711 +                the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
712 +                D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
713 +        }
714 +        
715 + #ifdef SHEEPSHAVER
716 +        // Create cursor
717 +        if ((sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, 0, 0)) != NULL) {
718 +                SDL_SetCursor(sdl_cursor);
719 +                cursor_changed = false;
720 +        }
721   #else
722 <        // Allocate memory for frame buffer
723 <        the_buffer_size = (aligned_height + 2) * s->pitch;
582 <        the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
583 <        the_buffer = (uint8 *)calloc(1, the_buffer_size);
584 <        D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
722 >        // Hide cursor
723 >        SDL_ShowCursor(0);
724   #endif
725  
726          // Set window name/class
727          set_window_name(STR_WINDOW_TITLE);
728  
590        // Hide cursor
591        SDL_ShowCursor(0);
592
729          // Init blitting routines
730          SDL_PixelFormat *f = s->format;
731          VisualFormat visualFormat;
# Line 597 | Line 733 | driver_window::driver_window(SDL_monitor
733          visualFormat.Rmask = f->Rmask;
734          visualFormat.Gmask = f->Gmask;
735          visualFormat.Bmask = f->Bmask;
736 <        Screen_blitter_init(visualFormat, true, sdl_depth_of_video_depth(VIDEO_MODE_DEPTH));
736 >        Screen_blitter_init(visualFormat, true, mac_depth_of_video_depth(VIDEO_MODE_DEPTH));
737  
738          // Load gray ramp to 8->16/32 expand map
739          if (!IsDirectMode(mode))
# Line 664 | Line 800 | void driver_window::mouse_moved(int x, i
800          ADBMouseMoved(x, y);
801   }
802  
803 +
804 + /*
805 + *  Full-screen display driver
806 + */
807 +
808 + // Open display
809 + driver_fullscreen::driver_fullscreen(SDL_monitor_desc &m)
810 +        : driver_base(m)
811 + {
812 +        int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
813 +        int aligned_width = (width + 15) & ~15;
814 +        int aligned_height = (height + 15) & ~15;
815 +
816 +        // Set absolute mouse mode
817 +        ADBSetRelMouseMode(false);
818 +
819 +        // Create surface
820 +        int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
821 +        if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE | SDL_FULLSCREEN)) == NULL)
822 +                return;
823 +
824 + #ifdef ENABLE_VOSF
825 +        use_vosf = true;
826 +        // Allocate memory for frame buffer (SIZE is extended to page-boundary)
827 +        the_host_buffer = (uint8 *)s->pixels;
828 +        the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
829 +        the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
830 +        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
831 +        D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
832 +
833 +        // Check whether we can initialize the VOSF subsystem and it's profitable
834 +        if (!video_vosf_init(m)) {
835 +                WarningAlert(STR_VOSF_INIT_ERR);
836 +                use_vosf = false;
837 +        }
838 +        else if (!video_vosf_profitable()) {
839 +                video_vosf_exit();
840 +                printf("VOSF acceleration is not profitable on this platform, disabling it\n");
841 +                use_vosf = false;
842 +        }
843 +        if (!use_vosf) {
844 +                free(the_buffer_copy);
845 +                vm_release(the_buffer, the_buffer_size);
846 +                the_host_buffer = NULL;
847 +        }
848 + #endif
849 +        if (!use_vosf) {
850 +                // Allocate memory for frame buffer
851 +                the_buffer_size = (aligned_height + 2) * s->pitch;
852 +                the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
853 +                the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
854 +                D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
855 +        }
856 +        
857 +        // Hide cursor
858 +        SDL_ShowCursor(0);
859 +
860 +        // Init blitting routines
861 +        SDL_PixelFormat *f = s->format;
862 +        VisualFormat visualFormat;
863 +        visualFormat.depth = depth;
864 +        visualFormat.Rmask = f->Rmask;
865 +        visualFormat.Gmask = f->Gmask;
866 +        visualFormat.Bmask = f->Bmask;
867 +        Screen_blitter_init(visualFormat, true, mac_depth_of_video_depth(VIDEO_MODE_DEPTH));
868 +
869 +        // Load gray ramp to 8->16/32 expand map
870 +        if (!IsDirectMode(mode))
871 +                for (int i=0; i<256; i++)
872 +                        ExpandMap[i] = SDL_MapRGB(f, i, i, i);
873 +
874 +        // Set frame buffer base
875 +        set_mac_frame_buffer(monitor, VIDEO_MODE_DEPTH, true);
876 +
877 +        // Everything went well
878 +        init_ok = true;
879 + }
880 +
881 + // Close display
882 + driver_fullscreen::~driver_fullscreen()
883 + {
884 + #ifdef ENABLE_VOSF
885 +        if (use_vosf)
886 +                the_host_buffer = NULL; // don't free() in driver_base dtor
887 + #endif
888 +        if (s)
889 +                SDL_FreeSurface(s);
890 +
891 +        // Show cursor
892 +        SDL_ShowCursor(1);
893 + }
894 +
895 +
896   /*
897   *  Initialization
898   */
# Line 695 | Line 924 | static void keycode_init(void)
924                  SDL_VideoDriverName(video_driver, sizeof(video_driver));
925                  bool video_driver_found = false;
926                  char line[256];
927 +                int n_keys = 0;
928                  while (fgets(line, sizeof(line) - 1, f)) {
929                          // Read line
930                          int len = strlen(line);
# Line 707 | Line 937 | static void keycode_init(void)
937                                  continue;
938  
939                          if (video_driver_found) {
940 <                                // Skip aliases
941 <                                static const char alias_str[] = "alias";
942 <                                if (strncmp(line, alias_str, sizeof(alias_str) - 1) == 0)
940 >                                // Skip aliases as long as we have read keycodes yet
941 >                                // Otherwise, it's another mapping and we have to stop
942 >                                static const char sdl_str[] = "sdl";
943 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0 && n_keys == 0)
944                                          continue;
945  
946                                  // Read keycode
947                                  int x_code, mac_code;
948                                  if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
949 <                                        keycode_table[x_code & 0xff] = mac_code;
949 >                                        keycode_table[x_code & 0xff] = mac_code, n_keys++;
950                                  else
951                                          break;
952                          } else {
953                                  // Search for SDL video driver string
954 <                                static const char alias_sdl_str[] = "alias SDL";
955 <                                if (strncmp(line, alias_sdl_str, sizeof(alias_sdl_str) - 1) == 0) {
956 <                                        char *p = line + sizeof(alias_sdl_str);
954 >                                static const char sdl_str[] = "sdl";
955 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0) {
956 >                                        char *p = line + sizeof(sdl_str);
957                                          if (strstr(video_driver, p) == video_driver)
958                                                  video_driver_found = true;
959                                  }
# Line 740 | Line 971 | static void keycode_init(void)
971                          WarningAlert(str);
972                          return;
973                  }
974 +
975 +                D(bug("Using SDL/%s keycodes table, %d key mappings\n", video_driver, n_keys));
976          }
977   }
978  
# Line 748 | Line 981 | bool SDL_monitor_desc::video_open(void)
981   {
982          D(bug("video_open()\n"));
983          const VIDEO_MODE &mode = get_current_mode();
984 + #if DEBUG
985 +        D(bug("Current video mode:\n"));
986 +        D(bug(" %dx%d (ID %02x), %d bpp\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << (VIDEO_MODE_DEPTH & 0x0f)));
987 + #endif
988  
989          // Create display driver object of requested type
990          switch (display_type) {
991          case DISPLAY_WINDOW:
992                  drv = new(std::nothrow) driver_window(*this);
993                  break;
994 +        case DISPLAY_SCREEN:
995 +                drv = new(std::nothrow) driver_fullscreen(*this);
996 +                break;
997          }
998          if (drv == NULL)
999                  return false;
# Line 763 | Line 1003 | bool SDL_monitor_desc::video_open(void)
1003                  return false;
1004          }
1005  
766 #ifdef ENABLE_VOSF
767        if (use_vosf) {
768                // Initialize the VOSF system
769                if (!video_vosf_init(*this)) {
770                        ErrorAlert(STR_VOSF_INIT_ERR);
771                return false;
772                }
773        }
774 #endif
775        
1006          // Initialize VideoRefresh function
1007          VideoRefreshInit();
1008  
# Line 780 | Line 1010 | bool SDL_monitor_desc::video_open(void)
1010          LOCK_FRAME_BUFFER;
1011  
1012          // Start redraw/input thread
1013 + #ifndef USE_CPU_EMUL_SERVICES
1014          redraw_thread_cancel = false;
1015          redraw_thread_active = ((redraw_thread = SDL_CreateThread(redraw_func, NULL)) != NULL);
1016          if (!redraw_thread_active) {
1017                  printf("FATAL: cannot create redraw thread\n");
1018                  return false;
1019          }
1020 + #else
1021 +        redraw_thread_active = true;
1022 + #endif
1023          return true;
1024   }
1025  
# Line 820 | Line 1054 | bool VideoInit(bool classic)
1054          mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1055  
1056          // Get screen mode from preferences
1057 <        const char *mode_str;
1057 >        const char *mode_str = NULL;
1058          if (classic_mode)
1059                  mode_str = "win/512/342";
1060          else
# Line 840 | Line 1074 | bool VideoInit(bool classic)
1074          if (mode_str) {
1075                  if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
1076                          display_type = DISPLAY_WINDOW;
1077 +                else if (sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1078 +                        display_type = DISPLAY_SCREEN;
1079          }
1080          int max_width = 640, max_height = 480;
1081 <        if (display_type == DISPLAY_SCREEN) {
1082 <                SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
1083 <                if (modes && modes != (SDL_Rect **)-1) {
1084 <                        max_width = modes[0]->w;
1085 <                        max_height = modes[0]->h;
1081 >        SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
1082 >        if (modes && modes != (SDL_Rect **)-1) {
1083 >                // It turns out that on some implementations, and contrary to the documentation,
1084 >                // the returned list is not sorted from largest to smallest (e.g. Windows)
1085 >                for (int i = 0; modes[i] != NULL; i++) {
1086 >                        const int w = modes[i]->w;
1087 >                        const int h = modes[i]->h;
1088 >                        if (w > max_width && h > max_height) {
1089 >                                max_width = w;
1090 >                                max_height = h;
1091 >                        }
1092                  }
1093 +                if (default_width > max_width)
1094 +                        default_width = max_width;
1095 +                if (default_height > max_height)
1096 +                        default_height = max_height;
1097          }
1098          if (default_width <= 0)
1099                  default_width = max_width;
# Line 878 | Line 1124 | bool VideoInit(bool classic)
1124                          add_mode(display_type, 512, 342, 0x80, 64, VIDEO_DEPTH_1BIT);
1125                  else {
1126                          for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1127 <                                int bpp = (d <= VIDEO_DEPTH_8BIT ? 8 : sdl_depth_of_video_depth(d));
1127 >                                int bpp = sdl_depth_of_video_depth(d);
1128                                  if (SDL_VideoModeOK(max_width, max_height, bpp, SDL_HWSURFACE))
1129                                          add_window_modes(video_depth(d));
1130                          }
1131                  }
1132 <        } else
1133 <                add_mode(display_type, default_width, default_height, 0x80, TrivialBytesPerRow(default_width, (video_depth)default_depth), default_depth);
1132 >        } else if (display_type == DISPLAY_SCREEN) {
1133 >                struct {
1134 >                        int w;
1135 >                        int h;
1136 >                        int resolution_id;
1137 >                }
1138 >                video_modes[] = {
1139 >                        {   -1,   -1, 0x80 },
1140 >                        {  640,  480, 0x81 },
1141 >                        {  800,  600, 0x82 },
1142 >                        { 1024,  768, 0x83 },
1143 >                        { 1152,  870, 0x84 },
1144 >                        { 1280, 1024, 0x85 },
1145 >                        { 1600, 1200, 0x86 },
1146 >                        { 0, }
1147 >                };
1148 >                video_modes[0].w = default_width;
1149 >                video_modes[0].h = default_height;
1150 >
1151 >                for (int i = 0; video_modes[i].w != 0; i++) {
1152 >                        const int w = video_modes[i].w;
1153 >                        const int h = video_modes[i].h;
1154 >                        if (i > 0 && (w >= default_width || h >= default_height))
1155 >                                continue;
1156 > #ifdef ENABLE_VOSF
1157 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1158 >                                int bpp = sdl_depth_of_video_depth(d);
1159 >                                if (SDL_VideoModeOK(w, h, bpp, SDL_HWSURFACE | SDL_FULLSCREEN))
1160 >                                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)d), d);
1161 >                        }
1162 > #else
1163 >                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)default_depth), default_depth);
1164 > #endif
1165 >                }
1166 >        }
1167 >
1168          if (VideoModes.empty()) {
1169                  ErrorAlert(STR_NO_XVISUAL_ERR);
1170                  return false;
# Line 914 | Line 1194 | bool VideoInit(bool classic)
1194          }
1195  
1196   #ifdef SHEEPSHAVER
1197 <        for (int i = 0; i < VideoModes.size(); ++i)
1197 >        for (int i = 0; i < VideoModes.size(); i++)
1198                  VModes[i] = VideoModes[i];
1199 <
1200 <        const VIDEO_MODE & mode = VideoModes[cur_mode];
1201 <        D(bug("Current video mode\n"));
1202 <        D(bug(" %dx%d (ID %02x), %d bpp\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << (VIDEO_MODE_DEPTH - 0x80)));
1199 >        VideoInfo *p = &VModes[VideoModes.size()];
1200 >        p->viType = DIS_INVALID;        // End marker
1201 >        p->viRowBytes = 0;
1202 >        p->viXsize = p->viYsize = 0;
1203 >        p->viAppleMode = 0;
1204 >        p->viAppleID = 0;
1205   #endif
1206  
1207   #if DEBUG
# Line 954 | Line 1236 | void SDL_monitor_desc::video_close(void)
1236          D(bug("video_close()\n"));
1237  
1238          // Stop redraw thread
1239 + #ifndef USE_CPU_EMUL_SERVICES
1240          if (redraw_thread_active) {
1241                  redraw_thread_cancel = true;
1242                  SDL_WaitThread(redraw_thread, NULL);
1243          }
1244 + #endif
1245          redraw_thread_active = false;
1246  
1247          // Unlock frame buffer
1248          UNLOCK_FRAME_BUFFER;
1249          D(bug(" frame buffer unlocked\n"));
1250  
967 #ifdef ENABLE_VOSF
968        if (use_vosf) {
969                // Deinitialize VOSF
970                video_vosf_exit();
971        }
972 #endif
973
1251          // Close display
1252          delete drv;
1253          drv = NULL;
# Line 1068 | Line 1345 | void SDL_monitor_desc::set_palette(uint8
1345          const VIDEO_MODE &mode = get_current_mode();
1346  
1347          // FIXME: how can we handle the gamma ramp?
1348 <        if (VIDEO_MODE_DEPTH > VIDEO_DEPTH_8BIT)
1348 >        if ((int)VIDEO_MODE_DEPTH > VIDEO_DEPTH_8BIT)
1349                  return;
1350  
1351          LOCK_PALETTE;
# Line 1093 | Line 1370 | void SDL_monitor_desc::set_palette(uint8
1370                  }
1371  
1372   #ifdef ENABLE_VOSF
1373 <                // We have to redraw everything because the interpretation of pixel values changed
1374 <                LOCK_VOSF;
1375 <                PFLAG_SET_ALL;
1376 <                UNLOCK_VOSF;
1377 <                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1373 >                if (use_vosf) {
1374 >                        // We have to redraw everything because the interpretation of pixel values changed
1375 >                        LOCK_VOSF;
1376 >                        PFLAG_SET_ALL;
1377 >                        UNLOCK_VOSF;
1378 >                        memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1379 >                }
1380   #endif
1381          }
1382  
# Line 1127 | Line 1406 | int16 video_mode_change(VidLocals *csSav
1406                          csSave->saveData = ReadMacInt32(ParamPtr + csData);
1407                          csSave->savePage = ReadMacInt16(ParamPtr + csPage);
1408  
1409 <                        // Disable interrupts
1409 >                        // Disable interrupts and pause redraw thread
1410                          DisableInterrupt();
1411 +                        thread_stop_ack = false;
1412 +                        thread_stop_req = true;
1413 +                        while (!thread_stop_ack) ;
1414  
1415                          cur_mode = i;
1416                          monitor_desc *monitor = VideoMonitors[0];
# Line 1139 | Line 1421 | int16 video_mode_change(VidLocals *csSav
1421                          csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
1422                          csSave->saveMode=VModes[cur_mode].viAppleMode;
1423  
1424 <                        // Enable interrupts
1424 >                        // Enable interrupts and resume redraw thread
1425 >                        thread_stop_req = false;
1426                          EnableInterrupt();
1427                          return noErr;
1428                  }
# Line 1168 | Line 1451 | void SDL_monitor_desc::switch_to_current
1451   #ifdef SHEEPSHAVER
1452   bool video_can_change_cursor(void)
1453   {
1454 < //      return hw_mac_cursor_accl && (display_type != DISPLAY_SCREEN);
1172 <        return false;
1454 >        return (display_type == DISPLAY_WINDOW);
1455   }
1456   #endif
1457  
# Line 1181 | Line 1463 | bool video_can_change_cursor(void)
1463   #ifdef SHEEPSHAVER
1464   void video_set_cursor(void)
1465   {
1466 < //      cursor_changed = true;
1466 >        cursor_changed = true;
1467   }
1468   #endif
1469  
1470  
1471   /*
1472 < *  Install graphics acceleration
1472 > *  Keyboard-related utilify functions
1473   */
1474  
1475 < #ifdef SHEEPSHAVER
1194 < // Rectangle inversion
1195 < template< int bpp >
1196 < static inline void do_invrect(uint8 *dest, uint32 length)
1197 < {
1198 < #define INVERT_1(PTR, OFS) ((uint8  *)(PTR))[OFS] = ~((uint8  *)(PTR))[OFS]
1199 < #define INVERT_2(PTR, OFS) ((uint16 *)(PTR))[OFS] = ~((uint16 *)(PTR))[OFS]
1200 < #define INVERT_4(PTR, OFS) ((uint32 *)(PTR))[OFS] = ~((uint32 *)(PTR))[OFS]
1201 < #define INVERT_8(PTR, OFS) ((uint64 *)(PTR))[OFS] = ~((uint64 *)(PTR))[OFS]
1202 <
1203 < #ifndef UNALIGNED_PROFITABLE
1204 <        // Align on 16-bit boundaries
1205 <        if (bpp < 16 && (((uintptr)dest) & 1)) {
1206 <                INVERT_1(dest, 0);
1207 <                dest += 1; length -= 1;
1208 <        }
1209 <
1210 <        // Align on 32-bit boundaries
1211 <        if (bpp < 32 && (((uintptr)dest) & 2)) {
1212 <                INVERT_2(dest, 0);
1213 <                dest += 2; length -= 2;
1214 <        }
1215 < #endif
1216 <
1217 <        // Invert 8-byte words
1218 <        if (length >= 8) {
1219 <                const int r = (length / 8) % 8;
1220 <                dest += r * 8;
1221 <
1222 <                int n = ((length / 8) + 7) / 8;
1223 <                switch (r) {
1224 <                case 0: do {
1225 <                                dest += 64;
1226 <                                INVERT_8(dest, -8);
1227 <                case 7: INVERT_8(dest, -7);
1228 <                case 6: INVERT_8(dest, -6);
1229 <                case 5: INVERT_8(dest, -5);
1230 <                case 4: INVERT_8(dest, -4);
1231 <                case 3: INVERT_8(dest, -3);
1232 <                case 2: INVERT_8(dest, -2);
1233 <                case 1: INVERT_8(dest, -1);
1234 <                                } while (--n > 0);
1235 <                }
1236 <        }
1237 <
1238 <        // 32-bit cell to invert?
1239 <        if (length & 4) {
1240 <                INVERT_4(dest, 0);
1241 <                if (bpp <= 16)
1242 <                        dest += 4;
1243 <        }
1244 <
1245 <        // 16-bit cell to invert?
1246 <        if (bpp <= 16 && (length & 2)) {
1247 <                INVERT_2(dest, 0);
1248 <                if (bpp <= 8)
1249 <                        dest += 2;
1250 <        }
1251 <
1252 <        // 8-bit cell to invert?
1253 <        if (bpp <= 8 && (length & 1))
1254 <                INVERT_1(dest, 0);
1255 <
1256 < #undef INVERT_1
1257 < #undef INVERT_2
1258 < #undef INVERT_4
1259 < #undef INVERT_8
1260 < }
1261 <
1262 < void NQD_invrect(uint32 p)
1263 < {
1264 <        D(bug("accl_invrect %08x\n", p));
1265 <
1266 <        // Get inversion parameters
1267 <        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1268 <        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1269 <        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1270 <        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1271 <        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1272 <        D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1273 <
1274 <        //!!?? pen_mode == 14
1275 <
1276 <        // And perform the inversion
1277 <        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1278 <        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1279 <        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1280 <        width *= bpp;
1281 <        switch (bpp) {
1282 <        case 1:
1283 <                for (int i = 0; i < height; i++) {
1284 <                        do_invrect<8>(dest, width);
1285 <                        dest += dest_row_bytes;
1286 <                }
1287 <                break;
1288 <        case 2:
1289 <                for (int i = 0; i < height; i++) {
1290 <                        do_invrect<16>(dest, width);
1291 <                        dest += dest_row_bytes;
1292 <                }
1293 <                break;
1294 <        case 4:
1295 <                for (int i = 0; i < height; i++) {
1296 <                        do_invrect<32>(dest, width);
1297 <                        dest += dest_row_bytes;
1298 <                }
1299 <                break;
1300 <        }
1301 < }
1302 <
1303 < // Rectangle filling
1304 < template< int bpp >
1305 < static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1306 < {
1307 < #define FILL_1(PTR, OFS, VAL) ((uint8  *)(PTR))[OFS] = (VAL)
1308 < #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1309 < #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1310 < #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
1311 <
1312 < #ifndef UNALIGNED_PROFITABLE
1313 <        // Align on 16-bit boundaries
1314 <        if (bpp < 16 && (((uintptr)dest) & 1)) {
1315 <                FILL_1(dest, 0, color);
1316 <                dest += 1; length -= 1;
1317 <        }
1318 <
1319 <        // Align on 32-bit boundaries
1320 <        if (bpp < 32 && (((uintptr)dest) & 2)) {
1321 <                FILL_2(dest, 0, color);
1322 <                dest += 2; length -= 2;
1323 <        }
1324 < #endif
1325 <
1326 <        // Fill 8-byte words
1327 <        if (length >= 8) {
1328 <                const uint64 c = (((uint64)color) << 32) | color;
1329 <                const int r = (length / 8) % 8;
1330 <                dest += r * 8;
1331 <
1332 <                int n = ((length / 8) + 7) / 8;
1333 <                switch (r) {
1334 <                case 0: do {
1335 <                                dest += 64;
1336 <                                FILL_8(dest, -8, c);
1337 <                case 7: FILL_8(dest, -7, c);
1338 <                case 6: FILL_8(dest, -6, c);
1339 <                case 5: FILL_8(dest, -5, c);
1340 <                case 4: FILL_8(dest, -4, c);
1341 <                case 3: FILL_8(dest, -3, c);
1342 <                case 2: FILL_8(dest, -2, c);
1343 <                case 1: FILL_8(dest, -1, c);
1344 <                                } while (--n > 0);
1345 <                }
1346 <        }
1347 <
1348 <        // 32-bit cell to fill?
1349 <        if (length & 4) {
1350 <                FILL_4(dest, 0, color);
1351 <                if (bpp <= 16)
1352 <                        dest += 4;
1353 <        }
1354 <
1355 <        // 16-bit cell to fill?
1356 <        if (bpp <= 16 && (length & 2)) {
1357 <                FILL_2(dest, 0, color);
1358 <                if (bpp <= 8)
1359 <                        dest += 2;
1360 <        }
1361 <
1362 <        // 8-bit cell to fill?
1363 <        if (bpp <= 8 && (length & 1))
1364 <                FILL_1(dest, 0, color);
1365 <
1366 < #undef FILL_1
1367 < #undef FILL_2
1368 < #undef FILL_4
1369 < #undef FILL_8
1370 < }
1371 <
1372 < void NQD_fillrect(uint32 p)
1475 > static bool is_modifier_key(SDL_KeyboardEvent const & e)
1476   {
1477 <        D(bug("accl_fillrect %08x\n", p));
1478 <
1479 <        // Get filling parameters
1480 <        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1481 <        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1482 <        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1483 <        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1484 <        uint32 color = htonl(ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen));
1485 <        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1486 <        D(bug(" width %d, height %d\n", width, height));
1487 <        D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1488 <
1489 <        // And perform the fill
1490 <        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1491 <        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1492 <        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1390 <        width *= bpp;
1391 <        switch (bpp) {
1392 <        case 1:
1393 <                for (int i = 0; i < height; i++) {
1394 <                        memset(dest, color, width);
1395 <                        dest += dest_row_bytes;
1396 <                }
1397 <                break;
1398 <        case 2:
1399 <                for (int i = 0; i < height; i++) {
1400 <                        do_fillrect<16>(dest, color, width);
1401 <                        dest += dest_row_bytes;
1402 <                }
1403 <                break;
1404 <        case 4:
1405 <                for (int i = 0; i < height; i++) {
1406 <                        do_fillrect<32>(dest, color, width);
1407 <                        dest += dest_row_bytes;
1408 <                }
1409 <                break;
1410 <        }
1411 < }
1412 <
1413 < bool NQD_fillrect_hook(uint32 p)
1414 < {
1415 <        D(bug("accl_fillrect_hook %08x\n", p));
1416 <
1417 <        // Check if we can accelerate this fillrect
1418 <        if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1419 <                const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1420 <                if (transfer_mode == 8) {
1421 <                        // Fill
1422 <                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1423 <                        return true;
1424 <                }
1425 <                else if (transfer_mode == 10) {
1426 <                        // Invert
1427 <                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1428 <                        return true;
1429 <                }
1430 <        }
1431 <        return false;
1432 < }
1433 <
1434 < // Rectangle blitting
1435 < // TODO: optimize for VOSF and target pixmap == screen
1436 < void NQD_bitblt(uint32 p)
1437 < {
1438 <        D(bug("accl_bitblt %08x\n", p));
1439 <
1440 <        // Get blitting parameters
1441 <        int16 src_X  = (int16)ReadMacInt16(p + acclSrcRect + 2) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 2);
1442 <        int16 src_Y  = (int16)ReadMacInt16(p + acclSrcRect + 0) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 0);
1443 <        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1444 <        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1445 <        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1446 <        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1447 <        D(bug(" src addr %08x, dest addr %08x\n", ReadMacInt32(p + acclSrcBaseAddr), ReadMacInt32(p + acclDestBaseAddr)));
1448 <        D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1449 <        D(bug(" width %d, height %d\n", width, height));
1450 <
1451 <        // And perform the blit
1452 <        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1453 <        width *= bpp;
1454 <        if ((int32)ReadMacInt32(p + acclSrcRowBytes) > 0) {
1455 <                const int src_row_bytes = (int32)ReadMacInt32(p + acclSrcRowBytes);
1456 <                const int dst_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1457 <                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + (src_Y * src_row_bytes) + (src_X * bpp));
1458 <                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dst_row_bytes) + (dest_X * bpp));
1459 <                for (int i = 0; i < height; i++) {
1460 <                        memmove(dst, src, width);
1461 <                        src += src_row_bytes;
1462 <                        dst += dst_row_bytes;
1463 <                }
1464 <        }
1465 <        else {
1466 <                const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1467 <                const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1468 <                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp));
1469 <                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp));
1470 <                for (int i = height - 1; i >= 0; i--) {
1471 <                        memmove(dst, src, width);
1472 <                        src -= src_row_bytes;
1473 <                        dst -= dst_row_bytes;
1474 <                }
1475 <        }
1476 < }
1477 <
1478 < /*
1479 <  BitBlt transfer modes:
1480 <  0 : srcCopy
1481 <  1 : srcOr
1482 <  2 : srcXor
1483 <  3 : srcBic
1484 <  4 : notSrcCopy
1485 <  5 : notSrcOr
1486 <  6 : notSrcXor
1487 <  7 : notSrcBic
1488 <  32 : blend
1489 <  33 : addPin
1490 <  34 : addOver
1491 <  35 : subPin
1492 <  36 : transparent
1493 <  37 : adMax
1494 <  38 : subOver
1495 <  39 : adMin
1496 <  50 : hilite
1497 < */
1498 <
1499 < bool NQD_bitblt_hook(uint32 p)
1500 < {
1501 <        D(bug("accl_draw_hook %08x\n", p));
1502 <
1503 <        // Check if we can accelerate this bitblt
1504 <        if (ReadMacInt32(p + 0x018) + ReadMacInt32(p + 0x128) == 0 &&
1505 <                ReadMacInt32(p + 0x130) == 0 &&
1506 <                ReadMacInt32(p + acclSrcPixelSize) >= 8 &&
1507 <                ReadMacInt32(p + acclSrcPixelSize) == ReadMacInt32(p + acclDestPixelSize) &&
1508 <                (ReadMacInt32(p + acclSrcRowBytes) ^ ReadMacInt32(p + acclDestRowBytes)) >= 0 && // same sign?
1509 <                ReadMacInt32(p + acclTransferMode) == 0 &&                                                                               // srcCopy?
1510 <                ReadMacInt32(p + 0x15c) > 0) {
1511 <
1512 <                // Yes, set function pointer
1513 <                WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1477 >        switch (e.keysym.sym) {
1478 >        case SDLK_NUMLOCK:
1479 >        case SDLK_CAPSLOCK:
1480 >        case SDLK_SCROLLOCK:
1481 >        case SDLK_RSHIFT:
1482 >        case SDLK_LSHIFT:
1483 >        case SDLK_RCTRL:
1484 >        case SDLK_LCTRL:
1485 >        case SDLK_RALT:
1486 >        case SDLK_LALT:
1487 >        case SDLK_RMETA:
1488 >        case SDLK_LMETA:
1489 >        case SDLK_LSUPER:
1490 >        case SDLK_RSUPER:
1491 >        case SDLK_MODE:
1492 >        case SDLK_COMPOSE:
1493                  return true;
1494          }
1495          return false;
1496   }
1497  
1498 < // Wait for graphics operation to finish
1520 < bool NQD_sync_hook(uint32 arg)
1521 < {
1522 <        D(bug("accl_sync_hook %08x\n", arg));
1523 <        return true;
1524 < }
1525 <
1526 < void VideoInstallAccel(void)
1498 > static bool is_ctrl_down(SDL_keysym const & ks)
1499   {
1500 <        // Install acceleration hooks
1529 <        if (PrefsFindBool("gfxaccel")) {
1530 <                D(bug("Video: Installing acceleration hooks\n"));
1531 <                uint32 base;
1532 <
1533 <                SheepVar bitblt_hook_info(sizeof(accl_hook_info));
1534 <                base = bitblt_hook_info.addr();
1535 <                WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK));
1536 <                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1537 <                WriteMacInt32(base + 8, ACCL_BITBLT);
1538 <                NQDMisc(6, bitblt_hook_info.ptr());
1539 <
1540 <                SheepVar fillrect_hook_info(sizeof(accl_hook_info));
1541 <                base = fillrect_hook_info.addr();
1542 <                WriteMacInt32(base + 0, NativeTVECT(NATIVE_FILLRECT_HOOK));
1543 <                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1544 <                WriteMacInt32(base + 8, ACCL_FILLRECT);
1545 <                NQDMisc(6, fillrect_hook_info.ptr());
1546 <        }
1500 >        return ctrl_down || (ks.mod & KMOD_CTRL);
1501   }
1548 #endif
1502  
1503  
1504   /*
# Line 1553 | Line 1506 | void VideoInstallAccel(void)
1506   *  and -2 if the key was recognized as a hotkey
1507   */
1508  
1556 static bool is_ctrl_down(SDL_keysym const & ks)
1557 {
1558        return ctrl_down || (ks.mod & KMOD_CTRL);
1559 }
1560
1509   static int kc_decode(SDL_keysym const & ks, bool key_down)
1510   {
1511          switch (ks.sym) {
# Line 1590 | Line 1538 | static int kc_decode(SDL_keysym const &
1538  
1539          case SDLK_1: case SDLK_EXCLAIM: return 0x12;
1540          case SDLK_2: case SDLK_AT: return 0x13;
1541 < //      case SDLK_3: case SDLK_numbersign: return 0x14;
1541 >        case SDLK_3: case SDLK_HASH: return 0x14;
1542          case SDLK_4: case SDLK_DOLLAR: return 0x15;
1543 < //      case SDLK_5: case SDLK_percent: return 0x17;
1543 >        case SDLK_5: return 0x17;
1544          case SDLK_6: return 0x16;
1545          case SDLK_7: return 0x1a;
1546          case SDLK_8: return 0x1c;
1547          case SDLK_9: return 0x19;
1548          case SDLK_0: return 0x1d;
1549  
1550 < //      case SDLK_BACKQUOTE: case SDLK_asciitilde: return 0x0a;
1550 >        case SDLK_BACKQUOTE: return 0x0a;
1551          case SDLK_MINUS: case SDLK_UNDERSCORE: return 0x1b;
1552          case SDLK_EQUALS: case SDLK_PLUS: return 0x18;
1553 < //      case SDLK_bracketleft: case SDLK_braceleft: return 0x21;
1554 < //      case SDLK_bracketright: case SDLK_braceright: return 0x1e;
1555 < //      case SDLK_BACKSLASH: case SDLK_bar: return 0x2a;
1553 >        case SDLK_LEFTBRACKET: return 0x21;
1554 >        case SDLK_RIGHTBRACKET: return 0x1e;
1555 >        case SDLK_BACKSLASH: return 0x2a;
1556          case SDLK_SEMICOLON: case SDLK_COLON: return 0x29;
1557 < //      case SDLK_apostrophe: case SDLK_QUOTEDBL: return 0x27;
1557 >        case SDLK_QUOTE: case SDLK_QUOTEDBL: return 0x27;
1558          case SDLK_COMMA: case SDLK_LESS: return 0x2b;
1559          case SDLK_PERIOD: case SDLK_GREATER: return 0x2f;
1560          case SDLK_SLASH: case SDLK_QUESTION: return 0x2c;
# Line 1627 | Line 1575 | static int kc_decode(SDL_keysym const &
1575          case SDLK_RCTRL: return 0x36;
1576          case SDLK_LSHIFT: return 0x38;
1577          case SDLK_RSHIFT: return 0x38;
1578 + #if (defined(__APPLE__) && defined(__MACH__))
1579 +        case SDLK_LALT: return 0x3a;
1580 +        case SDLK_RALT: return 0x3a;
1581 +        case SDLK_LMETA: return 0x37;
1582 +        case SDLK_RMETA: return 0x37;
1583 + #else
1584          case SDLK_LALT: return 0x37;
1585          case SDLK_RALT: return 0x37;
1586          case SDLK_LMETA: return 0x3a;
1587          case SDLK_RMETA: return 0x3a;
1588 + #endif
1589 +        case SDLK_LSUPER: return 0x3a; // "Windows" key
1590 +        case SDLK_RSUPER: return 0x3a;
1591          case SDLK_MENU: return 0x32;
1592          case SDLK_CAPSLOCK: return 0x39;
1593          case SDLK_NUMLOCK: return 0x47;
# Line 1737 | Line 1694 | static void handle_events(void)
1694                          // Keyboard
1695                          case SDL_KEYDOWN: {
1696                                  int code = -1;
1697 <                                if (use_keycodes) {
1697 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1698                                          if (event2keycode(event.key, true) != -2)       // This is called to process the hotkeys
1699                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1700                                  } else
# Line 1765 | Line 1722 | static void handle_events(void)
1722                          }
1723                          case SDL_KEYUP: {
1724                                  int code = -1;
1725 <                                if (use_keycodes) {
1725 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1726                                          if (event2keycode(event.key, false) != -2)      // This is called to process the hotkeys
1727                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1728                                  } else
1729                                          code = event2keycode(event.key, false);
1730 <                                if (code >= 0 && code != 0x39) {        // Don't propagate Caps Lock releases
1731 <                                        ADBKeyUp(code);
1730 >                                if (code >= 0) {
1731 >                                        if (code == 0x39) {     // Caps Lock released
1732 >                                                if (caps_on) {
1733 >                                                        ADBKeyUp(code);
1734 >                                                        caps_on = false;
1735 >                                                } else {
1736 >                                                        ADBKeyDown(code);
1737 >                                                        caps_on = true;
1738 >                                                }
1739 >                                        } else
1740 >                                                ADBKeyUp(code);
1741                                          if (code == 0x36)
1742                                                  ctrl_down = false;
1743                                  }
# Line 1838 | Line 1804 | static void update_display_static(driver
1804  
1805          // Check for first column from left and first column from right that have changed
1806          if (high) {
1807 <                if (VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
1807 >                if ((int)VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
1808                          const int src_bytes_per_row = bytes_per_row;
1809                          const int dst_bytes_per_row = drv->s->pitch;
1810                          const int pixels_per_byte = VIDEO_MODE_X / src_bytes_per_row;
# Line 2015 | Line 1981 | static void video_refresh_dga_vosf(void)
1981                  tick_counter = 0;
1982                  if (mainBuffer.dirty) {
1983                          LOCK_VOSF;
1984 <                        update_display_dga_vosf();
1984 >                        update_display_dga_vosf(static_cast<driver_fullscreen *>(drv));
1985                          UNLOCK_VOSF;
1986                  }
1987          }
# Line 2079 | Line 2045 | static void VideoRefreshInit(void)
2045          }
2046   }
2047  
2048 + static inline void do_video_refresh(void)
2049 + {
2050 +        // Handle SDL events
2051 +        handle_events();
2052 +
2053 +        // Update display
2054 +        video_refresh();
2055 +
2056 + #ifdef SHEEPSHAVER
2057 +        // Set new cursor image if it was changed
2058 +        if (cursor_changed && sdl_cursor) {
2059 +                cursor_changed = false;
2060 +                SDL_FreeCursor(sdl_cursor);
2061 +                sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
2062 +                if (sdl_cursor)
2063 +                        SDL_SetCursor(sdl_cursor);
2064 +        }
2065 + #endif
2066 +
2067 +        // Set new palette if it was changed
2068 +        handle_palette_changes();
2069 + }
2070 +
2071 + // This function is called on non-threaded platforms from a timer interrupt
2072 + void VideoRefresh(void)
2073 + {
2074 +        // We need to check redraw_thread_active to inhibit refreshed during
2075 +        // mode changes on non-threaded platforms
2076 +        if (!redraw_thread_active)
2077 +                return;
2078 +
2079 +        // Process pending events and update display
2080 +        do_video_refresh();
2081 + }
2082 +
2083   const int VIDEO_REFRESH_HZ = 60;
2084   const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2085  
2086 + #ifndef USE_CPU_EMUL_SERVICES
2087   static int redraw_func(void *arg)
2088   {
2089          uint64 start = GetTicks_usec();
# Line 2099 | Line 2101 | static int redraw_func(void *arg)
2101                          next = GetTicks_usec();
2102                  ticks++;
2103  
2104 <                // Handle SDL events
2105 <                handle_events();
2106 <
2107 <                // Refresh display
2108 <                video_refresh();
2104 > #ifdef SHEEPSHAVER
2105 >                // Pause if requested (during video mode switches)
2106 >                if (thread_stop_req) {
2107 >                        thread_stop_ack = true;
2108 >                        continue;
2109 >                }
2110 > #endif
2111  
2112 <                // Set new palette if it was changed
2113 <                handle_palette_changes();
2112 >                // Process pending events and update display
2113 >                do_video_refresh();
2114          }
2115  
2116          uint64 end = GetTicks_usec();
2117          D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
2118          return 0;
2119   }
2120 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines