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.26 by cebix, 2000-10-27T17:01:40Z vs.
Revision 1.34 by gbeauche, 2001-01-11T16:38:48Z

# 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 >    char * dirtyPages;                  // Table of flags set if page was altered
201      ScreenPageInfo * pageInfo;  // Table of mappings page -> Mac scanlines
202   };
203  
204   static ScreenInfo mainBuffer;
205  
206 < #define PFLAG_SET(page)                 mainBuffer.dirtyPages[page] = 1
207 < #define PFLAG_CLEAR(page)               mainBuffer.dirtyPages[page] = 0
208 < #define PFLAG_ISSET(page)               mainBuffer.dirtyPages[page]
209 < #define PFLAG_ISCLEAR(page)             (mainBuffer.dirtyPages[page] == 0)
206 > #define PFLAG_SET_VALUE                 0x00
207 > #define PFLAG_CLEAR_VALUE               0x01
208 > #define PFLAG_SET_VALUE_4               0x00000000
209 > #define PFLAG_CLEAR_VALUE_4             0x01010101
210 > #define PFLAG_SET(page)                 mainBuffer.dirtyPages[page] = PFLAG_SET_VALUE
211 > #define PFLAG_CLEAR(page)               mainBuffer.dirtyPages[page] = PFLAG_CLEAR_VALUE
212 > #define PFLAG_ISSET(page)               (mainBuffer.dirtyPages[page] == PFLAG_SET_VALUE)
213 > #define PFLAG_ISCLEAR(page)             (mainBuffer.dirtyPages[page] != PFLAG_SET_VALUE)
214 >
215   #ifdef UNALIGNED_PROFITABLE
216 < # define PFLAG_ISCLEAR_4(page)  (*((uint32 *)(mainBuffer.dirtyPages + page)) == 0)
216 > # define PFLAG_ISSET_4(page)    (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_SET_VALUE_4)
217 > # define PFLAG_ISCLEAR_4(page)  (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_CLEAR_VALUE_4)
218 > #else
219 > # define PFLAG_ISSET_4(page) \
220 >                PFLAG_ISSET(page  ) && PFLAG_ISSET(page+1) \
221 >        &&      PFLAG_ISSET(page+2) && PFLAG_ISSET(page+3)
222 > # define PFLAG_ISCLEAR_4(page) \
223 >                PFLAG_ISCLEAR(page  ) && PFLAG_ISCLEAR(page+1) \
224 >        &&      PFLAG_ISCLEAR(page+2) && PFLAG_ISCLEAR(page+3)
225 > #endif
226 >
227 > // Set the selected page range [ first_page, last_page [ into the SET state
228 > #define PFLAG_SET_RANGE(first_page, last_page) \
229 >        memset(mainBuffer.dirtyPages + (first_page), PFLAG_SET_VALUE, \
230 >                (last_page) - (first_page))
231 >
232 > // Set the selected page range [ first_page, last_page [ into the CLEAR state
233 > #define PFLAG_CLEAR_RANGE(first_page, last_page) \
234 >        memset(mainBuffer.dirtyPages + (first_page), PFLAG_CLEAR_VALUE, \
235 >                (last_page) - (first_page))
236 >
237 > #define PFLAG_SET_ALL \
238 >        PFLAG_SET_RANGE(0, mainBuffer.pageCount)
239 >
240 > #define PFLAG_CLEAR_ALL \
241 >        PFLAG_CLEAR_RANGE(0, mainBuffer.pageCount)
242 >
243 > // Set the following macro definition to 1 if your system
244 > // provides a really fast strchr() implementation
245 > //#define HAVE_FAST_STRCHR 0
246 >
247 > static inline int find_next_page_set(int page)
248 > {
249 > #if HAVE_FAST_STRCHR
250 >        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_SET_VALUE);
251 >        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
252 > #else
253 >        while (PFLAG_ISCLEAR_4(page))
254 >                page += 4;
255 >        while (PFLAG_ISCLEAR(page))
256 >                page++;
257 >        return page;
258 > #endif
259 > }
260 >
261 > static inline int find_next_page_clear(int page)
262 > {
263 > #if HAVE_FAST_STRCHR
264 >        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_CLEAR_VALUE);
265 >        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
266   #else
267 < # define PFLAG_ISCLEAR_4(page)  \
268 <                (mainBuffer.dirtyPages[page  ] == 0) \
269 <        &&      (mainBuffer.dirtyPages[page+1] == 0) \
270 <        &&      (mainBuffer.dirtyPages[page+2] == 0) \
271 <        &&      (mainBuffer.dirtyPages[page+3] == 0)
267 >        // NOTE: the loop is bound to terminate because the last
268 >        // page in mainBuffer.dirtyPages[] shall be set to CLEAR
269 >        while (PFLAG_ISSET_4(page))
270 >                page += 4;
271 >        while (PFLAG_ISSET(page))
272 >                page++;
273 >        return page;
274   #endif
275 < #define PFLAG_CLEAR_ALL                 memset(mainBuffer.dirtyPages, 0, mainBuffer.pageCount)
211 < #define PFLAG_SET_ALL                   memset(mainBuffer.dirtyPages, 1, mainBuffer.pageCount)
275 > }
276  
277   static int zero_fd = -1;
278   static bool Screen_fault_handler_init();
279   static struct sigaction vosf_sa;
280  
281   #ifdef HAVE_PTHREADS
282 < static pthread_mutex_t Screen_draw_lock = PTHREAD_MUTEX_INITIALIZER;    // Mutex to protect frame buffer (dirtyPages in fact)
282 > static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer (dirtyPages in fact)
283 > #define LOCK_VOSF pthread_mutex_lock(&vosf_lock);
284 > #define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock);
285 > #else
286 > #define LOCK_VOSF
287 > #define UNLOCK_VOSF
288   #endif
289  
290   static int log_base_2(uint32 x)
# Line 228 | Line 297 | static int log_base_2(uint32 x)
297          }
298          return l;
299   }
300 <
232 < #endif
300 > #endif /* ENABLE_VOSF */
301  
302   // VideoRefresh function
303   void VideoRefreshInit(void);
# Line 237 | Line 305 | static void (*video_refresh)(void);
305  
306   // Prototypes
307   static void *redraw_func(void *arg);
308 < static int event2keycode(XKeyEvent *ev);
308 > static int event2keycode(XKeyEvent &ev);
309  
310  
311   // From main_unix.cpp
# Line 307 | Line 375 | void set_video_monitor(int width, int he
375          VideoMonitor.bytes_per_row = bytes_per_row;
376   }
377  
378 + // Set window name and class
379 + static void set_window_name(Window w, int name)
380 + {
381 +        const char *str = GetString(name);
382 +        XStoreName(x_display, w, str);
383 +        XSetIconName(x_display, w, str);
384 +
385 +        XClassHint *hints;
386 +        hints = XAllocClassHint();
387 +        if (hints) {
388 +                hints->res_name = "BasiliskII";
389 +                hints->res_class = "BasiliskII";
390 +                XSetClassHint(x_display, w, hints);
391 +                XFree(hints);
392 +        }
393 + }
394 +
395 + // Set window input focus flag
396 + static void set_window_focus(Window w)
397 + {
398 +        XWMHints *hints = XAllocWMHints();
399 +        if (hints) {
400 +                hints->input = True;
401 +                hints->initial_state = NormalState;
402 +                hints->flags = InputHint | StateHint;
403 +                XSetWMHints(x_display, w, hints);
404 +                XFree(hints);
405 +        }
406 + }
407 +
408 + // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
409 + static void set_window_delete_protocol(Window w)
410 + {
411 +        WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
412 +        XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
413 + }
414 +
415 + // Wait until window is mapped/unmapped
416 + void wait_mapped(Window w)
417 + {
418 +        XEvent e;
419 +        do {
420 +                XMaskEvent(x_display, StructureNotifyMask, &e);
421 +        } while ((e.type != MapNotify) || (e.xmap.event != w));
422 + }
423 +
424 + void wait_unmapped(Window w)
425 + {
426 +        XEvent e;
427 +        do {
428 +                XMaskEvent(x_display, StructureNotifyMask, &e);
429 +        } while ((e.type != UnmapNotify) || (e.xmap.event != w));
430 + }
431 +
432   // Trap SHM errors
433   static bool shm_error = false;
434   static int (*old_error_handler)(Display *, XErrorEvent *);
# Line 336 | Line 458 | static bool init_window(int width, int h
458          XSetWindowAttributes wattr;
459          wattr.event_mask = eventmask = win_eventmask;
460          wattr.background_pixel = black_pixel;
461 <        wattr.border_pixel = black_pixel;
340 <        wattr.backing_store = NotUseful;
341 <        wattr.save_under = false;
342 <        wattr.backing_planes = xdepth;
461 >        wattr.colormap = cmap[0];
462  
344        XSync(x_display, false);
463          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
464 <                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
465 <                CWBackingStore | CWBackingPlanes, &wattr);
464 >                InputOutput, vis, CWEventMask | CWBackPixel | (depth == 8 ? CWColormap : 0), &wattr);
465 >
466 >        // Set window name/class
467 >        set_window_name(the_win, STR_WINDOW_TITLE);
468  
469          // Indicate that we want keyboard input
470 <        {
471 <                XWMHints *hints = XAllocWMHints();
472 <                if (hints) {
473 <                        hints->input = True;
354 <                        hints->flags = InputHint;
355 <                        XSetWMHints(x_display, the_win, hints);
356 <                        XFree((char *)hints);
357 <                }
358 <        }
470 >        set_window_focus(the_win);
471 >
472 >        // Set delete protocol property
473 >        set_window_delete_protocol(the_win);
474  
475          // Make window unresizable
476          {
# Line 367 | Line 482 | static bool init_window(int width, int h
482                          hints->max_height = height;
483                          hints->flags = PMinSize | PMaxSize;
484                          XSetWMNormalHints(x_display, the_win, hints);
485 <                        XFree((char *)hints);
485 >                        XFree(hints);
486                  }
487          }
488          
374        // Set window title
375        {
376                XTextProperty title_prop;
377                const char *title = GetString(STR_WINDOW_TITLE);
378                XStringListToTextProperty((char **)&title, 1, &title_prop);
379                XSetWMName(x_display, the_win, &title_prop);
380                XFree(title_prop.value);
381        }
382
383        // Set window class
384        {
385                XClassHint *hints;
386                hints = XAllocClassHint();
387                if (hints) {
388                        hints->res_name = "BasiliskII";
389                        hints->res_class = "BasiliskII";
390                        XSetClassHint(x_display, the_win, hints);
391                        XFree((char *)hints);
392                }
393        }
394
489          // Show window
490 <        XSync(x_display, false);
491 <        XMapRaised(x_display, the_win);
398 <        XFlush(x_display);
399 <
400 <        // Set colormap
401 <        if (depth == 8)
402 <                XSetWindowColormap(x_display, the_win, cmap[0]);
490 >        XMapWindow(x_display, the_win);
491 >        wait_mapped(the_win);
492  
493          // Try to create and attach SHM image
494          have_shm = false;
# Line 483 | Line 572 | static bool init_window(int width, int h
572          // Set VideoMonitor
573          bool native_byte_order;
574   #ifdef WORDS_BIGENDIAN
575 <        native_byte_order = (img->bitmap_bit_order == MSBFirst);
575 >        native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
576   #else
577 <        native_byte_order = (img->bitmap_bit_order == LSBFirst);
577 >        native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
578   #endif
579   #ifdef ENABLE_VOSF
580          do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW);
# Line 572 | Line 661 | static bool init_fbdev_dga(char *in_fb_n
661          
662          // Create window
663          XSetWindowAttributes wattr;
664 <        wattr.override_redirect = True;
665 <        wattr.backing_store             = NotUseful;
666 <        wattr.background_pixel  = white_pixel;
667 <        wattr.border_pixel              = black_pixel;
579 <        wattr.event_mask                = eventmask = dga_eventmask;
664 >        wattr.event_mask = eventmask = dga_eventmask;
665 >        wattr.background_pixel = white_pixel;
666 >        wattr.override_redirect = True;
667 >        wattr.colormap = cmap[0];
668          
581        XSync(x_display, false);
669          the_win = XCreateWindow(x_display, rootwin,
670                  0, 0, width, height,
671                  0, xdepth, InputOutput, vis,
672 <                CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWBackingStore,
672 >                CWEventMask | CWBackPixel | CWOverrideRedirect | (depth == 8 ? CWColormap : 0),
673                  &wattr);
674 <        XSync(x_display, false);
674 >
675 >        // Set window name/class
676 >        set_window_name(the_win, STR_WINDOW_TITLE);
677 >
678 >        // Indicate that we want keyboard input
679 >        set_window_focus(the_win);
680 >
681 >        // Show window
682          XMapRaised(x_display, the_win);
683 <        XSync(x_display, false);
683 >        wait_mapped(the_win);
684          
685          // Grab mouse and keyboard
686          XGrabKeyboard(x_display, the_win, True,
# Line 595 | Line 689 | static bool init_fbdev_dga(char *in_fb_n
689                  PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
690                  GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
691          
598        // Set colormap
599        if (depth == 8)
600                XSetWindowColormap(x_display, the_win, cmap[0]);
601        
692          // Set VideoMonitor
693          int bytes_per_row = width;
694          switch (depth) {
# Line 678 | 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  
775          // Create window
776          XSetWindowAttributes wattr;
777          wattr.event_mask = eventmask = dga_eventmask;
687        wattr.border_pixel = black_pixel;
778          wattr.override_redirect = True;
779  
690        XSync(x_display, false);
780          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
781 <                InputOutput, vis, CWEventMask | CWBorderPixel | CWOverrideRedirect, &wattr);
782 <        XSync(x_display, false);
783 <        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
781 >                InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
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 712 | 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 849 | Line 945 | bool VideoInitBuffer()
945                  if (mainBuffer.dirtyPages != 0)
946                          free(mainBuffer.dirtyPages);
947  
948 <                mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount);
948 >                mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 1);
949  
950                  if (mainBuffer.pageInfo != 0)
951                          free(mainBuffer.pageInfo);
# Line 860 | Line 956 | bool VideoInitBuffer()
956                          return false;
957  
958                  PFLAG_CLEAR_ALL;
959 +                
960 +                // Make sure there is at least one page marked, so the
961 +                // loops in the update routine will terminate
962 +                // gb-- Set the last page as cleared because the update
963 +                // routine finally searches for a page that was not touched
964 +                PFLAG_CLEAR(mainBuffer.pageCount);
965  
966                  uint32 a = 0;
967                  for (int i = 0; i < mainBuffer.pageCount; i++) {
# Line 912 | Line 1014 | bool VideoInit(bool classic)
1014          keycode_init();
1015  
1016          // Read prefs
1017 <        mouse_wheel_mode = PrefsFindInt16("mousewheelmode");
1018 <        mouse_wheel_lines = PrefsFindInt16("mousewheellines");
1017 >        mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
1018 >        mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1019  
1020          // Find screen and root window
1021          screen = XDefaultScreen(x_display);
# Line 1050 | Line 1152 | bool VideoInit(bool classic)
1152                          break;
1153          }
1154  
1053 #ifdef HAVE_PTHREADS
1155          // Lock down frame buffer
1156 <        pthread_mutex_lock(&frame_buffer_lock);
1056 < #endif
1156 >        LOCK_FRAME_BUFFER;
1157  
1158   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1159          // Set variables for UAE memory mapping
# Line 1118 | Line 1218 | void VideoExit(void)
1218          }
1219   #endif
1220  
1121 #ifdef HAVE_PTHREADS
1221          // Unlock frame buffer
1222 <        pthread_mutex_unlock(&frame_buffer_lock);
1124 < #endif
1222 >        UNLOCK_FRAME_BUFFER;
1223  
1224          // Close window and server connection
1225          if (x_display != NULL) {
# Line 1224 | Line 1322 | void VideoInterrupt(void)
1322          if (emerg_quit)
1323                  QuitEmulator();
1324  
1227 #ifdef HAVE_PTHREADS
1325          // Temporarily give up frame buffer lock (this is the point where
1326          // we are suspended when the user presses Ctrl-Tab)
1327 <        pthread_mutex_unlock(&frame_buffer_lock);
1328 <        pthread_mutex_lock(&frame_buffer_lock);
1232 < #endif
1327 >        UNLOCK_FRAME_BUFFER;
1328 >        LOCK_FRAME_BUFFER;
1329   }
1330  
1331  
# Line 1239 | Line 1335 | void VideoInterrupt(void)
1335  
1336   void video_set_palette(uint8 *pal)
1337   {
1338 < #ifdef HAVE_PTHREDS
1243 <        pthread_mutex_lock(&palette_lock);
1244 < #endif
1338 >        LOCK_PALETTE;
1339  
1340          // Convert colors to XColor array
1341          for (int i=0; i<256; i++) {
# Line 1255 | Line 1349 | void video_set_palette(uint8 *pal)
1349          // Tell redraw thread to change palette
1350          palette_changed = true;
1351  
1352 < #ifdef HAVE_PTHREADS
1259 <        pthread_mutex_unlock(&palette_lock);
1260 < #endif
1352 >        UNLOCK_PALETTE;
1353   }
1354  
1355  
# Line 1273 | Line 1365 | static void suspend_emul(void)
1365                  ADBKeyUp(0x36);
1366                  ctrl_down = false;
1367  
1276 #ifdef HAVE_PTHREADS
1368                  // Lock frame buffer (this will stop the MacOS thread)
1369 <                pthread_mutex_lock(&frame_buffer_lock);
1279 < #endif
1369 >                LOCK_FRAME_BUFFER;
1370  
1371                  // Save frame buffer
1372                  fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
# Line 1290 | Line 1380 | static void suspend_emul(void)
1380                  XUngrabPointer(x_display, CurrentTime);
1381                  XUngrabKeyboard(x_display, CurrentTime);
1382                  XUnmapWindow(x_display, the_win);
1383 <                XSync(x_display, false);
1383 >                wait_unmapped(the_win);
1384  
1385                  // Open "suspend" window
1386                  XSetWindowAttributes wattr;
1387                  wattr.event_mask = KeyPressMask;
1388                  wattr.background_pixel = black_pixel;
1299                wattr.border_pixel = black_pixel;
1300                wattr.backing_store = Always;
1301                wattr.backing_planes = xdepth;
1302                wattr.colormap = DefaultColormap(x_display, screen);
1389                  
1304                XSync(x_display, false);
1390                  suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1391 <                        InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1392 <                        CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1393 <                XSync(x_display, false);
1394 <                XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1310 <                XMapRaised(x_display, suspend_win);
1311 <                XSync(x_display, false);
1391 >                        InputOutput, vis, CWEventMask | CWBackPixel, &wattr);
1392 >                set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
1393 >                set_window_focus(suspend_win);
1394 >                XMapWindow(x_display, suspend_win);
1395                  emul_suspended = true;
1396          }
1397   }
# Line 1321 | Line 1404 | static void resume_emul(void)
1404  
1405          // Reopen full screen display
1406          XMapRaised(x_display, the_win);
1407 +        wait_mapped(the_win);
1408          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1325        XSync(x_display, false);
1409          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1410          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1411   #ifdef ENABLE_XF86_DGA
# Line 1336 | Line 1419 | static void resume_emul(void)
1419          // not necessary.
1420   #ifdef ENABLE_VOSF
1421          if (use_vosf) {
1422 < #ifdef HAVE_PTHREADS
1340 <                pthread_mutex_lock(&Screen_draw_lock);
1341 < #endif
1422 >                LOCK_VOSF;
1423                  PFLAG_SET_ALL;
1424 < #ifdef HAVE_PTHREADS
1344 <                pthread_mutex_unlock(&Screen_draw_lock);
1345 < #endif
1424 >                UNLOCK_VOSF;
1425                  memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1426          }
1427   #endif
# Line 1363 | Line 1442 | static void resume_emul(void)
1442                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1443   #endif
1444  
1366 #ifdef HAVE_PTHREADS
1445          // Unlock frame buffer (and continue MacOS thread)
1446 <        pthread_mutex_unlock(&frame_buffer_lock);
1446 >        UNLOCK_FRAME_BUFFER;
1447          emul_suspended = false;
1370 #endif
1448   }
1449   #endif
1450  
# Line 1521 | Line 1598 | static int kc_decode(KeySym ks)
1598          return -1;
1599   }
1600  
1601 < static int event2keycode(XKeyEvent *ev)
1601 > static int event2keycode(XKeyEvent &ev)
1602   {
1603          KeySym ks;
1604          int as;
1605          int i = 0;
1606  
1607          do {
1608 <                ks = XLookupKeysym(ev, i++);
1608 >                ks = XLookupKeysym(&ev, i++);
1609                  as = kc_decode(ks);
1610                  if (as != -1)
1611                          return as;
# Line 1544 | Line 1621 | static int event2keycode(XKeyEvent *ev)
1621  
1622   static void handle_events(void)
1623   {
1624 <        XEvent event;
1625 <        for (;;) {
1626 <                if (!XCheckMaskEvent(x_display, eventmask, &event))
1550 <                        break;
1624 >        while (XPending(x_display)) {
1625 >                XEvent event;
1626 >                XNextEvent(x_display, &event);
1627  
1628                  switch (event.type) {
1629                          // Mouse button
1630                          case ButtonPress: {
1631 <                                unsigned int button = ((XButtonEvent *)&event)->button;
1631 >                                unsigned int button = event.xbutton.button;
1632                                  if (button < 4)
1633                                          ADBMouseDown(button - 1);
1634                                  else if (button < 6) {  // Wheel mouse
# Line 1571 | Line 1647 | static void handle_events(void)
1647                                  break;
1648                          }
1649                          case ButtonRelease: {
1650 <                                unsigned int button = ((XButtonEvent *)&event)->button;
1650 >                                unsigned int button = event.xbutton.button;
1651                                  if (button < 4)
1652                                          ADBMouseUp(button - 1);
1653                                  break;
# Line 1579 | Line 1655 | static void handle_events(void)
1655  
1656                          // Mouse moved
1657                          case EnterNotify:
1582                                ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1583                                break;
1658                          case MotionNotify:
1659 <                                ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1659 >                                ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1660                                  break;
1661  
1662                          // Keyboard
1663                          case KeyPress: {
1664                                  int code;
1665                                  if (use_keycodes) {
1666 <                                        event2keycode((XKeyEvent *)&event);     // This is called to process the hotkeys
1667 <                                        code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1666 >                                        event2keycode(event.xkey);      // This is called to process the hotkeys
1667 >                                        code = keycode_table[event.xkey.keycode & 0xff];
1668                                  } else
1669 <                                        code = event2keycode((XKeyEvent *)&event);
1669 >                                        code = event2keycode(event.xkey);
1670                                  if (code != -1) {
1671                                          if (!emul_suspended) {
1672                                                  if (code == 0x39) {     // Caps Lock pressed
# Line 1619 | Line 1693 | static void handle_events(void)
1693                          case KeyRelease: {
1694                                  int code;
1695                                  if (use_keycodes) {
1696 <                                        event2keycode((XKeyEvent *)&event);     // This is called to process the hotkeys
1697 <                                        code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1696 >                                        event2keycode(event.xkey);      // This is called to process the hotkeys
1697 >                                        code = keycode_table[event.xkey.keycode & 0xff];
1698                                  } else
1699 <                                        code = event2keycode((XKeyEvent *)&event);
1699 >                                        code = event2keycode(event.xkey);
1700                                  if (code != -1 && code != 0x39) {       // Don't propagate Caps Lock releases
1701                                          ADBKeyUp(code);
1702                                          if (code == 0x36)
# Line 1636 | Line 1710 | static void handle_events(void)
1710                                  if (display_type == DISPLAY_WINDOW) {
1711   #ifdef ENABLE_VOSF
1712                                          if (use_vosf) {                 // VOSF refresh
1713 < #ifdef HAVE_PTHREADS
1640 <                                                pthread_mutex_lock(&Screen_draw_lock);
1641 < #endif
1713 >                                                LOCK_VOSF;
1714                                                  PFLAG_SET_ALL;
1715 < #ifdef HAVE_PTHREADS
1644 <                                                pthread_mutex_unlock(&Screen_draw_lock);
1645 < #endif
1715 >                                                UNLOCK_VOSF;
1716                                                  memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1717                                          }
1718                                          else
# Line 1658 | Line 1728 | static void handle_events(void)
1728                                  }
1729                                  break;
1730  
1731 <                        case FocusIn:
1732 <                        case FocusOut:
1731 >                        // Window "close" widget clicked
1732 >                        case ClientMessage:
1733 >                                if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
1734 >                                        ADBKeyDown(0x7f);       // Power key
1735 >                                        ADBKeyUp(0x7f);
1736 >                                }
1737                                  break;
1738                  }
1739          }
# Line 1879 | Line 1953 | static void update_display_static(void)
1953   *      Screen refresh functions
1954   */
1955  
1956 < // The specialisations hereunder are meant to enable VOSF with DGA in direct
1957 < // addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could
1958 < // not get mapped correctly with respect to the predetermined host frame
1959 < // buffer base address.
1960 < //
1887 < // Hmm, in other words, when in direct addressing mode and DGA is requested,
1888 < // we first try to "triple allocate" the address spaces according to the real
1889 < // host frame buffer address. Then, if it fails, we will use a temporary
1890 < // frame buffer thus making the real host frame buffer updated when pages
1891 < // of the temp frame buffer are altered.
1892 < //
1893 < // As a side effect, a little speed gain in screen updates could be noticed
1894 < // for other modes than DGA.
1895 < //
1896 < // The following two functions below are inline so that a clever compiler
1897 < // could specialise the code according to the current screen depth and
1898 < // display type. A more clever compiler would the job by itself though...
1899 < // (update_display_vosf is inlined as well)
1956 > // We suggest the compiler to inline the next two functions so that it
1957 > // may specialise the code according to the current screen depth and
1958 > // display type. A clever compiler would that job by itself though...
1959 >
1960 > // NOTE: update_display_vosf is inlined too
1961  
1962   static inline void possibly_quit_dga_mode()
1963   {
# Line 1917 | Line 1978 | static inline void possibly_quit_dga_mod
1978  
1979   static inline void handle_palette_changes(int depth, int display_type)
1980   {
1981 < #ifdef HAVE_PTHREADS
1982 <        pthread_mutex_lock(&palette_lock);
1922 < #endif
1981 >        LOCK_PALETTE;
1982 >
1983          if (palette_changed) {
1984                  palette_changed = false;
1985                  if (depth == 8) {
1986                          XStoreColors(x_display, cmap[0], palette, 256);
1987                          XStoreColors(x_display, cmap[1], palette, 256);
1988 +                        XSync(x_display, false);
1989                                  
1990   #ifdef ENABLE_XF86_DGA
1991                          if (display_type == DISPLAY_DGA) {
# Line 1934 | Line 1995 | static inline void handle_palette_change
1995   #endif
1996                  }
1997          }
1998 < #ifdef HAVE_PTHREADS
1999 <        pthread_mutex_unlock(&palette_lock);
1939 < #endif
1998 >
1999 >        UNLOCK_PALETTE;
2000   }
2001  
2002   static void video_refresh_dga(void)
# Line 1968 | Line 2028 | static void video_refresh_dga_vosf(void)
2028          static int tick_counter = 0;
2029          if (++tick_counter >= frame_skip) {
2030                  tick_counter = 0;
2031 < #ifdef HAVE_PTHREADS
1972 <                pthread_mutex_lock(&Screen_draw_lock);
1973 < #endif
2031 >                LOCK_VOSF;
2032                  update_display_dga_vosf();
2033 < #ifdef HAVE_PTHREADS
1976 <                pthread_mutex_unlock(&Screen_draw_lock);
1977 < #endif
2033 >                UNLOCK_VOSF;
2034          }
2035   }
2036   #endif
# Line 1994 | Line 2050 | static void video_refresh_window_vosf(vo
2050          static int tick_counter = 0;
2051          if (++tick_counter >= frame_skip) {
2052                  tick_counter = 0;
2053 < #ifdef HAVE_PTHREADS
1998 <                pthread_mutex_lock(&Screen_draw_lock);
1999 < #endif
2053 >                LOCK_VOSF;
2054                  update_display_window_vosf();
2055 < #ifdef HAVE_PTHREADS
2002 <                pthread_mutex_unlock(&Screen_draw_lock);
2003 < #endif
2055 >                UNLOCK_VOSF;
2056          }
2057   }
2058   #endif // def ENABLE_VOSF
# Line 2070 | Line 2122 | void VideoRefresh(void)
2122          video_refresh();
2123   }
2124  
2073 #if 0
2074 void VideoRefresh(void)
2075 {
2076 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2077        // Quit DGA mode if requested
2078        if (quit_full_screen) {
2079                quit_full_screen = false;
2080                if (display_type == DISPLAY_DGA) {
2081 #ifdef ENABLE_XF86_DGA
2082                        XF86DGADirectVideo(x_display, screen, 0);
2083 #endif
2084                        XUngrabPointer(x_display, CurrentTime);
2085                        XUngrabKeyboard(x_display, CurrentTime);
2086                        XUnmapWindow(x_display, the_win);
2087                        XSync(x_display, false);
2088                }
2089        }
2090 #endif
2091
2092        // Handle X events
2093        handle_events();
2094
2095        // Handle palette changes
2096 #ifdef HAVE_PTHREADS
2097        pthread_mutex_lock(&palette_lock);
2098 #endif
2099        if (palette_changed) {
2100                palette_changed = false;
2101                if (depth == 8) {
2102                        XStoreColors(x_display, cmap[0], palette, 256);
2103                        XStoreColors(x_display, cmap[1], palette, 256);
2104                                
2105 #ifdef ENABLE_XF86_DGA
2106                        if (display_type == DISPLAY_DGA) {
2107                                current_dga_cmap ^= 1;
2108                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2109                        }
2110 #endif
2111                }
2112        }
2113 #ifdef HAVE_PTHREADS
2114        pthread_mutex_unlock(&palette_lock);
2115 #endif
2116
2117        // In window mode, update display
2118        static int tick_counter = 0;
2119        if (display_type == DISPLAY_WINDOW) {
2120                tick_counter++;
2121                if (frame_skip == 0)
2122                        update_display_dynamic(tick_counter);
2123                else if (tick_counter >= frame_skip) {
2124                        tick_counter = 0;
2125                        update_display_static();
2126                }
2127        }
2128 }
2129 #endif
2130
2125   #ifdef HAVE_PTHREADS
2126   static void *redraw_func(void *arg)
2127   {
# Line 2135 | Line 2129 | static void *redraw_func(void *arg)
2129          int64 ticks = 0;
2130          uint64 next = GetTicks_usec();
2131          while (!redraw_thread_cancel) {
2138 //              VideoRefresh();
2132                  video_refresh();
2133                  next += 16667;
2134                  int64 delay = next - GetTicks_usec();
# Line 2146 | Line 2139 | static void *redraw_func(void *arg)
2139                  ticks++;
2140          }
2141          uint64 end = GetTicks_usec();
2142 <        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks);
2142 >        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start));
2143          return NULL;
2144   }
2145   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines