ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.3
Committed: 2004-06-23T22:55:47Z (20 years ago) by gbeauche
Branch: MAIN
Changes since 1.2: +2 -4 lines
Log Message:
SDL_WaitThread() works better when the thread arg is valid. ;-)

File Contents

# Content
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 * - 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 */
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 static const int sdl_eventmask = SDL_MOUSEBUTTONDOWNMASK | SDL_MOUSEBUTTONUPMASK | SDL_MOUSEMOTIONMASK | SDL_KEYUPMASK | SDL_KEYDOWNMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK;
109
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 = ((redraw_thread = 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);
754 }
755 redraw_thread_active = false;
756
757 // Unlock frame buffer
758 UNLOCK_FRAME_BUFFER;
759 D(bug(" frame buffer unlocked\n"));
760
761 #ifdef ENABLE_VOSF
762 if (use_vosf) {
763 // Deinitialize VOSF
764 video_vosf_exit();
765 }
766 #endif
767
768 // Close display
769 delete drv;
770 drv = NULL;
771 }
772
773 void VideoExit(void)
774 {
775 // Close displays
776 vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
777 for (i = VideoMonitors.begin(); i != end; ++i)
778 dynamic_cast<SDL_monitor_desc *>(*i)->video_close();
779
780 // Destroy locks
781 if (frame_buffer_lock)
782 SDL_DestroyMutex(frame_buffer_lock);
783 if (sdl_palette_lock)
784 SDL_DestroyMutex(sdl_palette_lock);
785 }
786
787
788 /*
789 * Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode)
790 */
791
792 void VideoQuitFullScreen(void)
793 {
794 D(bug("VideoQuitFullScreen()\n"));
795 quit_full_screen = true;
796 }
797
798
799 /*
800 * Mac VBL interrupt
801 */
802
803 void VideoInterrupt(void)
804 {
805 // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
806 SDL_PumpEvents();
807
808 // Emergency quit requested? Then quit
809 if (emerg_quit)
810 QuitEmulator();
811
812 // Temporarily give up frame buffer lock (this is the point where
813 // we are suspended when the user presses Ctrl-Tab)
814 UNLOCK_FRAME_BUFFER;
815 LOCK_FRAME_BUFFER;
816 }
817
818
819 /*
820 * Set palette
821 */
822
823 void SDL_monitor_desc::set_palette(uint8 *pal, int num_in)
824 {
825 const video_mode &mode = get_current_mode();
826
827 // FIXME: how can we handle the gamma ramp?
828 if (mode.depth > VDEPTH_8BIT)
829 return;
830
831 LOCK_PALETTE;
832
833 // Convert colors to XColor array
834 int num_out = 256;
835 bool stretch = false;
836 SDL_Color *p = sdl_palette;
837 for (int i=0; i<num_out; i++) {
838 int c = (stretch ? (i * num_in) / num_out : i);
839 p->r = pal[c*3 + 0] * 0x0101;
840 p->g = pal[c*3 + 1] * 0x0101;
841 p->b = pal[c*3 + 2] * 0x0101;
842 p++;
843 }
844
845 // Recalculate pixel color expansion map
846 if (!IsDirectMode(mode)) {
847 for (int i=0; i<256; i++) {
848 int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
849 ExpandMap[i] = SDL_MapRGB(drv->s->format, pal[c*3+0], pal[c*3+1], pal[c*3+2]);
850 }
851
852 #ifdef ENABLE_VOSF
853 // We have to redraw everything because the interpretation of pixel values changed
854 LOCK_VOSF;
855 PFLAG_SET_ALL;
856 UNLOCK_VOSF;
857 memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
858 #endif
859 }
860
861 // Tell redraw thread to change palette
862 sdl_palette_changed = true;
863
864 UNLOCK_PALETTE;
865 }
866
867
868 /*
869 * Switch video mode
870 */
871
872 void SDL_monitor_desc::switch_to_current_mode(void)
873 {
874 // Close and reopen display
875 video_close();
876 video_open();
877
878 if (drv == NULL) {
879 ErrorAlert(STR_OPEN_WINDOW_ERR);
880 QuitEmulator();
881 }
882 }
883
884
885 /*
886 * Translate key event to Mac keycode, returns -1 if no keycode was found
887 * and -2 if the key was recognized as a hotkey
888 */
889
890 static bool is_ctrl_down(SDL_keysym const & ks)
891 {
892 return ctrl_down || (ks.mod & KMOD_CTRL);
893 }
894
895 static int kc_decode(SDL_keysym const & ks, bool key_down)
896 {
897 switch (ks.sym) {
898 case SDLK_a: return 0x00;
899 case SDLK_b: return 0x0b;
900 case SDLK_c: return 0x08;
901 case SDLK_d: return 0x02;
902 case SDLK_e: return 0x0e;
903 case SDLK_f: return 0x03;
904 case SDLK_g: return 0x05;
905 case SDLK_h: return 0x04;
906 case SDLK_i: return 0x22;
907 case SDLK_j: return 0x26;
908 case SDLK_k: return 0x28;
909 case SDLK_l: return 0x25;
910 case SDLK_m: return 0x2e;
911 case SDLK_n: return 0x2d;
912 case SDLK_o: return 0x1f;
913 case SDLK_p: return 0x23;
914 case SDLK_q: return 0x0c;
915 case SDLK_r: return 0x0f;
916 case SDLK_s: return 0x01;
917 case SDLK_t: return 0x11;
918 case SDLK_u: return 0x20;
919 case SDLK_v: return 0x09;
920 case SDLK_w: return 0x0d;
921 case SDLK_x: return 0x07;
922 case SDLK_y: return 0x10;
923 case SDLK_z: return 0x06;
924
925 case SDLK_1: case SDLK_EXCLAIM: return 0x12;
926 case SDLK_2: case SDLK_AT: return 0x13;
927 // case SDLK_3: case SDLK_numbersign: return 0x14;
928 case SDLK_4: case SDLK_DOLLAR: return 0x15;
929 // case SDLK_5: case SDLK_percent: return 0x17;
930 case SDLK_6: return 0x16;
931 case SDLK_7: return 0x1a;
932 case SDLK_8: return 0x1c;
933 case SDLK_9: return 0x19;
934 case SDLK_0: return 0x1d;
935
936 // case SDLK_BACKQUOTE: case SDLK_asciitilde: return 0x0a;
937 case SDLK_MINUS: case SDLK_UNDERSCORE: return 0x1b;
938 case SDLK_EQUALS: case SDLK_PLUS: return 0x18;
939 // case SDLK_bracketleft: case SDLK_braceleft: return 0x21;
940 // case SDLK_bracketright: case SDLK_braceright: return 0x1e;
941 // case SDLK_BACKSLASH: case SDLK_bar: return 0x2a;
942 case SDLK_SEMICOLON: case SDLK_COLON: return 0x29;
943 // case SDLK_apostrophe: case SDLK_QUOTEDBL: return 0x27;
944 case SDLK_COMMA: case SDLK_LESS: return 0x2b;
945 case SDLK_PERIOD: case SDLK_GREATER: return 0x2f;
946 case SDLK_SLASH: case SDLK_QUESTION: return 0x2c;
947
948 case SDLK_TAB: if (is_ctrl_down(ks)) {if (!key_down) drv->suspend(); return -2;} else return 0x30;
949 case SDLK_RETURN: return 0x24;
950 case SDLK_SPACE: return 0x31;
951 case SDLK_BACKSPACE: return 0x33;
952
953 case SDLK_DELETE: return 0x75;
954 case SDLK_INSERT: return 0x72;
955 case SDLK_HOME: case SDLK_HELP: return 0x73;
956 case SDLK_END: return 0x77;
957 case SDLK_PAGEUP: return 0x74;
958 case SDLK_PAGEDOWN: return 0x79;
959
960 case SDLK_LCTRL: return 0x36;
961 case SDLK_RCTRL: return 0x36;
962 case SDLK_LSHIFT: return 0x38;
963 case SDLK_RSHIFT: return 0x38;
964 case SDLK_LALT: return 0x37;
965 case SDLK_RALT: return 0x37;
966 case SDLK_LMETA: return 0x3a;
967 case SDLK_RMETA: return 0x3a;
968 case SDLK_MENU: return 0x32;
969 case SDLK_CAPSLOCK: return 0x39;
970 case SDLK_NUMLOCK: return 0x47;
971
972 case SDLK_UP: return 0x3e;
973 case SDLK_DOWN: return 0x3d;
974 case SDLK_LEFT: return 0x3b;
975 case SDLK_RIGHT: return 0x3c;
976
977 case SDLK_ESCAPE: if (is_ctrl_down(ks)) {if (!key_down) { quit_full_screen = true; emerg_quit = true; } return -2;} else return 0x35;
978
979 case SDLK_F1: if (is_ctrl_down(ks)) {if (!key_down) SysMountFirstFloppy(); return -2;} else return 0x7a;
980 case SDLK_F2: return 0x78;
981 case SDLK_F3: return 0x63;
982 case SDLK_F4: return 0x76;
983 case SDLK_F5: if (is_ctrl_down(ks)) {if (!key_down) drv->toggle_mouse_grab(); return -2;} else return 0x60;
984 case SDLK_F6: return 0x61;
985 case SDLK_F7: return 0x62;
986 case SDLK_F8: return 0x64;
987 case SDLK_F9: return 0x65;
988 case SDLK_F10: return 0x6d;
989 case SDLK_F11: return 0x67;
990 case SDLK_F12: return 0x6f;
991
992 case SDLK_PRINT: return 0x69;
993 case SDLK_SCROLLOCK: return 0x6b;
994 case SDLK_PAUSE: return 0x71;
995
996 case SDLK_KP0: return 0x52;
997 case SDLK_KP1: return 0x53;
998 case SDLK_KP2: return 0x54;
999 case SDLK_KP3: return 0x55;
1000 case SDLK_KP4: return 0x56;
1001 case SDLK_KP5: return 0x57;
1002 case SDLK_KP6: return 0x58;
1003 case SDLK_KP7: return 0x59;
1004 case SDLK_KP8: return 0x5b;
1005 case SDLK_KP9: return 0x5c;
1006 case SDLK_KP_PERIOD: return 0x41;
1007 case SDLK_KP_PLUS: return 0x45;
1008 case SDLK_KP_MINUS: return 0x4e;
1009 case SDLK_KP_MULTIPLY: return 0x43;
1010 case SDLK_KP_DIVIDE: return 0x4b;
1011 case SDLK_KP_ENTER: return 0x4c;
1012 case SDLK_KP_EQUALS: return 0x51;
1013 }
1014 D(bug("Unhandled SDL keysym: %d\n", ks.sym));
1015 return -1;
1016 }
1017
1018 static int event2keycode(SDL_KeyboardEvent const &ev, bool key_down)
1019 {
1020 return kc_decode(ev.keysym, key_down);
1021 }
1022
1023
1024 /*
1025 * SDL event handling
1026 */
1027
1028 static void handle_events(void)
1029 {
1030 SDL_Event events[10];
1031 const int n_max_events = sizeof(events) / sizeof(events[0]);
1032 int n_events;
1033
1034 while ((n_events = SDL_PeepEvents(events, n_max_events, SDL_GETEVENT, sdl_eventmask)) > 0) {
1035 for (int i = 0; i < n_events; i++) {
1036 SDL_Event const & event = events[i];
1037 switch (event.type) {
1038
1039 // Mouse button
1040 case SDL_MOUSEBUTTONDOWN: {
1041 unsigned int button = event.button.button;
1042 if (button < 4)
1043 ADBMouseDown(button - 1);
1044 else if (button < 6) { // Wheel mouse
1045 if (mouse_wheel_mode == 0) {
1046 int key = (button == 5) ? 0x79 : 0x74; // Page up/down
1047 ADBKeyDown(key);
1048 ADBKeyUp(key);
1049 } else {
1050 int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down
1051 for(int i=0; i<mouse_wheel_lines; i++) {
1052 ADBKeyDown(key);
1053 ADBKeyUp(key);
1054 }
1055 }
1056 }
1057 break;
1058 }
1059 case SDL_MOUSEBUTTONUP: {
1060 unsigned int button = event.button.button;
1061 if (button < 4)
1062 ADBMouseUp(button - 1);
1063 break;
1064 }
1065
1066 // Mouse moved
1067 case SDL_MOUSEMOTION:
1068 drv->mouse_moved(event.motion.x, event.motion.y);
1069 break;
1070
1071 // Keyboard
1072 case SDL_KEYDOWN: {
1073 int code = -1;
1074 if (use_keycodes) {
1075 if (event2keycode(event.key, true) != -2) // This is called to process the hotkeys
1076 code = keycode_table[event.key.keysym.scancode & 0xff];
1077 } else
1078 code = event2keycode(event.key, true);
1079 if (code >= 0) {
1080 if (!emul_suspended) {
1081 if (code == 0x39) { // Caps Lock pressed
1082 if (caps_on) {
1083 ADBKeyUp(code);
1084 caps_on = false;
1085 } else {
1086 ADBKeyDown(code);
1087 caps_on = true;
1088 }
1089 } else
1090 ADBKeyDown(code);
1091 if (code == 0x36)
1092 ctrl_down = true;
1093 } else {
1094 if (code == 0x31)
1095 drv->resume(); // Space wakes us up
1096 }
1097 }
1098 break;
1099 }
1100 case SDL_KEYUP: {
1101 int code = -1;
1102 if (use_keycodes) {
1103 if (event2keycode(event.key, false) != -2) // This is called to process the hotkeys
1104 code = keycode_table[event.key.keysym.scancode & 0xff];
1105 } else
1106 code = event2keycode(event.key, false);
1107 if (code >= 0 && code != 0x39) { // Don't propagate Caps Lock releases
1108 ADBKeyUp(code);
1109 if (code == 0x36)
1110 ctrl_down = false;
1111 }
1112 break;
1113 }
1114
1115 // Hidden parts exposed, force complete refresh of window
1116 case SDL_VIDEOEXPOSE:
1117 if (display_type == DISPLAY_WINDOW) {
1118 const video_mode &mode = VideoMonitors[0]->get_current_mode();
1119 #ifdef ENABLE_VOSF
1120 if (use_vosf) { // VOSF refresh
1121 LOCK_VOSF;
1122 PFLAG_SET_ALL;
1123 UNLOCK_VOSF;
1124 memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1125 }
1126 else
1127 #endif
1128 memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1129 }
1130 break;
1131
1132 // Window "close" widget clicked
1133 case SDL_QUIT:
1134 ADBKeyDown(0x7f); // Power key
1135 ADBKeyUp(0x7f);
1136 break;
1137 }
1138 }
1139 }
1140 }
1141
1142
1143 /*
1144 * Window display update
1145 */
1146
1147 // Static display update (fixed frame rate, but incremental)
1148 static void update_display_static(driver_window *drv)
1149 {
1150 // Incremental update code
1151 int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1152 const video_mode &mode = drv->mode;
1153 int bytes_per_row = mode.bytes_per_row;
1154 uint8 *p, *p2;
1155
1156 // Check for first line from top and first line from bottom that have changed
1157 y1 = 0;
1158 for (j=0; j<mode.y; j++) {
1159 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1160 y1 = j;
1161 break;
1162 }
1163 }
1164 y2 = y1 - 1;
1165 for (j=mode.y-1; j>=y1; j--) {
1166 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1167 y2 = j;
1168 break;
1169 }
1170 }
1171 high = y2 - y1 + 1;
1172
1173 // Check for first column from left and first column from right that have changed
1174 if (high) {
1175 if (mode.depth < VDEPTH_8BIT) {
1176 const int src_bytes_per_row = bytes_per_row;
1177 const int dst_bytes_per_row = drv->s->pitch;
1178 const int pixels_per_byte = mode.x / src_bytes_per_row;
1179
1180 x1 = mode.x / pixels_per_byte;
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 for (i = 0; i < x1; i++) {
1185 if (*p != *p2) {
1186 x1 = i;
1187 break;
1188 }
1189 p++; p2++;
1190 }
1191 }
1192 x2 = x1;
1193 for (j = y1; j <= y2; j++) {
1194 p = &the_buffer[j * bytes_per_row];
1195 p2 = &the_buffer_copy[j * bytes_per_row];
1196 p += bytes_per_row;
1197 p2 += bytes_per_row;
1198 for (i = (mode.x / pixels_per_byte); i > x2; i--) {
1199 p--; p2--;
1200 if (*p != *p2) {
1201 x2 = i;
1202 break;
1203 }
1204 }
1205 }
1206 x1 *= pixels_per_byte;
1207 x2 *= pixels_per_byte;
1208 wide = (x2 - x1 + pixels_per_byte - 1) & -pixels_per_byte;
1209
1210 // Update copy of the_buffer
1211 if (high && wide) {
1212
1213 // Lock surface, if required
1214 if (SDL_MUSTLOCK(drv->s))
1215 SDL_LockSurface(drv->s);
1216
1217 // Blit to screen surface
1218 int si = y1 * src_bytes_per_row + (x1 / pixels_per_byte);
1219 int di = y1 * dst_bytes_per_row + x1;
1220 for (j = y1; j <= y2; j++) {
1221 memcpy(the_buffer_copy + si, the_buffer + si, wide / pixels_per_byte);
1222 Screen_blit((uint8 *)drv->s->pixels + di, the_buffer + si, wide / pixels_per_byte);
1223 si += src_bytes_per_row;
1224 di += dst_bytes_per_row;
1225 }
1226
1227 // Unlock surface, if required
1228 if (SDL_MUSTLOCK(drv->s))
1229 SDL_UnlockSurface(drv->s);
1230
1231 // Refresh display
1232 SDL_UpdateRect(drv->s, x1, y1, wide, high);
1233 }
1234
1235 } else {
1236 const int bytes_per_pixel = mode.bytes_per_row / mode.x;
1237
1238 x1 = mode.x;
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 for (i=0; i<x1*bytes_per_pixel; i++) {
1243 if (*p != *p2) {
1244 x1 = i / bytes_per_pixel;
1245 break;
1246 }
1247 p++; p2++;
1248 }
1249 }
1250 x2 = x1;
1251 for (j=y1; j<=y2; j++) {
1252 p = &the_buffer[j * bytes_per_row];
1253 p2 = &the_buffer_copy[j * bytes_per_row];
1254 p += bytes_per_row;
1255 p2 += bytes_per_row;
1256 for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1257 p--;
1258 p2--;
1259 if (*p != *p2) {
1260 x2 = i / bytes_per_pixel;
1261 break;
1262 }
1263 }
1264 }
1265 wide = x2 - x1;
1266
1267 // Update copy of the_buffer
1268 if (high && wide) {
1269
1270 // Lock surface, if required
1271 if (SDL_MUSTLOCK(drv->s))
1272 SDL_LockSurface(drv->s);
1273
1274 // Blit to screen surface
1275 for (j=y1; j<=y2; j++) {
1276 i = j * bytes_per_row + x1 * bytes_per_pixel;
1277 memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1278 Screen_blit((uint8 *)drv->s->pixels + i, the_buffer + i, bytes_per_pixel * wide);
1279 }
1280
1281 // Unlock surface, if required
1282 if (SDL_MUSTLOCK(drv->s))
1283 SDL_UnlockSurface(drv->s);
1284
1285 // Refresh display
1286 SDL_UpdateRect(drv->s, x1, y1, wide, high);
1287 }
1288 }
1289 }
1290 }
1291
1292
1293 // We suggest the compiler to inline the next two functions so that it
1294 // may specialise the code according to the current screen depth and
1295 // display type. A clever compiler would do that job by itself though...
1296
1297 // NOTE: update_display_vosf is inlined too
1298
1299 static inline void possibly_quit_dga_mode()
1300 {
1301 // Quit DGA mode if requested (something terrible has happened and we
1302 // want to give control back to the user)
1303 if (quit_full_screen) {
1304 quit_full_screen = false;
1305 delete drv;
1306 drv = NULL;
1307 }
1308 }
1309
1310 static inline void possibly_ungrab_mouse()
1311 {
1312 // Ungrab mouse if requested (something terrible has happened and we
1313 // want to give control back to the user)
1314 if (quit_full_screen) {
1315 quit_full_screen = false;
1316 if (drv)
1317 drv->ungrab_mouse();
1318 }
1319 }
1320
1321 static inline void handle_palette_changes(void)
1322 {
1323 LOCK_PALETTE;
1324
1325 if (sdl_palette_changed) {
1326 sdl_palette_changed = false;
1327 drv->update_palette();
1328 }
1329
1330 UNLOCK_PALETTE;
1331 }
1332
1333 static void video_refresh_dga(void)
1334 {
1335 // Quit DGA mode if requested
1336 possibly_quit_dga_mode();
1337 }
1338
1339 #ifdef ENABLE_VOSF
1340 #if REAL_ADDRESSING || DIRECT_ADDRESSING
1341 static void video_refresh_dga_vosf(void)
1342 {
1343 // Quit DGA mode if requested
1344 possibly_quit_dga_mode();
1345
1346 // Update display (VOSF variant)
1347 static int tick_counter = 0;
1348 if (++tick_counter >= frame_skip) {
1349 tick_counter = 0;
1350 if (mainBuffer.dirty) {
1351 LOCK_VOSF;
1352 update_display_dga_vosf();
1353 UNLOCK_VOSF;
1354 }
1355 }
1356 }
1357 #endif
1358
1359 static void video_refresh_window_vosf(void)
1360 {
1361 // Ungrab mouse if requested
1362 possibly_ungrab_mouse();
1363
1364 // Update display (VOSF variant)
1365 static int tick_counter = 0;
1366 if (++tick_counter >= frame_skip) {
1367 tick_counter = 0;
1368 if (mainBuffer.dirty) {
1369 LOCK_VOSF;
1370 update_display_window_vosf(static_cast<driver_window *>(drv));
1371 UNLOCK_VOSF;
1372 }
1373 }
1374 }
1375 #endif // def ENABLE_VOSF
1376
1377 static void video_refresh_window_static(void)
1378 {
1379 // Ungrab mouse if requested
1380 possibly_ungrab_mouse();
1381
1382 // Update display (static variant)
1383 static int tick_counter = 0;
1384 if (++tick_counter >= frame_skip) {
1385 tick_counter = 0;
1386 update_display_static(static_cast<driver_window *>(drv));
1387 }
1388 }
1389
1390
1391 /*
1392 * Thread for screen refresh, input handling etc.
1393 */
1394
1395 static void VideoRefreshInit(void)
1396 {
1397 // TODO: set up specialised 8bpp VideoRefresh handlers ?
1398 if (display_type == DISPLAY_SCREEN) {
1399 #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
1400 if (use_vosf)
1401 video_refresh = video_refresh_dga_vosf;
1402 else
1403 #endif
1404 video_refresh = video_refresh_dga;
1405 }
1406 else {
1407 #ifdef ENABLE_VOSF
1408 if (use_vosf)
1409 video_refresh = video_refresh_window_vosf;
1410 else
1411 #endif
1412 video_refresh = video_refresh_window_static;
1413 }
1414 }
1415
1416 static int redraw_func(void *arg)
1417 {
1418 uint64 start = GetTicks_usec();
1419 int64 ticks = 0;
1420
1421 while (!redraw_thread_cancel) {
1422
1423 // Wait
1424 Delay_usec(16667);
1425
1426 // Handle SDL events
1427 handle_events();
1428
1429 // Refresh display
1430 video_refresh();
1431 ticks++;
1432
1433 // Set new palette if it was changed
1434 handle_palette_changes();
1435 }
1436
1437 uint64 end = GetTicks_usec();
1438 D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1439 return 0;
1440 }