1 |
|
/* |
2 |
|
* video_x.cpp - Video/graphics emulation, X11 specific stuff |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2001 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2005 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 |
39 |
|
|
40 |
|
#include <algorithm> |
41 |
|
|
42 |
– |
#ifndef NO_STD_NAMESPACE |
43 |
– |
using std::sort; |
44 |
– |
#endif |
45 |
– |
|
42 |
|
#ifdef HAVE_PTHREADS |
43 |
|
# include <pthread.h> |
44 |
|
#endif |
62 |
|
#include "prefs.h" |
63 |
|
#include "user_strings.h" |
64 |
|
#include "video.h" |
65 |
+ |
#include "video_blit.h" |
66 |
|
|
67 |
|
#define DEBUG 0 |
68 |
|
#include "debug.h" |
69 |
|
|
70 |
|
|
71 |
+ |
// Supported video modes |
72 |
+ |
static vector<video_mode> VideoModes; |
73 |
+ |
|
74 |
|
// Display types |
75 |
|
enum { |
76 |
|
DISPLAY_WINDOW, // X11 window, using MIT SHM extensions if possible |
97 |
|
|
98 |
|
static bool redraw_thread_active = false; // Flag: Redraw thread installed |
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 |
|
|
124 |
|
static int keycode_table[256]; // X keycode -> Mac keycode translation table |
125 |
|
|
126 |
|
// X11 variables |
127 |
+ |
char *x_display_name = NULL; // X11 display name |
128 |
+ |
Display *x_display = NULL; // X11 display handle |
129 |
|
static int screen; // Screen number |
130 |
|
static Window rootwin; // Root window and our window |
131 |
|
static int num_depths = 0; // Number of available X depths |
135 |
|
static int eventmask; |
136 |
|
|
137 |
|
static int xdepth; // Depth of X screen |
138 |
+ |
static VisualFormat visualFormat; |
139 |
|
static XVisualInfo visualInfo; |
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 palette[256]; // Color palette to be used as CLUT and gamma table |
149 |
< |
static bool palette_changed = false; // Flag: Palette changed, redraw thread must set new colors |
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 |
152 |
|
static int fbdev_fd = -1; |
159 |
|
|
160 |
|
// Mutex to protect palette |
161 |
|
#ifdef HAVE_PTHREADS |
162 |
< |
static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER; |
163 |
< |
#define LOCK_PALETTE pthread_mutex_lock(&palette_lock) |
164 |
< |
#define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock) |
162 |
> |
static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER; |
163 |
> |
#define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock) |
164 |
> |
#define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock) |
165 |
|
#else |
166 |
|
#define LOCK_PALETTE |
167 |
|
#define UNLOCK_PALETTE |
190 |
|
|
191 |
|
// Prototypes |
192 |
|
static void *redraw_func(void *arg); |
187 |
– |
static int event2keycode(XKeyEvent &ev); |
193 |
|
|
194 |
|
// From main_unix.cpp |
195 |
|
extern char *x_display_name; |
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 |
208 |
+ |
*/ |
209 |
+ |
|
210 |
+ |
class X11_monitor_desc : public monitor_desc { |
211 |
+ |
public: |
212 |
+ |
X11_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {} |
213 |
+ |
~X11_monitor_desc() {} |
214 |
+ |
|
215 |
+ |
virtual void switch_to_current_mode(void); |
216 |
+ |
virtual void set_palette(uint8 *pal, int num); |
217 |
+ |
|
218 |
+ |
bool video_open(void); |
219 |
+ |
void video_close(void); |
220 |
+ |
}; |
221 |
+ |
|
222 |
|
|
223 |
|
/* |
224 |
|
* Utility functions |
225 |
|
*/ |
226 |
|
|
227 |
+ |
// Map video_mode depth ID to numerical depth value |
228 |
+ |
static inline int depth_of_video_mode(video_mode const & mode) |
229 |
+ |
{ |
230 |
+ |
int depth = -1; |
231 |
+ |
switch (mode.depth) { |
232 |
+ |
case VDEPTH_1BIT: |
233 |
+ |
depth = 1; |
234 |
+ |
break; |
235 |
+ |
case VDEPTH_2BIT: |
236 |
+ |
depth = 2; |
237 |
+ |
break; |
238 |
+ |
case VDEPTH_4BIT: |
239 |
+ |
depth = 4; |
240 |
+ |
break; |
241 |
+ |
case VDEPTH_8BIT: |
242 |
+ |
depth = 8; |
243 |
+ |
break; |
244 |
+ |
case VDEPTH_16BIT: |
245 |
+ |
depth = 16; |
246 |
+ |
break; |
247 |
+ |
case VDEPTH_32BIT: |
248 |
+ |
depth = 32; |
249 |
+ |
break; |
250 |
+ |
default: |
251 |
+ |
abort(); |
252 |
+ |
} |
253 |
+ |
return depth; |
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 |
280 |
|
{ |
281 |
|
D(bug("have_visual_for_depth(%d)\n", 1 << depth)); |
282 |
|
|
283 |
+ |
// 1-bit works always and uses default visual |
284 |
+ |
if (depth == VDEPTH_1BIT) { |
285 |
+ |
vis = DefaultVisual(x_display, screen); |
286 |
+ |
visualInfo.visualid = XVisualIDFromVisual(vis); |
287 |
+ |
int num = 0; |
288 |
+ |
XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num); |
289 |
+ |
visualInfo = vi[0]; |
290 |
+ |
XFree(vi); |
291 |
+ |
xdepth = visualInfo.depth; |
292 |
+ |
color_class = visualInfo.c_class; |
293 |
+ |
D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth)); |
294 |
+ |
return true; |
295 |
+ |
} |
296 |
+ |
|
297 |
|
// Calculate minimum and maximum supported X depth |
298 |
|
int min_depth = 1, max_depth = 32; |
299 |
|
switch (depth) { |
216 |
– |
case VDEPTH_1BIT: // 1-bit works always and uses default visual |
217 |
– |
min_depth = max_depth = DefaultDepth(x_display, screen); |
218 |
– |
break; |
300 |
|
#ifdef ENABLE_VOSF |
301 |
|
case VDEPTH_2BIT: |
302 |
|
case VDEPTH_4BIT: // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit |
374 |
|
case DirectColor: D(bug("DirectColor\n")); break; |
375 |
|
} |
376 |
|
#endif |
377 |
+ |
return true; |
378 |
|
} |
379 |
|
|
380 |
|
// Add mode to list of supported modes |
402 |
|
} |
403 |
|
|
404 |
|
// Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac) |
405 |
< |
static void set_mac_frame_buffer(video_depth depth, bool native_byte_order) |
405 |
> |
static void set_mac_frame_buffer(X11_monitor_desc &monitor, video_depth depth, bool native_byte_order) |
406 |
|
{ |
407 |
|
#if !REAL_ADDRESSING && !DIRECT_ADDRESSING |
408 |
|
int layout = FLAYOUT_DIRECT; |
414 |
|
MacFrameLayout = layout; |
415 |
|
else |
416 |
|
MacFrameLayout = FLAYOUT_DIRECT; |
417 |
< |
VideoMonitor.mac_frame_base = MacFrameBaseMac; |
417 |
> |
monitor.set_mac_frame_base(MacFrameBaseMac); |
418 |
|
|
419 |
|
// Set variables used by UAE memory banking |
420 |
+ |
const video_mode &mode = monitor.get_current_mode(); |
421 |
|
MacFrameBaseHost = the_buffer; |
422 |
< |
MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y; |
422 |
> |
MacFrameSize = mode.bytes_per_row * mode.y; |
423 |
|
InitFrameBufferMapping(); |
424 |
|
#else |
425 |
< |
VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer); |
425 |
> |
monitor.set_mac_frame_base(Host2MacAddr(the_buffer)); |
426 |
|
#endif |
427 |
< |
D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base)); |
427 |
> |
D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base())); |
428 |
|
} |
429 |
|
|
430 |
|
// Set window name and class |
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 |
|
|
530 |
|
class driver_base { |
531 |
|
public: |
532 |
< |
driver_base(); |
532 |
> |
driver_base(X11_monitor_desc &m); |
533 |
|
virtual ~driver_base(); |
534 |
|
|
535 |
|
virtual void update_palette(void); |
545 |
|
virtual void ungrab_mouse(void) {} |
546 |
|
|
547 |
|
public: |
548 |
+ |
X11_monitor_desc &monitor; // Associated video monitor |
549 |
+ |
const video_mode &mode; // Video mode handled by the driver |
550 |
+ |
|
551 |
|
bool init_ok; // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer) |
552 |
|
Window w; // The window we draw into |
553 |
|
|
565 |
|
friend void update_display_static(driver_window *drv); |
566 |
|
|
567 |
|
public: |
568 |
< |
driver_window(const video_mode &mode); |
568 |
> |
driver_window(X11_monitor_desc &monitor); |
569 |
|
~driver_window(); |
570 |
|
|
571 |
|
void toggle_mouse_grab(void); |
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 |
620 |
|
# include "video_vosf.h" |
621 |
|
#endif |
622 |
|
|
623 |
< |
driver_base::driver_base() |
624 |
< |
: init_ok(false), w(0) |
623 |
> |
driver_base::driver_base(X11_monitor_desc &m) |
624 |
> |
: monitor(m), mode(m.get_current_mode()), init_ok(false), w(0) |
625 |
|
{ |
626 |
|
the_buffer = NULL; |
627 |
|
the_buffer_copy = NULL; |
655 |
|
} |
656 |
|
#ifdef ENABLE_VOSF |
657 |
|
else { |
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_framebuffer(the_buffer, the_buffer_size); |
662 |
+ |
the_buffer = NULL; |
663 |
+ |
} |
664 |
|
if (the_host_buffer) { |
665 |
+ |
D(bug(" freeing the_host_buffer at %p\n", the_host_buffer)); |
666 |
|
free(the_host_buffer); |
667 |
|
the_host_buffer = NULL; |
668 |
|
} |
519 |
– |
if (the_buffer) { |
520 |
– |
free(the_buffer); |
521 |
– |
the_buffer = NULL; |
522 |
– |
} |
669 |
|
if (the_buffer_copy) { |
670 |
+ |
D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy)); |
671 |
|
free(the_buffer_copy); |
672 |
|
the_buffer_copy = NULL; |
673 |
|
} |
680 |
|
{ |
681 |
|
if (color_class == PseudoColor || color_class == DirectColor) { |
682 |
|
int num = vis->map_entries; |
683 |
< |
if (!IsDirectMode(VideoMonitor.mode) && color_class == DirectColor) |
683 |
> |
if (!IsDirectMode(monitor.get_current_mode()) && color_class == DirectColor) |
684 |
|
return; // Indexed mode on true color screen, don't set CLUT |
685 |
< |
XStoreColors(x_display, cmap[0], palette, num); |
686 |
< |
XStoreColors(x_display, cmap[1], palette, num); |
685 |
> |
XStoreColors(x_display, cmap[0], x_palette, num); |
686 |
> |
XStoreColors(x_display, cmap[1], x_palette, num); |
687 |
|
} |
688 |
|
XSync(x_display, false); |
689 |
|
} |
706 |
|
*/ |
707 |
|
|
708 |
|
// Open display |
709 |
< |
driver_window::driver_window(const video_mode &mode) |
710 |
< |
: gc(0), img(NULL), have_shm(false), mouse_grabbed(false), mac_cursor(0) |
709 |
> |
driver_window::driver_window(X11_monitor_desc &m) |
710 |
> |
: driver_base(m), gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false) |
711 |
|
{ |
712 |
|
int width = mode.x, height = mode.y; |
713 |
|
int aligned_width = (width + 15) & ~15; |
716 |
|
// Set absolute mouse mode |
717 |
|
ADBSetRelMouseMode(mouse_grabbed); |
718 |
|
|
719 |
< |
// Create window (setting backround_pixel, border_pixel and colormap is |
719 |
> |
// Create window (setting background_pixel, border_pixel and colormap is |
720 |
|
// mandatory when using a non-default visual; in 1-bit mode we use the |
721 |
|
// default visual, so we can also use the default colormap) |
722 |
|
XSetWindowAttributes wattr; |
726 |
|
wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]); |
727 |
|
w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, |
728 |
|
InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWColormap, &wattr); |
729 |
+ |
D(bug(" window created\n")); |
730 |
|
|
731 |
|
// Set window name/class |
732 |
|
set_window_name(w, STR_WINDOW_TITLE); |
750 |
|
XFree(hints); |
751 |
|
} |
752 |
|
} |
753 |
+ |
D(bug(" window attributes set\n")); |
754 |
|
|
755 |
|
// Show window |
756 |
|
XMapWindow(x_display, w); |
757 |
|
wait_mapped(w); |
758 |
+ |
D(bug(" window mapped\n")); |
759 |
|
|
760 |
|
// 1-bit mode is big-endian; if the X server is little-endian, we can't |
761 |
|
// use SHM because that doesn't allow changing the image byte order |
766 |
|
|
767 |
|
// Create SHM image ("height + 2" for safety) |
768 |
|
img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height); |
769 |
+ |
D(bug(" shm image created\n")); |
770 |
|
shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777); |
771 |
|
the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0); |
772 |
|
shminfo.shmaddr = img->data = (char *)the_buffer_copy; |
787 |
|
have_shm = true; |
788 |
|
shmctl(shminfo.shmid, IPC_RMID, 0); |
789 |
|
} |
790 |
+ |
D(bug(" shm image attached\n")); |
791 |
|
} |
792 |
|
|
793 |
|
// Create normal X image if SHM doesn't work ("height + 2" for safety) |
795 |
|
int bytes_per_row = (mode.depth == VDEPTH_1BIT ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth))); |
796 |
|
the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row); |
797 |
|
img = XCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row); |
798 |
+ |
D(bug(" X image created\n")); |
799 |
|
} |
800 |
|
|
801 |
|
if (need_msb_image) { |
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_copy = (uint8 *)vm_acquire(the_buffer_size); |
812 |
< |
the_buffer = (uint8 *)vm_acquire(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 |
815 |
|
// Allocate memory for frame buffer |
829 |
|
XDefineCursor(x_display, w, mac_cursor); |
830 |
|
|
831 |
|
// Init blitting routines |
679 |
– |
bool native_byte_order; |
680 |
– |
#ifdef WORDS_BIGENDIAN |
681 |
– |
native_byte_order = (XImageByteOrder(x_display) == MSBFirst); |
682 |
– |
#else |
683 |
– |
native_byte_order = (XImageByteOrder(x_display) == LSBFirst); |
684 |
– |
#endif |
832 |
|
#ifdef ENABLE_VOSF |
833 |
< |
Screen_blitter_init(&visualInfo, native_byte_order, mode.depth); |
833 |
> |
Screen_blitter_init(visualFormat, x_native_byte_order, depth_of_video_mode(mode)); |
834 |
|
#endif |
835 |
|
|
836 |
< |
// Set VideoMonitor |
837 |
< |
VideoMonitor.mode = mode; |
691 |
< |
set_mac_frame_buffer(mode.depth, native_byte_order); |
836 |
> |
// Set frame buffer base |
837 |
> |
set_mac_frame_buffer(monitor, mode.depth, x_native_byte_order); |
838 |
|
|
839 |
|
// Everything went well |
840 |
|
init_ok = true; |
851 |
|
the_buffer_copy = NULL; // don't free() in driver_base dtor |
852 |
|
#endif |
853 |
|
} |
854 |
< |
#ifdef ENABLE_VOSF |
855 |
< |
if (use_vosf) { |
856 |
< |
// don't free() memory mapped buffers in driver_base dtor |
711 |
< |
if (the_buffer != VM_MAP_FAILED) { |
712 |
< |
vm_release(the_buffer, the_buffer_size); |
713 |
< |
the_buffer = NULL; |
714 |
< |
} |
715 |
< |
if (the_buffer_copy != VM_MAP_FAILED) { |
716 |
< |
vm_release(the_buffer_copy, the_buffer_size); |
717 |
< |
the_buffer_copy = NULL; |
718 |
< |
} |
719 |
< |
} |
720 |
< |
#endif |
721 |
< |
if (img) |
854 |
> |
if (img) { |
855 |
> |
if (!have_shm) |
856 |
> |
img->data = NULL; |
857 |
|
XDestroyImage(img); |
858 |
+ |
} |
859 |
|
if (have_shm) { |
860 |
|
shmdt(shminfo.shmaddr); |
861 |
|
shmctl(shminfo.shmid, IPC_RMID, 0); |
914 |
|
// Warped mouse motion (this code is taken from SDL) |
915 |
|
|
916 |
|
// Post first mouse event |
917 |
< |
int width = VideoMonitor.mode.x, height = VideoMonitor.mode.y; |
917 |
> |
int width = monitor.get_current_mode().x, height = monitor.get_current_mode().y; |
918 |
|
int delta_x = x - mouse_last_x, delta_y = y - mouse_last_y; |
919 |
|
mouse_last_x = x; mouse_last_y = y; |
920 |
|
ADBMouseMoved(delta_x, delta_y); |
949 |
|
* DGA display driver base class |
950 |
|
*/ |
951 |
|
|
952 |
< |
class driver_dga : public driver_base { |
953 |
< |
public: |
818 |
< |
driver_dga(); |
819 |
< |
~driver_dga(); |
820 |
< |
|
821 |
< |
void suspend(void); |
822 |
< |
void resume(void); |
823 |
< |
|
824 |
< |
private: |
825 |
< |
Window suspend_win; // "Suspend" information window |
826 |
< |
void *fb_save; // Saved frame buffer for suspend/resume |
827 |
< |
}; |
828 |
< |
|
829 |
< |
driver_dga::driver_dga() |
830 |
< |
: suspend_win(0), fb_save(NULL) |
952 |
> |
driver_dga::driver_dga(X11_monitor_desc &m) |
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 |
974 |
|
LOCK_FRAME_BUFFER; |
975 |
|
|
976 |
|
// Save frame buffer |
977 |
< |
fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row); |
977 |
> |
fb_save = malloc(mode.y * mode.bytes_per_row); |
978 |
|
if (fb_save) |
979 |
< |
memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row); |
979 |
> |
memcpy(fb_save, the_buffer, mode.y * mode.bytes_per_row); |
980 |
|
|
981 |
|
// Close full screen display |
982 |
|
#ifdef ENABLE_XF86_DGA |
1029 |
|
LOCK_VOSF; |
1030 |
|
PFLAG_SET_ALL; |
1031 |
|
UNLOCK_VOSF; |
1032 |
< |
memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); |
1032 |
> |
memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y); |
1033 |
|
} |
1034 |
|
#endif |
1035 |
|
|
1039 |
|
// Don't copy fb_save to the temporary frame buffer in VOSF mode |
1040 |
|
if (!use_vosf) |
1041 |
|
#endif |
1042 |
< |
memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row); |
1042 |
> |
memcpy(the_buffer, fb_save, mode.y * mode.bytes_per_row); |
1043 |
|
free(fb_save); |
1044 |
|
fb_save = NULL; |
1045 |
|
} |
1061 |
|
|
1062 |
|
class driver_fbdev : public driver_dga { |
1063 |
|
public: |
1064 |
< |
driver_fbdev(const video_mode &mode); |
1064 |
> |
driver_fbdev(X11_monitor_desc &monitor); |
1065 |
|
~driver_fbdev(); |
1066 |
|
}; |
1067 |
|
|
1068 |
|
// Open display |
1069 |
< |
driver_fbdev::driver_fbdev(const video_mode &mode) |
1069 |
> |
driver_fbdev::driver_fbdev(X11_monitor_desc &m) : driver_dga(m) |
1070 |
|
{ |
1071 |
|
int width = mode.x, height = mode.y; |
1072 |
|
|
1114 |
|
if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0')) |
1115 |
|
continue; |
1116 |
|
|
1117 |
< |
if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3) |
1117 |
> |
if ((sscanf(line, "%19s %d %x", fb_name, &fb_depth, &fb_offset) == 3) |
1118 |
|
&& (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) { |
1119 |
|
device_found = true; |
1120 |
|
break; |
1181 |
|
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
1182 |
|
// Screen_blitter_init() returns TRUE if VOSF is mandatory |
1183 |
|
// i.e. the framebuffer update function is not Blit_Copy_Raw |
1184 |
< |
use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth); |
1184 |
> |
use_vosf = Screen_blitter_init(visualFormat, true, mode.depth); |
1185 |
|
|
1186 |
|
if (use_vosf) { |
1187 |
|
// Allocate memory for frame buffer (SIZE is extended to page-boundary) |
1188 |
|
the_host_buffer = the_buffer; |
1189 |
|
the_buffer_size = page_extend((height + 2) * bytes_per_row); |
1190 |
< |
the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size); |
1191 |
< |
the_buffer = (uint8 *)vm_acquire(the_buffer_size); |
1190 |
> |
the_buffer_copy = (uint8 *)malloc(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; |
1198 |
|
#endif |
1199 |
|
#endif |
1200 |
|
|
1201 |
< |
// Set VideoMonitor |
1202 |
< |
VideoModes[0].bytes_per_row = bytes_per_row; |
1203 |
< |
VideoModes[0].depth = DepthModeForPixelDepth(fb_depth); |
1204 |
< |
VideoMonitor.mode = mode; |
1076 |
< |
set_mac_frame_buffer(mode.depth, true); |
1201 |
> |
// Set frame buffer base |
1202 |
> |
const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row; |
1203 |
> |
const_cast<video_mode *>(&mode)->depth = DepthModeForPixelDepth(fb_depth); |
1204 |
> |
set_mac_frame_buffer(monitor, mode.depth, true); |
1205 |
|
|
1206 |
|
// Everything went well |
1207 |
|
init_ok = true; |
1224 |
|
munmap(the_host_buffer, the_buffer_size); |
1225 |
|
the_host_buffer = NULL; |
1226 |
|
} |
1099 |
– |
if (the_buffer_copy != VM_MAP_FAILED) { |
1100 |
– |
vm_release(the_buffer_copy, the_buffer_size); |
1101 |
– |
the_buffer_copy = NULL; |
1102 |
– |
} |
1103 |
– |
if (the_buffer != VM_MAP_FAILED) { |
1104 |
– |
vm_release(the_buffer, the_buffer_size); |
1105 |
– |
the_buffer = NULL; |
1106 |
– |
} |
1227 |
|
} |
1228 |
|
#endif |
1229 |
|
} |
1237 |
|
|
1238 |
|
class driver_xf86dga : public driver_dga { |
1239 |
|
public: |
1240 |
< |
driver_xf86dga(const video_mode &mode); |
1240 |
> |
driver_xf86dga(X11_monitor_desc &monitor); |
1241 |
|
~driver_xf86dga(); |
1242 |
|
|
1243 |
|
void update_palette(void); |
1248 |
|
}; |
1249 |
|
|
1250 |
|
// Open display |
1251 |
< |
driver_xf86dga::driver_xf86dga(const video_mode &mode) |
1252 |
< |
: current_dga_cmap(0) |
1251 |
> |
driver_xf86dga::driver_xf86dga(X11_monitor_desc &m) |
1252 |
> |
: driver_dga(m), current_dga_cmap(0) |
1253 |
|
{ |
1254 |
|
int width = mode.x, height = mode.y; |
1255 |
|
|
1276 |
|
XSetWindowAttributes wattr; |
1277 |
|
wattr.event_mask = eventmask = dga_eventmask; |
1278 |
|
wattr.override_redirect = True; |
1279 |
+ |
wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]); |
1280 |
|
|
1281 |
|
w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, |
1282 |
< |
InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr); |
1282 |
> |
InputOutput, vis, CWEventMask | CWOverrideRedirect | |
1283 |
> |
(color_class == DirectColor ? CWColormap : 0), &wattr); |
1284 |
|
|
1285 |
|
// Set window name/class |
1286 |
|
set_window_name(w, STR_WINDOW_TITLE); |
1314 |
|
|
1315 |
|
// Init blitting routines |
1316 |
|
int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth); |
1317 |
< |
#if VIDEO_VOSF |
1317 |
> |
#if ENABLE_VOSF |
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(&visualInfo, true, mode.depth); |
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 *)vm_acquire(the_buffer_size); |
1328 |
< |
the_buffer = (uint8 *)vm_acquire(the_buffer_size); |
1327 |
> |
the_buffer_copy = (uint8 *)malloc(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; |
1335 |
|
#endif |
1336 |
|
#endif |
1337 |
|
|
1338 |
< |
// Set VideoMonitor |
1338 |
> |
// Set frame buffer base |
1339 |
|
const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row; |
1340 |
< |
VideoMonitor.mode = mode; |
1216 |
< |
set_mac_frame_buffer(mode.depth, true); |
1340 |
> |
set_mac_frame_buffer(monitor, mode.depth, true); |
1341 |
|
|
1342 |
|
// Everything went well |
1343 |
|
init_ok = true; |
1355 |
|
else { |
1356 |
|
// don't free() the screen buffer in driver_base dtor |
1357 |
|
the_host_buffer = NULL; |
1234 |
– |
|
1235 |
– |
if (the_buffer_copy != VM_MAP_FAILED) { |
1236 |
– |
vm_release(the_buffer_copy, the_buffer_size); |
1237 |
– |
the_buffer_copy = NULL; |
1238 |
– |
} |
1239 |
– |
if (the_buffer != VM_MAP_FAILED) { |
1240 |
– |
vm_release(the_buffer, the_buffer_size); |
1241 |
– |
the_buffer = NULL; |
1242 |
– |
} |
1358 |
|
} |
1359 |
|
#endif |
1360 |
|
#ifdef ENABLE_XF86_VIDMODE |
1368 |
|
{ |
1369 |
|
driver_dga::update_palette(); |
1370 |
|
current_dga_cmap ^= 1; |
1371 |
< |
if (!IsDirectMode(VideoMonitor.mode) && cmap[current_dga_cmap]) |
1371 |
> |
if (!IsDirectMode(monitor.get_current_mode()) && cmap[current_dga_cmap]) |
1372 |
|
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
1373 |
|
} |
1374 |
|
|
1376 |
|
void driver_xf86dga::resume(void) |
1377 |
|
{ |
1378 |
|
driver_dga::resume(); |
1379 |
< |
if (!IsDirectMode(VideoMonitor.mode)) |
1379 |
> |
if (!IsDirectMode(monitor.get_current_mode())) |
1380 |
|
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
1381 |
|
} |
1382 |
|
#endif |
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)) { |
1455 |
|
} |
1456 |
|
} |
1457 |
|
|
1458 |
< |
// Open display for specified mode |
1459 |
< |
static bool video_open(const video_mode &mode) |
1458 |
> |
// Open display for current mode |
1459 |
> |
bool X11_monitor_desc::video_open(void) |
1460 |
|
{ |
1461 |
+ |
D(bug("video_open()\n")); |
1462 |
+ |
const video_mode &mode = get_current_mode(); |
1463 |
+ |
|
1464 |
|
// Find best available X visual |
1465 |
|
if (!find_visual_for_depth(mode.depth)) { |
1466 |
|
ErrorAlert(STR_NO_XVISUAL_ERR); |
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; |
1482 |
+ |
visualFormat.Bmask = visualInfo.blue_mask; |
1483 |
+ |
|
1484 |
|
// Create color maps |
1485 |
|
if (color_class == PseudoColor || color_class == DirectColor) { |
1486 |
|
cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll); |
1514 |
|
int num = vis->map_entries; |
1515 |
|
for (int i=0; i<num; i++) { |
1516 |
|
int c = (i * 256) / num; |
1517 |
< |
palette[i].pixel = map_rgb(c, c, c); |
1518 |
< |
palette[i].flags = DoRed | DoGreen | DoBlue; |
1517 |
> |
x_palette[i].pixel = map_rgb(c, c, c); |
1518 |
> |
x_palette[i].flags = DoRed | DoGreen | DoBlue; |
1519 |
|
} |
1520 |
|
} else if (color_class == PseudoColor) { |
1521 |
|
for (int i=0; i<256; i++) { |
1522 |
< |
palette[i].pixel = i; |
1523 |
< |
palette[i].flags = DoRed | DoGreen | DoBlue; |
1522 |
> |
x_palette[i].pixel = i; |
1523 |
> |
x_palette[i].flags = DoRed | DoGreen | DoBlue; |
1524 |
|
} |
1525 |
|
} |
1526 |
|
|
1528 |
|
int num = (color_class == DirectColor ? vis->map_entries : 256); |
1529 |
|
for (int i=0; i<num; i++) { |
1530 |
|
int c = (i * 256) / num; |
1531 |
< |
palette[i].red = c * 0x0101; |
1532 |
< |
palette[i].green = c * 0x0101; |
1533 |
< |
palette[i].blue = c * 0x0101; |
1531 |
> |
x_palette[i].red = c * 0x0101; |
1532 |
> |
x_palette[i].green = c * 0x0101; |
1533 |
> |
x_palette[i].blue = c * 0x0101; |
1534 |
|
} |
1535 |
|
if (color_class == PseudoColor || color_class == DirectColor) { |
1536 |
< |
XStoreColors(x_display, cmap[0], palette, num); |
1537 |
< |
XStoreColors(x_display, cmap[1], palette, num); |
1536 |
> |
XStoreColors(x_display, cmap[0], x_palette, num); |
1537 |
> |
XStoreColors(x_display, cmap[1], x_palette, num); |
1538 |
|
} |
1539 |
|
|
1540 |
|
#ifdef ENABLE_VOSF |
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 |
1548 |
|
switch (display_type) { |
1549 |
|
case DISPLAY_WINDOW: |
1550 |
< |
drv = new driver_window(mode); |
1550 |
> |
drv = new driver_window(*this); |
1551 |
|
break; |
1552 |
|
#ifdef ENABLE_FBDEV_DGA |
1553 |
|
case DISPLAY_DGA: |
1554 |
< |
drv = new driver_fbdev(mode); |
1554 |
> |
drv = new driver_fbdev(*this); |
1555 |
|
break; |
1556 |
|
#endif |
1557 |
|
#ifdef ENABLE_XF86_DGA |
1558 |
|
case DISPLAY_DGA: |
1559 |
< |
drv = new driver_xf86dga(mode); |
1559 |
> |
drv = new driver_xf86dga(*this); |
1560 |
|
break; |
1561 |
|
#endif |
1562 |
|
} |
1571 |
|
#ifdef ENABLE_VOSF |
1572 |
|
if (use_vosf) { |
1573 |
|
// Initialize the VOSF system |
1574 |
< |
if (!video_vosf_init()) { |
1574 |
> |
if (!video_vosf_init(*this)) { |
1575 |
|
ErrorAlert(STR_VOSF_INIT_ERR); |
1576 |
|
return false; |
1577 |
|
} |
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 |
< |
redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0); |
1591 |
> |
Set_pthread_attr(&redraw_thread_attr, 0); |
1592 |
> |
redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0); |
1593 |
|
if (!redraw_thread_active) { |
1594 |
|
printf("FATAL: cannot create redraw thread\n"); |
1595 |
|
return false; |
1633 |
|
ErrorAlert(STR_UNSUPP_DEPTH_ERR); |
1634 |
|
return false; |
1635 |
|
} |
1636 |
< |
sort(avail_depths, avail_depths + num_depths); |
1636 |
> |
std::sort(avail_depths, avail_depths + num_depths); |
1637 |
|
|
1638 |
|
#ifdef ENABLE_FBDEV_DGA |
1639 |
|
// Frame buffer name |
1736 |
|
ErrorAlert(STR_NO_XVISUAL_ERR); |
1737 |
|
return false; |
1738 |
|
} |
1739 |
< |
video_init_depth_list(); |
1739 |
> |
|
1740 |
> |
// Find requested default mode with specified dimensions |
1741 |
> |
uint32 default_id; |
1742 |
> |
std::vector<video_mode>::const_iterator i, end = VideoModes.end(); |
1743 |
> |
for (i = VideoModes.begin(); i != end; ++i) { |
1744 |
> |
if (i->x == default_width && i->y == default_height && i->depth == default_depth) { |
1745 |
> |
default_id = i->resolution_id; |
1746 |
> |
break; |
1747 |
> |
} |
1748 |
> |
} |
1749 |
> |
if (i == end) { // not found, use first available mode |
1750 |
> |
default_depth = VideoModes[0].depth; |
1751 |
> |
default_id = VideoModes[0].resolution_id; |
1752 |
> |
} |
1753 |
|
|
1754 |
|
#if DEBUG |
1755 |
|
D(bug("Available video modes:\n")); |
1756 |
< |
vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end(); |
1607 |
< |
while (i != end) { |
1756 |
> |
for (i = VideoModes.begin(); i != end; ++i) { |
1757 |
|
int bits = 1 << i->depth; |
1758 |
|
if (bits == 16) |
1759 |
|
bits = 15; |
1760 |
|
else if (bits == 32) |
1761 |
|
bits = 24; |
1762 |
|
D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits)); |
1614 |
– |
++i; |
1763 |
|
} |
1764 |
|
#endif |
1765 |
|
|
1766 |
< |
// Find requested default mode and open display |
1767 |
< |
if (VideoModes.size() == 1) |
1768 |
< |
return video_open(VideoModes[0]); |
1769 |
< |
else { |
1770 |
< |
// Find mode with specified dimensions |
1771 |
< |
std::vector<video_mode>::const_iterator i, end = VideoModes.end(); |
1624 |
< |
for (i = VideoModes.begin(); i != end; ++i) { |
1625 |
< |
if (i->x == default_width && i->y == default_height && i->depth == default_depth) |
1626 |
< |
return video_open(*i); |
1627 |
< |
} |
1628 |
< |
return video_open(VideoModes[0]); |
1629 |
< |
} |
1766 |
> |
// Create X11_monitor_desc for this (the only) display |
1767 |
> |
X11_monitor_desc *monitor = new X11_monitor_desc(VideoModes, default_depth, default_id); |
1768 |
> |
VideoMonitors.push_back(monitor); |
1769 |
> |
|
1770 |
> |
// Open display |
1771 |
> |
return monitor->video_open(); |
1772 |
|
} |
1773 |
|
|
1774 |
|
|
1777 |
|
*/ |
1778 |
|
|
1779 |
|
// Close display |
1780 |
< |
static void video_close(void) |
1780 |
> |
void X11_monitor_desc::video_close(void) |
1781 |
|
{ |
1782 |
+ |
D(bug("video_close()\n")); |
1783 |
+ |
|
1784 |
|
// Stop redraw thread |
1785 |
< |
#ifdef HAVE_PTHREADS |
1785 |
> |
#ifdef USE_PTHREADS_SERVICES |
1786 |
|
if (redraw_thread_active) { |
1787 |
|
redraw_thread_cancel = true; |
1788 |
< |
#ifdef HAVE_PTHREAD_CANCEL |
1645 |
< |
pthread_cancel(redraw_thread); |
1646 |
< |
#endif |
1788 |
> |
redraw_thread_cancel_ack = false; |
1789 |
|
pthread_join(redraw_thread, NULL); |
1790 |
+ |
while (!redraw_thread_cancel_ack) ; |
1791 |
|
} |
1792 |
|
#endif |
1793 |
|
redraw_thread_active = false; |
1795 |
|
// Unlock frame buffer |
1796 |
|
UNLOCK_FRAME_BUFFER; |
1797 |
|
XSync(x_display, false); |
1798 |
+ |
D(bug(" frame buffer unlocked\n")); |
1799 |
|
|
1800 |
|
#ifdef ENABLE_VOSF |
1801 |
|
if (use_vosf) { |
1821 |
|
|
1822 |
|
void VideoExit(void) |
1823 |
|
{ |
1824 |
< |
// Close display |
1825 |
< |
video_close(); |
1824 |
> |
// Close displays |
1825 |
> |
vector<monitor_desc *>::iterator i, end = VideoMonitors.end(); |
1826 |
> |
for (i = VideoMonitors.begin(); i != end; ++i) |
1827 |
> |
dynamic_cast<X11_monitor_desc *>(*i)->video_close(); |
1828 |
|
|
1829 |
|
#ifdef ENABLE_XF86_VIDMODE |
1830 |
|
// Free video mode list |
1882 |
|
* Set palette |
1883 |
|
*/ |
1884 |
|
|
1885 |
< |
void video_set_palette(uint8 *pal, int num_in) |
1885 |
> |
void X11_monitor_desc::set_palette(uint8 *pal, int num_in) |
1886 |
|
{ |
1887 |
+ |
const video_mode &mode = get_current_mode(); |
1888 |
+ |
|
1889 |
|
LOCK_PALETTE; |
1890 |
|
|
1891 |
|
// Convert colors to XColor array |
1892 |
|
int num_out = 256; |
1893 |
|
bool stretch = false; |
1894 |
< |
if (IsDirectMode(VideoMonitor.mode)) { |
1894 |
> |
if (IsDirectMode(mode)) { |
1895 |
|
// If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries |
1896 |
|
num_out = vis->map_entries; |
1897 |
|
stretch = true; |
1898 |
|
} |
1899 |
< |
XColor *p = palette; |
1899 |
> |
XColor *p = x_palette; |
1900 |
|
for (int i=0; i<num_out; i++) { |
1901 |
|
int c = (stretch ? (i * num_in) / num_out : i); |
1902 |
|
p->red = pal[c*3 + 0] * 0x0101; |
1907 |
|
|
1908 |
|
#ifdef ENABLE_VOSF |
1909 |
|
// Recalculate pixel color expansion map |
1910 |
< |
if (!IsDirectMode(VideoMonitor.mode) && xdepth > 8) { |
1910 |
> |
if (!IsDirectMode(mode) && xdepth > 8) { |
1911 |
|
for (int i=0; i<256; i++) { |
1912 |
|
int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier) |
1913 |
< |
ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]); |
1913 |
> |
ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2], true); |
1914 |
|
} |
1915 |
|
|
1916 |
|
// We have to redraw everything because the interpretation of pixel values changed |
1917 |
|
LOCK_VOSF; |
1918 |
|
PFLAG_SET_ALL; |
1919 |
|
UNLOCK_VOSF; |
1920 |
< |
memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); |
1920 |
> |
memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y); |
1921 |
|
} |
1922 |
|
#endif |
1923 |
|
|
1924 |
|
// Tell redraw thread to change palette |
1925 |
< |
palette_changed = true; |
1925 |
> |
x_palette_changed = true; |
1926 |
|
|
1927 |
|
UNLOCK_PALETTE; |
1928 |
|
} |
1932 |
|
* Switch video mode |
1933 |
|
*/ |
1934 |
|
|
1935 |
< |
void video_switch_to_mode(const video_mode &mode) |
1935 |
> |
void X11_monitor_desc::switch_to_current_mode(void) |
1936 |
|
{ |
1937 |
|
// Close and reopen display |
1938 |
|
video_close(); |
1939 |
< |
video_open(mode); |
1939 |
> |
video_open(); |
1940 |
|
|
1941 |
|
if (drv == NULL) { |
1942 |
|
ErrorAlert(STR_OPEN_WINDOW_ERR); |
2115 |
|
|
2116 |
|
static void handle_events(void) |
2117 |
|
{ |
2118 |
< |
while (XPending(x_display)) { |
2118 |
> |
for (;;) { |
2119 |
|
XEvent event; |
2120 |
< |
XNextEvent(x_display, &event); |
2120 |
> |
XDisplayLock(); |
2121 |
|
|
2122 |
+ |
if (!XCheckMaskEvent(x_display, eventmask, &event)) { |
2123 |
+ |
// Handle clipboard events |
2124 |
+ |
if (XCheckTypedEvent(x_display, SelectionRequest, &event)) |
2125 |
+ |
ClipboardSelectionRequest(&event.xselectionrequest); |
2126 |
+ |
else if (XCheckTypedEvent(x_display, SelectionClear, &event)) |
2127 |
+ |
ClipboardSelectionClear(&event.xselectionclear); |
2128 |
+ |
|
2129 |
+ |
// Window "close" widget clicked |
2130 |
+ |
else if (XCheckTypedEvent(x_display, ClientMessage, &event)) { |
2131 |
+ |
if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) { |
2132 |
+ |
ADBKeyDown(0x7f); // Power key |
2133 |
+ |
ADBKeyUp(0x7f); |
2134 |
+ |
} |
2135 |
+ |
} |
2136 |
+ |
XDisplayUnlock(); |
2137 |
+ |
break; |
2138 |
+ |
} |
2139 |
+ |
|
2140 |
|
switch (event.type) { |
2141 |
|
|
2142 |
|
// Mouse button |
2224 |
|
// Hidden parts exposed, force complete refresh of window |
2225 |
|
case Expose: |
2226 |
|
if (display_type == DISPLAY_WINDOW) { |
2227 |
+ |
const video_mode &mode = VideoMonitors[0]->get_current_mode(); |
2228 |
|
#ifdef ENABLE_VOSF |
2229 |
|
if (use_vosf) { // VOSF refresh |
2230 |
|
LOCK_VOSF; |
2231 |
|
PFLAG_SET_ALL; |
2232 |
|
UNLOCK_VOSF; |
2233 |
< |
memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); |
2233 |
> |
memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y); |
2234 |
|
} |
2235 |
|
else |
2236 |
|
#endif |
2241 |
|
updt_box[x1][y1] = true; |
2242 |
|
nr_boxes = 16 * 16; |
2243 |
|
} else // Static refresh |
2244 |
< |
memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); |
2078 |
< |
} |
2079 |
< |
break; |
2080 |
< |
|
2081 |
< |
// Window "close" widget clicked |
2082 |
< |
case ClientMessage: |
2083 |
< |
if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) { |
2084 |
< |
ADBKeyDown(0x7f); // Power key |
2085 |
< |
ADBKeyUp(0x7f); |
2244 |
> |
memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y); |
2245 |
|
} |
2246 |
|
break; |
2247 |
|
} |
2248 |
+ |
|
2249 |
+ |
XDisplayUnlock(); |
2250 |
|
} |
2251 |
|
} |
2252 |
|
|
2261 |
|
int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi; |
2262 |
|
int xil = 0; |
2263 |
|
int rxm = 0, rxmo = 0; |
2264 |
< |
int bytes_per_row = VideoMonitor.mode.bytes_per_row; |
2265 |
< |
int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x; |
2266 |
< |
int rx = VideoMonitor.mode.bytes_per_row / 16; |
2267 |
< |
int ry = VideoMonitor.mode.y / 16; |
2264 |
> |
const video_mode &mode = drv->monitor.get_current_mode(); |
2265 |
> |
int bytes_per_row = mode.bytes_per_row; |
2266 |
> |
int bytes_per_pixel = mode.bytes_per_row / mode.x; |
2267 |
> |
int rx = mode.bytes_per_row / 16; |
2268 |
> |
int ry = mode.y / 16; |
2269 |
|
int max_box; |
2270 |
|
|
2271 |
|
y2s = sm_uptd[ticker % 8]; |
2291 |
|
} |
2292 |
|
} |
2293 |
|
|
2294 |
+ |
XDisplayLock(); |
2295 |
|
if ((nr_boxes <= max_box) && (nr_boxes)) { |
2296 |
|
for (y1=0; y1<16; y1++) { |
2297 |
|
for (x1=0; x1<16; x1++) { |
2320 |
|
i = (yi * bytes_per_row) + xi; |
2321 |
|
for (y2=0; y2 < yil; y2++, i += bytes_per_row) |
2322 |
|
memcpy(&the_buffer_copy[i], &the_buffer[i], xil); |
2323 |
< |
if (VideoMonitor.mode.depth == VDEPTH_1BIT) { |
2323 |
> |
if (mode.depth == VDEPTH_1BIT) { |
2324 |
|
if (drv->have_shm) |
2325 |
|
XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0); |
2326 |
|
else |
2345 |
|
} |
2346 |
|
nr_boxes = 0; |
2347 |
|
} |
2348 |
+ |
XDisplayUnlock(); |
2349 |
|
} |
2350 |
|
|
2351 |
|
// Static display update (fixed frame rate, but incremental) |
2352 |
|
static void update_display_static(driver_window *drv) |
2353 |
|
{ |
2354 |
|
// Incremental update code |
2355 |
< |
int wide = 0, high = 0, x1, x2, y1, y2, i, j; |
2356 |
< |
int bytes_per_row = VideoMonitor.mode.bytes_per_row; |
2357 |
< |
int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x; |
2355 |
> |
unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j; |
2356 |
> |
const video_mode &mode = drv->monitor.get_current_mode(); |
2357 |
> |
int bytes_per_row = mode.bytes_per_row; |
2358 |
> |
int bytes_per_pixel = mode.bytes_per_row / mode.x; |
2359 |
|
uint8 *p, *p2; |
2360 |
|
|
2361 |
|
// Check for first line from top and first line from bottom that have changed |
2362 |
|
y1 = 0; |
2363 |
< |
for (j=0; j<VideoMonitor.mode.y; j++) { |
2363 |
> |
for (j=0; j<mode.y; j++) { |
2364 |
|
if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) { |
2365 |
|
y1 = j; |
2366 |
|
break; |
2367 |
|
} |
2368 |
|
} |
2369 |
|
y2 = y1 - 1; |
2370 |
< |
for (j=VideoMonitor.mode.y-1; j>=y1; j--) { |
2370 |
> |
for (j=mode.y-1; j>=y1; j--) { |
2371 |
|
if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) { |
2372 |
|
y2 = j; |
2373 |
|
break; |
2377 |
|
|
2378 |
|
// Check for first column from left and first column from right that have changed |
2379 |
|
if (high) { |
2380 |
< |
if (VideoMonitor.mode.depth == VDEPTH_1BIT) { |
2381 |
< |
x1 = VideoMonitor.mode.x - 1; |
2380 |
> |
if (mode.depth == VDEPTH_1BIT) { |
2381 |
> |
x1 = mode.x - 1; |
2382 |
|
for (j=y1; j<=y2; j++) { |
2383 |
|
p = &the_buffer[j * bytes_per_row]; |
2384 |
|
p2 = &the_buffer_copy[j * bytes_per_row]; |
2396 |
|
p2 = &the_buffer_copy[j * bytes_per_row]; |
2397 |
|
p += bytes_per_row; |
2398 |
|
p2 += bytes_per_row; |
2399 |
< |
for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) { |
2399 |
> |
for (i=(mode.x>>3); i>(x2>>3); i--) { |
2400 |
|
p--; p2--; |
2401 |
|
if (*p != *p2) { |
2402 |
|
x2 = (i << 3) + 7; |
2415 |
|
} |
2416 |
|
|
2417 |
|
} else { |
2418 |
< |
x1 = VideoMonitor.mode.x; |
2418 |
> |
x1 = mode.x; |
2419 |
|
for (j=y1; j<=y2; j++) { |
2420 |
|
p = &the_buffer[j * bytes_per_row]; |
2421 |
|
p2 = &the_buffer_copy[j * bytes_per_row]; |
2433 |
|
p2 = &the_buffer_copy[j * bytes_per_row]; |
2434 |
|
p += bytes_per_row; |
2435 |
|
p2 += bytes_per_row; |
2436 |
< |
for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) { |
2436 |
> |
for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) { |
2437 |
|
p--; |
2438 |
|
p2--; |
2439 |
|
if (*p != *p2) { |
2455 |
|
} |
2456 |
|
|
2457 |
|
// Refresh display |
2458 |
+ |
XDisplayLock(); |
2459 |
|
if (high && wide) { |
2460 |
|
if (drv->have_shm) |
2461 |
|
XShmPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high, 0); |
2462 |
|
else |
2463 |
|
XPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high); |
2464 |
|
} |
2465 |
+ |
XDisplayUnlock(); |
2466 |
|
} |
2467 |
|
|
2468 |
|
|
2502 |
|
{ |
2503 |
|
LOCK_PALETTE; |
2504 |
|
|
2505 |
< |
if (palette_changed) { |
2506 |
< |
palette_changed = false; |
2505 |
> |
if (x_palette_changed) { |
2506 |
> |
x_palette_changed = false; |
2507 |
> |
XDisplayLock(); |
2508 |
|
drv->update_palette(); |
2509 |
+ |
XDisplayUnlock(); |
2510 |
|
} |
2511 |
|
|
2512 |
|
UNLOCK_PALETTE; |
2531 |
|
tick_counter = 0; |
2532 |
|
if (mainBuffer.dirty) { |
2533 |
|
LOCK_VOSF; |
2534 |
< |
update_display_dga_vosf(); |
2534 |
> |
update_display_dga_vosf(static_cast<driver_dga *>(drv)); |
2535 |
|
UNLOCK_VOSF; |
2536 |
|
} |
2537 |
|
} |
2548 |
|
if (++tick_counter >= frame_skip) { |
2549 |
|
tick_counter = 0; |
2550 |
|
if (mainBuffer.dirty) { |
2551 |
+ |
XDisplayLock(); |
2552 |
|
LOCK_VOSF; |
2553 |
|
update_display_window_vosf(static_cast<driver_window *>(drv)); |
2554 |
|
UNLOCK_VOSF; |
2555 |
|
XSync(x_display, false); // Let the server catch up |
2556 |
+ |
XDisplayUnlock(); |
2557 |
|
} |
2558 |
|
} |
2559 |
|
} |
2633 |
|
const int VIDEO_REFRESH_HZ = 60; |
2634 |
|
const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ; |
2635 |
|
|
2636 |
< |
#ifdef HAVE_PTHREADS |
2636 |
> |
#ifdef USE_PTHREADS_SERVICES |
2637 |
|
static void *redraw_func(void *arg) |
2638 |
|
{ |
2639 |
|
int fd = ConnectionNumber(x_display); |
2674 |
|
} |
2675 |
|
|
2676 |
|
uint64 end = GetTicks_usec(); |
2677 |
< |
D(bug("%Ld refreshes in %Ld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
2677 |
> |
D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
2678 |
> |
|
2679 |
> |
redraw_thread_cancel_ack = true; |
2680 |
|
return NULL; |
2681 |
|
} |
2682 |
|
#endif |