ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.22
Committed: 2005-11-29T23:20:31Z (18 years, 7 months ago) by gbeauche
Branch: MAIN
CVS Tags: nigel-build-17
Changes since 1.21: +13 -0 lines
Log Message:
Protect the SDL events queue when changing the cursor map in SheepShaver,
aka fix "Xlib: unexpected async reply" messages in SDL/x11 builds.

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