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.29 by gbeauche, 2004-06-22T20:01:18Z vs.
Revision 1.40 by gbeauche, 2005-03-28T16:19:28Z

# 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 46 | Line 60
60   #include "about_window.h"
61   #include "video.h"
62   #include "video_defs.h"
63 + #include "video_blit.h"
64  
65   #define DEBUG 0
66   #include "debug.h"
# Line 101 | Line 116 | static int depth;                                                      // Depth of Mac
116   static Window rootwin, the_win;                         // Root window and our window
117   static int num_depths = 0;                                      // Number of available X depths
118   static int *avail_depths = NULL;                        // List of available X depths
119 + static VisualFormat visualFormat;
120   static XVisualInfo visualInfo;
121   static Visual *vis;
122   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 129 | 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 151 | 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 362 | 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 387 | 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 419 | 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 492 | 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 501 | 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 540 | Line 625 | static bool open_window(int width, int h
625          native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
626   #endif
627   #ifdef ENABLE_VOSF
628 <        Screen_blitter_init(&visualInfo, native_byte_order, depth);
628 >        Screen_blitter_init(visualFormat, native_byte_order, depth);
629   #endif
630  
631          // Set bytes per row
# Line 548 | 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 +        the_host_buffer = the_buffer;
745 +        the_host_buffer_row_bytes = TrivialBytesPerRow((width + 7) & ~7, DepthModeForPixelDepth(visualFormat.depth));
746 +        the_buffer_size = page_extend((height + 2) * the_host_buffer_row_bytes);
747 +        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
748 +        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
749 +        D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
750 + #endif
751 +
752 +        // Set frame buffer base
753 +        D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
754 +        screen_base = Host2MacAddr(the_buffer);
755 +        VModes[cur_mode].viRowBytes = TrivialBytesPerRow((width + 7) & ~7, DepthModeForPixelDepth(depth));
756 +        return true;
757 + #else
758 +        ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
759 +        return false;
760 + #endif
761 + }
762 +
763   // Open DGA display (!! should use X11 VidMode extensions to set mode)
764   static bool open_dga(int width, int height)
765   {
766 +        if (is_fbdev_dga_mode)
767 +                return false;
768 +
769   #ifdef ENABLE_XF86_DGA
770          // Set relative mouse mode
771          ADBSetRelMouseMode(true);
# Line 614 | Line 829 | static bool open_dga(int width, int heig
829   #if REAL_ADDRESSING || DIRECT_ADDRESSING
830          // Screen_blitter_init() returns TRUE if VOSF is mandatory
831          // i.e. the framebuffer update function is not Blit_Copy_Raw
832 <        use_vosf = Screen_blitter_init(&visualInfo, native_byte_order, depth);
832 >        use_vosf = Screen_blitter_init(visualFormat, native_byte_order, depth);
833          
834          if (use_vosf) {
835            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
836            the_host_buffer = the_buffer;
837 <          the_buffer_size = page_extend((height + 2) * bytes_per_row);
837 >          the_buffer_size = page_extend((height + 2) * (the_host_buffer_row_bytes = bytes_per_row));
838            the_buffer_copy = (uint8 *)malloc(the_buffer_size);
839            the_buffer = (uint8 *)vm_acquire(the_buffer_size);
840            D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
# Line 631 | Line 846 | static bool open_dga(int width, int heig
846  
847          // Set frame buffer base
848          D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
849 <        screen_base = (uint32)the_buffer;
849 >        screen_base = Host2MacAddr(the_buffer);
850          VModes[cur_mode].viRowBytes = bytes_per_row;
851          return true;
852   #else
# Line 645 | Line 860 | static bool open_display(void)
860          D(bug("open_display()\n"));
861          const VideoInfo &mode = VModes[cur_mode];
862  
863 +        // Get original mouse acceleration
864 +        XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold);
865 +
866          // Find best available X visual
867          if (!find_visual_for_depth(mode.viAppleMode)) {
868                  ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
869                  return false;
870          }
871  
872 +        // Build up visualFormat structure
873 +        visualFormat.fullscreen = (display_type == DIS_SCREEN);
874 +        visualFormat.depth = visualInfo.depth;
875 +        visualFormat.Rmask = visualInfo.red_mask;
876 +        visualFormat.Gmask = visualInfo.green_mask;
877 +        visualFormat.Bmask = visualInfo.blue_mask;
878 +
879          // Create color maps
880          if (color_class == PseudoColor || color_class == DirectColor) {
881                  cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
# Line 719 | Line 944 | static bool open_display(void)
944          depth = depth_of_video_mode(mode.viAppleMode);
945  
946          bool display_open = false;
947 <        if (display_type == DIS_SCREEN)
947 >        if (display_type == DIS_SCREEN) {
948                  display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
949 + #ifdef ENABLE_FBDEV_DGA
950 +                // Try to fallback to FBDev DGA mode
951 +                if (!display_open) {
952 +                        is_fbdev_dga_mode = true;
953 +                        display_open = open_fbdev(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
954 +                }
955 + #endif
956 +                
957 +        }
958          else if (display_type == DIS_WINDOW)
959                  display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
960  
# Line 769 | Line 1003 | static void close_window(void)
1003          XSync(x_display, false);
1004   }
1005  
1006 + // Close FBDev mode
1007 + static void close_fbdev(void)
1008 + {
1009 + #ifdef ENABLE_FBDEV_DGA
1010 +        XUngrabPointer(x_display, CurrentTime);
1011 +        XUngrabKeyboard(x_display, CurrentTime);
1012 +
1013 +        uint8 *fb_base;
1014 +        if (!use_vosf) {
1015 +                // don't free() the screen buffer in driver_base dtor
1016 +                fb_base = the_buffer;
1017 +                the_buffer = NULL;
1018 +        }
1019 + #ifdef ENABLE_VOSF
1020 +        else {
1021 +                // don't free() the screen buffer in driver_base dtor
1022 +                fb_base = the_host_buffer;
1023 +                the_host_buffer = NULL;
1024 +        }
1025 + #endif
1026 +        munmap(fb_base, fb_finfo.smem_len);
1027 + #endif
1028 + }
1029 +
1030   // Close DGA mode
1031   static void close_dga(void)
1032   {
1033 +        if (is_fbdev_dga_mode)
1034 +                return;
1035 +
1036   #ifdef ENABLE_XF86_DGA
1037          XF86DGADirectVideo(x_display, screen, 0);
1038          XUngrabPointer(x_display, CurrentTime);
# Line 797 | Line 1058 | static void close_dga(void)
1058  
1059   static void close_display(void)
1060   {
1061 <        if (display_type == DIS_SCREEN)
1062 <                close_dga();
1061 >        if (display_type == DIS_SCREEN) {
1062 >                if (is_fbdev_dga_mode)
1063 >                        close_fbdev();
1064 >                else
1065 >                        close_dga();
1066 >        }
1067          else if (display_type == DIS_WINDOW)
1068                  close_window();
1069  
# Line 853 | Line 1118 | static void close_display(void)
1118                  }
1119          }
1120   #endif
1121 +
1122 +        // Restore mouse acceleration
1123 +        restore_mouse_accel();
1124   }
1125  
1126  
# Line 884 | Line 1152 | static void keycode_init(void)
1152  
1153                  // Search for server vendor string, then read keycodes
1154                  const char *vendor = ServerVendor(x_display);
887 #if (defined(__APPLE__) && defined(__MACH__))
1155                  // Force use of MacX mappings on MacOS X with Apple's X server
1156                  int dummy;
1157                  if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy))
1158                          vendor = "MacX";
892 #endif
1159                  bool vendor_found = false;
1160                  char line[256];
1161                  while (fgets(line, 255, f)) {
# Line 964 | Line 1230 | static int find_mode(int apple_mode, int
1230          return -1;
1231   }
1232  
1233 + // Add custom video mode
1234 + static void add_custom_mode(VideoInfo *&p, int type, uint32 x, uint32 y, int apple_mode, int apple_id)
1235 + {
1236 +        p->viType = type;
1237 +        p->viXsize = x;
1238 +        p->viYsize = y;
1239 +        p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
1240 +        p->viAppleMode = apple_mode;
1241 +        p->viAppleID = apple_id;
1242 +        p++;
1243 + }
1244 +
1245   // Add mode to list of supported modes
1246   static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
1247   {
1248          if (allow & test) {
1249 <                p->viType = type;
1249 >                uint32 x = 0, y = 0;
1250                  switch (apple_id) {
1251                          case APPLE_W_640x480:
1252                          case APPLE_640x480:
1253 <                                p->viXsize = 640;
1254 <                                p->viYsize = 480;
1253 >                                x = 640;
1254 >                                y = 480;
1255                                  break;
1256                          case APPLE_W_800x600:
1257                          case APPLE_800x600:
1258 <                                p->viXsize = 800;
1259 <                                p->viYsize = 600;
1258 >                                x = 800;
1259 >                                y = 600;
1260                                  break;
1261                          case APPLE_1024x768:
1262 <                                p->viXsize = 1024;
1263 <                                p->viYsize = 768;
1262 >                                x = 1024;
1263 >                                y = 768;
1264                                  break;
1265                          case APPLE_1152x768:
1266 <                                p->viXsize = 1152;
1267 <                                p->viYsize = 768;
1266 >                                x = 1152;
1267 >                                y = 768;
1268                                  break;
1269                          case APPLE_1152x900:
1270 <                                p->viXsize = 1152;
1271 <                                p->viYsize = 900;
1270 >                                x = 1152;
1271 >                                y = 900;
1272                                  break;
1273                          case APPLE_1280x1024:
1274 <                                p->viXsize = 1280;
1275 <                                p->viYsize = 1024;
1274 >                                x = 1280;
1275 >                                y = 1024;
1276                                  break;
1277                          case APPLE_1600x1200:
1278 <                                p->viXsize = 1600;
1279 <                                p->viYsize = 1200;
1278 >                                x = 1600;
1279 >                                y = 1200;
1280                                  break;
1281                  }
1282 <                p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
1005 <                p->viAppleMode = apple_mode;
1006 <                p->viAppleID = apple_id;
1007 <                p++;
1282 >                add_custom_mode(p, type, x, y, apple_mode, apple_id);
1283          }
1284   }
1285  
# Line 1073 | Line 1348 | bool VideoInit(void)
1348   #ifdef ENABLE_XF86_DGA
1349          // DGA available?
1350      int event_base, error_base;
1351 +    is_fbdev_dga_mode = false;
1352      if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
1353                  int dga_flags = 0;
1354                  XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1355                  has_dga = dga_flags & XF86DGADirectPresent;
1356 + #if defined(__linux__)
1357 +                // Check r/w permission on /dev/mem for DGA mode to work
1358 +                if (has_dga && access("/dev/mem", R_OK | W_OK) != 0)
1359 +                        has_dga = false;
1360 + #endif
1361          } else
1362                  has_dga = false;
1363   #endif
# Line 1089 | Line 1370 | bool VideoInit(void)
1370                  XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1371   #endif
1372  
1373 + #ifdef ENABLE_FBDEV_DGA
1374 +        // FBDev available?
1375 +        bool has_fbdev_dga = false;
1376 +        if (local_X11) {
1377 +                if ((fb_dev_fd = open("/dev/fb0", O_RDWR)) > 0) {
1378 +                        if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo) != 0)
1379 +                                close(fb_dev_fd);
1380 +                        else {
1381 +                                has_fbdev_dga = true;
1382 +                                if (!has_dga) {
1383 +                                        // Fallback to FBDev DGA mode if XF86 DGA is not possible
1384 +                                        has_dga = true;
1385 +                                        is_fbdev_dga_mode = true;
1386 +                                }
1387 +                                fb_orig_vinfo = fb_vinfo;
1388 +                                D(bug("Frame buffer device initial resolution: %dx%dx%d\n", fb_vinfo.xres, fb_vinfo.yres, fb_vinfo.bits_per_pixel));
1389 +                        }
1390 +                }
1391 +        }
1392 + #endif
1393 +
1394          // Find black and white colors
1395          XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
1396          XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
# Line 1114 | Line 1416 | bool VideoInit(void)
1416                  break;
1417          }
1418  
1419 +        // Get screen mode from preferences
1420 +        const char *mode_str = PrefsFindString("screen");
1421 +        int default_width = 640, default_height = 480;
1422 +        if (mode_str) {
1423 +                display_type = DIS_INVALID;
1424 +                if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
1425 +                        display_type = DIS_WINDOW;
1426 + #ifdef ENABLE_XF86_DGA
1427 +                else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1428 +                        display_type = DIS_SCREEN;
1429 + #endif
1430 + #ifdef ENABLE_FBDEV_DGA
1431 +                else if (has_fbdev_dga && sscanf(mode_str, "fbdev/%d/%d", &default_width, &default_height) == 2) {
1432 +                        is_fbdev_dga_mode = true;
1433 +                        display_type = DIS_SCREEN;
1434 +                }
1435 + #endif
1436 +                if (display_type == DIS_INVALID) {
1437 +                        D(bug("Invalid screen mode specified, defaulting to old modes selection\n"));
1438 +                        mode_str = NULL;
1439 +                }
1440 +                else {
1441 +                        if (default_width <= 0)
1442 +                                default_width = DisplayWidth(x_display, screen);
1443 +                        else if (default_width > DisplayWidth(x_display, screen))
1444 +                                default_width = DisplayWidth(x_display, screen);
1445 +                        if (default_height <= 0)
1446 +                                default_height = DisplayHeight(x_display, screen);
1447 +                        else if (default_height > DisplayHeight(x_display, screen))
1448 +                                default_height = DisplayHeight(x_display, screen);
1449 +                }
1450 +        }
1451 +
1452          // Construct video mode table
1453          uint32 window_modes = PrefsFindInt32("windowmodes");
1454          uint32 screen_modes = PrefsFindInt32("screenmodes");
1455          if (!has_dga)
1456                  screen_modes = 0;
1457 <        if (window_modes == 0 && screen_modes == 0)
1457 >        if (mode_str)
1458 >                window_modes = screen_modes = 0;
1459 >        else if (window_modes == 0 && screen_modes == 0)
1460                  window_modes |= 3;      // Allow at least 640x480 and 800x600 window modes
1461  
1462          VideoInfo *p = VModes;
1463 <        for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1464 <                if (find_visual_for_depth(d))
1465 <                        add_window_modes(p, window_modes, d);
1466 <
1467 <        if (has_vidmode) {
1463 >        if (mode_str) {
1464 >                if (display_type == DIS_WINDOW) {
1465 >                        for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) {
1466 >                                if (find_visual_for_depth(d)) {
1467 >                                        if (default_width > 640 && default_height > 480)
1468 >                                                add_mode(p, 3, 1, d, APPLE_W_640x480, DIS_WINDOW);
1469 >                                        if (default_width > 800 && default_height > 600)
1470 >                                                add_mode(p, 3, 2, d, APPLE_W_800x600, DIS_WINDOW);
1471 >                                        add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM);
1472 >                                }
1473 >                        }
1474 > #ifdef ENABLE_VOSF
1475 >                } else if (display_type == DIS_SCREEN && is_fbdev_dga_mode) {
1476 >                        for (unsigned int d = APPLE_1_BIT; d <= default_mode; d++)
1477 >                                if (find_visual_for_depth(d))
1478 >                                        add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM);
1479 > #endif
1480 >                } else
1481 >                        add_custom_mode(p, display_type, default_width, default_height, default_mode, APPLE_CUSTOM);
1482 >        } else if (window_modes) {
1483 >                for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1484 >                        if (find_visual_for_depth(d))
1485 >                                add_window_modes(p, window_modes, d);
1486 >        } else if (has_vidmode) {
1487                  if (has_mode(640, 480))
1488                          add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1489                  if (has_mode(800, 600))
# Line 1168 | Line 1524 | bool VideoInit(void)
1524                  int apple_id = find_apple_resolution(screen_width, screen_height);
1525                  if (apple_id != -1)
1526                          cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN);
1527 <        }
1527 >        } else if (has_dga && mode_str)
1528 >                cur_mode = find_mode(default_mode, APPLE_CUSTOM, DIS_SCREEN);
1529 >
1530          if (cur_mode == -1) {
1531                  // pick up first windowed mode available
1532                  for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
# Line 1197 | Line 1555 | bool VideoInit(void)
1555          XSetErrorHandler(ignore_errors);
1556   #endif
1557  
1558 +        // Lock down frame buffer
1559 +        XSync(x_display, false);
1560 +        LOCK_FRAME_BUFFER;
1561 +
1562          // Start periodic thread
1563          XSync(x_display, false);
1564          Set_pthread_attr(&redraw_thread_attr, 0);
# Line 1221 | Line 1583 | void VideoExit(void)
1583                  redraw_thread_active = false;
1584          }
1585  
1586 +        // Unlock frame buffer
1587 +        UNLOCK_FRAME_BUFFER;
1588 +        XSync(x_display, false);
1589 +        D(bug(" frame buffer unlocked\n"));
1590 +
1591   #ifdef ENABLE_VOSF
1592          if (use_vosf) {
1593                  // Deinitialize VOSF
# Line 1235 | Line 1602 | void VideoExit(void)
1602                  XFlush(x_display);
1603                  XSync(x_display, false);
1604          }
1605 +
1606 + #ifdef ENABLE_FBDEV_DGA
1607 +        // Close framebuffer device
1608 +        if (fb_dev_fd >= 0) {
1609 +                close(fb_dev_fd);
1610 +                fb_dev_fd = -1;
1611 +        }
1612 + #endif
1613   }
1614  
1615  
# Line 1242 | Line 1617 | void VideoExit(void)
1617   *  Suspend/resume emulator
1618   */
1619  
1245 extern void PauseEmulator(void);
1246 extern void ResumeEmulator(void);
1247
1620   static void suspend_emul(void)
1621   {
1622          if (display_type == DIS_SCREEN) {
# Line 1252 | Line 1624 | static void suspend_emul(void)
1624                  ADBKeyUp(0x36);
1625                  ctrl_down = false;
1626  
1627 <                // Pause MacOS thread
1628 <                PauseEmulator();
1257 <                emul_suspended = true;
1627 >                // Lock frame buffer (this will stop the MacOS thread)
1628 >                LOCK_FRAME_BUFFER;
1629  
1630                  // Save frame buffer
1631                  fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1632                  if (fb_save)
1633 <                        memcpy(fb_save, (void *)screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1633 >                        Mac2Host_memcpy(fb_save, screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1634  
1635                  // Close full screen display
1636 + #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1637   #ifdef ENABLE_XF86_DGA
1638 <                XF86DGADirectVideo(x_display, screen, 0);
1638 >                if (!is_fbdev_dga_mode)
1639 >                        XF86DGADirectVideo(x_display, screen, 0);
1640 > #endif
1641                  XUngrabPointer(x_display, CurrentTime);
1642                  XUngrabKeyboard(x_display, CurrentTime);
1643   #endif
1644 +                restore_mouse_accel();
1645 +                XUnmapWindow(x_display, the_win);
1646 +                wait_unmapped(the_win);
1647                  XSync(x_display, false);
1648  
1649                  // Open "suspend" window
1650                  XSetWindowAttributes wattr;
1651                  wattr.event_mask = KeyPressMask;
1652 <                wattr.background_pixel = black_pixel;
1276 <                wattr.border_pixel = black_pixel;
1652 >                wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
1653                  wattr.backing_store = Always;
1654 <                wattr.backing_planes = xdepth;
1655 <                wattr.colormap = DefaultColormap(x_display, screen);
1280 <                XSync(x_display, false);
1654 >                wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
1655 >
1656                  suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1657 <                        InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1658 <                        CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1659 <                XSync(x_display, false);
1660 <                XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1661 <                XMapRaised(x_display, suspend_win);
1287 <                XSync(x_display, false);
1657 >                                                                        InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore | CWColormap, &wattr);
1658 >                set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
1659 >                set_window_focus(suspend_win);
1660 >                XMapWindow(x_display, suspend_win);
1661 >                emul_suspended = true;
1662          }
1663   }
1664  
# Line 1295 | Line 1669 | static void resume_emul(void)
1669          XSync(x_display, false);
1670  
1671          // Reopen full screen display
1672 <        XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1673 <        XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1672 >        XMapRaised(x_display, the_win);
1673 >        wait_mapped(the_win);
1674 >        XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1675 >        Window w = is_fbdev_dga_mode ? the_win : rootwin;
1676 >        XGrabKeyboard(x_display, w, True, GrabModeAsync, GrabModeAsync, CurrentTime);
1677 >        XGrabPointer(x_display, w, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1678 >        disable_mouse_accel();
1679   #ifdef ENABLE_XF86_DGA
1680 <        XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1681 <        XF86DGASetViewPort(x_display, screen, 0, 0);
1680 >        if (!is_fbdev_dga_mode) {
1681 >                XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1682 >                XF86DGASetViewPort(x_display, screen, 0, 0);
1683 >        }
1684   #endif
1685          XSync(x_display, false);
1686  
# Line 1321 | Line 1702 | static void resume_emul(void)
1702                  // Don't copy fb_save to the temporary frame buffer in VOSF mode
1703                  if (!use_vosf)
1704   #endif
1705 <                memcpy((void *)screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1705 >                Host2Mac_memcpy(screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1706                  free(fb_save);
1707                  fb_save = NULL;
1708          }
1328        if (depth == 8)
1329                palette_changed = true;
1709  
1710 <        // Resume MacOS thread
1710 >        // Unlock frame buffer (and continue MacOS thread)
1711 >        UNLOCK_FRAME_BUFFER;
1712          emul_suspended = false;
1333        ResumeEmulator();
1713   }
1714  
1715  
# Line 1646 | Line 2025 | void VideoVBL(void)
2025          if (emerg_quit)
2026                  QuitEmulator();
2027  
2028 +        // Temporarily give up frame buffer lock (this is the point where
2029 +        // we are suspended when the user presses Ctrl-Tab)
2030 +        UNLOCK_FRAME_BUFFER;
2031 +        LOCK_FRAME_BUFFER;
2032 +
2033          // Execute video VBL
2034          if (private_data != NULL && private_data->interruptsEnabled)
2035                  VSLDoInterruptService(private_data->vslServiceID);
# Line 1653 | Line 2037 | void VideoVBL(void)
2037  
2038  
2039   /*
1656 *  Install graphics acceleration
1657 */
1658
1659 // Rectangle inversion
1660 template< int bpp >
1661 static inline void do_invrect(uint8 *dest, uint32 length)
1662 {
1663 #define INVERT_1(PTR, OFS) ((uint8  *)(PTR))[OFS] = ~((uint8  *)(PTR))[OFS]
1664 #define INVERT_2(PTR, OFS) ((uint16 *)(PTR))[OFS] = ~((uint16 *)(PTR))[OFS]
1665 #define INVERT_4(PTR, OFS) ((uint32 *)(PTR))[OFS] = ~((uint32 *)(PTR))[OFS]
1666 #define INVERT_8(PTR, OFS) ((uint64 *)(PTR))[OFS] = ~((uint64 *)(PTR))[OFS]
1667
1668 #ifndef UNALIGNED_PROFITABLE
1669        // Align on 16-bit boundaries
1670        if (bpp < 16 && (((uintptr)dest) & 1)) {
1671                INVERT_1(dest, 0);
1672                dest += 1; length -= 1;
1673        }
1674
1675        // Align on 32-bit boundaries
1676        if (bpp < 32 && (((uintptr)dest) & 2)) {
1677                INVERT_2(dest, 0);
1678                dest += 2; length -= 2;
1679        }
1680 #endif
1681
1682        // Invert 8-byte words
1683        if (length >= 8) {
1684                const int r = (length / 8) % 8;
1685                dest += r * 8;
1686
1687                int n = ((length / 8) + 7) / 8;
1688                switch (r) {
1689                case 0: do {
1690                                dest += 64;
1691                                INVERT_8(dest, -8);
1692                case 7: INVERT_8(dest, -7);
1693                case 6: INVERT_8(dest, -6);
1694                case 5: INVERT_8(dest, -5);
1695                case 4: INVERT_8(dest, -4);
1696                case 3: INVERT_8(dest, -3);
1697                case 2: INVERT_8(dest, -2);
1698                case 1: INVERT_8(dest, -1);
1699                                } while (--n > 0);
1700                }
1701        }
1702
1703        // 32-bit cell to invert?
1704        if (length & 4) {
1705                INVERT_4(dest, 0);
1706                if (bpp <= 16)
1707                        dest += 4;
1708        }
1709
1710        // 16-bit cell to invert?
1711        if (bpp <= 16 && (length & 2)) {
1712                INVERT_2(dest, 0);
1713                if (bpp <= 8)
1714                        dest += 2;
1715        }
1716
1717        // 8-bit cell to invert?
1718        if (bpp <= 8 && (length & 1))
1719                INVERT_1(dest, 0);
1720
1721 #undef INVERT_1
1722 #undef INVERT_2
1723 #undef INVERT_4
1724 #undef INVERT_8
1725 }
1726
1727 void NQD_invrect(uint32 p)
1728 {
1729        D(bug("accl_invrect %08x\n", p));
1730
1731        // Get inversion parameters
1732        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1733        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1734        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1735        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1736        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1737        D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1738
1739        //!!?? pen_mode == 14
1740
1741        // And perform the inversion
1742        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1743        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1744        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1745        width *= bpp;
1746        switch (bpp) {
1747        case 1:
1748                for (int i = 0; i < height; i++) {
1749                        do_invrect<8>(dest, width);
1750                        dest += dest_row_bytes;
1751                }
1752                break;
1753        case 2:
1754                for (int i = 0; i < height; i++) {
1755                        do_invrect<16>(dest, width);
1756                        dest += dest_row_bytes;
1757                }
1758                break;
1759        case 4:
1760                for (int i = 0; i < height; i++) {
1761                        do_invrect<32>(dest, width);
1762                        dest += dest_row_bytes;
1763                }
1764                break;
1765        }
1766 }
1767
1768 // Rectangle filling
1769 template< int bpp >
1770 static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1771 {
1772 #define FILL_1(PTR, OFS, VAL) ((uint8  *)(PTR))[OFS] = (VAL)
1773 #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1774 #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1775 #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
1776
1777 #ifndef UNALIGNED_PROFITABLE
1778        // Align on 16-bit boundaries
1779        if (bpp < 16 && (((uintptr)dest) & 1)) {
1780                FILL_1(dest, 0, color);
1781                dest += 1; length -= 1;
1782        }
1783
1784        // Align on 32-bit boundaries
1785        if (bpp < 32 && (((uintptr)dest) & 2)) {
1786                FILL_2(dest, 0, color);
1787                dest += 2; length -= 2;
1788        }
1789 #endif
1790
1791        // Fill 8-byte words
1792        if (length >= 8) {
1793                const uint64 c = (((uint64)color) << 32) | color;
1794                const int r = (length / 8) % 8;
1795                dest += r * 8;
1796
1797                int n = ((length / 8) + 7) / 8;
1798                switch (r) {
1799                case 0: do {
1800                                dest += 64;
1801                                FILL_8(dest, -8, c);
1802                case 7: FILL_8(dest, -7, c);
1803                case 6: FILL_8(dest, -6, c);
1804                case 5: FILL_8(dest, -5, c);
1805                case 4: FILL_8(dest, -4, c);
1806                case 3: FILL_8(dest, -3, c);
1807                case 2: FILL_8(dest, -2, c);
1808                case 1: FILL_8(dest, -1, c);
1809                                } while (--n > 0);
1810                }
1811        }
1812
1813        // 32-bit cell to fill?
1814        if (length & 4) {
1815                FILL_4(dest, 0, color);
1816                if (bpp <= 16)
1817                        dest += 4;
1818        }
1819
1820        // 16-bit cell to fill?
1821        if (bpp <= 16 && (length & 2)) {
1822                FILL_2(dest, 0, color);
1823                if (bpp <= 8)
1824                        dest += 2;
1825        }
1826
1827        // 8-bit cell to fill?
1828        if (bpp <= 8 && (length & 1))
1829                FILL_1(dest, 0, color);
1830
1831 #undef FILL_1
1832 #undef FILL_2
1833 #undef FILL_4
1834 #undef FILL_8
1835 }
1836
1837 void NQD_fillrect(uint32 p)
1838 {
1839        D(bug("accl_fillrect %08x\n", p));
1840
1841        // Get filling parameters
1842        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1843        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1844        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1845        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1846        uint32 color = htonl(ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen));
1847        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1848        D(bug(" width %d, height %d\n", width, height));
1849        D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1850
1851        // And perform the fill
1852        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1853        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1854        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1855        width *= bpp;
1856        switch (bpp) {
1857        case 1:
1858                for (int i = 0; i < height; i++) {
1859                        memset(dest, color, width);
1860                        dest += dest_row_bytes;
1861                }
1862                break;
1863        case 2:
1864                for (int i = 0; i < height; i++) {
1865                        do_fillrect<16>(dest, color, width);
1866                        dest += dest_row_bytes;
1867                }
1868                break;
1869        case 4:
1870                for (int i = 0; i < height; i++) {
1871                        do_fillrect<32>(dest, color, width);
1872                        dest += dest_row_bytes;
1873                }
1874                break;
1875        }
1876 }
1877
1878 bool NQD_fillrect_hook(uint32 p)
1879 {
1880        D(bug("accl_fillrect_hook %08x\n", p));
1881
1882        // Check if we can accelerate this fillrect
1883        if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1884                const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1885                if (transfer_mode == 8) {
1886                        // Fill
1887                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1888                        return true;
1889                }
1890                else if (transfer_mode == 10) {
1891                        // Invert
1892                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1893                        return true;
1894                }
1895        }
1896        return false;
1897 }
1898
1899 // Rectangle blitting
1900 // TODO: optimize for VOSF and target pixmap == screen
1901 void NQD_bitblt(uint32 p)
1902 {
1903        D(bug("accl_bitblt %08x\n", p));
1904
1905        // Get blitting parameters
1906        int16 src_X  = (int16)ReadMacInt16(p + acclSrcRect + 2) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 2);
1907        int16 src_Y  = (int16)ReadMacInt16(p + acclSrcRect + 0) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 0);
1908        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1909        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1910        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1911        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1912        D(bug(" src addr %08x, dest addr %08x\n", ReadMacInt32(p + acclSrcBaseAddr), ReadMacInt32(p + acclDestBaseAddr)));
1913        D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1914        D(bug(" width %d, height %d\n", width, height));
1915
1916        // And perform the blit
1917        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1918        width *= bpp;
1919        if ((int32)ReadMacInt32(p + acclSrcRowBytes) > 0) {
1920                const int src_row_bytes = (int32)ReadMacInt32(p + acclSrcRowBytes);
1921                const int dst_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1922                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + (src_Y * src_row_bytes) + (src_X * bpp));
1923                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dst_row_bytes) + (dest_X * bpp));
1924                for (int i = 0; i < height; i++) {
1925                        memmove(dst, src, width);
1926                        src += src_row_bytes;
1927                        dst += dst_row_bytes;
1928                }
1929        }
1930        else {
1931                const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1932                const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1933                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp));
1934                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp));
1935                for (int i = height - 1; i >= 0; i--) {
1936                        memmove(dst, src, width);
1937                        src -= src_row_bytes;
1938                        dst -= dst_row_bytes;
1939                }
1940        }
1941 }
1942
1943 /*
1944  BitBlt transfer modes:
1945  0 : srcCopy
1946  1 : srcOr
1947  2 : srcXor
1948  3 : srcBic
1949  4 : notSrcCopy
1950  5 : notSrcOr
1951  6 : notSrcXor
1952  7 : notSrcBic
1953  32 : blend
1954  33 : addPin
1955  34 : addOver
1956  35 : subPin
1957  36 : transparent
1958  37 : adMax
1959  38 : subOver
1960  39 : adMin
1961  50 : hilite
1962 */
1963
1964 bool NQD_bitblt_hook(uint32 p)
1965 {
1966        D(bug("accl_draw_hook %08x\n", p));
1967
1968        // Check if we can accelerate this bitblt
1969        if (ReadMacInt32(p + 0x018) + ReadMacInt32(p + 0x128) == 0 &&
1970                ReadMacInt32(p + 0x130) == 0 &&
1971                ReadMacInt32(p + acclSrcPixelSize) >= 8 &&
1972                ReadMacInt32(p + acclSrcPixelSize) == ReadMacInt32(p + acclDestPixelSize) &&
1973                (ReadMacInt32(p + acclSrcRowBytes) ^ ReadMacInt32(p + acclDestRowBytes)) >= 0 && // same sign?
1974                ReadMacInt32(p + acclTransferMode) == 0 &&                                                                               // srcCopy?
1975                ReadMacInt32(p + 0x15c) > 0) {
1976
1977                // Yes, set function pointer
1978                WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1979                return true;
1980        }
1981        return false;
1982 }
1983
1984 // Wait for graphics operation to finish
1985 bool NQD_sync_hook(uint32 arg)
1986 {
1987        D(bug("accl_sync_hook %08x\n", arg));
1988        return true;
1989 }
1990
1991 void VideoInstallAccel(void)
1992 {
1993        // Install acceleration hooks
1994        if (PrefsFindBool("gfxaccel")) {
1995                D(bug("Video: Installing acceleration hooks\n"));
1996                uint32 base;
1997
1998                SheepVar bitblt_hook_info(sizeof(accl_hook_info));
1999                base = bitblt_hook_info.addr();
2000                WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK));
2001                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
2002                WriteMacInt32(base + 8, ACCL_BITBLT);
2003                NQDMisc(6, bitblt_hook_info.ptr());
2004
2005                SheepVar fillrect_hook_info(sizeof(accl_hook_info));
2006                base = fillrect_hook_info.addr();
2007                WriteMacInt32(base + 0, NativeTVECT(NATIVE_FILLRECT_HOOK));
2008                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
2009                WriteMacInt32(base + 8, ACCL_FILLRECT);
2010                NQDMisc(6, fillrect_hook_info.ptr());
2011        }
2012 }
2013
2014
2015 /*
2040   *  Change video mode
2041   */
2042  
# Line 2285 | Line 2309 | static void handle_palette_changes(void)
2309                  }
2310  
2311   #ifdef ENABLE_XF86_DGA
2312 <                if (display_type == DIS_SCREEN) {
2312 >                if (display_type == DIS_SCREEN && !is_fbdev_dga_mode) {
2313                          current_dga_cmap ^= 1;
2314                          if (!IsDirectMode(mode) && cmap[current_dga_cmap])
2315                                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
# Line 2330 | Line 2354 | static void *redraw_func(void *arg)
2354                                  quit_full_screen = false;
2355                                  if (display_type == DIS_SCREEN) {
2356                                          XDisplayLock();
2357 + #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2358   #ifdef ENABLE_XF86_DGA
2359 <                                        XF86DGADirectVideo(x_display, screen, 0);
2359 >                                        if (is_fbdev_dga_mode)
2360 >                                                XF86DGADirectVideo(x_display, screen, 0);
2361 > #endif
2362                                          XUngrabPointer(x_display, CurrentTime);
2363                                          XUngrabKeyboard(x_display, CurrentTime);
2364                                          XUnmapWindow(x_display, the_win);
# Line 2371 | Line 2398 | static void *redraw_func(void *arg)
2398                                          // Set new cursor image if it was changed
2399                                          if (hw_mac_cursor_accl && cursor_changed) {
2400                                                  cursor_changed = false;
2401 <                                                memcpy(cursor_image->data, MacCursor + 4, 32);
2402 <                                                memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2401 >                                                uint8 *x_data = (uint8 *)cursor_image->data;
2402 >                                                uint8 *x_mask = (uint8 *)cursor_mask_image->data;
2403 >                                                for (int i = 0; i < 32; i++) {
2404 >                                                        x_mask[i] = MacCursor[4 + i] | MacCursor[36 + i];
2405 >                                                        x_data[i] = MacCursor[4 + i];
2406 >                                                }
2407                                                  XDisplayLock();
2408                                                  XFreeCursor(x_display, mac_cursor);
2409                                                  XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines