1 |
|
/* |
2 |
|
* video_x.cpp - Video/graphics emulation, X11 specific stuff |
3 |
|
* |
4 |
< |
* SheepShaver (C) 1997-2002 Marc Hellwig and Christian Bauer |
4 |
> |
* SheepShaver (C) 1997-2004 Marc Hellwig and 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 |
18 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
*/ |
20 |
|
|
21 |
+ |
#include "sysdeps.h" |
22 |
+ |
|
23 |
|
#include <X11/Xlib.h> |
24 |
|
#include <X11/Xutil.h> |
25 |
|
#include <X11/keysym.h> |
26 |
|
#include <X11/extensions/XShm.h> |
27 |
|
#include <sys/ipc.h> |
28 |
|
#include <sys/shm.h> |
29 |
+ |
#include <errno.h> |
30 |
|
#include <pthread.h> |
31 |
|
|
32 |
< |
#include "sysdeps.h" |
32 |
> |
#include <algorithm> |
33 |
> |
|
34 |
> |
#ifdef ENABLE_XF86_DGA |
35 |
> |
#include <X11/extensions/xf86dga.h> |
36 |
> |
#endif |
37 |
> |
|
38 |
> |
#ifdef ENABLE_XF86_VIDMODE |
39 |
> |
# include <X11/extensions/xf86vmode.h> |
40 |
> |
#endif |
41 |
> |
|
42 |
|
#include "main.h" |
43 |
|
#include "adb.h" |
44 |
|
#include "prefs.h" |
50 |
|
#define DEBUG 0 |
51 |
|
#include "debug.h" |
52 |
|
|
53 |
< |
#ifdef ENABLE_XF86_DGA |
54 |
< |
#include <X11/extensions/xf86dga.h> |
53 |
> |
#ifndef NO_STD_NAMESPACE |
54 |
> |
using std::sort; |
55 |
|
#endif |
56 |
|
|
45 |
– |
#ifdef ENABLE_XF86_VIDMODE |
46 |
– |
#include <X11/extensions/xf86vmode.h> |
47 |
– |
#endif |
57 |
|
|
58 |
+ |
// Constants |
59 |
+ |
const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes"; |
60 |
|
|
61 |
|
// Global variables |
62 |
|
static int32 frame_skip; |
63 |
+ |
static int16 mouse_wheel_mode; |
64 |
+ |
static int16 mouse_wheel_lines; |
65 |
|
static bool redraw_thread_active = false; // Flag: Redraw thread installed |
66 |
|
static pthread_t redraw_thread; // Redraw thread |
67 |
|
|
87 |
|
static bool emul_suspended = false; // Flag: emulator suspended |
88 |
|
static Window suspend_win; // "Suspend" window |
89 |
|
static void *fb_save = NULL; // Saved frame buffer for suspend |
90 |
+ |
static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms |
91 |
+ |
static int keycode_table[256]; // X keycode -> Mac keycode translation table |
92 |
|
|
93 |
|
// X11 variables |
94 |
|
static int screen; // Screen number |
95 |
|
static int xdepth; // Depth of X screen |
96 |
|
static int depth; // Depth of Mac frame buffer |
97 |
|
static Window rootwin, the_win; // Root window and our window |
98 |
+ |
static int num_depths = 0; // Number of available X depths |
99 |
+ |
static int *avail_depths = NULL; // List of available X depths |
100 |
|
static XVisualInfo visualInfo; |
101 |
|
static Visual *vis; |
102 |
+ |
static int color_class; |
103 |
+ |
static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes |
104 |
|
static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode |
105 |
+ |
static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table |
106 |
+ |
|
107 |
|
static XColor black, white; |
108 |
|
static unsigned long black_pixel, white_pixel; |
109 |
|
static int eventmask; |
110 |
< |
static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask; |
111 |
< |
static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask; |
110 |
> |
static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask; |
111 |
> |
static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask; |
112 |
|
|
113 |
|
// Variables for window mode |
114 |
|
static GC the_gc; |
135 |
|
static int num_x_video_modes; |
136 |
|
#endif |
137 |
|
|
138 |
+ |
// Mutex to protect palette |
139 |
+ |
#ifdef HAVE_SPINLOCKS |
140 |
+ |
static spinlock_t x_palette_lock = SPIN_LOCK_UNLOCKED; |
141 |
+ |
#define LOCK_PALETTE spin_lock(&x_palette_lock) |
142 |
+ |
#define UNLOCK_PALETTE spin_unlock(&x_palette_lock) |
143 |
+ |
#elif defined(HAVE_PTHREADS) |
144 |
+ |
static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER; |
145 |
+ |
#define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock) |
146 |
+ |
#define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock) |
147 |
+ |
#else |
148 |
+ |
#define LOCK_PALETTE |
149 |
+ |
#define UNLOCK_PALETTE |
150 |
+ |
#endif |
151 |
+ |
|
152 |
|
|
153 |
|
// Prototypes |
154 |
|
static void *redraw_func(void *arg); |
161 |
|
// From sys_unix.cpp |
162 |
|
extern void SysMountFirstFloppy(void); |
163 |
|
|
164 |
+ |
// From clip_unix.cpp |
165 |
+ |
extern void ClipboardSelectionClear(XSelectionClearEvent *); |
166 |
+ |
extern void ClipboardSelectionRequest(XSelectionRequestEvent *); |
167 |
+ |
|
168 |
|
|
169 |
|
// Video acceleration through SIGSEGV |
170 |
|
#ifdef ENABLE_VOSF |
173 |
|
|
174 |
|
|
175 |
|
/* |
176 |
+ |
* Utility functions |
177 |
+ |
*/ |
178 |
+ |
|
179 |
+ |
// Get current video mode |
180 |
+ |
static inline int get_current_mode(void) |
181 |
+ |
{ |
182 |
+ |
return VModes[cur_mode].viAppleMode; |
183 |
+ |
} |
184 |
+ |
|
185 |
+ |
// Find palette size for given color depth |
186 |
+ |
static int palette_size(int mode) |
187 |
+ |
{ |
188 |
+ |
switch (mode) { |
189 |
+ |
case APPLE_1_BIT: return 2; |
190 |
+ |
case APPLE_2_BIT: return 4; |
191 |
+ |
case APPLE_4_BIT: return 16; |
192 |
+ |
case APPLE_8_BIT: return 256; |
193 |
+ |
case APPLE_16_BIT: return 32; |
194 |
+ |
case APPLE_32_BIT: return 256; |
195 |
+ |
default: return 0; |
196 |
+ |
} |
197 |
+ |
} |
198 |
+ |
|
199 |
+ |
// Map video_mode depth ID to numerical depth value |
200 |
+ |
static inline int depth_of_video_mode(int mode) |
201 |
+ |
{ |
202 |
+ |
int depth = -1; |
203 |
+ |
switch (mode) { |
204 |
+ |
case APPLE_1_BIT: |
205 |
+ |
depth = 1; |
206 |
+ |
break; |
207 |
+ |
case APPLE_2_BIT: |
208 |
+ |
depth = 2; |
209 |
+ |
break; |
210 |
+ |
case APPLE_4_BIT: |
211 |
+ |
depth = 4; |
212 |
+ |
break; |
213 |
+ |
case APPLE_8_BIT: |
214 |
+ |
depth = 8; |
215 |
+ |
break; |
216 |
+ |
case APPLE_16_BIT: |
217 |
+ |
depth = 16; |
218 |
+ |
break; |
219 |
+ |
case APPLE_32_BIT: |
220 |
+ |
depth = 32; |
221 |
+ |
break; |
222 |
+ |
default: |
223 |
+ |
abort(); |
224 |
+ |
} |
225 |
+ |
return depth; |
226 |
+ |
} |
227 |
+ |
|
228 |
+ |
// Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals) |
229 |
+ |
static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue) |
230 |
+ |
{ |
231 |
+ |
return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift); |
232 |
+ |
} |
233 |
+ |
|
234 |
+ |
|
235 |
+ |
// Do we have a visual for handling the specified Mac depth? If so, set the |
236 |
+ |
// global variables "xdepth", "visualInfo", "vis" and "color_class". |
237 |
+ |
static bool find_visual_for_depth(int depth) |
238 |
+ |
{ |
239 |
+ |
D(bug("have_visual_for_depth(%d)\n", depth_of_video_mode(depth))); |
240 |
+ |
|
241 |
+ |
// 1-bit works always and uses default visual |
242 |
+ |
if (depth == APPLE_1_BIT) { |
243 |
+ |
vis = DefaultVisual(x_display, screen); |
244 |
+ |
visualInfo.visualid = XVisualIDFromVisual(vis); |
245 |
+ |
int num = 0; |
246 |
+ |
XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num); |
247 |
+ |
visualInfo = vi[0]; |
248 |
+ |
XFree(vi); |
249 |
+ |
xdepth = visualInfo.depth; |
250 |
+ |
color_class = visualInfo.c_class; |
251 |
+ |
D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth)); |
252 |
+ |
return true; |
253 |
+ |
} |
254 |
+ |
|
255 |
+ |
// Calculate minimum and maximum supported X depth |
256 |
+ |
int min_depth = 1, max_depth = 32; |
257 |
+ |
switch (depth) { |
258 |
+ |
#ifdef ENABLE_VOSF |
259 |
+ |
case APPLE_2_BIT: |
260 |
+ |
case APPLE_4_BIT: // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit |
261 |
+ |
case APPLE_8_BIT: |
262 |
+ |
min_depth = 8; |
263 |
+ |
max_depth = 32; |
264 |
+ |
break; |
265 |
+ |
#else |
266 |
+ |
case APPLE_2_BIT: |
267 |
+ |
case APPLE_4_BIT: // 2/4-bit requires VOSF blitters |
268 |
+ |
return false; |
269 |
+ |
case APPLE_8_BIT: // 8-bit without VOSF requires an 8-bit visual |
270 |
+ |
min_depth = 8; |
271 |
+ |
max_depth = 8; |
272 |
+ |
break; |
273 |
+ |
#endif |
274 |
+ |
case APPLE_16_BIT: // 16-bit requires a 15/16-bit visual |
275 |
+ |
min_depth = 15; |
276 |
+ |
max_depth = 16; |
277 |
+ |
break; |
278 |
+ |
case APPLE_32_BIT: // 32-bit requires a 24/32-bit visual |
279 |
+ |
min_depth = 24; |
280 |
+ |
max_depth = 32; |
281 |
+ |
break; |
282 |
+ |
} |
283 |
+ |
D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth)); |
284 |
+ |
|
285 |
+ |
// Try to find a visual for one of the color depths |
286 |
+ |
bool visual_found = false; |
287 |
+ |
for (int i=0; i<num_depths && !visual_found; i++) { |
288 |
+ |
|
289 |
+ |
xdepth = avail_depths[i]; |
290 |
+ |
D(bug(" trying to find visual for depth %d\n", xdepth)); |
291 |
+ |
if (xdepth < min_depth || xdepth > max_depth) |
292 |
+ |
continue; |
293 |
+ |
|
294 |
+ |
// Determine best color class for this depth |
295 |
+ |
switch (xdepth) { |
296 |
+ |
case 1: // Try StaticGray or StaticColor |
297 |
+ |
if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo) |
298 |
+ |
|| XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo)) |
299 |
+ |
visual_found = true; |
300 |
+ |
break; |
301 |
+ |
case 8: // Need PseudoColor |
302 |
+ |
if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo)) |
303 |
+ |
visual_found = true; |
304 |
+ |
break; |
305 |
+ |
case 15: |
306 |
+ |
case 16: |
307 |
+ |
case 24: |
308 |
+ |
case 32: // Try DirectColor first, as this will allow gamma correction |
309 |
+ |
if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo) |
310 |
+ |
|| XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo)) |
311 |
+ |
visual_found = true; |
312 |
+ |
break; |
313 |
+ |
default: |
314 |
+ |
D(bug(" not a supported depth\n")); |
315 |
+ |
break; |
316 |
+ |
} |
317 |
+ |
} |
318 |
+ |
if (!visual_found) |
319 |
+ |
return false; |
320 |
+ |
|
321 |
+ |
// Visual was found |
322 |
+ |
vis = visualInfo.visual; |
323 |
+ |
color_class = visualInfo.c_class; |
324 |
+ |
D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth)); |
325 |
+ |
#if DEBUG |
326 |
+ |
switch (color_class) { |
327 |
+ |
case StaticGray: D(bug("StaticGray\n")); break; |
328 |
+ |
case GrayScale: D(bug("GrayScale\n")); break; |
329 |
+ |
case StaticColor: D(bug("StaticColor\n")); break; |
330 |
+ |
case PseudoColor: D(bug("PseudoColor\n")); break; |
331 |
+ |
case TrueColor: D(bug("TrueColor\n")); break; |
332 |
+ |
case DirectColor: D(bug("DirectColor\n")); break; |
333 |
+ |
} |
334 |
+ |
#endif |
335 |
+ |
return true; |
336 |
+ |
} |
337 |
+ |
|
338 |
+ |
|
339 |
+ |
/* |
340 |
|
* Open display (window or fullscreen) |
341 |
|
*/ |
342 |
|
|
343 |
+ |
// Wait until window is mapped/unmapped |
344 |
+ |
void wait_mapped(Window w) |
345 |
+ |
{ |
346 |
+ |
XEvent e; |
347 |
+ |
do { |
348 |
+ |
XMaskEvent(x_display, StructureNotifyMask, &e); |
349 |
+ |
} while ((e.type != MapNotify) || (e.xmap.event != w)); |
350 |
+ |
} |
351 |
+ |
|
352 |
+ |
void wait_unmapped(Window w) |
353 |
+ |
{ |
354 |
+ |
XEvent e; |
355 |
+ |
do { |
356 |
+ |
XMaskEvent(x_display, StructureNotifyMask, &e); |
357 |
+ |
} while ((e.type != UnmapNotify) || (e.xmap.event != w)); |
358 |
+ |
} |
359 |
+ |
|
360 |
|
// Trap SHM errors |
361 |
|
static bool shm_error = false; |
362 |
|
static int (*old_error_handler)(Display *, XErrorEvent *); |
379 |
|
// Set absolute mouse mode |
380 |
|
ADBSetRelMouseMode(false); |
381 |
|
|
162 |
– |
// Read frame skip prefs |
163 |
– |
frame_skip = PrefsFindInt32("frameskip"); |
164 |
– |
if (frame_skip == 0) |
165 |
– |
frame_skip = 1; |
166 |
– |
|
382 |
|
// Create window |
383 |
|
XSetWindowAttributes wattr; |
384 |
|
wattr.event_mask = eventmask = win_eventmask; |
385 |
< |
wattr.background_pixel = black_pixel; |
386 |
< |
wattr.border_pixel = black_pixel; |
385 |
> |
wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0); |
386 |
> |
wattr.border_pixel = 0; |
387 |
|
wattr.backing_store = NotUseful; |
388 |
< |
|
174 |
< |
XSync(x_display, false); |
388 |
> |
wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]); |
389 |
|
the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, |
390 |
< |
InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore, &wattr); |
177 |
< |
XSync(x_display, false); |
178 |
< |
XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); |
179 |
< |
XMapRaised(x_display, the_win); |
180 |
< |
XSync(x_display, false); |
390 |
> |
InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr); |
391 |
|
|
392 |
< |
// Set colormap |
393 |
< |
if (depth == 8) { |
184 |
< |
XSetWindowColormap(x_display, the_win, cmap[0]); |
185 |
< |
XSetWMColormapWindows(x_display, the_win, &the_win, 1); |
186 |
< |
} |
392 |
> |
// Set window name |
393 |
> |
XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); |
394 |
|
|
395 |
|
// Make window unresizable |
396 |
|
XSizeHints *hints; |
404 |
|
XFree((char *)hints); |
405 |
|
} |
406 |
|
|
407 |
+ |
// Show window |
408 |
+ |
XMapWindow(x_display, the_win); |
409 |
+ |
wait_mapped(the_win); |
410 |
+ |
|
411 |
+ |
// 1-bit mode is big-endian; if the X server is little-endian, we can't |
412 |
+ |
// use SHM because that doesn't allow changing the image byte order |
413 |
+ |
bool need_msb_image = (depth == 1 && XImageByteOrder(x_display) == LSBFirst); |
414 |
+ |
|
415 |
|
// Try to create and attach SHM image |
416 |
|
have_shm = false; |
417 |
< |
if (local_X11 && depth != 1 && XShmQueryExtension(x_display)) { |
417 |
> |
if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) { |
418 |
|
|
419 |
|
// Create SHM image ("height + 2" for safety) |
420 |
< |
img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height); |
421 |
< |
shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777); |
420 |
> |
img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height); |
421 |
> |
shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777); |
422 |
> |
D(bug(" shm image created\n")); |
423 |
|
the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0); |
424 |
|
shminfo.shmaddr = img->data = (char *)the_buffer_copy; |
425 |
|
shminfo.readOnly = False; |
438 |
|
have_shm = true; |
439 |
|
shmctl(shminfo.shmid, IPC_RMID, 0); |
440 |
|
} |
441 |
+ |
D(bug(" shm image attached\n")); |
442 |
|
} |
443 |
|
|
444 |
|
// Create normal X image if SHM doesn't work ("height + 2" for safety) |
445 |
|
if (!have_shm) { |
446 |
< |
int bytes_per_row = aligned_width; |
230 |
< |
switch (depth) { |
231 |
< |
case 1: |
232 |
< |
bytes_per_row /= 8; |
233 |
< |
break; |
234 |
< |
case 15: |
235 |
< |
case 16: |
236 |
< |
bytes_per_row *= 2; |
237 |
< |
break; |
238 |
< |
case 24: |
239 |
< |
case 32: |
240 |
< |
bytes_per_row *= 4; |
241 |
< |
break; |
242 |
< |
} |
446 |
> |
int bytes_per_row = depth == 1 ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth)); |
447 |
|
the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row); |
448 |
|
img = XCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row); |
449 |
+ |
D(bug(" X image created\n")); |
450 |
|
} |
451 |
|
|
452 |
|
// 1-Bit mode is big-endian |
453 |
< |
if (depth == 1) { |
453 |
> |
if (need_msb_image) { |
454 |
|
img->byte_order = MSBFirst; |
455 |
|
img->bitmap_bit_order = MSBFirst; |
456 |
|
} |
472 |
|
|
473 |
|
// Create GC |
474 |
|
the_gc = XCreateGC(x_display, the_win, 0, 0); |
475 |
< |
XSetForeground(x_display, the_gc, black_pixel); |
475 |
> |
XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes); |
476 |
|
|
477 |
|
// Create cursor |
478 |
|
cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2); |
500 |
|
#endif |
501 |
|
|
502 |
|
// Set bytes per row |
298 |
– |
VModes[cur_mode].viRowBytes = img->bytes_per_line; |
503 |
|
XSync(x_display, false); |
504 |
|
return true; |
505 |
|
} |
538 |
|
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
539 |
|
|
540 |
|
// Set bytes per row |
541 |
< |
int bytes_per_row = (dga_fb_width + 7) & ~7; |
338 |
< |
switch (depth) { |
339 |
< |
case 15: |
340 |
< |
case 16: |
341 |
< |
bytes_per_row *= 2; |
342 |
< |
break; |
343 |
< |
case 24: |
344 |
< |
case 32: |
345 |
< |
bytes_per_row *= 4; |
346 |
< |
break; |
347 |
< |
} |
541 |
> |
int bytes_per_row = TrivialBytesPerRow((dga_fb_width + 7) & ~7, DepthModeForPixelDepth(depth)); |
542 |
|
|
543 |
|
#if ENABLE_VOSF |
544 |
|
bool native_byte_order; |
577 |
|
|
578 |
|
static bool open_display(void) |
579 |
|
{ |
580 |
< |
display_type = VModes[cur_mode].viType; |
581 |
< |
switch (VModes[cur_mode].viAppleMode) { |
582 |
< |
case APPLE_1_BIT: |
583 |
< |
depth = 1; |
584 |
< |
break; |
585 |
< |
case APPLE_2_BIT: |
586 |
< |
depth = 2; |
393 |
< |
break; |
394 |
< |
case APPLE_4_BIT: |
395 |
< |
depth = 4; |
396 |
< |
break; |
397 |
< |
case APPLE_8_BIT: |
398 |
< |
depth = 8; |
399 |
< |
break; |
400 |
< |
case APPLE_16_BIT: |
401 |
< |
depth = xdepth == 15 ? 15 : 16; |
402 |
< |
break; |
403 |
< |
case APPLE_32_BIT: |
404 |
< |
depth = 32; |
405 |
< |
break; |
580 |
> |
D(bug("open_display()\n")); |
581 |
> |
const VideoInfo &mode = VModes[cur_mode]; |
582 |
> |
|
583 |
> |
// Find best available X visual |
584 |
> |
if (!find_visual_for_depth(mode.viAppleMode)) { |
585 |
> |
ErrorAlert(GetString(STR_NO_XVISUAL_ERR)); |
586 |
> |
return false; |
587 |
|
} |
588 |
|
|
589 |
+ |
// Create color maps |
590 |
+ |
if (color_class == PseudoColor || color_class == DirectColor) { |
591 |
+ |
cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll); |
592 |
+ |
cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll); |
593 |
+ |
} else { |
594 |
+ |
cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone); |
595 |
+ |
cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone); |
596 |
+ |
} |
597 |
+ |
|
598 |
+ |
// Find pixel format of direct modes |
599 |
+ |
if (color_class == DirectColor || color_class == TrueColor) { |
600 |
+ |
rshift = gshift = bshift = 0; |
601 |
+ |
rloss = gloss = bloss = 8; |
602 |
+ |
uint32 mask; |
603 |
+ |
for (mask=vis->red_mask; !(mask&1); mask>>=1) |
604 |
+ |
++rshift; |
605 |
+ |
for (; mask&1; mask>>=1) |
606 |
+ |
--rloss; |
607 |
+ |
for (mask=vis->green_mask; !(mask&1); mask>>=1) |
608 |
+ |
++gshift; |
609 |
+ |
for (; mask&1; mask>>=1) |
610 |
+ |
--gloss; |
611 |
+ |
for (mask=vis->blue_mask; !(mask&1); mask>>=1) |
612 |
+ |
++bshift; |
613 |
+ |
for (; mask&1; mask>>=1) |
614 |
+ |
--bloss; |
615 |
+ |
} |
616 |
+ |
|
617 |
+ |
// Preset palette pixel values for CLUT or gamma table |
618 |
+ |
if (color_class == DirectColor) { |
619 |
+ |
int num = vis->map_entries; |
620 |
+ |
for (int i=0; i<num; i++) { |
621 |
+ |
int c = (i * 256) / num; |
622 |
+ |
x_palette[i].pixel = map_rgb(c, c, c); |
623 |
+ |
x_palette[i].flags = DoRed | DoGreen | DoBlue; |
624 |
+ |
} |
625 |
+ |
} else if (color_class == PseudoColor) { |
626 |
+ |
for (int i=0; i<256; i++) { |
627 |
+ |
x_palette[i].pixel = i; |
628 |
+ |
x_palette[i].flags = DoRed | DoGreen | DoBlue; |
629 |
+ |
} |
630 |
+ |
} |
631 |
+ |
|
632 |
+ |
// Load gray ramp to color map |
633 |
+ |
int num = (color_class == DirectColor ? vis->map_entries : 256); |
634 |
+ |
for (int i=0; i<num; i++) { |
635 |
+ |
int c = (i * 256) / num; |
636 |
+ |
x_palette[i].red = c * 0x0101; |
637 |
+ |
x_palette[i].green = c * 0x0101; |
638 |
+ |
x_palette[i].blue = c * 0x0101; |
639 |
+ |
} |
640 |
+ |
if (color_class == PseudoColor || color_class == DirectColor) { |
641 |
+ |
XStoreColors(x_display, cmap[0], x_palette, num); |
642 |
+ |
XStoreColors(x_display, cmap[1], x_palette, num); |
643 |
+ |
} |
644 |
+ |
|
645 |
+ |
#ifdef ENABLE_VOSF |
646 |
+ |
// Load gray ramp to 8->16/32 expand map |
647 |
+ |
if (!IsDirectMode(get_current_mode()) && xdepth > 8) |
648 |
+ |
for (int i=0; i<256; i++) |
649 |
+ |
ExpandMap[i] = map_rgb(i, i, i); |
650 |
+ |
#endif |
651 |
+ |
|
652 |
+ |
// Create display of requested type |
653 |
+ |
display_type = mode.viType; |
654 |
+ |
depth = depth_of_video_mode(mode.viAppleMode); |
655 |
+ |
|
656 |
|
bool display_open = false; |
657 |
|
if (display_type == DIS_SCREEN) |
658 |
|
display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize); |
701 |
|
XFreeGC(x_display, the_gc); |
702 |
|
|
703 |
|
// Close window |
704 |
< |
XDestroyWindow(x_display, the_win); |
704 |
> |
if (the_win) { |
705 |
> |
XUnmapWindow(x_display, the_win); |
706 |
> |
wait_unmapped(the_win); |
707 |
> |
XDestroyWindow(x_display, the_win); |
708 |
> |
} |
709 |
> |
|
710 |
> |
XFlush(x_display); |
711 |
> |
XSync(x_display, false); |
712 |
|
} |
713 |
|
|
714 |
|
// Close DGA mode |
744 |
|
else if (display_type == DIS_WINDOW) |
745 |
|
close_window(); |
746 |
|
|
747 |
+ |
// Free colormaps |
748 |
+ |
if (cmap[0]) { |
749 |
+ |
XFreeColormap(x_display, cmap[0]); |
750 |
+ |
cmap[0] = 0; |
751 |
+ |
} |
752 |
+ |
if (cmap[1]) { |
753 |
+ |
XFreeColormap(x_display, cmap[1]); |
754 |
+ |
cmap[1] = 0; |
755 |
+ |
} |
756 |
+ |
|
757 |
|
#ifdef ENABLE_VOSF |
758 |
|
if (use_vosf) { |
759 |
|
// Deinitialize VOSF |
795 |
|
* Initialization |
796 |
|
*/ |
797 |
|
|
798 |
< |
static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, long apple_mode, long apple_id, int type) |
798 |
> |
// Init keycode translation table |
799 |
> |
static void keycode_init(void) |
800 |
> |
{ |
801 |
> |
bool use_kc = PrefsFindBool("keycodes"); |
802 |
> |
if (use_kc) { |
803 |
> |
|
804 |
> |
// Get keycode file path from preferences |
805 |
> |
const char *kc_path = PrefsFindString("keycodefile"); |
806 |
> |
|
807 |
> |
// Open keycode table |
808 |
> |
FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r"); |
809 |
> |
if (f == NULL) { |
810 |
> |
char str[256]; |
811 |
> |
sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno)); |
812 |
> |
WarningAlert(str); |
813 |
> |
return; |
814 |
> |
} |
815 |
> |
|
816 |
> |
// Default translation table |
817 |
> |
for (int i=0; i<256; i++) |
818 |
> |
keycode_table[i] = -1; |
819 |
> |
|
820 |
> |
// Search for server vendor string, then read keycodes |
821 |
> |
const char *vendor = ServerVendor(x_display); |
822 |
> |
bool vendor_found = false; |
823 |
> |
char line[256]; |
824 |
> |
while (fgets(line, 255, f)) { |
825 |
> |
// Read line |
826 |
> |
int len = strlen(line); |
827 |
> |
if (len == 0) |
828 |
> |
continue; |
829 |
> |
line[len-1] = 0; |
830 |
> |
|
831 |
> |
// Comments begin with "#" or ";" |
832 |
> |
if (line[0] == '#' || line[0] == ';' || line[0] == 0) |
833 |
> |
continue; |
834 |
> |
|
835 |
> |
if (vendor_found) { |
836 |
> |
// Read keycode |
837 |
> |
int x_code, mac_code; |
838 |
> |
if (sscanf(line, "%d %d", &x_code, &mac_code) == 2) |
839 |
> |
keycode_table[x_code & 0xff] = mac_code; |
840 |
> |
else |
841 |
> |
break; |
842 |
> |
} else { |
843 |
> |
// Search for vendor string |
844 |
> |
if (strstr(vendor, line) == vendor) |
845 |
> |
vendor_found = true; |
846 |
> |
} |
847 |
> |
} |
848 |
> |
|
849 |
> |
// Keycode file completely read |
850 |
> |
fclose(f); |
851 |
> |
use_keycodes = vendor_found; |
852 |
> |
|
853 |
> |
// Vendor not found? Then display warning |
854 |
> |
if (!vendor_found) { |
855 |
> |
char str[256]; |
856 |
> |
sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME); |
857 |
> |
WarningAlert(str); |
858 |
> |
return; |
859 |
> |
} |
860 |
> |
} |
861 |
> |
} |
862 |
> |
|
863 |
> |
// Add mode to list of supported modes |
864 |
> |
static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type) |
865 |
|
{ |
866 |
|
if (allow & test) { |
867 |
|
p->viType = type; |
893 |
|
p->viYsize = 1200; |
894 |
|
break; |
895 |
|
} |
896 |
< |
switch (apple_mode) { |
566 |
< |
case APPLE_8_BIT: |
567 |
< |
p->viRowBytes = p->viXsize; |
568 |
< |
break; |
569 |
< |
case APPLE_16_BIT: |
570 |
< |
p->viRowBytes = p->viXsize * 2; |
571 |
< |
break; |
572 |
< |
case APPLE_32_BIT: |
573 |
< |
p->viRowBytes = p->viXsize * 4; |
574 |
< |
break; |
575 |
< |
} |
896 |
> |
p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode); |
897 |
|
p->viAppleMode = apple_mode; |
898 |
|
p->viAppleID = apple_id; |
899 |
|
p++; |
900 |
|
} |
901 |
|
} |
902 |
|
|
903 |
+ |
// Add standard list of windowed modes for given color depth |
904 |
+ |
static void add_window_modes(VideoInfo *&p, int window_modes, int mode) |
905 |
+ |
{ |
906 |
+ |
add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW); |
907 |
+ |
add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW); |
908 |
+ |
} |
909 |
+ |
|
910 |
|
static bool has_mode(int x, int y) |
911 |
|
{ |
912 |
|
#ifdef ENABLE_XF86_VIDMODE |
931 |
|
local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0) |
932 |
|
|| (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0); |
933 |
|
|
934 |
+ |
// Init keycode translation |
935 |
+ |
keycode_init(); |
936 |
+ |
|
937 |
+ |
// Read frame skip prefs |
938 |
+ |
frame_skip = PrefsFindInt32("frameskip"); |
939 |
+ |
if (frame_skip == 0) |
940 |
+ |
frame_skip = 1; |
941 |
+ |
|
942 |
+ |
// Read mouse wheel prefs |
943 |
+ |
mouse_wheel_mode = PrefsFindInt32("mousewheelmode"); |
944 |
+ |
mouse_wheel_lines = PrefsFindInt32("mousewheellines"); |
945 |
+ |
|
946 |
|
// Init variables |
947 |
|
private_data = NULL; |
608 |
– |
cur_mode = 0; // Window 640x480 |
948 |
|
video_activated = true; |
949 |
|
|
950 |
|
// Find screen and root window |
951 |
|
screen = XDefaultScreen(x_display); |
952 |
|
rootwin = XRootWindow(x_display, screen); |
953 |
|
|
954 |
+ |
// Get sorted list of available depths |
955 |
+ |
avail_depths = XListDepths(x_display, screen, &num_depths); |
956 |
+ |
if (avail_depths == NULL) { |
957 |
+ |
ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR)); |
958 |
+ |
return false; |
959 |
+ |
} |
960 |
+ |
sort(avail_depths, avail_depths + num_depths); |
961 |
+ |
|
962 |
|
// Get screen depth |
963 |
|
xdepth = DefaultDepth(x_display, screen); |
964 |
|
|
989 |
|
black_pixel = BlackPixel(x_display, screen); |
990 |
|
white_pixel = WhitePixel(x_display, screen); |
991 |
|
|
645 |
– |
// Get appropriate visual |
646 |
– |
int color_class; |
647 |
– |
switch (xdepth) { |
648 |
– |
#if 0 |
649 |
– |
case 1: |
650 |
– |
color_class = StaticGray; |
651 |
– |
break; |
652 |
– |
#endif |
653 |
– |
case 8: |
654 |
– |
color_class = PseudoColor; |
655 |
– |
break; |
656 |
– |
case 15: |
657 |
– |
case 16: |
658 |
– |
case 24: |
659 |
– |
case 32: |
660 |
– |
color_class = TrueColor; |
661 |
– |
break; |
662 |
– |
default: |
663 |
– |
ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR)); |
664 |
– |
return false; |
665 |
– |
} |
666 |
– |
if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) { |
667 |
– |
ErrorAlert(GetString(STR_NO_XVISUAL_ERR)); |
668 |
– |
return false; |
669 |
– |
} |
670 |
– |
if (visualInfo.depth != xdepth) { |
671 |
– |
ErrorAlert(GetString(STR_NO_XVISUAL_ERR)); |
672 |
– |
return false; |
673 |
– |
} |
674 |
– |
vis = visualInfo.visual; |
675 |
– |
|
992 |
|
// Mac screen depth follows X depth (for now) |
993 |
< |
depth = xdepth; |
994 |
< |
|
995 |
< |
// Create color maps for 8 bit mode |
996 |
< |
if (depth == 8) { |
997 |
< |
cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll); |
998 |
< |
cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll); |
999 |
< |
XInstallColormap(x_display, cmap[0]); |
1000 |
< |
XInstallColormap(x_display, cmap[1]); |
993 |
> |
int default_mode = APPLE_8_BIT; |
994 |
> |
switch (DefaultDepth(x_display, screen)) { |
995 |
> |
case 1: |
996 |
> |
default_mode = APPLE_1_BIT; |
997 |
> |
break; |
998 |
> |
case 8: |
999 |
> |
default_mode = APPLE_8_BIT; |
1000 |
> |
break; |
1001 |
> |
case 15: case 16: |
1002 |
> |
default_mode = APPLE_16_BIT; |
1003 |
> |
break; |
1004 |
> |
case 24: case 32: |
1005 |
> |
default_mode = APPLE_32_BIT; |
1006 |
> |
break; |
1007 |
|
} |
1008 |
|
|
1009 |
|
// Construct video mode table |
688 |
– |
int mode = APPLE_8_BIT; |
689 |
– |
int bpr_mult = 8; |
690 |
– |
switch (depth) { |
691 |
– |
case 1: |
692 |
– |
mode = APPLE_1_BIT; |
693 |
– |
bpr_mult = 1; |
694 |
– |
break; |
695 |
– |
case 8: |
696 |
– |
mode = APPLE_8_BIT; |
697 |
– |
bpr_mult = 8; |
698 |
– |
break; |
699 |
– |
case 15: |
700 |
– |
case 16: |
701 |
– |
mode = APPLE_16_BIT; |
702 |
– |
bpr_mult = 16; |
703 |
– |
break; |
704 |
– |
case 24: |
705 |
– |
case 32: |
706 |
– |
mode = APPLE_32_BIT; |
707 |
– |
bpr_mult = 32; |
708 |
– |
break; |
709 |
– |
} |
710 |
– |
|
1010 |
|
uint32 window_modes = PrefsFindInt32("windowmodes"); |
1011 |
|
uint32 screen_modes = PrefsFindInt32("screenmodes"); |
1012 |
|
if (!has_dga) |
1015 |
|
window_modes |= 3; // Allow at least 640x480 and 800x600 window modes |
1016 |
|
|
1017 |
|
VideoInfo *p = VModes; |
1018 |
< |
add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW); |
1019 |
< |
add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW); |
1018 |
> |
for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) |
1019 |
> |
if (find_visual_for_depth(d)) |
1020 |
> |
add_window_modes(p, window_modes, d); |
1021 |
> |
|
1022 |
|
if (has_vidmode) { |
1023 |
|
if (has_mode(640, 480)) |
1024 |
< |
add_mode(p, screen_modes, 1, mode, APPLE_640x480, DIS_SCREEN); |
1024 |
> |
add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN); |
1025 |
|
if (has_mode(800, 600)) |
1026 |
< |
add_mode(p, screen_modes, 2, mode, APPLE_800x600, DIS_SCREEN); |
1026 |
> |
add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN); |
1027 |
|
if (has_mode(1024, 768)) |
1028 |
< |
add_mode(p, screen_modes, 4, mode, APPLE_1024x768, DIS_SCREEN); |
1028 |
> |
add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN); |
1029 |
|
if (has_mode(1152, 900)) |
1030 |
< |
add_mode(p, screen_modes, 8, mode, APPLE_1152x900, DIS_SCREEN); |
1030 |
> |
add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN); |
1031 |
|
if (has_mode(1280, 1024)) |
1032 |
< |
add_mode(p, screen_modes, 16, mode, APPLE_1280x1024, DIS_SCREEN); |
1032 |
> |
add_mode(p, screen_modes, 16, default_mode, APPLE_1280x1024, DIS_SCREEN); |
1033 |
|
if (has_mode(1600, 1200)) |
1034 |
< |
add_mode(p, screen_modes, 32, mode, APPLE_1600x1200, DIS_SCREEN); |
1034 |
> |
add_mode(p, screen_modes, 32, default_mode, APPLE_1600x1200, DIS_SCREEN); |
1035 |
|
} else if (screen_modes) { |
1036 |
|
int xsize = DisplayWidth(x_display, screen); |
1037 |
|
int ysize = DisplayHeight(x_display, screen); |
1052 |
|
p->viRowBytes = 0; |
1053 |
|
p->viXsize = xsize; |
1054 |
|
p->viYsize = ysize; |
1055 |
< |
p->viAppleMode = mode; |
1055 |
> |
p->viAppleMode = default_mode; |
1056 |
|
p->viAppleID = apple_id; |
1057 |
|
p++; |
1058 |
|
} |
1062 |
|
p->viAppleMode = 0; |
1063 |
|
p->viAppleID = 0; |
1064 |
|
|
1065 |
+ |
// Find default mode (window 640x480) |
1066 |
+ |
cur_mode = -1; |
1067 |
+ |
for (p = VModes; p->viType != DIS_INVALID; p++) { |
1068 |
+ |
if (p->viType == DIS_WINDOW |
1069 |
+ |
&& p->viAppleID == APPLE_W_640x480 |
1070 |
+ |
&& p->viAppleMode == default_mode) { |
1071 |
+ |
cur_mode = p - VModes; |
1072 |
+ |
break; |
1073 |
+ |
} |
1074 |
+ |
} |
1075 |
+ |
assert(cur_mode != -1); |
1076 |
+ |
|
1077 |
+ |
#if DEBUG |
1078 |
+ |
D(bug("Available video modes:\n")); |
1079 |
+ |
for (p = VModes; p->viType != DIS_INVALID; p++) { |
1080 |
+ |
int bits = depth_of_video_mode(p->viAppleMode); |
1081 |
+ |
D(bug(" %dx%d (ID %02x), %d colors\n", p->viXsize, p->viYsize, p->viAppleID, 1 << bits)); |
1082 |
+ |
} |
1083 |
+ |
#endif |
1084 |
+ |
|
1085 |
|
#ifdef ENABLE_XF86_DGA |
1086 |
|
if (has_dga && screen_modes) { |
1087 |
|
int v_bank, v_size; |
1133 |
|
close_display(); |
1134 |
|
XFlush(x_display); |
1135 |
|
XSync(x_display, false); |
815 |
– |
if (depth == 8) { |
816 |
– |
XFreeColormap(x_display, cmap[0]); |
817 |
– |
XFreeColormap(x_display, cmap[1]); |
818 |
– |
} |
1136 |
|
} |
1137 |
|
} |
1138 |
|
|
1196 |
|
// Reopen full screen display |
1197 |
|
XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime); |
1198 |
|
XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); |
1199 |
+ |
#ifdef ENABLE_XF86_DGA |
1200 |
|
XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse); |
1201 |
|
XF86DGASetViewPort(x_display, screen, 0, 0); |
1202 |
+ |
#endif |
1203 |
|
XSync(x_display, false); |
1204 |
|
|
1205 |
|
// the_buffer already contains the data to restore. i.e. since a temporary |
1393 |
|
return -1; |
1394 |
|
} |
1395 |
|
|
1396 |
< |
static int event2keycode(XKeyEvent *ev) |
1396 |
> |
static int event2keycode(XKeyEvent &ev) |
1397 |
|
{ |
1398 |
|
KeySym ks; |
1399 |
|
int as; |
1400 |
|
int i = 0; |
1401 |
|
|
1402 |
|
do { |
1403 |
< |
ks = XLookupKeysym(ev, i++); |
1403 |
> |
ks = XLookupKeysym(&ev, i++); |
1404 |
|
as = kc_decode(ks); |
1405 |
|
if (as != -1) |
1406 |
|
return as; |
1415 |
|
for (;;) { |
1416 |
|
XEvent event; |
1417 |
|
|
1418 |
< |
if (!XCheckMaskEvent(x_display, eventmask, &event)) |
1418 |
> |
XDisplayLock(); |
1419 |
> |
if (!XCheckMaskEvent(x_display, eventmask, &event)) { |
1420 |
> |
// Handle clipboard events |
1421 |
> |
if (XCheckTypedEvent(x_display, SelectionRequest, &event)) |
1422 |
> |
ClipboardSelectionRequest(&event.xselectionrequest); |
1423 |
> |
else if (XCheckTypedEvent(x_display, SelectionClear, &event)) |
1424 |
> |
ClipboardSelectionClear(&event.xselectionclear); |
1425 |
> |
|
1426 |
> |
XDisplayUnlock(); |
1427 |
|
break; |
1428 |
+ |
} |
1429 |
+ |
XDisplayUnlock(); |
1430 |
|
|
1431 |
|
switch (event.type) { |
1432 |
|
// Mouse button |
1434 |
|
unsigned int button = ((XButtonEvent *)&event)->button; |
1435 |
|
if (button < 4) |
1436 |
|
ADBMouseDown(button - 1); |
1437 |
+ |
else if (button < 6) { // Wheel mouse |
1438 |
+ |
if (mouse_wheel_mode == 0) { |
1439 |
+ |
int key = (button == 5) ? 0x79 : 0x74; // Page up/down |
1440 |
+ |
ADBKeyDown(key); |
1441 |
+ |
ADBKeyUp(key); |
1442 |
+ |
} else { |
1443 |
+ |
int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down |
1444 |
+ |
for(int i=0; i<mouse_wheel_lines; i++) { |
1445 |
+ |
ADBKeyDown(key); |
1446 |
+ |
ADBKeyUp(key); |
1447 |
+ |
} |
1448 |
+ |
} |
1449 |
+ |
} |
1450 |
|
break; |
1451 |
|
} |
1452 |
|
case ButtonRelease: { |
1466 |
|
|
1467 |
|
// Keyboard |
1468 |
|
case KeyPress: { |
1469 |
< |
int code; |
1470 |
< |
if ((code = event2keycode((XKeyEvent *)&event)) != -1) { |
1469 |
> |
int code = event2keycode(event.xkey); |
1470 |
> |
if (use_keycodes && code != -1) |
1471 |
> |
code = keycode_table[event.xkey.keycode & 0xff]; |
1472 |
> |
if (code != -1) { |
1473 |
|
if (!emul_suspended) { |
1474 |
|
ADBKeyDown(code); |
1475 |
|
if (code == 0x36) |
1482 |
|
break; |
1483 |
|
} |
1484 |
|
case KeyRelease: { |
1485 |
< |
int code; |
1486 |
< |
if ((code = event2keycode((XKeyEvent *)&event)) != -1) { |
1485 |
> |
int code = event2keycode(event.xkey); |
1486 |
> |
if (use_keycodes && code != 1) |
1487 |
> |
code = keycode_table[event.xkey.keycode & 0xff]; |
1488 |
> |
if (code != -1) { |
1489 |
|
ADBKeyUp(code); |
1490 |
|
if (code == 0x36) |
1491 |
|
ctrl_down = false; |
1726 |
|
|
1727 |
|
void video_set_palette(void) |
1728 |
|
{ |
1729 |
+ |
LOCK_PALETTE; |
1730 |
+ |
|
1731 |
+ |
// Convert colors to XColor array |
1732 |
+ |
int mode = get_current_mode(); |
1733 |
+ |
int num_in = palette_size(mode); |
1734 |
+ |
int num_out = 256; |
1735 |
+ |
bool stretch = false; |
1736 |
+ |
if (IsDirectMode(mode)) { |
1737 |
+ |
// If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries |
1738 |
+ |
num_out = vis->map_entries; |
1739 |
+ |
stretch = true; |
1740 |
+ |
} |
1741 |
+ |
XColor *p = x_palette; |
1742 |
+ |
for (int i=0; i<num_out; i++) { |
1743 |
+ |
int c = (stretch ? (i * num_in) / num_out : i); |
1744 |
+ |
p->red = mac_pal[c].red * 0x0101; |
1745 |
+ |
p->green = mac_pal[c].green * 0x0101; |
1746 |
+ |
p->blue = mac_pal[c].blue * 0x0101; |
1747 |
+ |
p++; |
1748 |
+ |
} |
1749 |
+ |
|
1750 |
+ |
#ifdef ENABLE_VOSF |
1751 |
+ |
// Recalculate pixel color expansion map |
1752 |
+ |
if (!IsDirectMode(mode) && xdepth > 8) { |
1753 |
+ |
for (int i=0; i<256; i++) { |
1754 |
+ |
int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier) |
1755 |
+ |
ExpandMap[i] = map_rgb(mac_pal[c].red, mac_pal[c].green, mac_pal[c].blue); |
1756 |
+ |
} |
1757 |
+ |
|
1758 |
+ |
// We have to redraw everything because the interpretation of pixel values changed |
1759 |
+ |
LOCK_VOSF; |
1760 |
+ |
PFLAG_SET_ALL; |
1761 |
+ |
UNLOCK_VOSF; |
1762 |
+ |
memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); |
1763 |
+ |
} |
1764 |
+ |
#endif |
1765 |
+ |
|
1766 |
+ |
// Tell redraw thread to change palette |
1767 |
|
palette_changed = true; |
1768 |
+ |
|
1769 |
+ |
UNLOCK_PALETTE; |
1770 |
|
} |
1771 |
|
|
1772 |
|
|
1893 |
|
|
1894 |
|
// Refresh display |
1895 |
|
if (high && wide) { |
1896 |
+ |
XDisplayLock(); |
1897 |
|
if (have_shm) |
1898 |
|
XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0); |
1899 |
|
else |
1900 |
|
XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high); |
1901 |
+ |
XDisplayUnlock(); |
1902 |
|
} |
1903 |
|
} |
1904 |
|
|
1905 |
+ |
const int VIDEO_REFRESH_HZ = 60; |
1906 |
+ |
const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ; |
1907 |
+ |
|
1908 |
+ |
static void handle_palette_changes(void) |
1909 |
+ |
{ |
1910 |
+ |
LOCK_PALETTE; |
1911 |
+ |
|
1912 |
+ |
if (palette_changed && !emul_suspended) { |
1913 |
+ |
palette_changed = false; |
1914 |
+ |
|
1915 |
+ |
int mode = get_current_mode(); |
1916 |
+ |
if (color_class == PseudoColor || color_class == DirectColor) { |
1917 |
+ |
int num = vis->map_entries; |
1918 |
+ |
bool set_clut = true; |
1919 |
+ |
if (!IsDirectMode(mode) && color_class == DirectColor) { |
1920 |
+ |
if (display_type == DIS_WINDOW) |
1921 |
+ |
set_clut = false; // Indexed mode on true color screen, don't set CLUT |
1922 |
+ |
} |
1923 |
+ |
|
1924 |
+ |
if (set_clut) { |
1925 |
+ |
XDisplayLock(); |
1926 |
+ |
XStoreColors(x_display, cmap[0], x_palette, num); |
1927 |
+ |
XStoreColors(x_display, cmap[1], x_palette, num); |
1928 |
+ |
XSync(x_display, false); |
1929 |
+ |
XDisplayUnlock(); |
1930 |
+ |
} |
1931 |
+ |
} |
1932 |
+ |
|
1933 |
+ |
#ifdef ENABLE_XF86_DGA |
1934 |
+ |
if (display_type == DIS_SCREEN) { |
1935 |
+ |
current_dga_cmap ^= 1; |
1936 |
+ |
if (!IsDirectMode(mode) && cmap[current_dga_cmap]) |
1937 |
+ |
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
1938 |
+ |
} |
1939 |
+ |
#endif |
1940 |
+ |
} |
1941 |
+ |
|
1942 |
+ |
UNLOCK_PALETTE; |
1943 |
+ |
} |
1944 |
+ |
|
1945 |
|
static void *redraw_func(void *arg) |
1946 |
|
{ |
1947 |
< |
int tick_counter = 0; |
1520 |
< |
struct timespec req = {0, 16666667}; |
1947 |
> |
int fd = ConnectionNumber(x_display); |
1948 |
|
|
1949 |
< |
for (;;) { |
1949 |
> |
uint64 start = GetTicks_usec(); |
1950 |
> |
int64 ticks = 0; |
1951 |
> |
uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY; |
1952 |
|
|
1953 |
< |
// Wait |
1525 |
< |
nanosleep(&req, NULL); |
1953 |
> |
for (;;) { |
1954 |
|
|
1955 |
|
// Pause if requested (during video mode switches) |
1956 |
|
while (thread_stop_req) |
1957 |
|
thread_stop_ack = true; |
1958 |
|
|
1959 |
< |
// Handle X11 events |
1960 |
< |
handle_events(); |
1959 |
> |
int64 delay = next - GetTicks_usec(); |
1960 |
> |
if (delay < -VIDEO_REFRESH_DELAY) { |
1961 |
> |
|
1962 |
> |
// We are lagging far behind, so we reset the delay mechanism |
1963 |
> |
next = GetTicks_usec(); |
1964 |
|
|
1965 |
< |
// Quit DGA mode if requested |
1966 |
< |
if (quit_full_screen) { |
1967 |
< |
quit_full_screen = false; |
1968 |
< |
if (display_type == DIS_SCREEN) { |
1965 |
> |
} else if (delay <= 0) { |
1966 |
> |
|
1967 |
> |
// Delay expired, refresh display |
1968 |
> |
next += VIDEO_REFRESH_DELAY; |
1969 |
> |
ticks++; |
1970 |
> |
|
1971 |
> |
// Handle X11 events |
1972 |
> |
handle_events(); |
1973 |
> |
|
1974 |
> |
// Quit DGA mode if requested |
1975 |
> |
if (quit_full_screen) { |
1976 |
> |
quit_full_screen = false; |
1977 |
> |
if (display_type == DIS_SCREEN) { |
1978 |
> |
XDisplayLock(); |
1979 |
|
#ifdef ENABLE_XF86_DGA |
1980 |
< |
XF86DGADirectVideo(x_display, screen, 0); |
1981 |
< |
XUngrabPointer(x_display, CurrentTime); |
1982 |
< |
XUngrabKeyboard(x_display, CurrentTime); |
1983 |
< |
#endif |
1984 |
< |
XSync(x_display, false); |
1985 |
< |
quit_full_screen_ack = true; |
1986 |
< |
return NULL; |
1980 |
> |
XF86DGADirectVideo(x_display, screen, 0); |
1981 |
> |
XUngrabPointer(x_display, CurrentTime); |
1982 |
> |
XUngrabKeyboard(x_display, CurrentTime); |
1983 |
> |
#endif |
1984 |
> |
XSync(x_display, false); |
1985 |
> |
XDisplayUnlock(); |
1986 |
> |
quit_full_screen_ack = true; |
1987 |
> |
return NULL; |
1988 |
> |
} |
1989 |
|
} |
1547 |
– |
} |
1990 |
|
|
1991 |
< |
// Refresh display and set cursor image in window mode |
1992 |
< |
if (display_type == DIS_WINDOW) { |
1993 |
< |
tick_counter++; |
1994 |
< |
if (tick_counter >= frame_skip) { |
1995 |
< |
tick_counter = 0; |
1991 |
> |
// Refresh display and set cursor image in window mode |
1992 |
> |
static int tick_counter = 0; |
1993 |
> |
if (display_type == DIS_WINDOW) { |
1994 |
> |
tick_counter++; |
1995 |
> |
if (tick_counter >= frame_skip) { |
1996 |
> |
tick_counter = 0; |
1997 |
|
|
1998 |
< |
// Update display |
1998 |
> |
// Update display |
1999 |
|
#ifdef ENABLE_VOSF |
2000 |
< |
if (use_vosf) { |
2001 |
< |
if (mainBuffer.dirty) { |
2002 |
< |
LOCK_VOSF; |
2003 |
< |
update_display_window_vosf(); |
2004 |
< |
UNLOCK_VOSF; |
2005 |
< |
XSync(x_display, false); // Let the server catch up |
2000 |
> |
if (use_vosf) { |
2001 |
> |
XDisplayLock(); |
2002 |
> |
if (mainBuffer.dirty) { |
2003 |
> |
LOCK_VOSF; |
2004 |
> |
update_display_window_vosf(); |
2005 |
> |
UNLOCK_VOSF; |
2006 |
> |
XSync(x_display, false); // Let the server catch up |
2007 |
> |
} |
2008 |
> |
XDisplayUnlock(); |
2009 |
|
} |
2010 |
< |
} |
1565 |
< |
else |
2010 |
> |
else |
2011 |
|
#endif |
2012 |
< |
update_display(); |
2012 |
> |
update_display(); |
2013 |
|
|
2014 |
< |
// Set new cursor image if it was changed |
2015 |
< |
if (cursor_changed) { |
2016 |
< |
cursor_changed = false; |
2017 |
< |
memcpy(cursor_image->data, MacCursor + 4, 32); |
2018 |
< |
memcpy(cursor_mask_image->data, MacCursor + 36, 32); |
2019 |
< |
XFreeCursor(x_display, mac_cursor); |
2020 |
< |
XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16); |
2021 |
< |
XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16); |
2022 |
< |
mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]); |
2023 |
< |
XDefineCursor(x_display, the_win, mac_cursor); |
2014 |
> |
// Set new cursor image if it was changed |
2015 |
> |
if (cursor_changed) { |
2016 |
> |
cursor_changed = false; |
2017 |
> |
memcpy(cursor_image->data, MacCursor + 4, 32); |
2018 |
> |
memcpy(cursor_mask_image->data, MacCursor + 36, 32); |
2019 |
> |
XDisplayLock(); |
2020 |
> |
XFreeCursor(x_display, mac_cursor); |
2021 |
> |
XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16); |
2022 |
> |
XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16); |
2023 |
> |
mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]); |
2024 |
> |
XDefineCursor(x_display, the_win, mac_cursor); |
2025 |
> |
XDisplayUnlock(); |
2026 |
> |
} |
2027 |
|
} |
2028 |
|
} |
1581 |
– |
} |
2029 |
|
#ifdef ENABLE_VOSF |
2030 |
< |
else if (use_vosf) { |
2031 |
< |
// Update display (VOSF variant) |
2032 |
< |
static int tick_counter = 0; |
2033 |
< |
if (++tick_counter >= frame_skip) { |
2034 |
< |
tick_counter = 0; |
2035 |
< |
if (mainBuffer.dirty) { |
2036 |
< |
LOCK_VOSF; |
2037 |
< |
update_display_dga_vosf(); |
2038 |
< |
UNLOCK_VOSF; |
2030 |
> |
else if (use_vosf) { |
2031 |
> |
// Update display (VOSF variant) |
2032 |
> |
if (++tick_counter >= frame_skip) { |
2033 |
> |
tick_counter = 0; |
2034 |
> |
if (mainBuffer.dirty) { |
2035 |
> |
LOCK_VOSF; |
2036 |
> |
update_display_dga_vosf(); |
2037 |
> |
UNLOCK_VOSF; |
2038 |
> |
} |
2039 |
|
} |
2040 |
|
} |
1594 |
– |
} |
2041 |
|
#endif |
2042 |
|
|
2043 |
< |
// Set new palette if it was changed |
2044 |
< |
if (palette_changed && !emul_suspended) { |
2045 |
< |
palette_changed = false; |
2046 |
< |
XColor c[256]; |
2047 |
< |
for (int i=0; i<256; i++) { |
2048 |
< |
c[i].pixel = i; |
2049 |
< |
c[i].red = mac_pal[i].red * 0x0101; |
2050 |
< |
c[i].green = mac_pal[i].green * 0x0101; |
2051 |
< |
c[i].blue = mac_pal[i].blue * 0x0101; |
2052 |
< |
c[i].flags = DoRed | DoGreen | DoBlue; |
2053 |
< |
} |
2054 |
< |
if (depth == 8) { |
2055 |
< |
XStoreColors(x_display, cmap[0], c, 256); |
2056 |
< |
XStoreColors(x_display, cmap[1], c, 256); |
1611 |
< |
#ifdef ENABLE_XF86_DGA |
1612 |
< |
if (display_type == DIS_SCREEN) { |
1613 |
< |
current_dga_cmap ^= 1; |
1614 |
< |
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
1615 |
< |
} |
1616 |
< |
#endif |
1617 |
< |
} |
2043 |
> |
// Set new palette if it was changed |
2044 |
> |
handle_palette_changes(); |
2045 |
> |
|
2046 |
> |
} else { |
2047 |
> |
|
2048 |
> |
// No display refresh pending, check for X events |
2049 |
> |
fd_set readfds; |
2050 |
> |
FD_ZERO(&readfds); |
2051 |
> |
FD_SET(fd, &readfds); |
2052 |
> |
struct timeval timeout; |
2053 |
> |
timeout.tv_sec = 0; |
2054 |
> |
timeout.tv_usec = delay; |
2055 |
> |
if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0) |
2056 |
> |
handle_events(); |
2057 |
|
} |
2058 |
|
} |
2059 |
|
return NULL; |