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

Comparing BasiliskII/src/Unix/video_x.cpp (file contents):
Revision 1.46 by cebix, 2001-06-30T22:23:44Z vs.
Revision 1.55 by cebix, 2001-07-06T22:37:23Z

# Line 24 | Line 24
24   *      Ctrl-Tab = suspend DGA mode
25   *      Ctrl-Esc = emergency quit
26   *      Ctrl-F1 = mount floppy
27 + *      Ctrl-F5 = grab mouse (in windowed mode)
28   */
29  
30   #include "sysdeps.h"
# Line 36 | Line 37
37   #include <sys/shm.h>
38   #include <errno.h>
39  
40 + #include <algorithm>
41 +
42 + #ifndef NO_STD_NAMESPACE
43 + using std::sort;
44 + #endif
45 +
46   #ifdef HAVE_PTHREADS
47   # include <pthread.h>
48   #endif
# Line 73 | Line 80 | enum {
80   // Constants
81   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
82  
83 < static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
83 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | StructureNotifyMask;
84   static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
85  
86  
# Line 115 | Line 122 | static int keycode_table[256];                                         // X
122  
123   // X11 variables
124   static int screen;                                                                      // Screen number
118 static int xdepth;                                                                      // Depth of X screen
125   static Window rootwin;                                                          // Root window and our window
126 < static XVisualInfo visualInfo;
127 < static Visual *vis;
122 < static Colormap cmap[2] = {0, 0};                                       // Colormaps for indexed modes (DGA needs two of them)
126 > static int num_depths = 0;                                                      // Number of available X depths
127 > static int *avail_depths = NULL;                                        // List of available X depths
128   static XColor black, white;
129   static unsigned long black_pixel, white_pixel;
130   static int eventmask;
131  
132 < static XColor palette[256];                                                     // Color palette for indexed modes
132 > static int xdepth;                                                                      // Depth of X screen
133 > static XVisualInfo visualInfo;
134 > static Visual *vis;
135 > static int color_class;
136 >
137 > static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
138 >
139 > static Colormap cmap[2] = {0, 0};                                       // Colormaps for indexed modes (DGA needs two of them)
140 >
141 > static XColor palette[256];                                                     // Color palette to be used as CLUT and gamma table
142   static bool palette_changed = false;                            // Flag: Palette changed, redraw thread must set new colors
143  
144   #ifdef ENABLE_FBDEV_DGA
# Line 183 | Line 197 | extern void SysMountFirstFloppy(void);
197   *  Utility functions
198   */
199  
200 + // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
201 + static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
202 + {
203 +        return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift);
204 + }
205 +
206 + // Do we have a visual for handling the specified Mac depth? If so, set the
207 + // global variables "xdepth", "visualInfo", "vis" and "color_class".
208 + static bool find_visual_for_depth(video_depth depth)
209 + {
210 +        D(bug("have_visual_for_depth(%d)\n", 1 << depth));
211 +
212 +        // Calculate minimum and maximum supported X depth
213 +        int min_depth = 1, max_depth = 32;
214 +        switch (depth) {
215 +                case VDEPTH_1BIT:       // 1-bit works always
216 +                        min_depth = 1;
217 +                        max_depth = 32;
218 +                        break;
219 + #ifdef ENABLE_VOSF
220 +                case VDEPTH_2BIT:
221 +                case VDEPTH_4BIT:       // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
222 +                case VDEPTH_8BIT:
223 +                        min_depth = 8;
224 +                        max_depth = 32;
225 +                        break;
226 + #else
227 +                case VDEPTH_2BIT:
228 +                case VDEPTH_4BIT:       // 2/4-bit requires VOSF blitters
229 +                        return false;
230 +                case VDEPTH_8BIT:       // 8-bit without VOSF requires an 8-bit visual
231 +                        min_depth = 8;
232 +                        max_depth = 8;
233 +                        break;
234 + #endif
235 +                case VDEPTH_16BIT:      // 16-bit requires a 15/16-bit visual
236 +                        min_depth = 15;
237 +                        max_depth = 16;
238 +                        break;
239 +                case VDEPTH_32BIT:      // 32-bit requires a 24/32-bit visual
240 +                        min_depth = 24;
241 +                        max_depth = 32;
242 +                        break;
243 +        }
244 +        D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth));
245 +
246 +        // Try to find a visual for one of the color depths
247 +        bool visual_found = false;
248 +        for (int i=0; i<num_depths && !visual_found; i++) {
249 +
250 +                xdepth = avail_depths[i];
251 +                D(bug(" trying to find visual for depth %d\n", xdepth));
252 +                if (xdepth < min_depth || xdepth > max_depth)
253 +                        continue;
254 +
255 +                // Determine best color class for this depth
256 +                switch (xdepth) {
257 +                        case 1: // Try StaticGray or StaticColor
258 +                                if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo)
259 +                                 || XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo))
260 +                                        visual_found = true;
261 +                                break;
262 +                        case 8: // Need PseudoColor
263 +                                if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo))
264 +                                        visual_found = true;
265 +                                break;
266 +                        case 15:
267 +                        case 16:
268 +                        case 24:
269 +                        case 32: // Try DirectColor first, as this will allow gamma correction
270 +                                if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo)
271 +                                 || XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo))
272 +                                        visual_found = true;
273 +                                break;
274 +                        default:
275 +                                D(bug("  not a supported depth\n"));
276 +                                break;
277 +                }
278 +        }
279 +        if (!visual_found)
280 +                return false;
281 +
282 +        // Visual was found
283 +        vis = visualInfo.visual;
284 +        color_class = visualInfo.c_class;
285 +        D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth));
286 + #if DEBUG
287 +        switch (color_class) {
288 +                case StaticGray: D(bug("StaticGray\n")); break;
289 +                case GrayScale: D(bug("GrayScale\n")); break;
290 +                case StaticColor: D(bug("StaticColor\n")); break;
291 +                case PseudoColor: D(bug("PseudoColor\n")); break;
292 +                case TrueColor: D(bug("TrueColor\n")); break;
293 +                case DirectColor: D(bug("DirectColor\n")); break;
294 +        }
295 + #endif
296 + }
297 +
298   // Add mode to list of supported modes
299   static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth)
300   {
# Line 195 | Line 307 | static void add_mode(uint32 width, uint3
307          VideoModes.push_back(mode);
308   }
309  
310 + // Add standard list of windowed modes for given color depth
311 + static void add_window_modes(video_depth depth)
312 + {
313 +        add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth);
314 +        add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth);
315 +        add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth);
316 +        add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth), depth);
317 +        add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth);
318 +        add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth);
319 +        add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth);
320 + }
321 +
322   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
323   static void set_mac_frame_buffer(video_depth depth, bool native_byte_order)
324   {
# Line 203 | Line 327 | static void set_mac_frame_buffer(video_d
327          if (depth == VDEPTH_16BIT)
328                  layout = (xdepth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
329          else if (depth == VDEPTH_32BIT)
330 <                layour = (xdepth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
330 >                layout = (xdepth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
331          if (native_byte_order)
332                  MacFrameLayout = layout;
333          else
# Line 216 | Line 340 | static void set_mac_frame_buffer(video_d
340          InitFrameBufferMapping();
341   #else
342          VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
219        D(bug("Host frame buffer = %p, ", the_buffer));
343   #endif
344          D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base));
345   }
# Line 302 | Line 425 | public:
425          virtual void update_palette(void);
426          virtual void suspend(void) {}
427          virtual void resume(void) {}
428 +        virtual void toggle_mouse_grab(void) {}
429 +        virtual void mouse_moved(int x, int y) { ADBMouseMoved(x, y); }
430 +
431 +        virtual void grab_mouse(void) {}
432 +        virtual void ungrab_mouse(void) {}
433  
434   public:
435          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
# Line 322 | Line 450 | public:
450          driver_window(const video_mode &mode);
451          ~driver_window();
452  
453 +        void toggle_mouse_grab(void);
454 +        void mouse_moved(int x, int y);
455 +
456 +        void grab_mouse(void);
457 +        void ungrab_mouse(void);
458 +
459   private:
460          GC gc;
461          XImage *img;
462 <        bool have_shm;                          // Flag: SHM extensions available
462 >        bool have_shm;                                  // Flag: SHM extensions available
463          XShmSegmentInfo shminfo;
464          Cursor mac_cursor;
465 +        bool mouse_grabbed;                             // Flag: mouse pointer grabbed, using relative mouse mode
466 +        int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode)
467   };
468  
469   static driver_base *drv = NULL; // Pointer to currently used driver object
# Line 345 | Line 481 | driver_base::driver_base()
481  
482   driver_base::~driver_base()
483   {
484 +        ungrab_mouse();
485 +
486          if (w) {
487                  XUnmapWindow(x_display, w);
488                  wait_unmapped(w);
# Line 367 | Line 505 | driver_base::~driver_base()
505          }
506   #ifdef ENABLE_VOSF
507          else {
508 +                if (the_host_buffer) {
509 +                        free(the_host_buffer);
510 +                        the_host_buffer = NULL;
511 +                }
512                  if (the_buffer != (uint8 *)VM_MAP_FAILED) {
513                          vm_release(the_buffer, the_buffer_size);
514                          the_buffer = NULL;
# Line 383 | Line 525 | driver_base::~driver_base()
525   void driver_base::update_palette(void)
526   {
527          if (cmap[0] && cmap[1]) {
528 <                int num = 256;
529 <                if (vis->c_class == DirectColor && VideoMonitor.mode.depth == VDEPTH_16BIT)
530 <                        num = vis->map_entries;
528 >                int num = vis->map_entries;
529 >                if (!IsDirectMode(VideoMonitor.mode) && color_class == DirectColor)
530 >                        return; // Indexed mode on true color screen, don't set CLUT
531                  XStoreColors(x_display, cmap[0], palette, num);
532                  XStoreColors(x_display, cmap[1], palette, num);
533          }
# Line 399 | Line 541 | void driver_base::update_palette(void)
541  
542   // Open display
543   driver_window::driver_window(const video_mode &mode)
544 < : gc(0), img(NULL), have_shm(false), mac_cursor(0)
544 > : gc(0), img(NULL), have_shm(false), mouse_grabbed(false), mac_cursor(0)
545   {
546          int width = mode.x, height = mode.y;
547          int aligned_width = (width + 15) & ~15;
548          int aligned_height = (height + 15) & ~15;
549  
550          // Set absolute mouse mode
551 <        ADBSetRelMouseMode(false);
551 >        ADBSetRelMouseMode(mouse_grabbed);
552  
553          // Create window
554          XSetWindowAttributes wattr;
555          wattr.event_mask = eventmask = win_eventmask;
556          wattr.background_pixel = black_pixel;
557 <        wattr.colormap = cmap[0];
557 >        wattr.colormap = (mode.depth == VDEPTH_1BIT && color_class == PseudoColor ? DefaultColormap(x_display, screen) : cmap[0]);
558          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
559 <                InputOutput, vis, CWEventMask | CWBackPixel | ((mode.depth == VDEPTH_1BIT || cmap[0] == 0) ? 0 : CWColormap), &wattr);
559 >                InputOutput, vis, CWEventMask | CWBackPixel | (color_class == PseudoColor || color_class == DirectColor ? CWColormap : 0), &wattr);
560  
561          // Set window name/class
562          set_window_name(w, STR_WINDOW_TITLE);
# Line 443 | Line 585 | driver_window::driver_window(const video
585          XMapWindow(x_display, w);
586          wait_mapped(w);
587  
588 +        // 1-bit mode is big-endian; if the X server is little-endian, we can't
589 +        // use SHM because that doesn't allow changing the image byte order
590 +        bool need_msb_image = (mode.depth == VDEPTH_1BIT && XImageByteOrder(x_display) == LSBFirst);
591 +
592          // Try to create and attach SHM image
593 <        if (local_X11 && XShmQueryExtension(x_display)) {
593 >        if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) {
594  
595                  // Create SHM image ("height + 2" for safety)
596                  img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
# Line 462 | Line 608 | driver_window::driver_window(const video
608                  if (shm_error) {
609                          shmdt(shminfo.shmaddr);
610                          XDestroyImage(img);
611 +                        img = NULL;
612                          shminfo.shmid = -1;
613                  } else {
614                          have_shm = true;
# Line 471 | Line 618 | driver_window::driver_window(const video
618          
619          // Create normal X image if SHM doesn't work ("height + 2" for safety)
620          if (!have_shm) {
621 <                int bytes_per_row = TrivialBytesPerRow(aligned_width, mode.depth);
621 >                int bytes_per_row = (mode.depth == VDEPTH_1BIT ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth)));
622                  the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
623                  img = XCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
624          }
625  
626 <        // 1-Bit mode is big-endian
480 <        if (mode.depth == VDEPTH_1BIT) {
626 >        if (need_msb_image) {
627                  img->byte_order = MSBFirst;
628                  img->bitmap_bit_order = MSBFirst;
629          }
# Line 488 | Line 634 | driver_window::driver_window(const video
634          the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
635          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
636          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
637 +        D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
638   #else
639          // Allocate memory for frame buffer
640          the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
641 +        D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
642   #endif
643  
644          // Create GC
# Line 512 | Line 660 | driver_window::driver_window(const video
660          native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
661   #endif
662   #ifdef ENABLE_VOSF
663 <        Screen_blitter_init(&visualInfo, native_byte_order);
663 >        Screen_blitter_init(&visualInfo, native_byte_order, mode.depth);
664   #endif
665  
666          // Set VideoMonitor
# Line 526 | Line 674 | driver_window::driver_window(const video
674   // Close display
675   driver_window::~driver_window()
676   {
529        if (img)
530                XDestroyImage(img);
677          if (have_shm) {
678                  XShmDetach(x_display, &shminfo);
679 + #ifdef ENABLE_VOSF
680 +                the_host_buffer = NULL; // don't free() in driver_base dtor
681 + #else
682                  the_buffer_copy = NULL; // don't free() in driver_base dtor
683 + #endif
684 +        }
685 +        if (img)
686 +                XDestroyImage(img);
687 +        if (have_shm) {
688 +                shmdt(shminfo.shmaddr);
689 +                shmctl(shminfo.shmid, IPC_RMID, 0);
690          }
691          if (gc)
692                  XFreeGC(x_display, gc);
693   }
694  
695 + // Toggle mouse grab
696 + void driver_window::toggle_mouse_grab(void)
697 + {
698 +        if (mouse_grabbed)
699 +                ungrab_mouse();
700 +        else
701 +                grab_mouse();
702 + }
703 +
704 + // Grab mouse, switch to relative mouse mode
705 + void driver_window::grab_mouse(void)
706 + {
707 +        int result;
708 +        for (int i=0; i<10; i++) {
709 +                result = XGrabPointer(x_display, w, True, 0,
710 +                        GrabModeAsync, GrabModeAsync, w, None, CurrentTime);
711 +                if (result != AlreadyGrabbed)
712 +                        break;
713 +                Delay_usec(100000);
714 +        }
715 +        if (result == GrabSuccess) {
716 +                ADBSetRelMouseMode(mouse_grabbed = true);
717 +                XStoreName(x_display, w, GetString(STR_WINDOW_TITLE_GRABBED));
718 +                XSync(x_display, false);
719 +        }
720 + }
721 +
722 + // Ungrab mouse, switch to absolute mouse mode
723 + void driver_window::ungrab_mouse(void)
724 + {
725 +        if (mouse_grabbed) {
726 +                XUngrabPointer(x_display, CurrentTime);
727 +                XStoreName(x_display, w, GetString(STR_WINDOW_TITLE));
728 +                ADBSetRelMouseMode(mouse_grabbed = false);
729 +        }
730 + }
731 +
732 + // Mouse moved
733 + void driver_window::mouse_moved(int x, int y)
734 + {
735 +        if (!mouse_grabbed) {
736 +                mouse_last_x = x; mouse_last_y = y;
737 +                ADBMouseMoved(x, y);
738 +                return;
739 +        }
740 +
741 +        // Warped mouse motion (this code is taken from SDL)
742 +
743 +        // Post first mouse event
744 +        int width = VideoMonitor.mode.x, height = VideoMonitor.mode.y;
745 +        int delta_x = x - mouse_last_x, delta_y = y - mouse_last_y;
746 +        mouse_last_x = x; mouse_last_y = y;
747 +        ADBMouseMoved(delta_x, delta_y);
748 +
749 +        // Only warp the pointer when it has reached the edge
750 +        const int MOUSE_FUDGE_FACTOR = 8;
751 +        if (x < MOUSE_FUDGE_FACTOR || x > (width - MOUSE_FUDGE_FACTOR)
752 +         || y < MOUSE_FUDGE_FACTOR || y > (height - MOUSE_FUDGE_FACTOR)) {
753 +                XEvent event;
754 +                while (XCheckTypedEvent(x_display, MotionNotify, &event)) {
755 +                        delta_x = x - mouse_last_x; delta_y = y - mouse_last_y;
756 +                        mouse_last_x = x; mouse_last_y = y;
757 +                        ADBMouseMoved(delta_x, delta_y);
758 +                }
759 +                mouse_last_x = width/2;
760 +                mouse_last_y = height/2;
761 +                XWarpPointer(x_display, None, w, 0, 0, 0, 0, mouse_last_x, mouse_last_y);
762 +                for (int i=0; i<10; i++) {
763 +                        XMaskEvent(x_display, PointerMotionMask, &event);
764 +                        if (event.xmotion.x > (mouse_last_x - MOUSE_FUDGE_FACTOR)
765 +                         && event.xmotion.x < (mouse_last_x + MOUSE_FUDGE_FACTOR)
766 +                         && event.xmotion.y > (mouse_last_y - MOUSE_FUDGE_FACTOR)
767 +                         && event.xmotion.y < (mouse_last_y + MOUSE_FUDGE_FACTOR))
768 +                                break;
769 +                }
770 +        }
771 + }
772 +
773  
774   #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
775   /*
# Line 614 | Line 848 | void driver_dga::resume(void)
848          XMapRaised(x_display, w);
849          wait_mapped(w);
850          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
851 <        XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
852 <        XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
851 >        XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
852 >        XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
853   #ifdef ENABLE_XF86_DGA
854          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
855          XF86DGASetViewPort(x_display, screen, 0, 0);
# Line 780 | Line 1014 | driver_fbdev::driver_fbdev(const video_m
1014   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1015          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1016          // i.e. the framebuffer update function is not Blit_Copy_Raw
1017 <        use_vosf = Screen_blitter_init(&visualInfo, true);
1017 >        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1018          
1019          if (use_vosf) {
1020            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
# Line 896 | Line 1130 | driver_xf86dga::driver_xf86dga(const vid
1130   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1131          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1132          // i.e. the framebuffer update function is not Blit_Copy_Raw
1133 <        use_vosf = Screen_blitter_init(&visualInfo, true);
1133 >        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1134          
1135          if (use_vosf) {
1136            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
# Line 1020 | Line 1254 | static void keycode_init(void)
1254   // Open display for specified mode
1255   static bool video_open(const video_mode &mode)
1256   {
1257 +        // Find best available X visual
1258 +        if (!find_visual_for_depth(mode.depth)) {
1259 +                ErrorAlert(STR_NO_XVISUAL_ERR);
1260 +                return false;
1261 +        }
1262 +
1263 +        // Create color maps
1264 +        if (color_class == PseudoColor || color_class == DirectColor) {
1265 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1266 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1267 +        }
1268 +
1269 +        // Find pixel format of direct modes
1270 +        if (color_class == DirectColor || color_class == TrueColor) {
1271 +                rshift = gshift = bshift = 0;
1272 +                rloss = gloss = bloss = 8;
1273 +                uint32 mask;
1274 +                for (mask=vis->red_mask; !(mask&1); mask>>=1)
1275 +                        ++rshift;
1276 +                for (; mask&1; mask>>=1)
1277 +                        --rloss;
1278 +                for (mask=vis->green_mask; !(mask&1); mask>>=1)
1279 +                        ++gshift;
1280 +                for (; mask&1; mask>>=1)
1281 +                        --gloss;
1282 +                for (mask=vis->blue_mask; !(mask&1); mask>>=1)
1283 +                        ++bshift;
1284 +                for (; mask&1; mask>>=1)
1285 +                        --bloss;
1286 +        }
1287 +
1288 +        // Preset palette pixel values for gamma table
1289 +        if (color_class == DirectColor) {
1290 +                int num = vis->map_entries;
1291 +                for (int i=0; i<num; i++) {
1292 +                        int c = (i * 256) / num;
1293 +                        palette[i].pixel = map_rgb(c, c, c);
1294 +                }
1295 +        }
1296 +
1297 +        // Load gray ramp to color map
1298 +        int num = (color_class == DirectColor ? vis->map_entries : 256);
1299 +        for (int i=0; i<num; i++) {
1300 +                int c = (i * 256) / num;
1301 +                palette[i].red = c * 0x0101;
1302 +                palette[i].green = c * 0x0101;
1303 +                palette[i].blue = c * 0x0101;
1304 +                if (color_class == PseudoColor)
1305 +                        palette[i].pixel = i;
1306 +                palette[i].flags = DoRed | DoGreen | DoBlue;
1307 +        }
1308 +        if (cmap[0] && cmap[1]) {
1309 +                XStoreColors(x_display, cmap[0], palette, num);
1310 +                XStoreColors(x_display, cmap[1], palette, num);
1311 +        }
1312 +
1313 + #ifdef ENABLE_VOSF
1314 +        // Load gray ramp to 8->16/32 expand map
1315 +        if (!IsDirectMode(mode) && (color_class == TrueColor || color_class == DirectColor))
1316 +                for (int i=0; i<256; i++)
1317 +                        ExpandMap[i] = map_rgb(i, i, i);
1318 + #endif
1319 +
1320          // Create display driver object of requested type
1321          switch (display_type) {
1322                  case DISPLAY_WINDOW:
# Line 1107 | Line 1404 | bool VideoInit(bool classic)
1404          // Find screen and root window
1405          screen = XDefaultScreen(x_display);
1406          rootwin = XRootWindow(x_display, screen);
1407 <        
1408 <        // Get screen depth
1409 <        xdepth = DefaultDepth(x_display, screen);
1407 >
1408 >        // Get sorted list of available depths
1409 >        avail_depths = XListDepths(x_display, screen, &num_depths);
1410 >        if (avail_depths == NULL) {
1411 >                ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1412 >                return false;
1413 >        }
1414 >        sort(avail_depths, avail_depths + num_depths);
1415          
1416   #ifdef ENABLE_FBDEV_DGA
1417          // Frame buffer name
# Line 1149 | Line 1451 | bool VideoInit(bool classic)
1451          black_pixel = BlackPixel(x_display, screen);
1452          white_pixel = WhitePixel(x_display, screen);
1453  
1152        // Get appropriate visual
1153        int color_class;
1154        switch (xdepth) {
1155                case 1:
1156                        color_class = StaticGray;
1157                        break;
1158                case 8:
1159                        color_class = PseudoColor;
1160                        break;
1161                case 15:
1162                case 16:
1163                case 24:
1164                case 32: // Try DirectColor first, as this will allow gamma correction
1165                        color_class = DirectColor;
1166                        if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo))
1167                                color_class = TrueColor;
1168                        break;
1169                default:
1170                        ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1171                        return false;
1172        }
1173        if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
1174                ErrorAlert(STR_NO_XVISUAL_ERR);
1175                return false;
1176        }
1177        if (visualInfo.depth != xdepth) {
1178                ErrorAlert(STR_NO_XVISUAL_ERR);
1179                return false;
1180        }
1181        vis = visualInfo.visual;
1182
1183        // Create color maps
1184        if (color_class == PseudoColor || color_class == DirectColor) {
1185                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1186                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1187
1188                // Preset pixel members for gamma table
1189                if (color_class == DirectColor) {
1190                        int num = vis->map_entries;
1191                        uint32 rmask = vis->red_mask, gmask = vis->green_mask, bmask = vis->blue_mask;
1192                        uint32 mask;
1193                        int rloss = 8, rshift = 0;
1194                        for (mask=rmask; !(mask&1); mask>>=1)
1195                                ++rshift;
1196                        for (; mask&1; mask>>=1)
1197                                --rloss;
1198                        int gloss = 8, gshift = 0;
1199                        for (mask=gmask; !(mask&1); mask>>=1)
1200                                ++gshift;
1201                        for (; mask&1; mask>>=1)
1202                                --gloss;
1203                        int bloss = 8, bshift = 0;
1204                        for (mask=bmask; !(mask&1); mask>>=1)
1205                                ++bshift;
1206                        for (; mask&1; mask>>=1)
1207                                --bloss;
1208                        for (int i=0; i<num; i++) {
1209                                int c = (i * 256) / num;
1210                                palette[i].pixel = ((c >> rloss) << rshift) | ((c >> gloss) << gshift) | ((c >> bloss) << bshift);
1211                        }
1212                }
1213        }
1214
1454          // Get screen mode from preferences
1455          const char *mode_str;
1456          if (classic_mode)
# Line 1245 | Line 1484 | bool VideoInit(bool classic)
1484          else if (default_height > DisplayHeight(x_display, screen))
1485                  default_height = DisplayHeight(x_display, screen);
1486  
1487 <        // Mac screen depth is always 1 bit in Classic mode, but follows X depth otherwise
1488 <        int depth = (classic_mode ? 1 : xdepth);
1489 <        video_depth depth_mode = DepthModeForPixelDepth(depth);
1487 >        // Mac screen depth follows X depth
1488 >        video_depth default_depth = VDEPTH_1BIT;
1489 >        switch (DefaultDepth(x_display, screen)) {
1490 >                case 8:
1491 >                        default_depth = VDEPTH_8BIT;
1492 >                        break;
1493 >                case 15: case 16:
1494 >                        default_depth = VDEPTH_16BIT;
1495 >                        break;
1496 >                case 24: case 32:
1497 >                        default_depth = VDEPTH_32BIT;
1498 >                        break;
1499 >        }
1500  
1501          // Construct list of supported modes
1502          if (display_type == DISPLAY_WINDOW) {
1503                  if (classic)
1504 <                        add_mode(512, 342, 0x80, 64, depth_mode);
1504 >                        add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
1505                  else {
1506 <                        add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth_mode), depth_mode);
1507 <                        add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth_mode), depth_mode);
1508 <                        add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth_mode), depth_mode);
1509 <                        add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth_mode), depth_mode);
1261 <                        add_mode(1280, 1024, 0x84, TrivialBytesPerRow(1280, depth_mode), depth_mode);
1506 >                        for (unsigned d=VDEPTH_1BIT; d<=VDEPTH_32BIT; d++) {
1507 >                                if (find_visual_for_depth(video_depth(d)))
1508 >                                        add_window_modes(video_depth(d));
1509 >                        }
1510                  }
1511          } else
1512 <                add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, depth_mode), depth_mode);
1512 >                add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
1513 >        if (VideoModes.empty()) {
1514 >                ErrorAlert(STR_NO_XVISUAL_ERR);
1515 >                return false;
1516 >        }
1517 >        video_init_depth_list();
1518 >
1519 > #if DEBUG
1520 >        D(bug("Available video modes:\n"));
1521 >        vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end();
1522 >        while (i != end) {
1523 >                int bits = 1 << i->depth;
1524 >                if (bits == 16)
1525 >                        bits = 15;
1526 >                else if (bits == 32)
1527 >                        bits = 24;
1528 >                D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1529 >                ++i;
1530 >        }
1531 > #endif
1532  
1533          // Find requested default mode and open display
1534          if (VideoModes.size() == 1)
1535                  return video_open(VideoModes[0]);
1536          else {
1537                  // Find mode with specified dimensions
1538 <                std::vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end();
1539 <                while (i != end) {
1540 <                        if (i->x == default_width && i->y == default_height)
1538 >                std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1539 >                for (i = VideoModes.begin(); i != end; ++i) {
1540 >                        if (i->x == default_width && i->y == default_height && i->depth == default_depth)
1541                                  return video_open(*i);
1275                        ++i;
1542                  }
1543                  return video_open(VideoModes[0]);
1544          }
# Line 1319 | Line 1585 | static void video_close(void)
1585          // Close display
1586          delete drv;
1587          drv = NULL;
1322 }
1323
1324 void VideoExit(void)
1325 {
1326        // Close display
1327        video_close();
1588  
1589          // Free colormaps
1590          if (cmap[0]) {
# Line 1335 | Line 1595 | void VideoExit(void)
1595                  XFreeColormap(x_display, cmap[1]);
1596                  cmap[1] = 0;
1597          }
1598 + }
1599 +
1600 + void VideoExit(void)
1601 + {
1602 +        // Close display
1603 +        video_close();
1604  
1605   #ifdef ENABLE_XF86_VIDMODE
1606          // Free video mode list
# Line 1351 | Line 1617 | void VideoExit(void)
1617                  fbdev_fd = -1;
1618          }
1619   #endif
1620 +
1621 +        // Free depth list
1622 +        if (avail_depths) {
1623 +                XFree(avail_depths);
1624 +                avail_depths = NULL;
1625 +        }
1626   }
1627  
1628  
# Line 1386 | Line 1658 | void VideoInterrupt(void)
1658   *  Set palette
1659   */
1660  
1661 < void video_set_palette(uint8 *pal)
1661 > void video_set_palette(uint8 *pal, int num_in)
1662   {
1663          LOCK_PALETTE;
1664  
1665          // Convert colors to XColor array
1666 <        int num_in = 256, num_out = 256;
1667 <        if (VideoMonitor.mode.depth == VDEPTH_16BIT) {
1668 <                num_in = 32;
1669 <                // If X is in 565 mode we have to stretch the palette from 32 to 64 entries
1670 <                if (vis->c_class == DirectColor)
1671 <                        num_out = vis->map_entries;
1666 >        int num_out = 256;
1667 >        bool stretch = false;
1668 >        if (IsDirectMode(VideoMonitor.mode)) {
1669 >                // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1670 >                num_out = vis->map_entries;
1671 >                stretch = true;
1672          }
1673          XColor *p = palette;
1674          for (int i=0; i<num_out; i++) {
1675 <                int c = (i * num_in) / num_out;
1675 >                int c = (stretch ? (i * num_in) / num_out : i);
1676                  p->red = pal[c*3 + 0] * 0x0101;
1677                  p->green = pal[c*3 + 1] * 0x0101;
1678                  p->blue = pal[c*3 + 2] * 0x0101;
1679 <                if (!IsDirectMode(VideoMonitor.mode))
1679 >                if (color_class == PseudoColor)
1680                          p->pixel = i;
1681                  p->flags = DoRed | DoGreen | DoBlue;
1682                  p++;
1683          }
1684  
1685 + #ifdef ENABLE_VOSF
1686 +        // Recalculate pixel color expansion map
1687 +        if (!IsDirectMode(VideoMonitor.mode) && (color_class == TrueColor || color_class == DirectColor)) {
1688 +                for (int i=0; i<256; i++) {
1689 +                        int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1690 +                        ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
1691 +                }
1692 +
1693 +                // We have to redraw everything because the interpretation of pixel values changed
1694 +                LOCK_VOSF;
1695 +                PFLAG_SET_ALL;
1696 +                UNLOCK_VOSF;
1697 +                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1698 +        }
1699 + #endif
1700 +
1701          // Tell redraw thread to change palette
1702          palette_changed = true;
1703  
# Line 1435 | Line 1723 | void video_switch_to_mode(const video_mo
1723  
1724  
1725   /*
1726 < *  Translate key event to Mac keycode
1726 > *  Translate key event to Mac keycode, returns -1 if no keycode was found
1727 > *  and -2 if the key was recognized as a hotkey
1728   */
1729  
1730 < static int kc_decode(KeySym ks)
1730 > static int kc_decode(KeySym ks, bool key_down)
1731   {
1732          switch (ks) {
1733                  case XK_A: case XK_a: return 0x00;
# Line 1491 | Line 1780 | static int kc_decode(KeySym ks)
1780                  case XK_period: case XK_greater: return 0x2f;
1781                  case XK_slash: case XK_question: return 0x2c;
1782  
1783 < #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1495 <                case XK_Tab: if (ctrl_down) {drv->suspend(); return -1;} else return 0x30;
1496 < #else
1497 <                case XK_Tab: return 0x30;
1498 < #endif
1783 >                case XK_Tab: if (ctrl_down) {if (key_down) drv->suspend(); return -2;} else return 0x30;
1784                  case XK_Return: return 0x24;
1785                  case XK_space: return 0x31;
1786                  case XK_BackSpace: return 0x33;
# Line 1529 | Line 1814 | static int kc_decode(KeySym ks)
1814                  case XK_Left: return 0x3b;
1815                  case XK_Right: return 0x3c;
1816  
1817 <                case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
1817 >                case XK_Escape: if (ctrl_down) {if (key_down) { quit_full_screen = true; emerg_quit = true; } return -2;} else return 0x35;
1818  
1819 <                case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
1819 >                case XK_F1: if (ctrl_down) {if (key_down) SysMountFirstFloppy(); return -2;} else return 0x7a;
1820                  case XK_F2: return 0x78;
1821                  case XK_F3: return 0x63;
1822                  case XK_F4: return 0x76;
1823 <                case XK_F5: return 0x60;
1823 >                case XK_F5: if (ctrl_down) {if (key_down) drv->toggle_mouse_grab(); return -2;} else return 0x60;
1824                  case XK_F6: return 0x61;
1825                  case XK_F7: return 0x62;
1826                  case XK_F8: return 0x64;
# Line 1583 | Line 1868 | static int kc_decode(KeySym ks)
1868          return -1;
1869   }
1870  
1871 < static int event2keycode(XKeyEvent &ev)
1871 > static int event2keycode(XKeyEvent &ev, bool key_down)
1872   {
1873          KeySym ks;
1589        int as;
1874          int i = 0;
1875  
1876          do {
1877                  ks = XLookupKeysym(&ev, i++);
1878 <                as = kc_decode(ks);
1879 <                if (as != -1)
1878 >                int as = kc_decode(ks, key_down);
1879 >                if (as >= 0)
1880 >                        return as;
1881 >                if (as == -2)
1882                          return as;
1883          } while (ks != NoSymbol);
1884  
# Line 1639 | Line 1925 | static void handle_events(void)
1925                          }
1926  
1927                          // Mouse moved
1642                        case EnterNotify:
1928                          case MotionNotify:
1929 <                                ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1929 >                                drv->mouse_moved(event.xmotion.x, event.xmotion.y);
1930                                  break;
1931  
1932                          // Keyboard
1933                          case KeyPress: {
1934 <                                int code;
1934 >                                int code = -1;
1935                                  if (use_keycodes) {
1936 <                                        event2keycode(event.xkey);      // This is called to process the hotkeys
1937 <                                        code = keycode_table[event.xkey.keycode & 0xff];
1936 >                                        if (event2keycode(event.xkey, true) != -2)      // This is called to process the hotkeys
1937 >                                                code = keycode_table[event.xkey.keycode & 0xff];
1938                                  } else
1939 <                                        code = event2keycode(event.xkey);
1940 <                                if (code != -1) {
1939 >                                        code = event2keycode(event.xkey, true);
1940 >                                if (code >= 0) {
1941                                          if (!emul_suspended) {
1942                                                  if (code == 0x39) {     // Caps Lock pressed
1943                                                          if (caps_on) {
# Line 1674 | Line 1959 | static void handle_events(void)
1959                                  break;
1960                          }
1961                          case KeyRelease: {
1962 <                                int code;
1962 >                                int code = -1;
1963                                  if (use_keycodes) {
1964 <                                        event2keycode(event.xkey);      // This is called to process the hotkeys
1965 <                                        code = keycode_table[event.xkey.keycode & 0xff];
1964 >                                        if (event2keycode(event.xkey, false) != -2)     // This is called to process the hotkeys
1965 >                                                code = keycode_table[event.xkey.keycode & 0xff];
1966                                  } else
1967 <                                        code = event2keycode(event.xkey);
1968 <                                if (code != -1 && code != 0x39) {       // Don't propagate Caps Lock releases
1967 >                                        code = event2keycode(event.xkey, false);
1968 >                                if (code >= 0 && code != 0x39) {        // Don't propagate Caps Lock releases
1969                                          ADBKeyUp(code);
1970                                          if (code == 0x36)
1971                                                  ctrl_down = false;
# Line 1944 | Line 2229 | static void update_display_static(driver
2229  
2230   static inline void possibly_quit_dga_mode()
2231   {
2232 <        // Quit DGA mode if requested
2232 >        // Quit DGA mode if requested (something terrible has happened and we
2233 >        // want to give control back to the user)
2234          if (quit_full_screen) {
2235                  quit_full_screen = false;
2236                  delete drv;
# Line 1952 | Line 2238 | static inline void possibly_quit_dga_mod
2238          }
2239   }
2240  
2241 + static inline void possibly_ungrab_mouse()
2242 + {
2243 +        // Ungrab mouse if requested (something terrible has happened and we
2244 +        // want to give control back to the user)
2245 +        if (quit_full_screen) {
2246 +                quit_full_screen = false;
2247 +                if (drv)
2248 +                        drv->ungrab_mouse();
2249 +        }
2250 + }
2251 +
2252   static inline void handle_palette_changes(void)
2253   {
2254          LOCK_PALETTE;
# Line 2004 | Line 2301 | static void video_refresh_dga_vosf(void)
2301  
2302   static void video_refresh_window_vosf(void)
2303   {
2304 <        // Quit DGA mode if requested
2305 <        possibly_quit_dga_mode();
2304 >        // Ungrab mouse if requested
2305 >        possibly_ungrab_mouse();
2306          
2307          // Handle X events
2308          handle_events();
# Line 2029 | Line 2326 | static void video_refresh_window_vosf(vo
2326  
2327   static void video_refresh_window_static(void)
2328   {
2329 +        // Ungrab mouse if requested
2330 +        possibly_ungrab_mouse();
2331 +
2332          // Handle X events
2333          handle_events();
2334          
# Line 2045 | Line 2345 | static void video_refresh_window_static(
2345  
2346   static void video_refresh_window_dynamic(void)
2347   {
2348 +        // Ungrab mouse if requested
2349 +        possibly_ungrab_mouse();
2350 +
2351          // Handle X events
2352          handle_events();
2353          

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines