ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/video_x.cpp
(Generate patch)

Comparing SheepShaver/src/Unix/video_x.cpp (file contents):
Revision 1.5 by gbeauche, 2003-11-20T16:24:57Z vs.
Revision 1.14 by gbeauche, 2004-04-11T10:46:32Z

# Line 1 | Line 1
1   /*
2   *  video_x.cpp - Video/graphics emulation, X11 specific stuff
3   *
4 < *  SheepShaver (C) 1997-2002 Marc Hellwig and Christian Bauer
4 > *  SheepShaver (C) 1997-2004 Marc Hellwig and 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
# Line 18 | Line 18
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  
21 + #include "sysdeps.h"
22 +
23   #include <X11/Xlib.h>
24   #include <X11/Xutil.h>
25   #include <X11/keysym.h>
26   #include <X11/extensions/XShm.h>
27   #include <sys/ipc.h>
28   #include <sys/shm.h>
29 + #include <errno.h>
30   #include <pthread.h>
31  
32 < #include "sysdeps.h"
32 > #include <algorithm>
33 >
34 > #ifdef ENABLE_XF86_DGA
35 > #include <X11/extensions/xf86dga.h>
36 > #endif
37 >
38 > #ifdef ENABLE_XF86_VIDMODE
39 > # include <X11/extensions/xf86vmode.h>
40 > #endif
41 >
42   #include "main.h"
43   #include "adb.h"
44   #include "prefs.h"
# Line 38 | Line 50
50   #define DEBUG 0
51   #include "debug.h"
52  
53 < #ifdef ENABLE_XF86_DGA
54 < #include <X11/extensions/xf86dga.h>
53 > #ifndef NO_STD_NAMESPACE
54 > using std::sort;
55   #endif
56  
45 #ifdef ENABLE_XF86_VIDMODE
46 #include <X11/extensions/xf86vmode.h>
47 #endif
57  
58 + // Constants
59 + const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
60  
61   // Global variables
62   static int32 frame_skip;
63 + static int16 mouse_wheel_mode;
64 + static int16 mouse_wheel_lines;
65   static bool redraw_thread_active = false;       // Flag: Redraw thread installed
66   static pthread_t redraw_thread;                         // Redraw thread
67  
# Line 74 | Line 87 | static bool emerg_quit = false;                                // Fl
87   static bool emul_suspended = false;                     // Flag: emulator suspended
88   static Window suspend_win;                                      // "Suspend" window
89   static void *fb_save = NULL;                            // Saved frame buffer for suspend
90 + static bool use_keycodes = false;                       // Flag: Use keycodes rather than keysyms
91 + static int keycode_table[256];                          // X keycode -> Mac keycode translation table
92  
93   // X11 variables
94   static int screen;                                                      // Screen number
95   static int xdepth;                                                      // Depth of X screen
96   static int depth;                                                       // Depth of Mac frame buffer
97   static Window rootwin, the_win;                         // Root window and our window
98 + static int num_depths = 0;                                      // Number of available X depths
99 + static int *avail_depths = NULL;                        // List of available X depths
100   static XVisualInfo visualInfo;
101   static Visual *vis;
102 + static int color_class;
103 + static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
104   static Colormap cmap[2];                                        // Two colormaps (DGA) for 8-bit mode
105 + static XColor x_palette[256];                           // Color palette to be used as CLUT and gamma table
106 +
107   static XColor black, white;
108   static unsigned long black_pixel, white_pixel;
109   static int eventmask;
110 < static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask;
111 < static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
110 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
111 > static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
112  
113   // Variables for window mode
114   static GC the_gc;
# Line 114 | Line 135 | static XF86VidModeModeInfo **x_video_mod
135   static int num_x_video_modes;
136   #endif
137  
138 + // Mutex to protect palette
139 + #ifdef HAVE_SPINLOCKS
140 + static spinlock_t x_palette_lock = SPIN_LOCK_UNLOCKED;
141 + #define LOCK_PALETTE spin_lock(&x_palette_lock)
142 + #define UNLOCK_PALETTE spin_unlock(&x_palette_lock)
143 + #elif defined(HAVE_PTHREADS)
144 + static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER;
145 + #define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock)
146 + #define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock)
147 + #else
148 + #define LOCK_PALETTE
149 + #define UNLOCK_PALETTE
150 + #endif
151 +
152  
153   // Prototypes
154   static void *redraw_func(void *arg);
# Line 126 | Line 161 | extern Display *x_display;
161   // From sys_unix.cpp
162   extern void SysMountFirstFloppy(void);
163  
164 + // From clip_unix.cpp
165 + extern void ClipboardSelectionClear(XSelectionClearEvent *);
166 + extern void ClipboardSelectionRequest(XSelectionRequestEvent *);
167 +
168  
169   // Video acceleration through SIGSEGV
170   #ifdef ENABLE_VOSF
# Line 134 | Line 173 | extern void SysMountFirstFloppy(void);
173  
174  
175   /*
176 + *  Utility functions
177 + */
178 +
179 + // Get current video mode
180 + static inline int get_current_mode(void)
181 + {
182 +        return VModes[cur_mode].viAppleMode;
183 + }
184 +
185 + // Find palette size for given color depth
186 + static int palette_size(int mode)
187 + {
188 +        switch (mode) {
189 +        case APPLE_1_BIT: return 2;
190 +        case APPLE_2_BIT: return 4;
191 +        case APPLE_4_BIT: return 16;
192 +        case APPLE_8_BIT: return 256;
193 +        case APPLE_16_BIT: return 32;
194 +        case APPLE_32_BIT: return 256;
195 +        default: return 0;
196 +        }
197 + }
198 +
199 + // Map video_mode depth ID to numerical depth value
200 + static inline int depth_of_video_mode(int mode)
201 + {
202 +        int depth = -1;
203 +        switch (mode) {
204 +        case APPLE_1_BIT:
205 +                depth = 1;
206 +                break;
207 +        case APPLE_2_BIT:
208 +                depth = 2;
209 +                break;
210 +        case APPLE_4_BIT:
211 +                depth = 4;
212 +                break;
213 +        case APPLE_8_BIT:
214 +                depth = 8;
215 +                break;
216 +        case APPLE_16_BIT:
217 +                depth = 16;
218 +                break;
219 +        case APPLE_32_BIT:
220 +                depth = 32;
221 +                break;
222 +        default:
223 +                abort();
224 +        }
225 +        return depth;
226 + }
227 +
228 + // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
229 + static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
230 + {
231 +        return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift);
232 + }
233 +
234 +
235 + // Do we have a visual for handling the specified Mac depth? If so, set the
236 + // global variables "xdepth", "visualInfo", "vis" and "color_class".
237 + static bool find_visual_for_depth(int depth)
238 + {
239 +        D(bug("have_visual_for_depth(%d)\n", depth_of_video_mode(depth)));
240 +
241 +        // 1-bit works always and uses default visual
242 +        if (depth == APPLE_1_BIT) {
243 +                vis = DefaultVisual(x_display, screen);
244 +                visualInfo.visualid = XVisualIDFromVisual(vis);
245 +                int num = 0;
246 +                XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num);
247 +                visualInfo = vi[0];
248 +                XFree(vi);
249 +                xdepth = visualInfo.depth;
250 +                color_class = visualInfo.c_class;
251 +                D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth));
252 +                return true;
253 +        }
254 +
255 +        // Calculate minimum and maximum supported X depth
256 +        int min_depth = 1, max_depth = 32;
257 +        switch (depth) {
258 + #ifdef ENABLE_VOSF
259 +                case APPLE_2_BIT:
260 +                case APPLE_4_BIT:       // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
261 +                case APPLE_8_BIT:
262 +                        min_depth = 8;
263 +                        max_depth = 32;
264 +                        break;
265 + #else
266 +                case APPLE_2_BIT:
267 +                case APPLE_4_BIT:       // 2/4-bit requires VOSF blitters
268 +                        return false;
269 +                case APPLE_8_BIT:       // 8-bit without VOSF requires an 8-bit visual
270 +                        min_depth = 8;
271 +                        max_depth = 8;
272 +                        break;
273 + #endif
274 +                case APPLE_16_BIT:      // 16-bit requires a 15/16-bit visual
275 +                        min_depth = 15;
276 +                        max_depth = 16;
277 +                        break;
278 +                case APPLE_32_BIT:      // 32-bit requires a 24/32-bit visual
279 +                        min_depth = 24;
280 +                        max_depth = 32;
281 +                        break;
282 +        }
283 +        D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth));
284 +
285 +        // Try to find a visual for one of the color depths
286 +        bool visual_found = false;
287 +        for (int i=0; i<num_depths && !visual_found; i++) {
288 +
289 +                xdepth = avail_depths[i];
290 +                D(bug(" trying to find visual for depth %d\n", xdepth));
291 +                if (xdepth < min_depth || xdepth > max_depth)
292 +                        continue;
293 +
294 +                // Determine best color class for this depth
295 +                switch (xdepth) {
296 +                        case 1: // Try StaticGray or StaticColor
297 +                                if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo)
298 +                                 || XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo))
299 +                                        visual_found = true;
300 +                                break;
301 +                        case 8: // Need PseudoColor
302 +                                if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo))
303 +                                        visual_found = true;
304 +                                break;
305 +                        case 15:
306 +                        case 16:
307 +                        case 24:
308 +                        case 32: // Try DirectColor first, as this will allow gamma correction
309 +                                if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo)
310 +                                 || XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo))
311 +                                        visual_found = true;
312 +                                break;
313 +                        default:
314 +                                D(bug("  not a supported depth\n"));
315 +                                break;
316 +                }
317 +        }
318 +        if (!visual_found)
319 +                return false;
320 +
321 +        // Visual was found
322 +        vis = visualInfo.visual;
323 +        color_class = visualInfo.c_class;
324 +        D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth));
325 + #if DEBUG
326 +        switch (color_class) {
327 +                case StaticGray: D(bug("StaticGray\n")); break;
328 +                case GrayScale: D(bug("GrayScale\n")); break;
329 +                case StaticColor: D(bug("StaticColor\n")); break;
330 +                case PseudoColor: D(bug("PseudoColor\n")); break;
331 +                case TrueColor: D(bug("TrueColor\n")); break;
332 +                case DirectColor: D(bug("DirectColor\n")); break;
333 +        }
334 + #endif
335 +        return true;
336 + }
337 +
338 +
339 + /*
340   *  Open display (window or fullscreen)
341   */
342  
343 + // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
344 + static Atom WM_DELETE_WINDOW = (Atom)0;
345 + static void set_window_delete_protocol(Window w)
346 + {
347 +        WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
348 +        XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
349 + }
350 +
351 + // Wait until window is mapped/unmapped
352 + void wait_mapped(Window w)
353 + {
354 +        XEvent e;
355 +        do {
356 +                XMaskEvent(x_display, StructureNotifyMask, &e);
357 +        } while ((e.type != MapNotify) || (e.xmap.event != w));
358 + }
359 +
360 + void wait_unmapped(Window w)
361 + {
362 +        XEvent e;
363 +        do {
364 +                XMaskEvent(x_display, StructureNotifyMask, &e);
365 +        } while ((e.type != UnmapNotify) || (e.xmap.event != w));
366 + }
367 +
368   // Trap SHM errors
369   static bool shm_error = false;
370   static int (*old_error_handler)(Display *, XErrorEvent *);
# Line 159 | Line 387 | static bool open_window(int width, int h
387          // Set absolute mouse mode
388          ADBSetRelMouseMode(false);
389  
162        // Read frame skip prefs
163        frame_skip = PrefsFindInt32("frameskip");
164        if (frame_skip == 0)
165                frame_skip = 1;
166
390          // Create window
391          XSetWindowAttributes wattr;
392          wattr.event_mask = eventmask = win_eventmask;
393 <        wattr.background_pixel = black_pixel;
394 <        wattr.border_pixel = black_pixel;
393 >        wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
394 >        wattr.border_pixel = 0;
395          wattr.backing_store = NotUseful;
396 <
174 <        XSync(x_display, false);
396 >        wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
397          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
398 <                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore, &wattr);
399 <        XSync(x_display, false);
398 >                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr);
399 >
400 >        // Set window name
401          XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
179        XMapRaised(x_display, the_win);
180        XSync(x_display, false);
402  
403 <        // Set colormap
404 <        if (depth == 8) {
184 <                XSetWindowColormap(x_display, the_win, cmap[0]);
185 <                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
186 <        }
403 >        // Set delete protocol property
404 >        set_window_delete_protocol(the_win);
405  
406          // Make window unresizable
407          XSizeHints *hints;
# Line 197 | Line 415 | static bool open_window(int width, int h
415                  XFree((char *)hints);
416          }
417  
418 +        // Show window
419 +        XMapWindow(x_display, the_win);
420 +        wait_mapped(the_win);
421 +
422          // 1-bit mode is big-endian; if the X server is little-endian, we can't
423          // use SHM because that doesn't allow changing the image byte order
424          bool need_msb_image = (depth == 1 && XImageByteOrder(x_display) == LSBFirst);
# Line 207 | Line 429 | static bool open_window(int width, int h
429  
430                  // Create SHM image ("height + 2" for safety)
431                  img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
432 <                shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
432 >                shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
433 >                D(bug(" shm image created\n"));
434                  the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
435                  shminfo.shmaddr = img->data = (char *)the_buffer_copy;
436                  shminfo.readOnly = False;
# Line 226 | Line 449 | static bool open_window(int width, int h
449                          have_shm = true;
450                          shmctl(shminfo.shmid, IPC_RMID, 0);
451                  }
452 +                D(bug(" shm image attached\n"));
453          }
454  
455          // Create normal X image if SHM doesn't work ("height + 2" for safety)
456          if (!have_shm) {
457 <                int bytes_per_row = aligned_width;
234 <                switch (depth) {
235 <                        case 1:
236 <                                bytes_per_row /= 8;
237 <                                break;
238 <                        case 15:
239 <                        case 16:
240 <                                bytes_per_row *= 2;
241 <                                break;
242 <                        case 24:
243 <                        case 32:
244 <                                bytes_per_row *= 4;
245 <                                break;
246 <                }
457 >                int bytes_per_row = depth == 1 ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth));
458                  the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
459                  img = XCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
460 +                D(bug(" X image created\n"));
461          }
462  
463          // 1-Bit mode is big-endian
464 <    if (depth == 1) {
464 >    if (need_msb_image) {
465          img->byte_order = MSBFirst;
466          img->bitmap_bit_order = MSBFirst;
467      }
# Line 271 | Line 483 | static bool open_window(int width, int h
483  
484          // Create GC
485          the_gc = XCreateGC(x_display, the_win, 0, 0);
486 <        XSetForeground(x_display, the_gc, black_pixel);
486 >        XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
487  
488          // Create cursor
489          cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
# Line 299 | Line 511 | static bool open_window(int width, int h
511   #endif
512  
513          // Set bytes per row
302        VModes[cur_mode].viRowBytes = img->bytes_per_line;
514          XSync(x_display, false);
515          return true;
516   }
# Line 338 | Line 549 | static bool open_dga(int width, int heig
549                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
550  
551          // Set bytes per row
552 <        int bytes_per_row = (dga_fb_width + 7) & ~7;
342 <        switch (depth) {
343 <                case 15:
344 <                case 16:
345 <                        bytes_per_row *= 2;
346 <                        break;
347 <                case 24:
348 <                case 32:
349 <                        bytes_per_row *= 4;
350 <                        break;
351 <        }
552 >        int bytes_per_row = TrivialBytesPerRow((dga_fb_width + 7) & ~7, DepthModeForPixelDepth(depth));
553  
554   #if ENABLE_VOSF
555          bool native_byte_order;
# Line 387 | Line 588 | static bool open_dga(int width, int heig
588  
589   static bool open_display(void)
590   {
591 <        display_type = VModes[cur_mode].viType;
592 <        switch (VModes[cur_mode].viAppleMode) {
593 <                case APPLE_1_BIT:
594 <                        depth = 1;
595 <                        break;
596 <                case APPLE_2_BIT:
597 <                        depth = 2;
397 <                        break;
398 <                case APPLE_4_BIT:
399 <                        depth = 4;
400 <                        break;
401 <                case APPLE_8_BIT:
402 <                        depth = 8;
403 <                        break;
404 <                case APPLE_16_BIT:
405 <                        depth = xdepth == 15 ? 15 : 16;
406 <                        break;
407 <                case APPLE_32_BIT:
408 <                        depth = 32;
409 <                        break;
591 >        D(bug("open_display()\n"));
592 >        const VideoInfo &mode = VModes[cur_mode];
593 >
594 >        // Find best available X visual
595 >        if (!find_visual_for_depth(mode.viAppleMode)) {
596 >                ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
597 >                return false;
598          }
599  
600 +        // Create color maps
601 +        if (color_class == PseudoColor || color_class == DirectColor) {
602 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
603 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
604 +        } else {
605 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
606 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
607 +        }
608 +
609 +        // Find pixel format of direct modes
610 +        if (color_class == DirectColor || color_class == TrueColor) {
611 +                rshift = gshift = bshift = 0;
612 +                rloss = gloss = bloss = 8;
613 +                uint32 mask;
614 +                for (mask=vis->red_mask; !(mask&1); mask>>=1)
615 +                        ++rshift;
616 +                for (; mask&1; mask>>=1)
617 +                        --rloss;
618 +                for (mask=vis->green_mask; !(mask&1); mask>>=1)
619 +                        ++gshift;
620 +                for (; mask&1; mask>>=1)
621 +                        --gloss;
622 +                for (mask=vis->blue_mask; !(mask&1); mask>>=1)
623 +                        ++bshift;
624 +                for (; mask&1; mask>>=1)
625 +                        --bloss;
626 +        }
627 +
628 +        // Preset palette pixel values for CLUT or gamma table
629 +        if (color_class == DirectColor) {
630 +                int num = vis->map_entries;
631 +                for (int i=0; i<num; i++) {
632 +                        int c = (i * 256) / num;
633 +                        x_palette[i].pixel = map_rgb(c, c, c);
634 +                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
635 +                }
636 +        } else if (color_class == PseudoColor) {
637 +                for (int i=0; i<256; i++) {
638 +                        x_palette[i].pixel = i;
639 +                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
640 +                }
641 +        }
642 +
643 +        // Load gray ramp to color map
644 +        int num = (color_class == DirectColor ? vis->map_entries : 256);
645 +        for (int i=0; i<num; i++) {
646 +                int c = (i * 256) / num;
647 +                x_palette[i].red = c * 0x0101;
648 +                x_palette[i].green = c * 0x0101;
649 +                x_palette[i].blue = c * 0x0101;
650 +        }
651 +        if (color_class == PseudoColor || color_class == DirectColor) {
652 +                XStoreColors(x_display, cmap[0], x_palette, num);
653 +                XStoreColors(x_display, cmap[1], x_palette, num);
654 +        }
655 +
656 + #ifdef ENABLE_VOSF
657 +        // Load gray ramp to 8->16/32 expand map
658 +        if (!IsDirectMode(get_current_mode()) && xdepth > 8)
659 +                for (int i=0; i<256; i++)
660 +                        ExpandMap[i] = map_rgb(i, i, i);
661 + #endif
662 +
663 +        // Create display of requested type
664 +        display_type = mode.viType;
665 +        depth = depth_of_video_mode(mode.viAppleMode);
666 +
667          bool display_open = false;
668          if (display_type == DIS_SCREEN)
669                  display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
# Line 457 | Line 712 | static void close_window(void)
712                  XFreeGC(x_display, the_gc);
713  
714          // Close window
715 <        XDestroyWindow(x_display, the_win);
715 >        if (the_win) {
716 >                XUnmapWindow(x_display, the_win);
717 >                wait_unmapped(the_win);
718 >                XDestroyWindow(x_display, the_win);
719 >        }
720 >
721 >        XFlush(x_display);
722 >        XSync(x_display, false);
723   }
724  
725   // Close DGA mode
# Line 493 | Line 755 | static void close_display(void)
755          else if (display_type == DIS_WINDOW)
756                  close_window();
757  
758 +        // Free colormaps
759 +        if (cmap[0]) {
760 +                XFreeColormap(x_display, cmap[0]);
761 +                cmap[0] = 0;
762 +        }
763 +        if (cmap[1]) {
764 +                XFreeColormap(x_display, cmap[1]);
765 +                cmap[1] = 0;
766 +        }
767 +
768   #ifdef ENABLE_VOSF
769          if (use_vosf) {
770                  // Deinitialize VOSF
# Line 534 | Line 806 | static void close_display(void)
806   *  Initialization
807   */
808  
809 < static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, long apple_mode, long apple_id, int type)
809 > // Init keycode translation table
810 > static void keycode_init(void)
811 > {
812 >        bool use_kc = PrefsFindBool("keycodes");
813 >        if (use_kc) {
814 >
815 >                // Get keycode file path from preferences
816 >                const char *kc_path = PrefsFindString("keycodefile");
817 >
818 >                // Open keycode table
819 >                FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
820 >                if (f == NULL) {
821 >                        char str[256];
822 >                        sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
823 >                        WarningAlert(str);
824 >                        return;
825 >                }
826 >
827 >                // Default translation table
828 >                for (int i=0; i<256; i++)
829 >                        keycode_table[i] = -1;
830 >
831 >                // Search for server vendor string, then read keycodes
832 >                const char *vendor = ServerVendor(x_display);
833 >                bool vendor_found = false;
834 >                char line[256];
835 >                while (fgets(line, 255, f)) {
836 >                        // Read line
837 >                        int len = strlen(line);
838 >                        if (len == 0)
839 >                                continue;
840 >                        line[len-1] = 0;
841 >
842 >                        // Comments begin with "#" or ";"
843 >                        if (line[0] == '#' || line[0] == ';' || line[0] == 0)
844 >                                continue;
845 >
846 >                        if (vendor_found) {
847 >                                // Read keycode
848 >                                int x_code, mac_code;
849 >                                if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
850 >                                        keycode_table[x_code & 0xff] = mac_code;
851 >                                else
852 >                                        break;
853 >                        } else {
854 >                                // Search for vendor string
855 >                                if (strstr(vendor, line) == vendor)
856 >                                        vendor_found = true;
857 >                        }
858 >                }
859 >
860 >                // Keycode file completely read
861 >                fclose(f);
862 >                use_keycodes = vendor_found;
863 >
864 >                // Vendor not found? Then display warning
865 >                if (!vendor_found) {
866 >                        char str[256];
867 >                        sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME);
868 >                        WarningAlert(str);
869 >                        return;
870 >                }
871 >        }
872 > }
873 >
874 > // Add mode to list of supported modes
875 > static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
876   {
877          if (allow & test) {
878                  p->viType = type;
# Line 566 | Line 904 | static void add_mode(VideoInfo *&p, uint
904                                  p->viYsize = 1200;
905                                  break;
906                  }
907 <                switch (apple_mode) {
570 <                        case APPLE_8_BIT:
571 <                                p->viRowBytes = p->viXsize;
572 <                                break;
573 <                        case APPLE_16_BIT:
574 <                                p->viRowBytes = p->viXsize * 2;
575 <                                break;
576 <                        case APPLE_32_BIT:
577 <                                p->viRowBytes = p->viXsize * 4;
578 <                                break;
579 <                }
907 >                p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
908                  p->viAppleMode = apple_mode;
909                  p->viAppleID = apple_id;
910                  p++;
911          }
912   }
913  
914 + // Add standard list of windowed modes for given color depth
915 + static void add_window_modes(VideoInfo *&p, int window_modes, int mode)
916 + {
917 +        add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);
918 +        add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);
919 + }
920 +
921   static bool has_mode(int x, int y)
922   {
923   #ifdef ENABLE_XF86_VIDMODE
# Line 607 | Line 942 | bool VideoInit(void)
942          local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
943                   || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
944      
945 +        // Init keycode translation
946 +        keycode_init();
947 +
948 +        // Read frame skip prefs
949 +        frame_skip = PrefsFindInt32("frameskip");
950 +        if (frame_skip == 0)
951 +                frame_skip = 1;
952 +
953 +        // Read mouse wheel prefs
954 +        mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
955 +        mouse_wheel_lines = PrefsFindInt32("mousewheellines");
956 +
957          // Init variables
958          private_data = NULL;
612        cur_mode = 0;   // Window 640x480
959          video_activated = true;
960  
961          // Find screen and root window
962          screen = XDefaultScreen(x_display);
963          rootwin = XRootWindow(x_display, screen);
964  
965 +        // Get sorted list of available depths
966 +        avail_depths = XListDepths(x_display, screen, &num_depths);
967 +        if (avail_depths == NULL) {
968 +                ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
969 +                return false;
970 +        }
971 +        sort(avail_depths, avail_depths + num_depths);
972 +        
973          // Get screen depth
974          xdepth = DefaultDepth(x_display, screen);
975  
# Line 646 | Line 1000 | bool VideoInit(void)
1000          black_pixel = BlackPixel(x_display, screen);
1001          white_pixel = WhitePixel(x_display, screen);
1002  
649        // Get appropriate visual
650        int color_class;
651        switch (xdepth) {
652 #if 0
653                case 1:
654                        color_class = StaticGray;
655                        break;
656 #endif
657                case 8:
658                        color_class = PseudoColor;
659                        break;
660                case 15:
661                case 16:
662                case 24:
663                case 32:
664                        color_class = TrueColor;
665                        break;
666                default:
667                        ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
668                        return false;
669        }
670        if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
671                ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
672                return false;
673        }
674        if (visualInfo.depth != xdepth) {
675                ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
676                return false;
677        }
678        vis = visualInfo.visual;
679
1003          // Mac screen depth follows X depth (for now)
1004 <        depth = xdepth;
1005 <
1006 <        // Create color maps for 8 bit mode
1007 <        if (depth == 8) {
1008 <                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1009 <                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1010 <                XInstallColormap(x_display, cmap[0]);
1011 <                XInstallColormap(x_display, cmap[1]);
1004 >        int default_mode = APPLE_8_BIT;
1005 >        switch (DefaultDepth(x_display, screen)) {
1006 >        case 1:
1007 >                default_mode = APPLE_1_BIT;
1008 >                break;
1009 >        case 8:
1010 >                default_mode = APPLE_8_BIT;
1011 >                break;
1012 >        case 15: case 16:
1013 >                default_mode = APPLE_16_BIT;
1014 >                break;
1015 >        case 24: case 32:
1016 >                default_mode = APPLE_32_BIT;
1017 >                break;
1018          }
1019  
1020          // Construct video mode table
692        int mode = APPLE_8_BIT;
693        int bpr_mult = 8;
694        switch (depth) {
695                case 1:
696                        mode = APPLE_1_BIT;
697                        bpr_mult = 1;
698                        break;
699                case 8:
700                        mode = APPLE_8_BIT;
701                        bpr_mult = 8;
702                        break;
703                case 15:
704                case 16:
705                        mode = APPLE_16_BIT;
706                        bpr_mult = 16;
707                        break;
708                case 24:
709                case 32:
710                        mode = APPLE_32_BIT;
711                        bpr_mult = 32;
712                        break;
713        }
714
1021          uint32 window_modes = PrefsFindInt32("windowmodes");
1022          uint32 screen_modes = PrefsFindInt32("screenmodes");
1023          if (!has_dga)
# Line 720 | Line 1026 | bool VideoInit(void)
1026                  window_modes |= 3;      // Allow at least 640x480 and 800x600 window modes
1027  
1028          VideoInfo *p = VModes;
1029 <        add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);
1030 <        add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);
1029 >        for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1030 >                if (find_visual_for_depth(d))
1031 >                        add_window_modes(p, window_modes, d);
1032 >
1033          if (has_vidmode) {
1034                  if (has_mode(640, 480))
1035 <                        add_mode(p, screen_modes, 1, mode, APPLE_640x480, DIS_SCREEN);
1035 >                        add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1036                  if (has_mode(800, 600))
1037 <                        add_mode(p, screen_modes, 2, mode, APPLE_800x600, DIS_SCREEN);
1037 >                        add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN);
1038                  if (has_mode(1024, 768))
1039 <                        add_mode(p, screen_modes, 4, mode, APPLE_1024x768, DIS_SCREEN);
1039 >                        add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN);
1040                  if (has_mode(1152, 900))
1041 <                        add_mode(p, screen_modes, 8, mode, APPLE_1152x900, DIS_SCREEN);
1041 >                        add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN);
1042                  if (has_mode(1280, 1024))
1043 <                        add_mode(p, screen_modes, 16, mode, APPLE_1280x1024, DIS_SCREEN);
1043 >                        add_mode(p, screen_modes, 16, default_mode, APPLE_1280x1024, DIS_SCREEN);
1044                  if (has_mode(1600, 1200))
1045 <                        add_mode(p, screen_modes, 32, mode, APPLE_1600x1200, DIS_SCREEN);
1045 >                        add_mode(p, screen_modes, 32, default_mode, APPLE_1600x1200, DIS_SCREEN);
1046          } else if (screen_modes) {
1047                  int xsize = DisplayWidth(x_display, screen);
1048                  int ysize = DisplayHeight(x_display, screen);
# Line 755 | Line 1063 | bool VideoInit(void)
1063                  p->viRowBytes = 0;
1064                  p->viXsize = xsize;
1065                  p->viYsize = ysize;
1066 <                p->viAppleMode = mode;
1066 >                p->viAppleMode = default_mode;
1067                  p->viAppleID = apple_id;
1068                  p++;
1069          }
# Line 765 | Line 1073 | bool VideoInit(void)
1073          p->viAppleMode = 0;
1074          p->viAppleID = 0;
1075  
1076 +        // Find default mode (window 640x480)
1077 +        cur_mode = -1;
1078 +        for (p = VModes; p->viType != DIS_INVALID; p++) {
1079 +                if (p->viType == DIS_WINDOW
1080 +                        && p->viAppleID == APPLE_W_640x480
1081 +                        && p->viAppleMode == default_mode) {
1082 +                        cur_mode = p - VModes;
1083 +                        break;
1084 +                }
1085 +        }
1086 +        assert(cur_mode != -1);
1087 +
1088 + #if DEBUG
1089 +        D(bug("Available video modes:\n"));
1090 +        for (p = VModes; p->viType != DIS_INVALID; p++) {
1091 +                int bits = depth_of_video_mode(p->viAppleMode);
1092 +                D(bug(" %dx%d (ID %02x), %d colors\n", p->viXsize, p->viYsize, p->viAppleID, 1 << bits));
1093 +        }
1094 + #endif
1095 +
1096   #ifdef ENABLE_XF86_DGA
1097          if (has_dga && screen_modes) {
1098                  int v_bank, v_size;
# Line 816 | Line 1144 | void VideoExit(void)
1144                  close_display();
1145                  XFlush(x_display);
1146                  XSync(x_display, false);
819                if (depth == 8) {
820                        XFreeColormap(x_display, cmap[0]);
821                        XFreeColormap(x_display, cmap[1]);
822                }
1147          }
1148   }
1149  
# Line 883 | Line 1207 | static void resume_emul(void)
1207          // Reopen full screen display
1208          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1209          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1210 + #ifdef ENABLE_XF86_DGA
1211          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1212          XF86DGASetViewPort(x_display, screen, 0, 0);
1213 + #endif
1214          XSync(x_display, false);
1215  
1216          // the_buffer already contains the data to restore. i.e. since a temporary
# Line 1078 | Line 1404 | static int kc_decode(KeySym ks)
1404          return -1;
1405   }
1406  
1407 < static int event2keycode(XKeyEvent *ev)
1407 > static int event2keycode(XKeyEvent &ev)
1408   {
1409          KeySym ks;
1410          int as;
1411          int i = 0;
1412  
1413          do {
1414 <                ks = XLookupKeysym(ev, i++);
1414 >                ks = XLookupKeysym(&ev, i++);
1415                  as = kc_decode(ks);
1416                  if (as != -1)
1417                          return as;
# Line 1100 | Line 1426 | static void handle_events(void)
1426          for (;;) {
1427                  XEvent event;
1428  
1429 <                if (!XCheckMaskEvent(x_display, eventmask, &event))
1429 >                XDisplayLock();
1430 >                if (!XCheckMaskEvent(x_display, eventmask, &event)) {
1431 >                        // Handle clipboard events
1432 >                        if (XCheckTypedEvent(x_display, SelectionRequest, &event))
1433 >                                ClipboardSelectionRequest(&event.xselectionrequest);
1434 >                        else if (XCheckTypedEvent(x_display, SelectionClear, &event))
1435 >                                ClipboardSelectionClear(&event.xselectionclear);
1436 >
1437 >                        // Window "close" widget clicked
1438 >                        else if (XCheckTypedEvent(x_display, ClientMessage, &event)) {
1439 >                                if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
1440 >                                        ADBKeyDown(0x7f);       // Power key
1441 >                                        ADBKeyUp(0x7f);
1442 >                                }
1443 >                        }
1444 >
1445 >                        XDisplayUnlock();
1446                          break;
1447 +                }
1448 +                XDisplayUnlock();
1449  
1450                  switch (event.type) {
1451                          // Mouse button
# Line 1109 | Line 1453 | static void handle_events(void)
1453                                  unsigned int button = ((XButtonEvent *)&event)->button;
1454                                  if (button < 4)
1455                                          ADBMouseDown(button - 1);
1456 +                                else if (button < 6) {  // Wheel mouse
1457 +                                        if (mouse_wheel_mode == 0) {
1458 +                                                int key = (button == 5) ? 0x79 : 0x74;  // Page up/down
1459 +                                                ADBKeyDown(key);
1460 +                                                ADBKeyUp(key);
1461 +                                        } else {
1462 +                                                int key = (button == 5) ? 0x3d : 0x3e;  // Cursor up/down
1463 +                                                for(int i=0; i<mouse_wheel_lines; i++) {
1464 +                                                        ADBKeyDown(key);
1465 +                                                        ADBKeyUp(key);
1466 +                                                }
1467 +                                        }
1468 +                                }
1469                                  break;
1470                          }
1471                          case ButtonRelease: {
# Line 1128 | Line 1485 | static void handle_events(void)
1485  
1486                          // Keyboard
1487                          case KeyPress: {
1488 <                                int code;
1489 <                                if ((code = event2keycode((XKeyEvent *)&event)) != -1) {
1488 >                                int code = event2keycode(event.xkey);
1489 >                                if (use_keycodes && code != -1)
1490 >                                        code = keycode_table[event.xkey.keycode & 0xff];
1491 >                                if (code != -1) {
1492                                          if (!emul_suspended) {
1493                                                  ADBKeyDown(code);
1494                                                  if (code == 0x36)
# Line 1142 | Line 1501 | static void handle_events(void)
1501                                  break;
1502                          }
1503                          case KeyRelease: {
1504 <                                int code;
1505 <                                if ((code = event2keycode((XKeyEvent *)&event)) != -1) {
1504 >                                int code = event2keycode(event.xkey);
1505 >                                if (use_keycodes && code != 1)
1506 >                                        code = keycode_table[event.xkey.keycode & 0xff];
1507 >                                if (code != -1) {
1508                                          ADBKeyUp(code);
1509                                          if (code == 0x36)
1510                                                  ctrl_down = false;
# Line 1384 | Line 1745 | int16 video_mode_change(VidLocals *csSav
1745  
1746   void video_set_palette(void)
1747   {
1748 +        LOCK_PALETTE;
1749 +
1750 +        // Convert colors to XColor array
1751 +        int mode = get_current_mode();
1752 +        int num_in = palette_size(mode);
1753 +        int num_out = 256;
1754 +        bool stretch = false;
1755 +        if (IsDirectMode(mode)) {
1756 +                // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1757 +                num_out = vis->map_entries;
1758 +                stretch = true;
1759 +        }
1760 +        XColor *p = x_palette;
1761 +        for (int i=0; i<num_out; i++) {
1762 +                int c = (stretch ? (i * num_in) / num_out : i);
1763 +                p->red = mac_pal[c].red * 0x0101;
1764 +                p->green = mac_pal[c].green * 0x0101;
1765 +                p->blue = mac_pal[c].blue * 0x0101;
1766 +                p++;
1767 +        }
1768 +
1769 + #ifdef ENABLE_VOSF
1770 +        // Recalculate pixel color expansion map
1771 +        if (!IsDirectMode(mode) && xdepth > 8) {
1772 +                for (int i=0; i<256; i++) {
1773 +                        int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1774 +                        ExpandMap[i] = map_rgb(mac_pal[c].red, mac_pal[c].green, mac_pal[c].blue);
1775 +                }
1776 +
1777 +                // We have to redraw everything because the interpretation of pixel values changed
1778 +                LOCK_VOSF;
1779 +                PFLAG_SET_ALL;
1780 +                UNLOCK_VOSF;
1781 +                memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1782 +        }
1783 + #endif
1784 +
1785 +        // Tell redraw thread to change palette
1786          palette_changed = true;
1787 +
1788 +        UNLOCK_PALETTE;
1789   }
1790  
1791  
# Line 1511 | Line 1912 | static void update_display(void)
1912  
1913          // Refresh display
1914          if (high && wide) {
1915 +                XDisplayLock();
1916                  if (have_shm)
1917                          XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
1918                  else
1919                          XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1920 +                XDisplayUnlock();
1921 +        }
1922 + }
1923 +
1924 + const int VIDEO_REFRESH_HZ = 60;
1925 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
1926 +
1927 + static void handle_palette_changes(void)
1928 + {
1929 +        LOCK_PALETTE;
1930 +
1931 +        if (palette_changed && !emul_suspended) {
1932 +                palette_changed = false;
1933 +
1934 +                int mode = get_current_mode();
1935 +                if (color_class == PseudoColor || color_class == DirectColor) {
1936 +                        int num = vis->map_entries;
1937 +                        bool set_clut = true;
1938 +                        if (!IsDirectMode(mode) && color_class == DirectColor) {
1939 +                                if (display_type == DIS_WINDOW)
1940 +                                        set_clut = false; // Indexed mode on true color screen, don't set CLUT
1941 +                        }
1942 +
1943 +                        if (set_clut) {
1944 +                                XDisplayLock();
1945 +                                XStoreColors(x_display, cmap[0], x_palette, num);
1946 +                                XStoreColors(x_display, cmap[1], x_palette, num);
1947 +                                XSync(x_display, false);
1948 +                                XDisplayUnlock();
1949 +                        }
1950 +                }
1951 +
1952 + #ifdef ENABLE_XF86_DGA
1953 +                if (display_type == DIS_SCREEN) {
1954 +                        current_dga_cmap ^= 1;
1955 +                        if (!IsDirectMode(mode) && cmap[current_dga_cmap])
1956 +                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1957 +                }
1958 + #endif
1959          }
1960 +
1961 +        UNLOCK_PALETTE;
1962   }
1963  
1964   static void *redraw_func(void *arg)
1965   {
1966 <        int tick_counter = 0;
1524 <        struct timespec req = {0, 16666667};
1966 >        int fd = ConnectionNumber(x_display);
1967  
1968 <        for (;;) {
1968 >        uint64 start = GetTicks_usec();
1969 >        int64 ticks = 0;
1970 >        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
1971  
1972 <                // Wait
1529 <                nanosleep(&req, NULL);
1972 >        for (;;) {
1973  
1974                  // Pause if requested (during video mode switches)
1975                  while (thread_stop_req)
1976                          thread_stop_ack = true;
1977  
1978 <                // Handle X11 events
1979 <                handle_events();
1978 >                int64 delay = next - GetTicks_usec();
1979 >                if (delay < -VIDEO_REFRESH_DELAY) {
1980 >
1981 >                        // We are lagging far behind, so we reset the delay mechanism
1982 >                        next = GetTicks_usec();
1983  
1984 <                // Quit DGA mode if requested
1985 <                if (quit_full_screen) {
1986 <                        quit_full_screen = false;
1987 <                        if (display_type == DIS_SCREEN) {
1984 >                } else if (delay <= 0) {
1985 >
1986 >                        // Delay expired, refresh display
1987 >                        next += VIDEO_REFRESH_DELAY;
1988 >                        ticks++;
1989 >
1990 >                        // Handle X11 events
1991 >                        handle_events();
1992 >
1993 >                        // Quit DGA mode if requested
1994 >                        if (quit_full_screen) {
1995 >                                quit_full_screen = false;
1996 >                                if (display_type == DIS_SCREEN) {
1997 >                                        XDisplayLock();
1998   #ifdef ENABLE_XF86_DGA
1999 <                                XF86DGADirectVideo(x_display, screen, 0);
2000 <                                XUngrabPointer(x_display, CurrentTime);
2001 <                                XUngrabKeyboard(x_display, CurrentTime);
2002 < #endif
2003 <                                XSync(x_display, false);
2004 <                                quit_full_screen_ack = true;
2005 <                                return NULL;
1999 >                                        XF86DGADirectVideo(x_display, screen, 0);
2000 >                                        XUngrabPointer(x_display, CurrentTime);
2001 >                                        XUngrabKeyboard(x_display, CurrentTime);
2002 > #endif
2003 >                                        XSync(x_display, false);
2004 >                                        XDisplayUnlock();
2005 >                                        quit_full_screen_ack = true;
2006 >                                        return NULL;
2007 >                                }
2008                          }
1551                }
2009  
2010 <                // Refresh display and set cursor image in window mode
2011 <                if (display_type == DIS_WINDOW) {
2012 <                        tick_counter++;
2013 <                        if (tick_counter >= frame_skip) {
2014 <                                tick_counter = 0;
2010 >                        // Refresh display and set cursor image in window mode
2011 >                        static int tick_counter = 0;
2012 >                        if (display_type == DIS_WINDOW) {
2013 >                                tick_counter++;
2014 >                                if (tick_counter >= frame_skip) {
2015 >                                        tick_counter = 0;
2016  
2017 <                                // Update display
2017 >                                        // Update display
2018   #ifdef ENABLE_VOSF
2019 <                                if (use_vosf) {
2020 <                                        if (mainBuffer.dirty) {
2021 <                                                LOCK_VOSF;
2022 <                                                update_display_window_vosf();
2023 <                                                UNLOCK_VOSF;
2024 <                                                XSync(x_display, false); // Let the server catch up
2019 >                                        if (use_vosf) {
2020 >                                                XDisplayLock();
2021 >                                                if (mainBuffer.dirty) {
2022 >                                                        LOCK_VOSF;
2023 >                                                        update_display_window_vosf();
2024 >                                                        UNLOCK_VOSF;
2025 >                                                        XSync(x_display, false); // Let the server catch up
2026 >                                                }
2027 >                                                XDisplayUnlock();
2028                                          }
2029 <                                }
1569 <                                else
2029 >                                        else
2030   #endif
2031 <                                        update_display();
2031 >                                                update_display();
2032  
2033 <                                // Set new cursor image if it was changed
2034 <                                if (cursor_changed) {
2035 <                                        cursor_changed = false;
2036 <                                        memcpy(cursor_image->data, MacCursor + 4, 32);
2037 <                                        memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2038 <                                        XFreeCursor(x_display, mac_cursor);
2039 <                                        XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
2040 <                                        XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
2041 <                                        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2042 <                                        XDefineCursor(x_display, the_win, mac_cursor);
2033 >                                        // Set new cursor image if it was changed
2034 >                                        if (cursor_changed) {
2035 >                                                cursor_changed = false;
2036 >                                                memcpy(cursor_image->data, MacCursor + 4, 32);
2037 >                                                memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2038 >                                                XDisplayLock();
2039 >                                                XFreeCursor(x_display, mac_cursor);
2040 >                                                XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
2041 >                                                XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
2042 >                                                mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2043 >                                                XDefineCursor(x_display, the_win, mac_cursor);
2044 >                                                XDisplayUnlock();
2045 >                                        }
2046                                  }
2047                          }
1585                }
2048   #ifdef ENABLE_VOSF
2049 <                else if (use_vosf) {
2050 <                        // Update display (VOSF variant)
2051 <                        static int tick_counter = 0;
2052 <                        if (++tick_counter >= frame_skip) {
2053 <                                tick_counter = 0;
2054 <                                if (mainBuffer.dirty) {
2055 <                                        LOCK_VOSF;
2056 <                                        update_display_dga_vosf();
2057 <                                        UNLOCK_VOSF;
2049 >                        else if (use_vosf) {
2050 >                                // Update display (VOSF variant)
2051 >                                if (++tick_counter >= frame_skip) {
2052 >                                        tick_counter = 0;
2053 >                                        if (mainBuffer.dirty) {
2054 >                                                LOCK_VOSF;
2055 >                                                update_display_dga_vosf();
2056 >                                                UNLOCK_VOSF;
2057 >                                        }
2058                                  }
2059                          }
1598                }
2060   #endif
2061  
2062 <                // Set new palette if it was changed
2063 <                if (palette_changed && !emul_suspended) {
2064 <                        palette_changed = false;
2065 <                        XColor c[256];
2066 <                        for (int i=0; i<256; i++) {
2067 <                                c[i].pixel = i;
2068 <                                c[i].red = mac_pal[i].red * 0x0101;
2069 <                                c[i].green = mac_pal[i].green * 0x0101;
2070 <                                c[i].blue = mac_pal[i].blue * 0x0101;
2071 <                                c[i].flags = DoRed | DoGreen | DoBlue;
2072 <                        }
2073 <                        if (depth == 8) {
2074 <                                XStoreColors(x_display, cmap[0], c, 256);
2075 <                                XStoreColors(x_display, cmap[1], c, 256);
1615 < #ifdef ENABLE_XF86_DGA
1616 <                                if (display_type == DIS_SCREEN) {
1617 <                                        current_dga_cmap ^= 1;
1618 <                                        XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1619 <                                }
1620 < #endif
1621 <                        }
2062 >                        // Set new palette if it was changed
2063 >                        handle_palette_changes();
2064 >
2065 >                } else {
2066 >
2067 >                        // No display refresh pending, check for X events
2068 >                        fd_set readfds;
2069 >                        FD_ZERO(&readfds);
2070 >                        FD_SET(fd, &readfds);
2071 >                        struct timeval timeout;
2072 >                        timeout.tv_sec = 0;
2073 >                        timeout.tv_usec = delay;
2074 >                        if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2075 >                                handle_events();
2076                  }
2077          }
2078          return NULL;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines