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.7 by gbeauche, 2003-12-04T22:29:15Z vs.
Revision 1.29 by gbeauche, 2004-06-22T20:01:18Z

# 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 29 | Line 29
29   #include <errno.h>
30   #include <pthread.h>
31  
32 + #include <algorithm>
33 +
34   #ifdef ENABLE_XF86_DGA
35 < #include <X11/extensions/xf86dga.h>
35 > # include <X11/extensions/xf86dga.h>
36   #endif
37  
38   #ifdef ENABLE_XF86_VIDMODE
# Line 48 | Line 50
50   #define DEBUG 0
51   #include "debug.h"
52  
53 + #ifndef NO_STD_NAMESPACE
54 + using std::sort;
55 + #endif
56 +
57  
58   // Constants
59   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
60 + static const bool hw_mac_cursor_accl = true;    // Flag: Enable MacOS to X11 copy of cursor?
61  
62   // Global variables
63   static int32 frame_skip;
64 + static int16 mouse_wheel_mode;
65 + static int16 mouse_wheel_lines;
66   static bool redraw_thread_active = false;       // Flag: Redraw thread installed
67 + static pthread_attr_t redraw_thread_attr;       // Redraw thread attributes
68 + static volatile bool redraw_thread_cancel;      // Flag: Cancel Redraw thread
69   static pthread_t redraw_thread;                         // Redraw thread
70  
71   static bool local_X11;                                          // Flag: X server running on local machine?
# Line 72 | Line 83 | static const bool use_vosf = false;                    //
83  
84   static bool palette_changed = false;            // Flag: Palette changed, redraw thread must update palette
85   static bool ctrl_down = false;                          // Flag: Ctrl key pressed
86 + static bool caps_on = false;                            // Flag: Caps Lock on
87   static bool quit_full_screen = false;           // Flag: DGA close requested from redraw thread
88   static volatile bool quit_full_screen_ack = false;      // Acknowledge for quit_full_screen
89   static bool emerg_quit = false;                         // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
# Line 87 | Line 99 | static int screen;                                                     // Screen numbe
99   static int xdepth;                                                      // Depth of X screen
100   static int depth;                                                       // Depth of Mac frame buffer
101   static Window rootwin, the_win;                         // Root window and our window
102 + static int num_depths = 0;                                      // Number of available X depths
103 + static int *avail_depths = NULL;                        // List of available X depths
104   static XVisualInfo visualInfo;
105   static Visual *vis;
106 + static int color_class;
107 + static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
108   static Colormap cmap[2];                                        // Two colormaps (DGA) for 8-bit mode
109 + static XColor x_palette[256];                           // Color palette to be used as CLUT and gamma table
110 +
111   static XColor black, white;
112   static unsigned long black_pixel, white_pixel;
113   static int eventmask;
114 < static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask;
115 < static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
114 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
115 > static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
116  
117   // Variables for window mode
118   static GC the_gc;
# Line 111 | Line 129 | static uint8 *the_buffer_copy = NULL;          /
129   static uint32 the_buffer_size;                          // Size of allocated the_buffer
130  
131   // Variables for DGA mode
114 static char *dga_screen_base;
115 static int dga_fb_width;
132   static int current_dga_cmap;
133  
134   #ifdef ENABLE_XF86_VIDMODE
# Line 121 | Line 137 | static XF86VidModeModeInfo **x_video_mod
137   static int num_x_video_modes;
138   #endif
139  
140 + // Mutex to protect palette
141 + #ifdef HAVE_SPINLOCKS
142 + static spinlock_t x_palette_lock = SPIN_LOCK_UNLOCKED;
143 + #define LOCK_PALETTE spin_lock(&x_palette_lock)
144 + #define UNLOCK_PALETTE spin_unlock(&x_palette_lock)
145 + #elif defined(HAVE_PTHREADS)
146 + static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER;
147 + #define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock)
148 + #define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock)
149 + #else
150 + #define LOCK_PALETTE
151 + #define UNLOCK_PALETTE
152 + #endif
153 +
154  
155   // Prototypes
156   static void *redraw_func(void *arg);
# Line 133 | Line 163 | extern Display *x_display;
163   // From sys_unix.cpp
164   extern void SysMountFirstFloppy(void);
165  
166 + // From clip_unix.cpp
167 + extern void ClipboardSelectionClear(XSelectionClearEvent *);
168 + extern void ClipboardSelectionRequest(XSelectionRequestEvent *);
169 +
170  
171   // Video acceleration through SIGSEGV
172   #ifdef ENABLE_VOSF
# Line 141 | Line 175 | extern void SysMountFirstFloppy(void);
175  
176  
177   /*
178 + *  Utility functions
179 + */
180 +
181 + // Get current video mode
182 + static inline int get_current_mode(void)
183 + {
184 +        return VModes[cur_mode].viAppleMode;
185 + }
186 +
187 + // Find palette size for given color depth
188 + static int palette_size(int mode)
189 + {
190 +        switch (mode) {
191 +        case APPLE_1_BIT: return 2;
192 +        case APPLE_2_BIT: return 4;
193 +        case APPLE_4_BIT: return 16;
194 +        case APPLE_8_BIT: return 256;
195 +        case APPLE_16_BIT: return 32;
196 +        case APPLE_32_BIT: return 256;
197 +        default: return 0;
198 +        }
199 + }
200 +
201 + // Return bits per pixel for requested depth
202 + static inline int bytes_per_pixel(int depth)
203 + {
204 +        int bpp;
205 +        switch (depth) {
206 +        case 8:
207 +                bpp = 1;
208 +                break;
209 +        case 15: case 16:
210 +                bpp = 2;
211 +                break;
212 +        case 24: case 32:
213 +                bpp = 4;
214 +                break;
215 +        default:
216 +                abort();
217 +        }
218 +        return bpp;
219 + }
220 +
221 + // Map video_mode depth ID to numerical depth value
222 + static inline int depth_of_video_mode(int mode)
223 + {
224 +        int depth;
225 +        switch (mode) {
226 +        case APPLE_1_BIT:
227 +                depth = 1;
228 +                break;
229 +        case APPLE_2_BIT:
230 +                depth = 2;
231 +                break;
232 +        case APPLE_4_BIT:
233 +                depth = 4;
234 +                break;
235 +        case APPLE_8_BIT:
236 +                depth = 8;
237 +                break;
238 +        case APPLE_16_BIT:
239 +                depth = 16;
240 +                break;
241 +        case APPLE_32_BIT:
242 +                depth = 32;
243 +                break;
244 +        default:
245 +                abort();
246 +        }
247 +        return depth;
248 + }
249 +
250 + // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
251 + static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
252 + {
253 +        return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift);
254 + }
255 +
256 +
257 + // Do we have a visual for handling the specified Mac depth? If so, set the
258 + // global variables "xdepth", "visualInfo", "vis" and "color_class".
259 + static bool find_visual_for_depth(int depth)
260 + {
261 +        D(bug("have_visual_for_depth(%d)\n", depth_of_video_mode(depth)));
262 +
263 +        // 1-bit works always and uses default visual
264 +        if (depth == APPLE_1_BIT) {
265 +                vis = DefaultVisual(x_display, screen);
266 +                visualInfo.visualid = XVisualIDFromVisual(vis);
267 +                int num = 0;
268 +                XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num);
269 +                visualInfo = vi[0];
270 +                XFree(vi);
271 +                xdepth = visualInfo.depth;
272 +                color_class = visualInfo.c_class;
273 +                D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth));
274 +                return true;
275 +        }
276 +
277 +        // Calculate minimum and maximum supported X depth
278 +        int min_depth = 1, max_depth = 32;
279 +        switch (depth) {
280 + #ifdef ENABLE_VOSF
281 +                case APPLE_2_BIT:
282 +                case APPLE_4_BIT:       // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
283 +                case APPLE_8_BIT:
284 +                        min_depth = 8;
285 +                        max_depth = 32;
286 +                        break;
287 + #else
288 +                case APPLE_2_BIT:
289 +                case APPLE_4_BIT:       // 2/4-bit requires VOSF blitters
290 +                        return false;
291 +                case APPLE_8_BIT:       // 8-bit without VOSF requires an 8-bit visual
292 +                        min_depth = 8;
293 +                        max_depth = 8;
294 +                        break;
295 + #endif
296 +                case APPLE_16_BIT:      // 16-bit requires a 15/16-bit visual
297 +                        min_depth = 15;
298 +                        max_depth = 16;
299 +                        break;
300 +                case APPLE_32_BIT:      // 32-bit requires a 24/32-bit visual
301 +                        min_depth = 24;
302 +                        max_depth = 32;
303 +                        break;
304 +        }
305 +        D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth));
306 +
307 +        // Try to find a visual for one of the color depths
308 +        bool visual_found = false;
309 +        for (int i=0; i<num_depths && !visual_found; i++) {
310 +
311 +                xdepth = avail_depths[i];
312 +                D(bug(" trying to find visual for depth %d\n", xdepth));
313 +                if (xdepth < min_depth || xdepth > max_depth)
314 +                        continue;
315 +
316 +                // Determine best color class for this depth
317 +                switch (xdepth) {
318 +                        case 1: // Try StaticGray or StaticColor
319 +                                if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo)
320 +                                 || XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo))
321 +                                        visual_found = true;
322 +                                break;
323 +                        case 8: // Need PseudoColor
324 +                                if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo))
325 +                                        visual_found = true;
326 +                                break;
327 +                        case 15:
328 +                        case 16:
329 +                        case 24:
330 +                        case 32: // Try DirectColor first, as this will allow gamma correction
331 +                                if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo)
332 +                                 || XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo))
333 +                                        visual_found = true;
334 +                                break;
335 +                        default:
336 +                                D(bug("  not a supported depth\n"));
337 +                                break;
338 +                }
339 +        }
340 +        if (!visual_found)
341 +                return false;
342 +
343 +        // Visual was found
344 +        vis = visualInfo.visual;
345 +        color_class = visualInfo.c_class;
346 +        D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth));
347 + #if DEBUG
348 +        switch (color_class) {
349 +                case StaticGray: D(bug("StaticGray\n")); break;
350 +                case GrayScale: D(bug("GrayScale\n")); break;
351 +                case StaticColor: D(bug("StaticColor\n")); break;
352 +                case PseudoColor: D(bug("PseudoColor\n")); break;
353 +                case TrueColor: D(bug("TrueColor\n")); break;
354 +                case DirectColor: D(bug("DirectColor\n")); break;
355 +        }
356 + #endif
357 +        return true;
358 + }
359 +
360 +
361 + /*
362   *  Open display (window or fullscreen)
363   */
364  
365 + // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
366 + static Atom WM_DELETE_WINDOW = (Atom)0;
367 + static void set_window_delete_protocol(Window w)
368 + {
369 +        WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
370 +        XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
371 + }
372 +
373 + // Wait until window is mapped/unmapped
374 + static void wait_mapped(Window w)
375 + {
376 +        XEvent e;
377 +        do {
378 +                XMaskEvent(x_display, StructureNotifyMask, &e);
379 +        } while ((e.type != MapNotify) || (e.xmap.event != w));
380 + }
381 +
382 + static void wait_unmapped(Window w)
383 + {
384 +        XEvent e;
385 +        do {
386 +                XMaskEvent(x_display, StructureNotifyMask, &e);
387 +        } while ((e.type != UnmapNotify) || (e.xmap.event != w));
388 + }
389 +
390   // Trap SHM errors
391   static bool shm_error = false;
392   static int (*old_error_handler)(Display *, XErrorEvent *);
# Line 166 | Line 409 | static bool open_window(int width, int h
409          // Set absolute mouse mode
410          ADBSetRelMouseMode(false);
411  
169        // Read frame skip prefs
170        frame_skip = PrefsFindInt32("frameskip");
171        if (frame_skip == 0)
172                frame_skip = 1;
173
412          // Create window
413          XSetWindowAttributes wattr;
414          wattr.event_mask = eventmask = win_eventmask;
415 <        wattr.background_pixel = black_pixel;
416 <        wattr.border_pixel = black_pixel;
415 >        wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
416 >        wattr.border_pixel = 0;
417          wattr.backing_store = NotUseful;
418 <
181 <        XSync(x_display, false);
418 >        wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
419          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
420 <                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore, &wattr);
421 <        XSync(x_display, false);
420 >                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr);
421 >
422 >        // Set window name
423          XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
186        XMapRaised(x_display, the_win);
187        XSync(x_display, false);
424  
425 <        // Set colormap
426 <        if (depth == 8) {
191 <                XSetWindowColormap(x_display, the_win, cmap[0]);
192 <                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
193 <        }
425 >        // Set delete protocol property
426 >        set_window_delete_protocol(the_win);
427  
428          // Make window unresizable
429          XSizeHints *hints;
# Line 204 | Line 437 | static bool open_window(int width, int h
437                  XFree((char *)hints);
438          }
439  
440 +        // Show window
441 +        XMapWindow(x_display, the_win);
442 +        wait_mapped(the_win);
443 +
444          // 1-bit mode is big-endian; if the X server is little-endian, we can't
445          // use SHM because that doesn't allow changing the image byte order
446          bool need_msb_image = (depth == 1 && XImageByteOrder(x_display) == LSBFirst);
# Line 214 | Line 451 | static bool open_window(int width, int h
451  
452                  // Create SHM image ("height + 2" for safety)
453                  img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
454 <                shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
454 >                shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
455 >                D(bug(" shm image created\n"));
456                  the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
457                  shminfo.shmaddr = img->data = (char *)the_buffer_copy;
458                  shminfo.readOnly = False;
# Line 233 | Line 471 | static bool open_window(int width, int h
471                          have_shm = true;
472                          shmctl(shminfo.shmid, IPC_RMID, 0);
473                  }
474 +                D(bug(" shm image attached\n"));
475          }
476  
477          // Create normal X image if SHM doesn't work ("height + 2" for safety)
478          if (!have_shm) {
479 <                int bytes_per_row = aligned_width;
241 <                switch (depth) {
242 <                        case 1:
243 <                                bytes_per_row /= 8;
244 <                                break;
245 <                        case 15:
246 <                        case 16:
247 <                                bytes_per_row *= 2;
248 <                                break;
249 <                        case 24:
250 <                        case 32:
251 <                                bytes_per_row *= 4;
252 <                                break;
253 <                }
479 >                int bytes_per_row = depth == 1 ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth));
480                  the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
481                  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);
482 +                D(bug(" X image created\n"));
483          }
484  
485          // 1-Bit mode is big-endian
486 <    if (depth == 1) {
486 >    if (need_msb_image) {
487          img->byte_order = MSBFirst;
488          img->bitmap_bit_order = MSBFirst;
489      }
# Line 278 | Line 505 | static bool open_window(int width, int h
505  
506          // Create GC
507          the_gc = XCreateGC(x_display, the_win, 0, 0);
508 <        XSetForeground(x_display, the_gc, black_pixel);
508 >        XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
509  
510          // Create cursor
511 <        cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
512 <        cursor_image->byte_order = MSBFirst;
513 <        cursor_image->bitmap_bit_order = MSBFirst;
514 <        cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 36, 16, 16, 16, 2);
515 <        cursor_mask_image->byte_order = MSBFirst;
516 <        cursor_mask_image->bitmap_bit_order = MSBFirst;
517 <        cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
518 <        cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
519 <        cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
520 <        cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
521 <        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
522 <        cursor_changed = false;
511 >        if (hw_mac_cursor_accl) {
512 >                cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
513 >                cursor_image->byte_order = MSBFirst;
514 >                cursor_image->bitmap_bit_order = MSBFirst;
515 >                cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 36, 16, 16, 16, 2);
516 >                cursor_mask_image->byte_order = MSBFirst;
517 >                cursor_mask_image->bitmap_bit_order = MSBFirst;
518 >                cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
519 >                cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
520 >                cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
521 >                cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
522 >                mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
523 >                cursor_changed = false;
524 >        }
525 >
526 >        // Create no_cursor
527 >        else {
528 >                mac_cursor = XCreatePixmapCursor(x_display,
529 >                        XCreatePixmap(x_display, the_win, 1, 1, 1),
530 >                        XCreatePixmap(x_display, the_win, 1, 1, 1),
531 >                        &black, &white, 0, 0);
532 >                XDefineCursor(x_display, the_win, mac_cursor);
533 >        }
534  
535          // Init blitting routines
536          bool native_byte_order;
# Line 306 | Line 544 | static bool open_window(int width, int h
544   #endif
545  
546          // Set bytes per row
309        VModes[cur_mode].viRowBytes = img->bytes_per_line;
547          XSync(x_display, false);
548          return true;
549   }
# Line 318 | Line 555 | static bool open_dga(int width, int heig
555          // Set relative mouse mode
556          ADBSetRelMouseMode(true);
557  
558 +        // Create window
559 +        XSetWindowAttributes wattr;
560 +        wattr.event_mask = eventmask = dga_eventmask;
561 +        wattr.override_redirect = True;
562 +        wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
563 +        the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
564 +                InputOutput, vis, CWEventMask | CWOverrideRedirect |
565 +                (color_class == DirectColor ? CWColormap : 0), &wattr);
566 +
567 +        // Show window
568 +        XMapRaised(x_display, the_win);
569 +        wait_mapped(the_win);
570 +
571   #ifdef ENABLE_XF86_VIDMODE
572          // Switch to best mode
573          if (has_vidmode) {
# Line 334 | Line 584 | static bool open_dga(int width, int heig
584   #endif
585  
586          // Establish direct screen connection
587 +        XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
588 +        XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
589          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
590          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
591 +
592 +        int v_width, v_bank, v_size;
593 +        XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
594          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
595          XF86DGASetViewPort(x_display, screen, 0, 0);
596          XF86DGASetVidPage(x_display, screen, 0);
597  
598          // Set colormap
599 <        if (depth == 8)
599 >        if (!IsDirectMode(get_current_mode())) {
600 >                XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
601                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
346
347        // Set bytes per row
348        int bytes_per_row = (dga_fb_width + 7) & ~7;
349        switch (depth) {
350                case 15:
351                case 16:
352                        bytes_per_row *= 2;
353                        break;
354                case 24:
355                case 32:
356                        bytes_per_row *= 4;
357                        break;
602          }
603 +        XSync(x_display, false);
604  
605 +        // Init blitting routines
606 +        int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, DepthModeForPixelDepth(depth));
607   #if ENABLE_VOSF
608          bool native_byte_order;
609   #ifdef WORDS_BIGENDIAN
# Line 375 | Line 622 | static bool open_dga(int width, int heig
622            the_buffer_size = page_extend((height + 2) * bytes_per_row);
623            the_buffer_copy = (uint8 *)malloc(the_buffer_size);
624            the_buffer = (uint8 *)vm_acquire(the_buffer_size);
625 +          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
626          }
627   #else
628          use_vosf = false;
381        the_buffer = dga_screen_base;
629   #endif
630   #endif
384        screen_base = (uint32)the_buffer;
631  
632 +        // Set frame buffer base
633 +        D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
634 +        screen_base = (uint32)the_buffer;
635          VModes[cur_mode].viRowBytes = bytes_per_row;
387        XSync(x_display, false);
636          return true;
637   #else
638          ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
# Line 394 | Line 642 | static bool open_dga(int width, int heig
642  
643   static bool open_display(void)
644   {
645 <        display_type = VModes[cur_mode].viType;
646 <        switch (VModes[cur_mode].viAppleMode) {
647 <                case APPLE_1_BIT:
648 <                        depth = 1;
649 <                        break;
650 <                case APPLE_2_BIT:
651 <                        depth = 2;
404 <                        break;
405 <                case APPLE_4_BIT:
406 <                        depth = 4;
407 <                        break;
408 <                case APPLE_8_BIT:
409 <                        depth = 8;
410 <                        break;
411 <                case APPLE_16_BIT:
412 <                        depth = xdepth == 15 ? 15 : 16;
413 <                        break;
414 <                case APPLE_32_BIT:
415 <                        depth = 32;
416 <                        break;
645 >        D(bug("open_display()\n"));
646 >        const VideoInfo &mode = VModes[cur_mode];
647 >
648 >        // Find best available X visual
649 >        if (!find_visual_for_depth(mode.viAppleMode)) {
650 >                ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
651 >                return false;
652          }
653  
654 +        // Create color maps
655 +        if (color_class == PseudoColor || color_class == DirectColor) {
656 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
657 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
658 +        } else {
659 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
660 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
661 +        }
662 +
663 +        // Find pixel format of direct modes
664 +        if (color_class == DirectColor || color_class == TrueColor) {
665 +                rshift = gshift = bshift = 0;
666 +                rloss = gloss = bloss = 8;
667 +                uint32 mask;
668 +                for (mask=vis->red_mask; !(mask&1); mask>>=1)
669 +                        ++rshift;
670 +                for (; mask&1; mask>>=1)
671 +                        --rloss;
672 +                for (mask=vis->green_mask; !(mask&1); mask>>=1)
673 +                        ++gshift;
674 +                for (; mask&1; mask>>=1)
675 +                        --gloss;
676 +                for (mask=vis->blue_mask; !(mask&1); mask>>=1)
677 +                        ++bshift;
678 +                for (; mask&1; mask>>=1)
679 +                        --bloss;
680 +        }
681 +
682 +        // Preset palette pixel values for CLUT or gamma table
683 +        if (color_class == DirectColor) {
684 +                int num = vis->map_entries;
685 +                for (int i=0; i<num; i++) {
686 +                        int c = (i * 256) / num;
687 +                        x_palette[i].pixel = map_rgb(c, c, c);
688 +                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
689 +                }
690 +        } else if (color_class == PseudoColor) {
691 +                for (int i=0; i<256; i++) {
692 +                        x_palette[i].pixel = i;
693 +                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
694 +                }
695 +        }
696 +
697 +        // Load gray ramp to color map
698 +        int num = (color_class == DirectColor ? vis->map_entries : 256);
699 +        for (int i=0; i<num; i++) {
700 +                int c = (i * 256) / num;
701 +                x_palette[i].red = c * 0x0101;
702 +                x_palette[i].green = c * 0x0101;
703 +                x_palette[i].blue = c * 0x0101;
704 +        }
705 +        if (color_class == PseudoColor || color_class == DirectColor) {
706 +                XStoreColors(x_display, cmap[0], x_palette, num);
707 +                XStoreColors(x_display, cmap[1], x_palette, num);
708 +        }
709 +
710 + #ifdef ENABLE_VOSF
711 +        // Load gray ramp to 8->16/32 expand map
712 +        if (!IsDirectMode(get_current_mode()) && xdepth > 8)
713 +                for (int i=0; i<256; i++)
714 +                        ExpandMap[i] = map_rgb(i, i, i);
715 + #endif
716 +
717 +        // Create display of requested type
718 +        display_type = mode.viType;
719 +        depth = depth_of_video_mode(mode.viAppleMode);
720 +
721          bool display_open = false;
722          if (display_type == DIS_SCREEN)
723                  display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
# Line 463 | Line 765 | static void close_window(void)
765          if (the_gc)
766                  XFreeGC(x_display, the_gc);
767  
768 <        // Close window
769 <        XDestroyWindow(x_display, the_win);
768 >        XFlush(x_display);
769 >        XSync(x_display, false);
770   }
771  
772   // Close DGA mode
# Line 500 | Line 802 | static void close_display(void)
802          else if (display_type == DIS_WINDOW)
803                  close_window();
804  
805 +        // Close window
806 +        if (the_win) {
807 +                XUnmapWindow(x_display, the_win);
808 +                wait_unmapped(the_win);
809 +                XDestroyWindow(x_display, the_win);
810 +        }
811 +
812 +        // Free colormaps
813 +        if (cmap[0]) {
814 +                XFreeColormap(x_display, cmap[0]);
815 +                cmap[0] = 0;
816 +        }
817 +        if (cmap[1]) {
818 +                XFreeColormap(x_display, cmap[1]);
819 +                cmap[1] = 0;
820 +        }
821 +
822   #ifdef ENABLE_VOSF
823          if (use_vosf) {
824                  // Deinitialize VOSF
# Line 565 | Line 884 | static void keycode_init(void)
884  
885                  // Search for server vendor string, then read keycodes
886                  const char *vendor = ServerVendor(x_display);
887 + #if (defined(__APPLE__) && defined(__MACH__))
888 +                // Force use of MacX mappings on MacOS X with Apple's X server
889 +                int dummy;
890 +                if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy))
891 +                        vendor = "MacX";
892 + #endif
893                  bool vendor_found = false;
894                  char line[256];
895                  while (fgets(line, 255, f)) {
# Line 606 | Line 931 | static void keycode_init(void)
931          }
932   }
933  
934 < static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, long apple_mode, long apple_id, int type)
934 > // Find Apple mode matching best specified dimensions
935 > static int find_apple_resolution(int xsize, int ysize)
936 > {
937 >        int apple_id;
938 >        if (xsize < 800)
939 >                apple_id = APPLE_640x480;
940 >        else if (xsize < 1024)
941 >                apple_id = APPLE_800x600;
942 >        else if (xsize < 1152)
943 >                apple_id = APPLE_1024x768;
944 >        else if (xsize < 1280) {
945 >                if (ysize < 900)
946 >                        apple_id = APPLE_1152x768;
947 >                else
948 >                        apple_id = APPLE_1152x900;
949 >        }
950 >        else if (xsize < 1600)
951 >                apple_id = APPLE_1280x1024;
952 >        else
953 >                apple_id = APPLE_1600x1200;
954 >        return apple_id;
955 > }
956 >
957 > // Find mode in list of supported modes
958 > static int find_mode(int apple_mode, int apple_id, int type)
959 > {
960 >        for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
961 >                if (p->viType == type && p->viAppleID == apple_id && p->viAppleMode == apple_mode)
962 >                        return p - VModes;
963 >        }
964 >        return -1;
965 > }
966 >
967 > // Add mode to list of supported modes
968 > static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
969   {
970          if (allow & test) {
971                  p->viType = type;
# Line 625 | Line 984 | static void add_mode(VideoInfo *&p, uint
984                                  p->viXsize = 1024;
985                                  p->viYsize = 768;
986                                  break;
987 +                        case APPLE_1152x768:
988 +                                p->viXsize = 1152;
989 +                                p->viYsize = 768;
990 +                                break;
991                          case APPLE_1152x900:
992                                  p->viXsize = 1152;
993                                  p->viYsize = 900;
# Line 638 | Line 1001 | static void add_mode(VideoInfo *&p, uint
1001                                  p->viYsize = 1200;
1002                                  break;
1003                  }
1004 <                switch (apple_mode) {
642 <                        case APPLE_8_BIT:
643 <                                p->viRowBytes = p->viXsize;
644 <                                break;
645 <                        case APPLE_16_BIT:
646 <                                p->viRowBytes = p->viXsize * 2;
647 <                                break;
648 <                        case APPLE_32_BIT:
649 <                                p->viRowBytes = p->viXsize * 4;
650 <                                break;
651 <                }
1004 >                p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
1005                  p->viAppleMode = apple_mode;
1006                  p->viAppleID = apple_id;
1007                  p++;
1008          }
1009   }
1010  
1011 + // Add standard list of windowed modes for given color depth
1012 + static void add_window_modes(VideoInfo *&p, int window_modes, int mode)
1013 + {
1014 +        add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);
1015 +        add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);
1016 + }
1017 +
1018   static bool has_mode(int x, int y)
1019   {
1020   #ifdef ENABLE_XF86_VIDMODE
# Line 682 | Line 1042 | bool VideoInit(void)
1042          // Init keycode translation
1043          keycode_init();
1044  
1045 +        // Read frame skip prefs
1046 +        frame_skip = PrefsFindInt32("frameskip");
1047 +        if (frame_skip == 0)
1048 +                frame_skip = 1;
1049 +
1050 +        // Read mouse wheel prefs
1051 +        mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
1052 +        mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1053 +
1054          // Init variables
1055          private_data = NULL;
687        cur_mode = 0;   // Window 640x480
1056          video_activated = true;
1057  
1058          // Find screen and root window
1059          screen = XDefaultScreen(x_display);
1060          rootwin = XRootWindow(x_display, screen);
1061  
1062 +        // Get sorted list of available depths
1063 +        avail_depths = XListDepths(x_display, screen, &num_depths);
1064 +        if (avail_depths == NULL) {
1065 +                ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
1066 +                return false;
1067 +        }
1068 +        sort(avail_depths, avail_depths + num_depths);
1069 +        
1070          // Get screen depth
1071          xdepth = DefaultDepth(x_display, screen);
1072  
# Line 721 | Line 1097 | bool VideoInit(void)
1097          black_pixel = BlackPixel(x_display, screen);
1098          white_pixel = WhitePixel(x_display, screen);
1099  
724        // Get appropriate visual
725        int color_class;
726        switch (xdepth) {
727 #if 0
728                case 1:
729                        color_class = StaticGray;
730                        break;
731 #endif
732                case 8:
733                        color_class = PseudoColor;
734                        break;
735                case 15:
736                case 16:
737                case 24:
738                case 32:
739                        color_class = TrueColor;
740                        break;
741                default:
742                        ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
743                        return false;
744        }
745        if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
746                ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
747                return false;
748        }
749        if (visualInfo.depth != xdepth) {
750                ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
751                return false;
752        }
753        vis = visualInfo.visual;
754
1100          // Mac screen depth follows X depth (for now)
1101 <        depth = xdepth;
1102 <
1103 <        // Create color maps for 8 bit mode
1104 <        if (depth == 8) {
1105 <                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1106 <                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1107 <                XInstallColormap(x_display, cmap[0]);
1108 <                XInstallColormap(x_display, cmap[1]);
1101 >        int default_mode = APPLE_8_BIT;
1102 >        switch (DefaultDepth(x_display, screen)) {
1103 >        case 1:
1104 >                default_mode = APPLE_1_BIT;
1105 >                break;
1106 >        case 8:
1107 >                default_mode = APPLE_8_BIT;
1108 >                break;
1109 >        case 15: case 16:
1110 >                default_mode = APPLE_16_BIT;
1111 >                break;
1112 >        case 24: case 32:
1113 >                default_mode = APPLE_32_BIT;
1114 >                break;
1115          }
1116  
1117          // Construct video mode table
767        int mode = APPLE_8_BIT;
768        int bpr_mult = 8;
769        switch (depth) {
770                case 1:
771                        mode = APPLE_1_BIT;
772                        bpr_mult = 1;
773                        break;
774                case 8:
775                        mode = APPLE_8_BIT;
776                        bpr_mult = 8;
777                        break;
778                case 15:
779                case 16:
780                        mode = APPLE_16_BIT;
781                        bpr_mult = 16;
782                        break;
783                case 24:
784                case 32:
785                        mode = APPLE_32_BIT;
786                        bpr_mult = 32;
787                        break;
788        }
789
1118          uint32 window_modes = PrefsFindInt32("windowmodes");
1119          uint32 screen_modes = PrefsFindInt32("screenmodes");
1120          if (!has_dga)
# Line 795 | Line 1123 | bool VideoInit(void)
1123                  window_modes |= 3;      // Allow at least 640x480 and 800x600 window modes
1124  
1125          VideoInfo *p = VModes;
1126 <        add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);
1127 <        add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);
1126 >        for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1127 >                if (find_visual_for_depth(d))
1128 >                        add_window_modes(p, window_modes, d);
1129 >
1130          if (has_vidmode) {
1131                  if (has_mode(640, 480))
1132 <                        add_mode(p, screen_modes, 1, mode, APPLE_640x480, DIS_SCREEN);
1132 >                        add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1133                  if (has_mode(800, 600))
1134 <                        add_mode(p, screen_modes, 2, mode, APPLE_800x600, DIS_SCREEN);
1134 >                        add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN);
1135                  if (has_mode(1024, 768))
1136 <                        add_mode(p, screen_modes, 4, mode, APPLE_1024x768, DIS_SCREEN);
1136 >                        add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN);
1137 >                if (has_mode(1152, 768))
1138 >                        add_mode(p, screen_modes, 64, default_mode, APPLE_1152x768, DIS_SCREEN);
1139                  if (has_mode(1152, 900))
1140 <                        add_mode(p, screen_modes, 8, mode, APPLE_1152x900, DIS_SCREEN);
1140 >                        add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN);
1141                  if (has_mode(1280, 1024))
1142 <                        add_mode(p, screen_modes, 16, mode, APPLE_1280x1024, DIS_SCREEN);
1142 >                        add_mode(p, screen_modes, 16, default_mode, APPLE_1280x1024, DIS_SCREEN);
1143                  if (has_mode(1600, 1200))
1144 <                        add_mode(p, screen_modes, 32, mode, APPLE_1600x1200, DIS_SCREEN);
1144 >                        add_mode(p, screen_modes, 32, default_mode, APPLE_1600x1200, DIS_SCREEN);
1145          } else if (screen_modes) {
1146                  int xsize = DisplayWidth(x_display, screen);
1147                  int ysize = DisplayHeight(x_display, screen);
1148 <                int apple_id;
817 <                if (xsize < 800)
818 <                        apple_id = APPLE_640x480;
819 <                else if (xsize < 1024)
820 <                        apple_id = APPLE_800x600;
821 <                else if (xsize < 1152)
822 <                        apple_id = APPLE_1024x768;
823 <                else if (xsize < 1280)
824 <                        apple_id = APPLE_1152x900;
825 <                else if (xsize < 1600)
826 <                        apple_id = APPLE_1280x1024;
827 <                else
828 <                        apple_id = APPLE_1600x1200;
1148 >                int apple_id = find_apple_resolution(xsize, ysize);
1149                  p->viType = DIS_SCREEN;
1150                  p->viRowBytes = 0;
1151                  p->viXsize = xsize;
1152                  p->viYsize = ysize;
1153 <                p->viAppleMode = mode;
1153 >                p->viAppleMode = default_mode;
1154                  p->viAppleID = apple_id;
1155                  p++;
1156          }
# Line 840 | Line 1160 | bool VideoInit(void)
1160          p->viAppleMode = 0;
1161          p->viAppleID = 0;
1162  
1163 < #ifdef ENABLE_XF86_DGA
1163 >        // Find default mode (window 640x480)
1164 >        cur_mode = -1;
1165          if (has_dga && screen_modes) {
1166 <                int v_bank, v_size;
1167 <                XF86DGAGetVideo(x_display, screen, &dga_screen_base, &dga_fb_width, &v_bank, &v_size);
1168 <                D(bug("DGA screen_base %p, v_width %d\n", dga_screen_base, dga_fb_width));
1166 >                int screen_width = DisplayWidth(x_display, screen);
1167 >                int screen_height = DisplayHeight(x_display, screen);
1168 >                int apple_id = find_apple_resolution(screen_width, screen_height);
1169 >                if (apple_id != -1)
1170 >                        cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN);
1171 >        }
1172 >        if (cur_mode == -1) {
1173 >                // pick up first windowed mode available
1174 >                for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
1175 >                        if (p->viType == DIS_WINDOW && p->viAppleMode == default_mode) {
1176 >                                cur_mode = p - VModes;
1177 >                                break;
1178 >                        }
1179 >                }
1180 >        }
1181 >        assert(cur_mode != -1);
1182 >
1183 > #if DEBUG
1184 >        D(bug("Available video modes:\n"));
1185 >        for (p = VModes; p->viType != DIS_INVALID; p++) {
1186 >                int bits = depth_of_video_mode(p->viAppleMode);
1187 >                D(bug(" %dx%d (ID %02x), %d colors\n", p->viXsize, p->viYsize, p->viAppleID, 1 << bits));
1188          }
1189   #endif
1190  
# Line 859 | Line 1199 | bool VideoInit(void)
1199  
1200          // Start periodic thread
1201          XSync(x_display, false);
1202 <        redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1202 >        Set_pthread_attr(&redraw_thread_attr, 0);
1203 >        redraw_thread_cancel = false;
1204 >        redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1205          D(bug("Redraw thread installed (%ld)\n", redraw_thread));
1206          return true;
1207   }
# Line 873 | Line 1215 | void VideoExit(void)
1215   {
1216          // Stop redraw thread
1217          if (redraw_thread_active) {
1218 +                redraw_thread_cancel = true;
1219                  pthread_cancel(redraw_thread);
1220                  pthread_join(redraw_thread, NULL);
1221                  redraw_thread_active = false;
# Line 891 | Line 1234 | void VideoExit(void)
1234                  close_display();
1235                  XFlush(x_display);
1236                  XSync(x_display, false);
894                if (depth == 8) {
895                        XFreeColormap(x_display, cmap[0]);
896                        XFreeColormap(x_display, cmap[1]);
897                }
1237          }
1238   }
1239  
# Line 958 | Line 1297 | static void resume_emul(void)
1297          // Reopen full screen display
1298          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1299          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1300 + #ifdef ENABLE_XF86_DGA
1301          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1302          XF86DGASetViewPort(x_display, screen, 0, 0);
1303 + #endif
1304          XSync(x_display, false);
1305  
1306          // the_buffer already contains the data to restore. i.e. since a temporary
# Line 1153 | Line 1494 | static int kc_decode(KeySym ks)
1494          return -1;
1495   }
1496  
1497 < static int event2keycode(XKeyEvent &ev)
1497 > static int event2keycode(XKeyEvent &ev, bool key_down)
1498   {
1499          KeySym ks;
1159        int as;
1500          int i = 0;
1501  
1502          do {
1503                  ks = XLookupKeysym(&ev, i++);
1504 <                as = kc_decode(ks);
1505 <                if (as != -1)
1504 >                int as = kc_decode(ks);
1505 >                if (as >= 0)
1506 >                        return as;
1507 >                if (as == -2)
1508                          return as;
1509          } while (ks != NoSymbol);
1510  
# Line 1175 | Line 1517 | static void handle_events(void)
1517          for (;;) {
1518                  XEvent event;
1519  
1520 <                if (!XCheckMaskEvent(x_display, eventmask, &event))
1520 >                XDisplayLock();
1521 >                if (!XCheckMaskEvent(x_display, eventmask, &event)) {
1522 >                        // Handle clipboard events
1523 >                        if (XCheckTypedEvent(x_display, SelectionRequest, &event))
1524 >                                ClipboardSelectionRequest(&event.xselectionrequest);
1525 >                        else if (XCheckTypedEvent(x_display, SelectionClear, &event))
1526 >                                ClipboardSelectionClear(&event.xselectionclear);
1527 >
1528 >                        // Window "close" widget clicked
1529 >                        else if (XCheckTypedEvent(x_display, ClientMessage, &event)) {
1530 >                                if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
1531 >                                        ADBKeyDown(0x7f);       // Power key
1532 >                                        ADBKeyUp(0x7f);
1533 >                                }
1534 >                        }
1535 >
1536 >                        XDisplayUnlock();
1537                          break;
1538 +                }
1539 +                XDisplayUnlock();
1540  
1541                  switch (event.type) {
1542                          // Mouse button
# Line 1184 | Line 1544 | static void handle_events(void)
1544                                  unsigned int button = ((XButtonEvent *)&event)->button;
1545                                  if (button < 4)
1546                                          ADBMouseDown(button - 1);
1547 +                                else if (button < 6) {  // Wheel mouse
1548 +                                        if (mouse_wheel_mode == 0) {
1549 +                                                int key = (button == 5) ? 0x79 : 0x74;  // Page up/down
1550 +                                                ADBKeyDown(key);
1551 +                                                ADBKeyUp(key);
1552 +                                        } else {
1553 +                                                int key = (button == 5) ? 0x3d : 0x3e;  // Cursor up/down
1554 +                                                for(int i=0; i<mouse_wheel_lines; i++) {
1555 +                                                        ADBKeyDown(key);
1556 +                                                        ADBKeyUp(key);
1557 +                                                }
1558 +                                        }
1559 +                                }
1560                                  break;
1561                          }
1562                          case ButtonRelease: {
# Line 1193 | Line 1566 | static void handle_events(void)
1566                                  break;
1567                          }
1568  
1569 <                        // Mouse moved
1569 >                        // Mouse entered window
1570                          case EnterNotify:
1571 <                                ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1571 >                                if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
1572 >                                        ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1573                                  break;
1574 +
1575 +                        // Mouse moved
1576                          case MotionNotify:
1577 <                                ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1577 >                                ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1578                                  break;
1579  
1580                          // Keyboard
1581                          case KeyPress: {
1582 <                                int code = event2keycode(event.xkey);
1583 <                                if (use_keycodes && code != -1)
1584 <                                        code = keycode_table[event.xkey.keycode & 0xff];
1585 <                                if (code != -1) {
1582 >                                int code = -1;
1583 >                                if (use_keycodes) {
1584 >                                        if (event2keycode(event.xkey, true) != -2)      // This is called to process the hotkeys
1585 >                                                code = keycode_table[event.xkey.keycode & 0xff];
1586 >                                } else
1587 >                                        code = event2keycode(event.xkey, true);
1588 >                                if (code >= 0) {
1589                                          if (!emul_suspended) {
1590 <                                                ADBKeyDown(code);
1590 >                                                if (code == 0x39) {     // Caps Lock pressed
1591 >                                                        if (caps_on) {
1592 >                                                                ADBKeyUp(code);
1593 >                                                                caps_on = false;
1594 >                                                        } else {
1595 >                                                                ADBKeyDown(code);
1596 >                                                                caps_on = true;
1597 >                                                        }
1598 >                                                } else
1599 >                                                        ADBKeyDown(code);
1600                                                  if (code == 0x36)
1601                                                          ctrl_down = true;
1602                                          } else {
# Line 1219 | Line 1607 | static void handle_events(void)
1607                                  break;
1608                          }
1609                          case KeyRelease: {
1610 <                                int code = event2keycode(event.xkey);
1611 <                                if (use_keycodes && code != 1)
1612 <                                        code = keycode_table[event.xkey.keycode & 0xff];
1613 <                                if (code != -1) {
1610 >                                int code = -1;
1611 >                                if (use_keycodes) {
1612 >                                        if (event2keycode(event.xkey, false) != -2)     // This is called to process the hotkeys
1613 >                                                code = keycode_table[event.xkey.keycode & 0xff];
1614 >                                } else
1615 >                                        code = event2keycode(event.xkey, false);
1616 >                                if (code >= 0 && code != 0x39) {        // Don't propagate Caps Lock releases
1617                                          ADBKeyUp(code);
1618                                          if (code == 0x36)
1619                                                  ctrl_down = false;
# Line 1265 | Line 1656 | void VideoVBL(void)
1656   *  Install graphics acceleration
1657   */
1658  
1659 < #if 0
1660 < // Rectangle blitting
1661 < static void accl_bitblt(accl_params *p)
1659 > // Rectangle inversion
1660 > template< int bpp >
1661 > static inline void do_invrect(uint8 *dest, uint32 length)
1662   {
1663 <        D(bug("accl_bitblt\n"));
1663 > #define INVERT_1(PTR, OFS) ((uint8  *)(PTR))[OFS] = ~((uint8  *)(PTR))[OFS]
1664 > #define INVERT_2(PTR, OFS) ((uint16 *)(PTR))[OFS] = ~((uint16 *)(PTR))[OFS]
1665 > #define INVERT_4(PTR, OFS) ((uint32 *)(PTR))[OFS] = ~((uint32 *)(PTR))[OFS]
1666 > #define INVERT_8(PTR, OFS) ((uint64 *)(PTR))[OFS] = ~((uint64 *)(PTR))[OFS]
1667  
1668 <        // Get blitting parameters
1669 <        int16 src_X = p->src_rect[1] - p->src_bounds[1];
1670 <        int16 src_Y = p->src_rect[0] - p->src_bounds[0];
1671 <        int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1672 <        int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1673 <        int16 width = p->dest_rect[3] - p->dest_rect[1] - 1;
1280 <        int16 height = p->dest_rect[2] - p->dest_rect[0] - 1;
1281 <        D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1282 <        D(bug(" width %d, height %d\n", width, height));
1668 > #ifndef UNALIGNED_PROFITABLE
1669 >        // Align on 16-bit boundaries
1670 >        if (bpp < 16 && (((uintptr)dest) & 1)) {
1671 >                INVERT_1(dest, 0);
1672 >                dest += 1; length -= 1;
1673 >        }
1674  
1675 <        // And perform the blit
1676 <        bitblt_hook(src_X, src_Y, dest_X, dest_Y, width, height);
1677 < }
1675 >        // Align on 32-bit boundaries
1676 >        if (bpp < 32 && (((uintptr)dest) & 2)) {
1677 >                INVERT_2(dest, 0);
1678 >                dest += 2; length -= 2;
1679 >        }
1680 > #endif
1681  
1682 < static bool accl_bitblt_hook(accl_params *p)
1683 < {
1684 <        D(bug("accl_draw_hook %p\n", p));
1682 >        // Invert 8-byte words
1683 >        if (length >= 8) {
1684 >                const int r = (length / 8) % 8;
1685 >                dest += r * 8;
1686  
1687 <        // Check if we can accelerate this bitblt
1688 <        if (p->src_base_addr == screen_base && p->dest_base_addr == screen_base &&
1689 <                display_type == DIS_SCREEN && bitblt_hook != NULL &&
1690 <                ((uint32 *)p)[0x18 >> 2] + ((uint32 *)p)[0x128 >> 2] == 0 &&
1691 <                ((uint32 *)p)[0x130 >> 2] == 0 &&
1692 <                p->transfer_mode == 0 &&
1693 <                p->src_row_bytes > 0 && ((uint32 *)p)[0x15c >> 2] > 0) {
1687 >                int n = ((length / 8) + 7) / 8;
1688 >                switch (r) {
1689 >                case 0: do {
1690 >                                dest += 64;
1691 >                                INVERT_8(dest, -8);
1692 >                case 7: INVERT_8(dest, -7);
1693 >                case 6: INVERT_8(dest, -6);
1694 >                case 5: INVERT_8(dest, -5);
1695 >                case 4: INVERT_8(dest, -4);
1696 >                case 3: INVERT_8(dest, -3);
1697 >                case 2: INVERT_8(dest, -2);
1698 >                case 1: INVERT_8(dest, -1);
1699 >                                } while (--n > 0);
1700 >                }
1701 >        }
1702  
1703 <                // Yes, set function pointer
1704 <                p->draw_proc = accl_bitblt;
1705 <                return true;
1703 >        // 32-bit cell to invert?
1704 >        if (length & 4) {
1705 >                INVERT_4(dest, 0);
1706 >                if (bpp <= 16)
1707 >                        dest += 4;
1708          }
1709 <        return false;
1709 >
1710 >        // 16-bit cell to invert?
1711 >        if (bpp <= 16 && (length & 2)) {
1712 >                INVERT_2(dest, 0);
1713 >                if (bpp <= 8)
1714 >                        dest += 2;
1715 >        }
1716 >
1717 >        // 8-bit cell to invert?
1718 >        if (bpp <= 8 && (length & 1))
1719 >                INVERT_1(dest, 0);
1720 >
1721 > #undef INVERT_1
1722 > #undef INVERT_2
1723 > #undef INVERT_4
1724 > #undef INVERT_8
1725   }
1726  
1727 < // Rectangle filling/inversion
1308 < static void accl_fillrect8(accl_params *p)
1727 > void NQD_invrect(uint32 p)
1728   {
1729 <        D(bug("accl_fillrect8\n"));
1729 >        D(bug("accl_invrect %08x\n", p));
1730  
1731 <        // Get filling parameters
1732 <        int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1733 <        int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1734 <        int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;
1735 <        int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;
1317 <        uint8 color = p->pen_mode == 8 ? p->fore_pen : p->back_pen;
1731 >        // Get inversion parameters
1732 >        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1733 >        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1734 >        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1735 >        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1736          D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1737 <        D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));
1737 >        D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1738  
1739 <        // And perform the fill
1740 <        fillrect8_hook(dest_X, dest_Y, dest_X_max, dest_Y_max, color);
1739 >        //!!?? pen_mode == 14
1740 >
1741 >        // And perform the inversion
1742 >        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1743 >        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1744 >        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1745 >        width *= bpp;
1746 >        switch (bpp) {
1747 >        case 1:
1748 >                for (int i = 0; i < height; i++) {
1749 >                        do_invrect<8>(dest, width);
1750 >                        dest += dest_row_bytes;
1751 >                }
1752 >                break;
1753 >        case 2:
1754 >                for (int i = 0; i < height; i++) {
1755 >                        do_invrect<16>(dest, width);
1756 >                        dest += dest_row_bytes;
1757 >                }
1758 >                break;
1759 >        case 4:
1760 >                for (int i = 0; i < height; i++) {
1761 >                        do_invrect<32>(dest, width);
1762 >                        dest += dest_row_bytes;
1763 >                }
1764 >                break;
1765 >        }
1766   }
1767  
1768 < static void accl_fillrect32(accl_params *p)
1768 > // Rectangle filling
1769 > template< int bpp >
1770 > static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1771   {
1772 <        D(bug("accl_fillrect32\n"));
1772 > #define FILL_1(PTR, OFS, VAL) ((uint8  *)(PTR))[OFS] = (VAL)
1773 > #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1774 > #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1775 > #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
1776  
1777 <        // Get filling parameters
1778 <        int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1779 <        int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1780 <        int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;
1781 <        int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;
1782 <        uint32 color = p->pen_mode == 8 ? p->fore_pen : p->back_pen;
1335 <        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1336 <        D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));
1777 > #ifndef UNALIGNED_PROFITABLE
1778 >        // Align on 16-bit boundaries
1779 >        if (bpp < 16 && (((uintptr)dest) & 1)) {
1780 >                FILL_1(dest, 0, color);
1781 >                dest += 1; length -= 1;
1782 >        }
1783  
1784 <        // And perform the fill
1785 <        fillrect32_hook(dest_X, dest_Y, dest_X_max, dest_Y_max, color);
1784 >        // Align on 32-bit boundaries
1785 >        if (bpp < 32 && (((uintptr)dest) & 2)) {
1786 >                FILL_2(dest, 0, color);
1787 >                dest += 2; length -= 2;
1788 >        }
1789 > #endif
1790 >
1791 >        // Fill 8-byte words
1792 >        if (length >= 8) {
1793 >                const uint64 c = (((uint64)color) << 32) | color;
1794 >                const int r = (length / 8) % 8;
1795 >                dest += r * 8;
1796 >
1797 >                int n = ((length / 8) + 7) / 8;
1798 >                switch (r) {
1799 >                case 0: do {
1800 >                                dest += 64;
1801 >                                FILL_8(dest, -8, c);
1802 >                case 7: FILL_8(dest, -7, c);
1803 >                case 6: FILL_8(dest, -6, c);
1804 >                case 5: FILL_8(dest, -5, c);
1805 >                case 4: FILL_8(dest, -4, c);
1806 >                case 3: FILL_8(dest, -3, c);
1807 >                case 2: FILL_8(dest, -2, c);
1808 >                case 1: FILL_8(dest, -1, c);
1809 >                                } while (--n > 0);
1810 >                }
1811 >        }
1812 >
1813 >        // 32-bit cell to fill?
1814 >        if (length & 4) {
1815 >                FILL_4(dest, 0, color);
1816 >                if (bpp <= 16)
1817 >                        dest += 4;
1818 >        }
1819 >
1820 >        // 16-bit cell to fill?
1821 >        if (bpp <= 16 && (length & 2)) {
1822 >                FILL_2(dest, 0, color);
1823 >                if (bpp <= 8)
1824 >                        dest += 2;
1825 >        }
1826 >
1827 >        // 8-bit cell to fill?
1828 >        if (bpp <= 8 && (length & 1))
1829 >                FILL_1(dest, 0, color);
1830 >
1831 > #undef FILL_1
1832 > #undef FILL_2
1833 > #undef FILL_4
1834 > #undef FILL_8
1835   }
1836  
1837 < static void accl_invrect(accl_params *p)
1837 > void NQD_fillrect(uint32 p)
1838   {
1839 <        D(bug("accl_invrect\n"));
1839 >        D(bug("accl_fillrect %08x\n", p));
1840  
1841 <        // Get inversion parameters
1842 <        int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1843 <        int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1844 <        int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;
1845 <        int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;
1841 >        // Get filling parameters
1842 >        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1843 >        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1844 >        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1845 >        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1846 >        uint32 color = htonl(ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen));
1847          D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1848 <        D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));
1849 <
1354 <        //!!?? pen_mode == 14
1848 >        D(bug(" width %d, height %d\n", width, height));
1849 >        D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1850  
1851 <        // And perform the inversion
1852 <        invrect_hook(dest_X, dest_Y, dest_X_max, dest_Y_max);
1851 >        // And perform the fill
1852 >        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1853 >        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1854 >        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1855 >        width *= bpp;
1856 >        switch (bpp) {
1857 >        case 1:
1858 >                for (int i = 0; i < height; i++) {
1859 >                        memset(dest, color, width);
1860 >                        dest += dest_row_bytes;
1861 >                }
1862 >                break;
1863 >        case 2:
1864 >                for (int i = 0; i < height; i++) {
1865 >                        do_fillrect<16>(dest, color, width);
1866 >                        dest += dest_row_bytes;
1867 >                }
1868 >                break;
1869 >        case 4:
1870 >                for (int i = 0; i < height; i++) {
1871 >                        do_fillrect<32>(dest, color, width);
1872 >                        dest += dest_row_bytes;
1873 >                }
1874 >                break;
1875 >        }
1876   }
1877  
1878 < static bool accl_fillrect_hook(accl_params *p)
1878 > bool NQD_fillrect_hook(uint32 p)
1879   {
1880 <        D(bug("accl_fillrect_hook %p\n", p));
1880 >        D(bug("accl_fillrect_hook %08x\n", p));
1881  
1882          // Check if we can accelerate this fillrect
1883 <        if (p->dest_base_addr == screen_base && ((uint32 *)p)[0x284 >> 2] != 0 && display_type == DIS_SCREEN) {
1884 <                if (p->transfer_mode == 8) {
1883 >        if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1884 >                const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1885 >                if (transfer_mode == 8) {
1886                          // Fill
1887 <                        if (p->dest_pixel_size == 8 && fillrect8_hook != NULL) {
1888 <                                p->draw_proc = accl_fillrect8;
1889 <                                return true;
1890 <                        } else if (p->dest_pixel_size == 32 && fillrect32_hook != NULL) {
1372 <                                p->draw_proc = accl_fillrect32;
1373 <                                return true;
1374 <                        }
1375 <                } else if (p->transfer_mode == 10 && invrect_hook != NULL) {
1887 >                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1888 >                        return true;
1889 >                }
1890 >                else if (transfer_mode == 10) {
1891                          // Invert
1892 <                        p->draw_proc = accl_invrect;
1892 >                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1893                          return true;
1894                  }
1895          }
1896          return false;
1897   }
1898  
1899 + // Rectangle blitting
1900 + // TODO: optimize for VOSF and target pixmap == screen
1901 + void NQD_bitblt(uint32 p)
1902 + {
1903 +        D(bug("accl_bitblt %08x\n", p));
1904 +
1905 +        // Get blitting parameters
1906 +        int16 src_X  = (int16)ReadMacInt16(p + acclSrcRect + 2) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 2);
1907 +        int16 src_Y  = (int16)ReadMacInt16(p + acclSrcRect + 0) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 0);
1908 +        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1909 +        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1910 +        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1911 +        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1912 +        D(bug(" src addr %08x, dest addr %08x\n", ReadMacInt32(p + acclSrcBaseAddr), ReadMacInt32(p + acclDestBaseAddr)));
1913 +        D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1914 +        D(bug(" width %d, height %d\n", width, height));
1915 +
1916 +        // And perform the blit
1917 +        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1918 +        width *= bpp;
1919 +        if ((int32)ReadMacInt32(p + acclSrcRowBytes) > 0) {
1920 +                const int src_row_bytes = (int32)ReadMacInt32(p + acclSrcRowBytes);
1921 +                const int dst_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1922 +                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + (src_Y * src_row_bytes) + (src_X * bpp));
1923 +                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dst_row_bytes) + (dest_X * bpp));
1924 +                for (int i = 0; i < height; i++) {
1925 +                        memmove(dst, src, width);
1926 +                        src += src_row_bytes;
1927 +                        dst += dst_row_bytes;
1928 +                }
1929 +        }
1930 +        else {
1931 +                const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1932 +                const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1933 +                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp));
1934 +                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp));
1935 +                for (int i = height - 1; i >= 0; i--) {
1936 +                        memmove(dst, src, width);
1937 +                        src -= src_row_bytes;
1938 +                        dst -= dst_row_bytes;
1939 +                }
1940 +        }
1941 + }
1942 +
1943 + /*
1944 +  BitBlt transfer modes:
1945 +  0 : srcCopy
1946 +  1 : srcOr
1947 +  2 : srcXor
1948 +  3 : srcBic
1949 +  4 : notSrcCopy
1950 +  5 : notSrcOr
1951 +  6 : notSrcXor
1952 +  7 : notSrcBic
1953 +  32 : blend
1954 +  33 : addPin
1955 +  34 : addOver
1956 +  35 : subPin
1957 +  36 : transparent
1958 +  37 : adMax
1959 +  38 : subOver
1960 +  39 : adMin
1961 +  50 : hilite
1962 + */
1963 +
1964 + bool NQD_bitblt_hook(uint32 p)
1965 + {
1966 +        D(bug("accl_draw_hook %08x\n", p));
1967 +
1968 +        // Check if we can accelerate this bitblt
1969 +        if (ReadMacInt32(p + 0x018) + ReadMacInt32(p + 0x128) == 0 &&
1970 +                ReadMacInt32(p + 0x130) == 0 &&
1971 +                ReadMacInt32(p + acclSrcPixelSize) >= 8 &&
1972 +                ReadMacInt32(p + acclSrcPixelSize) == ReadMacInt32(p + acclDestPixelSize) &&
1973 +                (ReadMacInt32(p + acclSrcRowBytes) ^ ReadMacInt32(p + acclDestRowBytes)) >= 0 && // same sign?
1974 +                ReadMacInt32(p + acclTransferMode) == 0 &&                                                                               // srcCopy?
1975 +                ReadMacInt32(p + 0x15c) > 0) {
1976 +
1977 +                // Yes, set function pointer
1978 +                WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1979 +                return true;
1980 +        }
1981 +        return false;
1982 + }
1983 +
1984   // Wait for graphics operation to finish
1985 < static bool accl_sync_hook(void *arg)
1985 > bool NQD_sync_hook(uint32 arg)
1986   {
1987 <        D(bug("accl_sync_hook %p\n", arg));
1388 <        if (sync_hook != NULL)
1389 <                sync_hook();
1987 >        D(bug("accl_sync_hook %08x\n", arg));
1988          return true;
1989   }
1990  
1393 static struct accl_hook_info bitblt_hook_info = {accl_bitblt_hook, accl_sync_hook, ACCL_BITBLT};
1394 static struct accl_hook_info fillrect_hook_info = {accl_fillrect_hook, accl_sync_hook, ACCL_FILLRECT};
1395 #endif
1396
1991   void VideoInstallAccel(void)
1992   {
1993          // Install acceleration hooks
1994          if (PrefsFindBool("gfxaccel")) {
1995                  D(bug("Video: Installing acceleration hooks\n"));
1996 < //!!    NQDMisc(6, &bitblt_hook_info);
1997 < //              NQDMisc(6, &fillrect_hook_info);
1996 >                uint32 base;
1997 >
1998 >                SheepVar bitblt_hook_info(sizeof(accl_hook_info));
1999 >                base = bitblt_hook_info.addr();
2000 >                WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK));
2001 >                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
2002 >                WriteMacInt32(base + 8, ACCL_BITBLT);
2003 >                NQDMisc(6, bitblt_hook_info.ptr());
2004 >
2005 >                SheepVar fillrect_hook_info(sizeof(accl_hook_info));
2006 >                base = fillrect_hook_info.addr();
2007 >                WriteMacInt32(base + 0, NativeTVECT(NATIVE_FILLRECT_HOOK));
2008 >                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
2009 >                WriteMacInt32(base + 8, ACCL_FILLRECT);
2010 >                NQDMisc(6, fillrect_hook_info.ptr());
2011          }
2012   }
2013  
# Line 1463 | Line 2070 | int16 video_mode_change(VidLocals *csSav
2070  
2071   void video_set_palette(void)
2072   {
2073 +        LOCK_PALETTE;
2074 +
2075 +        // Convert colors to XColor array
2076 +        int mode = get_current_mode();
2077 +        int num_in = palette_size(mode);
2078 +        int num_out = 256;
2079 +        bool stretch = false;
2080 +        if (IsDirectMode(mode)) {
2081 +                // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
2082 +                num_out = vis->map_entries;
2083 +                stretch = true;
2084 +        }
2085 +        XColor *p = x_palette;
2086 +        for (int i=0; i<num_out; i++) {
2087 +                int c = (stretch ? (i * num_in) / num_out : i);
2088 +                p->red = mac_pal[c].red * 0x0101;
2089 +                p->green = mac_pal[c].green * 0x0101;
2090 +                p->blue = mac_pal[c].blue * 0x0101;
2091 +                p++;
2092 +        }
2093 +
2094 + #ifdef ENABLE_VOSF
2095 +        // Recalculate pixel color expansion map
2096 +        if (!IsDirectMode(mode) && xdepth > 8) {
2097 +                for (int i=0; i<256; i++) {
2098 +                        int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
2099 +                        ExpandMap[i] = map_rgb(mac_pal[c].red, mac_pal[c].green, mac_pal[c].blue);
2100 +                }
2101 +
2102 +                // We have to redraw everything because the interpretation of pixel values changed
2103 +                LOCK_VOSF;
2104 +                PFLAG_SET_ALL;
2105 +                UNLOCK_VOSF;
2106 +                memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2107 +        }
2108 + #endif
2109 +
2110 +        // Tell redraw thread to change palette
2111          palette_changed = true;
2112 +
2113 +        UNLOCK_PALETTE;
2114 + }
2115 +
2116 +
2117 + /*
2118 + *  Can we set the MacOS cursor image into the window?
2119 + */
2120 +
2121 + bool video_can_change_cursor(void)
2122 + {
2123 +        return hw_mac_cursor_accl && (display_type != DIS_SCREEN);
2124   }
2125  
2126  
# Line 1590 | Line 2247 | static void update_display(void)
2247  
2248          // Refresh display
2249          if (high && wide) {
2250 +                XDisplayLock();
2251                  if (have_shm)
2252                          XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
2253                  else
2254                          XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
2255 +                XDisplayUnlock();
2256 +        }
2257 + }
2258 +
2259 + const int VIDEO_REFRESH_HZ = 60;
2260 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2261 +
2262 + static void handle_palette_changes(void)
2263 + {
2264 +        LOCK_PALETTE;
2265 +
2266 +        if (palette_changed && !emul_suspended) {
2267 +                palette_changed = false;
2268 +
2269 +                int mode = get_current_mode();
2270 +                if (color_class == PseudoColor || color_class == DirectColor) {
2271 +                        int num = vis->map_entries;
2272 +                        bool set_clut = true;
2273 +                        if (!IsDirectMode(mode) && color_class == DirectColor) {
2274 +                                if (display_type == DIS_WINDOW)
2275 +                                        set_clut = false; // Indexed mode on true color screen, don't set CLUT
2276 +                        }
2277 +
2278 +                        if (set_clut) {
2279 +                                XDisplayLock();
2280 +                                XStoreColors(x_display, cmap[0], x_palette, num);
2281 +                                XStoreColors(x_display, cmap[1], x_palette, num);
2282 +                                XSync(x_display, false);
2283 +                                XDisplayUnlock();
2284 +                        }
2285 +                }
2286 +
2287 + #ifdef ENABLE_XF86_DGA
2288 +                if (display_type == DIS_SCREEN) {
2289 +                        current_dga_cmap ^= 1;
2290 +                        if (!IsDirectMode(mode) && cmap[current_dga_cmap])
2291 +                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2292 +                }
2293 + #endif
2294          }
2295 +
2296 +        UNLOCK_PALETTE;
2297   }
2298  
2299   static void *redraw_func(void *arg)
2300   {
2301 <        int tick_counter = 0;
1603 <        struct timespec req = {0, 16666667};
2301 >        int fd = ConnectionNumber(x_display);
2302  
2303 <        for (;;) {
2303 >        uint64 start = GetTicks_usec();
2304 >        int64 ticks = 0;
2305 >        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2306  
2307 <                // Wait
1608 <                nanosleep(&req, NULL);
2307 >        while (!redraw_thread_cancel) {
2308  
2309                  // Pause if requested (during video mode switches)
2310                  while (thread_stop_req)
2311                          thread_stop_ack = true;
2312  
2313 <                // Handle X11 events
2314 <                handle_events();
2313 >                int64 delay = next - GetTicks_usec();
2314 >                if (delay < -VIDEO_REFRESH_DELAY) {
2315 >
2316 >                        // We are lagging far behind, so we reset the delay mechanism
2317 >                        next = GetTicks_usec();
2318 >
2319 >                } else if (delay <= 0) {
2320 >
2321 >                        // Delay expired, refresh display
2322 >                        next += VIDEO_REFRESH_DELAY;
2323 >                        ticks++;
2324 >
2325 >                        // Handle X11 events
2326 >                        handle_events();
2327  
2328 <                // Quit DGA mode if requested
2329 <                if (quit_full_screen) {
2330 <                        quit_full_screen = false;
2331 <                        if (display_type == DIS_SCREEN) {
2328 >                        // Quit DGA mode if requested
2329 >                        if (quit_full_screen) {
2330 >                                quit_full_screen = false;
2331 >                                if (display_type == DIS_SCREEN) {
2332 >                                        XDisplayLock();
2333   #ifdef ENABLE_XF86_DGA
2334 <                                XF86DGADirectVideo(x_display, screen, 0);
2335 <                                XUngrabPointer(x_display, CurrentTime);
2336 <                                XUngrabKeyboard(x_display, CurrentTime);
2337 < #endif
2338 <                                XSync(x_display, false);
2339 <                                quit_full_screen_ack = true;
2340 <                                return NULL;
2334 >                                        XF86DGADirectVideo(x_display, screen, 0);
2335 >                                        XUngrabPointer(x_display, CurrentTime);
2336 >                                        XUngrabKeyboard(x_display, CurrentTime);
2337 >                                        XUnmapWindow(x_display, the_win);
2338 >                                        wait_unmapped(the_win);
2339 >                                        XDestroyWindow(x_display, the_win);
2340 > #endif
2341 >                                        XSync(x_display, false);
2342 >                                        XDisplayUnlock();
2343 >                                        quit_full_screen_ack = true;
2344 >                                        return NULL;
2345 >                                }
2346                          }
1630                }
2347  
2348 <                // Refresh display and set cursor image in window mode
2349 <                if (display_type == DIS_WINDOW) {
2350 <                        tick_counter++;
2351 <                        if (tick_counter >= frame_skip) {
2352 <                                tick_counter = 0;
2348 >                        // Refresh display and set cursor image in window mode
2349 >                        static int tick_counter = 0;
2350 >                        if (display_type == DIS_WINDOW) {
2351 >                                tick_counter++;
2352 >                                if (tick_counter >= frame_skip) {
2353 >                                        tick_counter = 0;
2354  
2355 <                                // Update display
2355 >                                        // Update display
2356   #ifdef ENABLE_VOSF
2357 <                                if (use_vosf) {
2358 <                                        if (mainBuffer.dirty) {
2359 <                                                LOCK_VOSF;
2360 <                                                update_display_window_vosf();
2361 <                                                UNLOCK_VOSF;
2362 <                                                XSync(x_display, false); // Let the server catch up
2357 >                                        if (use_vosf) {
2358 >                                                XDisplayLock();
2359 >                                                if (mainBuffer.dirty) {
2360 >                                                        LOCK_VOSF;
2361 >                                                        update_display_window_vosf();
2362 >                                                        UNLOCK_VOSF;
2363 >                                                        XSync(x_display, false); // Let the server catch up
2364 >                                                }
2365 >                                                XDisplayUnlock();
2366                                          }
2367 <                                }
1648 <                                else
2367 >                                        else
2368   #endif
2369 <                                        update_display();
2369 >                                                update_display();
2370  
2371 <                                // Set new cursor image if it was changed
2372 <                                if (cursor_changed) {
2373 <                                        cursor_changed = false;
2374 <                                        memcpy(cursor_image->data, MacCursor + 4, 32);
2375 <                                        memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2376 <                                        XFreeCursor(x_display, mac_cursor);
2377 <                                        XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
2378 <                                        XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
2379 <                                        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2380 <                                        XDefineCursor(x_display, the_win, mac_cursor);
2371 >                                        // Set new cursor image if it was changed
2372 >                                        if (hw_mac_cursor_accl && cursor_changed) {
2373 >                                                cursor_changed = false;
2374 >                                                memcpy(cursor_image->data, MacCursor + 4, 32);
2375 >                                                memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2376 >                                                XDisplayLock();
2377 >                                                XFreeCursor(x_display, mac_cursor);
2378 >                                                XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
2379 >                                                XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
2380 >                                                mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2381 >                                                XDefineCursor(x_display, the_win, mac_cursor);
2382 >                                                XDisplayUnlock();
2383 >                                        }
2384                                  }
2385                          }
1664                }
2386   #ifdef ENABLE_VOSF
2387 <                else if (use_vosf) {
2388 <                        // Update display (VOSF variant)
2389 <                        static int tick_counter = 0;
2390 <                        if (++tick_counter >= frame_skip) {
2391 <                                tick_counter = 0;
2392 <                                if (mainBuffer.dirty) {
2393 <                                        LOCK_VOSF;
2394 <                                        update_display_dga_vosf();
2395 <                                        UNLOCK_VOSF;
2387 >                        else if (use_vosf) {
2388 >                                // Update display (VOSF variant)
2389 >                                if (++tick_counter >= frame_skip) {
2390 >                                        tick_counter = 0;
2391 >                                        if (mainBuffer.dirty) {
2392 >                                                LOCK_VOSF;
2393 >                                                update_display_dga_vosf();
2394 >                                                UNLOCK_VOSF;
2395 >                                        }
2396                                  }
2397                          }
1677                }
2398   #endif
2399  
2400 <                // Set new palette if it was changed
2401 <                if (palette_changed && !emul_suspended) {
2402 <                        palette_changed = false;
2403 <                        XColor c[256];
2404 <                        for (int i=0; i<256; i++) {
2405 <                                c[i].pixel = i;
2406 <                                c[i].red = mac_pal[i].red * 0x0101;
2407 <                                c[i].green = mac_pal[i].green * 0x0101;
2408 <                                c[i].blue = mac_pal[i].blue * 0x0101;
2409 <                                c[i].flags = DoRed | DoGreen | DoBlue;
2410 <                        }
2411 <                        if (depth == 8) {
2412 <                                XStoreColors(x_display, cmap[0], c, 256);
2413 <                                XStoreColors(x_display, cmap[1], c, 256);
1694 < #ifdef ENABLE_XF86_DGA
1695 <                                if (display_type == DIS_SCREEN) {
1696 <                                        current_dga_cmap ^= 1;
1697 <                                        XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1698 <                                }
1699 < #endif
1700 <                        }
2400 >                        // Set new palette if it was changed
2401 >                        handle_palette_changes();
2402 >
2403 >                } else {
2404 >
2405 >                        // No display refresh pending, check for X events
2406 >                        fd_set readfds;
2407 >                        FD_ZERO(&readfds);
2408 >                        FD_SET(fd, &readfds);
2409 >                        struct timeval timeout;
2410 >                        timeout.tv_sec = 0;
2411 >                        timeout.tv_usec = delay;
2412 >                        if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2413 >                                handle_events();
2414                  }
2415          }
2416          return NULL;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines