ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.11
Committed: 2004-07-02T06:08:01Z (20 years ago) by gbeauche
Branch: MAIN
Changes since 1.10: +0 -363 lines
Log Message:
NQD moved to SheepShaver src/gfxaccel.cpp

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