ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.5
Committed: 1999-10-05T14:59:46Z (25 years, 1 month ago) by cebix
Branch: MAIN
CVS Tags: release-0_7-2
Changes since 1.4: +8 -4 lines
Log Message:
- fixed some compiler warnings
- video_x.cpp: now checks whether the X server has the XFree86DGA extension

File Contents

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