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