ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.18
Committed: 2005-03-20T23:43:17Z (19 years, 3 months ago) by gbeauche
Branch: MAIN
Changes since 1.17: +7 -7 lines
Log Message:
more sdl key conversions

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * video_sdl.cpp - Video/graphics emulation, SDL specific stuff
3     *
4 gbeauche 1.15 * Basilisk II (C) 1997-2005 Christian Bauer
5 gbeauche 1.1 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     /*
22     * NOTES:
23     * The Ctrl key works like a qualifier for special actions:
24     * Ctrl-Tab = suspend DGA mode
25     * Ctrl-Esc = emergency quit
26     * Ctrl-F1 = mount floppy
27     * Ctrl-F5 = grab mouse (in windowed mode)
28     *
29     * FIXMEs and TODOs:
30     * - Ctrl-Fn doesn't generate SDL_KEYDOWN events (SDL bug?)
31     * - Mouse acceleration, there is no API in SDL yet for that
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 gbeauche 1.2 * - 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 gbeauche 1.6 * - Refresh performance is still slow. Use SDL_CreateRGBSurface()?
39     * - Backport hw cursor acceleration to Basilisk II?
40 gbeauche 1.1 */
41    
42     #include "sysdeps.h"
43    
44     #include <SDL.h>
45     #include <SDL_mutex.h>
46     #include <SDL_thread.h>
47     #include <errno.h>
48 gbeauche 1.4 #include <vector>
49 gbeauche 1.1
50     #include "cpu_emulation.h"
51     #include "main.h"
52     #include "adb.h"
53     #include "macos_util.h"
54     #include "prefs.h"
55     #include "user_strings.h"
56     #include "video.h"
57 gbeauche 1.4 #include "video_defs.h"
58 gbeauche 1.1 #include "video_blit.h"
59 gbeauche 1.13 #include "vm_alloc.h"
60 gbeauche 1.1
61     #define DEBUG 0
62     #include "debug.h"
63    
64    
65     // Supported video modes
66 gbeauche 1.4 using std::vector;
67     static vector<VIDEO_MODE> VideoModes;
68 gbeauche 1.1
69     // Display types
70 gbeauche 1.4 #ifdef SHEEPSHAVER
71 gbeauche 1.1 enum {
72 gbeauche 1.4 DISPLAY_WINDOW = DIS_WINDOW, // windowed display
73     DISPLAY_SCREEN = DIS_SCREEN // fullscreen display
74 gbeauche 1.1 };
75 gbeauche 1.4 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 gbeauche 1.1
84     // Constants
85 gbeauche 1.16 #ifdef WIN32
86     const char KEYCODE_FILE_NAME[] = "BasiliskII_keycodes";
87     #else
88 gbeauche 1.1 const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
89 gbeauche 1.16 #endif
90 gbeauche 1.1
91    
92     // Global variables
93     static int32 frame_skip; // Prefs items
94     static int16 mouse_wheel_mode;
95     static int16 mouse_wheel_lines;
96    
97     static uint8 *the_buffer = NULL; // Mac frame buffer (where MacOS draws into)
98     static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer (for refreshed modes)
99     static uint32 the_buffer_size; // Size of allocated the_buffer
100    
101     static bool redraw_thread_active = false; // Flag: Redraw thread installed
102     static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread
103     static SDL_Thread *redraw_thread = NULL; // Redraw thread
104 gbeauche 1.13 static volatile bool thread_stop_req = false;
105     static volatile bool thread_stop_ack = false; // Acknowledge for thread_stop_req
106 gbeauche 1.1
107     #ifdef ENABLE_VOSF
108 gbeauche 1.7 static bool use_vosf = false; // Flag: VOSF enabled
109 gbeauche 1.1 #else
110     static const bool use_vosf = false; // VOSF not possible
111     #endif
112    
113     static bool ctrl_down = false; // Flag: Ctrl key pressed
114     static bool caps_on = false; // Flag: Caps Lock on
115     static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
116     static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
117     static bool emul_suspended = false; // Flag: Emulator suspended
118    
119     static bool classic_mode = false; // Flag: Classic Mac video mode
120    
121     static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
122     static int keycode_table[256]; // X keycode -> Mac keycode translation table
123    
124     // SDL variables
125     static int screen_depth; // Depth of current screen
126 gbeauche 1.6 static SDL_Cursor *sdl_cursor; // Copy of Mac cursor
127     static volatile bool cursor_changed = false; // Flag: cursor changed, redraw_func must update the cursor
128 gbeauche 1.1 static SDL_Color sdl_palette[256]; // Color palette to be used as CLUT and gamma table
129     static bool sdl_palette_changed = false; // Flag: Palette changed, redraw thread must set new colors
130 gbeauche 1.2 static const int sdl_eventmask = SDL_MOUSEBUTTONDOWNMASK | SDL_MOUSEBUTTONUPMASK | SDL_MOUSEMOTIONMASK | SDL_KEYUPMASK | SDL_KEYDOWNMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK;
131 gbeauche 1.1
132     // Mutex to protect palette
133     static SDL_mutex *sdl_palette_lock = NULL;
134     #define LOCK_PALETTE SDL_LockMutex(sdl_palette_lock)
135     #define UNLOCK_PALETTE SDL_UnlockMutex(sdl_palette_lock)
136    
137     // Mutex to protect frame buffer
138     static SDL_mutex *frame_buffer_lock = NULL;
139     #define LOCK_FRAME_BUFFER SDL_LockMutex(frame_buffer_lock)
140     #define UNLOCK_FRAME_BUFFER SDL_UnlockMutex(frame_buffer_lock)
141    
142     // Video refresh function
143     static void VideoRefreshInit(void);
144     static void (*video_refresh)(void);
145    
146    
147     // Prototypes
148     static int redraw_func(void *arg);
149    
150     // From sys_unix.cpp
151     extern void SysMountFirstFloppy(void);
152    
153    
154     /*
155 gbeauche 1.13 * Framebuffer allocation routines
156     */
157    
158     static void *vm_acquire_framebuffer(uint32 size)
159     {
160     #ifdef SHEEPSHAVER
161     #ifdef DIRECT_ADDRESSING_HACK
162     const uint32 FRAME_BUFFER_BASE = 0x61000000;
163     uint8 *fb = Mac2HostAddr(FRAME_BUFFER_BASE);
164     if (vm_acquire_fixed(fb, size) < 0)
165     fb = VM_MAP_FAILED;
166     return fb;
167     #endif
168     #endif
169     return vm_acquire(size);
170     }
171    
172     static inline void vm_release_framebuffer(void *fb, uint32 size)
173     {
174     vm_release(fb, size);
175     }
176    
177    
178     /*
179 gbeauche 1.4 * SheepShaver glue
180     */
181    
182     #ifdef SHEEPSHAVER
183     // Color depth modes type
184     typedef int video_depth;
185    
186     // 1, 2, 4 and 8 bit depths use a color palette
187     static inline bool IsDirectMode(VIDEO_MODE const & mode)
188     {
189     return IsDirectMode(mode.viAppleMode);
190     }
191    
192     // Abstract base class representing one (possibly virtual) monitor
193     // ("monitor" = rectangular display with a contiguous frame buffer)
194     class monitor_desc {
195     public:
196     monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) {}
197     virtual ~monitor_desc() {}
198    
199     // Get current Mac frame buffer base address
200     uint32 get_mac_frame_base(void) const {return screen_base;}
201    
202     // Set Mac frame buffer base address (called from switch_to_mode())
203     void set_mac_frame_base(uint32 base) {screen_base = base;}
204    
205     // Get current video mode
206     const VIDEO_MODE &get_current_mode(void) const {return VModes[cur_mode];}
207    
208     // Called by the video driver to switch the video mode on this display
209     // (must call set_mac_frame_base())
210     virtual void switch_to_current_mode(void) = 0;
211    
212     // Called by the video driver to set the color palette (in indexed modes)
213     // or the gamma table (in direct modes)
214     virtual void set_palette(uint8 *pal, int num) = 0;
215     };
216    
217     // Vector of pointers to available monitor descriptions, filled by VideoInit()
218     static vector<monitor_desc *> VideoMonitors;
219    
220     // Find Apple mode matching best specified dimensions
221     static int find_apple_resolution(int xsize, int ysize)
222     {
223     int apple_id;
224     if (xsize < 800)
225     apple_id = APPLE_640x480;
226     else if (xsize < 1024)
227     apple_id = APPLE_800x600;
228     else if (xsize < 1152)
229     apple_id = APPLE_1024x768;
230     else if (xsize < 1280) {
231     if (ysize < 900)
232     apple_id = APPLE_1152x768;
233     else
234     apple_id = APPLE_1152x900;
235     }
236     else if (xsize < 1600)
237     apple_id = APPLE_1280x1024;
238     else
239     apple_id = APPLE_1600x1200;
240     return apple_id;
241     }
242    
243     // Set parameters to specified Apple mode
244     static void set_apple_resolution(int apple_id, int &xsize, int &ysize)
245     {
246     switch (apple_id) {
247     case APPLE_640x480:
248     xsize = 640;
249     ysize = 480;
250     break;
251     case APPLE_800x600:
252     xsize = 800;
253     ysize = 600;
254     break;
255     case APPLE_1024x768:
256     xsize = 1024;
257     ysize = 768;
258     break;
259     case APPLE_1152x768:
260     xsize = 1152;
261     ysize = 768;
262     break;
263     case APPLE_1152x900:
264     xsize = 1152;
265     ysize = 900;
266     break;
267     case APPLE_1280x1024:
268     xsize = 1280;
269     ysize = 1024;
270     break;
271     case APPLE_1600x1200:
272     xsize = 1600;
273     ysize = 1200;
274     break;
275     default:
276     abort();
277     }
278     }
279    
280     // Match Apple mode matching best specified dimensions
281     static int match_apple_resolution(int &xsize, int &ysize)
282     {
283     int apple_id = find_apple_resolution(xsize, ysize);
284     set_apple_resolution(apple_id, xsize, ysize);
285     return apple_id;
286     }
287    
288     // Display error alert
289     static void ErrorAlert(int error)
290     {
291     ErrorAlert(GetString(error));
292     }
293 gbeauche 1.7
294     // Display warning alert
295     static void WarningAlert(int warning)
296     {
297     WarningAlert(GetString(warning));
298     }
299 gbeauche 1.4 #endif
300    
301    
302     /*
303 gbeauche 1.1 * monitor_desc subclass for SDL display
304     */
305    
306     class SDL_monitor_desc : public monitor_desc {
307     public:
308 gbeauche 1.4 SDL_monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
309 gbeauche 1.1 ~SDL_monitor_desc() {}
310    
311     virtual void switch_to_current_mode(void);
312     virtual void set_palette(uint8 *pal, int num);
313    
314     bool video_open(void);
315     void video_close(void);
316     };
317    
318    
319     /*
320     * Utility functions
321     */
322    
323 gbeauche 1.4 // Find palette size for given color depth
324     static int palette_size(int mode)
325     {
326     switch (mode) {
327     case VIDEO_DEPTH_1BIT: return 2;
328     case VIDEO_DEPTH_2BIT: return 4;
329     case VIDEO_DEPTH_4BIT: return 16;
330     case VIDEO_DEPTH_8BIT: return 256;
331     case VIDEO_DEPTH_16BIT: return 32;
332     case VIDEO_DEPTH_32BIT: return 256;
333     default: return 0;
334     }
335     }
336    
337     // Return bytes per pixel for requested depth
338     static inline int bytes_per_pixel(int depth)
339     {
340     int bpp;
341     switch (depth) {
342     case 8:
343     bpp = 1;
344     break;
345     case 15: case 16:
346     bpp = 2;
347     break;
348     case 24: case 32:
349     bpp = 4;
350     break;
351     default:
352     abort();
353     }
354     return bpp;
355     }
356    
357 gbeauche 1.1 // Map video_mode depth ID to numerical depth value
358 gbeauche 1.12 static int mac_depth_of_video_depth(int video_depth)
359 gbeauche 1.1 {
360     int depth = -1;
361     switch (video_depth) {
362 gbeauche 1.4 case VIDEO_DEPTH_1BIT:
363 gbeauche 1.1 depth = 1;
364     break;
365 gbeauche 1.4 case VIDEO_DEPTH_2BIT:
366 gbeauche 1.1 depth = 2;
367     break;
368 gbeauche 1.4 case VIDEO_DEPTH_4BIT:
369 gbeauche 1.1 depth = 4;
370     break;
371 gbeauche 1.4 case VIDEO_DEPTH_8BIT:
372 gbeauche 1.1 depth = 8;
373     break;
374 gbeauche 1.4 case VIDEO_DEPTH_16BIT:
375 gbeauche 1.1 depth = 16;
376     break;
377 gbeauche 1.4 case VIDEO_DEPTH_32BIT:
378 gbeauche 1.1 depth = 32;
379     break;
380     default:
381     abort();
382     }
383     return depth;
384     }
385    
386 gbeauche 1.12 // Map video_mode depth ID to SDL screen depth
387     static int sdl_depth_of_video_depth(int video_depth)
388     {
389     return (video_depth <= VIDEO_DEPTH_8BIT) ? 8 : mac_depth_of_video_depth(video_depth);
390     }
391    
392 gbeauche 1.5 // Check wether specified mode is available
393     static bool has_mode(int type, int width, int height)
394     {
395     // FIXME: no fullscreen support yet
396     if (type == DISPLAY_SCREEN)
397     return false;
398    
399     #ifdef SHEEPSHAVER
400     // Filter out Classic resolutiosn
401     if (width == 512 && height == 384)
402     return false;
403    
404     // Read window modes prefs
405     static uint32 window_modes = 0;
406     static uint32 screen_modes = 0;
407     if (window_modes == 0 || screen_modes == 0) {
408     window_modes = PrefsFindInt32("windowmodes");
409     screen_modes = PrefsFindInt32("screenmodes");
410     if (window_modes == 0 || screen_modes == 0)
411     window_modes |= 3; // Allow at least 640x480 and 800x600 window modes
412     }
413    
414     if (type == DISPLAY_WINDOW) {
415     int apple_mask, apple_id = find_apple_resolution(width, height);
416     switch (apple_id) {
417     case APPLE_640x480: apple_mask = 0x01; break;
418     case APPLE_800x600: apple_mask = 0x02; break;
419     case APPLE_1024x768: apple_mask = 0x04; break;
420     case APPLE_1152x768: apple_mask = 0x40; break;
421     case APPLE_1152x900: apple_mask = 0x08; break;
422     case APPLE_1280x1024: apple_mask = 0x10; break;
423     case APPLE_1600x1200: apple_mask = 0x20; break;
424     default: apple_mask = 0x00; break;
425     }
426     return (window_modes & apple_mask);
427     }
428     #else
429     return true;
430     #endif
431     return false;
432     }
433    
434 gbeauche 1.1 // Add mode to list of supported modes
435 gbeauche 1.4 static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
436 gbeauche 1.1 {
437 gbeauche 1.5 // Filter out unsupported modes
438     if (!has_mode(type, width, height))
439     return;
440    
441     // Fill in VideoMode entry
442 gbeauche 1.4 VIDEO_MODE mode;
443     #ifdef SHEEPSHAVER
444     // Recalculate dimensions to fit Apple modes
445     resolution_id = match_apple_resolution(width, height);
446     mode.viType = type;
447     #endif
448     VIDEO_MODE_X = width;
449     VIDEO_MODE_Y = height;
450     VIDEO_MODE_RESOLUTION = resolution_id;
451     VIDEO_MODE_ROW_BYTES = bytes_per_row;
452 gbeauche 1.10 VIDEO_MODE_DEPTH = (video_depth)depth;
453 gbeauche 1.1 VideoModes.push_back(mode);
454     }
455    
456     // Add standard list of windowed modes for given color depth
457 gbeauche 1.4 static void add_window_modes(int depth)
458 gbeauche 1.1 {
459 gbeauche 1.4 video_depth vdepth = (video_depth)depth;
460     add_mode(DISPLAY_WINDOW, 512, 384, 0x80, TrivialBytesPerRow(512, vdepth), depth);
461     add_mode(DISPLAY_WINDOW, 640, 480, 0x81, TrivialBytesPerRow(640, vdepth), depth);
462     add_mode(DISPLAY_WINDOW, 800, 600, 0x82, TrivialBytesPerRow(800, vdepth), depth);
463     add_mode(DISPLAY_WINDOW, 1024, 768, 0x83, TrivialBytesPerRow(1024, vdepth), depth);
464     add_mode(DISPLAY_WINDOW, 1152, 870, 0x84, TrivialBytesPerRow(1152, vdepth), depth);
465     add_mode(DISPLAY_WINDOW, 1280, 1024, 0x85, TrivialBytesPerRow(1280, vdepth), depth);
466     add_mode(DISPLAY_WINDOW, 1600, 1200, 0x86, TrivialBytesPerRow(1600, vdepth), depth);
467 gbeauche 1.1 }
468    
469     // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
470 gbeauche 1.4 static void set_mac_frame_buffer(SDL_monitor_desc &monitor, int depth, bool native_byte_order)
471 gbeauche 1.1 {
472     #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
473     int layout = FLAYOUT_DIRECT;
474 gbeauche 1.4 if (depth == VIDEO_DEPTH_16BIT)
475 gbeauche 1.1 layout = (screen_depth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
476 gbeauche 1.4 else if (depth == VIDEO_DEPTH_32BIT)
477 gbeauche 1.1 layout = (screen_depth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
478     if (native_byte_order)
479     MacFrameLayout = layout;
480     else
481     MacFrameLayout = FLAYOUT_DIRECT;
482     monitor.set_mac_frame_base(MacFrameBaseMac);
483    
484     // Set variables used by UAE memory banking
485 gbeauche 1.4 const VIDEO_MODE &mode = monitor.get_current_mode();
486 gbeauche 1.1 MacFrameBaseHost = the_buffer;
487 gbeauche 1.4 MacFrameSize = VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y;
488 gbeauche 1.1 InitFrameBufferMapping();
489     #else
490     monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
491     #endif
492     D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
493     }
494    
495     // Set window name and class
496     static void set_window_name(int name)
497     {
498     const SDL_VideoInfo *vi = SDL_GetVideoInfo();
499     if (vi && vi->wm_available) {
500     const char *str = GetString(name);
501     SDL_WM_SetCaption(str, str);
502     }
503     }
504    
505     // Set mouse grab mode
506     static SDL_GrabMode set_grab_mode(SDL_GrabMode mode)
507     {
508     const SDL_VideoInfo *vi =SDL_GetVideoInfo();
509     return (vi && vi->wm_available ? SDL_WM_GrabInput(mode) : SDL_GRAB_OFF);
510     }
511    
512    
513     /*
514     * Display "driver" classes
515     */
516    
517     class driver_base {
518     public:
519     driver_base(SDL_monitor_desc &m);
520     virtual ~driver_base();
521    
522     virtual void update_palette(void);
523     virtual void suspend(void) {}
524     virtual void resume(void) {}
525     virtual void toggle_mouse_grab(void) {}
526     virtual void mouse_moved(int x, int y) { ADBMouseMoved(x, y); }
527    
528     void disable_mouse_accel(void);
529     void restore_mouse_accel(void);
530    
531     virtual void grab_mouse(void) {}
532     virtual void ungrab_mouse(void) {}
533    
534     public:
535     SDL_monitor_desc &monitor; // Associated video monitor
536 gbeauche 1.4 const VIDEO_MODE &mode; // Video mode handled by the driver
537 gbeauche 1.1
538     bool init_ok; // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
539     SDL_Surface *s; // The surface we draw into
540     };
541    
542     class driver_window;
543     static void update_display_window_vosf(driver_window *drv);
544     static void update_display_dynamic(int ticker, driver_window *drv);
545     static void update_display_static(driver_window *drv);
546    
547     class driver_window : public driver_base {
548     friend void update_display_window_vosf(driver_window *drv);
549     friend void update_display_dynamic(int ticker, driver_window *drv);
550     friend void update_display_static(driver_window *drv);
551    
552     public:
553     driver_window(SDL_monitor_desc &monitor);
554     ~driver_window();
555    
556     void toggle_mouse_grab(void);
557     void mouse_moved(int x, int y);
558    
559     void grab_mouse(void);
560     void ungrab_mouse(void);
561    
562     private:
563     bool mouse_grabbed; // Flag: mouse pointer grabbed, using relative mouse mode
564     int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode)
565     };
566    
567     static driver_base *drv = NULL; // Pointer to currently used driver object
568    
569     #ifdef ENABLE_VOSF
570     # include "video_vosf.h"
571     #endif
572    
573     driver_base::driver_base(SDL_monitor_desc &m)
574     : monitor(m), mode(m.get_current_mode()), init_ok(false), s(NULL)
575     {
576     the_buffer = NULL;
577     the_buffer_copy = NULL;
578     }
579    
580     driver_base::~driver_base()
581     {
582     ungrab_mouse();
583     restore_mouse_accel();
584    
585     if (s)
586     SDL_FreeSurface(s);
587    
588 gbeauche 1.13 // the_buffer shall always be mapped through vm_acquire_framebuffer()
589     if (the_buffer != VM_MAP_FAILED) {
590     D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
591     vm_release_framebuffer(the_buffer, the_buffer_size);
592     the_buffer = NULL;
593     }
594    
595 gbeauche 1.1 // Free frame buffer(s)
596     if (!use_vosf) {
597     if (the_buffer_copy) {
598     free(the_buffer_copy);
599     the_buffer_copy = NULL;
600     }
601     }
602     #ifdef ENABLE_VOSF
603     else {
604     if (the_host_buffer) {
605     D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
606     free(the_host_buffer);
607     the_host_buffer = NULL;
608     }
609     if (the_buffer_copy) {
610     D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
611     free(the_buffer_copy);
612     the_buffer_copy = NULL;
613     }
614 gbeauche 1.13
615     // Deinitialize VOSF
616     video_vosf_exit();
617 gbeauche 1.1 }
618     #endif
619     }
620    
621     // Palette has changed
622     void driver_base::update_palette(void)
623     {
624 gbeauche 1.4 const VIDEO_MODE &mode = monitor.get_current_mode();
625 gbeauche 1.1
626 gbeauche 1.10 if ((int)VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT)
627 gbeauche 1.1 SDL_SetPalette(s, SDL_PHYSPAL, sdl_palette, 0, 256);
628     }
629    
630     // Disable mouse acceleration
631     void driver_base::disable_mouse_accel(void)
632     {
633     }
634    
635     // Restore mouse acceleration to original value
636     void driver_base::restore_mouse_accel(void)
637     {
638     }
639    
640    
641     /*
642     * Windowed display driver
643     */
644    
645     // Open display
646     driver_window::driver_window(SDL_monitor_desc &m)
647     : driver_base(m), mouse_grabbed(false)
648     {
649 gbeauche 1.4 int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
650 gbeauche 1.1 int aligned_width = (width + 15) & ~15;
651     int aligned_height = (height + 15) & ~15;
652    
653     // Set absolute mouse mode
654     ADBSetRelMouseMode(mouse_grabbed);
655    
656     // Create surface
657 gbeauche 1.12 int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
658 gbeauche 1.1 if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL)
659     return;
660    
661     #ifdef ENABLE_VOSF
662     use_vosf = true;
663     // Allocate memory for frame buffer (SIZE is extended to page-boundary)
664     the_host_buffer = (uint8 *)s->pixels;
665     the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
666 gbeauche 1.13 the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
667 gbeauche 1.1 the_buffer_copy = (uint8 *)malloc(the_buffer_size);
668     D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
669 gbeauche 1.7
670     // Check whether we can initialize the VOSF subsystem and it's profitable
671     if (!video_vosf_init(m)) {
672     WarningAlert(STR_VOSF_INIT_ERR);
673     use_vosf = false;
674     }
675     else if (!video_vosf_profitable()) {
676     video_vosf_exit();
677 gbeauche 1.8 printf("VOSF acceleration is not profitable on this platform, disabling it\n");
678 gbeauche 1.7 use_vosf = false;
679     }
680     if (!use_vosf) {
681     free(the_buffer_copy);
682     vm_release(the_buffer, the_buffer_size);
683     the_host_buffer = NULL;
684     }
685 gbeauche 1.1 #endif
686 gbeauche 1.7 if (!use_vosf) {
687     // Allocate memory for frame buffer
688     the_buffer_size = (aligned_height + 2) * s->pitch;
689     the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
690 gbeauche 1.13 the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
691 gbeauche 1.7 D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
692     }
693    
694 gbeauche 1.6 #ifdef SHEEPSHAVER
695     // Create cursor
696     if ((sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, 0, 0)) != NULL) {
697     SDL_SetCursor(sdl_cursor);
698     cursor_changed = false;
699     }
700     #else
701     // Hide cursor
702     SDL_ShowCursor(0);
703     #endif
704    
705 gbeauche 1.1 // Set window name/class
706     set_window_name(STR_WINDOW_TITLE);
707    
708     // Init blitting routines
709     SDL_PixelFormat *f = s->format;
710     VisualFormat visualFormat;
711     visualFormat.depth = depth;
712     visualFormat.Rmask = f->Rmask;
713     visualFormat.Gmask = f->Gmask;
714     visualFormat.Bmask = f->Bmask;
715 gbeauche 1.12 Screen_blitter_init(visualFormat, true, mac_depth_of_video_depth(VIDEO_MODE_DEPTH));
716 gbeauche 1.1
717     // Load gray ramp to 8->16/32 expand map
718     if (!IsDirectMode(mode))
719     for (int i=0; i<256; i++)
720     ExpandMap[i] = SDL_MapRGB(f, i, i, i);
721    
722     // Set frame buffer base
723 gbeauche 1.4 set_mac_frame_buffer(monitor, VIDEO_MODE_DEPTH, true);
724 gbeauche 1.1
725     // Everything went well
726     init_ok = true;
727     }
728    
729     // Close display
730     driver_window::~driver_window()
731     {
732     #ifdef ENABLE_VOSF
733     if (use_vosf)
734     the_host_buffer = NULL; // don't free() in driver_base dtor
735     #endif
736     if (s)
737     SDL_FreeSurface(s);
738     }
739    
740     // Toggle mouse grab
741     void driver_window::toggle_mouse_grab(void)
742     {
743     if (mouse_grabbed)
744     ungrab_mouse();
745     else
746     grab_mouse();
747     }
748    
749     // Grab mouse, switch to relative mouse mode
750     void driver_window::grab_mouse(void)
751     {
752     if (!mouse_grabbed) {
753     SDL_GrabMode new_mode = set_grab_mode(SDL_GRAB_ON);
754     if (new_mode == SDL_GRAB_ON) {
755     set_window_name(STR_WINDOW_TITLE_GRABBED);
756     disable_mouse_accel();
757     mouse_grabbed = true;
758     }
759     }
760     }
761    
762     // Ungrab mouse, switch to absolute mouse mode
763     void driver_window::ungrab_mouse(void)
764     {
765     if (mouse_grabbed) {
766     SDL_GrabMode new_mode = set_grab_mode(SDL_GRAB_OFF);
767     if (new_mode == SDL_GRAB_OFF) {
768     set_window_name(STR_WINDOW_TITLE);
769     restore_mouse_accel();
770     mouse_grabbed = false;
771     }
772     }
773     }
774    
775     // Mouse moved
776     void driver_window::mouse_moved(int x, int y)
777     {
778     mouse_last_x = x; mouse_last_y = y;
779     ADBMouseMoved(x, y);
780     }
781    
782     /*
783     * Initialization
784     */
785    
786     // Init keycode translation table
787     static void keycode_init(void)
788     {
789     bool use_kc = PrefsFindBool("keycodes");
790     if (use_kc) {
791    
792     // Get keycode file path from preferences
793     const char *kc_path = PrefsFindString("keycodefile");
794    
795     // Open keycode table
796     FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
797     if (f == NULL) {
798     char str[256];
799     sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
800     WarningAlert(str);
801     return;
802     }
803    
804     // Default translation table
805     for (int i=0; i<256; i++)
806     keycode_table[i] = -1;
807    
808     // Search for server vendor string, then read keycodes
809     char video_driver[256];
810     SDL_VideoDriverName(video_driver, sizeof(video_driver));
811     bool video_driver_found = false;
812     char line[256];
813 gbeauche 1.14 int n_keys = 0;
814 gbeauche 1.1 while (fgets(line, sizeof(line) - 1, f)) {
815     // Read line
816     int len = strlen(line);
817     if (len == 0)
818     continue;
819     line[len-1] = 0;
820    
821     // Comments begin with "#" or ";"
822     if (line[0] == '#' || line[0] == ';' || line[0] == 0)
823     continue;
824    
825     if (video_driver_found) {
826 gbeauche 1.14 // Skip aliases as long as we have read keycodes yet
827     // Otherwise, it's another mapping and we have to stop
828 gbeauche 1.9 static const char sdl_str[] = "sdl";
829 gbeauche 1.14 if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0 && n_keys == 0)
830 gbeauche 1.1 continue;
831    
832     // Read keycode
833     int x_code, mac_code;
834     if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
835 gbeauche 1.14 keycode_table[x_code & 0xff] = mac_code, n_keys++;
836 gbeauche 1.1 else
837     break;
838     } else {
839     // Search for SDL video driver string
840 gbeauche 1.9 static const char sdl_str[] = "sdl";
841     if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0) {
842     char *p = line + sizeof(sdl_str);
843 gbeauche 1.1 if (strstr(video_driver, p) == video_driver)
844     video_driver_found = true;
845     }
846     }
847     }
848    
849     // Keycode file completely read
850     fclose(f);
851     use_keycodes = video_driver_found;
852    
853     // Vendor not found? Then display warning
854     if (!video_driver_found) {
855     char str[256];
856     sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), video_driver, kc_path ? kc_path : KEYCODE_FILE_NAME);
857     WarningAlert(str);
858     return;
859     }
860 gbeauche 1.14
861     D(bug("Using SDL/%s keycodes table, %d key mappings\n", video_driver, n_keys));
862 gbeauche 1.1 }
863     }
864    
865     // Open display for current mode
866     bool SDL_monitor_desc::video_open(void)
867     {
868     D(bug("video_open()\n"));
869 gbeauche 1.4 const VIDEO_MODE &mode = get_current_mode();
870 gbeauche 1.5 #if DEBUG
871     D(bug("Current video mode:\n"));
872     D(bug(" %dx%d (ID %02x), %d bpp\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << (VIDEO_MODE_DEPTH & 0x0f)));
873     #endif
874 gbeauche 1.1
875     // Create display driver object of requested type
876     switch (display_type) {
877     case DISPLAY_WINDOW:
878     drv = new(std::nothrow) driver_window(*this);
879     break;
880     }
881     if (drv == NULL)
882     return false;
883     if (!drv->init_ok) {
884     delete drv;
885     drv = NULL;
886     return false;
887     }
888    
889     // Initialize VideoRefresh function
890     VideoRefreshInit();
891    
892     // Lock down frame buffer
893     LOCK_FRAME_BUFFER;
894    
895     // Start redraw/input thread
896     redraw_thread_cancel = false;
897 gbeauche 1.3 redraw_thread_active = ((redraw_thread = SDL_CreateThread(redraw_func, NULL)) != NULL);
898 gbeauche 1.1 if (!redraw_thread_active) {
899     printf("FATAL: cannot create redraw thread\n");
900     return false;
901     }
902     return true;
903     }
904    
905 gbeauche 1.4 #ifdef SHEEPSHAVER
906     bool VideoInit(void)
907     {
908     const bool classic = false;
909     #else
910 gbeauche 1.1 bool VideoInit(bool classic)
911     {
912 gbeauche 1.4 #endif
913 gbeauche 1.1 classic_mode = classic;
914    
915     #ifdef ENABLE_VOSF
916     // Zero the mainBuffer structure
917     mainBuffer.dirtyPages = NULL;
918     mainBuffer.pageInfo = NULL;
919     #endif
920    
921     // Create Mutexes
922     if ((sdl_palette_lock = SDL_CreateMutex()) == NULL)
923     return false;
924     if ((frame_buffer_lock = SDL_CreateMutex()) == NULL)
925     return false;
926    
927     // Init keycode translation
928     keycode_init();
929    
930     // Read prefs
931     frame_skip = PrefsFindInt32("frameskip");
932     mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
933     mouse_wheel_lines = PrefsFindInt32("mousewheellines");
934    
935     // Get screen mode from preferences
936 gbeauche 1.5 const char *mode_str = NULL;
937     #ifndef SHEEPSHAVER
938 gbeauche 1.1 if (classic_mode)
939     mode_str = "win/512/342";
940     else
941     mode_str = PrefsFindString("screen");
942 gbeauche 1.5 #endif
943 gbeauche 1.1
944     // Determine display type and default dimensions
945 gbeauche 1.4 int default_width, default_height;
946     if (classic) {
947     default_width = 512;
948     default_height = 384;
949     }
950     else {
951     default_width = 640;
952     default_height = 480;
953     }
954 gbeauche 1.1 display_type = DISPLAY_WINDOW;
955     if (mode_str) {
956     if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
957     display_type = DISPLAY_WINDOW;
958     }
959     int max_width = 640, max_height = 480;
960 gbeauche 1.5 SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
961     if (modes && modes != (SDL_Rect **)-1) {
962 gbeauche 1.17 // It turns out that on some implementations, and contrary to the documentation,
963     // the returned list is not sorted from largest to smallest (e.g. Windows)
964     for (int i = 0; modes[i] != NULL; i++) {
965     const int w = modes[i]->w;
966     const int h = modes[i]->h;
967     if (w > max_width && h > max_height) {
968     max_width = w;
969     max_height = h;
970     }
971     }
972 gbeauche 1.5 if (default_width > max_width)
973     default_width = max_width;
974     if (default_height > max_height)
975     default_height = max_height;
976 gbeauche 1.1 }
977     if (default_width <= 0)
978     default_width = max_width;
979     if (default_height <= 0)
980     default_height = max_height;
981    
982     // Mac screen depth follows X depth
983     screen_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
984 gbeauche 1.4 int default_depth;
985 gbeauche 1.1 switch (screen_depth) {
986     case 8:
987 gbeauche 1.4 default_depth = VIDEO_DEPTH_8BIT;
988 gbeauche 1.1 break;
989     case 15: case 16:
990 gbeauche 1.4 default_depth = VIDEO_DEPTH_16BIT;
991 gbeauche 1.1 break;
992     case 24: case 32:
993 gbeauche 1.4 default_depth = VIDEO_DEPTH_32BIT;
994 gbeauche 1.1 break;
995     default:
996 gbeauche 1.4 default_depth = VIDEO_DEPTH_1BIT;
997 gbeauche 1.1 break;
998     }
999    
1000     // Construct list of supported modes
1001     if (display_type == DISPLAY_WINDOW) {
1002     if (classic)
1003 gbeauche 1.4 add_mode(display_type, 512, 342, 0x80, 64, VIDEO_DEPTH_1BIT);
1004 gbeauche 1.1 else {
1005 gbeauche 1.4 for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1006 gbeauche 1.12 int bpp = sdl_depth_of_video_depth(d);
1007 gbeauche 1.1 if (SDL_VideoModeOK(max_width, max_height, bpp, SDL_HWSURFACE))
1008     add_window_modes(video_depth(d));
1009     }
1010     }
1011     } else
1012 gbeauche 1.4 add_mode(display_type, default_width, default_height, 0x80, TrivialBytesPerRow(default_width, (video_depth)default_depth), default_depth);
1013 gbeauche 1.1 if (VideoModes.empty()) {
1014     ErrorAlert(STR_NO_XVISUAL_ERR);
1015     return false;
1016     }
1017    
1018     // Find requested default mode with specified dimensions
1019     uint32 default_id;
1020 gbeauche 1.4 std::vector<VIDEO_MODE>::const_iterator i, end = VideoModes.end();
1021 gbeauche 1.1 for (i = VideoModes.begin(); i != end; ++i) {
1022 gbeauche 1.4 const VIDEO_MODE & mode = (*i);
1023     if (VIDEO_MODE_X == default_width && VIDEO_MODE_Y == default_height && VIDEO_MODE_DEPTH == default_depth) {
1024     default_id = VIDEO_MODE_RESOLUTION;
1025     #ifdef SHEEPSHAVER
1026     std::vector<VIDEO_MODE>::const_iterator begin = VideoModes.begin();
1027     cur_mode = distance(begin, i);
1028     #endif
1029 gbeauche 1.1 break;
1030     }
1031     }
1032     if (i == end) { // not found, use first available mode
1033 gbeauche 1.4 const VIDEO_MODE & mode = VideoModes[0];
1034     default_depth = VIDEO_MODE_DEPTH;
1035     default_id = VIDEO_MODE_RESOLUTION;
1036     #ifdef SHEEPSHAVER
1037     cur_mode = 0;
1038     #endif
1039 gbeauche 1.1 }
1040    
1041 gbeauche 1.4 #ifdef SHEEPSHAVER
1042 gbeauche 1.5 for (int i = 0; i < VideoModes.size(); i++)
1043 gbeauche 1.4 VModes[i] = VideoModes[i];
1044 gbeauche 1.5 VideoInfo *p = &VModes[VideoModes.size()];
1045     p->viType = DIS_INVALID; // End marker
1046     p->viRowBytes = 0;
1047     p->viXsize = p->viYsize = 0;
1048     p->viAppleMode = 0;
1049     p->viAppleID = 0;
1050 gbeauche 1.4 #endif
1051    
1052 gbeauche 1.1 #if DEBUG
1053     D(bug("Available video modes:\n"));
1054     for (i = VideoModes.begin(); i != end; ++i) {
1055 gbeauche 1.4 const VIDEO_MODE & mode = (*i);
1056     int bits = 1 << VIDEO_MODE_DEPTH;
1057 gbeauche 1.1 if (bits == 16)
1058     bits = 15;
1059     else if (bits == 32)
1060     bits = 24;
1061 gbeauche 1.4 D(bug(" %dx%d (ID %02x), %d colors\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << bits));
1062 gbeauche 1.1 }
1063     #endif
1064    
1065     // Create SDL_monitor_desc for this (the only) display
1066 gbeauche 1.4 SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, (video_depth)default_depth, default_id);
1067 gbeauche 1.1 VideoMonitors.push_back(monitor);
1068    
1069     // Open display
1070     return monitor->video_open();
1071     }
1072    
1073    
1074     /*
1075     * Deinitialization
1076     */
1077    
1078     // Close display
1079     void SDL_monitor_desc::video_close(void)
1080     {
1081     D(bug("video_close()\n"));
1082    
1083     // Stop redraw thread
1084     if (redraw_thread_active) {
1085     redraw_thread_cancel = true;
1086 gbeauche 1.3 SDL_WaitThread(redraw_thread, NULL);
1087 gbeauche 1.1 }
1088     redraw_thread_active = false;
1089    
1090     // Unlock frame buffer
1091     UNLOCK_FRAME_BUFFER;
1092     D(bug(" frame buffer unlocked\n"));
1093    
1094     // Close display
1095     delete drv;
1096     drv = NULL;
1097     }
1098    
1099     void VideoExit(void)
1100     {
1101     // Close displays
1102     vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
1103     for (i = VideoMonitors.begin(); i != end; ++i)
1104     dynamic_cast<SDL_monitor_desc *>(*i)->video_close();
1105    
1106     // Destroy locks
1107     if (frame_buffer_lock)
1108     SDL_DestroyMutex(frame_buffer_lock);
1109     if (sdl_palette_lock)
1110     SDL_DestroyMutex(sdl_palette_lock);
1111     }
1112    
1113    
1114     /*
1115     * Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode)
1116     */
1117    
1118     void VideoQuitFullScreen(void)
1119     {
1120     D(bug("VideoQuitFullScreen()\n"));
1121     quit_full_screen = true;
1122     }
1123    
1124    
1125     /*
1126     * Mac VBL interrupt
1127     */
1128    
1129 gbeauche 1.4 /*
1130     * Execute video VBL routine
1131     */
1132    
1133     #ifdef SHEEPSHAVER
1134     void VideoVBL(void)
1135     {
1136     // Emergency quit requested? Then quit
1137     if (emerg_quit)
1138     QuitEmulator();
1139    
1140     // Temporarily give up frame buffer lock (this is the point where
1141     // we are suspended when the user presses Ctrl-Tab)
1142     UNLOCK_FRAME_BUFFER;
1143     LOCK_FRAME_BUFFER;
1144    
1145     // Execute video VBL
1146     if (private_data != NULL && private_data->interruptsEnabled)
1147     VSLDoInterruptService(private_data->vslServiceID);
1148     }
1149     #else
1150 gbeauche 1.1 void VideoInterrupt(void)
1151     {
1152 gbeauche 1.2 // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
1153     SDL_PumpEvents();
1154    
1155 gbeauche 1.1 // Emergency quit requested? Then quit
1156     if (emerg_quit)
1157     QuitEmulator();
1158    
1159     // Temporarily give up frame buffer lock (this is the point where
1160     // we are suspended when the user presses Ctrl-Tab)
1161     UNLOCK_FRAME_BUFFER;
1162     LOCK_FRAME_BUFFER;
1163     }
1164 gbeauche 1.4 #endif
1165 gbeauche 1.1
1166    
1167     /*
1168     * Set palette
1169     */
1170    
1171 gbeauche 1.4 #ifdef SHEEPSHAVER
1172     void video_set_palette(void)
1173     {
1174     monitor_desc * monitor = VideoMonitors[0];
1175     int n_colors = palette_size(monitor->get_current_mode().viAppleMode);
1176     uint8 pal[256 * 3];
1177     for (int c = 0; c < n_colors; c++) {
1178     pal[c*3 + 0] = mac_pal[c].red;
1179     pal[c*3 + 1] = mac_pal[c].green;
1180     pal[c*3 + 2] = mac_pal[c].blue;
1181     }
1182     monitor->set_palette(pal, n_colors);
1183     }
1184     #endif
1185    
1186 gbeauche 1.1 void SDL_monitor_desc::set_palette(uint8 *pal, int num_in)
1187     {
1188 gbeauche 1.4 const VIDEO_MODE &mode = get_current_mode();
1189 gbeauche 1.1
1190     // FIXME: how can we handle the gamma ramp?
1191 gbeauche 1.10 if ((int)VIDEO_MODE_DEPTH > VIDEO_DEPTH_8BIT)
1192 gbeauche 1.1 return;
1193    
1194     LOCK_PALETTE;
1195    
1196     // Convert colors to XColor array
1197     int num_out = 256;
1198     bool stretch = false;
1199     SDL_Color *p = sdl_palette;
1200     for (int i=0; i<num_out; i++) {
1201     int c = (stretch ? (i * num_in) / num_out : i);
1202     p->r = pal[c*3 + 0] * 0x0101;
1203     p->g = pal[c*3 + 1] * 0x0101;
1204     p->b = pal[c*3 + 2] * 0x0101;
1205     p++;
1206     }
1207    
1208     // Recalculate pixel color expansion map
1209     if (!IsDirectMode(mode)) {
1210     for (int i=0; i<256; i++) {
1211     int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1212     ExpandMap[i] = SDL_MapRGB(drv->s->format, pal[c*3+0], pal[c*3+1], pal[c*3+2]);
1213     }
1214    
1215     #ifdef ENABLE_VOSF
1216 gbeauche 1.7 if (use_vosf) {
1217     // We have to redraw everything because the interpretation of pixel values changed
1218     LOCK_VOSF;
1219     PFLAG_SET_ALL;
1220     UNLOCK_VOSF;
1221     memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1222     }
1223 gbeauche 1.1 #endif
1224     }
1225    
1226     // Tell redraw thread to change palette
1227     sdl_palette_changed = true;
1228    
1229     UNLOCK_PALETTE;
1230     }
1231    
1232    
1233     /*
1234     * Switch video mode
1235     */
1236    
1237 gbeauche 1.4 #ifdef SHEEPSHAVER
1238     int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1239     {
1240     /* return if no mode change */
1241     if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1242     (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
1243    
1244     /* first find video mode in table */
1245     for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
1246     if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
1247     (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
1248     csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
1249     csSave->saveData = ReadMacInt32(ParamPtr + csData);
1250     csSave->savePage = ReadMacInt16(ParamPtr + csPage);
1251    
1252 gbeauche 1.13 // Disable interrupts and pause redraw thread
1253 gbeauche 1.4 DisableInterrupt();
1254 gbeauche 1.13 thread_stop_ack = false;
1255     thread_stop_req = true;
1256     while (!thread_stop_ack) ;
1257 gbeauche 1.4
1258     cur_mode = i;
1259     monitor_desc *monitor = VideoMonitors[0];
1260     monitor->switch_to_current_mode();
1261    
1262     WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
1263     csSave->saveBaseAddr=screen_base;
1264     csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
1265     csSave->saveMode=VModes[cur_mode].viAppleMode;
1266    
1267 gbeauche 1.13 // Enable interrupts and resume redraw thread
1268     thread_stop_req = false;
1269 gbeauche 1.4 EnableInterrupt();
1270     return noErr;
1271     }
1272     }
1273     return paramErr;
1274     }
1275     #endif
1276    
1277 gbeauche 1.1 void SDL_monitor_desc::switch_to_current_mode(void)
1278     {
1279     // Close and reopen display
1280     video_close();
1281     video_open();
1282    
1283     if (drv == NULL) {
1284     ErrorAlert(STR_OPEN_WINDOW_ERR);
1285     QuitEmulator();
1286     }
1287     }
1288    
1289    
1290     /*
1291 gbeauche 1.4 * Can we set the MacOS cursor image into the window?
1292     */
1293    
1294     #ifdef SHEEPSHAVER
1295     bool video_can_change_cursor(void)
1296     {
1297 gbeauche 1.6 return (display_type == DISPLAY_WINDOW);
1298 gbeauche 1.4 }
1299     #endif
1300    
1301    
1302     /*
1303     * Set cursor image for window
1304     */
1305    
1306     #ifdef SHEEPSHAVER
1307     void video_set_cursor(void)
1308     {
1309 gbeauche 1.6 cursor_changed = true;
1310 gbeauche 1.4 }
1311     #endif
1312    
1313    
1314     /*
1315 gbeauche 1.9 * Keyboard-related utilify functions
1316 gbeauche 1.1 */
1317    
1318 gbeauche 1.9 static bool is_modifier_key(SDL_KeyboardEvent const & e)
1319     {
1320     switch (e.keysym.sym) {
1321     case SDLK_NUMLOCK:
1322     case SDLK_CAPSLOCK:
1323     case SDLK_SCROLLOCK:
1324     case SDLK_RSHIFT:
1325     case SDLK_LSHIFT:
1326     case SDLK_RCTRL:
1327     case SDLK_LCTRL:
1328     case SDLK_RALT:
1329     case SDLK_LALT:
1330     case SDLK_RMETA:
1331     case SDLK_LMETA:
1332     case SDLK_LSUPER:
1333     case SDLK_RSUPER:
1334     case SDLK_MODE:
1335     case SDLK_COMPOSE:
1336     return true;
1337     }
1338     return false;
1339     }
1340    
1341 gbeauche 1.1 static bool is_ctrl_down(SDL_keysym const & ks)
1342     {
1343     return ctrl_down || (ks.mod & KMOD_CTRL);
1344     }
1345    
1346 gbeauche 1.9
1347     /*
1348     * Translate key event to Mac keycode, returns -1 if no keycode was found
1349     * and -2 if the key was recognized as a hotkey
1350     */
1351    
1352 gbeauche 1.1 static int kc_decode(SDL_keysym const & ks, bool key_down)
1353     {
1354     switch (ks.sym) {
1355     case SDLK_a: return 0x00;
1356     case SDLK_b: return 0x0b;
1357     case SDLK_c: return 0x08;
1358     case SDLK_d: return 0x02;
1359     case SDLK_e: return 0x0e;
1360     case SDLK_f: return 0x03;
1361     case SDLK_g: return 0x05;
1362     case SDLK_h: return 0x04;
1363     case SDLK_i: return 0x22;
1364     case SDLK_j: return 0x26;
1365     case SDLK_k: return 0x28;
1366     case SDLK_l: return 0x25;
1367     case SDLK_m: return 0x2e;
1368     case SDLK_n: return 0x2d;
1369     case SDLK_o: return 0x1f;
1370     case SDLK_p: return 0x23;
1371     case SDLK_q: return 0x0c;
1372     case SDLK_r: return 0x0f;
1373     case SDLK_s: return 0x01;
1374     case SDLK_t: return 0x11;
1375     case SDLK_u: return 0x20;
1376     case SDLK_v: return 0x09;
1377     case SDLK_w: return 0x0d;
1378     case SDLK_x: return 0x07;
1379     case SDLK_y: return 0x10;
1380     case SDLK_z: return 0x06;
1381    
1382     case SDLK_1: case SDLK_EXCLAIM: return 0x12;
1383     case SDLK_2: case SDLK_AT: return 0x13;
1384 gbeauche 1.18 case SDLK_3: case SDLK_HASH: return 0x14;
1385 gbeauche 1.1 case SDLK_4: case SDLK_DOLLAR: return 0x15;
1386 gbeauche 1.18 case SDLK_5: return 0x17;
1387 gbeauche 1.1 case SDLK_6: return 0x16;
1388     case SDLK_7: return 0x1a;
1389     case SDLK_8: return 0x1c;
1390     case SDLK_9: return 0x19;
1391     case SDLK_0: return 0x1d;
1392    
1393 gbeauche 1.18 case SDLK_BACKQUOTE: return 0x0a;
1394 gbeauche 1.1 case SDLK_MINUS: case SDLK_UNDERSCORE: return 0x1b;
1395     case SDLK_EQUALS: case SDLK_PLUS: return 0x18;
1396 gbeauche 1.18 case SDLK_LEFTBRACKET: return 0x21;
1397     case SDLK_RIGHTBRACKET: return 0x1e;
1398     case SDLK_BACKSLASH: return 0x2a;
1399 gbeauche 1.1 case SDLK_SEMICOLON: case SDLK_COLON: return 0x29;
1400 gbeauche 1.18 case SDLK_QUOTE: case SDLK_QUOTEDBL: return 0x27;
1401 gbeauche 1.1 case SDLK_COMMA: case SDLK_LESS: return 0x2b;
1402     case SDLK_PERIOD: case SDLK_GREATER: return 0x2f;
1403     case SDLK_SLASH: case SDLK_QUESTION: return 0x2c;
1404    
1405     case SDLK_TAB: if (is_ctrl_down(ks)) {if (!key_down) drv->suspend(); return -2;} else return 0x30;
1406     case SDLK_RETURN: return 0x24;
1407     case SDLK_SPACE: return 0x31;
1408     case SDLK_BACKSPACE: return 0x33;
1409    
1410     case SDLK_DELETE: return 0x75;
1411     case SDLK_INSERT: return 0x72;
1412     case SDLK_HOME: case SDLK_HELP: return 0x73;
1413     case SDLK_END: return 0x77;
1414     case SDLK_PAGEUP: return 0x74;
1415     case SDLK_PAGEDOWN: return 0x79;
1416    
1417     case SDLK_LCTRL: return 0x36;
1418     case SDLK_RCTRL: return 0x36;
1419     case SDLK_LSHIFT: return 0x38;
1420     case SDLK_RSHIFT: return 0x38;
1421 gbeauche 1.9 #if (defined(__APPLE__) && defined(__MACH__))
1422     case SDLK_LALT: return 0x3a;
1423     case SDLK_RALT: return 0x3a;
1424     case SDLK_LMETA: return 0x37;
1425     case SDLK_RMETA: return 0x37;
1426     #else
1427 gbeauche 1.1 case SDLK_LALT: return 0x37;
1428     case SDLK_RALT: return 0x37;
1429     case SDLK_LMETA: return 0x3a;
1430     case SDLK_RMETA: return 0x3a;
1431 gbeauche 1.9 #endif
1432 gbeauche 1.1 case SDLK_MENU: return 0x32;
1433     case SDLK_CAPSLOCK: return 0x39;
1434     case SDLK_NUMLOCK: return 0x47;
1435    
1436     case SDLK_UP: return 0x3e;
1437     case SDLK_DOWN: return 0x3d;
1438     case SDLK_LEFT: return 0x3b;
1439     case SDLK_RIGHT: return 0x3c;
1440    
1441     case SDLK_ESCAPE: if (is_ctrl_down(ks)) {if (!key_down) { quit_full_screen = true; emerg_quit = true; } return -2;} else return 0x35;
1442    
1443     case SDLK_F1: if (is_ctrl_down(ks)) {if (!key_down) SysMountFirstFloppy(); return -2;} else return 0x7a;
1444     case SDLK_F2: return 0x78;
1445     case SDLK_F3: return 0x63;
1446     case SDLK_F4: return 0x76;
1447     case SDLK_F5: if (is_ctrl_down(ks)) {if (!key_down) drv->toggle_mouse_grab(); return -2;} else return 0x60;
1448     case SDLK_F6: return 0x61;
1449     case SDLK_F7: return 0x62;
1450     case SDLK_F8: return 0x64;
1451     case SDLK_F9: return 0x65;
1452     case SDLK_F10: return 0x6d;
1453     case SDLK_F11: return 0x67;
1454     case SDLK_F12: return 0x6f;
1455    
1456     case SDLK_PRINT: return 0x69;
1457     case SDLK_SCROLLOCK: return 0x6b;
1458     case SDLK_PAUSE: return 0x71;
1459    
1460     case SDLK_KP0: return 0x52;
1461     case SDLK_KP1: return 0x53;
1462     case SDLK_KP2: return 0x54;
1463     case SDLK_KP3: return 0x55;
1464     case SDLK_KP4: return 0x56;
1465     case SDLK_KP5: return 0x57;
1466     case SDLK_KP6: return 0x58;
1467     case SDLK_KP7: return 0x59;
1468     case SDLK_KP8: return 0x5b;
1469     case SDLK_KP9: return 0x5c;
1470     case SDLK_KP_PERIOD: return 0x41;
1471     case SDLK_KP_PLUS: return 0x45;
1472     case SDLK_KP_MINUS: return 0x4e;
1473     case SDLK_KP_MULTIPLY: return 0x43;
1474     case SDLK_KP_DIVIDE: return 0x4b;
1475     case SDLK_KP_ENTER: return 0x4c;
1476     case SDLK_KP_EQUALS: return 0x51;
1477     }
1478     D(bug("Unhandled SDL keysym: %d\n", ks.sym));
1479     return -1;
1480     }
1481    
1482     static int event2keycode(SDL_KeyboardEvent const &ev, bool key_down)
1483     {
1484     return kc_decode(ev.keysym, key_down);
1485     }
1486    
1487    
1488     /*
1489     * SDL event handling
1490     */
1491    
1492     static void handle_events(void)
1493     {
1494 gbeauche 1.2 SDL_Event events[10];
1495     const int n_max_events = sizeof(events) / sizeof(events[0]);
1496     int n_events;
1497    
1498     while ((n_events = SDL_PeepEvents(events, n_max_events, SDL_GETEVENT, sdl_eventmask)) > 0) {
1499     for (int i = 0; i < n_events; i++) {
1500     SDL_Event const & event = events[i];
1501     switch (event.type) {
1502 gbeauche 1.1
1503     // Mouse button
1504     case SDL_MOUSEBUTTONDOWN: {
1505     unsigned int button = event.button.button;
1506     if (button < 4)
1507     ADBMouseDown(button - 1);
1508     else if (button < 6) { // Wheel mouse
1509     if (mouse_wheel_mode == 0) {
1510     int key = (button == 5) ? 0x79 : 0x74; // Page up/down
1511     ADBKeyDown(key);
1512     ADBKeyUp(key);
1513     } else {
1514     int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down
1515     for(int i=0; i<mouse_wheel_lines; i++) {
1516     ADBKeyDown(key);
1517     ADBKeyUp(key);
1518     }
1519     }
1520     }
1521     break;
1522     }
1523     case SDL_MOUSEBUTTONUP: {
1524     unsigned int button = event.button.button;
1525     if (button < 4)
1526     ADBMouseUp(button - 1);
1527     break;
1528     }
1529    
1530     // Mouse moved
1531     case SDL_MOUSEMOTION:
1532     drv->mouse_moved(event.motion.x, event.motion.y);
1533     break;
1534    
1535     // Keyboard
1536     case SDL_KEYDOWN: {
1537     int code = -1;
1538 gbeauche 1.9 if (use_keycodes && !is_modifier_key(event.key)) {
1539 gbeauche 1.1 if (event2keycode(event.key, true) != -2) // This is called to process the hotkeys
1540     code = keycode_table[event.key.keysym.scancode & 0xff];
1541     } else
1542     code = event2keycode(event.key, true);
1543     if (code >= 0) {
1544     if (!emul_suspended) {
1545     if (code == 0x39) { // Caps Lock pressed
1546     if (caps_on) {
1547     ADBKeyUp(code);
1548     caps_on = false;
1549     } else {
1550     ADBKeyDown(code);
1551     caps_on = true;
1552     }
1553     } else
1554     ADBKeyDown(code);
1555     if (code == 0x36)
1556     ctrl_down = true;
1557     } else {
1558     if (code == 0x31)
1559     drv->resume(); // Space wakes us up
1560     }
1561     }
1562     break;
1563     }
1564     case SDL_KEYUP: {
1565     int code = -1;
1566 gbeauche 1.9 if (use_keycodes && !is_modifier_key(event.key)) {
1567 gbeauche 1.1 if (event2keycode(event.key, false) != -2) // This is called to process the hotkeys
1568     code = keycode_table[event.key.keysym.scancode & 0xff];
1569     } else
1570     code = event2keycode(event.key, false);
1571 gbeauche 1.9 if (code >= 0) {
1572     if (code == 0x39) { // Caps Lock released
1573     if (caps_on) {
1574     ADBKeyUp(code);
1575     caps_on = false;
1576     } else {
1577     ADBKeyDown(code);
1578     caps_on = true;
1579     }
1580     } else
1581     ADBKeyUp(code);
1582 gbeauche 1.1 if (code == 0x36)
1583     ctrl_down = false;
1584     }
1585     break;
1586     }
1587    
1588     // Hidden parts exposed, force complete refresh of window
1589     case SDL_VIDEOEXPOSE:
1590     if (display_type == DISPLAY_WINDOW) {
1591 gbeauche 1.4 const VIDEO_MODE &mode = VideoMonitors[0]->get_current_mode();
1592 gbeauche 1.1 #ifdef ENABLE_VOSF
1593     if (use_vosf) { // VOSF refresh
1594     LOCK_VOSF;
1595     PFLAG_SET_ALL;
1596     UNLOCK_VOSF;
1597 gbeauche 1.4 memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1598 gbeauche 1.1 }
1599     else
1600     #endif
1601 gbeauche 1.4 memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1602 gbeauche 1.1 }
1603     break;
1604    
1605     // Window "close" widget clicked
1606     case SDL_QUIT:
1607     ADBKeyDown(0x7f); // Power key
1608     ADBKeyUp(0x7f);
1609     break;
1610 gbeauche 1.2 }
1611 gbeauche 1.1 }
1612     }
1613     }
1614    
1615    
1616     /*
1617     * Window display update
1618     */
1619    
1620     // Static display update (fixed frame rate, but incremental)
1621     static void update_display_static(driver_window *drv)
1622     {
1623     // Incremental update code
1624     int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1625 gbeauche 1.4 const VIDEO_MODE &mode = drv->mode;
1626     int bytes_per_row = VIDEO_MODE_ROW_BYTES;
1627 gbeauche 1.1 uint8 *p, *p2;
1628    
1629     // Check for first line from top and first line from bottom that have changed
1630     y1 = 0;
1631 gbeauche 1.4 for (j=0; j<VIDEO_MODE_Y; j++) {
1632 gbeauche 1.1 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1633     y1 = j;
1634     break;
1635     }
1636     }
1637     y2 = y1 - 1;
1638 gbeauche 1.4 for (j=VIDEO_MODE_Y-1; j>=y1; j--) {
1639 gbeauche 1.1 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1640     y2 = j;
1641     break;
1642     }
1643     }
1644     high = y2 - y1 + 1;
1645    
1646     // Check for first column from left and first column from right that have changed
1647     if (high) {
1648 gbeauche 1.10 if ((int)VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
1649 gbeauche 1.1 const int src_bytes_per_row = bytes_per_row;
1650     const int dst_bytes_per_row = drv->s->pitch;
1651 gbeauche 1.4 const int pixels_per_byte = VIDEO_MODE_X / src_bytes_per_row;
1652 gbeauche 1.1
1653 gbeauche 1.4 x1 = VIDEO_MODE_X / pixels_per_byte;
1654 gbeauche 1.1 for (j = y1; j <= y2; j++) {
1655     p = &the_buffer[j * bytes_per_row];
1656     p2 = &the_buffer_copy[j * bytes_per_row];
1657     for (i = 0; i < x1; i++) {
1658     if (*p != *p2) {
1659     x1 = i;
1660     break;
1661     }
1662     p++; p2++;
1663     }
1664     }
1665     x2 = x1;
1666     for (j = y1; j <= y2; j++) {
1667     p = &the_buffer[j * bytes_per_row];
1668     p2 = &the_buffer_copy[j * bytes_per_row];
1669     p += bytes_per_row;
1670     p2 += bytes_per_row;
1671 gbeauche 1.4 for (i = (VIDEO_MODE_X / pixels_per_byte); i > x2; i--) {
1672 gbeauche 1.1 p--; p2--;
1673     if (*p != *p2) {
1674     x2 = i;
1675     break;
1676     }
1677     }
1678     }
1679     x1 *= pixels_per_byte;
1680     x2 *= pixels_per_byte;
1681     wide = (x2 - x1 + pixels_per_byte - 1) & -pixels_per_byte;
1682    
1683     // Update copy of the_buffer
1684     if (high && wide) {
1685    
1686     // Lock surface, if required
1687     if (SDL_MUSTLOCK(drv->s))
1688     SDL_LockSurface(drv->s);
1689    
1690     // Blit to screen surface
1691     int si = y1 * src_bytes_per_row + (x1 / pixels_per_byte);
1692     int di = y1 * dst_bytes_per_row + x1;
1693     for (j = y1; j <= y2; j++) {
1694     memcpy(the_buffer_copy + si, the_buffer + si, wide / pixels_per_byte);
1695     Screen_blit((uint8 *)drv->s->pixels + di, the_buffer + si, wide / pixels_per_byte);
1696     si += src_bytes_per_row;
1697     di += dst_bytes_per_row;
1698     }
1699    
1700     // Unlock surface, if required
1701     if (SDL_MUSTLOCK(drv->s))
1702     SDL_UnlockSurface(drv->s);
1703    
1704     // Refresh display
1705     SDL_UpdateRect(drv->s, x1, y1, wide, high);
1706     }
1707    
1708     } else {
1709 gbeauche 1.4 const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
1710 gbeauche 1.1
1711 gbeauche 1.4 x1 = VIDEO_MODE_X;
1712 gbeauche 1.1 for (j=y1; j<=y2; j++) {
1713     p = &the_buffer[j * bytes_per_row];
1714     p2 = &the_buffer_copy[j * bytes_per_row];
1715     for (i=0; i<x1*bytes_per_pixel; i++) {
1716     if (*p != *p2) {
1717     x1 = i / bytes_per_pixel;
1718     break;
1719     }
1720     p++; p2++;
1721     }
1722     }
1723     x2 = x1;
1724     for (j=y1; j<=y2; j++) {
1725     p = &the_buffer[j * bytes_per_row];
1726     p2 = &the_buffer_copy[j * bytes_per_row];
1727     p += bytes_per_row;
1728     p2 += bytes_per_row;
1729 gbeauche 1.4 for (i=VIDEO_MODE_X*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1730 gbeauche 1.1 p--;
1731     p2--;
1732     if (*p != *p2) {
1733     x2 = i / bytes_per_pixel;
1734     break;
1735     }
1736     }
1737     }
1738     wide = x2 - x1;
1739    
1740     // Update copy of the_buffer
1741     if (high && wide) {
1742    
1743     // Lock surface, if required
1744     if (SDL_MUSTLOCK(drv->s))
1745     SDL_LockSurface(drv->s);
1746    
1747     // Blit to screen surface
1748     for (j=y1; j<=y2; j++) {
1749     i = j * bytes_per_row + x1 * bytes_per_pixel;
1750     memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1751     Screen_blit((uint8 *)drv->s->pixels + i, the_buffer + i, bytes_per_pixel * wide);
1752     }
1753    
1754     // Unlock surface, if required
1755     if (SDL_MUSTLOCK(drv->s))
1756     SDL_UnlockSurface(drv->s);
1757    
1758     // Refresh display
1759     SDL_UpdateRect(drv->s, x1, y1, wide, high);
1760     }
1761     }
1762     }
1763     }
1764    
1765    
1766     // We suggest the compiler to inline the next two functions so that it
1767     // may specialise the code according to the current screen depth and
1768     // display type. A clever compiler would do that job by itself though...
1769    
1770     // NOTE: update_display_vosf is inlined too
1771    
1772     static inline void possibly_quit_dga_mode()
1773     {
1774     // Quit DGA mode if requested (something terrible has happened and we
1775     // want to give control back to the user)
1776     if (quit_full_screen) {
1777     quit_full_screen = false;
1778     delete drv;
1779     drv = NULL;
1780     }
1781     }
1782    
1783     static inline void possibly_ungrab_mouse()
1784     {
1785     // Ungrab mouse if requested (something terrible has happened and we
1786     // want to give control back to the user)
1787     if (quit_full_screen) {
1788     quit_full_screen = false;
1789     if (drv)
1790     drv->ungrab_mouse();
1791     }
1792     }
1793    
1794     static inline void handle_palette_changes(void)
1795     {
1796     LOCK_PALETTE;
1797    
1798     if (sdl_palette_changed) {
1799     sdl_palette_changed = false;
1800     drv->update_palette();
1801     }
1802    
1803     UNLOCK_PALETTE;
1804     }
1805    
1806     static void video_refresh_dga(void)
1807     {
1808     // Quit DGA mode if requested
1809     possibly_quit_dga_mode();
1810     }
1811    
1812     #ifdef ENABLE_VOSF
1813     #if REAL_ADDRESSING || DIRECT_ADDRESSING
1814     static void video_refresh_dga_vosf(void)
1815     {
1816     // Quit DGA mode if requested
1817     possibly_quit_dga_mode();
1818    
1819     // Update display (VOSF variant)
1820     static int tick_counter = 0;
1821     if (++tick_counter >= frame_skip) {
1822     tick_counter = 0;
1823     if (mainBuffer.dirty) {
1824     LOCK_VOSF;
1825     update_display_dga_vosf();
1826     UNLOCK_VOSF;
1827     }
1828     }
1829     }
1830     #endif
1831    
1832     static void video_refresh_window_vosf(void)
1833     {
1834     // Ungrab mouse if requested
1835     possibly_ungrab_mouse();
1836    
1837     // Update display (VOSF variant)
1838     static int tick_counter = 0;
1839     if (++tick_counter >= frame_skip) {
1840     tick_counter = 0;
1841     if (mainBuffer.dirty) {
1842     LOCK_VOSF;
1843     update_display_window_vosf(static_cast<driver_window *>(drv));
1844     UNLOCK_VOSF;
1845     }
1846     }
1847     }
1848     #endif // def ENABLE_VOSF
1849    
1850     static void video_refresh_window_static(void)
1851     {
1852     // Ungrab mouse if requested
1853     possibly_ungrab_mouse();
1854    
1855     // Update display (static variant)
1856     static int tick_counter = 0;
1857     if (++tick_counter >= frame_skip) {
1858     tick_counter = 0;
1859     update_display_static(static_cast<driver_window *>(drv));
1860     }
1861     }
1862    
1863    
1864     /*
1865     * Thread for screen refresh, input handling etc.
1866     */
1867    
1868     static void VideoRefreshInit(void)
1869     {
1870     // TODO: set up specialised 8bpp VideoRefresh handlers ?
1871     if (display_type == DISPLAY_SCREEN) {
1872     #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
1873     if (use_vosf)
1874     video_refresh = video_refresh_dga_vosf;
1875     else
1876     #endif
1877     video_refresh = video_refresh_dga;
1878     }
1879     else {
1880     #ifdef ENABLE_VOSF
1881     if (use_vosf)
1882     video_refresh = video_refresh_window_vosf;
1883     else
1884     #endif
1885     video_refresh = video_refresh_window_static;
1886     }
1887     }
1888    
1889 gbeauche 1.4 const int VIDEO_REFRESH_HZ = 60;
1890     const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
1891    
1892 gbeauche 1.1 static int redraw_func(void *arg)
1893     {
1894     uint64 start = GetTicks_usec();
1895     int64 ticks = 0;
1896 gbeauche 1.4 uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
1897 gbeauche 1.1
1898     while (!redraw_thread_cancel) {
1899    
1900     // Wait
1901 gbeauche 1.4 next += VIDEO_REFRESH_DELAY;
1902     int64 delay = next - GetTicks_usec();
1903     if (delay > 0)
1904     Delay_usec(delay);
1905     else if (delay < -VIDEO_REFRESH_DELAY)
1906     next = GetTicks_usec();
1907     ticks++;
1908 gbeauche 1.1
1909 gbeauche 1.13 #ifdef SHEEPSHAVER
1910     // Pause if requested (during video mode switches)
1911     if (thread_stop_req) {
1912     thread_stop_ack = true;
1913     continue;
1914     }
1915     #endif
1916    
1917 gbeauche 1.1 // Handle SDL events
1918     handle_events();
1919    
1920     // Refresh display
1921     video_refresh();
1922    
1923 gbeauche 1.6 #ifdef SHEEPSHAVER
1924     // Set new cursor image if it was changed
1925     if (cursor_changed && sdl_cursor) {
1926     cursor_changed = false;
1927     SDL_FreeCursor(sdl_cursor);
1928     sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
1929     if (sdl_cursor)
1930     SDL_SetCursor(sdl_cursor);
1931     }
1932     #endif
1933    
1934 gbeauche 1.1 // Set new palette if it was changed
1935     handle_palette_changes();
1936     }
1937    
1938     uint64 end = GetTicks_usec();
1939     D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1940     return 0;
1941     }