ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.70
Committed: 2004-01-12T15:29:25Z (20 years, 10 months ago) by cebix
Branch: MAIN
CVS Tags: nigel-build-16, nigel-build-15
Changes since 1.69: +1 -1 lines
Log Message:
Happy New Year! :)

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * video_x.cpp - Video/graphics emulation, X11 specific stuff
3     *
4 cebix 1.70 * Basilisk II (C) 1997-2004 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     /*
22     * NOTES:
23     * The Ctrl key works like a qualifier for special actions:
24     * Ctrl-Tab = suspend DGA mode
25     * Ctrl-Esc = emergency quit
26     * Ctrl-F1 = mount floppy
27 cebix 1.51 * Ctrl-F5 = grab mouse (in windowed mode)
28 cebix 1.1 */
29    
30     #include "sysdeps.h"
31    
32     #include <X11/Xlib.h>
33     #include <X11/Xutil.h>
34     #include <X11/keysym.h>
35     #include <X11/extensions/XShm.h>
36     #include <sys/ipc.h>
37     #include <sys/shm.h>
38     #include <errno.h>
39    
40 cebix 1.53 #include <algorithm>
41    
42 cebix 1.15 #ifdef HAVE_PTHREADS
43     # include <pthread.h>
44     #endif
45    
46     #ifdef ENABLE_XF86_DGA
47     # include <X11/extensions/xf86dga.h>
48     #endif
49    
50     #ifdef ENABLE_XF86_VIDMODE
51     # include <X11/extensions/xf86vmode.h>
52     #endif
53    
54     #ifdef ENABLE_FBDEV_DGA
55     # include <sys/mman.h>
56     #endif
57    
58 cebix 1.1 #include "cpu_emulation.h"
59     #include "main.h"
60     #include "adb.h"
61     #include "macos_util.h"
62     #include "prefs.h"
63     #include "user_strings.h"
64     #include "video.h"
65    
66 cebix 1.55 #define DEBUG 0
67 cebix 1.1 #include "debug.h"
68    
69    
70 cebix 1.66 // Supported video modes
71     static vector<video_mode> VideoModes;
72    
73 cebix 1.1 // Display types
74     enum {
75     DISPLAY_WINDOW, // X11 window, using MIT SHM extensions if possible
76     DISPLAY_DGA // DGA fullscreen display
77     };
78    
79     // Constants
80 cebix 1.4 const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
81 cebix 1.44
82 cebix 1.57 static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
83 cebix 1.44 static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
84 cebix 1.1
85    
86     // Global variables
87 cebix 1.10 static int32 frame_skip; // Prefs items
88 cebix 1.44 static int16 mouse_wheel_mode;
89     static int16 mouse_wheel_lines;
90 cebix 1.10
91 cebix 1.1 static int display_type = DISPLAY_WINDOW; // See enum above
92 cebix 1.16 static bool local_X11; // Flag: X server running on local machine?
93 cebix 1.44 static uint8 *the_buffer = NULL; // Mac frame buffer (where MacOS draws into)
94     static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer (for refreshed modes)
95 gbeauche 1.56 static uint32 the_buffer_size; // Size of allocated the_buffer
96 cebix 1.15
97 cebix 1.45 static bool redraw_thread_active = false; // Flag: Redraw thread installed
98 cebix 1.15 #ifdef HAVE_PTHREADS
99 cebix 1.64 static pthread_attr_t redraw_thread_attr; // Redraw thread attributes
100 cebix 1.42 static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread
101 cebix 1.1 static pthread_t redraw_thread; // Redraw thread
102 cebix 1.15 #endif
103 cebix 1.1
104     static bool has_dga = false; // Flag: Video DGA capable
105 cebix 1.12 static bool has_vidmode = false; // Flag: VidMode extension available
106 cebix 1.1
107 cebix 1.44 #ifdef ENABLE_VOSF
108     static bool use_vosf = true; // Flag: VOSF enabled
109     #else
110     static const bool use_vosf = false; // VOSF not possible
111     #endif
112    
113 cebix 1.1 static bool ctrl_down = false; // Flag: Ctrl key pressed
114 cebix 1.3 static bool caps_on = false; // Flag: Caps Lock on
115 cebix 1.1 static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
116     static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
117     static bool emul_suspended = false; // Flag: Emulator suspended
118    
119     static bool classic_mode = false; // Flag: Classic Mac video mode
120    
121     static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
122     static int keycode_table[256]; // X keycode -> Mac keycode translation table
123    
124     // X11 variables
125     static int screen; // Screen number
126 cebix 1.44 static Window rootwin; // Root window and our window
127 cebix 1.53 static int num_depths = 0; // Number of available X depths
128     static int *avail_depths = NULL; // List of available X depths
129 cebix 1.1 static XColor black, white;
130     static unsigned long black_pixel, white_pixel;
131     static int eventmask;
132    
133 cebix 1.53 static int xdepth; // Depth of X screen
134     static XVisualInfo visualInfo;
135     static Visual *vis;
136     static int color_class;
137    
138 cebix 1.48 static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
139    
140 cebix 1.53 static Colormap cmap[2] = {0, 0}; // Colormaps for indexed modes (DGA needs two of them)
141    
142 cebix 1.66 static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table
143     static bool x_palette_changed = false; // Flag: Palette changed, redraw thread must set new colors
144 cebix 1.44
145     #ifdef ENABLE_FBDEV_DGA
146     static int fbdev_fd = -1;
147     #endif
148    
149     #ifdef ENABLE_XF86_VIDMODE
150     static XF86VidModeModeInfo **x_video_modes = NULL; // Array of all available modes
151     static int num_x_video_modes;
152     #endif
153    
154     // Mutex to protect palette
155 cebix 1.15 #ifdef HAVE_PTHREADS
156 cebix 1.66 static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER;
157     #define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock)
158     #define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock)
159 cebix 1.29 #else
160     #define LOCK_PALETTE
161     #define UNLOCK_PALETTE
162 cebix 1.15 #endif
163 cebix 1.1
164 cebix 1.44 // Mutex to protect frame buffer
165 cebix 1.15 #ifdef HAVE_PTHREADS
166 cebix 1.44 static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
167 cebix 1.29 #define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock);
168     #define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock);
169     #else
170     #define LOCK_FRAME_BUFFER
171     #define UNLOCK_FRAME_BUFFER
172 cebix 1.15 #endif
173 cebix 1.1
174 cebix 1.44 // Variables for non-VOSF incremental refresh
175     static const int sm_uptd[] = {4,1,6,3,0,5,2,7};
176     static int sm_no_boxes[] = {1,8,32,64,128,300};
177     static bool updt_box[17][17];
178     static int nr_boxes;
179 cebix 1.12
180 cebix 1.44 // Video refresh function
181 cebix 1.42 static void VideoRefreshInit(void);
182 gbeauche 1.19 static void (*video_refresh)(void);
183 cebix 1.1
184 cebix 1.44
185 cebix 1.1 // Prototypes
186     static void *redraw_func(void *arg);
187    
188     // From main_unix.cpp
189 cebix 1.16 extern char *x_display_name;
190 cebix 1.1 extern Display *x_display;
191    
192     // From sys_unix.cpp
193     extern void SysMountFirstFloppy(void);
194    
195 cebix 1.42
196 cebix 1.1 /*
197 cebix 1.66 * monitor_desc subclass for X11 display
198     */
199    
200     class X11_monitor_desc : public monitor_desc {
201     public:
202     X11_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
203     ~X11_monitor_desc() {}
204    
205     virtual void switch_to_current_mode(void);
206     virtual void set_palette(uint8 *pal, int num);
207    
208     bool video_open(void);
209     void video_close(void);
210     };
211    
212    
213     /*
214 cebix 1.44 * Utility functions
215 cebix 1.1 */
216    
217 gbeauche 1.69 // Map video_mode depth ID to numerical depth value
218     static inline int depth_of_video_mode(video_mode const & mode)
219     {
220     int depth = -1;
221     switch (mode.depth) {
222     case VDEPTH_1BIT:
223     depth = 1;
224     break;
225     case VDEPTH_2BIT:
226     depth = 2;
227     break;
228     case VDEPTH_4BIT:
229     depth = 4;
230     break;
231     case VDEPTH_8BIT:
232     depth = 8;
233     break;
234     case VDEPTH_16BIT:
235     depth = 16;
236     break;
237     case VDEPTH_32BIT:
238     depth = 32;
239     break;
240     default:
241     abort();
242     }
243     return depth;
244     }
245    
246 cebix 1.49 // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
247     static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
248     {
249     return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift);
250     }
251    
252 cebix 1.53 // Do we have a visual for handling the specified Mac depth? If so, set the
253     // global variables "xdepth", "visualInfo", "vis" and "color_class".
254     static bool find_visual_for_depth(video_depth depth)
255     {
256     D(bug("have_visual_for_depth(%d)\n", 1 << depth));
257    
258 cebix 1.61 // 1-bit works always and uses default visual
259     if (depth == VDEPTH_1BIT) {
260     vis = DefaultVisual(x_display, screen);
261     visualInfo.visualid = XVisualIDFromVisual(vis);
262     int num = 0;
263     XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num);
264     visualInfo = vi[0];
265     XFree(vi);
266     xdepth = visualInfo.depth;
267     color_class = visualInfo.c_class;
268     D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth));
269     return true;
270     }
271    
272 cebix 1.53 // Calculate minimum and maximum supported X depth
273     int min_depth = 1, max_depth = 32;
274     switch (depth) {
275     #ifdef ENABLE_VOSF
276     case VDEPTH_2BIT:
277 cebix 1.54 case VDEPTH_4BIT: // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
278     case VDEPTH_8BIT:
279 cebix 1.53 min_depth = 8;
280     max_depth = 32;
281     break;
282     #else
283     case VDEPTH_2BIT:
284     case VDEPTH_4BIT: // 2/4-bit requires VOSF blitters
285     return false;
286     case VDEPTH_8BIT: // 8-bit without VOSF requires an 8-bit visual
287     min_depth = 8;
288     max_depth = 8;
289     break;
290     #endif
291     case VDEPTH_16BIT: // 16-bit requires a 15/16-bit visual
292     min_depth = 15;
293     max_depth = 16;
294     break;
295     case VDEPTH_32BIT: // 32-bit requires a 24/32-bit visual
296     min_depth = 24;
297     max_depth = 32;
298     break;
299     }
300     D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth));
301    
302     // Try to find a visual for one of the color depths
303     bool visual_found = false;
304     for (int i=0; i<num_depths && !visual_found; i++) {
305    
306     xdepth = avail_depths[i];
307     D(bug(" trying to find visual for depth %d\n", xdepth));
308     if (xdepth < min_depth || xdepth > max_depth)
309     continue;
310    
311     // Determine best color class for this depth
312     switch (xdepth) {
313     case 1: // Try StaticGray or StaticColor
314     if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo)
315     || XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo))
316     visual_found = true;
317     break;
318     case 8: // Need PseudoColor
319     if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo))
320     visual_found = true;
321     break;
322     case 15:
323     case 16:
324     case 24:
325     case 32: // Try DirectColor first, as this will allow gamma correction
326     if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo)
327     || XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo))
328     visual_found = true;
329     break;
330     default:
331     D(bug(" not a supported depth\n"));
332     break;
333     }
334     }
335     if (!visual_found)
336     return false;
337    
338     // Visual was found
339     vis = visualInfo.visual;
340     color_class = visualInfo.c_class;
341     D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth));
342     #if DEBUG
343     switch (color_class) {
344     case StaticGray: D(bug("StaticGray\n")); break;
345     case GrayScale: D(bug("GrayScale\n")); break;
346     case StaticColor: D(bug("StaticColor\n")); break;
347     case PseudoColor: D(bug("PseudoColor\n")); break;
348     case TrueColor: D(bug("TrueColor\n")); break;
349     case DirectColor: D(bug("DirectColor\n")); break;
350     }
351     #endif
352 cebix 1.59 return true;
353 cebix 1.53 }
354    
355 cebix 1.42 // Add mode to list of supported modes
356     static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth)
357 cebix 1.1 {
358 cebix 1.40 video_mode mode;
359 cebix 1.42 mode.x = width;
360     mode.y = height;
361     mode.resolution_id = resolution_id;
362     mode.bytes_per_row = bytes_per_row;
363     mode.depth = depth;
364     VideoModes.push_back(mode);
365     }
366    
367 cebix 1.50 // Add standard list of windowed modes for given color depth
368     static void add_window_modes(video_depth depth)
369     {
370     add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth);
371     add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth);
372     add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth);
373     add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth), depth);
374     add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth);
375     add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth);
376     add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth);
377     }
378    
379 cebix 1.42 // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
380 cebix 1.66 static void set_mac_frame_buffer(X11_monitor_desc &monitor, video_depth depth, bool native_byte_order)
381 cebix 1.42 {
382 gbeauche 1.19 #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
383 cebix 1.5 int layout = FLAYOUT_DIRECT;
384 cebix 1.42 if (depth == VDEPTH_16BIT)
385     layout = (xdepth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
386     else if (depth == VDEPTH_32BIT)
387 cebix 1.48 layout = (xdepth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
388 cebix 1.15 if (native_byte_order)
389     MacFrameLayout = layout;
390     else
391     MacFrameLayout = FLAYOUT_DIRECT;
392 cebix 1.66 monitor.set_mac_frame_base(MacFrameBaseMac);
393 cebix 1.44
394     // Set variables used by UAE memory banking
395 cebix 1.66 const video_mode &mode = monitor.get_current_mode();
396 cebix 1.44 MacFrameBaseHost = the_buffer;
397 cebix 1.66 MacFrameSize = mode.bytes_per_row * mode.y;
398 cebix 1.44 InitFrameBufferMapping();
399 cebix 1.42 #else
400 cebix 1.66 monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
401 cebix 1.15 #endif
402 cebix 1.66 D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
403 cebix 1.1 }
404    
405 cebix 1.29 // Set window name and class
406     static void set_window_name(Window w, int name)
407     {
408     const char *str = GetString(name);
409     XStoreName(x_display, w, str);
410     XSetIconName(x_display, w, str);
411    
412     XClassHint *hints;
413     hints = XAllocClassHint();
414     if (hints) {
415     hints->res_name = "BasiliskII";
416     hints->res_class = "BasiliskII";
417     XSetClassHint(x_display, w, hints);
418 cebix 1.33 XFree(hints);
419 cebix 1.29 }
420     }
421    
422     // Set window input focus flag
423     static void set_window_focus(Window w)
424     {
425     XWMHints *hints = XAllocWMHints();
426     if (hints) {
427     hints->input = True;
428     hints->initial_state = NormalState;
429     hints->flags = InputHint | StateHint;
430     XSetWMHints(x_display, w, hints);
431 cebix 1.33 XFree(hints);
432 cebix 1.29 }
433     }
434    
435     // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
436 cebix 1.44 static Atom WM_DELETE_WINDOW = (Atom)0;
437 cebix 1.29 static void set_window_delete_protocol(Window w)
438     {
439     WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
440     XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
441     }
442    
443     // Wait until window is mapped/unmapped
444     void wait_mapped(Window w)
445     {
446     XEvent e;
447     do {
448     XMaskEvent(x_display, StructureNotifyMask, &e);
449     } while ((e.type != MapNotify) || (e.xmap.event != w));
450     }
451    
452     void wait_unmapped(Window w)
453     {
454     XEvent e;
455     do {
456     XMaskEvent(x_display, StructureNotifyMask, &e);
457     } while ((e.type != UnmapNotify) || (e.xmap.event != w));
458     }
459    
460 cebix 1.1 // Trap SHM errors
461     static bool shm_error = false;
462     static int (*old_error_handler)(Display *, XErrorEvent *);
463    
464     static int error_handler(Display *d, XErrorEvent *e)
465     {
466     if (e->error_code == BadAccess) {
467     shm_error = true;
468     return 0;
469     } else
470     return old_error_handler(d, e);
471     }
472    
473 cebix 1.44
474     /*
475     * Display "driver" classes
476     */
477    
478     class driver_base {
479     public:
480 cebix 1.66 driver_base(X11_monitor_desc &m);
481 cebix 1.44 virtual ~driver_base();
482    
483     virtual void update_palette(void);
484     virtual void suspend(void) {}
485     virtual void resume(void) {}
486 cebix 1.51 virtual void toggle_mouse_grab(void) {}
487     virtual void mouse_moved(int x, int y) { ADBMouseMoved(x, y); }
488    
489 cebix 1.57 void disable_mouse_accel(void);
490     void restore_mouse_accel(void);
491    
492 cebix 1.51 virtual void grab_mouse(void) {}
493     virtual void ungrab_mouse(void) {}
494 cebix 1.44
495     public:
496 cebix 1.66 X11_monitor_desc &monitor; // Associated video monitor
497     const video_mode &mode; // Video mode handled by the driver
498    
499 cebix 1.44 bool init_ok; // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
500     Window w; // The window we draw into
501 cebix 1.57
502     int orig_accel_numer, orig_accel_denom, orig_threshold; // Original mouse acceleration
503 cebix 1.44 };
504    
505     class driver_window;
506     static void update_display_window_vosf(driver_window *drv);
507     static void update_display_dynamic(int ticker, driver_window *drv);
508     static void update_display_static(driver_window *drv);
509    
510     class driver_window : public driver_base {
511     friend void update_display_window_vosf(driver_window *drv);
512     friend void update_display_dynamic(int ticker, driver_window *drv);
513     friend void update_display_static(driver_window *drv);
514    
515     public:
516 cebix 1.66 driver_window(X11_monitor_desc &monitor);
517 cebix 1.44 ~driver_window();
518    
519 cebix 1.51 void toggle_mouse_grab(void);
520     void mouse_moved(int x, int y);
521    
522     void grab_mouse(void);
523     void ungrab_mouse(void);
524    
525 cebix 1.44 private:
526     GC gc;
527     XImage *img;
528 cebix 1.51 bool have_shm; // Flag: SHM extensions available
529 cebix 1.44 XShmSegmentInfo shminfo;
530     Cursor mac_cursor;
531 cebix 1.51 bool mouse_grabbed; // Flag: mouse pointer grabbed, using relative mouse mode
532     int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode)
533 cebix 1.44 };
534    
535     static driver_base *drv = NULL; // Pointer to currently used driver object
536    
537     #ifdef ENABLE_VOSF
538     # include "video_vosf.h"
539     #endif
540    
541 cebix 1.66 driver_base::driver_base(X11_monitor_desc &m)
542     : monitor(m), mode(m.get_current_mode()), init_ok(false), w(0)
543 cebix 1.44 {
544     the_buffer = NULL;
545     the_buffer_copy = NULL;
546 cebix 1.57 XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold);
547 cebix 1.44 }
548    
549     driver_base::~driver_base()
550     {
551 cebix 1.51 ungrab_mouse();
552 cebix 1.57 restore_mouse_accel();
553 cebix 1.51
554 cebix 1.44 if (w) {
555     XUnmapWindow(x_display, w);
556     wait_unmapped(w);
557     XDestroyWindow(x_display, w);
558     }
559    
560 cebix 1.45 XFlush(x_display);
561     XSync(x_display, false);
562    
563 cebix 1.44 // Free frame buffer(s)
564     if (!use_vosf) {
565     if (the_buffer) {
566     free(the_buffer);
567     the_buffer = NULL;
568     }
569     if (the_buffer_copy) {
570     free(the_buffer_copy);
571     the_buffer_copy = NULL;
572     }
573     }
574     #ifdef ENABLE_VOSF
575     else {
576 gbeauche 1.62 // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
577     if (the_buffer != VM_MAP_FAILED) {
578     D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
579     vm_release(the_buffer, the_buffer_size);
580     the_buffer = NULL;
581     }
582 cebix 1.55 if (the_host_buffer) {
583 cebix 1.61 D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
584 cebix 1.55 free(the_host_buffer);
585     the_host_buffer = NULL;
586     }
587 gbeauche 1.56 if (the_buffer_copy) {
588 cebix 1.61 D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
589 gbeauche 1.56 free(the_buffer_copy);
590 cebix 1.44 the_buffer_copy = NULL;
591     }
592     }
593     #endif
594     }
595    
596     // Palette has changed
597     void driver_base::update_palette(void)
598     {
599 cebix 1.58 if (color_class == PseudoColor || color_class == DirectColor) {
600 cebix 1.54 int num = vis->map_entries;
601 cebix 1.66 if (!IsDirectMode(monitor.get_current_mode()) && color_class == DirectColor)
602 cebix 1.47 return; // Indexed mode on true color screen, don't set CLUT
603 cebix 1.66 XStoreColors(x_display, cmap[0], x_palette, num);
604     XStoreColors(x_display, cmap[1], x_palette, num);
605 cebix 1.44 }
606     XSync(x_display, false);
607     }
608    
609 cebix 1.57 // Disable mouse acceleration
610     void driver_base::disable_mouse_accel(void)
611     {
612     XChangePointerControl(x_display, True, False, 1, 1, 0);
613     }
614    
615     // Restore mouse acceleration to original value
616     void driver_base::restore_mouse_accel(void)
617     {
618     XChangePointerControl(x_display, True, True, orig_accel_numer, orig_accel_denom, orig_threshold);
619     }
620    
621 cebix 1.44
622     /*
623     * Windowed display driver
624     */
625    
626     // Open display
627 cebix 1.66 driver_window::driver_window(X11_monitor_desc &m)
628     : driver_base(m), gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
629 cebix 1.1 {
630 cebix 1.42 int width = mode.x, height = mode.y;
631 cebix 1.13 int aligned_width = (width + 15) & ~15;
632     int aligned_height = (height + 15) & ~15;
633    
634 cebix 1.1 // Set absolute mouse mode
635 cebix 1.51 ADBSetRelMouseMode(mouse_grabbed);
636 cebix 1.1
637 gbeauche 1.62 // Create window (setting background_pixel, border_pixel and colormap is
638 cebix 1.58 // mandatory when using a non-default visual; in 1-bit mode we use the
639     // default visual, so we can also use the default colormap)
640 cebix 1.1 XSetWindowAttributes wattr;
641     wattr.event_mask = eventmask = win_eventmask;
642 cebix 1.58 wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
643     wattr.border_pixel = 0;
644     wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
645 cebix 1.44 w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
646 cebix 1.58 InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWColormap, &wattr);
647 cebix 1.61 D(bug(" window created\n"));
648 cebix 1.29
649     // Set window name/class
650 cebix 1.44 set_window_name(w, STR_WINDOW_TITLE);
651 cebix 1.26
652     // Indicate that we want keyboard input
653 cebix 1.44 set_window_focus(w);
654 cebix 1.29
655     // Set delete protocol property
656 cebix 1.44 set_window_delete_protocol(w);
657 cebix 1.26
658     // Make window unresizable
659     {
660     XSizeHints *hints = XAllocSizeHints();
661     if (hints) {
662     hints->min_width = width;
663     hints->max_width = width;
664     hints->min_height = height;
665     hints->max_height = height;
666     hints->flags = PMinSize | PMaxSize;
667 cebix 1.44 XSetWMNormalHints(x_display, w, hints);
668 cebix 1.33 XFree(hints);
669 cebix 1.26 }
670     }
671 cebix 1.61 D(bug(" window attributes set\n"));
672 cebix 1.26
673     // Show window
674 cebix 1.44 XMapWindow(x_display, w);
675     wait_mapped(w);
676 cebix 1.61 D(bug(" window mapped\n"));
677 cebix 1.1
678 cebix 1.48 // 1-bit mode is big-endian; if the X server is little-endian, we can't
679     // use SHM because that doesn't allow changing the image byte order
680     bool need_msb_image = (mode.depth == VDEPTH_1BIT && XImageByteOrder(x_display) == LSBFirst);
681    
682 cebix 1.1 // Try to create and attach SHM image
683 cebix 1.48 if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) {
684 cebix 1.1
685     // Create SHM image ("height + 2" for safety)
686 cebix 1.42 img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
687 cebix 1.61 D(bug(" shm image created\n"));
688 cebix 1.13 shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
689     the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
690     shminfo.shmaddr = img->data = (char *)the_buffer_copy;
691 cebix 1.1 shminfo.readOnly = False;
692    
693     // Try to attach SHM image, catching errors
694     shm_error = false;
695     old_error_handler = XSetErrorHandler(error_handler);
696     XShmAttach(x_display, &shminfo);
697     XSync(x_display, false);
698     XSetErrorHandler(old_error_handler);
699     if (shm_error) {
700     shmdt(shminfo.shmaddr);
701     XDestroyImage(img);
702 cebix 1.55 img = NULL;
703 cebix 1.1 shminfo.shmid = -1;
704     } else {
705     have_shm = true;
706     shmctl(shminfo.shmid, IPC_RMID, 0);
707     }
708 cebix 1.61 D(bug(" shm image attached\n"));
709 cebix 1.1 }
710 cebix 1.7
711 cebix 1.1 // Create normal X image if SHM doesn't work ("height + 2" for safety)
712     if (!have_shm) {
713 cebix 1.49 int bytes_per_row = (mode.depth == VDEPTH_1BIT ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth)));
714 cebix 1.13 the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
715 cebix 1.42 img = XCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
716 cebix 1.61 D(bug(" X image created\n"));
717 cebix 1.1 }
718    
719 cebix 1.48 if (need_msb_image) {
720 cebix 1.1 img->byte_order = MSBFirst;
721     img->bitmap_bit_order = MSBFirst;
722     }
723    
724 gbeauche 1.19 #ifdef ENABLE_VOSF
725 gbeauche 1.56 use_vosf = true;
726 gbeauche 1.39 // Allocate memory for frame buffer (SIZE is extended to page-boundary)
727 gbeauche 1.19 the_host_buffer = the_buffer_copy;
728 gbeauche 1.39 the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
729     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
730 gbeauche 1.62 the_buffer_copy = (uint8 *)malloc(the_buffer_size);
731 cebix 1.55 D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
732 gbeauche 1.19 #else
733 cebix 1.16 // Allocate memory for frame buffer
734 cebix 1.13 the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
735 cebix 1.55 D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
736 gbeauche 1.19 #endif
737 cebix 1.1
738     // Create GC
739 cebix 1.44 gc = XCreateGC(x_display, w, 0, 0);
740     XSetState(x_display, gc, black_pixel, white_pixel, GXcopy, AllPlanes);
741 cebix 1.1
742 cebix 1.13 // Create no_cursor
743 cebix 1.16 mac_cursor = XCreatePixmapCursor(x_display,
744 cebix 1.44 XCreatePixmap(x_display, w, 1, 1, 1),
745     XCreatePixmap(x_display, w, 1, 1, 1),
746 cebix 1.16 &black, &white, 0, 0);
747 cebix 1.44 XDefineCursor(x_display, w, mac_cursor);
748 cebix 1.1
749 cebix 1.44 // Init blitting routines
750 gbeauche 1.19 bool native_byte_order;
751 cebix 1.1 #ifdef WORDS_BIGENDIAN
752 cebix 1.31 native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
753 cebix 1.1 #else
754 cebix 1.31 native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
755 cebix 1.1 #endif
756 gbeauche 1.19 #ifdef ENABLE_VOSF
757 gbeauche 1.69 Screen_blitter_init(&visualInfo, native_byte_order, depth_of_video_mode(mode));
758 gbeauche 1.19 #endif
759 cebix 1.44
760 cebix 1.66 // Set frame buffer base
761     set_mac_frame_buffer(monitor, mode.depth, native_byte_order);
762 cebix 1.44
763     // Everything went well
764     init_ok = true;
765     }
766    
767     // Close display
768     driver_window::~driver_window()
769     {
770 cebix 1.55 if (have_shm) {
771     XShmDetach(x_display, &shminfo);
772     #ifdef ENABLE_VOSF
773     the_host_buffer = NULL; // don't free() in driver_base dtor
774     #else
775     the_buffer_copy = NULL; // don't free() in driver_base dtor
776     #endif
777     }
778 cebix 1.61 if (img) {
779     if (!have_shm)
780     img->data = NULL;
781 cebix 1.44 XDestroyImage(img);
782 cebix 1.61 }
783 cebix 1.44 if (have_shm) {
784 cebix 1.55 shmdt(shminfo.shmaddr);
785     shmctl(shminfo.shmid, IPC_RMID, 0);
786 cebix 1.44 }
787     if (gc)
788     XFreeGC(x_display, gc);
789     }
790    
791 cebix 1.51 // Toggle mouse grab
792     void driver_window::toggle_mouse_grab(void)
793     {
794     if (mouse_grabbed)
795     ungrab_mouse();
796     else
797     grab_mouse();
798     }
799    
800     // Grab mouse, switch to relative mouse mode
801     void driver_window::grab_mouse(void)
802     {
803     int result;
804     for (int i=0; i<10; i++) {
805     result = XGrabPointer(x_display, w, True, 0,
806     GrabModeAsync, GrabModeAsync, w, None, CurrentTime);
807     if (result != AlreadyGrabbed)
808     break;
809     Delay_usec(100000);
810     }
811     if (result == GrabSuccess) {
812 cebix 1.57 XStoreName(x_display, w, GetString(STR_WINDOW_TITLE_GRABBED));
813 cebix 1.51 ADBSetRelMouseMode(mouse_grabbed = true);
814 cebix 1.57 disable_mouse_accel();
815 cebix 1.51 }
816     }
817    
818     // Ungrab mouse, switch to absolute mouse mode
819     void driver_window::ungrab_mouse(void)
820     {
821     if (mouse_grabbed) {
822     XUngrabPointer(x_display, CurrentTime);
823     XStoreName(x_display, w, GetString(STR_WINDOW_TITLE));
824     ADBSetRelMouseMode(mouse_grabbed = false);
825 cebix 1.57 restore_mouse_accel();
826 cebix 1.51 }
827     }
828    
829     // Mouse moved
830     void driver_window::mouse_moved(int x, int y)
831     {
832     if (!mouse_grabbed) {
833     mouse_last_x = x; mouse_last_y = y;
834     ADBMouseMoved(x, y);
835     return;
836     }
837    
838     // Warped mouse motion (this code is taken from SDL)
839    
840     // Post first mouse event
841 cebix 1.66 int width = monitor.get_current_mode().x, height = monitor.get_current_mode().y;
842 cebix 1.51 int delta_x = x - mouse_last_x, delta_y = y - mouse_last_y;
843     mouse_last_x = x; mouse_last_y = y;
844     ADBMouseMoved(delta_x, delta_y);
845    
846     // Only warp the pointer when it has reached the edge
847     const int MOUSE_FUDGE_FACTOR = 8;
848     if (x < MOUSE_FUDGE_FACTOR || x > (width - MOUSE_FUDGE_FACTOR)
849     || y < MOUSE_FUDGE_FACTOR || y > (height - MOUSE_FUDGE_FACTOR)) {
850     XEvent event;
851     while (XCheckTypedEvent(x_display, MotionNotify, &event)) {
852     delta_x = x - mouse_last_x; delta_y = y - mouse_last_y;
853     mouse_last_x = x; mouse_last_y = y;
854     ADBMouseMoved(delta_x, delta_y);
855     }
856     mouse_last_x = width/2;
857     mouse_last_y = height/2;
858     XWarpPointer(x_display, None, w, 0, 0, 0, 0, mouse_last_x, mouse_last_y);
859     for (int i=0; i<10; i++) {
860     XMaskEvent(x_display, PointerMotionMask, &event);
861     if (event.xmotion.x > (mouse_last_x - MOUSE_FUDGE_FACTOR)
862     && event.xmotion.x < (mouse_last_x + MOUSE_FUDGE_FACTOR)
863     && event.xmotion.y > (mouse_last_y - MOUSE_FUDGE_FACTOR)
864     && event.xmotion.y < (mouse_last_y + MOUSE_FUDGE_FACTOR))
865     break;
866     }
867     }
868     }
869    
870 cebix 1.44
871     #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
872     /*
873     * DGA display driver base class
874     */
875    
876     class driver_dga : public driver_base {
877     public:
878 cebix 1.66 driver_dga(X11_monitor_desc &monitor);
879 cebix 1.44 ~driver_dga();
880    
881     void suspend(void);
882     void resume(void);
883    
884     private:
885     Window suspend_win; // "Suspend" information window
886     void *fb_save; // Saved frame buffer for suspend/resume
887     };
888    
889 cebix 1.66 driver_dga::driver_dga(X11_monitor_desc &m)
890     : driver_base(m), suspend_win(0), fb_save(NULL)
891 cebix 1.44 {
892     }
893    
894     driver_dga::~driver_dga()
895     {
896     XUngrabPointer(x_display, CurrentTime);
897     XUngrabKeyboard(x_display, CurrentTime);
898     }
899    
900     // Suspend emulation
901     void driver_dga::suspend(void)
902     {
903     // Release ctrl key
904     ADBKeyUp(0x36);
905     ctrl_down = false;
906    
907     // Lock frame buffer (this will stop the MacOS thread)
908     LOCK_FRAME_BUFFER;
909    
910     // Save frame buffer
911 cebix 1.66 fb_save = malloc(mode.y * mode.bytes_per_row);
912 cebix 1.44 if (fb_save)
913 cebix 1.66 memcpy(fb_save, the_buffer, mode.y * mode.bytes_per_row);
914 cebix 1.44
915     // Close full screen display
916     #ifdef ENABLE_XF86_DGA
917     XF86DGADirectVideo(x_display, screen, 0);
918     #endif
919     XUngrabPointer(x_display, CurrentTime);
920     XUngrabKeyboard(x_display, CurrentTime);
921 cebix 1.57 restore_mouse_accel();
922 cebix 1.44 XUnmapWindow(x_display, w);
923     wait_unmapped(w);
924    
925     // Open "suspend" window
926     XSetWindowAttributes wattr;
927     wattr.event_mask = KeyPressMask;
928     wattr.background_pixel = black_pixel;
929    
930     suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
931     InputOutput, vis, CWEventMask | CWBackPixel, &wattr);
932     set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
933     set_window_focus(suspend_win);
934     XMapWindow(x_display, suspend_win);
935     emul_suspended = true;
936 cebix 1.7 }
937    
938 cebix 1.44 // Resume emulation
939     void driver_dga::resume(void)
940 cebix 1.7 {
941 cebix 1.44 // Close "suspend" window
942     XDestroyWindow(x_display, suspend_win);
943     XSync(x_display, false);
944    
945     // Reopen full screen display
946     XMapRaised(x_display, w);
947     wait_mapped(w);
948     XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
949 cebix 1.51 XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
950     XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
951 cebix 1.57 disable_mouse_accel();
952 cebix 1.44 #ifdef ENABLE_XF86_DGA
953     XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
954     XF86DGASetViewPort(x_display, screen, 0, 0);
955     #endif
956     XSync(x_display, false);
957    
958     // the_buffer already contains the data to restore. i.e. since a temporary
959     // frame buffer is used when VOSF is actually used, fb_save is therefore
960     // not necessary.
961     #ifdef ENABLE_VOSF
962     if (use_vosf) {
963     LOCK_VOSF;
964     PFLAG_SET_ALL;
965     UNLOCK_VOSF;
966 cebix 1.66 memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
967 cebix 1.44 }
968     #endif
969    
970     // Restore frame buffer
971     if (fb_save) {
972     #ifdef ENABLE_VOSF
973     // Don't copy fb_save to the temporary frame buffer in VOSF mode
974     if (!use_vosf)
975     #endif
976 cebix 1.66 memcpy(the_buffer, fb_save, mode.y * mode.bytes_per_row);
977 cebix 1.44 free(fb_save);
978     fb_save = NULL;
979     }
980    
981     // Unlock frame buffer (and continue MacOS thread)
982     UNLOCK_FRAME_BUFFER;
983     emul_suspended = false;
984     }
985     #endif
986    
987    
988 cebix 1.15 #ifdef ENABLE_FBDEV_DGA
989 cebix 1.44 /*
990     * fbdev DGA display driver
991     */
992    
993 cebix 1.45 const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices";
994     const char FBDEVICE_FILE_NAME[] = "/dev/fb";
995    
996 cebix 1.44 class driver_fbdev : public driver_dga {
997     public:
998 cebix 1.66 driver_fbdev(X11_monitor_desc &monitor);
999 cebix 1.44 ~driver_fbdev();
1000     };
1001    
1002     // Open display
1003 cebix 1.66 driver_fbdev::driver_fbdev(X11_monitor_desc &m) : driver_dga(m)
1004 cebix 1.44 {
1005 cebix 1.42 int width = mode.x, height = mode.y;
1006    
1007 cebix 1.44 // Set absolute mouse mode
1008     ADBSetRelMouseMode(false);
1009    
1010 cebix 1.7 // Find the maximum depth available
1011     int ndepths, max_depth(0);
1012     int *depths = XListDepths(x_display, screen, &ndepths);
1013     if (depths == NULL) {
1014 cebix 1.9 printf("FATAL: Could not determine the maximal depth available\n");
1015 cebix 1.44 return;
1016 cebix 1.7 } else {
1017     while (ndepths-- > 0) {
1018     if (depths[ndepths] > max_depth)
1019     max_depth = depths[ndepths];
1020     }
1021     }
1022    
1023     // Get fbdevices file path from preferences
1024 cebix 1.8 const char *fbd_path = PrefsFindString("fbdevicefile");
1025 cebix 1.7
1026     // Open fbdevices file
1027     FILE *fp = fopen(fbd_path ? fbd_path : FBDEVICES_FILE_NAME, "r");
1028     if (fp == NULL) {
1029     char str[256];
1030     sprintf(str, GetString(STR_NO_FBDEVICE_FILE_ERR), fbd_path ? fbd_path : FBDEVICES_FILE_NAME, strerror(errno));
1031     ErrorAlert(str);
1032 cebix 1.44 return;
1033 cebix 1.7 }
1034    
1035     int fb_depth; // supported depth
1036     uint32 fb_offset; // offset used for mmap(2)
1037     char fb_name[20];
1038     char line[256];
1039     bool device_found = false;
1040     while (fgets(line, 255, fp)) {
1041     // Read line
1042     int len = strlen(line);
1043     if (len == 0)
1044     continue;
1045     line[len - 1] = '\0';
1046    
1047     // Comments begin with "#" or ";"
1048     if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
1049     continue;
1050    
1051 cebix 1.65 if ((sscanf(line, "%19s %d %x", fb_name, &fb_depth, &fb_offset) == 3)
1052 cebix 1.44 && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) {
1053 cebix 1.7 device_found = true;
1054     break;
1055     }
1056     }
1057    
1058     // fbdevices file completely read
1059     fclose(fp);
1060    
1061     // Frame buffer name not found ? Then, display warning
1062     if (!device_found) {
1063     char str[256];
1064 cebix 1.42 sprintf(str, GetString(STR_FBDEV_NAME_ERR), fb_name, max_depth);
1065 cebix 1.7 ErrorAlert(str);
1066 cebix 1.44 return;
1067 cebix 1.7 }
1068    
1069     // Create window
1070     XSetWindowAttributes wattr;
1071 cebix 1.29 wattr.event_mask = eventmask = dga_eventmask;
1072     wattr.background_pixel = white_pixel;
1073     wattr.override_redirect = True;
1074     wattr.colormap = cmap[0];
1075 cebix 1.7
1076 cebix 1.44 w = XCreateWindow(x_display, rootwin,
1077 cebix 1.7 0, 0, width, height,
1078     0, xdepth, InputOutput, vis,
1079 cebix 1.44 CWEventMask | CWBackPixel | CWOverrideRedirect | (fb_depth <= 8 ? CWColormap : 0),
1080 cebix 1.7 &wattr);
1081 cebix 1.29
1082     // Set window name/class
1083 cebix 1.44 set_window_name(w, STR_WINDOW_TITLE);
1084 cebix 1.29
1085     // Indicate that we want keyboard input
1086 cebix 1.44 set_window_focus(w);
1087 cebix 1.29
1088     // Show window
1089 cebix 1.44 XMapRaised(x_display, w);
1090     wait_mapped(w);
1091 cebix 1.7
1092     // Grab mouse and keyboard
1093 cebix 1.44 XGrabKeyboard(x_display, w, True,
1094 cebix 1.7 GrabModeAsync, GrabModeAsync, CurrentTime);
1095 cebix 1.44 XGrabPointer(x_display, w, True,
1096 cebix 1.7 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1097 cebix 1.44 GrabModeAsync, GrabModeAsync, w, None, CurrentTime);
1098 cebix 1.57 disable_mouse_accel();
1099 cebix 1.7
1100 cebix 1.42 // Calculate bytes per row
1101     int bytes_per_row = TrivialBytesPerRow(mode.x, mode.depth);
1102 cebix 1.7
1103 cebix 1.42 // Map frame buffer
1104 gbeauche 1.56 the_buffer_size = height * bytes_per_row;
1105     if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
1106     if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
1107 cebix 1.7 char str[256];
1108     sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
1109     ErrorAlert(str);
1110 cebix 1.44 return;
1111 cebix 1.7 }
1112     }
1113    
1114 cebix 1.23 #if ENABLE_VOSF
1115 gbeauche 1.19 #if REAL_ADDRESSING || DIRECT_ADDRESSING
1116 gbeauche 1.36 // Screen_blitter_init() returns TRUE if VOSF is mandatory
1117     // i.e. the framebuffer update function is not Blit_Copy_Raw
1118 cebix 1.48 use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1119 gbeauche 1.19
1120     if (use_vosf) {
1121 gbeauche 1.39 // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1122     the_host_buffer = the_buffer;
1123     the_buffer_size = page_extend((height + 2) * bytes_per_row);
1124 gbeauche 1.62 the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1125 gbeauche 1.39 the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1126 gbeauche 1.19 }
1127 cebix 1.23 #else
1128 gbeauche 1.19 use_vosf = false;
1129     #endif
1130 cebix 1.23 #endif
1131 gbeauche 1.19
1132 cebix 1.66 // Set frame buffer base
1133     const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1134     const_cast<video_mode *>(&mode)->depth = DepthModeForPixelDepth(fb_depth);
1135 cebix 1.67 set_mac_frame_buffer(monitor, mode.depth, true);
1136 cebix 1.44
1137     // Everything went well
1138     init_ok = true;
1139     }
1140    
1141     // Close display
1142     driver_fbdev::~driver_fbdev()
1143     {
1144 gbeauche 1.56 if (!use_vosf) {
1145     if (the_buffer != MAP_FAILED) {
1146     // don't free() the screen buffer in driver_base dtor
1147     munmap(the_buffer, the_buffer_size);
1148     the_buffer = NULL;
1149     }
1150     }
1151     #ifdef ENABLE_VOSF
1152     else {
1153     if (the_host_buffer != MAP_FAILED) {
1154     // don't free() the screen buffer in driver_base dtor
1155     munmap(the_host_buffer, the_buffer_size);
1156     the_host_buffer = NULL;
1157     }
1158     }
1159     #endif
1160 cebix 1.44 }
1161 cebix 1.7 #endif
1162 cebix 1.1
1163 cebix 1.44
1164     #ifdef ENABLE_XF86_DGA
1165     /*
1166     * XFree86 DGA display driver
1167     */
1168    
1169     class driver_xf86dga : public driver_dga {
1170     public:
1171 cebix 1.66 driver_xf86dga(X11_monitor_desc &monitor);
1172 cebix 1.44 ~driver_xf86dga();
1173    
1174     void update_palette(void);
1175     void resume(void);
1176    
1177     private:
1178     int current_dga_cmap; // Number (0 or 1) of currently installed DGA colormap
1179     };
1180    
1181     // Open display
1182 cebix 1.66 driver_xf86dga::driver_xf86dga(X11_monitor_desc &m)
1183     : driver_dga(m), current_dga_cmap(0)
1184 cebix 1.1 {
1185 cebix 1.42 int width = mode.x, height = mode.y;
1186    
1187 cebix 1.1 // Set relative mouse mode
1188     ADBSetRelMouseMode(true);
1189    
1190 cebix 1.15 #ifdef ENABLE_XF86_VIDMODE
1191 cebix 1.12 // Switch to best mode
1192     if (has_vidmode) {
1193     int best = 0;
1194     for (int i=1; i<num_x_video_modes; i++) {
1195     if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
1196     x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
1197     best = i;
1198     }
1199     }
1200     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
1201     XF86VidModeSetViewPort(x_display, screen, 0, 0);
1202 cebix 1.29 XSync(x_display, false);
1203 cebix 1.12 }
1204     #endif
1205    
1206 cebix 1.1 // Create window
1207     XSetWindowAttributes wattr;
1208     wattr.event_mask = eventmask = dga_eventmask;
1209     wattr.override_redirect = True;
1210 gbeauche 1.68 wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
1211 cebix 1.1
1212 cebix 1.44 w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
1213 gbeauche 1.68 InputOutput, vis, CWEventMask | CWOverrideRedirect |
1214     (color_class == DirectColor ? CWColormap : 0), &wattr);
1215 cebix 1.29
1216     // Set window name/class
1217 cebix 1.44 set_window_name(w, STR_WINDOW_TITLE);
1218 cebix 1.29
1219     // Indicate that we want keyboard input
1220 cebix 1.44 set_window_focus(w);
1221 cebix 1.29
1222     // Show window
1223 cebix 1.44 XMapRaised(x_display, w);
1224     wait_mapped(w);
1225 cebix 1.1
1226     // Establish direct screen connection
1227 cebix 1.44 XMoveResizeWindow(x_display, w, 0, 0, width, height);
1228 cebix 1.1 XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1229     XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
1230     XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1231 cebix 1.57 disable_mouse_accel();
1232 cebix 1.1
1233     int v_width, v_bank, v_size;
1234     XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
1235     XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1236     XF86DGASetViewPort(x_display, screen, 0, 0);
1237     XF86DGASetVidPage(x_display, screen, 0);
1238    
1239     // Set colormap
1240 cebix 1.42 if (!IsDirectMode(mode)) {
1241 cebix 1.44 XSetWindowColormap(x_display, w, cmap[current_dga_cmap = 0]);
1242 cebix 1.1 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1243     }
1244 cebix 1.29 XSync(x_display, false);
1245 cebix 1.1
1246 cebix 1.44 // Init blitting routines
1247 cebix 1.42 int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
1248 gbeauche 1.68 #if ENABLE_VOSF
1249     bool native_byte_order;
1250     #ifdef WORDS_BIGENDIAN
1251     native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
1252     #else
1253     native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
1254     #endif
1255 gbeauche 1.19 #if REAL_ADDRESSING || DIRECT_ADDRESSING
1256 gbeauche 1.36 // Screen_blitter_init() returns TRUE if VOSF is mandatory
1257     // i.e. the framebuffer update function is not Blit_Copy_Raw
1258 gbeauche 1.69 use_vosf = Screen_blitter_init(&visualInfo, native_byte_order, depth_of_video_mode(mode));
1259 gbeauche 1.19
1260     if (use_vosf) {
1261 gbeauche 1.39 // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1262     the_host_buffer = the_buffer;
1263     the_buffer_size = page_extend((height + 2) * bytes_per_row);
1264 gbeauche 1.62 the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1265 gbeauche 1.39 the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1266 gbeauche 1.19 }
1267 gbeauche 1.39 #else
1268 gbeauche 1.19 use_vosf = false;
1269     #endif
1270 gbeauche 1.39 #endif
1271 gbeauche 1.19
1272 cebix 1.66 // Set frame buffer base
1273 cebix 1.42 const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1274 cebix 1.66 set_mac_frame_buffer(monitor, mode.depth, true);
1275 cebix 1.44
1276     // Everything went well
1277     init_ok = true;
1278     }
1279    
1280     // Close display
1281     driver_xf86dga::~driver_xf86dga()
1282     {
1283     XF86DGADirectVideo(x_display, screen, 0);
1284 gbeauche 1.56 if (!use_vosf) {
1285     // don't free() the screen buffer in driver_base dtor
1286     the_buffer = NULL;
1287     }
1288     #ifdef ENABLE_VOSF
1289     else {
1290     // don't free() the screen buffer in driver_base dtor
1291     the_host_buffer = NULL;
1292     }
1293     #endif
1294 cebix 1.44 #ifdef ENABLE_XF86_VIDMODE
1295     if (has_vidmode)
1296     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
1297 cebix 1.1 #endif
1298     }
1299    
1300 cebix 1.44 // Palette has changed
1301     void driver_xf86dga::update_palette(void)
1302     {
1303     driver_dga::update_palette();
1304     current_dga_cmap ^= 1;
1305 cebix 1.66 if (!IsDirectMode(monitor.get_current_mode()) && cmap[current_dga_cmap])
1306 cebix 1.44 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1307     }
1308    
1309     // Resume emulation
1310     void driver_xf86dga::resume(void)
1311     {
1312     driver_dga::resume();
1313 cebix 1.66 if (!IsDirectMode(monitor.get_current_mode()))
1314 cebix 1.44 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1315     }
1316     #endif
1317    
1318    
1319     /*
1320     * Initialization
1321     */
1322    
1323 cebix 1.1 // Init keycode translation table
1324     static void keycode_init(void)
1325     {
1326     bool use_kc = PrefsFindBool("keycodes");
1327     if (use_kc) {
1328    
1329     // Get keycode file path from preferences
1330     const char *kc_path = PrefsFindString("keycodefile");
1331    
1332     // Open keycode table
1333     FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
1334     if (f == NULL) {
1335     char str[256];
1336     sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
1337     WarningAlert(str);
1338     return;
1339     }
1340    
1341     // Default translation table
1342     for (int i=0; i<256; i++)
1343     keycode_table[i] = -1;
1344    
1345     // Search for server vendor string, then read keycodes
1346     const char *vendor = ServerVendor(x_display);
1347     bool vendor_found = false;
1348     char line[256];
1349     while (fgets(line, 255, f)) {
1350     // Read line
1351     int len = strlen(line);
1352     if (len == 0)
1353     continue;
1354     line[len-1] = 0;
1355    
1356     // Comments begin with "#" or ";"
1357     if (line[0] == '#' || line[0] == ';' || line[0] == 0)
1358     continue;
1359    
1360     if (vendor_found) {
1361     // Read keycode
1362     int x_code, mac_code;
1363     if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
1364     keycode_table[x_code & 0xff] = mac_code;
1365     else
1366     break;
1367     } else {
1368     // Search for vendor string
1369     if (strstr(vendor, line) == vendor)
1370     vendor_found = true;
1371     }
1372     }
1373    
1374     // Keycode file completely read
1375     fclose(f);
1376     use_keycodes = vendor_found;
1377    
1378     // Vendor not found? Then display warning
1379     if (!vendor_found) {
1380     char str[256];
1381     sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME);
1382     WarningAlert(str);
1383     return;
1384     }
1385     }
1386     }
1387    
1388 cebix 1.66 // Open display for current mode
1389     bool X11_monitor_desc::video_open(void)
1390 gbeauche 1.19 {
1391 cebix 1.61 D(bug("video_open()\n"));
1392 cebix 1.66 const video_mode &mode = get_current_mode();
1393 cebix 1.61
1394 cebix 1.53 // Find best available X visual
1395     if (!find_visual_for_depth(mode.depth)) {
1396     ErrorAlert(STR_NO_XVISUAL_ERR);
1397     return false;
1398     }
1399    
1400     // Create color maps
1401     if (color_class == PseudoColor || color_class == DirectColor) {
1402     cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1403     cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1404 cebix 1.58 } else {
1405     cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
1406     cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
1407 cebix 1.53 }
1408    
1409     // Find pixel format of direct modes
1410     if (color_class == DirectColor || color_class == TrueColor) {
1411     rshift = gshift = bshift = 0;
1412     rloss = gloss = bloss = 8;
1413     uint32 mask;
1414     for (mask=vis->red_mask; !(mask&1); mask>>=1)
1415     ++rshift;
1416     for (; mask&1; mask>>=1)
1417     --rloss;
1418     for (mask=vis->green_mask; !(mask&1); mask>>=1)
1419     ++gshift;
1420     for (; mask&1; mask>>=1)
1421     --gloss;
1422     for (mask=vis->blue_mask; !(mask&1); mask>>=1)
1423     ++bshift;
1424     for (; mask&1; mask>>=1)
1425     --bloss;
1426     }
1427    
1428 cebix 1.57 // Preset palette pixel values for CLUT or gamma table
1429 cebix 1.53 if (color_class == DirectColor) {
1430     int num = vis->map_entries;
1431     for (int i=0; i<num; i++) {
1432     int c = (i * 256) / num;
1433 cebix 1.66 x_palette[i].pixel = map_rgb(c, c, c);
1434     x_palette[i].flags = DoRed | DoGreen | DoBlue;
1435 cebix 1.57 }
1436     } else if (color_class == PseudoColor) {
1437     for (int i=0; i<256; i++) {
1438 cebix 1.66 x_palette[i].pixel = i;
1439     x_palette[i].flags = DoRed | DoGreen | DoBlue;
1440 cebix 1.53 }
1441     }
1442    
1443 cebix 1.48 // Load gray ramp to color map
1444 cebix 1.53 int num = (color_class == DirectColor ? vis->map_entries : 256);
1445 cebix 1.48 for (int i=0; i<num; i++) {
1446     int c = (i * 256) / num;
1447 cebix 1.66 x_palette[i].red = c * 0x0101;
1448     x_palette[i].green = c * 0x0101;
1449     x_palette[i].blue = c * 0x0101;
1450 cebix 1.48 }
1451 cebix 1.58 if (color_class == PseudoColor || color_class == DirectColor) {
1452 cebix 1.66 XStoreColors(x_display, cmap[0], x_palette, num);
1453     XStoreColors(x_display, cmap[1], x_palette, num);
1454 cebix 1.48 }
1455    
1456 cebix 1.49 #ifdef ENABLE_VOSF
1457     // Load gray ramp to 8->16/32 expand map
1458 cebix 1.57 if (!IsDirectMode(mode) && xdepth > 8)
1459 cebix 1.49 for (int i=0; i<256; i++)
1460     ExpandMap[i] = map_rgb(i, i, i);
1461     #endif
1462    
1463 cebix 1.44 // Create display driver object of requested type
1464 cebix 1.42 switch (display_type) {
1465     case DISPLAY_WINDOW:
1466 cebix 1.66 drv = new driver_window(*this);
1467 cebix 1.42 break;
1468 cebix 1.44 #ifdef ENABLE_FBDEV_DGA
1469 cebix 1.42 case DISPLAY_DGA:
1470 cebix 1.66 drv = new driver_fbdev(*this);
1471 cebix 1.44 break;
1472 cebix 1.42 #endif
1473 cebix 1.44 #ifdef ENABLE_XF86_DGA
1474     case DISPLAY_DGA:
1475 cebix 1.66 drv = new driver_xf86dga(*this);
1476 cebix 1.42 break;
1477 cebix 1.44 #endif
1478     }
1479     if (drv == NULL)
1480     return false;
1481     if (!drv->init_ok) {
1482     delete drv;
1483     drv = NULL;
1484     return false;
1485 cebix 1.42 }
1486 gbeauche 1.19
1487 cebix 1.42 #ifdef ENABLE_VOSF
1488     if (use_vosf) {
1489 gbeauche 1.56 // Initialize the VOSF system
1490 cebix 1.66 if (!video_vosf_init(*this)) {
1491 cebix 1.44 ErrorAlert(STR_VOSF_INIT_ERR);
1492 cebix 1.42 return false;
1493 gbeauche 1.19 }
1494     }
1495     #endif
1496 cebix 1.42
1497     // Initialize VideoRefresh function
1498     VideoRefreshInit();
1499 cebix 1.44
1500     // Lock down frame buffer
1501 cebix 1.42 XSync(x_display, false);
1502 cebix 1.44 LOCK_FRAME_BUFFER;
1503 cebix 1.42
1504 cebix 1.45 // Start redraw/input thread
1505 cebix 1.42 #ifdef HAVE_PTHREADS
1506     redraw_thread_cancel = false;
1507 cebix 1.64 Set_pthread_attr(&redraw_thread_attr, 0);
1508     redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1509 cebix 1.42 if (!redraw_thread_active) {
1510     printf("FATAL: cannot create redraw thread\n");
1511     return false;
1512     }
1513 cebix 1.45 #else
1514     redraw_thread_active = true;
1515 cebix 1.42 #endif
1516    
1517 gbeauche 1.19 return true;
1518     }
1519    
1520 cebix 1.1 bool VideoInit(bool classic)
1521     {
1522 cebix 1.42 classic_mode = classic;
1523    
1524 gbeauche 1.19 #ifdef ENABLE_VOSF
1525     // Zero the mainBuffer structure
1526 cebix 1.44 mainBuffer.dirtyPages = NULL;
1527     mainBuffer.pageInfo = NULL;
1528 gbeauche 1.19 #endif
1529    
1530 cebix 1.16 // Check if X server runs on local machine
1531     local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
1532     || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
1533    
1534 cebix 1.1 // Init keycode translation
1535     keycode_init();
1536    
1537 cebix 1.10 // Read prefs
1538 cebix 1.44 frame_skip = PrefsFindInt32("frameskip");
1539 cebix 1.32 mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
1540     mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1541 cebix 1.10
1542 cebix 1.1 // Find screen and root window
1543     screen = XDefaultScreen(x_display);
1544     rootwin = XRootWindow(x_display, screen);
1545 cebix 1.53
1546     // Get sorted list of available depths
1547     avail_depths = XListDepths(x_display, screen, &num_depths);
1548     if (avail_depths == NULL) {
1549     ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1550     return false;
1551     }
1552 cebix 1.66 std::sort(avail_depths, avail_depths + num_depths);
1553 cebix 1.7
1554 cebix 1.15 #ifdef ENABLE_FBDEV_DGA
1555 cebix 1.7 // Frame buffer name
1556     char fb_name[20];
1557    
1558 cebix 1.44 // Could do fbdev DGA?
1559 cebix 1.7 if ((fbdev_fd = open(FBDEVICE_FILE_NAME, O_RDWR)) != -1)
1560     has_dga = true;
1561     else
1562     has_dga = false;
1563     #endif
1564 cebix 1.12
1565 cebix 1.15 #ifdef ENABLE_XF86_DGA
1566 cebix 1.1 // DGA available?
1567 cebix 1.12 int dga_event_base, dga_error_base;
1568 cebix 1.16 if (local_X11 && XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
1569 cebix 1.5 int dga_flags = 0;
1570     XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1571     has_dga = dga_flags & XF86DGADirectPresent;
1572     } else
1573     has_dga = false;
1574 cebix 1.1 #endif
1575    
1576 cebix 1.15 #ifdef ENABLE_XF86_VIDMODE
1577 cebix 1.12 // VidMode available?
1578     int vm_event_base, vm_error_base;
1579     has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
1580     if (has_vidmode)
1581     XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1582     #endif
1583    
1584 cebix 1.1 // Find black and white colors
1585     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
1586     XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
1587     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white);
1588     XAllocColor(x_display, DefaultColormap(x_display, screen), &white);
1589     black_pixel = BlackPixel(x_display, screen);
1590     white_pixel = WhitePixel(x_display, screen);
1591    
1592     // Get screen mode from preferences
1593     const char *mode_str;
1594 cebix 1.42 if (classic_mode)
1595 cebix 1.1 mode_str = "win/512/342";
1596     else
1597     mode_str = PrefsFindString("screen");
1598    
1599 cebix 1.42 // Determine display type and default dimensions
1600     int default_width = 512, default_height = 384;
1601 cebix 1.1 display_type = DISPLAY_WINDOW;
1602     if (mode_str) {
1603 cebix 1.42 if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2) {
1604 cebix 1.1 display_type = DISPLAY_WINDOW;
1605 cebix 1.15 #ifdef ENABLE_FBDEV_DGA
1606 cebix 1.42 } else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
1607     display_type = DISPLAY_DGA;
1608 cebix 1.44 default_width = -1; default_height = -1; // use entire screen
1609     #endif
1610     #ifdef ENABLE_XF86_DGA
1611 cebix 1.42 } else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2) {
1612 cebix 1.1 display_type = DISPLAY_DGA;
1613 cebix 1.7 #endif
1614 cebix 1.42 }
1615 cebix 1.1 }
1616 cebix 1.42 if (default_width <= 0)
1617     default_width = DisplayWidth(x_display, screen);
1618     else if (default_width > DisplayWidth(x_display, screen))
1619     default_width = DisplayWidth(x_display, screen);
1620     if (default_height <= 0)
1621     default_height = DisplayHeight(x_display, screen);
1622     else if (default_height > DisplayHeight(x_display, screen))
1623     default_height = DisplayHeight(x_display, screen);
1624 cebix 1.1
1625 cebix 1.48 // Mac screen depth follows X depth
1626 cebix 1.53 video_depth default_depth = VDEPTH_1BIT;
1627     switch (DefaultDepth(x_display, screen)) {
1628     case 8:
1629     default_depth = VDEPTH_8BIT;
1630     break;
1631     case 15: case 16:
1632     default_depth = VDEPTH_16BIT;
1633     break;
1634     case 24: case 32:
1635     default_depth = VDEPTH_32BIT;
1636     break;
1637     }
1638 cebix 1.1
1639 cebix 1.42 // Construct list of supported modes
1640     if (display_type == DISPLAY_WINDOW) {
1641     if (classic)
1642 cebix 1.48 add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
1643 cebix 1.42 else {
1644 cebix 1.53 for (unsigned d=VDEPTH_1BIT; d<=VDEPTH_32BIT; d++) {
1645     if (find_visual_for_depth(video_depth(d)))
1646     add_window_modes(video_depth(d));
1647 cebix 1.49 }
1648 gbeauche 1.19 }
1649 cebix 1.42 } else
1650 cebix 1.48 add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
1651 cebix 1.53 if (VideoModes.empty()) {
1652     ErrorAlert(STR_NO_XVISUAL_ERR);
1653     return false;
1654     }
1655 cebix 1.66
1656     // Find requested default mode with specified dimensions
1657     uint32 default_id;
1658     std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1659     for (i = VideoModes.begin(); i != end; ++i) {
1660     if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1661     default_id = i->resolution_id;
1662     break;
1663     }
1664     }
1665     if (i == end) { // not found, use first available mode
1666     default_depth = VideoModes[0].depth;
1667     default_id = VideoModes[0].resolution_id;
1668     }
1669 gbeauche 1.19
1670 cebix 1.53 #if DEBUG
1671     D(bug("Available video modes:\n"));
1672 cebix 1.66 for (i = VideoModes.begin(); i != end; ++i) {
1673 cebix 1.53 int bits = 1 << i->depth;
1674     if (bits == 16)
1675     bits = 15;
1676     else if (bits == 32)
1677     bits = 24;
1678     D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1679     }
1680     #endif
1681    
1682 cebix 1.66 // Create X11_monitor_desc for this (the only) display
1683     X11_monitor_desc *monitor = new X11_monitor_desc(VideoModes, default_depth, default_id);
1684     VideoMonitors.push_back(monitor);
1685    
1686     // Open display
1687     return monitor->video_open();
1688 cebix 1.1 }
1689    
1690    
1691     /*
1692     * Deinitialization
1693     */
1694    
1695 cebix 1.42 // Close display
1696 cebix 1.66 void X11_monitor_desc::video_close(void)
1697 cebix 1.1 {
1698 cebix 1.61 D(bug("video_close()\n"));
1699    
1700 cebix 1.45 // Stop redraw thread
1701 cebix 1.15 #ifdef HAVE_PTHREADS
1702 cebix 1.1 if (redraw_thread_active) {
1703     redraw_thread_cancel = true;
1704     #ifdef HAVE_PTHREAD_CANCEL
1705     pthread_cancel(redraw_thread);
1706     #endif
1707     pthread_join(redraw_thread, NULL);
1708     }
1709 cebix 1.15 #endif
1710 cebix 1.45 redraw_thread_active = false;
1711 cebix 1.1
1712     // Unlock frame buffer
1713 cebix 1.29 UNLOCK_FRAME_BUFFER;
1714 cebix 1.44 XSync(x_display, false);
1715 cebix 1.61 D(bug(" frame buffer unlocked\n"));
1716 cebix 1.1
1717 gbeauche 1.19 #ifdef ENABLE_VOSF
1718     if (use_vosf) {
1719 gbeauche 1.56 // Deinitialize VOSF
1720     video_vosf_exit();
1721 cebix 1.1 }
1722 cebix 1.42 #endif
1723 cebix 1.44
1724     // Close display
1725     delete drv;
1726     drv = NULL;
1727    
1728     // Free colormaps
1729     if (cmap[0]) {
1730     XFreeColormap(x_display, cmap[0]);
1731     cmap[0] = 0;
1732     }
1733     if (cmap[1]) {
1734     XFreeColormap(x_display, cmap[1]);
1735     cmap[1] = 0;
1736     }
1737 cebix 1.53 }
1738    
1739     void VideoExit(void)
1740     {
1741 cebix 1.66 // Close displays
1742     vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
1743     for (i = VideoMonitors.begin(); i != end; ++i)
1744     dynamic_cast<X11_monitor_desc *>(*i)->video_close();
1745 cebix 1.44
1746     #ifdef ENABLE_XF86_VIDMODE
1747     // Free video mode list
1748     if (x_video_modes) {
1749     XFree(x_video_modes);
1750     x_video_modes = NULL;
1751     }
1752     #endif
1753    
1754     #ifdef ENABLE_FBDEV_DGA
1755     // Close framebuffer device
1756     if (fbdev_fd >= 0) {
1757     close(fbdev_fd);
1758     fbdev_fd = -1;
1759     }
1760     #endif
1761 cebix 1.53
1762     // Free depth list
1763     if (avail_depths) {
1764     XFree(avail_depths);
1765     avail_depths = NULL;
1766     }
1767 cebix 1.1 }
1768    
1769    
1770     /*
1771     * Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode)
1772     */
1773    
1774     void VideoQuitFullScreen(void)
1775     {
1776     D(bug("VideoQuitFullScreen()\n"));
1777 cebix 1.44 quit_full_screen = true;
1778 cebix 1.1 }
1779    
1780    
1781     /*
1782     * Mac VBL interrupt
1783     */
1784    
1785     void VideoInterrupt(void)
1786     {
1787     // Emergency quit requested? Then quit
1788     if (emerg_quit)
1789     QuitEmulator();
1790    
1791     // Temporarily give up frame buffer lock (this is the point where
1792     // we are suspended when the user presses Ctrl-Tab)
1793 cebix 1.29 UNLOCK_FRAME_BUFFER;
1794     LOCK_FRAME_BUFFER;
1795 cebix 1.1 }
1796    
1797    
1798     /*
1799     * Set palette
1800     */
1801    
1802 cebix 1.66 void X11_monitor_desc::set_palette(uint8 *pal, int num_in)
1803 cebix 1.1 {
1804 cebix 1.66 const video_mode &mode = get_current_mode();
1805    
1806 cebix 1.29 LOCK_PALETTE;
1807 cebix 1.1
1808     // Convert colors to XColor array
1809 cebix 1.50 int num_out = 256;
1810 cebix 1.54 bool stretch = false;
1811 cebix 1.66 if (IsDirectMode(mode)) {
1812 cebix 1.50 // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1813 cebix 1.47 num_out = vis->map_entries;
1814 cebix 1.54 stretch = true;
1815 cebix 1.46 }
1816 cebix 1.66 XColor *p = x_palette;
1817 cebix 1.46 for (int i=0; i<num_out; i++) {
1818 cebix 1.54 int c = (stretch ? (i * num_in) / num_out : i);
1819 cebix 1.46 p->red = pal[c*3 + 0] * 0x0101;
1820     p->green = pal[c*3 + 1] * 0x0101;
1821     p->blue = pal[c*3 + 2] * 0x0101;
1822     p++;
1823 cebix 1.1 }
1824 cebix 1.49
1825     #ifdef ENABLE_VOSF
1826     // Recalculate pixel color expansion map
1827 cebix 1.66 if (!IsDirectMode(mode) && xdepth > 8) {
1828 cebix 1.50 for (int i=0; i<256; i++) {
1829 cebix 1.51 int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1830 cebix 1.50 ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
1831     }
1832 cebix 1.49
1833     // We have to redraw everything because the interpretation of pixel values changed
1834     LOCK_VOSF;
1835     PFLAG_SET_ALL;
1836     UNLOCK_VOSF;
1837 cebix 1.66 memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1838 cebix 1.49 }
1839     #endif
1840 cebix 1.1
1841     // Tell redraw thread to change palette
1842 cebix 1.66 x_palette_changed = true;
1843 cebix 1.1
1844 cebix 1.29 UNLOCK_PALETTE;
1845 cebix 1.1 }
1846    
1847    
1848     /*
1849 cebix 1.41 * Switch video mode
1850     */
1851    
1852 cebix 1.66 void X11_monitor_desc::switch_to_current_mode(void)
1853 cebix 1.41 {
1854 cebix 1.44 // Close and reopen display
1855 cebix 1.42 video_close();
1856 cebix 1.66 video_open();
1857 cebix 1.41
1858 cebix 1.44 if (drv == NULL) {
1859     ErrorAlert(STR_OPEN_WINDOW_ERR);
1860     QuitEmulator();
1861 cebix 1.1 }
1862     }
1863    
1864    
1865     /*
1866 cebix 1.51 * Translate key event to Mac keycode, returns -1 if no keycode was found
1867     * and -2 if the key was recognized as a hotkey
1868 cebix 1.1 */
1869    
1870 cebix 1.51 static int kc_decode(KeySym ks, bool key_down)
1871 cebix 1.1 {
1872     switch (ks) {
1873     case XK_A: case XK_a: return 0x00;
1874     case XK_B: case XK_b: return 0x0b;
1875     case XK_C: case XK_c: return 0x08;
1876     case XK_D: case XK_d: return 0x02;
1877     case XK_E: case XK_e: return 0x0e;
1878     case XK_F: case XK_f: return 0x03;
1879     case XK_G: case XK_g: return 0x05;
1880     case XK_H: case XK_h: return 0x04;
1881     case XK_I: case XK_i: return 0x22;
1882     case XK_J: case XK_j: return 0x26;
1883     case XK_K: case XK_k: return 0x28;
1884     case XK_L: case XK_l: return 0x25;
1885     case XK_M: case XK_m: return 0x2e;
1886     case XK_N: case XK_n: return 0x2d;
1887     case XK_O: case XK_o: return 0x1f;
1888     case XK_P: case XK_p: return 0x23;
1889     case XK_Q: case XK_q: return 0x0c;
1890     case XK_R: case XK_r: return 0x0f;
1891     case XK_S: case XK_s: return 0x01;
1892     case XK_T: case XK_t: return 0x11;
1893     case XK_U: case XK_u: return 0x20;
1894     case XK_V: case XK_v: return 0x09;
1895     case XK_W: case XK_w: return 0x0d;
1896     case XK_X: case XK_x: return 0x07;
1897     case XK_Y: case XK_y: return 0x10;
1898     case XK_Z: case XK_z: return 0x06;
1899    
1900     case XK_1: case XK_exclam: return 0x12;
1901     case XK_2: case XK_at: return 0x13;
1902     case XK_3: case XK_numbersign: return 0x14;
1903     case XK_4: case XK_dollar: return 0x15;
1904     case XK_5: case XK_percent: return 0x17;
1905     case XK_6: return 0x16;
1906     case XK_7: return 0x1a;
1907     case XK_8: return 0x1c;
1908     case XK_9: return 0x19;
1909     case XK_0: return 0x1d;
1910    
1911     case XK_grave: case XK_asciitilde: return 0x0a;
1912     case XK_minus: case XK_underscore: return 0x1b;
1913     case XK_equal: case XK_plus: return 0x18;
1914     case XK_bracketleft: case XK_braceleft: return 0x21;
1915     case XK_bracketright: case XK_braceright: return 0x1e;
1916     case XK_backslash: case XK_bar: return 0x2a;
1917     case XK_semicolon: case XK_colon: return 0x29;
1918     case XK_apostrophe: case XK_quotedbl: return 0x27;
1919     case XK_comma: case XK_less: return 0x2b;
1920     case XK_period: case XK_greater: return 0x2f;
1921     case XK_slash: case XK_question: return 0x2c;
1922    
1923 cebix 1.51 case XK_Tab: if (ctrl_down) {if (key_down) drv->suspend(); return -2;} else return 0x30;
1924 cebix 1.1 case XK_Return: return 0x24;
1925     case XK_space: return 0x31;
1926     case XK_BackSpace: return 0x33;
1927    
1928     case XK_Delete: return 0x75;
1929     case XK_Insert: return 0x72;
1930     case XK_Home: case XK_Help: return 0x73;
1931     case XK_End: return 0x77;
1932     #ifdef __hpux
1933     case XK_Prior: return 0x74;
1934     case XK_Next: return 0x79;
1935     #else
1936     case XK_Page_Up: return 0x74;
1937     case XK_Page_Down: return 0x79;
1938     #endif
1939    
1940     case XK_Control_L: return 0x36;
1941     case XK_Control_R: return 0x36;
1942     case XK_Shift_L: return 0x38;
1943     case XK_Shift_R: return 0x38;
1944     case XK_Alt_L: return 0x37;
1945     case XK_Alt_R: return 0x37;
1946     case XK_Meta_L: return 0x3a;
1947     case XK_Meta_R: return 0x3a;
1948     case XK_Menu: return 0x32;
1949     case XK_Caps_Lock: return 0x39;
1950     case XK_Num_Lock: return 0x47;
1951    
1952     case XK_Up: return 0x3e;
1953     case XK_Down: return 0x3d;
1954     case XK_Left: return 0x3b;
1955     case XK_Right: return 0x3c;
1956    
1957 cebix 1.51 case XK_Escape: if (ctrl_down) {if (key_down) { quit_full_screen = true; emerg_quit = true; } return -2;} else return 0x35;
1958 cebix 1.1
1959 cebix 1.51 case XK_F1: if (ctrl_down) {if (key_down) SysMountFirstFloppy(); return -2;} else return 0x7a;
1960 cebix 1.1 case XK_F2: return 0x78;
1961     case XK_F3: return 0x63;
1962     case XK_F4: return 0x76;
1963 cebix 1.51 case XK_F5: if (ctrl_down) {if (key_down) drv->toggle_mouse_grab(); return -2;} else return 0x60;
1964 cebix 1.1 case XK_F6: return 0x61;
1965     case XK_F7: return 0x62;
1966     case XK_F8: return 0x64;
1967     case XK_F9: return 0x65;
1968     case XK_F10: return 0x6d;
1969     case XK_F11: return 0x67;
1970     case XK_F12: return 0x6f;
1971    
1972     case XK_Print: return 0x69;
1973     case XK_Scroll_Lock: return 0x6b;
1974     case XK_Pause: return 0x71;
1975    
1976     #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
1977     case XK_KP_0: case XK_KP_Insert: return 0x52;
1978     case XK_KP_1: case XK_KP_End: return 0x53;
1979     case XK_KP_2: case XK_KP_Down: return 0x54;
1980     case XK_KP_3: case XK_KP_Next: return 0x55;
1981     case XK_KP_4: case XK_KP_Left: return 0x56;
1982     case XK_KP_5: case XK_KP_Begin: return 0x57;
1983     case XK_KP_6: case XK_KP_Right: return 0x58;
1984     case XK_KP_7: case XK_KP_Home: return 0x59;
1985     case XK_KP_8: case XK_KP_Up: return 0x5b;
1986     case XK_KP_9: case XK_KP_Prior: return 0x5c;
1987     case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
1988     #else
1989     case XK_KP_0: return 0x52;
1990     case XK_KP_1: return 0x53;
1991     case XK_KP_2: return 0x54;
1992     case XK_KP_3: return 0x55;
1993     case XK_KP_4: return 0x56;
1994     case XK_KP_5: return 0x57;
1995     case XK_KP_6: return 0x58;
1996     case XK_KP_7: return 0x59;
1997     case XK_KP_8: return 0x5b;
1998     case XK_KP_9: return 0x5c;
1999     case XK_KP_Decimal: return 0x41;
2000     #endif
2001     case XK_KP_Add: return 0x45;
2002     case XK_KP_Subtract: return 0x4e;
2003     case XK_KP_Multiply: return 0x43;
2004     case XK_KP_Divide: return 0x4b;
2005     case XK_KP_Enter: return 0x4c;
2006     case XK_KP_Equal: return 0x51;
2007     }
2008     return -1;
2009     }
2010    
2011 cebix 1.51 static int event2keycode(XKeyEvent &ev, bool key_down)
2012 cebix 1.1 {
2013     KeySym ks;
2014     int i = 0;
2015    
2016     do {
2017 cebix 1.29 ks = XLookupKeysym(&ev, i++);
2018 cebix 1.51 int as = kc_decode(ks, key_down);
2019     if (as >= 0)
2020     return as;
2021     if (as == -2)
2022 cebix 1.1 return as;
2023     } while (ks != NoSymbol);
2024    
2025     return -1;
2026     }
2027    
2028    
2029     /*
2030     * X event handling
2031     */
2032    
2033     static void handle_events(void)
2034     {
2035 cebix 1.29 while (XPending(x_display)) {
2036     XEvent event;
2037     XNextEvent(x_display, &event);
2038 cebix 1.1
2039     switch (event.type) {
2040 cebix 1.57
2041 cebix 1.1 // Mouse button
2042     case ButtonPress: {
2043 cebix 1.29 unsigned int button = event.xbutton.button;
2044 cebix 1.1 if (button < 4)
2045     ADBMouseDown(button - 1);
2046 cebix 1.10 else if (button < 6) { // Wheel mouse
2047     if (mouse_wheel_mode == 0) {
2048     int key = (button == 5) ? 0x79 : 0x74; // Page up/down
2049     ADBKeyDown(key);
2050     ADBKeyUp(key);
2051     } else {
2052     int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down
2053     for(int i=0; i<mouse_wheel_lines; i++) {
2054     ADBKeyDown(key);
2055     ADBKeyUp(key);
2056     }
2057     }
2058     }
2059 cebix 1.1 break;
2060     }
2061     case ButtonRelease: {
2062 cebix 1.29 unsigned int button = event.xbutton.button;
2063 cebix 1.1 if (button < 4)
2064     ADBMouseUp(button - 1);
2065     break;
2066     }
2067    
2068     // Mouse moved
2069     case MotionNotify:
2070 cebix 1.51 drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2071 cebix 1.1 break;
2072    
2073 cebix 1.57 // Mouse entered window
2074     case EnterNotify:
2075     if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
2076     drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2077     break;
2078    
2079 cebix 1.1 // Keyboard
2080     case KeyPress: {
2081 cebix 1.51 int code = -1;
2082 cebix 1.1 if (use_keycodes) {
2083 cebix 1.51 if (event2keycode(event.xkey, true) != -2) // This is called to process the hotkeys
2084     code = keycode_table[event.xkey.keycode & 0xff];
2085 cebix 1.1 } else
2086 cebix 1.51 code = event2keycode(event.xkey, true);
2087     if (code >= 0) {
2088 cebix 1.1 if (!emul_suspended) {
2089 cebix 1.3 if (code == 0x39) { // Caps Lock pressed
2090     if (caps_on) {
2091     ADBKeyUp(code);
2092     caps_on = false;
2093     } else {
2094     ADBKeyDown(code);
2095     caps_on = true;
2096     }
2097     } else
2098     ADBKeyDown(code);
2099 cebix 1.1 if (code == 0x36)
2100     ctrl_down = true;
2101     } else {
2102     if (code == 0x31)
2103 cebix 1.44 drv->resume(); // Space wakes us up
2104 cebix 1.1 }
2105     }
2106     break;
2107     }
2108     case KeyRelease: {
2109 cebix 1.51 int code = -1;
2110 cebix 1.1 if (use_keycodes) {
2111 cebix 1.51 if (event2keycode(event.xkey, false) != -2) // This is called to process the hotkeys
2112     code = keycode_table[event.xkey.keycode & 0xff];
2113 cebix 1.1 } else
2114 cebix 1.51 code = event2keycode(event.xkey, false);
2115     if (code >= 0 && code != 0x39) { // Don't propagate Caps Lock releases
2116 cebix 1.1 ADBKeyUp(code);
2117     if (code == 0x36)
2118     ctrl_down = false;
2119     }
2120     break;
2121     }
2122    
2123     // Hidden parts exposed, force complete refresh of window
2124     case Expose:
2125 cebix 1.13 if (display_type == DISPLAY_WINDOW) {
2126 cebix 1.66 const video_mode &mode = VideoMonitors[0]->get_current_mode();
2127 gbeauche 1.19 #ifdef ENABLE_VOSF
2128 gbeauche 1.21 if (use_vosf) { // VOSF refresh
2129 cebix 1.29 LOCK_VOSF;
2130 gbeauche 1.20 PFLAG_SET_ALL;
2131 cebix 1.29 UNLOCK_VOSF;
2132 cebix 1.66 memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2133 gbeauche 1.21 }
2134     else
2135 gbeauche 1.20 #endif
2136 gbeauche 1.21 if (frame_skip == 0) { // Dynamic refresh
2137     int x1, y1;
2138     for (y1=0; y1<16; y1++)
2139     for (x1=0; x1<16; x1++)
2140     updt_box[x1][y1] = true;
2141     nr_boxes = 16 * 16;
2142     } else // Static refresh
2143 cebix 1.66 memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2144 cebix 1.13 }
2145 cebix 1.26 break;
2146    
2147 cebix 1.29 // Window "close" widget clicked
2148     case ClientMessage:
2149 cebix 1.30 if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
2150     ADBKeyDown(0x7f); // Power key
2151     ADBKeyUp(0x7f);
2152     }
2153 cebix 1.1 break;
2154     }
2155     }
2156     }
2157    
2158    
2159     /*
2160     * Window display update
2161     */
2162    
2163 cebix 1.16 // Dynamic display update (variable frame rate for each box)
2164 cebix 1.44 static void update_display_dynamic(int ticker, driver_window *drv)
2165 cebix 1.1 {
2166 cebix 1.17 int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
2167 cebix 1.13 int xil = 0;
2168     int rxm = 0, rxmo = 0;
2169 cebix 1.66 const video_mode &mode = drv->monitor.get_current_mode();
2170     int bytes_per_row = mode.bytes_per_row;
2171     int bytes_per_pixel = mode.bytes_per_row / mode.x;
2172     int rx = mode.bytes_per_row / 16;
2173     int ry = mode.y / 16;
2174 cebix 1.13 int max_box;
2175    
2176     y2s = sm_uptd[ticker % 8];
2177     y2a = 8;
2178     for (i = 0; i < 6; i++)
2179     if (ticker % (2 << i))
2180 cebix 1.1 break;
2181 cebix 1.13 max_box = sm_no_boxes[i];
2182 cebix 1.1
2183 cebix 1.13 if (y2a) {
2184     for (y1=0; y1<16; y1++) {
2185     for (y2=y2s; y2 < ry; y2 += y2a) {
2186     i = ((y1 * ry) + y2) * bytes_per_row;
2187     for (x1=0; x1<16; x1++, i += rx) {
2188     if (updt_box[x1][y1] == false) {
2189     if (memcmp(&the_buffer_copy[i], &the_buffer[i], rx)) {
2190     updt_box[x1][y1] = true;
2191     nr_boxes++;
2192     }
2193 cebix 1.1 }
2194     }
2195     }
2196 cebix 1.13 }
2197     }
2198 cebix 1.1
2199 cebix 1.13 if ((nr_boxes <= max_box) && (nr_boxes)) {
2200     for (y1=0; y1<16; y1++) {
2201     for (x1=0; x1<16; x1++) {
2202     if (updt_box[x1][y1] == true) {
2203     if (rxm == 0)
2204     xm = x1;
2205     rxm += rx;
2206     updt_box[x1][y1] = false;
2207 cebix 1.1 }
2208 cebix 1.13 if (((updt_box[x1+1][y1] == false) || (x1 == 15)) && (rxm)) {
2209     if ((rxmo != rxm) || (xmo != xm) || (yo != y1 - 1)) {
2210     if (rxmo) {
2211     xi = xmo * rx;
2212     yi = ymo * ry;
2213     xil = rxmo;
2214     yil = (yo - ymo +1) * ry;
2215     }
2216     rxmo = rxm;
2217     xmo = xm;
2218     ymo = y1;
2219 cebix 1.1 }
2220 cebix 1.13 rxm = 0;
2221     yo = y1;
2222     }
2223     if (xil) {
2224     i = (yi * bytes_per_row) + xi;
2225     for (y2=0; y2 < yil; y2++, i += bytes_per_row)
2226     memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
2227 cebix 1.66 if (mode.depth == VDEPTH_1BIT) {
2228 cebix 1.44 if (drv->have_shm)
2229     XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
2230 cebix 1.13 else
2231 cebix 1.44 XPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil);
2232 cebix 1.13 } else {
2233 cebix 1.44 if (drv->have_shm)
2234     XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0);
2235 cebix 1.13 else
2236 cebix 1.44 XPutImage(x_display, drv->w, drv->gc, drv->img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil);
2237 cebix 1.1 }
2238 cebix 1.13 xil = 0;
2239 cebix 1.1 }
2240 cebix 1.13 if ((x1 == 15) && (y1 == 15) && (rxmo)) {
2241     x1--;
2242     xi = xmo * rx;
2243     yi = ymo * ry;
2244     xil = rxmo;
2245     yil = (yo - ymo +1) * ry;
2246     rxmo = 0;
2247 cebix 1.1 }
2248     }
2249     }
2250 cebix 1.13 nr_boxes = 0;
2251 cebix 1.1 }
2252     }
2253    
2254 cebix 1.16 // Static display update (fixed frame rate, but incremental)
2255 cebix 1.44 static void update_display_static(driver_window *drv)
2256 cebix 1.16 {
2257     // Incremental update code
2258 cebix 1.60 unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j;
2259 cebix 1.66 const video_mode &mode = drv->monitor.get_current_mode();
2260     int bytes_per_row = mode.bytes_per_row;
2261     int bytes_per_pixel = mode.bytes_per_row / mode.x;
2262 cebix 1.16 uint8 *p, *p2;
2263    
2264     // Check for first line from top and first line from bottom that have changed
2265     y1 = 0;
2266 cebix 1.66 for (j=0; j<mode.y; j++) {
2267 cebix 1.16 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2268     y1 = j;
2269     break;
2270     }
2271     }
2272     y2 = y1 - 1;
2273 cebix 1.66 for (j=mode.y-1; j>=y1; j--) {
2274 cebix 1.16 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2275     y2 = j;
2276     break;
2277     }
2278     }
2279     high = y2 - y1 + 1;
2280    
2281     // Check for first column from left and first column from right that have changed
2282     if (high) {
2283 cebix 1.66 if (mode.depth == VDEPTH_1BIT) {
2284     x1 = mode.x - 1;
2285 cebix 1.16 for (j=y1; j<=y2; j++) {
2286     p = &the_buffer[j * bytes_per_row];
2287     p2 = &the_buffer_copy[j * bytes_per_row];
2288     for (i=0; i<(x1>>3); i++) {
2289     if (*p != *p2) {
2290     x1 = i << 3;
2291     break;
2292     }
2293 cebix 1.18 p++; p2++;
2294 cebix 1.16 }
2295     }
2296     x2 = x1;
2297     for (j=y1; j<=y2; j++) {
2298     p = &the_buffer[j * bytes_per_row];
2299     p2 = &the_buffer_copy[j * bytes_per_row];
2300     p += bytes_per_row;
2301     p2 += bytes_per_row;
2302 cebix 1.66 for (i=(mode.x>>3); i>(x2>>3); i--) {
2303 cebix 1.18 p--; p2--;
2304 cebix 1.16 if (*p != *p2) {
2305 cebix 1.25 x2 = (i << 3) + 7;
2306 cebix 1.16 break;
2307     }
2308     }
2309     }
2310 cebix 1.25 wide = x2 - x1 + 1;
2311 cebix 1.16
2312     // Update copy of the_buffer
2313     if (high && wide) {
2314     for (j=y1; j<=y2; j++) {
2315     i = j * bytes_per_row + (x1 >> 3);
2316     memcpy(the_buffer_copy + i, the_buffer + i, wide >> 3);
2317     }
2318     }
2319    
2320     } else {
2321 cebix 1.66 x1 = mode.x;
2322 cebix 1.16 for (j=y1; j<=y2; j++) {
2323     p = &the_buffer[j * bytes_per_row];
2324     p2 = &the_buffer_copy[j * bytes_per_row];
2325 cebix 1.18 for (i=0; i<x1*bytes_per_pixel; i++) {
2326     if (*p != *p2) {
2327     x1 = i / bytes_per_pixel;
2328 cebix 1.16 break;
2329     }
2330 cebix 1.18 p++; p2++;
2331 cebix 1.16 }
2332     }
2333     x2 = x1;
2334     for (j=y1; j<=y2; j++) {
2335     p = &the_buffer[j * bytes_per_row];
2336     p2 = &the_buffer_copy[j * bytes_per_row];
2337     p += bytes_per_row;
2338     p2 += bytes_per_row;
2339 cebix 1.66 for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2340 cebix 1.18 p--;
2341     p2--;
2342     if (*p != *p2) {
2343     x2 = i / bytes_per_pixel;
2344 cebix 1.16 break;
2345     }
2346     }
2347     }
2348     wide = x2 - x1;
2349    
2350     // Update copy of the_buffer
2351     if (high && wide) {
2352     for (j=y1; j<=y2; j++) {
2353     i = j * bytes_per_row + x1 * bytes_per_pixel;
2354     memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
2355     }
2356     }
2357     }
2358     }
2359    
2360     // Refresh display
2361     if (high && wide) {
2362 cebix 1.44 if (drv->have_shm)
2363     XShmPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high, 0);
2364 cebix 1.16 else
2365 cebix 1.44 XPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high);
2366 cebix 1.16 }
2367     }
2368    
2369 cebix 1.1
2370     /*
2371 gbeauche 1.19 * Screen refresh functions
2372     */
2373    
2374 gbeauche 1.34 // We suggest the compiler to inline the next two functions so that it
2375     // may specialise the code according to the current screen depth and
2376 gbeauche 1.35 // display type. A clever compiler would do that job by itself though...
2377 gbeauche 1.34
2378     // NOTE: update_display_vosf is inlined too
2379 gbeauche 1.19
2380     static inline void possibly_quit_dga_mode()
2381     {
2382 cebix 1.51 // Quit DGA mode if requested (something terrible has happened and we
2383     // want to give control back to the user)
2384 gbeauche 1.19 if (quit_full_screen) {
2385     quit_full_screen = false;
2386 cebix 1.44 delete drv;
2387     drv = NULL;
2388 gbeauche 1.19 }
2389     }
2390    
2391 cebix 1.51 static inline void possibly_ungrab_mouse()
2392     {
2393     // Ungrab mouse if requested (something terrible has happened and we
2394     // want to give control back to the user)
2395     if (quit_full_screen) {
2396     quit_full_screen = false;
2397     if (drv)
2398     drv->ungrab_mouse();
2399     }
2400     }
2401    
2402 cebix 1.44 static inline void handle_palette_changes(void)
2403 gbeauche 1.19 {
2404 cebix 1.29 LOCK_PALETTE;
2405    
2406 cebix 1.66 if (x_palette_changed) {
2407     x_palette_changed = false;
2408 cebix 1.44 drv->update_palette();
2409 gbeauche 1.19 }
2410 cebix 1.29
2411     UNLOCK_PALETTE;
2412 gbeauche 1.19 }
2413    
2414     static void video_refresh_dga(void)
2415     {
2416     // Quit DGA mode if requested
2417     possibly_quit_dga_mode();
2418     }
2419    
2420 cebix 1.23 #ifdef ENABLE_VOSF
2421 gbeauche 1.19 #if REAL_ADDRESSING || DIRECT_ADDRESSING
2422     static void video_refresh_dga_vosf(void)
2423     {
2424     // Quit DGA mode if requested
2425     possibly_quit_dga_mode();
2426    
2427     // Update display (VOSF variant)
2428     static int tick_counter = 0;
2429     if (++tick_counter >= frame_skip) {
2430     tick_counter = 0;
2431 gbeauche 1.36 if (mainBuffer.dirty) {
2432     LOCK_VOSF;
2433     update_display_dga_vosf();
2434     UNLOCK_VOSF;
2435     }
2436 gbeauche 1.19 }
2437     }
2438     #endif
2439    
2440     static void video_refresh_window_vosf(void)
2441     {
2442 cebix 1.51 // Ungrab mouse if requested
2443     possibly_ungrab_mouse();
2444 gbeauche 1.19
2445     // Update display (VOSF variant)
2446     static int tick_counter = 0;
2447     if (++tick_counter >= frame_skip) {
2448     tick_counter = 0;
2449 gbeauche 1.36 if (mainBuffer.dirty) {
2450     LOCK_VOSF;
2451 cebix 1.44 update_display_window_vosf(static_cast<driver_window *>(drv));
2452 gbeauche 1.36 UNLOCK_VOSF;
2453 cebix 1.37 XSync(x_display, false); // Let the server catch up
2454 gbeauche 1.36 }
2455 gbeauche 1.19 }
2456     }
2457 cebix 1.23 #endif // def ENABLE_VOSF
2458 gbeauche 1.19
2459     static void video_refresh_window_static(void)
2460     {
2461 cebix 1.51 // Ungrab mouse if requested
2462     possibly_ungrab_mouse();
2463    
2464 gbeauche 1.19 // Update display (static variant)
2465     static int tick_counter = 0;
2466     if (++tick_counter >= frame_skip) {
2467     tick_counter = 0;
2468 cebix 1.44 update_display_static(static_cast<driver_window *>(drv));
2469 gbeauche 1.19 }
2470     }
2471    
2472     static void video_refresh_window_dynamic(void)
2473     {
2474 cebix 1.51 // Ungrab mouse if requested
2475     possibly_ungrab_mouse();
2476    
2477 gbeauche 1.19 // Update display (dynamic variant)
2478     static int tick_counter = 0;
2479     tick_counter++;
2480 cebix 1.44 update_display_dynamic(tick_counter, static_cast<driver_window *>(drv));
2481 gbeauche 1.19 }
2482    
2483    
2484     /*
2485 cebix 1.1 * Thread for screen refresh, input handling etc.
2486     */
2487    
2488 cebix 1.42 static void VideoRefreshInit(void)
2489 gbeauche 1.19 {
2490     // TODO: set up specialised 8bpp VideoRefresh handlers ?
2491     if (display_type == DISPLAY_DGA) {
2492 cebix 1.23 #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
2493 gbeauche 1.19 if (use_vosf)
2494     video_refresh = video_refresh_dga_vosf;
2495     else
2496     #endif
2497     video_refresh = video_refresh_dga;
2498     }
2499     else {
2500     #ifdef ENABLE_VOSF
2501     if (use_vosf)
2502     video_refresh = video_refresh_window_vosf;
2503     else
2504     #endif
2505     if (frame_skip == 0)
2506     video_refresh = video_refresh_window_dynamic;
2507     else
2508     video_refresh = video_refresh_window_static;
2509     }
2510     }
2511    
2512 cebix 1.57 // This function is called on non-threaded platforms from a timer interrupt
2513 gbeauche 1.19 void VideoRefresh(void)
2514     {
2515 cebix 1.45 // We need to check redraw_thread_active to inhibit refreshed during
2516     // mode changes on non-threaded platforms
2517 cebix 1.57 if (!redraw_thread_active)
2518     return;
2519    
2520     // Handle X events
2521     handle_events();
2522    
2523     // Handle palette changes
2524     handle_palette_changes();
2525    
2526     // Update display
2527     video_refresh();
2528 gbeauche 1.19 }
2529    
2530 cebix 1.57 const int VIDEO_REFRESH_HZ = 60;
2531     const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2532    
2533 cebix 1.15 #ifdef HAVE_PTHREADS
2534     static void *redraw_func(void *arg)
2535     {
2536 cebix 1.57 int fd = ConnectionNumber(x_display);
2537    
2538 gbeauche 1.19 uint64 start = GetTicks_usec();
2539     int64 ticks = 0;
2540 cebix 1.57 uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2541    
2542 cebix 1.15 while (!redraw_thread_cancel) {
2543 cebix 1.57
2544 cebix 1.18 int64 delay = next - GetTicks_usec();
2545 cebix 1.57 if (delay < -VIDEO_REFRESH_DELAY) {
2546    
2547     // We are lagging far behind, so we reset the delay mechanism
2548 cebix 1.18 next = GetTicks_usec();
2549 cebix 1.57
2550     } else if (delay <= 0) {
2551    
2552     // Delay expired, refresh display
2553     handle_events();
2554     handle_palette_changes();
2555     video_refresh();
2556     next += VIDEO_REFRESH_DELAY;
2557     ticks++;
2558    
2559     } else {
2560    
2561     // No display refresh pending, check for X events
2562     fd_set readfds;
2563     FD_ZERO(&readfds);
2564     FD_SET(fd, &readfds);
2565     struct timeval timeout;
2566     timeout.tv_sec = 0;
2567     timeout.tv_usec = delay;
2568     if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2569     handle_events();
2570     }
2571 cebix 1.1 }
2572 cebix 1.57
2573 gbeauche 1.19 uint64 end = GetTicks_usec();
2574 cebix 1.57 D(bug("%Ld refreshes in %Ld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
2575 cebix 1.1 return NULL;
2576     }
2577 cebix 1.15 #endif