From 7464fd1a6e8da66b12a73d666667dcda132b92ab Mon Sep 17 00:00:00 2001 From: S-Marais Date: Tue, 7 Sep 2021 05:21:05 +0200 Subject: [PATCH 1/4] cleaning up the linux part: _ removing the need to use Xlib _adressing a few tweaks here and there --- Makefile.engine.linux.mak | 2 +- engine/src/core/input.h | 239 +++++++++- engine/src/core/logger.c | 4 +- engine/src/platform/platform_linux.c | 414 ++++-------------- engine/src/renderer/vulkan/vulkan_device.c | 2 +- .../src/renderer/vulkan/vulkan_renderpass.c | 2 +- 6 files changed, 324 insertions(+), 339 deletions(-) diff --git a/Makefile.engine.linux.mak b/Makefile.engine.linux.mak index bafc92b8..755c3043 100644 --- a/Makefile.engine.linux.mak +++ b/Makefile.engine.linux.mak @@ -6,7 +6,7 @@ ASSEMBLY := engine EXTENSION := .so COMPILER_FLAGS := -g -MD -Werror=vla -fdeclspec -fPIC INCLUDE_FLAGS := -Iengine/src -I$(VULKAN_SDK)/include -LINKER_FLAGS := -g -shared -lvulkan -lxcb -lX11 -lX11-xcb -lxkbcommon -L$(VULKAN_SDK)/lib -L/usr/X11R6/lib +LINKER_FLAGS := -g -shared -lvulkan -lxcb -lxcb-keysyms -lxcb-xkb -L$(VULKAN_SDK)/lib -L/usr/X11R6/lib DEFINES := -D_DEBUG -DKEXPORT # Make does not offer a recursive wildcard function, so here's one: diff --git a/engine/src/core/input.h b/engine/src/core/input.h index 3bb0560b..9657e734 100644 --- a/engine/src/core/input.h +++ b/engine/src/core/input.h @@ -12,6 +12,7 @@ typedef enum buttons { #define DEFINE_KEY(name, code) KEY_##name = code typedef enum keys { +#if KPLATFORM_WINDOWS DEFINE_KEY(BACKSPACE, 0x08), DEFINE_KEY(ENTER, 0x0D), DEFINE_KEY(TAB, 0x09), @@ -138,7 +139,137 @@ typedef enum keys { DEFINE_KEY(PERIOD, 0xBE), DEFINE_KEY(SLASH, 0xBF), DEFINE_KEY(GRAVE, 0xC0), - +#elif KPLATFORM_LINUX + // these are the keysyms but with different keys for 'A' and 'a' + DEFINE_KEY(SPACE, 0x0020), // 00032 + DEFINE_KEY(HASH, 0x0023), // 00035 + DEFINE_KEY(APOS, 0x0027), // 00039 + DEFINE_KEY(COMMA, 0x002c), // 00044 + DEFINE_KEY(MINUS, 0x002d), // 00045 + DEFINE_KEY(PERIOD, 0x002e), // 00046 + DEFINE_KEY(SLASH, 0x002f), // 00047 + DEFINE_KEY(0, 0x0030), // 00048 + DEFINE_KEY(1, 0x0031), // 00049 + DEFINE_KEY(2, 0x0032), // 00050 + DEFINE_KEY(3, 0x0033), // 00051 + DEFINE_KEY(4, 0x0034), // 00052 + DEFINE_KEY(5, 0x0035), // 00053 + DEFINE_KEY(6, 0x0036), // 00054 + DEFINE_KEY(7, 0x0037), // 00055 + DEFINE_KEY(8, 0x0038), // 00056 + DEFINE_KEY(9, 0x0039), // 00057 + DEFINE_KEY(SEMICOLON, 0x003b), // 00059 + DEFINE_KEY(EQUAL, 0x003d), // 00061 + DEFINE_KEY(A, 0x0041), // 00065 + DEFINE_KEY(B, 0x0042), // 00066 + DEFINE_KEY(C, 0x0043), // 00067 + DEFINE_KEY(D, 0x0044), // 00068 + DEFINE_KEY(E, 0x0045), // 00069 + DEFINE_KEY(F, 0x0046), // 00070 + DEFINE_KEY(G, 0x0047), // 00071 + DEFINE_KEY(H, 0x0048), // 00072 + DEFINE_KEY(I, 0x0049), // 00073 + DEFINE_KEY(J, 0x004a), // 00074 + DEFINE_KEY(K, 0x004b), // 00075 + DEFINE_KEY(L, 0x004c), // 00076 + DEFINE_KEY(M, 0x004d), // 00077 + DEFINE_KEY(N, 0x004e), // 00078 + DEFINE_KEY(O, 0x004f), // 00079 + DEFINE_KEY(P, 0x0050), // 00080 + DEFINE_KEY(Q, 0x0051), // 00081 + DEFINE_KEY(R, 0x0052), // 00082 + DEFINE_KEY(S, 0x0053), // 00083 + DEFINE_KEY(T, 0x0054), // 00084 + DEFINE_KEY(U, 0x0055), // 00085 + DEFINE_KEY(V, 0x0056), // 00086 + DEFINE_KEY(W, 0x0057), // 00087 + DEFINE_KEY(X, 0x0058), // 00088 + DEFINE_KEY(Y, 0x0059), // 00089 + DEFINE_KEY(Z, 0x005a), // 00090 + DEFINE_KEY(LBRACKET, 0x005b), // 00091 + DEFINE_KEY(BSLASH, 0x005c), // 00092 + DEFINE_KEY(RBRACKET, 0x005d), // 00093 + DEFINE_KEY(GRAVE, 0x0060), // 00094 + DEFINE_KEY(a, 0x0061), // 00095 + DEFINE_KEY(b, 0x0062), // 00096 + DEFINE_KEY(c, 0x0063), // 00097 + DEFINE_KEY(d, 0x0064), // 00098 + DEFINE_KEY(e, 0x0065), // 00099 + DEFINE_KEY(f, 0x0066), // 00100 + DEFINE_KEY(g, 0x0067), // 00101 + DEFINE_KEY(h, 0x0068), // 00102 + DEFINE_KEY(i, 0x0069), // 00103 + DEFINE_KEY(j, 0x006a), // 00104 + DEFINE_KEY(k, 0x006b), // 00105 + DEFINE_KEY(l, 0x006c), // 00106 + DEFINE_KEY(m, 0x006d), // 00107 + DEFINE_KEY(n, 0x006e), // 00108 + DEFINE_KEY(o, 0x006f), // 00109 + DEFINE_KEY(p, 0x0070), // 00110 + DEFINE_KEY(q, 0x0071), // 00111 + DEFINE_KEY(r, 0x0072), // 00112 + DEFINE_KEY(s, 0x0073), // 00113 + DEFINE_KEY(t, 0x0074), // 00114 + DEFINE_KEY(u, 0x0075), // 00115 + DEFINE_KEY(v, 0x0076), // 00116 + DEFINE_KEY(w, 0x0077), // 00117 + DEFINE_KEY(x, 0x0078), // 00118 + DEFINE_KEY(y, 0x0079), // 00119 + DEFINE_KEY(z, 0x007a), // 00120 + DEFINE_KEY(BACKSPACE, 0xff08), //65288 + DEFINE_KEY(TAB, 0xff09), // 65289 + DEFINE_KEY(ENTER, 0xff0d), // 65293 + DEFINE_KEY(PAUSE, 0xff13), // 65299 + DEFINE_KEY(SCROLL, 0xff14), // 65300 + DEFINE_KEY(ESCAPE, 0xff1b), // 65307 + DEFINE_KEY(HOME, 0xff50), //65360 + DEFINE_KEY(LEFT, 0xff51), // 65361 + DEFINE_KEY(UP, 0xff52), // 65362 + DEFINE_KEY(RIGHT, 0xff53), // 65363 + DEFINE_KEY(DOWN, 0xff54), // 65364 + DEFINE_KEY(PAGEUP, 0xff55), // 65365 + DEFINE_KEY(PAGEDOWN, 0xff56), // 65366 + DEFINE_KEY(END, 0xff57), // 65367 + DEFINE_KEY(PRINT, 0xff61), // 65377 + DEFINE_KEY(INSERT, 0xff63), // 65379 + DEFINE_KEY(NUMLOCK, 0xff7f), // 65407 + DEFINE_KEY(KP_ENTER, 0xff8d), // 65421 + DEFINE_KEY(KP_7, 0xff95), // 65429 + DEFINE_KEY(KP_4, 0xff96), // 65430 + DEFINE_KEY(KP_8, 0xff97), // 65431 + DEFINE_KEY(KP_6, 0xff98), // 65432 + DEFINE_KEY(KP_2, 0xff99), // 65433 + DEFINE_KEY(KP_9, 0xff9a), // 65434 + DEFINE_KEY(KP_3, 0xff9b), // 65435 + DEFINE_KEY(KP_1, 0xff9c), // 65436 + DEFINE_KEY(KP_5, 0xff9d), // 65437 + DEFINE_KEY(KP_0, 0xff9e), // 65438 + DEFINE_KEY(KP_DECIMAL, 0xff9f ), // 65439 + DEFINE_KEY(KP_MULTIPLY, 0xffaa), // 65450 + DEFINE_KEY(KP_ADD, 0xffab), // 65451 + DEFINE_KEY(KP_SUBTRACT, 0xffad), // 65453 + DEFINE_KEY(KP_DIVIDE, 0xffaf), // 65455 + DEFINE_KEY(F1, 0xffbe), // 65470 + DEFINE_KEY(F2, 0xffbf), // 65471 + DEFINE_KEY(F3, 0xffc0), // 65472 + DEFINE_KEY(F4, 0xffc1), // 65473 + DEFINE_KEY(F5, 0xffc2), // 65474 + DEFINE_KEY(F6, 0xffc3), // 65475 + DEFINE_KEY(F7, 0xffc4), // 65476 + DEFINE_KEY(F8, 0xffc5), // 65477 + DEFINE_KEY(F9, 0xffc6), // 65478 + DEFINE_KEY(F10, 0xffc7), // 65479 + DEFINE_KEY(F11, 0xffc8), // 65480 + DEFINE_KEY(F12, 0xffc9), // 65481 + DEFINE_KEY(LSHIFT, 0xffe1), // 65505 + DEFINE_KEY(RSHIFT, 0xffe2), // 65506 + DEFINE_KEY(LCONTROL, 0xffe3), // 65507 + DEFINE_KEY(RCONTROL, 0xffe4), // 65508 + DEFINE_KEY(CAPSLOCK, 0xffe5), // 65509 + DEFINE_KEY(LALT, 0xffe9), // 65513 + DEFINE_KEY(RALT, 0xfe03), // 65027 + DEFINE_KEY(DELETE, 0xffff), // 65535 +#endif KEYS_MAX_KEYS } keys; @@ -172,3 +303,109 @@ KAPI void input_get_previous_mouse_position(i32* x, i32* y); void input_process_button(buttons button, b8 pressed); void input_process_mouse_move(i16 x, i16 y); void input_process_mouse_wheel(i8 z_delta); + +/* // this should use xcb_keycode_t instead of xcb_keysym_t +// but are not properly tested in various locales +DEFINE_KEY(KeyEscape, 9), +DEFINE_KEY(Key_1, 10), +DEFINE_KEY(Key_2, 11), +DEFINE_KEY(Key_3, 12), +DEFINE_KEY(Key_4, 13), +DEFINE_KEY(Key_5, 14), +DEFINE_KEY(Key_6, 15), +DEFINE_KEY(Key_7, 16), +DEFINE_KEY(Key_8, 17), +DEFINE_KEY(Key_9, 18), +DEFINE_KEY(Key_0, 19), +DEFINE_KEY(KeyMinus, 20), +DEFINE_KEY(KeyEqual, 21), +DEFINE_KEY(KeyBackspace, 22), +DEFINE_KEY(KeyTab, 23), +DEFINE_KEY(Key_Q, 24), +DEFINE_KEY(Key_W, 25), +DEFINE_KEY(Key_E, 26), +DEFINE_KEY(Key_R, 27), +DEFINE_KEY(Key_T, 28), +DEFINE_KEY(Key_Y, 29), +DEFINE_KEY(Key_U, 30), +DEFINE_KEY(Key_I, 31), +DEFINE_KEY(Key_O, 32), +DEFINE_KEY(Key_P, 33), +DEFINE_KEY(KeyLeftBracket, 34), +DEFINE_KEY(KeyRightBracket, 35), +DEFINE_KEY(KeyEnter, 36), +DEFINE_KEY(KeyLeftControl, 37), +DEFINE_KEY(Key_A, 38), +DEFINE_KEY(Key_S, 39), +DEFINE_KEY(Key_D, 40), +DEFINE_KEY(Key_F, 41), +DEFINE_KEY(Key_G, 42), +DEFINE_KEY(Key_H, 43), +DEFINE_KEY(Key_J, 44), +DEFINE_KEY(Key_K, 45), +DEFINE_KEY(Key_L, 46), +DEFINE_KEY(KeySemiColon, 47), +DEFINE_KEY(KeyApostrophe, 48), +DEFINE_KEY(KeyGraveAccent, 49), +DEFINE_KEY(KeyLeftShift, 50), +DEFINE_KEY(KeyHashTag, 51), +DEFINE_KEY(Key_Z, 52), +DEFINE_KEY(Key_X, 53), +DEFINE_KEY(Key_C, 54), +DEFINE_KEY(Key_V, 55), +DEFINE_KEY(Key_B, 56), +DEFINE_KEY(Key_N, 57), +DEFINE_KEY(Key_M, 58), +DEFINE_KEY(KeyComma, 59), +DEFINE_KEY(KeyPeriod, 60), +DEFINE_KEY(KeySlash, 61), +DEFINE_KEY(KeyRightShift, 62), +DEFINE_KEY(KeyKPMultiply, 63), +DEFINE_KEY(KeyLeftAlt, 64), +DEFINE_KEY(KeySpace, 65), +DEFINE_KEY(KeyCapsLock, 66), +DEFINE_KEY(KeyF1, 67), +DEFINE_KEY(KeyF2, 68), +DEFINE_KEY(KeyF3, 69), +DEFINE_KEY(KeyF4, 70), +DEFINE_KEY(KeyF5, 71), +DEFINE_KEY(KeyF6, 72), +DEFINE_KEY(KeyF7, 73), +DEFINE_KEY(KeyF8, 74), +DEFINE_KEY(KeyF9, 75), +DEFINE_KEY(KeyF10, 76), +DEFINE_KEY(KeyNumLock, 77), +DEFINE_KEY(KeyScrollLock, 78), +DEFINE_KEY(KeyKP7, 79), +DEFINE_KEY(KeyKP8, 80), +DEFINE_KEY(KeyKP9, 81), +DEFINE_KEY(KeyKPSubtract, 82), +DEFINE_KEY(KeyKP4, 83), +DEFINE_KEY(KeyKP5, 84), +DEFINE_KEY(KeyKP6, 85), +DEFINE_KEY(KeyKPAdd, 86), +DEFINE_KEY(KeyKP1, 87), +DEFINE_KEY(KeyKP2, 88), +DEFINE_KEY(KeyKP3, 89), +DEFINE_KEY(KeyKP0, 90), +DEFINE_KEY(KeyKPDecimal, 91), +DEFINE_KEY(KeyBackSlash, 94), +DEFINE_KEY(KeyF11, 95), +DEFINE_KEY(KeyF12, 96), +DEFINE_KEY(KeyKPEnter, 104), +DEFINE_KEY(KeyRightControl, 105), +DEFINE_KEY(KeyKPDivide, 106), +DEFINE_KEY(KeyPrintScreen, 107), +DEFINE_KEY(KeyRightAlt, 108), +DEFINE_KEY(KeyHome, 110), +DEFINE_KEY(KeyUp, 111), +DEFINE_KEY(KeyPageUp, 112), +DEFINE_KEY(KeyLeft, 113), +DEFINE_KEY(KeyRight, 114), +DEFINE_KEY(KeyEnd, 115), +DEFINE_KEY(KeyDown, 116), +DEFINE_KEY(KeyPageDown=117), +DEFINE_KEY(KeyInsert, 118), +DEFINE_KEY(KeyDelete, 119), +DEFINE_KEY(KeyPause, 127), +*/ \ No newline at end of file diff --git a/engine/src/core/logger.c b/engine/src/core/logger.c index 625f6210..1b18477e 100644 --- a/engine/src/core/logger.c +++ b/engine/src/core/logger.c @@ -46,7 +46,7 @@ void log_output(log_level level, const char* message, ...) { // Technically imposes a 32k character limit on a single log entry, but... // DON'T DO THAT! const i32 msg_length = 32000; - char out_message[msg_length]; + char out_message[32000]; memset(out_message, 0, sizeof(out_message)); // Format original message. @@ -58,7 +58,7 @@ void log_output(log_level level, const char* message, ...) { vsnprintf(out_message, msg_length, message, arg_ptr); va_end(arg_ptr); - char out_message2[msg_length]; + char out_message2[32000]; sprintf(out_message2, "%s%s\n", level_strings[level], out_message); // Platform-specific output. diff --git a/engine/src/platform/platform_linux.c b/engine/src/platform/platform_linux.c index d4e1991b..dcb8b209 100644 --- a/engine/src/platform/platform_linux.c +++ b/engine/src/platform/platform_linux.c @@ -10,10 +10,8 @@ #include "containers/darray.h" #include -#include -#include // sudo apt-get install libx11-dev -#include -#include // sudo apt-get install libxkbcommon-x11-dev +#include // sudo apt install libxcb-keysym1-dev +#include // sudo apt install libxcb-xkb-dev #include #if _POSIX_C_SOURCE >= 199309L @@ -32,12 +30,12 @@ #include "renderer/vulkan/vulkan_types.inl" typedef struct platform_state { - Display* display; xcb_connection_t* connection; xcb_window_t window; xcb_screen_t* screen; xcb_atom_t wm_protocols; xcb_atom_t wm_delete_win; + xcb_key_symbols_t *syms; VkSurfaceKHR surface; } platform_state; @@ -61,32 +59,59 @@ b8 platform_system_startup( state_ptr = state; - // Connect to X - state_ptr->display = XOpenDisplay(NULL); - - // Turn off key repeats. - XAutoRepeatOff(state_ptr->display); - - // Retrieve the connection from the display. - state_ptr->connection = XGetXCBConnection(state_ptr->display); - + // we get the connection through xcb + int screenp = 0; + state_ptr->connection = xcb_connect(NULL, &screenp); if (xcb_connection_has_error(state_ptr->connection)) { KFATAL("Failed to connect to X server via XCB."); return false; } - // Get data from the X server - const struct xcb_setup_t* setup = xcb_get_setup(state_ptr->connection); + // unlike most reply_t this one must not be freed. + const xcb_query_extension_reply_t *ext_reply = xcb_get_extension_data(state_ptr->connection, &xcb_xkb_id); + if (!ext_reply) { + KFATAL("XKB extension not available on host X11 server."); + return false; + } - // Loop through screens using iterator - xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup); - int screen_p = 0; - for (i32 s = screen_p; s > 0; s--) { - xcb_screen_next(&it); + // we can now load xcb's extensions (xkb) + xcb_generic_error_t *error; + xcb_xkb_use_extension_cookie_t use_ext_cookie; + xcb_xkb_use_extension_reply_t *use_ext_reply = NULL; + use_ext_cookie = xcb_xkb_use_extension(state_ptr->connection, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION); + use_ext_reply = xcb_xkb_use_extension_reply(state_ptr->connection, use_ext_cookie, &error); + if (!use_ext_reply) { + KFATAL("Couldn't load the xcb-xkb extension"); + free(use_ext_reply); + return false; } + if (!use_ext_reply->supported) { + KFATAL("The XKB extension is not supported on this X server."); + free(use_ext_reply); + return false; + } + free(use_ext_reply); + + // we can now deactivate repeat for this app only + xcb_xkb_per_client_flags_cookie_t pcf_cookie; + xcb_xkb_per_client_flags_reply_t *pcf_reply; - // After screens have been looped through, assign it. - state_ptr->screen = it.data; + pcf_cookie = xcb_xkb_per_client_flags(state_ptr->connection, XCB_XKB_ID_USE_CORE_KBD, XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, 0, 0, 0); + pcf_reply = xcb_xkb_per_client_flags_reply(state_ptr->connection, pcf_cookie, &error); + free(pcf_reply); + if (error) { + KERROR("failed to set XCB per-client flags, not using detectable repeat. error code : %u", error->major_code); + return false; + } + + // Now let's grab the keysyms we will use later + state_ptr->syms = xcb_key_symbols_alloc(state_ptr->connection); + + // Get data from the X server + const xcb_setup_t* setup = xcb_get_setup(state_ptr->connection); + + // Loop through screens + state_ptr->screen = xcb_setup_roots_iterator(setup).data; // Allocate a XID for the window to be created. state_ptr->window = xcb_generate_id(state_ptr->connection); @@ -180,13 +205,14 @@ b8 platform_system_startup( void platform_system_shutdown(void* plat_state) { if (state_ptr) { - // Turn key repeats back on since this is global for the OS... just... wow. - XAutoRepeatOn(state_ptr->display); - xcb_destroy_window(state_ptr->connection, state_ptr->window); + xcb_key_symbols_free(state_ptr->syms); + xcb_disconnect(state_ptr->connection); } } +b8 internal_pool_for_event(xcb_generic_event_t **event); + b8 platform_pump_messages() { if (state_ptr) { xcb_generic_event_t* event; @@ -195,53 +221,37 @@ b8 platform_pump_messages() { b8 quit_flagged = false; // Poll for events until null is returned. - while (event != 0) { - event = xcb_poll_for_event(state_ptr->connection); - if (event == 0) { - break; - } - + while (internal_pool_for_event(&event)) { // Input events switch (event->response_type & ~0x80) { - case XCB_KEY_PRESS: + // while we could group PRESS and RELEASE together it's clearer to separate them + case XCB_KEY_PRESS: { + xcb_key_press_event_t *key_event = (xcb_key_press_event_t*)event; + xcb_keysym_t key_sym = xcb_key_symbols_get_keysym(state_ptr->syms, key_event->detail,0); + keys key = key_sym; + input_process_key(key, true); + } break; case XCB_KEY_RELEASE: { - // Key press event - xcb_key_press_event_t and xcb_key_release_event_t are the same - xcb_key_press_event_t* kb_event = (xcb_key_press_event_t*)event; - b8 pressed = event->response_type == XCB_KEY_PRESS; - xcb_keycode_t code = kb_event->detail; - KeySym key_sym = XkbKeycodeToKeysym( - state_ptr->display, - (KeyCode)code, //event.xkey.keycode, - 0, - code & ShiftMask ? 1 : 0); - - keys key = translate_keycode(key_sym); - - // Pass to the input subsystem for processing. - input_process_key(key, pressed); + xcb_key_release_event_t *key_event = (xcb_key_release_event_t *)event; + xcb_keysym_t key_sym = xcb_key_symbols_get_keysym(state_ptr->syms, key_event->detail,0); + keys key = key_sym; + input_process_key(key, false); } break; - case XCB_BUTTON_PRESS: - case XCB_BUTTON_RELEASE: { - xcb_button_press_event_t* mouse_event = (xcb_button_press_event_t*)event; - b8 pressed = event->response_type == XCB_BUTTON_PRESS; - buttons mouse_button = BUTTON_MAX_BUTTONS; - switch (mouse_event->detail) { - case XCB_BUTTON_INDEX_1: - mouse_button = BUTTON_LEFT; - break; - case XCB_BUTTON_INDEX_2: - mouse_button = BUTTON_MIDDLE; - break; - case XCB_BUTTON_INDEX_3: - mouse_button = BUTTON_RIGHT; - break; - } - - // Pass over to the input subsystem. - if (mouse_button != BUTTON_MAX_BUTTONS) { - input_process_button(mouse_button, pressed); + // we need to separate PRESS and RELEASE to handle the wheel event + case XCB_BUTTON_PRESS: { + xcb_button_press_event_t *button_event = (xcb_button_press_event_t*)event; + // the wheel event is mapped to button 4 and 5 + // 4 is down, while 5 is up + if (button_event->detail > 3) { + input_process_mouse_wheel(button_event->detail == 4 ? -1 : 1 ); + } else { + input_process_button(button_event->detail, true); } } break; + case XCB_BUTTON_RELEASE: { + xcb_button_release_event_t *button_event = (xcb_button_release_event_t*)event; + input_process_button(button_event->detail, false); + } break; case XCB_MOTION_NOTIFY: { // Mouse move xcb_motion_notify_event_t* move_event = (xcb_motion_notify_event_t*)event; @@ -359,273 +369,11 @@ b8 platform_create_vulkan_surface(vulkan_context* context) { return true; } -// Key translation -keys translate_keycode(u32 x_keycode) { - switch (x_keycode) { - case XK_BackSpace: - return KEY_BACKSPACE; - case XK_Return: - return KEY_ENTER; - case XK_Tab: - return KEY_TAB; - //case XK_Shift: return KEY_SHIFT; - //case XK_Control: return KEY_CONTROL; - - case XK_Pause: - return KEY_PAUSE; - case XK_Caps_Lock: - return KEY_CAPITAL; - - case XK_Escape: - return KEY_ESCAPE; - - // Not supported - // case : return KEY_CONVERT; - // case : return KEY_NONCONVERT; - // case : return KEY_ACCEPT; - - case XK_Mode_switch: - return KEY_MODECHANGE; - - case XK_space: - return KEY_SPACE; - case XK_Prior: - return KEY_PRIOR; - case XK_Next: - return KEY_NEXT; - case XK_End: - return KEY_END; - case XK_Home: - return KEY_HOME; - case XK_Left: - return KEY_LEFT; - case XK_Up: - return KEY_UP; - case XK_Right: - return KEY_RIGHT; - case XK_Down: - return KEY_DOWN; - case XK_Select: - return KEY_SELECT; - case XK_Print: - return KEY_PRINT; - case XK_Execute: - return KEY_EXECUTE; - // case XK_snapshot: return KEY_SNAPSHOT; // not supported - case XK_Insert: - return KEY_INSERT; - case XK_Delete: - return KEY_DELETE; - case XK_Help: - return KEY_HELP; - - case XK_Meta_L: - return KEY_LWIN; // TODO: not sure this is right - case XK_Meta_R: - return KEY_RWIN; - // case XK_apps: return KEY_APPS; // not supported - - // case XK_sleep: return KEY_SLEEP; //not supported - - case XK_KP_0: - return KEY_NUMPAD0; - case XK_KP_1: - return KEY_NUMPAD1; - case XK_KP_2: - return KEY_NUMPAD2; - case XK_KP_3: - return KEY_NUMPAD3; - case XK_KP_4: - return KEY_NUMPAD4; - case XK_KP_5: - return KEY_NUMPAD5; - case XK_KP_6: - return KEY_NUMPAD6; - case XK_KP_7: - return KEY_NUMPAD7; - case XK_KP_8: - return KEY_NUMPAD8; - case XK_KP_9: - return KEY_NUMPAD9; - case XK_multiply: - return KEY_MULTIPLY; - case XK_KP_Add: - return KEY_ADD; - case XK_KP_Separator: - return KEY_SEPARATOR; - case XK_KP_Subtract: - return KEY_SUBTRACT; - case XK_KP_Decimal: - return KEY_DECIMAL; - case XK_KP_Divide: - return KEY_DIVIDE; - case XK_F1: - return KEY_F1; - case XK_F2: - return KEY_F2; - case XK_F3: - return KEY_F3; - case XK_F4: - return KEY_F4; - case XK_F5: - return KEY_F5; - case XK_F6: - return KEY_F6; - case XK_F7: - return KEY_F7; - case XK_F8: - return KEY_F8; - case XK_F9: - return KEY_F9; - case XK_F10: - return KEY_F10; - case XK_F11: - return KEY_F11; - case XK_F12: - return KEY_F12; - case XK_F13: - return KEY_F13; - case XK_F14: - return KEY_F14; - case XK_F15: - return KEY_F15; - case XK_F16: - return KEY_F16; - case XK_F17: - return KEY_F17; - case XK_F18: - return KEY_F18; - case XK_F19: - return KEY_F19; - case XK_F20: - return KEY_F20; - case XK_F21: - return KEY_F21; - case XK_F22: - return KEY_F22; - case XK_F23: - return KEY_F23; - case XK_F24: - return KEY_F24; - - case XK_Num_Lock: - return KEY_NUMLOCK; - case XK_Scroll_Lock: - return KEY_SCROLL; - - case XK_KP_Equal: - return KEY_NUMPAD_EQUAL; - - case XK_Shift_L: - return KEY_LSHIFT; - case XK_Shift_R: - return KEY_RSHIFT; - case XK_Control_L: - return KEY_LCONTROL; - case XK_Control_R: - return KEY_RCONTROL; - case XK_Alt_L: - return KEY_LALT; - case XK_Alt_R: - return KEY_RALT; - - case XK_semicolon: - return KEY_SEMICOLON; - case XK_plus: - return KEY_PLUS; - case XK_comma: - return KEY_COMMA; - case XK_minus: - return KEY_MINUS; - case XK_period: - return KEY_PERIOD; - case XK_slash: - return KEY_SLASH; - case XK_grave: - return KEY_GRAVE; - - case XK_a: - case XK_A: - return KEY_A; - case XK_b: - case XK_B: - return KEY_B; - case XK_c: - case XK_C: - return KEY_C; - case XK_d: - case XK_D: - return KEY_D; - case XK_e: - case XK_E: - return KEY_E; - case XK_f: - case XK_F: - return KEY_F; - case XK_g: - case XK_G: - return KEY_G; - case XK_h: - case XK_H: - return KEY_H; - case XK_i: - case XK_I: - return KEY_I; - case XK_j: - case XK_J: - return KEY_J; - case XK_k: - case XK_K: - return KEY_K; - case XK_l: - case XK_L: - return KEY_L; - case XK_m: - case XK_M: - return KEY_M; - case XK_n: - case XK_N: - return KEY_N; - case XK_o: - case XK_O: - return KEY_O; - case XK_p: - case XK_P: - return KEY_P; - case XK_q: - case XK_Q: - return KEY_Q; - case XK_r: - case XK_R: - return KEY_R; - case XK_s: - case XK_S: - return KEY_S; - case XK_t: - case XK_T: - return KEY_T; - case XK_u: - case XK_U: - return KEY_U; - case XK_v: - case XK_V: - return KEY_V; - case XK_w: - case XK_W: - return KEY_W; - case XK_x: - case XK_X: - return KEY_X; - case XK_y: - case XK_Y: - return KEY_Y; - case XK_z: - case XK_Z: - return KEY_Z; - - default: - return 0; +b8 internal_pool_for_event(xcb_generic_event_t **event) { + if (state_ptr) { + *event = xcb_poll_for_event(state_ptr->connection); } + return (*event != NULL); } #endif \ No newline at end of file diff --git a/engine/src/renderer/vulkan/vulkan_device.c b/engine/src/renderer/vulkan/vulkan_device.c index 11ea5b0e..73a332b1 100644 --- a/engine/src/renderer/vulkan/vulkan_device.c +++ b/engine/src/renderer/vulkan/vulkan_device.c @@ -284,7 +284,7 @@ b8 select_physical_device(vulkan_context* context) { return false; } const u32 max_device_count = 32; - VkPhysicalDevice physical_devices[max_device_count]; + VkPhysicalDevice physical_devices[32]; VK_CHECK(vkEnumeratePhysicalDevices(context->instance, &physical_device_count, physical_devices)); for (u32 i = 0; i < physical_device_count; ++i) { VkPhysicalDeviceProperties properties; diff --git a/engine/src/renderer/vulkan/vulkan_renderpass.c b/engine/src/renderer/vulkan/vulkan_renderpass.c index c1dfa462..dc9daf7f 100644 --- a/engine/src/renderer/vulkan/vulkan_renderpass.c +++ b/engine/src/renderer/vulkan/vulkan_renderpass.c @@ -28,7 +28,7 @@ void vulkan_renderpass_create( // Attachments TODO: make this configurable. const u32 attachment_description_count = 2; - VkAttachmentDescription attachment_descriptions[attachment_description_count]; + VkAttachmentDescription attachment_descriptions[2]; // Color attachment VkAttachmentDescription color_attachment; From 11dddc9dd488f9ba03ac673b05e373dca4b7aa1b Mon Sep 17 00:00:00 2001 From: S-Marais Date: Sat, 11 Sep 2021 10:22:30 +0200 Subject: [PATCH 2/4] Linux cleanup update: reverting changes for vla error as it it irrelevant for this pr. implementing the minimize and raise event and suspending app. tweaking keycode keysym to use hopefully more accurate keys. --- engine/src/core/application.c | 31 +- engine/src/core/event.h | 6 + engine/src/core/input.h | 337 ++++++------------ engine/src/core/logger.c | 4 +- engine/src/platform/platform_linux.c | 33 +- engine/src/renderer/vulkan/vulkan_device.c | 2 +- .../src/renderer/vulkan/vulkan_renderpass.c | 2 +- 7 files changed, 170 insertions(+), 245 deletions(-) diff --git a/engine/src/core/application.c b/engine/src/core/application.c index fd005ea5..97227389 100644 --- a/engine/src/core/application.c +++ b/engine/src/core/application.c @@ -49,6 +49,8 @@ static application_state* app_state; b8 application_on_event(u16 code, void* sender, void* listener_inst, event_context context); b8 application_on_key(u16 code, void* sender, void* listener_inst, event_context context); b8 application_on_resized(u16 code, void* sender, void* listener_inst, event_context context); +b8 application_on_minimized(u16 code, void *sender, void *listener_inst, event_context context); +b8 application_on_raised(u16 code, void *sender, void *listener_inst, event_context context); b8 application_create(game* game_inst) { if (game_inst->application_state) { @@ -95,6 +97,8 @@ b8 application_create(game* game_inst) { event_register(EVENT_CODE_KEY_PRESSED, 0, application_on_key); event_register(EVENT_CODE_KEY_RELEASED, 0, application_on_key); event_register(EVENT_CODE_RESIZED, 0, application_on_resized); + event_register(EVENT_CODE_MINIZED, 0, application_on_minimized); + event_register(EVENT_CODE_RAISED, 0, application_on_raised); // Platform platform_system_startup(&app_state->platform_system_memory_requirement, 0, 0, 0, 0, 0, 0); @@ -206,6 +210,9 @@ b8 application_run() { event_unregister(EVENT_CODE_APPLICATION_QUIT, 0, application_on_event); event_unregister(EVENT_CODE_KEY_PRESSED, 0, application_on_key); event_unregister(EVENT_CODE_KEY_RELEASED, 0, application_on_key); + event_unregister(EVENT_CODE_RESIZED, 0, application_on_resized); + event_unregister(EVENT_CODE_MINIZED, 0, application_on_minimized); + event_unregister(EVENT_CODE_RAISED, 0, application_on_raised); input_system_shutdown(app_state->input_system_state); @@ -295,4 +302,26 @@ b8 application_on_resized(u16 code, void* sender, void* listener_inst, event_con // Event purposely not handled to allow other listeners to get this. return false; -} \ No newline at end of file +} + +b8 application_on_minimized(u16 code, void *sender, void *listener_inst, event_context context) { + if (code == EVENT_CODE_MINIZED) { + if (!app_state->is_suspended) { + KDEBUG("Application was minimized and suspended."); + app_state->is_suspended = true; + } + } + + return true; +} + +b8 application_on_raised(u16 code, void *sender, void *listener_inst, event_context context) { + if (code == EVENT_CODE_RAISED) { + if (app_state->is_suspended) { + KDEBUG("Application was raised and unsuspended."); + app_state->is_suspended = false; + } + } + + return true; +} diff --git a/engine/src/core/event.h b/engine/src/core/event.h index 3c52c499..59178204 100644 --- a/engine/src/core/event.h +++ b/engine/src/core/event.h @@ -108,5 +108,11 @@ typedef enum system_event_code { */ EVENT_CODE_RESIZED = 0x08, + // Window was minimized + EVENT_CODE_MINIZED = 0x09, + + // Window was raised from minimized + EVENT_CODE_RAISED = 0x0a, + MAX_EVENT_CODE = 0xFF } system_event_code; \ No newline at end of file diff --git a/engine/src/core/input.h b/engine/src/core/input.h index 9657e734..1b89398b 100644 --- a/engine/src/core/input.h +++ b/engine/src/core/input.h @@ -140,135 +140,108 @@ typedef enum keys { DEFINE_KEY(SLASH, 0xBF), DEFINE_KEY(GRAVE, 0xC0), #elif KPLATFORM_LINUX - // these are the keysyms but with different keys for 'A' and 'a' - DEFINE_KEY(SPACE, 0x0020), // 00032 - DEFINE_KEY(HASH, 0x0023), // 00035 - DEFINE_KEY(APOS, 0x0027), // 00039 - DEFINE_KEY(COMMA, 0x002c), // 00044 - DEFINE_KEY(MINUS, 0x002d), // 00045 - DEFINE_KEY(PERIOD, 0x002e), // 00046 - DEFINE_KEY(SLASH, 0x002f), // 00047 - DEFINE_KEY(0, 0x0030), // 00048 - DEFINE_KEY(1, 0x0031), // 00049 - DEFINE_KEY(2, 0x0032), // 00050 - DEFINE_KEY(3, 0x0033), // 00051 - DEFINE_KEY(4, 0x0034), // 00052 - DEFINE_KEY(5, 0x0035), // 00053 - DEFINE_KEY(6, 0x0036), // 00054 - DEFINE_KEY(7, 0x0037), // 00055 - DEFINE_KEY(8, 0x0038), // 00056 - DEFINE_KEY(9, 0x0039), // 00057 - DEFINE_KEY(SEMICOLON, 0x003b), // 00059 - DEFINE_KEY(EQUAL, 0x003d), // 00061 - DEFINE_KEY(A, 0x0041), // 00065 - DEFINE_KEY(B, 0x0042), // 00066 - DEFINE_KEY(C, 0x0043), // 00067 - DEFINE_KEY(D, 0x0044), // 00068 - DEFINE_KEY(E, 0x0045), // 00069 - DEFINE_KEY(F, 0x0046), // 00070 - DEFINE_KEY(G, 0x0047), // 00071 - DEFINE_KEY(H, 0x0048), // 00072 - DEFINE_KEY(I, 0x0049), // 00073 - DEFINE_KEY(J, 0x004a), // 00074 - DEFINE_KEY(K, 0x004b), // 00075 - DEFINE_KEY(L, 0x004c), // 00076 - DEFINE_KEY(M, 0x004d), // 00077 - DEFINE_KEY(N, 0x004e), // 00078 - DEFINE_KEY(O, 0x004f), // 00079 - DEFINE_KEY(P, 0x0050), // 00080 - DEFINE_KEY(Q, 0x0051), // 00081 - DEFINE_KEY(R, 0x0052), // 00082 - DEFINE_KEY(S, 0x0053), // 00083 - DEFINE_KEY(T, 0x0054), // 00084 - DEFINE_KEY(U, 0x0055), // 00085 - DEFINE_KEY(V, 0x0056), // 00086 - DEFINE_KEY(W, 0x0057), // 00087 - DEFINE_KEY(X, 0x0058), // 00088 - DEFINE_KEY(Y, 0x0059), // 00089 - DEFINE_KEY(Z, 0x005a), // 00090 - DEFINE_KEY(LBRACKET, 0x005b), // 00091 - DEFINE_KEY(BSLASH, 0x005c), // 00092 - DEFINE_KEY(RBRACKET, 0x005d), // 00093 - DEFINE_KEY(GRAVE, 0x0060), // 00094 - DEFINE_KEY(a, 0x0061), // 00095 - DEFINE_KEY(b, 0x0062), // 00096 - DEFINE_KEY(c, 0x0063), // 00097 - DEFINE_KEY(d, 0x0064), // 00098 - DEFINE_KEY(e, 0x0065), // 00099 - DEFINE_KEY(f, 0x0066), // 00100 - DEFINE_KEY(g, 0x0067), // 00101 - DEFINE_KEY(h, 0x0068), // 00102 - DEFINE_KEY(i, 0x0069), // 00103 - DEFINE_KEY(j, 0x006a), // 00104 - DEFINE_KEY(k, 0x006b), // 00105 - DEFINE_KEY(l, 0x006c), // 00106 - DEFINE_KEY(m, 0x006d), // 00107 - DEFINE_KEY(n, 0x006e), // 00108 - DEFINE_KEY(o, 0x006f), // 00109 - DEFINE_KEY(p, 0x0070), // 00110 - DEFINE_KEY(q, 0x0071), // 00111 - DEFINE_KEY(r, 0x0072), // 00112 - DEFINE_KEY(s, 0x0073), // 00113 - DEFINE_KEY(t, 0x0074), // 00114 - DEFINE_KEY(u, 0x0075), // 00115 - DEFINE_KEY(v, 0x0076), // 00116 - DEFINE_KEY(w, 0x0077), // 00117 - DEFINE_KEY(x, 0x0078), // 00118 - DEFINE_KEY(y, 0x0079), // 00119 - DEFINE_KEY(z, 0x007a), // 00120 - DEFINE_KEY(BACKSPACE, 0xff08), //65288 - DEFINE_KEY(TAB, 0xff09), // 65289 - DEFINE_KEY(ENTER, 0xff0d), // 65293 - DEFINE_KEY(PAUSE, 0xff13), // 65299 - DEFINE_KEY(SCROLL, 0xff14), // 65300 - DEFINE_KEY(ESCAPE, 0xff1b), // 65307 - DEFINE_KEY(HOME, 0xff50), //65360 - DEFINE_KEY(LEFT, 0xff51), // 65361 - DEFINE_KEY(UP, 0xff52), // 65362 - DEFINE_KEY(RIGHT, 0xff53), // 65363 - DEFINE_KEY(DOWN, 0xff54), // 65364 - DEFINE_KEY(PAGEUP, 0xff55), // 65365 - DEFINE_KEY(PAGEDOWN, 0xff56), // 65366 - DEFINE_KEY(END, 0xff57), // 65367 - DEFINE_KEY(PRINT, 0xff61), // 65377 - DEFINE_KEY(INSERT, 0xff63), // 65379 - DEFINE_KEY(NUMLOCK, 0xff7f), // 65407 - DEFINE_KEY(KP_ENTER, 0xff8d), // 65421 - DEFINE_KEY(KP_7, 0xff95), // 65429 - DEFINE_KEY(KP_4, 0xff96), // 65430 - DEFINE_KEY(KP_8, 0xff97), // 65431 - DEFINE_KEY(KP_6, 0xff98), // 65432 - DEFINE_KEY(KP_2, 0xff99), // 65433 - DEFINE_KEY(KP_9, 0xff9a), // 65434 - DEFINE_KEY(KP_3, 0xff9b), // 65435 - DEFINE_KEY(KP_1, 0xff9c), // 65436 - DEFINE_KEY(KP_5, 0xff9d), // 65437 - DEFINE_KEY(KP_0, 0xff9e), // 65438 - DEFINE_KEY(KP_DECIMAL, 0xff9f ), // 65439 - DEFINE_KEY(KP_MULTIPLY, 0xffaa), // 65450 - DEFINE_KEY(KP_ADD, 0xffab), // 65451 - DEFINE_KEY(KP_SUBTRACT, 0xffad), // 65453 - DEFINE_KEY(KP_DIVIDE, 0xffaf), // 65455 - DEFINE_KEY(F1, 0xffbe), // 65470 - DEFINE_KEY(F2, 0xffbf), // 65471 - DEFINE_KEY(F3, 0xffc0), // 65472 - DEFINE_KEY(F4, 0xffc1), // 65473 - DEFINE_KEY(F5, 0xffc2), // 65474 - DEFINE_KEY(F6, 0xffc3), // 65475 - DEFINE_KEY(F7, 0xffc4), // 65476 - DEFINE_KEY(F8, 0xffc5), // 65477 - DEFINE_KEY(F9, 0xffc6), // 65478 - DEFINE_KEY(F10, 0xffc7), // 65479 - DEFINE_KEY(F11, 0xffc8), // 65480 - DEFINE_KEY(F12, 0xffc9), // 65481 - DEFINE_KEY(LSHIFT, 0xffe1), // 65505 - DEFINE_KEY(RSHIFT, 0xffe2), // 65506 - DEFINE_KEY(LCONTROL, 0xffe3), // 65507 - DEFINE_KEY(RCONTROL, 0xffe4), // 65508 - DEFINE_KEY(CAPSLOCK, 0xffe5), // 65509 - DEFINE_KEY(LALT, 0xffe9), // 65513 - DEFINE_KEY(RALT, 0xfe03), // 65027 - DEFINE_KEY(DELETE, 0xffff), // 65535 + DEFINE_KEY(SPACE, 0x0020), + DEFINE_KEY(HASH, 0x0023), + DEFINE_KEY(APOS, 0x0027), + DEFINE_KEY(COMMA, 0x002c), + DEFINE_KEY(MINUS, 0x002d), + DEFINE_KEY(PERIOD, 0x002e), + DEFINE_KEY(SLASH, 0x002f), + DEFINE_KEY(0, 0x0030), + DEFINE_KEY(1, 0x0031), + DEFINE_KEY(2, 0x0032), + DEFINE_KEY(3, 0x0033), + DEFINE_KEY(4, 0x0034), + DEFINE_KEY(5, 0x0035), + DEFINE_KEY(6, 0x0036), + DEFINE_KEY(7, 0x0037), + DEFINE_KEY(8, 0x0038), + DEFINE_KEY(9, 0x0039), + DEFINE_KEY(SEMICOLON, 0x003b), + DEFINE_KEY(EQUAL, 0x003d), + DEFINE_KEY(A, 0x0041), + DEFINE_KEY(B, 0x0042), + DEFINE_KEY(C, 0x0043), + DEFINE_KEY(D, 0x0044), + DEFINE_KEY(E, 0x0045), + DEFINE_KEY(F, 0x0046), + DEFINE_KEY(G, 0x0047), + DEFINE_KEY(H, 0x0048), + DEFINE_KEY(I, 0x0049), + DEFINE_KEY(J, 0x004a), + DEFINE_KEY(K, 0x004b), + DEFINE_KEY(L, 0x004c), + DEFINE_KEY(M, 0x004d), + DEFINE_KEY(N, 0x004e), + DEFINE_KEY(O, 0x004f), + DEFINE_KEY(P, 0x0050), + DEFINE_KEY(Q, 0x0051), + DEFINE_KEY(R, 0x0052), + DEFINE_KEY(S, 0x0053), + DEFINE_KEY(T, 0x0054), + DEFINE_KEY(U, 0x0055), + DEFINE_KEY(V, 0x0056), + DEFINE_KEY(W, 0x0057), + DEFINE_KEY(X, 0x0058), + DEFINE_KEY(Y, 0x0059), + DEFINE_KEY(Z, 0x005a), + DEFINE_KEY(LBRACKET, 0x005b), + DEFINE_KEY(BSLASH, 0x005c), + DEFINE_KEY(RBRACKET, 0x005d), + DEFINE_KEY(GRAVE, 0x0060), + DEFINE_KEY(BACKSPACE, 0xff08), + DEFINE_KEY(TAB, 0xff09), + DEFINE_KEY(ENTER, 0xff0d), + DEFINE_KEY(PAUSE, 0xff13), + DEFINE_KEY(SCROLL, 0xff14), + DEFINE_KEY(ESCAPE, 0xff1b), + DEFINE_KEY(HOME, 0xff50), + DEFINE_KEY(LEFT, 0xff51), + DEFINE_KEY(UP, 0xff52), + DEFINE_KEY(RIGHT, 0xff53), + DEFINE_KEY(DOWN, 0xff54), + DEFINE_KEY(PAGEUP, 0xff55), + DEFINE_KEY(PAGEDOWN, 0xff56), + DEFINE_KEY(END, 0xff57), + DEFINE_KEY(PRINT, 0xff61), + DEFINE_KEY(INSERT, 0xff63), + DEFINE_KEY(NUMLOCK, 0xff7f), + DEFINE_KEY(KP_ENTER, 0xff8d), + DEFINE_KEY(KP_7, 0xff95), + DEFINE_KEY(KP_4, 0xff96), + DEFINE_KEY(KP_8, 0xff97), + DEFINE_KEY(KP_6, 0xff98), + DEFINE_KEY(KP_2, 0xff99), + DEFINE_KEY(KP_9, 0xff9a), + DEFINE_KEY(KP_3, 0xff9b), + DEFINE_KEY(KP_1, 0xff9c), + DEFINE_KEY(KP_5, 0xff9d), + DEFINE_KEY(KP_0, 0xff9e), + DEFINE_KEY(KP_DECIMAL, 0xff9f ), + DEFINE_KEY(KP_MULTIPLY, 0xffaa), + DEFINE_KEY(KP_ADD, 0xffab), + DEFINE_KEY(KP_SUBTRACT, 0xffad), + DEFINE_KEY(KP_DIVIDE, 0xffaf), + DEFINE_KEY(F1, 0xffbe), + DEFINE_KEY(F2, 0xffbf), + DEFINE_KEY(F3, 0xffc0), + DEFINE_KEY(F4, 0xffc1), + DEFINE_KEY(F5, 0xffc2), + DEFINE_KEY(F6, 0xffc3), + DEFINE_KEY(F7, 0xffc4), + DEFINE_KEY(F8, 0xffc5), + DEFINE_KEY(F9, 0xffc6), + DEFINE_KEY(F10, 0xffc7), + DEFINE_KEY(F11, 0xffc8), + DEFINE_KEY(F12, 0xffc9), + DEFINE_KEY(LSHIFT, 0xffe1), + DEFINE_KEY(RSHIFT, 0xffe2), + DEFINE_KEY(LCONTROL, 0xffe3), + DEFINE_KEY(RCONTROL, 0xffe4), + DEFINE_KEY(CAPSLOCK, 0xffe5), + DEFINE_KEY(LALT, 0xffe9), + DEFINE_KEY(RALT, 0xfe03), + DEFINE_KEY(DELETE, 0xffff), #endif KEYS_MAX_KEYS } keys; @@ -303,109 +276,3 @@ KAPI void input_get_previous_mouse_position(i32* x, i32* y); void input_process_button(buttons button, b8 pressed); void input_process_mouse_move(i16 x, i16 y); void input_process_mouse_wheel(i8 z_delta); - -/* // this should use xcb_keycode_t instead of xcb_keysym_t -// but are not properly tested in various locales -DEFINE_KEY(KeyEscape, 9), -DEFINE_KEY(Key_1, 10), -DEFINE_KEY(Key_2, 11), -DEFINE_KEY(Key_3, 12), -DEFINE_KEY(Key_4, 13), -DEFINE_KEY(Key_5, 14), -DEFINE_KEY(Key_6, 15), -DEFINE_KEY(Key_7, 16), -DEFINE_KEY(Key_8, 17), -DEFINE_KEY(Key_9, 18), -DEFINE_KEY(Key_0, 19), -DEFINE_KEY(KeyMinus, 20), -DEFINE_KEY(KeyEqual, 21), -DEFINE_KEY(KeyBackspace, 22), -DEFINE_KEY(KeyTab, 23), -DEFINE_KEY(Key_Q, 24), -DEFINE_KEY(Key_W, 25), -DEFINE_KEY(Key_E, 26), -DEFINE_KEY(Key_R, 27), -DEFINE_KEY(Key_T, 28), -DEFINE_KEY(Key_Y, 29), -DEFINE_KEY(Key_U, 30), -DEFINE_KEY(Key_I, 31), -DEFINE_KEY(Key_O, 32), -DEFINE_KEY(Key_P, 33), -DEFINE_KEY(KeyLeftBracket, 34), -DEFINE_KEY(KeyRightBracket, 35), -DEFINE_KEY(KeyEnter, 36), -DEFINE_KEY(KeyLeftControl, 37), -DEFINE_KEY(Key_A, 38), -DEFINE_KEY(Key_S, 39), -DEFINE_KEY(Key_D, 40), -DEFINE_KEY(Key_F, 41), -DEFINE_KEY(Key_G, 42), -DEFINE_KEY(Key_H, 43), -DEFINE_KEY(Key_J, 44), -DEFINE_KEY(Key_K, 45), -DEFINE_KEY(Key_L, 46), -DEFINE_KEY(KeySemiColon, 47), -DEFINE_KEY(KeyApostrophe, 48), -DEFINE_KEY(KeyGraveAccent, 49), -DEFINE_KEY(KeyLeftShift, 50), -DEFINE_KEY(KeyHashTag, 51), -DEFINE_KEY(Key_Z, 52), -DEFINE_KEY(Key_X, 53), -DEFINE_KEY(Key_C, 54), -DEFINE_KEY(Key_V, 55), -DEFINE_KEY(Key_B, 56), -DEFINE_KEY(Key_N, 57), -DEFINE_KEY(Key_M, 58), -DEFINE_KEY(KeyComma, 59), -DEFINE_KEY(KeyPeriod, 60), -DEFINE_KEY(KeySlash, 61), -DEFINE_KEY(KeyRightShift, 62), -DEFINE_KEY(KeyKPMultiply, 63), -DEFINE_KEY(KeyLeftAlt, 64), -DEFINE_KEY(KeySpace, 65), -DEFINE_KEY(KeyCapsLock, 66), -DEFINE_KEY(KeyF1, 67), -DEFINE_KEY(KeyF2, 68), -DEFINE_KEY(KeyF3, 69), -DEFINE_KEY(KeyF4, 70), -DEFINE_KEY(KeyF5, 71), -DEFINE_KEY(KeyF6, 72), -DEFINE_KEY(KeyF7, 73), -DEFINE_KEY(KeyF8, 74), -DEFINE_KEY(KeyF9, 75), -DEFINE_KEY(KeyF10, 76), -DEFINE_KEY(KeyNumLock, 77), -DEFINE_KEY(KeyScrollLock, 78), -DEFINE_KEY(KeyKP7, 79), -DEFINE_KEY(KeyKP8, 80), -DEFINE_KEY(KeyKP9, 81), -DEFINE_KEY(KeyKPSubtract, 82), -DEFINE_KEY(KeyKP4, 83), -DEFINE_KEY(KeyKP5, 84), -DEFINE_KEY(KeyKP6, 85), -DEFINE_KEY(KeyKPAdd, 86), -DEFINE_KEY(KeyKP1, 87), -DEFINE_KEY(KeyKP2, 88), -DEFINE_KEY(KeyKP3, 89), -DEFINE_KEY(KeyKP0, 90), -DEFINE_KEY(KeyKPDecimal, 91), -DEFINE_KEY(KeyBackSlash, 94), -DEFINE_KEY(KeyF11, 95), -DEFINE_KEY(KeyF12, 96), -DEFINE_KEY(KeyKPEnter, 104), -DEFINE_KEY(KeyRightControl, 105), -DEFINE_KEY(KeyKPDivide, 106), -DEFINE_KEY(KeyPrintScreen, 107), -DEFINE_KEY(KeyRightAlt, 108), -DEFINE_KEY(KeyHome, 110), -DEFINE_KEY(KeyUp, 111), -DEFINE_KEY(KeyPageUp, 112), -DEFINE_KEY(KeyLeft, 113), -DEFINE_KEY(KeyRight, 114), -DEFINE_KEY(KeyEnd, 115), -DEFINE_KEY(KeyDown, 116), -DEFINE_KEY(KeyPageDown=117), -DEFINE_KEY(KeyInsert, 118), -DEFINE_KEY(KeyDelete, 119), -DEFINE_KEY(KeyPause, 127), -*/ \ No newline at end of file diff --git a/engine/src/core/logger.c b/engine/src/core/logger.c index 1b18477e..625f6210 100644 --- a/engine/src/core/logger.c +++ b/engine/src/core/logger.c @@ -46,7 +46,7 @@ void log_output(log_level level, const char* message, ...) { // Technically imposes a 32k character limit on a single log entry, but... // DON'T DO THAT! const i32 msg_length = 32000; - char out_message[32000]; + char out_message[msg_length]; memset(out_message, 0, sizeof(out_message)); // Format original message. @@ -58,7 +58,7 @@ void log_output(log_level level, const char* message, ...) { vsnprintf(out_message, msg_length, message, arg_ptr); va_end(arg_ptr); - char out_message2[32000]; + char out_message2[msg_length]; sprintf(out_message2, "%s%s\n", level_strings[level], out_message); // Platform-specific output. diff --git a/engine/src/platform/platform_linux.c b/engine/src/platform/platform_linux.c index dcb8b209..d2734695 100644 --- a/engine/src/platform/platform_linux.c +++ b/engine/src/platform/platform_linux.c @@ -228,14 +228,12 @@ b8 platform_pump_messages() { case XCB_KEY_PRESS: { xcb_key_press_event_t *key_event = (xcb_key_press_event_t*)event; xcb_keysym_t key_sym = xcb_key_symbols_get_keysym(state_ptr->syms, key_event->detail,0); - keys key = key_sym; - input_process_key(key, true); + input_process_key(translate_keycode(key_sym), true); } break; case XCB_KEY_RELEASE: { xcb_key_release_event_t *key_event = (xcb_key_release_event_t *)event; xcb_keysym_t key_sym = xcb_key_symbols_get_keysym(state_ptr->syms, key_event->detail,0); - keys key = key_sym; - input_process_key(key, false); + input_process_key(translate_keycode(key_sym), false); } break; // we need to separate PRESS and RELEASE to handle the wheel event case XCB_BUTTON_PRESS: { @@ -282,6 +280,20 @@ b8 platform_pump_messages() { quit_flagged = true; } } break; + case XCB_MAP_NOTIFY: { + // The window just got raised + event_context context; + context.data.u16[0] = 1; + context.data.u16[1] = 1; + event_fire(EVENT_CODE_RAISED, 0, context); + } break; + case XCB_UNMAP_NOTIFY: { + // The window just got minimized + event_context context; + context.data.u16[0] = 0; + context.data.u16[1] = 0; + event_fire(EVENT_CODE_MINIZED, 0, context); + } break; default: // Something else break; @@ -376,4 +388,15 @@ b8 internal_pool_for_event(xcb_generic_event_t **event) { return (*event != NULL); } -#endif \ No newline at end of file +keys translate_keycode(u32 x_keycode) { + xcb_keysym_t upper = x_keycode; + if ((x_keycode >> 8) == 0) { + if (x_keycode >= 0x0061 && x_keycode <= 0x007a) { + upper -= (0x0061 - 0x0041); + } + } + + return upper; +} + +#endif diff --git a/engine/src/renderer/vulkan/vulkan_device.c b/engine/src/renderer/vulkan/vulkan_device.c index 73a332b1..11ea5b0e 100644 --- a/engine/src/renderer/vulkan/vulkan_device.c +++ b/engine/src/renderer/vulkan/vulkan_device.c @@ -284,7 +284,7 @@ b8 select_physical_device(vulkan_context* context) { return false; } const u32 max_device_count = 32; - VkPhysicalDevice physical_devices[32]; + VkPhysicalDevice physical_devices[max_device_count]; VK_CHECK(vkEnumeratePhysicalDevices(context->instance, &physical_device_count, physical_devices)); for (u32 i = 0; i < physical_device_count; ++i) { VkPhysicalDeviceProperties properties; diff --git a/engine/src/renderer/vulkan/vulkan_renderpass.c b/engine/src/renderer/vulkan/vulkan_renderpass.c index dc9daf7f..c1dfa462 100644 --- a/engine/src/renderer/vulkan/vulkan_renderpass.c +++ b/engine/src/renderer/vulkan/vulkan_renderpass.c @@ -28,7 +28,7 @@ void vulkan_renderpass_create( // Attachments TODO: make this configurable. const u32 attachment_description_count = 2; - VkAttachmentDescription attachment_descriptions[2]; + VkAttachmentDescription attachment_descriptions[attachment_description_count]; // Color attachment VkAttachmentDescription color_attachment; From 1efc27389e2cbdf17933135ca2e1d8c85da71fbc Mon Sep 17 00:00:00 2001 From: S-Marais Date: Thu, 2 Jun 2022 00:25:34 +0200 Subject: [PATCH 3/4] Reverting unrelated changes. Keeping input changes to platform specific. --- engine/src/core/application.c | 28 -- engine/src/core/event.h | 6 - engine/src/core/input.h | 597 ++++++++++++++++----------- engine/src/platform/platform_linux.c | 331 ++++++++++++--- 4 files changed, 645 insertions(+), 317 deletions(-) diff --git a/engine/src/core/application.c b/engine/src/core/application.c index 97227389..e179f9ba 100644 --- a/engine/src/core/application.c +++ b/engine/src/core/application.c @@ -49,8 +49,6 @@ static application_state* app_state; b8 application_on_event(u16 code, void* sender, void* listener_inst, event_context context); b8 application_on_key(u16 code, void* sender, void* listener_inst, event_context context); b8 application_on_resized(u16 code, void* sender, void* listener_inst, event_context context); -b8 application_on_minimized(u16 code, void *sender, void *listener_inst, event_context context); -b8 application_on_raised(u16 code, void *sender, void *listener_inst, event_context context); b8 application_create(game* game_inst) { if (game_inst->application_state) { @@ -97,8 +95,6 @@ b8 application_create(game* game_inst) { event_register(EVENT_CODE_KEY_PRESSED, 0, application_on_key); event_register(EVENT_CODE_KEY_RELEASED, 0, application_on_key); event_register(EVENT_CODE_RESIZED, 0, application_on_resized); - event_register(EVENT_CODE_MINIZED, 0, application_on_minimized); - event_register(EVENT_CODE_RAISED, 0, application_on_raised); // Platform platform_system_startup(&app_state->platform_system_memory_requirement, 0, 0, 0, 0, 0, 0); @@ -211,8 +207,6 @@ b8 application_run() { event_unregister(EVENT_CODE_KEY_PRESSED, 0, application_on_key); event_unregister(EVENT_CODE_KEY_RELEASED, 0, application_on_key); event_unregister(EVENT_CODE_RESIZED, 0, application_on_resized); - event_unregister(EVENT_CODE_MINIZED, 0, application_on_minimized); - event_unregister(EVENT_CODE_RAISED, 0, application_on_raised); input_system_shutdown(app_state->input_system_state); @@ -303,25 +297,3 @@ b8 application_on_resized(u16 code, void* sender, void* listener_inst, event_con // Event purposely not handled to allow other listeners to get this. return false; } - -b8 application_on_minimized(u16 code, void *sender, void *listener_inst, event_context context) { - if (code == EVENT_CODE_MINIZED) { - if (!app_state->is_suspended) { - KDEBUG("Application was minimized and suspended."); - app_state->is_suspended = true; - } - } - - return true; -} - -b8 application_on_raised(u16 code, void *sender, void *listener_inst, event_context context) { - if (code == EVENT_CODE_RAISED) { - if (app_state->is_suspended) { - KDEBUG("Application was raised and unsuspended."); - app_state->is_suspended = false; - } - } - - return true; -} diff --git a/engine/src/core/event.h b/engine/src/core/event.h index 59178204..3c52c499 100644 --- a/engine/src/core/event.h +++ b/engine/src/core/event.h @@ -108,11 +108,5 @@ typedef enum system_event_code { */ EVENT_CODE_RESIZED = 0x08, - // Window was minimized - EVENT_CODE_MINIZED = 0x09, - - // Window was raised from minimized - EVENT_CODE_RAISED = 0x0a, - MAX_EVENT_CODE = 0xFF } system_event_code; \ No newline at end of file diff --git a/engine/src/core/input.h b/engine/src/core/input.h index 1b89398b..6eb0ae8a 100644 --- a/engine/src/core/input.h +++ b/engine/src/core/input.h @@ -1,248 +1,289 @@ +/** + * @file input.h + * @author Travis Vroman (travis@kohiengine.com) + * @brief This file contains everything having to do with input on deskop + * environments from keyboards and mice. Gamepads and touch controls will + * likely be handled separately at a future date. + * @version 1.0 + * @date 2022-01-10 + * + * @copyright Kohi Game Engine is Copyright (c) Travis Vroman 2021-2022 + * + */ #pragma once #include "defines.h" +/** + * @brief Represents available mouse buttons. + */ typedef enum buttons { + /** @brief The left mouse button */ BUTTON_LEFT, + /** @brief The right mouse button */ BUTTON_RIGHT, + /** @brief The middle mouse button (typically the wheel) */ BUTTON_MIDDLE, BUTTON_MAX_BUTTONS } buttons; -#define DEFINE_KEY(name, code) KEY_##name = code - +/** + * @brief Represents available keyboard keys. + */ typedef enum keys { -#if KPLATFORM_WINDOWS - DEFINE_KEY(BACKSPACE, 0x08), - DEFINE_KEY(ENTER, 0x0D), - DEFINE_KEY(TAB, 0x09), - DEFINE_KEY(SHIFT, 0x10), - DEFINE_KEY(CONTROL, 0x11), - - DEFINE_KEY(PAUSE, 0x13), - DEFINE_KEY(CAPITAL, 0x14), - - DEFINE_KEY(ESCAPE, 0x1B), - - DEFINE_KEY(CONVERT, 0x1C), - DEFINE_KEY(NONCONVERT, 0x1D), - DEFINE_KEY(ACCEPT, 0x1E), - DEFINE_KEY(MODECHANGE, 0x1F), - - DEFINE_KEY(SPACE, 0x20), - DEFINE_KEY(PRIOR, 0x21), - DEFINE_KEY(NEXT, 0x22), - DEFINE_KEY(END, 0x23), - DEFINE_KEY(HOME, 0x24), - DEFINE_KEY(LEFT, 0x25), - DEFINE_KEY(UP, 0x26), - DEFINE_KEY(RIGHT, 0x27), - DEFINE_KEY(DOWN, 0x28), - DEFINE_KEY(SELECT, 0x29), - DEFINE_KEY(PRINT, 0x2A), - DEFINE_KEY(EXECUTE, 0x2B), - DEFINE_KEY(SNAPSHOT, 0x2C), - DEFINE_KEY(INSERT, 0x2D), - DEFINE_KEY(DELETE, 0x2E), - DEFINE_KEY(HELP, 0x2F), - - DEFINE_KEY(A, 0x41), - DEFINE_KEY(B, 0x42), - DEFINE_KEY(C, 0x43), - DEFINE_KEY(D, 0x44), - DEFINE_KEY(E, 0x45), - DEFINE_KEY(F, 0x46), - DEFINE_KEY(G, 0x47), - DEFINE_KEY(H, 0x48), - DEFINE_KEY(I, 0x49), - DEFINE_KEY(J, 0x4A), - DEFINE_KEY(K, 0x4B), - DEFINE_KEY(L, 0x4C), - DEFINE_KEY(M, 0x4D), - DEFINE_KEY(N, 0x4E), - DEFINE_KEY(O, 0x4F), - DEFINE_KEY(P, 0x50), - DEFINE_KEY(Q, 0x51), - DEFINE_KEY(R, 0x52), - DEFINE_KEY(S, 0x53), - DEFINE_KEY(T, 0x54), - DEFINE_KEY(U, 0x55), - DEFINE_KEY(V, 0x56), - DEFINE_KEY(W, 0x57), - DEFINE_KEY(X, 0x58), - DEFINE_KEY(Y, 0x59), - DEFINE_KEY(Z, 0x5A), - - DEFINE_KEY(LWIN, 0x5B), - DEFINE_KEY(RWIN, 0x5C), - DEFINE_KEY(APPS, 0x5D), - - DEFINE_KEY(SLEEP, 0x5F), - - DEFINE_KEY(NUMPAD0, 0x60), - DEFINE_KEY(NUMPAD1, 0x61), - DEFINE_KEY(NUMPAD2, 0x62), - DEFINE_KEY(NUMPAD3, 0x63), - DEFINE_KEY(NUMPAD4, 0x64), - DEFINE_KEY(NUMPAD5, 0x65), - DEFINE_KEY(NUMPAD6, 0x66), - DEFINE_KEY(NUMPAD7, 0x67), - DEFINE_KEY(NUMPAD8, 0x68), - DEFINE_KEY(NUMPAD9, 0x69), - DEFINE_KEY(MULTIPLY, 0x6A), - DEFINE_KEY(ADD, 0x6B), - DEFINE_KEY(SEPARATOR, 0x6C), - DEFINE_KEY(SUBTRACT, 0x6D), - DEFINE_KEY(DECIMAL, 0x6E), - DEFINE_KEY(DIVIDE, 0x6F), - DEFINE_KEY(F1, 0x70), - DEFINE_KEY(F2, 0x71), - DEFINE_KEY(F3, 0x72), - DEFINE_KEY(F4, 0x73), - DEFINE_KEY(F5, 0x74), - DEFINE_KEY(F6, 0x75), - DEFINE_KEY(F7, 0x76), - DEFINE_KEY(F8, 0x77), - DEFINE_KEY(F9, 0x78), - DEFINE_KEY(F10, 0x79), - DEFINE_KEY(F11, 0x7A), - DEFINE_KEY(F12, 0x7B), - DEFINE_KEY(F13, 0x7C), - DEFINE_KEY(F14, 0x7D), - DEFINE_KEY(F15, 0x7E), - DEFINE_KEY(F16, 0x7F), - DEFINE_KEY(F17, 0x80), - DEFINE_KEY(F18, 0x81), - DEFINE_KEY(F19, 0x82), - DEFINE_KEY(F20, 0x83), - DEFINE_KEY(F21, 0x84), - DEFINE_KEY(F22, 0x85), - DEFINE_KEY(F23, 0x86), - DEFINE_KEY(F24, 0x87), - - DEFINE_KEY(NUMLOCK, 0x90), - DEFINE_KEY(SCROLL, 0x91), - - DEFINE_KEY(NUMPAD_EQUAL, 0x92), - - DEFINE_KEY(LSHIFT, 0xA0), - DEFINE_KEY(RSHIFT, 0xA1), - DEFINE_KEY(LCONTROL, 0xA2), - DEFINE_KEY(RCONTROL, 0xA3), - DEFINE_KEY(LALT, 0xA4), - DEFINE_KEY(RALT, 0xA5), - - DEFINE_KEY(SEMICOLON, 0xBA), - DEFINE_KEY(PLUS, 0xBB), - DEFINE_KEY(COMMA, 0xBC), - DEFINE_KEY(MINUS, 0xBD), - DEFINE_KEY(PERIOD, 0xBE), - DEFINE_KEY(SLASH, 0xBF), - DEFINE_KEY(GRAVE, 0xC0), -#elif KPLATFORM_LINUX - DEFINE_KEY(SPACE, 0x0020), - DEFINE_KEY(HASH, 0x0023), - DEFINE_KEY(APOS, 0x0027), - DEFINE_KEY(COMMA, 0x002c), - DEFINE_KEY(MINUS, 0x002d), - DEFINE_KEY(PERIOD, 0x002e), - DEFINE_KEY(SLASH, 0x002f), - DEFINE_KEY(0, 0x0030), - DEFINE_KEY(1, 0x0031), - DEFINE_KEY(2, 0x0032), - DEFINE_KEY(3, 0x0033), - DEFINE_KEY(4, 0x0034), - DEFINE_KEY(5, 0x0035), - DEFINE_KEY(6, 0x0036), - DEFINE_KEY(7, 0x0037), - DEFINE_KEY(8, 0x0038), - DEFINE_KEY(9, 0x0039), - DEFINE_KEY(SEMICOLON, 0x003b), - DEFINE_KEY(EQUAL, 0x003d), - DEFINE_KEY(A, 0x0041), - DEFINE_KEY(B, 0x0042), - DEFINE_KEY(C, 0x0043), - DEFINE_KEY(D, 0x0044), - DEFINE_KEY(E, 0x0045), - DEFINE_KEY(F, 0x0046), - DEFINE_KEY(G, 0x0047), - DEFINE_KEY(H, 0x0048), - DEFINE_KEY(I, 0x0049), - DEFINE_KEY(J, 0x004a), - DEFINE_KEY(K, 0x004b), - DEFINE_KEY(L, 0x004c), - DEFINE_KEY(M, 0x004d), - DEFINE_KEY(N, 0x004e), - DEFINE_KEY(O, 0x004f), - DEFINE_KEY(P, 0x0050), - DEFINE_KEY(Q, 0x0051), - DEFINE_KEY(R, 0x0052), - DEFINE_KEY(S, 0x0053), - DEFINE_KEY(T, 0x0054), - DEFINE_KEY(U, 0x0055), - DEFINE_KEY(V, 0x0056), - DEFINE_KEY(W, 0x0057), - DEFINE_KEY(X, 0x0058), - DEFINE_KEY(Y, 0x0059), - DEFINE_KEY(Z, 0x005a), - DEFINE_KEY(LBRACKET, 0x005b), - DEFINE_KEY(BSLASH, 0x005c), - DEFINE_KEY(RBRACKET, 0x005d), - DEFINE_KEY(GRAVE, 0x0060), - DEFINE_KEY(BACKSPACE, 0xff08), - DEFINE_KEY(TAB, 0xff09), - DEFINE_KEY(ENTER, 0xff0d), - DEFINE_KEY(PAUSE, 0xff13), - DEFINE_KEY(SCROLL, 0xff14), - DEFINE_KEY(ESCAPE, 0xff1b), - DEFINE_KEY(HOME, 0xff50), - DEFINE_KEY(LEFT, 0xff51), - DEFINE_KEY(UP, 0xff52), - DEFINE_KEY(RIGHT, 0xff53), - DEFINE_KEY(DOWN, 0xff54), - DEFINE_KEY(PAGEUP, 0xff55), - DEFINE_KEY(PAGEDOWN, 0xff56), - DEFINE_KEY(END, 0xff57), - DEFINE_KEY(PRINT, 0xff61), - DEFINE_KEY(INSERT, 0xff63), - DEFINE_KEY(NUMLOCK, 0xff7f), - DEFINE_KEY(KP_ENTER, 0xff8d), - DEFINE_KEY(KP_7, 0xff95), - DEFINE_KEY(KP_4, 0xff96), - DEFINE_KEY(KP_8, 0xff97), - DEFINE_KEY(KP_6, 0xff98), - DEFINE_KEY(KP_2, 0xff99), - DEFINE_KEY(KP_9, 0xff9a), - DEFINE_KEY(KP_3, 0xff9b), - DEFINE_KEY(KP_1, 0xff9c), - DEFINE_KEY(KP_5, 0xff9d), - DEFINE_KEY(KP_0, 0xff9e), - DEFINE_KEY(KP_DECIMAL, 0xff9f ), - DEFINE_KEY(KP_MULTIPLY, 0xffaa), - DEFINE_KEY(KP_ADD, 0xffab), - DEFINE_KEY(KP_SUBTRACT, 0xffad), - DEFINE_KEY(KP_DIVIDE, 0xffaf), - DEFINE_KEY(F1, 0xffbe), - DEFINE_KEY(F2, 0xffbf), - DEFINE_KEY(F3, 0xffc0), - DEFINE_KEY(F4, 0xffc1), - DEFINE_KEY(F5, 0xffc2), - DEFINE_KEY(F6, 0xffc3), - DEFINE_KEY(F7, 0xffc4), - DEFINE_KEY(F8, 0xffc5), - DEFINE_KEY(F9, 0xffc6), - DEFINE_KEY(F10, 0xffc7), - DEFINE_KEY(F11, 0xffc8), - DEFINE_KEY(F12, 0xffc9), - DEFINE_KEY(LSHIFT, 0xffe1), - DEFINE_KEY(RSHIFT, 0xffe2), - DEFINE_KEY(LCONTROL, 0xffe3), - DEFINE_KEY(RCONTROL, 0xffe4), - DEFINE_KEY(CAPSLOCK, 0xffe5), - DEFINE_KEY(LALT, 0xffe9), - DEFINE_KEY(RALT, 0xfe03), - DEFINE_KEY(DELETE, 0xffff), -#endif + /** @brief The backspace key. */ + KEY_BACKSPACE = 0x08, + /** @brief The enter key. */ + KEY_ENTER = 0x0D, + /** @brief The tab key. */ + KEY_TAB = 0x09, + /** @brief The shift key. */ + KEY_SHIFT = 0x10, + /** @brief The Control/Ctrl key. */ + KEY_CONTROL = 0x11, + + /** @brief The pause key. */ + KEY_PAUSE = 0x13, + /** @brief The Caps Lock key. */ + KEY_CAPITAL = 0x14, + + /** @brief The Escape key. */ + KEY_ESCAPE = 0x1B, + + KEY_CONVERT = 0x1C, + KEY_NONCONVERT = 0x1D, + KEY_ACCEPT = 0x1E, + KEY_MODECHANGE = 0x1F, + + /** @brief The spacebar key. */ + KEY_SPACE = 0x20, + KEY_PRIOR = 0x21, + KEY_NEXT = 0x22, + /** @brief The end key. */ + KEY_END = 0x23, + /** @brief The home key. */ + KEY_HOME = 0x24, + /** @brief The left arrow key. */ + KEY_LEFT = 0x25, + /** @brief The up arrow key. */ + KEY_UP = 0x26, + /** @brief The right arrow key. */ + KEY_RIGHT = 0x27, + /** @brief The down arrow key. */ + KEY_DOWN = 0x28, + KEY_SELECT = 0x29, + KEY_PRINT = 0x2A, + KEY_EXECUTE = 0x2B, + KEY_SNAPSHOT = 0x2C, + /** @brief The insert key. */ + KEY_INSERT = 0x2D, + /** @brief The delete key. */ + KEY_DELETE = 0x2E, + KEY_HELP = 0x2F, + + /** @brief The 0 key */ + KEY_0 = 0x30, + /** @brief The 1 key */ + KEY_1 = 0x31, + /** @brief The 2 key */ + KEY_2 = 0x32, + /** @brief The 3 key */ + KEY_3 = 0x33, + /** @brief The 4 key */ + KEY_4 = 0x34, + /** @brief The 5 key */ + KEY_5 = 0x35, + /** @brief The 6 key */ + KEY_6 = 0x36, + /** @brief The 7 key */ + KEY_7 = 0x37, + /** @brief The 8 key */ + KEY_8 = 0x38, + /** @brief The 9 key */ + KEY_9 = 0x39, + + /** @brief The A key. */ + KEY_A = 0x41, + /** @brief The B key. */ + KEY_B = 0x42, + /** @brief The C key. */ + KEY_C = 0x43, + /** @brief The D key. */ + KEY_D = 0x44, + /** @brief The E key. */ + KEY_E = 0x45, + /** @brief The F key. */ + KEY_F = 0x46, + /** @brief The G key. */ + KEY_G = 0x47, + /** @brief The H key. */ + KEY_H = 0x48, + /** @brief The I key. */ + KEY_I = 0x49, + /** @brief The J key. */ + KEY_J = 0x4A, + /** @brief The K key. */ + KEY_K = 0x4B, + /** @brief The L key. */ + KEY_L = 0x4C, + /** @brief The M key. */ + KEY_M = 0x4D, + /** @brief The N key. */ + KEY_N = 0x4E, + /** @brief The O key. */ + KEY_O = 0x4F, + /** @brief The P key. */ + KEY_P = 0x50, + /** @brief The Q key. */ + KEY_Q = 0x51, + /** @brief The R key. */ + KEY_R = 0x52, + /** @brief The S key. */ + KEY_S = 0x53, + /** @brief The T key. */ + KEY_T = 0x54, + /** @brief The U key. */ + KEY_U = 0x55, + /** @brief The V key. */ + KEY_V = 0x56, + /** @brief The W key. */ + KEY_W = 0x57, + /** @brief The X key. */ + KEY_X = 0x58, + /** @brief The Y key. */ + KEY_Y = 0x59, + /** @brief The Z key. */ + KEY_Z = 0x5A, + + /** @brief The left Windows/Super key. */ + KEY_LWIN = 0x5B, + /** @brief The right Windows/Super key. */ + KEY_RWIN = 0x5C, + KEY_APPS = 0x5D, + + /** @brief The sleep key. */ + KEY_SLEEP = 0x5F, + + /** @brief The numberpad 0 key. */ + KEY_NUMPAD0 = 0x60, + /** @brief The numberpad 1 key. */ + KEY_NUMPAD1 = 0x61, + /** @brief The numberpad 2 key. */ + KEY_NUMPAD2 = 0x62, + /** @brief The numberpad 3 key. */ + KEY_NUMPAD3 = 0x63, + /** @brief The numberpad 4 key. */ + KEY_NUMPAD4 = 0x64, + /** @brief The numberpad 5 key. */ + KEY_NUMPAD5 = 0x65, + /** @brief The numberpad 6 key. */ + KEY_NUMPAD6 = 0x66, + /** @brief The numberpad 7 key. */ + KEY_NUMPAD7 = 0x67, + /** @brief The numberpad 8 key. */ + KEY_NUMPAD8 = 0x68, + /** @brief The numberpad 9 key. */ + KEY_NUMPAD9 = 0x69, + /** @brief The numberpad multiply key. */ + KEY_MULTIPLY = 0x6A, + /** @brief The numberpad add key. */ + KEY_ADD = 0x6B, + /** @brief The numberpad separator key. */ + KEY_SEPARATOR = 0x6C, + /** @brief The numberpad subtract key. */ + KEY_SUBTRACT = 0x6D, + /** @brief The numberpad decimal key. */ + KEY_DECIMAL = 0x6E, + /** @brief The numberpad divide key. */ + KEY_DIVIDE = 0x6F, + + /** @brief The F1 key. */ + KEY_F1 = 0x70, + /** @brief The F2 key. */ + KEY_F2 = 0x71, + /** @brief The F3 key. */ + KEY_F3 = 0x72, + /** @brief The F4 key. */ + KEY_F4 = 0x73, + /** @brief The F5 key. */ + KEY_F5 = 0x74, + /** @brief The F6 key. */ + KEY_F6 = 0x75, + /** @brief The F7 key. */ + KEY_F7 = 0x76, + /** @brief The F8 key. */ + KEY_F8 = 0x77, + /** @brief The F9 key. */ + KEY_F9 = 0x78, + /** @brief The F10 key. */ + KEY_F10 = 0x79, + /** @brief The F11 key. */ + KEY_F11 = 0x7A, + /** @brief The F12 key. */ + KEY_F12 = 0x7B, + /** @brief The F13 key. */ + KEY_F13 = 0x7C, + /** @brief The F14 key. */ + KEY_F14 = 0x7D, + /** @brief The F15 key. */ + KEY_F15 = 0x7E, + /** @brief The F16 key. */ + KEY_F16 = 0x7F, + /** @brief The F17 key. */ + KEY_F17 = 0x80, + /** @brief The F18 key. */ + KEY_F18 = 0x81, + /** @brief The F19 key. */ + KEY_F19 = 0x82, + /** @brief The F20 key. */ + KEY_F20 = 0x83, + /** @brief The F21 key. */ + KEY_F21 = 0x84, + /** @brief The F22 key. */ + KEY_F22 = 0x85, + /** @brief The F23 key. */ + KEY_F23 = 0x86, + /** @brief The F24 key. */ + KEY_F24 = 0x87, + + /** @brief The number lock key. */ + KEY_NUMLOCK = 0x90, + + /** @brief The scroll lock key. */ + KEY_SCROLL = 0x91, + + /** @brief The numberpad equal key. */ + KEY_NUMPAD_EQUAL = 0x92, + + /** @brief The left shift key. */ + KEY_LSHIFT = 0xA0, + /** @brief The right shift key. */ + KEY_RSHIFT = 0xA1, + /** @brief The left control key. */ + KEY_LCONTROL = 0xA2, + /** @brief The right control key. */ + KEY_RCONTROL = 0xA3, + /** @brief The left alt key. */ + KEY_LALT = 0xA4, + /** @brief The right alt key. */ + KEY_RALT = 0xA5, + + /** @brief The semicolon key. */ + KEY_SEMICOLON = 0xBA, + /** @brief The plus key. */ + KEY_PLUS = 0xBB, + /** @brief The comma key. */ + KEY_COMMA = 0xBC, + /** @brief The minus key. */ + KEY_MINUS = 0xBD, + /** @brief The period key. */ + KEY_PERIOD = 0xBE, + /** @brief The slash key. */ + KEY_SLASH = 0xBF, + + /** @brief The grave key. */ + KEY_GRAVE = 0xC0, + KEYS_MAX_KEYS } keys; @@ -254,25 +295,115 @@ typedef enum keys { * @param state Either 0 or the allocated block of state memory. */ void input_system_initialize(u64* memory_requirement, void* state); + +/** + * @brief Shuts the input system down. + * @param state A pointer to the system state. + */ void input_system_shutdown(void* state); + +/** + * @brief Updates the input system every frame. + * @param delta_time The delta time in seconds since the last frame. + */ void input_update(f64 delta_time); // keyboard input + +/** + * @brief Indicates if the given key is currently pressed down. + * @param key They key to be checked. + * @returns True if currently pressed; otherwise false. + */ KAPI b8 input_is_key_down(keys key); + +/** + * @brief Indicates if the given key is NOT currently pressed down. + * @param key They key to be checked. + * @returns True if currently released; otherwise false. + */ KAPI b8 input_is_key_up(keys key); + +/** + * @brief Indicates if the given key was previously pressed down on the last frame. + * @param key They key to be checked. + * @returns True if was previously pressed; otherwise false. + */ KAPI b8 input_was_key_down(keys key); + +/** + * @brief Indicates if the given key was previously pressed down in the last frame. + * @param key They key to be checked. + * @returns True if previously released; otherwise false. + */ KAPI b8 input_was_key_up(keys key); +/** + * @brief Sets the state for the given key. + * @param key The key to be processed. + * @param pressed Indicates whether the key is currently pressed. + */ void input_process_key(keys key, b8 pressed); // mouse input + +/** + * @brief Indicates if the given mouse button is currently pressed. + * @param button The button to check. + * @returns True if currently pressed; otherwise false. + */ KAPI b8 input_is_button_down(buttons button); + +/** + * @brief Indicates if the given mouse button is currently released. + * @param button The button to check. + * @returns True if currently released; otherwise false. + */ KAPI b8 input_is_button_up(buttons button); + +/** + * @brief Indicates if the given mouse button was previously pressed in the last frame. + * @param button The button to check. + * @returns True if previously pressed; otherwise false. + */ KAPI b8 input_was_button_down(buttons button); + +/** + * @brief Indicates if the given mouse button was previously released in the last frame. + * @param button The button to check. + * @returns True if previously released; otherwise false. + */ KAPI b8 input_was_button_up(buttons button); + +/** + * @brief Obtains the current mouse position. + * @param x A pointer to hold the current mouse position on the x-axis. + * @param y A pointer to hold the current mouse position on the y-axis. + */ KAPI void input_get_mouse_position(i32* x, i32* y); + +/** + * @brief Obtains the previous mouse position. + * @param x A pointer to hold the previous mouse position on the x-axis. + * @param y A pointer to hold the previous mouse position on the y-axis. + */ KAPI void input_get_previous_mouse_position(i32* x, i32* y); +/** + * @brief Sets the press state of the given mouse button. + * @param button The mouse button whose state to set. + * @param pressed Indicates if the mouse button is currently pressed. + */ void input_process_button(buttons button, b8 pressed); + +/** + * @brief Sets the current position of the mouse to the given x and y positions. + * Also updates the previous position beforehand. + */ void input_process_mouse_move(i16 x, i16 y); + +/** + * @brief Processes mouse wheel scrolling. + * @param z_delta The amount of scrolling which occurred on the z axis (mouse wheel) + */ void input_process_mouse_wheel(i8 z_delta); diff --git a/engine/src/platform/platform_linux.c b/engine/src/platform/platform_linux.c index d2734695..aecb00d7 100644 --- a/engine/src/platform/platform_linux.c +++ b/engine/src/platform/platform_linux.c @@ -10,8 +10,8 @@ #include "containers/darray.h" #include -#include // sudo apt install libxcb-keysym1-dev -#include // sudo apt install libxcb-xkb-dev +#include +#include #include #if _POSIX_C_SOURCE >= 199309L @@ -35,12 +35,13 @@ typedef struct platform_state { xcb_screen_t* screen; xcb_atom_t wm_protocols; xcb_atom_t wm_delete_win; - xcb_key_symbols_t *syms; + xcb_key_symbols_t *syms; VkSurfaceKHR surface; } platform_state; static platform_state* state_ptr; +b8 internal_poll_for_event(xcb_generic_event_t **event); // Key translation keys translate_keycode(u32 x_keycode); @@ -59,29 +60,29 @@ b8 platform_system_startup( state_ptr = state; - // we get the connection through xcb + // We get the connection through xcb int screenp = 0; - state_ptr->connection = xcb_connect(NULL, &screenp); + state_ptr->connection = xcb_connect(0, &screenp); if (xcb_connection_has_error(state_ptr->connection)) { KFATAL("Failed to connect to X server via XCB."); return false; } - // unlike most reply_t this one must not be freed. - const xcb_query_extension_reply_t *ext_reply = xcb_get_extension_data(state_ptr->connection, &xcb_xkb_id); + // Unlike most reply_t this one must not be freed. + const xcb_query_extension_reply_t *ext_reply = xcb_get_extension_data(state_ptr->connection, &xcb_xkb_id); if (!ext_reply) { KFATAL("XKB extension not available on host X11 server."); return false; } - // we can now load xcb's extensions (xkb) + // We can now load xcb's extensions (xkb) xcb_generic_error_t *error; xcb_xkb_use_extension_cookie_t use_ext_cookie; - xcb_xkb_use_extension_reply_t *use_ext_reply = NULL; + xcb_xkb_use_extension_reply_t *use_ext_reply; use_ext_cookie = xcb_xkb_use_extension(state_ptr->connection, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION); use_ext_reply = xcb_xkb_use_extension_reply(state_ptr->connection, use_ext_cookie, &error); if (!use_ext_reply) { - KFATAL("Couldn't load the xcb-xkb extension"); + KFATAL("Couldn't load the xcb-xkb extension."); free(use_ext_reply); return false; } @@ -92,15 +93,19 @@ b8 platform_system_startup( } free(use_ext_reply); - // we can now deactivate repeat for this app only + // We can now deactivate repeat for this app only without affecting the system xcb_xkb_per_client_flags_cookie_t pcf_cookie; xcb_xkb_per_client_flags_reply_t *pcf_reply; - - pcf_cookie = xcb_xkb_per_client_flags(state_ptr->connection, XCB_XKB_ID_USE_CORE_KBD, XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, 0, 0, 0); + pcf_cookie = xcb_xkb_per_client_flags( + state_ptr->connection, + XCB_XKB_ID_USE_CORE_KBD, + XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, + XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, + 0, 0, 0); pcf_reply = xcb_xkb_per_client_flags_reply(state_ptr->connection, pcf_cookie, &error); free(pcf_reply); if (error) { - KERROR("failed to set XCB per-client flags, not using detectable repeat. error code : %u", error->major_code); + KERROR("Failed to set XKB per-client flags, not using detectable repeat. error code: %u", error->major_code); return false; } @@ -108,9 +113,8 @@ b8 platform_system_startup( state_ptr->syms = xcb_key_symbols_alloc(state_ptr->connection); // Get data from the X server - const xcb_setup_t* setup = xcb_get_setup(state_ptr->connection); + const struct xcb_setup_t* setup = xcb_get_setup(state_ptr->connection); - // Loop through screens state_ptr->screen = xcb_setup_roots_iterator(setup).data; // Allocate a XID for the window to be created. @@ -131,13 +135,13 @@ b8 platform_system_startup( u32 value_list[] = {state_ptr->screen->black_pixel, event_values}; // Create the window - xcb_void_cookie_t cookie = xcb_create_window( + xcb_create_window( state_ptr->connection, XCB_COPY_FROM_PARENT, // depth state_ptr->window, state_ptr->screen->root, // parent - x, //x - y, //y + (i16)x, //x + (i16)y, //y width, //width height, //height 0, // No border @@ -211,8 +215,6 @@ void platform_system_shutdown(void* plat_state) { } } -b8 internal_pool_for_event(xcb_generic_event_t **event); - b8 platform_pump_messages() { if (state_ptr) { xcb_generic_event_t* event; @@ -220,34 +222,33 @@ b8 platform_pump_messages() { b8 quit_flagged = false; - // Poll for events until null is returned. - while (internal_pool_for_event(&event)) { + // Poll for events until false is returned. + while (internal_poll_for_event(&event)) { // Input events switch (event->response_type & ~0x80) { - // while we could group PRESS and RELEASE together it's clearer to separate them case XCB_KEY_PRESS: { - xcb_key_press_event_t *key_event = (xcb_key_press_event_t*)event; - xcb_keysym_t key_sym = xcb_key_symbols_get_keysym(state_ptr->syms, key_event->detail,0); + xcb_key_press_event_t *key_event = (xcb_key_press_event_t *)event; + xcb_keysym_t key_sym = xcb_key_symbols_get_keysym(state_ptr->syms, key_event->detail, 0); input_process_key(translate_keycode(key_sym), true); } break; case XCB_KEY_RELEASE: { xcb_key_release_event_t *key_event = (xcb_key_release_event_t *)event; - xcb_keysym_t key_sym = xcb_key_symbols_get_keysym(state_ptr->syms, key_event->detail,0); + xcb_keysym_t key_sym = xcb_key_symbols_get_keysym(state_ptr->syms, key_event->detail, 0); input_process_key(translate_keycode(key_sym), false); } break; - // we need to separate PRESS and RELEASE to handle the wheel event + // we need to separate the PRESS and RELEASE events to handle the WHEEL event case XCB_BUTTON_PRESS: { - xcb_button_press_event_t *button_event = (xcb_button_press_event_t*)event; + xcb_button_press_event_t *button_event = (xcb_button_press_event_t *)event; // the wheel event is mapped to button 4 and 5 // 4 is down, while 5 is up if (button_event->detail > 3) { - input_process_mouse_wheel(button_event->detail == 4 ? -1 : 1 ); + input_process_mouse_wheel(button_event->detail == 4 ? -1 : 1); } else { input_process_button(button_event->detail, true); } - } break; + } case XCB_BUTTON_RELEASE: { - xcb_button_release_event_t *button_event = (xcb_button_release_event_t*)event; + xcb_button_press_event_t *button_event = (xcb_button_press_event_t *)event; input_process_button(button_event->detail, false); } break; case XCB_MOTION_NOTIFY: { @@ -280,20 +281,6 @@ b8 platform_pump_messages() { quit_flagged = true; } } break; - case XCB_MAP_NOTIFY: { - // The window just got raised - event_context context; - context.data.u16[0] = 1; - context.data.u16[1] = 1; - event_fire(EVENT_CODE_RAISED, 0, context); - } break; - case XCB_UNMAP_NOTIFY: { - // The window just got minimized - event_context context; - context.data.u16[0] = 0; - context.data.u16[1] = 0; - event_fire(EVENT_CODE_MINIZED, 0, context); - } break; default: // Something else break; @@ -381,13 +368,15 @@ b8 platform_create_vulkan_surface(vulkan_context* context) { return true; } -b8 internal_pool_for_event(xcb_generic_event_t **event) { +b8 internal_poll_for_event(xcb_generic_event_t **event) { if (state_ptr) { *event = xcb_poll_for_event(state_ptr->connection); } + return (*event != NULL); } +// Key translation keys translate_keycode(u32 x_keycode) { xcb_keysym_t upper = x_keycode; if ((x_keycode >> 8) == 0) { @@ -395,8 +384,250 @@ keys translate_keycode(u32 x_keycode) { upper -= (0x0061 - 0x0041); } } - - return upper; + switch (upper) { + case 0xff08: + return KEY_BACKSPACE; + case 0xff0d: + return KEY_ENTER; + case 0xff09: + return KEY_TAB; + case 0xff13: + return KEY_PAUSE; + case 0xffe5: + return KEY_CAPITAL; + case 0xff1b: + return KEY_ESCAPE; + case 0xff7e: + return KEY_MODECHANGE; + case 0x0020: + return KEY_SPACE; + case 0xff55: + return KEY_PRIOR; + case 0xff56: + return KEY_NEXT; + case 0xff57: + return KEY_END; + case 0xff50: + return KEY_HOME; + case 0xff51: + return KEY_LEFT; + case 0xff52: + return KEY_UP; + case 0xff53: + return KEY_RIGHT; + case 0xff54: + return KEY_DOWN; + case 0xff60: + return KEY_SELECT; + case 0xff61: + return KEY_PRINT; + case 0xff62: + return KEY_EXECUTE; + case 0xff63: + return KEY_INSERT; + case 0xffff: + return KEY_DELETE; + case 0xff6a: + return KEY_HELP; + + case 0xffeb: + return KEY_LWIN; // TODO: not sure this is right + case 0xffec: + return KEY_RWIN; + case 0xff9e: + return KEY_NUMPAD0; + case 0xff9c: + return KEY_NUMPAD1; + case 0xff99: + return KEY_NUMPAD2; + case 0xff9b: + return KEY_NUMPAD3; + case 0xff96: + return KEY_NUMPAD4; + case 0xff9d: + return KEY_NUMPAD5; + case 0xff98: + return KEY_NUMPAD6; + case 0xff95: + return KEY_NUMPAD7; + case 0xff97: + return KEY_NUMPAD8; + case 0xff9a: + return KEY_NUMPAD9; + case 0xffaa: + return KEY_MULTIPLY; + case 0xffab: + return KEY_ADD; + case 0xffac: + return KEY_SEPARATOR; + case 0xffad: + return KEY_SUBTRACT; + case 0xff9f: + return KEY_DECIMAL; + case 0xffaf: + return KEY_DIVIDE; + case 0xffbe: + return KEY_F1; + case 0xffbf: + return KEY_F2; + case 0xffc0: + return KEY_F3; + case 0xffc1: + return KEY_F4; + case 0xffc2: + return KEY_F5; + case 0xffc3: + return KEY_F6; + case 0xffc4: + return KEY_F7; + case 0xffc5: + return KEY_F8; + case 0xffc6: + return KEY_F9; + case 0xffc7: + return KEY_F10; + case 0xffc8: + return KEY_F11; + case 0xffc9: + return KEY_F12; + case 0xffca: + return KEY_F13; + case 0xffcb: + return KEY_F14; + case 0xffcc: + return KEY_F15; + case 0xffcd: + return KEY_F16; + case 0xffce: + return KEY_F17; + case 0xffcf: + return KEY_F18; + case 0xffd0: + return KEY_F19; + case 0xffd1: + return KEY_F20; + case 0xffd2: + return KEY_F21; + case 0xffd3: + return KEY_F22; + case 0xffd4: + return KEY_F23; + case 0xffd5: + return KEY_F24; + + case 0xff7f: + return KEY_NUMLOCK; + case 0xff14: + return KEY_SCROLL; + + case 0xffbd: + return KEY_NUMPAD_EQUAL; + + case 0xffe1: + return KEY_LSHIFT; + case 0xffe2: + return KEY_RSHIFT; + case 0xffe3: + return KEY_LCONTROL; + case 0xffe4: + return KEY_RCONTROL; + case 0xffe9: + return KEY_LALT; + case 0xfe03: + return KEY_RALT; + + case 0x003b: + return KEY_SEMICOLON; + case 0x002b: + return KEY_PLUS; + case 0x002c: + return KEY_COMMA; + case 0x002d: + return KEY_MINUS; + case 0x002e: + return KEY_PERIOD; + case 0x002f: + return KEY_SLASH; + case 0x0060: + return KEY_GRAVE; + + case 0x0030: + return KEY_0; + case 0x0031: + return KEY_1; + case 0x0032: + return KEY_2; + case 0x0033: + return KEY_3; + case 0x0034: + return KEY_4; + case 0x0035: + return KEY_5; + case 0x0036: + return KEY_6; + case 0x0037: + return KEY_7; + case 0x0038: + return KEY_8; + case 0x0039: + return KEY_9; + + case 0x0041: + return KEY_A; + case 0x0042: + return KEY_B; + case 0x0043: + return KEY_C; + case 0x0044: + return KEY_D; + case 0x0045: + return KEY_E; + case 0x0046: + return KEY_F; + case 0x0047: + return KEY_G; + case 0x0048: + return KEY_H; + case 0x0049: + return KEY_I; + case 0x004a: + return KEY_J; + case 0x004b: + return KEY_K; + case 0x004c: + return KEY_L; + case 0x004d: + return KEY_M; + case 0x004e: + return KEY_N; + case 0x004f: + return KEY_O; + case 0x0050: + return KEY_P; + case 0x0051: + return KEY_Q; + case 0x0052: + return KEY_R; + case 0x0053: + return KEY_S; + case 0x0054: + return KEY_T; + case 0x0055: + return KEY_U; + case 0x0056: + return KEY_V; + case 0x0057: + return KEY_W; + case 0x0058: + return KEY_X; + case 0x0059: + return KEY_Y; + case 0x005a: + return KEY_Z; + + default: + return 0; + } } -#endif +#endif \ No newline at end of file From 81eabb554510e2d9c63a8338b29fea9609cb3a42 Mon Sep 17 00:00:00 2001 From: S-Marais Date: Sat, 4 Jun 2022 21:13:19 +0200 Subject: [PATCH 4/4] Implementing key lookup table in platform file. Freeing some reply_t*. This should solve issue #53 --- engine/src/core/application.c | 6 +- engine/src/platform/platform_linux.c | 459 +++++++++------------------ 2 files changed, 149 insertions(+), 316 deletions(-) diff --git a/engine/src/core/application.c b/engine/src/core/application.c index 4dab9003..9ba88729 100644 --- a/engine/src/core/application.c +++ b/engine/src/core/application.c @@ -504,13 +504,9 @@ b8 application_run() { event_unregister(EVENT_CODE_APPLICATION_QUIT, 0, application_on_event); event_unregister(EVENT_CODE_KEY_PRESSED, 0, application_on_key); event_unregister(EVENT_CODE_KEY_RELEASED, 0, application_on_key); -<<<<<<< HEAD - event_unregister(EVENT_CODE_RESIZED, 0, application_on_resized); -======= // TODO: temp event_unregister(EVENT_CODE_DEBUG0, 0, event_on_debug_event); // TODO: end temp ->>>>>>> upstream/main input_system_shutdown(app_state->input_system_state); @@ -610,4 +606,4 @@ b8 application_on_resized(u16 code, void* sender, void* listener_inst, event_con // Event purposely not handled to allow other listeners to get this. return false; -} +} \ No newline at end of file diff --git a/engine/src/platform/platform_linux.c b/engine/src/platform/platform_linux.c index 45d456ee..165ec0ab 100644 --- a/engine/src/platform/platform_linux.c +++ b/engine/src/platform/platform_linux.c @@ -10,15 +10,8 @@ #include "containers/darray.h" #include -<<<<<<< HEAD #include #include -======= -#include -#include // sudo apt-get install libx11-dev -#include -#include // sudo apt-get install libxkbcommon-x11-dev libx11-xcb-dev ->>>>>>> upstream/main #include #if _POSIX_C_SOURCE >= 199309L @@ -36,6 +29,134 @@ #include #include "renderer/vulkan/vulkan_types.inl" +#define MAX_KEY_LOOKUP 232 + +static const u32 key_lookup_table[MAX_KEY_LOOKUP] = { + 0xff08, KEY_BACKSPACE, + 0xff0d, KEY_ENTER, + 0xff09, KEY_TAB, + 0xff13, KEY_PAUSE, + 0xffe5, KEY_CAPITAL, + 0xff1b, KEY_ESCAPE, + 0xff7e, KEY_MODECHANGE, + 0x0020, KEY_SPACE, + 0xff55, KEY_PRIOR, + 0xff56, KEY_NEXT, + 0xff57, KEY_END, + 0xff50, KEY_HOME, + 0xff51, KEY_LEFT, + 0xff52, KEY_UP, + 0xff53, KEY_RIGHT, + 0xff54, KEY_DOWN, + 0xff60, KEY_SELECT, + 0xff61, KEY_PRINT, + 0xff62, KEY_EXECUTE, + 0xff63, KEY_INSERT, + 0xffff, KEY_DELETE, + 0xff6a, KEY_HELP, + + 0xffeb, KEY_LWIN, // TODO: not sure this is right + 0xffec, KEY_RWIN, + 0xff9e, KEY_NUMPAD0, + 0xff9c, KEY_NUMPAD1, + 0xff99, KEY_NUMPAD2, + 0xff9b, KEY_NUMPAD3, + 0xff96, KEY_NUMPAD4, + 0xff9d, KEY_NUMPAD5, + 0xff98, KEY_NUMPAD6, + 0xff95, KEY_NUMPAD7, + 0xff97, KEY_NUMPAD8, + 0xff9a, KEY_NUMPAD9, + 0xffaa, KEY_MULTIPLY, + 0xffab, KEY_ADD, + 0xffac, KEY_SEPARATOR, + 0xffad, KEY_SUBTRACT, + 0xff9f, KEY_DECIMAL, + 0xffaf, KEY_DIVIDE, + 0xffbe, KEY_F1, + 0xffbf, KEY_F2, + 0xffc0, KEY_F3, + 0xffc1, KEY_F4, + 0xffc2, KEY_F5, + 0xffc3, KEY_F6, + 0xffc4, KEY_F7, + 0xffc5, KEY_F8, + 0xffc6, KEY_F9, + 0xffc7, KEY_F10, + 0xffc8, KEY_F11, + 0xffc9, KEY_F12, + 0xffca, KEY_F13, + 0xffcb, KEY_F14, + 0xffcc, KEY_F15, + 0xffcd, KEY_F16, + 0xffce, KEY_F17, + 0xffcf, KEY_F18, + 0xffd0, KEY_F19, + 0xffd1, KEY_F20, + 0xffd2, KEY_F21, + 0xffd3, KEY_F22, + 0xffd4, KEY_F23, + 0xffd5, KEY_F24, + + 0xff7f, KEY_NUMLOCK, + 0xff14, KEY_SCROLL, + + 0xffbd, KEY_NUMPAD_EQUAL, + + 0xffe1, KEY_LSHIFT, + 0xffe2, KEY_RSHIFT, + 0xffe3, KEY_LCONTROL, + 0xffe4, KEY_RCONTROL, + 0xffe9, KEY_LALT, + 0xfe03, KEY_RALT, + + 0x003b, KEY_SEMICOLON, + 0x002b, KEY_PLUS, + 0x002c, KEY_COMMA, + 0x002d, KEY_MINUS, + 0x002e, KEY_PERIOD, + 0x002f, KEY_SLASH, + 0x0060, KEY_GRAVE, + + 0x0030, KEY_0, + 0x0031, KEY_1, + 0x0032, KEY_2, + 0x0033, KEY_3, + 0x0034, KEY_4, + 0x0035, KEY_5, + 0x0036, KEY_6, + 0x0037, KEY_7, + 0x0038, KEY_8, + 0x0039, KEY_9, + + 0x0041, KEY_A, + 0x0042, KEY_B, + 0x0043, KEY_C, + 0x0044, KEY_D, + 0x0045, KEY_E, + 0x0046, KEY_F, + 0x0047, KEY_G, + 0x0048, KEY_H, + 0x0049, KEY_I, + 0x004a, KEY_J, + 0x004b, KEY_K, + 0x004c, KEY_L, + 0x004d, KEY_M, + 0x004e, KEY_N, + 0x004f, KEY_O, + 0x0050, KEY_P, + 0x0051, KEY_Q, + 0x0052, KEY_R, + 0x0053, KEY_S, + 0x0054, KEY_T, + 0x0055, KEY_U, + 0x0056, KEY_V, + 0x0057, KEY_W, + 0x0058, KEY_X, + 0x0059, KEY_Y, + 0x005a, KEY_Z +}; + typedef struct platform_state { xcb_connection_t* connection; xcb_window_t window; @@ -170,36 +291,38 @@ b8 platform_system_startup( // Tell the server to notify when the window manager // attempts to destroy the window. - xcb_intern_atom_cookie_t wm_delete_cookie = xcb_intern_atom( + xcb_intern_atom_cookie_t wm_cookie = xcb_intern_atom( state_ptr->connection, 0, strlen("WM_DELETE_WINDOW"), "WM_DELETE_WINDOW"); - xcb_intern_atom_cookie_t wm_protocols_cookie = xcb_intern_atom( + xcb_intern_atom_reply_t* wm_reply = xcb_intern_atom_reply( + state_ptr->connection, + wm_cookie, + NULL); + state_ptr->wm_delete_win = wm_reply->atom; + free(wm_reply); + wm_cookie = xcb_intern_atom( state_ptr->connection, 0, strlen("WM_PROTOCOLS"), "WM_PROTOCOLS"); - xcb_intern_atom_reply_t* wm_delete_reply = xcb_intern_atom_reply( - state_ptr->connection, - wm_delete_cookie, - NULL); - xcb_intern_atom_reply_t* wm_protocols_reply = xcb_intern_atom_reply( + wm_reply = xcb_intern_atom_reply( state_ptr->connection, - wm_protocols_cookie, + wm_cookie, NULL); - state_ptr->wm_delete_win = wm_delete_reply->atom; - state_ptr->wm_protocols = wm_protocols_reply->atom; + state_ptr->wm_protocols = wm_reply->atom; + free(wm_reply); xcb_change_property( state_ptr->connection, XCB_PROP_MODE_REPLACE, state_ptr->window, - wm_protocols_reply->atom, + state_ptr->wm_protocols, 4, 32, 1, - &wm_delete_reply->atom); + &state_ptr->wm_delete_win); // Map the window to the screen xcb_map_window(state_ptr->connection, state_ptr->window); @@ -229,7 +352,6 @@ b8 platform_pump_messages() { b8 quit_flagged = false; -<<<<<<< HEAD // Poll for events until false is returned. while (internal_poll_for_event(&event)) { // Input events @@ -238,28 +360,6 @@ b8 platform_pump_messages() { xcb_key_press_event_t *key_event = (xcb_key_press_event_t *)event; xcb_keysym_t key_sym = xcb_key_symbols_get_keysym(state_ptr->syms, key_event->detail, 0); input_process_key(translate_keycode(key_sym), true); -======= - // Poll for events until null is returned. - while ((event = xcb_poll_for_event(state_ptr->connection))) { - // Input events - switch (event->response_type & ~0x80) { - case XCB_KEY_PRESS: - case XCB_KEY_RELEASE: { - // Key press event - xcb_key_press_event_t and xcb_key_release_event_t are the same - xcb_key_press_event_t* kb_event = (xcb_key_press_event_t*)event; - b8 pressed = event->response_type == XCB_KEY_PRESS; - xcb_keycode_t code = kb_event->detail; - KeySym key_sym = XkbKeycodeToKeysym( - state_ptr->display, - (KeyCode)code, //event.xkey.keycode, - 0, - 0 /*code & ShiftMask ? 1 : 0*/); - - keys key = translate_keycode(key_sym); - - // Pass to the input subsystem for processing. - input_process_key(key, pressed); ->>>>>>> upstream/main } break; case XCB_KEY_RELEASE: { xcb_key_release_event_t *key_event = (xcb_key_release_event_t *)event; @@ -414,276 +514,13 @@ keys translate_keycode(u32 x_keycode) { upper -= (0x0061 - 0x0041); } } - switch (upper) { - case 0xff08: - return KEY_BACKSPACE; - case 0xff0d: - return KEY_ENTER; - case 0xff09: - return KEY_TAB; - case 0xff13: - return KEY_PAUSE; - case 0xffe5: - return KEY_CAPITAL; - case 0xff1b: - return KEY_ESCAPE; - case 0xff7e: - return KEY_MODECHANGE; - case 0x0020: - return KEY_SPACE; - case 0xff55: - return KEY_PRIOR; - case 0xff56: - return KEY_NEXT; - case 0xff57: - return KEY_END; - case 0xff50: - return KEY_HOME; - case 0xff51: - return KEY_LEFT; - case 0xff52: - return KEY_UP; - case 0xff53: - return KEY_RIGHT; - case 0xff54: - return KEY_DOWN; - case 0xff60: - return KEY_SELECT; - case 0xff61: - return KEY_PRINT; - case 0xff62: - return KEY_EXECUTE; - case 0xff63: - return KEY_INSERT; - case 0xffff: - return KEY_DELETE; - case 0xff6a: - return KEY_HELP; - - case 0xffeb: - return KEY_LWIN; // TODO: not sure this is right - case 0xffec: - return KEY_RWIN; - case 0xff9e: - return KEY_NUMPAD0; - case 0xff9c: - return KEY_NUMPAD1; - case 0xff99: - return KEY_NUMPAD2; - case 0xff9b: - return KEY_NUMPAD3; - case 0xff96: - return KEY_NUMPAD4; - case 0xff9d: - return KEY_NUMPAD5; - case 0xff98: - return KEY_NUMPAD6; - case 0xff95: - return KEY_NUMPAD7; - case 0xff97: - return KEY_NUMPAD8; - case 0xff9a: - return KEY_NUMPAD9; - case 0xffaa: - return KEY_MULTIPLY; - case 0xffab: - return KEY_ADD; - case 0xffac: - return KEY_SEPARATOR; - case 0xffad: - return KEY_SUBTRACT; - case 0xff9f: - return KEY_DECIMAL; - case 0xffaf: - return KEY_DIVIDE; - case 0xffbe: - return KEY_F1; - case 0xffbf: - return KEY_F2; - case 0xffc0: - return KEY_F3; - case 0xffc1: - return KEY_F4; - case 0xffc2: - return KEY_F5; - case 0xffc3: - return KEY_F6; - case 0xffc4: - return KEY_F7; - case 0xffc5: - return KEY_F8; - case 0xffc6: - return KEY_F9; - case 0xffc7: - return KEY_F10; - case 0xffc8: - return KEY_F11; - case 0xffc9: - return KEY_F12; - case 0xffca: - return KEY_F13; - case 0xffcb: - return KEY_F14; - case 0xffcc: - return KEY_F15; - case 0xffcd: - return KEY_F16; - case 0xffce: - return KEY_F17; - case 0xffcf: - return KEY_F18; - case 0xffd0: - return KEY_F19; - case 0xffd1: - return KEY_F20; - case 0xffd2: - return KEY_F21; - case 0xffd3: - return KEY_F22; - case 0xffd4: - return KEY_F23; - case 0xffd5: - return KEY_F24; - - case 0xff7f: - return KEY_NUMLOCK; - case 0xff14: - return KEY_SCROLL; - - case 0xffbd: - return KEY_NUMPAD_EQUAL; - - case 0xffe1: - return KEY_LSHIFT; - case 0xffe2: - return KEY_RSHIFT; - case 0xffe3: - return KEY_LCONTROL; - case 0xffe4: - return KEY_RCONTROL; - case 0xffe9: - return KEY_LALT; - case 0xfe03: - return KEY_RALT; - - case 0x003b: - return KEY_SEMICOLON; - case 0x002b: - return KEY_PLUS; - case 0x002c: - return KEY_COMMA; - case 0x002d: - return KEY_MINUS; - case 0x002e: - return KEY_PERIOD; - case 0x002f: - return KEY_SLASH; - case 0x0060: - return KEY_GRAVE; - -<<<<<<< HEAD - case 0x0030: - return KEY_0; - case 0x0031: - return KEY_1; - case 0x0032: - return KEY_2; - case 0x0033: - return KEY_3; - case 0x0034: - return KEY_4; - case 0x0035: - return KEY_5; - case 0x0036: - return KEY_6; - case 0x0037: - return KEY_7; - case 0x0038: - return KEY_8; - case 0x0039: - return KEY_9; - - case 0x0041: -======= - case XK_0: - return KEY_0; - case XK_1: - return KEY_1; - case XK_2: - return KEY_2; - case XK_3: - return KEY_3; - case XK_4: - return KEY_4; - case XK_5: - return KEY_5; - case XK_6: - return KEY_6; - case XK_7: - return KEY_7; - case XK_8: - return KEY_8; - case XK_9: - return KEY_9; - - case XK_a: - case XK_A: ->>>>>>> upstream/main - return KEY_A; - case 0x0042: - return KEY_B; - case 0x0043: - return KEY_C; - case 0x0044: - return KEY_D; - case 0x0045: - return KEY_E; - case 0x0046: - return KEY_F; - case 0x0047: - return KEY_G; - case 0x0048: - return KEY_H; - case 0x0049: - return KEY_I; - case 0x004a: - return KEY_J; - case 0x004b: - return KEY_K; - case 0x004c: - return KEY_L; - case 0x004d: - return KEY_M; - case 0x004e: - return KEY_N; - case 0x004f: - return KEY_O; - case 0x0050: - return KEY_P; - case 0x0051: - return KEY_Q; - case 0x0052: - return KEY_R; - case 0x0053: - return KEY_S; - case 0x0054: - return KEY_T; - case 0x0055: - return KEY_U; - case 0x0056: - return KEY_V; - case 0x0057: - return KEY_W; - case 0x0058: - return KEY_X; - case 0x0059: - return KEY_Y; - case 0x005a: - return KEY_Z; - - default: - return 0; + for (u32 i = 0; i < MAX_KEY_LOOKUP; ++i) { + if (key_lookup_table[i] == upper) { + return key_lookup_table[i + 1]; + } } + + return KEYS_MAX_KEYS; } #endif \ No newline at end of file