From b8ebf31762cc62b8509fb1a398347c47cafc77fc Mon Sep 17 00:00:00 2001 From: Roman Pytkov Date: Mon, 17 Nov 2025 13:41:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A4=D0=B0=D0=B9=D0=BB=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B2=D0=B2=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wayland/include/input-handle.h | 10 +++++ wayland/include/input.h | 16 ++++++- .../{wayland_runtime.h => wayland-runtime.h} | 3 ++ wayland/src/asm.asm | 2 - wayland/src/input-handle.c | 16 +++++++ wayland/src/input.c | 44 +++++++------------ wayland/src/registry.c | 1 + wayland/src/{c.c => wayland-runtime.c} | 10 ++++- 8 files changed, 69 insertions(+), 33 deletions(-) create mode 100644 wayland/include/input-handle.h rename wayland/include/{wayland_runtime.h => wayland-runtime.h} (85%) create mode 100644 wayland/src/input-handle.c rename wayland/src/{c.c => wayland-runtime.c} (94%) diff --git a/wayland/include/input-handle.h b/wayland/include/input-handle.h new file mode 100644 index 0000000..5c28a6d --- /dev/null +++ b/wayland/include/input-handle.h @@ -0,0 +1,10 @@ +#ifndef INPUT_HANDLE_H +#define INPUT_HANDLE_H + +#include +#include "window.h" +#include "input.h" + +void keyboard_key_handle(xkb_keycode_t kc, xkb_keysym_t ks, enum keyboard_key_state state, struct wayland_window* window); + +#endif diff --git a/wayland/include/input.h b/wayland/include/input.h index 6a56bff..fd87a5e 100644 --- a/wayland/include/input.h +++ b/wayland/include/input.h @@ -8,7 +8,19 @@ void input_register_seat(struct wl_seat *seat); /* Очистка input (разрушить keyboard и xkb state) */ void input_cleanup(void); -/* Возвращает wl_surface, имеющую фокус клавиатуры или NULL */ -struct wl_surface *input_get_keyboard_focus(void); +enum keyboard_key_state { + /** + * key is not pressed + */ + KEYBOARD_KEY_STATE_RELEASED = 0, + /** + * key is pressed + */ + KEYBOARD_KEY_STATE_PRESSED = 1, + /** + * key was repeated + */ + KEYBOARD_KEY_STATE_REPEATED = 2, +}; #endif diff --git a/wayland/include/wayland_runtime.h b/wayland/include/wayland-runtime.h similarity index 85% rename from wayland/include/wayland_runtime.h rename to wayland/include/wayland-runtime.h index 51f1b6f..58661b3 100644 --- a/wayland/include/wayland_runtime.h +++ b/wayland/include/wayland-runtime.h @@ -15,4 +15,7 @@ void wait_for_windows(void); /* Остановить оконные потоки, очистить input и закрыть соединение Wayland */ void destroy_wayland(void); +/* Поиск окна по wl_surface */ +struct wayland_window* get_window_by_surface(struct wl_surface* surf); + #endif diff --git a/wayland/src/asm.asm b/wayland/src/asm.asm index 4e5fe27..1df5af5 100644 --- a/wayland/src/asm.asm +++ b/wayland/src/asm.asm @@ -14,8 +14,6 @@ main: ; Launch the first window thread (duplicate calls for more windows) call run_window - call run_window - call run_window call wait_for_windows diff --git a/wayland/src/input-handle.c b/wayland/src/input-handle.c new file mode 100644 index 0000000..a774eb9 --- /dev/null +++ b/wayland/src/input-handle.c @@ -0,0 +1,16 @@ +#include "input-handle.h" + +void keyboard_key_handle(xkb_keycode_t kc, xkb_keysym_t ks, enum keyboard_key_state state, struct wayland_window *window) +{ + if (ks != XKB_KEY_NoSymbol) + { + char buf[64]; + int n = xkb_keysym_to_utf8(ks, buf, sizeof(buf)); + if (ks == XKB_KEY_Return) + sprintf(buf, "Return"); + if (n > 0) + printf("keyboard: symbol '%s' (keysym 0x%x) on surface:%p\n", buf, ks, window); + else + printf("keyboard: keysym 0x%x (no UTF-8 representation)\n", ks); + } +} \ No newline at end of file diff --git a/wayland/src/input.c b/wayland/src/input.c index 2bb305f..216c5ca 100644 --- a/wayland/src/input.c +++ b/wayland/src/input.c @@ -1,4 +1,3 @@ -#include "input.h" #include #include #include @@ -7,12 +6,17 @@ #include #include +#include "input.h" +#include "input-handle.h" +#include "wayland-runtime.h" +#include "window.h" + static struct wl_seat *seat = NULL; static struct wl_keyboard *keyboard = NULL; static struct xkb_context *xkb_ctx = NULL; static struct xkb_keymap *xkb_keymap = NULL; static struct xkb_state *xkb_state = NULL; -static struct wl_surface *keyboard_focus = NULL; +static struct wayland_window* focused_window = NULL; /* Обработчики клавиатуры */ static void keyboard_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) @@ -68,40 +72,29 @@ static void keyboard_enter(void *data, struct wl_keyboard *keyboard, uint32_t se { printf("keyboard: enter serial=%u surface=%p\n", serial, surface); /* Сохраняем поверхность, которая получила фокус клавиатуры */ - keyboard_focus = surface; + focused_window = get_window_by_surface(surface); } static void keyboard_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) { printf("keyboard: leave serial=%u surface=%p\n", serial, surface); /* Если уходим с фокусной поверхности — сбросить фокус */ - if (keyboard_focus == surface) - keyboard_focus = NULL; + if (focused_window && focused_window->wl_surface == surface) + focused_window = NULL; } static void keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) { - const char *s = (state == WL_KEYBOARD_KEY_STATE_PRESSED) ? "PRESSED" - : (state == WL_KEYBOARD_KEY_STATE_RELEASED) ? "RELEASED" - : (state == WL_KEYBOARD_KEY_STATE_REPEATED) ? "REPEATED" - : "Unknown"; - /* Отладочный вывод удалён */ + enum keyboard_key_state key_state = (state == WL_KEYBOARD_KEY_STATE_PRESSED) ? KEYBOARD_KEY_STATE_PRESSED + : (state == WL_KEYBOARD_KEY_STATE_REPEATED) ? KEYBOARD_KEY_STATE_REPEATED + : KEYBOARD_KEY_STATE_RELEASED; - if (xkb_state && keyboard_focus) + if (xkb_state && focused_window) { xkb_keycode_t kc = (xkb_keycode_t)key + 8; xkb_keysym_t ks = xkb_state_key_get_one_sym(xkb_state, kc); - if (ks != XKB_KEY_NoSymbol) - { - char buf[64]; - int n = xkb_keysym_to_utf8(ks, buf, sizeof(buf)); - if (ks == XKB_KEY_Return) - sprintf(buf, "Return"); - if (n > 0) - printf("keyboard: symbol '%s' (keysym 0x%x) on surface:%p\n", buf, ks, keyboard_focus); - else - printf("keyboard: keysym 0x%x (no UTF-8 representation)\n", ks); - } + + keyboard_key_handle(kc, ks, key_state, focused_window); } } @@ -207,10 +200,5 @@ void input_cleanup(void) xkb_context_unref(xkb_ctx); xkb_ctx = NULL; } - keyboard_focus = NULL; -} - -struct wl_surface *input_get_keyboard_focus(void) -{ - return keyboard_focus; + focused_window = NULL; } diff --git a/wayland/src/registry.c b/wayland/src/registry.c index d9f847d..3bfd0d1 100644 --- a/wayland/src/registry.c +++ b/wayland/src/registry.c @@ -218,3 +218,4 @@ struct xdg_wm_base *registry_get_xdg_wm_base(void) { return global_wm_base; } + diff --git a/wayland/src/c.c b/wayland/src/wayland-runtime.c similarity index 94% rename from wayland/src/c.c rename to wayland/src/wayland-runtime.c index fee66e2..ef6e2ff 100644 --- a/wayland/src/c.c +++ b/wayland/src/wayland-runtime.c @@ -8,7 +8,7 @@ #include "input.h" #include "registry.h" -#include "wayland_runtime.h" +#include "wayland-runtime.h" #include "window.h" #define MAX_WINDOW_THREADS 128 @@ -177,3 +177,11 @@ void destroy_wayland(void) g_initialized = 0; atomic_store(&g_shutdown, 0); } + +struct wayland_window* get_window_by_surface(struct wl_surface* surf) +{ + for(int i = 0; i < MAX_WINDOW_THREADS; i++) + if(g_slots[i].window.wl_surface == surf) + return &g_slots[i].window; + return NULL; +}