ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.28
Committed: 2006-05-14T13:49:53Z (18 years, 2 months ago) by gbeauche
Branch: MAIN
Changes since 1.27: +4 -0 lines
Log Message:
Fix build on Windows (<malloc.h> for alloca())

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