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.23 by gbeauche, 2004-05-16T15:48:25Z vs.
Revision 1.39 by gbeauche, 2005-03-28T09:05: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 65 | Line 80 | static int16 mouse_wheel_mode;
80   static int16 mouse_wheel_lines;
81   static bool redraw_thread_active = false;       // Flag: Redraw thread installed
82   static pthread_attr_t redraw_thread_attr;       // Redraw thread attributes
83 + static volatile bool redraw_thread_cancel;      // Flag: Cancel Redraw thread
84   static pthread_t redraw_thread;                         // Redraw thread
85  
86   static bool local_X11;                                          // Flag: X server running on local machine?
# Line 82 | Line 98 | static const bool use_vosf = false;                    //
98  
99   static bool palette_changed = false;            // Flag: Palette changed, redraw thread must update palette
100   static bool ctrl_down = false;                          // Flag: Ctrl key pressed
101 + static bool caps_on = false;                            // Flag: Caps Lock on
102   static bool quit_full_screen = false;           // Flag: DGA close requested from redraw thread
103   static volatile bool quit_full_screen_ack = false;      // Acknowledge for quit_full_screen
104   static bool emerg_quit = false;                         // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
# Line 99 | 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 127 | 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 149 | 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 360 | 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 385 | 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 417 | 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 499 | 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 538 | 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 546 | 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 +        int fb_type = fb_finfo.type;
647 +        const char *fb_type_str = NULL;
648 +        switch (fb_type) {
649 +        case FB_TYPE_PACKED_PIXELS:                     fb_type_str = "Packed Pixels";                  break;
650 +        case FB_TYPE_PLANES:                            fb_type_str = "Non interleaved planes"; break;
651 +        case FB_TYPE_INTERLEAVED_PLANES:        fb_type_str = "Interleaved planes";             break;
652 +        case FB_TYPE_TEXT:                                      fb_type_str = "Text/attributes";                break;
653 +        case FB_TYPE_VGA_PLANES:                        fb_type_str = "EGA/VGA planes";                 break;
654 +        default:                                                        fb_type_str = "<unknown>";                              break;
655 +        }
656 +        D(bug("[fbdev] type: %s\n", fb_type_str));
657 +        int fb_visual = fb_finfo.visual;
658 +        const char *fb_visual_str;
659 +        switch (fb_visual) {
660 +        case FB_VISUAL_MONO01:                          fb_visual_str = "Monochrome 1=Black 0=White";   break;
661 +        case FB_VISUAL_MONO10:                          fb_visual_str = "Monochrome 1=While 0=Black";   break;
662 +        case FB_VISUAL_TRUECOLOR:                       fb_visual_str = "True color";                                   break;
663 +        case FB_VISUAL_PSEUDOCOLOR:                     fb_visual_str = "Pseudo color (like atari)";    break;
664 +        case FB_VISUAL_DIRECTCOLOR:                     fb_visual_str = "Direct color";                                 break;
665 +        case FB_VISUAL_STATIC_PSEUDOCOLOR:      fb_visual_str = "Pseudo color readonly";                break;
666 +        default:                                                        fb_visual_str = "<unknown>";                                    break;
667 +        }
668 +        D(bug("[fbdev] visual: %s\n", fb_visual_str));
669 +
670 +        if (fb_type != FB_TYPE_PACKED_PIXELS) {
671 +                D(bug("[fbdev] type %s not supported\n", fb_type_str));
672 +                return false;
673 +        }
674 +        
675 +        // Map frame buffer
676 +        the_buffer = (uint8 *)mmap(NULL, fb_finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev_fd, 0);
677 +        if (the_buffer == MAP_FAILED) {
678 +                D(bug("[fbdev] Can't mmap /dev/fb0: %s\n", strerror(errno)));
679 +                return false;
680 +        }
681 +
682 +        // Set absolute mouse mode
683 +        ADBSetRelMouseMode(false);
684 +
685 +        // Create window
686 +        XSetWindowAttributes wattr;
687 +        wattr.event_mask = eventmask = dga_eventmask;
688 +        wattr.override_redirect = True;
689 +        wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
690 +        the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
691 +                                                        InputOutput, vis, CWEventMask | CWOverrideRedirect |
692 +                                                        (color_class == DirectColor ? CWColormap : 0), &wattr);
693 +
694 +        // Show window
695 +        XMapRaised(x_display, the_win);
696 +        wait_mapped(the_win);
697 +
698 +        // Grab mouse and keyboard
699 +        XGrabKeyboard(x_display, the_win, True,
700 +                                  GrabModeAsync, GrabModeAsync, CurrentTime);
701 +        XGrabPointer(x_display, the_win, True,
702 +                                 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
703 +                                 GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
704 +        disable_mouse_accel();
705 +
706 +        // Create no_cursor
707 +        mac_cursor = XCreatePixmapCursor(x_display,
708 +                                                                         XCreatePixmap(x_display, the_win, 1, 1, 1),
709 +                                                                         XCreatePixmap(x_display, the_win, 1, 1, 1),
710 +                                                                         &black, &white, 0, 0);
711 +        XDefineCursor(x_display, the_win, mac_cursor);
712 +
713 +        // Init blitting routines
714 +        int bytes_per_row = TrivialBytesPerRow((width + 7) & ~7, DepthModeForPixelDepth(depth));
715 + #if ENABLE_VOSF
716 +        bool native_byte_order;
717 + #ifdef WORDS_BIGENDIAN
718 +        native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
719 + #else
720 +        native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
721 + #endif
722 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
723 +        // Screen_blitter_init() returns TRUE if VOSF is mandatory
724 +        // i.e. the framebuffer update function is not Blit_Copy_Raw
725 +        use_vosf = Screen_blitter_init(visualFormat, native_byte_order, depth);
726 +        
727 +        if (use_vosf) {
728 +          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
729 +          the_host_buffer = the_buffer;
730 +          the_buffer_size = page_extend((height + 2) * bytes_per_row);
731 +          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
732 +          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
733 +          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
734 +        }
735 + #else
736 +        use_vosf = false;
737 + #endif
738 + #endif
739 +
740 +        // Set frame buffer base
741 +        D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
742 +        screen_base = Host2MacAddr(the_buffer);
743 +        VModes[cur_mode].viRowBytes = bytes_per_row;
744 +        return true;
745 + #else
746 +        ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
747 +        return false;
748 + #endif
749 + }
750 +
751   // Open DGA display (!! should use X11 VidMode extensions to set mode)
752   static bool open_dga(int width, int height)
753   {
754 +        if (is_fbdev_dga_mode)
755 +                return false;
756 +
757   #ifdef ENABLE_XF86_DGA
758          // Set relative mouse mode
759          ADBSetRelMouseMode(true);
# Line 612 | Line 817 | static bool open_dga(int width, int heig
817   #if REAL_ADDRESSING || DIRECT_ADDRESSING
818          // Screen_blitter_init() returns TRUE if VOSF is mandatory
819          // i.e. the framebuffer update function is not Blit_Copy_Raw
820 <        use_vosf = Screen_blitter_init(&visualInfo, native_byte_order, depth);
820 >        use_vosf = Screen_blitter_init(visualFormat, native_byte_order, depth);
821          
822          if (use_vosf) {
823            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
# Line 629 | Line 834 | static bool open_dga(int width, int heig
834  
835          // Set frame buffer base
836          D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
837 <        screen_base = (uint32)the_buffer;
837 >        screen_base = Host2MacAddr(the_buffer);
838          VModes[cur_mode].viRowBytes = bytes_per_row;
839          return true;
840   #else
# Line 643 | Line 848 | static bool open_display(void)
848          D(bug("open_display()\n"));
849          const VideoInfo &mode = VModes[cur_mode];
850  
851 +        // Get original mouse acceleration
852 +        XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold);
853 +
854          // Find best available X visual
855          if (!find_visual_for_depth(mode.viAppleMode)) {
856                  ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
857                  return false;
858          }
859  
860 +        // Build up visualFormat structure
861 +        visualFormat.depth = visualInfo.depth;
862 +        visualFormat.Rmask = visualInfo.red_mask;
863 +        visualFormat.Gmask = visualInfo.green_mask;
864 +        visualFormat.Bmask = visualInfo.blue_mask;
865 +
866          // Create color maps
867          if (color_class == PseudoColor || color_class == DirectColor) {
868                  cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
# Line 717 | Line 931 | static bool open_display(void)
931          depth = depth_of_video_mode(mode.viAppleMode);
932  
933          bool display_open = false;
934 <        if (display_type == DIS_SCREEN)
934 >        if (display_type == DIS_SCREEN) {
935                  display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
936 + #ifdef ENABLE_FBDEV_DGA
937 +                // Try to fallback to FBDev DGA mode
938 +                if (!display_open) {
939 +                        is_fbdev_dga_mode = true;
940 +                        display_open = open_fbdev(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
941 +                }
942 + #endif
943 +                
944 +        }
945          else if (display_type == DIS_WINDOW)
946                  display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
947  
# Line 767 | Line 990 | static void close_window(void)
990          XSync(x_display, false);
991   }
992  
993 + // Close FBDev mode
994 + static void close_fbdev(void)
995 + {
996 + #ifdef ENABLE_FBDEV_DGA
997 +        XUngrabPointer(x_display, CurrentTime);
998 +        XUngrabKeyboard(x_display, CurrentTime);
999 +
1000 +        uint8 *fb_base;
1001 +        if (!use_vosf) {
1002 +                // don't free() the screen buffer in driver_base dtor
1003 +                fb_base = the_buffer;
1004 +                the_buffer = NULL;
1005 +        }
1006 + #ifdef ENABLE_VOSF
1007 +        else {
1008 +                // don't free() the screen buffer in driver_base dtor
1009 +                fb_base = the_host_buffer;
1010 +                the_host_buffer = NULL;
1011 +        }
1012 + #endif
1013 +        munmap(fb_base, fb_finfo.smem_len);
1014 + #endif
1015 + }
1016 +
1017   // Close DGA mode
1018   static void close_dga(void)
1019   {
1020 +        if (is_fbdev_dga_mode)
1021 +                return;
1022 +
1023   #ifdef ENABLE_XF86_DGA
1024          XF86DGADirectVideo(x_display, screen, 0);
1025          XUngrabPointer(x_display, CurrentTime);
# Line 795 | Line 1045 | static void close_dga(void)
1045  
1046   static void close_display(void)
1047   {
1048 <        if (display_type == DIS_SCREEN)
1049 <                close_dga();
1048 >        if (display_type == DIS_SCREEN) {
1049 >                if (is_fbdev_dga_mode)
1050 >                        close_fbdev();
1051 >                else
1052 >                        close_dga();
1053 >        }
1054          else if (display_type == DIS_WINDOW)
1055                  close_window();
1056  
# Line 851 | Line 1105 | static void close_display(void)
1105                  }
1106          }
1107   #endif
1108 +
1109 +        // Restore mouse acceleration
1110 +        restore_mouse_accel();
1111   }
1112  
1113  
# Line 882 | Line 1139 | static void keycode_init(void)
1139  
1140                  // Search for server vendor string, then read keycodes
1141                  const char *vendor = ServerVendor(x_display);
1142 +                // Force use of MacX mappings on MacOS X with Apple's X server
1143 +                int dummy;
1144 +                if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy))
1145 +                        vendor = "MacX";
1146                  bool vendor_found = false;
1147                  char line[256];
1148                  while (fgets(line, 255, f)) {
# Line 956 | Line 1217 | static int find_mode(int apple_mode, int
1217          return -1;
1218   }
1219  
1220 + // Add custom video mode
1221 + static void add_custom_mode(VideoInfo *&p, int type, uint32 x, uint32 y, int apple_mode, int apple_id)
1222 + {
1223 +        p->viType = type;
1224 +        p->viXsize = x;
1225 +        p->viYsize = y;
1226 +        p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
1227 +        p->viAppleMode = apple_mode;
1228 +        p->viAppleID = apple_id;
1229 +        p++;
1230 + }
1231 +
1232   // Add mode to list of supported modes
1233   static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
1234   {
1235          if (allow & test) {
1236 <                p->viType = type;
1236 >                uint32 x = 0, y = 0;
1237                  switch (apple_id) {
1238                          case APPLE_W_640x480:
1239                          case APPLE_640x480:
1240 <                                p->viXsize = 640;
1241 <                                p->viYsize = 480;
1240 >                                x = 640;
1241 >                                y = 480;
1242                                  break;
1243                          case APPLE_W_800x600:
1244                          case APPLE_800x600:
1245 <                                p->viXsize = 800;
1246 <                                p->viYsize = 600;
1245 >                                x = 800;
1246 >                                y = 600;
1247                                  break;
1248                          case APPLE_1024x768:
1249 <                                p->viXsize = 1024;
1250 <                                p->viYsize = 768;
1249 >                                x = 1024;
1250 >                                y = 768;
1251                                  break;
1252                          case APPLE_1152x768:
1253 <                                p->viXsize = 1152;
1254 <                                p->viYsize = 768;
1253 >                                x = 1152;
1254 >                                y = 768;
1255                                  break;
1256                          case APPLE_1152x900:
1257 <                                p->viXsize = 1152;
1258 <                                p->viYsize = 900;
1257 >                                x = 1152;
1258 >                                y = 900;
1259                                  break;
1260                          case APPLE_1280x1024:
1261 <                                p->viXsize = 1280;
1262 <                                p->viYsize = 1024;
1261 >                                x = 1280;
1262 >                                y = 1024;
1263                                  break;
1264                          case APPLE_1600x1200:
1265 <                                p->viXsize = 1600;
1266 <                                p->viYsize = 1200;
1265 >                                x = 1600;
1266 >                                y = 1200;
1267                                  break;
1268                  }
1269 <                p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
997 <                p->viAppleMode = apple_mode;
998 <                p->viAppleID = apple_id;
999 <                p++;
1269 >                add_custom_mode(p, type, x, y, apple_mode, apple_id);
1270          }
1271   }
1272  
# Line 1065 | Line 1335 | bool VideoInit(void)
1335   #ifdef ENABLE_XF86_DGA
1336          // DGA available?
1337      int event_base, error_base;
1338 +    is_fbdev_dga_mode = false;
1339      if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
1340                  int dga_flags = 0;
1341                  XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1342                  has_dga = dga_flags & XF86DGADirectPresent;
1343 + #if defined(__linux__)
1344 +                // Check r/w permission on /dev/mem for DGA mode to work
1345 +                if (has_dga && access("/dev/mem", R_OK | W_OK) != 0)
1346 +                        has_dga = false;
1347 + #endif
1348          } else
1349                  has_dga = false;
1350   #endif
# Line 1081 | Line 1357 | bool VideoInit(void)
1357                  XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1358   #endif
1359  
1360 + #ifdef ENABLE_FBDEV_DGA
1361 +        // FBDev available?
1362 +        bool has_fbdev_dga = false;
1363 +        if (local_X11) {
1364 +                if ((fb_dev_fd = open("/dev/fb0", O_RDWR)) > 0) {
1365 +                        if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo) != 0)
1366 +                                close(fb_dev_fd);
1367 +                        else {
1368 +                                has_fbdev_dga = true;
1369 +                                if (!has_dga) {
1370 +                                        // Fallback to FBDev DGA mode if XF86 DGA is not possible
1371 +                                        has_dga = true;
1372 +                                        is_fbdev_dga_mode = true;
1373 +                                }
1374 +                                fb_orig_vinfo = fb_vinfo;
1375 +                                D(bug("Frame buffer device initial resolution: %dx%dx%d\n", fb_vinfo.xres, fb_vinfo.yres, fb_vinfo.bits_per_pixel));
1376 +                        }
1377 +                }
1378 +        }
1379 + #endif
1380 +
1381          // Find black and white colors
1382          XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
1383          XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
# Line 1106 | Line 1403 | bool VideoInit(void)
1403                  break;
1404          }
1405  
1406 +        // Get screen mode from preferences
1407 +        const char *mode_str = PrefsFindString("screen");
1408 +        int default_width = 640, default_height = 480;
1409 +        if (mode_str) {
1410 +                display_type = DIS_INVALID;
1411 +                if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
1412 +                        display_type = DIS_WINDOW;
1413 + #ifdef ENABLE_XF86_DGA
1414 +                else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1415 +                        display_type = DIS_SCREEN;
1416 + #endif
1417 + #ifdef ENABLE_FBDEV_DGA
1418 +                else if (has_fbdev_dga && sscanf(mode_str, "fbdev/%d/%d", &default_width, &default_height) == 2) {
1419 +                        is_fbdev_dga_mode = true;
1420 +                        display_type = DIS_SCREEN;
1421 +                }
1422 + #endif
1423 +                if (display_type == DIS_INVALID) {
1424 +                        D(bug("Invalid screen mode specified, defaulting to old modes selection\n"));
1425 +                        mode_str = NULL;
1426 +                }
1427 +                else {
1428 +                        if (default_width <= 0)
1429 +                                default_width = DisplayWidth(x_display, screen);
1430 +                        else if (default_width > DisplayWidth(x_display, screen))
1431 +                                default_width = DisplayWidth(x_display, screen);
1432 +                        if (default_height <= 0)
1433 +                                default_height = DisplayHeight(x_display, screen);
1434 +                        else if (default_height > DisplayHeight(x_display, screen))
1435 +                                default_height = DisplayHeight(x_display, screen);
1436 +                }
1437 +        }
1438 +
1439          // Construct video mode table
1440          uint32 window_modes = PrefsFindInt32("windowmodes");
1441          uint32 screen_modes = PrefsFindInt32("screenmodes");
1442          if (!has_dga)
1443                  screen_modes = 0;
1444 <        if (window_modes == 0 && screen_modes == 0)
1444 >        if (mode_str)
1445 >                window_modes = screen_modes = 0;
1446 >        else if (window_modes == 0 && screen_modes == 0)
1447                  window_modes |= 3;      // Allow at least 640x480 and 800x600 window modes
1448  
1449          VideoInfo *p = VModes;
1450 <        for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1451 <                if (find_visual_for_depth(d))
1452 <                        add_window_modes(p, window_modes, d);
1453 <
1454 <        if (has_vidmode) {
1450 >        if (mode_str) {
1451 >                if (display_type == DIS_WINDOW) {
1452 >                        for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) {
1453 >                                if (find_visual_for_depth(d)) {
1454 >                                        if (default_width > 640 && default_height > 480)
1455 >                                                add_mode(p, 3, 1, d, APPLE_W_640x480, DIS_WINDOW);
1456 >                                        if (default_width > 800 && default_height > 600)
1457 >                                                add_mode(p, 3, 2, d, APPLE_W_800x600, DIS_WINDOW);
1458 >                                        add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM);
1459 >                                }
1460 >                        }
1461 >                } else
1462 >                        add_custom_mode(p, display_type, default_width, default_height, default_mode, APPLE_CUSTOM);
1463 >        } else if (window_modes) {
1464 >                for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1465 >                        if (find_visual_for_depth(d))
1466 >                                add_window_modes(p, window_modes, d);
1467 >        } else if (has_vidmode) {
1468                  if (has_mode(640, 480))
1469                          add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1470                  if (has_mode(800, 600))
# Line 1160 | Line 1505 | bool VideoInit(void)
1505                  int apple_id = find_apple_resolution(screen_width, screen_height);
1506                  if (apple_id != -1)
1507                          cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN);
1508 +        } else if (has_dga && mode_str)
1509 +                cur_mode = find_mode(default_mode, APPLE_CUSTOM, DIS_SCREEN);
1510 +
1511 +        if (cur_mode == -1) {
1512 +                // pick up first windowed mode available
1513 +                for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
1514 +                        if (p->viType == DIS_WINDOW && p->viAppleMode == default_mode) {
1515 +                                cur_mode = p - VModes;
1516 +                                break;
1517 +                        }
1518 +                }
1519          }
1164        if (cur_mode == -1)
1165                cur_mode = find_mode(default_mode, APPLE_W_640x480, DIS_WINDOW);
1520          assert(cur_mode != -1);
1521  
1522   #if DEBUG
# Line 1182 | Line 1536 | bool VideoInit(void)
1536          XSetErrorHandler(ignore_errors);
1537   #endif
1538  
1539 +        // Lock down frame buffer
1540 +        XSync(x_display, false);
1541 +        LOCK_FRAME_BUFFER;
1542 +
1543          // Start periodic thread
1544          XSync(x_display, false);
1545          Set_pthread_attr(&redraw_thread_attr, 0);
1546 +        redraw_thread_cancel = false;
1547          redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1548          D(bug("Redraw thread installed (%ld)\n", redraw_thread));
1549          return true;
# Line 1199 | Line 1558 | void VideoExit(void)
1558   {
1559          // Stop redraw thread
1560          if (redraw_thread_active) {
1561 +                redraw_thread_cancel = true;
1562                  pthread_cancel(redraw_thread);
1563                  pthread_join(redraw_thread, NULL);
1564                  redraw_thread_active = false;
1565          }
1566  
1567 +        // Unlock frame buffer
1568 +        UNLOCK_FRAME_BUFFER;
1569 +        XSync(x_display, false);
1570 +        D(bug(" frame buffer unlocked\n"));
1571 +
1572   #ifdef ENABLE_VOSF
1573          if (use_vosf) {
1574                  // Deinitialize VOSF
# Line 1218 | Line 1583 | void VideoExit(void)
1583                  XFlush(x_display);
1584                  XSync(x_display, false);
1585          }
1586 +
1587 + #ifdef ENABLE_FBDEV_DGA
1588 +        // Close framebuffer device
1589 +        if (fb_dev_fd >= 0) {
1590 +                close(fb_dev_fd);
1591 +                fb_dev_fd = -1;
1592 +        }
1593 + #endif
1594   }
1595  
1596  
# Line 1225 | Line 1598 | void VideoExit(void)
1598   *  Suspend/resume emulator
1599   */
1600  
1228 extern void PauseEmulator(void);
1229 extern void ResumeEmulator(void);
1230
1601   static void suspend_emul(void)
1602   {
1603          if (display_type == DIS_SCREEN) {
# Line 1235 | Line 1605 | static void suspend_emul(void)
1605                  ADBKeyUp(0x36);
1606                  ctrl_down = false;
1607  
1608 <                // Pause MacOS thread
1609 <                PauseEmulator();
1240 <                emul_suspended = true;
1608 >                // Lock frame buffer (this will stop the MacOS thread)
1609 >                LOCK_FRAME_BUFFER;
1610  
1611                  // Save frame buffer
1612                  fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1613                  if (fb_save)
1614 <                        memcpy(fb_save, (void *)screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1614 >                        Mac2Host_memcpy(fb_save, screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1615  
1616                  // Close full screen display
1617 + #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1618   #ifdef ENABLE_XF86_DGA
1619 <                XF86DGADirectVideo(x_display, screen, 0);
1619 >                if (!is_fbdev_dga_mode)
1620 >                        XF86DGADirectVideo(x_display, screen, 0);
1621 > #endif
1622                  XUngrabPointer(x_display, CurrentTime);
1623                  XUngrabKeyboard(x_display, CurrentTime);
1624   #endif
1625 +                restore_mouse_accel();
1626 +                XUnmapWindow(x_display, the_win);
1627 +                wait_unmapped(the_win);
1628                  XSync(x_display, false);
1629  
1630                  // Open "suspend" window
1631                  XSetWindowAttributes wattr;
1632                  wattr.event_mask = KeyPressMask;
1633 <                wattr.background_pixel = black_pixel;
1259 <                wattr.border_pixel = black_pixel;
1633 >                wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
1634                  wattr.backing_store = Always;
1635 <                wattr.backing_planes = xdepth;
1636 <                wattr.colormap = DefaultColormap(x_display, screen);
1263 <                XSync(x_display, false);
1635 >                wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
1636 >
1637                  suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1638 <                        InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1639 <                        CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1640 <                XSync(x_display, false);
1641 <                XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1642 <                XMapRaised(x_display, suspend_win);
1270 <                XSync(x_display, false);
1638 >                                                                        InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore | CWColormap, &wattr);
1639 >                set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
1640 >                set_window_focus(suspend_win);
1641 >                XMapWindow(x_display, suspend_win);
1642 >                emul_suspended = true;
1643          }
1644   }
1645  
# Line 1278 | Line 1650 | static void resume_emul(void)
1650          XSync(x_display, false);
1651  
1652          // Reopen full screen display
1653 <        XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1654 <        XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1653 >        XMapRaised(x_display, the_win);
1654 >        wait_mapped(the_win);
1655 >        XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1656 >        Window w = is_fbdev_dga_mode ? the_win : rootwin;
1657 >        XGrabKeyboard(x_display, w, True, GrabModeAsync, GrabModeAsync, CurrentTime);
1658 >        XGrabPointer(x_display, w, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1659 >        disable_mouse_accel();
1660   #ifdef ENABLE_XF86_DGA
1661 <        XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1662 <        XF86DGASetViewPort(x_display, screen, 0, 0);
1661 >        if (!is_fbdev_dga_mode) {
1662 >                XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1663 >                XF86DGASetViewPort(x_display, screen, 0, 0);
1664 >        }
1665   #endif
1666          XSync(x_display, false);
1667  
# Line 1304 | Line 1683 | static void resume_emul(void)
1683                  // Don't copy fb_save to the temporary frame buffer in VOSF mode
1684                  if (!use_vosf)
1685   #endif
1686 <                memcpy((void *)screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1686 >                Host2Mac_memcpy(screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1687                  free(fb_save);
1688                  fb_save = NULL;
1689          }
1311        if (depth == 8)
1312                palette_changed = true;
1690  
1691 <        // Resume MacOS thread
1691 >        // Unlock frame buffer (and continue MacOS thread)
1692 >        UNLOCK_FRAME_BUFFER;
1693          emul_suspended = false;
1316        ResumeEmulator();
1694   }
1695  
1696  
# Line 1477 | Line 1854 | static int kc_decode(KeySym ks)
1854          return -1;
1855   }
1856  
1857 < static int event2keycode(XKeyEvent &ev)
1857 > static int event2keycode(XKeyEvent &ev, bool key_down)
1858   {
1859          KeySym ks;
1483        int as;
1860          int i = 0;
1861  
1862          do {
1863                  ks = XLookupKeysym(&ev, i++);
1864 <                as = kc_decode(ks);
1865 <                if (as != -1)
1864 >                int as = kc_decode(ks);
1865 >                if (as >= 0)
1866 >                        return as;
1867 >                if (as == -2)
1868                          return as;
1869          } while (ks != NoSymbol);
1870  
# Line 1561 | Line 1939 | static void handle_events(void)
1939  
1940                          // Keyboard
1941                          case KeyPress: {
1942 <                                int code = event2keycode(event.xkey);
1943 <                                if (use_keycodes && code != -1)
1944 <                                        code = keycode_table[event.xkey.keycode & 0xff];
1945 <                                if (code != -1) {
1942 >                                int code = -1;
1943 >                                if (use_keycodes) {
1944 >                                        if (event2keycode(event.xkey, true) != -2)      // This is called to process the hotkeys
1945 >                                                code = keycode_table[event.xkey.keycode & 0xff];
1946 >                                } else
1947 >                                        code = event2keycode(event.xkey, true);
1948 >                                if (code >= 0) {
1949                                          if (!emul_suspended) {
1950 <                                                ADBKeyDown(code);
1950 >                                                if (code == 0x39) {     // Caps Lock pressed
1951 >                                                        if (caps_on) {
1952 >                                                                ADBKeyUp(code);
1953 >                                                                caps_on = false;
1954 >                                                        } else {
1955 >                                                                ADBKeyDown(code);
1956 >                                                                caps_on = true;
1957 >                                                        }
1958 >                                                } else
1959 >                                                        ADBKeyDown(code);
1960                                                  if (code == 0x36)
1961                                                          ctrl_down = true;
1962                                          } else {
# Line 1577 | Line 1967 | static void handle_events(void)
1967                                  break;
1968                          }
1969                          case KeyRelease: {
1970 <                                int code = event2keycode(event.xkey);
1971 <                                if (use_keycodes && code != 1)
1972 <                                        code = keycode_table[event.xkey.keycode & 0xff];
1973 <                                if (code != -1) {
1970 >                                int code = -1;
1971 >                                if (use_keycodes) {
1972 >                                        if (event2keycode(event.xkey, false) != -2)     // This is called to process the hotkeys
1973 >                                                code = keycode_table[event.xkey.keycode & 0xff];
1974 >                                } else
1975 >                                        code = event2keycode(event.xkey, false);
1976 >                                if (code >= 0 && code != 0x39) {        // Don't propagate Caps Lock releases
1977                                          ADBKeyUp(code);
1978                                          if (code == 0x36)
1979                                                  ctrl_down = false;
# Line 1613 | Line 2006 | void VideoVBL(void)
2006          if (emerg_quit)
2007                  QuitEmulator();
2008  
2009 +        // Temporarily give up frame buffer lock (this is the point where
2010 +        // we are suspended when the user presses Ctrl-Tab)
2011 +        UNLOCK_FRAME_BUFFER;
2012 +        LOCK_FRAME_BUFFER;
2013 +
2014          // Execute video VBL
2015          if (private_data != NULL && private_data->interruptsEnabled)
2016                  VSLDoInterruptService(private_data->vslServiceID);
# Line 1620 | Line 2018 | void VideoVBL(void)
2018  
2019  
2020   /*
1623 *  Install graphics acceleration
1624 */
1625
1626 // Rectangle inversion
1627 template< int bpp >
1628 static inline void do_invrect(uint8 *dest, uint32 length)
1629 {
1630 #define INVERT_1(PTR, OFS) ((uint8  *)(PTR))[OFS] = ~((uint8  *)(PTR))[OFS]
1631 #define INVERT_2(PTR, OFS) ((uint16 *)(PTR))[OFS] = ~((uint16 *)(PTR))[OFS]
1632 #define INVERT_4(PTR, OFS) ((uint32 *)(PTR))[OFS] = ~((uint32 *)(PTR))[OFS]
1633 #define INVERT_8(PTR, OFS) ((uint64 *)(PTR))[OFS] = ~((uint64 *)(PTR))[OFS]
1634
1635 #ifndef UNALIGNED_PROFITABLE
1636        // Align on 16-bit boundaries
1637        if (bpp < 16 && (((uintptr)dest) & 1)) {
1638                INVERT_1(dest, 0);
1639                dest += 1; length -= 1;
1640        }
1641
1642        // Align on 32-bit boundaries
1643        if (bpp < 32 && (((uintptr)dest) & 2)) {
1644                INVERT_2(dest, 0);
1645                dest += 2; length -= 2;
1646        }
1647 #endif
1648
1649        // Invert 8-byte words
1650        if (length >= 8) {
1651                const int r = (length / 8) % 8;
1652                dest += r * 8;
1653
1654                int n = ((length / 8) + 7) / 8;
1655                switch (r) {
1656                case 0: do {
1657                                dest += 64;
1658                                INVERT_8(dest, -8);
1659                case 7: INVERT_8(dest, -7);
1660                case 6: INVERT_8(dest, -6);
1661                case 5: INVERT_8(dest, -5);
1662                case 4: INVERT_8(dest, -4);
1663                case 3: INVERT_8(dest, -3);
1664                case 2: INVERT_8(dest, -2);
1665                case 1: INVERT_8(dest, -1);
1666                                } while (--n > 0);
1667                }
1668        }
1669
1670        // 32-bit cell to invert?
1671        if (length & 4) {
1672                INVERT_4(dest, 0);
1673                if (bpp <= 16)
1674                        dest += 4;
1675        }
1676
1677        // 16-bit cell to invert?
1678        if (bpp <= 16 && (length & 2)) {
1679                INVERT_2(dest, 0);
1680                if (bpp <= 8)
1681                        dest += 2;
1682        }
1683
1684        // 8-bit cell to invert?
1685        if (bpp <= 8 && (length & 1))
1686                INVERT_1(dest, 0);
1687
1688 #undef INVERT_1
1689 #undef INVERT_2
1690 #undef INVERT_4
1691 #undef INVERT_8
1692 }
1693
1694 void NQD_invrect(uint32 p)
1695 {
1696        D(bug("accl_invrect %08x\n", p));
1697
1698        // Get inversion parameters
1699        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1700        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1701        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1702        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1703        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1704        D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1705
1706        //!!?? pen_mode == 14
1707
1708        // And perform the inversion
1709        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1710        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1711        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1712        width *= bpp;
1713        switch (bpp) {
1714        case 1:
1715                for (int i = 0; i < height; i++) {
1716                        do_invrect<8>(dest, width);
1717                        dest += dest_row_bytes;
1718                }
1719                break;
1720        case 2:
1721                for (int i = 0; i < height; i++) {
1722                        do_invrect<16>(dest, width);
1723                        dest += dest_row_bytes;
1724                }
1725                break;
1726        case 4:
1727                for (int i = 0; i < height; i++) {
1728                        do_invrect<32>(dest, width);
1729                        dest += dest_row_bytes;
1730                }
1731                break;
1732        }
1733 }
1734
1735 // Rectangle filling
1736 template< int bpp >
1737 static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1738 {
1739 #define FILL_1(PTR, OFS, VAL) ((uint8  *)(PTR))[OFS] = (VAL)
1740 #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1741 #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1742 #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
1743
1744 #ifndef UNALIGNED_PROFITABLE
1745        // Align on 16-bit boundaries
1746        if (bpp < 16 && (((uintptr)dest) & 1)) {
1747                FILL_1(dest, 0, color);
1748                dest += 1; length -= 1;
1749        }
1750
1751        // Align on 32-bit boundaries
1752        if (bpp < 32 && (((uintptr)dest) & 2)) {
1753                FILL_2(dest, 0, color);
1754                dest += 2; length -= 2;
1755        }
1756 #endif
1757
1758        // Fill 8-byte words
1759        if (length >= 8) {
1760                const uint64 c = (((uint64)color) << 32) | color;
1761                const int r = (length / 8) % 8;
1762                dest += r * 8;
1763
1764                int n = ((length / 8) + 7) / 8;
1765                switch (r) {
1766                case 0: do {
1767                                dest += 64;
1768                                FILL_8(dest, -8, c);
1769                case 7: FILL_8(dest, -7, c);
1770                case 6: FILL_8(dest, -6, c);
1771                case 5: FILL_8(dest, -5, c);
1772                case 4: FILL_8(dest, -4, c);
1773                case 3: FILL_8(dest, -3, c);
1774                case 2: FILL_8(dest, -2, c);
1775                case 1: FILL_8(dest, -1, c);
1776                                } while (--n > 0);
1777                }
1778        }
1779
1780        // 32-bit cell to fill?
1781        if (length & 4) {
1782                FILL_4(dest, 0, color);
1783                if (bpp <= 16)
1784                        dest += 4;
1785        }
1786
1787        // 16-bit cell to fill?
1788        if (bpp <= 16 && (length & 2)) {
1789                FILL_2(dest, 0, color);
1790                if (bpp <= 8)
1791                        dest += 2;
1792        }
1793
1794        // 8-bit cell to fill?
1795        if (bpp <= 8 && (length & 1))
1796                FILL_1(dest, 0, color);
1797
1798 #undef FILL_1
1799 #undef FILL_2
1800 #undef FILL_4
1801 #undef FILL_8
1802 }
1803
1804 void NQD_fillrect(uint32 p)
1805 {
1806        D(bug("accl_fillrect %08x\n", p));
1807
1808        // Get filling parameters
1809        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1810        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1811        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1812        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1813        uint32 color = ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen);
1814        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1815        D(bug(" width %d, height %d\n", width, height));
1816        D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1817
1818        // And perform the fill
1819        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1820        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1821        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1822        width *= bpp;
1823        switch (bpp) {
1824        case 1:
1825                for (int i = 0; i < height; i++) {
1826                        memset(dest, color, width);
1827                        dest += dest_row_bytes;
1828                }
1829                break;
1830        case 2:
1831                for (int i = 0; i < height; i++) {
1832                        do_fillrect<16>(dest, color, width);
1833                        dest += dest_row_bytes;
1834                }
1835                break;
1836        case 4:
1837                for (int i = 0; i < height; i++) {
1838                        do_fillrect<32>(dest, color, width);
1839                        dest += dest_row_bytes;
1840                }
1841                break;
1842        }
1843 }
1844
1845 bool NQD_fillrect_hook(uint32 p)
1846 {
1847        D(bug("accl_fillrect_hook %08x\n", p));
1848
1849        // Check if we can accelerate this fillrect
1850        if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1851                const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1852                if (transfer_mode == 8) {
1853                        // Fill
1854                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1855                        return true;
1856                }
1857                else if (transfer_mode == 10) {
1858                        // Invert
1859                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1860                        return true;
1861                }
1862        }
1863        return false;
1864 }
1865
1866 // Rectangle blitting
1867 // TODO: optimize for VOSF and target pixmap == screen
1868 void NQD_bitblt(uint32 p)
1869 {
1870        D(bug("accl_bitblt %08x\n", p));
1871
1872        // Get blitting parameters
1873        int16 src_X  = (int16)ReadMacInt16(p + acclSrcRect + 2) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 2);
1874        int16 src_Y  = (int16)ReadMacInt16(p + acclSrcRect + 0) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 0);
1875        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1876        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1877        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1878        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1879        D(bug(" src addr %08x, dest addr %08x\n", ReadMacInt32(p + acclSrcBaseAddr), ReadMacInt32(p + acclDestBaseAddr)));
1880        D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1881        D(bug(" width %d, height %d\n", width, height));
1882
1883        // And perform the blit
1884        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1885        width *= bpp;
1886        if ((int32)ReadMacInt32(p + acclSrcRowBytes) > 0) {
1887                const int src_row_bytes = (int32)ReadMacInt32(p + acclSrcRowBytes);
1888                const int dst_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1889                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + (src_Y * src_row_bytes) + (src_X * bpp));
1890                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dst_row_bytes) + (dest_X * bpp));
1891                for (int i = 0; i < height; i++) {
1892                        memmove(dst, src, width);
1893                        src += src_row_bytes;
1894                        dst += dst_row_bytes;
1895                }
1896        }
1897        else {
1898                const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1899                const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1900                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp));
1901                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp));
1902                for (int i = height - 1; i >= 0; i--) {
1903                        memmove(dst, src, width);
1904                        src -= src_row_bytes;
1905                        dst -= dst_row_bytes;
1906                }
1907        }
1908 }
1909
1910 /*
1911  BitBlt transfer modes:
1912  0 : srcCopy
1913  1 : srcOr
1914  2 : srcXor
1915  3 : srcBic
1916  4 : notSrcCopy
1917  5 : notSrcOr
1918  6 : notSrcXor
1919  7 : notSrcBic
1920  32 : blend
1921  33 : addPin
1922  34 : addOver
1923  35 : subPin
1924  36 : transparent
1925  37 : adMax
1926  38 : subOver
1927  39 : adMin
1928  50 : hilite
1929 */
1930
1931 bool NQD_bitblt_hook(uint32 p)
1932 {
1933        D(bug("accl_draw_hook %08x\n", p));
1934
1935        // Check if we can accelerate this bitblt
1936        if (ReadMacInt32(p + 0x018) + ReadMacInt32(p + 0x128) == 0 &&
1937                ReadMacInt32(p + 0x130) == 0 &&
1938                ReadMacInt32(p + acclSrcPixelSize) >= 8 &&
1939                ReadMacInt32(p + acclSrcPixelSize) == ReadMacInt32(p + acclDestPixelSize) &&
1940                (ReadMacInt32(p + acclSrcRowBytes) ^ ReadMacInt32(p + acclDestRowBytes)) >= 0 && // same sign?
1941                ReadMacInt32(p + acclTransferMode) == 0 &&                                                                               // srcCopy?
1942                ReadMacInt32(p + 0x15c) > 0) {
1943
1944                // Yes, set function pointer
1945                WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1946                return true;
1947        }
1948        return false;
1949 }
1950
1951 // Wait for graphics operation to finish
1952 bool NQD_sync_hook(uint32 arg)
1953 {
1954        D(bug("accl_sync_hook %08x\n", arg));
1955        return true;
1956 }
1957
1958 void VideoInstallAccel(void)
1959 {
1960        // Temporary hack until it's fixed for e.g. little-endian & 64-bit platforms
1961 #ifndef __powerpc__
1962        return;
1963 #endif
1964
1965        // Install acceleration hooks
1966        if (PrefsFindBool("gfxaccel")) {
1967                D(bug("Video: Installing acceleration hooks\n"));
1968                uint32 base;
1969
1970                SheepVar bitblt_hook_info(sizeof(accl_hook_info));
1971                base = bitblt_hook_info.addr();
1972                WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK));
1973                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1974                WriteMacInt32(base + 8, ACCL_BITBLT);
1975                NQDMisc(6, bitblt_hook_info.ptr());
1976
1977                SheepVar fillrect_hook_info(sizeof(accl_hook_info));
1978                base = fillrect_hook_info.addr();
1979                WriteMacInt32(base + 0, NativeTVECT(NATIVE_FILLRECT_HOOK));
1980                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1981                WriteMacInt32(base + 8, ACCL_FILLRECT);
1982                NQDMisc(6, fillrect_hook_info.ptr());
1983        }
1984 }
1985
1986
1987 /*
2021   *  Change video mode
2022   */
2023  
# Line 2257 | Line 2290 | static void handle_palette_changes(void)
2290                  }
2291  
2292   #ifdef ENABLE_XF86_DGA
2293 <                if (display_type == DIS_SCREEN) {
2293 >                if (display_type == DIS_SCREEN && !is_fbdev_dga_mode) {
2294                          current_dga_cmap ^= 1;
2295                          if (!IsDirectMode(mode) && cmap[current_dga_cmap])
2296                                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
# Line 2276 | Line 2309 | static void *redraw_func(void *arg)
2309          int64 ticks = 0;
2310          uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2311  
2312 <        for (;;) {
2312 >        while (!redraw_thread_cancel) {
2313  
2314                  // Pause if requested (during video mode switches)
2315                  while (thread_stop_req)
# Line 2302 | Line 2335 | static void *redraw_func(void *arg)
2335                                  quit_full_screen = false;
2336                                  if (display_type == DIS_SCREEN) {
2337                                          XDisplayLock();
2338 + #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2339   #ifdef ENABLE_XF86_DGA
2340 <                                        XF86DGADirectVideo(x_display, screen, 0);
2340 >                                        if (is_fbdev_dga_mode)
2341 >                                                XF86DGADirectVideo(x_display, screen, 0);
2342 > #endif
2343                                          XUngrabPointer(x_display, CurrentTime);
2344                                          XUngrabKeyboard(x_display, CurrentTime);
2345                                          XUnmapWindow(x_display, the_win);
# Line 2343 | Line 2379 | static void *redraw_func(void *arg)
2379                                          // Set new cursor image if it was changed
2380                                          if (hw_mac_cursor_accl && cursor_changed) {
2381                                                  cursor_changed = false;
2382 <                                                memcpy(cursor_image->data, MacCursor + 4, 32);
2383 <                                                memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2382 >                                                uint8 *x_data = (uint8 *)cursor_image->data;
2383 >                                                uint8 *x_mask = (uint8 *)cursor_mask_image->data;
2384 >                                                for (int i = 0; i < 32; i++) {
2385 >                                                        x_mask[i] = MacCursor[4 + i] | MacCursor[36 + i];
2386 >                                                        x_data[i] = MacCursor[4 + i];
2387 >                                                }
2388                                                  XDisplayLock();
2389                                                  XFreeCursor(x_display, mac_cursor);
2390                                                  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