ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.17
Committed: 2000-07-22T16:07:21Z (24 years, 4 months ago) by cebix
Branch: MAIN
Changes since 1.16: +1 -5 lines
Log Message:
- new FOURCC() macro in macos_util.h

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