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.14 by gbeauche, 2005-01-22T17:41:33Z vs.
Revision 1.21 by gbeauche, 2005-11-21T23:38:46Z

# Line 1 | Line 1
1   /*
2   *  video_sdl.cpp - Video/graphics emulation, SDL specific stuff
3   *
4 < *  Basilisk II (C) 1997-2004 Christian Bauer
4 > *  Basilisk II (C) 1997-2005 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 21 | Line 21
21   /*
22   *  NOTES:
23   *    The Ctrl key works like a qualifier for special actions:
24 < *      Ctrl-Tab = suspend DGA mode
24 > *      Ctrl-Tab = suspend DGA mode (TODO)
25   *      Ctrl-Esc = emergency quit
26   *      Ctrl-F1 = mount floppy
27   *      Ctrl-F5 = grab mouse (in windowed mode)
28   *
29   *  FIXMEs and TODOs:
30 + *  - Ctr-Tab for suspend/resume but how? SDL does not support that for non-Linux
31   *  - Ctrl-Fn doesn't generate SDL_KEYDOWN events (SDL bug?)
32   *  - Mouse acceleration, there is no API in SDL yet for that
33   *  - Force relative mode in Grab mode even if SDL provides absolute coordinates?
# Line 37 | Line 38
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()?
40   *  - Backport hw cursor acceleration to Basilisk II?
41 + *  - Factor out code
42   */
43  
44   #include "sysdeps.h"
# Line 82 | Line 84 | static int display_type = DISPLAY_WINDOW
84   #endif
85  
86   // Constants
87 + #ifdef WIN32
88 + const char KEYCODE_FILE_NAME[] = "BasiliskII_keycodes";
89 + #else
90   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
91 + #endif
92  
93  
94   // Global variables
# Line 95 | Line 101 | static uint8 *the_buffer_copy = NULL;
101   static uint32 the_buffer_size;                                          // Size of allocated the_buffer
102  
103   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
104 + #ifndef USE_CPU_EMUL_SERVICES
105   static volatile bool redraw_thread_cancel;                      // Flag: Cancel Redraw thread
106   static SDL_Thread *redraw_thread = NULL;                        // Redraw thread
107 + #ifdef SHEEPSHAVER
108   static volatile bool thread_stop_req = false;
109   static volatile bool thread_stop_ack = false;           // Acknowledge for thread_stop_req
110 + #endif
111 + #endif
112  
113   #ifdef ENABLE_VOSF
114   static bool use_vosf = false;                                           // Flag: VOSF enabled
# Line 148 | Line 158 | extern void SysMountFirstFloppy(void);
158  
159  
160   /*
161 + *  SDL surface locking glue
162 + */
163 +
164 + #ifdef ENABLE_VOSF
165 + #define SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE) do {                               \
166 +        if ((SURFACE)->flags & (SDL_HWSURFACE | SDL_FULLSCREEN))        \
167 +                the_host_buffer = (uint8 *)(SURFACE)->pixels;                   \
168 + } while (0)
169 + #else
170 + #define SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE)
171 + #endif
172 +
173 + #define SDL_VIDEO_LOCK_SURFACE(SURFACE) do {    \
174 +        if (SDL_MUSTLOCK(SURFACE)) {                            \
175 +                SDL_LockSurface(SURFACE);                               \
176 +                SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE);   \
177 +        }                                                                                       \
178 + } while (0)
179 +
180 + #define SDL_VIDEO_UNLOCK_SURFACE(SURFACE) do {  \
181 +        if (SDL_MUSTLOCK(SURFACE))                                      \
182 +                SDL_UnlockSurface(SURFACE);                             \
183 + } while (0)
184 +
185 +
186 + /*
187   *  Framebuffer allocation routines
188   */
189  
190   static void *vm_acquire_framebuffer(uint32 size)
191   {
156 #ifdef SHEEPSHAVER
157 #ifdef DIRECT_ADDRESSING_HACK
158        const uint32 FRAME_BUFFER_BASE = 0x61000000;
159        uint8 *fb = Mac2HostAddr(FRAME_BUFFER_BASE);
160        if (vm_acquire_fixed(fb, size) < 0)
161                fb = VM_MAP_FAILED;
162        return fb;
163 #endif
164 #endif
192          return vm_acquire(size);
193   }
194  
# Line 388 | Line 415 | static int sdl_depth_of_video_depth(int
415   // Check wether specified mode is available
416   static bool has_mode(int type, int width, int height)
417   {
391        // FIXME: no fullscreen support yet
392        if (type == DISPLAY_SCREEN)
393                return false;
394
418   #ifdef SHEEPSHAVER
419          // Filter out Classic resolutiosn
420          if (width == 512 && height == 384)
421                  return false;
422  
423 <        // Read window modes prefs
423 >        // "screen" prefs items always succeeds
424 >        if (PrefsFindString("screen"))
425 >                return true;
426 >
427 >        // Read window & screen modes prefs
428          static uint32 window_modes = 0;
429          static uint32 screen_modes = 0;
430          if (window_modes == 0 || screen_modes == 0) {
# Line 407 | Line 434 | static bool has_mode(int type, int width
434                          window_modes |= 3;                      // Allow at least 640x480 and 800x600 window modes
435          }
436  
437 <        if (type == DISPLAY_WINDOW) {
438 <                int apple_mask, apple_id = find_apple_resolution(width, height);
439 <                switch (apple_id) {
440 <                case APPLE_640x480:             apple_mask = 0x01; break;
441 <                case APPLE_800x600:             apple_mask = 0x02; break;
442 <                case APPLE_1024x768:    apple_mask = 0x04; break;
443 <                case APPLE_1152x768:    apple_mask = 0x40; break;
444 <                case APPLE_1152x900:    apple_mask = 0x08; break;
445 <                case APPLE_1280x1024:   apple_mask = 0x10; break;
446 <                case APPLE_1600x1200:   apple_mask = 0x20; break;
447 <                default:                                apple_mask = 0x00; break;
421 <                }
422 <                return (window_modes & apple_mask);
437 >        int test_modes;
438 >        switch (type) {
439 >        case DISPLAY_WINDOW:
440 >                test_modes = window_modes;
441 >                break;
442 >        case DISPLAY_SCREEN:
443 >                test_modes = screen_modes;
444 >                break;
445 >        default:
446 >                test_modes = 0;
447 >                break;
448          }
449 < #else
450 <        return true;
449 >
450 >        int apple_mask;
451 >        switch (find_apple_resolution(width, height)) {
452 >        case APPLE_640x480:             apple_mask = 0x01; break;
453 >        case APPLE_800x600:             apple_mask = 0x02; break;
454 >        case APPLE_1024x768:    apple_mask = 0x04; break;
455 >        case APPLE_1152x768:    apple_mask = 0x40; break;
456 >        case APPLE_1152x900:    apple_mask = 0x08; break;
457 >        case APPLE_1280x1024:   apple_mask = 0x10; break;
458 >        case APPLE_1600x1200:   apple_mask = 0x20; break;
459 >        default:                                apple_mask = 0x00; break;
460 >        }
461 >        return (test_modes & apple_mask);
462   #endif
463 <        return false;
463 >        return true;
464   }
465  
466   // Add mode to list of supported modes
# Line 501 | Line 537 | static void set_window_name(int name)
537   // Set mouse grab mode
538   static SDL_GrabMode set_grab_mode(SDL_GrabMode mode)
539   {
540 <        const SDL_VideoInfo *vi =SDL_GetVideoInfo();
540 >        const SDL_VideoInfo *vi = SDL_GetVideoInfo();
541          return (vi && vi->wm_available ? SDL_WM_GrabInput(mode) : SDL_GRAB_OFF);
542   }
543  
# Line 560 | Line 596 | private:
596          int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode)
597   };
598  
599 + class driver_fullscreen : public driver_base {
600 + public:
601 +        driver_fullscreen(SDL_monitor_desc &monitor);
602 +        ~driver_fullscreen();
603 + };
604 +
605   static driver_base *drv = NULL; // Pointer to currently used driver object
606  
607   #ifdef ENABLE_VOSF
# Line 775 | Line 817 | void driver_window::mouse_moved(int x, i
817          ADBMouseMoved(x, y);
818   }
819  
820 +
821 + /*
822 + *  Full-screen display driver
823 + */
824 +
825 + // Open display
826 + driver_fullscreen::driver_fullscreen(SDL_monitor_desc &m)
827 +        : driver_base(m)
828 + {
829 +        int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
830 +        int aligned_width = (width + 15) & ~15;
831 +        int aligned_height = (height + 15) & ~15;
832 +
833 +        // Set absolute mouse mode
834 +        ADBSetRelMouseMode(false);
835 +
836 +        // Create surface
837 +        int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
838 +        if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE | SDL_FULLSCREEN)) == NULL)
839 +                return;
840 +
841 + #ifdef ENABLE_VOSF
842 +        use_vosf = true;
843 +        // Allocate memory for frame buffer (SIZE is extended to page-boundary)
844 +        the_host_buffer = (uint8 *)s->pixels;
845 +        the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
846 +        the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
847 +        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
848 +        D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
849 +
850 +        // Check whether we can initialize the VOSF subsystem and it's profitable
851 +        if (!video_vosf_init(m)) {
852 +                WarningAlert(STR_VOSF_INIT_ERR);
853 +                use_vosf = false;
854 +        }
855 +        else if (!video_vosf_profitable()) {
856 +                video_vosf_exit();
857 +                printf("VOSF acceleration is not profitable on this platform, disabling it\n");
858 +                use_vosf = false;
859 +        }
860 +        if (!use_vosf) {
861 +                free(the_buffer_copy);
862 +                vm_release(the_buffer, the_buffer_size);
863 +                the_host_buffer = NULL;
864 +        }
865 + #endif
866 +        if (!use_vosf) {
867 +                // Allocate memory for frame buffer
868 +                the_buffer_size = (aligned_height + 2) * s->pitch;
869 +                the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
870 +                the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
871 +                D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
872 +        }
873 +        
874 +        // Hide cursor
875 +        SDL_ShowCursor(0);
876 +
877 +        // Init blitting routines
878 +        SDL_PixelFormat *f = s->format;
879 +        VisualFormat visualFormat;
880 +        visualFormat.depth = depth;
881 +        visualFormat.Rmask = f->Rmask;
882 +        visualFormat.Gmask = f->Gmask;
883 +        visualFormat.Bmask = f->Bmask;
884 +        Screen_blitter_init(visualFormat, true, mac_depth_of_video_depth(VIDEO_MODE_DEPTH));
885 +
886 +        // Load gray ramp to 8->16/32 expand map
887 +        if (!IsDirectMode(mode))
888 +                for (int i=0; i<256; i++)
889 +                        ExpandMap[i] = SDL_MapRGB(f, i, i, i);
890 +
891 +        // Set frame buffer base
892 +        set_mac_frame_buffer(monitor, VIDEO_MODE_DEPTH, true);
893 +
894 +        // Everything went well
895 +        init_ok = true;
896 + }
897 +
898 + // Close display
899 + driver_fullscreen::~driver_fullscreen()
900 + {
901 + #ifdef ENABLE_VOSF
902 +        if (use_vosf)
903 +                the_host_buffer = NULL; // don't free() in driver_base dtor
904 + #endif
905 +        if (s)
906 +                SDL_FreeSurface(s);
907 +
908 +        // Show cursor
909 +        SDL_ShowCursor(1);
910 + }
911 +
912 +
913   /*
914   *  Initialization
915   */
# Line 873 | Line 1008 | bool SDL_monitor_desc::video_open(void)
1008          case DISPLAY_WINDOW:
1009                  drv = new(std::nothrow) driver_window(*this);
1010                  break;
1011 +        case DISPLAY_SCREEN:
1012 +                drv = new(std::nothrow) driver_fullscreen(*this);
1013 +                break;
1014          }
1015          if (drv == NULL)
1016                  return false;
# Line 889 | Line 1027 | bool SDL_monitor_desc::video_open(void)
1027          LOCK_FRAME_BUFFER;
1028  
1029          // Start redraw/input thread
1030 + #ifndef USE_CPU_EMUL_SERVICES
1031          redraw_thread_cancel = false;
1032          redraw_thread_active = ((redraw_thread = SDL_CreateThread(redraw_func, NULL)) != NULL);
1033          if (!redraw_thread_active) {
1034                  printf("FATAL: cannot create redraw thread\n");
1035                  return false;
1036          }
1037 + #else
1038 +        redraw_thread_active = true;
1039 + #endif
1040          return true;
1041   }
1042  
# Line 930 | Line 1072 | bool VideoInit(bool classic)
1072  
1073          // Get screen mode from preferences
1074          const char *mode_str = NULL;
933 #ifndef SHEEPSHAVER
1075          if (classic_mode)
1076                  mode_str = "win/512/342";
1077          else
1078                  mode_str = PrefsFindString("screen");
938 #endif
1079  
1080          // Determine display type and default dimensions
1081          int default_width, default_height;
# Line 951 | Line 1091 | bool VideoInit(bool classic)
1091          if (mode_str) {
1092                  if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
1093                          display_type = DISPLAY_WINDOW;
1094 +                else if (sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1095 +                        display_type = DISPLAY_SCREEN;
1096          }
1097          int max_width = 640, max_height = 480;
1098          SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
1099          if (modes && modes != (SDL_Rect **)-1) {
1100 <                max_width = modes[0]->w;
1101 <                max_height = modes[0]->h;
1100 >                // It turns out that on some implementations, and contrary to the documentation,
1101 >                // the returned list is not sorted from largest to smallest (e.g. Windows)
1102 >                for (int i = 0; modes[i] != NULL; i++) {
1103 >                        const int w = modes[i]->w;
1104 >                        const int h = modes[i]->h;
1105 >                        if (w > max_width && h > max_height) {
1106 >                                max_width = w;
1107 >                                max_height = h;
1108 >                        }
1109 >                }
1110                  if (default_width > max_width)
1111                          default_width = max_width;
1112                  if (default_height > max_height)
# Line 996 | Line 1146 | bool VideoInit(bool classic)
1146                                          add_window_modes(video_depth(d));
1147                          }
1148                  }
1149 <        } else
1150 <                add_mode(display_type, default_width, default_height, 0x80, TrivialBytesPerRow(default_width, (video_depth)default_depth), default_depth);
1149 >        } else if (display_type == DISPLAY_SCREEN) {
1150 >                struct {
1151 >                        int w;
1152 >                        int h;
1153 >                        int resolution_id;
1154 >                }
1155 >                video_modes[] = {
1156 >                        {   -1,   -1, 0x80 },
1157 >                        {  640,  480, 0x81 },
1158 >                        {  800,  600, 0x82 },
1159 >                        { 1024,  768, 0x83 },
1160 >                        { 1152,  870, 0x84 },
1161 >                        { 1280, 1024, 0x85 },
1162 >                        { 1600, 1200, 0x86 },
1163 >                        { 0, }
1164 >                };
1165 >                video_modes[0].w = default_width;
1166 >                video_modes[0].h = default_height;
1167 >
1168 >                for (int i = 0; video_modes[i].w != 0; i++) {
1169 >                        const int w = video_modes[i].w;
1170 >                        const int h = video_modes[i].h;
1171 >                        if (i > 0 && (w >= default_width || h >= default_height))
1172 >                                continue;
1173 > #ifdef ENABLE_VOSF
1174 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1175 >                                int bpp = sdl_depth_of_video_depth(d);
1176 >                                if (SDL_VideoModeOK(w, h, bpp, SDL_HWSURFACE | SDL_FULLSCREEN))
1177 >                                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)d), d);
1178 >                        }
1179 > #else
1180 >                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)default_depth), default_depth);
1181 > #endif
1182 >                }
1183 >        }
1184 >
1185          if (VideoModes.empty()) {
1186                  ErrorAlert(STR_NO_XVISUAL_ERR);
1187                  return false;
# Line 1069 | Line 1253 | void SDL_monitor_desc::video_close(void)
1253          D(bug("video_close()\n"));
1254  
1255          // Stop redraw thread
1256 + #ifndef USE_CPU_EMUL_SERVICES
1257          if (redraw_thread_active) {
1258                  redraw_thread_cancel = true;
1259                  SDL_WaitThread(redraw_thread, NULL);
1260          }
1261 + #endif
1262          redraw_thread_active = false;
1263  
1264          // Unlock frame buffer
# Line 1369 | Line 1555 | static int kc_decode(SDL_keysym const &
1555  
1556          case SDLK_1: case SDLK_EXCLAIM: return 0x12;
1557          case SDLK_2: case SDLK_AT: return 0x13;
1558 < //      case SDLK_3: case SDLK_numbersign: return 0x14;
1558 >        case SDLK_3: case SDLK_HASH: return 0x14;
1559          case SDLK_4: case SDLK_DOLLAR: return 0x15;
1560 < //      case SDLK_5: case SDLK_percent: return 0x17;
1560 >        case SDLK_5: return 0x17;
1561          case SDLK_6: return 0x16;
1562          case SDLK_7: return 0x1a;
1563          case SDLK_8: return 0x1c;
1564          case SDLK_9: return 0x19;
1565          case SDLK_0: return 0x1d;
1566  
1567 < //      case SDLK_BACKQUOTE: case SDLK_asciitilde: return 0x0a;
1567 >        case SDLK_BACKQUOTE: return 0x0a;
1568          case SDLK_MINUS: case SDLK_UNDERSCORE: return 0x1b;
1569          case SDLK_EQUALS: case SDLK_PLUS: return 0x18;
1570 < //      case SDLK_bracketleft: case SDLK_braceleft: return 0x21;
1571 < //      case SDLK_bracketright: case SDLK_braceright: return 0x1e;
1572 < //      case SDLK_BACKSLASH: case SDLK_bar: return 0x2a;
1570 >        case SDLK_LEFTBRACKET: return 0x21;
1571 >        case SDLK_RIGHTBRACKET: return 0x1e;
1572 >        case SDLK_BACKSLASH: return 0x2a;
1573          case SDLK_SEMICOLON: case SDLK_COLON: return 0x29;
1574 < //      case SDLK_apostrophe: case SDLK_QUOTEDBL: return 0x27;
1574 >        case SDLK_QUOTE: case SDLK_QUOTEDBL: return 0x27;
1575          case SDLK_COMMA: case SDLK_LESS: return 0x2b;
1576          case SDLK_PERIOD: case SDLK_GREATER: return 0x2f;
1577          case SDLK_SLASH: case SDLK_QUESTION: return 0x2c;
# Line 1417 | Line 1603 | static int kc_decode(SDL_keysym const &
1603          case SDLK_LMETA: return 0x3a;
1604          case SDLK_RMETA: return 0x3a;
1605   #endif
1606 +        case SDLK_LSUPER: return 0x3a; // "Windows" key
1607 +        case SDLK_RSUPER: return 0x3a;
1608          case SDLK_MENU: return 0x32;
1609          case SDLK_CAPSLOCK: return 0x39;
1610          case SDLK_NUMLOCK: return 0x47;
# Line 1810 | Line 1998 | static void video_refresh_dga_vosf(void)
1998                  tick_counter = 0;
1999                  if (mainBuffer.dirty) {
2000                          LOCK_VOSF;
2001 <                        update_display_dga_vosf();
2001 >                        update_display_dga_vosf(static_cast<driver_fullscreen *>(drv));
2002                          UNLOCK_VOSF;
2003                  }
2004          }
# Line 1874 | Line 2062 | static void VideoRefreshInit(void)
2062          }
2063   }
2064  
2065 + static inline void do_video_refresh(void)
2066 + {
2067 +        // Handle SDL events
2068 +        handle_events();
2069 +
2070 +        // Update display
2071 +        video_refresh();
2072 +
2073 + #ifdef SHEEPSHAVER
2074 +        // Set new cursor image if it was changed
2075 +        if (cursor_changed && sdl_cursor) {
2076 +                cursor_changed = false;
2077 +                SDL_FreeCursor(sdl_cursor);
2078 +                sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
2079 +                if (sdl_cursor)
2080 +                        SDL_SetCursor(sdl_cursor);
2081 +        }
2082 + #endif
2083 +
2084 +        // Set new palette if it was changed
2085 +        handle_palette_changes();
2086 + }
2087 +
2088 + // This function is called on non-threaded platforms from a timer interrupt
2089 + void VideoRefresh(void)
2090 + {
2091 +        // We need to check redraw_thread_active to inhibit refreshed during
2092 +        // mode changes on non-threaded platforms
2093 +        if (!redraw_thread_active)
2094 +                return;
2095 +
2096 +        // Process pending events and update display
2097 +        do_video_refresh();
2098 + }
2099 +
2100   const int VIDEO_REFRESH_HZ = 60;
2101   const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2102  
2103 + #ifndef USE_CPU_EMUL_SERVICES
2104   static int redraw_func(void *arg)
2105   {
2106          uint64 start = GetTicks_usec();
# Line 1902 | Line 2126 | static int redraw_func(void *arg)
2126                  }
2127   #endif
2128  
2129 <                // Handle SDL events
2130 <                handle_events();
1907 <
1908 <                // Refresh display
1909 <                video_refresh();
1910 <
1911 < #ifdef SHEEPSHAVER
1912 <                // Set new cursor image if it was changed
1913 <                if (cursor_changed && sdl_cursor) {
1914 <                        cursor_changed = false;
1915 <                        SDL_FreeCursor(sdl_cursor);
1916 <                        sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
1917 <                        if (sdl_cursor)
1918 <                                SDL_SetCursor(sdl_cursor);
1919 <                }
1920 < #endif
1921 <
1922 <                // Set new palette if it was changed
1923 <                handle_palette_changes();
2129 >                // Process pending events and update display
2130 >                do_video_refresh();
2131          }
2132  
2133          uint64 end = GetTicks_usec();
2134          D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
2135          return 0;
2136   }
2137 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines