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.20 by gbeauche, 2005-06-14T22:35:42Z

# 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 388 | Line 398 | static int sdl_depth_of_video_depth(int
398   // Check wether specified mode is available
399   static bool has_mode(int type, int width, int height)
400   {
391        // FIXME: no fullscreen support yet
392        if (type == DISPLAY_SCREEN)
393                return false;
394
401   #ifdef SHEEPSHAVER
402          // Filter out Classic resolutiosn
403          if (width == 512 && height == 384)
404                  return false;
405  
406 <        // Read window modes prefs
406 >        // "screen" prefs items always succeeds
407 >        if (PrefsFindString("screen"))
408 >                return true;
409 >
410 >        // Read window & screen modes prefs
411          static uint32 window_modes = 0;
412          static uint32 screen_modes = 0;
413          if (window_modes == 0 || screen_modes == 0) {
# Line 407 | Line 417 | static bool has_mode(int type, int width
417                          window_modes |= 3;                      // Allow at least 640x480 and 800x600 window modes
418          }
419  
420 <        if (type == DISPLAY_WINDOW) {
421 <                int apple_mask, apple_id = find_apple_resolution(width, height);
422 <                switch (apple_id) {
423 <                case APPLE_640x480:             apple_mask = 0x01; break;
424 <                case APPLE_800x600:             apple_mask = 0x02; break;
425 <                case APPLE_1024x768:    apple_mask = 0x04; break;
426 <                case APPLE_1152x768:    apple_mask = 0x40; break;
427 <                case APPLE_1152x900:    apple_mask = 0x08; break;
428 <                case APPLE_1280x1024:   apple_mask = 0x10; break;
429 <                case APPLE_1600x1200:   apple_mask = 0x20; break;
430 <                default:                                apple_mask = 0x00; break;
421 <                }
422 <                return (window_modes & apple_mask);
420 >        int test_modes;
421 >        switch (type) {
422 >        case DISPLAY_WINDOW:
423 >                test_modes = window_modes;
424 >                break;
425 >        case DISPLAY_SCREEN:
426 >                test_modes = screen_modes;
427 >                break;
428 >        default:
429 >                test_modes = 0;
430 >                break;
431          }
432 < #else
433 <        return true;
432 >
433 >        int apple_mask;
434 >        switch (find_apple_resolution(width, height)) {
435 >        case APPLE_640x480:             apple_mask = 0x01; break;
436 >        case APPLE_800x600:             apple_mask = 0x02; break;
437 >        case APPLE_1024x768:    apple_mask = 0x04; break;
438 >        case APPLE_1152x768:    apple_mask = 0x40; break;
439 >        case APPLE_1152x900:    apple_mask = 0x08; break;
440 >        case APPLE_1280x1024:   apple_mask = 0x10; break;
441 >        case APPLE_1600x1200:   apple_mask = 0x20; break;
442 >        default:                                apple_mask = 0x00; break;
443 >        }
444 >        return (test_modes & apple_mask);
445   #endif
446 <        return false;
446 >        return true;
447   }
448  
449   // Add mode to list of supported modes
# Line 501 | Line 520 | static void set_window_name(int name)
520   // Set mouse grab mode
521   static SDL_GrabMode set_grab_mode(SDL_GrabMode mode)
522   {
523 <        const SDL_VideoInfo *vi =SDL_GetVideoInfo();
523 >        const SDL_VideoInfo *vi = SDL_GetVideoInfo();
524          return (vi && vi->wm_available ? SDL_WM_GrabInput(mode) : SDL_GRAB_OFF);
525   }
526  
# Line 560 | Line 579 | private:
579          int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode)
580   };
581  
582 + class driver_fullscreen : public driver_base {
583 + public:
584 +        driver_fullscreen(SDL_monitor_desc &monitor);
585 +        ~driver_fullscreen();
586 + };
587 +
588   static driver_base *drv = NULL; // Pointer to currently used driver object
589  
590   #ifdef ENABLE_VOSF
# Line 775 | Line 800 | void driver_window::mouse_moved(int x, i
800          ADBMouseMoved(x, y);
801   }
802  
803 +
804 + /*
805 + *  Full-screen display driver
806 + */
807 +
808 + // Open display
809 + driver_fullscreen::driver_fullscreen(SDL_monitor_desc &m)
810 +        : driver_base(m)
811 + {
812 +        int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
813 +        int aligned_width = (width + 15) & ~15;
814 +        int aligned_height = (height + 15) & ~15;
815 +
816 +        // Set absolute mouse mode
817 +        ADBSetRelMouseMode(false);
818 +
819 +        // Create surface
820 +        int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
821 +        if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE | SDL_FULLSCREEN)) == NULL)
822 +                return;
823 +
824 + #ifdef ENABLE_VOSF
825 +        use_vosf = true;
826 +        // Allocate memory for frame buffer (SIZE is extended to page-boundary)
827 +        the_host_buffer = (uint8 *)s->pixels;
828 +        the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
829 +        the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
830 +        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
831 +        D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
832 +
833 +        // Check whether we can initialize the VOSF subsystem and it's profitable
834 +        if (!video_vosf_init(m)) {
835 +                WarningAlert(STR_VOSF_INIT_ERR);
836 +                use_vosf = false;
837 +        }
838 +        else if (!video_vosf_profitable()) {
839 +                video_vosf_exit();
840 +                printf("VOSF acceleration is not profitable on this platform, disabling it\n");
841 +                use_vosf = false;
842 +        }
843 +        if (!use_vosf) {
844 +                free(the_buffer_copy);
845 +                vm_release(the_buffer, the_buffer_size);
846 +                the_host_buffer = NULL;
847 +        }
848 + #endif
849 +        if (!use_vosf) {
850 +                // Allocate memory for frame buffer
851 +                the_buffer_size = (aligned_height + 2) * s->pitch;
852 +                the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
853 +                the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
854 +                D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
855 +        }
856 +        
857 +        // Hide cursor
858 +        SDL_ShowCursor(0);
859 +
860 +        // Init blitting routines
861 +        SDL_PixelFormat *f = s->format;
862 +        VisualFormat visualFormat;
863 +        visualFormat.depth = depth;
864 +        visualFormat.Rmask = f->Rmask;
865 +        visualFormat.Gmask = f->Gmask;
866 +        visualFormat.Bmask = f->Bmask;
867 +        Screen_blitter_init(visualFormat, true, mac_depth_of_video_depth(VIDEO_MODE_DEPTH));
868 +
869 +        // Load gray ramp to 8->16/32 expand map
870 +        if (!IsDirectMode(mode))
871 +                for (int i=0; i<256; i++)
872 +                        ExpandMap[i] = SDL_MapRGB(f, i, i, i);
873 +
874 +        // Set frame buffer base
875 +        set_mac_frame_buffer(monitor, VIDEO_MODE_DEPTH, true);
876 +
877 +        // Everything went well
878 +        init_ok = true;
879 + }
880 +
881 + // Close display
882 + driver_fullscreen::~driver_fullscreen()
883 + {
884 + #ifdef ENABLE_VOSF
885 +        if (use_vosf)
886 +                the_host_buffer = NULL; // don't free() in driver_base dtor
887 + #endif
888 +        if (s)
889 +                SDL_FreeSurface(s);
890 +
891 +        // Show cursor
892 +        SDL_ShowCursor(1);
893 + }
894 +
895 +
896   /*
897   *  Initialization
898   */
# Line 873 | Line 991 | bool SDL_monitor_desc::video_open(void)
991          case DISPLAY_WINDOW:
992                  drv = new(std::nothrow) driver_window(*this);
993                  break;
994 +        case DISPLAY_SCREEN:
995 +                drv = new(std::nothrow) driver_fullscreen(*this);
996 +                break;
997          }
998          if (drv == NULL)
999                  return false;
# Line 889 | Line 1010 | bool SDL_monitor_desc::video_open(void)
1010          LOCK_FRAME_BUFFER;
1011  
1012          // Start redraw/input thread
1013 + #ifndef USE_CPU_EMUL_SERVICES
1014          redraw_thread_cancel = false;
1015          redraw_thread_active = ((redraw_thread = SDL_CreateThread(redraw_func, NULL)) != NULL);
1016          if (!redraw_thread_active) {
1017                  printf("FATAL: cannot create redraw thread\n");
1018                  return false;
1019          }
1020 + #else
1021 +        redraw_thread_active = true;
1022 + #endif
1023          return true;
1024   }
1025  
# Line 930 | Line 1055 | bool VideoInit(bool classic)
1055  
1056          // Get screen mode from preferences
1057          const char *mode_str = NULL;
933 #ifndef SHEEPSHAVER
1058          if (classic_mode)
1059                  mode_str = "win/512/342";
1060          else
1061                  mode_str = PrefsFindString("screen");
938 #endif
1062  
1063          // Determine display type and default dimensions
1064          int default_width, default_height;
# Line 951 | Line 1074 | bool VideoInit(bool classic)
1074          if (mode_str) {
1075                  if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
1076                          display_type = DISPLAY_WINDOW;
1077 +                else if (sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1078 +                        display_type = DISPLAY_SCREEN;
1079          }
1080          int max_width = 640, max_height = 480;
1081          SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
1082          if (modes && modes != (SDL_Rect **)-1) {
1083 <                max_width = modes[0]->w;
1084 <                max_height = modes[0]->h;
1083 >                // It turns out that on some implementations, and contrary to the documentation,
1084 >                // the returned list is not sorted from largest to smallest (e.g. Windows)
1085 >                for (int i = 0; modes[i] != NULL; i++) {
1086 >                        const int w = modes[i]->w;
1087 >                        const int h = modes[i]->h;
1088 >                        if (w > max_width && h > max_height) {
1089 >                                max_width = w;
1090 >                                max_height = h;
1091 >                        }
1092 >                }
1093                  if (default_width > max_width)
1094                          default_width = max_width;
1095                  if (default_height > max_height)
# Line 996 | Line 1129 | bool VideoInit(bool classic)
1129                                          add_window_modes(video_depth(d));
1130                          }
1131                  }
1132 <        } else
1133 <                add_mode(display_type, default_width, default_height, 0x80, TrivialBytesPerRow(default_width, (video_depth)default_depth), default_depth);
1132 >        } else if (display_type == DISPLAY_SCREEN) {
1133 >                struct {
1134 >                        int w;
1135 >                        int h;
1136 >                        int resolution_id;
1137 >                }
1138 >                video_modes[] = {
1139 >                        {   -1,   -1, 0x80 },
1140 >                        {  640,  480, 0x81 },
1141 >                        {  800,  600, 0x82 },
1142 >                        { 1024,  768, 0x83 },
1143 >                        { 1152,  870, 0x84 },
1144 >                        { 1280, 1024, 0x85 },
1145 >                        { 1600, 1200, 0x86 },
1146 >                        { 0, }
1147 >                };
1148 >                video_modes[0].w = default_width;
1149 >                video_modes[0].h = default_height;
1150 >
1151 >                for (int i = 0; video_modes[i].w != 0; i++) {
1152 >                        const int w = video_modes[i].w;
1153 >                        const int h = video_modes[i].h;
1154 >                        if (i > 0 && (w >= default_width || h >= default_height))
1155 >                                continue;
1156 > #ifdef ENABLE_VOSF
1157 >                        for (int d = VIDEO_DEPTH_1BIT; d <= default_depth; d++) {
1158 >                                int bpp = sdl_depth_of_video_depth(d);
1159 >                                if (SDL_VideoModeOK(w, h, bpp, SDL_HWSURFACE | SDL_FULLSCREEN))
1160 >                                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)d), d);
1161 >                        }
1162 > #else
1163 >                        add_mode(display_type, w, h, video_modes[i].resolution_id, TrivialBytesPerRow(w, (video_depth)default_depth), default_depth);
1164 > #endif
1165 >                }
1166 >        }
1167 >
1168          if (VideoModes.empty()) {
1169                  ErrorAlert(STR_NO_XVISUAL_ERR);
1170                  return false;
# Line 1069 | Line 1236 | void SDL_monitor_desc::video_close(void)
1236          D(bug("video_close()\n"));
1237  
1238          // Stop redraw thread
1239 + #ifndef USE_CPU_EMUL_SERVICES
1240          if (redraw_thread_active) {
1241                  redraw_thread_cancel = true;
1242                  SDL_WaitThread(redraw_thread, NULL);
1243          }
1244 + #endif
1245          redraw_thread_active = false;
1246  
1247          // Unlock frame buffer
# Line 1369 | Line 1538 | static int kc_decode(SDL_keysym const &
1538  
1539          case SDLK_1: case SDLK_EXCLAIM: return 0x12;
1540          case SDLK_2: case SDLK_AT: return 0x13;
1541 < //      case SDLK_3: case SDLK_numbersign: return 0x14;
1541 >        case SDLK_3: case SDLK_HASH: return 0x14;
1542          case SDLK_4: case SDLK_DOLLAR: return 0x15;
1543 < //      case SDLK_5: case SDLK_percent: return 0x17;
1543 >        case SDLK_5: return 0x17;
1544          case SDLK_6: return 0x16;
1545          case SDLK_7: return 0x1a;
1546          case SDLK_8: return 0x1c;
1547          case SDLK_9: return 0x19;
1548          case SDLK_0: return 0x1d;
1549  
1550 < //      case SDLK_BACKQUOTE: case SDLK_asciitilde: return 0x0a;
1550 >        case SDLK_BACKQUOTE: return 0x0a;
1551          case SDLK_MINUS: case SDLK_UNDERSCORE: return 0x1b;
1552          case SDLK_EQUALS: case SDLK_PLUS: return 0x18;
1553 < //      case SDLK_bracketleft: case SDLK_braceleft: return 0x21;
1554 < //      case SDLK_bracketright: case SDLK_braceright: return 0x1e;
1555 < //      case SDLK_BACKSLASH: case SDLK_bar: return 0x2a;
1553 >        case SDLK_LEFTBRACKET: return 0x21;
1554 >        case SDLK_RIGHTBRACKET: return 0x1e;
1555 >        case SDLK_BACKSLASH: return 0x2a;
1556          case SDLK_SEMICOLON: case SDLK_COLON: return 0x29;
1557 < //      case SDLK_apostrophe: case SDLK_QUOTEDBL: return 0x27;
1557 >        case SDLK_QUOTE: case SDLK_QUOTEDBL: return 0x27;
1558          case SDLK_COMMA: case SDLK_LESS: return 0x2b;
1559          case SDLK_PERIOD: case SDLK_GREATER: return 0x2f;
1560          case SDLK_SLASH: case SDLK_QUESTION: return 0x2c;
# Line 1417 | Line 1586 | static int kc_decode(SDL_keysym const &
1586          case SDLK_LMETA: return 0x3a;
1587          case SDLK_RMETA: return 0x3a;
1588   #endif
1589 +        case SDLK_LSUPER: return 0x3a; // "Windows" key
1590 +        case SDLK_RSUPER: return 0x3a;
1591          case SDLK_MENU: return 0x32;
1592          case SDLK_CAPSLOCK: return 0x39;
1593          case SDLK_NUMLOCK: return 0x47;
# Line 1810 | Line 1981 | static void video_refresh_dga_vosf(void)
1981                  tick_counter = 0;
1982                  if (mainBuffer.dirty) {
1983                          LOCK_VOSF;
1984 <                        update_display_dga_vosf();
1984 >                        update_display_dga_vosf(static_cast<driver_fullscreen *>(drv));
1985                          UNLOCK_VOSF;
1986                  }
1987          }
# Line 1874 | Line 2045 | static void VideoRefreshInit(void)
2045          }
2046   }
2047  
2048 + static inline void do_video_refresh(void)
2049 + {
2050 +        // Handle SDL events
2051 +        handle_events();
2052 +
2053 +        // Update display
2054 +        video_refresh();
2055 +
2056 + #ifdef SHEEPSHAVER
2057 +        // Set new cursor image if it was changed
2058 +        if (cursor_changed && sdl_cursor) {
2059 +                cursor_changed = false;
2060 +                SDL_FreeCursor(sdl_cursor);
2061 +                sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, MacCursor[2], MacCursor[3]);
2062 +                if (sdl_cursor)
2063 +                        SDL_SetCursor(sdl_cursor);
2064 +        }
2065 + #endif
2066 +
2067 +        // Set new palette if it was changed
2068 +        handle_palette_changes();
2069 + }
2070 +
2071 + // This function is called on non-threaded platforms from a timer interrupt
2072 + void VideoRefresh(void)
2073 + {
2074 +        // We need to check redraw_thread_active to inhibit refreshed during
2075 +        // mode changes on non-threaded platforms
2076 +        if (!redraw_thread_active)
2077 +                return;
2078 +
2079 +        // Process pending events and update display
2080 +        do_video_refresh();
2081 + }
2082 +
2083   const int VIDEO_REFRESH_HZ = 60;
2084   const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2085  
2086 + #ifndef USE_CPU_EMUL_SERVICES
2087   static int redraw_func(void *arg)
2088   {
2089          uint64 start = GetTicks_usec();
# Line 1902 | Line 2109 | static int redraw_func(void *arg)
2109                  }
2110   #endif
2111  
2112 <                // Handle SDL events
2113 <                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();
2112 >                // Process pending events and update display
2113 >                do_video_refresh();
2114          }
2115  
2116          uint64 end = GetTicks_usec();
2117          D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
2118          return 0;
2119   }
2120 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines