Revert SDL2 hotkey code to original version

This reverts commits 3b7a89aad9
and e28a9fc3a9.
This commit is contained in:
robxnano 2022-09-30 22:59:40 +01:00
parent 3b7a89aad9
commit 4c1c549ed6
3 changed files with 130 additions and 72 deletions

View File

@ -69,9 +69,6 @@
#define DEBUG 0
#include "debug.h"
#define CODE_INVALID -1
#define CODE_HOTKEY -2
// Supported video modes
using std::vector;
static vector<VIDEO_MODE> VideoModes;
@ -877,7 +874,7 @@ static void keycode_init(void)
// Default translation table
for (int i=0; i<256; i++)
keycode_table[i] = CODE_INVALID;
keycode_table[i] = -1;
// Search for server vendor string, then read keycodes
char video_driver[256];
@ -1661,8 +1658,8 @@ static int modify_opt_cmd(int code) {
}
/*
* Translate key event to Mac keycode, returns CODE_INVALID if no keycode was found
* and CODE_HOTKEY if the key was recognized as a hotkey
* Translate key event to Mac keycode, returns -1 if no keycode was found
* and -2 if the key was recognized as a hotkey
*/
static int kc_decode(SDL_keysym const & ks, bool key_down)
@ -1718,8 +1715,8 @@ static int kc_decode(SDL_keysym const & ks, bool key_down)
case SDLK_PERIOD: case SDLK_GREATER: return 0x2f;
case SDLK_SLASH: case SDLK_QUESTION: return 0x2c;
case SDLK_TAB: if (is_hotkey_down(ks)) {if (!key_down) drv->suspend(); return CODE_HOTKEY;} else return 0x30;
case SDLK_RETURN: if (is_hotkey_down(ks)) {if (!key_down) toggle_fullscreen = true; return CODE_HOTKEY;} else return 0x24;
case SDLK_TAB: if (is_hotkey_down(ks)) {if (!key_down) drv->suspend(); return -2;} else return 0x30;
case SDLK_RETURN: if (is_hotkey_down(ks)) {if (!key_down) toggle_fullscreen = true; return -2;} else return 0x24;
case SDLK_SPACE: return 0x31;
case SDLK_BACKSPACE: return 0x33;
@ -1730,11 +1727,13 @@ static int kc_decode(SDL_keysym const & ks, bool key_down)
case SDLK_PAGEUP: return 0x74;
case SDLK_PAGEDOWN: return 0x79;
case SDLK_LCTRL: case SDLK_RCTRL: ctrl_down = key_down; return 0x36;
case SDLK_LSHIFT: case SDLK_RSHIFT: return 0x38;
case SDLK_LALT: case SDLK_RALT: alt_down = key_down; return 0x3a;
case SDLK_LMETA: case SDLK_RMETA: super_down = key_down; return 0x37;
case SDLK_LSUPER: case SDLK_RSUPER: super_down = key_down; return 0x37; // "Windows" key
case SDLK_LCTRL: return 0x36;
case SDLK_RCTRL: return 0x36;
case SDLK_LSHIFT: return 0x38;
case SDLK_RSHIFT: return 0x38;
case SDLK_LALT: case SDLK_RALT: return 0x3a;
case SDLK_LMETA: case SDLK_RMETA: return 0x37;
case SDLK_LSUPER: case SDLK_RSUPER: return 0x37; // "Windows" key
case SDLK_MENU: return 0x32;
case SDLK_CAPSLOCK: return 0x39;
case SDLK_NUMLOCK: return 0x47;
@ -1744,13 +1743,13 @@ static int kc_decode(SDL_keysym const & ks, bool key_down)
case SDLK_LEFT: return 0x3b;
case SDLK_RIGHT: return 0x3c;
case SDLK_ESCAPE: if (is_hotkey_down(ks)) {if (!key_down) { quit_full_screen = true; emerg_quit = true; } return CODE_HOTKEY;} else return 0x35;
case SDLK_ESCAPE: if (is_hotkey_down(ks)) {if (!key_down) { quit_full_screen = true; emerg_quit = true; } return -2;} else return 0x35;
case SDLK_F1: if (is_hotkey_down(ks)) {if (!key_down) SysMountFirstFloppy(); return CODE_HOTKEY;} else return 0x7a;
case SDLK_F1: if (is_hotkey_down(ks)) {if (!key_down) SysMountFirstFloppy(); return -2;} else return 0x7a;
case SDLK_F2: return 0x78;
case SDLK_F3: return 0x63;
case SDLK_F4: return 0x76;
case SDLK_F5: if (is_hotkey_down(ks)) {if (!key_down) drv->toggle_mouse_grab(); return CODE_HOTKEY;} else return 0x60;
case SDLK_F5: if (is_hotkey_down(ks)) {if (!key_down) drv->toggle_mouse_grab(); return -2;} else return 0x60;
case SDLK_F6: return 0x61;
case SDLK_F7: return 0x62;
case SDLK_F8: return 0x64;
@ -1782,7 +1781,7 @@ static int kc_decode(SDL_keysym const & ks, bool key_down)
case SDLK_KP_EQUALS: return 0x51;
}
D(bug("Unhandled SDL keysym: %d\n", ks.sym));
return CODE_INVALID;
return -1;
}
static int event2keycode(SDL_KeyboardEvent const &ev, bool key_down)
@ -1865,16 +1864,23 @@ static void handle_events(void)
// Keyboard
case SDL_KEYDOWN: {
int code = CODE_INVALID;
// This is called to process the hotkeys
if (use_keycodes && !is_modifier_key(event.key) && event2keycode(event.key, true) != CODE_HOTKEY)
int code = -1;
if (use_keycodes && !is_modifier_key(event.key)) {
if (event2keycode(event.key, true) != -2) // This is called to process the hotkeys
code = keycode_table[event.key.keysym.scancode & 0xff];
if (code == CODE_INVALID)
} else
code = event2keycode(event.key, true);
if (code >= 0) {
if (!emul_suspended) {
if (code == 0x3a || code == 0x37)
code = modify_opt_cmd(code);
if (code == 0x36) {
ctrl_down = true;
} else if (code == 0x3a) {
alt_down = true;
code = modify_opt_cmd(code);
} else if (code == 0x37) {
super_down = true;
code = modify_opt_cmd(code);
}
if (code == 0x39) { // Caps Lock pressed
if (caps_on) {
ADBKeyUp(code);
@ -1893,15 +1899,22 @@ static void handle_events(void)
break;
}
case SDL_KEYUP: {
int code = CODE_INVALID;
// This is called to process the hotkeys
if (use_keycodes && !is_modifier_key(event.key) && event2keycode(event.key, false) != CODE_HOTKEY)
int code = -1;
if (use_keycodes && !is_modifier_key(event.key)) {
if (event2keycode(event.key, false) != -2) // This is called to process the hotkeys
code = keycode_table[event.key.keysym.scancode & 0xff];
if (code == CODE_INVALID)
} else
code = event2keycode(event.key, false);
if (code >= 0) {
if (code == 0x3a ||code == 0x37)
code = modify_opt_cmd(code);
if (code == 0x36) {
ctrl_down = false;
} else if (code == 0x3a) {
alt_down = false;
code = modify_opt_cmd(code);
} else if (code == 0x37) {
super_down = false;
code = modify_opt_cmd(code);
}
if (code == 0x39) { // Caps Lock released
if (caps_on) {
ADBKeyUp(code);

View File

@ -2142,10 +2142,12 @@ static int kc_decode(SDL_Keysym const & ks, bool key_down)
case SDLK_PAGEUP: return 0x74;
case SDLK_PAGEDOWN: return 0x79;
case SDLK_LCTRL: SDLK_RCTRL: ctrl_down = key_down; return 0x36;
case SDLK_LSHIFT: SDLK_RSHIFT: return 0x38;
case SDLK_LALT: case SDLK_RALT: alt_down = key_down; return 0x3a;
case SDLK_LGUI: case SDLK_RGUI: super_down = key_down; return 0x37;
case SDLK_LCTRL: return 0x36;
case SDLK_RCTRL: return 0x36;
case SDLK_LSHIFT: return 0x38;
case SDLK_RSHIFT: return 0x38;
case SDLK_LALT: case SDLK_RALT: return 0x3a;
case SDLK_LGUI: case SDLK_RGUI: return 0x37;
case SDLK_MENU: return 0x32;
case SDLK_CAPSLOCK: return 0x39;
case SDLK_NUMLOCKCLEAR: return 0x47;
@ -2355,15 +2357,24 @@ static void handle_events(void)
code = event2keycode(event.key, true);
if (code >= 0) {
if (!emul_suspended) {
if (code == 0x3a || code == 0x37)
code = modify_opt_cmd(code);
if (code == 0x39)
if (code == 0x36) {
ctrl_down = true;
} else if (code == 0x3a) {
alt_down = true;
code = modify_opt_cmd(code);
} else if (code == 0x37) {
super_down = true;
code = modify_opt_cmd(code);
}
if (code == 0x39) {
(SDL_GetModState() & KMOD_CAPS ? ADBKeyDown : ADBKeyUp)(code);
else
} else
ADBKeyDown(code);
} else if (code == 0x31)
} else {
if (code == 0x31)
drv->resume(); // Space wakes us up
}
}
break;
}
@ -2374,8 +2385,15 @@ static void handle_events(void)
if (code == CODE_INVALID)
code = event2keycode(event.key, false);
if (code >= 0) {
if (code == 0x3a ||code == 0x37)
code = modify_opt_cmd(code);
if (code == 0x36) {
ctrl_down = false;
} else if (code == 0x3a) {
alt_down = false;
code = modify_opt_cmd(code);
} else if (code == 0x37) {
super_down = false;
code = modify_opt_cmd(code);
}
if (code != 0x39)
ADBKeyUp(code);
}

View File

@ -69,9 +69,6 @@
#define DEBUG 0
#include "debug.h"
#define CODE_INVALID -1
#define CODE_HOTKEY -2
// Supported video modes
static vector<video_mode> VideoModes;
@ -481,14 +478,15 @@ static void set_window_name(Window w, int name)
static void set_window_name(Window w, bool mouse_grabbed) {
const char *title = PrefsFindString("title");
std::string s = title ? title : GetString(STR_WINDOW_TITLE);
if (mouse_grabbed) {
s += GetString(STR_WINDOW_TITLE_GRABBED_PRE);
if (mouse_grabbed)
{
s += GetString(STR_WINDOW_TITLE_GRABBED_PRE);
int hotkey = PrefsFindInt32("hotkey");
hotkey = hotkey ? hotkey : 1;
if (hotkey & 1) s += GetString(STR_WINDOW_TITLE_GRABBED1);
if (hotkey & 2) s += GetString(STR_WINDOW_TITLE_GRABBED2);
if (hotkey & 4) s += GetString(STR_WINDOW_TITLE_GRABBED4);
s += GetString(STR_WINDOW_TITLE_GRABBED_POST);
if (hotkey & 2) s += GetString(STR_WINDOW_TITLE_GRABBED2);
if (hotkey & 4) s += GetString(STR_WINDOW_TITLE_GRABBED4);
s += GetString(STR_WINDOW_TITLE_GRABBED_POST);
}
XStoreName(x_display, w, s.c_str());
XSetIconName(x_display, w, GetString(STR_WINDOW_TITLE));
@ -1470,7 +1468,7 @@ static void keycode_init(void)
// Default translation table
for (int i=0; i<256; i++)
keycode_table[i] = CODE_INVALID;
keycode_table[i] = -1;
// Search for server vendor string, then read keycodes
const char *vendor = ServerVendor(x_display);
@ -1757,7 +1755,7 @@ bool VideoInit(bool classic)
default_width = -1; default_height = -1; // use entire screen
#endif
#ifdef ENABLE_XF86_DGA
} else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2) {
} else if (has_dga & sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2) {
display_type = DISPLAY_DGA;
#endif
}
@ -2015,8 +2013,8 @@ void X11_monitor_desc::switch_to_current_mode(void)
/*
* Translate key event to Mac keycode, returns CODE_INVALID if no keycode was found
* and CODE_HOTKEY if the key was recognized as a hotkey
* Translate key event to Mac keycode, returns -1 if no keycode was found
* and -2 if the key was recognized as a hotkey
*/
static int kc_decode(KeySym ks, bool key_down)
@ -2072,7 +2070,7 @@ static int kc_decode(KeySym ks, bool key_down)
case XK_period: case XK_greater: return 0x2f;
case XK_slash: case XK_question: return 0x2c;
case XK_Tab: if (is_hotkey_down()) {if (key_down) drv->suspend(); return CODE_HOTKEY;} else return 0x30;
case XK_Tab: if (is_hotkey_down()) {if (key_down) drv->suspend(); return -2;} else return 0x30;
case XK_Return: return 0x24;
case XK_space: return 0x31;
case XK_BackSpace: return 0x33;
@ -2089,10 +2087,14 @@ static int kc_decode(KeySym ks, bool key_down)
case XK_Page_Down: return 0x79;
#endif
case XK_Control_L: case XK_Control_R: if (!emul_suspended) ctrl_down = key_down; return 0x36;
case XK_Shift_L: case XK_Shift_R: return 0x38;
case XK_Alt_L: case XK_Alt_R: if (!emul_suspended) alt_down = key_down; return 0x3a;
case XK_Meta_L: case XK_Meta_R: if (!emul_suspended) super_down = key_down; return 0x37;
case XK_Control_L: return 0x36;
case XK_Control_R: return 0x36;
case XK_Shift_L: return 0x38;
case XK_Shift_R: return 0x38;
case XK_Alt_L: return 0x3a;
case XK_Alt_R: return 0x3a;
case XK_Meta_L: return 0x37;
case XK_Meta_R: return 0x37;
case XK_Menu: return 0x32;
case XK_Caps_Lock: return 0x39;
case XK_Num_Lock: return 0x47;
@ -2102,13 +2104,13 @@ static int kc_decode(KeySym ks, bool key_down)
case XK_Left: return 0x3b;
case XK_Right: return 0x3c;
case XK_Escape: if (is_hotkey_down()) {if (key_down) { quit_full_screen = true; emerg_quit = true; } return CODE_HOTKEY;} else return 0x35;
case XK_Escape: if (is_hotkey_down()) {if (key_down) { quit_full_screen = true; emerg_quit = true; } return -2;} else return 0x35;
case XK_F1: if (is_hotkey_down()) {if (key_down) SysMountFirstFloppy(); return CODE_HOTKEY;} else return 0x7a;
case XK_F1: if (is_hotkey_down()) {if (key_down) SysMountFirstFloppy(); return -2;} else return 0x7a;
case XK_F2: return 0x78;
case XK_F3: return 0x63;
case XK_F4: return 0x76;
case XK_F5: if (is_hotkey_down()) {if (key_down) drv->toggle_mouse_grab(); return CODE_HOTKEY;} else return 0x60;
case XK_F5: if (is_hotkey_down()) {if (key_down) drv->toggle_mouse_grab(); return -2;} else return 0x60;
case XK_F6: return 0x61;
case XK_F7: return 0x62;
case XK_F8: return 0x64;
@ -2153,7 +2155,7 @@ static int kc_decode(KeySym ks, bool key_down)
case XK_KP_Enter: return 0x4c;
case XK_KP_Equal: return 0x51;
}
return CODE_INVALID;
return -1;
}
static int event2keycode(XKeyEvent &ev, bool key_down)
@ -2166,11 +2168,11 @@ static int event2keycode(XKeyEvent &ev, bool key_down)
int as = kc_decode(ks, key_down);
if (as >= 0)
return as;
if (as == CODE_HOTKEY)
if (as == -2)
return as;
} while (ks != NoSymbol);
return CODE_INVALID;
return -1;
}
@ -2245,10 +2247,22 @@ static void handle_events(void)
// Keyboard
case KeyPress: {
int code = event2keycode(event.xkey, true);
if (use_keycodes && code != CODE_HOTKEY) // This is called to process the hotkeys
code = keycode_table[event.xkey.keycode & 0xff];
if (code == 0x3a || code == 0x37)
code = modify_opt_cmd(code);
if(!emul_suspended)
{
if (code == 0x36) {
ctrl_down = true;
} else if (code == 0x3a) {
alt_down = true;
code = modify_opt_cmd(code);
} else if (code == 0x37) {
super_down = true;
code = modify_opt_cmd(code);
}
}
if (use_keycodes) {
if (code != -2) // This is called to process the hotkeys
code = keycode_table[event.xkey.keycode & 0xff];
}
if (code >= 0) {
if (!emul_suspended) {
if (code == 0x39) { // Caps Lock pressed
@ -2270,12 +2284,25 @@ static void handle_events(void)
}
case KeyRelease: {
int code = event2keycode(event.xkey, false);
if (use_keycodes && code != CODE_HOTKEY) // This is called to process the hotkeys
code = keycode_table[event.xkey.keycode & 0xff];
if (code == 0x3a || code == 0x37)
code = modify_opt_cmd(code);
if (code >= 0 && code != 0x39) // Don't propagate Caps Lock releases
if(!emul_suspended)
{
if (code == 0x36) {
ctrl_down = false;
} else if (code == 0x3a) {
alt_down = false;
code = modify_opt_cmd(code);
} else if (code == 0x37) {
super_down = false;
code = modify_opt_cmd(code);
}
}
if (use_keycodes) {
if (code != -2) // This is called to process the hotkeys
code = keycode_table[event.xkey.keycode & 0xff];
}
if (code >= 0 && code != 0x39) { // Don't propagate Caps Lock releases
ADBKeyUp(code);
}
break;
}