ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.39
Committed: 2001-06-26T22:35:41Z (22 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.38: +29 -33 lines
Log Message:
- added SIGSEGV support for Linux/Alpha (to be checked), Darwin/PPC
- added uniform virtual memory allocation
  (supports mmap(), vm_allocate(), or fallbacks to malloc()/free())
- cleaned up memory allocation in main_unix.cpp

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * video_x.cpp - Video/graphics emulation, X11 specific stuff
3     *
4 gbeauche 1.36 * Basilisk II (C) 1997-2001 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     */
28    
29     #include "sysdeps.h"
30    
31     #include <X11/Xlib.h>
32     #include <X11/Xutil.h>
33     #include <X11/keysym.h>
34     #include <X11/extensions/XShm.h>
35     #include <sys/ipc.h>
36     #include <sys/shm.h>
37     #include <errno.h>
38    
39 cebix 1.15 #ifdef HAVE_PTHREADS
40     # include <pthread.h>
41     #endif
42    
43     #ifdef ENABLE_XF86_DGA
44     # include <X11/extensions/xf86dga.h>
45     #endif
46    
47     #ifdef ENABLE_XF86_VIDMODE
48     # include <X11/extensions/xf86vmode.h>
49     #endif
50    
51     #ifdef ENABLE_FBDEV_DGA
52     # include <sys/mman.h>
53     #endif
54    
55 gbeauche 1.19 #ifdef ENABLE_VOSF
56     # include <fcntl.h>
57     # include <sys/mman.h>
58 gbeauche 1.38 # include "sigsegv.h"
59 gbeauche 1.39 # include "vm_alloc.h"
60 gbeauche 1.19 #endif
61    
62 cebix 1.1 #include "cpu_emulation.h"
63     #include "main.h"
64     #include "adb.h"
65     #include "macos_util.h"
66     #include "prefs.h"
67     #include "user_strings.h"
68     #include "video.h"
69    
70 cebix 1.16 #define DEBUG 0
71 cebix 1.1 #include "debug.h"
72    
73    
74     // Display types
75     enum {
76     DISPLAY_WINDOW, // X11 window, using MIT SHM extensions if possible
77     DISPLAY_DGA // DGA fullscreen display
78     };
79    
80     // Constants
81 cebix 1.4 const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
82 cebix 1.7 const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices";
83 cebix 1.1
84    
85     // Global variables
86 cebix 1.10 static int32 frame_skip; // Prefs items
87     static int16 mouse_wheel_mode = 1;
88     static int16 mouse_wheel_lines = 3;
89    
90 cebix 1.1 static int display_type = DISPLAY_WINDOW; // See enum above
91 cebix 1.16 static bool local_X11; // Flag: X server running on local machine?
92 cebix 1.1 static uint8 *the_buffer; // Mac frame buffer
93 cebix 1.15
94     #ifdef HAVE_PTHREADS
95 cebix 1.1 static bool redraw_thread_active = false; // Flag: Redraw thread installed
96     static volatile bool redraw_thread_cancel = false; // Flag: Cancel Redraw thread
97     static pthread_t redraw_thread; // Redraw thread
98 cebix 1.15 #endif
99 cebix 1.1
100     static bool has_dga = false; // Flag: Video DGA capable
101 cebix 1.12 static bool has_vidmode = false; // Flag: VidMode extension available
102 cebix 1.1
103     static bool ctrl_down = false; // Flag: Ctrl key pressed
104 cebix 1.3 static bool caps_on = false; // Flag: Caps Lock on
105 cebix 1.1 static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
106     static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
107     static bool emul_suspended = false; // Flag: Emulator suspended
108    
109     static bool classic_mode = false; // Flag: Classic Mac video mode
110    
111     static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
112     static int keycode_table[256]; // X keycode -> Mac keycode translation table
113    
114     // X11 variables
115     static int screen; // Screen number
116     static int xdepth; // Depth of X screen
117     static int depth; // Depth of Mac frame buffer
118     static Window rootwin, the_win; // Root window and our window
119     static XVisualInfo visualInfo;
120     static Visual *vis;
121     static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode
122     static XColor black, white;
123     static unsigned long black_pixel, white_pixel;
124     static int eventmask;
125 cebix 1.29 static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
126     static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
127     static Atom WM_DELETE_WINDOW = (Atom)0;
128 cebix 1.1
129     static XColor palette[256]; // Color palette for 8-bit mode
130     static bool palette_changed = false; // Flag: Palette changed, redraw thread must set new colors
131 cebix 1.15 #ifdef HAVE_PTHREADS
132     static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect palette
133 cebix 1.29 #define LOCK_PALETTE pthread_mutex_lock(&palette_lock)
134     #define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock)
135     #else
136     #define LOCK_PALETTE
137     #define UNLOCK_PALETTE
138 cebix 1.15 #endif
139 cebix 1.1
140     // Variables for window mode
141     static GC the_gc;
142     static XImage *img = NULL;
143     static XShmSegmentInfo shminfo;
144     static Cursor mac_cursor;
145     static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer
146     static bool have_shm = false; // Flag: SHM extensions available
147 cebix 1.13 static bool updt_box[17][17]; // Flag for Update
148     static int nr_boxes;
149     static const int sm_uptd[] = {4,1,6,3,0,5,2,7};
150     static int sm_no_boxes[] = {1,8,32,64,128,300};
151 cebix 1.1
152 cebix 1.7 // Variables for XF86 DGA mode
153 cebix 1.1 static int current_dga_cmap; // Number (0 or 1) of currently installed DGA colormap
154     static Window suspend_win; // "Suspend" window
155     static void *fb_save = NULL; // Saved frame buffer for suspend
156 cebix 1.15 #ifdef HAVE_PTHREADS
157 cebix 1.7 static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer
158 cebix 1.29 #define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock);
159     #define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock);
160     #else
161     #define LOCK_FRAME_BUFFER
162     #define UNLOCK_FRAME_BUFFER
163 cebix 1.15 #endif
164 cebix 1.1
165 cebix 1.7 // Variables for fbdev DGA mode
166     const char FBDEVICE_FILE_NAME[] = "/dev/fb";
167     static int fbdev_fd;
168 cebix 1.1
169 cebix 1.15 #ifdef ENABLE_XF86_VIDMODE
170 cebix 1.12 // Variables for XF86 VidMode support
171     static XF86VidModeModeInfo **x_video_modes; // Array of all available modes
172     static int num_x_video_modes;
173     #endif
174    
175 gbeauche 1.19 #ifdef ENABLE_VOSF
176     static bool use_vosf = true; // Flag: VOSF enabled
177     #else
178     static const bool use_vosf = false; // Flag: VOSF enabled
179     #endif
180    
181     #ifdef ENABLE_VOSF
182 cebix 1.29 // Variables for Video on SEGV support (taken from the Win32 port)
183     static uint8 *the_host_buffer; // Host frame buffer in VOSF mode
184 gbeauche 1.19 static uint32 the_buffer_size; // Size of allocated the_buffer
185    
186     struct ScreenPageInfo {
187     int top, bottom; // Mapping between this virtual page and Mac scanlines
188     };
189    
190     struct ScreenInfo {
191 gbeauche 1.39 uintptr memBase; // Real start address
192     uintptr memStart; // Start address aligned to page boundary
193     uintptr memEnd; // Address of one-past-the-end of the screen
194 gbeauche 1.19 uint32 memLength; // Length of the memory addressed by the screen pages
195    
196     uint32 pageSize; // Size of a page
197     int pageBits; // Shift count to get the page number
198     uint32 pageCount; // Number of pages allocated to the screen
199    
200 gbeauche 1.36 bool dirty; // Flag: set if the frame buffer was touched
201 gbeauche 1.34 char * dirtyPages; // Table of flags set if page was altered
202 gbeauche 1.19 ScreenPageInfo * pageInfo; // Table of mappings page -> Mac scanlines
203     };
204    
205     static ScreenInfo mainBuffer;
206    
207 gbeauche 1.34 #define PFLAG_SET_VALUE 0x00
208     #define PFLAG_CLEAR_VALUE 0x01
209     #define PFLAG_SET_VALUE_4 0x00000000
210     #define PFLAG_CLEAR_VALUE_4 0x01010101
211     #define PFLAG_SET(page) mainBuffer.dirtyPages[page] = PFLAG_SET_VALUE
212     #define PFLAG_CLEAR(page) mainBuffer.dirtyPages[page] = PFLAG_CLEAR_VALUE
213     #define PFLAG_ISSET(page) (mainBuffer.dirtyPages[page] == PFLAG_SET_VALUE)
214     #define PFLAG_ISCLEAR(page) (mainBuffer.dirtyPages[page] != PFLAG_SET_VALUE)
215    
216 gbeauche 1.19 #ifdef UNALIGNED_PROFITABLE
217 gbeauche 1.34 # define PFLAG_ISSET_4(page) (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_SET_VALUE_4)
218     # define PFLAG_ISCLEAR_4(page) (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_CLEAR_VALUE_4)
219     #else
220     # define PFLAG_ISSET_4(page) \
221     PFLAG_ISSET(page ) && PFLAG_ISSET(page+1) \
222     && PFLAG_ISSET(page+2) && PFLAG_ISSET(page+3)
223     # define PFLAG_ISCLEAR_4(page) \
224     PFLAG_ISCLEAR(page ) && PFLAG_ISCLEAR(page+1) \
225     && PFLAG_ISCLEAR(page+2) && PFLAG_ISCLEAR(page+3)
226     #endif
227    
228     // Set the selected page range [ first_page, last_page [ into the SET state
229     #define PFLAG_SET_RANGE(first_page, last_page) \
230     memset(mainBuffer.dirtyPages + (first_page), PFLAG_SET_VALUE, \
231     (last_page) - (first_page))
232    
233     // Set the selected page range [ first_page, last_page [ into the CLEAR state
234     #define PFLAG_CLEAR_RANGE(first_page, last_page) \
235     memset(mainBuffer.dirtyPages + (first_page), PFLAG_CLEAR_VALUE, \
236     (last_page) - (first_page))
237    
238 gbeauche 1.36 #define PFLAG_SET_ALL do { \
239     PFLAG_SET_RANGE(0, mainBuffer.pageCount); \
240     mainBuffer.dirty = true; \
241     } while (0)
242    
243     #define PFLAG_CLEAR_ALL do { \
244     PFLAG_CLEAR_RANGE(0, mainBuffer.pageCount); \
245     mainBuffer.dirty = false; \
246     } while (0)
247 gbeauche 1.34
248     // Set the following macro definition to 1 if your system
249     // provides a really fast strchr() implementation
250     //#define HAVE_FAST_STRCHR 0
251    
252     static inline int find_next_page_set(int page)
253     {
254     #if HAVE_FAST_STRCHR
255     char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_SET_VALUE);
256     return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
257     #else
258     while (PFLAG_ISCLEAR_4(page))
259     page += 4;
260     while (PFLAG_ISCLEAR(page))
261     page++;
262     return page;
263     #endif
264     }
265    
266     static inline int find_next_page_clear(int page)
267     {
268     #if HAVE_FAST_STRCHR
269     char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_CLEAR_VALUE);
270     return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
271 gbeauche 1.19 #else
272 gbeauche 1.34 while (PFLAG_ISSET_4(page))
273     page += 4;
274     while (PFLAG_ISSET(page))
275     page++;
276     return page;
277 gbeauche 1.19 #endif
278 gbeauche 1.34 }
279 gbeauche 1.19
280     static int zero_fd = -1;
281    
282     #ifdef HAVE_PTHREADS
283 cebix 1.29 static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer (dirtyPages in fact)
284     #define LOCK_VOSF pthread_mutex_lock(&vosf_lock);
285     #define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock);
286     #else
287     #define LOCK_VOSF
288     #define UNLOCK_VOSF
289 gbeauche 1.19 #endif
290    
291 cebix 1.25 static int log_base_2(uint32 x)
292     {
293     uint32 mask = 0x80000000;
294     int l = 31;
295     while (l >= 0 && (x & mask) == 0) {
296     mask >>= 1;
297     l--;
298     }
299     return l;
300     }
301 gbeauche 1.34 #endif /* ENABLE_VOSF */
302 gbeauche 1.19
303     // VideoRefresh function
304     void VideoRefreshInit(void);
305     static void (*video_refresh)(void);
306 cebix 1.1
307     // Prototypes
308     static void *redraw_func(void *arg);
309 cebix 1.29 static int event2keycode(XKeyEvent &ev);
310 cebix 1.1
311    
312     // From main_unix.cpp
313 cebix 1.16 extern char *x_display_name;
314 cebix 1.1 extern Display *x_display;
315    
316     // From sys_unix.cpp
317     extern void SysMountFirstFloppy(void);
318    
319 gbeauche 1.19 #ifdef ENABLE_VOSF
320     # include "video_vosf.h"
321     #endif
322    
323 cebix 1.1
324     /*
325     * Initialization
326     */
327    
328     // Set VideoMonitor according to video mode
329     void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order)
330     {
331 gbeauche 1.19 #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
332 cebix 1.5 int layout = FLAYOUT_DIRECT;
333 cebix 1.1 switch (depth) {
334     case 1:
335     layout = FLAYOUT_DIRECT;
336 cebix 1.15 break;
337     case 8:
338     layout = FLAYOUT_DIRECT;
339     break;
340     case 15:
341     layout = FLAYOUT_HOST_555;
342     break;
343     case 16:
344     layout = FLAYOUT_HOST_565;
345     break;
346     case 24:
347     case 32:
348     layout = FLAYOUT_HOST_888;
349     break;
350     }
351     if (native_byte_order)
352     MacFrameLayout = layout;
353     else
354     MacFrameLayout = FLAYOUT_DIRECT;
355     #endif
356     switch (depth) {
357     case 1:
358 cebix 1.1 VideoMonitor.mode = VMODE_1BIT;
359     break;
360     case 8:
361     VideoMonitor.mode = VMODE_8BIT;
362     break;
363     case 15:
364     VideoMonitor.mode = VMODE_16BIT;
365     break;
366     case 16:
367     VideoMonitor.mode = VMODE_16BIT;
368     break;
369     case 24:
370     case 32:
371     VideoMonitor.mode = VMODE_32BIT;
372     break;
373     }
374     VideoMonitor.x = width;
375     VideoMonitor.y = height;
376     VideoMonitor.bytes_per_row = bytes_per_row;
377     }
378    
379 cebix 1.29 // Set window name and class
380     static void set_window_name(Window w, int name)
381     {
382     const char *str = GetString(name);
383     XStoreName(x_display, w, str);
384     XSetIconName(x_display, w, str);
385    
386     XClassHint *hints;
387     hints = XAllocClassHint();
388     if (hints) {
389     hints->res_name = "BasiliskII";
390     hints->res_class = "BasiliskII";
391     XSetClassHint(x_display, w, hints);
392 cebix 1.33 XFree(hints);
393 cebix 1.29 }
394     }
395    
396     // Set window input focus flag
397     static void set_window_focus(Window w)
398     {
399     XWMHints *hints = XAllocWMHints();
400     if (hints) {
401     hints->input = True;
402     hints->initial_state = NormalState;
403     hints->flags = InputHint | StateHint;
404     XSetWMHints(x_display, w, hints);
405 cebix 1.33 XFree(hints);
406 cebix 1.29 }
407     }
408    
409     // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
410     static void set_window_delete_protocol(Window w)
411     {
412     WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
413     XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
414     }
415    
416     // Wait until window is mapped/unmapped
417     void wait_mapped(Window w)
418     {
419     XEvent e;
420     do {
421     XMaskEvent(x_display, StructureNotifyMask, &e);
422     } while ((e.type != MapNotify) || (e.xmap.event != w));
423     }
424    
425     void wait_unmapped(Window w)
426     {
427     XEvent e;
428     do {
429     XMaskEvent(x_display, StructureNotifyMask, &e);
430     } while ((e.type != UnmapNotify) || (e.xmap.event != w));
431     }
432    
433 cebix 1.1 // Trap SHM errors
434     static bool shm_error = false;
435     static int (*old_error_handler)(Display *, XErrorEvent *);
436    
437     static int error_handler(Display *d, XErrorEvent *e)
438     {
439     if (e->error_code == BadAccess) {
440     shm_error = true;
441     return 0;
442     } else
443     return old_error_handler(d, e);
444     }
445    
446     // Init window mode
447     static bool init_window(int width, int height)
448     {
449 cebix 1.13 int aligned_width = (width + 15) & ~15;
450     int aligned_height = (height + 15) & ~15;
451    
452 cebix 1.1 // Set absolute mouse mode
453     ADBSetRelMouseMode(false);
454    
455     // Read frame skip prefs
456     frame_skip = PrefsFindInt32("frameskip");
457    
458     // Create window
459     XSetWindowAttributes wattr;
460     wattr.event_mask = eventmask = win_eventmask;
461     wattr.background_pixel = black_pixel;
462 cebix 1.29 wattr.colormap = cmap[0];
463 cebix 1.1
464     the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
465 cebix 1.29 InputOutput, vis, CWEventMask | CWBackPixel | (depth == 8 ? CWColormap : 0), &wattr);
466    
467     // Set window name/class
468     set_window_name(the_win, STR_WINDOW_TITLE);
469 cebix 1.26
470     // Indicate that we want keyboard input
471 cebix 1.29 set_window_focus(the_win);
472    
473     // Set delete protocol property
474     set_window_delete_protocol(the_win);
475 cebix 1.26
476     // Make window unresizable
477     {
478     XSizeHints *hints = XAllocSizeHints();
479     if (hints) {
480     hints->min_width = width;
481     hints->max_width = width;
482     hints->min_height = height;
483     hints->max_height = height;
484     hints->flags = PMinSize | PMaxSize;
485     XSetWMNormalHints(x_display, the_win, hints);
486 cebix 1.33 XFree(hints);
487 cebix 1.26 }
488     }
489    
490     // Show window
491 cebix 1.29 XMapWindow(x_display, the_win);
492     wait_mapped(the_win);
493 cebix 1.1
494     // Try to create and attach SHM image
495     have_shm = false;
496 cebix 1.16 if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
497 cebix 1.1
498     // Create SHM image ("height + 2" for safety)
499     img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
500 cebix 1.13 shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
501     the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
502     shminfo.shmaddr = img->data = (char *)the_buffer_copy;
503 cebix 1.1 shminfo.readOnly = False;
504    
505     // Try to attach SHM image, catching errors
506     shm_error = false;
507     old_error_handler = XSetErrorHandler(error_handler);
508     XShmAttach(x_display, &shminfo);
509     XSync(x_display, false);
510     XSetErrorHandler(old_error_handler);
511     if (shm_error) {
512     shmdt(shminfo.shmaddr);
513     XDestroyImage(img);
514     shminfo.shmid = -1;
515     } else {
516     have_shm = true;
517     shmctl(shminfo.shmid, IPC_RMID, 0);
518     }
519     }
520 cebix 1.7
521 cebix 1.1 // Create normal X image if SHM doesn't work ("height + 2" for safety)
522     if (!have_shm) {
523 cebix 1.13 int bytes_per_row = aligned_width;
524 cebix 1.1 switch (depth) {
525     case 1:
526     bytes_per_row /= 8;
527     break;
528     case 15:
529     case 16:
530     bytes_per_row *= 2;
531     break;
532     case 24:
533     case 32:
534     bytes_per_row *= 4;
535     break;
536     }
537 cebix 1.13 the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
538     img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
539 cebix 1.1 }
540    
541     // 1-Bit mode is big-endian
542     if (depth == 1) {
543     img->byte_order = MSBFirst;
544     img->bitmap_bit_order = MSBFirst;
545     }
546    
547 gbeauche 1.19 #ifdef ENABLE_VOSF
548 gbeauche 1.39 // Allocate memory for frame buffer (SIZE is extended to page-boundary)
549 gbeauche 1.19 the_host_buffer = the_buffer_copy;
550 gbeauche 1.39 the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
551     the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
552     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
553 gbeauche 1.19 #else
554 cebix 1.16 // Allocate memory for frame buffer
555 cebix 1.13 the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
556 gbeauche 1.19 #endif
557 cebix 1.1
558     // Create GC
559     the_gc = XCreateGC(x_display, the_win, 0, 0);
560     XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
561    
562 cebix 1.13 // Create no_cursor
563 cebix 1.16 mac_cursor = XCreatePixmapCursor(x_display,
564     XCreatePixmap(x_display, the_win, 1, 1, 1),
565     XCreatePixmap(x_display, the_win, 1, 1, 1),
566     &black, &white, 0, 0);
567     XDefineCursor(x_display, the_win, mac_cursor);
568 cebix 1.1
569     // Set VideoMonitor
570 gbeauche 1.19 bool native_byte_order;
571 cebix 1.1 #ifdef WORDS_BIGENDIAN
572 cebix 1.31 native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
573 cebix 1.1 #else
574 cebix 1.31 native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
575 cebix 1.1 #endif
576 gbeauche 1.19 #ifdef ENABLE_VOSF
577 gbeauche 1.36 Screen_blitter_init(&visualInfo, native_byte_order);
578 gbeauche 1.19 #endif
579     set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
580 cebix 1.7
581 gbeauche 1.19 #if REAL_ADDRESSING || DIRECT_ADDRESSING
582     VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
583 cebix 1.7 #else
584     VideoMonitor.mac_frame_base = MacFrameBaseMac;
585     #endif
586     return true;
587     }
588    
589     // Init fbdev DGA display
590     static bool init_fbdev_dga(char *in_fb_name)
591     {
592 cebix 1.15 #ifdef ENABLE_FBDEV_DGA
593 cebix 1.7 // Find the maximum depth available
594     int ndepths, max_depth(0);
595     int *depths = XListDepths(x_display, screen, &ndepths);
596     if (depths == NULL) {
597 cebix 1.9 printf("FATAL: Could not determine the maximal depth available\n");
598 cebix 1.7 return false;
599     } else {
600     while (ndepths-- > 0) {
601     if (depths[ndepths] > max_depth)
602     max_depth = depths[ndepths];
603     }
604     }
605    
606     // Get fbdevices file path from preferences
607 cebix 1.8 const char *fbd_path = PrefsFindString("fbdevicefile");
608 cebix 1.7
609     // Open fbdevices file
610     FILE *fp = fopen(fbd_path ? fbd_path : FBDEVICES_FILE_NAME, "r");
611     if (fp == NULL) {
612     char str[256];
613     sprintf(str, GetString(STR_NO_FBDEVICE_FILE_ERR), fbd_path ? fbd_path : FBDEVICES_FILE_NAME, strerror(errno));
614     ErrorAlert(str);
615     return false;
616     }
617    
618     int fb_depth; // supported depth
619     uint32 fb_offset; // offset used for mmap(2)
620     char fb_name[20];
621     char line[256];
622     bool device_found = false;
623     while (fgets(line, 255, fp)) {
624     // Read line
625     int len = strlen(line);
626     if (len == 0)
627     continue;
628     line[len - 1] = '\0';
629    
630     // Comments begin with "#" or ";"
631     if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
632     continue;
633    
634 cebix 1.8 if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
635 cebix 1.7 && (strcmp(fb_name, in_fb_name) == 0) && (fb_depth == max_depth)) {
636     device_found = true;
637     break;
638     }
639     }
640    
641     // fbdevices file completely read
642     fclose(fp);
643    
644     // Frame buffer name not found ? Then, display warning
645     if (!device_found) {
646     char str[256];
647     sprintf(str, GetString(STR_FBDEV_NAME_ERR), in_fb_name, max_depth);
648     ErrorAlert(str);
649     return false;
650     }
651    
652     int width = DisplayWidth(x_display, screen);
653     int height = DisplayHeight(x_display, screen);
654     depth = fb_depth; // max_depth
655    
656     // Set relative mouse mode
657     ADBSetRelMouseMode(false);
658    
659     // Create window
660     XSetWindowAttributes wattr;
661 cebix 1.29 wattr.event_mask = eventmask = dga_eventmask;
662     wattr.background_pixel = white_pixel;
663     wattr.override_redirect = True;
664     wattr.colormap = cmap[0];
665 cebix 1.7
666     the_win = XCreateWindow(x_display, rootwin,
667     0, 0, width, height,
668     0, xdepth, InputOutput, vis,
669 cebix 1.29 CWEventMask | CWBackPixel | CWOverrideRedirect | (depth == 8 ? CWColormap : 0),
670 cebix 1.7 &wattr);
671 cebix 1.29
672     // Set window name/class
673     set_window_name(the_win, STR_WINDOW_TITLE);
674    
675     // Indicate that we want keyboard input
676     set_window_focus(the_win);
677    
678     // Show window
679 cebix 1.7 XMapRaised(x_display, the_win);
680 cebix 1.29 wait_mapped(the_win);
681 cebix 1.7
682     // Grab mouse and keyboard
683     XGrabKeyboard(x_display, the_win, True,
684     GrabModeAsync, GrabModeAsync, CurrentTime);
685     XGrabPointer(x_display, the_win, True,
686     PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
687     GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
688    
689     // Set VideoMonitor
690     int bytes_per_row = width;
691     switch (depth) {
692     case 1:
693     bytes_per_row = ((width | 7) & ~7) >> 3;
694     break;
695     case 15:
696     case 16:
697     bytes_per_row *= 2;
698     break;
699     case 24:
700     case 32:
701     bytes_per_row *= 4;
702     break;
703     }
704    
705     if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
706     if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
707     char str[256];
708     sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
709     ErrorAlert(str);
710     return false;
711     }
712     }
713    
714 cebix 1.23 #if ENABLE_VOSF
715 gbeauche 1.19 #if REAL_ADDRESSING || DIRECT_ADDRESSING
716 gbeauche 1.36 // Screen_blitter_init() returns TRUE if VOSF is mandatory
717     // i.e. the framebuffer update function is not Blit_Copy_Raw
718     use_vosf = Screen_blitter_init(&visualInfo, true);
719 gbeauche 1.19
720     if (use_vosf) {
721 gbeauche 1.39 // Allocate memory for frame buffer (SIZE is extended to page-boundary)
722     the_host_buffer = the_buffer;
723     the_buffer_size = page_extend((height + 2) * bytes_per_row);
724     the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
725     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
726 gbeauche 1.19 }
727 cebix 1.23 #else
728 gbeauche 1.19 use_vosf = false;
729     #endif
730 cebix 1.23 #endif
731 gbeauche 1.19
732 cebix 1.7 set_video_monitor(width, height, bytes_per_row, true);
733 gbeauche 1.19 #if REAL_ADDRESSING || DIRECT_ADDRESSING
734     VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
735 cebix 1.1 #else
736     VideoMonitor.mac_frame_base = MacFrameBaseMac;
737     #endif
738     return true;
739 cebix 1.7 #else
740     ErrorAlert("Basilisk II has been compiled with fbdev DGA support disabled.");
741     return false;
742     #endif
743 cebix 1.1 }
744    
745 cebix 1.7 // Init XF86 DGA display
746     static bool init_xf86_dga(int width, int height)
747 cebix 1.1 {
748 cebix 1.15 #ifdef ENABLE_XF86_DGA
749 cebix 1.1 // Set relative mouse mode
750     ADBSetRelMouseMode(true);
751    
752 cebix 1.15 #ifdef ENABLE_XF86_VIDMODE
753 cebix 1.12 // Switch to best mode
754     if (has_vidmode) {
755     int best = 0;
756     for (int i=1; i<num_x_video_modes; i++) {
757     if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
758     x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
759     best = i;
760     }
761     }
762     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
763     XF86VidModeSetViewPort(x_display, screen, 0, 0);
764 cebix 1.29 XSync(x_display, false);
765 cebix 1.12 }
766     #endif
767    
768 cebix 1.1 // Create window
769     XSetWindowAttributes wattr;
770     wattr.event_mask = eventmask = dga_eventmask;
771     wattr.override_redirect = True;
772    
773     the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
774 cebix 1.28 InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
775 cebix 1.29
776     // Set window name/class
777     set_window_name(the_win, STR_WINDOW_TITLE);
778    
779     // Indicate that we want keyboard input
780     set_window_focus(the_win);
781    
782     // Show window
783 cebix 1.1 XMapRaised(x_display, the_win);
784 cebix 1.29 wait_mapped(the_win);
785 cebix 1.1
786     // Establish direct screen connection
787     XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
788     XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
789     XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
790     XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
791    
792     int v_width, v_bank, v_size;
793     XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
794     XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
795     XF86DGASetViewPort(x_display, screen, 0, 0);
796     XF86DGASetVidPage(x_display, screen, 0);
797    
798     // Set colormap
799     if (depth == 8) {
800     XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
801     XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
802     }
803 cebix 1.29 XSync(x_display, false);
804 cebix 1.1
805     // Set VideoMonitor
806     int bytes_per_row = (v_width + 7) & ~7;
807     switch (depth) {
808     case 1:
809     bytes_per_row /= 8;
810     break;
811     case 15:
812     case 16:
813     bytes_per_row *= 2;
814     break;
815     case 24:
816     case 32:
817     bytes_per_row *= 4;
818     break;
819     }
820 gbeauche 1.39
821     #ifdef VIDEO_VOSF
822 gbeauche 1.19 #if REAL_ADDRESSING || DIRECT_ADDRESSING
823 gbeauche 1.36 // Screen_blitter_init() returns TRUE if VOSF is mandatory
824     // i.e. the framebuffer update function is not Blit_Copy_Raw
825     use_vosf = Screen_blitter_init(&visualInfo, true);
826 gbeauche 1.19
827     if (use_vosf) {
828 gbeauche 1.39 // Allocate memory for frame buffer (SIZE is extended to page-boundary)
829     the_host_buffer = the_buffer;
830     the_buffer_size = page_extend((height + 2) * bytes_per_row);
831     the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
832     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
833 gbeauche 1.19 }
834 gbeauche 1.39 #else
835 gbeauche 1.19 use_vosf = false;
836     #endif
837 gbeauche 1.39 #endif
838 gbeauche 1.19
839 cebix 1.1 set_video_monitor(width, height, bytes_per_row, true);
840 gbeauche 1.19 #if REAL_ADDRESSING || DIRECT_ADDRESSING
841     VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
842     // MacFrameLayout = FLAYOUT_DIRECT;
843 cebix 1.1 #else
844     VideoMonitor.mac_frame_base = MacFrameBaseMac;
845     #endif
846     return true;
847     #else
848 cebix 1.7 ErrorAlert("Basilisk II has been compiled with XF86 DGA support disabled.");
849 cebix 1.1 return false;
850     #endif
851     }
852    
853     // Init keycode translation table
854     static void keycode_init(void)
855     {
856     bool use_kc = PrefsFindBool("keycodes");
857     if (use_kc) {
858    
859     // Get keycode file path from preferences
860     const char *kc_path = PrefsFindString("keycodefile");
861    
862     // Open keycode table
863     FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
864     if (f == NULL) {
865     char str[256];
866     sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
867     WarningAlert(str);
868     return;
869     }
870    
871     // Default translation table
872     for (int i=0; i<256; i++)
873     keycode_table[i] = -1;
874    
875     // Search for server vendor string, then read keycodes
876     const char *vendor = ServerVendor(x_display);
877     bool vendor_found = false;
878     char line[256];
879     while (fgets(line, 255, f)) {
880     // Read line
881     int len = strlen(line);
882     if (len == 0)
883     continue;
884     line[len-1] = 0;
885    
886     // Comments begin with "#" or ";"
887     if (line[0] == '#' || line[0] == ';' || line[0] == 0)
888     continue;
889    
890     if (vendor_found) {
891     // Read keycode
892     int x_code, mac_code;
893     if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
894     keycode_table[x_code & 0xff] = mac_code;
895     else
896     break;
897     } else {
898     // Search for vendor string
899     if (strstr(vendor, line) == vendor)
900     vendor_found = true;
901     }
902     }
903    
904     // Keycode file completely read
905     fclose(f);
906     use_keycodes = vendor_found;
907    
908     // Vendor not found? Then display warning
909     if (!vendor_found) {
910     char str[256];
911     sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME);
912     WarningAlert(str);
913     return;
914     }
915     }
916     }
917    
918 gbeauche 1.19 bool VideoInitBuffer()
919     {
920     #ifdef ENABLE_VOSF
921     if (use_vosf) {
922     const uint32 page_size = getpagesize();
923     const uint32 page_mask = page_size - 1;
924    
925 gbeauche 1.39 mainBuffer.memBase = (uintptr) the_buffer;
926 gbeauche 1.19 // Align the frame buffer on page boundary
927 gbeauche 1.39 mainBuffer.memStart = (uintptr)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
928 gbeauche 1.19 mainBuffer.memLength = the_buffer_size;
929     mainBuffer.memEnd = mainBuffer.memStart + mainBuffer.memLength;
930    
931     mainBuffer.pageSize = page_size;
932     mainBuffer.pageCount = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
933 cebix 1.25 mainBuffer.pageBits = log_base_2(mainBuffer.pageSize);
934 gbeauche 1.19
935     if (mainBuffer.dirtyPages != 0)
936     free(mainBuffer.dirtyPages);
937    
938 gbeauche 1.35 mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
939 gbeauche 1.19
940     if (mainBuffer.pageInfo != 0)
941     free(mainBuffer.pageInfo);
942    
943     mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
944    
945     if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
946     return false;
947 gbeauche 1.36
948     mainBuffer.dirty = false;
949 gbeauche 1.19
950     PFLAG_CLEAR_ALL;
951 gbeauche 1.35 // Safety net to insure the loops in the update routines will terminate
952     // See a discussion in <video_vosf.h> for further details
953 gbeauche 1.34 PFLAG_CLEAR(mainBuffer.pageCount);
954 gbeauche 1.35 PFLAG_SET(mainBuffer.pageCount+1);
955 gbeauche 1.19
956     uint32 a = 0;
957     for (int i = 0; i < mainBuffer.pageCount; i++) {
958     int y1 = a / VideoMonitor.bytes_per_row;
959     if (y1 >= VideoMonitor.y)
960     y1 = VideoMonitor.y - 1;
961    
962     int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row;
963     if (y2 >= VideoMonitor.y)
964     y2 = VideoMonitor.y - 1;
965    
966     mainBuffer.pageInfo[i].top = y1;
967     mainBuffer.pageInfo[i].bottom = y2;
968    
969     a += mainBuffer.pageSize;
970     if (a > mainBuffer.memLength)
971     a = mainBuffer.memLength;
972     }
973    
974     // We can now write-protect the frame buffer
975 gbeauche 1.39 if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
976 gbeauche 1.19 return false;
977     }
978     #endif
979     return true;
980     }
981    
982 cebix 1.1 bool VideoInit(bool classic)
983     {
984 gbeauche 1.19 #ifdef ENABLE_VOSF
985     // Open /dev/zero
986     zero_fd = open("/dev/zero", O_RDWR);
987     if (zero_fd < 0) {
988     char str[256];
989     sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
990     ErrorAlert(str);
991     return false;
992     }
993    
994     // Zero the mainBuffer structure
995     mainBuffer.dirtyPages = 0;
996     mainBuffer.pageInfo = 0;
997     #endif
998    
999 cebix 1.16 // Check if X server runs on local machine
1000     local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
1001     || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
1002    
1003 cebix 1.1 // Init keycode translation
1004     keycode_init();
1005    
1006 cebix 1.10 // Read prefs
1007 cebix 1.32 mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
1008     mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1009 cebix 1.10
1010 cebix 1.1 // Find screen and root window
1011     screen = XDefaultScreen(x_display);
1012     rootwin = XRootWindow(x_display, screen);
1013 cebix 1.7
1014 cebix 1.1 // Get screen depth
1015     xdepth = DefaultDepth(x_display, screen);
1016 cebix 1.7
1017 cebix 1.15 #ifdef ENABLE_FBDEV_DGA
1018 cebix 1.7 // Frame buffer name
1019     char fb_name[20];
1020    
1021     // Could do fbdev dga ?
1022     if ((fbdev_fd = open(FBDEVICE_FILE_NAME, O_RDWR)) != -1)
1023     has_dga = true;
1024     else
1025     has_dga = false;
1026     #endif
1027 cebix 1.12
1028 cebix 1.15 #ifdef ENABLE_XF86_DGA
1029 cebix 1.1 // DGA available?
1030 cebix 1.12 int dga_event_base, dga_error_base;
1031 cebix 1.16 if (local_X11 && XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
1032 cebix 1.5 int dga_flags = 0;
1033     XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1034     has_dga = dga_flags & XF86DGADirectPresent;
1035     } else
1036     has_dga = false;
1037 cebix 1.1 #endif
1038    
1039 cebix 1.15 #ifdef ENABLE_XF86_VIDMODE
1040 cebix 1.12 // VidMode available?
1041     int vm_event_base, vm_error_base;
1042     has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
1043     if (has_vidmode)
1044     XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1045     #endif
1046    
1047 cebix 1.1 // Find black and white colors
1048     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
1049     XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
1050     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white);
1051     XAllocColor(x_display, DefaultColormap(x_display, screen), &white);
1052     black_pixel = BlackPixel(x_display, screen);
1053     white_pixel = WhitePixel(x_display, screen);
1054    
1055     // Get appropriate visual
1056     int color_class;
1057     switch (xdepth) {
1058     case 1:
1059     color_class = StaticGray;
1060     break;
1061     case 8:
1062     color_class = PseudoColor;
1063     break;
1064     case 15:
1065     case 16:
1066     case 24:
1067     case 32:
1068     color_class = TrueColor;
1069     break;
1070     default:
1071     ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
1072     return false;
1073     }
1074     if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
1075     ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
1076     return false;
1077     }
1078     if (visualInfo.depth != xdepth) {
1079     ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
1080     return false;
1081     }
1082     vis = visualInfo.visual;
1083    
1084     // Mac screen depth is always 1 bit in Classic mode, but follows X depth otherwise
1085     classic_mode = classic;
1086     if (classic)
1087     depth = 1;
1088     else
1089     depth = xdepth;
1090    
1091     // Create color maps for 8 bit mode
1092     if (depth == 8) {
1093     cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1094     cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1095     XInstallColormap(x_display, cmap[0]);
1096     XInstallColormap(x_display, cmap[1]);
1097     }
1098    
1099     // Get screen mode from preferences
1100     const char *mode_str;
1101     if (classic)
1102     mode_str = "win/512/342";
1103     else
1104     mode_str = PrefsFindString("screen");
1105    
1106     // Determine type and mode
1107     int width = 512, height = 384;
1108     display_type = DISPLAY_WINDOW;
1109     if (mode_str) {
1110     if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
1111     display_type = DISPLAY_WINDOW;
1112 cebix 1.15 #ifdef ENABLE_FBDEV_DGA
1113 cebix 1.8 else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
1114 cebix 1.7 #else
1115 cebix 1.3 else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) {
1116 cebix 1.7 #endif
1117 cebix 1.1 display_type = DISPLAY_DGA;
1118 cebix 1.3 if (width > DisplayWidth(x_display, screen))
1119     width = DisplayWidth(x_display, screen);
1120     if (height > DisplayHeight(x_display, screen))
1121     height = DisplayHeight(x_display, screen);
1122     }
1123     if (width <= 0)
1124 cebix 1.1 width = DisplayWidth(x_display, screen);
1125 cebix 1.3 if (height <= 0)
1126 cebix 1.1 height = DisplayHeight(x_display, screen);
1127     }
1128    
1129     // Initialize according to display type
1130     switch (display_type) {
1131     case DISPLAY_WINDOW:
1132     if (!init_window(width, height))
1133     return false;
1134     break;
1135     case DISPLAY_DGA:
1136 cebix 1.15 #ifdef ENABLE_FBDEV_DGA
1137 cebix 1.7 if (!init_fbdev_dga(fb_name))
1138     #else
1139     if (!init_xf86_dga(width, height))
1140     #endif
1141 cebix 1.1 return false;
1142     break;
1143     }
1144    
1145     // Lock down frame buffer
1146 cebix 1.29 LOCK_FRAME_BUFFER;
1147 cebix 1.1
1148 gbeauche 1.19 #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1149 cebix 1.1 // Set variables for UAE memory mapping
1150     MacFrameBaseHost = the_buffer;
1151     MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
1152    
1153     // No special frame buffer in Classic mode (frame buffer is in Mac RAM)
1154     if (classic)
1155     MacFrameLayout = FLAYOUT_NONE;
1156     #endif
1157    
1158 gbeauche 1.19 #ifdef ENABLE_VOSF
1159     if (use_vosf) {
1160     // Initialize the mainBuffer structure
1161     if (!VideoInitBuffer()) {
1162     // TODO: STR_VOSF_INIT_ERR ?
1163     ErrorAlert("Could not initialize Video on SEGV signals");
1164     return false;
1165     }
1166    
1167     // Initialize the handler for SIGSEGV
1168 gbeauche 1.38 if (!sigsegv_install_handler(screen_fault_handler)) {
1169 gbeauche 1.19 // TODO: STR_VOSF_INIT_ERR ?
1170     ErrorAlert("Could not initialize Video on SEGV signals");
1171     return false;
1172     }
1173     }
1174     #endif
1175    
1176     // Initialize VideoRefresh function
1177     VideoRefreshInit();
1178    
1179 cebix 1.15 XSync(x_display, false);
1180    
1181     #ifdef HAVE_PTHREADS
1182 cebix 1.1 // Start redraw/input thread
1183     redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1184 cebix 1.15 if (!redraw_thread_active) {
1185 cebix 1.1 printf("FATAL: cannot create redraw thread\n");
1186 cebix 1.15 return false;
1187     }
1188     #endif
1189     return true;
1190 cebix 1.1 }
1191    
1192    
1193     /*
1194     * Deinitialization
1195     */
1196    
1197     void VideoExit(void)
1198     {
1199 cebix 1.15 #ifdef HAVE_PTHREADS
1200 cebix 1.1 // Stop redraw thread
1201     if (redraw_thread_active) {
1202     redraw_thread_cancel = true;
1203     #ifdef HAVE_PTHREAD_CANCEL
1204     pthread_cancel(redraw_thread);
1205     #endif
1206     pthread_join(redraw_thread, NULL);
1207     redraw_thread_active = false;
1208     }
1209 cebix 1.15 #endif
1210 cebix 1.1
1211     // Unlock frame buffer
1212 cebix 1.29 UNLOCK_FRAME_BUFFER;
1213 cebix 1.1
1214     // Close window and server connection
1215     if (x_display != NULL) {
1216     XSync(x_display, false);
1217    
1218 cebix 1.15 #ifdef ENABLE_XF86_DGA
1219 cebix 1.1 if (display_type == DISPLAY_DGA) {
1220     XF86DGADirectVideo(x_display, screen, 0);
1221     XUngrabPointer(x_display, CurrentTime);
1222     XUngrabKeyboard(x_display, CurrentTime);
1223     }
1224 cebix 1.12 #endif
1225    
1226 cebix 1.15 #ifdef ENABLE_XF86_VIDMODE
1227 cebix 1.12 if (has_vidmode && display_type == DISPLAY_DGA)
1228     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
1229 cebix 1.1 #endif
1230    
1231 cebix 1.15 #ifdef ENABLE_FBDEV_DGA
1232 cebix 1.7 if (display_type == DISPLAY_DGA) {
1233     XUngrabPointer(x_display, CurrentTime);
1234     XUngrabKeyboard(x_display, CurrentTime);
1235     close(fbdev_fd);
1236     }
1237     #endif
1238 cebix 1.1
1239     XFlush(x_display);
1240     XSync(x_display, false);
1241     if (depth == 8) {
1242     XFreeColormap(x_display, cmap[0]);
1243     XFreeColormap(x_display, cmap[1]);
1244     }
1245 gbeauche 1.19
1246     if (!use_vosf) {
1247     if (the_buffer) {
1248     free(the_buffer);
1249     the_buffer = NULL;
1250     }
1251 cebix 1.13
1252 gbeauche 1.19 if (!have_shm && the_buffer_copy) {
1253     free(the_buffer_copy);
1254     the_buffer_copy = NULL;
1255     }
1256     }
1257     #ifdef ENABLE_VOSF
1258     else {
1259 gbeauche 1.39 if (the_buffer != (uint8 *)VM_MAP_FAILED) {
1260     vm_release(the_buffer, the_buffer_size);
1261 gbeauche 1.19 the_buffer = 0;
1262     }
1263    
1264 gbeauche 1.39 if (the_buffer_copy != (uint8 *)VM_MAP_FAILED) {
1265     vm_release(the_buffer_copy, the_buffer_size);
1266 gbeauche 1.19 the_buffer_copy = 0;
1267     }
1268     }
1269     #endif
1270     }
1271    
1272     #ifdef ENABLE_VOSF
1273     if (use_vosf) {
1274     // Clear mainBuffer data
1275     if (mainBuffer.pageInfo) {
1276     free(mainBuffer.pageInfo);
1277     mainBuffer.pageInfo = 0;
1278 cebix 1.13 }
1279    
1280 gbeauche 1.19 if (mainBuffer.dirtyPages) {
1281     free(mainBuffer.dirtyPages);
1282     mainBuffer.dirtyPages = 0;
1283 cebix 1.13 }
1284 cebix 1.1 }
1285 gbeauche 1.19
1286     // Close /dev/zero
1287     if (zero_fd > 0)
1288     close(zero_fd);
1289     #endif
1290 cebix 1.1 }
1291    
1292    
1293     /*
1294     * Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode)
1295     */
1296    
1297     void VideoQuitFullScreen(void)
1298     {
1299     D(bug("VideoQuitFullScreen()\n"));
1300     if (display_type == DISPLAY_DGA)
1301     quit_full_screen = true;
1302     }
1303    
1304    
1305     /*
1306     * Mac VBL interrupt
1307     */
1308    
1309     void VideoInterrupt(void)
1310     {
1311     // Emergency quit requested? Then quit
1312     if (emerg_quit)
1313     QuitEmulator();
1314    
1315     // Temporarily give up frame buffer lock (this is the point where
1316     // we are suspended when the user presses Ctrl-Tab)
1317 cebix 1.29 UNLOCK_FRAME_BUFFER;
1318     LOCK_FRAME_BUFFER;
1319 cebix 1.1 }
1320    
1321    
1322     /*
1323     * Set palette
1324     */
1325    
1326     void video_set_palette(uint8 *pal)
1327     {
1328 cebix 1.29 LOCK_PALETTE;
1329 cebix 1.1
1330     // Convert colors to XColor array
1331     for (int i=0; i<256; i++) {
1332     palette[i].pixel = i;
1333     palette[i].red = pal[i*3] * 0x0101;
1334     palette[i].green = pal[i*3+1] * 0x0101;
1335     palette[i].blue = pal[i*3+2] * 0x0101;
1336     palette[i].flags = DoRed | DoGreen | DoBlue;
1337     }
1338    
1339     // Tell redraw thread to change palette
1340     palette_changed = true;
1341    
1342 cebix 1.29 UNLOCK_PALETTE;
1343 cebix 1.1 }
1344    
1345    
1346     /*
1347     * Suspend/resume emulator
1348     */
1349    
1350 cebix 1.15 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1351 cebix 1.1 static void suspend_emul(void)
1352     {
1353     if (display_type == DISPLAY_DGA) {
1354     // Release ctrl key
1355     ADBKeyUp(0x36);
1356     ctrl_down = false;
1357    
1358     // Lock frame buffer (this will stop the MacOS thread)
1359 cebix 1.29 LOCK_FRAME_BUFFER;
1360 cebix 1.1
1361     // Save frame buffer
1362     fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
1363     if (fb_save)
1364     memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row);
1365    
1366     // Close full screen display
1367 cebix 1.15 #ifdef ENABLE_XF86_DGA
1368 cebix 1.1 XF86DGADirectVideo(x_display, screen, 0);
1369 cebix 1.7 #endif
1370 cebix 1.1 XUngrabPointer(x_display, CurrentTime);
1371     XUngrabKeyboard(x_display, CurrentTime);
1372     XUnmapWindow(x_display, the_win);
1373 cebix 1.29 wait_unmapped(the_win);
1374 cebix 1.1
1375     // Open "suspend" window
1376     XSetWindowAttributes wattr;
1377     wattr.event_mask = KeyPressMask;
1378     wattr.background_pixel = black_pixel;
1379 cebix 1.7
1380 cebix 1.1 suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1381 cebix 1.29 InputOutput, vis, CWEventMask | CWBackPixel, &wattr);
1382     set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
1383     set_window_focus(suspend_win);
1384     XMapWindow(x_display, suspend_win);
1385 cebix 1.1 emul_suspended = true;
1386     }
1387     }
1388    
1389     static void resume_emul(void)
1390     {
1391     // Close "suspend" window
1392     XDestroyWindow(x_display, suspend_win);
1393     XSync(x_display, false);
1394    
1395     // Reopen full screen display
1396     XMapRaised(x_display, the_win);
1397 cebix 1.29 wait_mapped(the_win);
1398 cebix 1.1 XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1399     XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1400     XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1401 cebix 1.15 #ifdef ENABLE_XF86_DGA
1402 cebix 1.1 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1403     XF86DGASetViewPort(x_display, screen, 0, 0);
1404 cebix 1.7 #endif
1405 cebix 1.1 XSync(x_display, false);
1406 gbeauche 1.21
1407     // the_buffer already contains the data to restore. i.e. since a temporary
1408     // frame buffer is used when VOSF is actually used, fb_save is therefore
1409     // not necessary.
1410     #ifdef ENABLE_VOSF
1411     if (use_vosf) {
1412 cebix 1.29 LOCK_VOSF;
1413 gbeauche 1.21 PFLAG_SET_ALL;
1414 cebix 1.29 UNLOCK_VOSF;
1415 gbeauche 1.21 memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1416     }
1417 gbeauche 1.19 #endif
1418 gbeauche 1.21
1419     // Restore frame buffer
1420     if (fb_save) {
1421     #ifdef ENABLE_VOSF
1422     // Don't copy fb_save to the temporary frame buffer in VOSF mode
1423     if (!use_vosf)
1424 gbeauche 1.19 #endif
1425 gbeauche 1.21 memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
1426 cebix 1.1 free(fb_save);
1427     fb_save = NULL;
1428     }
1429 cebix 1.7
1430 cebix 1.15 #ifdef ENABLE_XF86_DGA
1431 cebix 1.1 if (depth == 8)
1432     XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1433 cebix 1.7 #endif
1434 cebix 1.1
1435     // Unlock frame buffer (and continue MacOS thread)
1436 cebix 1.29 UNLOCK_FRAME_BUFFER;
1437 cebix 1.1 emul_suspended = false;
1438     }
1439     #endif
1440    
1441    
1442     /*
1443     * Translate key event to Mac keycode
1444     */
1445    
1446     static int kc_decode(KeySym ks)
1447     {
1448     switch (ks) {
1449     case XK_A: case XK_a: return 0x00;
1450     case XK_B: case XK_b: return 0x0b;
1451     case XK_C: case XK_c: return 0x08;
1452     case XK_D: case XK_d: return 0x02;
1453     case XK_E: case XK_e: return 0x0e;
1454     case XK_F: case XK_f: return 0x03;
1455     case XK_G: case XK_g: return 0x05;
1456     case XK_H: case XK_h: return 0x04;
1457     case XK_I: case XK_i: return 0x22;
1458     case XK_J: case XK_j: return 0x26;
1459     case XK_K: case XK_k: return 0x28;
1460     case XK_L: case XK_l: return 0x25;
1461     case XK_M: case XK_m: return 0x2e;
1462     case XK_N: case XK_n: return 0x2d;
1463     case XK_O: case XK_o: return 0x1f;
1464     case XK_P: case XK_p: return 0x23;
1465     case XK_Q: case XK_q: return 0x0c;
1466     case XK_R: case XK_r: return 0x0f;
1467     case XK_S: case XK_s: return 0x01;
1468     case XK_T: case XK_t: return 0x11;
1469     case XK_U: case XK_u: return 0x20;
1470     case XK_V: case XK_v: return 0x09;
1471     case XK_W: case XK_w: return 0x0d;
1472     case XK_X: case XK_x: return 0x07;
1473     case XK_Y: case XK_y: return 0x10;
1474     case XK_Z: case XK_z: return 0x06;
1475    
1476     case XK_1: case XK_exclam: return 0x12;
1477     case XK_2: case XK_at: return 0x13;
1478     case XK_3: case XK_numbersign: return 0x14;
1479     case XK_4: case XK_dollar: return 0x15;
1480     case XK_5: case XK_percent: return 0x17;
1481     case XK_6: return 0x16;
1482     case XK_7: return 0x1a;
1483     case XK_8: return 0x1c;
1484     case XK_9: return 0x19;
1485     case XK_0: return 0x1d;
1486    
1487     case XK_grave: case XK_asciitilde: return 0x0a;
1488     case XK_minus: case XK_underscore: return 0x1b;
1489     case XK_equal: case XK_plus: return 0x18;
1490     case XK_bracketleft: case XK_braceleft: return 0x21;
1491     case XK_bracketright: case XK_braceright: return 0x1e;
1492     case XK_backslash: case XK_bar: return 0x2a;
1493     case XK_semicolon: case XK_colon: return 0x29;
1494     case XK_apostrophe: case XK_quotedbl: return 0x27;
1495     case XK_comma: case XK_less: return 0x2b;
1496     case XK_period: case XK_greater: return 0x2f;
1497     case XK_slash: case XK_question: return 0x2c;
1498    
1499 cebix 1.15 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1500 cebix 1.1 case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1501     #else
1502     case XK_Tab: return 0x30;
1503     #endif
1504     case XK_Return: return 0x24;
1505     case XK_space: return 0x31;
1506     case XK_BackSpace: return 0x33;
1507    
1508     case XK_Delete: return 0x75;
1509     case XK_Insert: return 0x72;
1510     case XK_Home: case XK_Help: return 0x73;
1511     case XK_End: return 0x77;
1512     #ifdef __hpux
1513     case XK_Prior: return 0x74;
1514     case XK_Next: return 0x79;
1515     #else
1516     case XK_Page_Up: return 0x74;
1517     case XK_Page_Down: return 0x79;
1518     #endif
1519    
1520     case XK_Control_L: return 0x36;
1521     case XK_Control_R: return 0x36;
1522     case XK_Shift_L: return 0x38;
1523     case XK_Shift_R: return 0x38;
1524     case XK_Alt_L: return 0x37;
1525     case XK_Alt_R: return 0x37;
1526     case XK_Meta_L: return 0x3a;
1527     case XK_Meta_R: return 0x3a;
1528     case XK_Menu: return 0x32;
1529     case XK_Caps_Lock: return 0x39;
1530     case XK_Num_Lock: return 0x47;
1531    
1532     case XK_Up: return 0x3e;
1533     case XK_Down: return 0x3d;
1534     case XK_Left: return 0x3b;
1535     case XK_Right: return 0x3c;
1536    
1537     case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
1538    
1539     case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
1540     case XK_F2: return 0x78;
1541     case XK_F3: return 0x63;
1542     case XK_F4: return 0x76;
1543     case XK_F5: return 0x60;
1544     case XK_F6: return 0x61;
1545     case XK_F7: return 0x62;
1546     case XK_F8: return 0x64;
1547     case XK_F9: return 0x65;
1548     case XK_F10: return 0x6d;
1549     case XK_F11: return 0x67;
1550     case XK_F12: return 0x6f;
1551    
1552     case XK_Print: return 0x69;
1553     case XK_Scroll_Lock: return 0x6b;
1554     case XK_Pause: return 0x71;
1555    
1556     #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
1557     case XK_KP_0: case XK_KP_Insert: return 0x52;
1558     case XK_KP_1: case XK_KP_End: return 0x53;
1559     case XK_KP_2: case XK_KP_Down: return 0x54;
1560     case XK_KP_3: case XK_KP_Next: return 0x55;
1561     case XK_KP_4: case XK_KP_Left: return 0x56;
1562     case XK_KP_5: case XK_KP_Begin: return 0x57;
1563     case XK_KP_6: case XK_KP_Right: return 0x58;
1564     case XK_KP_7: case XK_KP_Home: return 0x59;
1565     case XK_KP_8: case XK_KP_Up: return 0x5b;
1566     case XK_KP_9: case XK_KP_Prior: return 0x5c;
1567     case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
1568     #else
1569     case XK_KP_0: return 0x52;
1570     case XK_KP_1: return 0x53;
1571     case XK_KP_2: return 0x54;
1572     case XK_KP_3: return 0x55;
1573     case XK_KP_4: return 0x56;
1574     case XK_KP_5: return 0x57;
1575     case XK_KP_6: return 0x58;
1576     case XK_KP_7: return 0x59;
1577     case XK_KP_8: return 0x5b;
1578     case XK_KP_9: return 0x5c;
1579     case XK_KP_Decimal: return 0x41;
1580     #endif
1581     case XK_KP_Add: return 0x45;
1582     case XK_KP_Subtract: return 0x4e;
1583     case XK_KP_Multiply: return 0x43;
1584     case XK_KP_Divide: return 0x4b;
1585     case XK_KP_Enter: return 0x4c;
1586     case XK_KP_Equal: return 0x51;
1587     }
1588     return -1;
1589     }
1590    
1591 cebix 1.29 static int event2keycode(XKeyEvent &ev)
1592 cebix 1.1 {
1593     KeySym ks;
1594     int as;
1595     int i = 0;
1596    
1597     do {
1598 cebix 1.29 ks = XLookupKeysym(&ev, i++);
1599 cebix 1.1 as = kc_decode(ks);
1600     if (as != -1)
1601     return as;
1602     } while (ks != NoSymbol);
1603    
1604     return -1;
1605     }
1606    
1607    
1608     /*
1609     * X event handling
1610     */
1611    
1612     static void handle_events(void)
1613     {
1614 cebix 1.29 while (XPending(x_display)) {
1615     XEvent event;
1616     XNextEvent(x_display, &event);
1617 cebix 1.1
1618     switch (event.type) {
1619     // Mouse button
1620     case ButtonPress: {
1621 cebix 1.29 unsigned int button = event.xbutton.button;
1622 cebix 1.1 if (button < 4)
1623     ADBMouseDown(button - 1);
1624 cebix 1.10 else if (button < 6) { // Wheel mouse
1625     if (mouse_wheel_mode == 0) {
1626     int key = (button == 5) ? 0x79 : 0x74; // Page up/down
1627     ADBKeyDown(key);
1628     ADBKeyUp(key);
1629     } else {
1630     int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down
1631     for(int i=0; i<mouse_wheel_lines; i++) {
1632     ADBKeyDown(key);
1633     ADBKeyUp(key);
1634     }
1635     }
1636     }
1637 cebix 1.1 break;
1638     }
1639     case ButtonRelease: {
1640 cebix 1.29 unsigned int button = event.xbutton.button;
1641 cebix 1.1 if (button < 4)
1642     ADBMouseUp(button - 1);
1643     break;
1644     }
1645    
1646     // Mouse moved
1647     case EnterNotify:
1648     case MotionNotify:
1649 cebix 1.29 ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1650 cebix 1.1 break;
1651    
1652     // Keyboard
1653     case KeyPress: {
1654     int code;
1655     if (use_keycodes) {
1656 cebix 1.29 event2keycode(event.xkey); // This is called to process the hotkeys
1657     code = keycode_table[event.xkey.keycode & 0xff];
1658 cebix 1.1 } else
1659 cebix 1.29 code = event2keycode(event.xkey);
1660 cebix 1.1 if (code != -1) {
1661     if (!emul_suspended) {
1662 cebix 1.3 if (code == 0x39) { // Caps Lock pressed
1663     if (caps_on) {
1664     ADBKeyUp(code);
1665     caps_on = false;
1666     } else {
1667     ADBKeyDown(code);
1668     caps_on = true;
1669     }
1670     } else
1671     ADBKeyDown(code);
1672 cebix 1.1 if (code == 0x36)
1673     ctrl_down = true;
1674     } else {
1675 cebix 1.15 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1676 cebix 1.1 if (code == 0x31)
1677     resume_emul(); // Space wakes us up
1678     #endif
1679     }
1680     }
1681     break;
1682     }
1683     case KeyRelease: {
1684     int code;
1685     if (use_keycodes) {
1686 cebix 1.29 event2keycode(event.xkey); // This is called to process the hotkeys
1687     code = keycode_table[event.xkey.keycode & 0xff];
1688 cebix 1.1 } else
1689 cebix 1.29 code = event2keycode(event.xkey);
1690 cebix 1.3 if (code != -1 && code != 0x39) { // Don't propagate Caps Lock releases
1691 cebix 1.1 ADBKeyUp(code);
1692     if (code == 0x36)
1693     ctrl_down = false;
1694     }
1695     break;
1696     }
1697    
1698     // Hidden parts exposed, force complete refresh of window
1699     case Expose:
1700 cebix 1.13 if (display_type == DISPLAY_WINDOW) {
1701 gbeauche 1.19 #ifdef ENABLE_VOSF
1702 gbeauche 1.21 if (use_vosf) { // VOSF refresh
1703 cebix 1.29 LOCK_VOSF;
1704 gbeauche 1.20 PFLAG_SET_ALL;
1705 cebix 1.29 UNLOCK_VOSF;
1706 gbeauche 1.21 memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1707     }
1708     else
1709 gbeauche 1.20 #endif
1710 gbeauche 1.21 if (frame_skip == 0) { // Dynamic refresh
1711     int x1, y1;
1712     for (y1=0; y1<16; y1++)
1713     for (x1=0; x1<16; x1++)
1714     updt_box[x1][y1] = true;
1715     nr_boxes = 16 * 16;
1716     } else // Static refresh
1717 gbeauche 1.20 memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1718 cebix 1.13 }
1719 cebix 1.26 break;
1720    
1721 cebix 1.29 // Window "close" widget clicked
1722     case ClientMessage:
1723 cebix 1.30 if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
1724     ADBKeyDown(0x7f); // Power key
1725     ADBKeyUp(0x7f);
1726     }
1727 cebix 1.1 break;
1728     }
1729     }
1730     }
1731    
1732    
1733     /*
1734     * Window display update
1735     */
1736    
1737 cebix 1.16 // Dynamic display update (variable frame rate for each box)
1738     static void update_display_dynamic(int ticker)
1739 cebix 1.1 {
1740 cebix 1.17 int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
1741 cebix 1.13 int xil = 0;
1742     int rxm = 0, rxmo = 0;
1743 cebix 1.1 int bytes_per_row = VideoMonitor.bytes_per_row;
1744     int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1745 cebix 1.13 int rx = VideoMonitor.bytes_per_row / 16;
1746     int ry = VideoMonitor.y / 16;
1747     int max_box;
1748    
1749     y2s = sm_uptd[ticker % 8];
1750     y2a = 8;
1751     for (i = 0; i < 6; i++)
1752     if (ticker % (2 << i))
1753 cebix 1.1 break;
1754 cebix 1.13 max_box = sm_no_boxes[i];
1755 cebix 1.1
1756 cebix 1.13 if (y2a) {
1757     for (y1=0; y1<16; y1++) {
1758     for (y2=y2s; y2 < ry; y2 += y2a) {
1759     i = ((y1 * ry) + y2) * bytes_per_row;
1760     for (x1=0; x1<16; x1++, i += rx) {
1761     if (updt_box[x1][y1] == false) {
1762     if (memcmp(&the_buffer_copy[i], &the_buffer[i], rx)) {
1763     updt_box[x1][y1] = true;
1764     nr_boxes++;
1765     }
1766 cebix 1.1 }
1767     }
1768     }
1769 cebix 1.13 }
1770     }
1771 cebix 1.1
1772 cebix 1.13 if ((nr_boxes <= max_box) && (nr_boxes)) {
1773     for (y1=0; y1<16; y1++) {
1774     for (x1=0; x1<16; x1++) {
1775     if (updt_box[x1][y1] == true) {
1776     if (rxm == 0)
1777     xm = x1;
1778     rxm += rx;
1779     updt_box[x1][y1] = false;
1780 cebix 1.1 }
1781 cebix 1.13 if (((updt_box[x1+1][y1] == false) || (x1 == 15)) && (rxm)) {
1782     if ((rxmo != rxm) || (xmo != xm) || (yo != y1 - 1)) {
1783     if (rxmo) {
1784     xi = xmo * rx;
1785     yi = ymo * ry;
1786     xil = rxmo;
1787     yil = (yo - ymo +1) * ry;
1788     }
1789     rxmo = rxm;
1790     xmo = xm;
1791     ymo = y1;
1792 cebix 1.1 }
1793 cebix 1.13 rxm = 0;
1794     yo = y1;
1795     }
1796     if (xil) {
1797     i = (yi * bytes_per_row) + xi;
1798     for (y2=0; y2 < yil; y2++, i += bytes_per_row)
1799     memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
1800     if (depth == 1) {
1801     if (have_shm)
1802     XShmPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
1803     else
1804     XPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil);
1805     } else {
1806     if (have_shm)
1807     XShmPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0);
1808     else
1809     XPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil);
1810 cebix 1.1 }
1811 cebix 1.13 xil = 0;
1812 cebix 1.1 }
1813 cebix 1.13 if ((x1 == 15) && (y1 == 15) && (rxmo)) {
1814     x1--;
1815     xi = xmo * rx;
1816     yi = ymo * ry;
1817     xil = rxmo;
1818     yil = (yo - ymo +1) * ry;
1819     rxmo = 0;
1820 cebix 1.1 }
1821     }
1822     }
1823 cebix 1.13 nr_boxes = 0;
1824 cebix 1.1 }
1825     }
1826    
1827 cebix 1.16 // Static display update (fixed frame rate, but incremental)
1828     static void update_display_static(void)
1829     {
1830     // Incremental update code
1831     int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1832     int bytes_per_row = VideoMonitor.bytes_per_row;
1833     int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1834     uint8 *p, *p2;
1835    
1836     // Check for first line from top and first line from bottom that have changed
1837     y1 = 0;
1838     for (j=0; j<VideoMonitor.y; j++) {
1839     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1840     y1 = j;
1841     break;
1842     }
1843     }
1844     y2 = y1 - 1;
1845     for (j=VideoMonitor.y-1; j>=y1; j--) {
1846     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1847     y2 = j;
1848     break;
1849     }
1850     }
1851     high = y2 - y1 + 1;
1852    
1853     // Check for first column from left and first column from right that have changed
1854     if (high) {
1855     if (depth == 1) {
1856 cebix 1.24 x1 = VideoMonitor.x - 1;
1857 cebix 1.16 for (j=y1; j<=y2; j++) {
1858     p = &the_buffer[j * bytes_per_row];
1859     p2 = &the_buffer_copy[j * bytes_per_row];
1860     for (i=0; i<(x1>>3); i++) {
1861     if (*p != *p2) {
1862     x1 = i << 3;
1863     break;
1864     }
1865 cebix 1.18 p++; p2++;
1866 cebix 1.16 }
1867     }
1868     x2 = x1;
1869     for (j=y1; j<=y2; j++) {
1870     p = &the_buffer[j * bytes_per_row];
1871     p2 = &the_buffer_copy[j * bytes_per_row];
1872     p += bytes_per_row;
1873     p2 += bytes_per_row;
1874     for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1875 cebix 1.18 p--; p2--;
1876 cebix 1.16 if (*p != *p2) {
1877 cebix 1.25 x2 = (i << 3) + 7;
1878 cebix 1.16 break;
1879     }
1880     }
1881     }
1882 cebix 1.25 wide = x2 - x1 + 1;
1883 cebix 1.16
1884     // Update copy of the_buffer
1885     if (high && wide) {
1886     for (j=y1; j<=y2; j++) {
1887     i = j * bytes_per_row + (x1 >> 3);
1888     memcpy(the_buffer_copy + i, the_buffer + i, wide >> 3);
1889     }
1890     }
1891    
1892     } else {
1893     x1 = VideoMonitor.x;
1894     for (j=y1; j<=y2; j++) {
1895     p = &the_buffer[j * bytes_per_row];
1896     p2 = &the_buffer_copy[j * bytes_per_row];
1897 cebix 1.18 for (i=0; i<x1*bytes_per_pixel; i++) {
1898     if (*p != *p2) {
1899     x1 = i / bytes_per_pixel;
1900 cebix 1.16 break;
1901     }
1902 cebix 1.18 p++; p2++;
1903 cebix 1.16 }
1904     }
1905     x2 = x1;
1906     for (j=y1; j<=y2; j++) {
1907     p = &the_buffer[j * bytes_per_row];
1908     p2 = &the_buffer_copy[j * bytes_per_row];
1909     p += bytes_per_row;
1910     p2 += bytes_per_row;
1911 cebix 1.18 for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1912     p--;
1913     p2--;
1914     if (*p != *p2) {
1915     x2 = i / bytes_per_pixel;
1916 cebix 1.16 break;
1917     }
1918     }
1919     }
1920     wide = x2 - x1;
1921    
1922     // Update copy of the_buffer
1923     if (high && wide) {
1924     for (j=y1; j<=y2; j++) {
1925     i = j * bytes_per_row + x1 * bytes_per_pixel;
1926     memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1927     }
1928     }
1929     }
1930     }
1931    
1932     // Refresh display
1933     if (high && wide) {
1934     if (have_shm)
1935     XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
1936     else
1937     XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1938     }
1939     }
1940    
1941 cebix 1.1
1942     /*
1943 gbeauche 1.19 * Screen refresh functions
1944     */
1945    
1946 gbeauche 1.34 // We suggest the compiler to inline the next two functions so that it
1947     // may specialise the code according to the current screen depth and
1948 gbeauche 1.35 // display type. A clever compiler would do that job by itself though...
1949 gbeauche 1.34
1950     // NOTE: update_display_vosf is inlined too
1951 gbeauche 1.19
1952     static inline void possibly_quit_dga_mode()
1953     {
1954     #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1955     // Quit DGA mode if requested
1956     if (quit_full_screen) {
1957     quit_full_screen = false;
1958     #ifdef ENABLE_XF86_DGA
1959     XF86DGADirectVideo(x_display, screen, 0);
1960     #endif
1961     XUngrabPointer(x_display, CurrentTime);
1962     XUngrabKeyboard(x_display, CurrentTime);
1963     XUnmapWindow(x_display, the_win);
1964     XSync(x_display, false);
1965     }
1966     #endif
1967     }
1968    
1969     static inline void handle_palette_changes(int depth, int display_type)
1970     {
1971 cebix 1.29 LOCK_PALETTE;
1972    
1973 gbeauche 1.19 if (palette_changed) {
1974     palette_changed = false;
1975     if (depth == 8) {
1976     XStoreColors(x_display, cmap[0], palette, 256);
1977     XStoreColors(x_display, cmap[1], palette, 256);
1978 cebix 1.29 XSync(x_display, false);
1979 gbeauche 1.19
1980     #ifdef ENABLE_XF86_DGA
1981     if (display_type == DISPLAY_DGA) {
1982     current_dga_cmap ^= 1;
1983     XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1984     }
1985     #endif
1986     }
1987     }
1988 cebix 1.29
1989     UNLOCK_PALETTE;
1990 gbeauche 1.19 }
1991    
1992     static void video_refresh_dga(void)
1993     {
1994     // Quit DGA mode if requested
1995     possibly_quit_dga_mode();
1996    
1997     // Handle X events
1998     handle_events();
1999    
2000     // Handle palette changes
2001     handle_palette_changes(depth, DISPLAY_DGA);
2002     }
2003    
2004 cebix 1.23 #ifdef ENABLE_VOSF
2005 gbeauche 1.19 #if REAL_ADDRESSING || DIRECT_ADDRESSING
2006     static void video_refresh_dga_vosf(void)
2007     {
2008     // Quit DGA mode if requested
2009     possibly_quit_dga_mode();
2010    
2011     // Handle X events
2012     handle_events();
2013    
2014     // Handle palette changes
2015     handle_palette_changes(depth, DISPLAY_DGA);
2016    
2017     // Update display (VOSF variant)
2018     static int tick_counter = 0;
2019     if (++tick_counter >= frame_skip) {
2020     tick_counter = 0;
2021 gbeauche 1.36 if (mainBuffer.dirty) {
2022     LOCK_VOSF;
2023     update_display_dga_vosf();
2024     UNLOCK_VOSF;
2025     }
2026 gbeauche 1.19 }
2027     }
2028     #endif
2029    
2030     static void video_refresh_window_vosf(void)
2031     {
2032     // Quit DGA mode if requested
2033     possibly_quit_dga_mode();
2034    
2035     // Handle X events
2036     handle_events();
2037    
2038     // Handle palette changes
2039     handle_palette_changes(depth, DISPLAY_WINDOW);
2040    
2041     // Update display (VOSF variant)
2042     static int tick_counter = 0;
2043     if (++tick_counter >= frame_skip) {
2044     tick_counter = 0;
2045 gbeauche 1.36 if (mainBuffer.dirty) {
2046     LOCK_VOSF;
2047     update_display_window_vosf();
2048     UNLOCK_VOSF;
2049 cebix 1.37 XSync(x_display, false); // Let the server catch up
2050 gbeauche 1.36 }
2051 gbeauche 1.19 }
2052     }
2053 cebix 1.23 #endif // def ENABLE_VOSF
2054 gbeauche 1.19
2055     static void video_refresh_window_static(void)
2056     {
2057     // Handle X events
2058     handle_events();
2059    
2060     // Handle_palette changes
2061     handle_palette_changes(depth, DISPLAY_WINDOW);
2062    
2063     // Update display (static variant)
2064     static int tick_counter = 0;
2065     if (++tick_counter >= frame_skip) {
2066     tick_counter = 0;
2067     update_display_static();
2068     }
2069     }
2070    
2071     static void video_refresh_window_dynamic(void)
2072     {
2073     // Handle X events
2074     handle_events();
2075    
2076     // Handle_palette changes
2077     handle_palette_changes(depth, DISPLAY_WINDOW);
2078    
2079     // Update display (dynamic variant)
2080     static int tick_counter = 0;
2081     tick_counter++;
2082     update_display_dynamic(tick_counter);
2083     }
2084    
2085    
2086     /*
2087 cebix 1.1 * Thread for screen refresh, input handling etc.
2088     */
2089    
2090 gbeauche 1.19 void VideoRefreshInit(void)
2091     {
2092     // TODO: set up specialised 8bpp VideoRefresh handlers ?
2093     if (display_type == DISPLAY_DGA) {
2094 cebix 1.23 #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
2095 gbeauche 1.19 if (use_vosf)
2096     video_refresh = video_refresh_dga_vosf;
2097     else
2098     #endif
2099     video_refresh = video_refresh_dga;
2100     }
2101     else {
2102     #ifdef ENABLE_VOSF
2103     if (use_vosf)
2104     video_refresh = video_refresh_window_vosf;
2105     else
2106     #endif
2107     if (frame_skip == 0)
2108     video_refresh = video_refresh_window_dynamic;
2109     else
2110     video_refresh = video_refresh_window_static;
2111     }
2112     }
2113    
2114     void VideoRefresh(void)
2115     {
2116     // TODO: make main_unix/VideoRefresh call directly video_refresh() ?
2117     video_refresh();
2118     }
2119    
2120 cebix 1.15 #ifdef HAVE_PTHREADS
2121     static void *redraw_func(void *arg)
2122     {
2123 gbeauche 1.19 uint64 start = GetTicks_usec();
2124     int64 ticks = 0;
2125 cebix 1.18 uint64 next = GetTicks_usec();
2126 cebix 1.15 while (!redraw_thread_cancel) {
2127 gbeauche 1.19 video_refresh();
2128 cebix 1.18 next += 16667;
2129     int64 delay = next - GetTicks_usec();
2130     if (delay > 0)
2131     Delay_usec(delay);
2132     else if (delay < -16667)
2133     next = GetTicks_usec();
2134 gbeauche 1.19 ticks++;
2135 cebix 1.1 }
2136 gbeauche 1.19 uint64 end = GetTicks_usec();
2137 cebix 1.37 // printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start));
2138 cebix 1.1 return NULL;
2139     }
2140 cebix 1.15 #endif