1 |
|
/* |
2 |
|
* video_x.cpp - Video/graphics emulation, X11 specific stuff |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2004 Christian Bauer |
4 |
> |
* Basilisk II (C) 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 |
44 |
|
#endif |
45 |
|
|
46 |
|
#ifdef ENABLE_XF86_DGA |
47 |
< |
# include <X11/extensions/xf86dga.h> |
47 |
> |
# include <X11/extensions/Xxf86dga.h> |
48 |
|
#endif |
49 |
|
|
50 |
|
#ifdef ENABLE_XF86_VIDMODE |
99 |
|
#ifdef HAVE_PTHREADS |
100 |
|
static pthread_attr_t redraw_thread_attr; // Redraw thread attributes |
101 |
|
static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread |
102 |
+ |
static volatile bool redraw_thread_cancel_ack; // Flag: Acknowledge for redraw thread cancellation |
103 |
|
static pthread_t redraw_thread; // Redraw thread |
104 |
|
#endif |
105 |
|
|
140 |
|
static Visual *vis; |
141 |
|
static int color_class; |
142 |
|
|
143 |
+ |
static bool x_native_byte_order; // XImage has native byte order? |
144 |
|
static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes |
145 |
|
|
146 |
|
static Colormap cmap[2] = {0, 0}; // Colormaps for indexed modes (DGA needs two of them) |
147 |
|
|
148 |
< |
static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table |
148 |
> |
static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table |
149 |
|
static bool x_palette_changed = false; // Flag: Palette changed, redraw thread must set new colors |
150 |
|
|
151 |
|
#ifdef ENABLE_FBDEV_DGA |
194 |
|
// From main_unix.cpp |
195 |
|
extern char *x_display_name; |
196 |
|
extern Display *x_display; |
195 |
– |
extern void *vm_acquire_mac(size_t size); |
197 |
|
|
198 |
|
// From sys_unix.cpp |
199 |
|
extern void SysMountFirstFloppy(void); |
200 |
|
|
201 |
+ |
// From clip_unix.cpp |
202 |
+ |
extern void ClipboardSelectionClear(XSelectionClearEvent *); |
203 |
+ |
extern void ClipboardSelectionRequest(XSelectionRequestEvent *); |
204 |
+ |
|
205 |
|
|
206 |
|
/* |
207 |
|
* monitor_desc subclass for X11 display |
254 |
|
} |
255 |
|
|
256 |
|
// Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals) |
257 |
< |
static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue) |
257 |
> |
static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue, bool fix_byte_order = false) |
258 |
|
{ |
259 |
< |
return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift); |
259 |
> |
uint32 val = ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift); |
260 |
> |
if (fix_byte_order && !x_native_byte_order) { |
261 |
> |
// We have to fix byte order in the ExpandMap[] |
262 |
> |
// NOTE: this is only an optimization since Screen_blitter_init() |
263 |
> |
// could be arranged to choose an NBO or OBO (with |
264 |
> |
// byteswapping) Blit_Expand_X_To_Y() function |
265 |
> |
switch (visualFormat.depth) { |
266 |
> |
case 15: case 16: |
267 |
> |
val = do_byteswap_16(val); |
268 |
> |
break; |
269 |
> |
case 24: case 32: |
270 |
> |
val = do_byteswap_32(val); |
271 |
> |
break; |
272 |
> |
} |
273 |
> |
} |
274 |
> |
return val; |
275 |
|
} |
276 |
|
|
277 |
|
// Do we have a visual for handling the specified Mac depth? If so, set the |
497 |
|
|
498 |
|
|
499 |
|
/* |
500 |
+ |
* Framebuffer allocation routines |
501 |
+ |
*/ |
502 |
+ |
|
503 |
+ |
#ifdef ENABLE_VOSF |
504 |
+ |
#include "vm_alloc.h" |
505 |
+ |
|
506 |
+ |
static void *vm_acquire_framebuffer(uint32 size) |
507 |
+ |
{ |
508 |
+ |
// always try to allocate framebuffer at the same address |
509 |
+ |
static void *fb = VM_MAP_FAILED; |
510 |
+ |
if (fb != VM_MAP_FAILED) { |
511 |
+ |
if (vm_acquire_fixed(fb, size) < 0) |
512 |
+ |
fb = VM_MAP_FAILED; |
513 |
+ |
} |
514 |
+ |
if (fb == VM_MAP_FAILED) |
515 |
+ |
fb = vm_acquire(size, VM_MAP_DEFAULT | VM_MAP_32BIT); |
516 |
+ |
return fb; |
517 |
+ |
} |
518 |
+ |
|
519 |
+ |
static inline void vm_release_framebuffer(void *fb, uint32 size) |
520 |
+ |
{ |
521 |
+ |
vm_release(fb, size); |
522 |
+ |
} |
523 |
+ |
#endif |
524 |
+ |
|
525 |
+ |
|
526 |
+ |
/* |
527 |
|
* Display "driver" classes |
528 |
|
*/ |
529 |
|
|
584 |
|
int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode) |
585 |
|
}; |
586 |
|
|
587 |
+ |
class driver_dga; |
588 |
+ |
static void update_display_dga_vosf(driver_dga *drv); |
589 |
+ |
|
590 |
+ |
class driver_dga : public driver_base { |
591 |
+ |
friend void update_display_dga_vosf(driver_dga *drv); |
592 |
+ |
|
593 |
+ |
public: |
594 |
+ |
driver_dga(X11_monitor_desc &monitor); |
595 |
+ |
~driver_dga(); |
596 |
+ |
|
597 |
+ |
void suspend(void); |
598 |
+ |
void resume(void); |
599 |
+ |
|
600 |
+ |
protected: |
601 |
+ |
struct FakeXImage { |
602 |
+ |
int width, height; // size of image |
603 |
+ |
int depth; // depth of image |
604 |
+ |
int bytes_per_line; // accelerator to next line |
605 |
+ |
|
606 |
+ |
FakeXImage(int w, int h, int d) |
607 |
+ |
: width(w), height(h), depth(d) |
608 |
+ |
{ bytes_per_line = TrivialBytesPerRow(width, DepthModeForPixelDepth(depth)); } |
609 |
+ |
}; |
610 |
+ |
FakeXImage *img; |
611 |
+ |
|
612 |
+ |
private: |
613 |
+ |
Window suspend_win; // "Suspend" information window |
614 |
+ |
void *fb_save; // Saved frame buffer for suspend/resume |
615 |
+ |
}; |
616 |
+ |
|
617 |
|
static driver_base *drv = NULL; // Pointer to currently used driver object |
618 |
|
|
619 |
|
#ifdef ENABLE_VOSF |
658 |
|
// the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will |
659 |
|
if (the_buffer != VM_MAP_FAILED) { |
660 |
|
D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size)); |
661 |
< |
vm_release(the_buffer, the_buffer_size); |
661 |
> |
vm_release_framebuffer(the_buffer, the_buffer_size); |
662 |
|
the_buffer = NULL; |
663 |
|
} |
664 |
|
if (the_host_buffer) { |
808 |
|
// Allocate memory for frame buffer (SIZE is extended to page-boundary) |
809 |
|
the_host_buffer = the_buffer_copy; |
810 |
|
the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line); |
811 |
< |
the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size); |
811 |
> |
the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size); |
812 |
|
the_buffer_copy = (uint8 *)malloc(the_buffer_size); |
813 |
|
D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer)); |
814 |
|
#else |
829 |
|
XDefineCursor(x_display, w, mac_cursor); |
830 |
|
|
831 |
|
// Init blitting routines |
755 |
– |
bool native_byte_order; |
756 |
– |
#ifdef WORDS_BIGENDIAN |
757 |
– |
native_byte_order = (XImageByteOrder(x_display) == MSBFirst); |
758 |
– |
#else |
759 |
– |
native_byte_order = (XImageByteOrder(x_display) == LSBFirst); |
760 |
– |
#endif |
832 |
|
#ifdef ENABLE_VOSF |
833 |
< |
Screen_blitter_init(visualFormat, native_byte_order, depth_of_video_mode(mode)); |
833 |
> |
Screen_blitter_init(visualFormat, x_native_byte_order, depth_of_video_mode(mode)); |
834 |
|
#endif |
835 |
|
|
836 |
|
// Set frame buffer base |
837 |
< |
set_mac_frame_buffer(monitor, mode.depth, native_byte_order); |
837 |
> |
set_mac_frame_buffer(monitor, mode.depth, x_native_byte_order); |
838 |
|
|
839 |
|
// Everything went well |
840 |
|
init_ok = true; |
949 |
|
* DGA display driver base class |
950 |
|
*/ |
951 |
|
|
881 |
– |
class driver_dga : public driver_base { |
882 |
– |
public: |
883 |
– |
driver_dga(X11_monitor_desc &monitor); |
884 |
– |
~driver_dga(); |
885 |
– |
|
886 |
– |
void suspend(void); |
887 |
– |
void resume(void); |
888 |
– |
|
889 |
– |
private: |
890 |
– |
Window suspend_win; // "Suspend" information window |
891 |
– |
void *fb_save; // Saved frame buffer for suspend/resume |
892 |
– |
}; |
893 |
– |
|
952 |
|
driver_dga::driver_dga(X11_monitor_desc &m) |
953 |
< |
: driver_base(m), suspend_win(0), fb_save(NULL) |
953 |
> |
: driver_base(m), suspend_win(0), fb_save(NULL), img(NULL) |
954 |
|
{ |
955 |
|
} |
956 |
|
|
958 |
|
{ |
959 |
|
XUngrabPointer(x_display, CurrentTime); |
960 |
|
XUngrabKeyboard(x_display, CurrentTime); |
961 |
+ |
|
962 |
+ |
if (img) |
963 |
+ |
delete img; |
964 |
|
} |
965 |
|
|
966 |
|
// Suspend emulation |
1188 |
|
the_host_buffer = the_buffer; |
1189 |
|
the_buffer_size = page_extend((height + 2) * bytes_per_row); |
1190 |
|
the_buffer_copy = (uint8 *)malloc(the_buffer_size); |
1191 |
< |
the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size); |
1191 |
> |
the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size); |
1192 |
> |
|
1193 |
> |
// Fake image for DGA/VOSF mode to know about display bounds |
1194 |
> |
img = new FakeXImage(width, height, depth_of_video_mode(mode)); |
1195 |
|
} |
1196 |
|
#else |
1197 |
|
use_vosf = false; |
1315 |
|
// Init blitting routines |
1316 |
|
int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth); |
1317 |
|
#if ENABLE_VOSF |
1254 |
– |
bool native_byte_order; |
1255 |
– |
#ifdef WORDS_BIGENDIAN |
1256 |
– |
native_byte_order = (XImageByteOrder(x_display) == MSBFirst); |
1257 |
– |
#else |
1258 |
– |
native_byte_order = (XImageByteOrder(x_display) == LSBFirst); |
1259 |
– |
#endif |
1318 |
|
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
1319 |
|
// Screen_blitter_init() returns TRUE if VOSF is mandatory |
1320 |
|
// i.e. the framebuffer update function is not Blit_Copy_Raw |
1321 |
< |
use_vosf = Screen_blitter_init(visualFormat, native_byte_order, depth_of_video_mode(mode)); |
1321 |
> |
use_vosf = Screen_blitter_init(visualFormat, x_native_byte_order, depth_of_video_mode(mode)); |
1322 |
|
|
1323 |
|
if (use_vosf) { |
1324 |
|
// Allocate memory for frame buffer (SIZE is extended to page-boundary) |
1325 |
|
the_host_buffer = the_buffer; |
1326 |
|
the_buffer_size = page_extend((height + 2) * bytes_per_row); |
1327 |
|
the_buffer_copy = (uint8 *)malloc(the_buffer_size); |
1328 |
< |
the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size); |
1328 |
> |
the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size); |
1329 |
> |
|
1330 |
> |
// Fake image for DGA/VOSF mode to know about display bounds |
1331 |
> |
img = new FakeXImage((v_width + 7) & ~7, height, depth_of_video_mode(mode)); |
1332 |
|
} |
1333 |
|
#else |
1334 |
|
use_vosf = false; |
1410 |
|
|
1411 |
|
// Search for server vendor string, then read keycodes |
1412 |
|
const char *vendor = ServerVendor(x_display); |
1413 |
+ |
// Force use of MacX mappings on MacOS X with Apple's X server |
1414 |
+ |
int dummy; |
1415 |
+ |
if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy)) |
1416 |
+ |
vendor = "MacX"; |
1417 |
|
bool vendor_found = false; |
1418 |
|
char line[256]; |
1419 |
|
while (fgets(line, 255, f)) { |
1467 |
|
return false; |
1468 |
|
} |
1469 |
|
|
1470 |
+ |
// Determine the byte order of an XImage content |
1471 |
+ |
#ifdef WORDS_BIGENDIAN |
1472 |
+ |
x_native_byte_order = (XImageByteOrder(x_display) == MSBFirst); |
1473 |
+ |
#else |
1474 |
+ |
x_native_byte_order = (XImageByteOrder(x_display) == LSBFirst); |
1475 |
+ |
#endif |
1476 |
+ |
|
1477 |
|
// Build up visualFormat structure |
1478 |
+ |
visualFormat.fullscreen = (display_type == DISPLAY_DGA); |
1479 |
|
visualFormat.depth = visualInfo.depth; |
1480 |
|
visualFormat.Rmask = visualInfo.red_mask; |
1481 |
|
visualFormat.Gmask = visualInfo.green_mask; |
1541 |
|
// Load gray ramp to 8->16/32 expand map |
1542 |
|
if (!IsDirectMode(mode) && xdepth > 8) |
1543 |
|
for (int i=0; i<256; i++) |
1544 |
< |
ExpandMap[i] = map_rgb(i, i, i); |
1544 |
> |
ExpandMap[i] = map_rgb(i, i, i, true); |
1545 |
|
#endif |
1546 |
|
|
1547 |
|
// Create display driver object of requested type |
1586 |
|
LOCK_FRAME_BUFFER; |
1587 |
|
|
1588 |
|
// Start redraw/input thread |
1589 |
< |
#ifdef HAVE_PTHREADS |
1589 |
> |
#ifdef USE_PTHREADS_SERVICES |
1590 |
|
redraw_thread_cancel = false; |
1591 |
|
Set_pthread_attr(&redraw_thread_attr, 0); |
1592 |
|
redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0); |
1613 |
|
|
1614 |
|
// Check if X server runs on local machine |
1615 |
|
local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0) |
1616 |
+ |
|| (strncmp(XDisplayName(x_display_name), "/", 1) == 0) |
1617 |
|
|| (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0); |
1618 |
|
|
1619 |
|
// Init keycode translation |
1783 |
|
D(bug("video_close()\n")); |
1784 |
|
|
1785 |
|
// Stop redraw thread |
1786 |
< |
#ifdef HAVE_PTHREADS |
1786 |
> |
#ifdef USE_PTHREADS_SERVICES |
1787 |
|
if (redraw_thread_active) { |
1788 |
|
redraw_thread_cancel = true; |
1789 |
< |
#ifdef HAVE_PTHREAD_CANCEL |
1716 |
< |
pthread_cancel(redraw_thread); |
1717 |
< |
#endif |
1789 |
> |
redraw_thread_cancel_ack = false; |
1790 |
|
pthread_join(redraw_thread, NULL); |
1791 |
+ |
while (!redraw_thread_cancel_ack) ; |
1792 |
|
} |
1793 |
|
#endif |
1794 |
|
redraw_thread_active = false; |
1911 |
|
if (!IsDirectMode(mode) && xdepth > 8) { |
1912 |
|
for (int i=0; i<256; i++) { |
1913 |
|
int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier) |
1914 |
< |
ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]); |
1914 |
> |
ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2], true); |
1915 |
|
} |
1916 |
|
|
1917 |
|
// We have to redraw everything because the interpretation of pixel values changed |
2116 |
|
|
2117 |
|
static void handle_events(void) |
2118 |
|
{ |
2119 |
< |
while (XPending(x_display)) { |
2119 |
> |
for (;;) { |
2120 |
|
XEvent event; |
2121 |
< |
XNextEvent(x_display, &event); |
2121 |
> |
XDisplayLock(); |
2122 |
> |
|
2123 |
> |
if (!XCheckMaskEvent(x_display, eventmask, &event)) { |
2124 |
> |
// Handle clipboard events |
2125 |
> |
if (XCheckTypedEvent(x_display, SelectionRequest, &event)) |
2126 |
> |
ClipboardSelectionRequest(&event.xselectionrequest); |
2127 |
> |
else if (XCheckTypedEvent(x_display, SelectionClear, &event)) |
2128 |
> |
ClipboardSelectionClear(&event.xselectionclear); |
2129 |
|
|
2130 |
+ |
// Window "close" widget clicked |
2131 |
+ |
else if (XCheckTypedEvent(x_display, ClientMessage, &event)) { |
2132 |
+ |
if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) { |
2133 |
+ |
ADBKeyDown(0x7f); // Power key |
2134 |
+ |
ADBKeyUp(0x7f); |
2135 |
+ |
} |
2136 |
+ |
} |
2137 |
+ |
XDisplayUnlock(); |
2138 |
+ |
break; |
2139 |
+ |
} |
2140 |
+ |
|
2141 |
|
switch (event.type) { |
2142 |
|
|
2143 |
|
// Mouse button |
2245 |
|
memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y); |
2246 |
|
} |
2247 |
|
break; |
2157 |
– |
|
2158 |
– |
// Window "close" widget clicked |
2159 |
– |
case ClientMessage: |
2160 |
– |
if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) { |
2161 |
– |
ADBKeyDown(0x7f); // Power key |
2162 |
– |
ADBKeyUp(0x7f); |
2163 |
– |
} |
2164 |
– |
break; |
2248 |
|
} |
2249 |
+ |
|
2250 |
+ |
XDisplayUnlock(); |
2251 |
|
} |
2252 |
|
} |
2253 |
|
|
2292 |
|
} |
2293 |
|
} |
2294 |
|
|
2295 |
+ |
XDisplayLock(); |
2296 |
|
if ((nr_boxes <= max_box) && (nr_boxes)) { |
2297 |
|
for (y1=0; y1<16; y1++) { |
2298 |
|
for (x1=0; x1<16; x1++) { |
2346 |
|
} |
2347 |
|
nr_boxes = 0; |
2348 |
|
} |
2349 |
+ |
XDisplayUnlock(); |
2350 |
|
} |
2351 |
|
|
2352 |
|
// Static display update (fixed frame rate, but incremental) |
2456 |
|
} |
2457 |
|
|
2458 |
|
// Refresh display |
2459 |
+ |
XDisplayLock(); |
2460 |
|
if (high && wide) { |
2461 |
|
if (drv->have_shm) |
2462 |
|
XShmPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high, 0); |
2463 |
|
else |
2464 |
|
XPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high); |
2465 |
|
} |
2466 |
+ |
XDisplayUnlock(); |
2467 |
|
} |
2468 |
|
|
2469 |
|
|
2505 |
|
|
2506 |
|
if (x_palette_changed) { |
2507 |
|
x_palette_changed = false; |
2508 |
+ |
XDisplayLock(); |
2509 |
|
drv->update_palette(); |
2510 |
+ |
XDisplayUnlock(); |
2511 |
|
} |
2512 |
|
|
2513 |
|
UNLOCK_PALETTE; |
2532 |
|
tick_counter = 0; |
2533 |
|
if (mainBuffer.dirty) { |
2534 |
|
LOCK_VOSF; |
2535 |
< |
update_display_dga_vosf(); |
2535 |
> |
update_display_dga_vosf(static_cast<driver_dga *>(drv)); |
2536 |
|
UNLOCK_VOSF; |
2537 |
|
} |
2538 |
|
} |
2549 |
|
if (++tick_counter >= frame_skip) { |
2550 |
|
tick_counter = 0; |
2551 |
|
if (mainBuffer.dirty) { |
2552 |
+ |
XDisplayLock(); |
2553 |
|
LOCK_VOSF; |
2554 |
|
update_display_window_vosf(static_cast<driver_window *>(drv)); |
2555 |
|
UNLOCK_VOSF; |
2556 |
|
XSync(x_display, false); // Let the server catch up |
2557 |
+ |
XDisplayUnlock(); |
2558 |
|
} |
2559 |
|
} |
2560 |
|
} |
2634 |
|
const int VIDEO_REFRESH_HZ = 60; |
2635 |
|
const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ; |
2636 |
|
|
2637 |
< |
#ifdef HAVE_PTHREADS |
2637 |
> |
#ifdef USE_PTHREADS_SERVICES |
2638 |
|
static void *redraw_func(void *arg) |
2639 |
|
{ |
2640 |
|
int fd = ConnectionNumber(x_display); |
2675 |
|
} |
2676 |
|
|
2677 |
|
uint64 end = GetTicks_usec(); |
2678 |
< |
D(bug("%Ld refreshes in %Ld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
2678 |
> |
D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
2679 |
> |
|
2680 |
> |
redraw_thread_cancel_ack = true; |
2681 |
|
return NULL; |
2682 |
|
} |
2683 |
|
#endif |