1 |
|
/* |
2 |
|
* video_x.cpp - Video/graphics emulation, X11 specific stuff |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2000 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2001 Christian Bauer |
5 |
|
* |
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 |
53 |
|
#endif |
54 |
|
|
55 |
|
#ifdef ENABLE_VOSF |
56 |
– |
# include <unistd.h> |
57 |
– |
# include <signal.h> |
56 |
|
# include <fcntl.h> |
57 |
|
# include <sys/mman.h> |
58 |
+ |
# include "sigsegv.h" |
59 |
|
#endif |
60 |
|
|
61 |
|
#include "cpu_emulation.h" |
121 |
|
static XColor black, white; |
122 |
|
static unsigned long black_pixel, white_pixel; |
123 |
|
static int eventmask; |
124 |
< |
static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | FocusChangeMask | ExposureMask; |
125 |
< |
static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask; |
124 |
> |
static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask; |
125 |
> |
static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask; |
126 |
> |
static Atom WM_DELETE_WINDOW = (Atom)0; |
127 |
|
|
128 |
|
static XColor palette[256]; // Color palette for 8-bit mode |
129 |
|
static bool palette_changed = false; // Flag: Palette changed, redraw thread must set new colors |
130 |
|
#ifdef HAVE_PTHREADS |
131 |
|
static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect palette |
132 |
+ |
#define LOCK_PALETTE pthread_mutex_lock(&palette_lock) |
133 |
+ |
#define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock) |
134 |
+ |
#else |
135 |
+ |
#define LOCK_PALETTE |
136 |
+ |
#define UNLOCK_PALETTE |
137 |
|
#endif |
138 |
|
|
139 |
|
// Variables for window mode |
154 |
|
static void *fb_save = NULL; // Saved frame buffer for suspend |
155 |
|
#ifdef HAVE_PTHREADS |
156 |
|
static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer |
157 |
+ |
#define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock); |
158 |
+ |
#define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock); |
159 |
+ |
#else |
160 |
+ |
#define LOCK_FRAME_BUFFER |
161 |
+ |
#define UNLOCK_FRAME_BUFFER |
162 |
|
#endif |
163 |
|
|
164 |
|
// Variables for fbdev DGA mode |
178 |
|
#endif |
179 |
|
|
180 |
|
#ifdef ENABLE_VOSF |
181 |
< |
static uint8 * the_host_buffer; // Host frame buffer in VOSF mode |
181 |
> |
// Variables for Video on SEGV support (taken from the Win32 port) |
182 |
> |
static uint8 *the_host_buffer; // Host frame buffer in VOSF mode |
183 |
|
static uint32 the_buffer_size; // Size of allocated the_buffer |
173 |
– |
#endif |
184 |
|
|
175 |
– |
#ifdef ENABLE_VOSF |
176 |
– |
// Variables for Video on SEGV support (taken from the Win32 port) |
185 |
|
struct ScreenPageInfo { |
186 |
|
int top, bottom; // Mapping between this virtual page and Mac scanlines |
187 |
|
}; |
196 |
|
int pageBits; // Shift count to get the page number |
197 |
|
uint32 pageCount; // Number of pages allocated to the screen |
198 |
|
|
199 |
< |
uint8 * dirtyPages; // Table of flags set if page was altered |
199 |
> |
bool dirty; // Flag: set if the frame buffer was touched |
200 |
> |
char * dirtyPages; // Table of flags set if page was altered |
201 |
|
ScreenPageInfo * pageInfo; // Table of mappings page -> Mac scanlines |
202 |
|
}; |
203 |
|
|
204 |
|
static ScreenInfo mainBuffer; |
205 |
|
|
206 |
< |
#define PFLAG_SET(page) mainBuffer.dirtyPages[page] = 1 |
207 |
< |
#define PFLAG_CLEAR(page) mainBuffer.dirtyPages[page] = 0 |
208 |
< |
#define PFLAG_ISSET(page) mainBuffer.dirtyPages[page] |
209 |
< |
#define PFLAG_ISCLEAR(page) (mainBuffer.dirtyPages[page] == 0) |
206 |
> |
#define PFLAG_SET_VALUE 0x00 |
207 |
> |
#define PFLAG_CLEAR_VALUE 0x01 |
208 |
> |
#define PFLAG_SET_VALUE_4 0x00000000 |
209 |
> |
#define PFLAG_CLEAR_VALUE_4 0x01010101 |
210 |
> |
#define PFLAG_SET(page) mainBuffer.dirtyPages[page] = PFLAG_SET_VALUE |
211 |
> |
#define PFLAG_CLEAR(page) mainBuffer.dirtyPages[page] = PFLAG_CLEAR_VALUE |
212 |
> |
#define PFLAG_ISSET(page) (mainBuffer.dirtyPages[page] == PFLAG_SET_VALUE) |
213 |
> |
#define PFLAG_ISCLEAR(page) (mainBuffer.dirtyPages[page] != PFLAG_SET_VALUE) |
214 |
> |
|
215 |
|
#ifdef UNALIGNED_PROFITABLE |
216 |
< |
# define PFLAG_ISCLEAR_4(page) (*((uint32 *)(mainBuffer.dirtyPages + page)) == 0) |
216 |
> |
# define PFLAG_ISSET_4(page) (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_SET_VALUE_4) |
217 |
> |
# define PFLAG_ISCLEAR_4(page) (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_CLEAR_VALUE_4) |
218 |
> |
#else |
219 |
> |
# define PFLAG_ISSET_4(page) \ |
220 |
> |
PFLAG_ISSET(page ) && PFLAG_ISSET(page+1) \ |
221 |
> |
&& PFLAG_ISSET(page+2) && PFLAG_ISSET(page+3) |
222 |
> |
# define PFLAG_ISCLEAR_4(page) \ |
223 |
> |
PFLAG_ISCLEAR(page ) && PFLAG_ISCLEAR(page+1) \ |
224 |
> |
&& PFLAG_ISCLEAR(page+2) && PFLAG_ISCLEAR(page+3) |
225 |
> |
#endif |
226 |
> |
|
227 |
> |
// Set the selected page range [ first_page, last_page [ into the SET state |
228 |
> |
#define PFLAG_SET_RANGE(first_page, last_page) \ |
229 |
> |
memset(mainBuffer.dirtyPages + (first_page), PFLAG_SET_VALUE, \ |
230 |
> |
(last_page) - (first_page)) |
231 |
> |
|
232 |
> |
// Set the selected page range [ first_page, last_page [ into the CLEAR state |
233 |
> |
#define PFLAG_CLEAR_RANGE(first_page, last_page) \ |
234 |
> |
memset(mainBuffer.dirtyPages + (first_page), PFLAG_CLEAR_VALUE, \ |
235 |
> |
(last_page) - (first_page)) |
236 |
> |
|
237 |
> |
#define PFLAG_SET_ALL do { \ |
238 |
> |
PFLAG_SET_RANGE(0, mainBuffer.pageCount); \ |
239 |
> |
mainBuffer.dirty = true; \ |
240 |
> |
} while (0) |
241 |
> |
|
242 |
> |
#define PFLAG_CLEAR_ALL do { \ |
243 |
> |
PFLAG_CLEAR_RANGE(0, mainBuffer.pageCount); \ |
244 |
> |
mainBuffer.dirty = false; \ |
245 |
> |
} while (0) |
246 |
> |
|
247 |
> |
// Set the following macro definition to 1 if your system |
248 |
> |
// provides a really fast strchr() implementation |
249 |
> |
//#define HAVE_FAST_STRCHR 0 |
250 |
> |
|
251 |
> |
static inline int find_next_page_set(int page) |
252 |
> |
{ |
253 |
> |
#if HAVE_FAST_STRCHR |
254 |
> |
char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_SET_VALUE); |
255 |
> |
return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount; |
256 |
> |
#else |
257 |
> |
while (PFLAG_ISCLEAR_4(page)) |
258 |
> |
page += 4; |
259 |
> |
while (PFLAG_ISCLEAR(page)) |
260 |
> |
page++; |
261 |
> |
return page; |
262 |
> |
#endif |
263 |
> |
} |
264 |
> |
|
265 |
> |
static inline int find_next_page_clear(int page) |
266 |
> |
{ |
267 |
> |
#if HAVE_FAST_STRCHR |
268 |
> |
char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_CLEAR_VALUE); |
269 |
> |
return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount; |
270 |
|
#else |
271 |
< |
# define PFLAG_ISCLEAR_4(page) \ |
272 |
< |
(mainBuffer.dirtyPages[page ] == 0) \ |
273 |
< |
&& (mainBuffer.dirtyPages[page+1] == 0) \ |
274 |
< |
&& (mainBuffer.dirtyPages[page+2] == 0) \ |
275 |
< |
&& (mainBuffer.dirtyPages[page+3] == 0) |
271 |
> |
while (PFLAG_ISSET_4(page)) |
272 |
> |
page += 4; |
273 |
> |
while (PFLAG_ISSET(page)) |
274 |
> |
page++; |
275 |
> |
return page; |
276 |
|
#endif |
277 |
< |
#define PFLAG_CLEAR_ALL memset(mainBuffer.dirtyPages, 0, mainBuffer.pageCount) |
211 |
< |
#define PFLAG_SET_ALL memset(mainBuffer.dirtyPages, 1, mainBuffer.pageCount) |
277 |
> |
} |
278 |
|
|
279 |
|
static int zero_fd = -1; |
214 |
– |
static bool Screen_fault_handler_init(); |
215 |
– |
static struct sigaction vosf_sa; |
280 |
|
|
281 |
|
#ifdef HAVE_PTHREADS |
282 |
< |
static pthread_mutex_t Screen_draw_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer (dirtyPages in fact) |
282 |
> |
static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer (dirtyPages in fact) |
283 |
> |
#define LOCK_VOSF pthread_mutex_lock(&vosf_lock); |
284 |
> |
#define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock); |
285 |
> |
#else |
286 |
> |
#define LOCK_VOSF |
287 |
> |
#define UNLOCK_VOSF |
288 |
|
#endif |
289 |
|
|
290 |
|
static int log_base_2(uint32 x) |
297 |
|
} |
298 |
|
return l; |
299 |
|
} |
300 |
< |
|
232 |
< |
#endif |
300 |
> |
#endif /* ENABLE_VOSF */ |
301 |
|
|
302 |
|
// VideoRefresh function |
303 |
|
void VideoRefreshInit(void); |
305 |
|
|
306 |
|
// Prototypes |
307 |
|
static void *redraw_func(void *arg); |
308 |
< |
static int event2keycode(XKeyEvent *ev); |
308 |
> |
static int event2keycode(XKeyEvent &ev); |
309 |
|
|
310 |
|
|
311 |
|
// From main_unix.cpp |
375 |
|
VideoMonitor.bytes_per_row = bytes_per_row; |
376 |
|
} |
377 |
|
|
378 |
+ |
// Set window name and class |
379 |
+ |
static void set_window_name(Window w, int name) |
380 |
+ |
{ |
381 |
+ |
const char *str = GetString(name); |
382 |
+ |
XStoreName(x_display, w, str); |
383 |
+ |
XSetIconName(x_display, w, str); |
384 |
+ |
|
385 |
+ |
XClassHint *hints; |
386 |
+ |
hints = XAllocClassHint(); |
387 |
+ |
if (hints) { |
388 |
+ |
hints->res_name = "BasiliskII"; |
389 |
+ |
hints->res_class = "BasiliskII"; |
390 |
+ |
XSetClassHint(x_display, w, hints); |
391 |
+ |
XFree(hints); |
392 |
+ |
} |
393 |
+ |
} |
394 |
+ |
|
395 |
+ |
// Set window input focus flag |
396 |
+ |
static void set_window_focus(Window w) |
397 |
+ |
{ |
398 |
+ |
XWMHints *hints = XAllocWMHints(); |
399 |
+ |
if (hints) { |
400 |
+ |
hints->input = True; |
401 |
+ |
hints->initial_state = NormalState; |
402 |
+ |
hints->flags = InputHint | StateHint; |
403 |
+ |
XSetWMHints(x_display, w, hints); |
404 |
+ |
XFree(hints); |
405 |
+ |
} |
406 |
+ |
} |
407 |
+ |
|
408 |
+ |
// Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget) |
409 |
+ |
static void set_window_delete_protocol(Window w) |
410 |
+ |
{ |
411 |
+ |
WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false); |
412 |
+ |
XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1); |
413 |
+ |
} |
414 |
+ |
|
415 |
+ |
// Wait until window is mapped/unmapped |
416 |
+ |
void wait_mapped(Window w) |
417 |
+ |
{ |
418 |
+ |
XEvent e; |
419 |
+ |
do { |
420 |
+ |
XMaskEvent(x_display, StructureNotifyMask, &e); |
421 |
+ |
} while ((e.type != MapNotify) || (e.xmap.event != w)); |
422 |
+ |
} |
423 |
+ |
|
424 |
+ |
void wait_unmapped(Window w) |
425 |
+ |
{ |
426 |
+ |
XEvent e; |
427 |
+ |
do { |
428 |
+ |
XMaskEvent(x_display, StructureNotifyMask, &e); |
429 |
+ |
} while ((e.type != UnmapNotify) || (e.xmap.event != w)); |
430 |
+ |
} |
431 |
+ |
|
432 |
|
// Trap SHM errors |
433 |
|
static bool shm_error = false; |
434 |
|
static int (*old_error_handler)(Display *, XErrorEvent *); |
458 |
|
XSetWindowAttributes wattr; |
459 |
|
wattr.event_mask = eventmask = win_eventmask; |
460 |
|
wattr.background_pixel = black_pixel; |
461 |
+ |
wattr.colormap = cmap[0]; |
462 |
|
|
340 |
– |
XSync(x_display, false); |
463 |
|
the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, |
464 |
< |
InputOutput, vis, CWEventMask | CWBackPixel, &wattr); |
464 |
> |
InputOutput, vis, CWEventMask | CWBackPixel | (depth == 8 ? CWColormap : 0), &wattr); |
465 |
> |
|
466 |
> |
// Set window name/class |
467 |
> |
set_window_name(the_win, STR_WINDOW_TITLE); |
468 |
|
|
469 |
|
// Indicate that we want keyboard input |
470 |
< |
{ |
471 |
< |
XWMHints *hints = XAllocWMHints(); |
472 |
< |
if (hints) { |
473 |
< |
hints->input = True; |
349 |
< |
hints->flags = InputHint; |
350 |
< |
XSetWMHints(x_display, the_win, hints); |
351 |
< |
XFree((char *)hints); |
352 |
< |
} |
353 |
< |
} |
470 |
> |
set_window_focus(the_win); |
471 |
> |
|
472 |
> |
// Set delete protocol property |
473 |
> |
set_window_delete_protocol(the_win); |
474 |
|
|
475 |
|
// Make window unresizable |
476 |
|
{ |
482 |
|
hints->max_height = height; |
483 |
|
hints->flags = PMinSize | PMaxSize; |
484 |
|
XSetWMNormalHints(x_display, the_win, hints); |
485 |
< |
XFree((char *)hints); |
485 |
> |
XFree(hints); |
486 |
|
} |
487 |
|
} |
488 |
|
|
369 |
– |
// Set window title |
370 |
– |
XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); |
371 |
– |
|
372 |
– |
// Set window class |
373 |
– |
{ |
374 |
– |
XClassHint *hints; |
375 |
– |
hints = XAllocClassHint(); |
376 |
– |
if (hints) { |
377 |
– |
hints->res_name = "BasiliskII"; |
378 |
– |
hints->res_class = "BasiliskII"; |
379 |
– |
XSetClassHint(x_display, the_win, hints); |
380 |
– |
XFree((char *)hints); |
381 |
– |
} |
382 |
– |
} |
383 |
– |
|
489 |
|
// Show window |
490 |
< |
XSync(x_display, false); |
491 |
< |
XMapRaised(x_display, the_win); |
387 |
< |
XFlush(x_display); |
388 |
< |
|
389 |
< |
// Set colormap |
390 |
< |
if (depth == 8) |
391 |
< |
XSetWindowColormap(x_display, the_win, cmap[0]); |
490 |
> |
XMapWindow(x_display, the_win); |
491 |
> |
wait_mapped(the_win); |
492 |
|
|
493 |
|
// Try to create and attach SHM image |
494 |
|
have_shm = false; |
572 |
|
// Set VideoMonitor |
573 |
|
bool native_byte_order; |
574 |
|
#ifdef WORDS_BIGENDIAN |
575 |
< |
native_byte_order = (img->bitmap_bit_order == MSBFirst); |
575 |
> |
native_byte_order = (XImageByteOrder(x_display) == MSBFirst); |
576 |
|
#else |
577 |
< |
native_byte_order = (img->bitmap_bit_order == LSBFirst); |
577 |
> |
native_byte_order = (XImageByteOrder(x_display) == LSBFirst); |
578 |
|
#endif |
579 |
|
#ifdef ENABLE_VOSF |
580 |
< |
do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW); |
580 |
> |
Screen_blitter_init(&visualInfo, native_byte_order); |
581 |
|
#endif |
582 |
|
set_video_monitor(width, height, img->bytes_per_line, native_byte_order); |
583 |
|
|
661 |
|
|
662 |
|
// Create window |
663 |
|
XSetWindowAttributes wattr; |
664 |
< |
wattr.event_mask = eventmask = dga_eventmask; |
665 |
< |
wattr.background_pixel = white_pixel; |
666 |
< |
wattr.override_redirect = True; |
664 |
> |
wattr.event_mask = eventmask = dga_eventmask; |
665 |
> |
wattr.background_pixel = white_pixel; |
666 |
> |
wattr.override_redirect = True; |
667 |
> |
wattr.colormap = cmap[0]; |
668 |
|
|
568 |
– |
XSync(x_display, false); |
669 |
|
the_win = XCreateWindow(x_display, rootwin, |
670 |
|
0, 0, width, height, |
671 |
|
0, xdepth, InputOutput, vis, |
672 |
< |
CWEventMask | CWBackPixel | CWOverrideRedirect, |
672 |
> |
CWEventMask | CWBackPixel | CWOverrideRedirect | (depth == 8 ? CWColormap : 0), |
673 |
|
&wattr); |
674 |
< |
XSync(x_display, false); |
674 |
> |
|
675 |
> |
// Set window name/class |
676 |
> |
set_window_name(the_win, STR_WINDOW_TITLE); |
677 |
> |
|
678 |
> |
// Indicate that we want keyboard input |
679 |
> |
set_window_focus(the_win); |
680 |
> |
|
681 |
> |
// Show window |
682 |
|
XMapRaised(x_display, the_win); |
683 |
< |
XSync(x_display, false); |
683 |
> |
wait_mapped(the_win); |
684 |
|
|
685 |
|
// Grab mouse and keyboard |
686 |
|
XGrabKeyboard(x_display, the_win, True, |
689 |
|
PointerMotionMask | ButtonPressMask | ButtonReleaseMask, |
690 |
|
GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime); |
691 |
|
|
585 |
– |
// Set colormap |
586 |
– |
if (depth == 8) |
587 |
– |
XSetWindowColormap(x_display, the_win, cmap[0]); |
588 |
– |
|
692 |
|
// Set VideoMonitor |
693 |
|
int bytes_per_row = width; |
694 |
|
switch (depth) { |
716 |
|
|
717 |
|
#if ENABLE_VOSF |
718 |
|
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
719 |
< |
// If the blit function is null, i.e. just a copy of the buffer, |
720 |
< |
// we first try to avoid the allocation of a temporary frame buffer |
721 |
< |
use_vosf = true; |
619 |
< |
do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA); |
620 |
< |
if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw)) |
621 |
< |
use_vosf = false; |
719 |
> |
// Screen_blitter_init() returns TRUE if VOSF is mandatory |
720 |
> |
// i.e. the framebuffer update function is not Blit_Copy_Raw |
721 |
> |
use_vosf = Screen_blitter_init(&visualInfo, true); |
722 |
|
|
723 |
|
if (use_vosf) { |
724 |
|
the_host_buffer = the_buffer; |
765 |
|
} |
766 |
|
XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]); |
767 |
|
XF86VidModeSetViewPort(x_display, screen, 0, 0); |
768 |
+ |
XSync(x_display, false); |
769 |
|
} |
770 |
|
#endif |
771 |
|
|
774 |
|
wattr.event_mask = eventmask = dga_eventmask; |
775 |
|
wattr.override_redirect = True; |
776 |
|
|
676 |
– |
XSync(x_display, false); |
777 |
|
the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, |
778 |
|
InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr); |
779 |
< |
XSync(x_display, false); |
780 |
< |
XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); |
779 |
> |
|
780 |
> |
// Set window name/class |
781 |
> |
set_window_name(the_win, STR_WINDOW_TITLE); |
782 |
> |
|
783 |
> |
// Indicate that we want keyboard input |
784 |
> |
set_window_focus(the_win); |
785 |
> |
|
786 |
> |
// Show window |
787 |
|
XMapRaised(x_display, the_win); |
788 |
< |
XSync(x_display, false); |
788 |
> |
wait_mapped(the_win); |
789 |
|
|
790 |
|
// Establish direct screen connection |
791 |
|
XMoveResizeWindow(x_display, the_win, 0, 0, width, height); |
804 |
|
XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]); |
805 |
|
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
806 |
|
} |
807 |
+ |
XSync(x_display, false); |
808 |
|
|
809 |
|
// Set VideoMonitor |
810 |
|
int bytes_per_row = (v_width + 7) & ~7; |
823 |
|
} |
824 |
|
|
825 |
|
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
826 |
< |
// If the blit function is null, i.e. just a copy of the buffer, |
827 |
< |
// we first try to avoid the allocation of a temporary frame buffer |
828 |
< |
use_vosf = true; |
722 |
< |
do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA); |
723 |
< |
if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw)) |
724 |
< |
use_vosf = false; |
826 |
> |
// Screen_blitter_init() returns TRUE if VOSF is mandatory |
827 |
> |
// i.e. the framebuffer update function is not Blit_Copy_Raw |
828 |
> |
use_vosf = Screen_blitter_init(&visualInfo, true); |
829 |
|
|
830 |
|
if (use_vosf) { |
831 |
|
the_host_buffer = the_buffer; |
939 |
|
if (mainBuffer.dirtyPages != 0) |
940 |
|
free(mainBuffer.dirtyPages); |
941 |
|
|
942 |
< |
mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount); |
942 |
> |
mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2); |
943 |
|
|
944 |
|
if (mainBuffer.pageInfo != 0) |
945 |
|
free(mainBuffer.pageInfo); |
948 |
|
|
949 |
|
if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0)) |
950 |
|
return false; |
951 |
+ |
|
952 |
+ |
mainBuffer.dirty = false; |
953 |
|
|
954 |
|
PFLAG_CLEAR_ALL; |
955 |
+ |
// Safety net to insure the loops in the update routines will terminate |
956 |
+ |
// See a discussion in <video_vosf.h> for further details |
957 |
+ |
PFLAG_CLEAR(mainBuffer.pageCount); |
958 |
+ |
PFLAG_SET(mainBuffer.pageCount+1); |
959 |
|
|
960 |
|
uint32 a = 0; |
961 |
|
for (int i = 0; i < mainBuffer.pageCount; i++) { |
1008 |
|
keycode_init(); |
1009 |
|
|
1010 |
|
// Read prefs |
1011 |
< |
mouse_wheel_mode = PrefsFindInt16("mousewheelmode"); |
1012 |
< |
mouse_wheel_lines = PrefsFindInt16("mousewheellines"); |
1011 |
> |
mouse_wheel_mode = PrefsFindInt32("mousewheelmode"); |
1012 |
> |
mouse_wheel_lines = PrefsFindInt32("mousewheellines"); |
1013 |
|
|
1014 |
|
// Find screen and root window |
1015 |
|
screen = XDefaultScreen(x_display); |
1146 |
|
break; |
1147 |
|
} |
1148 |
|
|
1039 |
– |
#ifdef HAVE_PTHREADS |
1149 |
|
// Lock down frame buffer |
1150 |
< |
pthread_mutex_lock(&frame_buffer_lock); |
1042 |
< |
#endif |
1150 |
> |
LOCK_FRAME_BUFFER; |
1151 |
|
|
1152 |
|
#if !REAL_ADDRESSING && !DIRECT_ADDRESSING |
1153 |
|
// Set variables for UAE memory mapping |
1169 |
|
} |
1170 |
|
|
1171 |
|
// Initialize the handler for SIGSEGV |
1172 |
< |
if (!Screen_fault_handler_init()) { |
1172 |
> |
if (!sigsegv_install_handler(screen_fault_handler)) { |
1173 |
|
// TODO: STR_VOSF_INIT_ERR ? |
1174 |
|
ErrorAlert("Could not initialize Video on SEGV signals"); |
1175 |
|
return false; |
1212 |
|
} |
1213 |
|
#endif |
1214 |
|
|
1107 |
– |
#ifdef HAVE_PTHREADS |
1215 |
|
// Unlock frame buffer |
1216 |
< |
pthread_mutex_unlock(&frame_buffer_lock); |
1110 |
< |
#endif |
1216 |
> |
UNLOCK_FRAME_BUFFER; |
1217 |
|
|
1218 |
|
// Close window and server connection |
1219 |
|
if (x_display != NULL) { |
1316 |
|
if (emerg_quit) |
1317 |
|
QuitEmulator(); |
1318 |
|
|
1213 |
– |
#ifdef HAVE_PTHREADS |
1319 |
|
// Temporarily give up frame buffer lock (this is the point where |
1320 |
|
// we are suspended when the user presses Ctrl-Tab) |
1321 |
< |
pthread_mutex_unlock(&frame_buffer_lock); |
1322 |
< |
pthread_mutex_lock(&frame_buffer_lock); |
1218 |
< |
#endif |
1321 |
> |
UNLOCK_FRAME_BUFFER; |
1322 |
> |
LOCK_FRAME_BUFFER; |
1323 |
|
} |
1324 |
|
|
1325 |
|
|
1329 |
|
|
1330 |
|
void video_set_palette(uint8 *pal) |
1331 |
|
{ |
1332 |
< |
#ifdef HAVE_PTHREADS |
1229 |
< |
pthread_mutex_lock(&palette_lock); |
1230 |
< |
#endif |
1332 |
> |
LOCK_PALETTE; |
1333 |
|
|
1334 |
|
// Convert colors to XColor array |
1335 |
|
for (int i=0; i<256; i++) { |
1343 |
|
// Tell redraw thread to change palette |
1344 |
|
palette_changed = true; |
1345 |
|
|
1346 |
< |
#ifdef HAVE_PTHREADS |
1245 |
< |
pthread_mutex_unlock(&palette_lock); |
1246 |
< |
#endif |
1346 |
> |
UNLOCK_PALETTE; |
1347 |
|
} |
1348 |
|
|
1349 |
|
|
1359 |
|
ADBKeyUp(0x36); |
1360 |
|
ctrl_down = false; |
1361 |
|
|
1262 |
– |
#ifdef HAVE_PTHREADS |
1362 |
|
// Lock frame buffer (this will stop the MacOS thread) |
1363 |
< |
pthread_mutex_lock(&frame_buffer_lock); |
1265 |
< |
#endif |
1363 |
> |
LOCK_FRAME_BUFFER; |
1364 |
|
|
1365 |
|
// Save frame buffer |
1366 |
|
fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row); |
1374 |
|
XUngrabPointer(x_display, CurrentTime); |
1375 |
|
XUngrabKeyboard(x_display, CurrentTime); |
1376 |
|
XUnmapWindow(x_display, the_win); |
1377 |
< |
XSync(x_display, false); |
1377 |
> |
wait_unmapped(the_win); |
1378 |
|
|
1379 |
|
// Open "suspend" window |
1380 |
|
XSetWindowAttributes wattr; |
1381 |
|
wattr.event_mask = KeyPressMask; |
1382 |
|
wattr.background_pixel = black_pixel; |
1285 |
– |
wattr.backing_store = WhenMapped; |
1286 |
– |
wattr.backing_planes = xdepth; |
1287 |
– |
wattr.colormap = DefaultColormap(x_display, screen); |
1383 |
|
|
1289 |
– |
XSync(x_display, false); |
1384 |
|
suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth, |
1385 |
< |
InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore | |
1386 |
< |
CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr); |
1387 |
< |
XSync(x_display, false); |
1388 |
< |
XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE)); |
1295 |
< |
XMapRaised(x_display, suspend_win); |
1296 |
< |
XSync(x_display, false); |
1385 |
> |
InputOutput, vis, CWEventMask | CWBackPixel, &wattr); |
1386 |
> |
set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE); |
1387 |
> |
set_window_focus(suspend_win); |
1388 |
> |
XMapWindow(x_display, suspend_win); |
1389 |
|
emul_suspended = true; |
1390 |
|
} |
1391 |
|
} |
1398 |
|
|
1399 |
|
// Reopen full screen display |
1400 |
|
XMapRaised(x_display, the_win); |
1401 |
+ |
wait_mapped(the_win); |
1402 |
|
XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); |
1310 |
– |
XSync(x_display, false); |
1403 |
|
XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime); |
1404 |
|
XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); |
1405 |
|
#ifdef ENABLE_XF86_DGA |
1413 |
|
// not necessary. |
1414 |
|
#ifdef ENABLE_VOSF |
1415 |
|
if (use_vosf) { |
1416 |
< |
#ifdef HAVE_PTHREADS |
1325 |
< |
pthread_mutex_lock(&Screen_draw_lock); |
1326 |
< |
#endif |
1416 |
> |
LOCK_VOSF; |
1417 |
|
PFLAG_SET_ALL; |
1418 |
< |
#ifdef HAVE_PTHREADS |
1329 |
< |
pthread_mutex_unlock(&Screen_draw_lock); |
1330 |
< |
#endif |
1418 |
> |
UNLOCK_VOSF; |
1419 |
|
memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); |
1420 |
|
} |
1421 |
|
#endif |
1436 |
|
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
1437 |
|
#endif |
1438 |
|
|
1351 |
– |
#ifdef HAVE_PTHREADS |
1439 |
|
// Unlock frame buffer (and continue MacOS thread) |
1440 |
< |
pthread_mutex_unlock(&frame_buffer_lock); |
1440 |
> |
UNLOCK_FRAME_BUFFER; |
1441 |
|
emul_suspended = false; |
1355 |
– |
#endif |
1442 |
|
} |
1443 |
|
#endif |
1444 |
|
|
1592 |
|
return -1; |
1593 |
|
} |
1594 |
|
|
1595 |
< |
static int event2keycode(XKeyEvent *ev) |
1595 |
> |
static int event2keycode(XKeyEvent &ev) |
1596 |
|
{ |
1597 |
|
KeySym ks; |
1598 |
|
int as; |
1599 |
|
int i = 0; |
1600 |
|
|
1601 |
|
do { |
1602 |
< |
ks = XLookupKeysym(ev, i++); |
1602 |
> |
ks = XLookupKeysym(&ev, i++); |
1603 |
|
as = kc_decode(ks); |
1604 |
|
if (as != -1) |
1605 |
|
return as; |
1615 |
|
|
1616 |
|
static void handle_events(void) |
1617 |
|
{ |
1618 |
< |
XEvent event; |
1619 |
< |
for (;;) { |
1620 |
< |
if (!XCheckMaskEvent(x_display, eventmask, &event)) |
1535 |
< |
break; |
1618 |
> |
while (XPending(x_display)) { |
1619 |
> |
XEvent event; |
1620 |
> |
XNextEvent(x_display, &event); |
1621 |
|
|
1622 |
|
switch (event.type) { |
1623 |
|
// Mouse button |
1624 |
|
case ButtonPress: { |
1625 |
< |
unsigned int button = ((XButtonEvent *)&event)->button; |
1625 |
> |
unsigned int button = event.xbutton.button; |
1626 |
|
if (button < 4) |
1627 |
|
ADBMouseDown(button - 1); |
1628 |
|
else if (button < 6) { // Wheel mouse |
1641 |
|
break; |
1642 |
|
} |
1643 |
|
case ButtonRelease: { |
1644 |
< |
unsigned int button = ((XButtonEvent *)&event)->button; |
1644 |
> |
unsigned int button = event.xbutton.button; |
1645 |
|
if (button < 4) |
1646 |
|
ADBMouseUp(button - 1); |
1647 |
|
break; |
1649 |
|
|
1650 |
|
// Mouse moved |
1651 |
|
case EnterNotify: |
1567 |
– |
ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); |
1568 |
– |
break; |
1652 |
|
case MotionNotify: |
1653 |
< |
ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); |
1653 |
> |
ADBMouseMoved(event.xmotion.x, event.xmotion.y); |
1654 |
|
break; |
1655 |
|
|
1656 |
|
// Keyboard |
1657 |
|
case KeyPress: { |
1658 |
|
int code; |
1659 |
|
if (use_keycodes) { |
1660 |
< |
event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys |
1661 |
< |
code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff]; |
1660 |
> |
event2keycode(event.xkey); // This is called to process the hotkeys |
1661 |
> |
code = keycode_table[event.xkey.keycode & 0xff]; |
1662 |
|
} else |
1663 |
< |
code = event2keycode((XKeyEvent *)&event); |
1663 |
> |
code = event2keycode(event.xkey); |
1664 |
|
if (code != -1) { |
1665 |
|
if (!emul_suspended) { |
1666 |
|
if (code == 0x39) { // Caps Lock pressed |
1687 |
|
case KeyRelease: { |
1688 |
|
int code; |
1689 |
|
if (use_keycodes) { |
1690 |
< |
event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys |
1691 |
< |
code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff]; |
1690 |
> |
event2keycode(event.xkey); // This is called to process the hotkeys |
1691 |
> |
code = keycode_table[event.xkey.keycode & 0xff]; |
1692 |
|
} else |
1693 |
< |
code = event2keycode((XKeyEvent *)&event); |
1693 |
> |
code = event2keycode(event.xkey); |
1694 |
|
if (code != -1 && code != 0x39) { // Don't propagate Caps Lock releases |
1695 |
|
ADBKeyUp(code); |
1696 |
|
if (code == 0x36) |
1704 |
|
if (display_type == DISPLAY_WINDOW) { |
1705 |
|
#ifdef ENABLE_VOSF |
1706 |
|
if (use_vosf) { // VOSF refresh |
1707 |
< |
#ifdef HAVE_PTHREADS |
1625 |
< |
pthread_mutex_lock(&Screen_draw_lock); |
1626 |
< |
#endif |
1707 |
> |
LOCK_VOSF; |
1708 |
|
PFLAG_SET_ALL; |
1709 |
< |
#ifdef HAVE_PTHREADS |
1629 |
< |
pthread_mutex_unlock(&Screen_draw_lock); |
1630 |
< |
#endif |
1709 |
> |
UNLOCK_VOSF; |
1710 |
|
memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); |
1711 |
|
} |
1712 |
|
else |
1722 |
|
} |
1723 |
|
break; |
1724 |
|
|
1725 |
< |
case FocusIn: |
1726 |
< |
case FocusOut: |
1725 |
> |
// Window "close" widget clicked |
1726 |
> |
case ClientMessage: |
1727 |
> |
if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) { |
1728 |
> |
ADBKeyDown(0x7f); // Power key |
1729 |
> |
ADBKeyUp(0x7f); |
1730 |
> |
} |
1731 |
|
break; |
1732 |
|
} |
1733 |
|
} |
1947 |
|
* Screen refresh functions |
1948 |
|
*/ |
1949 |
|
|
1950 |
< |
// The specialisations hereunder are meant to enable VOSF with DGA in direct |
1951 |
< |
// addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could |
1952 |
< |
// not get mapped correctly with respect to the predetermined host frame |
1953 |
< |
// buffer base address. |
1954 |
< |
// |
1872 |
< |
// Hmm, in other words, when in direct addressing mode and DGA is requested, |
1873 |
< |
// we first try to "triple allocate" the address spaces according to the real |
1874 |
< |
// host frame buffer address. Then, if it fails, we will use a temporary |
1875 |
< |
// frame buffer thus making the real host frame buffer updated when pages |
1876 |
< |
// of the temp frame buffer are altered. |
1877 |
< |
// |
1878 |
< |
// As a side effect, a little speed gain in screen updates could be noticed |
1879 |
< |
// for other modes than DGA. |
1880 |
< |
// |
1881 |
< |
// The following two functions below are inline so that a clever compiler |
1882 |
< |
// could specialise the code according to the current screen depth and |
1883 |
< |
// display type. A more clever compiler would the job by itself though... |
1884 |
< |
// (update_display_vosf is inlined as well) |
1950 |
> |
// We suggest the compiler to inline the next two functions so that it |
1951 |
> |
// may specialise the code according to the current screen depth and |
1952 |
> |
// display type. A clever compiler would do that job by itself though... |
1953 |
> |
|
1954 |
> |
// NOTE: update_display_vosf is inlined too |
1955 |
|
|
1956 |
|
static inline void possibly_quit_dga_mode() |
1957 |
|
{ |
1972 |
|
|
1973 |
|
static inline void handle_palette_changes(int depth, int display_type) |
1974 |
|
{ |
1975 |
< |
#ifdef HAVE_PTHREADS |
1976 |
< |
pthread_mutex_lock(&palette_lock); |
1907 |
< |
#endif |
1975 |
> |
LOCK_PALETTE; |
1976 |
> |
|
1977 |
|
if (palette_changed) { |
1978 |
|
palette_changed = false; |
1979 |
|
if (depth == 8) { |
1980 |
|
XStoreColors(x_display, cmap[0], palette, 256); |
1981 |
|
XStoreColors(x_display, cmap[1], palette, 256); |
1982 |
+ |
XSync(x_display, false); |
1983 |
|
|
1984 |
|
#ifdef ENABLE_XF86_DGA |
1985 |
|
if (display_type == DISPLAY_DGA) { |
1989 |
|
#endif |
1990 |
|
} |
1991 |
|
} |
1992 |
< |
#ifdef HAVE_PTHREADS |
1993 |
< |
pthread_mutex_unlock(&palette_lock); |
1924 |
< |
#endif |
1992 |
> |
|
1993 |
> |
UNLOCK_PALETTE; |
1994 |
|
} |
1995 |
|
|
1996 |
|
static void video_refresh_dga(void) |
2022 |
|
static int tick_counter = 0; |
2023 |
|
if (++tick_counter >= frame_skip) { |
2024 |
|
tick_counter = 0; |
2025 |
< |
#ifdef HAVE_PTHREADS |
2026 |
< |
pthread_mutex_lock(&Screen_draw_lock); |
2027 |
< |
#endif |
2028 |
< |
update_display_dga_vosf(); |
2029 |
< |
#ifdef HAVE_PTHREADS |
1961 |
< |
pthread_mutex_unlock(&Screen_draw_lock); |
1962 |
< |
#endif |
2025 |
> |
if (mainBuffer.dirty) { |
2026 |
> |
LOCK_VOSF; |
2027 |
> |
update_display_dga_vosf(); |
2028 |
> |
UNLOCK_VOSF; |
2029 |
> |
} |
2030 |
|
} |
2031 |
|
} |
2032 |
|
#endif |
2046 |
|
static int tick_counter = 0; |
2047 |
|
if (++tick_counter >= frame_skip) { |
2048 |
|
tick_counter = 0; |
2049 |
< |
#ifdef HAVE_PTHREADS |
2050 |
< |
pthread_mutex_lock(&Screen_draw_lock); |
2051 |
< |
#endif |
2052 |
< |
update_display_window_vosf(); |
2053 |
< |
#ifdef HAVE_PTHREADS |
2054 |
< |
pthread_mutex_unlock(&Screen_draw_lock); |
1988 |
< |
#endif |
2049 |
> |
if (mainBuffer.dirty) { |
2050 |
> |
LOCK_VOSF; |
2051 |
> |
update_display_window_vosf(); |
2052 |
> |
UNLOCK_VOSF; |
2053 |
> |
XSync(x_display, false); // Let the server catch up |
2054 |
> |
} |
2055 |
|
} |
2056 |
|
} |
2057 |
|
#endif // def ENABLE_VOSF |
2121 |
|
video_refresh(); |
2122 |
|
} |
2123 |
|
|
2058 |
– |
#if 0 |
2059 |
– |
void VideoRefresh(void) |
2060 |
– |
{ |
2061 |
– |
#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) |
2062 |
– |
// Quit DGA mode if requested |
2063 |
– |
if (quit_full_screen) { |
2064 |
– |
quit_full_screen = false; |
2065 |
– |
if (display_type == DISPLAY_DGA) { |
2066 |
– |
#ifdef ENABLE_XF86_DGA |
2067 |
– |
XF86DGADirectVideo(x_display, screen, 0); |
2068 |
– |
#endif |
2069 |
– |
XUngrabPointer(x_display, CurrentTime); |
2070 |
– |
XUngrabKeyboard(x_display, CurrentTime); |
2071 |
– |
XUnmapWindow(x_display, the_win); |
2072 |
– |
XSync(x_display, false); |
2073 |
– |
} |
2074 |
– |
} |
2075 |
– |
#endif |
2076 |
– |
|
2077 |
– |
// Handle X events |
2078 |
– |
handle_events(); |
2079 |
– |
|
2080 |
– |
// Handle palette changes |
2081 |
– |
#ifdef HAVE_PTHREADS |
2082 |
– |
pthread_mutex_lock(&palette_lock); |
2083 |
– |
#endif |
2084 |
– |
if (palette_changed) { |
2085 |
– |
palette_changed = false; |
2086 |
– |
if (depth == 8) { |
2087 |
– |
XStoreColors(x_display, cmap[0], palette, 256); |
2088 |
– |
XStoreColors(x_display, cmap[1], palette, 256); |
2089 |
– |
|
2090 |
– |
#ifdef ENABLE_XF86_DGA |
2091 |
– |
if (display_type == DISPLAY_DGA) { |
2092 |
– |
current_dga_cmap ^= 1; |
2093 |
– |
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
2094 |
– |
} |
2095 |
– |
#endif |
2096 |
– |
} |
2097 |
– |
} |
2098 |
– |
#ifdef HAVE_PTHREADS |
2099 |
– |
pthread_mutex_unlock(&palette_lock); |
2100 |
– |
#endif |
2101 |
– |
|
2102 |
– |
// In window mode, update display |
2103 |
– |
static int tick_counter = 0; |
2104 |
– |
if (display_type == DISPLAY_WINDOW) { |
2105 |
– |
tick_counter++; |
2106 |
– |
if (frame_skip == 0) |
2107 |
– |
update_display_dynamic(tick_counter); |
2108 |
– |
else if (tick_counter >= frame_skip) { |
2109 |
– |
tick_counter = 0; |
2110 |
– |
update_display_static(); |
2111 |
– |
} |
2112 |
– |
} |
2113 |
– |
} |
2114 |
– |
#endif |
2115 |
– |
|
2124 |
|
#ifdef HAVE_PTHREADS |
2125 |
|
static void *redraw_func(void *arg) |
2126 |
|
{ |
2128 |
|
int64 ticks = 0; |
2129 |
|
uint64 next = GetTicks_usec(); |
2130 |
|
while (!redraw_thread_cancel) { |
2123 |
– |
// VideoRefresh(); |
2131 |
|
video_refresh(); |
2132 |
|
next += 16667; |
2133 |
|
int64 delay = next - GetTicks_usec(); |
2138 |
|
ticks++; |
2139 |
|
} |
2140 |
|
uint64 end = GetTicks_usec(); |
2141 |
< |
printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks); |
2141 |
> |
// printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start)); |
2142 |
|
return NULL; |
2143 |
|
} |
2144 |
|
#endif |