ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.1
Committed: 2004-06-23T13:47:20Z (20 years ago) by gbeauche
Branch: MAIN
Log Message:
Initial SDL support.

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