shithub: choc

Download patch

ref: 05b7e25ca466172d071d17e9f3f665f992342431
parent: 5ad3fa6c5ee4731f8688931ff76595df55dd9bc5
parent: c7d2322532782e53c968d08ebe361ad412322a83
author: Simon Howard <fraggle+github@gmail.com>
date: Thu Apr 7 15:58:09 EDT 2016

Merge pull request #691 from jmtd/sdl2-branch-jmtd-input

Input-related fixups for textscreen/setup in SDL2

--- a/src/i_video.c
+++ b/src/i_video.c
@@ -1168,16 +1168,6 @@
 
     memset(I_VideoBuffer, 0, SCREENWIDTH * SCREENHEIGHT);
 
-    // We need SDL to give us translated versions of keys as well
-
-    // SDL2-TODO SDL_EnableUNICODE(1);
-
-    // Repeat key presses - this is what Vanilla Doom does
-    // Not sure about repeat rate - probably dependent on which DOS
-    // driver is used.  This is good enough though.
-
-    // SDL2-TODO SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
-
     // clear out any events waiting at the start and center the mouse
   
     while (SDL_PollEvent(&dummy));
--- a/src/setup/display.c
+++ b/src/setup/display.c
@@ -65,11 +65,6 @@
     { 0, 0},
 };
 
-// List of fullscreen modes generated at runtime
-
-static screen_mode_t *screen_modes_fullscreen = NULL;
-static int num_screen_modes_fullscreen;
-
 static int vidmode = 0;
 
 static char *video_driver = "";
@@ -88,7 +83,7 @@
 int png_screenshots = 0;
 
 // These are the last screen width/height values that were chosen by the
-// user.  These are used when finding the "nearest" mode, so when 
+// user.  These are used when finding the "nearest" mode, so when
 // changing the fullscreen / aspect ratio options, the setting does not
 // jump around.
 
@@ -141,82 +136,6 @@
     selected_screen_height = screen_height;
 }
 
-static int GoodFullscreenMode(screen_mode_t *mode)
-{
-    int w, h;
-
-    w = mode->w;
-    h = mode->h;
-
-    // 320x200 and 640x400 are always good (special case)
-
-    if ((w == 320 && h == 200) || (w == 640 && h == 400))
-    {
-        return 1;
-    }
-
-    // Special case: 320x240 letterboxed mode is okay (but not aspect
-    // ratio corrected 320x240)
-
-    if (w == 320 && h == 240 && !aspect_ratio_correct)
-    {
-        return 1;
-    }
-
-    // Ignore all modes less than 640x480
-
-    return w >= 640 && h >= 480;
-}
-
-// Build screen_modes_fullscreen
-
-static void BuildFullscreenModesList(void)
-{
-    screen_mode_t *m1;
-    screen_mode_t *m2;
-    screen_mode_t m;
-    int display = 0;  // SDL2-TODO
-    int num_modes;
-    int i;
-
-    // Free the existing modes list, if one exists
-
-    if (screen_modes_fullscreen != NULL)
-    {
-        free(screen_modes_fullscreen);
-    }
-
-    num_modes = SDL_GetNumDisplayModes(display);
-    screen_modes_fullscreen = calloc(num_modes, sizeof(screen_mode_t) + 1);
-
-    for (i = 0; i < SDL_GetNumDisplayModes(display); ++i)
-    {
-        SDL_DisplayMode mode;
-
-        SDL_GetDisplayMode(display, i, &mode);
-        screen_modes_fullscreen[i].w = mode.w;
-        screen_modes_fullscreen[i].h = mode.h;
-        // SDL2-TODO: Deal with duplicate modes due to different pixel formats.
-    }
-
-    screen_modes_fullscreen[num_modes].w = 0;
-    screen_modes_fullscreen[num_modes].h = 0;
-
-    // Reverse the order of the modes list (smallest modes first)
-
-    for (i=0; i<num_modes / 2; ++i)
-    {
-        m1 = &screen_modes_fullscreen[i];
-        m2 = &screen_modes_fullscreen[num_modes - 1 - i];
-
-        memcpy(&m, m1, sizeof(screen_mode_t));
-        memcpy(m1, m2, sizeof(screen_mode_t));
-        memcpy(m2, &m, sizeof(screen_mode_t));
-    }
-
-    num_screen_modes_fullscreen = num_modes;
-}
-
 static int FindBestMode(screen_mode_t *modes)
 {
     int i;
@@ -229,11 +148,6 @@
 
     for (i=0; modes[i].w != 0; ++i)
     {
-        if (fullscreen && !GoodFullscreenMode(&modes[i]))
-        {
-            continue;
-        }
-
         diff = (selected_screen_width - modes[i].w)
                   * (selected_screen_width - modes[i].w) 
              + (selected_screen_height - modes[i].h)
@@ -260,17 +174,8 @@
 
     // Pick which modes list to use
 
-    if (fullscreen)
+    if (aspect_ratio_correct)
     {
-        if (screen_modes_fullscreen == NULL)
-        {
-            BuildFullscreenModesList();
-        }
-
-        modes = screen_modes_fullscreen;
-    }
-    else if (aspect_ratio_correct) 
-    {
         modes = screen_modes_scaled;
     }
     else
@@ -283,15 +188,8 @@
     TXT_ClearTable(modes_table);
     TXT_SetColumnWidths(modes_table, 14, 14, 14, 14, 14);
 
-    for (i=0; modes[i].w != 0; ++i) 
+    for (i=0; modes[i].w != 0; ++i)
     {
-        // Skip bad fullscreen modes
-
-        if (fullscreen && !GoodFullscreenMode(&modes[i]))
-        {
-            continue;
-        }
-
         M_snprintf(buf, sizeof(buf), "%ix%i", modes[i].w, modes[i].h);
         rbutton = TXT_NewRadioButton(buf, &vidmode, i);
         TXT_AddWidget(modes_table, rbutton);
@@ -310,21 +208,6 @@
     }
 }
 
-static void UpdateModeSeparator(TXT_UNCAST_ARG(widget),
-                                TXT_UNCAST_ARG(separator))
-{
-    TXT_CAST_ARG(txt_separator_t, separator);
-
-    if (fullscreen)
-    {
-        TXT_SetSeparatorLabel(separator, "Screen mode");
-    }
-    else
-    {
-        TXT_SetSeparatorLabel(separator, "Window size");
-    }
-}
-
 static void AdvancedDisplayConfig(TXT_UNCAST_ARG(widget),
                                   TXT_UNCAST_ARG(modes_table))
 {
@@ -376,7 +259,6 @@
 {
     txt_window_t *window;
     txt_table_t *modes_table;
-    txt_separator_t *modes_separator;
     txt_window_action_t *advanced_button;
     txt_checkbox_t *fs_checkbox;
     int num_columns;
@@ -401,37 +283,20 @@
     // keep a limit of six lines by increasing the number of
     // columns.  In extreme cases, the window is moved up slightly.
 
-    BuildFullscreenModesList();
-
-    if (num_screen_modes_fullscreen <= 24)
-    {
-        num_columns = 3;
-    }
-    else if (num_screen_modes_fullscreen <= 40)
-    {
-        num_columns = 4;
-    }
-    else
-    {
-        num_columns = 5;
-    }
-
+    num_columns = 3;
     modes_table = TXT_NewTable(num_columns);
 
     // Build window:
 
-    TXT_AddWidget(window,
-                  fs_checkbox = TXT_NewCheckBox("Full screen", &fullscreen));
-
     TXT_AddWidgets(window,
-                   modes_separator = TXT_NewSeparator(""),
+                   TXT_NewSeparator("Window size"),
                    modes_table,
+                   TXT_NewSeparator("Options"),
+                   fs_checkbox = TXT_NewCheckBox("Full screen", &fullscreen),
                    NULL);
 
     TXT_SignalConnect(fs_checkbox, "changed",
                       GenerateModesTable, modes_table);
-    TXT_SignalConnect(fs_checkbox, "changed",
-                      UpdateModeSeparator, modes_separator);
 
     // How many rows high will the configuration window be?
     // Need to take into account number of fullscreen modes, and also
@@ -439,7 +304,7 @@
     // The windowed modes list is four rows, so take the maximum of
     // windowed and fullscreen.
 
-    num_rows = (num_screen_modes_fullscreen + num_columns - 1) / num_columns;
+    num_rows = (num_columns - 1) / num_columns;
 
     if (num_rows < 4)
     {
@@ -464,7 +329,6 @@
                                   TXT_SCREEN_W / 2, window_y);
 
     GenerateModesTable(NULL, modes_table);
-    UpdateModeSeparator(NULL, modes_separator);
 
     // Button to open "advanced" window.
     // Need to pass a pointer to the modes table, as some of the options
--- a/textscreen/txt_sdl.c
+++ b/textscreen/txt_sdl.c
@@ -67,6 +67,8 @@
 // normal_font otherwise.
 static const txt_font_t highdpi_font = { "normal-highdpi", NULL, 8, 16 };
 
+static const int scancode_translate_table[] = SCANCODE_TO_KEYS_ARRAY;
+
 //#define TANGO
 
 #ifndef TANGO
@@ -304,7 +306,6 @@
     SDL_LockSurface(screenbuffer);
     SDL_SetPaletteColors(screenbuffer->format->palette, ega_colors, 0, 16);
     SDL_UnlockSurface(screenbuffer);
-    // SDL2-TODO SDL_EnableUNICODE(1);
 
     screendata = malloc(TXT_SCREEN_W * TXT_SCREEN_H * 2);
     memset(screendata, 0, TXT_SCREEN_W * TXT_SCREEN_H * 2);
@@ -315,8 +316,6 @@
     // Repeat key presses so we can hold down arrows to scroll down the
     // menu, for example. This is what setup.exe does.
 
-    // SDL2-TODO SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
-
     return 1;
 }
 
@@ -478,123 +477,38 @@
 // Translates the SDL key
 //
 
+// XXX: duplicate from doomtype.h
+#define arrlen(array) (sizeof(array) / sizeof(*array))
+
 static int TranslateKey(SDL_Keysym *sym)
 {
-    switch(sym->sym)
-    {
-        case SDLK_LEFT:        return KEY_LEFTARROW;
-        case SDLK_RIGHT:       return KEY_RIGHTARROW;
-        case SDLK_DOWN:        return KEY_DOWNARROW;
-        case SDLK_UP:          return KEY_UPARROW;
-        case SDLK_ESCAPE:      return KEY_ESCAPE;
-        case SDLK_RETURN:      return KEY_ENTER;
-        case SDLK_TAB:         return KEY_TAB;
-        case SDLK_F1:          return KEY_F1;
-        case SDLK_F2:          return KEY_F2;
-        case SDLK_F3:          return KEY_F3;
-        case SDLK_F4:          return KEY_F4;
-        case SDLK_F5:          return KEY_F5;
-        case SDLK_F6:          return KEY_F6;
-        case SDLK_F7:          return KEY_F7;
-        case SDLK_F8:          return KEY_F8;
-        case SDLK_F9:          return KEY_F9;
-        case SDLK_F10:         return KEY_F10;
-        case SDLK_F11:         return KEY_F11;
-        case SDLK_F12:         return KEY_F12;
-        case SDLK_PRINTSCREEN:       return KEY_PRTSCR;
+    int scancode = sym->scancode;
 
-        case SDLK_BACKSPACE:   return KEY_BACKSPACE;
-        case SDLK_DELETE:      return KEY_DEL;
-
-        case SDLK_PAUSE:       return KEY_PAUSE;
-
-        case SDLK_LSHIFT:
-        case SDLK_RSHIFT:
-                               return KEY_RSHIFT;
-
-        case SDLK_LCTRL:
-        case SDLK_RCTRL:
-                               return KEY_RCTRL;
-
-        case SDLK_LALT:
-        case SDLK_RALT:
-                               return KEY_RALT;
-
-        case SDLK_CAPSLOCK:    return KEY_CAPSLOCK;
-        case SDLK_SCROLLLOCK:   return KEY_SCRLCK;
-
-        case SDLK_HOME:        return KEY_HOME;
-        case SDLK_INSERT:      return KEY_INS;
-        case SDLK_END:         return KEY_END;
-        case SDLK_PAGEUP:      return KEY_PGUP;
-        case SDLK_PAGEDOWN:    return KEY_PGDN;
-
-#ifdef SDL_HAVE_APP_KEYS
-        case SDLK_APP1:        return KEY_F1;
-        case SDLK_APP2:        return KEY_F2;
-        case SDLK_APP3:        return KEY_F3;
-        case SDLK_APP4:        return KEY_F4;
-        case SDLK_APP5:        return KEY_F5;
-        case SDLK_APP6:        return KEY_F6;
-#endif
-
-        default:               break;
-    }
-
-    // Returned value is different, depending on whether key mapping is
-    // enabled.  Key mapping is preferable most of the time, for typing
-    // in text, etc.  However, when we want to read raw keyboard codes
-    // for the setup keyboard configuration dialog, we want the raw
-    // key code.
-
-    if (key_mapping)
+    switch (scancode)
     {
-        // Unicode characters beyond the ASCII range need to be
-        // mapped up into textscreen's Unicode range.
+        case SDL_SCANCODE_LCTRL:
+        case SDL_SCANCODE_RCTRL:
+            return KEY_RCTRL;
 
-#if 0
-    // SDL2-TODO
-        if (sym->unicode < 128)
-        {
-            return sym->unicode;
-        }
-        else
-        {
-            return sym->unicode - 128 + TXT_UNICODE_BASE;
-        }
-#endif
-        return 0;
-    }
-    else
-    {
-        // Keypad mapping is only done when we want a raw value:
-        // most of the time, the keypad should behave as it normally
-        // does.
+        case SDL_SCANCODE_LSHIFT:
+        case SDL_SCANCODE_RSHIFT:
+            return KEY_RSHIFT;
 
-        switch (sym->sym)
-        {
-            case SDLK_KP_0:         return KEYP_0;
-            case SDLK_KP_1:         return KEYP_1;
-            case SDLK_KP_2:         return KEYP_2;
-            case SDLK_KP_3:         return KEYP_3;
-            case SDLK_KP_4:         return KEYP_4;
-            case SDLK_KP_5:         return KEYP_5;
-            case SDLK_KP_6:         return KEYP_6;
-            case SDLK_KP_7:         return KEYP_7;
-            case SDLK_KP_8:         return KEYP_8;
-            case SDLK_KP_9:         return KEYP_9;
+        case SDL_SCANCODE_LALT:
+            return KEY_LALT;
 
-            case SDLK_KP_PERIOD:   return KEYP_PERIOD;
-            case SDLK_KP_MULTIPLY: return KEYP_MULTIPLY;
-            case SDLK_KP_PLUS:     return KEYP_PLUS;
-            case SDLK_KP_MINUS:    return KEYP_MINUS;
-            case SDLK_KP_DIVIDE:   return KEYP_DIVIDE;
-            case SDLK_KP_EQUALS:   return KEYP_EQUALS;
-            case SDLK_KP_ENTER:    return KEYP_ENTER;
+        case SDL_SCANCODE_RALT:
+            return KEY_RALT;
 
-            default:
-                return tolower(sym->sym);
-        }
+        default:
+            if (scancode >= 0 && scancode < arrlen(scancode_translate_table))
+            {
+                return scancode_translate_table[scancode];
+            }
+            else
+            {
+                return 0;
+            }
     }
 }