ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.41
Committed: 2001-06-27T20:05:30Z (23 years ago) by cebix
Branch: MAIN
Changes since 1.40: +9 -0 lines
Log Message:
depth/resolution switching infrastructure should be complete now; slot ROM
contains all supported depths, default mode is stored in XPRAM upon startup,
and added video_switch_to_mode() call (currently unimplemented in all drivers)

File Contents

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