ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.5
Committed: 2004-06-24T21:46:55Z (20 years ago) by gbeauche
Branch: MAIN
Changes since 1.4: +69 -16 lines
Log Message:
Try to get maximum display width by assuming that would match maximum
possible resolution for fullscreen+hwsurface. Fix, termination of VModes[].
Really handle "windowmodes" prefs item, but this needs code factoring.

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