ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/video_x.cpp
Revision: 1.29
Committed: 2004-06-22T20:01:18Z (20 years, 5 months ago) by gbeauche
Branch: MAIN
Changes since 1.28: +6 -0 lines
Log Message:
Force use of MacX mappings on MacOS X with Apple's X server.

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 gbeauche 1.28 static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread
69 cebix 1.1 static pthread_t redraw_thread; // Redraw thread
70    
71 gbeauche 1.4 static bool local_X11; // Flag: X server running on local machine?
72 cebix 1.1 static volatile bool thread_stop_req = false;
73     static volatile bool thread_stop_ack = false; // Acknowledge for thread_stop_req
74    
75     static bool has_dga = false; // Flag: Video DGA capable
76     static bool has_vidmode = false; // Flag: VidMode extension available
77    
78 gbeauche 1.3 #ifdef ENABLE_VOSF
79     static bool use_vosf = true; // Flag: VOSF enabled
80     #else
81     static const bool use_vosf = false; // VOSF not possible
82     #endif
83    
84 cebix 1.1 static bool palette_changed = false; // Flag: Palette changed, redraw thread must update palette
85     static bool ctrl_down = false; // Flag: Ctrl key pressed
86 gbeauche 1.27 static bool caps_on = false; // Flag: Caps Lock on
87 cebix 1.1 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
90    
91     static bool emul_suspended = false; // Flag: emulator suspended
92     static Window suspend_win; // "Suspend" window
93     static void *fb_save = NULL; // Saved frame buffer for suspend
94 gbeauche 1.6 static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
95     static int keycode_table[256]; // X keycode -> Mac keycode translation table
96 cebix 1.1
97     // X11 variables
98     static int screen; // Screen number
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 gbeauche 1.13 static int num_depths = 0; // Number of available X depths
103     static int *avail_depths = NULL; // List of available X depths
104 cebix 1.1 static XVisualInfo visualInfo;
105     static Visual *vis;
106 gbeauche 1.13 static int color_class;
107     static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
108 cebix 1.1 static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode
109 gbeauche 1.13 static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table
110    
111 cebix 1.1 static XColor black, white;
112     static unsigned long black_pixel, white_pixel;
113     static int eventmask;
114 gbeauche 1.13 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 cebix 1.1
117     // Variables for window mode
118     static GC the_gc;
119     static XImage *img = NULL;
120     static XShmSegmentInfo shminfo;
121     static XImage *cursor_image, *cursor_mask_image;
122     static Pixmap cursor_map, cursor_mask_map;
123     static Cursor mac_cursor;
124     static GC cursor_gc, cursor_mask_gc;
125     static bool cursor_changed = false; // Flag: Cursor changed, window_func must update cursor
126     static bool have_shm = false; // Flag: SHM present and usable
127 gbeauche 1.3 static uint8 *the_buffer = NULL; // Pointer to Mac frame buffer
128 cebix 1.1 static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer
129 gbeauche 1.3 static uint32 the_buffer_size; // Size of allocated the_buffer
130 cebix 1.1
131     // Variables for DGA mode
132     static int current_dga_cmap;
133    
134     #ifdef ENABLE_XF86_VIDMODE
135     // Variables for XF86 VidMode support
136     static XF86VidModeModeInfo **x_video_modes; // Array of all available modes
137     static int num_x_video_modes;
138     #endif
139    
140 gbeauche 1.13 // 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 cebix 1.1
155     // Prototypes
156     static void *redraw_func(void *arg);
157    
158    
159 gbeauche 1.4 // From main_unix.cpp
160     extern char *x_display_name;
161 cebix 1.1 extern Display *x_display;
162    
163     // From sys_unix.cpp
164     extern void SysMountFirstFloppy(void);
165    
166 gbeauche 1.9 // From clip_unix.cpp
167     extern void ClipboardSelectionClear(XSelectionClearEvent *);
168     extern void ClipboardSelectionRequest(XSelectionRequestEvent *);
169    
170 cebix 1.1
171 gbeauche 1.3 // Video acceleration through SIGSEGV
172     #ifdef ENABLE_VOSF
173     # include "video_vosf.h"
174     #endif
175    
176    
177 cebix 1.1 /*
178 gbeauche 1.13 * 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 gbeauche 1.16 // 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 gbeauche 1.13 // Map video_mode depth ID to numerical depth value
222     static inline int depth_of_video_mode(int mode)
223     {
224 gbeauche 1.16 int depth;
225 gbeauche 1.13 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 cebix 1.1 * Open display (window or fullscreen)
363     */
364    
365 gbeauche 1.14 // 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 gbeauche 1.13 // Wait until window is mapped/unmapped
374 gbeauche 1.15 static void wait_mapped(Window w)
375 gbeauche 1.13 {
376     XEvent e;
377     do {
378     XMaskEvent(x_display, StructureNotifyMask, &e);
379     } while ((e.type != MapNotify) || (e.xmap.event != w));
380     }
381    
382 gbeauche 1.15 static void wait_unmapped(Window w)
383 gbeauche 1.13 {
384     XEvent e;
385     do {
386     XMaskEvent(x_display, StructureNotifyMask, &e);
387     } while ((e.type != UnmapNotify) || (e.xmap.event != w));
388     }
389    
390 cebix 1.1 // Trap SHM errors
391     static bool shm_error = false;
392     static int (*old_error_handler)(Display *, XErrorEvent *);
393    
394     static int error_handler(Display *d, XErrorEvent *e)
395     {
396     if (e->error_code == BadAccess) {
397     shm_error = true;
398     return 0;
399     } else
400     return old_error_handler(d, e);
401     }
402    
403     // Open window
404     static bool open_window(int width, int height)
405     {
406 gbeauche 1.3 int aligned_width = (width + 15) & ~15;
407     int aligned_height = (height + 15) & ~15;
408    
409 cebix 1.1 // Set absolute mouse mode
410     ADBSetRelMouseMode(false);
411    
412     // Create window
413     XSetWindowAttributes wattr;
414     wattr.event_mask = eventmask = win_eventmask;
415 gbeauche 1.13 wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
416     wattr.border_pixel = 0;
417 cebix 1.1 wattr.backing_store = NotUseful;
418 gbeauche 1.13 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 | CWColormap, &wattr);
421 cebix 1.1
422 gbeauche 1.13 // Set window name
423 cebix 1.1 XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
424    
425 gbeauche 1.14 // Set delete protocol property
426     set_window_delete_protocol(the_win);
427    
428 cebix 1.1 // Make window unresizable
429     XSizeHints *hints;
430     if ((hints = XAllocSizeHints()) != NULL) {
431     hints->min_width = width;
432     hints->max_width = width;
433     hints->min_height = height;
434     hints->max_height = height;
435     hints->flags = PMinSize | PMaxSize;
436     XSetWMNormalHints(x_display, the_win, hints);
437     XFree((char *)hints);
438     }
439    
440 gbeauche 1.13 // Show window
441     XMapWindow(x_display, the_win);
442     wait_mapped(the_win);
443    
444 gbeauche 1.5 // 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);
447    
448 cebix 1.1 // Try to create and attach SHM image
449     have_shm = false;
450 gbeauche 1.5 if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) {
451 cebix 1.1
452     // Create SHM image ("height + 2" for safety)
453 gbeauche 1.5 img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
454 gbeauche 1.13 shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
455     D(bug(" shm image created\n"));
456 gbeauche 1.3 the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
457     shminfo.shmaddr = img->data = (char *)the_buffer_copy;
458 cebix 1.1 shminfo.readOnly = False;
459    
460     // Try to attach SHM image, catching errors
461     shm_error = false;
462     old_error_handler = XSetErrorHandler(error_handler);
463     XShmAttach(x_display, &shminfo);
464     XSync(x_display, false);
465     XSetErrorHandler(old_error_handler);
466     if (shm_error) {
467     shmdt(shminfo.shmaddr);
468     XDestroyImage(img);
469     shminfo.shmid = -1;
470     } else {
471     have_shm = true;
472     shmctl(shminfo.shmid, IPC_RMID, 0);
473     }
474 gbeauche 1.13 D(bug(" shm image attached\n"));
475 cebix 1.1 }
476    
477     // Create normal X image if SHM doesn't work ("height + 2" for safety)
478     if (!have_shm) {
479 gbeauche 1.13 int bytes_per_row = depth == 1 ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth));
480 gbeauche 1.3 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 gbeauche 1.13 D(bug(" X image created\n"));
483 cebix 1.1 }
484    
485     // 1-Bit mode is big-endian
486 gbeauche 1.13 if (need_msb_image) {
487 cebix 1.1 img->byte_order = MSBFirst;
488     img->bitmap_bit_order = MSBFirst;
489     }
490    
491 gbeauche 1.3 #ifdef ENABLE_VOSF
492     use_vosf = true;
493     // Allocate memory for frame buffer (SIZE is extended to page-boundary)
494     the_host_buffer = the_buffer_copy;
495     the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
496     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
497     the_buffer_copy = (uint8 *)malloc(the_buffer_size);
498     D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
499     #else
500     // Allocate memory for frame buffer
501     the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
502     D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
503     #endif
504     screen_base = (uint32)the_buffer;
505 cebix 1.1
506     // Create GC
507     the_gc = XCreateGC(x_display, the_win, 0, 0);
508 gbeauche 1.13 XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
509 cebix 1.1
510     // Create cursor
511 gbeauche 1.22 if (hw_mac_cursor_accl) {
512 gbeauche 1.21 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 cebix 1.1
535 gbeauche 1.3 // Init blitting routines
536     bool native_byte_order;
537     #ifdef WORDS_BIGENDIAN
538     native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
539     #else
540     native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
541     #endif
542     #ifdef ENABLE_VOSF
543     Screen_blitter_init(&visualInfo, native_byte_order, depth);
544     #endif
545    
546 cebix 1.1 // Set bytes per row
547     XSync(x_display, false);
548     return true;
549     }
550    
551     // Open DGA display (!! should use X11 VidMode extensions to set mode)
552     static bool open_dga(int width, int height)
553     {
554     #ifdef ENABLE_XF86_DGA
555     // Set relative mouse mode
556     ADBSetRelMouseMode(true);
557    
558 gbeauche 1.15 // 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 cebix 1.1 #ifdef ENABLE_XF86_VIDMODE
572     // Switch to best mode
573     if (has_vidmode) {
574     int best = 0;
575     for (int i=1; i<num_x_video_modes; i++) {
576     if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
577     x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
578     best = i;
579     }
580     }
581     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
582     XF86VidModeSetViewPort(x_display, screen, 0, 0);
583     }
584     #endif
585    
586     // Establish direct screen connection
587 gbeauche 1.15 XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
588     XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
589 cebix 1.1 XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
590     XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
591 gbeauche 1.15
592     int v_width, v_bank, v_size;
593     XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
594 cebix 1.1 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 gbeauche 1.15 if (!IsDirectMode(get_current_mode())) {
600     XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
601 cebix 1.1 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
602 gbeauche 1.15 }
603     XSync(x_display, false);
604 cebix 1.1
605 gbeauche 1.15 // Init blitting routines
606     int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, DepthModeForPixelDepth(depth));
607 gbeauche 1.3 #if ENABLE_VOSF
608     bool native_byte_order;
609     #ifdef WORDS_BIGENDIAN
610     native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
611     #else
612     native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
613     #endif
614     #if REAL_ADDRESSING || DIRECT_ADDRESSING
615     // Screen_blitter_init() returns TRUE if VOSF is mandatory
616     // i.e. the framebuffer update function is not Blit_Copy_Raw
617     use_vosf = Screen_blitter_init(&visualInfo, native_byte_order, depth);
618    
619     if (use_vosf) {
620     // Allocate memory for frame buffer (SIZE is extended to page-boundary)
621     the_host_buffer = the_buffer;
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 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));
626 gbeauche 1.3 }
627     #else
628     use_vosf = false;
629     #endif
630     #endif
631 gbeauche 1.15
632     // Set frame buffer base
633     D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
634 gbeauche 1.3 screen_base = (uint32)the_buffer;
635 cebix 1.1 VModes[cur_mode].viRowBytes = bytes_per_row;
636     return true;
637     #else
638     ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
639     return false;
640     #endif
641     }
642    
643     static bool open_display(void)
644     {
645 gbeauche 1.13 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 cebix 1.1 }
709 gbeauche 1.3
710 gbeauche 1.13 #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 gbeauche 1.3 bool display_open = false;
722 cebix 1.1 if (display_type == DIS_SCREEN)
723 gbeauche 1.3 display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
724 cebix 1.1 else if (display_type == DIS_WINDOW)
725 gbeauche 1.3 display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
726    
727     #ifdef ENABLE_VOSF
728     if (use_vosf) {
729     // Initialize the VOSF system
730     if (!video_vosf_init()) {
731     ErrorAlert(GetString(STR_VOSF_INIT_ERR));
732     return false;
733     }
734     }
735     #endif
736    
737     return display_open;
738 cebix 1.1 }
739    
740    
741     /*
742     * Close display
743     */
744    
745     // Close window
746     static void close_window(void)
747     {
748 gbeauche 1.3 if (have_shm) {
749     XShmDetach(x_display, &shminfo);
750     #ifdef ENABLE_VOSF
751     the_host_buffer = NULL; // don't free() in driver_base dtor
752     #else
753     the_buffer_copy = NULL; // don't free() in driver_base dtor
754     #endif
755     }
756     if (img) {
757     if (!have_shm)
758     img->data = NULL;
759     XDestroyImage(img);
760     }
761     if (have_shm) {
762     shmdt(shminfo.shmaddr);
763     shmctl(shminfo.shmid, IPC_RMID, 0);
764     }
765     if (the_gc)
766     XFreeGC(x_display, the_gc);
767    
768 gbeauche 1.13 XFlush(x_display);
769     XSync(x_display, false);
770 cebix 1.1 }
771    
772     // Close DGA mode
773     static void close_dga(void)
774     {
775     #ifdef ENABLE_XF86_DGA
776     XF86DGADirectVideo(x_display, screen, 0);
777     XUngrabPointer(x_display, CurrentTime);
778     XUngrabKeyboard(x_display, CurrentTime);
779     #endif
780    
781     #ifdef ENABLE_XF86_VIDMODE
782     if (has_vidmode)
783     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
784     #endif
785 gbeauche 1.3
786     if (!use_vosf) {
787     // don't free() the screen buffer in driver_base dtor
788     the_buffer = NULL;
789     }
790     #ifdef ENABLE_VOSF
791     else {
792     // don't free() the screen buffer in driver_base dtor
793     the_host_buffer = NULL;
794     }
795     #endif
796 cebix 1.1 }
797    
798     static void close_display(void)
799     {
800     if (display_type == DIS_SCREEN)
801     close_dga();
802     else if (display_type == DIS_WINDOW)
803     close_window();
804 gbeauche 1.3
805 gbeauche 1.15 // 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 gbeauche 1.13 // 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 gbeauche 1.3 #ifdef ENABLE_VOSF
823     if (use_vosf) {
824     // Deinitialize VOSF
825     video_vosf_exit();
826     }
827     #endif
828    
829     // Free frame buffer(s)
830     if (!use_vosf) {
831     if (the_buffer_copy) {
832     free(the_buffer_copy);
833     the_buffer_copy = NULL;
834     }
835     }
836     #ifdef ENABLE_VOSF
837     else {
838     // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
839     if (the_buffer != VM_MAP_FAILED) {
840     D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
841     vm_release(the_buffer, the_buffer_size);
842     the_buffer = NULL;
843     }
844     if (the_host_buffer) {
845     D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
846     free(the_host_buffer);
847     the_host_buffer = NULL;
848     }
849     if (the_buffer_copy) {
850     D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
851     free(the_buffer_copy);
852     the_buffer_copy = NULL;
853     }
854     }
855     #endif
856 cebix 1.1 }
857    
858    
859     /*
860     * Initialization
861     */
862    
863 gbeauche 1.6 // Init keycode translation table
864     static void keycode_init(void)
865     {
866     bool use_kc = PrefsFindBool("keycodes");
867     if (use_kc) {
868    
869     // Get keycode file path from preferences
870     const char *kc_path = PrefsFindString("keycodefile");
871    
872     // Open keycode table
873     FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
874     if (f == NULL) {
875     char str[256];
876     sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
877     WarningAlert(str);
878     return;
879     }
880    
881     // Default translation table
882     for (int i=0; i<256; i++)
883     keycode_table[i] = -1;
884    
885     // Search for server vendor string, then read keycodes
886     const char *vendor = ServerVendor(x_display);
887 gbeauche 1.29 #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 gbeauche 1.6 bool vendor_found = false;
894     char line[256];
895     while (fgets(line, 255, f)) {
896     // Read line
897     int len = strlen(line);
898     if (len == 0)
899     continue;
900     line[len-1] = 0;
901    
902     // Comments begin with "#" or ";"
903     if (line[0] == '#' || line[0] == ';' || line[0] == 0)
904     continue;
905    
906     if (vendor_found) {
907     // Read keycode
908     int x_code, mac_code;
909     if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
910     keycode_table[x_code & 0xff] = mac_code;
911     else
912     break;
913     } else {
914     // Search for vendor string
915     if (strstr(vendor, line) == vendor)
916     vendor_found = true;
917     }
918     }
919    
920     // Keycode file completely read
921     fclose(f);
922     use_keycodes = vendor_found;
923    
924     // Vendor not found? Then display warning
925     if (!vendor_found) {
926     char str[256];
927     sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME);
928     WarningAlert(str);
929     return;
930     }
931     }
932     }
933    
934 gbeauche 1.15 // 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 gbeauche 1.13 // 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 cebix 1.1 {
970     if (allow & test) {
971     p->viType = type;
972     switch (apple_id) {
973     case APPLE_W_640x480:
974     case APPLE_640x480:
975     p->viXsize = 640;
976     p->viYsize = 480;
977     break;
978     case APPLE_W_800x600:
979     case APPLE_800x600:
980     p->viXsize = 800;
981     p->viYsize = 600;
982     break;
983     case APPLE_1024x768:
984     p->viXsize = 1024;
985     p->viYsize = 768;
986     break;
987 gbeauche 1.15 case APPLE_1152x768:
988     p->viXsize = 1152;
989     p->viYsize = 768;
990     break;
991 cebix 1.1 case APPLE_1152x900:
992     p->viXsize = 1152;
993     p->viYsize = 900;
994     break;
995     case APPLE_1280x1024:
996     p->viXsize = 1280;
997     p->viYsize = 1024;
998     break;
999     case APPLE_1600x1200:
1000     p->viXsize = 1600;
1001     p->viYsize = 1200;
1002     break;
1003     }
1004 gbeauche 1.13 p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
1005 cebix 1.1 p->viAppleMode = apple_mode;
1006     p->viAppleID = apple_id;
1007     p++;
1008     }
1009     }
1010    
1011 gbeauche 1.13 // 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 cebix 1.1 static bool has_mode(int x, int y)
1019     {
1020     #ifdef ENABLE_XF86_VIDMODE
1021     for (int i=0; i<num_x_video_modes; i++)
1022     if (x_video_modes[i]->hdisplay >= x && x_video_modes[i]->vdisplay >= y)
1023     return true;
1024     return false;
1025     #else
1026     return DisplayWidth(x_display, screen) >= x && DisplayHeight(x_display, screen) >= y;
1027     #endif
1028     }
1029    
1030     bool VideoInit(void)
1031     {
1032 gbeauche 1.3 #ifdef ENABLE_VOSF
1033     // Zero the mainBuffer structure
1034     mainBuffer.dirtyPages = NULL;
1035     mainBuffer.pageInfo = NULL;
1036     #endif
1037    
1038 gbeauche 1.4 // Check if X server runs on local machine
1039     local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
1040     || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
1041    
1042 gbeauche 1.6 // Init keycode translation
1043     keycode_init();
1044    
1045 gbeauche 1.8 // 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 cebix 1.1 // Init variables
1055     private_data = NULL;
1056     video_activated = true;
1057    
1058     // Find screen and root window
1059     screen = XDefaultScreen(x_display);
1060     rootwin = XRootWindow(x_display, screen);
1061    
1062 gbeauche 1.13 // 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 cebix 1.1 // Get screen depth
1071     xdepth = DefaultDepth(x_display, screen);
1072    
1073     #ifdef ENABLE_XF86_DGA
1074     // DGA available?
1075     int event_base, error_base;
1076 gbeauche 1.4 if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
1077 cebix 1.1 int dga_flags = 0;
1078     XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1079     has_dga = dga_flags & XF86DGADirectPresent;
1080     } else
1081     has_dga = false;
1082     #endif
1083    
1084     #ifdef ENABLE_XF86_VIDMODE
1085     // VidMode available?
1086     int vm_event_base, vm_error_base;
1087     has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
1088     if (has_vidmode)
1089     XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1090     #endif
1091    
1092     // Find black and white colors
1093     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
1094     XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
1095     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white);
1096     XAllocColor(x_display, DefaultColormap(x_display, screen), &white);
1097     black_pixel = BlackPixel(x_display, screen);
1098     white_pixel = WhitePixel(x_display, screen);
1099    
1100     // Mac screen depth follows X depth (for now)
1101 gbeauche 1.13 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 cebix 1.1 }
1116    
1117     // Construct video mode table
1118     uint32 window_modes = PrefsFindInt32("windowmodes");
1119     uint32 screen_modes = PrefsFindInt32("screenmodes");
1120     if (!has_dga)
1121     screen_modes = 0;
1122     if (window_modes == 0 && screen_modes == 0)
1123     window_modes |= 3; // Allow at least 640x480 and 800x600 window modes
1124    
1125     VideoInfo *p = VModes;
1126 gbeauche 1.13 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 cebix 1.1 if (has_vidmode) {
1131     if (has_mode(640, 480))
1132 gbeauche 1.13 add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1133 cebix 1.1 if (has_mode(800, 600))
1134 gbeauche 1.13 add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN);
1135 cebix 1.1 if (has_mode(1024, 768))
1136 gbeauche 1.13 add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN);
1137 gbeauche 1.15 if (has_mode(1152, 768))
1138     add_mode(p, screen_modes, 64, default_mode, APPLE_1152x768, DIS_SCREEN);
1139 cebix 1.1 if (has_mode(1152, 900))
1140 gbeauche 1.13 add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN);
1141 cebix 1.1 if (has_mode(1280, 1024))
1142 gbeauche 1.13 add_mode(p, screen_modes, 16, default_mode, APPLE_1280x1024, DIS_SCREEN);
1143 cebix 1.1 if (has_mode(1600, 1200))
1144 gbeauche 1.13 add_mode(p, screen_modes, 32, default_mode, APPLE_1600x1200, DIS_SCREEN);
1145 cebix 1.1 } else if (screen_modes) {
1146     int xsize = DisplayWidth(x_display, screen);
1147     int ysize = DisplayHeight(x_display, screen);
1148 gbeauche 1.15 int apple_id = find_apple_resolution(xsize, ysize);
1149 cebix 1.1 p->viType = DIS_SCREEN;
1150     p->viRowBytes = 0;
1151     p->viXsize = xsize;
1152     p->viYsize = ysize;
1153 gbeauche 1.13 p->viAppleMode = default_mode;
1154 cebix 1.1 p->viAppleID = apple_id;
1155     p++;
1156     }
1157     p->viType = DIS_INVALID; // End marker
1158     p->viRowBytes = 0;
1159     p->viXsize = p->viYsize = 0;
1160     p->viAppleMode = 0;
1161     p->viAppleID = 0;
1162    
1163 gbeauche 1.13 // Find default mode (window 640x480)
1164     cur_mode = -1;
1165 gbeauche 1.15 if (has_dga && screen_modes) {
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 gbeauche 1.13 }
1172 gbeauche 1.25 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 gbeauche 1.13 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    
1191 cebix 1.1 // Open window/screen
1192     if (!open_display())
1193     return false;
1194    
1195     #if 0
1196     // Ignore errors from now on
1197     XSetErrorHandler(ignore_errors);
1198     #endif
1199    
1200     // Start periodic thread
1201     XSync(x_display, false);
1202 gbeauche 1.15 Set_pthread_attr(&redraw_thread_attr, 0);
1203 gbeauche 1.28 redraw_thread_cancel = false;
1204 gbeauche 1.15 redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1205 cebix 1.1 D(bug("Redraw thread installed (%ld)\n", redraw_thread));
1206     return true;
1207     }
1208    
1209    
1210     /*
1211     * Deinitialization
1212     */
1213    
1214     void VideoExit(void)
1215     {
1216     // Stop redraw thread
1217     if (redraw_thread_active) {
1218 gbeauche 1.28 redraw_thread_cancel = true;
1219 cebix 1.1 pthread_cancel(redraw_thread);
1220     pthread_join(redraw_thread, NULL);
1221     redraw_thread_active = false;
1222     }
1223    
1224 gbeauche 1.3 #ifdef ENABLE_VOSF
1225     if (use_vosf) {
1226     // Deinitialize VOSF
1227     video_vosf_exit();
1228     }
1229     #endif
1230    
1231 cebix 1.1 // Close window and server connection
1232     if (x_display != NULL) {
1233     XSync(x_display, false);
1234     close_display();
1235     XFlush(x_display);
1236     XSync(x_display, false);
1237     }
1238     }
1239    
1240    
1241     /*
1242     * Suspend/resume emulator
1243     */
1244    
1245     extern void PauseEmulator(void);
1246     extern void ResumeEmulator(void);
1247    
1248     static void suspend_emul(void)
1249     {
1250     if (display_type == DIS_SCREEN) {
1251     // Release ctrl key
1252     ADBKeyUp(0x36);
1253     ctrl_down = false;
1254    
1255     // Pause MacOS thread
1256     PauseEmulator();
1257     emul_suspended = true;
1258    
1259     // Save frame buffer
1260     fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1261     if (fb_save)
1262     memcpy(fb_save, (void *)screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1263    
1264     // Close full screen display
1265     #ifdef ENABLE_XF86_DGA
1266     XF86DGADirectVideo(x_display, screen, 0);
1267     XUngrabPointer(x_display, CurrentTime);
1268     XUngrabKeyboard(x_display, CurrentTime);
1269     #endif
1270     XSync(x_display, false);
1271    
1272     // Open "suspend" window
1273     XSetWindowAttributes wattr;
1274     wattr.event_mask = KeyPressMask;
1275     wattr.background_pixel = black_pixel;
1276     wattr.border_pixel = black_pixel;
1277     wattr.backing_store = Always;
1278     wattr.backing_planes = xdepth;
1279     wattr.colormap = DefaultColormap(x_display, screen);
1280     XSync(x_display, false);
1281     suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1282     InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1283     CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1284     XSync(x_display, false);
1285     XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1286     XMapRaised(x_display, suspend_win);
1287     XSync(x_display, false);
1288     }
1289     }
1290    
1291     static void resume_emul(void)
1292     {
1293     // Close "suspend" window
1294     XDestroyWindow(x_display, suspend_win);
1295     XSync(x_display, false);
1296    
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 gbeauche 1.12 #ifdef ENABLE_XF86_DGA
1301 cebix 1.1 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1302     XF86DGASetViewPort(x_display, screen, 0, 0);
1303 gbeauche 1.12 #endif
1304 cebix 1.1 XSync(x_display, false);
1305    
1306 gbeauche 1.3 // the_buffer already contains the data to restore. i.e. since a temporary
1307     // frame buffer is used when VOSF is actually used, fb_save is therefore
1308     // not necessary.
1309     #ifdef ENABLE_VOSF
1310     if (use_vosf) {
1311     LOCK_VOSF;
1312     PFLAG_SET_ALL;
1313     UNLOCK_VOSF;
1314     memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1315     }
1316     #endif
1317    
1318 cebix 1.1 // Restore frame buffer
1319     if (fb_save) {
1320 gbeauche 1.3 #ifdef ENABLE_VOSF
1321     // Don't copy fb_save to the temporary frame buffer in VOSF mode
1322     if (!use_vosf)
1323     #endif
1324 cebix 1.1 memcpy((void *)screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1325     free(fb_save);
1326     fb_save = NULL;
1327     }
1328     if (depth == 8)
1329     palette_changed = true;
1330    
1331     // Resume MacOS thread
1332     emul_suspended = false;
1333     ResumeEmulator();
1334     }
1335    
1336    
1337     /*
1338     * Close screen in full-screen mode
1339     */
1340    
1341     void VideoQuitFullScreen(void)
1342     {
1343     D(bug("VideoQuitFullScreen()\n"));
1344     if (display_type == DIS_SCREEN) {
1345     quit_full_screen = true;
1346     while (!quit_full_screen_ack) ;
1347     }
1348     }
1349    
1350    
1351     /*
1352     * X11 event handling
1353     */
1354    
1355     // Translate key event to Mac keycode
1356     static int kc_decode(KeySym ks)
1357     {
1358     switch (ks) {
1359     case XK_A: case XK_a: return 0x00;
1360     case XK_B: case XK_b: return 0x0b;
1361     case XK_C: case XK_c: return 0x08;
1362     case XK_D: case XK_d: return 0x02;
1363     case XK_E: case XK_e: return 0x0e;
1364     case XK_F: case XK_f: return 0x03;
1365     case XK_G: case XK_g: return 0x05;
1366     case XK_H: case XK_h: return 0x04;
1367     case XK_I: case XK_i: return 0x22;
1368     case XK_J: case XK_j: return 0x26;
1369     case XK_K: case XK_k: return 0x28;
1370     case XK_L: case XK_l: return 0x25;
1371     case XK_M: case XK_m: return 0x2e;
1372     case XK_N: case XK_n: return 0x2d;
1373     case XK_O: case XK_o: return 0x1f;
1374     case XK_P: case XK_p: return 0x23;
1375     case XK_Q: case XK_q: return 0x0c;
1376     case XK_R: case XK_r: return 0x0f;
1377     case XK_S: case XK_s: return 0x01;
1378     case XK_T: case XK_t: return 0x11;
1379     case XK_U: case XK_u: return 0x20;
1380     case XK_V: case XK_v: return 0x09;
1381     case XK_W: case XK_w: return 0x0d;
1382     case XK_X: case XK_x: return 0x07;
1383     case XK_Y: case XK_y: return 0x10;
1384     case XK_Z: case XK_z: return 0x06;
1385    
1386     case XK_1: case XK_exclam: return 0x12;
1387     case XK_2: case XK_at: return 0x13;
1388     case XK_3: case XK_numbersign: return 0x14;
1389     case XK_4: case XK_dollar: return 0x15;
1390     case XK_5: case XK_percent: return 0x17;
1391     case XK_6: return 0x16;
1392     case XK_7: return 0x1a;
1393     case XK_8: return 0x1c;
1394     case XK_9: return 0x19;
1395     case XK_0: return 0x1d;
1396    
1397     case XK_grave: case XK_asciitilde: return 0x0a;
1398     case XK_minus: case XK_underscore: return 0x1b;
1399     case XK_equal: case XK_plus: return 0x18;
1400     case XK_bracketleft: case XK_braceleft: return 0x21;
1401     case XK_bracketright: case XK_braceright: return 0x1e;
1402     case XK_backslash: case XK_bar: return 0x2a;
1403     case XK_semicolon: case XK_colon: return 0x29;
1404     case XK_apostrophe: case XK_quotedbl: return 0x27;
1405     case XK_comma: case XK_less: return 0x2b;
1406     case XK_period: case XK_greater: return 0x2f;
1407     case XK_slash: case XK_question: return 0x2c;
1408    
1409     case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1410     case XK_Return: return 0x24;
1411     case XK_space: return 0x31;
1412     case XK_BackSpace: return 0x33;
1413    
1414     case XK_Delete: return 0x75;
1415     case XK_Insert: return 0x72;
1416     case XK_Home: case XK_Help: return 0x73;
1417     case XK_End: return 0x77;
1418     #ifdef __hpux
1419     case XK_Prior: return 0x74;
1420     case XK_Next: return 0x79;
1421     #else
1422     case XK_Page_Up: return 0x74;
1423     case XK_Page_Down: return 0x79;
1424     #endif
1425    
1426     case XK_Control_L: return 0x36;
1427     case XK_Control_R: return 0x36;
1428     case XK_Shift_L: return 0x38;
1429     case XK_Shift_R: return 0x38;
1430     case XK_Alt_L: return 0x37;
1431     case XK_Alt_R: return 0x37;
1432     case XK_Meta_L: return 0x3a;
1433     case XK_Meta_R: return 0x3a;
1434     case XK_Menu: return 0x32;
1435     case XK_Caps_Lock: return 0x39;
1436     case XK_Num_Lock: return 0x47;
1437    
1438     case XK_Up: return 0x3e;
1439     case XK_Down: return 0x3d;
1440     case XK_Left: return 0x3b;
1441     case XK_Right: return 0x3c;
1442    
1443     case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
1444    
1445     case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
1446     case XK_F2: return 0x78;
1447     case XK_F3: return 0x63;
1448     case XK_F4: return 0x76;
1449     case XK_F5: return 0x60;
1450     case XK_F6: return 0x61;
1451     case XK_F7: return 0x62;
1452     case XK_F8: return 0x64;
1453     case XK_F9: return 0x65;
1454     case XK_F10: return 0x6d;
1455     case XK_F11: return 0x67;
1456     case XK_F12: return 0x6f;
1457    
1458     case XK_Print: return 0x69;
1459     case XK_Scroll_Lock: return 0x6b;
1460     case XK_Pause: return 0x71;
1461    
1462     #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
1463     case XK_KP_0: case XK_KP_Insert: return 0x52;
1464     case XK_KP_1: case XK_KP_End: return 0x53;
1465     case XK_KP_2: case XK_KP_Down: return 0x54;
1466     case XK_KP_3: case XK_KP_Next: return 0x55;
1467     case XK_KP_4: case XK_KP_Left: return 0x56;
1468     case XK_KP_5: case XK_KP_Begin: return 0x57;
1469     case XK_KP_6: case XK_KP_Right: return 0x58;
1470     case XK_KP_7: case XK_KP_Home: return 0x59;
1471     case XK_KP_8: case XK_KP_Up: return 0x5b;
1472     case XK_KP_9: case XK_KP_Prior: return 0x5c;
1473     case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
1474     #else
1475     case XK_KP_0: return 0x52;
1476     case XK_KP_1: return 0x53;
1477     case XK_KP_2: return 0x54;
1478     case XK_KP_3: return 0x55;
1479     case XK_KP_4: return 0x56;
1480     case XK_KP_5: return 0x57;
1481     case XK_KP_6: return 0x58;
1482     case XK_KP_7: return 0x59;
1483     case XK_KP_8: return 0x5b;
1484     case XK_KP_9: return 0x5c;
1485     case XK_KP_Decimal: return 0x41;
1486     #endif
1487     case XK_KP_Add: return 0x45;
1488     case XK_KP_Subtract: return 0x4e;
1489     case XK_KP_Multiply: return 0x43;
1490     case XK_KP_Divide: return 0x4b;
1491     case XK_KP_Enter: return 0x4c;
1492     case XK_KP_Equal: return 0x51;
1493     }
1494     return -1;
1495     }
1496    
1497 gbeauche 1.27 static int event2keycode(XKeyEvent &ev, bool key_down)
1498 cebix 1.1 {
1499     KeySym ks;
1500     int i = 0;
1501    
1502     do {
1503 gbeauche 1.6 ks = XLookupKeysym(&ev, i++);
1504 gbeauche 1.27 int as = kc_decode(ks);
1505     if (as >= 0)
1506     return as;
1507     if (as == -2)
1508 cebix 1.1 return as;
1509     } while (ks != NoSymbol);
1510    
1511     return -1;
1512     }
1513    
1514     static void handle_events(void)
1515     {
1516     // Handle events
1517     for (;;) {
1518     XEvent event;
1519    
1520 gbeauche 1.10 XDisplayLock();
1521 gbeauche 1.9 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 gbeauche 1.14
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 gbeauche 1.10
1536     XDisplayUnlock();
1537 cebix 1.1 break;
1538 gbeauche 1.9 }
1539 gbeauche 1.10 XDisplayUnlock();
1540 cebix 1.1
1541     switch (event.type) {
1542     // Mouse button
1543     case ButtonPress: {
1544     unsigned int button = ((XButtonEvent *)&event)->button;
1545     if (button < 4)
1546     ADBMouseDown(button - 1);
1547 gbeauche 1.8 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 cebix 1.1 break;
1561     }
1562     case ButtonRelease: {
1563     unsigned int button = ((XButtonEvent *)&event)->button;
1564     if (button < 4)
1565     ADBMouseUp(button - 1);
1566     break;
1567     }
1568    
1569 gbeauche 1.15 // Mouse entered window
1570 cebix 1.1 case EnterNotify:
1571 gbeauche 1.15 if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
1572     ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1573 cebix 1.1 break;
1574 gbeauche 1.15
1575     // Mouse moved
1576 cebix 1.1 case MotionNotify:
1577 gbeauche 1.15 ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1578 cebix 1.1 break;
1579    
1580     // Keyboard
1581     case KeyPress: {
1582 gbeauche 1.27 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 cebix 1.1 if (!emul_suspended) {
1590 gbeauche 1.27 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 cebix 1.1 if (code == 0x36)
1601     ctrl_down = true;
1602     } else {
1603     if (code == 0x31)
1604     resume_emul(); // Space wakes us up
1605     }
1606     }
1607     break;
1608     }
1609     case KeyRelease: {
1610 gbeauche 1.27 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 cebix 1.1 ADBKeyUp(code);
1618     if (code == 0x36)
1619     ctrl_down = false;
1620     }
1621     break;
1622     }
1623    
1624     // Hidden parts exposed, force complete refresh
1625     case Expose:
1626 gbeauche 1.3 #ifdef ENABLE_VOSF
1627     if (use_vosf) { // VOSF refresh
1628     LOCK_VOSF;
1629     PFLAG_SET_ALL;
1630     UNLOCK_VOSF;
1631     }
1632     #endif
1633 cebix 1.1 memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1634     break;
1635     }
1636     }
1637     }
1638    
1639    
1640     /*
1641     * Execute video VBL routine
1642     */
1643    
1644     void VideoVBL(void)
1645     {
1646     if (emerg_quit)
1647     QuitEmulator();
1648    
1649     // Execute video VBL
1650     if (private_data != NULL && private_data->interruptsEnabled)
1651     VSLDoInterruptService(private_data->vslServiceID);
1652     }
1653    
1654    
1655     /*
1656     * Install graphics acceleration
1657     */
1658    
1659 gbeauche 1.18 // Rectangle inversion
1660     template< int bpp >
1661     static inline void do_invrect(uint8 *dest, uint32 length)
1662 cebix 1.1 {
1663 gbeauche 1.18 #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     #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     // 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     // Invert 8-byte words
1683     if (length >= 8) {
1684     const int r = (length / 8) % 8;
1685     dest += r * 8;
1686    
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 cebix 1.1
1703 gbeauche 1.18 // 32-bit cell to invert?
1704     if (length & 4) {
1705     INVERT_4(dest, 0);
1706     if (bpp <= 16)
1707     dest += 4;
1708     }
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 gbeauche 1.19 void NQD_invrect(uint32 p)
1728 gbeauche 1.18 {
1729 gbeauche 1.19 D(bug("accl_invrect %08x\n", p));
1730 gbeauche 1.18
1731     // Get inversion parameters
1732 gbeauche 1.19 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 cebix 1.1 D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1737 gbeauche 1.19 D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1738 cebix 1.1
1739 gbeauche 1.18 //!!?? pen_mode == 14
1740    
1741     // And perform the inversion
1742 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1743     const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1744 gbeauche 1.20 uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1745 gbeauche 1.18 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 cebix 1.1 }
1767    
1768 gbeauche 1.18 // Rectangle filling
1769     template< int bpp >
1770     static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1771 cebix 1.1 {
1772 gbeauche 1.18 #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     #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     // 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 cebix 1.1
1813 gbeauche 1.18 // 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 cebix 1.1
1827 gbeauche 1.18 // 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 cebix 1.1 }
1836    
1837 gbeauche 1.19 void NQD_fillrect(uint32 p)
1838 cebix 1.1 {
1839 gbeauche 1.19 D(bug("accl_fillrect %08x\n", p));
1840 cebix 1.1
1841 gbeauche 1.18 // Get filling parameters
1842 gbeauche 1.19 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 gbeauche 1.24 uint32 color = htonl(ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen));
1847 cebix 1.1 D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1848 gbeauche 1.18 D(bug(" width %d, height %d\n", width, height));
1849 gbeauche 1.19 D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1850 cebix 1.1
1851 gbeauche 1.18 // And perform the fill
1852 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1853     const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1854 gbeauche 1.20 uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1855 gbeauche 1.18 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 cebix 1.1 }
1877    
1878 gbeauche 1.19 bool NQD_fillrect_hook(uint32 p)
1879 cebix 1.1 {
1880 gbeauche 1.19 D(bug("accl_fillrect_hook %08x\n", p));
1881 cebix 1.1
1882     // Check if we can accelerate this fillrect
1883 gbeauche 1.19 if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1884     const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1885     if (transfer_mode == 8) {
1886 cebix 1.1 // Fill
1887 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1888 gbeauche 1.18 return true;
1889     }
1890 gbeauche 1.19 else if (transfer_mode == 10) {
1891 cebix 1.1 // Invert
1892 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1893 cebix 1.1 return true;
1894     }
1895     }
1896     return false;
1897     }
1898    
1899 gbeauche 1.16 // Rectangle blitting
1900     // TODO: optimize for VOSF and target pixmap == screen
1901 gbeauche 1.19 void NQD_bitblt(uint32 p)
1902 gbeauche 1.16 {
1903 gbeauche 1.19 D(bug("accl_bitblt %08x\n", p));
1904 gbeauche 1.16
1905     // Get blitting parameters
1906 gbeauche 1.19 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 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));
1914     D(bug(" width %d, height %d\n", width, height));
1915    
1916     // And perform the blit
1917 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1918 gbeauche 1.16 width *= bpp;
1919 gbeauche 1.19 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 gbeauche 1.20 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 gbeauche 1.16 for (int i = 0; i < height; i++) {
1925 gbeauche 1.23 memmove(dst, src, width);
1926 gbeauche 1.16 src += src_row_bytes;
1927     dst += dst_row_bytes;
1928     }
1929     }
1930     else {
1931 gbeauche 1.19 const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1932     const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1933 gbeauche 1.20 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 gbeauche 1.16 for (int i = height - 1; i >= 0; i--) {
1936 gbeauche 1.23 memmove(dst, src, width);
1937 gbeauche 1.16 src -= src_row_bytes;
1938     dst -= dst_row_bytes;
1939     }
1940     }
1941     }
1942    
1943 gbeauche 1.18 /*
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 gbeauche 1.19 bool NQD_bitblt_hook(uint32 p)
1965 gbeauche 1.16 {
1966 gbeauche 1.19 D(bug("accl_draw_hook %08x\n", p));
1967 gbeauche 1.16
1968     // Check if we can accelerate this bitblt
1969 gbeauche 1.19 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 gbeauche 1.16
1977     // Yes, set function pointer
1978 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1979 gbeauche 1.16 return true;
1980     }
1981     return false;
1982     }
1983    
1984 cebix 1.1 // Wait for graphics operation to finish
1985 gbeauche 1.16 bool NQD_sync_hook(uint32 arg)
1986 cebix 1.1 {
1987 gbeauche 1.16 D(bug("accl_sync_hook %08x\n", arg));
1988 cebix 1.1 return true;
1989     }
1990    
1991     void VideoInstallAccel(void)
1992     {
1993     // Install acceleration hooks
1994     if (PrefsFindBool("gfxaccel")) {
1995     D(bug("Video: Installing acceleration hooks\n"));
1996 gbeauche 1.16 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 gbeauche 1.18 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 cebix 1.1 }
2012     }
2013    
2014    
2015     /*
2016     * Change video mode
2017     */
2018    
2019     int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
2020     {
2021     /* return if no mode change */
2022     if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
2023     (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
2024    
2025     /* first find video mode in table */
2026     for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
2027     if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
2028     (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
2029     csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
2030     csSave->saveData = ReadMacInt32(ParamPtr + csData);
2031     csSave->savePage = ReadMacInt16(ParamPtr + csPage);
2032    
2033     // Disable interrupts and pause redraw thread
2034     DisableInterrupt();
2035     thread_stop_ack = false;
2036     thread_stop_req = true;
2037     while (!thread_stop_ack) ;
2038    
2039     /* close old display */
2040     close_display();
2041    
2042     /* open new display */
2043     cur_mode = i;
2044     bool ok = open_display();
2045    
2046     /* opening the screen failed? Then bail out */
2047     if (!ok) {
2048     ErrorAlert(GetString(STR_FULL_SCREEN_ERR));
2049     QuitEmulator();
2050     }
2051    
2052     WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
2053     csSave->saveBaseAddr=screen_base;
2054     csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
2055     csSave->saveMode=VModes[cur_mode].viAppleMode;
2056    
2057     // Enable interrupts and resume redraw thread
2058     thread_stop_req = false;
2059     EnableInterrupt();
2060     return noErr;
2061     }
2062     }
2063     return paramErr;
2064     }
2065    
2066    
2067     /*
2068     * Set color palette
2069     */
2070    
2071     void video_set_palette(void)
2072     {
2073 gbeauche 1.13 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 cebix 1.1 palette_changed = true;
2112 gbeauche 1.13
2113     UNLOCK_PALETTE;
2114 cebix 1.1 }
2115    
2116    
2117     /*
2118 gbeauche 1.21 * Can we set the MacOS cursor image into the window?
2119     */
2120    
2121     bool video_can_change_cursor(void)
2122     {
2123 gbeauche 1.22 return hw_mac_cursor_accl && (display_type != DIS_SCREEN);
2124 gbeauche 1.21 }
2125    
2126    
2127     /*
2128 cebix 1.1 * Set cursor image for window
2129     */
2130    
2131     void video_set_cursor(void)
2132     {
2133     cursor_changed = true;
2134     }
2135    
2136    
2137     /*
2138     * Thread for window refresh, event handling and other periodic actions
2139     */
2140    
2141     static void update_display(void)
2142     {
2143     // Incremental update code
2144     int wide = 0, high = 0, x1, x2, y1, y2, i, j;
2145     int bytes_per_row = VModes[cur_mode].viRowBytes;
2146     int bytes_per_pixel = VModes[cur_mode].viRowBytes / VModes[cur_mode].viXsize;
2147     uint8 *p, *p2;
2148    
2149     // Check for first line from top and first line from bottom that have changed
2150     y1 = 0;
2151     for (j=0; j<VModes[cur_mode].viYsize; j++) {
2152     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2153     y1 = j;
2154     break;
2155     }
2156     }
2157     y2 = y1 - 1;
2158     for (j=VModes[cur_mode].viYsize-1; j>=y1; j--) {
2159     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2160     y2 = j;
2161     break;
2162     }
2163     }
2164     high = y2 - y1 + 1;
2165    
2166     // Check for first column from left and first column from right that have changed
2167     if (high) {
2168     if (depth == 1) {
2169     x1 = VModes[cur_mode].viXsize;
2170     for (j=y1; j<=y2; j++) {
2171     p = &the_buffer[j * bytes_per_row];
2172     p2 = &the_buffer_copy[j * bytes_per_row];
2173     for (i=0; i<(x1>>3); i++) {
2174     if (*p != *p2) {
2175     x1 = i << 3;
2176     break;
2177     }
2178     p++;
2179     p2++;
2180     }
2181     }
2182     x2 = x1;
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     p += bytes_per_row;
2187     p2 += bytes_per_row;
2188     for (i=(VModes[cur_mode].viXsize>>3); i>(x2>>3); i--) {
2189     p--;
2190     p2--;
2191     if (*p != *p2) {
2192     x2 = i << 3;
2193     break;
2194     }
2195     }
2196     }
2197     wide = x2 - x1;
2198    
2199     // Update copy of the_buffer
2200     if (high && wide) {
2201     for (j=y1; j<=y2; j++) {
2202     i = j * bytes_per_row + (x1 >> 3);
2203     memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
2204     }
2205     }
2206    
2207     } else {
2208     x1 = VModes[cur_mode].viXsize;
2209     for (j=y1; j<=y2; j++) {
2210     p = &the_buffer[j * bytes_per_row];
2211     p2 = &the_buffer_copy[j * bytes_per_row];
2212     for (i=0; i<x1; i++) {
2213     if (memcmp(p, p2, bytes_per_pixel)) {
2214     x1 = i;
2215     break;
2216     }
2217     p += bytes_per_pixel;
2218     p2 += bytes_per_pixel;
2219     }
2220     }
2221     x2 = x1;
2222     for (j=y1; j<=y2; j++) {
2223     p = &the_buffer[j * bytes_per_row];
2224     p2 = &the_buffer_copy[j * bytes_per_row];
2225     p += bytes_per_row;
2226     p2 += bytes_per_row;
2227     for (i=VModes[cur_mode].viXsize; i>x2; i--) {
2228     p -= bytes_per_pixel;
2229     p2 -= bytes_per_pixel;
2230     if (memcmp(p, p2, bytes_per_pixel)) {
2231     x2 = i;
2232     break;
2233     }
2234     }
2235     }
2236     wide = x2 - x1;
2237    
2238     // Update copy of the_buffer
2239     if (high && wide) {
2240     for (j=y1; j<=y2; j++) {
2241     i = j * bytes_per_row + x1 * bytes_per_pixel;
2242     memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
2243     }
2244     }
2245     }
2246     }
2247    
2248     // Refresh display
2249     if (high && wide) {
2250 gbeauche 1.10 XDisplayLock();
2251 cebix 1.1 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 gbeauche 1.10 XDisplayUnlock();
2256 cebix 1.1 }
2257     }
2258    
2259 gbeauche 1.10 const int VIDEO_REFRESH_HZ = 60;
2260     const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2261    
2262 gbeauche 1.13 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 cebix 1.1 static void *redraw_func(void *arg)
2300     {
2301 gbeauche 1.10 int fd = ConnectionNumber(x_display);
2302    
2303     uint64 start = GetTicks_usec();
2304     int64 ticks = 0;
2305     uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2306 cebix 1.1
2307 gbeauche 1.28 while (!redraw_thread_cancel) {
2308 cebix 1.1
2309     // Pause if requested (during video mode switches)
2310     while (thread_stop_req)
2311     thread_stop_ack = true;
2312    
2313 gbeauche 1.10 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 cebix 1.1
2319 gbeauche 1.10 } 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) {
2332     XDisplayLock();
2333 cebix 1.1 #ifdef ENABLE_XF86_DGA
2334 gbeauche 1.10 XF86DGADirectVideo(x_display, screen, 0);
2335     XUngrabPointer(x_display, CurrentTime);
2336     XUngrabKeyboard(x_display, CurrentTime);
2337 gbeauche 1.15 XUnmapWindow(x_display, the_win);
2338     wait_unmapped(the_win);
2339     XDestroyWindow(x_display, the_win);
2340 gbeauche 1.10 #endif
2341     XSync(x_display, false);
2342     XDisplayUnlock();
2343     quit_full_screen_ack = true;
2344     return NULL;
2345     }
2346 cebix 1.1 }
2347    
2348 gbeauche 1.10 // 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 cebix 1.1
2355 gbeauche 1.10 // Update display
2356 gbeauche 1.3 #ifdef ENABLE_VOSF
2357 gbeauche 1.10 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 gbeauche 1.3 }
2367 gbeauche 1.10 else
2368 gbeauche 1.3 #endif
2369 gbeauche 1.10 update_display();
2370 cebix 1.1
2371 gbeauche 1.10 // Set new cursor image if it was changed
2372 gbeauche 1.22 if (hw_mac_cursor_accl && cursor_changed) {
2373 gbeauche 1.10 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 cebix 1.1 }
2385     }
2386 gbeauche 1.3 #ifdef ENABLE_VOSF
2387 gbeauche 1.10 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 gbeauche 1.3 }
2397     }
2398     #endif
2399 cebix 1.1
2400 gbeauche 1.10 // Set new palette if it was changed
2401 gbeauche 1.13 handle_palette_changes();
2402 gbeauche 1.10
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 cebix 1.1 }
2415     }
2416     return NULL;
2417     }