ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.2
Committed: 2004-06-23T22:37:33Z (20 years ago) by gbeauche
Branch: MAIN
Changes since 1.1: +16 -3 lines
Log Message:
Fix events processing on MacOS X, though mouse motion is still not smooth
enough.

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    
47     #include "cpu_emulation.h"
48     #include "main.h"
49     #include "adb.h"
50     #include "macos_util.h"
51     #include "prefs.h"
52     #include "user_strings.h"
53     #include "video.h"
54     #include "video_blit.h"
55    
56     #define DEBUG 0
57     #include "debug.h"
58    
59    
60     // Supported video modes
61     static vector<video_mode> VideoModes;
62    
63     // Display types
64     enum {
65     DISPLAY_WINDOW, // windowed display
66     DISPLAY_SCREEN // fullscreen display
67     };
68    
69     // Constants
70     const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
71    
72    
73     // Global variables
74     static int32 frame_skip; // Prefs items
75     static int16 mouse_wheel_mode;
76     static int16 mouse_wheel_lines;
77    
78     static int display_type = DISPLAY_WINDOW; // See enum above
79     static uint8 *the_buffer = NULL; // Mac frame buffer (where MacOS draws into)
80     static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer (for refreshed modes)
81     static uint32 the_buffer_size; // Size of allocated the_buffer
82    
83     static bool redraw_thread_active = false; // Flag: Redraw thread installed
84     static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread
85     static SDL_Thread *redraw_thread = NULL; // Redraw thread
86    
87     #ifdef ENABLE_VOSF
88     static bool use_vosf = true; // Flag: VOSF enabled
89     #else
90     static const bool use_vosf = false; // VOSF not possible
91     #endif
92    
93     static bool ctrl_down = false; // Flag: Ctrl key pressed
94     static bool caps_on = false; // Flag: Caps Lock on
95     static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
96     static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
97     static bool emul_suspended = false; // Flag: Emulator suspended
98    
99     static bool classic_mode = false; // Flag: Classic Mac video mode
100    
101     static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
102     static int keycode_table[256]; // X keycode -> Mac keycode translation table
103    
104     // SDL variables
105     static int screen_depth; // Depth of current screen
106     static SDL_Color sdl_palette[256]; // Color palette to be used as CLUT and gamma table
107     static bool sdl_palette_changed = false; // Flag: Palette changed, redraw thread must set new colors
108 gbeauche 1.2 static const int sdl_eventmask = SDL_MOUSEBUTTONDOWNMASK | SDL_MOUSEBUTTONUPMASK | SDL_MOUSEMOTIONMASK | SDL_KEYUPMASK | SDL_KEYDOWNMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK;
109 gbeauche 1.1
110     // Mutex to protect palette
111     static SDL_mutex *sdl_palette_lock = NULL;
112     #define LOCK_PALETTE SDL_LockMutex(sdl_palette_lock)
113     #define UNLOCK_PALETTE SDL_UnlockMutex(sdl_palette_lock)
114    
115     // Mutex to protect frame buffer
116     static SDL_mutex *frame_buffer_lock = NULL;
117     #define LOCK_FRAME_BUFFER SDL_LockMutex(frame_buffer_lock)
118     #define UNLOCK_FRAME_BUFFER SDL_UnlockMutex(frame_buffer_lock)
119    
120     // Video refresh function
121     static void VideoRefreshInit(void);
122     static void (*video_refresh)(void);
123    
124    
125     // Prototypes
126     static int redraw_func(void *arg);
127    
128     // From sys_unix.cpp
129     extern void SysMountFirstFloppy(void);
130    
131    
132     /*
133     * monitor_desc subclass for SDL display
134     */
135    
136     class SDL_monitor_desc : public monitor_desc {
137     public:
138     SDL_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
139     ~SDL_monitor_desc() {}
140    
141     virtual void switch_to_current_mode(void);
142     virtual void set_palette(uint8 *pal, int num);
143    
144     bool video_open(void);
145     void video_close(void);
146     };
147    
148    
149     /*
150     * Utility functions
151     */
152    
153     // Map video_mode depth ID to numerical depth value
154     static int sdl_depth_of_video_depth(int video_depth)
155     {
156     int depth = -1;
157     switch (video_depth) {
158     case VDEPTH_1BIT:
159     depth = 1;
160     break;
161     case VDEPTH_2BIT:
162     depth = 2;
163     break;
164     case VDEPTH_4BIT:
165     depth = 4;
166     break;
167     case VDEPTH_8BIT:
168     depth = 8;
169     break;
170     case VDEPTH_16BIT:
171     depth = 16;
172     break;
173     case VDEPTH_32BIT:
174     depth = 32;
175     break;
176     default:
177     abort();
178     }
179     return depth;
180     }
181    
182     // Add mode to list of supported modes
183     static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth)
184     {
185     video_mode mode;
186     mode.x = width;
187     mode.y = height;
188     mode.resolution_id = resolution_id;
189     mode.bytes_per_row = bytes_per_row;
190     mode.depth = depth;
191     VideoModes.push_back(mode);
192     }
193    
194     // Add standard list of windowed modes for given color depth
195     static void add_window_modes(video_depth depth)
196     {
197     add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth);
198     add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth);
199     add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth);
200     add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth), depth);
201     add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth);
202     add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth);
203     add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth);
204     }
205    
206     // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
207     static void set_mac_frame_buffer(SDL_monitor_desc &monitor, video_depth depth, bool native_byte_order)
208     {
209     #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
210     int layout = FLAYOUT_DIRECT;
211     if (depth == VDEPTH_16BIT)
212     layout = (screen_depth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
213     else if (depth == VDEPTH_32BIT)
214     layout = (screen_depth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
215     if (native_byte_order)
216     MacFrameLayout = layout;
217     else
218     MacFrameLayout = FLAYOUT_DIRECT;
219     monitor.set_mac_frame_base(MacFrameBaseMac);
220    
221     // Set variables used by UAE memory banking
222     const video_mode &mode = monitor.get_current_mode();
223     MacFrameBaseHost = the_buffer;
224     MacFrameSize = mode.bytes_per_row * mode.y;
225     InitFrameBufferMapping();
226     #else
227     monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
228     #endif
229     D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
230     }
231    
232     // Set window name and class
233     static void set_window_name(int name)
234     {
235     const SDL_VideoInfo *vi = SDL_GetVideoInfo();
236     if (vi && vi->wm_available) {
237     const char *str = GetString(name);
238     SDL_WM_SetCaption(str, str);
239     }
240     }
241    
242     // Set mouse grab mode
243     static SDL_GrabMode set_grab_mode(SDL_GrabMode mode)
244     {
245     const SDL_VideoInfo *vi =SDL_GetVideoInfo();
246     return (vi && vi->wm_available ? SDL_WM_GrabInput(mode) : SDL_GRAB_OFF);
247     }
248    
249    
250     /*
251     * Display "driver" classes
252     */
253    
254     class driver_base {
255     public:
256     driver_base(SDL_monitor_desc &m);
257     virtual ~driver_base();
258    
259     virtual void update_palette(void);
260     virtual void suspend(void) {}
261     virtual void resume(void) {}
262     virtual void toggle_mouse_grab(void) {}
263     virtual void mouse_moved(int x, int y) { ADBMouseMoved(x, y); }
264    
265     void disable_mouse_accel(void);
266     void restore_mouse_accel(void);
267    
268     virtual void grab_mouse(void) {}
269     virtual void ungrab_mouse(void) {}
270    
271     public:
272     SDL_monitor_desc &monitor; // Associated video monitor
273     const video_mode &mode; // Video mode handled by the driver
274    
275     bool init_ok; // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
276     SDL_Surface *s; // The surface we draw into
277     };
278    
279     class driver_window;
280     static void update_display_window_vosf(driver_window *drv);
281     static void update_display_dynamic(int ticker, driver_window *drv);
282     static void update_display_static(driver_window *drv);
283    
284     class driver_window : public driver_base {
285     friend void update_display_window_vosf(driver_window *drv);
286     friend void update_display_dynamic(int ticker, driver_window *drv);
287     friend void update_display_static(driver_window *drv);
288    
289     public:
290     driver_window(SDL_monitor_desc &monitor);
291     ~driver_window();
292    
293     void toggle_mouse_grab(void);
294     void mouse_moved(int x, int y);
295    
296     void grab_mouse(void);
297     void ungrab_mouse(void);
298    
299     private:
300     bool mouse_grabbed; // Flag: mouse pointer grabbed, using relative mouse mode
301     int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode)
302     };
303    
304     static driver_base *drv = NULL; // Pointer to currently used driver object
305    
306     #ifdef ENABLE_VOSF
307     # include "video_vosf.h"
308     #endif
309    
310     driver_base::driver_base(SDL_monitor_desc &m)
311     : monitor(m), mode(m.get_current_mode()), init_ok(false), s(NULL)
312     {
313     the_buffer = NULL;
314     the_buffer_copy = NULL;
315     }
316    
317     driver_base::~driver_base()
318     {
319     ungrab_mouse();
320     restore_mouse_accel();
321    
322     if (s)
323     SDL_FreeSurface(s);
324    
325     // Free frame buffer(s)
326     if (!use_vosf) {
327     if (the_buffer) {
328     free(the_buffer);
329     the_buffer = NULL;
330     }
331     if (the_buffer_copy) {
332     free(the_buffer_copy);
333     the_buffer_copy = NULL;
334     }
335     }
336     #ifdef ENABLE_VOSF
337     else {
338     // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
339     if (the_buffer != VM_MAP_FAILED) {
340     D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
341     vm_release(the_buffer, the_buffer_size);
342     the_buffer = NULL;
343     }
344     if (the_host_buffer) {
345     D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
346     free(the_host_buffer);
347     the_host_buffer = NULL;
348     }
349     if (the_buffer_copy) {
350     D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
351     free(the_buffer_copy);
352     the_buffer_copy = NULL;
353     }
354     }
355     #endif
356     }
357    
358     // Palette has changed
359     void driver_base::update_palette(void)
360     {
361     const video_mode &mode = monitor.get_current_mode();
362    
363     if (mode.depth <= VDEPTH_8BIT)
364     SDL_SetPalette(s, SDL_PHYSPAL, sdl_palette, 0, 256);
365     }
366    
367     // Disable mouse acceleration
368     void driver_base::disable_mouse_accel(void)
369     {
370     }
371    
372     // Restore mouse acceleration to original value
373     void driver_base::restore_mouse_accel(void)
374     {
375     }
376    
377    
378     /*
379     * Windowed display driver
380     */
381    
382     // Open display
383     driver_window::driver_window(SDL_monitor_desc &m)
384     : driver_base(m), mouse_grabbed(false)
385     {
386     int width = mode.x, height = mode.y;
387     int aligned_width = (width + 15) & ~15;
388     int aligned_height = (height + 15) & ~15;
389    
390     // Set absolute mouse mode
391     ADBSetRelMouseMode(mouse_grabbed);
392    
393     // Create surface
394     int depth = (mode.depth <= VDEPTH_8BIT ? 8 : screen_depth);
395     if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL)
396     return;
397    
398     #ifdef ENABLE_VOSF
399     use_vosf = true;
400     // Allocate memory for frame buffer (SIZE is extended to page-boundary)
401     the_host_buffer = (uint8 *)s->pixels;
402     the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
403     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
404     the_buffer_copy = (uint8 *)malloc(the_buffer_size);
405     D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
406     #else
407     // Allocate memory for frame buffer
408     the_buffer_size = (aligned_height + 2) * s->pitch;
409     the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
410     the_buffer = (uint8 *)calloc(1, the_buffer_size);
411     D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
412     #endif
413    
414     // Set window name/class
415     set_window_name(STR_WINDOW_TITLE);
416    
417     // Hide cursor
418     SDL_ShowCursor(0);
419    
420     // Init blitting routines
421     SDL_PixelFormat *f = s->format;
422     VisualFormat visualFormat;
423     visualFormat.depth = depth;
424     visualFormat.Rmask = f->Rmask;
425     visualFormat.Gmask = f->Gmask;
426     visualFormat.Bmask = f->Bmask;
427     Screen_blitter_init(visualFormat, true, sdl_depth_of_video_depth(mode.depth));
428    
429     // Load gray ramp to 8->16/32 expand map
430     if (!IsDirectMode(mode))
431     for (int i=0; i<256; i++)
432     ExpandMap[i] = SDL_MapRGB(f, i, i, i);
433    
434     // Set frame buffer base
435     set_mac_frame_buffer(monitor, mode.depth, true);
436    
437     // Everything went well
438     init_ok = true;
439     }
440    
441     // Close display
442     driver_window::~driver_window()
443     {
444     #ifdef ENABLE_VOSF
445     if (use_vosf)
446     the_host_buffer = NULL; // don't free() in driver_base dtor
447     #endif
448     if (s)
449     SDL_FreeSurface(s);
450     }
451    
452     // Toggle mouse grab
453     void driver_window::toggle_mouse_grab(void)
454     {
455     if (mouse_grabbed)
456     ungrab_mouse();
457     else
458     grab_mouse();
459     }
460    
461     // Grab mouse, switch to relative mouse mode
462     void driver_window::grab_mouse(void)
463     {
464     if (!mouse_grabbed) {
465     SDL_GrabMode new_mode = set_grab_mode(SDL_GRAB_ON);
466     if (new_mode == SDL_GRAB_ON) {
467     set_window_name(STR_WINDOW_TITLE_GRABBED);
468     disable_mouse_accel();
469     mouse_grabbed = true;
470     }
471     }
472     }
473    
474     // Ungrab mouse, switch to absolute mouse mode
475     void driver_window::ungrab_mouse(void)
476     {
477     if (mouse_grabbed) {
478     SDL_GrabMode new_mode = set_grab_mode(SDL_GRAB_OFF);
479     if (new_mode == SDL_GRAB_OFF) {
480     set_window_name(STR_WINDOW_TITLE);
481     restore_mouse_accel();
482     mouse_grabbed = false;
483     }
484     }
485     }
486    
487     // Mouse moved
488     void driver_window::mouse_moved(int x, int y)
489     {
490     mouse_last_x = x; mouse_last_y = y;
491     ADBMouseMoved(x, y);
492     }
493    
494     /*
495     * Initialization
496     */
497    
498     // Init keycode translation table
499     static void keycode_init(void)
500     {
501     bool use_kc = PrefsFindBool("keycodes");
502     if (use_kc) {
503    
504     // Get keycode file path from preferences
505     const char *kc_path = PrefsFindString("keycodefile");
506    
507     // Open keycode table
508     FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
509     if (f == NULL) {
510     char str[256];
511     sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
512     WarningAlert(str);
513     return;
514     }
515    
516     // Default translation table
517     for (int i=0; i<256; i++)
518     keycode_table[i] = -1;
519    
520     // Search for server vendor string, then read keycodes
521     char video_driver[256];
522     SDL_VideoDriverName(video_driver, sizeof(video_driver));
523     bool video_driver_found = false;
524     char line[256];
525     while (fgets(line, sizeof(line) - 1, f)) {
526     // Read line
527     int len = strlen(line);
528     if (len == 0)
529     continue;
530     line[len-1] = 0;
531    
532     // Comments begin with "#" or ";"
533     if (line[0] == '#' || line[0] == ';' || line[0] == 0)
534     continue;
535    
536     if (video_driver_found) {
537     // Skip aliases
538     static const char alias_str[] = "alias";
539     if (strncmp(line, alias_str, sizeof(alias_str) - 1) == 0)
540     continue;
541    
542     // Read keycode
543     int x_code, mac_code;
544     if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
545     keycode_table[x_code & 0xff] = mac_code;
546     else
547     break;
548     } else {
549     // Search for SDL video driver string
550     static const char alias_sdl_str[] = "alias SDL";
551     if (strncmp(line, alias_sdl_str, sizeof(alias_sdl_str) - 1) == 0) {
552     char *p = line + sizeof(alias_sdl_str);
553     if (strstr(video_driver, p) == video_driver)
554     video_driver_found = true;
555     }
556     }
557     }
558    
559     // Keycode file completely read
560     fclose(f);
561     use_keycodes = video_driver_found;
562    
563     // Vendor not found? Then display warning
564     if (!video_driver_found) {
565     char str[256];
566     sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), video_driver, kc_path ? kc_path : KEYCODE_FILE_NAME);
567     WarningAlert(str);
568     return;
569     }
570     }
571     }
572    
573     // Open display for current mode
574     bool SDL_monitor_desc::video_open(void)
575     {
576     D(bug("video_open()\n"));
577     const video_mode &mode = get_current_mode();
578    
579     // Create display driver object of requested type
580     switch (display_type) {
581     case DISPLAY_WINDOW:
582     drv = new(std::nothrow) driver_window(*this);
583     break;
584     }
585     if (drv == NULL)
586     return false;
587     if (!drv->init_ok) {
588     delete drv;
589     drv = NULL;
590     return false;
591     }
592    
593     #ifdef ENABLE_VOSF
594     if (use_vosf) {
595     // Initialize the VOSF system
596     if (!video_vosf_init(*this)) {
597     ErrorAlert(STR_VOSF_INIT_ERR);
598     return false;
599     }
600     }
601     #endif
602    
603     // Initialize VideoRefresh function
604     VideoRefreshInit();
605    
606     // Lock down frame buffer
607     LOCK_FRAME_BUFFER;
608    
609     // Start redraw/input thread
610     redraw_thread_cancel = false;
611     redraw_thread_active = (SDL_CreateThread(redraw_func, NULL) != NULL);
612     if (!redraw_thread_active) {
613     printf("FATAL: cannot create redraw thread\n");
614     return false;
615     }
616     return true;
617     }
618    
619     bool VideoInit(bool classic)
620     {
621     classic_mode = classic;
622    
623     #ifdef ENABLE_VOSF
624     // Zero the mainBuffer structure
625     mainBuffer.dirtyPages = NULL;
626     mainBuffer.pageInfo = NULL;
627     #endif
628    
629     // Create Mutexes
630     if ((sdl_palette_lock = SDL_CreateMutex()) == NULL)
631     return false;
632     if ((frame_buffer_lock = SDL_CreateMutex()) == NULL)
633     return false;
634    
635     // Init keycode translation
636     keycode_init();
637    
638     // Read prefs
639     frame_skip = PrefsFindInt32("frameskip");
640     mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
641     mouse_wheel_lines = PrefsFindInt32("mousewheellines");
642    
643     // Get screen mode from preferences
644     const char *mode_str;
645     if (classic_mode)
646     mode_str = "win/512/342";
647     else
648     mode_str = PrefsFindString("screen");
649    
650     // Determine display type and default dimensions
651     int default_width = 512, default_height = 384;
652     display_type = DISPLAY_WINDOW;
653     if (mode_str) {
654     if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
655     display_type = DISPLAY_WINDOW;
656     }
657     int max_width = 640, max_height = 480;
658     if (display_type == DISPLAY_SCREEN) {
659     SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
660     if (modes && modes != (SDL_Rect **)-1) {
661     max_width = modes[0]->w;
662     max_height = modes[0]->h;
663     }
664     }
665     if (default_width <= 0)
666     default_width = max_width;
667     if (default_height <= 0)
668     default_height = max_height;
669    
670     // Mac screen depth follows X depth
671     screen_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
672     video_depth default_depth;
673     switch (screen_depth) {
674     case 8:
675     default_depth = VDEPTH_8BIT;
676     break;
677     case 15: case 16:
678     default_depth = VDEPTH_16BIT;
679     break;
680     case 24: case 32:
681     default_depth = VDEPTH_32BIT;
682     break;
683     default:
684     default_depth = VDEPTH_1BIT;
685     break;
686     }
687    
688     // Construct list of supported modes
689     if (display_type == DISPLAY_WINDOW) {
690     if (classic)
691     add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
692     else {
693     for (int d = VDEPTH_1BIT; d <= default_depth; d++) {
694     int bpp = (d <= VDEPTH_8BIT ? 8 : sdl_depth_of_video_depth(d));
695     if (SDL_VideoModeOK(max_width, max_height, bpp, SDL_HWSURFACE))
696     add_window_modes(video_depth(d));
697     }
698     }
699     } else
700     add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
701     if (VideoModes.empty()) {
702     ErrorAlert(STR_NO_XVISUAL_ERR);
703     return false;
704     }
705    
706     // Find requested default mode with specified dimensions
707     uint32 default_id;
708     std::vector<video_mode>::const_iterator i, end = VideoModes.end();
709     for (i = VideoModes.begin(); i != end; ++i) {
710     if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
711     default_id = i->resolution_id;
712     break;
713     }
714     }
715     if (i == end) { // not found, use first available mode
716     default_depth = VideoModes[0].depth;
717     default_id = VideoModes[0].resolution_id;
718     }
719    
720     #if DEBUG
721     D(bug("Available video modes:\n"));
722     for (i = VideoModes.begin(); i != end; ++i) {
723     int bits = 1 << i->depth;
724     if (bits == 16)
725     bits = 15;
726     else if (bits == 32)
727     bits = 24;
728     D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
729     }
730     #endif
731    
732     // Create SDL_monitor_desc for this (the only) display
733     SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, default_depth, default_id);
734     VideoMonitors.push_back(monitor);
735    
736     // Open display
737     return monitor->video_open();
738     }
739    
740    
741     /*
742     * Deinitialization
743     */
744    
745     // Close display
746     void SDL_monitor_desc::video_close(void)
747     {
748     D(bug("video_close()\n"));
749    
750     // Stop redraw thread
751     if (redraw_thread_active) {
752     redraw_thread_cancel = true;
753     // SDL_WaitThread(redraw_thread, NULL); doesn't work
754     while (redraw_thread_cancel);
755     }
756     redraw_thread_active = false;
757    
758     // Unlock frame buffer
759     UNLOCK_FRAME_BUFFER;
760     D(bug(" frame buffer unlocked\n"));
761    
762     #ifdef ENABLE_VOSF
763     if (use_vosf) {
764     // Deinitialize VOSF
765     video_vosf_exit();
766     }
767     #endif
768    
769     // Close display
770     delete drv;
771     drv = NULL;
772     }
773    
774     void VideoExit(void)
775     {
776     // Close displays
777     vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
778     for (i = VideoMonitors.begin(); i != end; ++i)
779     dynamic_cast<SDL_monitor_desc *>(*i)->video_close();
780    
781     // Destroy locks
782     if (frame_buffer_lock)
783     SDL_DestroyMutex(frame_buffer_lock);
784     if (sdl_palette_lock)
785     SDL_DestroyMutex(sdl_palette_lock);
786     }
787    
788    
789     /*
790     * Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode)
791     */
792    
793     void VideoQuitFullScreen(void)
794     {
795     D(bug("VideoQuitFullScreen()\n"));
796     quit_full_screen = true;
797     }
798    
799    
800     /*
801     * Mac VBL interrupt
802     */
803    
804     void VideoInterrupt(void)
805     {
806 gbeauche 1.2 // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
807     SDL_PumpEvents();
808    
809 gbeauche 1.1 // Emergency quit requested? Then quit
810     if (emerg_quit)
811     QuitEmulator();
812    
813     // Temporarily give up frame buffer lock (this is the point where
814     // we are suspended when the user presses Ctrl-Tab)
815     UNLOCK_FRAME_BUFFER;
816     LOCK_FRAME_BUFFER;
817     }
818    
819    
820     /*
821     * Set palette
822     */
823    
824     void SDL_monitor_desc::set_palette(uint8 *pal, int num_in)
825     {
826     const video_mode &mode = get_current_mode();
827    
828     // FIXME: how can we handle the gamma ramp?
829     if (mode.depth > VDEPTH_8BIT)
830     return;
831    
832     LOCK_PALETTE;
833    
834     // Convert colors to XColor array
835     int num_out = 256;
836     bool stretch = false;
837     SDL_Color *p = sdl_palette;
838     for (int i=0; i<num_out; i++) {
839     int c = (stretch ? (i * num_in) / num_out : i);
840     p->r = pal[c*3 + 0] * 0x0101;
841     p->g = pal[c*3 + 1] * 0x0101;
842     p->b = pal[c*3 + 2] * 0x0101;
843     p++;
844     }
845    
846     // Recalculate pixel color expansion map
847     if (!IsDirectMode(mode)) {
848     for (int i=0; i<256; i++) {
849     int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
850     ExpandMap[i] = SDL_MapRGB(drv->s->format, pal[c*3+0], pal[c*3+1], pal[c*3+2]);
851     }
852    
853     #ifdef ENABLE_VOSF
854     // We have to redraw everything because the interpretation of pixel values changed
855     LOCK_VOSF;
856     PFLAG_SET_ALL;
857     UNLOCK_VOSF;
858     memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
859     #endif
860     }
861    
862     // Tell redraw thread to change palette
863     sdl_palette_changed = true;
864    
865     UNLOCK_PALETTE;
866     }
867    
868    
869     /*
870     * Switch video mode
871     */
872    
873     void SDL_monitor_desc::switch_to_current_mode(void)
874     {
875     // Close and reopen display
876     video_close();
877     video_open();
878    
879     if (drv == NULL) {
880     ErrorAlert(STR_OPEN_WINDOW_ERR);
881     QuitEmulator();
882     }
883     }
884    
885    
886     /*
887     * Translate key event to Mac keycode, returns -1 if no keycode was found
888     * and -2 if the key was recognized as a hotkey
889     */
890    
891     static bool is_ctrl_down(SDL_keysym const & ks)
892     {
893     return ctrl_down || (ks.mod & KMOD_CTRL);
894     }
895    
896     static int kc_decode(SDL_keysym const & ks, bool key_down)
897     {
898     switch (ks.sym) {
899     case SDLK_a: return 0x00;
900     case SDLK_b: return 0x0b;
901     case SDLK_c: return 0x08;
902     case SDLK_d: return 0x02;
903     case SDLK_e: return 0x0e;
904     case SDLK_f: return 0x03;
905     case SDLK_g: return 0x05;
906     case SDLK_h: return 0x04;
907     case SDLK_i: return 0x22;
908     case SDLK_j: return 0x26;
909     case SDLK_k: return 0x28;
910     case SDLK_l: return 0x25;
911     case SDLK_m: return 0x2e;
912     case SDLK_n: return 0x2d;
913     case SDLK_o: return 0x1f;
914     case SDLK_p: return 0x23;
915     case SDLK_q: return 0x0c;
916     case SDLK_r: return 0x0f;
917     case SDLK_s: return 0x01;
918     case SDLK_t: return 0x11;
919     case SDLK_u: return 0x20;
920     case SDLK_v: return 0x09;
921     case SDLK_w: return 0x0d;
922     case SDLK_x: return 0x07;
923     case SDLK_y: return 0x10;
924     case SDLK_z: return 0x06;
925    
926     case SDLK_1: case SDLK_EXCLAIM: return 0x12;
927     case SDLK_2: case SDLK_AT: return 0x13;
928     // case SDLK_3: case SDLK_numbersign: return 0x14;
929     case SDLK_4: case SDLK_DOLLAR: return 0x15;
930     // case SDLK_5: case SDLK_percent: return 0x17;
931     case SDLK_6: return 0x16;
932     case SDLK_7: return 0x1a;
933     case SDLK_8: return 0x1c;
934     case SDLK_9: return 0x19;
935     case SDLK_0: return 0x1d;
936    
937     // case SDLK_BACKQUOTE: case SDLK_asciitilde: return 0x0a;
938     case SDLK_MINUS: case SDLK_UNDERSCORE: return 0x1b;
939     case SDLK_EQUALS: case SDLK_PLUS: return 0x18;
940     // case SDLK_bracketleft: case SDLK_braceleft: return 0x21;
941     // case SDLK_bracketright: case SDLK_braceright: return 0x1e;
942     // case SDLK_BACKSLASH: case SDLK_bar: return 0x2a;
943     case SDLK_SEMICOLON: case SDLK_COLON: return 0x29;
944     // case SDLK_apostrophe: case SDLK_QUOTEDBL: return 0x27;
945     case SDLK_COMMA: case SDLK_LESS: return 0x2b;
946     case SDLK_PERIOD: case SDLK_GREATER: return 0x2f;
947     case SDLK_SLASH: case SDLK_QUESTION: return 0x2c;
948    
949     case SDLK_TAB: if (is_ctrl_down(ks)) {if (!key_down) drv->suspend(); return -2;} else return 0x30;
950     case SDLK_RETURN: return 0x24;
951     case SDLK_SPACE: return 0x31;
952     case SDLK_BACKSPACE: return 0x33;
953    
954     case SDLK_DELETE: return 0x75;
955     case SDLK_INSERT: return 0x72;
956     case SDLK_HOME: case SDLK_HELP: return 0x73;
957     case SDLK_END: return 0x77;
958     case SDLK_PAGEUP: return 0x74;
959     case SDLK_PAGEDOWN: return 0x79;
960    
961     case SDLK_LCTRL: return 0x36;
962     case SDLK_RCTRL: return 0x36;
963     case SDLK_LSHIFT: return 0x38;
964     case SDLK_RSHIFT: return 0x38;
965     case SDLK_LALT: return 0x37;
966     case SDLK_RALT: return 0x37;
967     case SDLK_LMETA: return 0x3a;
968     case SDLK_RMETA: return 0x3a;
969     case SDLK_MENU: return 0x32;
970     case SDLK_CAPSLOCK: return 0x39;
971     case SDLK_NUMLOCK: return 0x47;
972    
973     case SDLK_UP: return 0x3e;
974     case SDLK_DOWN: return 0x3d;
975     case SDLK_LEFT: return 0x3b;
976     case SDLK_RIGHT: return 0x3c;
977    
978     case SDLK_ESCAPE: if (is_ctrl_down(ks)) {if (!key_down) { quit_full_screen = true; emerg_quit = true; } return -2;} else return 0x35;
979    
980     case SDLK_F1: if (is_ctrl_down(ks)) {if (!key_down) SysMountFirstFloppy(); return -2;} else return 0x7a;
981     case SDLK_F2: return 0x78;
982     case SDLK_F3: return 0x63;
983     case SDLK_F4: return 0x76;
984     case SDLK_F5: if (is_ctrl_down(ks)) {if (!key_down) drv->toggle_mouse_grab(); return -2;} else return 0x60;
985     case SDLK_F6: return 0x61;
986     case SDLK_F7: return 0x62;
987     case SDLK_F8: return 0x64;
988     case SDLK_F9: return 0x65;
989     case SDLK_F10: return 0x6d;
990     case SDLK_F11: return 0x67;
991     case SDLK_F12: return 0x6f;
992    
993     case SDLK_PRINT: return 0x69;
994     case SDLK_SCROLLOCK: return 0x6b;
995     case SDLK_PAUSE: return 0x71;
996    
997     case SDLK_KP0: return 0x52;
998     case SDLK_KP1: return 0x53;
999     case SDLK_KP2: return 0x54;
1000     case SDLK_KP3: return 0x55;
1001     case SDLK_KP4: return 0x56;
1002     case SDLK_KP5: return 0x57;
1003     case SDLK_KP6: return 0x58;
1004     case SDLK_KP7: return 0x59;
1005     case SDLK_KP8: return 0x5b;
1006     case SDLK_KP9: return 0x5c;
1007     case SDLK_KP_PERIOD: return 0x41;
1008     case SDLK_KP_PLUS: return 0x45;
1009     case SDLK_KP_MINUS: return 0x4e;
1010     case SDLK_KP_MULTIPLY: return 0x43;
1011     case SDLK_KP_DIVIDE: return 0x4b;
1012     case SDLK_KP_ENTER: return 0x4c;
1013     case SDLK_KP_EQUALS: return 0x51;
1014     }
1015     D(bug("Unhandled SDL keysym: %d\n", ks.sym));
1016     return -1;
1017     }
1018    
1019     static int event2keycode(SDL_KeyboardEvent const &ev, bool key_down)
1020     {
1021     return kc_decode(ev.keysym, key_down);
1022     }
1023    
1024    
1025     /*
1026     * SDL event handling
1027     */
1028    
1029     static void handle_events(void)
1030     {
1031 gbeauche 1.2 SDL_Event events[10];
1032     const int n_max_events = sizeof(events) / sizeof(events[0]);
1033     int n_events;
1034    
1035     while ((n_events = SDL_PeepEvents(events, n_max_events, SDL_GETEVENT, sdl_eventmask)) > 0) {
1036     for (int i = 0; i < n_events; i++) {
1037     SDL_Event const & event = events[i];
1038     switch (event.type) {
1039 gbeauche 1.1
1040     // Mouse button
1041     case SDL_MOUSEBUTTONDOWN: {
1042     unsigned int button = event.button.button;
1043     if (button < 4)
1044     ADBMouseDown(button - 1);
1045     else if (button < 6) { // Wheel mouse
1046     if (mouse_wheel_mode == 0) {
1047     int key = (button == 5) ? 0x79 : 0x74; // Page up/down
1048     ADBKeyDown(key);
1049     ADBKeyUp(key);
1050     } else {
1051     int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down
1052     for(int i=0; i<mouse_wheel_lines; i++) {
1053     ADBKeyDown(key);
1054     ADBKeyUp(key);
1055     }
1056     }
1057     }
1058     break;
1059     }
1060     case SDL_MOUSEBUTTONUP: {
1061     unsigned int button = event.button.button;
1062     if (button < 4)
1063     ADBMouseUp(button - 1);
1064     break;
1065     }
1066    
1067     // Mouse moved
1068     case SDL_MOUSEMOTION:
1069     drv->mouse_moved(event.motion.x, event.motion.y);
1070     break;
1071    
1072     // Keyboard
1073     case SDL_KEYDOWN: {
1074     int code = -1;
1075     if (use_keycodes) {
1076     if (event2keycode(event.key, true) != -2) // This is called to process the hotkeys
1077     code = keycode_table[event.key.keysym.scancode & 0xff];
1078     } else
1079     code = event2keycode(event.key, true);
1080     if (code >= 0) {
1081     if (!emul_suspended) {
1082     if (code == 0x39) { // Caps Lock pressed
1083     if (caps_on) {
1084     ADBKeyUp(code);
1085     caps_on = false;
1086     } else {
1087     ADBKeyDown(code);
1088     caps_on = true;
1089     }
1090     } else
1091     ADBKeyDown(code);
1092     if (code == 0x36)
1093     ctrl_down = true;
1094     } else {
1095     if (code == 0x31)
1096     drv->resume(); // Space wakes us up
1097     }
1098     }
1099     break;
1100     }
1101     case SDL_KEYUP: {
1102     int code = -1;
1103     if (use_keycodes) {
1104     if (event2keycode(event.key, false) != -2) // This is called to process the hotkeys
1105     code = keycode_table[event.key.keysym.scancode & 0xff];
1106     } else
1107     code = event2keycode(event.key, false);
1108     if (code >= 0 && code != 0x39) { // Don't propagate Caps Lock releases
1109     ADBKeyUp(code);
1110     if (code == 0x36)
1111     ctrl_down = false;
1112     }
1113     break;
1114     }
1115    
1116     // Hidden parts exposed, force complete refresh of window
1117     case SDL_VIDEOEXPOSE:
1118     if (display_type == DISPLAY_WINDOW) {
1119     const video_mode &mode = VideoMonitors[0]->get_current_mode();
1120     #ifdef ENABLE_VOSF
1121     if (use_vosf) { // VOSF refresh
1122     LOCK_VOSF;
1123     PFLAG_SET_ALL;
1124     UNLOCK_VOSF;
1125     memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1126     }
1127     else
1128     #endif
1129     memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1130     }
1131     break;
1132    
1133     // Window "close" widget clicked
1134     case SDL_QUIT:
1135     ADBKeyDown(0x7f); // Power key
1136     ADBKeyUp(0x7f);
1137     break;
1138 gbeauche 1.2 }
1139 gbeauche 1.1 }
1140     }
1141     }
1142    
1143    
1144     /*
1145     * Window display update
1146     */
1147    
1148     // Static display update (fixed frame rate, but incremental)
1149     static void update_display_static(driver_window *drv)
1150     {
1151     // Incremental update code
1152     int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1153     const video_mode &mode = drv->mode;
1154     int bytes_per_row = mode.bytes_per_row;
1155     uint8 *p, *p2;
1156    
1157     // Check for first line from top and first line from bottom that have changed
1158     y1 = 0;
1159     for (j=0; j<mode.y; j++) {
1160     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1161     y1 = j;
1162     break;
1163     }
1164     }
1165     y2 = y1 - 1;
1166     for (j=mode.y-1; j>=y1; j--) {
1167     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1168     y2 = j;
1169     break;
1170     }
1171     }
1172     high = y2 - y1 + 1;
1173    
1174     // Check for first column from left and first column from right that have changed
1175     if (high) {
1176     if (mode.depth < VDEPTH_8BIT) {
1177     const int src_bytes_per_row = bytes_per_row;
1178     const int dst_bytes_per_row = drv->s->pitch;
1179     const int pixels_per_byte = mode.x / src_bytes_per_row;
1180    
1181     x1 = mode.x / pixels_per_byte;
1182     for (j = y1; j <= y2; j++) {
1183     p = &the_buffer[j * bytes_per_row];
1184     p2 = &the_buffer_copy[j * bytes_per_row];
1185     for (i = 0; i < x1; i++) {
1186     if (*p != *p2) {
1187     x1 = i;
1188     break;
1189     }
1190     p++; p2++;
1191     }
1192     }
1193     x2 = x1;
1194     for (j = y1; j <= y2; j++) {
1195     p = &the_buffer[j * bytes_per_row];
1196     p2 = &the_buffer_copy[j * bytes_per_row];
1197     p += bytes_per_row;
1198     p2 += bytes_per_row;
1199     for (i = (mode.x / pixels_per_byte); i > x2; i--) {
1200     p--; p2--;
1201     if (*p != *p2) {
1202     x2 = i;
1203     break;
1204     }
1205     }
1206     }
1207     x1 *= pixels_per_byte;
1208     x2 *= pixels_per_byte;
1209     wide = (x2 - x1 + pixels_per_byte - 1) & -pixels_per_byte;
1210    
1211     // Update copy of the_buffer
1212     if (high && wide) {
1213    
1214     // Lock surface, if required
1215     if (SDL_MUSTLOCK(drv->s))
1216     SDL_LockSurface(drv->s);
1217    
1218     // Blit to screen surface
1219     int si = y1 * src_bytes_per_row + (x1 / pixels_per_byte);
1220     int di = y1 * dst_bytes_per_row + x1;
1221     for (j = y1; j <= y2; j++) {
1222     memcpy(the_buffer_copy + si, the_buffer + si, wide / pixels_per_byte);
1223     Screen_blit((uint8 *)drv->s->pixels + di, the_buffer + si, wide / pixels_per_byte);
1224     si += src_bytes_per_row;
1225     di += dst_bytes_per_row;
1226     }
1227    
1228     // Unlock surface, if required
1229     if (SDL_MUSTLOCK(drv->s))
1230     SDL_UnlockSurface(drv->s);
1231    
1232     // Refresh display
1233     SDL_UpdateRect(drv->s, x1, y1, wide, high);
1234     }
1235    
1236     } else {
1237     const int bytes_per_pixel = mode.bytes_per_row / mode.x;
1238    
1239     x1 = mode.x;
1240     for (j=y1; j<=y2; j++) {
1241     p = &the_buffer[j * bytes_per_row];
1242     p2 = &the_buffer_copy[j * bytes_per_row];
1243     for (i=0; i<x1*bytes_per_pixel; i++) {
1244     if (*p != *p2) {
1245     x1 = i / bytes_per_pixel;
1246     break;
1247     }
1248     p++; p2++;
1249     }
1250     }
1251     x2 = x1;
1252     for (j=y1; j<=y2; j++) {
1253     p = &the_buffer[j * bytes_per_row];
1254     p2 = &the_buffer_copy[j * bytes_per_row];
1255     p += bytes_per_row;
1256     p2 += bytes_per_row;
1257     for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1258     p--;
1259     p2--;
1260     if (*p != *p2) {
1261     x2 = i / bytes_per_pixel;
1262     break;
1263     }
1264     }
1265     }
1266     wide = x2 - x1;
1267    
1268     // Update copy of the_buffer
1269     if (high && wide) {
1270    
1271     // Lock surface, if required
1272     if (SDL_MUSTLOCK(drv->s))
1273     SDL_LockSurface(drv->s);
1274    
1275     // Blit to screen surface
1276     for (j=y1; j<=y2; j++) {
1277     i = j * bytes_per_row + x1 * bytes_per_pixel;
1278     memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1279     Screen_blit((uint8 *)drv->s->pixels + i, the_buffer + i, bytes_per_pixel * wide);
1280     }
1281    
1282     // Unlock surface, if required
1283     if (SDL_MUSTLOCK(drv->s))
1284     SDL_UnlockSurface(drv->s);
1285    
1286     // Refresh display
1287     SDL_UpdateRect(drv->s, x1, y1, wide, high);
1288     }
1289     }
1290     }
1291     }
1292    
1293    
1294     // We suggest the compiler to inline the next two functions so that it
1295     // may specialise the code according to the current screen depth and
1296     // display type. A clever compiler would do that job by itself though...
1297    
1298     // NOTE: update_display_vosf is inlined too
1299    
1300     static inline void possibly_quit_dga_mode()
1301     {
1302     // Quit DGA mode if requested (something terrible has happened and we
1303     // want to give control back to the user)
1304     if (quit_full_screen) {
1305     quit_full_screen = false;
1306     delete drv;
1307     drv = NULL;
1308     }
1309     }
1310    
1311     static inline void possibly_ungrab_mouse()
1312     {
1313     // Ungrab mouse if requested (something terrible has happened and we
1314     // want to give control back to the user)
1315     if (quit_full_screen) {
1316     quit_full_screen = false;
1317     if (drv)
1318     drv->ungrab_mouse();
1319     }
1320     }
1321    
1322     static inline void handle_palette_changes(void)
1323     {
1324     LOCK_PALETTE;
1325    
1326     if (sdl_palette_changed) {
1327     sdl_palette_changed = false;
1328     drv->update_palette();
1329     }
1330    
1331     UNLOCK_PALETTE;
1332     }
1333    
1334     static void video_refresh_dga(void)
1335     {
1336     // Quit DGA mode if requested
1337     possibly_quit_dga_mode();
1338     }
1339    
1340     #ifdef ENABLE_VOSF
1341     #if REAL_ADDRESSING || DIRECT_ADDRESSING
1342     static void video_refresh_dga_vosf(void)
1343     {
1344     // Quit DGA mode if requested
1345     possibly_quit_dga_mode();
1346    
1347     // Update display (VOSF variant)
1348     static int tick_counter = 0;
1349     if (++tick_counter >= frame_skip) {
1350     tick_counter = 0;
1351     if (mainBuffer.dirty) {
1352     LOCK_VOSF;
1353     update_display_dga_vosf();
1354     UNLOCK_VOSF;
1355     }
1356     }
1357     }
1358     #endif
1359    
1360     static void video_refresh_window_vosf(void)
1361     {
1362     // Ungrab mouse if requested
1363     possibly_ungrab_mouse();
1364    
1365     // Update display (VOSF variant)
1366     static int tick_counter = 0;
1367     if (++tick_counter >= frame_skip) {
1368     tick_counter = 0;
1369     if (mainBuffer.dirty) {
1370     LOCK_VOSF;
1371     update_display_window_vosf(static_cast<driver_window *>(drv));
1372     UNLOCK_VOSF;
1373     }
1374     }
1375     }
1376     #endif // def ENABLE_VOSF
1377    
1378     static void video_refresh_window_static(void)
1379     {
1380     // Ungrab mouse if requested
1381     possibly_ungrab_mouse();
1382    
1383     // Update display (static variant)
1384     static int tick_counter = 0;
1385     if (++tick_counter >= frame_skip) {
1386     tick_counter = 0;
1387     update_display_static(static_cast<driver_window *>(drv));
1388     }
1389     }
1390    
1391    
1392     /*
1393     * Thread for screen refresh, input handling etc.
1394     */
1395    
1396     static void VideoRefreshInit(void)
1397     {
1398     // TODO: set up specialised 8bpp VideoRefresh handlers ?
1399     if (display_type == DISPLAY_SCREEN) {
1400     #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
1401     if (use_vosf)
1402     video_refresh = video_refresh_dga_vosf;
1403     else
1404     #endif
1405     video_refresh = video_refresh_dga;
1406     }
1407     else {
1408     #ifdef ENABLE_VOSF
1409     if (use_vosf)
1410     video_refresh = video_refresh_window_vosf;
1411     else
1412     #endif
1413     video_refresh = video_refresh_window_static;
1414     }
1415     }
1416    
1417     static int redraw_func(void *arg)
1418     {
1419     uint64 start = GetTicks_usec();
1420     int64 ticks = 0;
1421    
1422     while (!redraw_thread_cancel) {
1423    
1424     // Wait
1425     Delay_usec(16667);
1426    
1427     // Handle SDL events
1428     handle_events();
1429    
1430     // Refresh display
1431     video_refresh();
1432     ticks++;
1433    
1434     // Set new palette if it was changed
1435     handle_palette_changes();
1436     }
1437    
1438     uint64 end = GetTicks_usec();
1439     D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1440     redraw_thread_cancel = false;
1441     return 0;
1442     }