ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.8
Committed: 1999-10-21T16:07:36Z (24 years, 8 months ago) by cebix
Branch: MAIN
Changes since 1.7: +3 -3 lines
Log Message:
- added fbdev DGA preferences to GTK prefs editor

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