Файл для обработки ввода

This commit is contained in:
2025-11-17 13:41:46 +03:00
parent bc5bf92fee
commit b8ebf31762
8 changed files with 69 additions and 33 deletions

View File

@@ -0,0 +1,10 @@
#ifndef INPUT_HANDLE_H
#define INPUT_HANDLE_H
#include <xkbcommon/xkbcommon.h>
#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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -1,4 +1,3 @@
#include "input.h"
#include <stdio.h>
#include <xkbcommon/xkbcommon.h>
#include <wayland-client.h>
@@ -7,12 +6,17 @@
#include <fcntl.h>
#include <string.h>
#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;
}

View File

@@ -218,3 +218,4 @@ struct xdg_wm_base *registry_get_xdg_wm_base(void)
{
return global_wm_base;
}

View File

@@ -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;
}