ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.25
Committed: 2000-10-13T16:47:52Z (23 years, 9 months ago) by cebix
Branch: MAIN
Changes since 1.24: +14 -4 lines
Log Message:
- replaced floating-point page shift calculation by integer routine, fixing
  the VOSF problems under NetBSD/m68k
- fixed off-by-7 error in 1-bit window update routines

File Contents

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