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.52 by cebix, 2001-07-03T19:20:46Z vs.
Revision 1.60 by cebix, 2001-07-11T19:26:14Z

# Line 37 | 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 74 | 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 | ExposureMask | StructureNotifyMask;
83 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
84   static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
85  
86  
# Line 87 | Line 93 | static int display_type = DISPLAY_WINDOW
93   static bool local_X11;                                                          // Flag: X server running on local machine?
94   static uint8 *the_buffer = NULL;                                        // Mac frame buffer (where MacOS draws into)
95   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer (for refreshed modes)
96 + static uint32 the_buffer_size;                                          // Size of allocated the_buffer
97  
98   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
99   #ifdef HAVE_PTHREADS
# Line 116 | Line 123 | static int keycode_table[256];                                         // X
123  
124   // X11 variables
125   static int screen;                                                                      // Screen number
119 static int xdepth;                                                                      // Depth of X screen
126   static Window rootwin;                                                          // Root window and our window
127 < static XVisualInfo visualInfo;
128 < static Visual *vis;
123 < static Colormap cmap[2] = {0, 0};                                       // Colormaps for indexed modes (DGA needs two of them)
127 > static int num_depths = 0;                                                      // Number of available X depths
128 > static int *avail_depths = NULL;                                        // List of available X depths
129   static XColor black, white;
130   static unsigned long black_pixel, white_pixel;
131   static int eventmask;
132  
133 + static int xdepth;                                                                      // Depth of X screen
134 + static XVisualInfo visualInfo;
135 + static Visual *vis;
136 + static int color_class;
137 +
138   static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
139  
140 + static Colormap cmap[2] = {0, 0};                                       // Colormaps for indexed modes (DGA needs two of them)
141 +
142   static XColor palette[256];                                                     // Color palette to be used as CLUT and gamma table
143   static bool palette_changed = false;                            // Flag: Palette changed, redraw thread must set new colors
144  
# Line 172 | Line 184 | static void (*video_refresh)(void);
184  
185   // Prototypes
186   static void *redraw_func(void *arg);
175 static int event2keycode(XKeyEvent &ev);
187  
188   // From main_unix.cpp
189   extern char *x_display_name;
# Line 192 | Line 203 | static inline uint32 map_rgb(uint8 red,
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 and uses default visual
216 +                        min_depth = max_depth = DefaultDepth(x_display, screen);
217 +                        break;
218 + #ifdef ENABLE_VOSF
219 +                case VDEPTH_2BIT:
220 +                case VDEPTH_4BIT:       // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
221 +                case VDEPTH_8BIT:
222 +                        min_depth = 8;
223 +                        max_depth = 32;
224 +                        break;
225 + #else
226 +                case VDEPTH_2BIT:
227 +                case VDEPTH_4BIT:       // 2/4-bit requires VOSF blitters
228 +                        return false;
229 +                case VDEPTH_8BIT:       // 8-bit without VOSF requires an 8-bit visual
230 +                        min_depth = 8;
231 +                        max_depth = 8;
232 +                        break;
233 + #endif
234 +                case VDEPTH_16BIT:      // 16-bit requires a 15/16-bit visual
235 +                        min_depth = 15;
236 +                        max_depth = 16;
237 +                        break;
238 +                case VDEPTH_32BIT:      // 32-bit requires a 24/32-bit visual
239 +                        min_depth = 24;
240 +                        max_depth = 32;
241 +                        break;
242 +        }
243 +        D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth));
244 +
245 +        // Try to find a visual for one of the color depths
246 +        bool visual_found = false;
247 +        for (int i=0; i<num_depths && !visual_found; i++) {
248 +
249 +                xdepth = avail_depths[i];
250 +                D(bug(" trying to find visual for depth %d\n", xdepth));
251 +                if (xdepth < min_depth || xdepth > max_depth)
252 +                        continue;
253 +
254 +                // Determine best color class for this depth
255 +                switch (xdepth) {
256 +                        case 1: // Try StaticGray or StaticColor
257 +                                if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo)
258 +                                 || XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo))
259 +                                        visual_found = true;
260 +                                break;
261 +                        case 8: // Need PseudoColor
262 +                                if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo))
263 +                                        visual_found = true;
264 +                                break;
265 +                        case 15:
266 +                        case 16:
267 +                        case 24:
268 +                        case 32: // Try DirectColor first, as this will allow gamma correction
269 +                                if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo)
270 +                                 || XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo))
271 +                                        visual_found = true;
272 +                                break;
273 +                        default:
274 +                                D(bug("  not a supported depth\n"));
275 +                                break;
276 +                }
277 +        }
278 +        if (!visual_found)
279 +                return false;
280 +
281 +        // Visual was found
282 +        vis = visualInfo.visual;
283 +        color_class = visualInfo.c_class;
284 +        D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth));
285 + #if DEBUG
286 +        switch (color_class) {
287 +                case StaticGray: D(bug("StaticGray\n")); break;
288 +                case GrayScale: D(bug("GrayScale\n")); break;
289 +                case StaticColor: D(bug("StaticColor\n")); break;
290 +                case PseudoColor: D(bug("PseudoColor\n")); break;
291 +                case TrueColor: D(bug("TrueColor\n")); break;
292 +                case DirectColor: D(bug("DirectColor\n")); break;
293 +        }
294 + #endif
295 +        return true;
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 237 | Line 340 | static void set_mac_frame_buffer(video_d
340          InitFrameBufferMapping();
341   #else
342          VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
240        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 326 | Line 428 | public:
428          virtual void toggle_mouse_grab(void) {}
429          virtual void mouse_moved(int x, int y) { ADBMouseMoved(x, y); }
430  
431 +        void disable_mouse_accel(void);
432 +        void restore_mouse_accel(void);
433 +
434          virtual void grab_mouse(void) {}
435          virtual void ungrab_mouse(void) {}
436  
437   public:
438          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
439          Window w;               // The window we draw into
440 +
441 +        int orig_accel_numer, orig_accel_denom, orig_threshold; // Original mouse acceleration
442   };
443  
444   class driver_window;
# Line 375 | Line 482 | driver_base::driver_base()
482   {
483          the_buffer = NULL;
484          the_buffer_copy = NULL;
485 +        XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold);
486   }
487  
488   driver_base::~driver_base()
489   {
490          ungrab_mouse();
491 +        restore_mouse_accel();
492  
493          if (w) {
494                  XUnmapWindow(x_display, w);
# Line 403 | Line 512 | driver_base::~driver_base()
512          }
513   #ifdef ENABLE_VOSF
514          else {
515 <                if (the_buffer != (uint8 *)VM_MAP_FAILED) {
516 <                        vm_release(the_buffer, the_buffer_size);
515 >                if (the_host_buffer) {
516 >                        free(the_host_buffer);
517 >                        the_host_buffer = NULL;
518 >                }
519 >                if (the_buffer) {
520 >                        free(the_buffer);
521                          the_buffer = NULL;
522                  }
523 <                if (the_buffer_copy != (uint8 *)VM_MAP_FAILED) {
524 <                        vm_release(the_buffer_copy, the_buffer_size);
523 >                if (the_buffer_copy) {
524 >                        free(the_buffer_copy);
525                          the_buffer_copy = NULL;
526                  }
527          }
# Line 418 | Line 531 | driver_base::~driver_base()
531   // Palette has changed
532   void driver_base::update_palette(void)
533   {
534 <        if (cmap[0] && cmap[1]) {
535 <                int num = 256;
536 <                if (IsDirectMode(VideoMonitor.mode))
424 <                        num = vis->map_entries; // Palette is gamma table
425 <                else if (vis->c_class == DirectColor)
534 >        if (color_class == PseudoColor || color_class == DirectColor) {
535 >                int num = vis->map_entries;
536 >                if (!IsDirectMode(VideoMonitor.mode) && color_class == DirectColor)
537                          return; // Indexed mode on true color screen, don't set CLUT
538                  XStoreColors(x_display, cmap[0], palette, num);
539                  XStoreColors(x_display, cmap[1], palette, num);
# Line 430 | Line 541 | void driver_base::update_palette(void)
541          XSync(x_display, false);
542   }
543  
544 + // Disable mouse acceleration
545 + void driver_base::disable_mouse_accel(void)
546 + {
547 +        XChangePointerControl(x_display, True, False, 1, 1, 0);
548 + }
549 +
550 + // Restore mouse acceleration to original value
551 + void driver_base::restore_mouse_accel(void)
552 + {
553 +        XChangePointerControl(x_display, True, True, orig_accel_numer, orig_accel_denom, orig_threshold);
554 + }
555 +
556  
557   /*
558   *  Windowed display driver
# Line 437 | Line 560 | void driver_base::update_palette(void)
560  
561   // Open display
562   driver_window::driver_window(const video_mode &mode)
563 < : gc(0), img(NULL), have_shm(false), mouse_grabbed(false), mac_cursor(0)
563 > : gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
564   {
565          int width = mode.x, height = mode.y;
566          int aligned_width = (width + 15) & ~15;
# Line 446 | Line 569 | driver_window::driver_window(const video
569          // Set absolute mouse mode
570          ADBSetRelMouseMode(mouse_grabbed);
571  
572 <        // Create window
572 >        // Create window (setting backround_pixel, border_pixel and colormap is
573 >        // mandatory when using a non-default visual; in 1-bit mode we use the
574 >        // default visual, so we can also use the default colormap)
575          XSetWindowAttributes wattr;
576          wattr.event_mask = eventmask = win_eventmask;
577 <        wattr.background_pixel = black_pixel;
578 <        wattr.colormap = (mode.depth == VDEPTH_1BIT && vis->c_class == PseudoColor ? DefaultColormap(x_display, screen) : cmap[0]);
577 >        wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
578 >        wattr.border_pixel = 0;
579 >        wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
580          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
581 <                InputOutput, vis, CWEventMask | CWBackPixel | (vis->c_class == PseudoColor || vis->c_class == DirectColor ? CWColormap : 0), &wattr);
581 >                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWColormap, &wattr);
582  
583          // Set window name/class
584          set_window_name(w, STR_WINDOW_TITLE);
# Line 504 | Line 630 | driver_window::driver_window(const video
630                  if (shm_error) {
631                          shmdt(shminfo.shmaddr);
632                          XDestroyImage(img);
633 +                        img = NULL;
634                          shminfo.shmid = -1;
635                  } else {
636                          have_shm = true;
# Line 524 | Line 651 | driver_window::driver_window(const video
651          }
652  
653   #ifdef ENABLE_VOSF
654 +        use_vosf = true;
655          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
656          the_host_buffer = the_buffer_copy;
657          the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
658          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
659          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
660 +        D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
661   #else
662          // Allocate memory for frame buffer
663          the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
664 +        D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
665   #endif
666  
667          // Create GC
# Line 567 | Line 697 | driver_window::driver_window(const video
697   // Close display
698   driver_window::~driver_window()
699   {
570        if (img)
571                XDestroyImage(img);
700          if (have_shm) {
701                  XShmDetach(x_display, &shminfo);
702 + #ifdef ENABLE_VOSF
703 +                the_host_buffer = NULL; // don't free() in driver_base dtor
704 + #else
705                  the_buffer_copy = NULL; // don't free() in driver_base dtor
706 + #endif
707 +        }
708 + #ifdef ENABLE_VOSF
709 +        if (use_vosf) {
710 +                // don't free() memory mapped buffers in driver_base dtor
711 +                if (the_buffer != VM_MAP_FAILED) {
712 +                        vm_release(the_buffer, the_buffer_size);
713 +                        the_buffer = NULL;
714 +                }
715 +                if (the_buffer_copy != VM_MAP_FAILED) {
716 +                        vm_release(the_buffer_copy, the_buffer_size);
717 +                        the_buffer_copy = NULL;
718 +                }
719 +        }
720 + #endif
721 +        if (img)
722 +                XDestroyImage(img);
723 +        if (have_shm) {
724 +                shmdt(shminfo.shmaddr);
725 +                shmctl(shminfo.shmid, IPC_RMID, 0);
726          }
727          if (gc)
728                  XFreeGC(x_display, gc);
# Line 598 | Line 749 | void driver_window::grab_mouse(void)
749                  Delay_usec(100000);
750          }
751          if (result == GrabSuccess) {
601                ADBSetRelMouseMode(mouse_grabbed = true);
752                  XStoreName(x_display, w, GetString(STR_WINDOW_TITLE_GRABBED));
753 <                XSync(x_display, false);
753 >                ADBSetRelMouseMode(mouse_grabbed = true);
754 >                disable_mouse_accel();
755          }
756   }
757  
# Line 611 | Line 762 | void driver_window::ungrab_mouse(void)
762                  XUngrabPointer(x_display, CurrentTime);
763                  XStoreName(x_display, w, GetString(STR_WINDOW_TITLE));
764                  ADBSetRelMouseMode(mouse_grabbed = false);
765 +                restore_mouse_accel();
766          }
767   }
768  
# Line 706 | Line 858 | void driver_dga::suspend(void)
858   #endif
859          XUngrabPointer(x_display, CurrentTime);
860          XUngrabKeyboard(x_display, CurrentTime);
861 +        restore_mouse_accel();
862          XUnmapWindow(x_display, w);
863          wait_unmapped(w);
864  
# Line 735 | Line 888 | void driver_dga::resume(void)
888          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
889          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
890          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
891 +        disable_mouse_accel();
892   #ifdef ENABLE_XF86_DGA
893          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
894          XF86DGASetViewPort(x_display, screen, 0, 0);
# Line 881 | Line 1035 | driver_fbdev::driver_fbdev(const video_m
1035          XGrabPointer(x_display, w, True,
1036                  PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1037                  GrabModeAsync, GrabModeAsync, w, None, CurrentTime);
1038 +        disable_mouse_accel();
1039          
1040          // Calculate bytes per row
1041          int bytes_per_row = TrivialBytesPerRow(mode.x, mode.depth);
1042          
1043          // Map frame buffer
1044 <        if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
1045 <                if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
1044 >        the_buffer_size = height * bytes_per_row;
1045 >        if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
1046 >                if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
1047                          char str[256];
1048                          sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
1049                          ErrorAlert(str);
# Line 926 | Line 1082 | driver_fbdev::driver_fbdev(const video_m
1082   // Close display
1083   driver_fbdev::~driver_fbdev()
1084   {
1085 +        if (!use_vosf) {
1086 +                if (the_buffer != MAP_FAILED) {
1087 +                        // don't free() the screen buffer in driver_base dtor
1088 +                        munmap(the_buffer, the_buffer_size);
1089 +                        the_buffer = NULL;
1090 +                }
1091 +        }
1092 + #ifdef ENABLE_VOSF
1093 +        else {
1094 +                if (the_host_buffer != MAP_FAILED) {
1095 +                        // don't free() the screen buffer in driver_base dtor
1096 +                        munmap(the_host_buffer, the_buffer_size);
1097 +                        the_host_buffer = NULL;
1098 +                }
1099 +                if (the_buffer_copy != VM_MAP_FAILED) {
1100 +                        vm_release(the_buffer_copy, the_buffer_size);
1101 +                        the_buffer_copy = NULL;
1102 +                }
1103 +                if (the_buffer != VM_MAP_FAILED) {
1104 +                        vm_release(the_buffer, the_buffer_size);
1105 +                        the_buffer = NULL;
1106 +                }
1107 +        }
1108 + #endif
1109   }
1110   #endif
1111  
# Line 995 | Line 1175 | driver_xf86dga::driver_xf86dga(const vid
1175          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1176          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
1177          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1178 +        disable_mouse_accel();
1179  
1180          int v_width, v_bank, v_size;
1181          XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
# Line 1011 | Line 1192 | driver_xf86dga::driver_xf86dga(const vid
1192  
1193          // Init blitting routines
1194          int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
1195 < #ifdef VIDEO_VOSF
1195 > #if VIDEO_VOSF
1196   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1197          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1198          // i.e. the framebuffer update function is not Blit_Copy_Raw
# Line 1042 | Line 1223 | driver_xf86dga::driver_xf86dga(const vid
1223   driver_xf86dga::~driver_xf86dga()
1224   {
1225          XF86DGADirectVideo(x_display, screen, 0);
1226 +        if (!use_vosf) {
1227 +                // don't free() the screen buffer in driver_base dtor
1228 +                the_buffer = NULL;
1229 +        }
1230 + #ifdef ENABLE_VOSF
1231 +        else {
1232 +                // don't free() the screen buffer in driver_base dtor
1233 +                the_host_buffer = NULL;
1234 +                
1235 +                if (the_buffer_copy != VM_MAP_FAILED) {
1236 +                        vm_release(the_buffer_copy, the_buffer_size);
1237 +                        the_buffer_copy = NULL;
1238 +                }
1239 +                if (the_buffer != VM_MAP_FAILED) {
1240 +                        vm_release(the_buffer, the_buffer_size);
1241 +                        the_buffer = NULL;
1242 +                }
1243 +        }
1244 + #endif
1245   #ifdef ENABLE_XF86_VIDMODE
1246          if (has_vidmode)
1247                  XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
# Line 1139 | Line 1339 | static void keycode_init(void)
1339   // Open display for specified mode
1340   static bool video_open(const video_mode &mode)
1341   {
1342 +        // Find best available X visual
1343 +        if (!find_visual_for_depth(mode.depth)) {
1344 +                ErrorAlert(STR_NO_XVISUAL_ERR);
1345 +                return false;
1346 +        }
1347 +
1348 +        // Create color maps
1349 +        if (color_class == PseudoColor || color_class == DirectColor) {
1350 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1351 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1352 +        } else {
1353 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
1354 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
1355 +        }
1356 +
1357 +        // Find pixel format of direct modes
1358 +        if (color_class == DirectColor || color_class == TrueColor) {
1359 +                rshift = gshift = bshift = 0;
1360 +                rloss = gloss = bloss = 8;
1361 +                uint32 mask;
1362 +                for (mask=vis->red_mask; !(mask&1); mask>>=1)
1363 +                        ++rshift;
1364 +                for (; mask&1; mask>>=1)
1365 +                        --rloss;
1366 +                for (mask=vis->green_mask; !(mask&1); mask>>=1)
1367 +                        ++gshift;
1368 +                for (; mask&1; mask>>=1)
1369 +                        --gloss;
1370 +                for (mask=vis->blue_mask; !(mask&1); mask>>=1)
1371 +                        ++bshift;
1372 +                for (; mask&1; mask>>=1)
1373 +                        --bloss;
1374 +        }
1375 +
1376 +        // Preset palette pixel values for CLUT or gamma table
1377 +        if (color_class == DirectColor) {
1378 +                int num = vis->map_entries;
1379 +                for (int i=0; i<num; i++) {
1380 +                        int c = (i * 256) / num;
1381 +                        palette[i].pixel = map_rgb(c, c, c);
1382 +                        palette[i].flags = DoRed | DoGreen | DoBlue;
1383 +                }
1384 +        } else if (color_class == PseudoColor) {
1385 +                for (int i=0; i<256; i++) {
1386 +                        palette[i].pixel = i;
1387 +                        palette[i].flags = DoRed | DoGreen | DoBlue;
1388 +                }
1389 +        }
1390 +
1391          // Load gray ramp to color map
1392 <        int num = (vis->c_class == DirectColor ? vis->map_entries : 256);
1392 >        int num = (color_class == DirectColor ? vis->map_entries : 256);
1393          for (int i=0; i<num; i++) {
1394                  int c = (i * 256) / num;
1395                  palette[i].red = c * 0x0101;
1396                  palette[i].green = c * 0x0101;
1397                  palette[i].blue = c * 0x0101;
1149                if (vis->c_class == PseudoColor)
1150                        palette[i].pixel = i;
1151                palette[i].flags = DoRed | DoGreen | DoBlue;
1398          }
1399 <        if (cmap[0] && cmap[1]) {
1399 >        if (color_class == PseudoColor || color_class == DirectColor) {
1400                  XStoreColors(x_display, cmap[0], palette, num);
1401                  XStoreColors(x_display, cmap[1], palette, num);
1402          }
1403  
1404   #ifdef ENABLE_VOSF
1405          // Load gray ramp to 8->16/32 expand map
1406 <        if (!IsDirectMode(mode) && (vis->c_class == TrueColor || vis->c_class == DirectColor))
1406 >        if (!IsDirectMode(mode) && xdepth > 8)
1407                  for (int i=0; i<256; i++)
1408                          ExpandMap[i] = map_rgb(i, i, i);
1409   #endif
# Line 1188 | Line 1434 | static bool video_open(const video_mode
1434  
1435   #ifdef ENABLE_VOSF
1436          if (use_vosf) {
1437 <                // Initialize the mainBuffer structure
1438 <                if (!video_init_buffer()) {
1437 >                // Initialize the VOSF system
1438 >                if (!video_vosf_init()) {
1439                          ErrorAlert(STR_VOSF_INIT_ERR);
1440                  return false;
1441                  }
1196
1197                // Initialize the handler for SIGSEGV
1198                if (!sigsegv_install_handler(screen_fault_handler)) {
1199                        ErrorAlert("Could not initialize Video on SEGV signals");
1200                        return false;
1201                }
1442          }
1443   #endif
1444          
# Line 1249 | Line 1489 | bool VideoInit(bool classic)
1489          // Find screen and root window
1490          screen = XDefaultScreen(x_display);
1491          rootwin = XRootWindow(x_display, screen);
1492 <        
1493 <        // Get screen depth
1494 <        xdepth = DefaultDepth(x_display, screen);
1492 >
1493 >        // Get sorted list of available depths
1494 >        avail_depths = XListDepths(x_display, screen, &num_depths);
1495 >        if (avail_depths == NULL) {
1496 >                ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1497 >                return false;
1498 >        }
1499 >        sort(avail_depths, avail_depths + num_depths);
1500          
1501   #ifdef ENABLE_FBDEV_DGA
1502          // Frame buffer name
# Line 1291 | Line 1536 | bool VideoInit(bool classic)
1536          black_pixel = BlackPixel(x_display, screen);
1537          white_pixel = WhitePixel(x_display, screen);
1538  
1294        // Get appropriate visual
1295        int color_class;
1296        switch (xdepth) {
1297                case 1:
1298                        color_class = StaticGray;
1299                        break;
1300                case 8:
1301                        color_class = PseudoColor;
1302                        break;
1303                case 15:
1304                case 16:
1305                case 24:
1306                case 32: // Try DirectColor first, as this will allow gamma correction
1307                        color_class = DirectColor;
1308                        if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo))
1309                                color_class = TrueColor;
1310                        break;
1311                default:
1312                        ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1313                        return false;
1314        }
1315        if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
1316                ErrorAlert(STR_NO_XVISUAL_ERR);
1317                return false;
1318        }
1319        if (visualInfo.depth != xdepth) {
1320                ErrorAlert(STR_NO_XVISUAL_ERR);
1321                return false;
1322        }
1323        vis = visualInfo.visual;
1324
1325        // Create color maps
1326        if (color_class == PseudoColor || color_class == DirectColor) {
1327                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1328                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1329        }
1330
1331        // Find pixel format of direct modes
1332        if (color_class == DirectColor || color_class == TrueColor) {
1333                rshift = gshift = bshift = 0;
1334                rloss = gloss = bloss = 8;
1335                uint32 mask;
1336                for (mask=vis->red_mask; !(mask&1); mask>>=1)
1337                        ++rshift;
1338                for (; mask&1; mask>>=1)
1339                        --rloss;
1340                for (mask=vis->green_mask; !(mask&1); mask>>=1)
1341                        ++gshift;
1342                for (; mask&1; mask>>=1)
1343                        --gloss;
1344                for (mask=vis->blue_mask; !(mask&1); mask>>=1)
1345                        ++bshift;
1346                for (; mask&1; mask>>=1)
1347                        --bloss;
1348        }
1349
1350        // Preset palette pixel values for gamma table
1351        if (color_class == DirectColor) {
1352                int num = vis->map_entries;
1353                for (int i=0; i<num; i++) {
1354                        int c = (i * 256) / num;
1355                        palette[i].pixel = map_rgb(c, c, c);
1356                }
1357        }
1358
1539          // Get screen mode from preferences
1540          const char *mode_str;
1541          if (classic_mode)
# Line 1390 | Line 1570 | bool VideoInit(bool classic)
1570                  default_height = DisplayHeight(x_display, screen);
1571  
1572          // Mac screen depth follows X depth
1573 <        video_depth default_depth = DepthModeForPixelDepth(xdepth);
1573 >        video_depth default_depth = VDEPTH_1BIT;
1574 >        switch (DefaultDepth(x_display, screen)) {
1575 >                case 8:
1576 >                        default_depth = VDEPTH_8BIT;
1577 >                        break;
1578 >                case 15: case 16:
1579 >                        default_depth = VDEPTH_16BIT;
1580 >                        break;
1581 >                case 24: case 32:
1582 >                        default_depth = VDEPTH_32BIT;
1583 >                        break;
1584 >        }
1585  
1586          // Construct list of supported modes
1587          if (display_type == DISPLAY_WINDOW) {
1588                  if (classic)
1589                          add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
1590                  else {
1591 <                        if (default_depth != VDEPTH_1BIT)
1592 <                                add_window_modes(VDEPTH_1BIT);  // 1-bit modes are always available
1593 < #ifdef ENABLE_VOSF
1403 <                        if (default_depth > VDEPTH_8BIT) {
1404 <                                add_window_modes(VDEPTH_2BIT);  // 2, 4 and 8-bit modes are also possible on 16/32-bit screens with VOSF blitters
1405 <                                add_window_modes(VDEPTH_4BIT);
1406 <                                add_window_modes(VDEPTH_8BIT);
1591 >                        for (unsigned d=VDEPTH_1BIT; d<=VDEPTH_32BIT; d++) {
1592 >                                if (find_visual_for_depth(video_depth(d)))
1593 >                                        add_window_modes(video_depth(d));
1594                          }
1408 #endif
1409                        add_window_modes(default_depth);
1595                  }
1596          } else
1597                  add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
1598 +        if (VideoModes.empty()) {
1599 +                ErrorAlert(STR_NO_XVISUAL_ERR);
1600 +                return false;
1601 +        }
1602          video_init_depth_list();
1603  
1604 + #if DEBUG
1605 +        D(bug("Available video modes:\n"));
1606 +        vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end();
1607 +        while (i != end) {
1608 +                int bits = 1 << i->depth;
1609 +                if (bits == 16)
1610 +                        bits = 15;
1611 +                else if (bits == 32)
1612 +                        bits = 24;
1613 +                D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1614 +                ++i;
1615 +        }
1616 + #endif
1617 +
1618          // Find requested default mode and open display
1619          if (VideoModes.size() == 1)
1620                  return video_open(VideoModes[0]);
# Line 1451 | Line 1654 | static void video_close(void)
1654          XSync(x_display, false);
1655  
1656   #ifdef ENABLE_VOSF
1454        // Deinitialize VOSF
1657          if (use_vosf) {
1658 <                if (mainBuffer.pageInfo) {
1659 <                        free(mainBuffer.pageInfo);
1458 <                        mainBuffer.pageInfo = NULL;
1459 <                }
1460 <                if (mainBuffer.dirtyPages) {
1461 <                        free(mainBuffer.dirtyPages);
1462 <                        mainBuffer.dirtyPages = NULL;
1463 <                }
1658 >                // Deinitialize VOSF
1659 >                video_vosf_exit();
1660          }
1661   #endif
1662  
1663          // Close display
1664          delete drv;
1665          drv = NULL;
1470 }
1471
1472 void VideoExit(void)
1473 {
1474        // Close display
1475        video_close();
1666  
1667          // Free colormaps
1668          if (cmap[0]) {
# Line 1483 | Line 1673 | void VideoExit(void)
1673                  XFreeColormap(x_display, cmap[1]);
1674                  cmap[1] = 0;
1675          }
1676 + }
1677 +
1678 + void VideoExit(void)
1679 + {
1680 +        // Close display
1681 +        video_close();
1682  
1683   #ifdef ENABLE_XF86_VIDMODE
1684          // Free video mode list
# Line 1499 | Line 1695 | void VideoExit(void)
1695                  fbdev_fd = -1;
1696          }
1697   #endif
1698 +
1699 +        // Free depth list
1700 +        if (avail_depths) {
1701 +                XFree(avail_depths);
1702 +                avail_depths = NULL;
1703 +        }
1704   }
1705  
1706  
# Line 1540 | Line 1742 | void video_set_palette(uint8 *pal, int n
1742  
1743          // Convert colors to XColor array
1744          int num_out = 256;
1745 +        bool stretch = false;
1746          if (IsDirectMode(VideoMonitor.mode)) {
1747                  // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1748                  num_out = vis->map_entries;
1749 +                stretch = true;
1750          }
1751          XColor *p = palette;
1752          for (int i=0; i<num_out; i++) {
1753 <                int c = (i * num_in) / num_out;
1753 >                int c = (stretch ? (i * num_in) / num_out : i);
1754                  p->red = pal[c*3 + 0] * 0x0101;
1755                  p->green = pal[c*3 + 1] * 0x0101;
1756                  p->blue = pal[c*3 + 2] * 0x0101;
1553                if (vis->c_class == PseudoColor)
1554                        p->pixel = i;
1555                p->flags = DoRed | DoGreen | DoBlue;
1757                  p++;
1758          }
1759  
1760   #ifdef ENABLE_VOSF
1761          // Recalculate pixel color expansion map
1762 <        if (!IsDirectMode(VideoMonitor.mode) && (vis->c_class == TrueColor || vis->c_class == DirectColor)) {
1762 >        if (!IsDirectMode(VideoMonitor.mode) && xdepth > 8) {
1763                  for (int i=0; i<256; i++) {
1764                          int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1765                          ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
# Line 1771 | Line 1972 | static void handle_events(void)
1972                  XNextEvent(x_display, &event);
1973  
1974                  switch (event.type) {
1975 +
1976                          // Mouse button
1977                          case ButtonPress: {
1978                                  unsigned int button = event.xbutton.button;
# Line 1803 | Line 2005 | static void handle_events(void)
2005                                  drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2006                                  break;
2007  
2008 +                        // Mouse entered window
2009 +                        case EnterNotify:
2010 +                                if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
2011 +                                        drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2012 +                                break;
2013 +
2014                          // Keyboard
2015                          case KeyPress: {
2016                                  int code = -1;
# Line 1980 | Line 2188 | static void update_display_dynamic(int t
2188   static void update_display_static(driver_window *drv)
2189   {
2190          // Incremental update code
2191 <        int wide = 0, high = 0, x1, x2, y1, y2, i, j;
2191 >        unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j;
2192          int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2193          int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2194          uint8 *p, *p2;
# Line 2139 | Line 2347 | static void video_refresh_dga(void)
2347   {
2348          // Quit DGA mode if requested
2349          possibly_quit_dga_mode();
2142        
2143        // Handle X events
2144        handle_events();
2145        
2146        // Handle palette changes
2147        handle_palette_changes();
2350   }
2351  
2352   #ifdef ENABLE_VOSF
# Line 2154 | Line 2356 | static void video_refresh_dga_vosf(void)
2356          // Quit DGA mode if requested
2357          possibly_quit_dga_mode();
2358          
2157        // Handle X events
2158        handle_events();
2159        
2160        // Handle palette changes
2161        handle_palette_changes();
2162        
2359          // Update display (VOSF variant)
2360          static int tick_counter = 0;
2361          if (++tick_counter >= frame_skip) {
# Line 2178 | Line 2374 | static void video_refresh_window_vosf(vo
2374          // Ungrab mouse if requested
2375          possibly_ungrab_mouse();
2376          
2181        // Handle X events
2182        handle_events();
2183        
2184        // Handle palette changes
2185        handle_palette_changes();
2186        
2377          // Update display (VOSF variant)
2378          static int tick_counter = 0;
2379          if (++tick_counter >= frame_skip) {
# Line 2203 | Line 2393 | static void video_refresh_window_static(
2393          // Ungrab mouse if requested
2394          possibly_ungrab_mouse();
2395  
2206        // Handle X events
2207        handle_events();
2208        
2209        // Handle_palette changes
2210        handle_palette_changes();
2211        
2396          // Update display (static variant)
2397          static int tick_counter = 0;
2398          if (++tick_counter >= frame_skip) {
# Line 2222 | Line 2406 | static void video_refresh_window_dynamic
2406          // Ungrab mouse if requested
2407          possibly_ungrab_mouse();
2408  
2225        // Handle X events
2226        handle_events();
2227        
2228        // Handle_palette changes
2229        handle_palette_changes();
2230        
2409          // Update display (dynamic variant)
2410          static int tick_counter = 0;
2411          tick_counter++;
# Line 2263 | Line 2441 | static void VideoRefreshInit(void)
2441          }
2442   }
2443  
2444 + // This function is called on non-threaded platforms from a timer interrupt
2445   void VideoRefresh(void)
2446   {
2447          // We need to check redraw_thread_active to inhibit refreshed during
2448          // mode changes on non-threaded platforms
2449 <        if (redraw_thread_active)
2450 <                video_refresh();
2449 >        if (!redraw_thread_active)
2450 >                return;
2451 >
2452 >        // Handle X events
2453 >        handle_events();
2454 >
2455 >        // Handle palette changes
2456 >        handle_palette_changes();
2457 >
2458 >        // Update display
2459 >        video_refresh();
2460   }
2461  
2462 + const int VIDEO_REFRESH_HZ = 60;
2463 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2464 +
2465   #ifdef HAVE_PTHREADS
2466   static void *redraw_func(void *arg)
2467   {
2468 +        int fd = ConnectionNumber(x_display);
2469 +
2470          uint64 start = GetTicks_usec();
2471          int64 ticks = 0;
2472 <        uint64 next = GetTicks_usec();
2472 >        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2473 >
2474          while (!redraw_thread_cancel) {
2475 <                video_refresh();
2282 <                next += 16667;
2475 >
2476                  int64 delay = next - GetTicks_usec();
2477 <                if (delay > 0)
2478 <                        Delay_usec(delay);
2479 <                else if (delay < -16667)
2477 >                if (delay < -VIDEO_REFRESH_DELAY) {
2478 >
2479 >                        // We are lagging far behind, so we reset the delay mechanism
2480                          next = GetTicks_usec();
2481 <                ticks++;
2481 >
2482 >                } else if (delay <= 0) {
2483 >
2484 >                        // Delay expired, refresh display
2485 >                        handle_events();
2486 >                        handle_palette_changes();
2487 >                        video_refresh();
2488 >                        next += VIDEO_REFRESH_DELAY;
2489 >                        ticks++;
2490 >
2491 >                } else {
2492 >
2493 >                        // No display refresh pending, check for X events
2494 >                        fd_set readfds;
2495 >                        FD_ZERO(&readfds);
2496 >                        FD_SET(fd, &readfds);
2497 >                        struct timeval timeout;
2498 >                        timeout.tv_sec = 0;
2499 >                        timeout.tv_usec = delay;
2500 >                        if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2501 >                                handle_events();
2502 >                }
2503          }
2504 +
2505          uint64 end = GetTicks_usec();
2506 <        // printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start));
2506 >        D(bug("%Ld refreshes in %Ld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
2507          return NULL;
2508   }
2509   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines