ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.9
Committed: 1999-10-21T16:40:49Z (24 years, 8 months ago) by cebix
Branch: MAIN
CVS Tags: snapshot-21101999
Changes since 1.8: +2 -12 lines
Log Message:
- small fixes to fbdev DGA code

File Contents

# Content
1 /*
2 * video_x.cpp - Video/graphics emulation, X11 specific stuff
3 *
4 * Basilisk II (C) 1997-1999 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 */
28
29 #include "sysdeps.h"
30
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33 #include <X11/keysym.h>
34 #include <X11/extensions/XShm.h>
35 #include <sys/ipc.h>
36 #include <sys/shm.h>
37 #include <pthread.h>
38 #include <errno.h>
39
40 #include "cpu_emulation.h"
41 #include "main.h"
42 #include "adb.h"
43 #include "macos_util.h"
44 #include "prefs.h"
45 #include "user_strings.h"
46 #include "video.h"
47
48 #define DEBUG 1
49 #include "debug.h"
50
51 #if ENABLE_XF86_DGA
52 #include <X11/extensions/xf86dga.h>
53 #endif
54
55 #if ENABLE_FBDEV_DGA
56 #include <sys/mman.h>
57 #endif
58
59
60
61 // Display types
62 enum {
63 DISPLAY_WINDOW, // X11 window, using MIT SHM extensions if possible
64 DISPLAY_DGA // DGA fullscreen display
65 };
66
67
68 // Constants
69 const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
70 const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices";
71
72
73 // Global variables
74 static int32 frame_skip;
75 static int display_type = DISPLAY_WINDOW; // See enum above
76 static uint8 *the_buffer; // Mac frame buffer
77 static bool redraw_thread_active = false; // Flag: Redraw thread installed
78 static volatile bool redraw_thread_cancel = false; // Flag: Cancel Redraw thread
79 static pthread_t redraw_thread; // Redraw thread
80
81 static bool has_dga = false; // Flag: Video DGA capable
82
83 static bool ctrl_down = false; // Flag: Ctrl key pressed
84 static bool caps_on = false; // Flag: Caps Lock on
85 static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
86 static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
87 static bool emul_suspended = false; // Flag: Emulator suspended
88
89 static bool classic_mode = false; // Flag: Classic Mac video mode
90
91 static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
92 static int keycode_table[256]; // X keycode -> Mac keycode translation table
93
94 // X11 variables
95 static int screen; // Screen number
96 static int xdepth; // Depth of X screen
97 static int depth; // Depth of Mac frame buffer
98 static Window rootwin, the_win; // Root window and our window
99 static XVisualInfo visualInfo;
100 static Visual *vis;
101 static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode
102 static XColor black, white;
103 static unsigned long black_pixel, white_pixel;
104 static int eventmask;
105 static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask;
106 static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
107
108 static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect palette
109 static XColor palette[256]; // Color palette for 8-bit mode
110 static bool palette_changed = false; // Flag: Palette changed, redraw thread must set new colors
111
112 // Variables for window mode
113 static GC the_gc;
114 static XImage *img = NULL;
115 static XShmSegmentInfo shminfo;
116 static XImage *cursor_image, *cursor_mask_image;
117 static Pixmap cursor_map, cursor_mask_map;
118 static Cursor mac_cursor;
119 static GC cursor_gc, cursor_mask_gc;
120 static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer
121 static uint8 the_cursor[64]; // Cursor image data
122 static bool have_shm = false; // Flag: SHM extensions available
123
124 // Variables for XF86 DGA mode
125 static int current_dga_cmap; // Number (0 or 1) of currently installed DGA colormap
126 static Window suspend_win; // "Suspend" window
127 static void *fb_save = NULL; // Saved frame buffer for suspend
128 static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer
129
130 // Variables for fbdev DGA mode
131 const char FBDEVICE_FILE_NAME[] = "/dev/fb";
132 static int fbdev_fd;
133
134
135 // Prototypes
136 static void *redraw_func(void *arg);
137 static int event2keycode(XKeyEvent *ev);
138
139
140 // From main_unix.cpp
141 extern Display *x_display;
142
143 // From sys_unix.cpp
144 extern void SysMountFirstFloppy(void);
145
146
147 /*
148 * Initialization
149 */
150
151 // Set VideoMonitor according to video mode
152 void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order)
153 {
154 int layout = FLAYOUT_DIRECT;
155 switch (depth) {
156 case 1:
157 layout = FLAYOUT_DIRECT;
158 VideoMonitor.mode = VMODE_1BIT;
159 break;
160 case 8:
161 layout = FLAYOUT_DIRECT;
162 VideoMonitor.mode = VMODE_8BIT;
163 break;
164 case 15:
165 layout = FLAYOUT_HOST_555;
166 VideoMonitor.mode = VMODE_16BIT;
167 break;
168 case 16:
169 layout = FLAYOUT_HOST_565;
170 VideoMonitor.mode = VMODE_16BIT;
171 break;
172 case 24:
173 case 32:
174 layout = FLAYOUT_HOST_888;
175 VideoMonitor.mode = VMODE_32BIT;
176 break;
177 }
178 VideoMonitor.x = width;
179 VideoMonitor.y = height;
180 VideoMonitor.bytes_per_row = bytes_per_row;
181 if (native_byte_order)
182 MacFrameLayout = layout;
183 else
184 MacFrameLayout = FLAYOUT_DIRECT;
185 }
186
187 // Trap SHM errors
188 static bool shm_error = false;
189 static int (*old_error_handler)(Display *, XErrorEvent *);
190
191 static int error_handler(Display *d, XErrorEvent *e)
192 {
193 if (e->error_code == BadAccess) {
194 shm_error = true;
195 return 0;
196 } else
197 return old_error_handler(d, e);
198 }
199
200 // Init window mode
201 static bool init_window(int width, int height)
202 {
203 // Set absolute mouse mode
204 ADBSetRelMouseMode(false);
205
206 // Read frame skip prefs
207 frame_skip = PrefsFindInt32("frameskip");
208 if (frame_skip == 0)
209 frame_skip = 1;
210
211 // Create window
212 XSetWindowAttributes wattr;
213 wattr.event_mask = eventmask = win_eventmask;
214 wattr.background_pixel = black_pixel;
215 wattr.border_pixel = black_pixel;
216 wattr.backing_store = Always;
217 wattr.backing_planes = xdepth;
218
219 XSync(x_display, false);
220 the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
221 InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
222 CWBackingStore | CWBackingPlanes, &wattr);
223 XSync(x_display, false);
224 XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
225 XMapRaised(x_display, the_win);
226 XSync(x_display, false);
227
228 // Set colormap
229 if (depth == 8) {
230 XSetWindowColormap(x_display, the_win, cmap[0]);
231 XSetWMColormapWindows(x_display, the_win, &the_win, 1);
232 }
233
234 // Make window unresizable
235 XSizeHints *hints;
236 if ((hints = XAllocSizeHints()) != NULL) {
237 hints->min_width = width;
238 hints->max_width = width;
239 hints->min_height = height;
240 hints->max_height = height;
241 hints->flags = PMinSize | PMaxSize;
242 XSetWMNormalHints(x_display, the_win, hints);
243 XFree((char *)hints);
244 }
245
246 // Try to create and attach SHM image
247 have_shm = false;
248 if (depth != 1 && XShmQueryExtension(x_display)) {
249
250 // Create SHM image ("height + 2" for safety)
251 img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
252 shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
253 the_buffer = (uint8 *)shmat(shminfo.shmid, 0, 0);
254 shminfo.shmaddr = img->data = (char *)the_buffer;
255 shminfo.readOnly = False;
256
257 // Try to attach SHM image, catching errors
258 shm_error = false;
259 old_error_handler = XSetErrorHandler(error_handler);
260 XShmAttach(x_display, &shminfo);
261 XSync(x_display, false);
262 XSetErrorHandler(old_error_handler);
263 if (shm_error) {
264 shmdt(shminfo.shmaddr);
265 XDestroyImage(img);
266 shminfo.shmid = -1;
267 } else {
268 have_shm = true;
269 shmctl(shminfo.shmid, IPC_RMID, 0);
270 }
271 }
272
273 // Create normal X image if SHM doesn't work ("height + 2" for safety)
274 if (!have_shm) {
275 int bytes_per_row = width;
276 switch (depth) {
277 case 1:
278 bytes_per_row /= 8;
279 break;
280 case 15:
281 case 16:
282 bytes_per_row *= 2;
283 break;
284 case 24:
285 case 32:
286 bytes_per_row *= 4;
287 break;
288 }
289 the_buffer = (uint8 *)malloc((height + 2) * bytes_per_row);
290 img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer, width, height, 32, bytes_per_row);
291 }
292
293 // 1-Bit mode is big-endian
294 if (depth == 1) {
295 img->byte_order = MSBFirst;
296 img->bitmap_bit_order = MSBFirst;
297 }
298
299 // Allocate memory for frame buffer copy
300 the_buffer_copy = (uint8 *)malloc((height + 2) * img->bytes_per_line);
301
302 // Create GC
303 the_gc = XCreateGC(x_display, the_win, 0, 0);
304 XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
305
306 // Create cursor
307 cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor, 16, 16, 16, 2);
308 cursor_image->byte_order = MSBFirst;
309 cursor_image->bitmap_bit_order = MSBFirst;
310 cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor+32, 16, 16, 16, 2);
311 cursor_mask_image->byte_order = MSBFirst;
312 cursor_mask_image->bitmap_bit_order = MSBFirst;
313 cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
314 cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
315 cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
316 cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
317 mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
318
319 // Set VideoMonitor
320 #ifdef WORDS_BIGENDIAN
321 set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst);
322 #else
323 set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst);
324 #endif
325
326 #if REAL_ADDRESSING
327 VideoMonitor.mac_frame_base = (uint32)the_buffer;
328 MacFrameLayout = FLAYOUT_DIRECT;
329 #else
330 VideoMonitor.mac_frame_base = MacFrameBaseMac;
331 #endif
332 return true;
333 }
334
335 // Init fbdev DGA display
336 static bool init_fbdev_dga(char *in_fb_name)
337 {
338 #if ENABLE_FBDEV_DGA
339 // Find the maximum depth available
340 int ndepths, max_depth(0);
341 int *depths = XListDepths(x_display, screen, &ndepths);
342 if (depths == NULL) {
343 printf("FATAL: Could not determine the maximal depth available\n");
344 return false;
345 } else {
346 while (ndepths-- > 0) {
347 if (depths[ndepths] > max_depth)
348 max_depth = depths[ndepths];
349 }
350 }
351
352 // Get fbdevices file path from preferences
353 const char *fbd_path = PrefsFindString("fbdevicefile");
354
355 // Open fbdevices file
356 FILE *fp = fopen(fbd_path ? fbd_path : FBDEVICES_FILE_NAME, "r");
357 if (fp == NULL) {
358 char str[256];
359 sprintf(str, GetString(STR_NO_FBDEVICE_FILE_ERR), fbd_path ? fbd_path : FBDEVICES_FILE_NAME, strerror(errno));
360 ErrorAlert(str);
361 return false;
362 }
363
364 int fb_depth; // supported depth
365 uint32 fb_offset; // offset used for mmap(2)
366 char fb_name[20];
367 char line[256];
368 bool device_found = false;
369 while (fgets(line, 255, fp)) {
370 // Read line
371 int len = strlen(line);
372 if (len == 0)
373 continue;
374 line[len - 1] = '\0';
375
376 // Comments begin with "#" or ";"
377 if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
378 continue;
379
380 if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
381 && (strcmp(fb_name, in_fb_name) == 0) && (fb_depth == max_depth)) {
382 device_found = true;
383 break;
384 }
385 }
386
387 // fbdevices file completely read
388 fclose(fp);
389
390 // Frame buffer name not found ? Then, display warning
391 if (!device_found) {
392 char str[256];
393 sprintf(str, GetString(STR_FBDEV_NAME_ERR), in_fb_name, max_depth);
394 ErrorAlert(str);
395 return false;
396 }
397
398 int width = DisplayWidth(x_display, screen);
399 int height = DisplayHeight(x_display, screen);
400 depth = fb_depth; // max_depth
401
402 // Set relative mouse mode
403 ADBSetRelMouseMode(false);
404
405 // Create window
406 XSetWindowAttributes wattr;
407 wattr.override_redirect = True;
408 wattr.backing_store = NotUseful;
409 wattr.background_pixel = white_pixel;
410 wattr.border_pixel = black_pixel;
411 wattr.event_mask = eventmask = dga_eventmask;
412
413 XSync(x_display, false);
414 the_win = XCreateWindow(x_display, rootwin,
415 0, 0, width, height,
416 0, xdepth, InputOutput, vis,
417 CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWBackingStore,
418 &wattr);
419 XSync(x_display, false);
420 XMapRaised(x_display, the_win);
421 XSync(x_display, false);
422
423 // Grab mouse and keyboard
424 XGrabKeyboard(x_display, the_win, True,
425 GrabModeAsync, GrabModeAsync, CurrentTime);
426 XGrabPointer(x_display, the_win, True,
427 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
428 GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
429
430 // Set colormap
431 if (depth == 8) {
432 XSetWindowColormap(x_display, the_win, cmap[0]);
433 XSetWMColormapWindows(x_display, the_win, &the_win, 1);
434 }
435
436 // Set VideoMonitor
437 int bytes_per_row = width;
438 switch (depth) {
439 case 1:
440 bytes_per_row = ((width | 7) & ~7) >> 3;
441 break;
442 case 15:
443 case 16:
444 bytes_per_row *= 2;
445 break;
446 case 24:
447 case 32:
448 bytes_per_row *= 4;
449 break;
450 }
451
452 if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
453 if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
454 char str[256];
455 sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
456 ErrorAlert(str);
457 return false;
458 }
459 }
460
461 set_video_monitor(width, height, bytes_per_row, true);
462 #if REAL_ADDRESSING
463 VideoMonitor.mac_frame_base = (uint32)the_buffer;
464 MacFrameLayout = FLAYOUT_DIRECT;
465 #else
466 VideoMonitor.mac_frame_base = MacFrameBaseMac;
467 #endif
468 return true;
469 #else
470 ErrorAlert("Basilisk II has been compiled with fbdev DGA support disabled.");
471 return false;
472 #endif
473 }
474
475 // Init XF86 DGA display
476 static bool init_xf86_dga(int width, int height)
477 {
478 #if ENABLE_XF86_DGA
479 // Set relative mouse mode
480 ADBSetRelMouseMode(true);
481
482 // Create window
483 XSetWindowAttributes wattr;
484 wattr.event_mask = eventmask = dga_eventmask;
485 wattr.border_pixel = black_pixel;
486 wattr.override_redirect = True;
487
488 XSync(x_display, false);
489 the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
490 InputOutput, vis, CWEventMask | CWBorderPixel | CWOverrideRedirect, &wattr);
491 XSync(x_display, false);
492 XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
493 XMapRaised(x_display, the_win);
494 XSync(x_display, false);
495
496 // Establish direct screen connection
497 XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
498 XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
499 XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
500 XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
501
502 int v_width, v_bank, v_size;
503 XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
504 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
505 XF86DGASetViewPort(x_display, screen, 0, 0);
506 XF86DGASetVidPage(x_display, screen, 0);
507
508 // Set colormap
509 if (depth == 8) {
510 XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
511 XSetWMColormapWindows(x_display, the_win, &the_win, 1);
512 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
513 }
514
515 // Set VideoMonitor
516 int bytes_per_row = (v_width + 7) & ~7;
517 switch (depth) {
518 case 1:
519 bytes_per_row /= 8;
520 break;
521 case 15:
522 case 16:
523 bytes_per_row *= 2;
524 break;
525 case 24:
526 case 32:
527 bytes_per_row *= 4;
528 break;
529 }
530 set_video_monitor(width, height, bytes_per_row, true);
531 #if REAL_ADDRESSING
532 VideoMonitor.mac_frame_base = (uint32)the_buffer;
533 MacFrameLayout = FLAYOUT_DIRECT;
534 #else
535 VideoMonitor.mac_frame_base = MacFrameBaseMac;
536 #endif
537 return true;
538 #else
539 ErrorAlert("Basilisk II has been compiled with XF86 DGA support disabled.");
540 return false;
541 #endif
542 }
543
544 // Init keycode translation table
545 static void keycode_init(void)
546 {
547 bool use_kc = PrefsFindBool("keycodes");
548 if (use_kc) {
549
550 // Get keycode file path from preferences
551 const char *kc_path = PrefsFindString("keycodefile");
552
553 // Open keycode table
554 FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
555 if (f == NULL) {
556 char str[256];
557 sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
558 WarningAlert(str);
559 return;
560 }
561
562 // Default translation table
563 for (int i=0; i<256; i++)
564 keycode_table[i] = -1;
565
566 // Search for server vendor string, then read keycodes
567 const char *vendor = ServerVendor(x_display);
568 bool vendor_found = false;
569 char line[256];
570 while (fgets(line, 255, f)) {
571 // Read line
572 int len = strlen(line);
573 if (len == 0)
574 continue;
575 line[len-1] = 0;
576
577 // Comments begin with "#" or ";"
578 if (line[0] == '#' || line[0] == ';' || line[0] == 0)
579 continue;
580
581 if (vendor_found) {
582 // Read keycode
583 int x_code, mac_code;
584 if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
585 keycode_table[x_code & 0xff] = mac_code;
586 else
587 break;
588 } else {
589 // Search for vendor string
590 if (strstr(vendor, line) == vendor)
591 vendor_found = true;
592 }
593 }
594
595 // Keycode file completely read
596 fclose(f);
597 use_keycodes = vendor_found;
598
599 // Vendor not found? Then display warning
600 if (!vendor_found) {
601 char str[256];
602 sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME);
603 WarningAlert(str);
604 return;
605 }
606 }
607 }
608
609 bool VideoInit(bool classic)
610 {
611 // Init keycode translation
612 keycode_init();
613
614 // Find screen and root window
615 screen = XDefaultScreen(x_display);
616 rootwin = XRootWindow(x_display, screen);
617
618 // Get screen depth
619 xdepth = DefaultDepth(x_display, screen);
620
621 #if ENABLE_FBDEV_DGA
622 // Frame buffer name
623 char fb_name[20];
624
625 // Could do fbdev dga ?
626 if ((fbdev_fd = open(FBDEVICE_FILE_NAME, O_RDWR)) != -1)
627 has_dga = true;
628 else
629 has_dga = false;
630 #endif
631
632 #if ENABLE_XF86_DGA
633 // DGA available?
634 int event_base, error_base;
635 if (XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
636 int dga_flags = 0;
637 XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
638 has_dga = dga_flags & XF86DGADirectPresent;
639 } else
640 has_dga = false;
641 #endif
642
643 // Find black and white colors
644 XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
645 XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
646 XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white);
647 XAllocColor(x_display, DefaultColormap(x_display, screen), &white);
648 black_pixel = BlackPixel(x_display, screen);
649 white_pixel = WhitePixel(x_display, screen);
650
651 // Get appropriate visual
652 int color_class;
653 switch (xdepth) {
654 case 1:
655 color_class = StaticGray;
656 break;
657 case 8:
658 color_class = PseudoColor;
659 break;
660 case 15:
661 case 16:
662 case 24:
663 case 32:
664 color_class = TrueColor;
665 break;
666 default:
667 ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
668 return false;
669 }
670 if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
671 ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
672 return false;
673 }
674 if (visualInfo.depth != xdepth) {
675 ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
676 return false;
677 }
678 vis = visualInfo.visual;
679
680 // Mac screen depth is always 1 bit in Classic mode, but follows X depth otherwise
681 classic_mode = classic;
682 if (classic)
683 depth = 1;
684 else
685 depth = xdepth;
686
687 // Create color maps for 8 bit mode
688 if (depth == 8) {
689 cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
690 cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
691 XInstallColormap(x_display, cmap[0]);
692 XInstallColormap(x_display, cmap[1]);
693 }
694
695 // Get screen mode from preferences
696 const char *mode_str;
697 if (classic)
698 mode_str = "win/512/342";
699 else
700 mode_str = PrefsFindString("screen");
701
702 // Determine type and mode
703 int width = 512, height = 384;
704 display_type = DISPLAY_WINDOW;
705 if (mode_str) {
706 if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
707 display_type = DISPLAY_WINDOW;
708 #if ENABLE_FBDEV_DGA
709 else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
710 #else
711 else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) {
712 #endif
713 display_type = DISPLAY_DGA;
714 if (width > DisplayWidth(x_display, screen))
715 width = DisplayWidth(x_display, screen);
716 if (height > DisplayHeight(x_display, screen))
717 height = DisplayHeight(x_display, screen);
718 }
719 if (width <= 0)
720 width = DisplayWidth(x_display, screen);
721 if (height <= 0)
722 height = DisplayHeight(x_display, screen);
723 }
724
725 // Initialize according to display type
726 switch (display_type) {
727 case DISPLAY_WINDOW:
728 if (!init_window(width, height))
729 return false;
730 break;
731 case DISPLAY_DGA:
732 #if ENABLE_FBDEV_DGA
733 if (!init_fbdev_dga(fb_name))
734 #else
735 if (!init_xf86_dga(width, height))
736 #endif
737 return false;
738 break;
739 }
740
741 // Lock down frame buffer
742 pthread_mutex_lock(&frame_buffer_lock);
743
744 #if !REAL_ADDRESSING
745 // Set variables for UAE memory mapping
746 MacFrameBaseHost = the_buffer;
747 MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
748
749 // No special frame buffer in Classic mode (frame buffer is in Mac RAM)
750 if (classic)
751 MacFrameLayout = FLAYOUT_NONE;
752 #endif
753
754 // Start redraw/input thread
755 XSync(x_display, false);
756 redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
757 if (!redraw_thread_active)
758 printf("FATAL: cannot create redraw thread\n");
759 return redraw_thread_active;
760 }
761
762
763 /*
764 * Deinitialization
765 */
766
767 void VideoExit(void)
768 {
769 // Stop redraw thread
770 if (redraw_thread_active) {
771 redraw_thread_cancel = true;
772 #ifdef HAVE_PTHREAD_CANCEL
773 pthread_cancel(redraw_thread);
774 #endif
775 pthread_join(redraw_thread, NULL);
776 redraw_thread_active = false;
777 }
778
779 // Unlock frame buffer
780 pthread_mutex_unlock(&frame_buffer_lock);
781
782 // Close window and server connection
783 if (x_display != NULL) {
784 XSync(x_display, false);
785
786 #if ENABLE_XF86_DGA
787 if (display_type == DISPLAY_DGA) {
788 XF86DGADirectVideo(x_display, screen, 0);
789 XUngrabPointer(x_display, CurrentTime);
790 XUngrabKeyboard(x_display, CurrentTime);
791 }
792 #endif
793
794 #if ENABLE_FBDEV_DGA
795 if (display_type == DISPLAY_DGA) {
796 XUngrabPointer(x_display, CurrentTime);
797 XUngrabKeyboard(x_display, CurrentTime);
798 close(fbdev_fd);
799 }
800 #endif
801
802 if (the_buffer_copy) {
803 free(the_buffer_copy);
804 the_buffer_copy = NULL;
805 }
806
807 XFlush(x_display);
808 XSync(x_display, false);
809 if (depth == 8) {
810 XFreeColormap(x_display, cmap[0]);
811 XFreeColormap(x_display, cmap[1]);
812 }
813 }
814 }
815
816
817 /*
818 * Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode)
819 */
820
821 void VideoQuitFullScreen(void)
822 {
823 D(bug("VideoQuitFullScreen()\n"));
824 if (display_type == DISPLAY_DGA)
825 quit_full_screen = true;
826 }
827
828
829 /*
830 * Mac VBL interrupt
831 */
832
833 void VideoInterrupt(void)
834 {
835 // Emergency quit requested? Then quit
836 if (emerg_quit)
837 QuitEmulator();
838
839 // Temporarily give up frame buffer lock (this is the point where
840 // we are suspended when the user presses Ctrl-Tab)
841 pthread_mutex_unlock(&frame_buffer_lock);
842 pthread_mutex_lock(&frame_buffer_lock);
843 }
844
845
846 /*
847 * Set palette
848 */
849
850 void video_set_palette(uint8 *pal)
851 {
852 pthread_mutex_lock(&palette_lock);
853
854 // Convert colors to XColor array
855 for (int i=0; i<256; i++) {
856 palette[i].pixel = i;
857 palette[i].red = pal[i*3] * 0x0101;
858 palette[i].green = pal[i*3+1] * 0x0101;
859 palette[i].blue = pal[i*3+2] * 0x0101;
860 palette[i].flags = DoRed | DoGreen | DoBlue;
861 }
862
863 // Tell redraw thread to change palette
864 palette_changed = true;
865
866 pthread_mutex_unlock(&palette_lock);
867 }
868
869
870 /*
871 * Suspend/resume emulator
872 */
873
874 #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
875 static void suspend_emul(void)
876 {
877 if (display_type == DISPLAY_DGA) {
878 // Release ctrl key
879 ADBKeyUp(0x36);
880 ctrl_down = false;
881
882 // Lock frame buffer (this will stop the MacOS thread)
883 pthread_mutex_lock(&frame_buffer_lock);
884
885 // Save frame buffer
886 fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
887 if (fb_save)
888 memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row);
889
890 // Close full screen display
891 #if ENABLE_XF86_DGA
892 XF86DGADirectVideo(x_display, screen, 0);
893 #endif
894 XUngrabPointer(x_display, CurrentTime);
895 XUngrabKeyboard(x_display, CurrentTime);
896 XUnmapWindow(x_display, the_win);
897 XSync(x_display, false);
898
899 // Open "suspend" window
900 XSetWindowAttributes wattr;
901 wattr.event_mask = KeyPressMask;
902 wattr.background_pixel = black_pixel;
903 wattr.border_pixel = black_pixel;
904 wattr.backing_store = Always;
905 wattr.backing_planes = xdepth;
906 wattr.colormap = DefaultColormap(x_display, screen);
907
908 XSync(x_display, false);
909 suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
910 InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
911 CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
912 XSync(x_display, false);
913 XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
914 XMapRaised(x_display, suspend_win);
915 XSync(x_display, false);
916 emul_suspended = true;
917 }
918 }
919
920 static void resume_emul(void)
921 {
922 // Close "suspend" window
923 XDestroyWindow(x_display, suspend_win);
924 XSync(x_display, false);
925
926 // Reopen full screen display
927 XMapRaised(x_display, the_win);
928 XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
929 XSync(x_display, false);
930 XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
931 XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
932 #if ENABLE_XF86_DGA
933 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
934 XF86DGASetViewPort(x_display, screen, 0, 0);
935 #endif
936 XSync(x_display, false);
937
938 // Restore frame buffer
939 if (fb_save) {
940 memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
941 free(fb_save);
942 fb_save = NULL;
943 }
944
945 if (depth == 8)
946 #if ENABLE_XF86_DGA
947 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
948 #endif
949
950 // Unlock frame buffer (and continue MacOS thread)
951 pthread_mutex_unlock(&frame_buffer_lock);
952 emul_suspended = false;
953 }
954 #endif
955
956
957 /*
958 * Translate key event to Mac keycode
959 */
960
961 static int kc_decode(KeySym ks)
962 {
963 switch (ks) {
964 case XK_A: case XK_a: return 0x00;
965 case XK_B: case XK_b: return 0x0b;
966 case XK_C: case XK_c: return 0x08;
967 case XK_D: case XK_d: return 0x02;
968 case XK_E: case XK_e: return 0x0e;
969 case XK_F: case XK_f: return 0x03;
970 case XK_G: case XK_g: return 0x05;
971 case XK_H: case XK_h: return 0x04;
972 case XK_I: case XK_i: return 0x22;
973 case XK_J: case XK_j: return 0x26;
974 case XK_K: case XK_k: return 0x28;
975 case XK_L: case XK_l: return 0x25;
976 case XK_M: case XK_m: return 0x2e;
977 case XK_N: case XK_n: return 0x2d;
978 case XK_O: case XK_o: return 0x1f;
979 case XK_P: case XK_p: return 0x23;
980 case XK_Q: case XK_q: return 0x0c;
981 case XK_R: case XK_r: return 0x0f;
982 case XK_S: case XK_s: return 0x01;
983 case XK_T: case XK_t: return 0x11;
984 case XK_U: case XK_u: return 0x20;
985 case XK_V: case XK_v: return 0x09;
986 case XK_W: case XK_w: return 0x0d;
987 case XK_X: case XK_x: return 0x07;
988 case XK_Y: case XK_y: return 0x10;
989 case XK_Z: case XK_z: return 0x06;
990
991 case XK_1: case XK_exclam: return 0x12;
992 case XK_2: case XK_at: return 0x13;
993 case XK_3: case XK_numbersign: return 0x14;
994 case XK_4: case XK_dollar: return 0x15;
995 case XK_5: case XK_percent: return 0x17;
996 case XK_6: return 0x16;
997 case XK_7: return 0x1a;
998 case XK_8: return 0x1c;
999 case XK_9: return 0x19;
1000 case XK_0: return 0x1d;
1001
1002 case XK_grave: case XK_asciitilde: return 0x0a;
1003 case XK_minus: case XK_underscore: return 0x1b;
1004 case XK_equal: case XK_plus: return 0x18;
1005 case XK_bracketleft: case XK_braceleft: return 0x21;
1006 case XK_bracketright: case XK_braceright: return 0x1e;
1007 case XK_backslash: case XK_bar: return 0x2a;
1008 case XK_semicolon: case XK_colon: return 0x29;
1009 case XK_apostrophe: case XK_quotedbl: return 0x27;
1010 case XK_comma: case XK_less: return 0x2b;
1011 case XK_period: case XK_greater: return 0x2f;
1012 case XK_slash: case XK_question: return 0x2c;
1013
1014 #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1015 case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1016 #else
1017 case XK_Tab: return 0x30;
1018 #endif
1019 case XK_Return: return 0x24;
1020 case XK_space: return 0x31;
1021 case XK_BackSpace: return 0x33;
1022
1023 case XK_Delete: return 0x75;
1024 case XK_Insert: return 0x72;
1025 case XK_Home: case XK_Help: return 0x73;
1026 case XK_End: return 0x77;
1027 #ifdef __hpux
1028 case XK_Prior: return 0x74;
1029 case XK_Next: return 0x79;
1030 #else
1031 case XK_Page_Up: return 0x74;
1032 case XK_Page_Down: return 0x79;
1033 #endif
1034
1035 case XK_Control_L: return 0x36;
1036 case XK_Control_R: return 0x36;
1037 case XK_Shift_L: return 0x38;
1038 case XK_Shift_R: return 0x38;
1039 case XK_Alt_L: return 0x37;
1040 case XK_Alt_R: return 0x37;
1041 case XK_Meta_L: return 0x3a;
1042 case XK_Meta_R: return 0x3a;
1043 case XK_Menu: return 0x32;
1044 case XK_Caps_Lock: return 0x39;
1045 case XK_Num_Lock: return 0x47;
1046
1047 case XK_Up: return 0x3e;
1048 case XK_Down: return 0x3d;
1049 case XK_Left: return 0x3b;
1050 case XK_Right: return 0x3c;
1051
1052 case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
1053
1054 case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
1055 case XK_F2: return 0x78;
1056 case XK_F3: return 0x63;
1057 case XK_F4: return 0x76;
1058 case XK_F5: return 0x60;
1059 case XK_F6: return 0x61;
1060 case XK_F7: return 0x62;
1061 case XK_F8: return 0x64;
1062 case XK_F9: return 0x65;
1063 case XK_F10: return 0x6d;
1064 case XK_F11: return 0x67;
1065 case XK_F12: return 0x6f;
1066
1067 case XK_Print: return 0x69;
1068 case XK_Scroll_Lock: return 0x6b;
1069 case XK_Pause: return 0x71;
1070
1071 #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
1072 case XK_KP_0: case XK_KP_Insert: return 0x52;
1073 case XK_KP_1: case XK_KP_End: return 0x53;
1074 case XK_KP_2: case XK_KP_Down: return 0x54;
1075 case XK_KP_3: case XK_KP_Next: return 0x55;
1076 case XK_KP_4: case XK_KP_Left: return 0x56;
1077 case XK_KP_5: case XK_KP_Begin: return 0x57;
1078 case XK_KP_6: case XK_KP_Right: return 0x58;
1079 case XK_KP_7: case XK_KP_Home: return 0x59;
1080 case XK_KP_8: case XK_KP_Up: return 0x5b;
1081 case XK_KP_9: case XK_KP_Prior: return 0x5c;
1082 case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
1083 #else
1084 case XK_KP_0: return 0x52;
1085 case XK_KP_1: return 0x53;
1086 case XK_KP_2: return 0x54;
1087 case XK_KP_3: return 0x55;
1088 case XK_KP_4: return 0x56;
1089 case XK_KP_5: return 0x57;
1090 case XK_KP_6: return 0x58;
1091 case XK_KP_7: return 0x59;
1092 case XK_KP_8: return 0x5b;
1093 case XK_KP_9: return 0x5c;
1094 case XK_KP_Decimal: return 0x41;
1095 #endif
1096 case XK_KP_Add: return 0x45;
1097 case XK_KP_Subtract: return 0x4e;
1098 case XK_KP_Multiply: return 0x43;
1099 case XK_KP_Divide: return 0x4b;
1100 case XK_KP_Enter: return 0x4c;
1101 case XK_KP_Equal: return 0x51;
1102 }
1103 return -1;
1104 }
1105
1106 static int event2keycode(XKeyEvent *ev)
1107 {
1108 KeySym ks;
1109 int as;
1110 int i = 0;
1111
1112 do {
1113 ks = XLookupKeysym(ev, i++);
1114 as = kc_decode(ks);
1115 if (as != -1)
1116 return as;
1117 } while (ks != NoSymbol);
1118
1119 return -1;
1120 }
1121
1122
1123 /*
1124 * X event handling
1125 */
1126
1127 static void handle_events(void)
1128 {
1129 XEvent event;
1130 for (;;) {
1131 if (!XCheckMaskEvent(x_display, eventmask, &event))
1132 break;
1133
1134 switch (event.type) {
1135 // Mouse button
1136 case ButtonPress: {
1137 unsigned int button = ((XButtonEvent *)&event)->button;
1138 if (button < 4)
1139 ADBMouseDown(button - 1);
1140 break;
1141 }
1142 case ButtonRelease: {
1143 unsigned int button = ((XButtonEvent *)&event)->button;
1144 if (button < 4)
1145 ADBMouseUp(button - 1);
1146 break;
1147 }
1148
1149 // Mouse moved
1150 case EnterNotify:
1151 ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1152 break;
1153 case MotionNotify:
1154 ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1155 break;
1156
1157 // Keyboard
1158 case KeyPress: {
1159 int code;
1160 if (use_keycodes) {
1161 event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys
1162 code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1163 } else
1164 code = event2keycode((XKeyEvent *)&event);
1165 if (code != -1) {
1166 if (!emul_suspended) {
1167 if (code == 0x39) { // Caps Lock pressed
1168 if (caps_on) {
1169 ADBKeyUp(code);
1170 caps_on = false;
1171 } else {
1172 ADBKeyDown(code);
1173 caps_on = true;
1174 }
1175 } else
1176 ADBKeyDown(code);
1177 if (code == 0x36)
1178 ctrl_down = true;
1179 } else {
1180 #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1181 if (code == 0x31)
1182 resume_emul(); // Space wakes us up
1183 #endif
1184 }
1185 }
1186 break;
1187 }
1188 case KeyRelease: {
1189 int code;
1190 if (use_keycodes) {
1191 event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys
1192 code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1193 } else
1194 code = event2keycode((XKeyEvent *)&event);
1195 if (code != -1 && code != 0x39) { // Don't propagate Caps Lock releases
1196 ADBKeyUp(code);
1197 if (code == 0x36)
1198 ctrl_down = false;
1199 }
1200 break;
1201 }
1202
1203 // Hidden parts exposed, force complete refresh of window
1204 case Expose:
1205 if (display_type == DISPLAY_WINDOW)
1206 memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1207 break;
1208 }
1209 }
1210 }
1211
1212
1213 /*
1214 * Window display update
1215 */
1216
1217 static void update_display(void)
1218 {
1219 // In classic mode, copy the frame buffer from Mac RAM
1220 if (classic_mode)
1221 memcpy(the_buffer, Mac2HostAddr(0x3fa700), VideoMonitor.bytes_per_row * VideoMonitor.y);
1222
1223 // Incremental update code
1224 int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1225 int bytes_per_row = VideoMonitor.bytes_per_row;
1226 int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1227 uint8 *p, *p2;
1228
1229 // Check for first line from top and first line from bottom that have changed
1230 y1 = 0;
1231 for (j=0; j<VideoMonitor.y; j++) {
1232 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1233 y1 = j;
1234 break;
1235 }
1236 }
1237 y2 = y1 - 1;
1238 for (j=VideoMonitor.y-1; j>=y1; j--) {
1239 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1240 y2 = j;
1241 break;
1242 }
1243 }
1244 high = y2 - y1 + 1;
1245
1246 // Check for first column from left and first column from right that have changed
1247 if (high) {
1248 if (depth == 1) {
1249 x1 = VideoMonitor.x;
1250 for (j=y1; j<=y2; j++) {
1251 p = &the_buffer[j * bytes_per_row];
1252 p2 = &the_buffer_copy[j * bytes_per_row];
1253 for (i=0; i<(x1>>3); i++) {
1254 if (*p != *p2) {
1255 x1 = i << 3;
1256 break;
1257 }
1258 p++;
1259 p2++;
1260 }
1261 }
1262 x2 = x1;
1263 for (j=y1; j<=y2; j++) {
1264 p = &the_buffer[j * bytes_per_row];
1265 p2 = &the_buffer_copy[j * bytes_per_row];
1266 p += bytes_per_row;
1267 p2 += bytes_per_row;
1268 for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1269 p--;
1270 p2--;
1271 if (*p != *p2) {
1272 x2 = i << 3;
1273 break;
1274 }
1275 }
1276 }
1277 wide = x2 - x1;
1278
1279 // Update copy of the_buffer
1280 if (high && wide) {
1281 for (j=y1; j<=y2; j++) {
1282 i = j * bytes_per_row + (x1 >> 3);
1283 memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
1284 }
1285 }
1286
1287 } else {
1288 x1 = VideoMonitor.x;
1289 for (j=y1; j<=y2; j++) {
1290 p = &the_buffer[j * bytes_per_row];
1291 p2 = &the_buffer_copy[j * bytes_per_row];
1292 for (i=0; i<x1; i++) {
1293 if (memcmp(p, p2, bytes_per_pixel)) {
1294 x1 = i;
1295 break;
1296 }
1297 p += bytes_per_pixel;
1298 p2 += bytes_per_pixel;
1299 }
1300 }
1301 x2 = x1;
1302 for (j=y1; j<=y2; j++) {
1303 p = &the_buffer[j * bytes_per_row];
1304 p2 = &the_buffer_copy[j * bytes_per_row];
1305 p += bytes_per_row;
1306 p2 += bytes_per_row;
1307 for (i=VideoMonitor.x; i>x2; i--) {
1308 p -= bytes_per_pixel;
1309 p2 -= bytes_per_pixel;
1310 if (memcmp(p, p2, bytes_per_pixel)) {
1311 x2 = i;
1312 break;
1313 }
1314 }
1315 }
1316 wide = x2 - x1;
1317
1318 // Update copy of the_buffer
1319 if (high && wide) {
1320 for (j=y1; j<=y2; j++) {
1321 i = j * bytes_per_row + x1 * bytes_per_pixel;
1322 memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
1323 }
1324 }
1325 }
1326 }
1327
1328 // Refresh display
1329 if (high && wide) {
1330 if (have_shm)
1331 XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
1332 else
1333 XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1334 }
1335
1336 // Has the Mac started? (cursor data is not valid otherwise)
1337 if (HasMacStarted()) {
1338
1339 // Set new cursor image if it was changed
1340 if (memcmp(the_cursor, Mac2HostAddr(0x844), 64)) {
1341 memcpy(the_cursor, Mac2HostAddr(0x844), 64);
1342 memcpy(cursor_image->data, the_cursor, 32);
1343 memcpy(cursor_mask_image->data, the_cursor+32, 32);
1344 XFreeCursor(x_display, mac_cursor);
1345 XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
1346 XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
1347 mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, ReadMacInt8(0x885), ReadMacInt8(0x887));
1348 XDefineCursor(x_display, the_win, mac_cursor);
1349 }
1350 }
1351 }
1352
1353
1354 /*
1355 * Thread for screen refresh, input handling etc.
1356 */
1357
1358 static void *redraw_func(void *arg)
1359 {
1360 int tick_counter = 0;
1361
1362 while (!redraw_thread_cancel) {
1363
1364 // Wait
1365 #ifdef HAVE_NANOSLEEP
1366 struct timespec req = {0, 16666667};
1367 nanosleep(&req, NULL);
1368 #else
1369 usleep(16667);
1370 #endif
1371
1372 #if ENABLE_XF86_DGA
1373 // Quit DGA mode if requested
1374 if (quit_full_screen) {
1375 quit_full_screen = false;
1376 if (display_type == DISPLAY_DGA) {
1377 XF86DGADirectVideo(x_display, screen, 0);
1378 XUngrabPointer(x_display, CurrentTime);
1379 XUngrabKeyboard(x_display, CurrentTime);
1380 XUnmapWindow(x_display, the_win);
1381 XSync(x_display, false);
1382 }
1383 }
1384 #endif
1385
1386 #if ENABLE_FBDEV_DGA
1387 // Quit DGA mode if requested
1388 if (quit_full_screen) {
1389 quit_full_screen = false;
1390 if (display_type == DISPLAY_DGA) {
1391 XUngrabPointer(x_display, CurrentTime);
1392 XUngrabKeyboard(x_display, CurrentTime);
1393 XUnmapWindow(x_display, the_win);
1394 XSync(x_display, false);
1395 }
1396 }
1397 #endif
1398 // Handle X events
1399 handle_events();
1400
1401 // Handle palette changes
1402 pthread_mutex_lock(&palette_lock);
1403 if (palette_changed) {
1404 palette_changed = false;
1405 if (depth == 8) {
1406 XStoreColors(x_display, cmap[0], palette, 256);
1407 XStoreColors(x_display, cmap[1], palette, 256);
1408
1409 #if ENABLE_XF86_DGA
1410 if (display_type == DISPLAY_DGA) {
1411 current_dga_cmap ^= 1;
1412 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1413 }
1414 #endif
1415 }
1416 }
1417 pthread_mutex_unlock(&palette_lock);
1418
1419 // In window mode, update display and mouse pointer
1420 if (display_type == DISPLAY_WINDOW) {
1421 tick_counter++;
1422 if (tick_counter >= frame_skip) {
1423 tick_counter = 0;
1424 update_display();
1425 }
1426 }
1427 }
1428 return NULL;
1429 }