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.14 by gbeauche, 2005-01-22T17:41:33Z

# 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   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
# Line 72 | Line 90 | static int32 frame_skip;                                                       // Prefs
90   static int16 mouse_wheel_mode;
91   static int16 mouse_wheel_lines;
92  
75 static int display_type = DISPLAY_WINDOW;                       // See enum above
93   static uint8 *the_buffer = NULL;                                        // Mac frame buffer (where MacOS draws into)
94   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer (for refreshed modes)
95   static uint32 the_buffer_size;                                          // Size of allocated the_buffer
# Line 80 | Line 97 | static uint32 the_buffer_size;                                         // S
97   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
98   static volatile bool redraw_thread_cancel;                      // Flag: Cancel Redraw thread
99   static SDL_Thread *redraw_thread = NULL;                        // Redraw thread
100 + static volatile bool thread_stop_req = false;
101 + static volatile bool thread_stop_ack = false;           // Acknowledge for thread_stop_req
102  
103   #ifdef ENABLE_VOSF
104 < static bool use_vosf = true;                                            // Flag: VOSF enabled
104 > static bool use_vosf = false;                                           // Flag: VOSF enabled
105   #else
106   static const bool use_vosf = false;                                     // VOSF not possible
107   #endif
# Line 100 | Line 119 | static int keycode_table[256];                                         // X
119  
120   // SDL variables
121   static int screen_depth;                                                        // Depth of current screen
122 + static SDL_Cursor *sdl_cursor;                                          // Copy of Mac cursor
123 + static volatile bool cursor_changed = false;            // Flag: cursor changed, redraw_func must update the cursor
124   static SDL_Color sdl_palette[256];                                      // Color palette to be used as CLUT and gamma table
125   static bool sdl_palette_changed = false;                        // Flag: Palette changed, redraw thread must set new colors
126 + static const int sdl_eventmask = SDL_MOUSEBUTTONDOWNMASK | SDL_MOUSEBUTTONUPMASK | SDL_MOUSEMOTIONMASK | SDL_KEYUPMASK | SDL_KEYDOWNMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK;
127  
128   // Mutex to protect palette
129   static SDL_mutex *sdl_palette_lock = NULL;
# Line 126 | Line 148 | extern void SysMountFirstFloppy(void);
148  
149  
150   /*
151 + *  Framebuffer allocation routines
152 + */
153 +
154 + static void *vm_acquire_framebuffer(uint32 size)
155 + {
156 + #ifdef SHEEPSHAVER
157 + #ifdef DIRECT_ADDRESSING_HACK
158 +        const uint32 FRAME_BUFFER_BASE = 0x61000000;
159 +        uint8 *fb = Mac2HostAddr(FRAME_BUFFER_BASE);
160 +        if (vm_acquire_fixed(fb, size) < 0)
161 +                fb = VM_MAP_FAILED;
162 +        return fb;
163 + #endif
164 + #endif
165 +        return vm_acquire(size);
166 + }
167 +
168 + static inline void vm_release_framebuffer(void *fb, uint32 size)
169 + {
170 +        vm_release(fb, size);
171 + }
172 +
173 +
174 + /*
175 + *  SheepShaver glue
176 + */
177 +
178 + #ifdef SHEEPSHAVER
179 + // Color depth modes type
180 + typedef int video_depth;
181 +
182 + // 1, 2, 4 and 8 bit depths use a color palette
183 + static inline bool IsDirectMode(VIDEO_MODE const & mode)
184 + {
185 +        return IsDirectMode(mode.viAppleMode);
186 + }
187 +
188 + // Abstract base class representing one (possibly virtual) monitor
189 + // ("monitor" = rectangular display with a contiguous frame buffer)
190 + class monitor_desc {
191 + public:
192 +        monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) {}
193 +        virtual ~monitor_desc() {}
194 +
195 +        // Get current Mac frame buffer base address
196 +        uint32 get_mac_frame_base(void) const {return screen_base;}
197 +
198 +        // Set Mac frame buffer base address (called from switch_to_mode())
199 +        void set_mac_frame_base(uint32 base) {screen_base = base;}
200 +
201 +        // Get current video mode
202 +        const VIDEO_MODE &get_current_mode(void) const {return VModes[cur_mode];}
203 +
204 +        // Called by the video driver to switch the video mode on this display
205 +        // (must call set_mac_frame_base())
206 +        virtual void switch_to_current_mode(void) = 0;
207 +
208 +        // Called by the video driver to set the color palette (in indexed modes)
209 +        // or the gamma table (in direct modes)
210 +        virtual void set_palette(uint8 *pal, int num) = 0;
211 + };
212 +
213 + // Vector of pointers to available monitor descriptions, filled by VideoInit()
214 + static vector<monitor_desc *> VideoMonitors;
215 +
216 + // Find Apple mode matching best specified dimensions
217 + static int find_apple_resolution(int xsize, int ysize)
218 + {
219 +        int apple_id;
220 +        if (xsize < 800)
221 +                apple_id = APPLE_640x480;
222 +        else if (xsize < 1024)
223 +                apple_id = APPLE_800x600;
224 +        else if (xsize < 1152)
225 +                apple_id = APPLE_1024x768;
226 +        else if (xsize < 1280) {
227 +                if (ysize < 900)
228 +                        apple_id = APPLE_1152x768;
229 +                else
230 +                        apple_id = APPLE_1152x900;
231 +        }
232 +        else if (xsize < 1600)
233 +                apple_id = APPLE_1280x1024;
234 +        else
235 +                apple_id = APPLE_1600x1200;
236 +        return apple_id;
237 + }
238 +
239 + // Set parameters to specified Apple mode
240 + static void set_apple_resolution(int apple_id, int &xsize, int &ysize)
241 + {
242 +        switch (apple_id) {
243 +        case APPLE_640x480:
244 +                xsize = 640;
245 +                ysize = 480;
246 +                break;
247 +        case APPLE_800x600:
248 +                xsize = 800;
249 +                ysize = 600;
250 +                break;
251 +        case APPLE_1024x768:
252 +                xsize = 1024;
253 +                ysize = 768;
254 +                break;
255 +        case APPLE_1152x768:
256 +                xsize = 1152;
257 +                ysize = 768;
258 +                break;
259 +        case APPLE_1152x900:
260 +                xsize = 1152;
261 +                ysize = 900;
262 +                break;
263 +        case APPLE_1280x1024:
264 +                xsize = 1280;
265 +                ysize = 1024;
266 +                break;
267 +        case APPLE_1600x1200:
268 +                xsize = 1600;
269 +                ysize = 1200;
270 +                break;
271 +        default:
272 +                abort();
273 +        }
274 + }
275 +
276 + // Match Apple mode matching best specified dimensions
277 + static int match_apple_resolution(int &xsize, int &ysize)
278 + {
279 +        int apple_id = find_apple_resolution(xsize, ysize);
280 +        set_apple_resolution(apple_id, xsize, ysize);
281 +        return apple_id;
282 + }
283 +
284 + // Display error alert
285 + static void ErrorAlert(int error)
286 + {
287 +        ErrorAlert(GetString(error));
288 + }
289 +
290 + // Display warning alert
291 + static void WarningAlert(int warning)
292 + {
293 +        WarningAlert(GetString(warning));
294 + }
295 + #endif
296 +
297 +
298 + /*
299   *  monitor_desc subclass for SDL display
300   */
301  
302   class SDL_monitor_desc : public monitor_desc {
303   public:
304 <        SDL_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
304 >        SDL_monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
305          ~SDL_monitor_desc() {}
306  
307          virtual void switch_to_current_mode(void);
# Line 146 | Line 316 | public:
316   *  Utility functions
317   */
318  
319 + // Find palette size for given color depth
320 + static int palette_size(int mode)
321 + {
322 +        switch (mode) {
323 +        case VIDEO_DEPTH_1BIT: return 2;
324 +        case VIDEO_DEPTH_2BIT: return 4;
325 +        case VIDEO_DEPTH_4BIT: return 16;
326 +        case VIDEO_DEPTH_8BIT: return 256;
327 +        case VIDEO_DEPTH_16BIT: return 32;
328 +        case VIDEO_DEPTH_32BIT: return 256;
329 +        default: return 0;
330 +        }
331 + }
332 +
333 + // Return bytes per pixel for requested depth
334 + static inline int bytes_per_pixel(int depth)
335 + {
336 +        int bpp;
337 +        switch (depth) {
338 +        case 8:
339 +                bpp = 1;
340 +                break;
341 +        case 15: case 16:
342 +                bpp = 2;
343 +                break;
344 +        case 24: case 32:
345 +                bpp = 4;
346 +                break;
347 +        default:
348 +                abort();
349 +        }
350 +        return bpp;
351 + }
352 +
353   // Map video_mode depth ID to numerical depth value
354 < static int sdl_depth_of_video_depth(int video_depth)
354 > static int mac_depth_of_video_depth(int video_depth)
355   {
356          int depth = -1;
357          switch (video_depth) {
358 <        case VDEPTH_1BIT:
358 >        case VIDEO_DEPTH_1BIT:
359                  depth = 1;
360                  break;
361 <        case VDEPTH_2BIT:
361 >        case VIDEO_DEPTH_2BIT:
362                  depth = 2;
363                  break;
364 <        case VDEPTH_4BIT:
364 >        case VIDEO_DEPTH_4BIT:
365                  depth = 4;
366                  break;
367 <        case VDEPTH_8BIT:
367 >        case VIDEO_DEPTH_8BIT:
368                  depth = 8;
369                  break;
370 <        case VDEPTH_16BIT:
370 >        case VIDEO_DEPTH_16BIT:
371                  depth = 16;
372                  break;
373 <        case VDEPTH_32BIT:
373 >        case VIDEO_DEPTH_32BIT:
374                  depth = 32;
375                  break;
376          default:
# Line 175 | Line 379 | static int sdl_depth_of_video_depth(int
379          return depth;
380   }
381  
382 + // Map video_mode depth ID to SDL screen depth
383 + static int sdl_depth_of_video_depth(int video_depth)
384 + {
385 +        return (video_depth <= VIDEO_DEPTH_8BIT) ? 8 : mac_depth_of_video_depth(video_depth);
386 + }
387 +
388 + // Check wether specified mode is available
389 + static bool has_mode(int type, int width, int height)
390 + {
391 +        // FIXME: no fullscreen support yet
392 +        if (type == DISPLAY_SCREEN)
393 +                return false;
394 +
395 + #ifdef SHEEPSHAVER
396 +        // Filter out Classic resolutiosn
397 +        if (width == 512 && height == 384)
398 +                return false;
399 +
400 +        // Read window modes prefs
401 +        static uint32 window_modes = 0;
402 +        static uint32 screen_modes = 0;
403 +        if (window_modes == 0 || screen_modes == 0) {
404 +                window_modes = PrefsFindInt32("windowmodes");
405 +                screen_modes = PrefsFindInt32("screenmodes");
406 +                if (window_modes == 0 || screen_modes == 0)
407 +                        window_modes |= 3;                      // Allow at least 640x480 and 800x600 window modes
408 +        }
409 +
410 +        if (type == DISPLAY_WINDOW) {
411 +                int apple_mask, apple_id = find_apple_resolution(width, height);
412 +                switch (apple_id) {
413 +                case APPLE_640x480:             apple_mask = 0x01; break;
414 +                case APPLE_800x600:             apple_mask = 0x02; break;
415 +                case APPLE_1024x768:    apple_mask = 0x04; break;
416 +                case APPLE_1152x768:    apple_mask = 0x40; break;
417 +                case APPLE_1152x900:    apple_mask = 0x08; break;
418 +                case APPLE_1280x1024:   apple_mask = 0x10; break;
419 +                case APPLE_1600x1200:   apple_mask = 0x20; break;
420 +                default:                                apple_mask = 0x00; break;
421 +                }
422 +                return (window_modes & apple_mask);
423 +        }
424 + #else
425 +        return true;
426 + #endif
427 +        return false;
428 + }
429 +
430   // Add mode to list of supported modes
431 < static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth)
431 > static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
432   {
433 <        video_mode mode;
434 <        mode.x = width;
435 <        mode.y = height;
436 <        mode.resolution_id = resolution_id;
437 <        mode.bytes_per_row = bytes_per_row;
438 <        mode.depth = depth;
433 >        // Filter out unsupported modes
434 >        if (!has_mode(type, width, height))
435 >                return;
436 >
437 >        // Fill in VideoMode entry
438 >        VIDEO_MODE mode;
439 > #ifdef SHEEPSHAVER
440 >        // Recalculate dimensions to fit Apple modes
441 >        resolution_id = match_apple_resolution(width, height);
442 >        mode.viType = type;
443 > #endif
444 >        VIDEO_MODE_X = width;
445 >        VIDEO_MODE_Y = height;
446 >        VIDEO_MODE_RESOLUTION = resolution_id;
447 >        VIDEO_MODE_ROW_BYTES = bytes_per_row;
448 >        VIDEO_MODE_DEPTH = (video_depth)depth;
449          VideoModes.push_back(mode);
450   }
451  
452   // Add standard list of windowed modes for given color depth
453 < static void add_window_modes(video_depth depth)
453 > static void add_window_modes(int depth)
454   {
455 <        add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth);
456 <        add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth);
457 <        add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth);
458 <        add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth), depth);
459 <        add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth);
460 <        add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth);
461 <        add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth);
455 >        video_depth vdepth = (video_depth)depth;
456 >        add_mode(DISPLAY_WINDOW, 512, 384, 0x80, TrivialBytesPerRow(512, vdepth), depth);
457 >        add_mode(DISPLAY_WINDOW, 640, 480, 0x81, TrivialBytesPerRow(640, vdepth), depth);
458 >        add_mode(DISPLAY_WINDOW, 800, 600, 0x82, TrivialBytesPerRow(800, vdepth), depth);
459 >        add_mode(DISPLAY_WINDOW, 1024, 768, 0x83, TrivialBytesPerRow(1024, vdepth), depth);
460 >        add_mode(DISPLAY_WINDOW, 1152, 870, 0x84, TrivialBytesPerRow(1152, vdepth), depth);
461 >        add_mode(DISPLAY_WINDOW, 1280, 1024, 0x85, TrivialBytesPerRow(1280, vdepth), depth);
462 >        add_mode(DISPLAY_WINDOW, 1600, 1200, 0x86, TrivialBytesPerRow(1600, vdepth), depth);
463   }
464  
465   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
466 < static void set_mac_frame_buffer(SDL_monitor_desc &monitor, video_depth depth, bool native_byte_order)
466 > static void set_mac_frame_buffer(SDL_monitor_desc &monitor, int depth, bool native_byte_order)
467   {
468   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
469          int layout = FLAYOUT_DIRECT;
470 <        if (depth == VDEPTH_16BIT)
470 >        if (depth == VIDEO_DEPTH_16BIT)
471                  layout = (screen_depth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
472 <        else if (depth == VDEPTH_32BIT)
472 >        else if (depth == VIDEO_DEPTH_32BIT)
473                  layout = (screen_depth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
474          if (native_byte_order)
475                  MacFrameLayout = layout;
# Line 215 | Line 478 | static void set_mac_frame_buffer(SDL_mon
478          monitor.set_mac_frame_base(MacFrameBaseMac);
479  
480          // Set variables used by UAE memory banking
481 <        const video_mode &mode = monitor.get_current_mode();
481 >        const VIDEO_MODE &mode = monitor.get_current_mode();
482          MacFrameBaseHost = the_buffer;
483 <        MacFrameSize = mode.bytes_per_row * mode.y;
483 >        MacFrameSize = VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y;
484          InitFrameBufferMapping();
485   #else
486          monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
# Line 266 | Line 529 | public:
529  
530   public:
531          SDL_monitor_desc &monitor; // Associated video monitor
532 <        const video_mode &mode;    // Video mode handled by the driver
532 >        const VIDEO_MODE &mode;    // Video mode handled by the driver
533  
534          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
535          SDL_Surface *s; // The surface we draw into
# Line 318 | Line 581 | driver_base::~driver_base()
581          if (s)
582                  SDL_FreeSurface(s);
583  
584 +        // the_buffer shall always be mapped through vm_acquire_framebuffer()
585 +        if (the_buffer != VM_MAP_FAILED) {
586 +                D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
587 +                vm_release_framebuffer(the_buffer, the_buffer_size);
588 +                the_buffer = NULL;
589 +        }
590 +
591          // Free frame buffer(s)
592          if (!use_vosf) {
323                if (the_buffer) {
324                        free(the_buffer);
325                        the_buffer = NULL;
326                }
593                  if (the_buffer_copy) {
594                          free(the_buffer_copy);
595                          the_buffer_copy = NULL;
# Line 331 | Line 597 | driver_base::~driver_base()
597          }
598   #ifdef ENABLE_VOSF
599          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                }
600                  if (the_host_buffer) {
601                          D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
602                          free(the_host_buffer);
# Line 347 | Line 607 | driver_base::~driver_base()
607                          free(the_buffer_copy);
608                          the_buffer_copy = NULL;
609                  }
610 +
611 +                // Deinitialize VOSF
612 +                video_vosf_exit();
613          }
614   #endif
615   }
# Line 354 | Line 617 | driver_base::~driver_base()
617   // Palette has changed
618   void driver_base::update_palette(void)
619   {
620 <        const video_mode &mode = monitor.get_current_mode();
620 >        const VIDEO_MODE &mode = monitor.get_current_mode();
621  
622 <        if (mode.depth <= VDEPTH_8BIT)
622 >        if ((int)VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT)
623                  SDL_SetPalette(s, SDL_PHYSPAL, sdl_palette, 0, 256);
624   }
625  
# Line 379 | Line 642 | void driver_base::restore_mouse_accel(vo
642   driver_window::driver_window(SDL_monitor_desc &m)
643          : driver_base(m), mouse_grabbed(false)
644   {
645 <        int width = mode.x, height = mode.y;
645 >        int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
646          int aligned_width = (width + 15) & ~15;
647          int aligned_height = (height + 15) & ~15;
648  
# Line 387 | Line 650 | driver_window::driver_window(SDL_monitor
650          ADBSetRelMouseMode(mouse_grabbed);
651  
652          // Create surface
653 <        int depth = (mode.depth <= VDEPTH_8BIT ? 8 : screen_depth);
653 >        int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
654          if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL)
655                  return;
656  
# Line 396 | Line 659 | driver_window::driver_window(SDL_monitor
659          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
660          the_host_buffer = (uint8 *)s->pixels;
661          the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
662 <        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
662 >        the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
663          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
664          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
665 +
666 +        // Check whether we can initialize the VOSF subsystem and it's profitable
667 +        if (!video_vosf_init(m)) {
668 +                WarningAlert(STR_VOSF_INIT_ERR);
669 +                use_vosf = false;
670 +        }
671 +        else if (!video_vosf_profitable()) {
672 +                video_vosf_exit();
673 +                printf("VOSF acceleration is not profitable on this platform, disabling it\n");
674 +                use_vosf = false;
675 +        }
676 +        if (!use_vosf) {
677 +                free(the_buffer_copy);
678 +                vm_release(the_buffer, the_buffer_size);
679 +                the_host_buffer = NULL;
680 +        }
681 + #endif
682 +        if (!use_vosf) {
683 +                // Allocate memory for frame buffer
684 +                the_buffer_size = (aligned_height + 2) * s->pitch;
685 +                the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
686 +                the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
687 +                D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
688 +        }
689 +        
690 + #ifdef SHEEPSHAVER
691 +        // Create cursor
692 +        if ((sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, 0, 0)) != NULL) {
693 +                SDL_SetCursor(sdl_cursor);
694 +                cursor_changed = false;
695 +        }
696   #else
697 <        // Allocate memory for frame buffer
698 <        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));
697 >        // Hide cursor
698 >        SDL_ShowCursor(0);
699   #endif
700  
701          // Set window name/class
702          set_window_name(STR_WINDOW_TITLE);
703  
413        // Hide cursor
414        SDL_ShowCursor(0);
415
704          // Init blitting routines
705          SDL_PixelFormat *f = s->format;
706          VisualFormat visualFormat;
# Line 420 | Line 708 | driver_window::driver_window(SDL_monitor
708          visualFormat.Rmask = f->Rmask;
709          visualFormat.Gmask = f->Gmask;
710          visualFormat.Bmask = f->Bmask;
711 <        Screen_blitter_init(visualFormat, true, sdl_depth_of_video_depth(mode.depth));
711 >        Screen_blitter_init(visualFormat, true, mac_depth_of_video_depth(VIDEO_MODE_DEPTH));
712  
713          // Load gray ramp to 8->16/32 expand map
714          if (!IsDirectMode(mode))
# Line 428 | Line 716 | driver_window::driver_window(SDL_monitor
716                          ExpandMap[i] = SDL_MapRGB(f, i, i, i);
717  
718          // Set frame buffer base
719 <        set_mac_frame_buffer(monitor, mode.depth, true);
719 >        set_mac_frame_buffer(monitor, VIDEO_MODE_DEPTH, true);
720  
721          // Everything went well
722          init_ok = true;
# Line 518 | Line 806 | static void keycode_init(void)
806                  SDL_VideoDriverName(video_driver, sizeof(video_driver));
807                  bool video_driver_found = false;
808                  char line[256];
809 +                int n_keys = 0;
810                  while (fgets(line, sizeof(line) - 1, f)) {
811                          // Read line
812                          int len = strlen(line);
# Line 530 | Line 819 | static void keycode_init(void)
819                                  continue;
820  
821                          if (video_driver_found) {
822 <                                // Skip aliases
823 <                                static const char alias_str[] = "alias";
824 <                                if (strncmp(line, alias_str, sizeof(alias_str) - 1) == 0)
822 >                                // Skip aliases as long as we have read keycodes yet
823 >                                // Otherwise, it's another mapping and we have to stop
824 >                                static const char sdl_str[] = "sdl";
825 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0 && n_keys == 0)
826                                          continue;
827  
828                                  // Read keycode
829                                  int x_code, mac_code;
830                                  if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
831 <                                        keycode_table[x_code & 0xff] = mac_code;
831 >                                        keycode_table[x_code & 0xff] = mac_code, n_keys++;
832                                  else
833                                          break;
834                          } else {
835                                  // Search for SDL video driver string
836 <                                static const char alias_sdl_str[] = "alias SDL";
837 <                                if (strncmp(line, alias_sdl_str, sizeof(alias_sdl_str) - 1) == 0) {
838 <                                        char *p = line + sizeof(alias_sdl_str);
836 >                                static const char sdl_str[] = "sdl";
837 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0) {
838 >                                        char *p = line + sizeof(sdl_str);
839                                          if (strstr(video_driver, p) == video_driver)
840                                                  video_driver_found = true;
841                                  }
# Line 563 | Line 853 | static void keycode_init(void)
853                          WarningAlert(str);
854                          return;
855                  }
856 +
857 +                D(bug("Using SDL/%s keycodes table, %d key mappings\n", video_driver, n_keys));
858          }
859   }
860  
# Line 570 | Line 862 | static void keycode_init(void)
862   bool SDL_monitor_desc::video_open(void)
863   {
864          D(bug("video_open()\n"));
865 <        const video_mode &mode = get_current_mode();
865 >        const VIDEO_MODE &mode = get_current_mode();
866 > #if DEBUG
867 >        D(bug("Current video mode:\n"));
868 >        D(bug(" %dx%d (ID %02x), %d bpp\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << (VIDEO_MODE_DEPTH & 0x0f)));
869 > #endif
870  
871          // Create display driver object of requested type
872          switch (display_type) {
# Line 586 | Line 882 | bool SDL_monitor_desc::video_open(void)
882                  return false;
883          }
884  
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        
885          // Initialize VideoRefresh function
886          VideoRefreshInit();
887  
# Line 604 | Line 890 | bool SDL_monitor_desc::video_open(void)
890  
891          // Start redraw/input thread
892          redraw_thread_cancel = false;
893 <        redraw_thread_active = (SDL_CreateThread(redraw_func, NULL) != NULL);
893 >        redraw_thread_active = ((redraw_thread = SDL_CreateThread(redraw_func, NULL)) != NULL);
894          if (!redraw_thread_active) {
895                  printf("FATAL: cannot create redraw thread\n");
896                  return false;
# Line 612 | Line 898 | bool SDL_monitor_desc::video_open(void)
898          return true;
899   }
900  
901 + #ifdef SHEEPSHAVER
902 + bool VideoInit(void)
903 + {
904 +        const bool classic = false;
905 + #else
906   bool VideoInit(bool classic)
907   {
908 + #endif
909          classic_mode = classic;
910  
911   #ifdef ENABLE_VOSF
# Line 637 | Line 929 | bool VideoInit(bool classic)
929          mouse_wheel_lines = PrefsFindInt32("mousewheellines");
930  
931          // Get screen mode from preferences
932 <        const char *mode_str;
932 >        const char *mode_str = NULL;
933 > #ifndef SHEEPSHAVER
934          if (classic_mode)
935                  mode_str = "win/512/342";
936          else
937                  mode_str = PrefsFindString("screen");
938 + #endif
939  
940          // Determine display type and default dimensions
941 <        int default_width = 512, default_height = 384;
941 >        int default_width, default_height;
942 >        if (classic) {
943 >                default_width = 512;
944 >                default_height = 384;
945 >        }
946 >        else {
947 >                default_width = 640;
948 >                default_height = 480;
949 >        }
950          display_type = DISPLAY_WINDOW;
951          if (mode_str) {
952                  if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
953                          display_type = DISPLAY_WINDOW;
954          }
955          int max_width = 640, max_height = 480;
956 <        if (display_type == DISPLAY_SCREEN) {
957 <                SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
958 <                if (modes && modes != (SDL_Rect **)-1) {
959 <                        max_width = modes[0]->w;
960 <                        max_height = modes[0]->h;
961 <                }
956 >        SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
957 >        if (modes && modes != (SDL_Rect **)-1) {
958 >                max_width = modes[0]->w;
959 >                max_height = modes[0]->h;
960 >                if (default_width > max_width)
961 >                        default_width = max_width;
962 >                if (default_height > max_height)
963 >                        default_height = max_height;
964          }
965          if (default_width <= 0)
966                  default_width = max_width;
# Line 665 | Line 969 | bool VideoInit(bool classic)
969  
970          // Mac screen depth follows X depth
971          screen_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
972 <        video_depth default_depth;
972 >        int default_depth;
973          switch (screen_depth) {
974          case 8:
975 <                default_depth = VDEPTH_8BIT;
975 >                default_depth = VIDEO_DEPTH_8BIT;
976                  break;
977          case 15: case 16:
978 <                default_depth = VDEPTH_16BIT;
978 >                default_depth = VIDEO_DEPTH_16BIT;
979                  break;
980          case 24: case 32:
981 <                default_depth = VDEPTH_32BIT;
981 >                default_depth = VIDEO_DEPTH_32BIT;
982                  break;
983          default:
984 <                default_depth =  VDEPTH_1BIT;
984 >                default_depth =  VIDEO_DEPTH_1BIT;
985                  break;
986          }
987  
988          // Construct list of supported modes
989          if (display_type == DISPLAY_WINDOW) {
990                  if (classic)
991 <                        add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
991 >                        add_mode(display_type, 512, 342, 0x80, 64, VIDEO_DEPTH_1BIT);
992                  else {
993 <                        for (int d = VDEPTH_1BIT; d <= default_depth; d++) {
994 <                                int bpp = (d <= VDEPTH_8BIT ? 8 : sdl_depth_of_video_depth(d));
993 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
994 >                                int bpp = sdl_depth_of_video_depth(d);
995                                  if (SDL_VideoModeOK(max_width, max_height, bpp, SDL_HWSURFACE))
996                                          add_window_modes(video_depth(d));
997                          }
998                  }
999          } else
1000 <                add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
1000 >                add_mode(display_type, default_width, default_height, 0x80, TrivialBytesPerRow(default_width, (video_depth)default_depth), default_depth);
1001          if (VideoModes.empty()) {
1002                  ErrorAlert(STR_NO_XVISUAL_ERR);
1003                  return false;
# Line 701 | Line 1005 | bool VideoInit(bool classic)
1005  
1006          // Find requested default mode with specified dimensions
1007          uint32 default_id;
1008 <        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1008 >        std::vector<VIDEO_MODE>::const_iterator i, end = VideoModes.end();
1009          for (i = VideoModes.begin(); i != end; ++i) {
1010 <                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1011 <                        default_id = i->resolution_id;
1010 >                const VIDEO_MODE & mode = (*i);
1011 >                if (VIDEO_MODE_X == default_width && VIDEO_MODE_Y == default_height && VIDEO_MODE_DEPTH == default_depth) {
1012 >                        default_id = VIDEO_MODE_RESOLUTION;
1013 > #ifdef SHEEPSHAVER
1014 >                        std::vector<VIDEO_MODE>::const_iterator begin = VideoModes.begin();
1015 >                        cur_mode = distance(begin, i);
1016 > #endif
1017                          break;
1018                  }
1019          }
1020          if (i == end) { // not found, use first available mode
1021 <                default_depth = VideoModes[0].depth;
1022 <                default_id = VideoModes[0].resolution_id;
1021 >                const VIDEO_MODE & mode = VideoModes[0];
1022 >                default_depth = VIDEO_MODE_DEPTH;
1023 >                default_id = VIDEO_MODE_RESOLUTION;
1024 > #ifdef SHEEPSHAVER
1025 >                cur_mode = 0;
1026 > #endif
1027          }
1028  
1029 + #ifdef SHEEPSHAVER
1030 +        for (int i = 0; i < VideoModes.size(); i++)
1031 +                VModes[i] = VideoModes[i];
1032 +        VideoInfo *p = &VModes[VideoModes.size()];
1033 +        p->viType = DIS_INVALID;        // End marker
1034 +        p->viRowBytes = 0;
1035 +        p->viXsize = p->viYsize = 0;
1036 +        p->viAppleMode = 0;
1037 +        p->viAppleID = 0;
1038 + #endif
1039 +
1040   #if DEBUG
1041          D(bug("Available video modes:\n"));
1042          for (i = VideoModes.begin(); i != end; ++i) {
1043 <                int bits = 1 << i->depth;
1043 >                const VIDEO_MODE & mode = (*i);
1044 >                int bits = 1 << VIDEO_MODE_DEPTH;
1045                  if (bits == 16)
1046                          bits = 15;
1047                  else if (bits == 32)
1048                          bits = 24;
1049 <                D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1049 >                D(bug(" %dx%d (ID %02x), %d colors\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << bits));
1050          }
1051   #endif
1052  
1053          // Create SDL_monitor_desc for this (the only) display
1054 <        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, default_depth, default_id);
1054 >        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, (video_depth)default_depth, default_id);
1055          VideoMonitors.push_back(monitor);
1056  
1057          // Open display
# Line 746 | Line 1071 | void SDL_monitor_desc::video_close(void)
1071          // Stop redraw thread
1072          if (redraw_thread_active) {
1073                  redraw_thread_cancel = true;
1074 < //              SDL_WaitThread(redraw_thread, NULL); doesn't work
750 <                while (redraw_thread_cancel);
1074 >                SDL_WaitThread(redraw_thread, NULL);
1075          }
1076          redraw_thread_active = false;
1077  
# Line 755 | Line 1079 | void SDL_monitor_desc::video_close(void)
1079          UNLOCK_FRAME_BUFFER;
1080          D(bug(" frame buffer unlocked\n"));
1081  
758 #ifdef ENABLE_VOSF
759        if (use_vosf) {
760                // Deinitialize VOSF
761                video_vosf_exit();
762        }
763 #endif
764
1082          // Close display
1083          delete drv;
1084          drv = NULL;
# Line 797 | Line 1114 | void VideoQuitFullScreen(void)
1114   *  Mac VBL interrupt
1115   */
1116  
1117 + /*
1118 + *  Execute video VBL routine
1119 + */
1120 +
1121 + #ifdef SHEEPSHAVER
1122 + void VideoVBL(void)
1123 + {
1124 +        // Emergency quit requested? Then quit
1125 +        if (emerg_quit)
1126 +                QuitEmulator();
1127 +
1128 +        // Temporarily give up frame buffer lock (this is the point where
1129 +        // we are suspended when the user presses Ctrl-Tab)
1130 +        UNLOCK_FRAME_BUFFER;
1131 +        LOCK_FRAME_BUFFER;
1132 +
1133 +        // Execute video VBL
1134 +        if (private_data != NULL && private_data->interruptsEnabled)
1135 +                VSLDoInterruptService(private_data->vslServiceID);
1136 + }
1137 + #else
1138   void VideoInterrupt(void)
1139   {
1140 +        // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
1141 +        SDL_PumpEvents();
1142 +
1143          // Emergency quit requested? Then quit
1144          if (emerg_quit)
1145                  QuitEmulator();
# Line 808 | Line 1149 | void VideoInterrupt(void)
1149          UNLOCK_FRAME_BUFFER;
1150          LOCK_FRAME_BUFFER;
1151   }
1152 + #endif
1153  
1154  
1155   /*
1156   *  Set palette
1157   */
1158  
1159 + #ifdef SHEEPSHAVER
1160 + void video_set_palette(void)
1161 + {
1162 +        monitor_desc * monitor = VideoMonitors[0];
1163 +        int n_colors = palette_size(monitor->get_current_mode().viAppleMode);
1164 +        uint8 pal[256 * 3];
1165 +        for (int c = 0; c < n_colors; c++) {
1166 +                pal[c*3 + 0] = mac_pal[c].red;
1167 +                pal[c*3 + 1] = mac_pal[c].green;
1168 +                pal[c*3 + 2] = mac_pal[c].blue;
1169 +        }
1170 +        monitor->set_palette(pal, n_colors);
1171 + }
1172 + #endif
1173 +
1174   void SDL_monitor_desc::set_palette(uint8 *pal, int num_in)
1175   {
1176 <        const video_mode &mode = get_current_mode();
1176 >        const VIDEO_MODE &mode = get_current_mode();
1177  
1178          // FIXME: how can we handle the gamma ramp?
1179 <        if (mode.depth > VDEPTH_8BIT)
1179 >        if ((int)VIDEO_MODE_DEPTH > VIDEO_DEPTH_8BIT)
1180                  return;
1181  
1182          LOCK_PALETTE;
# Line 844 | Line 1201 | void SDL_monitor_desc::set_palette(uint8
1201                  }
1202  
1203   #ifdef ENABLE_VOSF
1204 <                // We have to redraw everything because the interpretation of pixel values changed
1205 <                LOCK_VOSF;
1206 <                PFLAG_SET_ALL;
1207 <                UNLOCK_VOSF;
1208 <                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1204 >                if (use_vosf) {
1205 >                        // We have to redraw everything because the interpretation of pixel values changed
1206 >                        LOCK_VOSF;
1207 >                        PFLAG_SET_ALL;
1208 >                        UNLOCK_VOSF;
1209 >                        memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1210 >                }
1211   #endif
1212          }
1213  
# Line 863 | Line 1222 | void SDL_monitor_desc::set_palette(uint8
1222   *  Switch video mode
1223   */
1224  
1225 + #ifdef SHEEPSHAVER
1226 + int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1227 + {
1228 +        /* return if no mode change */
1229 +        if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1230 +            (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
1231 +
1232 +        /* first find video mode in table */
1233 +        for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
1234 +                if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
1235 +                    (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
1236 +                        csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
1237 +                        csSave->saveData = ReadMacInt32(ParamPtr + csData);
1238 +                        csSave->savePage = ReadMacInt16(ParamPtr + csPage);
1239 +
1240 +                        // Disable interrupts and pause redraw thread
1241 +                        DisableInterrupt();
1242 +                        thread_stop_ack = false;
1243 +                        thread_stop_req = true;
1244 +                        while (!thread_stop_ack) ;
1245 +
1246 +                        cur_mode = i;
1247 +                        monitor_desc *monitor = VideoMonitors[0];
1248 +                        monitor->switch_to_current_mode();
1249 +
1250 +                        WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
1251 +                        csSave->saveBaseAddr=screen_base;
1252 +                        csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
1253 +                        csSave->saveMode=VModes[cur_mode].viAppleMode;
1254 +
1255 +                        // Enable interrupts and resume redraw thread
1256 +                        thread_stop_req = false;
1257 +                        EnableInterrupt();
1258 +                        return noErr;
1259 +                }
1260 +        }
1261 +        return paramErr;
1262 + }
1263 + #endif
1264 +
1265   void SDL_monitor_desc::switch_to_current_mode(void)
1266   {
1267          // Close and reopen display
# Line 877 | Line 1276 | void SDL_monitor_desc::switch_to_current
1276  
1277  
1278   /*
1279 < *  Translate key event to Mac keycode, returns -1 if no keycode was found
1280 < *  and -2 if the key was recognized as a hotkey
1279 > *  Can we set the MacOS cursor image into the window?
1280 > */
1281 >
1282 > #ifdef SHEEPSHAVER
1283 > bool video_can_change_cursor(void)
1284 > {
1285 >        return (display_type == DISPLAY_WINDOW);
1286 > }
1287 > #endif
1288 >
1289 >
1290 > /*
1291 > *  Set cursor image for window
1292 > */
1293 >
1294 > #ifdef SHEEPSHAVER
1295 > void video_set_cursor(void)
1296 > {
1297 >        cursor_changed = true;
1298 > }
1299 > #endif
1300 >
1301 >
1302 > /*
1303 > *  Keyboard-related utilify functions
1304   */
1305  
1306 + static bool is_modifier_key(SDL_KeyboardEvent const & e)
1307 + {
1308 +        switch (e.keysym.sym) {
1309 +        case SDLK_NUMLOCK:
1310 +        case SDLK_CAPSLOCK:
1311 +        case SDLK_SCROLLOCK:
1312 +        case SDLK_RSHIFT:
1313 +        case SDLK_LSHIFT:
1314 +        case SDLK_RCTRL:
1315 +        case SDLK_LCTRL:
1316 +        case SDLK_RALT:
1317 +        case SDLK_LALT:
1318 +        case SDLK_RMETA:
1319 +        case SDLK_LMETA:
1320 +        case SDLK_LSUPER:
1321 +        case SDLK_RSUPER:
1322 +        case SDLK_MODE:
1323 +        case SDLK_COMPOSE:
1324 +                return true;
1325 +        }
1326 +        return false;
1327 + }
1328 +
1329   static bool is_ctrl_down(SDL_keysym const & ks)
1330   {
1331          return ctrl_down || (ks.mod & KMOD_CTRL);
1332   }
1333  
1334 +
1335 + /*
1336 + *  Translate key event to Mac keycode, returns -1 if no keycode was found
1337 + *  and -2 if the key was recognized as a hotkey
1338 + */
1339 +
1340   static int kc_decode(SDL_keysym const & ks, bool key_down)
1341   {
1342          switch (ks.sym) {
# Line 955 | Line 1406 | static int kc_decode(SDL_keysym const &
1406          case SDLK_RCTRL: return 0x36;
1407          case SDLK_LSHIFT: return 0x38;
1408          case SDLK_RSHIFT: return 0x38;
1409 + #if (defined(__APPLE__) && defined(__MACH__))
1410 +        case SDLK_LALT: return 0x3a;
1411 +        case SDLK_RALT: return 0x3a;
1412 +        case SDLK_LMETA: return 0x37;
1413 +        case SDLK_RMETA: return 0x37;
1414 + #else
1415          case SDLK_LALT: return 0x37;
1416          case SDLK_RALT: return 0x37;
1417          case SDLK_LMETA: return 0x3a;
1418          case SDLK_RMETA: return 0x3a;
1419 + #endif
1420          case SDLK_MENU: return 0x32;
1421          case SDLK_CAPSLOCK: return 0x39;
1422          case SDLK_NUMLOCK: return 0x47;
# Line 1021 | Line 1479 | static int event2keycode(SDL_KeyboardEve
1479  
1480   static void handle_events(void)
1481   {
1482 <        SDL_Event event;
1483 <        while (SDL_PollEvent(&event)) {
1484 <                switch (event.type) {
1482 >        SDL_Event events[10];
1483 >        const int n_max_events = sizeof(events) / sizeof(events[0]);
1484 >        int n_events;
1485 >
1486 >        while ((n_events = SDL_PeepEvents(events, n_max_events, SDL_GETEVENT, sdl_eventmask)) > 0) {
1487 >                for (int i = 0; i < n_events; i++) {
1488 >                        SDL_Event const & event = events[i];
1489 >                        switch (event.type) {
1490  
1491                          // Mouse button
1492                          case SDL_MOUSEBUTTONDOWN: {
# Line 1060 | Line 1523 | static void handle_events(void)
1523                          // Keyboard
1524                          case SDL_KEYDOWN: {
1525                                  int code = -1;
1526 <                                if (use_keycodes) {
1526 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1527                                          if (event2keycode(event.key, true) != -2)       // This is called to process the hotkeys
1528                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1529                                  } else
# Line 1088 | Line 1551 | static void handle_events(void)
1551                          }
1552                          case SDL_KEYUP: {
1553                                  int code = -1;
1554 <                                if (use_keycodes) {
1554 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1555                                          if (event2keycode(event.key, false) != -2)      // This is called to process the hotkeys
1556                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1557                                  } else
1558                                          code = event2keycode(event.key, false);
1559 <                                if (code >= 0 && code != 0x39) {        // Don't propagate Caps Lock releases
1560 <                                        ADBKeyUp(code);
1559 >                                if (code >= 0) {
1560 >                                        if (code == 0x39) {     // Caps Lock released
1561 >                                                if (caps_on) {
1562 >                                                        ADBKeyUp(code);
1563 >                                                        caps_on = false;
1564 >                                                } else {
1565 >                                                        ADBKeyDown(code);
1566 >                                                        caps_on = true;
1567 >                                                }
1568 >                                        } else
1569 >                                                ADBKeyUp(code);
1570                                          if (code == 0x36)
1571                                                  ctrl_down = false;
1572                                  }
# Line 1104 | Line 1576 | static void handle_events(void)
1576                          // Hidden parts exposed, force complete refresh of window
1577                          case SDL_VIDEOEXPOSE:
1578                                  if (display_type == DISPLAY_WINDOW) {
1579 <                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
1579 >                                        const VIDEO_MODE &mode = VideoMonitors[0]->get_current_mode();
1580   #ifdef ENABLE_VOSF
1581                                          if (use_vosf) {                 // VOSF refresh
1582                                                  LOCK_VOSF;
1583                                                  PFLAG_SET_ALL;
1584                                                  UNLOCK_VOSF;
1585 <                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1585 >                                                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1586                                          }
1587                                          else
1588   #endif
1589 <                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1589 >                                                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1590                                  }
1591                                  break;
1592  
# Line 1123 | Line 1595 | static void handle_events(void)
1595                                  ADBKeyDown(0x7f);       // Power key
1596                                  ADBKeyUp(0x7f);
1597                                  break;
1598 +                        }
1599                  }
1600          }
1601   }
# Line 1137 | Line 1610 | static void update_display_static(driver
1610   {
1611          // Incremental update code
1612          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1613 <        const video_mode &mode = drv->mode;
1614 <        int bytes_per_row = mode.bytes_per_row;
1613 >        const VIDEO_MODE &mode = drv->mode;
1614 >        int bytes_per_row = VIDEO_MODE_ROW_BYTES;
1615          uint8 *p, *p2;
1616  
1617          // Check for first line from top and first line from bottom that have changed
1618          y1 = 0;
1619 <        for (j=0; j<mode.y; j++) {
1619 >        for (j=0; j<VIDEO_MODE_Y; j++) {
1620                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1621                          y1 = j;
1622                          break;
1623                  }
1624          }
1625          y2 = y1 - 1;
1626 <        for (j=mode.y-1; j>=y1; j--) {
1626 >        for (j=VIDEO_MODE_Y-1; j>=y1; j--) {
1627                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1628                          y2 = j;
1629                          break;
# Line 1160 | Line 1633 | static void update_display_static(driver
1633  
1634          // Check for first column from left and first column from right that have changed
1635          if (high) {
1636 <                if (mode.depth < VDEPTH_8BIT) {
1636 >                if ((int)VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
1637                          const int src_bytes_per_row = bytes_per_row;
1638                          const int dst_bytes_per_row = drv->s->pitch;
1639 <                        const int pixels_per_byte = mode.x / src_bytes_per_row;
1639 >                        const int pixels_per_byte = VIDEO_MODE_X / src_bytes_per_row;
1640  
1641 <                        x1 = mode.x / pixels_per_byte;
1641 >                        x1 = VIDEO_MODE_X / pixels_per_byte;
1642                          for (j = y1; j <= y2; j++) {
1643                                  p = &the_buffer[j * bytes_per_row];
1644                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1183 | Line 1656 | static void update_display_static(driver
1656                                  p2 = &the_buffer_copy[j * bytes_per_row];
1657                                  p += bytes_per_row;
1658                                  p2 += bytes_per_row;
1659 <                                for (i = (mode.x / pixels_per_byte); i > x2; i--) {
1659 >                                for (i = (VIDEO_MODE_X / pixels_per_byte); i > x2; i--) {
1660                                          p--; p2--;
1661                                          if (*p != *p2) {
1662                                                  x2 = i;
# Line 1221 | Line 1694 | static void update_display_static(driver
1694                          }
1695  
1696                  } else {
1697 <                        const int bytes_per_pixel = mode.bytes_per_row / mode.x;
1697 >                        const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
1698  
1699 <                        x1 = mode.x;
1699 >                        x1 = VIDEO_MODE_X;
1700                          for (j=y1; j<=y2; j++) {
1701                                  p = &the_buffer[j * bytes_per_row];
1702                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1241 | Line 1714 | static void update_display_static(driver
1714                                  p2 = &the_buffer_copy[j * bytes_per_row];
1715                                  p += bytes_per_row;
1716                                  p2 += bytes_per_row;
1717 <                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1717 >                                for (i=VIDEO_MODE_X*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1718                                          p--;
1719                                          p2--;
1720                                          if (*p != *p2) {
# Line 1401 | Line 1874 | static void VideoRefreshInit(void)
1874          }
1875   }
1876  
1877 + const int VIDEO_REFRESH_HZ = 60;
1878 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
1879 +
1880   static int redraw_func(void *arg)
1881   {
1882          uint64 start = GetTicks_usec();
1883          int64 ticks = 0;
1884 +        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
1885  
1886          while (!redraw_thread_cancel) {
1887  
1888                  // Wait
1889 <                Delay_usec(16667);
1889 >                next += VIDEO_REFRESH_DELAY;
1890 >                int64 delay = next - GetTicks_usec();
1891 >                if (delay > 0)
1892 >                        Delay_usec(delay);
1893 >                else if (delay < -VIDEO_REFRESH_DELAY)
1894 >                        next = GetTicks_usec();
1895 >                ticks++;
1896 >
1897 > #ifdef SHEEPSHAVER
1898 >                // Pause if requested (during video mode switches)
1899 >                if (thread_stop_req) {
1900 >                        thread_stop_ack = true;
1901 >                        continue;
1902 >                }
1903 > #endif
1904  
1905                  // Handle SDL events
1906                  handle_events();
1907  
1908                  // Refresh display
1909                  video_refresh();
1910 <                ticks++;
1910 >
1911 > #ifdef SHEEPSHAVER
1912 >                // Set new cursor image if it was changed
1913 >                if (cursor_changed && sdl_cursor) {
1914 >                        cursor_changed = false;
1915 >                        SDL_FreeCursor(sdl_cursor);
1916 >                        sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
1917 >                        if (sdl_cursor)
1918 >                                SDL_SetCursor(sdl_cursor);
1919 >                }
1920 > #endif
1921  
1922                  // Set new palette if it was changed
1923                  handle_palette_changes();
# Line 1424 | Line 1925 | static int redraw_func(void *arg)
1925  
1926          uint64 end = GetTicks_usec();
1927          D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1427        redraw_thread_cancel = false;
1928          return 0;
1929   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines