ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.4
Committed: 1999-10-04T21:07:18Z (25 years, 2 months ago) by cebix
Branch: MAIN
Changes since 1.3: +1 -1 lines
Log Message:
- improved configuration and installation

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.4 const char KEYCODE_FILE_NAME[] = DATADIR "/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 cebix 1.3 static bool caps_on = false; // Flag: Caps Lock on
79 cebix 1.1 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;
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 dga_flags = 0;
474     XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
475     has_dga = dga_flags & XF86DGADirectPresent;
476     #endif
477    
478     // Find black and white colors
479     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
480     XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
481     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white);
482     XAllocColor(x_display, DefaultColormap(x_display, screen), &white);
483     black_pixel = BlackPixel(x_display, screen);
484     white_pixel = WhitePixel(x_display, screen);
485    
486     // Get appropriate visual
487     int color_class;
488     switch (xdepth) {
489     case 1:
490     color_class = StaticGray;
491     break;
492     case 8:
493     color_class = PseudoColor;
494     break;
495     case 15:
496     case 16:
497     case 24:
498     case 32:
499     color_class = TrueColor;
500     break;
501     default:
502     ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
503     return false;
504     }
505     if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
506     ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
507     return false;
508     }
509     if (visualInfo.depth != xdepth) {
510     ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
511     return false;
512     }
513     vis = visualInfo.visual;
514    
515     // Mac screen depth is always 1 bit in Classic mode, but follows X depth otherwise
516     classic_mode = classic;
517     if (classic)
518     depth = 1;
519     else
520     depth = xdepth;
521    
522     // Create color maps for 8 bit mode
523     if (depth == 8) {
524     cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
525     cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
526     XInstallColormap(x_display, cmap[0]);
527     XInstallColormap(x_display, cmap[1]);
528     }
529    
530     // Get screen mode from preferences
531     const char *mode_str;
532     if (classic)
533     mode_str = "win/512/342";
534     else
535     mode_str = PrefsFindString("screen");
536    
537     // Determine type and mode
538     int width = 512, height = 384;
539     display_type = DISPLAY_WINDOW;
540     if (mode_str) {
541     if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
542     display_type = DISPLAY_WINDOW;
543 cebix 1.3 else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) {
544 cebix 1.1 display_type = DISPLAY_DGA;
545 cebix 1.3 if (width > DisplayWidth(x_display, screen))
546     width = DisplayWidth(x_display, screen);
547     if (height > DisplayHeight(x_display, screen))
548     height = DisplayHeight(x_display, screen);
549     }
550     if (width <= 0)
551 cebix 1.1 width = DisplayWidth(x_display, screen);
552 cebix 1.3 if (height <= 0)
553 cebix 1.1 height = DisplayHeight(x_display, screen);
554     }
555    
556     // Initialize according to display type
557     switch (display_type) {
558     case DISPLAY_WINDOW:
559     if (!init_window(width, height))
560     return false;
561     break;
562     case DISPLAY_DGA:
563     if (!init_dga(width, height))
564     return false;
565     break;
566     }
567    
568     // Lock down frame buffer
569     pthread_mutex_lock(&frame_buffer_lock);
570    
571     #if !REAL_ADDRESSING
572     // Set variables for UAE memory mapping
573     MacFrameBaseHost = the_buffer;
574     MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
575    
576     // No special frame buffer in Classic mode (frame buffer is in Mac RAM)
577     if (classic)
578     MacFrameLayout = FLAYOUT_NONE;
579     #endif
580    
581     // Start redraw/input thread
582     XSync(x_display, false);
583     redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
584     if (!redraw_thread_active)
585     printf("FATAL: cannot create redraw thread\n");
586     return redraw_thread_active;
587     }
588    
589    
590     /*
591     * Deinitialization
592     */
593    
594     void VideoExit(void)
595     {
596     // Stop redraw thread
597     if (redraw_thread_active) {
598     redraw_thread_cancel = true;
599     #ifdef HAVE_PTHREAD_CANCEL
600     pthread_cancel(redraw_thread);
601     #endif
602     pthread_join(redraw_thread, NULL);
603     redraw_thread_active = false;
604     }
605    
606     // Unlock frame buffer
607     pthread_mutex_unlock(&frame_buffer_lock);
608    
609     // Close window and server connection
610     if (x_display != NULL) {
611     XSync(x_display, false);
612    
613     #if ENABLE_DGA
614     if (display_type == DISPLAY_DGA) {
615     XF86DGADirectVideo(x_display, screen, 0);
616     XUngrabPointer(x_display, CurrentTime);
617     XUngrabKeyboard(x_display, CurrentTime);
618     }
619     #endif
620    
621     if (the_buffer_copy) {
622     free(the_buffer_copy);
623     the_buffer_copy = NULL;
624     }
625    
626     XFlush(x_display);
627     XSync(x_display, false);
628     if (depth == 8) {
629     XFreeColormap(x_display, cmap[0]);
630     XFreeColormap(x_display, cmap[1]);
631     }
632     }
633     }
634    
635    
636     /*
637     * Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode)
638     */
639    
640     void VideoQuitFullScreen(void)
641     {
642     D(bug("VideoQuitFullScreen()\n"));
643     if (display_type == DISPLAY_DGA)
644     quit_full_screen = true;
645     }
646    
647    
648     /*
649     * Mac VBL interrupt
650     */
651    
652     void VideoInterrupt(void)
653     {
654     // Emergency quit requested? Then quit
655     if (emerg_quit)
656     QuitEmulator();
657    
658     // Temporarily give up frame buffer lock (this is the point where
659     // we are suspended when the user presses Ctrl-Tab)
660     pthread_mutex_unlock(&frame_buffer_lock);
661     pthread_mutex_lock(&frame_buffer_lock);
662     }
663    
664    
665     /*
666     * Set palette
667     */
668    
669     void video_set_palette(uint8 *pal)
670     {
671     pthread_mutex_lock(&palette_lock);
672    
673     // Convert colors to XColor array
674     for (int i=0; i<256; i++) {
675     palette[i].pixel = i;
676     palette[i].red = pal[i*3] * 0x0101;
677     palette[i].green = pal[i*3+1] * 0x0101;
678     palette[i].blue = pal[i*3+2] * 0x0101;
679     palette[i].flags = DoRed | DoGreen | DoBlue;
680     }
681    
682     // Tell redraw thread to change palette
683     palette_changed = true;
684    
685     pthread_mutex_unlock(&palette_lock);
686     }
687    
688    
689     /*
690     * Suspend/resume emulator
691     */
692    
693     #if ENABLE_DGA
694     static void suspend_emul(void)
695     {
696     if (display_type == DISPLAY_DGA) {
697     // Release ctrl key
698     ADBKeyUp(0x36);
699     ctrl_down = false;
700    
701     // Lock frame buffer (this will stop the MacOS thread)
702     pthread_mutex_lock(&frame_buffer_lock);
703    
704     // Save frame buffer
705     fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
706     if (fb_save)
707     memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row);
708    
709     // Close full screen display
710     XF86DGADirectVideo(x_display, screen, 0);
711     XUngrabPointer(x_display, CurrentTime);
712     XUngrabKeyboard(x_display, CurrentTime);
713     XUnmapWindow(x_display, the_win);
714     XSync(x_display, false);
715    
716     // Open "suspend" window
717     XSetWindowAttributes wattr;
718     wattr.event_mask = KeyPressMask;
719     wattr.background_pixel = black_pixel;
720     wattr.border_pixel = black_pixel;
721     wattr.backing_store = Always;
722     wattr.backing_planes = xdepth;
723     wattr.colormap = DefaultColormap(x_display, screen);
724     XSync(x_display, false);
725     suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
726     InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
727     CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
728     XSync(x_display, false);
729     XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
730     XMapRaised(x_display, suspend_win);
731     XSync(x_display, false);
732     emul_suspended = true;
733     }
734     }
735    
736     static void resume_emul(void)
737     {
738     // Close "suspend" window
739     XDestroyWindow(x_display, suspend_win);
740     XSync(x_display, false);
741    
742     // Reopen full screen display
743     XMapRaised(x_display, the_win);
744     XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
745     XSync(x_display, false);
746     XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
747     XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
748     XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
749     XF86DGASetViewPort(x_display, screen, 0, 0);
750     XSync(x_display, false);
751    
752     // Restore frame buffer
753     if (fb_save) {
754     memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
755     free(fb_save);
756     fb_save = NULL;
757     }
758     if (depth == 8)
759     XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
760    
761     // Unlock frame buffer (and continue MacOS thread)
762     pthread_mutex_unlock(&frame_buffer_lock);
763     emul_suspended = false;
764     }
765     #endif
766    
767    
768     /*
769     * Translate key event to Mac keycode
770     */
771    
772     static int kc_decode(KeySym ks)
773     {
774     switch (ks) {
775     case XK_A: case XK_a: return 0x00;
776     case XK_B: case XK_b: return 0x0b;
777     case XK_C: case XK_c: return 0x08;
778     case XK_D: case XK_d: return 0x02;
779     case XK_E: case XK_e: return 0x0e;
780     case XK_F: case XK_f: return 0x03;
781     case XK_G: case XK_g: return 0x05;
782     case XK_H: case XK_h: return 0x04;
783     case XK_I: case XK_i: return 0x22;
784     case XK_J: case XK_j: return 0x26;
785     case XK_K: case XK_k: return 0x28;
786     case XK_L: case XK_l: return 0x25;
787     case XK_M: case XK_m: return 0x2e;
788     case XK_N: case XK_n: return 0x2d;
789     case XK_O: case XK_o: return 0x1f;
790     case XK_P: case XK_p: return 0x23;
791     case XK_Q: case XK_q: return 0x0c;
792     case XK_R: case XK_r: return 0x0f;
793     case XK_S: case XK_s: return 0x01;
794     case XK_T: case XK_t: return 0x11;
795     case XK_U: case XK_u: return 0x20;
796     case XK_V: case XK_v: return 0x09;
797     case XK_W: case XK_w: return 0x0d;
798     case XK_X: case XK_x: return 0x07;
799     case XK_Y: case XK_y: return 0x10;
800     case XK_Z: case XK_z: return 0x06;
801    
802     case XK_1: case XK_exclam: return 0x12;
803     case XK_2: case XK_at: return 0x13;
804     case XK_3: case XK_numbersign: return 0x14;
805     case XK_4: case XK_dollar: return 0x15;
806     case XK_5: case XK_percent: return 0x17;
807     case XK_6: return 0x16;
808     case XK_7: return 0x1a;
809     case XK_8: return 0x1c;
810     case XK_9: return 0x19;
811     case XK_0: return 0x1d;
812    
813     case XK_grave: case XK_asciitilde: return 0x0a;
814     case XK_minus: case XK_underscore: return 0x1b;
815     case XK_equal: case XK_plus: return 0x18;
816     case XK_bracketleft: case XK_braceleft: return 0x21;
817     case XK_bracketright: case XK_braceright: return 0x1e;
818     case XK_backslash: case XK_bar: return 0x2a;
819     case XK_semicolon: case XK_colon: return 0x29;
820     case XK_apostrophe: case XK_quotedbl: return 0x27;
821     case XK_comma: case XK_less: return 0x2b;
822     case XK_period: case XK_greater: return 0x2f;
823     case XK_slash: case XK_question: return 0x2c;
824    
825     #if ENABLE_DGA
826     case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
827     #else
828     case XK_Tab: return 0x30;
829     #endif
830     case XK_Return: return 0x24;
831     case XK_space: return 0x31;
832     case XK_BackSpace: return 0x33;
833    
834     case XK_Delete: return 0x75;
835     case XK_Insert: return 0x72;
836     case XK_Home: case XK_Help: return 0x73;
837     case XK_End: return 0x77;
838     #ifdef __hpux
839     case XK_Prior: return 0x74;
840     case XK_Next: return 0x79;
841     #else
842     case XK_Page_Up: return 0x74;
843     case XK_Page_Down: return 0x79;
844     #endif
845    
846     case XK_Control_L: return 0x36;
847     case XK_Control_R: return 0x36;
848     case XK_Shift_L: return 0x38;
849     case XK_Shift_R: return 0x38;
850     case XK_Alt_L: return 0x37;
851     case XK_Alt_R: return 0x37;
852     case XK_Meta_L: return 0x3a;
853     case XK_Meta_R: return 0x3a;
854     case XK_Menu: return 0x32;
855     case XK_Caps_Lock: return 0x39;
856     case XK_Num_Lock: return 0x47;
857    
858     case XK_Up: return 0x3e;
859     case XK_Down: return 0x3d;
860     case XK_Left: return 0x3b;
861     case XK_Right: return 0x3c;
862    
863     case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
864    
865     case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
866     case XK_F2: return 0x78;
867     case XK_F3: return 0x63;
868     case XK_F4: return 0x76;
869     case XK_F5: return 0x60;
870     case XK_F6: return 0x61;
871     case XK_F7: return 0x62;
872     case XK_F8: return 0x64;
873     case XK_F9: return 0x65;
874     case XK_F10: return 0x6d;
875     case XK_F11: return 0x67;
876     case XK_F12: return 0x6f;
877    
878     case XK_Print: return 0x69;
879     case XK_Scroll_Lock: return 0x6b;
880     case XK_Pause: return 0x71;
881    
882     #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
883     case XK_KP_0: case XK_KP_Insert: return 0x52;
884     case XK_KP_1: case XK_KP_End: return 0x53;
885     case XK_KP_2: case XK_KP_Down: return 0x54;
886     case XK_KP_3: case XK_KP_Next: return 0x55;
887     case XK_KP_4: case XK_KP_Left: return 0x56;
888     case XK_KP_5: case XK_KP_Begin: return 0x57;
889     case XK_KP_6: case XK_KP_Right: return 0x58;
890     case XK_KP_7: case XK_KP_Home: return 0x59;
891     case XK_KP_8: case XK_KP_Up: return 0x5b;
892     case XK_KP_9: case XK_KP_Prior: return 0x5c;
893     case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
894     #else
895     case XK_KP_0: return 0x52;
896     case XK_KP_1: return 0x53;
897     case XK_KP_2: return 0x54;
898     case XK_KP_3: return 0x55;
899     case XK_KP_4: return 0x56;
900     case XK_KP_5: return 0x57;
901     case XK_KP_6: return 0x58;
902     case XK_KP_7: return 0x59;
903     case XK_KP_8: return 0x5b;
904     case XK_KP_9: return 0x5c;
905     case XK_KP_Decimal: return 0x41;
906     #endif
907     case XK_KP_Add: return 0x45;
908     case XK_KP_Subtract: return 0x4e;
909     case XK_KP_Multiply: return 0x43;
910     case XK_KP_Divide: return 0x4b;
911     case XK_KP_Enter: return 0x4c;
912     case XK_KP_Equal: return 0x51;
913     }
914     return -1;
915     }
916    
917     static int event2keycode(XKeyEvent *ev)
918     {
919     KeySym ks;
920     int as;
921     int i = 0;
922    
923     do {
924     ks = XLookupKeysym(ev, i++);
925     as = kc_decode(ks);
926     if (as != -1)
927     return as;
928     } while (ks != NoSymbol);
929    
930     return -1;
931     }
932    
933    
934     /*
935     * X event handling
936     */
937    
938     static void handle_events(void)
939     {
940     XEvent event;
941     for (;;) {
942     if (!XCheckMaskEvent(x_display, eventmask, &event))
943     break;
944    
945     switch (event.type) {
946     // Mouse button
947     case ButtonPress: {
948     unsigned int button = ((XButtonEvent *)&event)->button;
949     if (button < 4)
950     ADBMouseDown(button - 1);
951     break;
952     }
953     case ButtonRelease: {
954     unsigned int button = ((XButtonEvent *)&event)->button;
955     if (button < 4)
956     ADBMouseUp(button - 1);
957     break;
958     }
959    
960     // Mouse moved
961     case EnterNotify:
962     ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
963     break;
964     case MotionNotify:
965     ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
966     break;
967    
968     // Keyboard
969     case KeyPress: {
970     int code;
971     if (use_keycodes) {
972     event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys
973     code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
974     } else
975     code = event2keycode((XKeyEvent *)&event);
976     if (code != -1) {
977     if (!emul_suspended) {
978 cebix 1.3 if (code == 0x39) { // Caps Lock pressed
979     if (caps_on) {
980     ADBKeyUp(code);
981     caps_on = false;
982     } else {
983     ADBKeyDown(code);
984     caps_on = true;
985     }
986     } else
987     ADBKeyDown(code);
988 cebix 1.1 if (code == 0x36)
989     ctrl_down = true;
990     } else {
991     #if ENABLE_DGA
992     if (code == 0x31)
993     resume_emul(); // Space wakes us up
994     #endif
995     }
996     }
997     break;
998     }
999     case KeyRelease: {
1000     int code;
1001     if (use_keycodes) {
1002     event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys
1003     code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1004     } else
1005     code = event2keycode((XKeyEvent *)&event);
1006 cebix 1.3 if (code != -1 && code != 0x39) { // Don't propagate Caps Lock releases
1007 cebix 1.1 ADBKeyUp(code);
1008     if (code == 0x36)
1009     ctrl_down = false;
1010     }
1011     break;
1012     }
1013    
1014     // Hidden parts exposed, force complete refresh of window
1015     case Expose:
1016     if (display_type == DISPLAY_WINDOW)
1017     memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1018     break;
1019     }
1020     }
1021     }
1022    
1023    
1024     /*
1025     * Window display update
1026     */
1027    
1028     static void update_display(void)
1029     {
1030     // In classic mode, copy the frame buffer from Mac RAM
1031     if (classic_mode)
1032     memcpy(the_buffer, Mac2HostAddr(0x3fa700), VideoMonitor.bytes_per_row * VideoMonitor.y);
1033    
1034     // Incremental update code
1035     int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1036     int bytes_per_row = VideoMonitor.bytes_per_row;
1037     int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1038     uint8 *p, *p2;
1039    
1040     // Check for first line from top and first line from bottom that have changed
1041     y1 = 0;
1042     for (j=0; j<VideoMonitor.y; j++) {
1043     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1044     y1 = j;
1045     break;
1046     }
1047     }
1048     y2 = y1 - 1;
1049     for (j=VideoMonitor.y-1; j>=y1; j--) {
1050     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1051     y2 = j;
1052     break;
1053     }
1054     }
1055     high = y2 - y1 + 1;
1056    
1057     // Check for first column from left and first column from right that have changed
1058     if (high) {
1059     if (depth == 1) {
1060     x1 = VideoMonitor.x;
1061     for (j=y1; j<=y2; j++) {
1062     p = &the_buffer[j * bytes_per_row];
1063     p2 = &the_buffer_copy[j * bytes_per_row];
1064     for (i=0; i<(x1>>3); i++) {
1065     if (*p != *p2) {
1066     x1 = i << 3;
1067     break;
1068     }
1069     p++;
1070     p2++;
1071     }
1072     }
1073     x2 = x1;
1074     for (j=y1; j<=y2; j++) {
1075     p = &the_buffer[j * bytes_per_row];
1076     p2 = &the_buffer_copy[j * bytes_per_row];
1077     p += bytes_per_row;
1078     p2 += bytes_per_row;
1079     for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1080     p--;
1081     p2--;
1082     if (*p != *p2) {
1083     x2 = i << 3;
1084     break;
1085     }
1086     }
1087     }
1088     wide = x2 - x1;
1089    
1090     // Update copy of the_buffer
1091     if (high && wide) {
1092     for (j=y1; j<=y2; j++) {
1093     i = j * bytes_per_row + (x1 >> 3);
1094     memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
1095     }
1096     }
1097    
1098     } else {
1099     x1 = VideoMonitor.x;
1100     for (j=y1; j<=y2; j++) {
1101     p = &the_buffer[j * bytes_per_row];
1102     p2 = &the_buffer_copy[j * bytes_per_row];
1103     for (i=0; i<x1; i++) {
1104     if (memcmp(p, p2, bytes_per_pixel)) {
1105     x1 = i;
1106     break;
1107     }
1108     p += bytes_per_pixel;
1109     p2 += bytes_per_pixel;
1110     }
1111     }
1112     x2 = x1;
1113     for (j=y1; j<=y2; j++) {
1114     p = &the_buffer[j * bytes_per_row];
1115     p2 = &the_buffer_copy[j * bytes_per_row];
1116     p += bytes_per_row;
1117     p2 += bytes_per_row;
1118     for (i=VideoMonitor.x; i>x2; i--) {
1119     p -= bytes_per_pixel;
1120     p2 -= bytes_per_pixel;
1121     if (memcmp(p, p2, bytes_per_pixel)) {
1122     x2 = i;
1123     break;
1124     }
1125     }
1126     }
1127     wide = x2 - x1;
1128    
1129     // Update copy of the_buffer
1130     if (high && wide) {
1131     for (j=y1; j<=y2; j++) {
1132     i = j * bytes_per_row + x1 * bytes_per_pixel;
1133     memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
1134     }
1135     }
1136     }
1137     }
1138    
1139     // Refresh display
1140     if (high && wide) {
1141     if (have_shm)
1142     XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
1143     else
1144     XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1145     }
1146    
1147     // Has the Mac started? (cursor data is not valid otherwise)
1148     if (HasMacStarted()) {
1149    
1150     // Set new cursor image if it was changed
1151     if (memcmp(the_cursor, Mac2HostAddr(0x844), 64)) {
1152     memcpy(the_cursor, Mac2HostAddr(0x844), 64);
1153     memcpy(cursor_image->data, the_cursor, 32);
1154     memcpy(cursor_mask_image->data, the_cursor+32, 32);
1155     XFreeCursor(x_display, mac_cursor);
1156     XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
1157     XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
1158     mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, ReadMacInt8(0x885), ReadMacInt8(0x887));
1159     XDefineCursor(x_display, the_win, mac_cursor);
1160     }
1161     }
1162     }
1163    
1164    
1165     /*
1166     * Thread for screen refresh, input handling etc.
1167     */
1168    
1169     static void *redraw_func(void *arg)
1170     {
1171     int tick_counter = 0;
1172    
1173     while (!redraw_thread_cancel) {
1174    
1175     // Wait
1176     #ifdef HAVE_NANOSLEEP
1177     struct timespec req = {0, 16666667};
1178     nanosleep(&req, NULL);
1179     #else
1180     usleep(16667);
1181     #endif
1182    
1183     #if ENABLE_DGA
1184     // Quit DGA mode if requested
1185     if (quit_full_screen) {
1186     quit_full_screen = false;
1187     if (display_type == DISPLAY_DGA) {
1188     XF86DGADirectVideo(x_display, screen, 0);
1189     XUngrabPointer(x_display, CurrentTime);
1190     XUngrabKeyboard(x_display, CurrentTime);
1191     XUnmapWindow(x_display, the_win);
1192     XSync(x_display, false);
1193     }
1194     }
1195     #endif
1196    
1197     // Handle X events
1198     handle_events();
1199    
1200     // Handle palette changes
1201     pthread_mutex_lock(&palette_lock);
1202     if (palette_changed) {
1203     palette_changed = false;
1204     if (depth == 8) {
1205     XStoreColors(x_display, cmap[0], palette, 256);
1206     XStoreColors(x_display, cmap[1], palette, 256);
1207     #if ENABLE_DGA
1208     if (display_type == DISPLAY_DGA) {
1209     current_dga_cmap ^= 1;
1210     XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1211     }
1212     #endif
1213     }
1214     }
1215     pthread_mutex_unlock(&palette_lock);
1216    
1217     // In window mode, update display and mouse pointer
1218     if (display_type == DISPLAY_WINDOW) {
1219     tick_counter++;
1220     if (tick_counter >= frame_skip) {
1221     tick_counter = 0;
1222     update_display();
1223     }
1224     }
1225     }
1226     return NULL;
1227     }