diff options
| -rw-r--r-- | include/stl/window.h | 1 | ||||
| -rw-r--r-- | meson.build | 9 | ||||
| -rw-r--r-- | src/keys.h | 131 | ||||
| -rw-r--r-- | src/window.c | 6 | ||||
| -rw-r--r-- | src/window.h | 8 | ||||
| -rw-r--r-- | src/window_glfw.c | 36 | ||||
| -rw-r--r-- | src/window_glfw.h | 2 | ||||
| -rw-r--r-- | src/window_wayland.c | 136 | ||||
| -rw-r--r-- | src/window_wayland.h | 4 | ||||
| -rw-r--r-- | src/window_win32.c | 168 | ||||
| -rw-r--r-- | src/window_win32.h | 6 | ||||
| -rw-r--r-- | subprojects/libalabaster.wrap | 2 | ||||
| -rw-r--r-- | subprojects/libnaunet.wrap | 2 |
13 files changed, 477 insertions, 34 deletions
diff --git a/include/stl/window.h b/include/stl/window.h index b0c0068..e89a113 100644 --- a/include/stl/window.h +++ b/include/stl/window.h @@ -1,3 +1,4 @@ #pragma once #include "../../src/window.h" +#include "../../src/keys.h" diff --git a/meson.build b/meson.build index f96e19b..91cd1cc 100644 --- a/meson.build +++ b/meson.build @@ -4,8 +4,8 @@ project('stela', 'c', version: '0.01', compiler = meson.get_compiler('c') cmake = import('cmake') -is_debug = get_option('buildtype').startswith('debug') -is_minsize = get_option('buildtype') == 'minsize' +is_debug = get_option('debug') +is_minsize = get_option('optimization') == 's' is_linux = host_machine.system() == 'linux' is_windows = host_machine.system() == 'windows' @@ -184,9 +184,6 @@ elif get_option('window') == 'glfw' stela_src += ['src/window_glfw.c'] stela_deps += [glfw3] stela_args += ['-DSTELA_WINDOW_GLFW'] - if is_linux - stela_args += ['-DSTELA_USE_EGL'] - endif elif get_option('window') == 'glfm' glfm = dependency('glfm', required: false, allow_fallback: false) if not glfm.found() @@ -198,7 +195,7 @@ elif get_option('window') == 'glfm' endif stela_src += ['src/window_glfm.c'] stela_deps += [glfm] - stela_args += ['-DSTELA_WINDOW_GLFM', '-DSTELA_USE_EGL'] + stela_args += ['-DSTELA_WINDOW_GLFM'] endif stela = declare_dependency(include_directories: stela_inc, diff --git a/src/keys.h b/src/keys.h new file mode 100644 index 0000000..d4d17f0 --- /dev/null +++ b/src/keys.h @@ -0,0 +1,131 @@ +#pragma once + +// https://github.com/glfw/glfw/blob/e7ea71be039836da3a98cea55ae5569cb5eb885c/include/STELA/glfw3.h#L395C1-L519C50 + +/* Printable keys */ +#define STELA_KEY_0 48 +#define STELA_KEY_1 49 +#define STELA_KEY_2 50 +#define STELA_KEY_3 51 +#define STELA_KEY_4 52 +#define STELA_KEY_5 53 +#define STELA_KEY_6 54 +#define STELA_KEY_7 55 +#define STELA_KEY_8 56 +#define STELA_KEY_9 57 +#define STELA_KEY_A 65 +#define STELA_KEY_B 66 +#define STELA_KEY_C 67 +#define STELA_KEY_D 68 +#define STELA_KEY_E 69 +#define STELA_KEY_F 70 +#define STELA_KEY_G 71 +#define STELA_KEY_H 72 +#define STELA_KEY_I 73 +#define STELA_KEY_J 74 +#define STELA_KEY_K 75 +#define STELA_KEY_L 76 +#define STELA_KEY_M 77 +#define STELA_KEY_N 78 +#define STELA_KEY_O 79 +#define STELA_KEY_P 80 +#define STELA_KEY_Q 81 +#define STELA_KEY_R 82 +#define STELA_KEY_S 83 +#define STELA_KEY_T 84 +#define STELA_KEY_U 85 +#define STELA_KEY_V 86 +#define STELA_KEY_W 87 +#define STELA_KEY_X 88 +#define STELA_KEY_Y 89 +#define STELA_KEY_Z 90 + +#define STELA_KEY_SPACE 32 +#define STELA_KEY_APOSTROPHE 39 /* ' */ +#define STELA_KEY_COMMA 44 /* , */ +#define STELA_KEY_PERIOD 46 /* . */ +#define STELA_KEY_SLASH 47 /* / */ +#define STELA_KEY_BACKSLASH 92 /* \ */ +#define STELA_KEY_GRAVE_ACCENT 96 /* ` */ +#define STELA_KEY_MINUS 45 /* - */ +#define STELA_KEY_LEFT_BRACKET 91 /* [ */ +#define STELA_KEY_RIGHT_BRACKET 93 /* ] */ +#define STELA_KEY_SEMICOLON 59 /* ; */ +#define STELA_KEY_EQUAL 61 /* = */ + +/* Function keys */ +#define STELA_KEY_RETURN 257 +#define STELA_KEY_BACKSPACE 259 +#define STELA_KEY_TAB 258 +#define STELA_KEY_ESCAPE 256 +#define STELA_KEY_INSERT 260 +#define STELA_KEY_DELETE 261 +#define STELA_KEY_HOME 268 +#define STELA_KEY_END 269 +#define STELA_KEY_PAGE_UP 266 +#define STELA_KEY_PAGE_DOWN 267 +#define STELA_KEY_PRINT_SCREEN 283 +#define STELA_KEY_PAUSE 284 +#define STELA_KEY_CAPS_LOCK 280 +#define STELA_KEY_SCROLL_LOCK 281 +#define STELA_KEY_NUM_LOCK 282 + +#define STELA_KEY_LEFT 263 +#define STELA_KEY_RIGHT 262 +#define STELA_KEY_UP 265 +#define STELA_KEY_DOWN 264 + +#define STELA_KEY_F1 290 +#define STELA_KEY_F2 291 +#define STELA_KEY_F3 292 +#define STELA_KEY_F4 293 +#define STELA_KEY_F5 294 +#define STELA_KEY_F6 295 +#define STELA_KEY_F7 296 +#define STELA_KEY_F8 297 +#define STELA_KEY_F9 298 +#define STELA_KEY_F10 299 +#define STELA_KEY_F11 300 +#define STELA_KEY_F12 301 +#define STELA_KEY_F13 302 +#define STELA_KEY_F14 303 +#define STELA_KEY_F15 304 +#define STELA_KEY_F16 305 +#define STELA_KEY_F17 306 +#define STELA_KEY_F18 307 +#define STELA_KEY_F19 308 +#define STELA_KEY_F20 309 +#define STELA_KEY_F21 310 +#define STELA_KEY_F22 311 +#define STELA_KEY_F23 312 +#define STELA_KEY_F24 313 + +#define STELA_KEY_KP_0 320 +#define STELA_KEY_KP_1 321 +#define STELA_KEY_KP_2 322 +#define STELA_KEY_KP_3 323 +#define STELA_KEY_KP_4 324 +#define STELA_KEY_KP_5 325 +#define STELA_KEY_KP_6 326 +#define STELA_KEY_KP_7 327 +#define STELA_KEY_KP_8 328 +#define STELA_KEY_KP_9 329 +#define STELA_KEY_KP_ADD 334 +#define STELA_KEY_KP_SUBTRACT 333 +#define STELA_KEY_KP_MULTIPLY 332 +#define STELA_KEY_KP_DIVIDE 331 +#define STELA_KEY_KP_DECIMAL 330 +#define STELA_KEY_KP_EQUAL 336 +#define STELA_KEY_KP_RETURN 335 + +#define STELA_KEY_LEFT_CONTROL 341 +#define STELA_KEY_LEFT_ALT 342 +#define STELA_KEY_LEFT_SHIFT 340 +#define STELA_KEY_RIGHT_CONTROL 345 +#define STELA_KEY_RIGHT_ALT 346 +#define STELA_KEY_RIGHT_SHIFT 344 +#define STELA_KEY_LEFT_SUPER 343 +#define STELA_KEY_RIGHT_SUPER 347 +#define STELA_KEY_MENU 348 + +#define STELA_KEY_LAST STELA_KEY_MENU diff --git a/src/window.c b/src/window.c index 9afe285..1da52df 100644 --- a/src/window.c +++ b/src/window.c @@ -78,7 +78,7 @@ void stl_window_send_mouse_button_event(struct stl_window *window, u8 state, u8 #endif } -void stl_window_send_key_event(struct stl_window *window, u8 state, u8 button) +void stl_window_send_key_event(struct stl_window *window, u8 state, u16 button) { if (window->key_immediate_callback) { window->key_immediate_callback(window->userdata, state, button); @@ -87,7 +87,7 @@ void stl_window_send_key_event(struct stl_window *window, u8 state, u8 button) #ifdef STELA_EVENT_BUFFER GET_EVENT_CHUNK(STELA_EVENT_KEY); ptr[1] = state; - ptr[2] = button; + ((u16 *)(&ptr[2]))[0] = button; APPEND_EVENT_CHUNK(); #else if (window->key_callback) { @@ -203,7 +203,7 @@ bool stl_window_read_events(struct stl_window *window) } case STELA_EVENT_KEY: { u8 state = bc[1]; - u8 button = bc[2]; + u16 button = ((u16 *)(&bc[2]))[0]; if (window->key_callback) { if (window->key_callback(window->userdata, state, button)) { #ifdef STELA_PAUSE diff --git a/src/window.h b/src/window.h index 7df00a3..fadcebd 100644 --- a/src/window.h +++ b/src/window.h @@ -104,9 +104,9 @@ struct stl_window { bool (*pointer_pos_callback)(void *, f64, f64); bool (*scroll_callback)(void *, f64); bool (*mouse_button_callback)(void *, u8, u8); - bool (*key_callback)(void *, u8, u8); + bool (*key_callback)(void *, u8, u16); // Always runs on the thread that's processing events. - void (*key_immediate_callback)(void *, u8, u8); + void (*key_immediate_callback)(void *, u8, u16); void (*resize_callback)(void *, u32, u32); void (*refresh_callback)(void *); void (*should_close_callback)(void *); @@ -139,6 +139,8 @@ enum { #define STELA_EVENTS_SIZE (STELA_EVENT_SIZE * 128) #endif +extern s32 window_system_main(s32 argc, str *argv); + void stl_set_output_discovered_callback(void (*output_discovered)(void *, struct stl_monitor *), void *userdata); s32 stl_global_init(s32 argc, str *argv, s32 (*main)(s32, str *), bool skip_graphics); s32 stl_global_get_poll_fd(); @@ -150,7 +152,7 @@ struct stl_window *stl_window_create(void *context); void stl_window_send_pointer_pos_event(struct stl_window *window, f64 x, f64 y); void stl_window_send_scroll_event(struct stl_window *window, f64 y); void stl_window_send_mouse_button_event(struct stl_window *window, u8 state, u8 button); -void stl_window_send_key_event(struct stl_window *window, u8 state, u8 button); +void stl_window_send_key_event(struct stl_window *window, u8 state, u16 button); void stl_window_send_resize_event(struct stl_window *window, u32 width, u32 height); #ifdef STELA_POLL_INLINE void stl_window_send_refresh_event(struct stl_window *window); diff --git a/src/window_glfw.c b/src/window_glfw.c index bfd266f..0d381a5 100644 --- a/src/window_glfw.c +++ b/src/window_glfw.c @@ -68,18 +68,25 @@ void stl_global_close(void) static void key_callback(GLFWwindow *window, s32 key, s32 scancode, s32 action, s32 mods) { struct stl_window_glfw *glfw = glfwGetWindowUserPointer(window); - (void)key; + (void)scancode; (void)mods; + al_assert(key >= 0 && key <= UINT16_MAX); + if (key == GLFW_KEY_F25 || key == GLFW_KEY_WORLD_1 || key == GLFW_KEY_WORLD_2) { + al_log_warn("window_glfw", "Unmapped GLFW key pressed (%hu).", (u16)key); + return; + } if (action == GLFW_PRESS) { - stl_window_send_key_event(&glfw->w, STELA_BUTTON_PRESSED, scancode); + stl_window_send_key_event(&glfw->w, STELA_BUTTON_PRESSED, (u16)key); } else if (action == GLFW_RELEASE) { - stl_window_send_key_event(&glfw->w, STELA_BUTTON_RELEASED, scancode); + stl_window_send_key_event(&glfw->w, STELA_BUTTON_RELEASED, (u16)key); } } static void cursor_position_callback(GLFWwindow *window, f64 xpos, f64 ypos) { struct stl_window_glfw *glfw = glfwGetWindowUserPointer(window); + xpos *= glfw->scale_x; + ypos *= glfw->scale_y; stl_window_send_pointer_pos_event(&glfw->w, xpos, ypos); } @@ -139,6 +146,13 @@ static void resize_callback(GLFWwindow *window, s32 event_width, s32 event_heigh stl_window_send_resize_event(&glfw->w, glfw->w.width, glfw->w.height); } +static void content_scale_callback(GLFWwindow *window, f32 xscale, f32 yscale) +{ + struct stl_window_glfw *glfw = glfwGetWindowUserPointer(window); + glfw->scale_x = xscale; + glfw->scale_y = yscale; +} + #ifdef STELA_POLL_INLINE static void refresh_callback(GLFWwindow *window) { @@ -223,10 +237,12 @@ static bool window_glfw_create_window(struct stl_window *window, u32 width, u32 glfwSwapInterval((flags & STELA_WINDOW_VSYNC) ? 1 : 0); glfwMakeContextCurrent(NULL); #else + (void)flags; #endif s32 buffer_width, buffer_height; - glfwGetFramebufferSize(glfw->window, &buffer_width, &buffer_height); + //glfwGetFramebufferSize(glfw->window, &buffer_width, &buffer_height); + glfwGetWindowSize(glfw->window, &buffer_width, &buffer_height); al_assert(buffer_width > 0 && buffer_height > 0); glfw->w.width = (u32)buffer_width; glfw->w.height = (u32)buffer_height; @@ -243,7 +259,9 @@ static bool window_glfw_create_window(struct stl_window *window, u32 width, u32 glfwSetCursorPosCallback(glfw->window, cursor_position_callback); glfwSetMouseButtonCallback(glfw->window, mouse_button_callback); glfwSetScrollCallback(glfw->window, scroll_callback); - glfwSetFramebufferSizeCallback(glfw->window, resize_callback); + //glfwSetFramebufferSizeCallback(glfw->window, resize_callback); + glfwSetWindowSizeCallback(glfw->window, resize_callback); + glfwSetWindowContentScaleCallback(glfw->window, content_scale_callback); #ifdef STELA_POLL_INLINE glfwSetWindowRefreshCallback(glfw->window, refresh_callback); #endif @@ -326,12 +344,10 @@ static void window_glfw_toggle_fullscreen(struct stl_window *window) GLFWmonitor *monitor = get_current_monitor(glfw->window, glfw->prev.x, glfw->prev.y); if (!monitor) return; const GLFWvidmode *mode = glfwGetVideoMode(monitor); - f32 xscale, yscale; - glfwGetMonitorContentScale(monitor, &xscale, &yscale); - glfw->prev.width = glfw->w.width / xscale; - glfw->prev.height = glfw->w.height / yscale; + glfw->prev.width = glfw->w.width; + glfw->prev.height = glfw->w.height; glfwSetWindowMonitor(glfw->window, monitor, 0, 0, - mode->width / xscale, mode->height / yscale, mode->refreshRate); + mode->width, mode->height, mode->refreshRate); //glfwSetInputMode(glfw->window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); } else { glfwSetWindowMonitor(glfw->window, NULL, glfw->prev.x, glfw->prev.y, diff --git a/src/window_glfw.h b/src/window_glfw.h index 2cde53c..8f5857c 100644 --- a/src/window_glfw.h +++ b/src/window_glfw.h @@ -26,6 +26,8 @@ struct stl_glfw_global { struct stl_window_glfw { struct stl_window w; GLFWwindow *window; + f32 scale_x; + f32 scale_y; struct { s32 x; s32 y; diff --git a/src/window_wayland.c b/src/window_wayland.c index 64177b9..b289843 100644 --- a/src/window_wayland.c +++ b/src/window_wayland.c @@ -4,6 +4,7 @@ #include "window_wayland.h" #include "common.h" #include "poll_common.h" +#include "keys.h" static struct stl_wayland_global global; @@ -127,6 +128,135 @@ void stl_set_output_discovered_callback(void (*output_discovered)(void *, struct global.userdata = userdata; } +static void set_keycodes(void) +{ + al_memset(global.keycodes, 0, sizeof(global.keycodes)); + + global.keycodes[KEY_0] = STELA_KEY_0; + global.keycodes[KEY_1] = STELA_KEY_1; + global.keycodes[KEY_2] = STELA_KEY_2; + global.keycodes[KEY_3] = STELA_KEY_3; + global.keycodes[KEY_4] = STELA_KEY_4; + global.keycodes[KEY_5] = STELA_KEY_5; + global.keycodes[KEY_6] = STELA_KEY_6; + global.keycodes[KEY_7] = STELA_KEY_7; + global.keycodes[KEY_8] = STELA_KEY_8; + global.keycodes[KEY_9] = STELA_KEY_9; + global.keycodes[KEY_A] = STELA_KEY_A; + global.keycodes[KEY_B] = STELA_KEY_B; + global.keycodes[KEY_C] = STELA_KEY_C; + global.keycodes[KEY_D] = STELA_KEY_D; + global.keycodes[KEY_E] = STELA_KEY_E; + global.keycodes[KEY_F] = STELA_KEY_F; + global.keycodes[KEY_G] = STELA_KEY_G; + global.keycodes[KEY_H] = STELA_KEY_H; + global.keycodes[KEY_I] = STELA_KEY_I; + global.keycodes[KEY_J] = STELA_KEY_J; + global.keycodes[KEY_K] = STELA_KEY_K; + global.keycodes[KEY_L] = STELA_KEY_L; + global.keycodes[KEY_M] = STELA_KEY_M; + global.keycodes[KEY_N] = STELA_KEY_N; + global.keycodes[KEY_O] = STELA_KEY_O; + global.keycodes[KEY_P] = STELA_KEY_P; + global.keycodes[KEY_Q] = STELA_KEY_Q; + global.keycodes[KEY_R] = STELA_KEY_R; + global.keycodes[KEY_S] = STELA_KEY_S; + global.keycodes[KEY_T] = STELA_KEY_T; + global.keycodes[KEY_U] = STELA_KEY_U; + global.keycodes[KEY_V] = STELA_KEY_V; + global.keycodes[KEY_W] = STELA_KEY_W; + global.keycodes[KEY_Y] = STELA_KEY_Y; + global.keycodes[KEY_X] = STELA_KEY_X; + global.keycodes[KEY_Z] = STELA_KEY_Z; + + global.keycodes[KEY_SPACE] = STELA_KEY_SPACE; + global.keycodes[KEY_APOSTROPHE] = STELA_KEY_APOSTROPHE; + global.keycodes[KEY_COMMA] = STELA_KEY_COMMA; + global.keycodes[KEY_DOT] = STELA_KEY_PERIOD; + global.keycodes[KEY_SLASH] = STELA_KEY_SLASH; + global.keycodes[KEY_BACKSLASH] = STELA_KEY_BACKSLASH; + global.keycodes[KEY_GRAVE] = STELA_KEY_GRAVE_ACCENT; + global.keycodes[KEY_MINUS] = STELA_KEY_MINUS; + global.keycodes[KEY_LEFTBRACE] = STELA_KEY_LEFT_BRACKET; + global.keycodes[KEY_RIGHTBRACE] = STELA_KEY_RIGHT_BRACKET; + global.keycodes[KEY_SEMICOLON] = STELA_KEY_SEMICOLON; + global.keycodes[KEY_EQUAL] = STELA_KEY_EQUAL; + + global.keycodes[KEY_ENTER] = STELA_KEY_RETURN; + global.keycodes[KEY_BACKSPACE] = STELA_KEY_BACKSPACE; + global.keycodes[KEY_TAB] = STELA_KEY_TAB; + global.keycodes[KEY_ESC] = STELA_KEY_ESCAPE; + global.keycodes[KEY_INSERT] = STELA_KEY_INSERT; + global.keycodes[KEY_DELETE] = STELA_KEY_DELETE; + global.keycodes[KEY_HOME] = STELA_KEY_HOME; + global.keycodes[KEY_END] = STELA_KEY_END; + global.keycodes[KEY_PAGEUP] = STELA_KEY_PAGE_UP; + global.keycodes[KEY_PAGEDOWN] = STELA_KEY_PAGE_DOWN; + global.keycodes[KEY_PRINT] = STELA_KEY_PRINT_SCREEN; + global.keycodes[KEY_PAUSE] = STELA_KEY_PAUSE; + global.keycodes[KEY_CAPSLOCK] = STELA_KEY_CAPS_LOCK; + global.keycodes[KEY_SCROLLLOCK] = STELA_KEY_SCROLL_LOCK; + global.keycodes[KEY_NUMLOCK] = STELA_KEY_NUM_LOCK; + + global.keycodes[KEY_LEFT] = STELA_KEY_LEFT; + global.keycodes[KEY_RIGHT] = STELA_KEY_RIGHT; + global.keycodes[KEY_UP] = STELA_KEY_UP; + global.keycodes[KEY_DOWN] = STELA_KEY_DOWN; + + global.keycodes[KEY_F1] = STELA_KEY_F1; + global.keycodes[KEY_F2] = STELA_KEY_F2; + global.keycodes[KEY_F3] = STELA_KEY_F3; + global.keycodes[KEY_F4] = STELA_KEY_F4; + global.keycodes[KEY_F5] = STELA_KEY_F5; + global.keycodes[KEY_F6] = STELA_KEY_F6; + global.keycodes[KEY_F7] = STELA_KEY_F7; + global.keycodes[KEY_F8] = STELA_KEY_F8; + global.keycodes[KEY_F9] = STELA_KEY_F9; + global.keycodes[KEY_F10] = STELA_KEY_F10; + global.keycodes[KEY_F11] = STELA_KEY_F11; + global.keycodes[KEY_F12] = STELA_KEY_F12; + global.keycodes[KEY_F13] = STELA_KEY_F13; + global.keycodes[KEY_F14] = STELA_KEY_F14; + global.keycodes[KEY_F15] = STELA_KEY_F15; + global.keycodes[KEY_F16] = STELA_KEY_F16; + global.keycodes[KEY_F17] = STELA_KEY_F17; + global.keycodes[KEY_F18] = STELA_KEY_F18; + global.keycodes[KEY_F19] = STELA_KEY_F19; + global.keycodes[KEY_F20] = STELA_KEY_F20; + global.keycodes[KEY_F21] = STELA_KEY_F21; + global.keycodes[KEY_F22] = STELA_KEY_F22; + global.keycodes[KEY_F23] = STELA_KEY_F23; + global.keycodes[KEY_F24] = STELA_KEY_F24; + + global.keycodes[KEY_KP0] = STELA_KEY_KP_0; + global.keycodes[KEY_KP1] = STELA_KEY_KP_1; + global.keycodes[KEY_KP2] = STELA_KEY_KP_2; + global.keycodes[KEY_KP3] = STELA_KEY_KP_3; + global.keycodes[KEY_KP4] = STELA_KEY_KP_4; + global.keycodes[KEY_KP5] = STELA_KEY_KP_5; + global.keycodes[KEY_KP6] = STELA_KEY_KP_6; + global.keycodes[KEY_KP7] = STELA_KEY_KP_7; + global.keycodes[KEY_KP8] = STELA_KEY_KP_8; + global.keycodes[KEY_KP9] = STELA_KEY_KP_9; + global.keycodes[KEY_KPPLUS] = STELA_KEY_KP_ADD; + global.keycodes[KEY_KPMINUS] = STELA_KEY_KP_SUBTRACT; + global.keycodes[KEY_KPASTERISK] = STELA_KEY_KP_MULTIPLY; + global.keycodes[KEY_KPSLASH] = STELA_KEY_KP_DIVIDE; + global.keycodes[KEY_KPDOT] = STELA_KEY_KP_DECIMAL; + global.keycodes[KEY_KPEQUAL] = STELA_KEY_KP_EQUAL; + global.keycodes[KEY_KPENTER] = STELA_KEY_KP_RETURN; + + global.keycodes[KEY_LEFTCTRL] = STELA_KEY_LEFT_CONTROL; + global.keycodes[KEY_LEFTALT] = STELA_KEY_LEFT_ALT; + global.keycodes[KEY_LEFTSHIFT] = STELA_KEY_LEFT_SHIFT; + global.keycodes[KEY_RIGHTCTRL] = STELA_KEY_RIGHT_CONTROL; + global.keycodes[KEY_RIGHTALT] = STELA_KEY_RIGHT_ALT; + global.keycodes[KEY_RIGHTSHIFT] = STELA_KEY_RIGHT_SHIFT; + global.keycodes[KEY_LEFTMETA] = STELA_KEY_LEFT_SUPER; + global.keycodes[KEY_RIGHTMETA] = STELA_KEY_RIGHT_SUPER; + global.keycodes[KEY_COMPOSE] = STELA_KEY_MENU; +} + s32 stl_global_init(s32 argc, str *argv, s32 (*main)(s32, str *), bool skip_graphics) { global.display = wl_display_connect(NULL); @@ -164,6 +294,8 @@ s32 stl_global_init(s32 argc, str *argv, s32 (*main)(s32, str *), bool skip_grap wl_display_roundtrip(global.display); wl_display_roundtrip(global.display); + set_keycodes(); + return main(argc, argv); } @@ -431,9 +563,9 @@ static void keyboard_key(void *data, struct wl_keyboard *keyboard, } if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { - stl_window_send_key_event(&wl->w, STELA_BUTTON_PRESSED, key); + stl_window_send_key_event(&wl->w, STELA_BUTTON_PRESSED, global.keycodes[key]); } else if (state == WL_KEYBOARD_KEY_STATE_RELEASED) { - stl_window_send_key_event(&wl->w, STELA_BUTTON_RELEASED, key); + stl_window_send_key_event(&wl->w, STELA_BUTTON_RELEASED, global.keycodes[key]); } } diff --git a/src/window_wayland.h b/src/window_wayland.h index 05208d8..1700082 100644 --- a/src/window_wayland.h +++ b/src/window_wayland.h @@ -13,14 +13,18 @@ #include <cursor-shape-v1-client-protocol.h> #include <wlr-layer-shell-unstable-v1-client-protocol.h> //#include <presentation-time-client-protocol.h> +#include <linux/input.h> #include "window.h" #include "egl_common.h" +#define STELA_WAYLAND_KEY_MAX (KEY_MEDIA + 1) + struct stl_wayland_global { struct wl_display *display; s32 display_fd; struct wl_registry *registry; + u16 keycodes[STELA_WAYLAND_KEY_MAX]; struct wl_surface *active_surface; array(struct stl_monitor *) monitors; struct stl_egl_global egl; diff --git a/src/window_win32.c b/src/window_win32.c index 5367c1a..c0713b1 100644 --- a/src/window_win32.c +++ b/src/window_win32.c @@ -1,8 +1,8 @@ +#include <al/log.h> + #include "window_win32.h" #include "common.h" - -// All extra Windows headers come after <nnwt/windows.h>. -#include <windowsx.h> +#include "keys.h" void stl_set_output_discovered_callback(void (*output_discovered)(void *, struct stl_monitor *), void *userdata) { @@ -78,10 +78,140 @@ static DWORD WINAPI MainThread(LPVOID Param) return 0; } +static void set_keycodes(void) +{ + al_memset(global.keycodes, 0, sizeof(global.keycodes)); + + global.keycodes[0x30] = STELA_KEY_0; + global.keycodes[0x31] = STELA_KEY_1; + global.keycodes[0x32] = STELA_KEY_2; + global.keycodes[0x33] = STELA_KEY_3; + global.keycodes[0x34] = STELA_KEY_4; + global.keycodes[0x35] = STELA_KEY_5; + global.keycodes[0x36] = STELA_KEY_6; + global.keycodes[0x37] = STELA_KEY_7; + global.keycodes[0x38] = STELA_KEY_8; + global.keycodes[0x39] = STELA_KEY_9; + global.keycodes[0x41] = STELA_KEY_A; + global.keycodes[0x42] = STELA_KEY_B; + global.keycodes[0x43] = STELA_KEY_C; + global.keycodes[0x44] = STELA_KEY_D; + global.keycodes[0x45] = STELA_KEY_E; + global.keycodes[0x46] = STELA_KEY_F; + global.keycodes[0x47] = STELA_KEY_G; + global.keycodes[0x48] = STELA_KEY_H; + global.keycodes[0x49] = STELA_KEY_I; + global.keycodes[0x4a] = STELA_KEY_J; + global.keycodes[0x4b] = STELA_KEY_K; + global.keycodes[0x4c] = STELA_KEY_L; + global.keycodes[0x4d] = STELA_KEY_M; + global.keycodes[0x4e] = STELA_KEY_N; + global.keycodes[0x4f] = STELA_KEY_O; + global.keycodes[0x50] = STELA_KEY_P; + global.keycodes[0x51] = STELA_KEY_Q; + global.keycodes[0x52] = STELA_KEY_R; + global.keycodes[0x53] = STELA_KEY_S; + global.keycodes[0x54] = STELA_KEY_T; + global.keycodes[0x55] = STELA_KEY_U; + global.keycodes[0x56] = STELA_KEY_V; + global.keycodes[0x57] = STELA_KEY_W; + global.keycodes[0x58] = STELA_KEY_X; + global.keycodes[0x59] = STELA_KEY_Y; + global.keycodes[0x5a] = STELA_KEY_Z; + + global.keycodes[VK_SPACE] = STELA_KEY_SPACE; + global.keycodes[VK_OEM_7] = STELA_KEY_APOSTROPHE; + global.keycodes[VK_OEM_COMMA] = STELA_KEY_COMMA; + global.keycodes[VK_OEM_PERIOD] = STELA_KEY_PERIOD; + global.keycodes[VK_OEM_2] = STELA_KEY_SLASH; + global.keycodes[VK_OEM_5] = STELA_KEY_BACKSLASH; + global.keycodes[VK_OEM_3] = STELA_KEY_GRAVE_ACCENT; + global.keycodes[VK_OEM_MINUS] = STELA_KEY_MINUS; + global.keycodes[VK_OEM_4] = STELA_KEY_LEFT_BRACKET; + global.keycodes[VK_OEM_6] = STELA_KEY_RIGHT_BRACKET; + global.keycodes[VK_OEM_1] = STELA_KEY_SEMICOLON; + global.keycodes[VK_OEM_PLUS] = STELA_KEY_EQUAL; + + global.keycodes[VK_RETURN] = STELA_KEY_RETURN; + global.keycodes[VK_BACK] = STELA_KEY_BACKSPACE; + global.keycodes[VK_TAB] = STELA_KEY_TAB; + global.keycodes[VK_ESCAPE] = STELA_KEY_ESCAPE; + global.keycodes[VK_INSERT] = STELA_KEY_INSERT; + global.keycodes[VK_DELETE] = STELA_KEY_DELETE; + global.keycodes[VK_HOME] = STELA_KEY_HOME; + global.keycodes[VK_END] = STELA_KEY_END; + global.keycodes[VK_PRIOR] = STELA_KEY_PAGE_UP; + global.keycodes[VK_NEXT] = STELA_KEY_PAGE_DOWN; + global.keycodes[VK_SNAPSHOT] = STELA_KEY_PRINT_SCREEN; + global.keycodes[VK_PAUSE] = STELA_KEY_PAUSE; + global.keycodes[VK_CAPITAL] = STELA_KEY_CAPS_LOCK; + global.keycodes[VK_SCROLL] = STELA_KEY_SCROLL_LOCK; + global.keycodes[VK_NUMLOCK] = STELA_KEY_NUM_LOCK; + + global.keycodes[VK_LEFT] = STELA_KEY_LEFT; + global.keycodes[VK_RIGHT] = STELA_KEY_RIGHT; + global.keycodes[VK_UP] = STELA_KEY_UP; + global.keycodes[VK_DOWN] = STELA_KEY_DOWN; + + global.keycodes[VK_F1] = STELA_KEY_F1; + global.keycodes[VK_F2] = STELA_KEY_F2; + global.keycodes[VK_F3] = STELA_KEY_F3; + global.keycodes[VK_F4] = STELA_KEY_F4; + global.keycodes[VK_F5] = STELA_KEY_F5; + global.keycodes[VK_F6] = STELA_KEY_F6; + global.keycodes[VK_F7] = STELA_KEY_F7; + global.keycodes[VK_F8] = STELA_KEY_F8; + global.keycodes[VK_F9] = STELA_KEY_F9; + global.keycodes[VK_F10] = STELA_KEY_F10; + global.keycodes[VK_F11] = STELA_KEY_F11; + global.keycodes[VK_F12] = STELA_KEY_F12; + global.keycodes[VK_F13] = STELA_KEY_F13; + global.keycodes[VK_F14] = STELA_KEY_F14; + global.keycodes[VK_F15] = STELA_KEY_F15; + global.keycodes[VK_F16] = STELA_KEY_F16; + global.keycodes[VK_F17] = STELA_KEY_F17; + global.keycodes[VK_F18] = STELA_KEY_F18; + global.keycodes[VK_F19] = STELA_KEY_F19; + global.keycodes[VK_F20] = STELA_KEY_F20; + global.keycodes[VK_F21] = STELA_KEY_F21; + global.keycodes[VK_F22] = STELA_KEY_F22; + global.keycodes[VK_F23] = STELA_KEY_F23; + global.keycodes[VK_F24] = STELA_KEY_F24; + + global.keycodes[VK_NUMPAD0] = STELA_KEY_KP_0; + global.keycodes[VK_NUMPAD1] = STELA_KEY_KP_1; + global.keycodes[VK_NUMPAD2] = STELA_KEY_KP_2; + global.keycodes[VK_NUMPAD3] = STELA_KEY_KP_3; + global.keycodes[VK_NUMPAD4] = STELA_KEY_KP_4; + global.keycodes[VK_NUMPAD5] = STELA_KEY_KP_5; + global.keycodes[VK_NUMPAD6] = STELA_KEY_KP_6; + global.keycodes[VK_NUMPAD7] = STELA_KEY_KP_7; + global.keycodes[VK_NUMPAD8] = STELA_KEY_KP_8; + global.keycodes[VK_NUMPAD9] = STELA_KEY_KP_9; + global.keycodes[VK_ADD] = STELA_KEY_KP_ADD; + global.keycodes[VK_SUBTRACT] = STELA_KEY_KP_SUBTRACT; + global.keycodes[VK_MULTIPLY] = STELA_KEY_KP_MULTIPLY; + global.keycodes[VK_DIVIDE] = STELA_KEY_KP_DIVIDE; + global.keycodes[VK_DECIMAL] = STELA_KEY_KP_DECIMAL; + global.keycodes[VK_OEM_PLUS] = STELA_KEY_KP_EQUAL; + global.keycodes[VK_RETURN] = STELA_KEY_KP_RETURN; + + global.keycodes[VK_CONTROL] = STELA_KEY_LEFT_CONTROL; + global.keycodes[VK_MENU] = STELA_KEY_LEFT_ALT; + global.keycodes[VK_SHIFT] = STELA_KEY_LEFT_SHIFT; + global.keycodes[VK_CONTROL] = STELA_KEY_RIGHT_CONTROL; + global.keycodes[VK_MENU] = STELA_KEY_RIGHT_ALT; + global.keycodes[VK_SHIFT] = STELA_KEY_RIGHT_SHIFT; + global.keycodes[VK_LWIN] = STELA_KEY_LEFT_SUPER; + global.keycodes[VK_RWIN] = STELA_KEY_RIGHT_SUPER; +} + s32 stl_global_init(s32 argc, str *argv, s32 (*main)(s32, str *), bool skip_graphics) { (void)skip_graphics; + set_keycodes(); + WNDCLASSEXW WindowClass = { 0 }; WindowClass.cbSize = sizeof(WindowClass); WindowClass.lpfnWndProc = &ServiceWndProc; @@ -145,12 +275,17 @@ static LRESULT CALLBACK DisplayWndProc(HWND Window, UINT Message, WPARAM WParam, case WM_MOUSEMOVE: case WM_MOUSEWHEEL: case WM_KEYDOWN: + case WM_SYSKEYDOWN: case WM_KEYUP: + case WM_SYSKEYUP: case WM_LBUTTONDOWN: case WM_LBUTTONUP: + case WM_SETFOCUS: case WM_SIZE: PostThreadMessageW(MainThreadID, Message, WParam, LParam); break; + case WM_MOUSEACTIVATE: + return MA_ACTIVATEANDEAT; default: Result = DefWindowProcW(Window, Message, WParam, LParam); break; @@ -179,6 +314,9 @@ static bool window_win32_create_window(struct stl_window *window, u32 width, u32 WindowClass.lpszClassName = L"Dangerous Class"; RegisterClassExW(&WindowClass); + RECT Rect = { 0, 0, width, height }; + AdjustWindowRect(&Rect, WS_OVERLAPPEDWINDOW, false); + struct the_baby Baby = { 0 }; Baby.dwExStyle = 0; Baby.lpClassName = WindowClass.lpszClassName; @@ -187,8 +325,8 @@ static bool window_win32_create_window(struct stl_window *window, u32 width, u32 Baby.dwStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE; Baby.X = CW_USEDEFAULT; Baby.Y = CW_USEDEFAULT; - Baby.nWidth = width; - Baby.nHeight = height; + Baby.nWidth = Rect.right - Rect.left; + Baby.nHeight = Rect.bottom - Rect.top; Baby.hInstance = WindowClass.hInstance; Baby.lpParam = w32; w32->Window = (HWND)SendMessageW(global.ServiceWindow, CREATE_DANGEROUS_WINDOW, (WPARAM)&Baby, 0); @@ -229,23 +367,36 @@ static void win32_handle_message(struct stl_window_win32 *w32, MSG *Message) stl_window_send_scroll_event(&w32->w, -(f64)GET_WHEEL_DELTA_WPARAM(Message->wParam)); break; case WM_KEYDOWN: - stl_window_send_key_event(&w32->w, STELA_BUTTON_PRESSED, Message->wParam); - break; + case WM_SYSKEYDOWN: case WM_KEYUP: - stl_window_send_key_event(&w32->w, STELA_BUTTON_RELEASED, Message->wParam); + case WM_SYSKEYUP: { + //s32 scancode = (HIWORD(Message->lParam) & (KF_EXTENDED | 0xff)); + /* https://github.com/glfw/glfw/blob/e7ea71be039836da3a98cea55ae5569cb5eb885c/src/win32_window.c#L717C11-L721C75 + if (!scancode) { + scancode = MapVirtualKeyW((UINT)Message->wParam, MAPVK_VK_TO_VSC); + } + */ + //al_printf("0x%x\n", Message->wParam); + u8 state = (Message->message == WM_KEYDOWN || Message->message == WM_SYSKEYDOWN) ? STELA_BUTTON_PRESSED : STELA_BUTTON_RELEASED; + stl_window_send_key_event(&w32->w, state, global.keycodes[Message->wParam]); break; + } case WM_LBUTTONDOWN: stl_window_send_mouse_button_event(&w32->w, STELA_BUTTON_PRESSED, STELA_MOUSE1); break; case WM_LBUTTONUP: stl_window_send_mouse_button_event(&w32->w, STELA_BUTTON_RELEASED, STELA_MOUSE1); break; + case WM_SETFOCUS: + break; case WM_SIZE: w32->w.width = LOWORD(Message->lParam); w32->w.height = HIWORD(Message->lParam); stl_window_send_resize_event(&w32->w, w32->w.width, w32->w.height); break; case WAKE_WINDOW: + w32->w.pending_refresh = true; + break; default: break; } @@ -324,6 +475,7 @@ static void window_win32_gl_swap_buffers(void *data) struct stl_window *stl_window_create(void *context) { (void)context; + al_log_info("stela", "Creating Win32 window."); struct stl_window_win32 *w32 = al_alloc_object(struct stl_window_win32); stl_window_common_init(&w32->w); w32->w.fullscreen = true; diff --git a/src/window_win32.h b/src/window_win32.h index 59de230..26ec5cc 100644 --- a/src/window_win32.h +++ b/src/window_win32.h @@ -2,11 +2,17 @@ #include <al/wstr.h> #include <nnwt/windows.h> +// All extra Windows headers come after <nnwt/windows.h>. +#include <windowsx.h> +#include <winuser.h> #include "window.h" +#define STELA_WIN32_KEY_MAX (VK_OEM_7 + 1) + struct stl_win32_global { HWND ServiceWindow; + u16 keycodes[STELA_WIN32_KEY_MAX]; s32 argc; str *argv; s32 (*main)(s32, str *); diff --git a/subprojects/libalabaster.wrap b/subprojects/libalabaster.wrap index 5977202..adaec6a 100644 --- a/subprojects/libalabaster.wrap +++ b/subprojects/libalabaster.wrap @@ -1,4 +1,4 @@ [wrap-git] url = https://git.akon.city/libalabaster -revision = 5f80381cc8d2875d27d14288b6f11c31a72fb3cf +revision = 31715673a5b98991ef276decbd90f026559eaf4b depth = 1 diff --git a/subprojects/libnaunet.wrap b/subprojects/libnaunet.wrap index 0fb98e4..a869e82 100644 --- a/subprojects/libnaunet.wrap +++ b/subprojects/libnaunet.wrap @@ -1,4 +1,4 @@ [wrap-git] url = https://git.akon.city/libnaunet -revision = c4023623b89d7518c92f675f44355f5ae1196264 +revision = ce102493f377d0452e44c3a4bb6e6e15ab0c819d depth = 1 |