ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/SDL/video_sdl.cpp
(Generate patch)

Comparing BasiliskII/src/SDL/video_sdl.cpp (file contents):
Revision 1.1 by gbeauche, 2004-06-23T13:47:20Z vs.
Revision 1.10 by gbeauche, 2004-06-29T21:50:22Z

# Line 32 | Line 32
32   *  - Force relative mode in Grab mode even if SDL provides absolute coordinates?
33   *  - Fullscreen mode
34   *  - Gamma tables support is likely to be broken here
35 + *  - Events processing is bound to the general emulation thread as SDL requires
36 + *    to PumpEvents() within the same thread as the one that called SetVideoMode().
37 + *    Besides, there can't seem to be a way to call SetVideoMode() from a child thread.
38 + *  - Refresh performance is still slow. Use SDL_CreateRGBSurface()?
39 + *  - Backport hw cursor acceleration to Basilisk II?
40 + *  - Move generic Native QuickDraw acceleration routines to gfxaccel.cpp
41   */
42  
43   #include "sysdeps.h"
# Line 40 | Line 46
46   #include <SDL_mutex.h>
47   #include <SDL_thread.h>
48   #include <errno.h>
49 + #include <vector>
50  
51   #include "cpu_emulation.h"
52   #include "main.h"
# Line 48 | Line 55
55   #include "prefs.h"
56   #include "user_strings.h"
57   #include "video.h"
58 + #include "video_defs.h"
59   #include "video_blit.h"
60  
61   #define DEBUG 0
# Line 55 | Line 63
63  
64  
65   // Supported video modes
66 < static vector<video_mode> VideoModes;
66 > using std::vector;
67 > static vector<VIDEO_MODE> VideoModes;
68  
69   // Display types
70 + #ifdef SHEEPSHAVER
71   enum {
72 <        DISPLAY_WINDOW, // windowed display
73 <        DISPLAY_SCREEN  // fullscreen display
72 >        DISPLAY_WINDOW = DIS_WINDOW,                                    // windowed display
73 >        DISPLAY_SCREEN = DIS_SCREEN                                             // fullscreen display
74   };
75 + extern int display_type;                                                        // See enum above
76 + #else
77 + enum {
78 +        DISPLAY_WINDOW,                                                                 // windowed display
79 +        DISPLAY_SCREEN                                                                  // fullscreen display
80 + };
81 + static int display_type = DISPLAY_WINDOW;                       // See enum above
82 + #endif
83  
84   // Constants
85   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
# Line 72 | Line 90 | static int32 frame_skip;                                                       // Prefs
90   static int16 mouse_wheel_mode;
91   static int16 mouse_wheel_lines;
92  
75 static int display_type = DISPLAY_WINDOW;                       // See enum above
93   static uint8 *the_buffer = NULL;                                        // Mac frame buffer (where MacOS draws into)
94   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer (for refreshed modes)
95   static uint32 the_buffer_size;                                          // Size of allocated the_buffer
# Line 82 | Line 99 | static volatile bool redraw_thread_cance
99   static SDL_Thread *redraw_thread = NULL;                        // Redraw thread
100  
101   #ifdef ENABLE_VOSF
102 < static bool use_vosf = true;                                            // Flag: VOSF enabled
102 > static bool use_vosf = false;                                           // Flag: VOSF enabled
103   #else
104   static const bool use_vosf = false;                                     // VOSF not possible
105   #endif
# Line 100 | Line 117 | static int keycode_table[256];                                         // X
117  
118   // SDL variables
119   static int screen_depth;                                                        // Depth of current screen
120 + static SDL_Cursor *sdl_cursor;                                          // Copy of Mac cursor
121 + static volatile bool cursor_changed = false;            // Flag: cursor changed, redraw_func must update the cursor
122   static SDL_Color sdl_palette[256];                                      // Color palette to be used as CLUT and gamma table
123   static bool sdl_palette_changed = false;                        // Flag: Palette changed, redraw thread must set new colors
124 + static const int sdl_eventmask = SDL_MOUSEBUTTONDOWNMASK | SDL_MOUSEBUTTONUPMASK | SDL_MOUSEMOTIONMASK | SDL_KEYUPMASK | SDL_KEYDOWNMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK;
125  
126   // Mutex to protect palette
127   static SDL_mutex *sdl_palette_lock = NULL;
# Line 126 | Line 146 | extern void SysMountFirstFloppy(void);
146  
147  
148   /*
149 + *  SheepShaver glue
150 + */
151 +
152 + #ifdef SHEEPSHAVER
153 + // Color depth modes type
154 + typedef int video_depth;
155 +
156 + // 1, 2, 4 and 8 bit depths use a color palette
157 + static inline bool IsDirectMode(VIDEO_MODE const & mode)
158 + {
159 +        return IsDirectMode(mode.viAppleMode);
160 + }
161 +
162 + // Abstract base class representing one (possibly virtual) monitor
163 + // ("monitor" = rectangular display with a contiguous frame buffer)
164 + class monitor_desc {
165 + public:
166 +        monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) {}
167 +        virtual ~monitor_desc() {}
168 +
169 +        // Get current Mac frame buffer base address
170 +        uint32 get_mac_frame_base(void) const {return screen_base;}
171 +
172 +        // Set Mac frame buffer base address (called from switch_to_mode())
173 +        void set_mac_frame_base(uint32 base) {screen_base = base;}
174 +
175 +        // Get current video mode
176 +        const VIDEO_MODE &get_current_mode(void) const {return VModes[cur_mode];}
177 +
178 +        // Called by the video driver to switch the video mode on this display
179 +        // (must call set_mac_frame_base())
180 +        virtual void switch_to_current_mode(void) = 0;
181 +
182 +        // Called by the video driver to set the color palette (in indexed modes)
183 +        // or the gamma table (in direct modes)
184 +        virtual void set_palette(uint8 *pal, int num) = 0;
185 + };
186 +
187 + // Vector of pointers to available monitor descriptions, filled by VideoInit()
188 + static vector<monitor_desc *> VideoMonitors;
189 +
190 + // Find Apple mode matching best specified dimensions
191 + static int find_apple_resolution(int xsize, int ysize)
192 + {
193 +        int apple_id;
194 +        if (xsize < 800)
195 +                apple_id = APPLE_640x480;
196 +        else if (xsize < 1024)
197 +                apple_id = APPLE_800x600;
198 +        else if (xsize < 1152)
199 +                apple_id = APPLE_1024x768;
200 +        else if (xsize < 1280) {
201 +                if (ysize < 900)
202 +                        apple_id = APPLE_1152x768;
203 +                else
204 +                        apple_id = APPLE_1152x900;
205 +        }
206 +        else if (xsize < 1600)
207 +                apple_id = APPLE_1280x1024;
208 +        else
209 +                apple_id = APPLE_1600x1200;
210 +        return apple_id;
211 + }
212 +
213 + // Set parameters to specified Apple mode
214 + static void set_apple_resolution(int apple_id, int &xsize, int &ysize)
215 + {
216 +        switch (apple_id) {
217 +        case APPLE_640x480:
218 +                xsize = 640;
219 +                ysize = 480;
220 +                break;
221 +        case APPLE_800x600:
222 +                xsize = 800;
223 +                ysize = 600;
224 +                break;
225 +        case APPLE_1024x768:
226 +                xsize = 1024;
227 +                ysize = 768;
228 +                break;
229 +        case APPLE_1152x768:
230 +                xsize = 1152;
231 +                ysize = 768;
232 +                break;
233 +        case APPLE_1152x900:
234 +                xsize = 1152;
235 +                ysize = 900;
236 +                break;
237 +        case APPLE_1280x1024:
238 +                xsize = 1280;
239 +                ysize = 1024;
240 +                break;
241 +        case APPLE_1600x1200:
242 +                xsize = 1600;
243 +                ysize = 1200;
244 +                break;
245 +        default:
246 +                abort();
247 +        }
248 + }
249 +
250 + // Match Apple mode matching best specified dimensions
251 + static int match_apple_resolution(int &xsize, int &ysize)
252 + {
253 +        int apple_id = find_apple_resolution(xsize, ysize);
254 +        set_apple_resolution(apple_id, xsize, ysize);
255 +        return apple_id;
256 + }
257 +
258 + // Display error alert
259 + static void ErrorAlert(int error)
260 + {
261 +        ErrorAlert(GetString(error));
262 + }
263 +
264 + // Display warning alert
265 + static void WarningAlert(int warning)
266 + {
267 +        WarningAlert(GetString(warning));
268 + }
269 + #endif
270 +
271 +
272 + /*
273   *  monitor_desc subclass for SDL display
274   */
275  
276   class SDL_monitor_desc : public monitor_desc {
277   public:
278 <        SDL_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
278 >        SDL_monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
279          ~SDL_monitor_desc() {}
280  
281          virtual void switch_to_current_mode(void);
# Line 146 | Line 290 | public:
290   *  Utility functions
291   */
292  
293 + // Find palette size for given color depth
294 + static int palette_size(int mode)
295 + {
296 +        switch (mode) {
297 +        case VIDEO_DEPTH_1BIT: return 2;
298 +        case VIDEO_DEPTH_2BIT: return 4;
299 +        case VIDEO_DEPTH_4BIT: return 16;
300 +        case VIDEO_DEPTH_8BIT: return 256;
301 +        case VIDEO_DEPTH_16BIT: return 32;
302 +        case VIDEO_DEPTH_32BIT: return 256;
303 +        default: return 0;
304 +        }
305 + }
306 +
307 + // Return bytes per pixel for requested depth
308 + static inline int bytes_per_pixel(int depth)
309 + {
310 +        int bpp;
311 +        switch (depth) {
312 +        case 8:
313 +                bpp = 1;
314 +                break;
315 +        case 15: case 16:
316 +                bpp = 2;
317 +                break;
318 +        case 24: case 32:
319 +                bpp = 4;
320 +                break;
321 +        default:
322 +                abort();
323 +        }
324 +        return bpp;
325 + }
326 +
327   // Map video_mode depth ID to numerical depth value
328   static int sdl_depth_of_video_depth(int video_depth)
329   {
330          int depth = -1;
331          switch (video_depth) {
332 <        case VDEPTH_1BIT:
332 >        case VIDEO_DEPTH_1BIT:
333                  depth = 1;
334                  break;
335 <        case VDEPTH_2BIT:
335 >        case VIDEO_DEPTH_2BIT:
336                  depth = 2;
337                  break;
338 <        case VDEPTH_4BIT:
338 >        case VIDEO_DEPTH_4BIT:
339                  depth = 4;
340                  break;
341 <        case VDEPTH_8BIT:
341 >        case VIDEO_DEPTH_8BIT:
342                  depth = 8;
343                  break;
344 <        case VDEPTH_16BIT:
344 >        case VIDEO_DEPTH_16BIT:
345                  depth = 16;
346                  break;
347 <        case VDEPTH_32BIT:
347 >        case VIDEO_DEPTH_32BIT:
348                  depth = 32;
349                  break;
350          default:
# Line 175 | Line 353 | static int sdl_depth_of_video_depth(int
353          return depth;
354   }
355  
356 + // Check wether specified mode is available
357 + static bool has_mode(int type, int width, int height)
358 + {
359 +        // FIXME: no fullscreen support yet
360 +        if (type == DISPLAY_SCREEN)
361 +                return false;
362 +
363 + #ifdef SHEEPSHAVER
364 +        // Filter out Classic resolutiosn
365 +        if (width == 512 && height == 384)
366 +                return false;
367 +
368 +        // Read window modes prefs
369 +        static uint32 window_modes = 0;
370 +        static uint32 screen_modes = 0;
371 +        if (window_modes == 0 || screen_modes == 0) {
372 +                window_modes = PrefsFindInt32("windowmodes");
373 +                screen_modes = PrefsFindInt32("screenmodes");
374 +                if (window_modes == 0 || screen_modes == 0)
375 +                        window_modes |= 3;                      // Allow at least 640x480 and 800x600 window modes
376 +        }
377 +
378 +        if (type == DISPLAY_WINDOW) {
379 +                int apple_mask, apple_id = find_apple_resolution(width, height);
380 +                switch (apple_id) {
381 +                case APPLE_640x480:             apple_mask = 0x01; break;
382 +                case APPLE_800x600:             apple_mask = 0x02; break;
383 +                case APPLE_1024x768:    apple_mask = 0x04; break;
384 +                case APPLE_1152x768:    apple_mask = 0x40; break;
385 +                case APPLE_1152x900:    apple_mask = 0x08; break;
386 +                case APPLE_1280x1024:   apple_mask = 0x10; break;
387 +                case APPLE_1600x1200:   apple_mask = 0x20; break;
388 +                default:                                apple_mask = 0x00; break;
389 +                }
390 +                return (window_modes & apple_mask);
391 +        }
392 + #else
393 +        return true;
394 + #endif
395 +        return false;
396 + }
397 +
398   // Add mode to list of supported modes
399 < static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth)
399 > static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
400   {
401 <        video_mode mode;
402 <        mode.x = width;
403 <        mode.y = height;
404 <        mode.resolution_id = resolution_id;
405 <        mode.bytes_per_row = bytes_per_row;
406 <        mode.depth = depth;
401 >        // Filter out unsupported modes
402 >        if (!has_mode(type, width, height))
403 >                return;
404 >
405 >        // Fill in VideoMode entry
406 >        VIDEO_MODE mode;
407 > #ifdef SHEEPSHAVER
408 >        // Recalculate dimensions to fit Apple modes
409 >        resolution_id = match_apple_resolution(width, height);
410 >        mode.viType = type;
411 > #endif
412 >        VIDEO_MODE_X = width;
413 >        VIDEO_MODE_Y = height;
414 >        VIDEO_MODE_RESOLUTION = resolution_id;
415 >        VIDEO_MODE_ROW_BYTES = bytes_per_row;
416 >        VIDEO_MODE_DEPTH = (video_depth)depth;
417          VideoModes.push_back(mode);
418   }
419  
420   // Add standard list of windowed modes for given color depth
421 < static void add_window_modes(video_depth depth)
421 > static void add_window_modes(int depth)
422   {
423 <        add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth);
424 <        add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth);
425 <        add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth);
426 <        add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth), depth);
427 <        add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth);
428 <        add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth);
429 <        add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth);
423 >        video_depth vdepth = (video_depth)depth;
424 >        add_mode(DISPLAY_WINDOW, 512, 384, 0x80, TrivialBytesPerRow(512, vdepth), depth);
425 >        add_mode(DISPLAY_WINDOW, 640, 480, 0x81, TrivialBytesPerRow(640, vdepth), depth);
426 >        add_mode(DISPLAY_WINDOW, 800, 600, 0x82, TrivialBytesPerRow(800, vdepth), depth);
427 >        add_mode(DISPLAY_WINDOW, 1024, 768, 0x83, TrivialBytesPerRow(1024, vdepth), depth);
428 >        add_mode(DISPLAY_WINDOW, 1152, 870, 0x84, TrivialBytesPerRow(1152, vdepth), depth);
429 >        add_mode(DISPLAY_WINDOW, 1280, 1024, 0x85, TrivialBytesPerRow(1280, vdepth), depth);
430 >        add_mode(DISPLAY_WINDOW, 1600, 1200, 0x86, TrivialBytesPerRow(1600, vdepth), depth);
431   }
432  
433   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
434 < static void set_mac_frame_buffer(SDL_monitor_desc &monitor, video_depth depth, bool native_byte_order)
434 > static void set_mac_frame_buffer(SDL_monitor_desc &monitor, int depth, bool native_byte_order)
435   {
436   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
437          int layout = FLAYOUT_DIRECT;
438 <        if (depth == VDEPTH_16BIT)
438 >        if (depth == VIDEO_DEPTH_16BIT)
439                  layout = (screen_depth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
440 <        else if (depth == VDEPTH_32BIT)
440 >        else if (depth == VIDEO_DEPTH_32BIT)
441                  layout = (screen_depth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
442          if (native_byte_order)
443                  MacFrameLayout = layout;
# Line 215 | Line 446 | static void set_mac_frame_buffer(SDL_mon
446          monitor.set_mac_frame_base(MacFrameBaseMac);
447  
448          // Set variables used by UAE memory banking
449 <        const video_mode &mode = monitor.get_current_mode();
449 >        const VIDEO_MODE &mode = monitor.get_current_mode();
450          MacFrameBaseHost = the_buffer;
451 <        MacFrameSize = mode.bytes_per_row * mode.y;
451 >        MacFrameSize = VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y;
452          InitFrameBufferMapping();
453   #else
454          monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
# Line 266 | Line 497 | public:
497  
498   public:
499          SDL_monitor_desc &monitor; // Associated video monitor
500 <        const video_mode &mode;    // Video mode handled by the driver
500 >        const VIDEO_MODE &mode;    // Video mode handled by the driver
501  
502          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
503          SDL_Surface *s; // The surface we draw into
# Line 354 | Line 585 | driver_base::~driver_base()
585   // Palette has changed
586   void driver_base::update_palette(void)
587   {
588 <        const video_mode &mode = monitor.get_current_mode();
588 >        const VIDEO_MODE &mode = monitor.get_current_mode();
589  
590 <        if (mode.depth <= VDEPTH_8BIT)
590 >        if ((int)VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT)
591                  SDL_SetPalette(s, SDL_PHYSPAL, sdl_palette, 0, 256);
592   }
593  
# Line 379 | Line 610 | void driver_base::restore_mouse_accel(vo
610   driver_window::driver_window(SDL_monitor_desc &m)
611          : driver_base(m), mouse_grabbed(false)
612   {
613 <        int width = mode.x, height = mode.y;
613 >        int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
614          int aligned_width = (width + 15) & ~15;
615          int aligned_height = (height + 15) & ~15;
616  
# Line 387 | Line 618 | driver_window::driver_window(SDL_monitor
618          ADBSetRelMouseMode(mouse_grabbed);
619  
620          // Create surface
621 <        int depth = (mode.depth <= VDEPTH_8BIT ? 8 : screen_depth);
621 >        int depth = ((int)VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT ? 8 : screen_depth);
622          if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL)
623                  return;
624  
# Line 399 | Line 630 | driver_window::driver_window(SDL_monitor
630          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
631          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
632          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
633 +
634 +        // Check whether we can initialize the VOSF subsystem and it's profitable
635 +        if (!video_vosf_init(m)) {
636 +                WarningAlert(STR_VOSF_INIT_ERR);
637 +                use_vosf = false;
638 +        }
639 +        else if (!video_vosf_profitable()) {
640 +                video_vosf_exit();
641 +                printf("VOSF acceleration is not profitable on this platform, disabling it\n");
642 +                use_vosf = false;
643 +        }
644 +        if (!use_vosf) {
645 +                free(the_buffer_copy);
646 +                vm_release(the_buffer, the_buffer_size);
647 +                the_host_buffer = NULL;
648 +        }
649 + #endif
650 +        if (!use_vosf) {
651 +                // Allocate memory for frame buffer
652 +                the_buffer_size = (aligned_height + 2) * s->pitch;
653 +                the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
654 +                the_buffer = (uint8 *)calloc(1, the_buffer_size);
655 +                D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
656 +        }
657 +        
658 + #ifdef SHEEPSHAVER
659 +        // Create cursor
660 +        if ((sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, 0, 0)) != NULL) {
661 +                SDL_SetCursor(sdl_cursor);
662 +                cursor_changed = false;
663 +        }
664   #else
665 <        // Allocate memory for frame buffer
666 <        the_buffer_size = (aligned_height + 2) * s->pitch;
405 <        the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
406 <        the_buffer = (uint8 *)calloc(1, the_buffer_size);
407 <        D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
665 >        // Hide cursor
666 >        SDL_ShowCursor(0);
667   #endif
668  
669          // Set window name/class
670          set_window_name(STR_WINDOW_TITLE);
671  
413        // Hide cursor
414        SDL_ShowCursor(0);
415
672          // Init blitting routines
673          SDL_PixelFormat *f = s->format;
674          VisualFormat visualFormat;
# Line 420 | Line 676 | driver_window::driver_window(SDL_monitor
676          visualFormat.Rmask = f->Rmask;
677          visualFormat.Gmask = f->Gmask;
678          visualFormat.Bmask = f->Bmask;
679 <        Screen_blitter_init(visualFormat, true, sdl_depth_of_video_depth(mode.depth));
679 >        Screen_blitter_init(visualFormat, true, sdl_depth_of_video_depth(VIDEO_MODE_DEPTH));
680  
681          // Load gray ramp to 8->16/32 expand map
682          if (!IsDirectMode(mode))
# Line 428 | Line 684 | driver_window::driver_window(SDL_monitor
684                          ExpandMap[i] = SDL_MapRGB(f, i, i, i);
685  
686          // Set frame buffer base
687 <        set_mac_frame_buffer(monitor, mode.depth, true);
687 >        set_mac_frame_buffer(monitor, VIDEO_MODE_DEPTH, true);
688  
689          // Everything went well
690          init_ok = true;
# Line 531 | Line 787 | static void keycode_init(void)
787  
788                          if (video_driver_found) {
789                                  // Skip aliases
790 <                                static const char alias_str[] = "alias";
791 <                                if (strncmp(line, alias_str, sizeof(alias_str) - 1) == 0)
790 >                                static const char sdl_str[] = "sdl";
791 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0)
792                                          continue;
793  
794                                  // Read keycode
# Line 543 | Line 799 | static void keycode_init(void)
799                                          break;
800                          } else {
801                                  // Search for SDL video driver string
802 <                                static const char alias_sdl_str[] = "alias SDL";
803 <                                if (strncmp(line, alias_sdl_str, sizeof(alias_sdl_str) - 1) == 0) {
804 <                                        char *p = line + sizeof(alias_sdl_str);
802 >                                static const char sdl_str[] = "sdl";
803 >                                if (strncmp(line, sdl_str, sizeof(sdl_str) - 1) == 0) {
804 >                                        char *p = line + sizeof(sdl_str);
805                                          if (strstr(video_driver, p) == video_driver)
806                                                  video_driver_found = true;
807                                  }
# Line 570 | Line 826 | static void keycode_init(void)
826   bool SDL_monitor_desc::video_open(void)
827   {
828          D(bug("video_open()\n"));
829 <        const video_mode &mode = get_current_mode();
829 >        const VIDEO_MODE &mode = get_current_mode();
830 > #if DEBUG
831 >        D(bug("Current video mode:\n"));
832 >        D(bug(" %dx%d (ID %02x), %d bpp\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << (VIDEO_MODE_DEPTH & 0x0f)));
833 > #endif
834  
835          // Create display driver object of requested type
836          switch (display_type) {
# Line 586 | Line 846 | bool SDL_monitor_desc::video_open(void)
846                  return false;
847          }
848  
589 #ifdef ENABLE_VOSF
590        if (use_vosf) {
591                // Initialize the VOSF system
592                if (!video_vosf_init(*this)) {
593                        ErrorAlert(STR_VOSF_INIT_ERR);
594                return false;
595                }
596        }
597 #endif
598        
849          // Initialize VideoRefresh function
850          VideoRefreshInit();
851  
# Line 604 | Line 854 | bool SDL_monitor_desc::video_open(void)
854  
855          // Start redraw/input thread
856          redraw_thread_cancel = false;
857 <        redraw_thread_active = (SDL_CreateThread(redraw_func, NULL) != NULL);
857 >        redraw_thread_active = ((redraw_thread = SDL_CreateThread(redraw_func, NULL)) != NULL);
858          if (!redraw_thread_active) {
859                  printf("FATAL: cannot create redraw thread\n");
860                  return false;
# Line 612 | Line 862 | bool SDL_monitor_desc::video_open(void)
862          return true;
863   }
864  
865 + #ifdef SHEEPSHAVER
866 + bool VideoInit(void)
867 + {
868 +        const bool classic = false;
869 + #else
870   bool VideoInit(bool classic)
871   {
872 + #endif
873          classic_mode = classic;
874  
875   #ifdef ENABLE_VOSF
# Line 637 | Line 893 | bool VideoInit(bool classic)
893          mouse_wheel_lines = PrefsFindInt32("mousewheellines");
894  
895          // Get screen mode from preferences
896 <        const char *mode_str;
896 >        const char *mode_str = NULL;
897 > #ifndef SHEEPSHAVER
898          if (classic_mode)
899                  mode_str = "win/512/342";
900          else
901                  mode_str = PrefsFindString("screen");
902 + #endif
903  
904          // Determine display type and default dimensions
905 <        int default_width = 512, default_height = 384;
905 >        int default_width, default_height;
906 >        if (classic) {
907 >                default_width = 512;
908 >                default_height = 384;
909 >        }
910 >        else {
911 >                default_width = 640;
912 >                default_height = 480;
913 >        }
914          display_type = DISPLAY_WINDOW;
915          if (mode_str) {
916                  if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
917                          display_type = DISPLAY_WINDOW;
918          }
919          int max_width = 640, max_height = 480;
920 <        if (display_type == DISPLAY_SCREEN) {
921 <                SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
922 <                if (modes && modes != (SDL_Rect **)-1) {
923 <                        max_width = modes[0]->w;
924 <                        max_height = modes[0]->h;
925 <                }
920 >        SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
921 >        if (modes && modes != (SDL_Rect **)-1) {
922 >                max_width = modes[0]->w;
923 >                max_height = modes[0]->h;
924 >                if (default_width > max_width)
925 >                        default_width = max_width;
926 >                if (default_height > max_height)
927 >                        default_height = max_height;
928          }
929          if (default_width <= 0)
930                  default_width = max_width;
# Line 665 | Line 933 | bool VideoInit(bool classic)
933  
934          // Mac screen depth follows X depth
935          screen_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
936 <        video_depth default_depth;
936 >        int default_depth;
937          switch (screen_depth) {
938          case 8:
939 <                default_depth = VDEPTH_8BIT;
939 >                default_depth = VIDEO_DEPTH_8BIT;
940                  break;
941          case 15: case 16:
942 <                default_depth = VDEPTH_16BIT;
942 >                default_depth = VIDEO_DEPTH_16BIT;
943                  break;
944          case 24: case 32:
945 <                default_depth = VDEPTH_32BIT;
945 >                default_depth = VIDEO_DEPTH_32BIT;
946                  break;
947          default:
948 <                default_depth =  VDEPTH_1BIT;
948 >                default_depth =  VIDEO_DEPTH_1BIT;
949                  break;
950          }
951  
952          // Construct list of supported modes
953          if (display_type == DISPLAY_WINDOW) {
954                  if (classic)
955 <                        add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
955 >                        add_mode(display_type, 512, 342, 0x80, 64, VIDEO_DEPTH_1BIT);
956                  else {
957 <                        for (int d = VDEPTH_1BIT; d <= default_depth; d++) {
958 <                                int bpp = (d <= VDEPTH_8BIT ? 8 : sdl_depth_of_video_depth(d));
957 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
958 >                                int bpp = (d <= VIDEO_DEPTH_8BIT ? 8 : sdl_depth_of_video_depth(d));
959                                  if (SDL_VideoModeOK(max_width, max_height, bpp, SDL_HWSURFACE))
960                                          add_window_modes(video_depth(d));
961                          }
962                  }
963          } else
964 <                add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
964 >                add_mode(display_type, default_width, default_height, 0x80, TrivialBytesPerRow(default_width, (video_depth)default_depth), default_depth);
965          if (VideoModes.empty()) {
966                  ErrorAlert(STR_NO_XVISUAL_ERR);
967                  return false;
# Line 701 | Line 969 | bool VideoInit(bool classic)
969  
970          // Find requested default mode with specified dimensions
971          uint32 default_id;
972 <        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
972 >        std::vector<VIDEO_MODE>::const_iterator i, end = VideoModes.end();
973          for (i = VideoModes.begin(); i != end; ++i) {
974 <                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
975 <                        default_id = i->resolution_id;
974 >                const VIDEO_MODE & mode = (*i);
975 >                if (VIDEO_MODE_X == default_width && VIDEO_MODE_Y == default_height && VIDEO_MODE_DEPTH == default_depth) {
976 >                        default_id = VIDEO_MODE_RESOLUTION;
977 > #ifdef SHEEPSHAVER
978 >                        std::vector<VIDEO_MODE>::const_iterator begin = VideoModes.begin();
979 >                        cur_mode = distance(begin, i);
980 > #endif
981                          break;
982                  }
983          }
984          if (i == end) { // not found, use first available mode
985 <                default_depth = VideoModes[0].depth;
986 <                default_id = VideoModes[0].resolution_id;
985 >                const VIDEO_MODE & mode = VideoModes[0];
986 >                default_depth = VIDEO_MODE_DEPTH;
987 >                default_id = VIDEO_MODE_RESOLUTION;
988 > #ifdef SHEEPSHAVER
989 >                cur_mode = 0;
990 > #endif
991          }
992  
993 + #ifdef SHEEPSHAVER
994 +        for (int i = 0; i < VideoModes.size(); i++)
995 +                VModes[i] = VideoModes[i];
996 +        VideoInfo *p = &VModes[VideoModes.size()];
997 +        p->viType = DIS_INVALID;        // End marker
998 +        p->viRowBytes = 0;
999 +        p->viXsize = p->viYsize = 0;
1000 +        p->viAppleMode = 0;
1001 +        p->viAppleID = 0;
1002 + #endif
1003 +
1004   #if DEBUG
1005          D(bug("Available video modes:\n"));
1006          for (i = VideoModes.begin(); i != end; ++i) {
1007 <                int bits = 1 << i->depth;
1007 >                const VIDEO_MODE & mode = (*i);
1008 >                int bits = 1 << VIDEO_MODE_DEPTH;
1009                  if (bits == 16)
1010                          bits = 15;
1011                  else if (bits == 32)
1012                          bits = 24;
1013 <                D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1013 >                D(bug(" %dx%d (ID %02x), %d colors\n", VIDEO_MODE_X, VIDEO_MODE_Y, VIDEO_MODE_RESOLUTION, 1 << bits));
1014          }
1015   #endif
1016  
1017          // Create SDL_monitor_desc for this (the only) display
1018 <        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, default_depth, default_id);
1018 >        SDL_monitor_desc *monitor = new SDL_monitor_desc(VideoModes, (video_depth)default_depth, default_id);
1019          VideoMonitors.push_back(monitor);
1020  
1021          // Open display
# Line 746 | Line 1035 | void SDL_monitor_desc::video_close(void)
1035          // Stop redraw thread
1036          if (redraw_thread_active) {
1037                  redraw_thread_cancel = true;
1038 < //              SDL_WaitThread(redraw_thread, NULL); doesn't work
750 <                while (redraw_thread_cancel);
1038 >                SDL_WaitThread(redraw_thread, NULL);
1039          }
1040          redraw_thread_active = false;
1041  
# Line 797 | Line 1085 | void VideoQuitFullScreen(void)
1085   *  Mac VBL interrupt
1086   */
1087  
1088 + /*
1089 + *  Execute video VBL routine
1090 + */
1091 +
1092 + #ifdef SHEEPSHAVER
1093 + void VideoVBL(void)
1094 + {
1095 +        // Emergency quit requested? Then quit
1096 +        if (emerg_quit)
1097 +                QuitEmulator();
1098 +
1099 +        // Temporarily give up frame buffer lock (this is the point where
1100 +        // we are suspended when the user presses Ctrl-Tab)
1101 +        UNLOCK_FRAME_BUFFER;
1102 +        LOCK_FRAME_BUFFER;
1103 +
1104 +        // Execute video VBL
1105 +        if (private_data != NULL && private_data->interruptsEnabled)
1106 +                VSLDoInterruptService(private_data->vslServiceID);
1107 + }
1108 + #else
1109   void VideoInterrupt(void)
1110   {
1111 +        // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
1112 +        SDL_PumpEvents();
1113 +
1114          // Emergency quit requested? Then quit
1115          if (emerg_quit)
1116                  QuitEmulator();
# Line 808 | Line 1120 | void VideoInterrupt(void)
1120          UNLOCK_FRAME_BUFFER;
1121          LOCK_FRAME_BUFFER;
1122   }
1123 + #endif
1124  
1125  
1126   /*
1127   *  Set palette
1128   */
1129  
1130 + #ifdef SHEEPSHAVER
1131 + void video_set_palette(void)
1132 + {
1133 +        monitor_desc * monitor = VideoMonitors[0];
1134 +        int n_colors = palette_size(monitor->get_current_mode().viAppleMode);
1135 +        uint8 pal[256 * 3];
1136 +        for (int c = 0; c < n_colors; c++) {
1137 +                pal[c*3 + 0] = mac_pal[c].red;
1138 +                pal[c*3 + 1] = mac_pal[c].green;
1139 +                pal[c*3 + 2] = mac_pal[c].blue;
1140 +        }
1141 +        monitor->set_palette(pal, n_colors);
1142 + }
1143 + #endif
1144 +
1145   void SDL_monitor_desc::set_palette(uint8 *pal, int num_in)
1146   {
1147 <        const video_mode &mode = get_current_mode();
1147 >        const VIDEO_MODE &mode = get_current_mode();
1148  
1149          // FIXME: how can we handle the gamma ramp?
1150 <        if (mode.depth > VDEPTH_8BIT)
1150 >        if ((int)VIDEO_MODE_DEPTH > VIDEO_DEPTH_8BIT)
1151                  return;
1152  
1153          LOCK_PALETTE;
# Line 844 | Line 1172 | void SDL_monitor_desc::set_palette(uint8
1172                  }
1173  
1174   #ifdef ENABLE_VOSF
1175 <                // We have to redraw everything because the interpretation of pixel values changed
1176 <                LOCK_VOSF;
1177 <                PFLAG_SET_ALL;
1178 <                UNLOCK_VOSF;
1179 <                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1175 >                if (use_vosf) {
1176 >                        // We have to redraw everything because the interpretation of pixel values changed
1177 >                        LOCK_VOSF;
1178 >                        PFLAG_SET_ALL;
1179 >                        UNLOCK_VOSF;
1180 >                        memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1181 >                }
1182   #endif
1183          }
1184  
# Line 863 | Line 1193 | void SDL_monitor_desc::set_palette(uint8
1193   *  Switch video mode
1194   */
1195  
1196 + #ifdef SHEEPSHAVER
1197 + int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1198 + {
1199 +        /* return if no mode change */
1200 +        if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1201 +            (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
1202 +
1203 +        /* first find video mode in table */
1204 +        for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
1205 +                if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
1206 +                    (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
1207 +                        csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
1208 +                        csSave->saveData = ReadMacInt32(ParamPtr + csData);
1209 +                        csSave->savePage = ReadMacInt16(ParamPtr + csPage);
1210 +
1211 +                        // Disable interrupts
1212 +                        DisableInterrupt();
1213 +
1214 +                        cur_mode = i;
1215 +                        monitor_desc *monitor = VideoMonitors[0];
1216 +                        monitor->switch_to_current_mode();
1217 +
1218 +                        WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
1219 +                        csSave->saveBaseAddr=screen_base;
1220 +                        csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
1221 +                        csSave->saveMode=VModes[cur_mode].viAppleMode;
1222 +
1223 +                        // Enable interrupts
1224 +                        EnableInterrupt();
1225 +                        return noErr;
1226 +                }
1227 +        }
1228 +        return paramErr;
1229 + }
1230 + #endif
1231 +
1232   void SDL_monitor_desc::switch_to_current_mode(void)
1233   {
1234          // Close and reopen display
# Line 877 | Line 1243 | void SDL_monitor_desc::switch_to_current
1243  
1244  
1245   /*
1246 < *  Translate key event to Mac keycode, returns -1 if no keycode was found
1247 < *  and -2 if the key was recognized as a hotkey
1246 > *  Can we set the MacOS cursor image into the window?
1247 > */
1248 >
1249 > #ifdef SHEEPSHAVER
1250 > bool video_can_change_cursor(void)
1251 > {
1252 >        return (display_type == DISPLAY_WINDOW);
1253 > }
1254 > #endif
1255 >
1256 >
1257 > /*
1258 > *  Set cursor image for window
1259 > */
1260 >
1261 > #ifdef SHEEPSHAVER
1262 > void video_set_cursor(void)
1263 > {
1264 >        cursor_changed = true;
1265 > }
1266 > #endif
1267 >
1268 >
1269 > /*
1270 > *  Install graphics acceleration
1271   */
1272  
1273 + #ifdef SHEEPSHAVER
1274 + // Rectangle inversion
1275 + template< int bpp >
1276 + static inline void do_invrect(uint8 *dest, uint32 length)
1277 + {
1278 + #define INVERT_1(PTR, OFS) ((uint8  *)(PTR))[OFS] = ~((uint8  *)(PTR))[OFS]
1279 + #define INVERT_2(PTR, OFS) ((uint16 *)(PTR))[OFS] = ~((uint16 *)(PTR))[OFS]
1280 + #define INVERT_4(PTR, OFS) ((uint32 *)(PTR))[OFS] = ~((uint32 *)(PTR))[OFS]
1281 + #define INVERT_8(PTR, OFS) ((uint64 *)(PTR))[OFS] = ~((uint64 *)(PTR))[OFS]
1282 +
1283 + #ifndef UNALIGNED_PROFITABLE
1284 +        // Align on 16-bit boundaries
1285 +        if (bpp < 16 && (((uintptr)dest) & 1)) {
1286 +                INVERT_1(dest, 0);
1287 +                dest += 1; length -= 1;
1288 +        }
1289 +
1290 +        // Align on 32-bit boundaries
1291 +        if (bpp < 32 && (((uintptr)dest) & 2)) {
1292 +                INVERT_2(dest, 0);
1293 +                dest += 2; length -= 2;
1294 +        }
1295 + #endif
1296 +
1297 +        // Invert 8-byte words
1298 +        if (length >= 8) {
1299 +                const int r = (length / 8) % 8;
1300 +                dest += r * 8;
1301 +
1302 +                int n = ((length / 8) + 7) / 8;
1303 +                switch (r) {
1304 +                case 0: do {
1305 +                                dest += 64;
1306 +                                INVERT_8(dest, -8);
1307 +                case 7: INVERT_8(dest, -7);
1308 +                case 6: INVERT_8(dest, -6);
1309 +                case 5: INVERT_8(dest, -5);
1310 +                case 4: INVERT_8(dest, -4);
1311 +                case 3: INVERT_8(dest, -3);
1312 +                case 2: INVERT_8(dest, -2);
1313 +                case 1: INVERT_8(dest, -1);
1314 +                                } while (--n > 0);
1315 +                }
1316 +        }
1317 +
1318 +        // 32-bit cell to invert?
1319 +        if (length & 4) {
1320 +                INVERT_4(dest, 0);
1321 +                if (bpp <= 16)
1322 +                        dest += 4;
1323 +        }
1324 +
1325 +        // 16-bit cell to invert?
1326 +        if (bpp <= 16 && (length & 2)) {
1327 +                INVERT_2(dest, 0);
1328 +                if (bpp <= 8)
1329 +                        dest += 2;
1330 +        }
1331 +
1332 +        // 8-bit cell to invert?
1333 +        if (bpp <= 8 && (length & 1))
1334 +                INVERT_1(dest, 0);
1335 +
1336 + #undef INVERT_1
1337 + #undef INVERT_2
1338 + #undef INVERT_4
1339 + #undef INVERT_8
1340 + }
1341 +
1342 + void NQD_invrect(uint32 p)
1343 + {
1344 +        D(bug("accl_invrect %08x\n", p));
1345 +
1346 +        // Get inversion parameters
1347 +        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1348 +        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1349 +        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1350 +        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1351 +        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1352 +        D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1353 +
1354 +        //!!?? pen_mode == 14
1355 +
1356 +        // And perform the inversion
1357 +        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1358 +        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1359 +        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1360 +        width *= bpp;
1361 +        switch (bpp) {
1362 +        case 1:
1363 +                for (int i = 0; i < height; i++) {
1364 +                        do_invrect<8>(dest, width);
1365 +                        dest += dest_row_bytes;
1366 +                }
1367 +                break;
1368 +        case 2:
1369 +                for (int i = 0; i < height; i++) {
1370 +                        do_invrect<16>(dest, width);
1371 +                        dest += dest_row_bytes;
1372 +                }
1373 +                break;
1374 +        case 4:
1375 +                for (int i = 0; i < height; i++) {
1376 +                        do_invrect<32>(dest, width);
1377 +                        dest += dest_row_bytes;
1378 +                }
1379 +                break;
1380 +        }
1381 + }
1382 +
1383 + // Rectangle filling
1384 + template< int bpp >
1385 + static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1386 + {
1387 + #define FILL_1(PTR, OFS, VAL) ((uint8  *)(PTR))[OFS] = (VAL)
1388 + #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1389 + #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1390 + #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
1391 +
1392 + #ifndef UNALIGNED_PROFITABLE
1393 +        // Align on 16-bit boundaries
1394 +        if (bpp < 16 && (((uintptr)dest) & 1)) {
1395 +                FILL_1(dest, 0, color);
1396 +                dest += 1; length -= 1;
1397 +        }
1398 +
1399 +        // Align on 32-bit boundaries
1400 +        if (bpp < 32 && (((uintptr)dest) & 2)) {
1401 +                FILL_2(dest, 0, color);
1402 +                dest += 2; length -= 2;
1403 +        }
1404 + #endif
1405 +
1406 +        // Fill 8-byte words
1407 +        if (length >= 8) {
1408 +                const uint64 c = (((uint64)color) << 32) | color;
1409 +                const int r = (length / 8) % 8;
1410 +                dest += r * 8;
1411 +
1412 +                int n = ((length / 8) + 7) / 8;
1413 +                switch (r) {
1414 +                case 0: do {
1415 +                                dest += 64;
1416 +                                FILL_8(dest, -8, c);
1417 +                case 7: FILL_8(dest, -7, c);
1418 +                case 6: FILL_8(dest, -6, c);
1419 +                case 5: FILL_8(dest, -5, c);
1420 +                case 4: FILL_8(dest, -4, c);
1421 +                case 3: FILL_8(dest, -3, c);
1422 +                case 2: FILL_8(dest, -2, c);
1423 +                case 1: FILL_8(dest, -1, c);
1424 +                                } while (--n > 0);
1425 +                }
1426 +        }
1427 +
1428 +        // 32-bit cell to fill?
1429 +        if (length & 4) {
1430 +                FILL_4(dest, 0, color);
1431 +                if (bpp <= 16)
1432 +                        dest += 4;
1433 +        }
1434 +
1435 +        // 16-bit cell to fill?
1436 +        if (bpp <= 16 && (length & 2)) {
1437 +                FILL_2(dest, 0, color);
1438 +                if (bpp <= 8)
1439 +                        dest += 2;
1440 +        }
1441 +
1442 +        // 8-bit cell to fill?
1443 +        if (bpp <= 8 && (length & 1))
1444 +                FILL_1(dest, 0, color);
1445 +
1446 + #undef FILL_1
1447 + #undef FILL_2
1448 + #undef FILL_4
1449 + #undef FILL_8
1450 + }
1451 +
1452 + void NQD_fillrect(uint32 p)
1453 + {
1454 +        D(bug("accl_fillrect %08x\n", p));
1455 +
1456 +        // Get filling parameters
1457 +        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1458 +        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1459 +        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1460 +        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1461 +        uint32 color = htonl(ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen));
1462 +        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1463 +        D(bug(" width %d, height %d\n", width, height));
1464 +        D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1465 +
1466 +        // And perform the fill
1467 +        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1468 +        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1469 +        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1470 +        width *= bpp;
1471 +        switch (bpp) {
1472 +        case 1:
1473 +                for (int i = 0; i < height; i++) {
1474 +                        memset(dest, color, width);
1475 +                        dest += dest_row_bytes;
1476 +                }
1477 +                break;
1478 +        case 2:
1479 +                for (int i = 0; i < height; i++) {
1480 +                        do_fillrect<16>(dest, color, width);
1481 +                        dest += dest_row_bytes;
1482 +                }
1483 +                break;
1484 +        case 4:
1485 +                for (int i = 0; i < height; i++) {
1486 +                        do_fillrect<32>(dest, color, width);
1487 +                        dest += dest_row_bytes;
1488 +                }
1489 +                break;
1490 +        }
1491 + }
1492 +
1493 + bool NQD_fillrect_hook(uint32 p)
1494 + {
1495 +        D(bug("accl_fillrect_hook %08x\n", p));
1496 +
1497 +        // Check if we can accelerate this fillrect
1498 +        if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1499 +                const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1500 +                if (transfer_mode == 8) {
1501 +                        // Fill
1502 +                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1503 +                        return true;
1504 +                }
1505 +                else if (transfer_mode == 10) {
1506 +                        // Invert
1507 +                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1508 +                        return true;
1509 +                }
1510 +        }
1511 +        return false;
1512 + }
1513 +
1514 + // Rectangle blitting
1515 + // TODO: optimize for VOSF and target pixmap == screen
1516 + void NQD_bitblt(uint32 p)
1517 + {
1518 +        D(bug("accl_bitblt %08x\n", p));
1519 +
1520 +        // Get blitting parameters
1521 +        int16 src_X  = (int16)ReadMacInt16(p + acclSrcRect + 2) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 2);
1522 +        int16 src_Y  = (int16)ReadMacInt16(p + acclSrcRect + 0) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 0);
1523 +        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1524 +        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1525 +        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1526 +        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1527 +        D(bug(" src addr %08x, dest addr %08x\n", ReadMacInt32(p + acclSrcBaseAddr), ReadMacInt32(p + acclDestBaseAddr)));
1528 +        D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1529 +        D(bug(" width %d, height %d\n", width, height));
1530 +
1531 +        // And perform the blit
1532 +        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1533 +        width *= bpp;
1534 +        if ((int32)ReadMacInt32(p + acclSrcRowBytes) > 0) {
1535 +                const int src_row_bytes = (int32)ReadMacInt32(p + acclSrcRowBytes);
1536 +                const int dst_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1537 +                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + (src_Y * src_row_bytes) + (src_X * bpp));
1538 +                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dst_row_bytes) + (dest_X * bpp));
1539 +                for (int i = 0; i < height; i++) {
1540 +                        memmove(dst, src, width);
1541 +                        src += src_row_bytes;
1542 +                        dst += dst_row_bytes;
1543 +                }
1544 +        }
1545 +        else {
1546 +                const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1547 +                const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1548 +                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp));
1549 +                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp));
1550 +                for (int i = height - 1; i >= 0; i--) {
1551 +                        memmove(dst, src, width);
1552 +                        src -= src_row_bytes;
1553 +                        dst -= dst_row_bytes;
1554 +                }
1555 +        }
1556 + }
1557 +
1558 + /*
1559 +  BitBlt transfer modes:
1560 +  0 : srcCopy
1561 +  1 : srcOr
1562 +  2 : srcXor
1563 +  3 : srcBic
1564 +  4 : notSrcCopy
1565 +  5 : notSrcOr
1566 +  6 : notSrcXor
1567 +  7 : notSrcBic
1568 +  32 : blend
1569 +  33 : addPin
1570 +  34 : addOver
1571 +  35 : subPin
1572 +  36 : transparent
1573 +  37 : adMax
1574 +  38 : subOver
1575 +  39 : adMin
1576 +  50 : hilite
1577 + */
1578 +
1579 + bool NQD_bitblt_hook(uint32 p)
1580 + {
1581 +        D(bug("accl_draw_hook %08x\n", p));
1582 +
1583 +        // Check if we can accelerate this bitblt
1584 +        if (ReadMacInt32(p + 0x018) + ReadMacInt32(p + 0x128) == 0 &&
1585 +                ReadMacInt32(p + 0x130) == 0 &&
1586 +                ReadMacInt32(p + acclSrcPixelSize) >= 8 &&
1587 +                ReadMacInt32(p + acclSrcPixelSize) == ReadMacInt32(p + acclDestPixelSize) &&
1588 +                (ReadMacInt32(p + acclSrcRowBytes) ^ ReadMacInt32(p + acclDestRowBytes)) >= 0 && // same sign?
1589 +                ReadMacInt32(p + acclTransferMode) == 0 &&                                                                               // srcCopy?
1590 +                ReadMacInt32(p + 0x15c) > 0) {
1591 +
1592 +                // Yes, set function pointer
1593 +                WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1594 +                return true;
1595 +        }
1596 +        return false;
1597 + }
1598 +
1599 + // Wait for graphics operation to finish
1600 + bool NQD_sync_hook(uint32 arg)
1601 + {
1602 +        D(bug("accl_sync_hook %08x\n", arg));
1603 +        return true;
1604 + }
1605 +
1606 + void VideoInstallAccel(void)
1607 + {
1608 +        // Install acceleration hooks
1609 +        if (PrefsFindBool("gfxaccel")) {
1610 +                D(bug("Video: Installing acceleration hooks\n"));
1611 +                uint32 base;
1612 +
1613 +                SheepVar bitblt_hook_info(sizeof(accl_hook_info));
1614 +                base = bitblt_hook_info.addr();
1615 +                WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK));
1616 +                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1617 +                WriteMacInt32(base + 8, ACCL_BITBLT);
1618 +                NQDMisc(6, bitblt_hook_info.ptr());
1619 +
1620 +                SheepVar fillrect_hook_info(sizeof(accl_hook_info));
1621 +                base = fillrect_hook_info.addr();
1622 +                WriteMacInt32(base + 0, NativeTVECT(NATIVE_FILLRECT_HOOK));
1623 +                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1624 +                WriteMacInt32(base + 8, ACCL_FILLRECT);
1625 +                NQDMisc(6, fillrect_hook_info.ptr());
1626 +        }
1627 + }
1628 + #endif
1629 +
1630 +
1631 + /*
1632 + *  Keyboard-related utilify functions
1633 + */
1634 +
1635 + static bool is_modifier_key(SDL_KeyboardEvent const & e)
1636 + {
1637 +        switch (e.keysym.sym) {
1638 +        case SDLK_NUMLOCK:
1639 +        case SDLK_CAPSLOCK:
1640 +        case SDLK_SCROLLOCK:
1641 +        case SDLK_RSHIFT:
1642 +        case SDLK_LSHIFT:
1643 +        case SDLK_RCTRL:
1644 +        case SDLK_LCTRL:
1645 +        case SDLK_RALT:
1646 +        case SDLK_LALT:
1647 +        case SDLK_RMETA:
1648 +        case SDLK_LMETA:
1649 +        case SDLK_LSUPER:
1650 +        case SDLK_RSUPER:
1651 +        case SDLK_MODE:
1652 +        case SDLK_COMPOSE:
1653 +                return true;
1654 +        }
1655 +        return false;
1656 + }
1657 +
1658   static bool is_ctrl_down(SDL_keysym const & ks)
1659   {
1660          return ctrl_down || (ks.mod & KMOD_CTRL);
1661   }
1662  
1663 +
1664 + /*
1665 + *  Translate key event to Mac keycode, returns -1 if no keycode was found
1666 + *  and -2 if the key was recognized as a hotkey
1667 + */
1668 +
1669   static int kc_decode(SDL_keysym const & ks, bool key_down)
1670   {
1671          switch (ks.sym) {
# Line 955 | Line 1735 | static int kc_decode(SDL_keysym const &
1735          case SDLK_RCTRL: return 0x36;
1736          case SDLK_LSHIFT: return 0x38;
1737          case SDLK_RSHIFT: return 0x38;
1738 + #if (defined(__APPLE__) && defined(__MACH__))
1739 +        case SDLK_LALT: return 0x3a;
1740 +        case SDLK_RALT: return 0x3a;
1741 +        case SDLK_LMETA: return 0x37;
1742 +        case SDLK_RMETA: return 0x37;
1743 + #else
1744          case SDLK_LALT: return 0x37;
1745          case SDLK_RALT: return 0x37;
1746          case SDLK_LMETA: return 0x3a;
1747          case SDLK_RMETA: return 0x3a;
1748 + #endif
1749          case SDLK_MENU: return 0x32;
1750          case SDLK_CAPSLOCK: return 0x39;
1751          case SDLK_NUMLOCK: return 0x47;
# Line 1021 | Line 1808 | static int event2keycode(SDL_KeyboardEve
1808  
1809   static void handle_events(void)
1810   {
1811 <        SDL_Event event;
1812 <        while (SDL_PollEvent(&event)) {
1813 <                switch (event.type) {
1811 >        SDL_Event events[10];
1812 >        const int n_max_events = sizeof(events) / sizeof(events[0]);
1813 >        int n_events;
1814 >
1815 >        while ((n_events = SDL_PeepEvents(events, n_max_events, SDL_GETEVENT, sdl_eventmask)) > 0) {
1816 >                for (int i = 0; i < n_events; i++) {
1817 >                        SDL_Event const & event = events[i];
1818 >                        switch (event.type) {
1819  
1820                          // Mouse button
1821                          case SDL_MOUSEBUTTONDOWN: {
# Line 1060 | Line 1852 | static void handle_events(void)
1852                          // Keyboard
1853                          case SDL_KEYDOWN: {
1854                                  int code = -1;
1855 <                                if (use_keycodes) {
1855 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1856                                          if (event2keycode(event.key, true) != -2)       // This is called to process the hotkeys
1857                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1858                                  } else
# Line 1088 | Line 1880 | static void handle_events(void)
1880                          }
1881                          case SDL_KEYUP: {
1882                                  int code = -1;
1883 <                                if (use_keycodes) {
1883 >                                if (use_keycodes && !is_modifier_key(event.key)) {
1884                                          if (event2keycode(event.key, false) != -2)      // This is called to process the hotkeys
1885                                                  code = keycode_table[event.key.keysym.scancode & 0xff];
1886                                  } else
1887                                          code = event2keycode(event.key, false);
1888 <                                if (code >= 0 && code != 0x39) {        // Don't propagate Caps Lock releases
1889 <                                        ADBKeyUp(code);
1888 >                                if (code >= 0) {
1889 >                                        if (code == 0x39) {     // Caps Lock released
1890 >                                                if (caps_on) {
1891 >                                                        ADBKeyUp(code);
1892 >                                                        caps_on = false;
1893 >                                                } else {
1894 >                                                        ADBKeyDown(code);
1895 >                                                        caps_on = true;
1896 >                                                }
1897 >                                        } else
1898 >                                                ADBKeyUp(code);
1899                                          if (code == 0x36)
1900                                                  ctrl_down = false;
1901                                  }
# Line 1104 | Line 1905 | static void handle_events(void)
1905                          // Hidden parts exposed, force complete refresh of window
1906                          case SDL_VIDEOEXPOSE:
1907                                  if (display_type == DISPLAY_WINDOW) {
1908 <                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
1908 >                                        const VIDEO_MODE &mode = VideoMonitors[0]->get_current_mode();
1909   #ifdef ENABLE_VOSF
1910                                          if (use_vosf) {                 // VOSF refresh
1911                                                  LOCK_VOSF;
1912                                                  PFLAG_SET_ALL;
1913                                                  UNLOCK_VOSF;
1914 <                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1914 >                                                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1915                                          }
1916                                          else
1917   #endif
1918 <                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1918 >                                                memset(the_buffer_copy, 0, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
1919                                  }
1920                                  break;
1921  
# Line 1123 | Line 1924 | static void handle_events(void)
1924                                  ADBKeyDown(0x7f);       // Power key
1925                                  ADBKeyUp(0x7f);
1926                                  break;
1927 +                        }
1928                  }
1929          }
1930   }
# Line 1137 | Line 1939 | static void update_display_static(driver
1939   {
1940          // Incremental update code
1941          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1942 <        const video_mode &mode = drv->mode;
1943 <        int bytes_per_row = mode.bytes_per_row;
1942 >        const VIDEO_MODE &mode = drv->mode;
1943 >        int bytes_per_row = VIDEO_MODE_ROW_BYTES;
1944          uint8 *p, *p2;
1945  
1946          // Check for first line from top and first line from bottom that have changed
1947          y1 = 0;
1948 <        for (j=0; j<mode.y; j++) {
1948 >        for (j=0; j<VIDEO_MODE_Y; j++) {
1949                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1950                          y1 = j;
1951                          break;
1952                  }
1953          }
1954          y2 = y1 - 1;
1955 <        for (j=mode.y-1; j>=y1; j--) {
1955 >        for (j=VIDEO_MODE_Y-1; j>=y1; j--) {
1956                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1957                          y2 = j;
1958                          break;
# Line 1160 | Line 1962 | static void update_display_static(driver
1962  
1963          // Check for first column from left and first column from right that have changed
1964          if (high) {
1965 <                if (mode.depth < VDEPTH_8BIT) {
1965 >                if ((int)VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
1966                          const int src_bytes_per_row = bytes_per_row;
1967                          const int dst_bytes_per_row = drv->s->pitch;
1968 <                        const int pixels_per_byte = mode.x / src_bytes_per_row;
1968 >                        const int pixels_per_byte = VIDEO_MODE_X / src_bytes_per_row;
1969  
1970 <                        x1 = mode.x / pixels_per_byte;
1970 >                        x1 = VIDEO_MODE_X / pixels_per_byte;
1971                          for (j = y1; j <= y2; j++) {
1972                                  p = &the_buffer[j * bytes_per_row];
1973                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1183 | Line 1985 | static void update_display_static(driver
1985                                  p2 = &the_buffer_copy[j * bytes_per_row];
1986                                  p += bytes_per_row;
1987                                  p2 += bytes_per_row;
1988 <                                for (i = (mode.x / pixels_per_byte); i > x2; i--) {
1988 >                                for (i = (VIDEO_MODE_X / pixels_per_byte); i > x2; i--) {
1989                                          p--; p2--;
1990                                          if (*p != *p2) {
1991                                                  x2 = i;
# Line 1221 | Line 2023 | static void update_display_static(driver
2023                          }
2024  
2025                  } else {
2026 <                        const int bytes_per_pixel = mode.bytes_per_row / mode.x;
2026 >                        const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
2027  
2028 <                        x1 = mode.x;
2028 >                        x1 = VIDEO_MODE_X;
2029                          for (j=y1; j<=y2; j++) {
2030                                  p = &the_buffer[j * bytes_per_row];
2031                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1241 | Line 2043 | static void update_display_static(driver
2043                                  p2 = &the_buffer_copy[j * bytes_per_row];
2044                                  p += bytes_per_row;
2045                                  p2 += bytes_per_row;
2046 <                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2046 >                                for (i=VIDEO_MODE_X*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2047                                          p--;
2048                                          p2--;
2049                                          if (*p != *p2) {
# Line 1401 | Line 2203 | static void VideoRefreshInit(void)
2203          }
2204   }
2205  
2206 + const int VIDEO_REFRESH_HZ = 60;
2207 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2208 +
2209   static int redraw_func(void *arg)
2210   {
2211          uint64 start = GetTicks_usec();
2212          int64 ticks = 0;
2213 +        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2214  
2215          while (!redraw_thread_cancel) {
2216  
2217                  // Wait
2218 <                Delay_usec(16667);
2218 >                next += VIDEO_REFRESH_DELAY;
2219 >                int64 delay = next - GetTicks_usec();
2220 >                if (delay > 0)
2221 >                        Delay_usec(delay);
2222 >                else if (delay < -VIDEO_REFRESH_DELAY)
2223 >                        next = GetTicks_usec();
2224 >                ticks++;
2225  
2226                  // Handle SDL events
2227                  handle_events();
2228  
2229                  // Refresh display
2230                  video_refresh();
2231 <                ticks++;
2231 >
2232 > #ifdef SHEEPSHAVER
2233 >                // Set new cursor image if it was changed
2234 >                if (cursor_changed && sdl_cursor) {
2235 >                        cursor_changed = false;
2236 >                        SDL_FreeCursor(sdl_cursor);
2237 >                        sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
2238 >                        if (sdl_cursor)
2239 >                                SDL_SetCursor(sdl_cursor);
2240 >                }
2241 > #endif
2242  
2243                  // Set new palette if it was changed
2244                  handle_palette_changes();
# Line 1424 | Line 2246 | static int redraw_func(void *arg)
2246  
2247          uint64 end = GetTicks_usec();
2248          D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1427        redraw_thread_cancel = false;
2249          return 0;
2250   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines