ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/video_x.cpp
Revision: 1.52
Committed: 2010-02-21T09:59:52Z (14 years, 2 months ago) by cebix
Branch: MAIN
CVS Tags: HEAD
Changes since 1.51: +2 -2 lines
Log Message:
fixed warnings

File Contents

# Content
1 /*
2 * video_x.cpp - Video/graphics emulation, X11 specific stuff
3 *
4 * SheepShaver (C) Marc Hellwig and 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
30 #include "sysdeps.h"
31
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
34 #include <X11/keysym.h>
35 #include <X11/extensions/XShm.h>
36 #include <sys/ipc.h>
37 #include <sys/shm.h>
38 #include <errno.h>
39 #include <pthread.h>
40 #include <semaphore.h>
41
42 #include <algorithm>
43
44 #ifdef ENABLE_FBDEV_DGA
45 # include <linux/fb.h>
46 # include <sys/ioctl.h>
47 #endif
48
49 #ifdef ENABLE_XF86_DGA
50 # include <X11/extensions/Xxf86dga.h>
51 #endif
52
53 #ifdef ENABLE_XF86_VIDMODE
54 # include <X11/extensions/xf86vmode.h>
55 #endif
56
57 #ifdef ENABLE_FBDEV_DGA
58 # include <sys/mman.h>
59 #endif
60
61 #include "main.h"
62 #include "adb.h"
63 #include "prefs.h"
64 #include "user_strings.h"
65 #include "about_window.h"
66 #include "video.h"
67 #include "video_defs.h"
68 #include "video_blit.h"
69
70 #define DEBUG 0
71 #include "debug.h"
72
73 #ifndef NO_STD_NAMESPACE
74 using std::sort;
75 #endif
76
77
78 // Constants
79 const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
80 static const bool hw_mac_cursor_accl = true; // Flag: Enable MacOS to X11 copy of cursor?
81
82 // Global variables
83 static int32 frame_skip;
84 static int16 mouse_wheel_mode;
85 static int16 mouse_wheel_lines;
86 static bool redraw_thread_active = false; // Flag: Redraw thread installed
87 static pthread_attr_t redraw_thread_attr; // Redraw thread attributes
88 static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread
89 static pthread_t redraw_thread; // Redraw thread
90
91 static volatile bool thread_stop_req = false;
92 static sem_t thread_stop_ack;
93 static sem_t thread_resume_req;
94
95 static bool local_X11; // Flag: X server running on local machine?
96 static bool has_dga = false; // Flag: Video DGA capable
97 static bool has_vidmode = false; // Flag: VidMode extension available
98
99 #ifdef ENABLE_VOSF
100 static bool use_vosf = true; // Flag: VOSF enabled
101 #else
102 static const bool use_vosf = false; // VOSF not possible
103 #endif
104
105 static bool palette_changed = false; // Flag: Palette changed, redraw thread must update palette
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 volatile bool quit_full_screen_ack = false; // Acknowledge for quit_full_screen
110 static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
111
112 static bool emul_suspended = false; // Flag: emulator suspended
113 static Window suspend_win; // "Suspend" window
114 static void *fb_save = NULL; // Saved frame buffer for suspend
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 // X11 variables
119 static int screen; // Screen number
120 static int xdepth; // Depth of X screen
121 static int depth; // Depth of Mac frame buffer
122 static Window rootwin, the_win; // Root window and our window
123 static int num_depths = 0; // Number of available X depths
124 static int *avail_depths = NULL; // List of available X depths
125 static VisualFormat visualFormat;
126 static XVisualInfo visualInfo;
127 static Visual *vis;
128 static int color_class;
129 static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
130 static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode
131 static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table
132 static int orig_accel_numer, orig_accel_denom, orig_threshold; // Original mouse acceleration
133
134 static XColor black, white;
135 static unsigned long black_pixel, white_pixel;
136 static int eventmask;
137 static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
138 static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
139
140 // Variables for window mode
141 static GC the_gc;
142 static XImage *img = NULL;
143 static XShmSegmentInfo shminfo;
144 static XImage *cursor_image, *cursor_mask_image;
145 static Pixmap cursor_map, cursor_mask_map;
146 static Cursor mac_cursor;
147 static GC cursor_gc, cursor_mask_gc;
148 static bool cursor_changed = false; // Flag: Cursor changed, window_func must update cursor
149 static bool have_shm = false; // Flag: SHM present and usable
150 static uint8 *the_buffer = NULL; // Pointer to Mac frame buffer
151 static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer
152 static uint32 the_buffer_size; // Size of allocated the_buffer
153
154 // Variables for DGA mode
155 static bool is_fbdev_dga_mode = false; // Flag: Use FBDev DGA mode?
156 static int current_dga_cmap;
157
158 #ifdef ENABLE_FBDEV_DGA
159 static int fb_dev_fd = -1; // Handle to fb device name
160 static struct fb_fix_screeninfo fb_finfo; // Fixed info
161 static struct fb_var_screeninfo fb_vinfo; // Variable info
162 static struct fb_var_screeninfo fb_orig_vinfo; // Variable info to restore later
163 static struct fb_cmap fb_oldcmap; // Colormap to restore later
164 #endif
165
166 #ifdef ENABLE_XF86_VIDMODE
167 // Variables for XF86 VidMode support
168 static XF86VidModeModeInfo **x_video_modes; // Array of all available modes
169 static int num_x_video_modes;
170 #endif
171
172 // Mutex to protect palette
173 #if defined(HAVE_PTHREADS)
174 static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER;
175 #define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock)
176 #define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock)
177 #elif defined(HAVE_SPINLOCKS)
178 static spinlock_t x_palette_lock = SPIN_LOCK_UNLOCKED;
179 #define LOCK_PALETTE spin_lock(&x_palette_lock)
180 #define UNLOCK_PALETTE spin_unlock(&x_palette_lock)
181 #else
182 #define LOCK_PALETTE
183 #define UNLOCK_PALETTE
184 #endif
185
186 // Mutex to protect frame buffer
187 #if defined(HAVE_PTHREADS)
188 static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
189 #define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock);
190 #define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock);
191 #elif defined(HAVE_SPINLOCKS)
192 static spinlock_t frame_buffer_lock = SPIN_LOCK_UNLOCKED;
193 #define LOCK_FRAME_BUFFER spin_lock(&frame_buffer_lock)
194 #define UNLOCK_FRAME_BUFFER spin_unlock(&frame_buffer_lock)
195 #else
196 #define LOCK_FRAME_BUFFER
197 #define UNLOCK_FRAME_BUFFER
198 #endif
199
200
201 // Prototypes
202 static void *redraw_func(void *arg);
203
204
205 // From main_unix.cpp
206 extern char *x_display_name;
207 extern Display *x_display;
208
209 // From sys_unix.cpp
210 extern void SysMountFirstFloppy(void);
211
212 // From clip_unix.cpp
213 extern void ClipboardSelectionClear(XSelectionClearEvent *);
214 extern void ClipboardSelectionRequest(XSelectionRequestEvent *);
215
216
217 // Video acceleration through SIGSEGV
218 #ifdef ENABLE_VOSF
219 # include "video_vosf.h"
220 #endif
221
222
223 /*
224 * Utility functions
225 */
226
227 // Get current video mode
228 static inline int get_current_mode(void)
229 {
230 return VModes[cur_mode].viAppleMode;
231 }
232
233 // Find palette size for given color depth
234 static int palette_size(int mode)
235 {
236 switch (mode) {
237 case APPLE_1_BIT: return 2;
238 case APPLE_2_BIT: return 4;
239 case APPLE_4_BIT: return 16;
240 case APPLE_8_BIT: return 256;
241 case APPLE_16_BIT: return 32;
242 case APPLE_32_BIT: return 256;
243 default: return 0;
244 }
245 }
246
247 // Return bits per pixel for requested depth
248 static inline int bytes_per_pixel(int depth)
249 {
250 int bpp;
251 switch (depth) {
252 case 8:
253 bpp = 1;
254 break;
255 case 15: case 16:
256 bpp = 2;
257 break;
258 case 24: case 32:
259 bpp = 4;
260 break;
261 default:
262 abort();
263 }
264 return bpp;
265 }
266
267 // Map video_mode depth ID to numerical depth value
268 static inline int depth_of_video_mode(int mode)
269 {
270 int depth;
271 switch (mode) {
272 case APPLE_1_BIT:
273 depth = 1;
274 break;
275 case APPLE_2_BIT:
276 depth = 2;
277 break;
278 case APPLE_4_BIT:
279 depth = 4;
280 break;
281 case APPLE_8_BIT:
282 depth = 8;
283 break;
284 case APPLE_16_BIT:
285 depth = 16;
286 break;
287 case APPLE_32_BIT:
288 depth = 32;
289 break;
290 default:
291 abort();
292 }
293 return depth;
294 }
295
296 // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
297 static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
298 {
299 return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift);
300 }
301
302
303 // Do we have a visual for handling the specified Mac depth? If so, set the
304 // global variables "xdepth", "visualInfo", "vis" and "color_class".
305 static bool find_visual_for_depth(int depth)
306 {
307 D(bug("have_visual_for_depth(%d)\n", depth_of_video_mode(depth)));
308
309 // 1-bit works always and uses default visual
310 if (depth == APPLE_1_BIT) {
311 vis = DefaultVisual(x_display, screen);
312 visualInfo.visualid = XVisualIDFromVisual(vis);
313 int num = 0;
314 XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num);
315 visualInfo = vi[0];
316 XFree(vi);
317 xdepth = visualInfo.depth;
318 color_class = visualInfo.c_class;
319 D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth));
320 return true;
321 }
322
323 // Calculate minimum and maximum supported X depth
324 int min_depth = 1, max_depth = 32;
325 switch (depth) {
326 #ifdef ENABLE_VOSF
327 case APPLE_2_BIT:
328 case APPLE_4_BIT: // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
329 case APPLE_8_BIT:
330 min_depth = 8;
331 max_depth = 32;
332 break;
333 #else
334 case APPLE_2_BIT:
335 case APPLE_4_BIT: // 2/4-bit requires VOSF blitters
336 return false;
337 case APPLE_8_BIT: // 8-bit without VOSF requires an 8-bit visual
338 min_depth = 8;
339 max_depth = 8;
340 break;
341 #endif
342 case APPLE_16_BIT: // 16-bit requires a 15/16-bit visual
343 min_depth = 15;
344 max_depth = 16;
345 break;
346 case APPLE_32_BIT: // 32-bit requires a 24/32-bit visual
347 min_depth = 24;
348 max_depth = 32;
349 break;
350 }
351 D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth));
352
353 // Try to find a visual for one of the color depths
354 bool visual_found = false;
355 for (int i=0; i<num_depths && !visual_found; i++) {
356
357 xdepth = avail_depths[i];
358 D(bug(" trying to find visual for depth %d\n", xdepth));
359 if (xdepth < min_depth || xdepth > max_depth)
360 continue;
361
362 // Determine best color class for this depth
363 switch (xdepth) {
364 case 1: // Try StaticGray or StaticColor
365 if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo)
366 || XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo))
367 visual_found = true;
368 break;
369 case 8: // Need PseudoColor
370 if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo))
371 visual_found = true;
372 break;
373 case 15:
374 case 16:
375 case 24:
376 case 32: // Try DirectColor first, as this will allow gamma correction
377 if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo)
378 || XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo))
379 visual_found = true;
380 break;
381 default:
382 D(bug(" not a supported depth\n"));
383 break;
384 }
385 }
386 if (!visual_found)
387 return false;
388
389 // Visual was found
390 vis = visualInfo.visual;
391 color_class = visualInfo.c_class;
392 D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth));
393 #if DEBUG
394 switch (color_class) {
395 case StaticGray: D(bug("StaticGray\n")); break;
396 case GrayScale: D(bug("GrayScale\n")); break;
397 case StaticColor: D(bug("StaticColor\n")); break;
398 case PseudoColor: D(bug("PseudoColor\n")); break;
399 case TrueColor: D(bug("TrueColor\n")); break;
400 case DirectColor: D(bug("DirectColor\n")); break;
401 }
402 #endif
403 return true;
404 }
405
406
407 /*
408 * Open display (window or fullscreen)
409 */
410
411 // Set window name and class
412 static void set_window_name(Window w, int name)
413 {
414 const char *str = GetString(name);
415 XStoreName(x_display, w, str);
416 XSetIconName(x_display, w, str);
417
418 XClassHint *hints;
419 hints = XAllocClassHint();
420 if (hints) {
421 hints->res_name = "SheepShaver";
422 hints->res_class = "SheepShaver";
423 XSetClassHint(x_display, w, hints);
424 XFree(hints);
425 }
426 }
427
428 // Set window input focus flag
429 static void set_window_focus(Window w)
430 {
431 XWMHints *hints = XAllocWMHints();
432 if (hints) {
433 hints->input = True;
434 hints->initial_state = NormalState;
435 hints->flags = InputHint | StateHint;
436 XSetWMHints(x_display, w, hints);
437 XFree(hints);
438 }
439 }
440
441 // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
442 static Atom WM_DELETE_WINDOW = (Atom)0;
443 static void set_window_delete_protocol(Window w)
444 {
445 WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
446 XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
447 }
448
449 // Wait until window is mapped/unmapped
450 static void wait_mapped(Window w)
451 {
452 XEvent e;
453 do {
454 XMaskEvent(x_display, StructureNotifyMask, &e);
455 } while ((e.type != MapNotify) || (e.xmap.event != w));
456 }
457
458 static void wait_unmapped(Window w)
459 {
460 XEvent e;
461 do {
462 XMaskEvent(x_display, StructureNotifyMask, &e);
463 } while ((e.type != UnmapNotify) || (e.xmap.event != w));
464 }
465
466 // Disable mouse acceleration
467 static void disable_mouse_accel(void)
468 {
469 XChangePointerControl(x_display, True, False, 1, 1, 0);
470 }
471
472 // Restore mouse acceleration to original value
473 static void restore_mouse_accel(void)
474 {
475 XChangePointerControl(x_display, True, True, orig_accel_numer, orig_accel_denom, orig_threshold);
476 }
477
478 // Trap SHM errors
479 static bool shm_error = false;
480 static int (*old_error_handler)(Display *, XErrorEvent *);
481
482 static int error_handler(Display *d, XErrorEvent *e)
483 {
484 if (e->error_code == BadAccess) {
485 shm_error = true;
486 return 0;
487 } else
488 return old_error_handler(d, e);
489 }
490
491 // Open window
492 static bool open_window(int width, int height)
493 {
494 int aligned_width = (width + 15) & ~15;
495 int aligned_height = (height + 15) & ~15;
496
497 // Set absolute mouse mode
498 ADBSetRelMouseMode(false);
499
500 // Create window
501 XSetWindowAttributes wattr;
502 wattr.event_mask = eventmask = win_eventmask;
503 wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
504 wattr.border_pixel = 0;
505 wattr.backing_store = NotUseful;
506 wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
507 the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
508 InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr);
509
510 // Set window name/class
511 set_window_name(the_win, STR_WINDOW_TITLE);
512
513 // Indicate that we want keyboard input
514 set_window_focus(the_win);
515
516 // Set delete protocol property
517 set_window_delete_protocol(the_win);
518
519 // Make window unresizable
520 XSizeHints *hints;
521 if ((hints = XAllocSizeHints()) != NULL) {
522 hints->min_width = width;
523 hints->max_width = width;
524 hints->min_height = height;
525 hints->max_height = height;
526 hints->flags = PMinSize | PMaxSize;
527 XSetWMNormalHints(x_display, the_win, hints);
528 XFree((char *)hints);
529 }
530
531 // Show window
532 XMapWindow(x_display, the_win);
533 wait_mapped(the_win);
534
535 // 1-bit mode is big-endian; if the X server is little-endian, we can't
536 // use SHM because that doesn't allow changing the image byte order
537 bool need_msb_image = (depth == 1 && XImageByteOrder(x_display) == LSBFirst);
538
539 // Try to create and attach SHM image
540 have_shm = false;
541 if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) {
542
543 // Create SHM image ("height + 2" for safety)
544 img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
545 shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
546 D(bug(" shm image created\n"));
547 the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
548 shminfo.shmaddr = img->data = (char *)the_buffer_copy;
549 shminfo.readOnly = False;
550
551 // Try to attach SHM image, catching errors
552 shm_error = false;
553 old_error_handler = XSetErrorHandler(error_handler);
554 XShmAttach(x_display, &shminfo);
555 XSync(x_display, false);
556 XSetErrorHandler(old_error_handler);
557 if (shm_error) {
558 shmdt(shminfo.shmaddr);
559 XDestroyImage(img);
560 shminfo.shmid = -1;
561 } else {
562 have_shm = true;
563 shmctl(shminfo.shmid, IPC_RMID, 0);
564 }
565 D(bug(" shm image attached\n"));
566 }
567
568 // Create normal X image if SHM doesn't work ("height + 2" for safety)
569 if (!have_shm) {
570 int bytes_per_row = depth == 1 ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth));
571 the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
572 img = XCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
573 D(bug(" X image created\n"));
574 }
575
576 // 1-Bit mode is big-endian
577 if (need_msb_image) {
578 img->byte_order = MSBFirst;
579 img->bitmap_bit_order = MSBFirst;
580 }
581
582 #ifdef ENABLE_VOSF
583 use_vosf = true;
584 // Allocate memory for frame buffer (SIZE is extended to page-boundary)
585 the_host_buffer = the_buffer_copy;
586 the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
587 the_buffer = (uint8 *)vm_acquire(the_buffer_size);
588 the_buffer_copy = (uint8 *)malloc(the_buffer_size);
589 D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
590 #else
591 // Allocate memory for frame buffer
592 the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
593 D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
594 #endif
595 screen_base = Host2MacAddr(the_buffer);
596
597 // Create GC
598 the_gc = XCreateGC(x_display, the_win, 0, 0);
599 XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
600
601 // Create cursor
602 if (hw_mac_cursor_accl) {
603 cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
604 cursor_image->byte_order = MSBFirst;
605 cursor_image->bitmap_bit_order = MSBFirst;
606 cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 36, 16, 16, 16, 2);
607 cursor_mask_image->byte_order = MSBFirst;
608 cursor_mask_image->bitmap_bit_order = MSBFirst;
609 cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
610 cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
611 cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
612 cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
613 mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
614 cursor_changed = false;
615 }
616
617 // Create no_cursor
618 else {
619 mac_cursor = XCreatePixmapCursor(x_display,
620 XCreatePixmap(x_display, the_win, 1, 1, 1),
621 XCreatePixmap(x_display, the_win, 1, 1, 1),
622 &black, &white, 0, 0);
623 XDefineCursor(x_display, the_win, mac_cursor);
624 }
625
626 // Init blitting routines
627 bool native_byte_order;
628 #ifdef WORDS_BIGENDIAN
629 native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
630 #else
631 native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
632 #endif
633 #ifdef ENABLE_VOSF
634 Screen_blitter_init(visualFormat, native_byte_order, depth);
635 #endif
636
637 // Set bytes per row
638 XSync(x_display, false);
639 return true;
640 }
641
642 // Open FBDev DGA display
643 static bool open_fbdev_dga(int width, int height)
644 {
645 #ifdef ENABLE_FBDEV_DGA
646 #ifdef ENABLE_XF86_VIDMODE
647 // Switch to best mode
648 if (has_vidmode) {
649 int best = -1;
650 for (int i = 0; i < num_x_video_modes; i++) {
651 if (x_video_modes[i]->hdisplay == width && x_video_modes[i]->vdisplay == height) {
652 best = i;
653 break;
654 }
655 };
656 assert(best != -1);
657 XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
658 XF86VidModeSetViewPort(x_display, screen, 0, 0);
659 D(bug("[fbdev] VideoMode %d: %d x %d @ %d\n", best,
660 x_video_modes[best]->hdisplay, x_video_modes[best]->vdisplay,
661 1000 * x_video_modes[best]->dotclock / (x_video_modes[best]->htotal * x_video_modes[best]->vtotal)));
662 }
663 #endif
664
665 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo) != 0) {
666 D(bug("[fbdev] Can't get FSCREENINFO: %s\n", strerror(errno)));
667 return false;
668 }
669 D(bug("[fbdev] Device ID: %s\n", fb_finfo.id));
670 D(bug("[fbdev] smem_start: %p [%d bytes]\n", fb_finfo.smem_start, fb_finfo.smem_len));
671
672 int fb_type = fb_finfo.type;
673 const char *fb_type_str = NULL;
674 switch (fb_type) {
675 case FB_TYPE_PACKED_PIXELS: fb_type_str = "Packed Pixels"; break;
676 case FB_TYPE_PLANES: fb_type_str = "Non interleaved planes"; break;
677 case FB_TYPE_INTERLEAVED_PLANES: fb_type_str = "Interleaved planes"; break;
678 case FB_TYPE_TEXT: fb_type_str = "Text/attributes"; break;
679 case FB_TYPE_VGA_PLANES: fb_type_str = "EGA/VGA planes"; break;
680 default: fb_type_str = "<unknown>"; break;
681 }
682 D(bug("[fbdev] type: %s\n", fb_type_str));
683
684 if (fb_type != FB_TYPE_PACKED_PIXELS) {
685 D(bug("[fbdev] type '%s' not supported\n", fb_type_str));
686 return false;
687 }
688
689 int fb_visual = fb_finfo.visual;
690 const char *fb_visual_str;
691 switch (fb_visual) {
692 case FB_VISUAL_MONO01: fb_visual_str = "Monochrome 1=Black 0=White"; break;
693 case FB_VISUAL_MONO10: fb_visual_str = "Monochrome 1=While 0=Black"; break;
694 case FB_VISUAL_TRUECOLOR: fb_visual_str = "True color"; break;
695 case FB_VISUAL_PSEUDOCOLOR: fb_visual_str = "Pseudo color (like atari)"; break;
696 case FB_VISUAL_DIRECTCOLOR: fb_visual_str = "Direct color"; break;
697 case FB_VISUAL_STATIC_PSEUDOCOLOR: fb_visual_str = "Pseudo color readonly"; break;
698 default: fb_visual_str = "<unknown>"; break;
699 }
700 D(bug("[fbdev] visual: %s\n", fb_visual_str));
701
702 if (fb_visual != FB_VISUAL_TRUECOLOR && fb_visual != FB_VISUAL_DIRECTCOLOR) {
703 D(bug("[fbdev] visual '%s' not supported\n", fb_visual_str));
704 return false;
705 }
706
707 // Map frame buffer
708 the_buffer = (uint8 *)mmap(NULL, fb_finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev_fd, 0);
709 if (the_buffer == MAP_FAILED) {
710 D(bug("[fbdev] Can't mmap /dev/fb0: %s\n", strerror(errno)));
711 return false;
712 }
713
714 // Set absolute mouse mode
715 ADBSetRelMouseMode(false);
716
717 // Create window
718 XSetWindowAttributes wattr;
719 wattr.event_mask = eventmask = dga_eventmask;
720 wattr.override_redirect = True;
721 the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
722 InputOutput, DefaultVisual(x_display, screen),
723 CWEventMask | CWOverrideRedirect, &wattr);
724
725 // Show window
726 XMapRaised(x_display, the_win);
727 wait_mapped(the_win);
728
729 // Grab mouse and keyboard
730 XGrabKeyboard(x_display, the_win, True,
731 GrabModeAsync, GrabModeAsync, CurrentTime);
732 XGrabPointer(x_display, the_win, True,
733 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
734 GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
735 disable_mouse_accel();
736
737 // Create no_cursor
738 mac_cursor = XCreatePixmapCursor(x_display,
739 XCreatePixmap(x_display, the_win, 1, 1, 1),
740 XCreatePixmap(x_display, the_win, 1, 1, 1),
741 &black, &white, 0, 0);
742 XDefineCursor(x_display, the_win, mac_cursor);
743
744 // Init blitting routines
745 int bytes_per_row = TrivialBytesPerRow((width + 7) & ~7, DepthModeForPixelDepth(depth));
746 #if ENABLE_VOSF
747 // Extract current screen color masks (we are in True Color mode)
748 VisualFormat visualFormat;
749 visualFormat.depth = xdepth = DefaultDepth(x_display, screen);
750 XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo);
751 assert(visualFormat.depth == visualInfo.depth);
752 visualFormat.Rmask = visualInfo.red_mask;
753 visualFormat.Gmask = visualInfo.green_mask;
754 visualFormat.Bmask = visualInfo.blue_mask;
755 D(bug("[fbdev] %d bpp, (%08x,%08x,%08x)\n",
756 visualFormat.depth,
757 visualFormat.Rmask, visualFormat.Gmask, visualFormat.Bmask));
758 D(bug("[fbdev] Mac depth %d bpp\n", depth));
759
760 // Screen_blitter_init() returns TRUE if VOSF is mandatory
761 // i.e. the framebuffer update function is not Blit_Copy_Raw
762 #ifdef WORDS_BIGENDIAN
763 const bool native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
764 #else
765 const bool native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
766 #endif
767 Screen_blitter_init(visualFormat, native_byte_order, depth);
768
769 // Allocate memory for frame buffer (SIZE is extended to page-boundary)
770 use_vosf = true;
771 the_host_buffer = the_buffer;
772 the_buffer_size = page_extend((height + 2) * bytes_per_row);
773 the_buffer_copy = (uint8 *)malloc(the_buffer_size);
774 the_buffer = (uint8 *)vm_acquire(the_buffer_size);
775 D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
776 #endif
777
778 // Set frame buffer base
779 D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
780 screen_base = Host2MacAddr(the_buffer);
781 VModes[cur_mode].viRowBytes = bytes_per_row;
782 return true;
783 #else
784 ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
785 return false;
786 #endif
787 }
788
789 // Open XF86 DGA display (!! should use X11 VidMode extensions to set mode)
790 static bool open_xf86_dga(int width, int height)
791 {
792 if (is_fbdev_dga_mode)
793 return false;
794
795 #ifdef ENABLE_XF86_DGA
796 // Set relative mouse mode
797 ADBSetRelMouseMode(true);
798
799 // Create window
800 XSetWindowAttributes wattr;
801 wattr.event_mask = eventmask = dga_eventmask;
802 wattr.override_redirect = True;
803 wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
804 the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
805 InputOutput, vis, CWEventMask | CWOverrideRedirect |
806 (color_class == DirectColor ? CWColormap : 0), &wattr);
807
808 // Show window
809 XMapRaised(x_display, the_win);
810 wait_mapped(the_win);
811
812 #ifdef ENABLE_XF86_VIDMODE
813 // Switch to best mode
814 if (has_vidmode) {
815 int best = 0;
816 for (int i=1; i<num_x_video_modes; i++) {
817 if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
818 x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
819 best = i;
820 }
821 }
822 XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
823 XF86VidModeSetViewPort(x_display, screen, 0, 0);
824 }
825 #endif
826
827 // Establish direct screen connection
828 XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
829 XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
830 XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
831 XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
832
833 int v_width, v_bank, v_size;
834 XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
835 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
836 XF86DGASetViewPort(x_display, screen, 0, 0);
837 XF86DGASetVidPage(x_display, screen, 0);
838
839 // Set colormap
840 if (!IsDirectMode(get_current_mode())) {
841 XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
842 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
843 }
844 XSync(x_display, false);
845
846 // Init blitting routines
847 int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, DepthModeForPixelDepth(depth));
848 #if ENABLE_VOSF
849 bool native_byte_order;
850 #ifdef WORDS_BIGENDIAN
851 native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
852 #else
853 native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
854 #endif
855 #if REAL_ADDRESSING || DIRECT_ADDRESSING
856 // Screen_blitter_init() returns TRUE if VOSF is mandatory
857 // i.e. the framebuffer update function is not Blit_Copy_Raw
858 use_vosf = Screen_blitter_init(visualFormat, native_byte_order, depth);
859
860 if (use_vosf) {
861 // Allocate memory for frame buffer (SIZE is extended to page-boundary)
862 the_host_buffer = the_buffer;
863 the_buffer_size = page_extend((height + 2) * bytes_per_row);
864 the_buffer_copy = (uint8 *)malloc(the_buffer_size);
865 the_buffer = (uint8 *)vm_acquire(the_buffer_size);
866 D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
867 }
868 #else
869 use_vosf = false;
870 #endif
871 #endif
872
873 // Set frame buffer base
874 D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
875 screen_base = Host2MacAddr(the_buffer);
876 VModes[cur_mode].viRowBytes = bytes_per_row;
877 return true;
878 #else
879 ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
880 return false;
881 #endif
882 }
883
884 // Open DGA display
885 static bool open_dga(int width, int height)
886 {
887 bool display_open;
888
889 display_open = open_xf86_dga(width, height);
890 #ifdef ENABLE_FBDEV_DGA
891 // Try to fallback to FBDev DGA mode
892 if (!display_open) {
893 is_fbdev_dga_mode = true;
894 display_open = open_fbdev_dga(width, height);
895 }
896 #endif
897
898 // Common DGA display initialization
899 if (display_open) {
900
901 // Fake image to get display bounds in the refresh function
902 if ((img = (XImage *)malloc(sizeof(*img))) == NULL)
903 return false;
904 img->width = DisplayWidth(x_display, screen);
905 img->height = DisplayHeight(x_display, screen);
906 img->depth = is_fbdev_dga_mode ? xdepth : depth;
907 img->bytes_per_line = TrivialBytesPerRow(img->width, DepthModeForPixelDepth(img->depth));
908 }
909
910 return display_open;
911 }
912
913 static bool open_display(void)
914 {
915 D(bug("open_display()\n"));
916 const VideoInfo &mode = VModes[cur_mode];
917
918 // Get original mouse acceleration
919 XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold);
920
921 // Find best available X visual
922 if (!find_visual_for_depth(mode.viAppleMode)) {
923 ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
924 return false;
925 }
926
927 // Build up visualFormat structure
928 visualFormat.fullscreen = (display_type == DIS_SCREEN);
929 visualFormat.depth = visualInfo.depth;
930 visualFormat.Rmask = visualInfo.red_mask;
931 visualFormat.Gmask = visualInfo.green_mask;
932 visualFormat.Bmask = visualInfo.blue_mask;
933
934 // Create color maps
935 if (color_class == PseudoColor || color_class == DirectColor) {
936 cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
937 cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
938 } else {
939 cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
940 cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
941 }
942
943 // Find pixel format of direct modes
944 if (color_class == DirectColor || color_class == TrueColor) {
945 rshift = gshift = bshift = 0;
946 rloss = gloss = bloss = 8;
947 uint32 mask;
948 for (mask=vis->red_mask; !(mask&1); mask>>=1)
949 ++rshift;
950 for (; mask&1; mask>>=1)
951 --rloss;
952 for (mask=vis->green_mask; !(mask&1); mask>>=1)
953 ++gshift;
954 for (; mask&1; mask>>=1)
955 --gloss;
956 for (mask=vis->blue_mask; !(mask&1); mask>>=1)
957 ++bshift;
958 for (; mask&1; mask>>=1)
959 --bloss;
960 }
961
962 // Preset palette pixel values for CLUT or gamma table
963 if (color_class == DirectColor) {
964 int num = vis->map_entries;
965 for (int i=0; i<num; i++) {
966 int c = (i * 256) / num;
967 x_palette[i].pixel = map_rgb(c, c, c);
968 x_palette[i].flags = DoRed | DoGreen | DoBlue;
969 }
970 } else if (color_class == PseudoColor) {
971 for (int i=0; i<256; i++) {
972 x_palette[i].pixel = i;
973 x_palette[i].flags = DoRed | DoGreen | DoBlue;
974 }
975 }
976
977 // Load gray ramp to color map
978 int num = (color_class == DirectColor ? vis->map_entries : 256);
979 for (int i=0; i<num; i++) {
980 int c = (i * 256) / num;
981 x_palette[i].red = c * 0x0101;
982 x_palette[i].green = c * 0x0101;
983 x_palette[i].blue = c * 0x0101;
984 }
985 if (color_class == PseudoColor || color_class == DirectColor) {
986 XStoreColors(x_display, cmap[0], x_palette, num);
987 XStoreColors(x_display, cmap[1], x_palette, num);
988 }
989
990 #ifdef ENABLE_VOSF
991 // Load gray ramp to 8->16/32 expand map
992 if (!IsDirectMode(get_current_mode()) && xdepth > 8)
993 for (int i=0; i<256; i++)
994 ExpandMap[i] = map_rgb(i, i, i);
995 #endif
996
997 // Create display of requested type
998 display_type = mode.viType;
999 depth = depth_of_video_mode(mode.viAppleMode);
1000
1001 bool display_open;
1002 switch (display_type) {
1003 case DIS_SCREEN:
1004 display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
1005 break;
1006 case DIS_WINDOW:
1007 display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
1008 break;
1009 default:
1010 display_open = false;
1011 break;
1012 }
1013
1014 #ifdef ENABLE_VOSF
1015 if (use_vosf) {
1016 // Initialize the VOSF system
1017 LOCK_VOSF;
1018 if (!video_vosf_init()) {
1019 ErrorAlert(GetString(STR_VOSF_INIT_ERR));
1020 UNLOCK_VOSF;
1021 return false;
1022 }
1023 UNLOCK_VOSF;
1024 }
1025 #endif
1026
1027 // Zero screen buffers, viRowBytes is initialized at this stage
1028 if (display_open) {
1029 memset(the_buffer, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1030 memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1031 }
1032 return display_open;
1033 }
1034
1035
1036 /*
1037 * Close display
1038 */
1039
1040 // Close window
1041 static void close_window(void)
1042 {
1043 if (have_shm) {
1044 XShmDetach(x_display, &shminfo);
1045 #ifdef ENABLE_VOSF
1046 the_host_buffer = NULL; // don't free() in driver_base dtor
1047 #else
1048 the_buffer_copy = NULL; // don't free() in driver_base dtor
1049 #endif
1050 }
1051 if (img) {
1052 if (!have_shm)
1053 img->data = NULL;
1054 XDestroyImage(img);
1055 }
1056 if (have_shm) {
1057 shmdt(shminfo.shmaddr);
1058 shmctl(shminfo.shmid, IPC_RMID, 0);
1059 }
1060 if (the_gc)
1061 XFreeGC(x_display, the_gc);
1062
1063 XFlush(x_display);
1064 XSync(x_display, false);
1065 }
1066
1067 // Close FBDev mode
1068 static void close_fbdev_dga(void)
1069 {
1070 #ifdef ENABLE_FBDEV_DGA
1071 uint8 *fb_base;
1072 if (!use_vosf)
1073 fb_base = the_buffer;
1074 #ifdef ENABLE_VOSF
1075 else
1076 fb_base = the_host_buffer;
1077 #endif
1078 munmap(fb_base, fb_finfo.smem_len);
1079 #endif
1080 }
1081
1082 // Close XF86 DGA mode
1083 static void close_xf86_dga(void)
1084 {
1085 #ifdef ENABLE_XF86_DGA
1086 XF86DGADirectVideo(x_display, screen, 0);
1087 #endif
1088 }
1089
1090 // Close DGA mode
1091 static void close_dga(void)
1092 {
1093 if (is_fbdev_dga_mode)
1094 close_fbdev_dga();
1095 else
1096 close_xf86_dga();
1097
1098 XUngrabPointer(x_display, CurrentTime);
1099 XUngrabKeyboard(x_display, CurrentTime);
1100
1101 #ifdef ENABLE_XF86_VIDMODE
1102 if (has_vidmode)
1103 XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
1104 #endif
1105
1106 // Release fake image (it's not a normal XImage!)
1107 free(img);
1108 img = NULL;
1109
1110 if (!use_vosf) {
1111 // don't free() the screen buffer in driver_base dtor
1112 the_buffer = NULL;
1113 }
1114 #ifdef ENABLE_VOSF
1115 else {
1116 // don't free() the screen buffer in driver_base dtor
1117 the_host_buffer = NULL;
1118 }
1119 #endif
1120 }
1121
1122 static void close_display(void)
1123 {
1124 if (display_type == DIS_SCREEN)
1125 close_dga();
1126 else if (display_type == DIS_WINDOW)
1127 close_window();
1128
1129 // Close window
1130 if (the_win) {
1131 XUnmapWindow(x_display, the_win);
1132 wait_unmapped(the_win);
1133 XDestroyWindow(x_display, the_win);
1134 }
1135
1136 // Free colormaps
1137 if (cmap[0]) {
1138 XFreeColormap(x_display, cmap[0]);
1139 cmap[0] = 0;
1140 }
1141 if (cmap[1]) {
1142 XFreeColormap(x_display, cmap[1]);
1143 cmap[1] = 0;
1144 }
1145
1146 #ifdef ENABLE_VOSF
1147 if (use_vosf) {
1148 // Deinitialize VOSF
1149 video_vosf_exit();
1150 }
1151 #endif
1152
1153 // Free frame buffer(s)
1154 if (!use_vosf) {
1155 if (the_buffer_copy) {
1156 free(the_buffer_copy);
1157 the_buffer_copy = NULL;
1158 }
1159 }
1160 #ifdef ENABLE_VOSF
1161 else {
1162 // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
1163 if (the_buffer != VM_MAP_FAILED) {
1164 D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
1165 vm_release(the_buffer, the_buffer_size);
1166 the_buffer = NULL;
1167 }
1168 if (the_host_buffer) {
1169 D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
1170 free(the_host_buffer);
1171 the_host_buffer = NULL;
1172 }
1173 if (the_buffer_copy) {
1174 D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
1175 free(the_buffer_copy);
1176 the_buffer_copy = NULL;
1177 }
1178 }
1179 #endif
1180
1181 // Restore mouse acceleration
1182 restore_mouse_accel();
1183 }
1184
1185
1186 /*
1187 * Initialization
1188 */
1189
1190 // Init keycode translation table
1191 static void keycode_init(void)
1192 {
1193 bool use_kc = PrefsFindBool("keycodes");
1194 if (use_kc) {
1195
1196 // Get keycode file path from preferences
1197 const char *kc_path = PrefsFindString("keycodefile");
1198
1199 // Open keycode table
1200 FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
1201 if (f == NULL) {
1202 char str[256];
1203 sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
1204 WarningAlert(str);
1205 return;
1206 }
1207
1208 // Default translation table
1209 for (int i=0; i<256; i++)
1210 keycode_table[i] = -1;
1211
1212 // Search for server vendor string, then read keycodes
1213 const char *vendor = ServerVendor(x_display);
1214 // Force use of MacX mappings on MacOS X with Apple's X server
1215 int dummy;
1216 if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy))
1217 vendor = "MacX";
1218 bool vendor_found = false;
1219 char line[256];
1220 while (fgets(line, 255, f)) {
1221 // Read line
1222 int len = strlen(line);
1223 if (len == 0)
1224 continue;
1225 line[len-1] = 0;
1226
1227 // Comments begin with "#" or ";"
1228 if (line[0] == '#' || line[0] == ';' || line[0] == 0)
1229 continue;
1230
1231 if (vendor_found) {
1232 // Read keycode
1233 int x_code, mac_code;
1234 if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
1235 keycode_table[x_code & 0xff] = mac_code;
1236 else
1237 break;
1238 } else {
1239 // Search for vendor string
1240 if (strstr(vendor, line) == vendor)
1241 vendor_found = true;
1242 }
1243 }
1244
1245 // Keycode file completely read
1246 fclose(f);
1247 use_keycodes = vendor_found;
1248
1249 // Vendor not found? Then display warning
1250 if (!vendor_found) {
1251 char str[256];
1252 sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME);
1253 WarningAlert(str);
1254 return;
1255 }
1256 }
1257 }
1258
1259 // Find Apple mode matching best specified dimensions
1260 static int find_apple_resolution(int xsize, int ysize)
1261 {
1262 int apple_id;
1263 if (xsize < 800)
1264 apple_id = APPLE_640x480;
1265 else if (xsize < 1024)
1266 apple_id = APPLE_800x600;
1267 else if (xsize < 1152)
1268 apple_id = APPLE_1024x768;
1269 else if (xsize < 1280) {
1270 if (ysize < 900)
1271 apple_id = APPLE_1152x768;
1272 else
1273 apple_id = APPLE_1152x900;
1274 }
1275 else if (xsize < 1600)
1276 apple_id = APPLE_1280x1024;
1277 else
1278 apple_id = APPLE_1600x1200;
1279 return apple_id;
1280 }
1281
1282 // Find mode in list of supported modes
1283 static int find_mode(int apple_mode, int apple_id, int type)
1284 {
1285 for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
1286 if (p->viType == type && p->viAppleID == apple_id && p->viAppleMode == apple_mode)
1287 return p - VModes;
1288 }
1289 return -1;
1290 }
1291
1292 // Add custom video mode
1293 static void add_custom_mode(VideoInfo *&p, int type, uint32 x, uint32 y, int apple_mode, int apple_id)
1294 {
1295 p->viType = type;
1296 p->viXsize = x;
1297 p->viYsize = y;
1298 p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
1299 p->viAppleMode = apple_mode;
1300 p->viAppleID = apple_id;
1301 p++;
1302 }
1303
1304 // Add mode to list of supported modes
1305 static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
1306 {
1307 if (allow & test) {
1308 uint32 x = 0, y = 0;
1309 switch (apple_id) {
1310 case APPLE_W_640x480:
1311 case APPLE_640x480:
1312 x = 640;
1313 y = 480;
1314 break;
1315 case APPLE_W_800x600:
1316 case APPLE_800x600:
1317 x = 800;
1318 y = 600;
1319 break;
1320 case APPLE_1024x768:
1321 x = 1024;
1322 y = 768;
1323 break;
1324 case APPLE_1152x768:
1325 x = 1152;
1326 y = 768;
1327 break;
1328 case APPLE_1152x900:
1329 x = 1152;
1330 y = 900;
1331 break;
1332 case APPLE_1280x1024:
1333 x = 1280;
1334 y = 1024;
1335 break;
1336 case APPLE_1600x1200:
1337 x = 1600;
1338 y = 1200;
1339 break;
1340 }
1341 add_custom_mode(p, type, x, y, apple_mode, apple_id);
1342 }
1343 }
1344
1345 // Add standard list of windowed modes for given color depth
1346 static void add_window_modes(VideoInfo *&p, int window_modes, int mode)
1347 {
1348 add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);
1349 add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);
1350 }
1351
1352 static bool has_mode(int x, int y)
1353 {
1354 #ifdef ENABLE_XF86_VIDMODE
1355 if (has_vidmode) {
1356 for (int i=0; i<num_x_video_modes; i++)
1357 if (x_video_modes[i]->hdisplay == x && x_video_modes[i]->vdisplay == y)
1358 return true;
1359 return false;
1360 }
1361 #endif
1362 return DisplayWidth(x_display, screen) >= x && DisplayHeight(x_display, screen) >= y;
1363 }
1364
1365 bool VideoInit(void)
1366 {
1367 #ifdef ENABLE_VOSF
1368 // Zero the mainBuffer structure
1369 mainBuffer.dirtyPages = NULL;
1370 mainBuffer.pageInfo = NULL;
1371 #endif
1372
1373 // Check if X server runs on local machine
1374 local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
1375 || (strncmp(XDisplayName(x_display_name), "/", 1) == 0)
1376 || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
1377
1378 // Init keycode translation
1379 keycode_init();
1380
1381 // Read frame skip prefs
1382 frame_skip = PrefsFindInt32("frameskip");
1383 if (frame_skip == 0)
1384 frame_skip = 1;
1385
1386 // Read mouse wheel prefs
1387 mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
1388 mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1389
1390 // Init variables
1391 private_data = NULL;
1392 video_activated = true;
1393
1394 // Find screen and root window
1395 screen = XDefaultScreen(x_display);
1396 rootwin = XRootWindow(x_display, screen);
1397
1398 // Get sorted list of available depths
1399 avail_depths = XListDepths(x_display, screen, &num_depths);
1400 if (avail_depths == NULL) {
1401 ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
1402 return false;
1403 }
1404 sort(avail_depths, avail_depths + num_depths);
1405
1406 // Get screen depth
1407 xdepth = DefaultDepth(x_display, screen);
1408
1409 #ifdef ENABLE_XF86_DGA
1410 // DGA available?
1411 int event_base, error_base;
1412 is_fbdev_dga_mode = false;
1413 if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
1414 int dga_flags = 0;
1415 XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1416 has_dga = dga_flags & XF86DGADirectPresent;
1417 #if defined(__linux__)
1418 // Check r/w permission on /dev/mem for DGA mode to work
1419 if (has_dga && access("/dev/mem", R_OK | W_OK) != 0)
1420 has_dga = false;
1421 #endif
1422 } else
1423 has_dga = false;
1424 #endif
1425
1426 #ifdef ENABLE_XF86_VIDMODE
1427 // VidMode available?
1428 int vm_event_base, vm_error_base;
1429 has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
1430 if (has_vidmode) {
1431 int vm_major_version, vm_minor_version;
1432 XF86VidModeQueryVersion(x_display, &vm_major_version, &vm_minor_version);
1433 D(bug("VidMode extension %d.%d available\n", vm_major_version, vm_minor_version));
1434 XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1435 }
1436 #endif
1437
1438 #ifdef ENABLE_FBDEV_DGA
1439 // FBDev available?
1440 bool has_fbdev_dga = false;
1441 if (local_X11) {
1442 if ((fb_dev_fd = open("/dev/fb0", O_RDWR)) > 0) {
1443 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo) != 0)
1444 close(fb_dev_fd);
1445 else {
1446 has_fbdev_dga = true;
1447 if (!has_dga) {
1448 // Fallback to FBDev DGA mode if XF86 DGA is not possible
1449 has_dga = true;
1450 is_fbdev_dga_mode = true;
1451 }
1452 fb_orig_vinfo = fb_vinfo;
1453 D(bug("Frame buffer device initial resolution: %dx%dx%d\n", fb_vinfo.xres, fb_vinfo.yres, fb_vinfo.bits_per_pixel));
1454 }
1455 }
1456 }
1457 #endif
1458
1459 // Find black and white colors
1460 XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
1461 XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
1462 XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white);
1463 XAllocColor(x_display, DefaultColormap(x_display, screen), &white);
1464 black_pixel = BlackPixel(x_display, screen);
1465 white_pixel = WhitePixel(x_display, screen);
1466
1467 // Mac screen depth follows X depth (for now)
1468 int default_mode = APPLE_8_BIT;
1469 switch (DefaultDepth(x_display, screen)) {
1470 case 1:
1471 default_mode = APPLE_1_BIT;
1472 break;
1473 case 8:
1474 default_mode = APPLE_8_BIT;
1475 break;
1476 case 15: case 16:
1477 default_mode = APPLE_16_BIT;
1478 break;
1479 case 24: case 32:
1480 default_mode = APPLE_32_BIT;
1481 break;
1482 }
1483
1484 // Get screen mode from preferences
1485 const char *mode_str = PrefsFindString("screen");
1486 int default_width = 640, default_height = 480;
1487 if (mode_str) {
1488 display_type = DIS_INVALID;
1489 if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
1490 display_type = DIS_WINDOW;
1491 #ifdef ENABLE_XF86_DGA
1492 else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1493 display_type = DIS_SCREEN;
1494 #endif
1495 #ifdef ENABLE_FBDEV_DGA
1496 else if (has_fbdev_dga && sscanf(mode_str, "fbdev/%d/%d", &default_width, &default_height) == 2) {
1497 is_fbdev_dga_mode = true;
1498 display_type = DIS_SCREEN;
1499 }
1500 #endif
1501 if (display_type == DIS_INVALID) {
1502 D(bug("Invalid screen mode specified, defaulting to old modes selection\n"));
1503 mode_str = NULL;
1504 }
1505 else {
1506 if (default_width <= 0)
1507 default_width = DisplayWidth(x_display, screen);
1508 else if (default_width > DisplayWidth(x_display, screen))
1509 default_width = DisplayWidth(x_display, screen);
1510 if (default_height <= 0)
1511 default_height = DisplayHeight(x_display, screen);
1512 else if (default_height > DisplayHeight(x_display, screen))
1513 default_height = DisplayHeight(x_display, screen);
1514 }
1515 }
1516
1517 // Construct video mode table
1518 uint32 window_modes = PrefsFindInt32("windowmodes");
1519 uint32 screen_modes = PrefsFindInt32("screenmodes");
1520 if (!has_dga)
1521 screen_modes = 0;
1522 if (mode_str)
1523 window_modes = screen_modes = 0;
1524 else if (window_modes == 0 && screen_modes == 0)
1525 window_modes |= 3; // Allow at least 640x480 and 800x600 window modes
1526
1527 VideoInfo *p = VModes;
1528 if (mode_str) {
1529 if (display_type == DIS_WINDOW) {
1530 for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) {
1531 if (find_visual_for_depth(d)) {
1532 if (default_width > 640 && default_height > 480)
1533 add_mode(p, 3, 1, d, APPLE_W_640x480, DIS_WINDOW);
1534 if (default_width > 800 && default_height > 600)
1535 add_mode(p, 3, 2, d, APPLE_W_800x600, DIS_WINDOW);
1536 add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM);
1537 }
1538 }
1539 #ifdef ENABLE_VOSF
1540 } else if (display_type == DIS_SCREEN && is_fbdev_dga_mode) {
1541 for (unsigned int d = APPLE_1_BIT; d <= default_mode; d++)
1542 if (find_visual_for_depth(d))
1543 add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM);
1544 #endif
1545 } else
1546 add_custom_mode(p, display_type, default_width, default_height, default_mode, APPLE_CUSTOM);
1547
1548 // Add extra VidMode capable modes
1549 if (display_type == DIS_SCREEN) {
1550 struct {
1551 int w;
1552 int h;
1553 int apple_id;
1554 }
1555 video_modes[] = {
1556 { 640, 480, APPLE_640x480 },
1557 { 800, 600, APPLE_800x600 },
1558 { 1024, 768, APPLE_1024x768 },
1559 { 1152, 768, APPLE_1152x768 },
1560 { 1152, 900, APPLE_1152x900 },
1561 { 1280, 1024, APPLE_1280x1024 },
1562 { 1600, 1200, APPLE_1600x1200 },
1563 { 0, }
1564 };
1565
1566 for (int i = 0; video_modes[i].w != 0; i++) {
1567 const int w = video_modes[i].w;
1568 const int h = video_modes[i].h;
1569 if (w >= default_width || h >= default_height)
1570 continue;
1571 if (has_mode(w, h)) {
1572 #ifdef ENABLE_VOSF
1573 if (is_fbdev_dga_mode) {
1574 for (unsigned int d = APPLE_1_BIT; d <= default_mode; d++)
1575 if (find_visual_for_depth(d))
1576 add_custom_mode(p, display_type, w, h, d, video_modes[i].apple_id);
1577 } else
1578 #endif
1579 add_custom_mode(p, display_type, w, h, default_mode, video_modes[i].apple_id);
1580 }
1581 }
1582 }
1583 } else if (window_modes) {
1584 for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1585 if (find_visual_for_depth(d))
1586 add_window_modes(p, window_modes, d);
1587 } else if (has_vidmode) {
1588 if (has_mode(640, 480))
1589 add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1590 if (has_mode(800, 600))
1591 add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN);
1592 if (has_mode(1024, 768))
1593 add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN);
1594 if (has_mode(1152, 768))
1595 add_mode(p, screen_modes, 64, default_mode, APPLE_1152x768, DIS_SCREEN);
1596 if (has_mode(1152, 900))
1597 add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN);
1598 if (has_mode(1280, 1024))
1599 add_mode(p, screen_modes, 16, default_mode, APPLE_1280x1024, DIS_SCREEN);
1600 if (has_mode(1600, 1200))
1601 add_mode(p, screen_modes, 32, default_mode, APPLE_1600x1200, DIS_SCREEN);
1602 } else if (screen_modes) {
1603 int xsize = DisplayWidth(x_display, screen);
1604 int ysize = DisplayHeight(x_display, screen);
1605 int apple_id = find_apple_resolution(xsize, ysize);
1606 p->viType = DIS_SCREEN;
1607 p->viRowBytes = 0;
1608 p->viXsize = xsize;
1609 p->viYsize = ysize;
1610 p->viAppleMode = default_mode;
1611 p->viAppleID = apple_id;
1612 p++;
1613 }
1614 p->viType = DIS_INVALID; // End marker
1615 p->viRowBytes = 0;
1616 p->viXsize = p->viYsize = 0;
1617 p->viAppleMode = 0;
1618 p->viAppleID = 0;
1619
1620 // Find default mode (window 640x480)
1621 cur_mode = -1;
1622 if (has_dga && screen_modes) {
1623 int screen_width = DisplayWidth(x_display, screen);
1624 int screen_height = DisplayHeight(x_display, screen);
1625 int apple_id = find_apple_resolution(screen_width, screen_height);
1626 if (apple_id != -1)
1627 cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN);
1628 } else if (has_dga && mode_str)
1629 cur_mode = find_mode(default_mode, APPLE_CUSTOM, DIS_SCREEN);
1630
1631 if (cur_mode == -1) {
1632 // pick up first windowed mode available
1633 for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
1634 if (p->viType == DIS_WINDOW && p->viAppleMode == default_mode) {
1635 cur_mode = p - VModes;
1636 break;
1637 }
1638 }
1639 }
1640 assert(cur_mode != -1);
1641
1642 #if DEBUG
1643 D(bug("Available video modes:\n"));
1644 for (p = VModes; p->viType != DIS_INVALID; p++) {
1645 int bits = depth_of_video_mode(p->viAppleMode);
1646 D(bug(" %dx%d (ID %02x), %d colors\n", p->viXsize, p->viYsize, p->viAppleID, 1 << bits));
1647 }
1648 #endif
1649
1650 // Open window/screen
1651 if (!open_display()) {
1652 ErrorAlert(GetString(STR_OPEN_WINDOW_ERR));
1653 return false;
1654 }
1655
1656 #if 0
1657 // Ignore errors from now on
1658 XSetErrorHandler(ignore_errors);
1659 #endif
1660
1661 // Lock down frame buffer
1662 XSync(x_display, false);
1663 LOCK_FRAME_BUFFER;
1664
1665 // Start periodic thread
1666 XSync(x_display, false);
1667 if (sem_init(&thread_stop_ack, 0, 0) < 0)
1668 return false;
1669 if (sem_init(&thread_resume_req, 0, 0) < 0)
1670 return false;
1671 Set_pthread_attr(&redraw_thread_attr, 0);
1672 redraw_thread_cancel = false;
1673 redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1674 D(bug("Redraw thread installed (%ld)\n", redraw_thread));
1675 return true;
1676 }
1677
1678
1679 /*
1680 * Deinitialization
1681 */
1682
1683 void VideoExit(void)
1684 {
1685 // Stop redraw thread
1686 if (redraw_thread_active) {
1687 redraw_thread_cancel = true;
1688 pthread_cancel(redraw_thread);
1689 pthread_join(redraw_thread, NULL);
1690 sem_destroy(&thread_stop_ack);
1691 sem_destroy(&thread_resume_req);
1692 redraw_thread_active = false;
1693 }
1694
1695 // Unlock frame buffer
1696 UNLOCK_FRAME_BUFFER;
1697 XSync(x_display, false);
1698 D(bug(" frame buffer unlocked\n"));
1699
1700 #ifdef ENABLE_VOSF
1701 if (use_vosf) {
1702 // Deinitialize VOSF
1703 video_vosf_exit();
1704 }
1705 #endif
1706
1707 // Close window and server connection
1708 if (x_display != NULL) {
1709 XSync(x_display, false);
1710 close_display();
1711 XFlush(x_display);
1712 XSync(x_display, false);
1713 }
1714
1715 #ifdef ENABLE_FBDEV_DGA
1716 // Close framebuffer device
1717 if (fb_dev_fd >= 0) {
1718 close(fb_dev_fd);
1719 fb_dev_fd = -1;
1720 }
1721 #endif
1722 }
1723
1724
1725 /*
1726 * Suspend/resume emulator
1727 */
1728
1729 static void suspend_emul(void)
1730 {
1731 if (display_type == DIS_SCREEN) {
1732 // Release ctrl key
1733 ADBKeyUp(0x36);
1734 ctrl_down = false;
1735
1736 // Lock frame buffer (this will stop the MacOS thread)
1737 LOCK_FRAME_BUFFER;
1738
1739 // Save frame buffer
1740 fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1741 if (fb_save)
1742 Mac2Host_memcpy(fb_save, screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1743
1744 // Close full screen display
1745 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1746 #ifdef ENABLE_XF86_DGA
1747 if (!is_fbdev_dga_mode)
1748 XF86DGADirectVideo(x_display, screen, 0);
1749 #endif
1750 XUngrabPointer(x_display, CurrentTime);
1751 XUngrabKeyboard(x_display, CurrentTime);
1752 #endif
1753 restore_mouse_accel();
1754 XUnmapWindow(x_display, the_win);
1755 wait_unmapped(the_win);
1756 XSync(x_display, false);
1757
1758 // Open "suspend" window
1759 XSetWindowAttributes wattr;
1760 wattr.event_mask = KeyPressMask;
1761 wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
1762 wattr.backing_store = Always;
1763 wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
1764
1765 suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1766 InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore | CWColormap, &wattr);
1767 set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
1768 set_window_focus(suspend_win);
1769 XMapWindow(x_display, suspend_win);
1770 emul_suspended = true;
1771 }
1772 }
1773
1774 static void resume_emul(void)
1775 {
1776 // Close "suspend" window
1777 XDestroyWindow(x_display, suspend_win);
1778 XSync(x_display, false);
1779
1780 // Reopen full screen display
1781 XMapRaised(x_display, the_win);
1782 wait_mapped(the_win);
1783 XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1784 Window w = is_fbdev_dga_mode ? the_win : rootwin;
1785 XGrabKeyboard(x_display, w, True, GrabModeAsync, GrabModeAsync, CurrentTime);
1786 XGrabPointer(x_display, w, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, is_fbdev_dga_mode ? w : None, None, CurrentTime);
1787 disable_mouse_accel();
1788 #ifdef ENABLE_XF86_DGA
1789 if (!is_fbdev_dga_mode) {
1790 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1791 XF86DGASetViewPort(x_display, screen, 0, 0);
1792 }
1793 #endif
1794 XSync(x_display, false);
1795
1796 // the_buffer already contains the data to restore. i.e. since a temporary
1797 // frame buffer is used when VOSF is actually used, fb_save is therefore
1798 // not necessary.
1799 #ifdef ENABLE_VOSF
1800 if (use_vosf) {
1801 LOCK_VOSF;
1802 PFLAG_SET_ALL;
1803 memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1804 UNLOCK_VOSF;
1805 }
1806 #endif
1807
1808 // Restore frame buffer
1809 if (fb_save) {
1810 #ifdef ENABLE_VOSF
1811 // Don't copy fb_save to the temporary frame buffer in VOSF mode
1812 if (!use_vosf)
1813 #endif
1814 Host2Mac_memcpy(screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1815 free(fb_save);
1816 fb_save = NULL;
1817 }
1818
1819 // Unlock frame buffer (and continue MacOS thread)
1820 UNLOCK_FRAME_BUFFER;
1821 emul_suspended = false;
1822 }
1823
1824
1825 /*
1826 * Close screen in full-screen mode
1827 */
1828
1829 void VideoQuitFullScreen(void)
1830 {
1831 D(bug("VideoQuitFullScreen()\n"));
1832 if (display_type == DIS_SCREEN) {
1833 quit_full_screen = true;
1834 while (!quit_full_screen_ack) ;
1835 }
1836 }
1837
1838
1839 /*
1840 * X11 event handling
1841 */
1842
1843 // Translate key event to Mac keycode
1844 static int kc_decode(KeySym ks)
1845 {
1846 switch (ks) {
1847 case XK_A: case XK_a: return 0x00;
1848 case XK_B: case XK_b: return 0x0b;
1849 case XK_C: case XK_c: return 0x08;
1850 case XK_D: case XK_d: return 0x02;
1851 case XK_E: case XK_e: return 0x0e;
1852 case XK_F: case XK_f: return 0x03;
1853 case XK_G: case XK_g: return 0x05;
1854 case XK_H: case XK_h: return 0x04;
1855 case XK_I: case XK_i: return 0x22;
1856 case XK_J: case XK_j: return 0x26;
1857 case XK_K: case XK_k: return 0x28;
1858 case XK_L: case XK_l: return 0x25;
1859 case XK_M: case XK_m: return 0x2e;
1860 case XK_N: case XK_n: return 0x2d;
1861 case XK_O: case XK_o: return 0x1f;
1862 case XK_P: case XK_p: return 0x23;
1863 case XK_Q: case XK_q: return 0x0c;
1864 case XK_R: case XK_r: return 0x0f;
1865 case XK_S: case XK_s: return 0x01;
1866 case XK_T: case XK_t: return 0x11;
1867 case XK_U: case XK_u: return 0x20;
1868 case XK_V: case XK_v: return 0x09;
1869 case XK_W: case XK_w: return 0x0d;
1870 case XK_X: case XK_x: return 0x07;
1871 case XK_Y: case XK_y: return 0x10;
1872 case XK_Z: case XK_z: return 0x06;
1873
1874 case XK_1: case XK_exclam: return 0x12;
1875 case XK_2: case XK_at: return 0x13;
1876 case XK_3: case XK_numbersign: return 0x14;
1877 case XK_4: case XK_dollar: return 0x15;
1878 case XK_5: case XK_percent: return 0x17;
1879 case XK_6: return 0x16;
1880 case XK_7: return 0x1a;
1881 case XK_8: return 0x1c;
1882 case XK_9: return 0x19;
1883 case XK_0: return 0x1d;
1884
1885 case XK_grave: case XK_asciitilde: return 0x0a;
1886 case XK_minus: case XK_underscore: return 0x1b;
1887 case XK_equal: case XK_plus: return 0x18;
1888 case XK_bracketleft: case XK_braceleft: return 0x21;
1889 case XK_bracketright: case XK_braceright: return 0x1e;
1890 case XK_backslash: case XK_bar: return 0x2a;
1891 case XK_semicolon: case XK_colon: return 0x29;
1892 case XK_apostrophe: case XK_quotedbl: return 0x27;
1893 case XK_comma: case XK_less: return 0x2b;
1894 case XK_period: case XK_greater: return 0x2f;
1895 case XK_slash: case XK_question: return 0x2c;
1896
1897 case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1898 case XK_Return: return 0x24;
1899 case XK_space: return 0x31;
1900 case XK_BackSpace: return 0x33;
1901
1902 case XK_Delete: return 0x75;
1903 case XK_Insert: return 0x72;
1904 case XK_Home: case XK_Help: return 0x73;
1905 case XK_End: return 0x77;
1906 #ifdef __hpux
1907 case XK_Prior: return 0x74;
1908 case XK_Next: return 0x79;
1909 #else
1910 case XK_Page_Up: return 0x74;
1911 case XK_Page_Down: return 0x79;
1912 #endif
1913
1914 case XK_Control_L: return 0x36;
1915 case XK_Control_R: return 0x36;
1916 case XK_Shift_L: return 0x38;
1917 case XK_Shift_R: return 0x38;
1918 case XK_Alt_L: return 0x37;
1919 case XK_Alt_R: return 0x37;
1920 case XK_Meta_L: return 0x3a;
1921 case XK_Meta_R: return 0x3a;
1922 case XK_Menu: return 0x32;
1923 case XK_Caps_Lock: return 0x39;
1924 case XK_Num_Lock: return 0x47;
1925
1926 case XK_Up: return 0x3e;
1927 case XK_Down: return 0x3d;
1928 case XK_Left: return 0x3b;
1929 case XK_Right: return 0x3c;
1930
1931 case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
1932
1933 case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
1934 case XK_F2: return 0x78;
1935 case XK_F3: return 0x63;
1936 case XK_F4: return 0x76;
1937 case XK_F5: return 0x60;
1938 case XK_F6: return 0x61;
1939 case XK_F7: return 0x62;
1940 case XK_F8: return 0x64;
1941 case XK_F9: return 0x65;
1942 case XK_F10: return 0x6d;
1943 case XK_F11: return 0x67;
1944 case XK_F12: return 0x6f;
1945
1946 case XK_Print: return 0x69;
1947 case XK_Scroll_Lock: return 0x6b;
1948 case XK_Pause: return 0x71;
1949
1950 #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
1951 case XK_KP_0: case XK_KP_Insert: return 0x52;
1952 case XK_KP_1: case XK_KP_End: return 0x53;
1953 case XK_KP_2: case XK_KP_Down: return 0x54;
1954 case XK_KP_3: case XK_KP_Next: return 0x55;
1955 case XK_KP_4: case XK_KP_Left: return 0x56;
1956 case XK_KP_5: case XK_KP_Begin: return 0x57;
1957 case XK_KP_6: case XK_KP_Right: return 0x58;
1958 case XK_KP_7: case XK_KP_Home: return 0x59;
1959 case XK_KP_8: case XK_KP_Up: return 0x5b;
1960 case XK_KP_9: case XK_KP_Prior: return 0x5c;
1961 case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
1962 #else
1963 case XK_KP_0: return 0x52;
1964 case XK_KP_1: return 0x53;
1965 case XK_KP_2: return 0x54;
1966 case XK_KP_3: return 0x55;
1967 case XK_KP_4: return 0x56;
1968 case XK_KP_5: return 0x57;
1969 case XK_KP_6: return 0x58;
1970 case XK_KP_7: return 0x59;
1971 case XK_KP_8: return 0x5b;
1972 case XK_KP_9: return 0x5c;
1973 case XK_KP_Decimal: return 0x41;
1974 #endif
1975 case XK_KP_Add: return 0x45;
1976 case XK_KP_Subtract: return 0x4e;
1977 case XK_KP_Multiply: return 0x43;
1978 case XK_KP_Divide: return 0x4b;
1979 case XK_KP_Enter: return 0x4c;
1980 case XK_KP_Equal: return 0x51;
1981 }
1982 return -1;
1983 }
1984
1985 static int event2keycode(XKeyEvent &ev, bool key_down)
1986 {
1987 KeySym ks;
1988 int i = 0;
1989
1990 do {
1991 ks = XLookupKeysym(&ev, i++);
1992 int as = kc_decode(ks);
1993 if (as >= 0)
1994 return as;
1995 if (as == -2)
1996 return as;
1997 } while (ks != NoSymbol);
1998
1999 return -1;
2000 }
2001
2002 static void handle_events(void)
2003 {
2004 // Handle events
2005 for (;;) {
2006 XEvent event;
2007
2008 XDisplayLock();
2009 if (!XCheckMaskEvent(x_display, eventmask, &event)) {
2010 // Handle clipboard events
2011 if (XCheckTypedEvent(x_display, SelectionRequest, &event))
2012 ClipboardSelectionRequest(&event.xselectionrequest);
2013 else if (XCheckTypedEvent(x_display, SelectionClear, &event))
2014 ClipboardSelectionClear(&event.xselectionclear);
2015
2016 // Window "close" widget clicked
2017 else if (XCheckTypedEvent(x_display, ClientMessage, &event)) {
2018 if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
2019 ADBKeyDown(0x7f); // Power key
2020 ADBKeyUp(0x7f);
2021 }
2022 }
2023
2024 XDisplayUnlock();
2025 break;
2026 }
2027 XDisplayUnlock();
2028
2029 switch (event.type) {
2030 // Mouse button
2031 case ButtonPress: {
2032 unsigned int button = ((XButtonEvent *)&event)->button;
2033 if (button < 4)
2034 ADBMouseDown(button - 1);
2035 else if (button < 6) { // Wheel mouse
2036 if (mouse_wheel_mode == 0) {
2037 int key = (button == 5) ? 0x79 : 0x74; // Page up/down
2038 ADBKeyDown(key);
2039 ADBKeyUp(key);
2040 } else {
2041 int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down
2042 for(int i=0; i<mouse_wheel_lines; i++) {
2043 ADBKeyDown(key);
2044 ADBKeyUp(key);
2045 }
2046 }
2047 }
2048 break;
2049 }
2050 case ButtonRelease: {
2051 unsigned int button = ((XButtonEvent *)&event)->button;
2052 if (button < 4)
2053 ADBMouseUp(button - 1);
2054 break;
2055 }
2056
2057 // Mouse entered window
2058 case EnterNotify:
2059 if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
2060 ADBMouseMoved(event.xmotion.x, event.xmotion.y);
2061 break;
2062
2063 // Mouse moved
2064 case MotionNotify:
2065 ADBMouseMoved(event.xmotion.x, event.xmotion.y);
2066 break;
2067
2068 // Keyboard
2069 case KeyPress: {
2070 int code = -1;
2071 if (use_keycodes) {
2072 if (event2keycode(event.xkey, true) != -2) // This is called to process the hotkeys
2073 code = keycode_table[event.xkey.keycode & 0xff];
2074 } else
2075 code = event2keycode(event.xkey, true);
2076 if (code >= 0) {
2077 if (!emul_suspended) {
2078 if (code == 0x39) { // Caps Lock pressed
2079 if (caps_on) {
2080 ADBKeyUp(code);
2081 caps_on = false;
2082 } else {
2083 ADBKeyDown(code);
2084 caps_on = true;
2085 }
2086 } else
2087 ADBKeyDown(code);
2088 if (code == 0x36)
2089 ctrl_down = true;
2090 } else {
2091 if (code == 0x31)
2092 resume_emul(); // Space wakes us up
2093 }
2094 }
2095 break;
2096 }
2097 case KeyRelease: {
2098 int code = -1;
2099 if (use_keycodes) {
2100 if (event2keycode(event.xkey, false) != -2) // This is called to process the hotkeys
2101 code = keycode_table[event.xkey.keycode & 0xff];
2102 } else
2103 code = event2keycode(event.xkey, false);
2104 if (code >= 0 && code != 0x39) { // Don't propagate Caps Lock releases
2105 ADBKeyUp(code);
2106 if (code == 0x36)
2107 ctrl_down = false;
2108 }
2109 break;
2110 }
2111
2112 // Hidden parts exposed, force complete refresh
2113 case Expose:
2114 #ifdef ENABLE_VOSF
2115 if (use_vosf) { // VOSF refresh
2116 LOCK_VOSF;
2117 PFLAG_SET_ALL;
2118 memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2119 UNLOCK_VOSF;
2120 }
2121 else
2122 #endif
2123 memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2124 break;
2125 }
2126 }
2127 }
2128
2129
2130 /*
2131 * Execute video VBL routine
2132 */
2133
2134 void VideoVBL(void)
2135 {
2136 if (emerg_quit)
2137 QuitEmulator();
2138
2139 // Temporarily give up frame buffer lock (this is the point where
2140 // we are suspended when the user presses Ctrl-Tab)
2141 UNLOCK_FRAME_BUFFER;
2142 LOCK_FRAME_BUFFER;
2143
2144 // Execute video VBL
2145 if (private_data != NULL && private_data->interruptsEnabled)
2146 VSLDoInterruptService(private_data->vslServiceID);
2147 }
2148
2149
2150 /*
2151 * Change video mode
2152 */
2153
2154 int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
2155 {
2156 /* return if no mode change */
2157 if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
2158 (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
2159
2160 /* first find video mode in table */
2161 for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
2162 if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
2163 (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
2164 csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
2165 csSave->saveData = ReadMacInt32(ParamPtr + csData);
2166 csSave->savePage = ReadMacInt16(ParamPtr + csPage);
2167
2168 // Disable interrupts and pause redraw thread
2169 thread_stop_req = true;
2170 sem_wait(&thread_stop_ack);
2171 thread_stop_req = false;
2172 DisableInterrupt();
2173
2174 /* close old display */
2175 close_display();
2176
2177 /* open new display */
2178 cur_mode = i;
2179 bool ok = open_display();
2180
2181 /* opening the screen failed? Then bail out */
2182 if (!ok) {
2183 ErrorAlert(GetString(STR_FULL_SCREEN_ERR));
2184 QuitEmulator();
2185 }
2186
2187 WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
2188 csSave->saveBaseAddr=screen_base;
2189 csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
2190 csSave->saveMode=VModes[cur_mode].viAppleMode;
2191
2192 // Enable interrupts and resume redraw thread
2193 EnableInterrupt();
2194 sem_post(&thread_resume_req);
2195 return noErr;
2196 }
2197 }
2198 return paramErr;
2199 }
2200
2201
2202 /*
2203 * Set color palette
2204 */
2205
2206 void video_set_palette(void)
2207 {
2208 LOCK_PALETTE;
2209
2210 // Convert colors to XColor array
2211 int mode = get_current_mode();
2212 int num_in = palette_size(mode);
2213 int num_out = 256;
2214 bool stretch = false;
2215 if (IsDirectMode(mode)) {
2216 // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
2217 num_out = vis->map_entries;
2218 stretch = true;
2219 }
2220 XColor *p = x_palette;
2221 for (int i=0; i<num_out; i++) {
2222 int c = (stretch ? (i * num_in) / num_out : i);
2223 p->red = mac_pal[c].red * 0x0101;
2224 p->green = mac_pal[c].green * 0x0101;
2225 p->blue = mac_pal[c].blue * 0x0101;
2226 p++;
2227 }
2228
2229 #ifdef ENABLE_VOSF
2230 // Recalculate pixel color expansion map
2231 if (!IsDirectMode(mode) && xdepth > 8) {
2232 for (int i=0; i<256; i++) {
2233 int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
2234 ExpandMap[i] = map_rgb(mac_pal[c].red, mac_pal[c].green, mac_pal[c].blue);
2235 }
2236
2237 // We have to redraw everything because the interpretation of pixel values changed
2238 LOCK_VOSF;
2239 PFLAG_SET_ALL;
2240 if (display_type == DIS_SCREEN)
2241 PFLAG_SET_VERY_DIRTY;
2242 UNLOCK_VOSF;
2243 }
2244 #endif
2245
2246 // Tell redraw thread to change palette
2247 palette_changed = true;
2248
2249 UNLOCK_PALETTE;
2250 }
2251
2252
2253 /*
2254 * Can we set the MacOS cursor image into the window?
2255 */
2256
2257 bool video_can_change_cursor(void)
2258 {
2259 return hw_mac_cursor_accl && (display_type != DIS_SCREEN);
2260 }
2261
2262
2263 /*
2264 * Set cursor image for window
2265 */
2266
2267 void video_set_cursor(void)
2268 {
2269 cursor_changed = true;
2270 }
2271
2272
2273 /*
2274 * Thread for window refresh, event handling and other periodic actions
2275 */
2276
2277 static void update_display(void)
2278 {
2279 // Incremental update code
2280 int wide = 0, high = 0, x1, x2, y1, y2, i, j;
2281 int bytes_per_row = VModes[cur_mode].viRowBytes;
2282 int bytes_per_pixel = VModes[cur_mode].viRowBytes / VModes[cur_mode].viXsize;
2283 uint8 *p, *p2;
2284
2285 // Check for first line from top and first line from bottom that have changed
2286 y1 = 0;
2287 for (j=0; j<VModes[cur_mode].viYsize; j++) {
2288 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2289 y1 = j;
2290 break;
2291 }
2292 }
2293 y2 = y1 - 1;
2294 for (j=VModes[cur_mode].viYsize-1; j>=y1; j--) {
2295 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2296 y2 = j;
2297 break;
2298 }
2299 }
2300 high = y2 - y1 + 1;
2301
2302 // Check for first column from left and first column from right that have changed
2303 if (high) {
2304 if (depth == 1) {
2305 x1 = VModes[cur_mode].viXsize;
2306 for (j=y1; j<=y2; j++) {
2307 p = &the_buffer[j * bytes_per_row];
2308 p2 = &the_buffer_copy[j * bytes_per_row];
2309 for (i=0; i<(x1>>3); i++) {
2310 if (*p != *p2) {
2311 x1 = i << 3;
2312 break;
2313 }
2314 p++;
2315 p2++;
2316 }
2317 }
2318 x2 = x1;
2319 for (j=y1; j<=y2; j++) {
2320 p = &the_buffer[j * bytes_per_row];
2321 p2 = &the_buffer_copy[j * bytes_per_row];
2322 p += bytes_per_row;
2323 p2 += bytes_per_row;
2324 for (i=(VModes[cur_mode].viXsize>>3); i>(x2>>3); i--) {
2325 p--;
2326 p2--;
2327 if (*p != *p2) {
2328 x2 = i << 3;
2329 break;
2330 }
2331 }
2332 }
2333 wide = x2 - x1;
2334
2335 // Update copy of the_buffer
2336 if (high && wide) {
2337 for (j=y1; j<=y2; j++) {
2338 i = j * bytes_per_row + (x1 >> 3);
2339 memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
2340 }
2341 }
2342
2343 } else {
2344 x1 = VModes[cur_mode].viXsize;
2345 for (j=y1; j<=y2; j++) {
2346 p = &the_buffer[j * bytes_per_row];
2347 p2 = &the_buffer_copy[j * bytes_per_row];
2348 for (i=0; i<x1; i++) {
2349 if (memcmp(p, p2, bytes_per_pixel)) {
2350 x1 = i;
2351 break;
2352 }
2353 p += bytes_per_pixel;
2354 p2 += bytes_per_pixel;
2355 }
2356 }
2357 x2 = x1;
2358 for (j=y1; j<=y2; j++) {
2359 p = &the_buffer[j * bytes_per_row];
2360 p2 = &the_buffer_copy[j * bytes_per_row];
2361 p += bytes_per_row;
2362 p2 += bytes_per_row;
2363 for (i=VModes[cur_mode].viXsize; i>x2; i--) {
2364 p -= bytes_per_pixel;
2365 p2 -= bytes_per_pixel;
2366 if (memcmp(p, p2, bytes_per_pixel)) {
2367 x2 = i;
2368 break;
2369 }
2370 }
2371 }
2372 wide = x2 - x1;
2373
2374 // Update copy of the_buffer
2375 if (high && wide) {
2376 for (j=y1; j<=y2; j++) {
2377 i = j * bytes_per_row + x1 * bytes_per_pixel;
2378 memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
2379 }
2380 }
2381 }
2382 }
2383
2384 // Refresh display
2385 if (high && wide) {
2386 XDisplayLock();
2387 if (have_shm)
2388 XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
2389 else
2390 XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
2391 XDisplayUnlock();
2392 }
2393 }
2394
2395 const int VIDEO_REFRESH_HZ = 60;
2396 const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2397
2398 static void handle_palette_changes(void)
2399 {
2400 LOCK_PALETTE;
2401
2402 if (palette_changed && !emul_suspended) {
2403 palette_changed = false;
2404
2405 int mode = get_current_mode();
2406 if (color_class == PseudoColor || color_class == DirectColor) {
2407 int num = vis->map_entries;
2408 bool set_clut = true;
2409 if (!IsDirectMode(mode) && color_class == DirectColor) {
2410 if (display_type == DIS_WINDOW)
2411 set_clut = false; // Indexed mode on true color screen, don't set CLUT
2412 }
2413
2414 if (set_clut) {
2415 XDisplayLock();
2416 XStoreColors(x_display, cmap[0], x_palette, num);
2417 XStoreColors(x_display, cmap[1], x_palette, num);
2418 XSync(x_display, false);
2419 XDisplayUnlock();
2420 }
2421 }
2422
2423 #ifdef ENABLE_XF86_DGA
2424 if (display_type == DIS_SCREEN && !is_fbdev_dga_mode) {
2425 current_dga_cmap ^= 1;
2426 if (!IsDirectMode(mode) && cmap[current_dga_cmap])
2427 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2428 }
2429 #endif
2430 }
2431
2432 UNLOCK_PALETTE;
2433 }
2434
2435 static void *redraw_func(void *arg)
2436 {
2437 int fd = ConnectionNumber(x_display);
2438
2439 uint64 start = GetTicks_usec();
2440 int64 ticks = 0;
2441 uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2442
2443 while (!redraw_thread_cancel) {
2444
2445 // Pause if requested (during video mode switches)
2446 if (thread_stop_req) {
2447 sem_post(&thread_stop_ack);
2448 sem_wait(&thread_resume_req);
2449 }
2450
2451 int64 delay = next - GetTicks_usec();
2452 if (delay < -VIDEO_REFRESH_DELAY) {
2453
2454 // We are lagging far behind, so we reset the delay mechanism
2455 next = GetTicks_usec();
2456
2457 } else if (delay <= 0) {
2458
2459 // Delay expired, refresh display
2460 next += VIDEO_REFRESH_DELAY;
2461 ticks++;
2462
2463 // Handle X11 events
2464 handle_events();
2465
2466 // Quit DGA mode if requested
2467 if (quit_full_screen) {
2468 quit_full_screen = false;
2469 if (display_type == DIS_SCREEN) {
2470 XDisplayLock();
2471 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2472 #ifdef ENABLE_XF86_DGA
2473 if (!is_fbdev_dga_mode)
2474 XF86DGADirectVideo(x_display, screen, 0);
2475 #endif
2476 XUngrabPointer(x_display, CurrentTime);
2477 XUngrabKeyboard(x_display, CurrentTime);
2478 XUnmapWindow(x_display, the_win);
2479 wait_unmapped(the_win);
2480 XDestroyWindow(x_display, the_win);
2481 #endif
2482 XSync(x_display, false);
2483 XDisplayUnlock();
2484 quit_full_screen_ack = true;
2485 return NULL;
2486 }
2487 }
2488
2489 // Refresh display and set cursor image in window mode
2490 static int tick_counter = 0;
2491 if (display_type == DIS_WINDOW) {
2492 tick_counter++;
2493 if (tick_counter >= frame_skip) {
2494 tick_counter = 0;
2495
2496 // Update display
2497 #ifdef ENABLE_VOSF
2498 if (use_vosf) {
2499 XDisplayLock();
2500 if (mainBuffer.dirty) {
2501 LOCK_VOSF;
2502 update_display_window_vosf();
2503 UNLOCK_VOSF;
2504 XSync(x_display, false); // Let the server catch up
2505 }
2506 XDisplayUnlock();
2507 }
2508 else
2509 #endif
2510 update_display();
2511
2512 // Set new cursor image if it was changed
2513 if (hw_mac_cursor_accl && cursor_changed) {
2514 cursor_changed = false;
2515 uint8 *x_data = (uint8 *)cursor_image->data;
2516 uint8 *x_mask = (uint8 *)cursor_mask_image->data;
2517 for (int i = 0; i < 32; i++) {
2518 x_mask[i] = MacCursor[4 + i] | MacCursor[36 + i];
2519 x_data[i] = MacCursor[4 + i];
2520 }
2521 XDisplayLock();
2522 XFreeCursor(x_display, mac_cursor);
2523 XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
2524 XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
2525 mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2526 XDefineCursor(x_display, the_win, mac_cursor);
2527 XDisplayUnlock();
2528 }
2529 }
2530 }
2531 #ifdef ENABLE_VOSF
2532 else if (use_vosf) {
2533 // Update display (VOSF variant)
2534 if (++tick_counter >= frame_skip) {
2535 tick_counter = 0;
2536 if (mainBuffer.dirty) {
2537 LOCK_VOSF;
2538 update_display_dga_vosf();
2539 UNLOCK_VOSF;
2540 }
2541 }
2542 }
2543 #endif
2544
2545 // Set new palette if it was changed
2546 handle_palette_changes();
2547
2548 } else {
2549
2550 // No display refresh pending, check for X events
2551 fd_set readfds;
2552 FD_ZERO(&readfds);
2553 FD_SET(fd, &readfds);
2554 struct timeval timeout;
2555 timeout.tv_sec = 0;
2556 timeout.tv_usec = delay;
2557 if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2558 handle_events();
2559 }
2560 }
2561 return NULL;
2562 }
2563
2564
2565 /*
2566 * Record dirty area from NQD
2567 */
2568
2569 void video_set_dirty_area(int x, int y, int w, int h)
2570 {
2571 VideoInfo const & mode = VModes[cur_mode];
2572 const int screen_width = VIDEO_MODE_X;
2573 const int screen_height = VIDEO_MODE_Y;
2574 const int bytes_per_row = VIDEO_MODE_ROW_BYTES;
2575
2576 #ifdef ENABLE_VOSF
2577 if (use_vosf) {
2578 vosf_set_dirty_area(x, y, w, h, screen_width, screen_height, bytes_per_row);
2579 return;
2580 }
2581 #endif
2582
2583 // XXX handle dirty bounding boxes for non-VOSF modes
2584 }