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.2 by gbeauche, 2004-06-23T22:37:33Z vs.
Revision 1.13 by gbeauche, 2004-11-28T19:31:11Z

# Line 35 | Line 35
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 43 | 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 51 | 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 75 | Line 90 | static int32 frame_skip;                                                       // Prefs
90   static int16 mouse_wheel_mode;
91   static int16 mouse_wheel_lines;
92  
78 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 83 | 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 103 | 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;
# Line 130 | 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 150 | 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 179 | 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 219 | 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 270 | 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 322 | 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) {
327                if (the_buffer) {
328                        free(the_buffer);
329                        the_buffer = NULL;
330                }
593                  if (the_buffer_copy) {
594                          free(the_buffer_copy);
595                          the_buffer_copy = NULL;
# Line 335 | Line 597 | driver_base::~driver_base()
597          }
598   #ifdef ENABLE_VOSF
599          else {
338                // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
339                if (the_buffer != VM_MAP_FAILED) {
340                        D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
341                        vm_release(the_buffer, the_buffer_size);
342                        the_buffer = NULL;
343                }
600                  if (the_host_buffer) {
601                          D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
602                          free(the_host_buffer);
# Line 351 | 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 358 | 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 383 | 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 391 | 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 400 | 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;
409 <        the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
410 <        the_buffer = (uint8 *)calloc(1, the_buffer_size);
411 <        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  
417        // Hide cursor
418        SDL_ShowCursor(0);
419
704          // Init blitting routines
705          SDL_PixelFormat *f = s->format;
706          VisualFormat visualFormat;
# Line 424 | 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 432 | 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 535 | Line 819 | static void keycode_init(void)
819  
820                          if (video_driver_found) {
821                                  // Skip aliases
822 <                                static const char alias_str[] = "alias";
823 <                                if (strncmp(line, alias_str, sizeof(alias_str) - 1) == 0)
822 >                                static const char sdl_str[] = "sdl";
823 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0)
824                                          continue;
825  
826                                  // Read keycode
# Line 547 | Line 831 | static void keycode_init(void)
831                                          break;
832                          } else {
833                                  // Search for SDL video driver string
834 <                                static const char alias_sdl_str[] = "alias SDL";
835 <                                if (strncmp(line, alias_sdl_str, sizeof(alias_sdl_str) - 1) == 0) {
836 <                                        char *p = line + sizeof(alias_sdl_str);
834 >                                static const char sdl_str[] = "sdl";
835 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0) {
836 >                                        char *p = line + sizeof(sdl_str);
837                                          if (strstr(video_driver, p) == video_driver)
838                                                  video_driver_found = true;
839                                  }
# Line 574 | Line 858 | static void keycode_init(void)
858   bool SDL_monitor_desc::video_open(void)
859   {
860          D(bug("video_open()\n"));
861 <        const video_mode &mode = get_current_mode();
861 >        const VIDEO_MODE &mode = get_current_mode();
862 > #if DEBUG
863 >        D(bug("Current video mode:\n"));
864 >        D(bug(" %dx%d (ID %02x), %d bpp\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << (VIDEO_MODE_DEPTH & 0x0f)));
865 > #endif
866  
867          // Create display driver object of requested type
868          switch (display_type) {
# Line 590 | Line 878 | bool SDL_monitor_desc::video_open(void)
878                  return false;
879          }
880  
593 #ifdef ENABLE_VOSF
594        if (use_vosf) {
595                // Initialize the VOSF system
596                if (!video_vosf_init(*this)) {
597                        ErrorAlert(STR_VOSF_INIT_ERR);
598                return false;
599                }
600        }
601 #endif
602        
881          // Initialize VideoRefresh function
882          VideoRefreshInit();
883  
# Line 608 | Line 886 | bool SDL_monitor_desc::video_open(void)
886  
887          // Start redraw/input thread
888          redraw_thread_cancel = false;
889 <        redraw_thread_active = (SDL_CreateThread(redraw_func, NULL) != NULL);
889 >        redraw_thread_active = ((redraw_thread = SDL_CreateThread(redraw_func, NULL)) != NULL);
890          if (!redraw_thread_active) {
891                  printf("FATAL: cannot create redraw thread\n");
892                  return false;
# Line 616 | Line 894 | bool SDL_monitor_desc::video_open(void)
894          return true;
895   }
896  
897 + #ifdef SHEEPSHAVER
898 + bool VideoInit(void)
899 + {
900 +        const bool classic = false;
901 + #else
902   bool VideoInit(bool classic)
903   {
904 + #endif
905          classic_mode = classic;
906  
907   #ifdef ENABLE_VOSF
# Line 641 | Line 925 | bool VideoInit(bool classic)
925          mouse_wheel_lines = PrefsFindInt32("mousewheellines");
926  
927          // Get screen mode from preferences
928 <        const char *mode_str;
928 >        const char *mode_str = NULL;
929 > #ifndef SHEEPSHAVER
930          if (classic_mode)
931                  mode_str = "win/512/342";
932          else
933                  mode_str = PrefsFindString("screen");
934 + #endif
935  
936          // Determine display type and default dimensions
937 <        int default_width = 512, default_height = 384;
937 >        int default_width, default_height;
938 >        if (classic) {
939 >                default_width = 512;
940 >                default_height = 384;
941 >        }
942 >        else {
943 >                default_width = 640;
944 >                default_height = 480;
945 >        }
946          display_type = DISPLAY_WINDOW;
947          if (mode_str) {
948                  if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
949                          display_type = DISPLAY_WINDOW;
950          }
951          int max_width = 640, max_height = 480;
952 <        if (display_type == DISPLAY_SCREEN) {
953 <                SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
954 <                if (modes && modes != (SDL_Rect **)-1) {
955 <                        max_width = modes[0]->w;
956 <                        max_height = modes[0]->h;
957 <                }
952 >        SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
953 >        if (modes && modes != (SDL_Rect **)-1) {
954 >                max_width = modes[0]->w;
955 >                max_height = modes[0]->h;
956 >                if (default_width > max_width)
957 >                        default_width = max_width;
958 >                if (default_height > max_height)
959 >                        default_height = max_height;
960          }
961          if (default_width <= 0)
962                  default_width = max_width;
# Line 669 | Line 965 | bool VideoInit(bool classic)
965  
966          // Mac screen depth follows X depth
967          screen_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
968 <        video_depth default_depth;
968 >        int default_depth;
969          switch (screen_depth) {
970          case 8:
971 <                default_depth = VDEPTH_8BIT;
971 >                default_depth = VIDEO_DEPTH_8BIT;
972                  break;
973          case 15: case 16:
974 <                default_depth = VDEPTH_16BIT;
974 >                default_depth = VIDEO_DEPTH_16BIT;
975                  break;
976          case 24: case 32:
977 <                default_depth = VDEPTH_32BIT;
977 >                default_depth = VIDEO_DEPTH_32BIT;
978                  break;
979          default:
980 <                default_depth =  VDEPTH_1BIT;
980 >                default_depth =  VIDEO_DEPTH_1BIT;
981                  break;
982          }
983  
984          // Construct list of supported modes
985          if (display_type == DISPLAY_WINDOW) {
986                  if (classic)
987 <                        add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
987 >                        add_mode(display_type, 512, 342, 0x80, 64, VIDEO_DEPTH_1BIT);
988                  else {
989 <                        for (int d = VDEPTH_1BIT; d <= default_depth; d++) {
990 <                                int bpp = (d <= VDEPTH_8BIT ? 8 : sdl_depth_of_video_depth(d));
989 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
990 >                                int bpp = sdl_depth_of_video_depth(d);
991                                  if (SDL_VideoModeOK(max_width, max_height, bpp, SDL_HWSURFACE))
992                                          add_window_modes(video_depth(d));
993                          }
994                  }
995          } else
996 <                add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
996 >                add_mode(display_type, default_width, default_height, 0x80, TrivialBytesPerRow(default_width, (video_depth)default_depth), default_depth);
997          if (VideoModes.empty()) {
998                  ErrorAlert(STR_NO_XVISUAL_ERR);
999                  return false;
# Line 705 | Line 1001 | bool VideoInit(bool classic)
1001  
1002          // Find requested default mode with specified dimensions
1003          uint32 default_id;
1004 <        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1004 >        std::vector<VIDEO_MODE>::const_iterator i, end = VideoModes.end();
1005          for (i = VideoModes.begin(); i != end; ++i) {
1006 <                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1007 <                        default_id = i->resolution_id;
1006 >                const VIDEO_MODE & mode = (*i);
1007 >                if (VIDEO_MODE_X == default_width && VIDEO_MODE_Y == default_height && VIDEO_MODE_DEPTH == default_depth) {
1008 >                        default_id = VIDEO_MODE_RESOLUTION;
1009 > #ifdef SHEEPSHAVER
1010 >                        std::vector<VIDEO_MODE>::const_iterator begin = VideoModes.begin();
1011 >                        cur_mode = distance(begin, i);
1012 > #endif
1013                          break;
1014                  }
1015          }
1016          if (i == end) { // not found, use first available mode
1017 <                default_depth = VideoModes[0].depth;
1018 <                default_id = VideoModes[0].resolution_id;
1017 >                const VIDEO_MODE & mode = VideoModes[0];
1018 >                default_depth = VIDEO_MODE_DEPTH;
1019 >                default_id = VIDEO_MODE_RESOLUTION;
1020 > #ifdef SHEEPSHAVER
1021 >                cur_mode = 0;
1022 > #endif
1023          }
1024  
1025 + #ifdef SHEEPSHAVER
1026 +        for (int i = 0; i < VideoModes.size(); i++)
1027 +                VModes[i] = VideoModes[i];
1028 +        VideoInfo *p = &VModes[VideoModes.size()];
1029 +        p->viType = DIS_INVALID;        // End marker
1030 +        p->viRowBytes = 0;
1031 +        p->viXsize = p->viYsize = 0;
1032 +        p->viAppleMode = 0;
1033 +        p->viAppleID = 0;
1034 + #endif
1035 +
1036   #if DEBUG
1037          D(bug("Available video modes:\n"));
1038          for (i = VideoModes.begin(); i != end; ++i) {
1039 <                int bits = 1 << i->depth;
1039 >                const VIDEO_MODE & mode = (*i);
1040 >                int bits = 1 << VIDEO_MODE_DEPTH;
1041                  if (bits == 16)
1042                          bits = 15;
1043                  else if (bits == 32)
1044                          bits = 24;
1045 <                D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1045 >                D(bug(" %dx%d (ID %02x), %d colors\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << bits));
1046          }
1047   #endif
1048  
1049          // Create SDL_monitor_desc for this (the only) display
1050 <        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, default_depth, default_id);
1050 >        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, (video_depth)default_depth, default_id);
1051          VideoMonitors.push_back(monitor);
1052  
1053          // Open display
# Line 750 | Line 1067 | void SDL_monitor_desc::video_close(void)
1067          // Stop redraw thread
1068          if (redraw_thread_active) {
1069                  redraw_thread_cancel = true;
1070 < //              SDL_WaitThread(redraw_thread, NULL); doesn't work
754 <                while (redraw_thread_cancel);
1070 >                SDL_WaitThread(redraw_thread, NULL);
1071          }
1072          redraw_thread_active = false;
1073  
# Line 759 | Line 1075 | void SDL_monitor_desc::video_close(void)
1075          UNLOCK_FRAME_BUFFER;
1076          D(bug(" frame buffer unlocked\n"));
1077  
762 #ifdef ENABLE_VOSF
763        if (use_vosf) {
764                // Deinitialize VOSF
765                video_vosf_exit();
766        }
767 #endif
768
1078          // Close display
1079          delete drv;
1080          drv = NULL;
# Line 801 | Line 1110 | void VideoQuitFullScreen(void)
1110   *  Mac VBL interrupt
1111   */
1112  
1113 + /*
1114 + *  Execute video VBL routine
1115 + */
1116 +
1117 + #ifdef SHEEPSHAVER
1118 + void VideoVBL(void)
1119 + {
1120 +        // Emergency quit requested? Then quit
1121 +        if (emerg_quit)
1122 +                QuitEmulator();
1123 +
1124 +        // Temporarily give up frame buffer lock (this is the point where
1125 +        // we are suspended when the user presses Ctrl-Tab)
1126 +        UNLOCK_FRAME_BUFFER;
1127 +        LOCK_FRAME_BUFFER;
1128 +
1129 +        // Execute video VBL
1130 +        if (private_data != NULL && private_data->interruptsEnabled)
1131 +                VSLDoInterruptService(private_data->vslServiceID);
1132 + }
1133 + #else
1134   void VideoInterrupt(void)
1135   {
1136          // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
# Line 815 | Line 1145 | void VideoInterrupt(void)
1145          UNLOCK_FRAME_BUFFER;
1146          LOCK_FRAME_BUFFER;
1147   }
1148 + #endif
1149  
1150  
1151   /*
1152   *  Set palette
1153   */
1154  
1155 + #ifdef SHEEPSHAVER
1156 + void video_set_palette(void)
1157 + {
1158 +        monitor_desc * monitor = VideoMonitors[0];
1159 +        int n_colors = palette_size(monitor->get_current_mode().viAppleMode);
1160 +        uint8 pal[256 * 3];
1161 +        for (int c = 0; c < n_colors; c++) {
1162 +                pal[c*3 + 0] = mac_pal[c].red;
1163 +                pal[c*3 + 1] = mac_pal[c].green;
1164 +                pal[c*3 + 2] = mac_pal[c].blue;
1165 +        }
1166 +        monitor->set_palette(pal, n_colors);
1167 + }
1168 + #endif
1169 +
1170   void SDL_monitor_desc::set_palette(uint8 *pal, int num_in)
1171   {
1172 <        const video_mode &mode = get_current_mode();
1172 >        const VIDEO_MODE &mode = get_current_mode();
1173  
1174          // FIXME: how can we handle the gamma ramp?
1175 <        if (mode.depth > VDEPTH_8BIT)
1175 >        if ((int)VIDEO_MODE_DEPTH > VIDEO_DEPTH_8BIT)
1176                  return;
1177  
1178          LOCK_PALETTE;
# Line 851 | Line 1197 | void SDL_monitor_desc::set_palette(uint8
1197                  }
1198  
1199   #ifdef ENABLE_VOSF
1200 <                // We have to redraw everything because the interpretation of pixel values changed
1201 <                LOCK_VOSF;
1202 <                PFLAG_SET_ALL;
1203 <                UNLOCK_VOSF;
1204 <                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1200 >                if (use_vosf) {
1201 >                        // We have to redraw everything because the interpretation of pixel values changed
1202 >                        LOCK_VOSF;
1203 >                        PFLAG_SET_ALL;
1204 >                        UNLOCK_VOSF;
1205 >                        memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1206 >                }
1207   #endif
1208          }
1209  
# Line 870 | Line 1218 | void SDL_monitor_desc::set_palette(uint8
1218   *  Switch video mode
1219   */
1220  
1221 + #ifdef SHEEPSHAVER
1222 + int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1223 + {
1224 +        /* return if no mode change */
1225 +        if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1226 +            (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
1227 +
1228 +        /* first find video mode in table */
1229 +        for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
1230 +                if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
1231 +                    (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
1232 +                        csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
1233 +                        csSave->saveData = ReadMacInt32(ParamPtr + csData);
1234 +                        csSave->savePage = ReadMacInt16(ParamPtr + csPage);
1235 +
1236 +                        // Disable interrupts and pause redraw thread
1237 +                        DisableInterrupt();
1238 +                        thread_stop_ack = false;
1239 +                        thread_stop_req = true;
1240 +                        while (!thread_stop_ack) ;
1241 +
1242 +                        cur_mode = i;
1243 +                        monitor_desc *monitor = VideoMonitors[0];
1244 +                        monitor->switch_to_current_mode();
1245 +
1246 +                        WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
1247 +                        csSave->saveBaseAddr=screen_base;
1248 +                        csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
1249 +                        csSave->saveMode=VModes[cur_mode].viAppleMode;
1250 +
1251 +                        // Enable interrupts and resume redraw thread
1252 +                        thread_stop_req = false;
1253 +                        EnableInterrupt();
1254 +                        return noErr;
1255 +                }
1256 +        }
1257 +        return paramErr;
1258 + }
1259 + #endif
1260 +
1261   void SDL_monitor_desc::switch_to_current_mode(void)
1262   {
1263          // Close and reopen display
# Line 884 | Line 1272 | void SDL_monitor_desc::switch_to_current
1272  
1273  
1274   /*
1275 < *  Translate key event to Mac keycode, returns -1 if no keycode was found
888 < *  and -2 if the key was recognized as a hotkey
1275 > *  Can we set the MacOS cursor image into the window?
1276   */
1277  
1278 + #ifdef SHEEPSHAVER
1279 + bool video_can_change_cursor(void)
1280 + {
1281 +        return (display_type == DISPLAY_WINDOW);
1282 + }
1283 + #endif
1284 +
1285 +
1286 + /*
1287 + *  Set cursor image for window
1288 + */
1289 +
1290 + #ifdef SHEEPSHAVER
1291 + void video_set_cursor(void)
1292 + {
1293 +        cursor_changed = true;
1294 + }
1295 + #endif
1296 +
1297 +
1298 + /*
1299 + *  Keyboard-related utilify functions
1300 + */
1301 +
1302 + static bool is_modifier_key(SDL_KeyboardEvent const & e)
1303 + {
1304 +        switch (e.keysym.sym) {
1305 +        case SDLK_NUMLOCK:
1306 +        case SDLK_CAPSLOCK:
1307 +        case SDLK_SCROLLOCK:
1308 +        case SDLK_RSHIFT:
1309 +        case SDLK_LSHIFT:
1310 +        case SDLK_RCTRL:
1311 +        case SDLK_LCTRL:
1312 +        case SDLK_RALT:
1313 +        case SDLK_LALT:
1314 +        case SDLK_RMETA:
1315 +        case SDLK_LMETA:
1316 +        case SDLK_LSUPER:
1317 +        case SDLK_RSUPER:
1318 +        case SDLK_MODE:
1319 +        case SDLK_COMPOSE:
1320 +                return true;
1321 +        }
1322 +        return false;
1323 + }
1324 +
1325   static bool is_ctrl_down(SDL_keysym const & ks)
1326   {
1327          return ctrl_down || (ks.mod & KMOD_CTRL);
1328   }
1329  
1330 +
1331 + /*
1332 + *  Translate key event to Mac keycode, returns -1 if no keycode was found
1333 + *  and -2 if the key was recognized as a hotkey
1334 + */
1335 +
1336   static int kc_decode(SDL_keysym const & ks, bool key_down)
1337   {
1338          switch (ks.sym) {
# Line 962 | Line 1402 | static int kc_decode(SDL_keysym const &
1402          case SDLK_RCTRL: return 0x36;
1403          case SDLK_LSHIFT: return 0x38;
1404          case SDLK_RSHIFT: return 0x38;
1405 + #if (defined(__APPLE__) && defined(__MACH__))
1406 +        case SDLK_LALT: return 0x3a;
1407 +        case SDLK_RALT: return 0x3a;
1408 +        case SDLK_LMETA: return 0x37;
1409 +        case SDLK_RMETA: return 0x37;
1410 + #else
1411          case SDLK_LALT: return 0x37;
1412          case SDLK_RALT: return 0x37;
1413          case SDLK_LMETA: return 0x3a;
1414          case SDLK_RMETA: return 0x3a;
1415 + #endif
1416          case SDLK_MENU: return 0x32;
1417          case SDLK_CAPSLOCK: return 0x39;
1418          case SDLK_NUMLOCK: return 0x47;
# Line 1072 | Line 1519 | static void handle_events(void)
1519                          // Keyboard
1520                          case SDL_KEYDOWN: {
1521                                  int code = -1;
1522 <                                if (use_keycodes) {
1522 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1523                                          if (event2keycode(event.key, true) != -2)       // This is called to process the hotkeys
1524                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1525                                  } else
# Line 1100 | Line 1547 | static void handle_events(void)
1547                          }
1548                          case SDL_KEYUP: {
1549                                  int code = -1;
1550 <                                if (use_keycodes) {
1550 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1551                                          if (event2keycode(event.key, false) != -2)      // This is called to process the hotkeys
1552                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1553                                  } else
1554                                          code = event2keycode(event.key, false);
1555 <                                if (code >= 0 && code != 0x39) {        // Don't propagate Caps Lock releases
1556 <                                        ADBKeyUp(code);
1555 >                                if (code >= 0) {
1556 >                                        if (code == 0x39) {     // Caps Lock released
1557 >                                                if (caps_on) {
1558 >                                                        ADBKeyUp(code);
1559 >                                                        caps_on = false;
1560 >                                                } else {
1561 >                                                        ADBKeyDown(code);
1562 >                                                        caps_on = true;
1563 >                                                }
1564 >                                        } else
1565 >                                                ADBKeyUp(code);
1566                                          if (code == 0x36)
1567                                                  ctrl_down = false;
1568                                  }
# Line 1116 | Line 1572 | static void handle_events(void)
1572                          // Hidden parts exposed, force complete refresh of window
1573                          case SDL_VIDEOEXPOSE:
1574                                  if (display_type == DISPLAY_WINDOW) {
1575 <                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
1575 >                                        const VIDEO_MODE &mode = VideoMonitors[0]->get_current_mode();
1576   #ifdef ENABLE_VOSF
1577                                          if (use_vosf) {                 // VOSF refresh
1578                                                  LOCK_VOSF;
1579                                                  PFLAG_SET_ALL;
1580                                                  UNLOCK_VOSF;
1581 <                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1581 >                                                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1582                                          }
1583                                          else
1584   #endif
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                                  break;
1588  
# Line 1150 | Line 1606 | static void update_display_static(driver
1606   {
1607          // Incremental update code
1608          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1609 <        const video_mode &mode = drv->mode;
1610 <        int bytes_per_row = mode.bytes_per_row;
1609 >        const VIDEO_MODE &mode = drv->mode;
1610 >        int bytes_per_row = VIDEO_MODE_ROW_BYTES;
1611          uint8 *p, *p2;
1612  
1613          // Check for first line from top and first line from bottom that have changed
1614          y1 = 0;
1615 <        for (j=0; j<mode.y; j++) {
1615 >        for (j=0; j<VIDEO_MODE_Y; j++) {
1616                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1617                          y1 = j;
1618                          break;
1619                  }
1620          }
1621          y2 = y1 - 1;
1622 <        for (j=mode.y-1; j>=y1; j--) {
1622 >        for (j=VIDEO_MODE_Y-1; j>=y1; j--) {
1623                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1624                          y2 = j;
1625                          break;
# Line 1173 | Line 1629 | static void update_display_static(driver
1629  
1630          // Check for first column from left and first column from right that have changed
1631          if (high) {
1632 <                if (mode.depth < VDEPTH_8BIT) {
1632 >                if ((int)VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
1633                          const int src_bytes_per_row = bytes_per_row;
1634                          const int dst_bytes_per_row = drv->s->pitch;
1635 <                        const int pixels_per_byte = mode.x / src_bytes_per_row;
1635 >                        const int pixels_per_byte = VIDEO_MODE_X / src_bytes_per_row;
1636  
1637 <                        x1 = mode.x / pixels_per_byte;
1637 >                        x1 = VIDEO_MODE_X / pixels_per_byte;
1638                          for (j = y1; j <= y2; j++) {
1639                                  p = &the_buffer[j * bytes_per_row];
1640                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1196 | Line 1652 | static void update_display_static(driver
1652                                  p2 = &the_buffer_copy[j * bytes_per_row];
1653                                  p += bytes_per_row;
1654                                  p2 += bytes_per_row;
1655 <                                for (i = (mode.x / pixels_per_byte); i > x2; i--) {
1655 >                                for (i = (VIDEO_MODE_X / pixels_per_byte); i > x2; i--) {
1656                                          p--; p2--;
1657                                          if (*p != *p2) {
1658                                                  x2 = i;
# Line 1234 | Line 1690 | static void update_display_static(driver
1690                          }
1691  
1692                  } else {
1693 <                        const int bytes_per_pixel = mode.bytes_per_row / mode.x;
1693 >                        const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
1694  
1695 <                        x1 = mode.x;
1695 >                        x1 = VIDEO_MODE_X;
1696                          for (j=y1; j<=y2; j++) {
1697                                  p = &the_buffer[j * bytes_per_row];
1698                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1254 | Line 1710 | static void update_display_static(driver
1710                                  p2 = &the_buffer_copy[j * bytes_per_row];
1711                                  p += bytes_per_row;
1712                                  p2 += bytes_per_row;
1713 <                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1713 >                                for (i=VIDEO_MODE_X*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1714                                          p--;
1715                                          p2--;
1716                                          if (*p != *p2) {
# Line 1414 | Line 1870 | static void VideoRefreshInit(void)
1870          }
1871   }
1872  
1873 + const int VIDEO_REFRESH_HZ = 60;
1874 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
1875 +
1876   static int redraw_func(void *arg)
1877   {
1878          uint64 start = GetTicks_usec();
1879          int64 ticks = 0;
1880 +        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
1881  
1882          while (!redraw_thread_cancel) {
1883  
1884                  // Wait
1885 <                Delay_usec(16667);
1885 >                next += VIDEO_REFRESH_DELAY;
1886 >                int64 delay = next - GetTicks_usec();
1887 >                if (delay > 0)
1888 >                        Delay_usec(delay);
1889 >                else if (delay < -VIDEO_REFRESH_DELAY)
1890 >                        next = GetTicks_usec();
1891 >                ticks++;
1892 >
1893 > #ifdef SHEEPSHAVER
1894 >                // Pause if requested (during video mode switches)
1895 >                if (thread_stop_req) {
1896 >                        thread_stop_ack = true;
1897 >                        continue;
1898 >                }
1899 > #endif
1900  
1901                  // Handle SDL events
1902                  handle_events();
1903  
1904                  // Refresh display
1905                  video_refresh();
1906 <                ticks++;
1906 >
1907 > #ifdef SHEEPSHAVER
1908 >                // Set new cursor image if it was changed
1909 >                if (cursor_changed && sdl_cursor) {
1910 >                        cursor_changed = false;
1911 >                        SDL_FreeCursor(sdl_cursor);
1912 >                        sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
1913 >                        if (sdl_cursor)
1914 >                                SDL_SetCursor(sdl_cursor);
1915 >                }
1916 > #endif
1917  
1918                  // Set new palette if it was changed
1919                  handle_palette_changes();
# Line 1437 | Line 1921 | static int redraw_func(void *arg)
1921  
1922          uint64 end = GetTicks_usec();
1923          D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1440        redraw_thread_cancel = false;
1924          return 0;
1925   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines