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.34 by gbeauche, 2004-12-18T18:34:56Z vs.
Revision 1.43 by gbeauche, 2005-06-22T12:24:36Z

# 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 39 | Line 53
53   # include <X11/extensions/xf86vmode.h>
54   #endif
55  
56 + #ifdef ENABLE_FBDEV_DGA
57 + # include <sys/mman.h>
58 + #endif
59 +
60   #include "main.h"
61   #include "adb.h"
62   #include "prefs.h"
# Line 109 | Line 127 | static int color_class;
127   static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
128   static Colormap cmap[2];                                        // Two colormaps (DGA) for 8-bit mode
129   static XColor x_palette[256];                           // Color palette to be used as CLUT and gamma table
130 + static int orig_accel_numer, orig_accel_denom, orig_threshold;  // Original mouse acceleration
131  
132   static XColor black, white;
133   static unsigned long black_pixel, white_pixel;
# Line 131 | Line 150 | static uint8 *the_buffer_copy = NULL;          /
150   static uint32 the_buffer_size;                          // Size of allocated the_buffer
151  
152   // Variables for DGA mode
153 + static bool is_fbdev_dga_mode = false;          // Flag: Use FBDev DGA mode?
154   static int current_dga_cmap;
155  
156 + #ifdef ENABLE_FBDEV_DGA
157 + static int fb_dev_fd = -1;                                              // Handle to fb device name
158 + static struct fb_fix_screeninfo fb_finfo;               // Fixed info
159 + static struct fb_var_screeninfo fb_vinfo;               // Variable info
160 + static struct fb_var_screeninfo fb_orig_vinfo;  // Variable info to restore later
161 + static struct fb_cmap fb_oldcmap;                               // Colormap to restore later
162 + #endif
163 +
164   #ifdef ENABLE_XF86_VIDMODE
165   // Variables for XF86 VidMode support
166   static XF86VidModeModeInfo **x_video_modes;             // Array of all available modes
# Line 153 | Line 181 | static pthread_mutex_t x_palette_lock =
181   #define UNLOCK_PALETTE
182   #endif
183  
184 + // Mutex to protect frame buffer
185 + #ifdef HAVE_SPINLOCKS
186 + static spinlock_t frame_buffer_lock = SPIN_LOCK_UNLOCKED;
187 + #define LOCK_FRAME_BUFFER spin_lock(&frame_buffer_lock)
188 + #define UNLOCK_FRAME_BUFFER spin_unlock(&frame_buffer_lock)
189 + #elif defined(HAVE_PTHREADS)
190 + static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
191 + #define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock);
192 + #define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock);
193 + #else
194 + #define LOCK_FRAME_BUFFER
195 + #define UNLOCK_FRAME_BUFFER
196 + #endif
197 +
198  
199   // Prototypes
200   static void *redraw_func(void *arg);
# Line 364 | Line 406 | static bool find_visual_for_depth(int de
406   *  Open display (window or fullscreen)
407   */
408  
409 + // Set window name and class
410 + static void set_window_name(Window w, int name)
411 + {
412 +        const char *str = GetString(name);
413 +        XStoreName(x_display, w, str);
414 +        XSetIconName(x_display, w, str);
415 +
416 +        XClassHint *hints;
417 +        hints = XAllocClassHint();
418 +        if (hints) {
419 +                hints->res_name = "SheepShaver";
420 +                hints->res_class = "SheepShaver";
421 +                XSetClassHint(x_display, w, hints);
422 +                XFree(hints);
423 +        }
424 + }
425 +
426 + // Set window input focus flag
427 + static void set_window_focus(Window w)
428 + {
429 +        XWMHints *hints = XAllocWMHints();
430 +        if (hints) {
431 +                hints->input = True;
432 +                hints->initial_state = NormalState;
433 +                hints->flags = InputHint | StateHint;
434 +                XSetWMHints(x_display, w, hints);
435 +                XFree(hints);
436 +        }
437 + }
438 +
439   // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
440   static Atom WM_DELETE_WINDOW = (Atom)0;
441   static void set_window_delete_protocol(Window w)
# Line 389 | Line 461 | static void wait_unmapped(Window w)
461          } while ((e.type != UnmapNotify) || (e.xmap.event != w));
462   }
463  
464 + // Disable mouse acceleration
465 + static void disable_mouse_accel(void)
466 + {
467 +        XChangePointerControl(x_display, True, False, 1, 1, 0);
468 + }
469 +
470 + // Restore mouse acceleration to original value
471 + static void restore_mouse_accel(void)
472 + {
473 +        XChangePointerControl(x_display, True, True, orig_accel_numer, orig_accel_denom, orig_threshold);
474 + }
475 +
476   // Trap SHM errors
477   static bool shm_error = false;
478   static int (*old_error_handler)(Display *, XErrorEvent *);
# Line 421 | Line 505 | static bool open_window(int width, int h
505          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
506                  InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr);
507  
508 <        // Set window name
509 <        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
508 >        // Set window name/class
509 >        set_window_name(the_win, STR_WINDOW_TITLE);
510 >
511 >        // Indicate that we want keyboard input
512 >        set_window_focus(the_win);
513  
514          // Set delete protocol property
515          set_window_delete_protocol(the_win);
# Line 550 | Line 637 | static bool open_window(int width, int h
637          return true;
638   }
639  
640 < // Open DGA display (!! should use X11 VidMode extensions to set mode)
641 < static bool open_dga(int width, int height)
640 > // Open FBDev DGA display
641 > static bool open_fbdev_dga(int width, int height)
642   {
643 + #ifdef ENABLE_FBDEV_DGA
644 + #ifdef ENABLE_XF86_VIDMODE
645 +        // Switch to best mode
646 +        if (has_vidmode) {
647 +                int best = -1;
648 +                for (int i = 0; i < num_x_video_modes; i++) {
649 +                        if (x_video_modes[i]->hdisplay == width && x_video_modes[i]->vdisplay == height) {
650 +                                best = i;
651 +                                break;
652 +                        }
653 +                };
654 +                assert(best != -1);
655 +                XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
656 +                XF86VidModeSetViewPort(x_display, screen, 0, 0);
657 +                D(bug("[fbdev] VideoMode %d: %d x %d @ %d\n", best,
658 +                          x_video_modes[best]->hdisplay, x_video_modes[best]->vdisplay,
659 +                          1000 * x_video_modes[best]->dotclock / (x_video_modes[best]->htotal * x_video_modes[best]->vtotal)));
660 +        }
661 + #endif
662 +
663 +        if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo) != 0) {
664 +                D(bug("[fbdev] Can't get FSCREENINFO: %s\n", strerror(errno)));
665 +                return false;
666 +        }
667 +        D(bug("[fbdev] Device ID: %s\n", fb_finfo.id));
668 +        D(bug("[fbdev] smem_start: %p [%d bytes]\n", fb_finfo.smem_start, fb_finfo.smem_len));
669 +
670 +        int fb_type = fb_finfo.type;
671 +        const char *fb_type_str = NULL;
672 +        switch (fb_type) {
673 +        case FB_TYPE_PACKED_PIXELS:                     fb_type_str = "Packed Pixels";                  break;
674 +        case FB_TYPE_PLANES:                            fb_type_str = "Non interleaved planes"; break;
675 +        case FB_TYPE_INTERLEAVED_PLANES:        fb_type_str = "Interleaved planes";             break;
676 +        case FB_TYPE_TEXT:                                      fb_type_str = "Text/attributes";                break;
677 +        case FB_TYPE_VGA_PLANES:                        fb_type_str = "EGA/VGA planes";                 break;
678 +        default:                                                        fb_type_str = "<unknown>";                              break;
679 +        }
680 +        D(bug("[fbdev] type: %s\n", fb_type_str));
681 +
682 +        if (fb_type != FB_TYPE_PACKED_PIXELS) {
683 +                D(bug("[fbdev] type '%s' not supported\n", fb_type_str));
684 +                return false;
685 +        }
686 +
687 +        int fb_visual = fb_finfo.visual;
688 +        const char *fb_visual_str;
689 +        switch (fb_visual) {
690 +        case FB_VISUAL_MONO01:                          fb_visual_str = "Monochrome 1=Black 0=White";   break;
691 +        case FB_VISUAL_MONO10:                          fb_visual_str = "Monochrome 1=While 0=Black";   break;
692 +        case FB_VISUAL_TRUECOLOR:                       fb_visual_str = "True color";                                   break;
693 +        case FB_VISUAL_PSEUDOCOLOR:                     fb_visual_str = "Pseudo color (like atari)";    break;
694 +        case FB_VISUAL_DIRECTCOLOR:                     fb_visual_str = "Direct color";                                 break;
695 +        case FB_VISUAL_STATIC_PSEUDOCOLOR:      fb_visual_str = "Pseudo color readonly";                break;
696 +        default:                                                        fb_visual_str = "<unknown>";                                    break;
697 +        }
698 +        D(bug("[fbdev] visual: %s\n", fb_visual_str));
699 +
700 +        if (fb_visual != FB_VISUAL_TRUECOLOR) {
701 +                D(bug("[fbdev] visual '%s' not supported\n", fb_visual_str));
702 +                return false;
703 +        }
704 +        
705 +        // Map frame buffer
706 +        the_buffer = (uint8 *)mmap(NULL, fb_finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev_fd, 0);
707 +        if (the_buffer == MAP_FAILED) {
708 +                D(bug("[fbdev] Can't mmap /dev/fb0: %s\n", strerror(errno)));
709 +                return false;
710 +        }
711 +
712 +        // Set absolute mouse mode
713 +        ADBSetRelMouseMode(false);
714 +
715 +        // Create window
716 +        XSetWindowAttributes wattr;
717 +        wattr.event_mask = eventmask = dga_eventmask;
718 +        wattr.override_redirect = True;
719 +        the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
720 +                                                        InputOutput, DefaultVisual(x_display, screen),
721 +                                                        CWEventMask | CWOverrideRedirect, &wattr);
722 +
723 +        // Show window
724 +        XMapRaised(x_display, the_win);
725 +        wait_mapped(the_win);
726 +
727 +        // Grab mouse and keyboard
728 +        XGrabKeyboard(x_display, the_win, True,
729 +                                  GrabModeAsync, GrabModeAsync, CurrentTime);
730 +        XGrabPointer(x_display, the_win, True,
731 +                                 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
732 +                                 GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
733 +        disable_mouse_accel();
734 +
735 +        // Create no_cursor
736 +        mac_cursor = XCreatePixmapCursor(x_display,
737 +                                                                         XCreatePixmap(x_display, the_win, 1, 1, 1),
738 +                                                                         XCreatePixmap(x_display, the_win, 1, 1, 1),
739 +                                                                         &black, &white, 0, 0);
740 +        XDefineCursor(x_display, the_win, mac_cursor);
741 +
742 +        // Init blitting routines
743 +        int bytes_per_row = TrivialBytesPerRow((width + 7) & ~7, DepthModeForPixelDepth(depth));
744 + #if ENABLE_VOSF
745 +        // Extract current screen color masks (we are in True Color mode)
746 +        VisualFormat visualFormat;
747 +        visualFormat.depth = xdepth = DefaultDepth(x_display, screen);
748 +        XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo);
749 +        assert(visualFormat.depth == visualInfo.depth);
750 +        visualFormat.Rmask = visualInfo.red_mask;
751 +        visualFormat.Gmask = visualInfo.green_mask;
752 +        visualFormat.Bmask = visualInfo.blue_mask;
753 +        D(bug("[fbdev] %d bpp, (%08x,%08x,%08x)\n",
754 +                  visualFormat.depth,
755 +                  visualFormat.Rmask, visualFormat.Gmask, visualFormat.Bmask));
756 +        D(bug("[fbdev] Mac depth %d bpp\n", depth));
757 +
758 +        // Screen_blitter_init() returns TRUE if VOSF is mandatory
759 +        // i.e. the framebuffer update function is not Blit_Copy_Raw
760 + #ifdef WORDS_BIGENDIAN
761 +        const bool native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
762 + #else
763 +        const bool native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
764 + #endif
765 +        Screen_blitter_init(visualFormat, native_byte_order, depth);
766 +        
767 +        // Allocate memory for frame buffer (SIZE is extended to page-boundary)
768 +        use_vosf = true;
769 +        the_host_buffer = the_buffer;
770 +        the_buffer_size = page_extend((height + 2) * bytes_per_row);
771 +        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
772 +        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
773 +        D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
774 + #endif
775 +
776 +        // Set frame buffer base
777 +        D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
778 +        screen_base = Host2MacAddr(the_buffer);
779 +        VModes[cur_mode].viRowBytes = bytes_per_row;
780 +        return true;
781 + #else
782 +        ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
783 +        return false;
784 + #endif
785 + }
786 +
787 + // Open XF86 DGA display (!! should use X11 VidMode extensions to set mode)
788 + static bool open_xf86_dga(int width, int height)
789 + {
790 +        if (is_fbdev_dga_mode)
791 +                return false;
792 +
793   #ifdef ENABLE_XF86_DGA
794          // Set relative mouse mode
795          ADBSetRelMouseMode(true);
# Line 642 | Line 879 | static bool open_dga(int width, int heig
879   #endif
880   }
881  
882 + // Open DGA display
883 + static bool open_dga(int width, int height)
884 + {
885 +        bool display_open;
886 +
887 +        display_open = open_xf86_dga(width, height);
888 + #ifdef ENABLE_FBDEV_DGA
889 +        // Try to fallback to FBDev DGA mode
890 +        if (!display_open) {
891 +                is_fbdev_dga_mode = true;
892 +                display_open = open_fbdev_dga(width, height);
893 +        }
894 + #endif
895 +
896 +        // Common DGA display initialization
897 +        if (display_open) {
898 +
899 +                // Fake image to get display bounds in the refresh function
900 +                if ((img = (XImage *)malloc(sizeof(*img))) == NULL)
901 +                        return false;
902 +                img->width = DisplayWidth(x_display, screen);
903 +                img->height = DisplayHeight(x_display, screen);
904 +                img->depth = is_fbdev_dga_mode ? xdepth : depth;
905 +                img->bytes_per_line = TrivialBytesPerRow(img->width, DepthModeForPixelDepth(img->depth));
906 +        }
907 +
908 +        return display_open;
909 + }
910 +
911   static bool open_display(void)
912   {
913          D(bug("open_display()\n"));
914          const VideoInfo &mode = VModes[cur_mode];
915  
916 +        // Get original mouse acceleration
917 +        XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold);
918 +
919          // Find best available X visual
920          if (!find_visual_for_depth(mode.viAppleMode)) {
921                  ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
# Line 654 | Line 923 | static bool open_display(void)
923          }
924  
925          // Build up visualFormat structure
926 +        visualFormat.fullscreen = (display_type == DIS_SCREEN);
927          visualFormat.depth = visualInfo.depth;
928          visualFormat.Rmask = visualInfo.red_mask;
929          visualFormat.Gmask = visualInfo.green_mask;
# Line 726 | Line 996 | static bool open_display(void)
996          display_type = mode.viType;
997          depth = depth_of_video_mode(mode.viAppleMode);
998  
999 <        bool display_open = false;
1000 <        if (display_type == DIS_SCREEN)
999 >        bool display_open;
1000 >        switch (display_type) {
1001 >        case DIS_SCREEN:
1002                  display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
1003 <        else if (display_type == DIS_WINDOW)
1003 >                break;
1004 >        case DIS_WINDOW:
1005                  display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
1006 +                break;
1007 +        default:
1008 +                display_open = false;
1009 +                break;
1010 +        }
1011  
1012   #ifdef ENABLE_VOSF
1013          if (use_vosf) {
1014                  // Initialize the VOSF system
1015 +                LOCK_VOSF;
1016                  if (!video_vosf_init()) {
1017                          ErrorAlert(GetString(STR_VOSF_INIT_ERR));
1018 +                        UNLOCK_VOSF;
1019                          return false;
1020                  }
1021 +                UNLOCK_VOSF;
1022          }
1023   #endif
1024 <        
1024 >
1025 >        // Zero screen buffers, viRowBytes is initialized at this stage
1026 >        memset(the_buffer, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1027 >        memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1028          return display_open;
1029   }
1030  
# Line 777 | Line 1060 | static void close_window(void)
1060          XSync(x_display, false);
1061   }
1062  
1063 < // Close DGA mode
1064 < static void close_dga(void)
1063 > // Close FBDev mode
1064 > static void close_fbdev_dga(void)
1065 > {
1066 > #ifdef ENABLE_FBDEV_DGA
1067 >        uint8 *fb_base;
1068 >        if (!use_vosf)
1069 >                fb_base = the_buffer;
1070 > #ifdef ENABLE_VOSF
1071 >        else
1072 >                fb_base = the_host_buffer;
1073 > #endif
1074 >        munmap(fb_base, fb_finfo.smem_len);
1075 > #endif
1076 > }
1077 >
1078 > // Close XF86 DGA mode
1079 > static void close_xf86_dga(void)
1080   {
1081   #ifdef ENABLE_XF86_DGA
1082          XF86DGADirectVideo(x_display, screen, 0);
1083 + #endif
1084 + }
1085 +
1086 + // Close DGA mode
1087 + static void close_dga(void)
1088 + {
1089 +        if (is_fbdev_dga_mode)
1090 +                close_fbdev_dga();
1091 +        else
1092 +                close_xf86_dga();
1093 +
1094          XUngrabPointer(x_display, CurrentTime);
1095          XUngrabKeyboard(x_display, CurrentTime);
787 #endif
1096  
1097   #ifdef ENABLE_XF86_VIDMODE
1098          if (has_vidmode)
1099                  XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
1100   #endif
1101  
1102 +        // Release fake image (it's not a normal XImage!)
1103 +        free(img);
1104 +        img = NULL;
1105 +
1106          if (!use_vosf) {
1107                  // don't free() the screen buffer in driver_base dtor
1108                  the_buffer = NULL;
# Line 861 | Line 1173 | static void close_display(void)
1173                  }
1174          }
1175   #endif
1176 +
1177 +        // Restore mouse acceleration
1178 +        restore_mouse_accel();
1179   }
1180  
1181  
# Line 970 | Line 1285 | static int find_mode(int apple_mode, int
1285          return -1;
1286   }
1287  
1288 + // Add custom video mode
1289 + static void add_custom_mode(VideoInfo *&p, int type, uint32 x, uint32 y, int apple_mode, int apple_id)
1290 + {
1291 +        p->viType = type;
1292 +        p->viXsize = x;
1293 +        p->viYsize = y;
1294 +        p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
1295 +        p->viAppleMode = apple_mode;
1296 +        p->viAppleID = apple_id;
1297 +        p++;
1298 + }
1299 +
1300   // Add mode to list of supported modes
1301   static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
1302   {
1303          if (allow & test) {
1304 <                p->viType = type;
1304 >                uint32 x = 0, y = 0;
1305                  switch (apple_id) {
1306                          case APPLE_W_640x480:
1307                          case APPLE_640x480:
1308 <                                p->viXsize = 640;
1309 <                                p->viYsize = 480;
1308 >                                x = 640;
1309 >                                y = 480;
1310                                  break;
1311                          case APPLE_W_800x600:
1312                          case APPLE_800x600:
1313 <                                p->viXsize = 800;
1314 <                                p->viYsize = 600;
1313 >                                x = 800;
1314 >                                y = 600;
1315                                  break;
1316                          case APPLE_1024x768:
1317 <                                p->viXsize = 1024;
1318 <                                p->viYsize = 768;
1317 >                                x = 1024;
1318 >                                y = 768;
1319                                  break;
1320                          case APPLE_1152x768:
1321 <                                p->viXsize = 1152;
1322 <                                p->viYsize = 768;
1321 >                                x = 1152;
1322 >                                y = 768;
1323                                  break;
1324                          case APPLE_1152x900:
1325 <                                p->viXsize = 1152;
1326 <                                p->viYsize = 900;
1325 >                                x = 1152;
1326 >                                y = 900;
1327                                  break;
1328                          case APPLE_1280x1024:
1329 <                                p->viXsize = 1280;
1330 <                                p->viYsize = 1024;
1329 >                                x = 1280;
1330 >                                y = 1024;
1331                                  break;
1332                          case APPLE_1600x1200:
1333 <                                p->viXsize = 1600;
1334 <                                p->viYsize = 1200;
1333 >                                x = 1600;
1334 >                                y = 1200;
1335                                  break;
1336                  }
1337 <                p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
1011 <                p->viAppleMode = apple_mode;
1012 <                p->viAppleID = apple_id;
1013 <                p++;
1337 >                add_custom_mode(p, type, x, y, apple_mode, apple_id);
1338          }
1339   }
1340  
# Line 1024 | Line 1348 | static void add_window_modes(VideoInfo *
1348   static bool has_mode(int x, int y)
1349   {
1350   #ifdef ENABLE_XF86_VIDMODE
1351 <        for (int i=0; i<num_x_video_modes; i++)
1352 <                if (x_video_modes[i]->hdisplay >= x && x_video_modes[i]->vdisplay >= y)
1353 <                        return true;
1354 <        return false;
1355 < #else
1356 <        return DisplayWidth(x_display, screen) >= x && DisplayHeight(x_display, screen) >= y;
1351 >        if (has_vidmode) {
1352 >                for (int i=0; i<num_x_video_modes; i++)
1353 >                        if (x_video_modes[i]->hdisplay == x && x_video_modes[i]->vdisplay == y)
1354 >                                return true;
1355 >                return false;
1356 >        }
1357   #endif
1358 +        return DisplayWidth(x_display, screen) >= x && DisplayHeight(x_display, screen) >= y;
1359   }
1360  
1361   bool VideoInit(void)
# Line 1079 | Line 1404 | bool VideoInit(void)
1404   #ifdef ENABLE_XF86_DGA
1405          // DGA available?
1406      int event_base, error_base;
1407 +    is_fbdev_dga_mode = false;
1408      if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
1409                  int dga_flags = 0;
1410                  XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1411                  has_dga = dga_flags & XF86DGADirectPresent;
1412 + #if defined(__linux__)
1413 +                // Check r/w permission on /dev/mem for DGA mode to work
1414 +                if (has_dga && access("/dev/mem", R_OK | W_OK) != 0)
1415 +                        has_dga = false;
1416 + #endif
1417          } else
1418                  has_dga = false;
1419   #endif
# Line 1091 | Line 1422 | bool VideoInit(void)
1422          // VidMode available?
1423          int vm_event_base, vm_error_base;
1424          has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
1425 <        if (has_vidmode)
1425 >        if (has_vidmode) {
1426 >                int vm_major_version, vm_minor_version;
1427 >                XF86VidModeQueryVersion(x_display, &vm_major_version, &vm_minor_version);
1428 >                D(bug("VidMode extension %d.%d available\n", vm_major_version, vm_minor_version));
1429                  XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1430 +        }
1431 + #endif
1432 +
1433 + #ifdef ENABLE_FBDEV_DGA
1434 +        // FBDev available?
1435 +        bool has_fbdev_dga = false;
1436 +        if (local_X11) {
1437 +                if ((fb_dev_fd = open("/dev/fb0", O_RDWR)) > 0) {
1438 +                        if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo) != 0)
1439 +                                close(fb_dev_fd);
1440 +                        else {
1441 +                                has_fbdev_dga = true;
1442 +                                if (!has_dga) {
1443 +                                        // Fallback to FBDev DGA mode if XF86 DGA is not possible
1444 +                                        has_dga = true;
1445 +                                        is_fbdev_dga_mode = true;
1446 +                                }
1447 +                                fb_orig_vinfo = fb_vinfo;
1448 +                                D(bug("Frame buffer device initial resolution: %dx%dx%d\n", fb_vinfo.xres, fb_vinfo.yres, fb_vinfo.bits_per_pixel));
1449 +                        }
1450 +                }
1451 +        }
1452   #endif
1453  
1454          // Find black and white colors
# Line 1120 | Line 1476 | bool VideoInit(void)
1476                  break;
1477          }
1478  
1479 +        // Get screen mode from preferences
1480 +        const char *mode_str = PrefsFindString("screen");
1481 +        int default_width = 640, default_height = 480;
1482 +        if (mode_str) {
1483 +                display_type = DIS_INVALID;
1484 +                if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
1485 +                        display_type = DIS_WINDOW;
1486 + #ifdef ENABLE_XF86_DGA
1487 +                else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1488 +                        display_type = DIS_SCREEN;
1489 + #endif
1490 + #ifdef ENABLE_FBDEV_DGA
1491 +                else if (has_fbdev_dga && sscanf(mode_str, "fbdev/%d/%d", &default_width, &default_height) == 2) {
1492 +                        is_fbdev_dga_mode = true;
1493 +                        display_type = DIS_SCREEN;
1494 +                }
1495 + #endif
1496 +                if (display_type == DIS_INVALID) {
1497 +                        D(bug("Invalid screen mode specified, defaulting to old modes selection\n"));
1498 +                        mode_str = NULL;
1499 +                }
1500 +                else {
1501 +                        if (default_width <= 0)
1502 +                                default_width = DisplayWidth(x_display, screen);
1503 +                        else if (default_width > DisplayWidth(x_display, screen))
1504 +                                default_width = DisplayWidth(x_display, screen);
1505 +                        if (default_height <= 0)
1506 +                                default_height = DisplayHeight(x_display, screen);
1507 +                        else if (default_height > DisplayHeight(x_display, screen))
1508 +                                default_height = DisplayHeight(x_display, screen);
1509 +                }
1510 +        }
1511 +
1512          // Construct video mode table
1513          uint32 window_modes = PrefsFindInt32("windowmodes");
1514          uint32 screen_modes = PrefsFindInt32("screenmodes");
1515          if (!has_dga)
1516                  screen_modes = 0;
1517 <        if (window_modes == 0 && screen_modes == 0)
1517 >        if (mode_str)
1518 >                window_modes = screen_modes = 0;
1519 >        else if (window_modes == 0 && screen_modes == 0)
1520                  window_modes |= 3;      // Allow at least 640x480 and 800x600 window modes
1521  
1522          VideoInfo *p = VModes;
1523 <        for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1524 <                if (find_visual_for_depth(d))
1525 <                        add_window_modes(p, window_modes, d);
1523 >        if (mode_str) {
1524 >                if (display_type == DIS_WINDOW) {
1525 >                        for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) {
1526 >                                if (find_visual_for_depth(d)) {
1527 >                                        if (default_width > 640 && default_height > 480)
1528 >                                                add_mode(p, 3, 1, d, APPLE_W_640x480, DIS_WINDOW);
1529 >                                        if (default_width > 800 && default_height > 600)
1530 >                                                add_mode(p, 3, 2, d, APPLE_W_800x600, DIS_WINDOW);
1531 >                                        add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM);
1532 >                                }
1533 >                        }
1534 > #ifdef ENABLE_VOSF
1535 >                } else if (display_type == DIS_SCREEN && is_fbdev_dga_mode) {
1536 >                        for (unsigned int d = APPLE_1_BIT; d <= default_mode; d++)
1537 >                                if (find_visual_for_depth(d))
1538 >                                        add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM);
1539 > #endif
1540 >                } else
1541 >                        add_custom_mode(p, display_type, default_width, default_height, default_mode, APPLE_CUSTOM);
1542  
1543 <        if (has_vidmode) {
1543 >                // Add extra VidMode capable modes
1544 >                if (display_type == DIS_SCREEN) {
1545 >                        struct {
1546 >                                int w;
1547 >                                int h;
1548 >                                int apple_id;
1549 >                        }
1550 >                        video_modes[] = {
1551 >                                {  640,  480, APPLE_640x480   },
1552 >                                {  800,  600, APPLE_800x600   },
1553 >                                { 1024,  768, APPLE_1024x768  },
1554 >                                { 1152,  768, APPLE_1152x768  },
1555 >                                { 1152,  900, APPLE_1152x900  },
1556 >                                { 1280, 1024, APPLE_1280x1024 },
1557 >                                { 1600, 1200, APPLE_1600x1200 },
1558 >                                { 0, }
1559 >                        };
1560 >
1561 >                        for (int i = 0; video_modes[i].w != 0; i++) {
1562 >                                const int w = video_modes[i].w;
1563 >                                const int h = video_modes[i].h;
1564 >                                if (w >= default_width || h >= default_height)
1565 >                                        continue;
1566 >                                if (has_mode(w, h)) {
1567 > #ifdef ENABLE_VOSF
1568 >                                        if (is_fbdev_dga_mode) {
1569 >                                                for (unsigned int d = APPLE_1_BIT; d <= default_mode; d++)
1570 >                                                        if (find_visual_for_depth(d))
1571 >                                                                add_custom_mode(p, display_type, w, h, d, video_modes[i].apple_id);
1572 >                                        } else
1573 > #endif
1574 >                                                add_custom_mode(p, display_type, w, h, default_mode, video_modes[i].apple_id);
1575 >                                }
1576 >                        }
1577 >                }
1578 >        } else if (window_modes) {
1579 >                for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1580 >                        if (find_visual_for_depth(d))
1581 >                                add_window_modes(p, window_modes, d);
1582 >        } else if (has_vidmode) {
1583                  if (has_mode(640, 480))
1584                          add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1585                  if (has_mode(800, 600))
# Line 1174 | Line 1620 | bool VideoInit(void)
1620                  int apple_id = find_apple_resolution(screen_width, screen_height);
1621                  if (apple_id != -1)
1622                          cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN);
1623 <        }
1623 >        } else if (has_dga && mode_str)
1624 >                cur_mode = find_mode(default_mode, APPLE_CUSTOM, DIS_SCREEN);
1625 >
1626          if (cur_mode == -1) {
1627                  // pick up first windowed mode available
1628                  for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
# Line 1203 | Line 1651 | bool VideoInit(void)
1651          XSetErrorHandler(ignore_errors);
1652   #endif
1653  
1654 +        // Lock down frame buffer
1655 +        XSync(x_display, false);
1656 +        LOCK_FRAME_BUFFER;
1657 +
1658          // Start periodic thread
1659          XSync(x_display, false);
1660          Set_pthread_attr(&redraw_thread_attr, 0);
# Line 1227 | Line 1679 | void VideoExit(void)
1679                  redraw_thread_active = false;
1680          }
1681  
1682 +        // Unlock frame buffer
1683 +        UNLOCK_FRAME_BUFFER;
1684 +        XSync(x_display, false);
1685 +        D(bug(" frame buffer unlocked\n"));
1686 +
1687   #ifdef ENABLE_VOSF
1688          if (use_vosf) {
1689                  // Deinitialize VOSF
# Line 1241 | Line 1698 | void VideoExit(void)
1698                  XFlush(x_display);
1699                  XSync(x_display, false);
1700          }
1701 +
1702 + #ifdef ENABLE_FBDEV_DGA
1703 +        // Close framebuffer device
1704 +        if (fb_dev_fd >= 0) {
1705 +                close(fb_dev_fd);
1706 +                fb_dev_fd = -1;
1707 +        }
1708 + #endif
1709   }
1710  
1711  
# Line 1248 | Line 1713 | void VideoExit(void)
1713   *  Suspend/resume emulator
1714   */
1715  
1251 extern void PauseEmulator(void);
1252 extern void ResumeEmulator(void);
1253
1716   static void suspend_emul(void)
1717   {
1718          if (display_type == DIS_SCREEN) {
# Line 1258 | Line 1720 | static void suspend_emul(void)
1720                  ADBKeyUp(0x36);
1721                  ctrl_down = false;
1722  
1723 <                // Pause MacOS thread
1724 <                PauseEmulator();
1263 <                emul_suspended = true;
1723 >                // Lock frame buffer (this will stop the MacOS thread)
1724 >                LOCK_FRAME_BUFFER;
1725  
1726                  // Save frame buffer
1727                  fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
# Line 1268 | Line 1729 | static void suspend_emul(void)
1729                          Mac2Host_memcpy(fb_save, screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1730  
1731                  // Close full screen display
1732 + #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1733   #ifdef ENABLE_XF86_DGA
1734 <                XF86DGADirectVideo(x_display, screen, 0);
1734 >                if (!is_fbdev_dga_mode)
1735 >                        XF86DGADirectVideo(x_display, screen, 0);
1736 > #endif
1737                  XUngrabPointer(x_display, CurrentTime);
1738                  XUngrabKeyboard(x_display, CurrentTime);
1739   #endif
1740 +                restore_mouse_accel();
1741 +                XUnmapWindow(x_display, the_win);
1742 +                wait_unmapped(the_win);
1743                  XSync(x_display, false);
1744  
1745                  // Open "suspend" window
1746                  XSetWindowAttributes wattr;
1747                  wattr.event_mask = KeyPressMask;
1748 <                wattr.background_pixel = black_pixel;
1282 <                wattr.border_pixel = black_pixel;
1748 >                wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
1749                  wattr.backing_store = Always;
1750 <                wattr.backing_planes = xdepth;
1751 <                wattr.colormap = DefaultColormap(x_display, screen);
1286 <                XSync(x_display, false);
1750 >                wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
1751 >
1752                  suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1753 <                        InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1754 <                        CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1755 <                XSync(x_display, false);
1756 <                XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1757 <                XMapRaised(x_display, suspend_win);
1293 <                XSync(x_display, false);
1753 >                                                                        InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore | CWColormap, &wattr);
1754 >                set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
1755 >                set_window_focus(suspend_win);
1756 >                XMapWindow(x_display, suspend_win);
1757 >                emul_suspended = true;
1758          }
1759   }
1760  
# Line 1301 | Line 1765 | static void resume_emul(void)
1765          XSync(x_display, false);
1766  
1767          // Reopen full screen display
1768 <        XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1769 <        XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1768 >        XMapRaised(x_display, the_win);
1769 >        wait_mapped(the_win);
1770 >        XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1771 >        Window w = is_fbdev_dga_mode ? the_win : rootwin;
1772 >        XGrabKeyboard(x_display, w, True, GrabModeAsync, GrabModeAsync, CurrentTime);
1773 >        XGrabPointer(x_display, w, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, is_fbdev_dga_mode ? w : None, None, CurrentTime);
1774 >        disable_mouse_accel();
1775   #ifdef ENABLE_XF86_DGA
1776 <        XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1777 <        XF86DGASetViewPort(x_display, screen, 0, 0);
1776 >        if (!is_fbdev_dga_mode) {
1777 >                XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1778 >                XF86DGASetViewPort(x_display, screen, 0, 0);
1779 >        }
1780   #endif
1781          XSync(x_display, false);
1782  
# Line 1316 | Line 1787 | static void resume_emul(void)
1787          if (use_vosf) {
1788                  LOCK_VOSF;
1789                  PFLAG_SET_ALL;
1319                UNLOCK_VOSF;
1790                  memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1791 +                UNLOCK_VOSF;
1792          }
1793   #endif
1794          
# Line 1331 | Line 1802 | static void resume_emul(void)
1802                  free(fb_save);
1803                  fb_save = NULL;
1804          }
1334        if (depth == 8)
1335                palette_changed = true;
1805  
1806 <        // Resume MacOS thread
1806 >        // Unlock frame buffer (and continue MacOS thread)
1807 >        UNLOCK_FRAME_BUFFER;
1808          emul_suspended = false;
1339        ResumeEmulator();
1809   }
1810  
1811  
# Line 1633 | Line 2102 | static void handle_events(void)
2102                                  if (use_vosf) {                 // VOSF refresh
2103                                          LOCK_VOSF;
2104                                          PFLAG_SET_ALL;
2105 +                                        memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2106                                          UNLOCK_VOSF;
2107                                  }
2108 +                                else
2109   #endif
2110 <                                memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2110 >                                        memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2111                                  break;
2112                  }
2113          }
# Line 1652 | Line 2123 | void VideoVBL(void)
2123          if (emerg_quit)
2124                  QuitEmulator();
2125  
2126 +        // Temporarily give up frame buffer lock (this is the point where
2127 +        // we are suspended when the user presses Ctrl-Tab)
2128 +        UNLOCK_FRAME_BUFFER;
2129 +        LOCK_FRAME_BUFFER;
2130 +
2131          // Execute video VBL
2132          if (private_data != NULL && private_data->interruptsEnabled)
2133                  VSLDoInterruptService(private_data->vslServiceID);
# Line 1748 | Line 2224 | void video_set_palette(void)
2224                  // We have to redraw everything because the interpretation of pixel values changed
2225                  LOCK_VOSF;
2226                  PFLAG_SET_ALL;
2227 +                if (display_type == DIS_SCREEN)
2228 +                        PFLAG_SET_VERY_DIRTY;
2229                  UNLOCK_VOSF;
1752                memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2230          }
2231   #endif
2232  
# Line 1931 | Line 2408 | static void handle_palette_changes(void)
2408                  }
2409  
2410   #ifdef ENABLE_XF86_DGA
2411 <                if (display_type == DIS_SCREEN) {
2411 >                if (display_type == DIS_SCREEN && !is_fbdev_dga_mode) {
2412                          current_dga_cmap ^= 1;
2413                          if (!IsDirectMode(mode) && cmap[current_dga_cmap])
2414                                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
# Line 1976 | Line 2453 | static void *redraw_func(void *arg)
2453                                  quit_full_screen = false;
2454                                  if (display_type == DIS_SCREEN) {
2455                                          XDisplayLock();
2456 + #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2457   #ifdef ENABLE_XF86_DGA
2458 <                                        XF86DGADirectVideo(x_display, screen, 0);
2458 >                                        if (!is_fbdev_dga_mode)
2459 >                                                XF86DGADirectVideo(x_display, screen, 0);
2460 > #endif
2461                                          XUngrabPointer(x_display, CurrentTime);
2462                                          XUngrabKeyboard(x_display, CurrentTime);
2463                                          XUnmapWindow(x_display, the_win);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines