29 |
|
#include <errno.h> |
30 |
|
#include <pthread.h> |
31 |
|
|
32 |
+ |
#include <algorithm> |
33 |
+ |
|
34 |
|
#ifdef ENABLE_XF86_DGA |
35 |
|
#include <X11/extensions/xf86dga.h> |
36 |
|
#endif |
50 |
|
#define DEBUG 0 |
51 |
|
#include "debug.h" |
52 |
|
|
53 |
+ |
#ifndef NO_STD_NAMESPACE |
54 |
+ |
using std::sort; |
55 |
+ |
#endif |
56 |
+ |
|
57 |
|
|
58 |
|
// Constants |
59 |
|
const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes"; |
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); |
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 *); |
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 |
< |
|
182 |
< |
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); |
185 |
< |
XSync(x_display, false); |
186 |
< |
XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); |
187 |
< |
XMapRaised(x_display, the_win); |
188 |
< |
XSync(x_display, false); |
390 |
> |
InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr); |
391 |
|
|
392 |
< |
// Set colormap |
393 |
< |
if (depth == 8) { |
192 |
< |
XSetWindowColormap(x_display, the_win, cmap[0]); |
193 |
< |
XSetWMColormapWindows(x_display, the_win, &the_win, 1); |
194 |
< |
} |
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); |
418 |
|
|
419 |
|
// Create SHM image ("height + 2" for safety) |
420 |
|
img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height); |
421 |
< |
shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777); |
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; |
242 |
< |
switch (depth) { |
243 |
< |
case 1: |
244 |
< |
bytes_per_row /= 8; |
245 |
< |
break; |
246 |
< |
case 15: |
247 |
< |
case 16: |
248 |
< |
bytes_per_row *= 2; |
249 |
< |
break; |
250 |
< |
case 24: |
251 |
< |
case 32: |
252 |
< |
bytes_per_row *= 4; |
253 |
< |
break; |
254 |
< |
} |
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 |
310 |
– |
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; |
350 |
< |
switch (depth) { |
351 |
< |
case 15: |
352 |
< |
case 16: |
353 |
< |
bytes_per_row *= 2; |
354 |
< |
break; |
355 |
< |
case 24: |
356 |
< |
case 32: |
357 |
< |
bytes_per_row *= 4; |
358 |
< |
break; |
359 |
< |
} |
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; |
587 |
< |
break; |
588 |
< |
case APPLE_4_BIT: |
589 |
< |
depth = 4; |
590 |
< |
break; |
591 |
< |
case APPLE_8_BIT: |
592 |
< |
depth = 8; |
593 |
< |
break; |
594 |
< |
case APPLE_16_BIT: |
595 |
< |
depth = xdepth == 15 ? 15 : 16; |
596 |
< |
break; |
597 |
< |
case APPLE_32_BIT: |
598 |
< |
depth = 32; |
599 |
< |
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 |
860 |
|
} |
861 |
|
} |
862 |
|
|
863 |
< |
static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, long apple_mode, long apple_id, int type) |
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) { |
643 |
< |
case APPLE_8_BIT: |
644 |
< |
p->viRowBytes = p->viXsize; |
645 |
< |
break; |
646 |
< |
case APPLE_16_BIT: |
647 |
< |
p->viRowBytes = p->viXsize * 2; |
648 |
< |
break; |
649 |
< |
case APPLE_32_BIT: |
650 |
< |
p->viRowBytes = p->viXsize * 4; |
651 |
< |
break; |
652 |
< |
} |
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 |
945 |
|
|
946 |
|
// Init variables |
947 |
|
private_data = NULL; |
697 |
– |
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 |
|
|
734 |
– |
// Get appropriate visual |
735 |
– |
int color_class; |
736 |
– |
switch (xdepth) { |
737 |
– |
#if 0 |
738 |
– |
case 1: |
739 |
– |
color_class = StaticGray; |
740 |
– |
break; |
741 |
– |
#endif |
742 |
– |
case 8: |
743 |
– |
color_class = PseudoColor; |
744 |
– |
break; |
745 |
– |
case 15: |
746 |
– |
case 16: |
747 |
– |
case 24: |
748 |
– |
case 32: |
749 |
– |
color_class = TrueColor; |
750 |
– |
break; |
751 |
– |
default: |
752 |
– |
ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR)); |
753 |
– |
return false; |
754 |
– |
} |
755 |
– |
if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) { |
756 |
– |
ErrorAlert(GetString(STR_NO_XVISUAL_ERR)); |
757 |
– |
return false; |
758 |
– |
} |
759 |
– |
if (visualInfo.depth != xdepth) { |
760 |
– |
ErrorAlert(GetString(STR_NO_XVISUAL_ERR)); |
761 |
– |
return false; |
762 |
– |
} |
763 |
– |
vis = visualInfo.visual; |
764 |
– |
|
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 |
777 |
– |
int mode = APPLE_8_BIT; |
778 |
– |
int bpr_mult = 8; |
779 |
– |
switch (depth) { |
780 |
– |
case 1: |
781 |
– |
mode = APPLE_1_BIT; |
782 |
– |
bpr_mult = 1; |
783 |
– |
break; |
784 |
– |
case 8: |
785 |
– |
mode = APPLE_8_BIT; |
786 |
– |
bpr_mult = 8; |
787 |
– |
break; |
788 |
– |
case 15: |
789 |
– |
case 16: |
790 |
– |
mode = APPLE_16_BIT; |
791 |
– |
bpr_mult = 16; |
792 |
– |
break; |
793 |
– |
case 24: |
794 |
– |
case 32: |
795 |
– |
mode = APPLE_32_BIT; |
796 |
– |
bpr_mult = 32; |
797 |
– |
break; |
798 |
– |
} |
799 |
– |
|
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); |
904 |
– |
if (depth == 8) { |
905 |
– |
XFreeColormap(x_display, cmap[0]); |
906 |
– |
XFreeColormap(x_display, cmap[1]); |
907 |
– |
} |
1136 |
|
} |
1137 |
|
} |
1138 |
|
|
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 |
|
|
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 fd = ConnectionNumber(x_display); |
2041 |
|
#endif |
2042 |
|
|
2043 |
|
// Set new palette if it was changed |
2044 |
< |
if (palette_changed && !emul_suspended) { |
1740 |
< |
palette_changed = false; |
1741 |
< |
XColor c[256]; |
1742 |
< |
for (int i=0; i<256; i++) { |
1743 |
< |
c[i].pixel = i; |
1744 |
< |
c[i].red = mac_pal[i].red * 0x0101; |
1745 |
< |
c[i].green = mac_pal[i].green * 0x0101; |
1746 |
< |
c[i].blue = mac_pal[i].blue * 0x0101; |
1747 |
< |
c[i].flags = DoRed | DoGreen | DoBlue; |
1748 |
< |
} |
1749 |
< |
if (depth == 8) { |
1750 |
< |
XDisplayLock(); |
1751 |
< |
XStoreColors(x_display, cmap[0], c, 256); |
1752 |
< |
XStoreColors(x_display, cmap[1], c, 256); |
1753 |
< |
#ifdef ENABLE_XF86_DGA |
1754 |
< |
if (display_type == DIS_SCREEN) { |
1755 |
< |
current_dga_cmap ^= 1; |
1756 |
< |
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
1757 |
< |
} |
1758 |
< |
#endif |
1759 |
< |
XDisplayUnlock(); |
1760 |
< |
} |
1761 |
< |
} |
2044 |
> |
handle_palette_changes(); |
2045 |
|
|
2046 |
|
} else { |
2047 |
|
|