ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.19
Committed: 2005-06-06T21:20:28Z (19 years, 1 month ago) by gbeauche
Branch: MAIN
Changes since 1.18: +177 -28 lines
Log Message:
Add experimental fullscreen SDL video graphics. Map Windows keys to Option.

File Contents

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