/[cebix]/SheepShaver/src/Unix/video_x.cpp
ViewVC logotype

Diff of /SheepShaver/src/Unix/video_x.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.13

Christian Bauer">Christian Bauer
ViewVC Help
Powered by ViewVC 1.1.15