1 |
|
/* |
2 |
|
* video_sdl.cpp - Video/graphics emulation, SDL specific stuff |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2005 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2008 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 |
27 |
|
* Ctrl-F5 = grab mouse (in windowed mode) |
28 |
|
* |
29 |
|
* FIXMEs and TODOs: |
30 |
+ |
* - Windows requires an extra mouse event to update the actual cursor image? |
31 |
|
* - Ctr-Tab for suspend/resume but how? SDL does not support that for non-Linux |
32 |
|
* - Ctrl-Fn doesn't generate SDL_KEYDOWN events (SDL bug?) |
33 |
|
* - Mouse acceleration, there is no API in SDL yet for that |
34 |
|
* - Force relative mode in Grab mode even if SDL provides absolute coordinates? |
34 |
– |
* - Fullscreen mode |
35 |
|
* - Gamma tables support is likely to be broken here |
36 |
|
* - Events processing is bound to the general emulation thread as SDL requires |
37 |
|
* to PumpEvents() within the same thread as the one that called SetVideoMode(). |
38 |
|
* Besides, there can't seem to be a way to call SetVideoMode() from a child thread. |
39 |
– |
* - Refresh performance is still slow. Use SDL_CreateRGBSurface()? |
39 |
|
* - Backport hw cursor acceleration to Basilisk II? |
40 |
|
* - Factor out code |
41 |
|
*/ |
48 |
|
#include <errno.h> |
49 |
|
#include <vector> |
50 |
|
|
51 |
+ |
#ifdef WIN32 |
52 |
+ |
#include <malloc.h> /* alloca() */ |
53 |
+ |
#endif |
54 |
+ |
|
55 |
|
#include "cpu_emulation.h" |
56 |
|
#include "main.h" |
57 |
|
#include "adb.h" |
136 |
|
static volatile bool cursor_changed = false; // Flag: cursor changed, redraw_func must update the cursor |
137 |
|
static SDL_Color sdl_palette[256]; // Color palette to be used as CLUT and gamma table |
138 |
|
static bool sdl_palette_changed = false; // Flag: Palette changed, redraw thread must set new colors |
139 |
< |
static const int sdl_eventmask = SDL_MOUSEBUTTONDOWNMASK | SDL_MOUSEBUTTONUPMASK | SDL_MOUSEMOTIONMASK | SDL_KEYUPMASK | SDL_KEYDOWNMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK; |
139 |
> |
static const int sdl_eventmask = SDL_MOUSEEVENTMASK | SDL_KEYEVENTMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK | SDL_ACTIVEEVENTMASK; |
140 |
|
|
141 |
|
// Mutex to protect SDL events |
142 |
|
static SDL_mutex *sdl_events_lock = NULL; |
197 |
|
|
198 |
|
static void *vm_acquire_framebuffer(uint32 size) |
199 |
|
{ |
200 |
< |
return vm_acquire(size); |
200 |
> |
// always try to reallocate framebuffer at the same address |
201 |
> |
static void *fb = VM_MAP_FAILED; |
202 |
> |
if (fb != VM_MAP_FAILED) { |
203 |
> |
if (vm_acquire_fixed(fb, size) < 0) { |
204 |
> |
#ifndef SHEEPSHAVER |
205 |
> |
printf("FATAL: Could not reallocate framebuffer at previous address\n"); |
206 |
> |
#endif |
207 |
> |
fb = VM_MAP_FAILED; |
208 |
> |
} |
209 |
> |
} |
210 |
> |
if (fb == VM_MAP_FAILED) |
211 |
> |
fb = vm_acquire(size, VM_MAP_DEFAULT | VM_MAP_32BIT); |
212 |
> |
return fb; |
213 |
|
} |
214 |
|
|
215 |
|
static inline void vm_release_framebuffer(void *fb, uint32 size) |
625 |
|
class driver_window; |
626 |
|
static void update_display_window_vosf(driver_window *drv); |
627 |
|
static void update_display_dynamic(int ticker, driver_window *drv); |
628 |
< |
static void update_display_static(driver_window *drv); |
628 |
> |
static void update_display_static(driver_base *drv); |
629 |
|
|
630 |
|
class driver_window : public driver_base { |
631 |
|
friend void update_display_window_vosf(driver_window *drv); |
632 |
|
friend void update_display_dynamic(int ticker, driver_window *drv); |
633 |
< |
friend void update_display_static(driver_window *drv); |
633 |
> |
friend void update_display_static(driver_base *drv); |
634 |
|
|
635 |
|
public: |
636 |
|
driver_window(SDL_monitor_desc &monitor); |
731 |
|
* Windowed display driver |
732 |
|
*/ |
733 |
|
|
734 |
+ |
static bool SDL_display_opened = false; |
735 |
+ |
|
736 |
|
// Open display |
737 |
|
driver_window::driver_window(SDL_monitor_desc &m) |
738 |
|
: driver_base(m), mouse_grabbed(false) |
744 |
|
// Set absolute mouse mode |
745 |
|
ADBSetRelMouseMode(mouse_grabbed); |
746 |
|
|
747 |
+ |
// This is ugly: |
748 |
+ |
// If we're switching resolutions (ie, not setting it for the first time), |
749 |
+ |
// there's a bug in SDL where the SDL_Surface created will not be properly |
750 |
+ |
// setup. The solution is to SDL_QuitSubSystem(SDL_INIT_VIDEO) before calling |
751 |
+ |
// SDL_SetVideoMode for the second time (SDL_SetVideoMode will call SDL_Init() |
752 |
+ |
// and all will be well). Without this, the video becomes corrupted (at least |
753 |
+ |
// on Mac OS X), after the resolution switch. |
754 |
+ |
if (SDL_display_opened) |
755 |
+ |
SDL_QuitSubSystem(SDL_INIT_VIDEO); |
756 |
+ |
|
757 |
|
// Create surface |
758 |
|
int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH); |
759 |
|
if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL) |
760 |
|
return; |
761 |
|
|
762 |
+ |
SDL_display_opened = true; |
763 |
+ |
|
764 |
|
#ifdef ENABLE_VOSF |
765 |
|
use_vosf = true; |
766 |
|
// Allocate memory for frame buffer (SIZE is extended to page-boundary) |
1238 |
|
continue; |
1239 |
|
if (w == 512 && h == 384) |
1240 |
|
continue; |
1212 |
– |
#ifdef ENABLE_VOSF |
1241 |
|
for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) |
1242 |
|
add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)d), d); |
1215 |
– |
#else |
1216 |
– |
add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)default_depth), default_depth); |
1217 |
– |
#endif |
1243 |
|
} |
1244 |
|
} |
1245 |
|
|
1539 |
|
#ifdef SHEEPSHAVER |
1540 |
|
bool video_can_change_cursor(void) |
1541 |
|
{ |
1542 |
< |
return (display_type == DISPLAY_WINDOW); |
1542 |
> |
static char driver[] = "Quartz?"; |
1543 |
> |
static int quartzok = -1; |
1544 |
> |
|
1545 |
> |
if (display_type != DISPLAY_WINDOW) |
1546 |
> |
return false; |
1547 |
> |
|
1548 |
> |
if (quartzok < 0) { |
1549 |
> |
if (SDL_VideoDriverName(driver, sizeof driver) == NULL || strncmp(driver, "Quartz", sizeof driver)) |
1550 |
> |
quartzok = true; |
1551 |
> |
else { |
1552 |
> |
// Quartz driver bug prevents cursor changing in SDL 1.2.11 to 1.2.13. |
1553 |
> |
const SDL_version *vp = SDL_Linked_Version(); |
1554 |
> |
int version = SDL_VERSIONNUM(vp->major, vp->minor, vp->patch); |
1555 |
> |
quartzok = (version <= SDL_VERSIONNUM(1, 2, 10) || version >= SDL_VERSIONNUM(1, 2, 14)); |
1556 |
> |
} |
1557 |
> |
} |
1558 |
> |
|
1559 |
> |
return quartzok; |
1560 |
|
} |
1561 |
|
#endif |
1562 |
|
|
1767 |
|
// Mouse button |
1768 |
|
case SDL_MOUSEBUTTONDOWN: { |
1769 |
|
unsigned int button = event.button.button; |
1770 |
< |
if (button < 4) |
1771 |
< |
ADBMouseDown(button - 1); |
1770 |
> |
if (button == SDL_BUTTON_LEFT) |
1771 |
> |
ADBMouseDown(0); |
1772 |
> |
else if (button == SDL_BUTTON_RIGHT) |
1773 |
> |
ADBMouseDown(1); |
1774 |
> |
else if (button == SDL_BUTTON_MIDDLE) |
1775 |
> |
ADBMouseDown(2); |
1776 |
|
else if (button < 6) { // Wheel mouse |
1777 |
|
if (mouse_wheel_mode == 0) { |
1778 |
|
int key = (button == 5) ? 0x79 : 0x74; // Page up/down |
1790 |
|
} |
1791 |
|
case SDL_MOUSEBUTTONUP: { |
1792 |
|
unsigned int button = event.button.button; |
1793 |
< |
if (button < 4) |
1794 |
< |
ADBMouseUp(button - 1); |
1793 |
> |
if (button == SDL_BUTTON_LEFT) |
1794 |
> |
ADBMouseUp(0); |
1795 |
> |
else if (button == SDL_BUTTON_RIGHT) |
1796 |
> |
ADBMouseUp(1); |
1797 |
> |
else if (button == SDL_BUTTON_MIDDLE) |
1798 |
> |
ADBMouseUp(2); |
1799 |
|
break; |
1800 |
|
} |
1801 |
|
|
1879 |
|
ADBKeyDown(0x7f); // Power key |
1880 |
|
ADBKeyUp(0x7f); |
1881 |
|
break; |
1882 |
+ |
|
1883 |
+ |
// Application activate/deactivate; consume the event but otherwise ignore it |
1884 |
+ |
case SDL_ACTIVEEVENT: |
1885 |
+ |
break; |
1886 |
|
} |
1887 |
|
} |
1888 |
|
} |
1894 |
|
*/ |
1895 |
|
|
1896 |
|
// Static display update (fixed frame rate, but incremental) |
1897 |
< |
static void update_display_static(driver_window *drv) |
1897 |
> |
static void update_display_static(driver_base *drv) |
1898 |
|
{ |
1899 |
|
// Incremental update code |
1900 |
|
int wide = 0, high = 0, x1, x2, y1, y2, i, j; |
1983 |
|
|
1984 |
|
} else { |
1985 |
|
const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X; |
1986 |
+ |
const int dst_bytes_per_row = drv->s->pitch; |
1987 |
|
|
1988 |
|
x1 = VIDEO_MODE_X; |
1989 |
|
for (j=y1; j<=y2; j++) { |
2024 |
|
// Blit to screen surface |
2025 |
|
for (j=y1; j<=y2; j++) { |
2026 |
|
i = j * bytes_per_row + x1 * bytes_per_pixel; |
2027 |
+ |
int dst_i = j * dst_bytes_per_row + x1 * bytes_per_pixel; |
2028 |
|
memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide); |
2029 |
< |
Screen_blit((uint8 *)drv->s->pixels + i, the_buffer + i, bytes_per_pixel * wide); |
2029 |
> |
Screen_blit((uint8 *)drv->s->pixels + dst_i, the_buffer + i, bytes_per_pixel * wide); |
2030 |
|
} |
2031 |
|
|
2032 |
|
// Unlock surface, if required |
2042 |
|
|
2043 |
|
// Static display update (fixed frame rate, bounding boxes based) |
2044 |
|
// XXX use NQD bounding boxes to help detect dirty areas? |
2045 |
< |
static void update_display_static_bbox(driver_window *drv) |
2045 |
> |
static void update_display_static_bbox(driver_base *drv) |
2046 |
|
{ |
2047 |
|
const VIDEO_MODE &mode = drv->mode; |
2048 |
|
|
2060 |
|
// Update the surface from Mac screen |
2061 |
|
const int bytes_per_row = VIDEO_MODE_ROW_BYTES; |
2062 |
|
const int bytes_per_pixel = bytes_per_row / VIDEO_MODE_X; |
2063 |
+ |
const int dst_bytes_per_row = drv->s->pitch; |
2064 |
|
int x, y; |
2065 |
|
for (y = 0; y < VIDEO_MODE_Y; y += N_PIXELS) { |
2066 |
|
int h = N_PIXELS; |
2075 |
|
bool dirty = false; |
2076 |
|
for (int j = y; j < (y + h); j++) { |
2077 |
|
const int yb = j * bytes_per_row; |
2078 |
+ |
const int dst_yb = j * dst_bytes_per_row; |
2079 |
|
if (memcmp(&the_buffer[yb + xb], &the_buffer_copy[yb + xb], xs) != 0) { |
2080 |
|
memcpy(&the_buffer_copy[yb + xb], &the_buffer[yb + xb], xs); |
2081 |
< |
Screen_blit((uint8 *)drv->s->pixels + yb + xb, the_buffer + yb + xb, xs); |
2081 |
> |
Screen_blit((uint8 *)drv->s->pixels + dst_yb + xb, the_buffer + yb + xb, xs); |
2082 |
|
dirty = true; |
2083 |
|
} |
2084 |
|
} |
2142 |
|
UNLOCK_PALETTE; |
2143 |
|
} |
2144 |
|
|
2145 |
+ |
static void video_refresh_window_static(void); |
2146 |
+ |
|
2147 |
|
static void video_refresh_dga(void) |
2148 |
|
{ |
2149 |
|
// Quit DGA mode if requested |
2150 |
|
possibly_quit_dga_mode(); |
2151 |
+ |
video_refresh_window_static(); |
2152 |
|
} |
2153 |
|
|
2154 |
|
#ifdef ENABLE_VOSF |
2200 |
|
tick_counter = 0; |
2201 |
|
const VIDEO_MODE &mode = drv->mode; |
2202 |
|
if ((int)VIDEO_MODE_DEPTH >= VIDEO_DEPTH_8BIT) |
2203 |
< |
update_display_static_bbox(static_cast<driver_window *>(drv)); |
2203 |
> |
update_display_static_bbox(drv); |
2204 |
|
else |
2205 |
< |
update_display_static(static_cast<driver_window *>(drv)); |
2205 |
> |
update_display_static(drv); |
2206 |
|
} |
2207 |
|
} |
2208 |
|
|
2247 |
|
LOCK_EVENTS; |
2248 |
|
SDL_FreeCursor(sdl_cursor); |
2249 |
|
sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]); |
2250 |
< |
if (sdl_cursor) |
2250 |
> |
if (sdl_cursor) { |
2251 |
> |
SDL_ShowCursor(private_data == NULL || private_data->cursorVisible); |
2252 |
|
SDL_SetCursor(sdl_cursor); |
2253 |
+ |
#ifdef WIN32 |
2254 |
+ |
// XXX Windows apparently needs an extra mouse event to |
2255 |
+ |
// make the new cursor image visible |
2256 |
+ |
int visible = SDL_ShowCursor(-1); |
2257 |
+ |
if (visible) { |
2258 |
+ |
int x, y; |
2259 |
+ |
SDL_GetMouseState(&x, &y); |
2260 |
+ |
SDL_WarpMouse(x, y); |
2261 |
+ |
} |
2262 |
+ |
#endif |
2263 |
+ |
} |
2264 |
|
UNLOCK_EVENTS; |
2265 |
|
} |
2266 |
|
#endif |
2328 |
|
#ifdef SHEEPSHAVER |
2329 |
|
void video_set_dirty_area(int x, int y, int w, int h) |
2330 |
|
{ |
2331 |
+ |
#ifdef ENABLE_VOSF |
2332 |
|
const VIDEO_MODE &mode = drv->mode; |
2333 |
|
const int screen_width = VIDEO_MODE_X; |
2334 |
|
const int screen_height = VIDEO_MODE_Y; |
2335 |
|
const int bytes_per_row = VIDEO_MODE_ROW_BYTES; |
2336 |
|
|
2263 |
– |
#ifdef ENABLE_VOSF |
2337 |
|
if (use_vosf) { |
2338 |
< |
vosf_set_dirty_area(x, y, w, h, screen_width, bytes_per_row); |
2338 |
> |
vosf_set_dirty_area(x, y, w, h, screen_width, screen_height, bytes_per_row); |
2339 |
|
return; |
2340 |
|
} |
2341 |
|
#endif |