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

Comparing SheepShaver/src/Unix/video_x.cpp (file contents):
Revision 1.32 by gbeauche, 2004-07-02T06:06:34Z vs.
Revision 1.41 by gbeauche, 2005-04-02T09:54:16Z

# Line 1 | Line 1
1   /*
2   *  video_x.cpp - Video/graphics emulation, X11 specific stuff
3   *
4 < *  SheepShaver (C) 1997-2004 Marc Hellwig and Christian Bauer
4 > *  SheepShaver (C) 1997-2005 Marc Hellwig and 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 18 | Line 18
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  
21 + /*
22 + *  NOTES:
23 + *    The Ctrl key works like a qualifier for special actions:
24 + *      Ctrl-Tab = suspend DGA mode
25 + *      Ctrl-Esc = emergency quit
26 + *      Ctrl-F1 = mount floppy
27 + *      Ctrl-F5 = grab mouse (in windowed mode)
28 + */
29 +
30   #include "sysdeps.h"
31  
32   #include <X11/Xlib.h>
# Line 31 | Line 40
40  
41   #include <algorithm>
42  
43 + #ifdef ENABLE_FBDEV_DGA
44 + # include <linux/fb.h>
45 + # include <sys/ioctl.h>
46 + #endif
47 +
48   #ifdef ENABLE_XF86_DGA
49   # include <X11/extensions/xf86dga.h>
50   #endif
# Line 109 | Line 123 | static int color_class;
123   static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
124   static Colormap cmap[2];                                        // Two colormaps (DGA) for 8-bit mode
125   static XColor x_palette[256];                           // Color palette to be used as CLUT and gamma table
126 + static int orig_accel_numer, orig_accel_denom, orig_threshold;  // Original mouse acceleration
127  
128   static XColor black, white;
129   static unsigned long black_pixel, white_pixel;
# Line 131 | Line 146 | static uint8 *the_buffer_copy = NULL;          /
146   static uint32 the_buffer_size;                          // Size of allocated the_buffer
147  
148   // Variables for DGA mode
149 + static bool is_fbdev_dga_mode = false;          // Flag: Use FBDev DGA mode?
150   static int current_dga_cmap;
151  
152 + #ifdef ENABLE_FBDEV_DGA
153 + static int fb_dev_fd = -1;                                              // Handle to fb device name
154 + static struct fb_fix_screeninfo fb_finfo;               // Fixed info
155 + static struct fb_var_screeninfo fb_vinfo;               // Variable info
156 + static struct fb_var_screeninfo fb_orig_vinfo;  // Variable info to restore later
157 + static struct fb_cmap fb_oldcmap;                               // Colormap to restore later
158 + #endif
159 +
160   #ifdef ENABLE_XF86_VIDMODE
161   // Variables for XF86 VidMode support
162   static XF86VidModeModeInfo **x_video_modes;             // Array of all available modes
# Line 153 | Line 177 | static pthread_mutex_t x_palette_lock =
177   #define UNLOCK_PALETTE
178   #endif
179  
180 + // Mutex to protect frame buffer
181 + #ifdef HAVE_SPINLOCKS
182 + static spinlock_t frame_buffer_lock = SPIN_LOCK_UNLOCKED;
183 + #define LOCK_FRAME_BUFFER spin_lock(&frame_buffer_lock)
184 + #define UNLOCK_FRAME_BUFFER spin_unlock(&frame_buffer_lock)
185 + #elif defined(HAVE_PTHREADS)
186 + static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
187 + #define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock);
188 + #define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock);
189 + #else
190 + #define LOCK_FRAME_BUFFER
191 + #define UNLOCK_FRAME_BUFFER
192 + #endif
193 +
194  
195   // Prototypes
196   static void *redraw_func(void *arg);
# Line 364 | Line 402 | static bool find_visual_for_depth(int de
402   *  Open display (window or fullscreen)
403   */
404  
405 + // Set window name and class
406 + static void set_window_name(Window w, int name)
407 + {
408 +        const char *str = GetString(name);
409 +        XStoreName(x_display, w, str);
410 +        XSetIconName(x_display, w, str);
411 +
412 +        XClassHint *hints;
413 +        hints = XAllocClassHint();
414 +        if (hints) {
415 +                hints->res_name = "SheepShaver";
416 +                hints->res_class = "SheepShaver";
417 +                XSetClassHint(x_display, w, hints);
418 +                XFree(hints);
419 +        }
420 + }
421 +
422 + // Set window input focus flag
423 + static void set_window_focus(Window w)
424 + {
425 +        XWMHints *hints = XAllocWMHints();
426 +        if (hints) {
427 +                hints->input = True;
428 +                hints->initial_state = NormalState;
429 +                hints->flags = InputHint | StateHint;
430 +                XSetWMHints(x_display, w, hints);
431 +                XFree(hints);
432 +        }
433 + }
434 +
435   // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
436   static Atom WM_DELETE_WINDOW = (Atom)0;
437   static void set_window_delete_protocol(Window w)
# Line 389 | Line 457 | static void wait_unmapped(Window w)
457          } while ((e.type != UnmapNotify) || (e.xmap.event != w));
458   }
459  
460 + // Disable mouse acceleration
461 + static void disable_mouse_accel(void)
462 + {
463 +        XChangePointerControl(x_display, True, False, 1, 1, 0);
464 + }
465 +
466 + // Restore mouse acceleration to original value
467 + static void restore_mouse_accel(void)
468 + {
469 +        XChangePointerControl(x_display, True, True, orig_accel_numer, orig_accel_denom, orig_threshold);
470 + }
471 +
472   // Trap SHM errors
473   static bool shm_error = false;
474   static int (*old_error_handler)(Display *, XErrorEvent *);
# Line 421 | Line 501 | static bool open_window(int width, int h
501          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
502                  InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr);
503  
504 <        // Set window name
505 <        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
504 >        // Set window name/class
505 >        set_window_name(the_win, STR_WINDOW_TITLE);
506 >
507 >        // Indicate that we want keyboard input
508 >        set_window_focus(the_win);
509  
510          // Set delete protocol property
511          set_window_delete_protocol(the_win);
# Line 494 | Line 577 | static bool open_window(int width, int h
577          use_vosf = true;
578          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
579          the_host_buffer = the_buffer_copy;
580 <        the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
580 >        the_buffer_size = page_extend((aligned_height + 2) * (the_host_buffer_row_bytes = img->bytes_per_line));
581          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
582          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
583          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
# Line 503 | Line 586 | static bool open_window(int width, int h
586          the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
587          D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
588   #endif
589 <        screen_base = (uint32)the_buffer;
589 >        screen_base = Host2MacAddr(the_buffer);
590  
591          // Create GC
592          the_gc = XCreateGC(x_display, the_win, 0, 0);
# Line 550 | Line 633 | static bool open_window(int width, int h
633          return true;
634   }
635  
636 + // Open FBDev display
637 + static bool open_fbdev(int width, int height)
638 + {
639 + #ifdef ENABLE_FBDEV_DGA
640 +        if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo) != 0) {
641 +                D(bug("[fbdev] Can't get FSCREENINFO: %s\n", strerror(errno)));
642 +                return false;
643 +        }
644 +        D(bug("[fbdev] Device ID: %s\n", fb_finfo.id));
645 +        D(bug("[fbdev] smem_start: %p [%d bytes]\n", fb_finfo.smem_start, fb_finfo.smem_len));
646 +
647 +        int fb_type = fb_finfo.type;
648 +        const char *fb_type_str = NULL;
649 +        switch (fb_type) {
650 +        case FB_TYPE_PACKED_PIXELS:                     fb_type_str = "Packed Pixels";                  break;
651 +        case FB_TYPE_PLANES:                            fb_type_str = "Non interleaved planes"; break;
652 +        case FB_TYPE_INTERLEAVED_PLANES:        fb_type_str = "Interleaved planes";             break;
653 +        case FB_TYPE_TEXT:                                      fb_type_str = "Text/attributes";                break;
654 +        case FB_TYPE_VGA_PLANES:                        fb_type_str = "EGA/VGA planes";                 break;
655 +        default:                                                        fb_type_str = "<unknown>";                              break;
656 +        }
657 +        D(bug("[fbdev] type: %s\n", fb_type_str));
658 +
659 +        if (fb_type != FB_TYPE_PACKED_PIXELS) {
660 +                D(bug("[fbdev] type '%s' not supported\n", fb_type_str));
661 +                return false;
662 +        }
663 +
664 +        int fb_visual = fb_finfo.visual;
665 +        const char *fb_visual_str;
666 +        switch (fb_visual) {
667 +        case FB_VISUAL_MONO01:                          fb_visual_str = "Monochrome 1=Black 0=White";   break;
668 +        case FB_VISUAL_MONO10:                          fb_visual_str = "Monochrome 1=While 0=Black";   break;
669 +        case FB_VISUAL_TRUECOLOR:                       fb_visual_str = "True color";                                   break;
670 +        case FB_VISUAL_PSEUDOCOLOR:                     fb_visual_str = "Pseudo color (like atari)";    break;
671 +        case FB_VISUAL_DIRECTCOLOR:                     fb_visual_str = "Direct color";                                 break;
672 +        case FB_VISUAL_STATIC_PSEUDOCOLOR:      fb_visual_str = "Pseudo color readonly";                break;
673 +        default:                                                        fb_visual_str = "<unknown>";                                    break;
674 +        }
675 +        D(bug("[fbdev] visual: %s\n", fb_visual_str));
676 +
677 +        if (fb_visual != FB_VISUAL_TRUECOLOR) {
678 +                D(bug("[fbdev] visual '%s' not supported\n", fb_visual_str));
679 +                return false;
680 +        }
681 +        
682 +        // Map frame buffer
683 +        the_buffer = (uint8 *)mmap(NULL, fb_finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev_fd, 0);
684 +        if (the_buffer == MAP_FAILED) {
685 +                D(bug("[fbdev] Can't mmap /dev/fb0: %s\n", strerror(errno)));
686 +                return false;
687 +        }
688 +
689 +        // Set absolute mouse mode
690 +        ADBSetRelMouseMode(false);
691 +
692 +        // Create window
693 +        XSetWindowAttributes wattr;
694 +        wattr.event_mask = eventmask = dga_eventmask;
695 +        wattr.override_redirect = True;
696 +        the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
697 +                                                        InputOutput, DefaultVisual(x_display, screen),
698 +                                                        CWEventMask | CWOverrideRedirect, &wattr);
699 +
700 +        // Show window
701 +        XMapRaised(x_display, the_win);
702 +        wait_mapped(the_win);
703 +
704 +        // Grab mouse and keyboard
705 +        XGrabKeyboard(x_display, the_win, True,
706 +                                  GrabModeAsync, GrabModeAsync, CurrentTime);
707 +        XGrabPointer(x_display, the_win, True,
708 +                                 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
709 +                                 GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
710 +        disable_mouse_accel();
711 +
712 +        // Create no_cursor
713 +        mac_cursor = XCreatePixmapCursor(x_display,
714 +                                                                         XCreatePixmap(x_display, the_win, 1, 1, 1),
715 +                                                                         XCreatePixmap(x_display, the_win, 1, 1, 1),
716 +                                                                         &black, &white, 0, 0);
717 +        XDefineCursor(x_display, the_win, mac_cursor);
718 +
719 +        // Init blitting routines
720 + #if ENABLE_VOSF
721 +        // Extract current screen color masks (we are in True Color mode)
722 +        VisualFormat visualFormat;
723 +        visualFormat.depth = xdepth = DefaultDepth(x_display, screen);
724 +        XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo);
725 +        assert(visualFormat.depth == visualInfo.depth);
726 +        visualFormat.Rmask = visualInfo.red_mask;
727 +        visualFormat.Gmask = visualInfo.green_mask;
728 +        visualFormat.Bmask = visualInfo.blue_mask;
729 +        D(bug("[fbdev] %d bpp, (%08x,%08x,%08x)\n",
730 +                  visualFormat.depth,
731 +                  visualFormat.Rmask, visualFormat.Gmask, visualFormat.Bmask));
732 +        D(bug("[fbdev] Mac depth %d bpp\n", depth));
733 +
734 +        // Screen_blitter_init() returns TRUE if VOSF is mandatory
735 +        // i.e. the framebuffer update function is not Blit_Copy_Raw
736 + #ifdef WORDS_BIGENDIAN
737 +        const bool native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
738 + #else
739 +        const bool native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
740 + #endif
741 +        Screen_blitter_init(visualFormat, native_byte_order, depth);
742 +        
743 +        // Allocate memory for frame buffer (SIZE is extended to page-boundary)
744 +        use_vosf = true;
745 +        the_host_buffer = the_buffer;
746 +        the_host_buffer_row_bytes = TrivialBytesPerRow((width + 7) & ~7, DepthModeForPixelDepth(visualFormat.depth));
747 +        the_buffer_size = page_extend((height + 2) * the_host_buffer_row_bytes);
748 +        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
749 +        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
750 +        D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
751 + #endif
752 +
753 +        // Set frame buffer base
754 +        D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
755 +        screen_base = Host2MacAddr(the_buffer);
756 +        VModes[cur_mode].viRowBytes = TrivialBytesPerRow((width + 7) & ~7, DepthModeForPixelDepth(depth));
757 +        return true;
758 + #else
759 +        ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
760 +        return false;
761 + #endif
762 + }
763 +
764   // Open DGA display (!! should use X11 VidMode extensions to set mode)
765   static bool open_dga(int width, int height)
766   {
767 +        if (is_fbdev_dga_mode)
768 +                return false;
769 +
770   #ifdef ENABLE_XF86_DGA
771          // Set relative mouse mode
772          ADBSetRelMouseMode(true);
# Line 621 | Line 835 | static bool open_dga(int width, int heig
835          if (use_vosf) {
836            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
837            the_host_buffer = the_buffer;
838 <          the_buffer_size = page_extend((height + 2) * bytes_per_row);
838 >          the_buffer_size = page_extend((height + 2) * (the_host_buffer_row_bytes = bytes_per_row));
839            the_buffer_copy = (uint8 *)malloc(the_buffer_size);
840            the_buffer = (uint8 *)vm_acquire(the_buffer_size);
841            D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
# Line 633 | Line 847 | static bool open_dga(int width, int heig
847  
848          // Set frame buffer base
849          D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
850 <        screen_base = (uint32)the_buffer;
850 >        screen_base = Host2MacAddr(the_buffer);
851          VModes[cur_mode].viRowBytes = bytes_per_row;
852          return true;
853   #else
# Line 647 | Line 861 | static bool open_display(void)
861          D(bug("open_display()\n"));
862          const VideoInfo &mode = VModes[cur_mode];
863  
864 +        // Get original mouse acceleration
865 +        XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold);
866 +
867          // Find best available X visual
868          if (!find_visual_for_depth(mode.viAppleMode)) {
869                  ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
# Line 654 | Line 871 | static bool open_display(void)
871          }
872  
873          // Build up visualFormat structure
874 +        visualFormat.fullscreen = (display_type == DIS_SCREEN);
875          visualFormat.depth = visualInfo.depth;
876          visualFormat.Rmask = visualInfo.red_mask;
877          visualFormat.Gmask = visualInfo.green_mask;
# Line 727 | Line 945 | static bool open_display(void)
945          depth = depth_of_video_mode(mode.viAppleMode);
946  
947          bool display_open = false;
948 <        if (display_type == DIS_SCREEN)
948 >        if (display_type == DIS_SCREEN) {
949                  display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
950 + #ifdef ENABLE_FBDEV_DGA
951 +                // Try to fallback to FBDev DGA mode
952 +                if (!display_open) {
953 +                        is_fbdev_dga_mode = true;
954 +                        display_open = open_fbdev(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
955 +                }
956 + #endif
957 +                
958 +        }
959          else if (display_type == DIS_WINDOW)
960                  display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
961  
962   #ifdef ENABLE_VOSF
963          if (use_vosf) {
964                  // Initialize the VOSF system
965 +                LOCK_VOSF;
966                  if (!video_vosf_init()) {
967                          ErrorAlert(GetString(STR_VOSF_INIT_ERR));
968 +                        UNLOCK_VOSF;
969                          return false;
970                  }
971 +                UNLOCK_VOSF;
972          }
973   #endif
974 <        
974 >
975 >        // Zero screen buffers, viRowBytes is initialized at this stage
976 >        memset(the_buffer, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
977 >        memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
978          return display_open;
979   }
980  
# Line 777 | Line 1010 | static void close_window(void)
1010          XSync(x_display, false);
1011   }
1012  
1013 + // Close FBDev mode
1014 + static void close_fbdev(void)
1015 + {
1016 + #ifdef ENABLE_FBDEV_DGA
1017 +        XUngrabPointer(x_display, CurrentTime);
1018 +        XUngrabKeyboard(x_display, CurrentTime);
1019 +
1020 +        uint8 *fb_base;
1021 +        if (!use_vosf) {
1022 +                // don't free() the screen buffer in driver_base dtor
1023 +                fb_base = the_buffer;
1024 +                the_buffer = NULL;
1025 +        }
1026 + #ifdef ENABLE_VOSF
1027 +        else {
1028 +                // don't free() the screen buffer in driver_base dtor
1029 +                fb_base = the_host_buffer;
1030 +                the_host_buffer = NULL;
1031 +        }
1032 + #endif
1033 +        munmap(fb_base, fb_finfo.smem_len);
1034 + #endif
1035 + }
1036 +
1037   // Close DGA mode
1038   static void close_dga(void)
1039   {
1040 +        if (is_fbdev_dga_mode)
1041 +                return;
1042 +
1043   #ifdef ENABLE_XF86_DGA
1044          XF86DGADirectVideo(x_display, screen, 0);
1045          XUngrabPointer(x_display, CurrentTime);
# Line 805 | Line 1065 | static void close_dga(void)
1065  
1066   static void close_display(void)
1067   {
1068 <        if (display_type == DIS_SCREEN)
1069 <                close_dga();
1068 >        if (display_type == DIS_SCREEN) {
1069 >                if (is_fbdev_dga_mode)
1070 >                        close_fbdev();
1071 >                else
1072 >                        close_dga();
1073 >        }
1074          else if (display_type == DIS_WINDOW)
1075                  close_window();
1076  
# Line 861 | Line 1125 | static void close_display(void)
1125                  }
1126          }
1127   #endif
1128 +
1129 +        // Restore mouse acceleration
1130 +        restore_mouse_accel();
1131   }
1132  
1133  
# Line 892 | Line 1159 | static void keycode_init(void)
1159  
1160                  // Search for server vendor string, then read keycodes
1161                  const char *vendor = ServerVendor(x_display);
895 #if (defined(__APPLE__) && defined(__MACH__))
1162                  // Force use of MacX mappings on MacOS X with Apple's X server
1163                  int dummy;
1164                  if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy))
1165                          vendor = "MacX";
900 #endif
1166                  bool vendor_found = false;
1167                  char line[256];
1168                  while (fgets(line, 255, f)) {
# Line 972 | Line 1237 | static int find_mode(int apple_mode, int
1237          return -1;
1238   }
1239  
1240 + // Add custom video mode
1241 + static void add_custom_mode(VideoInfo *&p, int type, uint32 x, uint32 y, int apple_mode, int apple_id)
1242 + {
1243 +        p->viType = type;
1244 +        p->viXsize = x;
1245 +        p->viYsize = y;
1246 +        p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
1247 +        p->viAppleMode = apple_mode;
1248 +        p->viAppleID = apple_id;
1249 +        p++;
1250 + }
1251 +
1252   // Add mode to list of supported modes
1253   static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
1254   {
1255          if (allow & test) {
1256 <                p->viType = type;
1256 >                uint32 x = 0, y = 0;
1257                  switch (apple_id) {
1258                          case APPLE_W_640x480:
1259                          case APPLE_640x480:
1260 <                                p->viXsize = 640;
1261 <                                p->viYsize = 480;
1260 >                                x = 640;
1261 >                                y = 480;
1262                                  break;
1263                          case APPLE_W_800x600:
1264                          case APPLE_800x600:
1265 <                                p->viXsize = 800;
1266 <                                p->viYsize = 600;
1265 >                                x = 800;
1266 >                                y = 600;
1267                                  break;
1268                          case APPLE_1024x768:
1269 <                                p->viXsize = 1024;
1270 <                                p->viYsize = 768;
1269 >                                x = 1024;
1270 >                                y = 768;
1271                                  break;
1272                          case APPLE_1152x768:
1273 <                                p->viXsize = 1152;
1274 <                                p->viYsize = 768;
1273 >                                x = 1152;
1274 >                                y = 768;
1275                                  break;
1276                          case APPLE_1152x900:
1277 <                                p->viXsize = 1152;
1278 <                                p->viYsize = 900;
1277 >                                x = 1152;
1278 >                                y = 900;
1279                                  break;
1280                          case APPLE_1280x1024:
1281 <                                p->viXsize = 1280;
1282 <                                p->viYsize = 1024;
1281 >                                x = 1280;
1282 >                                y = 1024;
1283                                  break;
1284                          case APPLE_1600x1200:
1285 <                                p->viXsize = 1600;
1286 <                                p->viYsize = 1200;
1285 >                                x = 1600;
1286 >                                y = 1200;
1287                                  break;
1288                  }
1289 <                p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
1013 <                p->viAppleMode = apple_mode;
1014 <                p->viAppleID = apple_id;
1015 <                p++;
1289 >                add_custom_mode(p, type, x, y, apple_mode, apple_id);
1290          }
1291   }
1292  
# Line 1081 | Line 1355 | bool VideoInit(void)
1355   #ifdef ENABLE_XF86_DGA
1356          // DGA available?
1357      int event_base, error_base;
1358 +    is_fbdev_dga_mode = false;
1359      if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
1360                  int dga_flags = 0;
1361                  XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1362                  has_dga = dga_flags & XF86DGADirectPresent;
1363 + #if defined(__linux__)
1364 +                // Check r/w permission on /dev/mem for DGA mode to work
1365 +                if (has_dga && access("/dev/mem", R_OK | W_OK) != 0)
1366 +                        has_dga = false;
1367 + #endif
1368          } else
1369                  has_dga = false;
1370   #endif
# Line 1097 | Line 1377 | bool VideoInit(void)
1377                  XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1378   #endif
1379  
1380 + #ifdef ENABLE_FBDEV_DGA
1381 +        // FBDev available?
1382 +        bool has_fbdev_dga = false;
1383 +        if (local_X11) {
1384 +                if ((fb_dev_fd = open("/dev/fb0", O_RDWR)) > 0) {
1385 +                        if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo) != 0)
1386 +                                close(fb_dev_fd);
1387 +                        else {
1388 +                                has_fbdev_dga = true;
1389 +                                if (!has_dga) {
1390 +                                        // Fallback to FBDev DGA mode if XF86 DGA is not possible
1391 +                                        has_dga = true;
1392 +                                        is_fbdev_dga_mode = true;
1393 +                                }
1394 +                                fb_orig_vinfo = fb_vinfo;
1395 +                                D(bug("Frame buffer device initial resolution: %dx%dx%d\n", fb_vinfo.xres, fb_vinfo.yres, fb_vinfo.bits_per_pixel));
1396 +                        }
1397 +                }
1398 +        }
1399 + #endif
1400 +
1401          // Find black and white colors
1402          XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
1403          XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
# Line 1122 | Line 1423 | bool VideoInit(void)
1423                  break;
1424          }
1425  
1426 +        // Get screen mode from preferences
1427 +        const char *mode_str = PrefsFindString("screen");
1428 +        int default_width = 640, default_height = 480;
1429 +        if (mode_str) {
1430 +                display_type = DIS_INVALID;
1431 +                if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
1432 +                        display_type = DIS_WINDOW;
1433 + #ifdef ENABLE_XF86_DGA
1434 +                else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1435 +                        display_type = DIS_SCREEN;
1436 + #endif
1437 + #ifdef ENABLE_FBDEV_DGA
1438 +                else if (has_fbdev_dga && sscanf(mode_str, "fbdev/%d/%d", &default_width, &default_height) == 2) {
1439 +                        is_fbdev_dga_mode = true;
1440 +                        display_type = DIS_SCREEN;
1441 +                }
1442 + #endif
1443 +                if (display_type == DIS_INVALID) {
1444 +                        D(bug("Invalid screen mode specified, defaulting to old modes selection\n"));
1445 +                        mode_str = NULL;
1446 +                }
1447 +                else {
1448 +                        if (default_width <= 0)
1449 +                                default_width = DisplayWidth(x_display, screen);
1450 +                        else if (default_width > DisplayWidth(x_display, screen))
1451 +                                default_width = DisplayWidth(x_display, screen);
1452 +                        if (default_height <= 0)
1453 +                                default_height = DisplayHeight(x_display, screen);
1454 +                        else if (default_height > DisplayHeight(x_display, screen))
1455 +                                default_height = DisplayHeight(x_display, screen);
1456 +                }
1457 +        }
1458 +
1459          // Construct video mode table
1460          uint32 window_modes = PrefsFindInt32("windowmodes");
1461          uint32 screen_modes = PrefsFindInt32("screenmodes");
1462          if (!has_dga)
1463                  screen_modes = 0;
1464 <        if (window_modes == 0 && screen_modes == 0)
1464 >        if (mode_str)
1465 >                window_modes = screen_modes = 0;
1466 >        else if (window_modes == 0 && screen_modes == 0)
1467                  window_modes |= 3;      // Allow at least 640x480 and 800x600 window modes
1468  
1469          VideoInfo *p = VModes;
1470 <        for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1471 <                if (find_visual_for_depth(d))
1472 <                        add_window_modes(p, window_modes, d);
1473 <
1474 <        if (has_vidmode) {
1470 >        if (mode_str) {
1471 >                if (display_type == DIS_WINDOW) {
1472 >                        for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) {
1473 >                                if (find_visual_for_depth(d)) {
1474 >                                        if (default_width > 640 && default_height > 480)
1475 >                                                add_mode(p, 3, 1, d, APPLE_W_640x480, DIS_WINDOW);
1476 >                                        if (default_width > 800 && default_height > 600)
1477 >                                                add_mode(p, 3, 2, d, APPLE_W_800x600, DIS_WINDOW);
1478 >                                        add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM);
1479 >                                }
1480 >                        }
1481 > #ifdef ENABLE_VOSF
1482 >                } else if (display_type == DIS_SCREEN && is_fbdev_dga_mode) {
1483 >                        for (unsigned int d = APPLE_1_BIT; d <= default_mode; d++)
1484 >                                if (find_visual_for_depth(d))
1485 >                                        add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM);
1486 > #endif
1487 >                } else
1488 >                        add_custom_mode(p, display_type, default_width, default_height, default_mode, APPLE_CUSTOM);
1489 >        } else if (window_modes) {
1490 >                for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1491 >                        if (find_visual_for_depth(d))
1492 >                                add_window_modes(p, window_modes, d);
1493 >        } else if (has_vidmode) {
1494                  if (has_mode(640, 480))
1495                          add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1496                  if (has_mode(800, 600))
# Line 1176 | Line 1531 | bool VideoInit(void)
1531                  int apple_id = find_apple_resolution(screen_width, screen_height);
1532                  if (apple_id != -1)
1533                          cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN);
1534 <        }
1534 >        } else if (has_dga && mode_str)
1535 >                cur_mode = find_mode(default_mode, APPLE_CUSTOM, DIS_SCREEN);
1536 >
1537          if (cur_mode == -1) {
1538                  // pick up first windowed mode available
1539                  for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
# Line 1205 | Line 1562 | bool VideoInit(void)
1562          XSetErrorHandler(ignore_errors);
1563   #endif
1564  
1565 +        // Lock down frame buffer
1566 +        XSync(x_display, false);
1567 +        LOCK_FRAME_BUFFER;
1568 +
1569          // Start periodic thread
1570          XSync(x_display, false);
1571          Set_pthread_attr(&redraw_thread_attr, 0);
# Line 1229 | Line 1590 | void VideoExit(void)
1590                  redraw_thread_active = false;
1591          }
1592  
1593 +        // Unlock frame buffer
1594 +        UNLOCK_FRAME_BUFFER;
1595 +        XSync(x_display, false);
1596 +        D(bug(" frame buffer unlocked\n"));
1597 +
1598   #ifdef ENABLE_VOSF
1599          if (use_vosf) {
1600                  // Deinitialize VOSF
# Line 1243 | Line 1609 | void VideoExit(void)
1609                  XFlush(x_display);
1610                  XSync(x_display, false);
1611          }
1612 +
1613 + #ifdef ENABLE_FBDEV_DGA
1614 +        // Close framebuffer device
1615 +        if (fb_dev_fd >= 0) {
1616 +                close(fb_dev_fd);
1617 +                fb_dev_fd = -1;
1618 +        }
1619 + #endif
1620   }
1621  
1622  
# Line 1250 | Line 1624 | void VideoExit(void)
1624   *  Suspend/resume emulator
1625   */
1626  
1253 extern void PauseEmulator(void);
1254 extern void ResumeEmulator(void);
1255
1627   static void suspend_emul(void)
1628   {
1629          if (display_type == DIS_SCREEN) {
# Line 1260 | Line 1631 | static void suspend_emul(void)
1631                  ADBKeyUp(0x36);
1632                  ctrl_down = false;
1633  
1634 <                // Pause MacOS thread
1635 <                PauseEmulator();
1265 <                emul_suspended = true;
1634 >                // Lock frame buffer (this will stop the MacOS thread)
1635 >                LOCK_FRAME_BUFFER;
1636  
1637                  // Save frame buffer
1638                  fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1639                  if (fb_save)
1640 <                        memcpy(fb_save, (void *)screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1640 >                        Mac2Host_memcpy(fb_save, screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1641  
1642                  // Close full screen display
1643 + #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1644   #ifdef ENABLE_XF86_DGA
1645 <                XF86DGADirectVideo(x_display, screen, 0);
1645 >                if (!is_fbdev_dga_mode)
1646 >                        XF86DGADirectVideo(x_display, screen, 0);
1647 > #endif
1648                  XUngrabPointer(x_display, CurrentTime);
1649                  XUngrabKeyboard(x_display, CurrentTime);
1650   #endif
1651 +                restore_mouse_accel();
1652 +                XUnmapWindow(x_display, the_win);
1653 +                wait_unmapped(the_win);
1654                  XSync(x_display, false);
1655  
1656                  // Open "suspend" window
1657                  XSetWindowAttributes wattr;
1658                  wattr.event_mask = KeyPressMask;
1659 <                wattr.background_pixel = black_pixel;
1284 <                wattr.border_pixel = black_pixel;
1659 >                wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
1660                  wattr.backing_store = Always;
1661 <                wattr.backing_planes = xdepth;
1662 <                wattr.colormap = DefaultColormap(x_display, screen);
1288 <                XSync(x_display, false);
1661 >                wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
1662 >
1663                  suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1664 <                        InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1665 <                        CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1666 <                XSync(x_display, false);
1667 <                XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1668 <                XMapRaised(x_display, suspend_win);
1295 <                XSync(x_display, false);
1664 >                                                                        InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore | CWColormap, &wattr);
1665 >                set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
1666 >                set_window_focus(suspend_win);
1667 >                XMapWindow(x_display, suspend_win);
1668 >                emul_suspended = true;
1669          }
1670   }
1671  
# Line 1303 | Line 1676 | static void resume_emul(void)
1676          XSync(x_display, false);
1677  
1678          // Reopen full screen display
1679 <        XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1680 <        XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1679 >        XMapRaised(x_display, the_win);
1680 >        wait_mapped(the_win);
1681 >        XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1682 >        Window w = is_fbdev_dga_mode ? the_win : rootwin;
1683 >        XGrabKeyboard(x_display, w, True, GrabModeAsync, GrabModeAsync, CurrentTime);
1684 >        XGrabPointer(x_display, w, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1685 >        disable_mouse_accel();
1686   #ifdef ENABLE_XF86_DGA
1687 <        XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1688 <        XF86DGASetViewPort(x_display, screen, 0, 0);
1687 >        if (!is_fbdev_dga_mode) {
1688 >                XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1689 >                XF86DGASetViewPort(x_display, screen, 0, 0);
1690 >        }
1691   #endif
1692          XSync(x_display, false);
1693  
# Line 1318 | Line 1698 | static void resume_emul(void)
1698          if (use_vosf) {
1699                  LOCK_VOSF;
1700                  PFLAG_SET_ALL;
1321                UNLOCK_VOSF;
1701                  memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1702 +                UNLOCK_VOSF;
1703          }
1704   #endif
1705          
# Line 1329 | Line 1709 | static void resume_emul(void)
1709                  // Don't copy fb_save to the temporary frame buffer in VOSF mode
1710                  if (!use_vosf)
1711   #endif
1712 <                memcpy((void *)screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1712 >                Host2Mac_memcpy(screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1713                  free(fb_save);
1714                  fb_save = NULL;
1715          }
1336        if (depth == 8)
1337                palette_changed = true;
1716  
1717 <        // Resume MacOS thread
1717 >        // Unlock frame buffer (and continue MacOS thread)
1718 >        UNLOCK_FRAME_BUFFER;
1719          emul_suspended = false;
1341        ResumeEmulator();
1720   }
1721  
1722  
# Line 1635 | Line 2013 | static void handle_events(void)
2013                                  if (use_vosf) {                 // VOSF refresh
2014                                          LOCK_VOSF;
2015                                          PFLAG_SET_ALL;
2016 +                                        memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2017                                          UNLOCK_VOSF;
2018                                  }
2019 +                                else
2020   #endif
2021 <                                memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2021 >                                        memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2022                                  break;
2023                  }
2024          }
# Line 1654 | Line 2034 | void VideoVBL(void)
2034          if (emerg_quit)
2035                  QuitEmulator();
2036  
2037 +        // Temporarily give up frame buffer lock (this is the point where
2038 +        // we are suspended when the user presses Ctrl-Tab)
2039 +        UNLOCK_FRAME_BUFFER;
2040 +        LOCK_FRAME_BUFFER;
2041 +
2042          // Execute video VBL
2043          if (private_data != NULL && private_data->interruptsEnabled)
2044                  VSLDoInterruptService(private_data->vslServiceID);
# Line 1750 | Line 2135 | void video_set_palette(void)
2135                  // We have to redraw everything because the interpretation of pixel values changed
2136                  LOCK_VOSF;
2137                  PFLAG_SET_ALL;
2138 +                if (display_type == DIS_SCREEN)
2139 +                        PFLAG_SET_VERY_DIRTY;
2140                  UNLOCK_VOSF;
1754                memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2141          }
2142   #endif
2143  
# Line 1933 | Line 2319 | static void handle_palette_changes(void)
2319                  }
2320  
2321   #ifdef ENABLE_XF86_DGA
2322 <                if (display_type == DIS_SCREEN) {
2322 >                if (display_type == DIS_SCREEN && !is_fbdev_dga_mode) {
2323                          current_dga_cmap ^= 1;
2324                          if (!IsDirectMode(mode) && cmap[current_dga_cmap])
2325                                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
# Line 1978 | Line 2364 | static void *redraw_func(void *arg)
2364                                  quit_full_screen = false;
2365                                  if (display_type == DIS_SCREEN) {
2366                                          XDisplayLock();
2367 + #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2368   #ifdef ENABLE_XF86_DGA
2369 <                                        XF86DGADirectVideo(x_display, screen, 0);
2369 >                                        if (is_fbdev_dga_mode)
2370 >                                                XF86DGADirectVideo(x_display, screen, 0);
2371 > #endif
2372                                          XUngrabPointer(x_display, CurrentTime);
2373                                          XUngrabKeyboard(x_display, CurrentTime);
2374                                          XUnmapWindow(x_display, the_win);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines