ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.4
Committed: 2004-06-24T15:25:57Z (20 years, 4 months ago) by gbeauche
Branch: MAIN
Changes since 1.3: +751 -76 lines
Log Message:
Initial SDL support for SheepShaver, though it seems to slow down the
emulator somehow. Also use better timing in redraw_func().

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