ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.2
Committed: 1999-10-03T16:20:08Z (25 years, 2 months ago) by cebix
Branch: MAIN
Changes since 1.1: +1 -1 lines
Log Message:
- renamed basilisk_ii_keycodes to keycodes
- default location of keycodes file is now $(sharedir)/keycodes
  (depends on install prefix)
- improved the install target

File Contents

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