ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/video_x.cpp
(Generate patch)

Comparing SheepShaver/src/Unix/video_x.cpp (file contents):
Revision 1.9 by gbeauche, 2003-12-31T11:37:26Z vs.
Revision 1.14 by gbeauche, 2004-04-11T10:46:32Z

# Line 1 | Line 1
1   /*
2   *  video_x.cpp - Video/graphics emulation, X11 specific stuff
3   *
4 < *  SheepShaver (C) 1997-2003 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
# Line 29 | Line 29
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
# Line 48 | Line 50
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";
# Line 89 | Line 95 | static int screen;                                                     // Screen numbe
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;
# Line 123 | Line 135 | static XF86VidModeModeInfo **x_video_mod
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);
# Line 138 | Line 164 | extern void SysMountFirstFloppy(void);
164   // From clip_unix.cpp
165   extern void ClipboardSelectionClear(XSelectionClearEvent *);
166   extern void ClipboardSelectionRequest(XSelectionRequestEvent *);
141 extern void ClipboardSelectionNotify(XSelectionEvent *req);
167  
168  
169   // Video acceleration through SIGSEGV
# Line 148 | Line 173 | extern void ClipboardSelectionNotify(XSe
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 + // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
344 + static Atom WM_DELETE_WINDOW = (Atom)0;
345 + static void set_window_delete_protocol(Window w)
346 + {
347 +        WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
348 +        XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
349 + }
350 +
351 + // Wait until window is mapped/unmapped
352 + void wait_mapped(Window w)
353 + {
354 +        XEvent e;
355 +        do {
356 +                XMaskEvent(x_display, StructureNotifyMask, &e);
357 +        } while ((e.type != MapNotify) || (e.xmap.event != w));
358 + }
359 +
360 + void wait_unmapped(Window w)
361 + {
362 +        XEvent e;
363 +        do {
364 +                XMaskEvent(x_display, StructureNotifyMask, &e);
365 +        } while ((e.type != UnmapNotify) || (e.xmap.event != w));
366 + }
367 +
368   // Trap SHM errors
369   static bool shm_error = false;
370   static int (*old_error_handler)(Display *, XErrorEvent *);
# Line 176 | Line 390 | static bool open_window(int width, int h
390          // Create window
391          XSetWindowAttributes wattr;
392          wattr.event_mask = eventmask = win_eventmask;
393 <        wattr.background_pixel = black_pixel;
394 <        wattr.border_pixel = black_pixel;
393 >        wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
394 >        wattr.border_pixel = 0;
395          wattr.backing_store = NotUseful;
396 <
183 <        XSync(x_display, false);
396 >        wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
397          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
398 <                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore, &wattr);
399 <        XSync(x_display, false);
398 >                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr);
399 >
400 >        // Set window name
401          XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
188        XMapRaised(x_display, the_win);
189        XSync(x_display, false);
402  
403 <        // Set colormap
404 <        if (depth == 8) {
193 <                XSetWindowColormap(x_display, the_win, cmap[0]);
194 <                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
195 <        }
403 >        // Set delete protocol property
404 >        set_window_delete_protocol(the_win);
405  
406          // Make window unresizable
407          XSizeHints *hints;
# Line 206 | Line 415 | static bool open_window(int width, int h
415                  XFree((char *)hints);
416          }
417  
418 +        // Show window
419 +        XMapWindow(x_display, the_win);
420 +        wait_mapped(the_win);
421 +
422          // 1-bit mode is big-endian; if the X server is little-endian, we can't
423          // use SHM because that doesn't allow changing the image byte order
424          bool need_msb_image = (depth == 1 && XImageByteOrder(x_display) == LSBFirst);
# Line 216 | Line 429 | static bool open_window(int width, int h
429  
430                  // Create SHM image ("height + 2" for safety)
431                  img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
432 <                shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
432 >                shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
433 >                D(bug(" shm image created\n"));
434                  the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
435                  shminfo.shmaddr = img->data = (char *)the_buffer_copy;
436                  shminfo.readOnly = False;
# Line 235 | Line 449 | static bool open_window(int width, int h
449                          have_shm = true;
450                          shmctl(shminfo.shmid, IPC_RMID, 0);
451                  }
452 +                D(bug(" shm image attached\n"));
453          }
454  
455          // Create normal X image if SHM doesn't work ("height + 2" for safety)
456          if (!have_shm) {
457 <                int bytes_per_row = aligned_width;
243 <                switch (depth) {
244 <                        case 1:
245 <                                bytes_per_row /= 8;
246 <                                break;
247 <                        case 15:
248 <                        case 16:
249 <                                bytes_per_row *= 2;
250 <                                break;
251 <                        case 24:
252 <                        case 32:
253 <                                bytes_per_row *= 4;
254 <                                break;
255 <                }
457 >                int bytes_per_row = depth == 1 ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth));
458                  the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
459                  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);
460 +                D(bug(" X image created\n"));
461          }
462  
463          // 1-Bit mode is big-endian
464 <    if (depth == 1) {
464 >    if (need_msb_image) {
465          img->byte_order = MSBFirst;
466          img->bitmap_bit_order = MSBFirst;
467      }
# Line 280 | Line 483 | static bool open_window(int width, int h
483  
484          // Create GC
485          the_gc = XCreateGC(x_display, the_win, 0, 0);
486 <        XSetForeground(x_display, the_gc, black_pixel);
486 >        XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
487  
488          // Create cursor
489          cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
# Line 308 | Line 511 | static bool open_window(int width, int h
511   #endif
512  
513          // Set bytes per row
311        VModes[cur_mode].viRowBytes = img->bytes_per_line;
514          XSync(x_display, false);
515          return true;
516   }
# Line 347 | Line 549 | static bool open_dga(int width, int heig
549                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
550  
551          // Set bytes per row
552 <        int bytes_per_row = (dga_fb_width + 7) & ~7;
351 <        switch (depth) {
352 <                case 15:
353 <                case 16:
354 <                        bytes_per_row *= 2;
355 <                        break;
356 <                case 24:
357 <                case 32:
358 <                        bytes_per_row *= 4;
359 <                        break;
360 <        }
552 >        int bytes_per_row = TrivialBytesPerRow((dga_fb_width + 7) & ~7, DepthModeForPixelDepth(depth));
553  
554   #if ENABLE_VOSF
555          bool native_byte_order;
# Line 396 | Line 588 | static bool open_dga(int width, int heig
588  
589   static bool open_display(void)
590   {
591 <        display_type = VModes[cur_mode].viType;
592 <        switch (VModes[cur_mode].viAppleMode) {
593 <                case APPLE_1_BIT:
594 <                        depth = 1;
595 <                        break;
596 <                case APPLE_2_BIT:
597 <                        depth = 2;
598 <                        break;
599 <                case APPLE_4_BIT:
600 <                        depth = 4;
601 <                        break;
602 <                case APPLE_8_BIT:
603 <                        depth = 8;
604 <                        break;
605 <                case APPLE_16_BIT:
606 <                        depth = xdepth == 15 ? 15 : 16;
607 <                        break;
608 <                case APPLE_32_BIT:
609 <                        depth = 32;
610 <                        break;
591 >        D(bug("open_display()\n"));
592 >        const VideoInfo &mode = VModes[cur_mode];
593 >
594 >        // Find best available X visual
595 >        if (!find_visual_for_depth(mode.viAppleMode)) {
596 >                ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
597 >                return false;
598 >        }
599 >
600 >        // Create color maps
601 >        if (color_class == PseudoColor || color_class == DirectColor) {
602 >                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
603 >                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
604 >        } else {
605 >                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
606 >                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
607 >        }
608 >
609 >        // Find pixel format of direct modes
610 >        if (color_class == DirectColor || color_class == TrueColor) {
611 >                rshift = gshift = bshift = 0;
612 >                rloss = gloss = bloss = 8;
613 >                uint32 mask;
614 >                for (mask=vis->red_mask; !(mask&1); mask>>=1)
615 >                        ++rshift;
616 >                for (; mask&1; mask>>=1)
617 >                        --rloss;
618 >                for (mask=vis->green_mask; !(mask&1); mask>>=1)
619 >                        ++gshift;
620 >                for (; mask&1; mask>>=1)
621 >                        --gloss;
622 >                for (mask=vis->blue_mask; !(mask&1); mask>>=1)
623 >                        ++bshift;
624 >                for (; mask&1; mask>>=1)
625 >                        --bloss;
626 >        }
627 >
628 >        // Preset palette pixel values for CLUT or gamma table
629 >        if (color_class == DirectColor) {
630 >                int num = vis->map_entries;
631 >                for (int i=0; i<num; i++) {
632 >                        int c = (i * 256) / num;
633 >                        x_palette[i].pixel = map_rgb(c, c, c);
634 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
635 >                }
636 >        } else if (color_class == PseudoColor) {
637 >                for (int i=0; i<256; i++) {
638 >                        x_palette[i].pixel = i;
639 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
640 >                }
641 >        }
642 >
643 >        // Load gray ramp to color map
644 >        int num = (color_class == DirectColor ? vis->map_entries : 256);
645 >        for (int i=0; i<num; i++) {
646 >                int c = (i * 256) / num;
647 >                x_palette[i].red = c * 0x0101;
648 >                x_palette[i].green = c * 0x0101;
649 >                x_palette[i].blue = c * 0x0101;
650 >        }
651 >        if (color_class == PseudoColor || color_class == DirectColor) {
652 >                XStoreColors(x_display, cmap[0], x_palette, num);
653 >                XStoreColors(x_display, cmap[1], x_palette, num);
654          }
655  
656 + #ifdef ENABLE_VOSF
657 +        // Load gray ramp to 8->16/32 expand map
658 +        if (!IsDirectMode(get_current_mode()) && xdepth > 8)
659 +                for (int i=0; i<256; i++)
660 +                        ExpandMap[i] = map_rgb(i, i, i);
661 + #endif
662 +
663 +        // Create display of requested type
664 +        display_type = mode.viType;
665 +        depth = depth_of_video_mode(mode.viAppleMode);
666 +
667          bool display_open = false;
668          if (display_type == DIS_SCREEN)
669                  display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
# Line 466 | Line 712 | static void close_window(void)
712                  XFreeGC(x_display, the_gc);
713  
714          // Close window
715 <        XDestroyWindow(x_display, the_win);
715 >        if (the_win) {
716 >                XUnmapWindow(x_display, the_win);
717 >                wait_unmapped(the_win);
718 >                XDestroyWindow(x_display, the_win);
719 >        }
720 >
721 >        XFlush(x_display);
722 >        XSync(x_display, false);
723   }
724  
725   // Close DGA mode
# Line 502 | Line 755 | static void close_display(void)
755          else if (display_type == DIS_WINDOW)
756                  close_window();
757  
758 +        // Free colormaps
759 +        if (cmap[0]) {
760 +                XFreeColormap(x_display, cmap[0]);
761 +                cmap[0] = 0;
762 +        }
763 +        if (cmap[1]) {
764 +                XFreeColormap(x_display, cmap[1]);
765 +                cmap[1] = 0;
766 +        }
767 +
768   #ifdef ENABLE_VOSF
769          if (use_vosf) {
770                  // Deinitialize VOSF
# Line 608 | Line 871 | static void keycode_init(void)
871          }
872   }
873  
874 < static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, long apple_mode, long apple_id, int type)
874 > // Add mode to list of supported modes
875 > static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
876   {
877          if (allow & test) {
878                  p->viType = type;
# Line 640 | Line 904 | static void add_mode(VideoInfo *&p, uint
904                                  p->viYsize = 1200;
905                                  break;
906                  }
907 <                switch (apple_mode) {
644 <                        case APPLE_8_BIT:
645 <                                p->viRowBytes = p->viXsize;
646 <                                break;
647 <                        case APPLE_16_BIT:
648 <                                p->viRowBytes = p->viXsize * 2;
649 <                                break;
650 <                        case APPLE_32_BIT:
651 <                                p->viRowBytes = p->viXsize * 4;
652 <                                break;
653 <                }
907 >                p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
908                  p->viAppleMode = apple_mode;
909                  p->viAppleID = apple_id;
910                  p++;
911          }
912   }
913  
914 + // Add standard list of windowed modes for given color depth
915 + static void add_window_modes(VideoInfo *&p, int window_modes, int mode)
916 + {
917 +        add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);
918 +        add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);
919 + }
920 +
921   static bool has_mode(int x, int y)
922   {
923   #ifdef ENABLE_XF86_VIDMODE
# Line 695 | Line 956 | bool VideoInit(void)
956  
957          // Init variables
958          private_data = NULL;
698        cur_mode = 0;   // Window 640x480
959          video_activated = true;
960  
961          // Find screen and root window
962          screen = XDefaultScreen(x_display);
963          rootwin = XRootWindow(x_display, screen);
964  
965 +        // Get sorted list of available depths
966 +        avail_depths = XListDepths(x_display, screen, &num_depths);
967 +        if (avail_depths == NULL) {
968 +                ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
969 +                return false;
970 +        }
971 +        sort(avail_depths, avail_depths + num_depths);
972 +        
973          // Get screen depth
974          xdepth = DefaultDepth(x_display, screen);
975  
# Line 732 | Line 1000 | bool VideoInit(void)
1000          black_pixel = BlackPixel(x_display, screen);
1001          white_pixel = WhitePixel(x_display, screen);
1002  
735        // Get appropriate visual
736        int color_class;
737        switch (xdepth) {
738 #if 0
739                case 1:
740                        color_class = StaticGray;
741                        break;
742 #endif
743                case 8:
744                        color_class = PseudoColor;
745                        break;
746                case 15:
747                case 16:
748                case 24:
749                case 32:
750                        color_class = TrueColor;
751                        break;
752                default:
753                        ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
754                        return false;
755        }
756        if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
757                ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
758                return false;
759        }
760        if (visualInfo.depth != xdepth) {
761                ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
762                return false;
763        }
764        vis = visualInfo.visual;
765
1003          // Mac screen depth follows X depth (for now)
1004 <        depth = xdepth;
1005 <
1006 <        // Create color maps for 8 bit mode
1007 <        if (depth == 8) {
1008 <                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1009 <                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1010 <                XInstallColormap(x_display, cmap[0]);
1011 <                XInstallColormap(x_display, cmap[1]);
1004 >        int default_mode = APPLE_8_BIT;
1005 >        switch (DefaultDepth(x_display, screen)) {
1006 >        case 1:
1007 >                default_mode = APPLE_1_BIT;
1008 >                break;
1009 >        case 8:
1010 >                default_mode = APPLE_8_BIT;
1011 >                break;
1012 >        case 15: case 16:
1013 >                default_mode = APPLE_16_BIT;
1014 >                break;
1015 >        case 24: case 32:
1016 >                default_mode = APPLE_32_BIT;
1017 >                break;
1018          }
1019  
1020          // Construct video mode table
778        int mode = APPLE_8_BIT;
779        int bpr_mult = 8;
780        switch (depth) {
781                case 1:
782                        mode = APPLE_1_BIT;
783                        bpr_mult = 1;
784                        break;
785                case 8:
786                        mode = APPLE_8_BIT;
787                        bpr_mult = 8;
788                        break;
789                case 15:
790                case 16:
791                        mode = APPLE_16_BIT;
792                        bpr_mult = 16;
793                        break;
794                case 24:
795                case 32:
796                        mode = APPLE_32_BIT;
797                        bpr_mult = 32;
798                        break;
799        }
800
1021          uint32 window_modes = PrefsFindInt32("windowmodes");
1022          uint32 screen_modes = PrefsFindInt32("screenmodes");
1023          if (!has_dga)
# Line 806 | Line 1026 | bool VideoInit(void)
1026                  window_modes |= 3;      // Allow at least 640x480 and 800x600 window modes
1027  
1028          VideoInfo *p = VModes;
1029 <        add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);
1030 <        add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);
1029 >        for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1030 >                if (find_visual_for_depth(d))
1031 >                        add_window_modes(p, window_modes, d);
1032 >
1033          if (has_vidmode) {
1034                  if (has_mode(640, 480))
1035 <                        add_mode(p, screen_modes, 1, mode, APPLE_640x480, DIS_SCREEN);
1035 >                        add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1036                  if (has_mode(800, 600))
1037 <                        add_mode(p, screen_modes, 2, mode, APPLE_800x600, DIS_SCREEN);
1037 >                        add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN);
1038                  if (has_mode(1024, 768))
1039 <                        add_mode(p, screen_modes, 4, mode, APPLE_1024x768, DIS_SCREEN);
1039 >                        add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN);
1040                  if (has_mode(1152, 900))
1041 <                        add_mode(p, screen_modes, 8, mode, APPLE_1152x900, DIS_SCREEN);
1041 >                        add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN);
1042                  if (has_mode(1280, 1024))
1043 <                        add_mode(p, screen_modes, 16, mode, APPLE_1280x1024, DIS_SCREEN);
1043 >                        add_mode(p, screen_modes, 16, default_mode, APPLE_1280x1024, DIS_SCREEN);
1044                  if (has_mode(1600, 1200))
1045 <                        add_mode(p, screen_modes, 32, mode, APPLE_1600x1200, DIS_SCREEN);
1045 >                        add_mode(p, screen_modes, 32, default_mode, APPLE_1600x1200, DIS_SCREEN);
1046          } else if (screen_modes) {
1047                  int xsize = DisplayWidth(x_display, screen);
1048                  int ysize = DisplayHeight(x_display, screen);
# Line 841 | Line 1063 | bool VideoInit(void)
1063                  p->viRowBytes = 0;
1064                  p->viXsize = xsize;
1065                  p->viYsize = ysize;
1066 <                p->viAppleMode = mode;
1066 >                p->viAppleMode = default_mode;
1067                  p->viAppleID = apple_id;
1068                  p++;
1069          }
# Line 851 | Line 1073 | bool VideoInit(void)
1073          p->viAppleMode = 0;
1074          p->viAppleID = 0;
1075  
1076 +        // Find default mode (window 640x480)
1077 +        cur_mode = -1;
1078 +        for (p = VModes; p->viType != DIS_INVALID; p++) {
1079 +                if (p->viType == DIS_WINDOW
1080 +                        && p->viAppleID == APPLE_W_640x480
1081 +                        && p->viAppleMode == default_mode) {
1082 +                        cur_mode = p - VModes;
1083 +                        break;
1084 +                }
1085 +        }
1086 +        assert(cur_mode != -1);
1087 +
1088 + #if DEBUG
1089 +        D(bug("Available video modes:\n"));
1090 +        for (p = VModes; p->viType != DIS_INVALID; p++) {
1091 +                int bits = depth_of_video_mode(p->viAppleMode);
1092 +                D(bug(" %dx%d (ID %02x), %d colors\n", p->viXsize, p->viYsize, p->viAppleID, 1 << bits));
1093 +        }
1094 + #endif
1095 +
1096   #ifdef ENABLE_XF86_DGA
1097          if (has_dga && screen_modes) {
1098                  int v_bank, v_size;
# Line 902 | Line 1144 | void VideoExit(void)
1144                  close_display();
1145                  XFlush(x_display);
1146                  XSync(x_display, false);
905                if (depth == 8) {
906                        XFreeColormap(x_display, cmap[0]);
907                        XFreeColormap(x_display, cmap[1]);
908                }
1147          }
1148   }
1149  
# Line 969 | Line 1207 | static void resume_emul(void)
1207          // Reopen full screen display
1208          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1209          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1210 + #ifdef ENABLE_XF86_DGA
1211          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1212          XF86DGASetViewPort(x_display, screen, 0, 0);
1213 + #endif
1214          XSync(x_display, false);
1215  
1216          // the_buffer already contains the data to restore. i.e. since a temporary
# Line 1186 | Line 1426 | static void handle_events(void)
1426          for (;;) {
1427                  XEvent event;
1428  
1429 +                XDisplayLock();
1430                  if (!XCheckMaskEvent(x_display, eventmask, &event)) {
1431                          // Handle clipboard events
1432                          if (XCheckTypedEvent(x_display, SelectionRequest, &event))
1433                                  ClipboardSelectionRequest(&event.xselectionrequest);
1434                          else if (XCheckTypedEvent(x_display, SelectionClear, &event))
1435                                  ClipboardSelectionClear(&event.xselectionclear);
1436 <                        else if (XCheckTypedEvent(x_display, SelectionNotify, &event))
1437 <                                ClipboardSelectionNotify(&event.xselection);
1436 >
1437 >                        // Window "close" widget clicked
1438 >                        else if (XCheckTypedEvent(x_display, ClientMessage, &event)) {
1439 >                                if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
1440 >                                        ADBKeyDown(0x7f);       // Power key
1441 >                                        ADBKeyUp(0x7f);
1442 >                                }
1443 >                        }
1444 >
1445 >                        XDisplayUnlock();
1446                          break;
1447                  }
1448 +                XDisplayUnlock();
1449  
1450                  switch (event.type) {
1451                          // Mouse button
# Line 1495 | Line 1745 | int16 video_mode_change(VidLocals *csSav
1745  
1746   void video_set_palette(void)
1747   {
1748 +        LOCK_PALETTE;
1749 +
1750 +        // Convert colors to XColor array
1751 +        int mode = get_current_mode();
1752 +        int num_in = palette_size(mode);
1753 +        int num_out = 256;
1754 +        bool stretch = false;
1755 +        if (IsDirectMode(mode)) {
1756 +                // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1757 +                num_out = vis->map_entries;
1758 +                stretch = true;
1759 +        }
1760 +        XColor *p = x_palette;
1761 +        for (int i=0; i<num_out; i++) {
1762 +                int c = (stretch ? (i * num_in) / num_out : i);
1763 +                p->red = mac_pal[c].red * 0x0101;
1764 +                p->green = mac_pal[c].green * 0x0101;
1765 +                p->blue = mac_pal[c].blue * 0x0101;
1766 +                p++;
1767 +        }
1768 +
1769 + #ifdef ENABLE_VOSF
1770 +        // Recalculate pixel color expansion map
1771 +        if (!IsDirectMode(mode) && xdepth > 8) {
1772 +                for (int i=0; i<256; i++) {
1773 +                        int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1774 +                        ExpandMap[i] = map_rgb(mac_pal[c].red, mac_pal[c].green, mac_pal[c].blue);
1775 +                }
1776 +
1777 +                // We have to redraw everything because the interpretation of pixel values changed
1778 +                LOCK_VOSF;
1779 +                PFLAG_SET_ALL;
1780 +                UNLOCK_VOSF;
1781 +                memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1782 +        }
1783 + #endif
1784 +
1785 +        // Tell redraw thread to change palette
1786          palette_changed = true;
1787 +
1788 +        UNLOCK_PALETTE;
1789   }
1790  
1791  
# Line 1622 | Line 1912 | static void update_display(void)
1912  
1913          // Refresh display
1914          if (high && wide) {
1915 +                XDisplayLock();
1916                  if (have_shm)
1917                          XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
1918                  else
1919                          XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1920 +                XDisplayUnlock();
1921          }
1922   }
1923  
1924 + const int VIDEO_REFRESH_HZ = 60;
1925 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
1926 +
1927 + static void handle_palette_changes(void)
1928 + {
1929 +        LOCK_PALETTE;
1930 +
1931 +        if (palette_changed && !emul_suspended) {
1932 +                palette_changed = false;
1933 +
1934 +                int mode = get_current_mode();
1935 +                if (color_class == PseudoColor || color_class == DirectColor) {
1936 +                        int num = vis->map_entries;
1937 +                        bool set_clut = true;
1938 +                        if (!IsDirectMode(mode) && color_class == DirectColor) {
1939 +                                if (display_type == DIS_WINDOW)
1940 +                                        set_clut = false; // Indexed mode on true color screen, don't set CLUT
1941 +                        }
1942 +
1943 +                        if (set_clut) {
1944 +                                XDisplayLock();
1945 +                                XStoreColors(x_display, cmap[0], x_palette, num);
1946 +                                XStoreColors(x_display, cmap[1], x_palette, num);
1947 +                                XSync(x_display, false);
1948 +                                XDisplayUnlock();
1949 +                        }
1950 +                }
1951 +
1952 + #ifdef ENABLE_XF86_DGA
1953 +                if (display_type == DIS_SCREEN) {
1954 +                        current_dga_cmap ^= 1;
1955 +                        if (!IsDirectMode(mode) && cmap[current_dga_cmap])
1956 +                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1957 +                }
1958 + #endif
1959 +        }
1960 +
1961 +        UNLOCK_PALETTE;
1962 + }
1963 +
1964   static void *redraw_func(void *arg)
1965   {
1966 <        int tick_counter = 0;
1635 <        struct timespec req = {0, 16666667};
1966 >        int fd = ConnectionNumber(x_display);
1967  
1968 <        for (;;) {
1968 >        uint64 start = GetTicks_usec();
1969 >        int64 ticks = 0;
1970 >        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
1971  
1972 <                // Wait
1640 <                nanosleep(&req, NULL);
1972 >        for (;;) {
1973  
1974                  // Pause if requested (during video mode switches)
1975                  while (thread_stop_req)
1976                          thread_stop_ack = true;
1977  
1978 <                // Handle X11 events
1979 <                handle_events();
1978 >                int64 delay = next - GetTicks_usec();
1979 >                if (delay < -VIDEO_REFRESH_DELAY) {
1980  
1981 <                // Quit DGA mode if requested
1982 <                if (quit_full_screen) {
1983 <                        quit_full_screen = false;
1984 <                        if (display_type == DIS_SCREEN) {
1981 >                        // We are lagging far behind, so we reset the delay mechanism
1982 >                        next = GetTicks_usec();
1983 >
1984 >                } else if (delay <= 0) {
1985 >
1986 >                        // Delay expired, refresh display
1987 >                        next += VIDEO_REFRESH_DELAY;
1988 >                        ticks++;
1989 >
1990 >                        // Handle X11 events
1991 >                        handle_events();
1992 >
1993 >                        // Quit DGA mode if requested
1994 >                        if (quit_full_screen) {
1995 >                                quit_full_screen = false;
1996 >                                if (display_type == DIS_SCREEN) {
1997 >                                        XDisplayLock();
1998   #ifdef ENABLE_XF86_DGA
1999 <                                XF86DGADirectVideo(x_display, screen, 0);
2000 <                                XUngrabPointer(x_display, CurrentTime);
2001 <                                XUngrabKeyboard(x_display, CurrentTime);
2002 < #endif
2003 <                                XSync(x_display, false);
2004 <                                quit_full_screen_ack = true;
2005 <                                return NULL;
1999 >                                        XF86DGADirectVideo(x_display, screen, 0);
2000 >                                        XUngrabPointer(x_display, CurrentTime);
2001 >                                        XUngrabKeyboard(x_display, CurrentTime);
2002 > #endif
2003 >                                        XSync(x_display, false);
2004 >                                        XDisplayUnlock();
2005 >                                        quit_full_screen_ack = true;
2006 >                                        return NULL;
2007 >                                }
2008                          }
1662                }
2009  
2010 <                // Refresh display and set cursor image in window mode
2011 <                if (display_type == DIS_WINDOW) {
2012 <                        tick_counter++;
2013 <                        if (tick_counter >= frame_skip) {
2014 <                                tick_counter = 0;
2010 >                        // Refresh display and set cursor image in window mode
2011 >                        static int tick_counter = 0;
2012 >                        if (display_type == DIS_WINDOW) {
2013 >                                tick_counter++;
2014 >                                if (tick_counter >= frame_skip) {
2015 >                                        tick_counter = 0;
2016  
2017 <                                // Update display
2017 >                                        // Update display
2018   #ifdef ENABLE_VOSF
2019 <                                if (use_vosf) {
2020 <                                        if (mainBuffer.dirty) {
2021 <                                                LOCK_VOSF;
2022 <                                                update_display_window_vosf();
2023 <                                                UNLOCK_VOSF;
2024 <                                                XSync(x_display, false); // Let the server catch up
2019 >                                        if (use_vosf) {
2020 >                                                XDisplayLock();
2021 >                                                if (mainBuffer.dirty) {
2022 >                                                        LOCK_VOSF;
2023 >                                                        update_display_window_vosf();
2024 >                                                        UNLOCK_VOSF;
2025 >                                                        XSync(x_display, false); // Let the server catch up
2026 >                                                }
2027 >                                                XDisplayUnlock();
2028                                          }
2029 <                                }
1680 <                                else
2029 >                                        else
2030   #endif
2031 <                                        update_display();
2031 >                                                update_display();
2032  
2033 <                                // Set new cursor image if it was changed
2034 <                                if (cursor_changed) {
2035 <                                        cursor_changed = false;
2036 <                                        memcpy(cursor_image->data, MacCursor + 4, 32);
2037 <                                        memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2038 <                                        XFreeCursor(x_display, mac_cursor);
2039 <                                        XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
2040 <                                        XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
2041 <                                        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2042 <                                        XDefineCursor(x_display, the_win, mac_cursor);
2033 >                                        // Set new cursor image if it was changed
2034 >                                        if (cursor_changed) {
2035 >                                                cursor_changed = false;
2036 >                                                memcpy(cursor_image->data, MacCursor + 4, 32);
2037 >                                                memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2038 >                                                XDisplayLock();
2039 >                                                XFreeCursor(x_display, mac_cursor);
2040 >                                                XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
2041 >                                                XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
2042 >                                                mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2043 >                                                XDefineCursor(x_display, the_win, mac_cursor);
2044 >                                                XDisplayUnlock();
2045 >                                        }
2046                                  }
2047                          }
1696                }
2048   #ifdef ENABLE_VOSF
2049 <                else if (use_vosf) {
2050 <                        // Update display (VOSF variant)
2051 <                        static int tick_counter = 0;
2052 <                        if (++tick_counter >= frame_skip) {
2053 <                                tick_counter = 0;
2054 <                                if (mainBuffer.dirty) {
2055 <                                        LOCK_VOSF;
2056 <                                        update_display_dga_vosf();
2057 <                                        UNLOCK_VOSF;
2049 >                        else if (use_vosf) {
2050 >                                // Update display (VOSF variant)
2051 >                                if (++tick_counter >= frame_skip) {
2052 >                                        tick_counter = 0;
2053 >                                        if (mainBuffer.dirty) {
2054 >                                                LOCK_VOSF;
2055 >                                                update_display_dga_vosf();
2056 >                                                UNLOCK_VOSF;
2057 >                                        }
2058                                  }
2059                          }
1709                }
2060   #endif
2061  
2062 <                // Set new palette if it was changed
2063 <                if (palette_changed && !emul_suspended) {
2064 <                        palette_changed = false;
2065 <                        XColor c[256];
2066 <                        for (int i=0; i<256; i++) {
2067 <                                c[i].pixel = i;
2068 <                                c[i].red = mac_pal[i].red * 0x0101;
2069 <                                c[i].green = mac_pal[i].green * 0x0101;
2070 <                                c[i].blue = mac_pal[i].blue * 0x0101;
2071 <                                c[i].flags = DoRed | DoGreen | DoBlue;
2072 <                        }
2073 <                        if (depth == 8) {
2074 <                                XStoreColors(x_display, cmap[0], c, 256);
2075 <                                XStoreColors(x_display, cmap[1], c, 256);
1726 < #ifdef ENABLE_XF86_DGA
1727 <                                if (display_type == DIS_SCREEN) {
1728 <                                        current_dga_cmap ^= 1;
1729 <                                        XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1730 <                                }
1731 < #endif
1732 <                        }
2062 >                        // Set new palette if it was changed
2063 >                        handle_palette_changes();
2064 >
2065 >                } else {
2066 >
2067 >                        // No display refresh pending, check for X events
2068 >                        fd_set readfds;
2069 >                        FD_ZERO(&readfds);
2070 >                        FD_SET(fd, &readfds);
2071 >                        struct timeval timeout;
2072 >                        timeout.tv_sec = 0;
2073 >                        timeout.tv_usec = delay;
2074 >                        if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2075 >                                handle_events();
2076                  }
2077          }
2078          return NULL;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines