ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.16
Committed: 2005-03-17T00:19:39Z (19 years, 3 months ago) by gbeauche
Branch: MAIN
Changes since 1.15: +4 -0 lines
Log Message:
Default to BasiliskII_keycodes file on Windows if user wants keycodes but
no keycodefile is specified

File Contents

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