ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/video_x.cpp
Revision: 1.26
Committed: 2004-06-05T06:30:58Z (20 years, 5 months ago) by gbeauche
Branch: MAIN
Changes since 1.25: +0 -2 lines
Log Message:
remove now obsolete code

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * video_x.cpp - Video/graphics emulation, X11 specific stuff
3     *
4 cebix 1.11 * SheepShaver (C) 1997-2004 Marc Hellwig and Christian Bauer
5 cebix 1.1 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21 gbeauche 1.6 #include "sysdeps.h"
22    
23 cebix 1.1 #include <X11/Xlib.h>
24     #include <X11/Xutil.h>
25     #include <X11/keysym.h>
26     #include <X11/extensions/XShm.h>
27     #include <sys/ipc.h>
28     #include <sys/shm.h>
29 gbeauche 1.6 #include <errno.h>
30 gbeauche 1.7 #include <pthread.h>
31 gbeauche 1.6
32 gbeauche 1.13 #include <algorithm>
33    
34 gbeauche 1.6 #ifdef ENABLE_XF86_DGA
35 gbeauche 1.15 # include <X11/extensions/xf86dga.h>
36 gbeauche 1.6 #endif
37    
38     #ifdef ENABLE_XF86_VIDMODE
39     # include <X11/extensions/xf86vmode.h>
40     #endif
41 cebix 1.1
42     #include "main.h"
43     #include "adb.h"
44     #include "prefs.h"
45     #include "user_strings.h"
46     #include "about_window.h"
47     #include "video.h"
48     #include "video_defs.h"
49    
50     #define DEBUG 0
51     #include "debug.h"
52    
53 gbeauche 1.13 #ifndef NO_STD_NAMESPACE
54     using std::sort;
55     #endif
56    
57 cebix 1.1
58 gbeauche 1.6 // Constants
59     const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
60 gbeauche 1.22 static const bool hw_mac_cursor_accl = true; // Flag: Enable MacOS to X11 copy of cursor?
61 cebix 1.1
62     // Global variables
63     static int32 frame_skip;
64 gbeauche 1.8 static int16 mouse_wheel_mode;
65     static int16 mouse_wheel_lines;
66 cebix 1.1 static bool redraw_thread_active = false; // Flag: Redraw thread installed
67 gbeauche 1.15 static pthread_attr_t redraw_thread_attr; // Redraw thread attributes
68 cebix 1.1 static pthread_t redraw_thread; // Redraw thread
69    
70 gbeauche 1.4 static bool local_X11; // Flag: X server running on local machine?
71 cebix 1.1 static volatile bool thread_stop_req = false;
72     static volatile bool thread_stop_ack = false; // Acknowledge for thread_stop_req
73    
74     static bool has_dga = false; // Flag: Video DGA capable
75     static bool has_vidmode = false; // Flag: VidMode extension available
76    
77 gbeauche 1.3 #ifdef ENABLE_VOSF
78     static bool use_vosf = true; // Flag: VOSF enabled
79     #else
80     static const bool use_vosf = false; // VOSF not possible
81     #endif
82    
83 cebix 1.1 static bool palette_changed = false; // Flag: Palette changed, redraw thread must update palette
84     static bool ctrl_down = false; // Flag: Ctrl key pressed
85     static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
86     static volatile bool quit_full_screen_ack = false; // Acknowledge for quit_full_screen
87     static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
88    
89     static bool emul_suspended = false; // Flag: emulator suspended
90     static Window suspend_win; // "Suspend" window
91     static void *fb_save = NULL; // Saved frame buffer for suspend
92 gbeauche 1.6 static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
93     static int keycode_table[256]; // X keycode -> Mac keycode translation table
94 cebix 1.1
95     // X11 variables
96     static int screen; // Screen number
97     static int xdepth; // Depth of X screen
98     static int depth; // Depth of Mac frame buffer
99     static Window rootwin, the_win; // Root window and our window
100 gbeauche 1.13 static int num_depths = 0; // Number of available X depths
101     static int *avail_depths = NULL; // List of available X depths
102 cebix 1.1 static XVisualInfo visualInfo;
103     static Visual *vis;
104 gbeauche 1.13 static int color_class;
105     static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
106 cebix 1.1 static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode
107 gbeauche 1.13 static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table
108    
109 cebix 1.1 static XColor black, white;
110     static unsigned long black_pixel, white_pixel;
111     static int eventmask;
112 gbeauche 1.13 static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
113     static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
114 cebix 1.1
115     // Variables for window mode
116     static GC the_gc;
117     static XImage *img = NULL;
118     static XShmSegmentInfo shminfo;
119     static XImage *cursor_image, *cursor_mask_image;
120     static Pixmap cursor_map, cursor_mask_map;
121     static Cursor mac_cursor;
122     static GC cursor_gc, cursor_mask_gc;
123     static bool cursor_changed = false; // Flag: Cursor changed, window_func must update cursor
124     static bool have_shm = false; // Flag: SHM present and usable
125 gbeauche 1.3 static uint8 *the_buffer = NULL; // Pointer to Mac frame buffer
126 cebix 1.1 static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer
127 gbeauche 1.3 static uint32 the_buffer_size; // Size of allocated the_buffer
128 cebix 1.1
129     // Variables for DGA mode
130     static int current_dga_cmap;
131    
132     #ifdef ENABLE_XF86_VIDMODE
133     // Variables for XF86 VidMode support
134     static XF86VidModeModeInfo **x_video_modes; // Array of all available modes
135     static int num_x_video_modes;
136     #endif
137    
138 gbeauche 1.13 // Mutex to protect palette
139     #ifdef HAVE_SPINLOCKS
140     static spinlock_t x_palette_lock = SPIN_LOCK_UNLOCKED;
141     #define LOCK_PALETTE spin_lock(&x_palette_lock)
142     #define UNLOCK_PALETTE spin_unlock(&x_palette_lock)
143     #elif defined(HAVE_PTHREADS)
144     static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER;
145     #define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock)
146     #define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock)
147     #else
148     #define LOCK_PALETTE
149     #define UNLOCK_PALETTE
150     #endif
151    
152 cebix 1.1
153     // Prototypes
154     static void *redraw_func(void *arg);
155    
156    
157 gbeauche 1.4 // From main_unix.cpp
158     extern char *x_display_name;
159 cebix 1.1 extern Display *x_display;
160    
161     // From sys_unix.cpp
162     extern void SysMountFirstFloppy(void);
163    
164 gbeauche 1.9 // From clip_unix.cpp
165     extern void ClipboardSelectionClear(XSelectionClearEvent *);
166     extern void ClipboardSelectionRequest(XSelectionRequestEvent *);
167    
168 cebix 1.1
169 gbeauche 1.3 // Video acceleration through SIGSEGV
170     #ifdef ENABLE_VOSF
171     # include "video_vosf.h"
172     #endif
173    
174    
175 cebix 1.1 /*
176 gbeauche 1.13 * Utility functions
177     */
178    
179     // Get current video mode
180     static inline int get_current_mode(void)
181     {
182     return VModes[cur_mode].viAppleMode;
183     }
184    
185     // Find palette size for given color depth
186     static int palette_size(int mode)
187     {
188     switch (mode) {
189     case APPLE_1_BIT: return 2;
190     case APPLE_2_BIT: return 4;
191     case APPLE_4_BIT: return 16;
192     case APPLE_8_BIT: return 256;
193     case APPLE_16_BIT: return 32;
194     case APPLE_32_BIT: return 256;
195     default: return 0;
196     }
197     }
198    
199 gbeauche 1.16 // Return bits per pixel for requested depth
200     static inline int bytes_per_pixel(int depth)
201     {
202     int bpp;
203     switch (depth) {
204     case 8:
205     bpp = 1;
206     break;
207     case 15: case 16:
208     bpp = 2;
209     break;
210     case 24: case 32:
211     bpp = 4;
212     break;
213     default:
214     abort();
215     }
216     return bpp;
217     }
218    
219 gbeauche 1.13 // Map video_mode depth ID to numerical depth value
220     static inline int depth_of_video_mode(int mode)
221     {
222 gbeauche 1.16 int depth;
223 gbeauche 1.13 switch (mode) {
224     case APPLE_1_BIT:
225     depth = 1;
226     break;
227     case APPLE_2_BIT:
228     depth = 2;
229     break;
230     case APPLE_4_BIT:
231     depth = 4;
232     break;
233     case APPLE_8_BIT:
234     depth = 8;
235     break;
236     case APPLE_16_BIT:
237     depth = 16;
238     break;
239     case APPLE_32_BIT:
240     depth = 32;
241     break;
242     default:
243     abort();
244     }
245     return depth;
246     }
247    
248     // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
249     static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
250     {
251     return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift);
252     }
253    
254    
255     // Do we have a visual for handling the specified Mac depth? If so, set the
256     // global variables "xdepth", "visualInfo", "vis" and "color_class".
257     static bool find_visual_for_depth(int depth)
258     {
259     D(bug("have_visual_for_depth(%d)\n", depth_of_video_mode(depth)));
260    
261     // 1-bit works always and uses default visual
262     if (depth == APPLE_1_BIT) {
263     vis = DefaultVisual(x_display, screen);
264     visualInfo.visualid = XVisualIDFromVisual(vis);
265     int num = 0;
266     XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num);
267     visualInfo = vi[0];
268     XFree(vi);
269     xdepth = visualInfo.depth;
270     color_class = visualInfo.c_class;
271     D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth));
272     return true;
273     }
274    
275     // Calculate minimum and maximum supported X depth
276     int min_depth = 1, max_depth = 32;
277     switch (depth) {
278     #ifdef ENABLE_VOSF
279     case APPLE_2_BIT:
280     case APPLE_4_BIT: // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
281     case APPLE_8_BIT:
282     min_depth = 8;
283     max_depth = 32;
284     break;
285     #else
286     case APPLE_2_BIT:
287     case APPLE_4_BIT: // 2/4-bit requires VOSF blitters
288     return false;
289     case APPLE_8_BIT: // 8-bit without VOSF requires an 8-bit visual
290     min_depth = 8;
291     max_depth = 8;
292     break;
293     #endif
294     case APPLE_16_BIT: // 16-bit requires a 15/16-bit visual
295     min_depth = 15;
296     max_depth = 16;
297     break;
298     case APPLE_32_BIT: // 32-bit requires a 24/32-bit visual
299     min_depth = 24;
300     max_depth = 32;
301     break;
302     }
303     D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth));
304    
305     // Try to find a visual for one of the color depths
306     bool visual_found = false;
307     for (int i=0; i<num_depths && !visual_found; i++) {
308    
309     xdepth = avail_depths[i];
310     D(bug(" trying to find visual for depth %d\n", xdepth));
311     if (xdepth < min_depth || xdepth > max_depth)
312     continue;
313    
314     // Determine best color class for this depth
315     switch (xdepth) {
316     case 1: // Try StaticGray or StaticColor
317     if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo)
318     || XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo))
319     visual_found = true;
320     break;
321     case 8: // Need PseudoColor
322     if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo))
323     visual_found = true;
324     break;
325     case 15:
326     case 16:
327     case 24:
328     case 32: // Try DirectColor first, as this will allow gamma correction
329     if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo)
330     || XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo))
331     visual_found = true;
332     break;
333     default:
334     D(bug(" not a supported depth\n"));
335     break;
336     }
337     }
338     if (!visual_found)
339     return false;
340    
341     // Visual was found
342     vis = visualInfo.visual;
343     color_class = visualInfo.c_class;
344     D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth));
345     #if DEBUG
346     switch (color_class) {
347     case StaticGray: D(bug("StaticGray\n")); break;
348     case GrayScale: D(bug("GrayScale\n")); break;
349     case StaticColor: D(bug("StaticColor\n")); break;
350     case PseudoColor: D(bug("PseudoColor\n")); break;
351     case TrueColor: D(bug("TrueColor\n")); break;
352     case DirectColor: D(bug("DirectColor\n")); break;
353     }
354     #endif
355     return true;
356     }
357    
358    
359     /*
360 cebix 1.1 * Open display (window or fullscreen)
361     */
362    
363 gbeauche 1.14 // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
364     static Atom WM_DELETE_WINDOW = (Atom)0;
365     static void set_window_delete_protocol(Window w)
366     {
367     WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
368     XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
369     }
370    
371 gbeauche 1.13 // Wait until window is mapped/unmapped
372 gbeauche 1.15 static void wait_mapped(Window w)
373 gbeauche 1.13 {
374     XEvent e;
375     do {
376     XMaskEvent(x_display, StructureNotifyMask, &e);
377     } while ((e.type != MapNotify) || (e.xmap.event != w));
378     }
379    
380 gbeauche 1.15 static void wait_unmapped(Window w)
381 gbeauche 1.13 {
382     XEvent e;
383     do {
384     XMaskEvent(x_display, StructureNotifyMask, &e);
385     } while ((e.type != UnmapNotify) || (e.xmap.event != w));
386     }
387    
388 cebix 1.1 // Trap SHM errors
389     static bool shm_error = false;
390     static int (*old_error_handler)(Display *, XErrorEvent *);
391    
392     static int error_handler(Display *d, XErrorEvent *e)
393     {
394     if (e->error_code == BadAccess) {
395     shm_error = true;
396     return 0;
397     } else
398     return old_error_handler(d, e);
399     }
400    
401     // Open window
402     static bool open_window(int width, int height)
403     {
404 gbeauche 1.3 int aligned_width = (width + 15) & ~15;
405     int aligned_height = (height + 15) & ~15;
406    
407 cebix 1.1 // Set absolute mouse mode
408     ADBSetRelMouseMode(false);
409    
410     // Create window
411     XSetWindowAttributes wattr;
412     wattr.event_mask = eventmask = win_eventmask;
413 gbeauche 1.13 wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
414     wattr.border_pixel = 0;
415 cebix 1.1 wattr.backing_store = NotUseful;
416 gbeauche 1.13 wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
417     the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
418     InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr);
419 cebix 1.1
420 gbeauche 1.13 // Set window name
421 cebix 1.1 XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
422    
423 gbeauche 1.14 // Set delete protocol property
424     set_window_delete_protocol(the_win);
425    
426 cebix 1.1 // Make window unresizable
427     XSizeHints *hints;
428     if ((hints = XAllocSizeHints()) != NULL) {
429     hints->min_width = width;
430     hints->max_width = width;
431     hints->min_height = height;
432     hints->max_height = height;
433     hints->flags = PMinSize | PMaxSize;
434     XSetWMNormalHints(x_display, the_win, hints);
435     XFree((char *)hints);
436     }
437    
438 gbeauche 1.13 // Show window
439     XMapWindow(x_display, the_win);
440     wait_mapped(the_win);
441    
442 gbeauche 1.5 // 1-bit mode is big-endian; if the X server is little-endian, we can't
443     // use SHM because that doesn't allow changing the image byte order
444     bool need_msb_image = (depth == 1 && XImageByteOrder(x_display) == LSBFirst);
445    
446 cebix 1.1 // Try to create and attach SHM image
447     have_shm = false;
448 gbeauche 1.5 if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) {
449 cebix 1.1
450     // Create SHM image ("height + 2" for safety)
451 gbeauche 1.5 img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
452 gbeauche 1.13 shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
453     D(bug(" shm image created\n"));
454 gbeauche 1.3 the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
455     shminfo.shmaddr = img->data = (char *)the_buffer_copy;
456 cebix 1.1 shminfo.readOnly = False;
457    
458     // Try to attach SHM image, catching errors
459     shm_error = false;
460     old_error_handler = XSetErrorHandler(error_handler);
461     XShmAttach(x_display, &shminfo);
462     XSync(x_display, false);
463     XSetErrorHandler(old_error_handler);
464     if (shm_error) {
465     shmdt(shminfo.shmaddr);
466     XDestroyImage(img);
467     shminfo.shmid = -1;
468     } else {
469     have_shm = true;
470     shmctl(shminfo.shmid, IPC_RMID, 0);
471     }
472 gbeauche 1.13 D(bug(" shm image attached\n"));
473 cebix 1.1 }
474    
475     // Create normal X image if SHM doesn't work ("height + 2" for safety)
476     if (!have_shm) {
477 gbeauche 1.13 int bytes_per_row = depth == 1 ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth));
478 gbeauche 1.3 the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
479     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);
480 gbeauche 1.13 D(bug(" X image created\n"));
481 cebix 1.1 }
482    
483     // 1-Bit mode is big-endian
484 gbeauche 1.13 if (need_msb_image) {
485 cebix 1.1 img->byte_order = MSBFirst;
486     img->bitmap_bit_order = MSBFirst;
487     }
488    
489 gbeauche 1.3 #ifdef ENABLE_VOSF
490     use_vosf = true;
491     // Allocate memory for frame buffer (SIZE is extended to page-boundary)
492     the_host_buffer = the_buffer_copy;
493     the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
494     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
495     the_buffer_copy = (uint8 *)malloc(the_buffer_size);
496     D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
497     #else
498     // Allocate memory for frame buffer
499     the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
500     D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
501     #endif
502     screen_base = (uint32)the_buffer;
503 cebix 1.1
504     // Create GC
505     the_gc = XCreateGC(x_display, the_win, 0, 0);
506 gbeauche 1.13 XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
507 cebix 1.1
508     // Create cursor
509 gbeauche 1.22 if (hw_mac_cursor_accl) {
510 gbeauche 1.21 cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
511     cursor_image->byte_order = MSBFirst;
512     cursor_image->bitmap_bit_order = MSBFirst;
513     cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 36, 16, 16, 16, 2);
514     cursor_mask_image->byte_order = MSBFirst;
515     cursor_mask_image->bitmap_bit_order = MSBFirst;
516     cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
517     cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
518     cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
519     cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
520     mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
521     cursor_changed = false;
522     }
523    
524     // Create no_cursor
525     else {
526     mac_cursor = XCreatePixmapCursor(x_display,
527     XCreatePixmap(x_display, the_win, 1, 1, 1),
528     XCreatePixmap(x_display, the_win, 1, 1, 1),
529     &black, &white, 0, 0);
530     XDefineCursor(x_display, the_win, mac_cursor);
531     }
532 cebix 1.1
533 gbeauche 1.3 // Init blitting routines
534     bool native_byte_order;
535     #ifdef WORDS_BIGENDIAN
536     native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
537     #else
538     native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
539     #endif
540     #ifdef ENABLE_VOSF
541     Screen_blitter_init(&visualInfo, native_byte_order, depth);
542     #endif
543    
544 cebix 1.1 // Set bytes per row
545     XSync(x_display, false);
546     return true;
547     }
548    
549     // Open DGA display (!! should use X11 VidMode extensions to set mode)
550     static bool open_dga(int width, int height)
551     {
552     #ifdef ENABLE_XF86_DGA
553     // Set relative mouse mode
554     ADBSetRelMouseMode(true);
555    
556 gbeauche 1.15 // Create window
557     XSetWindowAttributes wattr;
558     wattr.event_mask = eventmask = dga_eventmask;
559     wattr.override_redirect = True;
560     wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
561     the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
562     InputOutput, vis, CWEventMask | CWOverrideRedirect |
563     (color_class == DirectColor ? CWColormap : 0), &wattr);
564    
565     // Show window
566     XMapRaised(x_display, the_win);
567     wait_mapped(the_win);
568    
569 cebix 1.1 #ifdef ENABLE_XF86_VIDMODE
570     // Switch to best mode
571     if (has_vidmode) {
572     int best = 0;
573     for (int i=1; i<num_x_video_modes; i++) {
574     if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
575     x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
576     best = i;
577     }
578     }
579     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
580     XF86VidModeSetViewPort(x_display, screen, 0, 0);
581     }
582     #endif
583    
584     // Establish direct screen connection
585 gbeauche 1.15 XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
586     XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
587 cebix 1.1 XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
588     XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
589 gbeauche 1.15
590     int v_width, v_bank, v_size;
591     XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
592 cebix 1.1 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
593     XF86DGASetViewPort(x_display, screen, 0, 0);
594     XF86DGASetVidPage(x_display, screen, 0);
595    
596     // Set colormap
597 gbeauche 1.15 if (!IsDirectMode(get_current_mode())) {
598     XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
599 cebix 1.1 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
600 gbeauche 1.15 }
601     XSync(x_display, false);
602 cebix 1.1
603 gbeauche 1.15 // Init blitting routines
604     int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, DepthModeForPixelDepth(depth));
605 gbeauche 1.3 #if ENABLE_VOSF
606     bool native_byte_order;
607     #ifdef WORDS_BIGENDIAN
608     native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
609     #else
610     native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
611     #endif
612     #if REAL_ADDRESSING || DIRECT_ADDRESSING
613     // Screen_blitter_init() returns TRUE if VOSF is mandatory
614     // i.e. the framebuffer update function is not Blit_Copy_Raw
615     use_vosf = Screen_blitter_init(&visualInfo, native_byte_order, depth);
616    
617     if (use_vosf) {
618     // Allocate memory for frame buffer (SIZE is extended to page-boundary)
619     the_host_buffer = the_buffer;
620     the_buffer_size = page_extend((height + 2) * bytes_per_row);
621     the_buffer_copy = (uint8 *)malloc(the_buffer_size);
622     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
623 gbeauche 1.15 D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
624 gbeauche 1.3 }
625     #else
626     use_vosf = false;
627     #endif
628     #endif
629 gbeauche 1.15
630     // Set frame buffer base
631     D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
632 gbeauche 1.3 screen_base = (uint32)the_buffer;
633 cebix 1.1 VModes[cur_mode].viRowBytes = bytes_per_row;
634     return true;
635     #else
636     ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
637     return false;
638     #endif
639     }
640    
641     static bool open_display(void)
642     {
643 gbeauche 1.13 D(bug("open_display()\n"));
644     const VideoInfo &mode = VModes[cur_mode];
645    
646     // Find best available X visual
647     if (!find_visual_for_depth(mode.viAppleMode)) {
648     ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
649     return false;
650     }
651    
652     // Create color maps
653     if (color_class == PseudoColor || color_class == DirectColor) {
654     cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
655     cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
656     } else {
657     cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
658     cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
659     }
660    
661     // Find pixel format of direct modes
662     if (color_class == DirectColor || color_class == TrueColor) {
663     rshift = gshift = bshift = 0;
664     rloss = gloss = bloss = 8;
665     uint32 mask;
666     for (mask=vis->red_mask; !(mask&1); mask>>=1)
667     ++rshift;
668     for (; mask&1; mask>>=1)
669     --rloss;
670     for (mask=vis->green_mask; !(mask&1); mask>>=1)
671     ++gshift;
672     for (; mask&1; mask>>=1)
673     --gloss;
674     for (mask=vis->blue_mask; !(mask&1); mask>>=1)
675     ++bshift;
676     for (; mask&1; mask>>=1)
677     --bloss;
678     }
679    
680     // Preset palette pixel values for CLUT or gamma table
681     if (color_class == DirectColor) {
682     int num = vis->map_entries;
683     for (int i=0; i<num; i++) {
684     int c = (i * 256) / num;
685     x_palette[i].pixel = map_rgb(c, c, c);
686     x_palette[i].flags = DoRed | DoGreen | DoBlue;
687     }
688     } else if (color_class == PseudoColor) {
689     for (int i=0; i<256; i++) {
690     x_palette[i].pixel = i;
691     x_palette[i].flags = DoRed | DoGreen | DoBlue;
692     }
693     }
694    
695     // Load gray ramp to color map
696     int num = (color_class == DirectColor ? vis->map_entries : 256);
697     for (int i=0; i<num; i++) {
698     int c = (i * 256) / num;
699     x_palette[i].red = c * 0x0101;
700     x_palette[i].green = c * 0x0101;
701     x_palette[i].blue = c * 0x0101;
702     }
703     if (color_class == PseudoColor || color_class == DirectColor) {
704     XStoreColors(x_display, cmap[0], x_palette, num);
705     XStoreColors(x_display, cmap[1], x_palette, num);
706 cebix 1.1 }
707 gbeauche 1.3
708 gbeauche 1.13 #ifdef ENABLE_VOSF
709     // Load gray ramp to 8->16/32 expand map
710     if (!IsDirectMode(get_current_mode()) && xdepth > 8)
711     for (int i=0; i<256; i++)
712     ExpandMap[i] = map_rgb(i, i, i);
713     #endif
714    
715     // Create display of requested type
716     display_type = mode.viType;
717     depth = depth_of_video_mode(mode.viAppleMode);
718    
719 gbeauche 1.3 bool display_open = false;
720 cebix 1.1 if (display_type == DIS_SCREEN)
721 gbeauche 1.3 display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
722 cebix 1.1 else if (display_type == DIS_WINDOW)
723 gbeauche 1.3 display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
724    
725     #ifdef ENABLE_VOSF
726     if (use_vosf) {
727     // Initialize the VOSF system
728     if (!video_vosf_init()) {
729     ErrorAlert(GetString(STR_VOSF_INIT_ERR));
730     return false;
731     }
732     }
733     #endif
734    
735     return display_open;
736 cebix 1.1 }
737    
738    
739     /*
740     * Close display
741     */
742    
743     // Close window
744     static void close_window(void)
745     {
746 gbeauche 1.3 if (have_shm) {
747     XShmDetach(x_display, &shminfo);
748     #ifdef ENABLE_VOSF
749     the_host_buffer = NULL; // don't free() in driver_base dtor
750     #else
751     the_buffer_copy = NULL; // don't free() in driver_base dtor
752     #endif
753     }
754     if (img) {
755     if (!have_shm)
756     img->data = NULL;
757     XDestroyImage(img);
758     }
759     if (have_shm) {
760     shmdt(shminfo.shmaddr);
761     shmctl(shminfo.shmid, IPC_RMID, 0);
762     }
763     if (the_gc)
764     XFreeGC(x_display, the_gc);
765    
766 gbeauche 1.13 XFlush(x_display);
767     XSync(x_display, false);
768 cebix 1.1 }
769    
770     // Close DGA mode
771     static void close_dga(void)
772     {
773     #ifdef ENABLE_XF86_DGA
774     XF86DGADirectVideo(x_display, screen, 0);
775     XUngrabPointer(x_display, CurrentTime);
776     XUngrabKeyboard(x_display, CurrentTime);
777     #endif
778    
779     #ifdef ENABLE_XF86_VIDMODE
780     if (has_vidmode)
781     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
782     #endif
783 gbeauche 1.3
784     if (!use_vosf) {
785     // don't free() the screen buffer in driver_base dtor
786     the_buffer = NULL;
787     }
788     #ifdef ENABLE_VOSF
789     else {
790     // don't free() the screen buffer in driver_base dtor
791     the_host_buffer = NULL;
792     }
793     #endif
794 cebix 1.1 }
795    
796     static void close_display(void)
797     {
798     if (display_type == DIS_SCREEN)
799     close_dga();
800     else if (display_type == DIS_WINDOW)
801     close_window();
802 gbeauche 1.3
803 gbeauche 1.15 // Close window
804     if (the_win) {
805     XUnmapWindow(x_display, the_win);
806     wait_unmapped(the_win);
807     XDestroyWindow(x_display, the_win);
808     }
809    
810 gbeauche 1.13 // Free colormaps
811     if (cmap[0]) {
812     XFreeColormap(x_display, cmap[0]);
813     cmap[0] = 0;
814     }
815     if (cmap[1]) {
816     XFreeColormap(x_display, cmap[1]);
817     cmap[1] = 0;
818     }
819    
820 gbeauche 1.3 #ifdef ENABLE_VOSF
821     if (use_vosf) {
822     // Deinitialize VOSF
823     video_vosf_exit();
824     }
825     #endif
826    
827     // Free frame buffer(s)
828     if (!use_vosf) {
829     if (the_buffer_copy) {
830     free(the_buffer_copy);
831     the_buffer_copy = NULL;
832     }
833     }
834     #ifdef ENABLE_VOSF
835     else {
836     // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
837     if (the_buffer != VM_MAP_FAILED) {
838     D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
839     vm_release(the_buffer, the_buffer_size);
840     the_buffer = NULL;
841     }
842     if (the_host_buffer) {
843     D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
844     free(the_host_buffer);
845     the_host_buffer = NULL;
846     }
847     if (the_buffer_copy) {
848     D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
849     free(the_buffer_copy);
850     the_buffer_copy = NULL;
851     }
852     }
853     #endif
854 cebix 1.1 }
855    
856    
857     /*
858     * Initialization
859     */
860    
861 gbeauche 1.6 // Init keycode translation table
862     static void keycode_init(void)
863     {
864     bool use_kc = PrefsFindBool("keycodes");
865     if (use_kc) {
866    
867     // Get keycode file path from preferences
868     const char *kc_path = PrefsFindString("keycodefile");
869    
870     // Open keycode table
871     FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
872     if (f == NULL) {
873     char str[256];
874     sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
875     WarningAlert(str);
876     return;
877     }
878    
879     // Default translation table
880     for (int i=0; i<256; i++)
881     keycode_table[i] = -1;
882    
883     // Search for server vendor string, then read keycodes
884     const char *vendor = ServerVendor(x_display);
885     bool vendor_found = false;
886     char line[256];
887     while (fgets(line, 255, f)) {
888     // Read line
889     int len = strlen(line);
890     if (len == 0)
891     continue;
892     line[len-1] = 0;
893    
894     // Comments begin with "#" or ";"
895     if (line[0] == '#' || line[0] == ';' || line[0] == 0)
896     continue;
897    
898     if (vendor_found) {
899     // Read keycode
900     int x_code, mac_code;
901     if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
902     keycode_table[x_code & 0xff] = mac_code;
903     else
904     break;
905     } else {
906     // Search for vendor string
907     if (strstr(vendor, line) == vendor)
908     vendor_found = true;
909     }
910     }
911    
912     // Keycode file completely read
913     fclose(f);
914     use_keycodes = vendor_found;
915    
916     // Vendor not found? Then display warning
917     if (!vendor_found) {
918     char str[256];
919     sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME);
920     WarningAlert(str);
921     return;
922     }
923     }
924     }
925    
926 gbeauche 1.15 // Find Apple mode matching best specified dimensions
927     static int find_apple_resolution(int xsize, int ysize)
928     {
929     int apple_id;
930     if (xsize < 800)
931     apple_id = APPLE_640x480;
932     else if (xsize < 1024)
933     apple_id = APPLE_800x600;
934     else if (xsize < 1152)
935     apple_id = APPLE_1024x768;
936     else if (xsize < 1280) {
937     if (ysize < 900)
938     apple_id = APPLE_1152x768;
939     else
940     apple_id = APPLE_1152x900;
941     }
942     else if (xsize < 1600)
943     apple_id = APPLE_1280x1024;
944     else
945     apple_id = APPLE_1600x1200;
946     return apple_id;
947     }
948    
949     // Find mode in list of supported modes
950     static int find_mode(int apple_mode, int apple_id, int type)
951     {
952     for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
953     if (p->viType == type && p->viAppleID == apple_id && p->viAppleMode == apple_mode)
954     return p - VModes;
955     }
956     return -1;
957     }
958    
959 gbeauche 1.13 // Add mode to list of supported modes
960     static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
961 cebix 1.1 {
962     if (allow & test) {
963     p->viType = type;
964     switch (apple_id) {
965     case APPLE_W_640x480:
966     case APPLE_640x480:
967     p->viXsize = 640;
968     p->viYsize = 480;
969     break;
970     case APPLE_W_800x600:
971     case APPLE_800x600:
972     p->viXsize = 800;
973     p->viYsize = 600;
974     break;
975     case APPLE_1024x768:
976     p->viXsize = 1024;
977     p->viYsize = 768;
978     break;
979 gbeauche 1.15 case APPLE_1152x768:
980     p->viXsize = 1152;
981     p->viYsize = 768;
982     break;
983 cebix 1.1 case APPLE_1152x900:
984     p->viXsize = 1152;
985     p->viYsize = 900;
986     break;
987     case APPLE_1280x1024:
988     p->viXsize = 1280;
989     p->viYsize = 1024;
990     break;
991     case APPLE_1600x1200:
992     p->viXsize = 1600;
993     p->viYsize = 1200;
994     break;
995     }
996 gbeauche 1.13 p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
997 cebix 1.1 p->viAppleMode = apple_mode;
998     p->viAppleID = apple_id;
999     p++;
1000     }
1001     }
1002    
1003 gbeauche 1.13 // Add standard list of windowed modes for given color depth
1004     static void add_window_modes(VideoInfo *&p, int window_modes, int mode)
1005     {
1006     add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);
1007     add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);
1008     }
1009    
1010 cebix 1.1 static bool has_mode(int x, int y)
1011     {
1012     #ifdef ENABLE_XF86_VIDMODE
1013     for (int i=0; i<num_x_video_modes; i++)
1014     if (x_video_modes[i]->hdisplay >= x && x_video_modes[i]->vdisplay >= y)
1015     return true;
1016     return false;
1017     #else
1018     return DisplayWidth(x_display, screen) >= x && DisplayHeight(x_display, screen) >= y;
1019     #endif
1020     }
1021    
1022     bool VideoInit(void)
1023     {
1024 gbeauche 1.3 #ifdef ENABLE_VOSF
1025     // Zero the mainBuffer structure
1026     mainBuffer.dirtyPages = NULL;
1027     mainBuffer.pageInfo = NULL;
1028     #endif
1029    
1030 gbeauche 1.4 // Check if X server runs on local machine
1031     local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
1032     || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
1033    
1034 gbeauche 1.6 // Init keycode translation
1035     keycode_init();
1036    
1037 gbeauche 1.8 // Read frame skip prefs
1038     frame_skip = PrefsFindInt32("frameskip");
1039     if (frame_skip == 0)
1040     frame_skip = 1;
1041    
1042     // Read mouse wheel prefs
1043     mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
1044     mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1045    
1046 cebix 1.1 // Init variables
1047     private_data = NULL;
1048     video_activated = true;
1049    
1050     // Find screen and root window
1051     screen = XDefaultScreen(x_display);
1052     rootwin = XRootWindow(x_display, screen);
1053    
1054 gbeauche 1.13 // Get sorted list of available depths
1055     avail_depths = XListDepths(x_display, screen, &num_depths);
1056     if (avail_depths == NULL) {
1057     ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
1058     return false;
1059     }
1060     sort(avail_depths, avail_depths + num_depths);
1061    
1062 cebix 1.1 // Get screen depth
1063     xdepth = DefaultDepth(x_display, screen);
1064    
1065     #ifdef ENABLE_XF86_DGA
1066     // DGA available?
1067     int event_base, error_base;
1068 gbeauche 1.4 if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
1069 cebix 1.1 int dga_flags = 0;
1070     XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1071     has_dga = dga_flags & XF86DGADirectPresent;
1072     } else
1073     has_dga = false;
1074     #endif
1075    
1076     #ifdef ENABLE_XF86_VIDMODE
1077     // VidMode available?
1078     int vm_event_base, vm_error_base;
1079     has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
1080     if (has_vidmode)
1081     XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1082     #endif
1083    
1084     // Find black and white colors
1085     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
1086     XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
1087     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white);
1088     XAllocColor(x_display, DefaultColormap(x_display, screen), &white);
1089     black_pixel = BlackPixel(x_display, screen);
1090     white_pixel = WhitePixel(x_display, screen);
1091    
1092     // Mac screen depth follows X depth (for now)
1093 gbeauche 1.13 int default_mode = APPLE_8_BIT;
1094     switch (DefaultDepth(x_display, screen)) {
1095     case 1:
1096     default_mode = APPLE_1_BIT;
1097     break;
1098     case 8:
1099     default_mode = APPLE_8_BIT;
1100     break;
1101     case 15: case 16:
1102     default_mode = APPLE_16_BIT;
1103     break;
1104     case 24: case 32:
1105     default_mode = APPLE_32_BIT;
1106     break;
1107 cebix 1.1 }
1108    
1109     // Construct video mode table
1110     uint32 window_modes = PrefsFindInt32("windowmodes");
1111     uint32 screen_modes = PrefsFindInt32("screenmodes");
1112     if (!has_dga)
1113     screen_modes = 0;
1114     if (window_modes == 0 && screen_modes == 0)
1115     window_modes |= 3; // Allow at least 640x480 and 800x600 window modes
1116    
1117     VideoInfo *p = VModes;
1118 gbeauche 1.13 for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1119     if (find_visual_for_depth(d))
1120     add_window_modes(p, window_modes, d);
1121    
1122 cebix 1.1 if (has_vidmode) {
1123     if (has_mode(640, 480))
1124 gbeauche 1.13 add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1125 cebix 1.1 if (has_mode(800, 600))
1126 gbeauche 1.13 add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN);
1127 cebix 1.1 if (has_mode(1024, 768))
1128 gbeauche 1.13 add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN);
1129 gbeauche 1.15 if (has_mode(1152, 768))
1130     add_mode(p, screen_modes, 64, default_mode, APPLE_1152x768, DIS_SCREEN);
1131 cebix 1.1 if (has_mode(1152, 900))
1132 gbeauche 1.13 add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN);
1133 cebix 1.1 if (has_mode(1280, 1024))
1134 gbeauche 1.13 add_mode(p, screen_modes, 16, default_mode, APPLE_1280x1024, DIS_SCREEN);
1135 cebix 1.1 if (has_mode(1600, 1200))
1136 gbeauche 1.13 add_mode(p, screen_modes, 32, default_mode, APPLE_1600x1200, DIS_SCREEN);
1137 cebix 1.1 } else if (screen_modes) {
1138     int xsize = DisplayWidth(x_display, screen);
1139     int ysize = DisplayHeight(x_display, screen);
1140 gbeauche 1.15 int apple_id = find_apple_resolution(xsize, ysize);
1141 cebix 1.1 p->viType = DIS_SCREEN;
1142     p->viRowBytes = 0;
1143     p->viXsize = xsize;
1144     p->viYsize = ysize;
1145 gbeauche 1.13 p->viAppleMode = default_mode;
1146 cebix 1.1 p->viAppleID = apple_id;
1147     p++;
1148     }
1149     p->viType = DIS_INVALID; // End marker
1150     p->viRowBytes = 0;
1151     p->viXsize = p->viYsize = 0;
1152     p->viAppleMode = 0;
1153     p->viAppleID = 0;
1154    
1155 gbeauche 1.13 // Find default mode (window 640x480)
1156     cur_mode = -1;
1157 gbeauche 1.15 if (has_dga && screen_modes) {
1158     int screen_width = DisplayWidth(x_display, screen);
1159     int screen_height = DisplayHeight(x_display, screen);
1160     int apple_id = find_apple_resolution(screen_width, screen_height);
1161     if (apple_id != -1)
1162     cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN);
1163 gbeauche 1.13 }
1164 gbeauche 1.25 if (cur_mode == -1) {
1165     // pick up first windowed mode available
1166     for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
1167     if (p->viType == DIS_WINDOW && p->viAppleMode == default_mode) {
1168     cur_mode = p - VModes;
1169     break;
1170     }
1171     }
1172     }
1173 gbeauche 1.13 assert(cur_mode != -1);
1174    
1175     #if DEBUG
1176     D(bug("Available video modes:\n"));
1177     for (p = VModes; p->viType != DIS_INVALID; p++) {
1178     int bits = depth_of_video_mode(p->viAppleMode);
1179     D(bug(" %dx%d (ID %02x), %d colors\n", p->viXsize, p->viYsize, p->viAppleID, 1 << bits));
1180     }
1181     #endif
1182    
1183 cebix 1.1 // Open window/screen
1184     if (!open_display())
1185     return false;
1186    
1187     #if 0
1188     // Ignore errors from now on
1189     XSetErrorHandler(ignore_errors);
1190     #endif
1191    
1192     // Start periodic thread
1193     XSync(x_display, false);
1194 gbeauche 1.15 Set_pthread_attr(&redraw_thread_attr, 0);
1195     redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1196 cebix 1.1 D(bug("Redraw thread installed (%ld)\n", redraw_thread));
1197     return true;
1198     }
1199    
1200    
1201     /*
1202     * Deinitialization
1203     */
1204    
1205     void VideoExit(void)
1206     {
1207     // Stop redraw thread
1208     if (redraw_thread_active) {
1209     pthread_cancel(redraw_thread);
1210     pthread_join(redraw_thread, NULL);
1211     redraw_thread_active = false;
1212     }
1213    
1214 gbeauche 1.3 #ifdef ENABLE_VOSF
1215     if (use_vosf) {
1216     // Deinitialize VOSF
1217     video_vosf_exit();
1218     }
1219     #endif
1220    
1221 cebix 1.1 // Close window and server connection
1222     if (x_display != NULL) {
1223     XSync(x_display, false);
1224     close_display();
1225     XFlush(x_display);
1226     XSync(x_display, false);
1227     }
1228     }
1229    
1230    
1231     /*
1232     * Suspend/resume emulator
1233     */
1234    
1235     extern void PauseEmulator(void);
1236     extern void ResumeEmulator(void);
1237    
1238     static void suspend_emul(void)
1239     {
1240     if (display_type == DIS_SCREEN) {
1241     // Release ctrl key
1242     ADBKeyUp(0x36);
1243     ctrl_down = false;
1244    
1245     // Pause MacOS thread
1246     PauseEmulator();
1247     emul_suspended = true;
1248    
1249     // Save frame buffer
1250     fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1251     if (fb_save)
1252     memcpy(fb_save, (void *)screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1253    
1254     // Close full screen display
1255     #ifdef ENABLE_XF86_DGA
1256     XF86DGADirectVideo(x_display, screen, 0);
1257     XUngrabPointer(x_display, CurrentTime);
1258     XUngrabKeyboard(x_display, CurrentTime);
1259     #endif
1260     XSync(x_display, false);
1261    
1262     // Open "suspend" window
1263     XSetWindowAttributes wattr;
1264     wattr.event_mask = KeyPressMask;
1265     wattr.background_pixel = black_pixel;
1266     wattr.border_pixel = black_pixel;
1267     wattr.backing_store = Always;
1268     wattr.backing_planes = xdepth;
1269     wattr.colormap = DefaultColormap(x_display, screen);
1270     XSync(x_display, false);
1271     suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1272     InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1273     CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1274     XSync(x_display, false);
1275     XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1276     XMapRaised(x_display, suspend_win);
1277     XSync(x_display, false);
1278     }
1279     }
1280    
1281     static void resume_emul(void)
1282     {
1283     // Close "suspend" window
1284     XDestroyWindow(x_display, suspend_win);
1285     XSync(x_display, false);
1286    
1287     // Reopen full screen display
1288     XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1289     XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1290 gbeauche 1.12 #ifdef ENABLE_XF86_DGA
1291 cebix 1.1 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1292     XF86DGASetViewPort(x_display, screen, 0, 0);
1293 gbeauche 1.12 #endif
1294 cebix 1.1 XSync(x_display, false);
1295    
1296 gbeauche 1.3 // the_buffer already contains the data to restore. i.e. since a temporary
1297     // frame buffer is used when VOSF is actually used, fb_save is therefore
1298     // not necessary.
1299     #ifdef ENABLE_VOSF
1300     if (use_vosf) {
1301     LOCK_VOSF;
1302     PFLAG_SET_ALL;
1303     UNLOCK_VOSF;
1304     memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1305     }
1306     #endif
1307    
1308 cebix 1.1 // Restore frame buffer
1309     if (fb_save) {
1310 gbeauche 1.3 #ifdef ENABLE_VOSF
1311     // Don't copy fb_save to the temporary frame buffer in VOSF mode
1312     if (!use_vosf)
1313     #endif
1314 cebix 1.1 memcpy((void *)screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1315     free(fb_save);
1316     fb_save = NULL;
1317     }
1318     if (depth == 8)
1319     palette_changed = true;
1320    
1321     // Resume MacOS thread
1322     emul_suspended = false;
1323     ResumeEmulator();
1324     }
1325    
1326    
1327     /*
1328     * Close screen in full-screen mode
1329     */
1330    
1331     void VideoQuitFullScreen(void)
1332     {
1333     D(bug("VideoQuitFullScreen()\n"));
1334     if (display_type == DIS_SCREEN) {
1335     quit_full_screen = true;
1336     while (!quit_full_screen_ack) ;
1337     }
1338     }
1339    
1340    
1341     /*
1342     * X11 event handling
1343     */
1344    
1345     // Translate key event to Mac keycode
1346     static int kc_decode(KeySym ks)
1347     {
1348     switch (ks) {
1349     case XK_A: case XK_a: return 0x00;
1350     case XK_B: case XK_b: return 0x0b;
1351     case XK_C: case XK_c: return 0x08;
1352     case XK_D: case XK_d: return 0x02;
1353     case XK_E: case XK_e: return 0x0e;
1354     case XK_F: case XK_f: return 0x03;
1355     case XK_G: case XK_g: return 0x05;
1356     case XK_H: case XK_h: return 0x04;
1357     case XK_I: case XK_i: return 0x22;
1358     case XK_J: case XK_j: return 0x26;
1359     case XK_K: case XK_k: return 0x28;
1360     case XK_L: case XK_l: return 0x25;
1361     case XK_M: case XK_m: return 0x2e;
1362     case XK_N: case XK_n: return 0x2d;
1363     case XK_O: case XK_o: return 0x1f;
1364     case XK_P: case XK_p: return 0x23;
1365     case XK_Q: case XK_q: return 0x0c;
1366     case XK_R: case XK_r: return 0x0f;
1367     case XK_S: case XK_s: return 0x01;
1368     case XK_T: case XK_t: return 0x11;
1369     case XK_U: case XK_u: return 0x20;
1370     case XK_V: case XK_v: return 0x09;
1371     case XK_W: case XK_w: return 0x0d;
1372     case XK_X: case XK_x: return 0x07;
1373     case XK_Y: case XK_y: return 0x10;
1374     case XK_Z: case XK_z: return 0x06;
1375    
1376     case XK_1: case XK_exclam: return 0x12;
1377     case XK_2: case XK_at: return 0x13;
1378     case XK_3: case XK_numbersign: return 0x14;
1379     case XK_4: case XK_dollar: return 0x15;
1380     case XK_5: case XK_percent: return 0x17;
1381     case XK_6: return 0x16;
1382     case XK_7: return 0x1a;
1383     case XK_8: return 0x1c;
1384     case XK_9: return 0x19;
1385     case XK_0: return 0x1d;
1386    
1387     case XK_grave: case XK_asciitilde: return 0x0a;
1388     case XK_minus: case XK_underscore: return 0x1b;
1389     case XK_equal: case XK_plus: return 0x18;
1390     case XK_bracketleft: case XK_braceleft: return 0x21;
1391     case XK_bracketright: case XK_braceright: return 0x1e;
1392     case XK_backslash: case XK_bar: return 0x2a;
1393     case XK_semicolon: case XK_colon: return 0x29;
1394     case XK_apostrophe: case XK_quotedbl: return 0x27;
1395     case XK_comma: case XK_less: return 0x2b;
1396     case XK_period: case XK_greater: return 0x2f;
1397     case XK_slash: case XK_question: return 0x2c;
1398    
1399     case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1400     case XK_Return: return 0x24;
1401     case XK_space: return 0x31;
1402     case XK_BackSpace: return 0x33;
1403    
1404     case XK_Delete: return 0x75;
1405     case XK_Insert: return 0x72;
1406     case XK_Home: case XK_Help: return 0x73;
1407     case XK_End: return 0x77;
1408     #ifdef __hpux
1409     case XK_Prior: return 0x74;
1410     case XK_Next: return 0x79;
1411     #else
1412     case XK_Page_Up: return 0x74;
1413     case XK_Page_Down: return 0x79;
1414     #endif
1415    
1416     case XK_Control_L: return 0x36;
1417     case XK_Control_R: return 0x36;
1418     case XK_Shift_L: return 0x38;
1419     case XK_Shift_R: return 0x38;
1420     case XK_Alt_L: return 0x37;
1421     case XK_Alt_R: return 0x37;
1422     case XK_Meta_L: return 0x3a;
1423     case XK_Meta_R: return 0x3a;
1424     case XK_Menu: return 0x32;
1425     case XK_Caps_Lock: return 0x39;
1426     case XK_Num_Lock: return 0x47;
1427    
1428     case XK_Up: return 0x3e;
1429     case XK_Down: return 0x3d;
1430     case XK_Left: return 0x3b;
1431     case XK_Right: return 0x3c;
1432    
1433     case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
1434    
1435     case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
1436     case XK_F2: return 0x78;
1437     case XK_F3: return 0x63;
1438     case XK_F4: return 0x76;
1439     case XK_F5: return 0x60;
1440     case XK_F6: return 0x61;
1441     case XK_F7: return 0x62;
1442     case XK_F8: return 0x64;
1443     case XK_F9: return 0x65;
1444     case XK_F10: return 0x6d;
1445     case XK_F11: return 0x67;
1446     case XK_F12: return 0x6f;
1447    
1448     case XK_Print: return 0x69;
1449     case XK_Scroll_Lock: return 0x6b;
1450     case XK_Pause: return 0x71;
1451    
1452     #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
1453     case XK_KP_0: case XK_KP_Insert: return 0x52;
1454     case XK_KP_1: case XK_KP_End: return 0x53;
1455     case XK_KP_2: case XK_KP_Down: return 0x54;
1456     case XK_KP_3: case XK_KP_Next: return 0x55;
1457     case XK_KP_4: case XK_KP_Left: return 0x56;
1458     case XK_KP_5: case XK_KP_Begin: return 0x57;
1459     case XK_KP_6: case XK_KP_Right: return 0x58;
1460     case XK_KP_7: case XK_KP_Home: return 0x59;
1461     case XK_KP_8: case XK_KP_Up: return 0x5b;
1462     case XK_KP_9: case XK_KP_Prior: return 0x5c;
1463     case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
1464     #else
1465     case XK_KP_0: return 0x52;
1466     case XK_KP_1: return 0x53;
1467     case XK_KP_2: return 0x54;
1468     case XK_KP_3: return 0x55;
1469     case XK_KP_4: return 0x56;
1470     case XK_KP_5: return 0x57;
1471     case XK_KP_6: return 0x58;
1472     case XK_KP_7: return 0x59;
1473     case XK_KP_8: return 0x5b;
1474     case XK_KP_9: return 0x5c;
1475     case XK_KP_Decimal: return 0x41;
1476     #endif
1477     case XK_KP_Add: return 0x45;
1478     case XK_KP_Subtract: return 0x4e;
1479     case XK_KP_Multiply: return 0x43;
1480     case XK_KP_Divide: return 0x4b;
1481     case XK_KP_Enter: return 0x4c;
1482     case XK_KP_Equal: return 0x51;
1483     }
1484     return -1;
1485     }
1486    
1487 gbeauche 1.6 static int event2keycode(XKeyEvent &ev)
1488 cebix 1.1 {
1489     KeySym ks;
1490     int as;
1491     int i = 0;
1492    
1493     do {
1494 gbeauche 1.6 ks = XLookupKeysym(&ev, i++);
1495 cebix 1.1 as = kc_decode(ks);
1496     if (as != -1)
1497     return as;
1498     } while (ks != NoSymbol);
1499    
1500     return -1;
1501     }
1502    
1503     static void handle_events(void)
1504     {
1505     // Handle events
1506     for (;;) {
1507     XEvent event;
1508    
1509 gbeauche 1.10 XDisplayLock();
1510 gbeauche 1.9 if (!XCheckMaskEvent(x_display, eventmask, &event)) {
1511     // Handle clipboard events
1512     if (XCheckTypedEvent(x_display, SelectionRequest, &event))
1513     ClipboardSelectionRequest(&event.xselectionrequest);
1514     else if (XCheckTypedEvent(x_display, SelectionClear, &event))
1515     ClipboardSelectionClear(&event.xselectionclear);
1516 gbeauche 1.14
1517     // Window "close" widget clicked
1518     else if (XCheckTypedEvent(x_display, ClientMessage, &event)) {
1519     if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
1520     ADBKeyDown(0x7f); // Power key
1521     ADBKeyUp(0x7f);
1522     }
1523     }
1524 gbeauche 1.10
1525     XDisplayUnlock();
1526 cebix 1.1 break;
1527 gbeauche 1.9 }
1528 gbeauche 1.10 XDisplayUnlock();
1529 cebix 1.1
1530     switch (event.type) {
1531     // Mouse button
1532     case ButtonPress: {
1533     unsigned int button = ((XButtonEvent *)&event)->button;
1534     if (button < 4)
1535     ADBMouseDown(button - 1);
1536 gbeauche 1.8 else if (button < 6) { // Wheel mouse
1537     if (mouse_wheel_mode == 0) {
1538     int key = (button == 5) ? 0x79 : 0x74; // Page up/down
1539     ADBKeyDown(key);
1540     ADBKeyUp(key);
1541     } else {
1542     int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down
1543     for(int i=0; i<mouse_wheel_lines; i++) {
1544     ADBKeyDown(key);
1545     ADBKeyUp(key);
1546     }
1547     }
1548     }
1549 cebix 1.1 break;
1550     }
1551     case ButtonRelease: {
1552     unsigned int button = ((XButtonEvent *)&event)->button;
1553     if (button < 4)
1554     ADBMouseUp(button - 1);
1555     break;
1556     }
1557    
1558 gbeauche 1.15 // Mouse entered window
1559 cebix 1.1 case EnterNotify:
1560 gbeauche 1.15 if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
1561     ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1562 cebix 1.1 break;
1563 gbeauche 1.15
1564     // Mouse moved
1565 cebix 1.1 case MotionNotify:
1566 gbeauche 1.15 ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1567 cebix 1.1 break;
1568    
1569     // Keyboard
1570     case KeyPress: {
1571 gbeauche 1.6 int code = event2keycode(event.xkey);
1572     if (use_keycodes && code != -1)
1573     code = keycode_table[event.xkey.keycode & 0xff];
1574     if (code != -1) {
1575 cebix 1.1 if (!emul_suspended) {
1576     ADBKeyDown(code);
1577     if (code == 0x36)
1578     ctrl_down = true;
1579     } else {
1580     if (code == 0x31)
1581     resume_emul(); // Space wakes us up
1582     }
1583     }
1584     break;
1585     }
1586     case KeyRelease: {
1587 gbeauche 1.6 int code = event2keycode(event.xkey);
1588     if (use_keycodes && code != 1)
1589     code = keycode_table[event.xkey.keycode & 0xff];
1590     if (code != -1) {
1591 cebix 1.1 ADBKeyUp(code);
1592     if (code == 0x36)
1593     ctrl_down = false;
1594     }
1595     break;
1596     }
1597    
1598     // Hidden parts exposed, force complete refresh
1599     case Expose:
1600 gbeauche 1.3 #ifdef ENABLE_VOSF
1601     if (use_vosf) { // VOSF refresh
1602     LOCK_VOSF;
1603     PFLAG_SET_ALL;
1604     UNLOCK_VOSF;
1605     }
1606     #endif
1607 cebix 1.1 memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1608     break;
1609     }
1610     }
1611     }
1612    
1613    
1614     /*
1615     * Execute video VBL routine
1616     */
1617    
1618     void VideoVBL(void)
1619     {
1620     if (emerg_quit)
1621     QuitEmulator();
1622    
1623     // Execute video VBL
1624     if (private_data != NULL && private_data->interruptsEnabled)
1625     VSLDoInterruptService(private_data->vslServiceID);
1626     }
1627    
1628    
1629     /*
1630     * Install graphics acceleration
1631     */
1632    
1633 gbeauche 1.18 // Rectangle inversion
1634     template< int bpp >
1635     static inline void do_invrect(uint8 *dest, uint32 length)
1636 cebix 1.1 {
1637 gbeauche 1.18 #define INVERT_1(PTR, OFS) ((uint8 *)(PTR))[OFS] = ~((uint8 *)(PTR))[OFS]
1638     #define INVERT_2(PTR, OFS) ((uint16 *)(PTR))[OFS] = ~((uint16 *)(PTR))[OFS]
1639     #define INVERT_4(PTR, OFS) ((uint32 *)(PTR))[OFS] = ~((uint32 *)(PTR))[OFS]
1640     #define INVERT_8(PTR, OFS) ((uint64 *)(PTR))[OFS] = ~((uint64 *)(PTR))[OFS]
1641    
1642     #ifndef UNALIGNED_PROFITABLE
1643     // Align on 16-bit boundaries
1644     if (bpp < 16 && (((uintptr)dest) & 1)) {
1645     INVERT_1(dest, 0);
1646     dest += 1; length -= 1;
1647     }
1648    
1649     // Align on 32-bit boundaries
1650     if (bpp < 32 && (((uintptr)dest) & 2)) {
1651     INVERT_2(dest, 0);
1652     dest += 2; length -= 2;
1653     }
1654     #endif
1655    
1656     // Invert 8-byte words
1657     if (length >= 8) {
1658     const int r = (length / 8) % 8;
1659     dest += r * 8;
1660    
1661     int n = ((length / 8) + 7) / 8;
1662     switch (r) {
1663     case 0: do {
1664     dest += 64;
1665     INVERT_8(dest, -8);
1666     case 7: INVERT_8(dest, -7);
1667     case 6: INVERT_8(dest, -6);
1668     case 5: INVERT_8(dest, -5);
1669     case 4: INVERT_8(dest, -4);
1670     case 3: INVERT_8(dest, -3);
1671     case 2: INVERT_8(dest, -2);
1672     case 1: INVERT_8(dest, -1);
1673     } while (--n > 0);
1674     }
1675     }
1676 cebix 1.1
1677 gbeauche 1.18 // 32-bit cell to invert?
1678     if (length & 4) {
1679     INVERT_4(dest, 0);
1680     if (bpp <= 16)
1681     dest += 4;
1682     }
1683    
1684     // 16-bit cell to invert?
1685     if (bpp <= 16 && (length & 2)) {
1686     INVERT_2(dest, 0);
1687     if (bpp <= 8)
1688     dest += 2;
1689     }
1690    
1691     // 8-bit cell to invert?
1692     if (bpp <= 8 && (length & 1))
1693     INVERT_1(dest, 0);
1694    
1695     #undef INVERT_1
1696     #undef INVERT_2
1697     #undef INVERT_4
1698     #undef INVERT_8
1699     }
1700    
1701 gbeauche 1.19 void NQD_invrect(uint32 p)
1702 gbeauche 1.18 {
1703 gbeauche 1.19 D(bug("accl_invrect %08x\n", p));
1704 gbeauche 1.18
1705     // Get inversion parameters
1706 gbeauche 1.19 int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1707     int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1708     int16 width = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1709     int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1710 cebix 1.1 D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1711 gbeauche 1.19 D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1712 cebix 1.1
1713 gbeauche 1.18 //!!?? pen_mode == 14
1714    
1715     // And perform the inversion
1716 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1717     const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1718 gbeauche 1.20 uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1719 gbeauche 1.18 width *= bpp;
1720     switch (bpp) {
1721     case 1:
1722     for (int i = 0; i < height; i++) {
1723     do_invrect<8>(dest, width);
1724     dest += dest_row_bytes;
1725     }
1726     break;
1727     case 2:
1728     for (int i = 0; i < height; i++) {
1729     do_invrect<16>(dest, width);
1730     dest += dest_row_bytes;
1731     }
1732     break;
1733     case 4:
1734     for (int i = 0; i < height; i++) {
1735     do_invrect<32>(dest, width);
1736     dest += dest_row_bytes;
1737     }
1738     break;
1739     }
1740 cebix 1.1 }
1741    
1742 gbeauche 1.18 // Rectangle filling
1743     template< int bpp >
1744     static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1745 cebix 1.1 {
1746 gbeauche 1.18 #define FILL_1(PTR, OFS, VAL) ((uint8 *)(PTR))[OFS] = (VAL)
1747     #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1748     #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1749     #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
1750    
1751     #ifndef UNALIGNED_PROFITABLE
1752     // Align on 16-bit boundaries
1753     if (bpp < 16 && (((uintptr)dest) & 1)) {
1754     FILL_1(dest, 0, color);
1755     dest += 1; length -= 1;
1756     }
1757    
1758     // Align on 32-bit boundaries
1759     if (bpp < 32 && (((uintptr)dest) & 2)) {
1760     FILL_2(dest, 0, color);
1761     dest += 2; length -= 2;
1762     }
1763     #endif
1764    
1765     // Fill 8-byte words
1766     if (length >= 8) {
1767     const uint64 c = (((uint64)color) << 32) | color;
1768     const int r = (length / 8) % 8;
1769     dest += r * 8;
1770    
1771     int n = ((length / 8) + 7) / 8;
1772     switch (r) {
1773     case 0: do {
1774     dest += 64;
1775     FILL_8(dest, -8, c);
1776     case 7: FILL_8(dest, -7, c);
1777     case 6: FILL_8(dest, -6, c);
1778     case 5: FILL_8(dest, -5, c);
1779     case 4: FILL_8(dest, -4, c);
1780     case 3: FILL_8(dest, -3, c);
1781     case 2: FILL_8(dest, -2, c);
1782     case 1: FILL_8(dest, -1, c);
1783     } while (--n > 0);
1784     }
1785     }
1786 cebix 1.1
1787 gbeauche 1.18 // 32-bit cell to fill?
1788     if (length & 4) {
1789     FILL_4(dest, 0, color);
1790     if (bpp <= 16)
1791     dest += 4;
1792     }
1793    
1794     // 16-bit cell to fill?
1795     if (bpp <= 16 && (length & 2)) {
1796     FILL_2(dest, 0, color);
1797     if (bpp <= 8)
1798     dest += 2;
1799     }
1800 cebix 1.1
1801 gbeauche 1.18 // 8-bit cell to fill?
1802     if (bpp <= 8 && (length & 1))
1803     FILL_1(dest, 0, color);
1804    
1805     #undef FILL_1
1806     #undef FILL_2
1807     #undef FILL_4
1808     #undef FILL_8
1809 cebix 1.1 }
1810    
1811 gbeauche 1.19 void NQD_fillrect(uint32 p)
1812 cebix 1.1 {
1813 gbeauche 1.19 D(bug("accl_fillrect %08x\n", p));
1814 cebix 1.1
1815 gbeauche 1.18 // Get filling parameters
1816 gbeauche 1.19 int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1817     int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1818     int16 width = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1819     int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1820 gbeauche 1.24 uint32 color = htonl(ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen));
1821 cebix 1.1 D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1822 gbeauche 1.18 D(bug(" width %d, height %d\n", width, height));
1823 gbeauche 1.19 D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1824 cebix 1.1
1825 gbeauche 1.18 // And perform the fill
1826 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1827     const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1828 gbeauche 1.20 uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1829 gbeauche 1.18 width *= bpp;
1830     switch (bpp) {
1831     case 1:
1832     for (int i = 0; i < height; i++) {
1833     memset(dest, color, width);
1834     dest += dest_row_bytes;
1835     }
1836     break;
1837     case 2:
1838     for (int i = 0; i < height; i++) {
1839     do_fillrect<16>(dest, color, width);
1840     dest += dest_row_bytes;
1841     }
1842     break;
1843     case 4:
1844     for (int i = 0; i < height; i++) {
1845     do_fillrect<32>(dest, color, width);
1846     dest += dest_row_bytes;
1847     }
1848     break;
1849     }
1850 cebix 1.1 }
1851    
1852 gbeauche 1.19 bool NQD_fillrect_hook(uint32 p)
1853 cebix 1.1 {
1854 gbeauche 1.19 D(bug("accl_fillrect_hook %08x\n", p));
1855 cebix 1.1
1856     // Check if we can accelerate this fillrect
1857 gbeauche 1.19 if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1858     const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1859     if (transfer_mode == 8) {
1860 cebix 1.1 // Fill
1861 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1862 gbeauche 1.18 return true;
1863     }
1864 gbeauche 1.19 else if (transfer_mode == 10) {
1865 cebix 1.1 // Invert
1866 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1867 cebix 1.1 return true;
1868     }
1869     }
1870     return false;
1871     }
1872    
1873 gbeauche 1.16 // Rectangle blitting
1874     // TODO: optimize for VOSF and target pixmap == screen
1875 gbeauche 1.19 void NQD_bitblt(uint32 p)
1876 gbeauche 1.16 {
1877 gbeauche 1.19 D(bug("accl_bitblt %08x\n", p));
1878 gbeauche 1.16
1879     // Get blitting parameters
1880 gbeauche 1.19 int16 src_X = (int16)ReadMacInt16(p + acclSrcRect + 2) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 2);
1881     int16 src_Y = (int16)ReadMacInt16(p + acclSrcRect + 0) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 0);
1882     int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1883     int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1884     int16 width = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1885     int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1886     D(bug(" src addr %08x, dest addr %08x\n", ReadMacInt32(p + acclSrcBaseAddr), ReadMacInt32(p + acclDestBaseAddr)));
1887 gbeauche 1.16 D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1888     D(bug(" width %d, height %d\n", width, height));
1889    
1890     // And perform the blit
1891 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1892 gbeauche 1.16 width *= bpp;
1893 gbeauche 1.19 if ((int32)ReadMacInt32(p + acclSrcRowBytes) > 0) {
1894     const int src_row_bytes = (int32)ReadMacInt32(p + acclSrcRowBytes);
1895     const int dst_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1896 gbeauche 1.20 uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + (src_Y * src_row_bytes) + (src_X * bpp));
1897     uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dst_row_bytes) + (dest_X * bpp));
1898 gbeauche 1.16 for (int i = 0; i < height; i++) {
1899 gbeauche 1.23 memmove(dst, src, width);
1900 gbeauche 1.16 src += src_row_bytes;
1901     dst += dst_row_bytes;
1902     }
1903     }
1904     else {
1905 gbeauche 1.19 const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1906     const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1907 gbeauche 1.20 uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp));
1908     uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp));
1909 gbeauche 1.16 for (int i = height - 1; i >= 0; i--) {
1910 gbeauche 1.23 memmove(dst, src, width);
1911 gbeauche 1.16 src -= src_row_bytes;
1912     dst -= dst_row_bytes;
1913     }
1914     }
1915     }
1916    
1917 gbeauche 1.18 /*
1918     BitBlt transfer modes:
1919     0 : srcCopy
1920     1 : srcOr
1921     2 : srcXor
1922     3 : srcBic
1923     4 : notSrcCopy
1924     5 : notSrcOr
1925     6 : notSrcXor
1926     7 : notSrcBic
1927     32 : blend
1928     33 : addPin
1929     34 : addOver
1930     35 : subPin
1931     36 : transparent
1932     37 : adMax
1933     38 : subOver
1934     39 : adMin
1935     50 : hilite
1936     */
1937    
1938 gbeauche 1.19 bool NQD_bitblt_hook(uint32 p)
1939 gbeauche 1.16 {
1940 gbeauche 1.19 D(bug("accl_draw_hook %08x\n", p));
1941 gbeauche 1.16
1942     // Check if we can accelerate this bitblt
1943 gbeauche 1.19 if (ReadMacInt32(p + 0x018) + ReadMacInt32(p + 0x128) == 0 &&
1944     ReadMacInt32(p + 0x130) == 0 &&
1945     ReadMacInt32(p + acclSrcPixelSize) >= 8 &&
1946     ReadMacInt32(p + acclSrcPixelSize) == ReadMacInt32(p + acclDestPixelSize) &&
1947     (ReadMacInt32(p + acclSrcRowBytes) ^ ReadMacInt32(p + acclDestRowBytes)) >= 0 && // same sign?
1948     ReadMacInt32(p + acclTransferMode) == 0 && // srcCopy?
1949     ReadMacInt32(p + 0x15c) > 0) {
1950 gbeauche 1.16
1951     // Yes, set function pointer
1952 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1953 gbeauche 1.16 return true;
1954     }
1955     return false;
1956     }
1957    
1958 cebix 1.1 // Wait for graphics operation to finish
1959 gbeauche 1.16 bool NQD_sync_hook(uint32 arg)
1960 cebix 1.1 {
1961 gbeauche 1.16 D(bug("accl_sync_hook %08x\n", arg));
1962 cebix 1.1 return true;
1963     }
1964    
1965     void VideoInstallAccel(void)
1966     {
1967     // Install acceleration hooks
1968     if (PrefsFindBool("gfxaccel")) {
1969     D(bug("Video: Installing acceleration hooks\n"));
1970 gbeauche 1.16 uint32 base;
1971    
1972     SheepVar bitblt_hook_info(sizeof(accl_hook_info));
1973     base = bitblt_hook_info.addr();
1974     WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK));
1975     WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1976     WriteMacInt32(base + 8, ACCL_BITBLT);
1977     NQDMisc(6, bitblt_hook_info.ptr());
1978    
1979 gbeauche 1.18 SheepVar fillrect_hook_info(sizeof(accl_hook_info));
1980     base = fillrect_hook_info.addr();
1981     WriteMacInt32(base + 0, NativeTVECT(NATIVE_FILLRECT_HOOK));
1982     WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1983     WriteMacInt32(base + 8, ACCL_FILLRECT);
1984     NQDMisc(6, fillrect_hook_info.ptr());
1985 cebix 1.1 }
1986     }
1987    
1988    
1989     /*
1990     * Change video mode
1991     */
1992    
1993     int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1994     {
1995     /* return if no mode change */
1996     if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1997     (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
1998    
1999     /* first find video mode in table */
2000     for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
2001     if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
2002     (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
2003     csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
2004     csSave->saveData = ReadMacInt32(ParamPtr + csData);
2005     csSave->savePage = ReadMacInt16(ParamPtr + csPage);
2006    
2007     // Disable interrupts and pause redraw thread
2008     DisableInterrupt();
2009     thread_stop_ack = false;
2010     thread_stop_req = true;
2011     while (!thread_stop_ack) ;
2012    
2013     /* close old display */
2014     close_display();
2015    
2016     /* open new display */
2017     cur_mode = i;
2018     bool ok = open_display();
2019    
2020     /* opening the screen failed? Then bail out */
2021     if (!ok) {
2022     ErrorAlert(GetString(STR_FULL_SCREEN_ERR));
2023     QuitEmulator();
2024     }
2025    
2026     WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
2027     csSave->saveBaseAddr=screen_base;
2028     csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
2029     csSave->saveMode=VModes[cur_mode].viAppleMode;
2030    
2031     // Enable interrupts and resume redraw thread
2032     thread_stop_req = false;
2033     EnableInterrupt();
2034     return noErr;
2035     }
2036     }
2037     return paramErr;
2038     }
2039    
2040    
2041     /*
2042     * Set color palette
2043     */
2044    
2045     void video_set_palette(void)
2046     {
2047 gbeauche 1.13 LOCK_PALETTE;
2048    
2049     // Convert colors to XColor array
2050     int mode = get_current_mode();
2051     int num_in = palette_size(mode);
2052     int num_out = 256;
2053     bool stretch = false;
2054     if (IsDirectMode(mode)) {
2055     // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
2056     num_out = vis->map_entries;
2057     stretch = true;
2058     }
2059     XColor *p = x_palette;
2060     for (int i=0; i<num_out; i++) {
2061     int c = (stretch ? (i * num_in) / num_out : i);
2062     p->red = mac_pal[c].red * 0x0101;
2063     p->green = mac_pal[c].green * 0x0101;
2064     p->blue = mac_pal[c].blue * 0x0101;
2065     p++;
2066     }
2067    
2068     #ifdef ENABLE_VOSF
2069     // Recalculate pixel color expansion map
2070     if (!IsDirectMode(mode) && xdepth > 8) {
2071     for (int i=0; i<256; i++) {
2072     int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
2073     ExpandMap[i] = map_rgb(mac_pal[c].red, mac_pal[c].green, mac_pal[c].blue);
2074     }
2075    
2076     // We have to redraw everything because the interpretation of pixel values changed
2077     LOCK_VOSF;
2078     PFLAG_SET_ALL;
2079     UNLOCK_VOSF;
2080     memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2081     }
2082     #endif
2083    
2084     // Tell redraw thread to change palette
2085 cebix 1.1 palette_changed = true;
2086 gbeauche 1.13
2087     UNLOCK_PALETTE;
2088 cebix 1.1 }
2089    
2090    
2091     /*
2092 gbeauche 1.21 * Can we set the MacOS cursor image into the window?
2093     */
2094    
2095     bool video_can_change_cursor(void)
2096     {
2097 gbeauche 1.22 return hw_mac_cursor_accl && (display_type != DIS_SCREEN);
2098 gbeauche 1.21 }
2099    
2100    
2101     /*
2102 cebix 1.1 * Set cursor image for window
2103     */
2104    
2105     void video_set_cursor(void)
2106     {
2107     cursor_changed = true;
2108     }
2109    
2110    
2111     /*
2112     * Thread for window refresh, event handling and other periodic actions
2113     */
2114    
2115     static void update_display(void)
2116     {
2117     // Incremental update code
2118     int wide = 0, high = 0, x1, x2, y1, y2, i, j;
2119     int bytes_per_row = VModes[cur_mode].viRowBytes;
2120     int bytes_per_pixel = VModes[cur_mode].viRowBytes / VModes[cur_mode].viXsize;
2121     uint8 *p, *p2;
2122    
2123     // Check for first line from top and first line from bottom that have changed
2124     y1 = 0;
2125     for (j=0; j<VModes[cur_mode].viYsize; j++) {
2126     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2127     y1 = j;
2128     break;
2129     }
2130     }
2131     y2 = y1 - 1;
2132     for (j=VModes[cur_mode].viYsize-1; j>=y1; j--) {
2133     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2134     y2 = j;
2135     break;
2136     }
2137     }
2138     high = y2 - y1 + 1;
2139    
2140     // Check for first column from left and first column from right that have changed
2141     if (high) {
2142     if (depth == 1) {
2143     x1 = VModes[cur_mode].viXsize;
2144     for (j=y1; j<=y2; j++) {
2145     p = &the_buffer[j * bytes_per_row];
2146     p2 = &the_buffer_copy[j * bytes_per_row];
2147     for (i=0; i<(x1>>3); i++) {
2148     if (*p != *p2) {
2149     x1 = i << 3;
2150     break;
2151     }
2152     p++;
2153     p2++;
2154     }
2155     }
2156     x2 = x1;
2157     for (j=y1; j<=y2; j++) {
2158     p = &the_buffer[j * bytes_per_row];
2159     p2 = &the_buffer_copy[j * bytes_per_row];
2160     p += bytes_per_row;
2161     p2 += bytes_per_row;
2162     for (i=(VModes[cur_mode].viXsize>>3); i>(x2>>3); i--) {
2163     p--;
2164     p2--;
2165     if (*p != *p2) {
2166     x2 = i << 3;
2167     break;
2168     }
2169     }
2170     }
2171     wide = x2 - x1;
2172    
2173     // Update copy of the_buffer
2174     if (high && wide) {
2175     for (j=y1; j<=y2; j++) {
2176     i = j * bytes_per_row + (x1 >> 3);
2177     memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
2178     }
2179     }
2180    
2181     } else {
2182     x1 = VModes[cur_mode].viXsize;
2183     for (j=y1; j<=y2; j++) {
2184     p = &the_buffer[j * bytes_per_row];
2185     p2 = &the_buffer_copy[j * bytes_per_row];
2186     for (i=0; i<x1; i++) {
2187     if (memcmp(p, p2, bytes_per_pixel)) {
2188     x1 = i;
2189     break;
2190     }
2191     p += bytes_per_pixel;
2192     p2 += bytes_per_pixel;
2193     }
2194     }
2195     x2 = x1;
2196     for (j=y1; j<=y2; j++) {
2197     p = &the_buffer[j * bytes_per_row];
2198     p2 = &the_buffer_copy[j * bytes_per_row];
2199     p += bytes_per_row;
2200     p2 += bytes_per_row;
2201     for (i=VModes[cur_mode].viXsize; i>x2; i--) {
2202     p -= bytes_per_pixel;
2203     p2 -= bytes_per_pixel;
2204     if (memcmp(p, p2, bytes_per_pixel)) {
2205     x2 = i;
2206     break;
2207     }
2208     }
2209     }
2210     wide = x2 - x1;
2211    
2212     // Update copy of the_buffer
2213     if (high && wide) {
2214     for (j=y1; j<=y2; j++) {
2215     i = j * bytes_per_row + x1 * bytes_per_pixel;
2216     memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
2217     }
2218     }
2219     }
2220     }
2221    
2222     // Refresh display
2223     if (high && wide) {
2224 gbeauche 1.10 XDisplayLock();
2225 cebix 1.1 if (have_shm)
2226     XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
2227     else
2228     XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
2229 gbeauche 1.10 XDisplayUnlock();
2230 cebix 1.1 }
2231     }
2232    
2233 gbeauche 1.10 const int VIDEO_REFRESH_HZ = 60;
2234     const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2235    
2236 gbeauche 1.13 static void handle_palette_changes(void)
2237     {
2238     LOCK_PALETTE;
2239    
2240     if (palette_changed && !emul_suspended) {
2241     palette_changed = false;
2242    
2243     int mode = get_current_mode();
2244     if (color_class == PseudoColor || color_class == DirectColor) {
2245     int num = vis->map_entries;
2246     bool set_clut = true;
2247     if (!IsDirectMode(mode) && color_class == DirectColor) {
2248     if (display_type == DIS_WINDOW)
2249     set_clut = false; // Indexed mode on true color screen, don't set CLUT
2250     }
2251    
2252     if (set_clut) {
2253     XDisplayLock();
2254     XStoreColors(x_display, cmap[0], x_palette, num);
2255     XStoreColors(x_display, cmap[1], x_palette, num);
2256     XSync(x_display, false);
2257     XDisplayUnlock();
2258     }
2259     }
2260    
2261     #ifdef ENABLE_XF86_DGA
2262     if (display_type == DIS_SCREEN) {
2263     current_dga_cmap ^= 1;
2264     if (!IsDirectMode(mode) && cmap[current_dga_cmap])
2265     XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2266     }
2267     #endif
2268     }
2269    
2270     UNLOCK_PALETTE;
2271     }
2272    
2273 cebix 1.1 static void *redraw_func(void *arg)
2274     {
2275 gbeauche 1.10 int fd = ConnectionNumber(x_display);
2276    
2277     uint64 start = GetTicks_usec();
2278     int64 ticks = 0;
2279     uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2280 cebix 1.1
2281     for (;;) {
2282    
2283     // Pause if requested (during video mode switches)
2284     while (thread_stop_req)
2285     thread_stop_ack = true;
2286    
2287 gbeauche 1.10 int64 delay = next - GetTicks_usec();
2288     if (delay < -VIDEO_REFRESH_DELAY) {
2289    
2290     // We are lagging far behind, so we reset the delay mechanism
2291     next = GetTicks_usec();
2292 cebix 1.1
2293 gbeauche 1.10 } else if (delay <= 0) {
2294    
2295     // Delay expired, refresh display
2296     next += VIDEO_REFRESH_DELAY;
2297     ticks++;
2298    
2299     // Handle X11 events
2300     handle_events();
2301    
2302     // Quit DGA mode if requested
2303     if (quit_full_screen) {
2304     quit_full_screen = false;
2305     if (display_type == DIS_SCREEN) {
2306     XDisplayLock();
2307 cebix 1.1 #ifdef ENABLE_XF86_DGA
2308 gbeauche 1.10 XF86DGADirectVideo(x_display, screen, 0);
2309     XUngrabPointer(x_display, CurrentTime);
2310     XUngrabKeyboard(x_display, CurrentTime);
2311 gbeauche 1.15 XUnmapWindow(x_display, the_win);
2312     wait_unmapped(the_win);
2313     XDestroyWindow(x_display, the_win);
2314 gbeauche 1.10 #endif
2315     XSync(x_display, false);
2316     XDisplayUnlock();
2317     quit_full_screen_ack = true;
2318     return NULL;
2319     }
2320 cebix 1.1 }
2321    
2322 gbeauche 1.10 // Refresh display and set cursor image in window mode
2323     static int tick_counter = 0;
2324     if (display_type == DIS_WINDOW) {
2325     tick_counter++;
2326     if (tick_counter >= frame_skip) {
2327     tick_counter = 0;
2328 cebix 1.1
2329 gbeauche 1.10 // Update display
2330 gbeauche 1.3 #ifdef ENABLE_VOSF
2331 gbeauche 1.10 if (use_vosf) {
2332     XDisplayLock();
2333     if (mainBuffer.dirty) {
2334     LOCK_VOSF;
2335     update_display_window_vosf();
2336     UNLOCK_VOSF;
2337     XSync(x_display, false); // Let the server catch up
2338     }
2339     XDisplayUnlock();
2340 gbeauche 1.3 }
2341 gbeauche 1.10 else
2342 gbeauche 1.3 #endif
2343 gbeauche 1.10 update_display();
2344 cebix 1.1
2345 gbeauche 1.10 // Set new cursor image if it was changed
2346 gbeauche 1.22 if (hw_mac_cursor_accl && cursor_changed) {
2347 gbeauche 1.10 cursor_changed = false;
2348     memcpy(cursor_image->data, MacCursor + 4, 32);
2349     memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2350     XDisplayLock();
2351     XFreeCursor(x_display, mac_cursor);
2352     XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
2353     XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
2354     mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2355     XDefineCursor(x_display, the_win, mac_cursor);
2356     XDisplayUnlock();
2357     }
2358 cebix 1.1 }
2359     }
2360 gbeauche 1.3 #ifdef ENABLE_VOSF
2361 gbeauche 1.10 else if (use_vosf) {
2362     // Update display (VOSF variant)
2363     if (++tick_counter >= frame_skip) {
2364     tick_counter = 0;
2365     if (mainBuffer.dirty) {
2366     LOCK_VOSF;
2367     update_display_dga_vosf();
2368     UNLOCK_VOSF;
2369     }
2370 gbeauche 1.3 }
2371     }
2372     #endif
2373 cebix 1.1
2374 gbeauche 1.10 // Set new palette if it was changed
2375 gbeauche 1.13 handle_palette_changes();
2376 gbeauche 1.10
2377     } else {
2378    
2379     // No display refresh pending, check for X events
2380     fd_set readfds;
2381     FD_ZERO(&readfds);
2382     FD_SET(fd, &readfds);
2383     struct timeval timeout;
2384     timeout.tv_sec = 0;
2385     timeout.tv_usec = delay;
2386     if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2387     handle_events();
2388 cebix 1.1 }
2389     }
2390     return NULL;
2391     }