ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.19
Committed: 2000-09-22T17:16:31Z (24 years, 2 months ago) by gbeauche
Branch: MAIN
Changes since 1.18: +515 -23 lines
Log Message:
- added direct and real addressing modes support
- added Video on SEGV signals support

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