ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.9
Committed: 2004-06-27T17:31:21Z (20 years ago) by gbeauche
Branch: MAIN
Changes since 1.8: +55 -11 lines
Log Message:
Force processing of modifier keys through SDL keysyms. Fix mapping of
Option & Command keys on MacOS X. Fix scroll lock on MacOS X too.

File Contents

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