From 1e07bbcbb0ff3d151342350bdea83f457cc9747e Mon Sep 17 00:00:00 2001 From: Rosalie Wanders Date: Mon, 9 Nov 2020 23:01:33 +0100 Subject: [PATCH] osal_keys_unix: add linux support --- projects/msvc/GLideNUI.vcxproj | 1 + projects/msvc/GLideNUI.vcxproj.filters | 3 + src/CMakeLists.txt | 1 + src/Debugger.cpp | 1 + src/GLideNUI/ConfigDialog.cpp | 13 +- src/GLideNUI/GLideNUI.pro | 3 +- src/GLideNUI/QtKeyToHID.cpp | 393 +++++++++++++++++++++++++ src/GLideNUI/QtKeyToHID.h | 8 + src/VI.cpp | 2 + src/common/CommonAPIImpl_common.cpp | 4 + src/osal/CMakeLists.txt | 16 +- src/osal/osal_keys.h | 8 +- src/osal/osal_keys_linux.c | 202 +++++++++++++ src/osal/osal_keys_unix.c | 14 +- src/osal/osal_keys_win.c | 37 +-- 15 files changed, 663 insertions(+), 43 deletions(-) create mode 100644 src/GLideNUI/QtKeyToHID.cpp create mode 100644 src/GLideNUI/QtKeyToHID.h create mode 100644 src/osal/osal_keys_linux.c diff --git a/projects/msvc/GLideNUI.vcxproj b/projects/msvc/GLideNUI.vcxproj index e7ec7264..edc6217f 100644 --- a/projects/msvc/GLideNUI.vcxproj +++ b/projects/msvc/GLideNUI.vcxproj @@ -107,6 +107,7 @@ + true diff --git a/projects/msvc/GLideNUI.vcxproj.filters b/projects/msvc/GLideNUI.vcxproj.filters index 67b6c557..614be9ef 100644 --- a/projects/msvc/GLideNUI.vcxproj.filters +++ b/projects/msvc/GLideNUI.vcxproj.filters @@ -90,6 +90,9 @@ Generated Files + + Source Files + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e95ce6eb..60b5d415 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -130,6 +130,7 @@ set(GLideNUI_SOURCES GLideNUI/GLideNUI.cpp GLideNUI/ScreenShot.cpp GLideNUI/Settings.cpp + GLideNUI/QtKeyToHID.cpp GLideNUI/configDialog.ui GLideNUI/icon.qrc ) diff --git a/src/Debugger.cpp b/src/Debugger.cpp index 29339308..f8cb09ad 100644 --- a/src/Debugger.cpp +++ b/src/Debugger.cpp @@ -1265,6 +1265,7 @@ void Debugger::_drawDebugInfo() const f32 lry = -((f32)(winHeight * 5 / 8)* (2.0f * scaleY) - 1.0f); while (!osal_is_key_pressed(KEY_Insert, 0x0001)) { + osal_keys_update_state(); _debugKeys(); _drawFrameBuffer(frameBufferList().findBuffer(*m_curFBAddr)); _drawTextureCache(); diff --git a/src/GLideNUI/ConfigDialog.cpp b/src/GLideNUI/ConfigDialog.cpp index 94dea89d..d7a67d76 100644 --- a/src/GLideNUI/ConfigDialog.cpp +++ b/src/GLideNUI/ConfigDialog.cpp @@ -18,6 +18,7 @@ #include "FullscreenResolutions.h" #include "qevent.h" #include "osal_keys.h" +#include "QtKeyToHID.h" static struct @@ -1094,26 +1095,22 @@ public: QMessageBox::keyPressEvent(pEvent); return; } - m_nativeVK = pEvent->nativeVirtualKey(); + m_Key = pEvent->key(); QMessageBox::keyPressEvent(pEvent); close(); } - quint32 m_nativeVK = 0; + quint32 m_Key = Qt::Key::Key_unknown; }; void ConfigDialog::on_btn_clicked() { if (QPushButton* pBtn = qobject_cast(sender())) { if (QLabel* pLabel = pBtn->parent()->findChild< QLabel* >()) { - //QMessageBox::information(this, "Button was clicked!", e->text()); HotkeyMessageBox msgBox(this); - //msgBox.setWindowTitle("Hotkey"); - //msgBox.setText("Press a key"); msgBox.setInformativeText(QString("Press a key for ") + pLabel->text()); - //msgBox.setIcon(QMessageBox::Information); msgBox.exec(); - if (msgBox.m_nativeVK != 0) { - const unsigned int hidCode = osal_virtual_key_to_hid(msgBox.m_nativeVK); + if (msgBox.m_Key != Qt::Key::Key_unknown) { + const unsigned int hidCode = QtKeyToHID(msgBox.m_Key); for (quint32 idx = 0; idx < Config::HotKey::hkTotal; ++idx) { QListWidgetItem * pItem = ui->hotkeyListWidget->item(idx); HotkeyItemWidget* pWgt = (HotkeyItemWidget*)ui->hotkeyListWidget->itemWidget(pItem); diff --git a/src/GLideNUI/GLideNUI.pro b/src/GLideNUI/GLideNUI.pro index 7be53681..895c3c08 100644 --- a/src/GLideNUI/GLideNUI.pro +++ b/src/GLideNUI/GLideNUI.pro @@ -20,7 +20,8 @@ SOURCES += \ FullscreenResolutions_windows.cpp \ Settings.cpp \ ScreenShot.cpp \ - AboutDialog.cpp + AboutDialog.cpp \ + QtKeyToHID.cpp HEADERS += \ ConfigDialog.h \ diff --git a/src/GLideNUI/QtKeyToHID.cpp b/src/GLideNUI/QtKeyToHID.cpp new file mode 100644 index 00000000..5d21bdce --- /dev/null +++ b/src/GLideNUI/QtKeyToHID.cpp @@ -0,0 +1,393 @@ +#include "QtKeyToHID.h" +#include "keycode/keycode.h" + +unsigned int QtKeyToHID(quint32 key) +{ + unsigned int returnValue = KEY_None; + + switch (key) + { + default: + returnValue = KEY_None; + break; + case Qt::Key::Key_A: + returnValue = KEY_A; + break; + case Qt::Key::Key_B: + returnValue = KEY_B; + break; + case Qt::Key::Key_C: + returnValue = KEY_C; + break; + case Qt::Key::Key_D: + returnValue = KEY_D; + break; + case Qt::Key::Key_E: + returnValue = KEY_E; + break; + case Qt::Key::Key_F: + returnValue = KEY_F; + break; + case Qt::Key::Key_G: + returnValue = KEY_G; + break; + case Qt::Key::Key_H: + returnValue = KEY_H; + break; + case Qt::Key::Key_I: + returnValue = KEY_I; + break; + case Qt::Key::Key_J: + returnValue = KEY_J; + break; + case Qt::Key::Key_K: + returnValue = KEY_K; + break; + case Qt::Key::Key_L: + returnValue = KEY_L; + break; + case Qt::Key::Key_M: + returnValue = KEY_M; + break; + case Qt::Key::Key_N: + returnValue = KEY_N; + break; + case Qt::Key::Key_O: + returnValue = KEY_O; + break; + case Qt::Key::Key_P: + returnValue = KEY_P; + break; + case Qt::Key::Key_Q: + returnValue = KEY_Q; + break; + case Qt::Key::Key_R: + returnValue = KEY_R; + break; + case Qt::Key::Key_S: + returnValue = KEY_S; + break; + case Qt::Key::Key_T: + returnValue = KEY_T; + break; + case Qt::Key::Key_U: + returnValue = KEY_U; + break; + case Qt::Key::Key_V: + returnValue = KEY_V; + break; + case Qt::Key::Key_W: + returnValue = KEY_W; + break; + case Qt::Key::Key_X: + returnValue = KEY_X; + break; + case Qt::Key::Key_Y: + returnValue = KEY_Y; + break; + case Qt::Key::Key_Z: + returnValue = KEY_Z; + break; + case Qt::Key::Key_1: + returnValue = KEY_1; + break; + case Qt::Key::Key_2: + returnValue = KEY_2; + break; + case Qt::Key::Key_3: + returnValue = KEY_3; + break; + case Qt::Key::Key_4: + returnValue = KEY_4; + break; + case Qt::Key::Key_5: + returnValue = KEY_5; + break; + case Qt::Key::Key_6: + returnValue = KEY_6; + break; + case Qt::Key::Key_7: + returnValue = KEY_7; + break; + case Qt::Key::Key_8: + returnValue = KEY_8; + break; + case Qt::Key::Key_9: + returnValue = KEY_9; + break; + case Qt::Key::Key_0: + returnValue = KEY_0; + break; + case Qt::Key::Key_Escape: + returnValue = KEY_Escape; + break; + case Qt::Key::Key_Delete: + returnValue = KEY_Delete; + break; + case Qt::Key::Key_Tab: + returnValue = KEY_Tab; + break; + case Qt::Key::Key_Space: + returnValue = KEY_Space; + break; + case Qt::Key::Key_Minus: + returnValue = KEY_Minus; + break; + case Qt::Key::Key_Equal: + returnValue = KEY_Equals; + break; + case Qt::Key::Key_BracketLeft: + returnValue = KEY_LeftBracket; + break; + case Qt::Key::Key_BracketRight: + returnValue = KEY_RightBracket; + break; + case Qt::Key::Key_Backslash: + returnValue = KEY_Backslash; + break; + case Qt::Key::Key_Semicolon: + returnValue = KEY_Semicolon; + break; + case Qt::Key::Key_QuoteDbl: + returnValue = KEY_Quote; + break; + case Qt::Key::Key_Dead_Grave: + returnValue = KEY_Grave; + break; + case Qt::Key::Key_Comma: + returnValue = KEY_Comma; + break; + case Qt::Key::Key_Period: + returnValue = KEY_Period; + break; + case Qt::Key::Key_Slash: + returnValue = KEY_Slash; + break; + case Qt::Key::Key_CapsLock: + returnValue = KEY_CapsLock; + break; + case Qt::Key::Key_F1: + returnValue = KEY_F1; + break; + case Qt::Key::Key_F2: + returnValue = KEY_F2; + break; + case Qt::Key::Key_F3: + returnValue = KEY_F3; + break; + case Qt::Key::Key_F4: + returnValue = KEY_F4; + break; + case Qt::Key::Key_F5: + returnValue = KEY_F5; + break; + case Qt::Key::Key_F6: + returnValue = KEY_F6; + break; + case Qt::Key::Key_F7: + returnValue = KEY_F7; + break; + case Qt::Key::Key_F8: + returnValue = KEY_F8; + break; + case Qt::Key::Key_F9: + returnValue = KEY_F9; + break; + case Qt::Key::Key_F10: + returnValue = KEY_F10; + break; + case Qt::Key::Key_F11: + returnValue = KEY_F11; + break; + case Qt::Key::Key_F12: + returnValue = KEY_F12; + break; + /* TODO, this is a META key + case Qt::Key::Key_Screen: + returnValue = KEY_PrintScreen; + break;*/ + case Qt::Key::Key_ScrollLock: + returnValue = KEY_ScrollLock; + break; + case Qt::Key::Key_Pause: + returnValue = KEY_Pause; + break; + case Qt::Key::Key_Insert: + returnValue = KEY_Insert; + break; + case Qt::Key::Key_Home: + returnValue = KEY_Home; + break; + case Qt::Key::Key_PageUp: + returnValue = KEY_PageUp; + break; + /* ??? + case Qt::Key::Key_Delete: + returnValue = KEY_DeleteForward; + break;*/ + case Qt::Key::Key_End: + returnValue = KEY_End; + break; + case Qt::Key::Key_PageDown: + returnValue = KEY_PageDown; + break; + case Qt::Key::Key_Right: + returnValue = KEY_Right; + break; + case Qt::Key::Key_Left: + returnValue = KEY_Left; + break; + case Qt::Key::Key_Down: + returnValue = KEY_Down; + break; + case Qt::Key::Key_Up: + returnValue = KEY_Up; + break; + case Qt::Key::Key_NumLock: + returnValue = KP_NumLock; + break; + case Qt::Key::Key_division: + returnValue = KP_Divide; + break; + case Qt::Key::Key_multiply: + returnValue = KP_Multiply; + break; + /* ??? + case Qt::Key::Key_Minus: + returnValue = KP_Subtract; + break; */ + case Qt::Key::Key_Plus: + returnValue = KP_Add; + break; + case Qt::Key::Key_Enter: + returnValue = KP_Enter; + break; + /* see https://bugreports.qt.io/browse/QTBUG-2913 + case Qt::Key:: + returnValue = KP_1; + break; + case Qt::Key:: + returnValue = KP_2; + break; + case Qt::Key:: + returnValue = KP_3; + break; + case Qt::Key:: + returnValue = KP_4; + break; + case Qt::Key:: + returnValue = KP_5; + break; + case Qt::Key:: + returnValue = KP_6; + break; + case Qt::Key:: + returnValue = KP_7; + break; + case Qt::Key:: + returnValue = KP_8; + break; + case Qt::Key:: + returnValue = KP_9; + break; + case Qt::Key:: + returnValue = KP_0; + break; + case Qt::Key:: + returnValue = KP_Point; + break; + // ??? + case Qt::Key:: + returnValue = KEY_NonUSBackslash; + break; + case Qt::Key:: + returnValue = KP_Equals; + break; + */ + case Qt::Key::Key_F13: + returnValue = KEY_F13; + break; + case Qt::Key::Key_F14: + returnValue = KEY_F14; + break; + case Qt::Key::Key_F15: + returnValue = KEY_F15; + break; + case Qt::Key::Key_F16: + returnValue = KEY_F16; + break; + case Qt::Key::Key_F17: + returnValue = KEY_F17; + break; + case Qt::Key::Key_F18: + returnValue = KEY_F18; + break; + case Qt::Key::Key_F19: + returnValue = KEY_F19; + break; + case Qt::Key::Key_F20: + returnValue = KEY_F20; + break; + case Qt::Key::Key_F21: + returnValue = KEY_F21; + break; + case Qt::Key::Key_F22: + returnValue = KEY_F22; + break; + case Qt::Key::Key_F23: + returnValue = KEY_F23; + break; + case Qt::Key::Key_F24: + returnValue = KEY_F24; + break; + case Qt::Key::Key_Help: + returnValue = KEY_Help; + break; + case Qt::Key::Key_Menu: + returnValue = KEY_Menu; + break; + case Qt::Key::Key_VolumeMute: + returnValue = KEY_Mute; + break; + case Qt::Key::Key_SysReq: + returnValue = KEY_SysReq; + break; + case Qt::Key::Key_Return: + returnValue = KEY_Return; + break; + case Qt::Key::Key_Clear: + returnValue = KP_Clear; + break; + /* ??? + case Qt::Key::Key_d: + returnValue = KP_Decimal; + break; */ + case Qt::Key::Key_Control: + returnValue = KEY_LeftControl; + break; + case Qt::Key::Key_Shift: + returnValue = KEY_LeftShift; + break; + case Qt::Key::Key_Alt: + returnValue = KEY_LeftAlt; + break; + case Qt::Key::Key_Super_L: + returnValue = KEY_LeftGUI; + break; + /*case Qt::Key::Key_Control: + returnValue = KEY_RightControl; + break;*/ + /*case Qt::Key::Key_Shift: + returnValue = KEY_RightShift; + break;*/ + case Qt::Key::Key_AltGr: + returnValue = KEY_RightAlt; + break; + case Qt::Key::Key_Super_R: + returnValue = KEY_RightGUI; + break; + + } + + return returnValue; +} diff --git a/src/GLideNUI/QtKeyToHID.h b/src/GLideNUI/QtKeyToHID.h new file mode 100644 index 00000000..f35699e8 --- /dev/null +++ b/src/GLideNUI/QtKeyToHID.h @@ -0,0 +1,8 @@ +#ifndef QTKEYTOHID_H +#define QTKEYTOHID_H + +#include + +extern unsigned int QtKeyToHID(quint32 key); + +#endif // QTKEYTOHID_H diff --git a/src/VI.cpp b/src/VI.cpp index 25ba22be..46069500 100644 --- a/src/VI.cpp +++ b/src/VI.cpp @@ -101,6 +101,8 @@ void VI_UpdateSize() static void checkHotkeys() { + osal_keys_update_state(); + if (osal_is_key_pressed(KEY_G, 0x0001)) { SwitchDump(config.debug.dumpMode); } diff --git a/src/common/CommonAPIImpl_common.cpp b/src/common/CommonAPIImpl_common.cpp index fc4d9897..70f3eee9 100644 --- a/src/common/CommonAPIImpl_common.cpp +++ b/src/common/CommonAPIImpl_common.cpp @@ -18,6 +18,7 @@ #include #include "Graphics/Context.h" #include +#include PluginAPI & PluginAPI::get() { @@ -192,6 +193,7 @@ void PluginAPI::RomClosed() dwnd().stop(); GBI.destroy(); #endif + osal_keys_quit(); } int PluginAPI::RomOpen() @@ -210,6 +212,8 @@ int PluginAPI::RomOpen() if (!dwnd().start()) return 0; #endif + osal_keys_init(); + m_bRomOpen = true; return 1; diff --git a/src/osal/CMakeLists.txt b/src/osal/CMakeLists.txt index e59a30af..9543c035 100644 --- a/src/osal/CMakeLists.txt +++ b/src/osal/CMakeLists.txt @@ -5,11 +5,17 @@ project( osal ) LINK_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/lib ) if(UNIX) - set(OSAL_SOURCES osal_files_unix.c osal_keys_unix.c) - add_definitions( - -DNDEBUG - -DOS_LINUX - ) + if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") + set(OSAL_SOURCES osal_files_unix.c osal_keys_linux.c) + add_definitions( + -DOS_LINUX -DNDEBUG + ) + else() + set(OSAL_SOURCES osal_files_unix.c osal_keys_unix.c) + add_definitions( + -DNDEBUG + ) + endif() endif(UNIX) if(WIN32) diff --git a/src/osal/osal_keys.h b/src/osal/osal_keys.h index 8da774aa..f102a0df 100644 --- a/src/osal/osal_keys.h +++ b/src/osal/osal_keys.h @@ -8,9 +8,13 @@ extern "C" { #endif -EXPORT unsigned int CALL osal_is_key_pressed(unsigned int _key, unsigned int _mask); +EXPORT void CALL osal_keys_init(); -EXPORT unsigned int CALL osal_virtual_key_to_hid(unsigned int _key); +EXPORT void CALL osal_keys_quit(); + +EXPORT void CALL osal_keys_update_state(); + +EXPORT unsigned int CALL osal_is_key_pressed(unsigned int _key, unsigned int _mask); EXPORT const char * CALL osal_keycode_name(unsigned int _hidCode); diff --git a/src/osal/osal_keys_linux.c b/src/osal/osal_keys_linux.c new file mode 100644 index 00000000..8293a673 --- /dev/null +++ b/src/osal/osal_keys_linux.c @@ -0,0 +1,202 @@ +#include "osal_keys.h" + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +static const unsigned char LINUX_HID_TO_NATIVE[256] = { + 255, 255, 255, 255, KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, + KEY_F, KEY_G, KEY_H, KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, + KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T, KEY_U, + KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z, KEY_1, KEY_2, KEY_3, + KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0, 255, KEY_ESC, + KEY_DELETE, KEY_TAB, KEY_SPACE, KEY_MINUS, KEY_EQUAL, KEY_LEFTBRACE, + KEY_RIGHTBRACE, KEY_BACKSLASH, 255, KEY_SEMICOLON, KEY_APOSTROPHE, + KEY_GRAVE, KEY_COMMA, KEY_DOT, KEY_SLASH, KEY_CAPSLOCK, + KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, + KEY_F10, KEY_F11, KEY_F12, KEY_PRINT, KEY_SCROLLLOCK, KEY_PAUSE, + KEY_INSERT, KEY_HOME, KEY_PAGEUP, KEY_DELETE, + KEY_END, KEY_PAGEDOWN, KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP, + KEY_NUMLOCK, KEY_KPSLASH, KEY_KPASTERISK, KEY_KPMINUS, + KEY_KPPLUS, KEY_KPENTER, KEY_KP1, KEY_KP2, KEY_KP3, KEY_KP4, + KEY_KP5, KEY_KP6, KEY_KP7, KEY_KP8, KEY_KP9, KEY_KP0, KEY_KPDOT, + KEY_BACKSLASH, KEY_KPEQUAL, KEY_F13, KEY_F14, KEY_F15, KEY_F16, + KEY_F17, KEY_F18, KEY_F19, KEY_F20, KEY_F21, KEY_F22, KEY_F23, + KEY_F24, 255, KEY_HELP, KEY_MENU, 255, 255, 255, 255, 255, 255, + 255, 255, KEY_MUTE, KEY_SYSRQ, KEY_ENTER, + 255 /* KP_Clear */, KEY_KPCOMMA, + KEY_LEFTCTRL, KEY_LEFTSHIFT, KEY_LEFTALT, KEY_LEFTMETA, + KEY_RIGHTCTRL, KEY_RIGHTSHIFT, KEY_RIGHTALT, KEY_RIGHTMETA, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 +}; + +static const char* LINUX_HID_NAME[256] = { + 0, 0, 0, 0, "A", "B", "C", "D", "E", + "F", "G", "H", "I", "J", "K", "L", "M", + "N", "O", "P", "Q", "R", "S", "T", "U", + "V", "W", "X", "Y", "Z", "1", "2", "3", + "4", "5", "6", "7", "8", "9", "0", 0, + "Escape", "Delete", "Tab", "Space", "-", "=", + "[", "]", "Backslash", 0, ";", + "'", "Grave", ",", ".", "/", "Capslock", + "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", + "F10", "F11", "F12", "Print", "Scrolllock", "Pause", + "Insert", "Home", "Page Up", "Delete", "End", "Page Down", + "Right", "Left", "Down", "Up", "Numlock", "/", "*", "-", "+", + "Enter", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ".", + "\\", "=", "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", + "F21", "F22", "F23", "F24", 0, "Help", "Menu", 0, 0, 0, 0, 0, 0, + 0, 0, "Mute", "Sysrq", "Enter", "Clear", ",", "Left Control", "Left Shift", + "Left Alt", "Left Meta", "Right Control", "Right Shift", "Right Alt", "Right Meta", + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +typedef struct { + FILE* file; + char key_map[KEY_MAX/8 + 1]; + int last_key; +} keyboard_t; + +#define KEYBOARD_MAX 4 +#define DEVINPUTPATH "/dev/input/by-id" +#define KEYBOARD_VAR "GLIDEN64_KEYBOARD" + +static keyboard_t l_Keyboards[KEYBOARD_MAX] = { 0 }; +static int l_KeyBoardCount = 0; + +EXPORT void CALL osal_keys_init() +{ + DIR* dir; + struct dirent* entry; + char file_name[PATH_MAX]; + char real_file_name[PATH_MAX]; + char* env_var; + FILE* kbd_file; + int kbd_index = 0; + + /* if a keyboard file has been specified, use that */ + if ((env_var = getenv(KEYBOARD_VAR)) != NULL) + { + l_Keyboards[0].file = fopen(env_var, "r"); + l_KeyBoardCount++; + return; + } + + dir = opendir(DEVINPUTPATH); + if (dir == NULL) + return; + + while ((entry = readdir(dir)) != NULL) { + sprintf(file_name, "%s/%s", DEVINPUTPATH, entry->d_name); + + /* check if file_name contains kbd */ + if (strstr(file_name, "kbd") != NULL) { + /* follow symlink and get full path */ + realpath(file_name, real_file_name); + + /* attempt to open the file */ + kbd_file = l_Keyboards[kbd_index].file = fopen(real_file_name, "r"); + if (kbd_file != NULL) { + /* if we can open it, use it */ + if (kbd_index++ >= KEYBOARD_MAX) + break; + } + } + } + + l_KeyBoardCount = kbd_index; + closedir(dir); +} + +EXPORT void CALL osal_keys_quit() +{ + FILE* file; + for (int i = 0; i < l_KeyBoardCount; i++) { + file = l_Keyboards[i].file; + if (file != NULL) + fclose(file); + } +} + +EXPORT void CALL osal_keys_update_state() +{ +#ifdef OS_LINUX + keyboard_t* keyboard; + for (int i = 0; i < l_KeyBoardCount; i++) { + keyboard = &l_Keyboards[i]; + if (keyboard->file == NULL) + continue; + + // query keyboard state + ioctl(fileno(keyboard->file), EVIOCGKEY(sizeof(keyboard->key_map)), keyboard->key_map); + } +#endif // OS_LINUX +} + +EXPORT unsigned int CALL osal_is_key_pressed(unsigned int _key, unsigned int _mask) +{ + if (_key == 0 || _key > 255) + return 0; + + int key = LINUX_HID_TO_NATIVE[_key]; + + keyboard_t* keyboard; + int keys, mask; + for (int i = 0; i < l_KeyBoardCount; i++) { + keyboard = &l_Keyboards[i]; + if (keyboard->file == NULL) + continue; + + keys = keyboard->key_map[key/8]; + mask = 1 << (key % 8); + + if ((keys & mask)) { + /* if key is pressed, but key hasn't been released yet, return */ + if (keyboard->last_key == key) + return 0; + keyboard->last_key = key; + return 1; + } + else if (keyboard->last_key == key) { + /* if key has been let go, remove last_key */ + keyboard->last_key = 0; + } + } + + return 0; +} + +EXPORT const char * CALL osal_keycode_name(unsigned int _hidCode) +{ + return LINUX_HID_NAME[_hidCode]; +} + +#ifdef __cplusplus +} +#endif diff --git a/src/osal/osal_keys_unix.c b/src/osal/osal_keys_unix.c index a562e048..7b7d754e 100644 --- a/src/osal/osal_keys_unix.c +++ b/src/osal/osal_keys_unix.c @@ -4,12 +4,24 @@ extern "C" { #endif +EXPORT void CALL osal_keys_init() +{ +} + +EXPORT void CALL osal_keys_quit() +{ +} + +EXPORT void CALL osal_keys_update_state() +{ +} + EXPORT unsigned int CALL osal_is_key_pressed(unsigned int _key, unsigned int _mask) { return 0; } -EXPORT unsigned int CALL osal_virtual_key_to_hid(unsigned int _key) +EXPORT unsigned int CALL osal_virtual_key_to_hid(unsigned int _key, unsigned int _hidKey) { return 0; } diff --git a/src/osal/osal_keys_win.c b/src/osal/osal_keys_win.c index 2aace78f..e1e55e77 100644 --- a/src/osal/osal_keys_win.c +++ b/src/osal/osal_keys_win.c @@ -24,25 +24,6 @@ static const unsigned char WIN_HID_TO_NATIVE[256] = { 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255 }; -static const unsigned char WIN_NATIVE_TO_HID[256] = { - 255,255,255,255,255,255,255,255, 42, 43,255,255,255, 40,255,255, - 225,224,226, 72, 57,255,255,255,255,255,255, 41,255,255,255,255, - 44, 75, 78, 77, 74, 80, 82, 79, 81,255,255,255, 70, 73, 76,255, - 39, 30, 31, 32, 33, 34, 35, 36, 37, 38,255,255,255,255,255,255, - 255, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, - 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,255,255,255,255,255, - 98, 89, 90, 91, 92, 93, 94, 95, 96, 97, 85, 87,255, 86, 99, 84, - 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,104,105,106,107, - 108,109,110,111,112,113,114,115,255,255,255,255,255,255,255,255, - 83, 71,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255, 51, 46, 54, 45, 55, 56, - 53,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255, 47, 49, 48, 52,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255 -}; - static const char KEYCODE_WINDOWS_NAME_DATA[] = "\0'\0,\0-\0.\0/\0;\0=\0A\0B\0Backspace\0C\0Caps Lock\0D\0Delete\0E\0End\0" "Enter\0Escape\0F\0F1\0F10\0F11\0F12\0F2\0F3\0F4\0F5\0F6\0F7\0F8\0F9\0G\0H" @@ -61,6 +42,17 @@ static const unsigned short KEYCODE_WINDOWS_NAME_OFFSET[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,148,161,139,172,250,264,240,276 }; +EXPORT void CALL osal_keys_init() +{ +} + +EXPORT void CALL osal_keys_quit() +{ +} + +EXPORT void CALL osal_keys_update_state() +{ +} EXPORT unsigned int CALL osal_is_key_pressed(unsigned int _key, unsigned int _mask) { @@ -69,13 +61,6 @@ EXPORT unsigned int CALL osal_is_key_pressed(unsigned int _key, unsigned int _ma return GetAsyncKeyState(WIN_HID_TO_NATIVE[_key]) & _mask; } -EXPORT unsigned int CALL osal_virtual_key_to_hid(unsigned int _key) -{ - if (_key < 256) - return WIN_NATIVE_TO_HID[_key]; - return 0; -} - EXPORT const char * CALL osal_keycode_name(unsigned int _hidCode) { unsigned offset;