ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.12
Committed: 2004-07-27T21:40:52Z (19 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.11: +10 -4 lines
Log Message:
Try to fix 16 bpp over 32 bpp on MacOS X.

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