ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
Revision: 1.7
Committed: 2004-06-26T15:22:01Z (20 years ago) by gbeauche
Branch: MAIN
Changes since 1.6: +39 -23 lines
Log Message:
Add heuristic to deitermine run-time effect of VOSF acceleration, and
disable it if it turns out to not be profitable

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