ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.16
Committed: 2000-07-13T16:12:33Z (23 years, 11 months ago) by cebix
Branch: MAIN
CVS Tags: snapshot-13072000
Changes since 1.15: +148 -18 lines
Log Message:
- DGA and SHM are only tried on local X11 displays
- re-integrated old window update method (better performance over a networked
  display connection), frameskip=0 selects new method, other values select
  old method
- fixed compilation errors

File Contents

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