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.28 by cebix, 2000-11-03T12:23:49Z vs.
Revision 1.37 by cebix, 2001-03-06T18:41:12Z

# Line 1 | Line 1
1   /*
2   *  video_x.cpp - Video/graphics emulation, X11 specific stuff
3   *
4 < *  Basilisk II (C) 1997-2000 Christian Bauer
4 > *  Basilisk II (C) 1997-2001 Christian Bauer
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 122 | Line 122 | static Colormap cmap[2];                                                       // Two co
122   static XColor black, white;
123   static unsigned long black_pixel, white_pixel;
124   static int eventmask;
125 < static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | FocusChangeMask | ExposureMask;
126 < static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
125 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
126 > static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
127 > static Atom WM_DELETE_WINDOW = (Atom)0;
128  
129   static XColor palette[256];                                                     // Color palette for 8-bit mode
130   static bool palette_changed = false;                            // Flag: Palette changed, redraw thread must set new colors
131   #ifdef HAVE_PTHREADS
132   static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;        // Mutex to protect palette
133 + #define LOCK_PALETTE pthread_mutex_lock(&palette_lock)
134 + #define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock)
135 + #else
136 + #define LOCK_PALETTE
137 + #define UNLOCK_PALETTE
138   #endif
139  
140   // Variables for window mode
# Line 149 | Line 155 | static Window suspend_win;                                                     // "Sus
155   static void *fb_save = NULL;                                            // Saved frame buffer for suspend
156   #ifdef HAVE_PTHREADS
157   static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer
158 + #define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock);
159 + #define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock);
160 + #else
161 + #define LOCK_FRAME_BUFFER
162 + #define UNLOCK_FRAME_BUFFER
163   #endif
164  
165   // Variables for fbdev DGA mode
# Line 168 | Line 179 | static const bool use_vosf = false;
179   #endif
180  
181   #ifdef ENABLE_VOSF
182 < static uint8 * the_host_buffer;                                         // Host frame buffer in VOSF mode
182 > // Variables for Video on SEGV support (taken from the Win32 port)
183 > static uint8 *the_host_buffer;                                          // Host frame buffer in VOSF mode
184   static uint32 the_buffer_size;                                          // Size of allocated the_buffer
173 #endif
185  
175 #ifdef ENABLE_VOSF
176 // Variables for Video on SEGV support (taken from the Win32 port)
186   struct ScreenPageInfo {
187      int top, bottom;                    // Mapping between this virtual page and Mac scanlines
188   };
# Line 188 | Line 197 | struct ScreenInfo {
197      int pageBits;                               // Shift count to get the page number
198      uint32 pageCount;                   // Number of pages allocated to the screen
199      
200 <    uint8 * dirtyPages;                 // Table of flags set if page was altered
200 >        bool dirty;                                     // Flag: set if the frame buffer was touched
201 >    char * dirtyPages;                  // Table of flags set if page was altered
202      ScreenPageInfo * pageInfo;  // Table of mappings page -> Mac scanlines
203   };
204  
205   static ScreenInfo mainBuffer;
206  
207 < #define PFLAG_SET(page)                 mainBuffer.dirtyPages[page] = 1
208 < #define PFLAG_CLEAR(page)               mainBuffer.dirtyPages[page] = 0
209 < #define PFLAG_ISSET(page)               mainBuffer.dirtyPages[page]
210 < #define PFLAG_ISCLEAR(page)             (mainBuffer.dirtyPages[page] == 0)
207 > #define PFLAG_SET_VALUE                 0x00
208 > #define PFLAG_CLEAR_VALUE               0x01
209 > #define PFLAG_SET_VALUE_4               0x00000000
210 > #define PFLAG_CLEAR_VALUE_4             0x01010101
211 > #define PFLAG_SET(page)                 mainBuffer.dirtyPages[page] = PFLAG_SET_VALUE
212 > #define PFLAG_CLEAR(page)               mainBuffer.dirtyPages[page] = PFLAG_CLEAR_VALUE
213 > #define PFLAG_ISSET(page)               (mainBuffer.dirtyPages[page] == PFLAG_SET_VALUE)
214 > #define PFLAG_ISCLEAR(page)             (mainBuffer.dirtyPages[page] != PFLAG_SET_VALUE)
215 >
216   #ifdef UNALIGNED_PROFITABLE
217 < # define PFLAG_ISCLEAR_4(page)  (*((uint32 *)(mainBuffer.dirtyPages + page)) == 0)
217 > # define PFLAG_ISSET_4(page)    (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_SET_VALUE_4)
218 > # define PFLAG_ISCLEAR_4(page)  (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_CLEAR_VALUE_4)
219 > #else
220 > # define PFLAG_ISSET_4(page) \
221 >                PFLAG_ISSET(page  ) && PFLAG_ISSET(page+1) \
222 >        &&      PFLAG_ISSET(page+2) && PFLAG_ISSET(page+3)
223 > # define PFLAG_ISCLEAR_4(page) \
224 >                PFLAG_ISCLEAR(page  ) && PFLAG_ISCLEAR(page+1) \
225 >        &&      PFLAG_ISCLEAR(page+2) && PFLAG_ISCLEAR(page+3)
226 > #endif
227 >
228 > // Set the selected page range [ first_page, last_page [ into the SET state
229 > #define PFLAG_SET_RANGE(first_page, last_page) \
230 >        memset(mainBuffer.dirtyPages + (first_page), PFLAG_SET_VALUE, \
231 >                (last_page) - (first_page))
232 >
233 > // Set the selected page range [ first_page, last_page [ into the CLEAR state
234 > #define PFLAG_CLEAR_RANGE(first_page, last_page) \
235 >        memset(mainBuffer.dirtyPages + (first_page), PFLAG_CLEAR_VALUE, \
236 >                (last_page) - (first_page))
237 >
238 > #define PFLAG_SET_ALL do { \
239 >        PFLAG_SET_RANGE(0, mainBuffer.pageCount); \
240 >        mainBuffer.dirty = true; \
241 > } while (0)
242 >
243 > #define PFLAG_CLEAR_ALL do { \
244 >        PFLAG_CLEAR_RANGE(0, mainBuffer.pageCount); \
245 >        mainBuffer.dirty = false; \
246 > } while (0)
247 >
248 > // Set the following macro definition to 1 if your system
249 > // provides a really fast strchr() implementation
250 > //#define HAVE_FAST_STRCHR 0
251 >
252 > static inline int find_next_page_set(int page)
253 > {
254 > #if HAVE_FAST_STRCHR
255 >        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_SET_VALUE);
256 >        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
257 > #else
258 >        while (PFLAG_ISCLEAR_4(page))
259 >                page += 4;
260 >        while (PFLAG_ISCLEAR(page))
261 >                page++;
262 >        return page;
263 > #endif
264 > }
265 >
266 > static inline int find_next_page_clear(int page)
267 > {
268 > #if HAVE_FAST_STRCHR
269 >        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_CLEAR_VALUE);
270 >        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
271   #else
272 < # define PFLAG_ISCLEAR_4(page)  \
273 <                (mainBuffer.dirtyPages[page  ] == 0) \
274 <        &&      (mainBuffer.dirtyPages[page+1] == 0) \
275 <        &&      (mainBuffer.dirtyPages[page+2] == 0) \
276 <        &&      (mainBuffer.dirtyPages[page+3] == 0)
272 >        while (PFLAG_ISSET_4(page))
273 >                page += 4;
274 >        while (PFLAG_ISSET(page))
275 >                page++;
276 >        return page;
277   #endif
278 < #define PFLAG_CLEAR_ALL                 memset(mainBuffer.dirtyPages, 0, mainBuffer.pageCount)
211 < #define PFLAG_SET_ALL                   memset(mainBuffer.dirtyPages, 1, mainBuffer.pageCount)
278 > }
279  
280   static int zero_fd = -1;
281   static bool Screen_fault_handler_init();
282   static struct sigaction vosf_sa;
283  
284   #ifdef HAVE_PTHREADS
285 < static pthread_mutex_t Screen_draw_lock = PTHREAD_MUTEX_INITIALIZER;    // Mutex to protect frame buffer (dirtyPages in fact)
285 > static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer (dirtyPages in fact)
286 > #define LOCK_VOSF pthread_mutex_lock(&vosf_lock);
287 > #define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock);
288 > #else
289 > #define LOCK_VOSF
290 > #define UNLOCK_VOSF
291   #endif
292  
293   static int log_base_2(uint32 x)
# Line 228 | Line 300 | static int log_base_2(uint32 x)
300          }
301          return l;
302   }
303 <
232 < #endif
303 > #endif /* ENABLE_VOSF */
304  
305   // VideoRefresh function
306   void VideoRefreshInit(void);
# Line 237 | Line 308 | static void (*video_refresh)(void);
308  
309   // Prototypes
310   static void *redraw_func(void *arg);
311 < static int event2keycode(XKeyEvent *ev);
311 > static int event2keycode(XKeyEvent &ev);
312  
313  
314   // From main_unix.cpp
# Line 307 | Line 378 | void set_video_monitor(int width, int he
378          VideoMonitor.bytes_per_row = bytes_per_row;
379   }
380  
381 + // Set window name and class
382 + static void set_window_name(Window w, int name)
383 + {
384 +        const char *str = GetString(name);
385 +        XStoreName(x_display, w, str);
386 +        XSetIconName(x_display, w, str);
387 +
388 +        XClassHint *hints;
389 +        hints = XAllocClassHint();
390 +        if (hints) {
391 +                hints->res_name = "BasiliskII";
392 +                hints->res_class = "BasiliskII";
393 +                XSetClassHint(x_display, w, hints);
394 +                XFree(hints);
395 +        }
396 + }
397 +
398 + // Set window input focus flag
399 + static void set_window_focus(Window w)
400 + {
401 +        XWMHints *hints = XAllocWMHints();
402 +        if (hints) {
403 +                hints->input = True;
404 +                hints->initial_state = NormalState;
405 +                hints->flags = InputHint | StateHint;
406 +                XSetWMHints(x_display, w, hints);
407 +                XFree(hints);
408 +        }
409 + }
410 +
411 + // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
412 + static void set_window_delete_protocol(Window w)
413 + {
414 +        WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
415 +        XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
416 + }
417 +
418 + // Wait until window is mapped/unmapped
419 + void wait_mapped(Window w)
420 + {
421 +        XEvent e;
422 +        do {
423 +                XMaskEvent(x_display, StructureNotifyMask, &e);
424 +        } while ((e.type != MapNotify) || (e.xmap.event != w));
425 + }
426 +
427 + void wait_unmapped(Window w)
428 + {
429 +        XEvent e;
430 +        do {
431 +                XMaskEvent(x_display, StructureNotifyMask, &e);
432 +        } while ((e.type != UnmapNotify) || (e.xmap.event != w));
433 + }
434 +
435   // Trap SHM errors
436   static bool shm_error = false;
437   static int (*old_error_handler)(Display *, XErrorEvent *);
# Line 336 | Line 461 | static bool init_window(int width, int h
461          XSetWindowAttributes wattr;
462          wattr.event_mask = eventmask = win_eventmask;
463          wattr.background_pixel = black_pixel;
464 +        wattr.colormap = cmap[0];
465  
340        XSync(x_display, false);
466          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
467 <                InputOutput, vis, CWEventMask | CWBackPixel, &wattr);
467 >                InputOutput, vis, CWEventMask | CWBackPixel | (depth == 8 ? CWColormap : 0), &wattr);
468 >
469 >        // Set window name/class
470 >        set_window_name(the_win, STR_WINDOW_TITLE);
471  
472          // Indicate that we want keyboard input
473 <        {
474 <                XWMHints *hints = XAllocWMHints();
475 <                if (hints) {
476 <                        hints->input = True;
349 <                        hints->flags = InputHint;
350 <                        XSetWMHints(x_display, the_win, hints);
351 <                        XFree((char *)hints);
352 <                }
353 <        }
473 >        set_window_focus(the_win);
474 >
475 >        // Set delete protocol property
476 >        set_window_delete_protocol(the_win);
477  
478          // Make window unresizable
479          {
# Line 362 | Line 485 | static bool init_window(int width, int h
485                          hints->max_height = height;
486                          hints->flags = PMinSize | PMaxSize;
487                          XSetWMNormalHints(x_display, the_win, hints);
488 <                        XFree((char *)hints);
488 >                        XFree(hints);
489                  }
490          }
491          
369        // Set window title
370        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
371
372        // Set window class
373        {
374                XClassHint *hints;
375                hints = XAllocClassHint();
376                if (hints) {
377                        hints->res_name = "BasiliskII";
378                        hints->res_class = "BasiliskII";
379                        XSetClassHint(x_display, the_win, hints);
380                        XFree((char *)hints);
381                }
382        }
383
492          // Show window
493 <        XSync(x_display, false);
494 <        XMapRaised(x_display, the_win);
387 <        XFlush(x_display);
388 <
389 <        // Set colormap
390 <        if (depth == 8)
391 <                XSetWindowColormap(x_display, the_win, cmap[0]);
493 >        XMapWindow(x_display, the_win);
494 >        wait_mapped(the_win);
495  
496          // Try to create and attach SHM image
497          have_shm = false;
# Line 472 | Line 575 | static bool init_window(int width, int h
575          // Set VideoMonitor
576          bool native_byte_order;
577   #ifdef WORDS_BIGENDIAN
578 <        native_byte_order = (img->bitmap_bit_order == MSBFirst);
578 >        native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
579   #else
580 <        native_byte_order = (img->bitmap_bit_order == LSBFirst);
580 >        native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
581   #endif
582   #ifdef ENABLE_VOSF
583 <        do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW);
583 >        Screen_blitter_init(&visualInfo, native_byte_order);
584   #endif
585          set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
586          
# Line 561 | Line 664 | static bool init_fbdev_dga(char *in_fb_n
664          
665          // Create window
666          XSetWindowAttributes wattr;
667 <        wattr.event_mask                = eventmask = dga_eventmask;
668 <        wattr.background_pixel  = white_pixel;
669 <        wattr.override_redirect = True;
667 >        wattr.event_mask = eventmask = dga_eventmask;
668 >        wattr.background_pixel = white_pixel;
669 >        wattr.override_redirect = True;
670 >        wattr.colormap = cmap[0];
671          
568        XSync(x_display, false);
672          the_win = XCreateWindow(x_display, rootwin,
673                  0, 0, width, height,
674                  0, xdepth, InputOutput, vis,
675 <                CWEventMask | CWBackPixel | CWOverrideRedirect,
675 >                CWEventMask | CWBackPixel | CWOverrideRedirect | (depth == 8 ? CWColormap : 0),
676                  &wattr);
677 <        XSync(x_display, false);
677 >
678 >        // Set window name/class
679 >        set_window_name(the_win, STR_WINDOW_TITLE);
680 >
681 >        // Indicate that we want keyboard input
682 >        set_window_focus(the_win);
683 >
684 >        // Show window
685          XMapRaised(x_display, the_win);
686 <        XSync(x_display, false);
686 >        wait_mapped(the_win);
687          
688          // Grab mouse and keyboard
689          XGrabKeyboard(x_display, the_win, True,
# Line 582 | Line 692 | static bool init_fbdev_dga(char *in_fb_n
692                  PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
693                  GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
694          
585        // Set colormap
586        if (depth == 8)
587                XSetWindowColormap(x_display, the_win, cmap[0]);
588        
695          // Set VideoMonitor
696          int bytes_per_row = width;
697          switch (depth) {
# Line 613 | Line 719 | static bool init_fbdev_dga(char *in_fb_n
719          
720   #if ENABLE_VOSF
721   #if REAL_ADDRESSING || DIRECT_ADDRESSING
722 <        // If the blit function is null, i.e. just a copy of the buffer,
723 <        // we first try to avoid the allocation of a temporary frame buffer
724 <        use_vosf = true;
619 <        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
620 <        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
621 <                use_vosf = false;
722 >        // Screen_blitter_init() returns TRUE if VOSF is mandatory
723 >        // i.e. the framebuffer update function is not Blit_Copy_Raw
724 >        use_vosf = Screen_blitter_init(&visualInfo, true);
725          
726          if (use_vosf) {
727                  the_host_buffer = the_buffer;
# Line 665 | Line 768 | static bool init_xf86_dga(int width, int
768                  }
769                  XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
770                  XF86VidModeSetViewPort(x_display, screen, 0, 0);
771 +                XSync(x_display, false);
772          }
773   #endif
774  
# Line 673 | Line 777 | static bool init_xf86_dga(int width, int
777          wattr.event_mask = eventmask = dga_eventmask;
778          wattr.override_redirect = True;
779  
676        XSync(x_display, false);
780          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
781                  InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
782 <        XSync(x_display, false);
783 <        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
782 >
783 >        // Set window name/class
784 >        set_window_name(the_win, STR_WINDOW_TITLE);
785 >
786 >        // Indicate that we want keyboard input
787 >        set_window_focus(the_win);
788 >
789 >        // Show window
790          XMapRaised(x_display, the_win);
791 <        XSync(x_display, false);
791 >        wait_mapped(the_win);
792  
793          // Establish direct screen connection
794          XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
# Line 698 | Line 807 | static bool init_xf86_dga(int width, int
807                  XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
808                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
809          }
810 +        XSync(x_display, false);
811  
812          // Set VideoMonitor
813          int bytes_per_row = (v_width + 7) & ~7;
# Line 716 | Line 826 | static bool init_xf86_dga(int width, int
826          }
827          
828   #if REAL_ADDRESSING || DIRECT_ADDRESSING
829 <        // If the blit function is null, i.e. just a copy of the buffer,
830 <        // we first try to avoid the allocation of a temporary frame buffer
831 <        use_vosf = true;
722 <        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
723 <        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
724 <                use_vosf = false;
829 >        // Screen_blitter_init() returns TRUE if VOSF is mandatory
830 >        // i.e. the framebuffer update function is not Blit_Copy_Raw
831 >        use_vosf = Screen_blitter_init(&visualInfo, true);
832          
833          if (use_vosf) {
834                  the_host_buffer = the_buffer;
# Line 835 | Line 942 | bool VideoInitBuffer()
942                  if (mainBuffer.dirtyPages != 0)
943                          free(mainBuffer.dirtyPages);
944  
945 <                mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount);
945 >                mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
946  
947                  if (mainBuffer.pageInfo != 0)
948                          free(mainBuffer.pageInfo);
# Line 844 | Line 951 | bool VideoInitBuffer()
951  
952                  if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
953                          return false;
954 +                
955 +                mainBuffer.dirty = false;
956  
957                  PFLAG_CLEAR_ALL;
958 +                // Safety net to insure the loops in the update routines will terminate
959 +                // See a discussion in <video_vosf.h> for further details
960 +                PFLAG_CLEAR(mainBuffer.pageCount);
961 +                PFLAG_SET(mainBuffer.pageCount+1);
962  
963                  uint32 a = 0;
964                  for (int i = 0; i < mainBuffer.pageCount; i++) {
# Line 898 | Line 1011 | bool VideoInit(bool classic)
1011          keycode_init();
1012  
1013          // Read prefs
1014 <        mouse_wheel_mode = PrefsFindInt16("mousewheelmode");
1015 <        mouse_wheel_lines = PrefsFindInt16("mousewheellines");
1014 >        mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
1015 >        mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1016  
1017          // Find screen and root window
1018          screen = XDefaultScreen(x_display);
# Line 1036 | Line 1149 | bool VideoInit(bool classic)
1149                          break;
1150          }
1151  
1039 #ifdef HAVE_PTHREADS
1152          // Lock down frame buffer
1153 <        pthread_mutex_lock(&frame_buffer_lock);
1042 < #endif
1153 >        LOCK_FRAME_BUFFER;
1154  
1155   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1156          // Set variables for UAE memory mapping
# Line 1104 | Line 1215 | void VideoExit(void)
1215          }
1216   #endif
1217  
1107 #ifdef HAVE_PTHREADS
1218          // Unlock frame buffer
1219 <        pthread_mutex_unlock(&frame_buffer_lock);
1110 < #endif
1219 >        UNLOCK_FRAME_BUFFER;
1220  
1221          // Close window and server connection
1222          if (x_display != NULL) {
# Line 1210 | Line 1319 | void VideoInterrupt(void)
1319          if (emerg_quit)
1320                  QuitEmulator();
1321  
1213 #ifdef HAVE_PTHREADS
1322          // Temporarily give up frame buffer lock (this is the point where
1323          // we are suspended when the user presses Ctrl-Tab)
1324 <        pthread_mutex_unlock(&frame_buffer_lock);
1325 <        pthread_mutex_lock(&frame_buffer_lock);
1218 < #endif
1324 >        UNLOCK_FRAME_BUFFER;
1325 >        LOCK_FRAME_BUFFER;
1326   }
1327  
1328  
# Line 1225 | Line 1332 | void VideoInterrupt(void)
1332  
1333   void video_set_palette(uint8 *pal)
1334   {
1335 < #ifdef HAVE_PTHREADS
1229 <        pthread_mutex_lock(&palette_lock);
1230 < #endif
1335 >        LOCK_PALETTE;
1336  
1337          // Convert colors to XColor array
1338          for (int i=0; i<256; i++) {
# Line 1241 | Line 1346 | void video_set_palette(uint8 *pal)
1346          // Tell redraw thread to change palette
1347          palette_changed = true;
1348  
1349 < #ifdef HAVE_PTHREADS
1245 <        pthread_mutex_unlock(&palette_lock);
1246 < #endif
1349 >        UNLOCK_PALETTE;
1350   }
1351  
1352  
# Line 1259 | Line 1362 | static void suspend_emul(void)
1362                  ADBKeyUp(0x36);
1363                  ctrl_down = false;
1364  
1262 #ifdef HAVE_PTHREADS
1365                  // Lock frame buffer (this will stop the MacOS thread)
1366 <                pthread_mutex_lock(&frame_buffer_lock);
1265 < #endif
1366 >                LOCK_FRAME_BUFFER;
1367  
1368                  // Save frame buffer
1369                  fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
# Line 1276 | Line 1377 | static void suspend_emul(void)
1377                  XUngrabPointer(x_display, CurrentTime);
1378                  XUngrabKeyboard(x_display, CurrentTime);
1379                  XUnmapWindow(x_display, the_win);
1380 <                XSync(x_display, false);
1380 >                wait_unmapped(the_win);
1381  
1382                  // Open "suspend" window
1383                  XSetWindowAttributes wattr;
1384                  wattr.event_mask = KeyPressMask;
1385                  wattr.background_pixel = black_pixel;
1285                wattr.backing_store = WhenMapped;
1286                wattr.backing_planes = xdepth;
1287                wattr.colormap = DefaultColormap(x_display, screen);
1386                  
1289                XSync(x_display, false);
1387                  suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1388 <                        InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore |
1389 <                        CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1390 <                XSync(x_display, false);
1391 <                XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1295 <                XMapRaised(x_display, suspend_win);
1296 <                XSync(x_display, false);
1388 >                        InputOutput, vis, CWEventMask | CWBackPixel, &wattr);
1389 >                set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
1390 >                set_window_focus(suspend_win);
1391 >                XMapWindow(x_display, suspend_win);
1392                  emul_suspended = true;
1393          }
1394   }
# Line 1306 | Line 1401 | static void resume_emul(void)
1401  
1402          // Reopen full screen display
1403          XMapRaised(x_display, the_win);
1404 +        wait_mapped(the_win);
1405          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1310        XSync(x_display, false);
1406          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1407          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1408   #ifdef ENABLE_XF86_DGA
# Line 1321 | Line 1416 | static void resume_emul(void)
1416          // not necessary.
1417   #ifdef ENABLE_VOSF
1418          if (use_vosf) {
1419 < #ifdef HAVE_PTHREADS
1325 <                pthread_mutex_lock(&Screen_draw_lock);
1326 < #endif
1419 >                LOCK_VOSF;
1420                  PFLAG_SET_ALL;
1421 < #ifdef HAVE_PTHREADS
1329 <                pthread_mutex_unlock(&Screen_draw_lock);
1330 < #endif
1421 >                UNLOCK_VOSF;
1422                  memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1423          }
1424   #endif
# Line 1348 | Line 1439 | static void resume_emul(void)
1439                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1440   #endif
1441  
1351 #ifdef HAVE_PTHREADS
1442          // Unlock frame buffer (and continue MacOS thread)
1443 <        pthread_mutex_unlock(&frame_buffer_lock);
1443 >        UNLOCK_FRAME_BUFFER;
1444          emul_suspended = false;
1355 #endif
1445   }
1446   #endif
1447  
# Line 1506 | Line 1595 | static int kc_decode(KeySym ks)
1595          return -1;
1596   }
1597  
1598 < static int event2keycode(XKeyEvent *ev)
1598 > static int event2keycode(XKeyEvent &ev)
1599   {
1600          KeySym ks;
1601          int as;
1602          int i = 0;
1603  
1604          do {
1605 <                ks = XLookupKeysym(ev, i++);
1605 >                ks = XLookupKeysym(&ev, i++);
1606                  as = kc_decode(ks);
1607                  if (as != -1)
1608                          return as;
# Line 1529 | Line 1618 | static int event2keycode(XKeyEvent *ev)
1618  
1619   static void handle_events(void)
1620   {
1621 <        XEvent event;
1622 <        for (;;) {
1623 <                if (!XCheckMaskEvent(x_display, eventmask, &event))
1535 <                        break;
1621 >        while (XPending(x_display)) {
1622 >                XEvent event;
1623 >                XNextEvent(x_display, &event);
1624  
1625                  switch (event.type) {
1626                          // Mouse button
1627                          case ButtonPress: {
1628 <                                unsigned int button = ((XButtonEvent *)&event)->button;
1628 >                                unsigned int button = event.xbutton.button;
1629                                  if (button < 4)
1630                                          ADBMouseDown(button - 1);
1631                                  else if (button < 6) {  // Wheel mouse
# Line 1556 | Line 1644 | static void handle_events(void)
1644                                  break;
1645                          }
1646                          case ButtonRelease: {
1647 <                                unsigned int button = ((XButtonEvent *)&event)->button;
1647 >                                unsigned int button = event.xbutton.button;
1648                                  if (button < 4)
1649                                          ADBMouseUp(button - 1);
1650                                  break;
# Line 1564 | Line 1652 | static void handle_events(void)
1652  
1653                          // Mouse moved
1654                          case EnterNotify:
1567                                ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1568                                break;
1655                          case MotionNotify:
1656 <                                ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1656 >                                ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1657                                  break;
1658  
1659                          // Keyboard
1660                          case KeyPress: {
1661                                  int code;
1662                                  if (use_keycodes) {
1663 <                                        event2keycode((XKeyEvent *)&event);     // This is called to process the hotkeys
1664 <                                        code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1663 >                                        event2keycode(event.xkey);      // This is called to process the hotkeys
1664 >                                        code = keycode_table[event.xkey.keycode & 0xff];
1665                                  } else
1666 <                                        code = event2keycode((XKeyEvent *)&event);
1666 >                                        code = event2keycode(event.xkey);
1667                                  if (code != -1) {
1668                                          if (!emul_suspended) {
1669                                                  if (code == 0x39) {     // Caps Lock pressed
# Line 1604 | Line 1690 | static void handle_events(void)
1690                          case KeyRelease: {
1691                                  int code;
1692                                  if (use_keycodes) {
1693 <                                        event2keycode((XKeyEvent *)&event);     // This is called to process the hotkeys
1694 <                                        code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1693 >                                        event2keycode(event.xkey);      // This is called to process the hotkeys
1694 >                                        code = keycode_table[event.xkey.keycode & 0xff];
1695                                  } else
1696 <                                        code = event2keycode((XKeyEvent *)&event);
1696 >                                        code = event2keycode(event.xkey);
1697                                  if (code != -1 && code != 0x39) {       // Don't propagate Caps Lock releases
1698                                          ADBKeyUp(code);
1699                                          if (code == 0x36)
# Line 1621 | Line 1707 | static void handle_events(void)
1707                                  if (display_type == DISPLAY_WINDOW) {
1708   #ifdef ENABLE_VOSF
1709                                          if (use_vosf) {                 // VOSF refresh
1710 < #ifdef HAVE_PTHREADS
1625 <                                                pthread_mutex_lock(&Screen_draw_lock);
1626 < #endif
1710 >                                                LOCK_VOSF;
1711                                                  PFLAG_SET_ALL;
1712 < #ifdef HAVE_PTHREADS
1629 <                                                pthread_mutex_unlock(&Screen_draw_lock);
1630 < #endif
1712 >                                                UNLOCK_VOSF;
1713                                                  memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1714                                          }
1715                                          else
# Line 1643 | Line 1725 | static void handle_events(void)
1725                                  }
1726                                  break;
1727  
1728 <                        case FocusIn:
1729 <                        case FocusOut:
1728 >                        // Window "close" widget clicked
1729 >                        case ClientMessage:
1730 >                                if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
1731 >                                        ADBKeyDown(0x7f);       // Power key
1732 >                                        ADBKeyUp(0x7f);
1733 >                                }
1734                                  break;
1735                  }
1736          }
# Line 1864 | Line 1950 | static void update_display_static(void)
1950   *      Screen refresh functions
1951   */
1952  
1953 < // The specialisations hereunder are meant to enable VOSF with DGA in direct
1954 < // addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could
1955 < // not get mapped correctly with respect to the predetermined host frame
1956 < // buffer base address.
1957 < //
1872 < // Hmm, in other words, when in direct addressing mode and DGA is requested,
1873 < // we first try to "triple allocate" the address spaces according to the real
1874 < // host frame buffer address. Then, if it fails, we will use a temporary
1875 < // frame buffer thus making the real host frame buffer updated when pages
1876 < // of the temp frame buffer are altered.
1877 < //
1878 < // As a side effect, a little speed gain in screen updates could be noticed
1879 < // for other modes than DGA.
1880 < //
1881 < // The following two functions below are inline so that a clever compiler
1882 < // could specialise the code according to the current screen depth and
1883 < // display type. A more clever compiler would the job by itself though...
1884 < // (update_display_vosf is inlined as well)
1953 > // We suggest the compiler to inline the next two functions so that it
1954 > // may specialise the code according to the current screen depth and
1955 > // display type. A clever compiler would do that job by itself though...
1956 >
1957 > // NOTE: update_display_vosf is inlined too
1958  
1959   static inline void possibly_quit_dga_mode()
1960   {
# Line 1902 | Line 1975 | static inline void possibly_quit_dga_mod
1975  
1976   static inline void handle_palette_changes(int depth, int display_type)
1977   {
1978 < #ifdef HAVE_PTHREADS
1979 <        pthread_mutex_lock(&palette_lock);
1907 < #endif
1978 >        LOCK_PALETTE;
1979 >
1980          if (palette_changed) {
1981                  palette_changed = false;
1982                  if (depth == 8) {
1983                          XStoreColors(x_display, cmap[0], palette, 256);
1984                          XStoreColors(x_display, cmap[1], palette, 256);
1985 +                        XSync(x_display, false);
1986                                  
1987   #ifdef ENABLE_XF86_DGA
1988                          if (display_type == DISPLAY_DGA) {
# Line 1919 | Line 1992 | static inline void handle_palette_change
1992   #endif
1993                  }
1994          }
1995 < #ifdef HAVE_PTHREADS
1996 <        pthread_mutex_unlock(&palette_lock);
1924 < #endif
1995 >
1996 >        UNLOCK_PALETTE;
1997   }
1998  
1999   static void video_refresh_dga(void)
# Line 1953 | Line 2025 | static void video_refresh_dga_vosf(void)
2025          static int tick_counter = 0;
2026          if (++tick_counter >= frame_skip) {
2027                  tick_counter = 0;
2028 < #ifdef HAVE_PTHREADS
2029 <                pthread_mutex_lock(&Screen_draw_lock);
2030 < #endif
2031 <                update_display_dga_vosf();
2032 < #ifdef HAVE_PTHREADS
1961 <                pthread_mutex_unlock(&Screen_draw_lock);
1962 < #endif
2028 >                if (mainBuffer.dirty) {
2029 >                        LOCK_VOSF;
2030 >                        update_display_dga_vosf();
2031 >                        UNLOCK_VOSF;
2032 >                }
2033          }
2034   }
2035   #endif
# Line 1979 | Line 2049 | static void video_refresh_window_vosf(vo
2049          static int tick_counter = 0;
2050          if (++tick_counter >= frame_skip) {
2051                  tick_counter = 0;
2052 < #ifdef HAVE_PTHREADS
2053 <                pthread_mutex_lock(&Screen_draw_lock);
2054 < #endif
2055 <                update_display_window_vosf();
2056 < #ifdef HAVE_PTHREADS
2057 <                pthread_mutex_unlock(&Screen_draw_lock);
1988 < #endif
2052 >                if (mainBuffer.dirty) {
2053 >                        LOCK_VOSF;
2054 >                        update_display_window_vosf();
2055 >                        UNLOCK_VOSF;
2056 >                        XSync(x_display, false); // Let the server catch up
2057 >                }
2058          }
2059   }
2060   #endif // def ENABLE_VOSF
# Line 2055 | Line 2124 | void VideoRefresh(void)
2124          video_refresh();
2125   }
2126  
2058 #if 0
2059 void VideoRefresh(void)
2060 {
2061 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2062        // Quit DGA mode if requested
2063        if (quit_full_screen) {
2064                quit_full_screen = false;
2065                if (display_type == DISPLAY_DGA) {
2066 #ifdef ENABLE_XF86_DGA
2067                        XF86DGADirectVideo(x_display, screen, 0);
2068 #endif
2069                        XUngrabPointer(x_display, CurrentTime);
2070                        XUngrabKeyboard(x_display, CurrentTime);
2071                        XUnmapWindow(x_display, the_win);
2072                        XSync(x_display, false);
2073                }
2074        }
2075 #endif
2076
2077        // Handle X events
2078        handle_events();
2079
2080        // Handle palette changes
2081 #ifdef HAVE_PTHREADS
2082        pthread_mutex_lock(&palette_lock);
2083 #endif
2084        if (palette_changed) {
2085                palette_changed = false;
2086                if (depth == 8) {
2087                        XStoreColors(x_display, cmap[0], palette, 256);
2088                        XStoreColors(x_display, cmap[1], palette, 256);
2089                                
2090 #ifdef ENABLE_XF86_DGA
2091                        if (display_type == DISPLAY_DGA) {
2092                                current_dga_cmap ^= 1;
2093                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2094                        }
2095 #endif
2096                }
2097        }
2098 #ifdef HAVE_PTHREADS
2099        pthread_mutex_unlock(&palette_lock);
2100 #endif
2101
2102        // In window mode, update display
2103        static int tick_counter = 0;
2104        if (display_type == DISPLAY_WINDOW) {
2105                tick_counter++;
2106                if (frame_skip == 0)
2107                        update_display_dynamic(tick_counter);
2108                else if (tick_counter >= frame_skip) {
2109                        tick_counter = 0;
2110                        update_display_static();
2111                }
2112        }
2113 }
2114 #endif
2115
2127   #ifdef HAVE_PTHREADS
2128   static void *redraw_func(void *arg)
2129   {
# Line 2120 | Line 2131 | static void *redraw_func(void *arg)
2131          int64 ticks = 0;
2132          uint64 next = GetTicks_usec();
2133          while (!redraw_thread_cancel) {
2123 //              VideoRefresh();
2134                  video_refresh();
2135                  next += 16667;
2136                  int64 delay = next - GetTicks_usec();
# Line 2131 | Line 2141 | static void *redraw_func(void *arg)
2141                  ticks++;
2142          }
2143          uint64 end = GetTicks_usec();
2144 <        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks);
2144 >        // printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start));
2145          return NULL;
2146   }
2147   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines