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

Comparing BasiliskII/src/SDL/video_sdl.cpp (file contents):
Revision 1.1 by gbeauche, 2004-06-23T13:47:20Z vs.
Revision 1.17 by gbeauche, 2005-03-19T05:34:15Z

# 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 32 | Line 32
32   *  - Force relative mode in Grab mode even if SDL provides absolute coordinates?
33   *  - Fullscreen mode
34   *  - Gamma tables support is likely to be broken here
35 + *  - Events processing is bound to the general emulation thread as SDL requires
36 + *    to PumpEvents() within the same thread as the one that called SetVideoMode().
37 + *    Besides, there can't seem to be a way to call SetVideoMode() from a child thread.
38 + *  - Refresh performance is still slow. Use SDL_CreateRGBSurface()?
39 + *  - Backport hw cursor acceleration to Basilisk II?
40   */
41  
42   #include "sysdeps.h"
# Line 40 | Line 45
45   #include <SDL_mutex.h>
46   #include <SDL_thread.h>
47   #include <errno.h>
48 + #include <vector>
49  
50   #include "cpu_emulation.h"
51   #include "main.h"
# Line 48 | Line 54
54   #include "prefs.h"
55   #include "user_strings.h"
56   #include "video.h"
57 + #include "video_defs.h"
58   #include "video_blit.h"
59 + #include "vm_alloc.h"
60  
61   #define DEBUG 0
62   #include "debug.h"
63  
64  
65   // Supported video modes
66 < static vector<video_mode> VideoModes;
66 > using std::vector;
67 > static vector<VIDEO_MODE> VideoModes;
68  
69   // Display types
70 + #ifdef SHEEPSHAVER
71   enum {
72 <        DISPLAY_WINDOW, // windowed display
73 <        DISPLAY_SCREEN  // fullscreen display
72 >        DISPLAY_WINDOW = DIS_WINDOW,                                    // windowed display
73 >        DISPLAY_SCREEN = DIS_SCREEN                                             // fullscreen display
74   };
75 + extern int display_type;                                                        // See enum above
76 + #else
77 + enum {
78 +        DISPLAY_WINDOW,                                                                 // windowed display
79 +        DISPLAY_SCREEN                                                                  // fullscreen display
80 + };
81 + static int display_type = DISPLAY_WINDOW;                       // See enum above
82 + #endif
83  
84   // Constants
85 + #ifdef WIN32
86 + const char KEYCODE_FILE_NAME[] = "BasiliskII_keycodes";
87 + #else
88   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
89 + #endif
90  
91  
92   // Global variables
# Line 72 | Line 94 | static int32 frame_skip;                                                       // Prefs
94   static int16 mouse_wheel_mode;
95   static int16 mouse_wheel_lines;
96  
75 static int display_type = DISPLAY_WINDOW;                       // See enum above
97   static uint8 *the_buffer = NULL;                                        // Mac frame buffer (where MacOS draws into)
98   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer (for refreshed modes)
99   static uint32 the_buffer_size;                                          // Size of allocated the_buffer
# Line 80 | Line 101 | static uint32 the_buffer_size;                                         // S
101   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
102   static volatile bool redraw_thread_cancel;                      // Flag: Cancel Redraw thread
103   static SDL_Thread *redraw_thread = NULL;                        // Redraw thread
104 + static volatile bool thread_stop_req = false;
105 + static volatile bool thread_stop_ack = false;           // Acknowledge for thread_stop_req
106  
107   #ifdef ENABLE_VOSF
108 < static bool use_vosf = true;                                            // Flag: VOSF enabled
108 > static bool use_vosf = false;                                           // Flag: VOSF enabled
109   #else
110   static const bool use_vosf = false;                                     // VOSF not possible
111   #endif
# Line 100 | Line 123 | static int keycode_table[256];                                         // X
123  
124   // SDL variables
125   static int screen_depth;                                                        // Depth of current screen
126 + static SDL_Cursor *sdl_cursor;                                          // Copy of Mac cursor
127 + static volatile bool cursor_changed = false;            // Flag: cursor changed, redraw_func must update the cursor
128   static SDL_Color sdl_palette[256];                                      // Color palette to be used as CLUT and gamma table
129   static bool sdl_palette_changed = false;                        // Flag: Palette changed, redraw thread must set new colors
130 + static const int sdl_eventmask = SDL_MOUSEBUTTONDOWNMASK | SDL_MOUSEBUTTONUPMASK | SDL_MOUSEMOTIONMASK | SDL_KEYUPMASK | SDL_KEYDOWNMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK;
131  
132   // Mutex to protect palette
133   static SDL_mutex *sdl_palette_lock = NULL;
# Line 126 | Line 152 | extern void SysMountFirstFloppy(void);
152  
153  
154   /*
155 + *  Framebuffer allocation routines
156 + */
157 +
158 + static void *vm_acquire_framebuffer(uint32 size)
159 + {
160 + #ifdef SHEEPSHAVER
161 + #ifdef DIRECT_ADDRESSING_HACK
162 +        const uint32 FRAME_BUFFER_BASE = 0x61000000;
163 +        uint8 *fb = Mac2HostAddr(FRAME_BUFFER_BASE);
164 +        if (vm_acquire_fixed(fb, size) < 0)
165 +                fb = VM_MAP_FAILED;
166 +        return fb;
167 + #endif
168 + #endif
169 +        return vm_acquire(size);
170 + }
171 +
172 + static inline void vm_release_framebuffer(void *fb, uint32 size)
173 + {
174 +        vm_release(fb, size);
175 + }
176 +
177 +
178 + /*
179 + *  SheepShaver glue
180 + */
181 +
182 + #ifdef SHEEPSHAVER
183 + // Color depth modes type
184 + typedef int video_depth;
185 +
186 + // 1, 2, 4 and 8 bit depths use a color palette
187 + static inline bool IsDirectMode(VIDEO_MODE const & mode)
188 + {
189 +        return IsDirectMode(mode.viAppleMode);
190 + }
191 +
192 + // Abstract base class representing one (possibly virtual) monitor
193 + // ("monitor" = rectangular display with a contiguous frame buffer)
194 + class monitor_desc {
195 + public:
196 +        monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) {}
197 +        virtual ~monitor_desc() {}
198 +
199 +        // Get current Mac frame buffer base address
200 +        uint32 get_mac_frame_base(void) const {return screen_base;}
201 +
202 +        // Set Mac frame buffer base address (called from switch_to_mode())
203 +        void set_mac_frame_base(uint32 base) {screen_base = base;}
204 +
205 +        // Get current video mode
206 +        const VIDEO_MODE &get_current_mode(void) const {return VModes[cur_mode];}
207 +
208 +        // Called by the video driver to switch the video mode on this display
209 +        // (must call set_mac_frame_base())
210 +        virtual void switch_to_current_mode(void) = 0;
211 +
212 +        // Called by the video driver to set the color palette (in indexed modes)
213 +        // or the gamma table (in direct modes)
214 +        virtual void set_palette(uint8 *pal, int num) = 0;
215 + };
216 +
217 + // Vector of pointers to available monitor descriptions, filled by VideoInit()
218 + static vector<monitor_desc *> VideoMonitors;
219 +
220 + // Find Apple mode matching best specified dimensions
221 + static int find_apple_resolution(int xsize, int ysize)
222 + {
223 +        int apple_id;
224 +        if (xsize < 800)
225 +                apple_id = APPLE_640x480;
226 +        else if (xsize < 1024)
227 +                apple_id = APPLE_800x600;
228 +        else if (xsize < 1152)
229 +                apple_id = APPLE_1024x768;
230 +        else if (xsize < 1280) {
231 +                if (ysize < 900)
232 +                        apple_id = APPLE_1152x768;
233 +                else
234 +                        apple_id = APPLE_1152x900;
235 +        }
236 +        else if (xsize < 1600)
237 +                apple_id = APPLE_1280x1024;
238 +        else
239 +                apple_id = APPLE_1600x1200;
240 +        return apple_id;
241 + }
242 +
243 + // Set parameters to specified Apple mode
244 + static void set_apple_resolution(int apple_id, int &xsize, int &ysize)
245 + {
246 +        switch (apple_id) {
247 +        case APPLE_640x480:
248 +                xsize = 640;
249 +                ysize = 480;
250 +                break;
251 +        case APPLE_800x600:
252 +                xsize = 800;
253 +                ysize = 600;
254 +                break;
255 +        case APPLE_1024x768:
256 +                xsize = 1024;
257 +                ysize = 768;
258 +                break;
259 +        case APPLE_1152x768:
260 +                xsize = 1152;
261 +                ysize = 768;
262 +                break;
263 +        case APPLE_1152x900:
264 +                xsize = 1152;
265 +                ysize = 900;
266 +                break;
267 +        case APPLE_1280x1024:
268 +                xsize = 1280;
269 +                ysize = 1024;
270 +                break;
271 +        case APPLE_1600x1200:
272 +                xsize = 1600;
273 +                ysize = 1200;
274 +                break;
275 +        default:
276 +                abort();
277 +        }
278 + }
279 +
280 + // Match Apple mode matching best specified dimensions
281 + static int match_apple_resolution(int &xsize, int &ysize)
282 + {
283 +        int apple_id = find_apple_resolution(xsize, ysize);
284 +        set_apple_resolution(apple_id, xsize, ysize);
285 +        return apple_id;
286 + }
287 +
288 + // Display error alert
289 + static void ErrorAlert(int error)
290 + {
291 +        ErrorAlert(GetString(error));
292 + }
293 +
294 + // Display warning alert
295 + static void WarningAlert(int warning)
296 + {
297 +        WarningAlert(GetString(warning));
298 + }
299 + #endif
300 +
301 +
302 + /*
303   *  monitor_desc subclass for SDL display
304   */
305  
306   class SDL_monitor_desc : public monitor_desc {
307   public:
308 <        SDL_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
308 >        SDL_monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
309          ~SDL_monitor_desc() {}
310  
311          virtual void switch_to_current_mode(void);
# Line 146 | Line 320 | public:
320   *  Utility functions
321   */
322  
323 + // Find palette size for given color depth
324 + static int palette_size(int mode)
325 + {
326 +        switch (mode) {
327 +        case VIDEO_DEPTH_1BIT: return 2;
328 +        case VIDEO_DEPTH_2BIT: return 4;
329 +        case VIDEO_DEPTH_4BIT: return 16;
330 +        case VIDEO_DEPTH_8BIT: return 256;
331 +        case VIDEO_DEPTH_16BIT: return 32;
332 +        case VIDEO_DEPTH_32BIT: return 256;
333 +        default: return 0;
334 +        }
335 + }
336 +
337 + // Return bytes per pixel for requested depth
338 + static inline int bytes_per_pixel(int depth)
339 + {
340 +        int bpp;
341 +        switch (depth) {
342 +        case 8:
343 +                bpp = 1;
344 +                break;
345 +        case 15: case 16:
346 +                bpp = 2;
347 +                break;
348 +        case 24: case 32:
349 +                bpp = 4;
350 +                break;
351 +        default:
352 +                abort();
353 +        }
354 +        return bpp;
355 + }
356 +
357   // Map video_mode depth ID to numerical depth value
358 < static int sdl_depth_of_video_depth(int video_depth)
358 > static int mac_depth_of_video_depth(int video_depth)
359   {
360          int depth = -1;
361          switch (video_depth) {
362 <        case VDEPTH_1BIT:
362 >        case VIDEO_DEPTH_1BIT:
363                  depth = 1;
364                  break;
365 <        case VDEPTH_2BIT:
365 >        case VIDEO_DEPTH_2BIT:
366                  depth = 2;
367                  break;
368 <        case VDEPTH_4BIT:
368 >        case VIDEO_DEPTH_4BIT:
369                  depth = 4;
370                  break;
371 <        case VDEPTH_8BIT:
371 >        case VIDEO_DEPTH_8BIT:
372                  depth = 8;
373                  break;
374 <        case VDEPTH_16BIT:
374 >        case VIDEO_DEPTH_16BIT:
375                  depth = 16;
376                  break;
377 <        case VDEPTH_32BIT:
377 >        case VIDEO_DEPTH_32BIT:
378                  depth = 32;
379                  break;
380          default:
# Line 175 | Line 383 | static int sdl_depth_of_video_depth(int
383          return depth;
384   }
385  
386 + // Map video_mode depth ID to SDL screen depth
387 + static int sdl_depth_of_video_depth(int video_depth)
388 + {
389 +        return (video_depth <= VIDEO_DEPTH_8BIT) ? 8 : mac_depth_of_video_depth(video_depth);
390 + }
391 +
392 + // Check wether specified mode is available
393 + static bool has_mode(int type, int width, int height)
394 + {
395 +        // FIXME: no fullscreen support yet
396 +        if (type == DISPLAY_SCREEN)
397 +                return false;
398 +
399 + #ifdef SHEEPSHAVER
400 +        // Filter out Classic resolutiosn
401 +        if (width == 512 && height == 384)
402 +                return false;
403 +
404 +        // Read window modes prefs
405 +        static uint32 window_modes = 0;
406 +        static uint32 screen_modes = 0;
407 +        if (window_modes == 0 || screen_modes == 0) {
408 +                window_modes = PrefsFindInt32("windowmodes");
409 +                screen_modes = PrefsFindInt32("screenmodes");
410 +                if (window_modes == 0 || screen_modes == 0)
411 +                        window_modes |= 3;                      // Allow at least 640x480 and 800x600 window modes
412 +        }
413 +
414 +        if (type == DISPLAY_WINDOW) {
415 +                int apple_mask, apple_id = find_apple_resolution(width, height);
416 +                switch (apple_id) {
417 +                case APPLE_640x480:             apple_mask = 0x01; break;
418 +                case APPLE_800x600:             apple_mask = 0x02; break;
419 +                case APPLE_1024x768:    apple_mask = 0x04; break;
420 +                case APPLE_1152x768:    apple_mask = 0x40; break;
421 +                case APPLE_1152x900:    apple_mask = 0x08; break;
422 +                case APPLE_1280x1024:   apple_mask = 0x10; break;
423 +                case APPLE_1600x1200:   apple_mask = 0x20; break;
424 +                default:                                apple_mask = 0x00; break;
425 +                }
426 +                return (window_modes & apple_mask);
427 +        }
428 + #else
429 +        return true;
430 + #endif
431 +        return false;
432 + }
433 +
434   // Add mode to list of supported modes
435 < static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth)
435 > static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
436   {
437 <        video_mode mode;
438 <        mode.x = width;
439 <        mode.y = height;
440 <        mode.resolution_id = resolution_id;
441 <        mode.bytes_per_row = bytes_per_row;
442 <        mode.depth = depth;
437 >        // Filter out unsupported modes
438 >        if (!has_mode(type, width, height))
439 >                return;
440 >
441 >        // Fill in VideoMode entry
442 >        VIDEO_MODE mode;
443 > #ifdef SHEEPSHAVER
444 >        // Recalculate dimensions to fit Apple modes
445 >        resolution_id = match_apple_resolution(width, height);
446 >        mode.viType = type;
447 > #endif
448 >        VIDEO_MODE_X = width;
449 >        VIDEO_MODE_Y = height;
450 >        VIDEO_MODE_RESOLUTION = resolution_id;
451 >        VIDEO_MODE_ROW_BYTES = bytes_per_row;
452 >        VIDEO_MODE_DEPTH = (video_depth)depth;
453          VideoModes.push_back(mode);
454   }
455  
456   // Add standard list of windowed modes for given color depth
457 < static void add_window_modes(video_depth depth)
457 > static void add_window_modes(int depth)
458   {
459 <        add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth);
460 <        add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth);
461 <        add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth);
462 <        add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth), depth);
463 <        add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth);
464 <        add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth);
465 <        add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth);
459 >        video_depth vdepth = (video_depth)depth;
460 >        add_mode(DISPLAY_WINDOW, 512, 384, 0x80, TrivialBytesPerRow(512, vdepth), depth);
461 >        add_mode(DISPLAY_WINDOW, 640, 480, 0x81, TrivialBytesPerRow(640, vdepth), depth);
462 >        add_mode(DISPLAY_WINDOW, 800, 600, 0x82, TrivialBytesPerRow(800, vdepth), depth);
463 >        add_mode(DISPLAY_WINDOW, 1024, 768, 0x83, TrivialBytesPerRow(1024, vdepth), depth);
464 >        add_mode(DISPLAY_WINDOW, 1152, 870, 0x84, TrivialBytesPerRow(1152, vdepth), depth);
465 >        add_mode(DISPLAY_WINDOW, 1280, 1024, 0x85, TrivialBytesPerRow(1280, vdepth), depth);
466 >        add_mode(DISPLAY_WINDOW, 1600, 1200, 0x86, TrivialBytesPerRow(1600, vdepth), depth);
467   }
468  
469   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
470 < static void set_mac_frame_buffer(SDL_monitor_desc &monitor, video_depth depth, bool native_byte_order)
470 > static void set_mac_frame_buffer(SDL_monitor_desc &monitor, int depth, bool native_byte_order)
471   {
472   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
473          int layout = FLAYOUT_DIRECT;
474 <        if (depth == VDEPTH_16BIT)
474 >        if (depth == VIDEO_DEPTH_16BIT)
475                  layout = (screen_depth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
476 <        else if (depth == VDEPTH_32BIT)
476 >        else if (depth == VIDEO_DEPTH_32BIT)
477                  layout = (screen_depth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
478          if (native_byte_order)
479                  MacFrameLayout = layout;
# Line 215 | Line 482 | static void set_mac_frame_buffer(SDL_mon
482          monitor.set_mac_frame_base(MacFrameBaseMac);
483  
484          // Set variables used by UAE memory banking
485 <        const video_mode &mode = monitor.get_current_mode();
485 >        const VIDEO_MODE &mode = monitor.get_current_mode();
486          MacFrameBaseHost = the_buffer;
487 <        MacFrameSize = mode.bytes_per_row * mode.y;
487 >        MacFrameSize = VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y;
488          InitFrameBufferMapping();
489   #else
490          monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
# Line 266 | Line 533 | public:
533  
534   public:
535          SDL_monitor_desc &monitor; // Associated video monitor
536 <        const video_mode &mode;    // Video mode handled by the driver
536 >        const VIDEO_MODE &mode;    // Video mode handled by the driver
537  
538          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
539          SDL_Surface *s; // The surface we draw into
# Line 318 | Line 585 | driver_base::~driver_base()
585          if (s)
586                  SDL_FreeSurface(s);
587  
588 +        // the_buffer shall always be mapped through vm_acquire_framebuffer()
589 +        if (the_buffer != VM_MAP_FAILED) {
590 +                D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
591 +                vm_release_framebuffer(the_buffer, the_buffer_size);
592 +                the_buffer = NULL;
593 +        }
594 +
595          // Free frame buffer(s)
596          if (!use_vosf) {
323                if (the_buffer) {
324                        free(the_buffer);
325                        the_buffer = NULL;
326                }
597                  if (the_buffer_copy) {
598                          free(the_buffer_copy);
599                          the_buffer_copy = NULL;
# Line 331 | Line 601 | driver_base::~driver_base()
601          }
602   #ifdef ENABLE_VOSF
603          else {
334                // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
335                if (the_buffer != VM_MAP_FAILED) {
336                        D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
337                        vm_release(the_buffer, the_buffer_size);
338                        the_buffer = NULL;
339                }
604                  if (the_host_buffer) {
605                          D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
606                          free(the_host_buffer);
# Line 347 | Line 611 | driver_base::~driver_base()
611                          free(the_buffer_copy);
612                          the_buffer_copy = NULL;
613                  }
614 +
615 +                // Deinitialize VOSF
616 +                video_vosf_exit();
617          }
618   #endif
619   }
# Line 354 | Line 621 | driver_base::~driver_base()
621   // Palette has changed
622   void driver_base::update_palette(void)
623   {
624 <        const video_mode &mode = monitor.get_current_mode();
624 >        const VIDEO_MODE &mode = monitor.get_current_mode();
625  
626 <        if (mode.depth <= VDEPTH_8BIT)
626 >        if ((int)VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT)
627                  SDL_SetPalette(s, SDL_PHYSPAL, sdl_palette, 0, 256);
628   }
629  
# Line 379 | Line 646 | void driver_base::restore_mouse_accel(vo
646   driver_window::driver_window(SDL_monitor_desc &m)
647          : driver_base(m), mouse_grabbed(false)
648   {
649 <        int width = mode.x, height = mode.y;
649 >        int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
650          int aligned_width = (width + 15) & ~15;
651          int aligned_height = (height + 15) & ~15;
652  
# Line 387 | Line 654 | driver_window::driver_window(SDL_monitor
654          ADBSetRelMouseMode(mouse_grabbed);
655  
656          // Create surface
657 <        int depth = (mode.depth <= VDEPTH_8BIT ? 8 : screen_depth);
657 >        int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
658          if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL)
659                  return;
660  
# Line 396 | Line 663 | driver_window::driver_window(SDL_monitor
663          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
664          the_host_buffer = (uint8 *)s->pixels;
665          the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
666 <        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
666 >        the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
667          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
668          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
669 +
670 +        // Check whether we can initialize the VOSF subsystem and it's profitable
671 +        if (!video_vosf_init(m)) {
672 +                WarningAlert(STR_VOSF_INIT_ERR);
673 +                use_vosf = false;
674 +        }
675 +        else if (!video_vosf_profitable()) {
676 +                video_vosf_exit();
677 +                printf("VOSF acceleration is not profitable on this platform, disabling it\n");
678 +                use_vosf = false;
679 +        }
680 +        if (!use_vosf) {
681 +                free(the_buffer_copy);
682 +                vm_release(the_buffer, the_buffer_size);
683 +                the_host_buffer = NULL;
684 +        }
685 + #endif
686 +        if (!use_vosf) {
687 +                // Allocate memory for frame buffer
688 +                the_buffer_size = (aligned_height + 2) * s->pitch;
689 +                the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
690 +                the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
691 +                D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
692 +        }
693 +        
694 + #ifdef SHEEPSHAVER
695 +        // Create cursor
696 +        if ((sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, 0, 0)) != NULL) {
697 +                SDL_SetCursor(sdl_cursor);
698 +                cursor_changed = false;
699 +        }
700   #else
701 <        // Allocate memory for frame buffer
702 <        the_buffer_size = (aligned_height + 2) * s->pitch;
405 <        the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
406 <        the_buffer = (uint8 *)calloc(1, the_buffer_size);
407 <        D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
701 >        // Hide cursor
702 >        SDL_ShowCursor(0);
703   #endif
704  
705          // Set window name/class
706          set_window_name(STR_WINDOW_TITLE);
707  
413        // Hide cursor
414        SDL_ShowCursor(0);
415
708          // Init blitting routines
709          SDL_PixelFormat *f = s->format;
710          VisualFormat visualFormat;
# Line 420 | Line 712 | driver_window::driver_window(SDL_monitor
712          visualFormat.Rmask = f->Rmask;
713          visualFormat.Gmask = f->Gmask;
714          visualFormat.Bmask = f->Bmask;
715 <        Screen_blitter_init(visualFormat, true, sdl_depth_of_video_depth(mode.depth));
715 >        Screen_blitter_init(visualFormat, true, mac_depth_of_video_depth(VIDEO_MODE_DEPTH));
716  
717          // Load gray ramp to 8->16/32 expand map
718          if (!IsDirectMode(mode))
# Line 428 | Line 720 | driver_window::driver_window(SDL_monitor
720                          ExpandMap[i] = SDL_MapRGB(f, i, i, i);
721  
722          // Set frame buffer base
723 <        set_mac_frame_buffer(monitor, mode.depth, true);
723 >        set_mac_frame_buffer(monitor, VIDEO_MODE_DEPTH, true);
724  
725          // Everything went well
726          init_ok = true;
# Line 518 | Line 810 | static void keycode_init(void)
810                  SDL_VideoDriverName(video_driver, sizeof(video_driver));
811                  bool video_driver_found = false;
812                  char line[256];
813 +                int n_keys = 0;
814                  while (fgets(line, sizeof(line) - 1, f)) {
815                          // Read line
816                          int len = strlen(line);
# Line 530 | Line 823 | static void keycode_init(void)
823                                  continue;
824  
825                          if (video_driver_found) {
826 <                                // Skip aliases
827 <                                static const char alias_str[] = "alias";
828 <                                if (strncmp(line, alias_str, sizeof(alias_str) - 1) == 0)
826 >                                // Skip aliases as long as we have read keycodes yet
827 >                                // Otherwise, it's another mapping and we have to stop
828 >                                static const char sdl_str[] = "sdl";
829 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0 && n_keys == 0)
830                                          continue;
831  
832                                  // Read keycode
833                                  int x_code, mac_code;
834                                  if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
835 <                                        keycode_table[x_code & 0xff] = mac_code;
835 >                                        keycode_table[x_code & 0xff] = mac_code, n_keys++;
836                                  else
837                                          break;
838                          } else {
839                                  // Search for SDL video driver string
840 <                                static const char alias_sdl_str[] = "alias SDL";
841 <                                if (strncmp(line, alias_sdl_str, sizeof(alias_sdl_str) - 1) == 0) {
842 <                                        char *p = line + sizeof(alias_sdl_str);
840 >                                static const char sdl_str[] = "sdl";
841 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0) {
842 >                                        char *p = line + sizeof(sdl_str);
843                                          if (strstr(video_driver, p) == video_driver)
844                                                  video_driver_found = true;
845                                  }
# Line 563 | Line 857 | static void keycode_init(void)
857                          WarningAlert(str);
858                          return;
859                  }
860 +
861 +                D(bug("Using SDL/%s keycodes table, %d key mappings\n", video_driver, n_keys));
862          }
863   }
864  
# Line 570 | Line 866 | static void keycode_init(void)
866   bool SDL_monitor_desc::video_open(void)
867   {
868          D(bug("video_open()\n"));
869 <        const video_mode &mode = get_current_mode();
869 >        const VIDEO_MODE &mode = get_current_mode();
870 > #if DEBUG
871 >        D(bug("Current video mode:\n"));
872 >        D(bug(" %dx%d (ID %02x), %d bpp\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << (VIDEO_MODE_DEPTH & 0x0f)));
873 > #endif
874  
875          // Create display driver object of requested type
876          switch (display_type) {
# Line 586 | Line 886 | bool SDL_monitor_desc::video_open(void)
886                  return false;
887          }
888  
589 #ifdef ENABLE_VOSF
590        if (use_vosf) {
591                // Initialize the VOSF system
592                if (!video_vosf_init(*this)) {
593                        ErrorAlert(STR_VOSF_INIT_ERR);
594                return false;
595                }
596        }
597 #endif
598        
889          // Initialize VideoRefresh function
890          VideoRefreshInit();
891  
# Line 604 | Line 894 | bool SDL_monitor_desc::video_open(void)
894  
895          // Start redraw/input thread
896          redraw_thread_cancel = false;
897 <        redraw_thread_active = (SDL_CreateThread(redraw_func, NULL) != NULL);
897 >        redraw_thread_active = ((redraw_thread = SDL_CreateThread(redraw_func, NULL)) != NULL);
898          if (!redraw_thread_active) {
899                  printf("FATAL: cannot create redraw thread\n");
900                  return false;
# Line 612 | Line 902 | bool SDL_monitor_desc::video_open(void)
902          return true;
903   }
904  
905 + #ifdef SHEEPSHAVER
906 + bool VideoInit(void)
907 + {
908 +        const bool classic = false;
909 + #else
910   bool VideoInit(bool classic)
911   {
912 + #endif
913          classic_mode = classic;
914  
915   #ifdef ENABLE_VOSF
# Line 637 | Line 933 | bool VideoInit(bool classic)
933          mouse_wheel_lines = PrefsFindInt32("mousewheellines");
934  
935          // Get screen mode from preferences
936 <        const char *mode_str;
936 >        const char *mode_str = NULL;
937 > #ifndef SHEEPSHAVER
938          if (classic_mode)
939                  mode_str = "win/512/342";
940          else
941                  mode_str = PrefsFindString("screen");
942 + #endif
943  
944          // Determine display type and default dimensions
945 <        int default_width = 512, default_height = 384;
945 >        int default_width, default_height;
946 >        if (classic) {
947 >                default_width = 512;
948 >                default_height = 384;
949 >        }
950 >        else {
951 >                default_width = 640;
952 >                default_height = 480;
953 >        }
954          display_type = DISPLAY_WINDOW;
955          if (mode_str) {
956                  if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
957                          display_type = DISPLAY_WINDOW;
958          }
959          int max_width = 640, max_height = 480;
960 <        if (display_type == DISPLAY_SCREEN) {
961 <                SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
962 <                if (modes && modes != (SDL_Rect **)-1) {
963 <                        max_width = modes[0]->w;
964 <                        max_height = modes[0]->h;
960 >        SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
961 >        if (modes && modes != (SDL_Rect **)-1) {
962 >                // It turns out that on some implementations, and contrary to the documentation,
963 >                // the returned list is not sorted from largest to smallest (e.g. Windows)
964 >                for (int i = 0; modes[i] != NULL; i++) {
965 >                        const int w = modes[i]->w;
966 >                        const int h = modes[i]->h;
967 >                        if (w > max_width && h > max_height) {
968 >                                max_width = w;
969 >                                max_height = h;
970 >                        }
971                  }
972 +                if (default_width > max_width)
973 +                        default_width = max_width;
974 +                if (default_height > max_height)
975 +                        default_height = max_height;
976          }
977          if (default_width <= 0)
978                  default_width = max_width;
# Line 665 | Line 981 | bool VideoInit(bool classic)
981  
982          // Mac screen depth follows X depth
983          screen_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
984 <        video_depth default_depth;
984 >        int default_depth;
985          switch (screen_depth) {
986          case 8:
987 <                default_depth = VDEPTH_8BIT;
987 >                default_depth = VIDEO_DEPTH_8BIT;
988                  break;
989          case 15: case 16:
990 <                default_depth = VDEPTH_16BIT;
990 >                default_depth = VIDEO_DEPTH_16BIT;
991                  break;
992          case 24: case 32:
993 <                default_depth = VDEPTH_32BIT;
993 >                default_depth = VIDEO_DEPTH_32BIT;
994                  break;
995          default:
996 <                default_depth =  VDEPTH_1BIT;
996 >                default_depth =  VIDEO_DEPTH_1BIT;
997                  break;
998          }
999  
1000          // Construct list of supported modes
1001          if (display_type == DISPLAY_WINDOW) {
1002                  if (classic)
1003 <                        add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
1003 >                        add_mode(display_type, 512, 342, 0x80, 64, VIDEO_DEPTH_1BIT);
1004                  else {
1005 <                        for (int d = VDEPTH_1BIT; d <= default_depth; d++) {
1006 <                                int bpp = (d <= VDEPTH_8BIT ? 8 : sdl_depth_of_video_depth(d));
1005 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1006 >                                int bpp = sdl_depth_of_video_depth(d);
1007                                  if (SDL_VideoModeOK(max_width, max_height, bpp, SDL_HWSURFACE))
1008                                          add_window_modes(video_depth(d));
1009                          }
1010                  }
1011          } else
1012 <                add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
1012 >                add_mode(display_type, default_width, default_height, 0x80, TrivialBytesPerRow(default_width, (video_depth)default_depth), default_depth);
1013          if (VideoModes.empty()) {
1014                  ErrorAlert(STR_NO_XVISUAL_ERR);
1015                  return false;
# Line 701 | Line 1017 | bool VideoInit(bool classic)
1017  
1018          // Find requested default mode with specified dimensions
1019          uint32 default_id;
1020 <        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1020 >        std::vector<VIDEO_MODE>::const_iterator i, end = VideoModes.end();
1021          for (i = VideoModes.begin(); i != end; ++i) {
1022 <                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1023 <                        default_id = i->resolution_id;
1022 >                const VIDEO_MODE & mode = (*i);
1023 >                if (VIDEO_MODE_X == default_width && VIDEO_MODE_Y == default_height && VIDEO_MODE_DEPTH == default_depth) {
1024 >                        default_id = VIDEO_MODE_RESOLUTION;
1025 > #ifdef SHEEPSHAVER
1026 >                        std::vector<VIDEO_MODE>::const_iterator begin = VideoModes.begin();
1027 >                        cur_mode = distance(begin, i);
1028 > #endif
1029                          break;
1030                  }
1031          }
1032          if (i == end) { // not found, use first available mode
1033 <                default_depth = VideoModes[0].depth;
1034 <                default_id = VideoModes[0].resolution_id;
1033 >                const VIDEO_MODE & mode = VideoModes[0];
1034 >                default_depth = VIDEO_MODE_DEPTH;
1035 >                default_id = VIDEO_MODE_RESOLUTION;
1036 > #ifdef SHEEPSHAVER
1037 >                cur_mode = 0;
1038 > #endif
1039          }
1040  
1041 + #ifdef SHEEPSHAVER
1042 +        for (int i = 0; i < VideoModes.size(); i++)
1043 +                VModes[i] = VideoModes[i];
1044 +        VideoInfo *p = &VModes[VideoModes.size()];
1045 +        p->viType = DIS_INVALID;        // End marker
1046 +        p->viRowBytes = 0;
1047 +        p->viXsize = p->viYsize = 0;
1048 +        p->viAppleMode = 0;
1049 +        p->viAppleID = 0;
1050 + #endif
1051 +
1052   #if DEBUG
1053          D(bug("Available video modes:\n"));
1054          for (i = VideoModes.begin(); i != end; ++i) {
1055 <                int bits = 1 << i->depth;
1055 >                const VIDEO_MODE & mode = (*i);
1056 >                int bits = 1 << VIDEO_MODE_DEPTH;
1057                  if (bits == 16)
1058                          bits = 15;
1059                  else if (bits == 32)
1060                          bits = 24;
1061 <                D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1061 >                D(bug(" %dx%d (ID %02x), %d colors\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << bits));
1062          }
1063   #endif
1064  
1065          // Create SDL_monitor_desc for this (the only) display
1066 <        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, default_depth, default_id);
1066 >        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, (video_depth)default_depth, default_id);
1067          VideoMonitors.push_back(monitor);
1068  
1069          // Open display
# Line 746 | Line 1083 | void SDL_monitor_desc::video_close(void)
1083          // Stop redraw thread
1084          if (redraw_thread_active) {
1085                  redraw_thread_cancel = true;
1086 < //              SDL_WaitThread(redraw_thread, NULL); doesn't work
750 <                while (redraw_thread_cancel);
1086 >                SDL_WaitThread(redraw_thread, NULL);
1087          }
1088          redraw_thread_active = false;
1089  
# Line 755 | Line 1091 | void SDL_monitor_desc::video_close(void)
1091          UNLOCK_FRAME_BUFFER;
1092          D(bug(" frame buffer unlocked\n"));
1093  
758 #ifdef ENABLE_VOSF
759        if (use_vosf) {
760                // Deinitialize VOSF
761                video_vosf_exit();
762        }
763 #endif
764
1094          // Close display
1095          delete drv;
1096          drv = NULL;
# Line 797 | Line 1126 | void VideoQuitFullScreen(void)
1126   *  Mac VBL interrupt
1127   */
1128  
1129 + /*
1130 + *  Execute video VBL routine
1131 + */
1132 +
1133 + #ifdef SHEEPSHAVER
1134 + void VideoVBL(void)
1135 + {
1136 +        // Emergency quit requested? Then quit
1137 +        if (emerg_quit)
1138 +                QuitEmulator();
1139 +
1140 +        // Temporarily give up frame buffer lock (this is the point where
1141 +        // we are suspended when the user presses Ctrl-Tab)
1142 +        UNLOCK_FRAME_BUFFER;
1143 +        LOCK_FRAME_BUFFER;
1144 +
1145 +        // Execute video VBL
1146 +        if (private_data != NULL && private_data->interruptsEnabled)
1147 +                VSLDoInterruptService(private_data->vslServiceID);
1148 + }
1149 + #else
1150   void VideoInterrupt(void)
1151   {
1152 +        // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
1153 +        SDL_PumpEvents();
1154 +
1155          // Emergency quit requested? Then quit
1156          if (emerg_quit)
1157                  QuitEmulator();
# Line 808 | Line 1161 | void VideoInterrupt(void)
1161          UNLOCK_FRAME_BUFFER;
1162          LOCK_FRAME_BUFFER;
1163   }
1164 + #endif
1165  
1166  
1167   /*
1168   *  Set palette
1169   */
1170  
1171 + #ifdef SHEEPSHAVER
1172 + void video_set_palette(void)
1173 + {
1174 +        monitor_desc * monitor = VideoMonitors[0];
1175 +        int n_colors = palette_size(monitor->get_current_mode().viAppleMode);
1176 +        uint8 pal[256 * 3];
1177 +        for (int c = 0; c < n_colors; c++) {
1178 +                pal[c*3 + 0] = mac_pal[c].red;
1179 +                pal[c*3 + 1] = mac_pal[c].green;
1180 +                pal[c*3 + 2] = mac_pal[c].blue;
1181 +        }
1182 +        monitor->set_palette(pal, n_colors);
1183 + }
1184 + #endif
1185 +
1186   void SDL_monitor_desc::set_palette(uint8 *pal, int num_in)
1187   {
1188 <        const video_mode &mode = get_current_mode();
1188 >        const VIDEO_MODE &mode = get_current_mode();
1189  
1190          // FIXME: how can we handle the gamma ramp?
1191 <        if (mode.depth > VDEPTH_8BIT)
1191 >        if ((int)VIDEO_MODE_DEPTH > VIDEO_DEPTH_8BIT)
1192                  return;
1193  
1194          LOCK_PALETTE;
# Line 844 | Line 1213 | void SDL_monitor_desc::set_palette(uint8
1213                  }
1214  
1215   #ifdef ENABLE_VOSF
1216 <                // We have to redraw everything because the interpretation of pixel values changed
1217 <                LOCK_VOSF;
1218 <                PFLAG_SET_ALL;
1219 <                UNLOCK_VOSF;
1220 <                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1216 >                if (use_vosf) {
1217 >                        // We have to redraw everything because the interpretation of pixel values changed
1218 >                        LOCK_VOSF;
1219 >                        PFLAG_SET_ALL;
1220 >                        UNLOCK_VOSF;
1221 >                        memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1222 >                }
1223   #endif
1224          }
1225  
# Line 863 | Line 1234 | void SDL_monitor_desc::set_palette(uint8
1234   *  Switch video mode
1235   */
1236  
1237 + #ifdef SHEEPSHAVER
1238 + int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1239 + {
1240 +        /* return if no mode change */
1241 +        if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1242 +            (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
1243 +
1244 +        /* first find video mode in table */
1245 +        for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
1246 +                if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
1247 +                    (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
1248 +                        csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
1249 +                        csSave->saveData = ReadMacInt32(ParamPtr + csData);
1250 +                        csSave->savePage = ReadMacInt16(ParamPtr + csPage);
1251 +
1252 +                        // Disable interrupts and pause redraw thread
1253 +                        DisableInterrupt();
1254 +                        thread_stop_ack = false;
1255 +                        thread_stop_req = true;
1256 +                        while (!thread_stop_ack) ;
1257 +
1258 +                        cur_mode = i;
1259 +                        monitor_desc *monitor = VideoMonitors[0];
1260 +                        monitor->switch_to_current_mode();
1261 +
1262 +                        WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
1263 +                        csSave->saveBaseAddr=screen_base;
1264 +                        csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
1265 +                        csSave->saveMode=VModes[cur_mode].viAppleMode;
1266 +
1267 +                        // Enable interrupts and resume redraw thread
1268 +                        thread_stop_req = false;
1269 +                        EnableInterrupt();
1270 +                        return noErr;
1271 +                }
1272 +        }
1273 +        return paramErr;
1274 + }
1275 + #endif
1276 +
1277   void SDL_monitor_desc::switch_to_current_mode(void)
1278   {
1279          // Close and reopen display
# Line 877 | Line 1288 | void SDL_monitor_desc::switch_to_current
1288  
1289  
1290   /*
1291 < *  Translate key event to Mac keycode, returns -1 if no keycode was found
1292 < *  and -2 if the key was recognized as a hotkey
1291 > *  Can we set the MacOS cursor image into the window?
1292 > */
1293 >
1294 > #ifdef SHEEPSHAVER
1295 > bool video_can_change_cursor(void)
1296 > {
1297 >        return (display_type == DISPLAY_WINDOW);
1298 > }
1299 > #endif
1300 >
1301 >
1302 > /*
1303 > *  Set cursor image for window
1304 > */
1305 >
1306 > #ifdef SHEEPSHAVER
1307 > void video_set_cursor(void)
1308 > {
1309 >        cursor_changed = true;
1310 > }
1311 > #endif
1312 >
1313 >
1314 > /*
1315 > *  Keyboard-related utilify functions
1316   */
1317  
1318 + static bool is_modifier_key(SDL_KeyboardEvent const & e)
1319 + {
1320 +        switch (e.keysym.sym) {
1321 +        case SDLK_NUMLOCK:
1322 +        case SDLK_CAPSLOCK:
1323 +        case SDLK_SCROLLOCK:
1324 +        case SDLK_RSHIFT:
1325 +        case SDLK_LSHIFT:
1326 +        case SDLK_RCTRL:
1327 +        case SDLK_LCTRL:
1328 +        case SDLK_RALT:
1329 +        case SDLK_LALT:
1330 +        case SDLK_RMETA:
1331 +        case SDLK_LMETA:
1332 +        case SDLK_LSUPER:
1333 +        case SDLK_RSUPER:
1334 +        case SDLK_MODE:
1335 +        case SDLK_COMPOSE:
1336 +                return true;
1337 +        }
1338 +        return false;
1339 + }
1340 +
1341   static bool is_ctrl_down(SDL_keysym const & ks)
1342   {
1343          return ctrl_down || (ks.mod & KMOD_CTRL);
1344   }
1345  
1346 +
1347 + /*
1348 + *  Translate key event to Mac keycode, returns -1 if no keycode was found
1349 + *  and -2 if the key was recognized as a hotkey
1350 + */
1351 +
1352   static int kc_decode(SDL_keysym const & ks, bool key_down)
1353   {
1354          switch (ks.sym) {
# Line 955 | Line 1418 | static int kc_decode(SDL_keysym const &
1418          case SDLK_RCTRL: return 0x36;
1419          case SDLK_LSHIFT: return 0x38;
1420          case SDLK_RSHIFT: return 0x38;
1421 + #if (defined(__APPLE__) && defined(__MACH__))
1422 +        case SDLK_LALT: return 0x3a;
1423 +        case SDLK_RALT: return 0x3a;
1424 +        case SDLK_LMETA: return 0x37;
1425 +        case SDLK_RMETA: return 0x37;
1426 + #else
1427          case SDLK_LALT: return 0x37;
1428          case SDLK_RALT: return 0x37;
1429          case SDLK_LMETA: return 0x3a;
1430          case SDLK_RMETA: return 0x3a;
1431 + #endif
1432          case SDLK_MENU: return 0x32;
1433          case SDLK_CAPSLOCK: return 0x39;
1434          case SDLK_NUMLOCK: return 0x47;
# Line 1021 | Line 1491 | static int event2keycode(SDL_KeyboardEve
1491  
1492   static void handle_events(void)
1493   {
1494 <        SDL_Event event;
1495 <        while (SDL_PollEvent(&event)) {
1496 <                switch (event.type) {
1494 >        SDL_Event events[10];
1495 >        const int n_max_events = sizeof(events) / sizeof(events[0]);
1496 >        int n_events;
1497 >
1498 >        while ((n_events = SDL_PeepEvents(events, n_max_events, SDL_GETEVENT, sdl_eventmask)) > 0) {
1499 >                for (int i = 0; i < n_events; i++) {
1500 >                        SDL_Event const & event = events[i];
1501 >                        switch (event.type) {
1502  
1503                          // Mouse button
1504                          case SDL_MOUSEBUTTONDOWN: {
# Line 1060 | Line 1535 | static void handle_events(void)
1535                          // Keyboard
1536                          case SDL_KEYDOWN: {
1537                                  int code = -1;
1538 <                                if (use_keycodes) {
1538 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1539                                          if (event2keycode(event.key, true) != -2)       // This is called to process the hotkeys
1540                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1541                                  } else
# Line 1088 | Line 1563 | static void handle_events(void)
1563                          }
1564                          case SDL_KEYUP: {
1565                                  int code = -1;
1566 <                                if (use_keycodes) {
1566 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1567                                          if (event2keycode(event.key, false) != -2)      // This is called to process the hotkeys
1568                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1569                                  } else
1570                                          code = event2keycode(event.key, false);
1571 <                                if (code >= 0 && code != 0x39) {        // Don't propagate Caps Lock releases
1572 <                                        ADBKeyUp(code);
1571 >                                if (code >= 0) {
1572 >                                        if (code == 0x39) {     // Caps Lock released
1573 >                                                if (caps_on) {
1574 >                                                        ADBKeyUp(code);
1575 >                                                        caps_on = false;
1576 >                                                } else {
1577 >                                                        ADBKeyDown(code);
1578 >                                                        caps_on = true;
1579 >                                                }
1580 >                                        } else
1581 >                                                ADBKeyUp(code);
1582                                          if (code == 0x36)
1583                                                  ctrl_down = false;
1584                                  }
# Line 1104 | Line 1588 | static void handle_events(void)
1588                          // Hidden parts exposed, force complete refresh of window
1589                          case SDL_VIDEOEXPOSE:
1590                                  if (display_type == DISPLAY_WINDOW) {
1591 <                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
1591 >                                        const VIDEO_MODE &mode = VideoMonitors[0]->get_current_mode();
1592   #ifdef ENABLE_VOSF
1593                                          if (use_vosf) {                 // VOSF refresh
1594                                                  LOCK_VOSF;
1595                                                  PFLAG_SET_ALL;
1596                                                  UNLOCK_VOSF;
1597 <                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1597 >                                                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1598                                          }
1599                                          else
1600   #endif
1601 <                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1601 >                                                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1602                                  }
1603                                  break;
1604  
# Line 1123 | Line 1607 | static void handle_events(void)
1607                                  ADBKeyDown(0x7f);       // Power key
1608                                  ADBKeyUp(0x7f);
1609                                  break;
1610 +                        }
1611                  }
1612          }
1613   }
# Line 1137 | Line 1622 | static void update_display_static(driver
1622   {
1623          // Incremental update code
1624          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1625 <        const video_mode &mode = drv->mode;
1626 <        int bytes_per_row = mode.bytes_per_row;
1625 >        const VIDEO_MODE &mode = drv->mode;
1626 >        int bytes_per_row = VIDEO_MODE_ROW_BYTES;
1627          uint8 *p, *p2;
1628  
1629          // Check for first line from top and first line from bottom that have changed
1630          y1 = 0;
1631 <        for (j=0; j<mode.y; j++) {
1631 >        for (j=0; j<VIDEO_MODE_Y; j++) {
1632                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1633                          y1 = j;
1634                          break;
1635                  }
1636          }
1637          y2 = y1 - 1;
1638 <        for (j=mode.y-1; j>=y1; j--) {
1638 >        for (j=VIDEO_MODE_Y-1; j>=y1; j--) {
1639                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1640                          y2 = j;
1641                          break;
# Line 1160 | Line 1645 | static void update_display_static(driver
1645  
1646          // Check for first column from left and first column from right that have changed
1647          if (high) {
1648 <                if (mode.depth < VDEPTH_8BIT) {
1648 >                if ((int)VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
1649                          const int src_bytes_per_row = bytes_per_row;
1650                          const int dst_bytes_per_row = drv->s->pitch;
1651 <                        const int pixels_per_byte = mode.x / src_bytes_per_row;
1651 >                        const int pixels_per_byte = VIDEO_MODE_X / src_bytes_per_row;
1652  
1653 <                        x1 = mode.x / pixels_per_byte;
1653 >                        x1 = VIDEO_MODE_X / pixels_per_byte;
1654                          for (j = y1; j <= y2; j++) {
1655                                  p = &the_buffer[j * bytes_per_row];
1656                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1183 | Line 1668 | static void update_display_static(driver
1668                                  p2 = &the_buffer_copy[j * bytes_per_row];
1669                                  p += bytes_per_row;
1670                                  p2 += bytes_per_row;
1671 <                                for (i = (mode.x / pixels_per_byte); i > x2; i--) {
1671 >                                for (i = (VIDEO_MODE_X / pixels_per_byte); i > x2; i--) {
1672                                          p--; p2--;
1673                                          if (*p != *p2) {
1674                                                  x2 = i;
# Line 1221 | Line 1706 | static void update_display_static(driver
1706                          }
1707  
1708                  } else {
1709 <                        const int bytes_per_pixel = mode.bytes_per_row / mode.x;
1709 >                        const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
1710  
1711 <                        x1 = mode.x;
1711 >                        x1 = VIDEO_MODE_X;
1712                          for (j=y1; j<=y2; j++) {
1713                                  p = &the_buffer[j * bytes_per_row];
1714                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1241 | Line 1726 | static void update_display_static(driver
1726                                  p2 = &the_buffer_copy[j * bytes_per_row];
1727                                  p += bytes_per_row;
1728                                  p2 += bytes_per_row;
1729 <                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1729 >                                for (i=VIDEO_MODE_X*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1730                                          p--;
1731                                          p2--;
1732                                          if (*p != *p2) {
# Line 1401 | Line 1886 | static void VideoRefreshInit(void)
1886          }
1887   }
1888  
1889 + const int VIDEO_REFRESH_HZ = 60;
1890 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
1891 +
1892   static int redraw_func(void *arg)
1893   {
1894          uint64 start = GetTicks_usec();
1895          int64 ticks = 0;
1896 +        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
1897  
1898          while (!redraw_thread_cancel) {
1899  
1900                  // Wait
1901 <                Delay_usec(16667);
1901 >                next += VIDEO_REFRESH_DELAY;
1902 >                int64 delay = next - GetTicks_usec();
1903 >                if (delay > 0)
1904 >                        Delay_usec(delay);
1905 >                else if (delay < -VIDEO_REFRESH_DELAY)
1906 >                        next = GetTicks_usec();
1907 >                ticks++;
1908 >
1909 > #ifdef SHEEPSHAVER
1910 >                // Pause if requested (during video mode switches)
1911 >                if (thread_stop_req) {
1912 >                        thread_stop_ack = true;
1913 >                        continue;
1914 >                }
1915 > #endif
1916  
1917                  // Handle SDL events
1918                  handle_events();
1919  
1920                  // Refresh display
1921                  video_refresh();
1922 <                ticks++;
1922 >
1923 > #ifdef SHEEPSHAVER
1924 >                // Set new cursor image if it was changed
1925 >                if (cursor_changed && sdl_cursor) {
1926 >                        cursor_changed = false;
1927 >                        SDL_FreeCursor(sdl_cursor);
1928 >                        sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
1929 >                        if (sdl_cursor)
1930 >                                SDL_SetCursor(sdl_cursor);
1931 >                }
1932 > #endif
1933  
1934                  // Set new palette if it was changed
1935                  handle_palette_changes();
# Line 1424 | Line 1937 | static int redraw_func(void *arg)
1937  
1938          uint64 end = GetTicks_usec();
1939          D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1427        redraw_thread_cancel = false;
1940          return 0;
1941   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines