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