SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
input_wayland.c
Go to the documentation of this file.
1#include "linux_internal.h"
2#include <string.h>
3
5 switch (key) {
6 case 1: return SBGL_SCANCODE_ESCAPE;
7 case 28: return SBGL_SCANCODE_RETURN;
8 case 57: return SBGL_SCANCODE_SPACE;
9 case 15: return SBGL_SCANCODE_TAB;
10 case 14: return SBGL_SCANCODE_BACKSPACE;
11 case 103: return SBGL_SCANCODE_UP;
12 case 108: return SBGL_SCANCODE_DOWN;
13 case 105: return SBGL_SCANCODE_LEFT;
14 case 106: return SBGL_SCANCODE_RIGHT;
15 case 42: return SBGL_SCANCODE_LSHIFT;
16 case 29: return SBGL_SCANCODE_LCTRL;
17 case 56: return SBGL_SCANCODE_LALT;
18 case 2: return SBGL_SCANCODE_1;
19 case 3: return SBGL_SCANCODE_2;
20 case 4: return SBGL_SCANCODE_3;
21 case 5: return SBGL_SCANCODE_4;
22 case 6: return SBGL_SCANCODE_5;
23 case 7: return SBGL_SCANCODE_6;
24 case 8: return SBGL_SCANCODE_7;
25 case 9: return SBGL_SCANCODE_8;
26 case 10: return SBGL_SCANCODE_9;
27 case 11: return SBGL_SCANCODE_0;
28 case 12: return SBGL_SCANCODE_MINUS;
29 case 13: return SBGL_SCANCODE_EQUAL;
30 case 30: return SBGL_SCANCODE_A; case 48: return SBGL_SCANCODE_B; case 46: return SBGL_SCANCODE_C;
31 case 32: return SBGL_SCANCODE_D; case 18: return SBGL_SCANCODE_E; case 33: return SBGL_SCANCODE_F;
32 case 34: return SBGL_SCANCODE_G; case 35: return SBGL_SCANCODE_H; case 23: return SBGL_SCANCODE_I;
33 case 36: return SBGL_SCANCODE_J; case 37: return SBGL_SCANCODE_K; case 38: return SBGL_SCANCODE_L;
34 case 50: return SBGL_SCANCODE_M; case 49: return SBGL_SCANCODE_N; case 24: return SBGL_SCANCODE_O;
35 case 25: return SBGL_SCANCODE_P; case 16: return SBGL_SCANCODE_Q; case 19: return SBGL_SCANCODE_R;
36 case 31: return SBGL_SCANCODE_S; case 20: return SBGL_SCANCODE_T; case 22: return SBGL_SCANCODE_U;
37 case 47: return SBGL_SCANCODE_V; case 17: return SBGL_SCANCODE_W; case 45: return SBGL_SCANCODE_X;
38 case 21: return SBGL_SCANCODE_Y; case 44: return SBGL_SCANCODE_Z;
39 default: return SBGL_SCANCODE_UNKNOWN;
40 }
41}
42
43// --- Pointer Listeners ---
44static void pointer_enter(void* data, struct wl_pointer* p, uint32_t s, struct wl_surface* surf, wl_fixed_t sx, wl_fixed_t sy) {
45 (void)p; (void)s; (void)surf;
46 sbgl_InputState* input = ((struct sbgl_Window*)data)->input;
47 input->mouseX = wl_fixed_to_int(sx);
48 input->mouseY = wl_fixed_to_int(sy);
49}
50static void pointer_leave(void* data, struct wl_pointer* p, uint32_t s, struct wl_surface* surf) { (void)data; (void)p; (void)s; (void)surf; }
51static void pointer_motion(void* data, struct wl_pointer* p, uint32_t t, wl_fixed_t sx, wl_fixed_t sy) {
52 (void)p; (void)t;
53 sbgl_InputState* input = ((struct sbgl_Window*)data)->input;
54 input->mouseX = wl_fixed_to_int(sx);
55 input->mouseY = wl_fixed_to_int(sy);
56}
57static void pointer_button(void* data, struct wl_pointer* p, uint32_t s, uint32_t t, uint32_t button, uint32_t state) {
58 (void)p; (void)s; (void)t;
59 sbgl_InputState* input = ((struct sbgl_Window*)data)->input;
60 int btn = -1;
61 if (button == 0x110) btn = SBGL_MOUSE_BUTTON_LEFT;
62 else if (button == 0x111) btn = SBGL_MOUSE_BUTTON_RIGHT;
63 else if (button == 0x112) btn = SBGL_MOUSE_BUTTON_MIDDLE;
64 if (btn != -1 && btn < SBGL_MOUSE_BUTTON_MAX) {
65 input->mouseDown[btn] = (state == WL_POINTER_BUTTON_STATE_PRESSED);
66 }
67}
68static void pointer_axis(void* data, struct wl_pointer* p, uint32_t t, uint32_t axis, wl_fixed_t value) { (void)data; (void)p; (void)t; (void)axis; (void)value; }
69static void pointer_frame(void* data, struct wl_pointer* p) { (void)data; (void)p; }
70static void pointer_axis_source(void* data, struct wl_pointer* p, uint32_t src) { (void)data; (void)p; (void)src; }
71static void pointer_axis_stop(void* data, struct wl_pointer* p, uint32_t t, uint32_t axis) { (void)data; (void)p; (void)t; (void)axis; }
72static void pointer_axis_discrete(void* data, struct wl_pointer* p, uint32_t axis, int32_t discrete) { (void)data; (void)p; (void)axis; (void)discrete; }
73
74const struct wl_pointer_listener pointer_listener = {
75 .enter = pointer_enter, .leave = pointer_leave, .motion = pointer_motion, .button = pointer_button,
76 .axis = pointer_axis, .frame = pointer_frame, .axis_source = pointer_axis_source,
77 .axis_stop = pointer_axis_stop, .axis_discrete = pointer_axis_discrete
78};
79
80// --- Keyboard Listeners ---
81static void keyboard_keymap(void* data, struct wl_keyboard* k, uint32_t format, int32_t fd, uint32_t size) { (void)data; (void)k; (void)format; (void)fd; (void)size; }
82static void keyboard_enter(void* data, struct wl_keyboard* k, uint32_t s, struct wl_surface* surf, struct wl_array* keys) {
83 (void)k; (void)s; (void)surf; (void)keys;
84 ((sbgl_Window*)data)->focused = true;
85}
86static void keyboard_leave(void* data, struct wl_keyboard* k, uint32_t s, struct wl_surface* surf) {
87 (void)k; (void)s; (void)surf;
88 ((sbgl_Window*)data)->focused = false;
89}
90static void keyboard_key(void* data, struct wl_keyboard* k, uint32_t s, uint32_t t, uint32_t key, uint32_t state) {
91 (void)k; (void)s; (void)t;
92 sbgl_InputState* input = ((struct sbgl_Window*)data)->input;
94 if (code < SBGL_SCANCODE_MAX) {
95 bool down = (state == WL_KEYBOARD_KEY_STATE_PRESSED);
96 if (down && !input->keysDown[code]) {
97 input->keysPressed[code] = true;
98 }
99 input->keysDown[code] = down;
100 }
101}
102static void keyboard_modifiers(void* data, struct wl_keyboard* k, uint32_t s, uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group) { (void)data; (void)k; (void)s; (void)depressed; (void)latched; (void)locked; (void)group; }
103static void keyboard_repeat_info(void* data, struct wl_keyboard* k, int32_t rate, int32_t delay) { (void)data; (void)k; (void)rate; (void)delay; }
104
105const struct wl_keyboard_listener keyboard_listener = {
106 .keymap = keyboard_keymap, .enter = keyboard_enter, .leave = keyboard_leave,
107 .key = keyboard_key, .modifiers = keyboard_modifiers, .repeat_info = keyboard_repeat_info
108};
109
110// --- Relative Pointer Listener ---
111static void relative_motion(void* data, struct zwp_relative_pointer_v1* p, uint32_t hi, uint32_t lo, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t dx_unaccel, wl_fixed_t dy_unaccel) {
112 (void)p; (void)hi; (void)lo; (void)dx_unaccel; (void)dy_unaccel;
113 sbgl_Window* window = (sbgl_Window*)data;
114 window->input->mouseDeltaX += wl_fixed_to_int(dx);
115 window->input->mouseDeltaY += wl_fixed_to_int(dy);
116}
117
118const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
119 .relative_motion = relative_motion
120};
121
122void linux_init_input(struct wl_registry* registry, uint32_t name, uint32_t version, sbgl_Window* window) {
123 window->seat = wl_registry_bind(registry, name, &wl_seat_interface, version);
124}
125
126// --- HAL Implementation ---
128 sbgl_InputState* input = window->input;
129 if (!input) return;
130
131 if (!window->relative_pointer) {
132 input->mouseDeltaX = input->mouseX - input->_internalMouseX;
133 input->mouseDeltaY = input->mouseY - input->_internalMouseY;
134 }
135 // Note: If relative_pointer is active, mouseDeltaX/Y were accumulated in relative_motion.
136 // We do NOT reset them here, as they should be reset at the start of PollEvents
137 // or we should handle the accumulation window carefully.
138 // For now, assume PollEvents or the next frame will clear them if needed,
139 // but actually sbgl_InputState usually expects deltas to be "since last frame".
140
141 input->_internalMouseX = input->mouseX;
142 input->_internalMouseY = input->mouseY;
143}
SBGL_Scancode
OS-independent physical scancodes.
Definition sbgl_input.h:21
@ SBGL_SCANCODE_V
Definition sbgl_input.h:44
@ SBGL_SCANCODE_M
Definition sbgl_input.h:35
@ SBGL_SCANCODE_UNKNOWN
Definition sbgl_input.h:22
@ SBGL_SCANCODE_0
Definition sbgl_input.h:59
@ SBGL_SCANCODE_K
Definition sbgl_input.h:33
@ SBGL_SCANCODE_SPACE
Definition sbgl_input.h:65
@ SBGL_SCANCODE_RIGHT
Definition sbgl_input.h:70
@ SBGL_SCANCODE_9
Definition sbgl_input.h:58
@ SBGL_SCANCODE_C
Definition sbgl_input.h:25
@ SBGL_SCANCODE_2
Definition sbgl_input.h:51
@ SBGL_SCANCODE_8
Definition sbgl_input.h:57
@ SBGL_SCANCODE_A
Definition sbgl_input.h:23
@ SBGL_SCANCODE_Y
Definition sbgl_input.h:47
@ SBGL_SCANCODE_LALT
Definition sbgl_input.h:77
@ SBGL_SCANCODE_LEFT
Definition sbgl_input.h:71
@ SBGL_SCANCODE_O
Definition sbgl_input.h:37
@ SBGL_SCANCODE_R
Definition sbgl_input.h:40
@ SBGL_SCANCODE_4
Definition sbgl_input.h:53
@ SBGL_SCANCODE_Z
Definition sbgl_input.h:48
@ SBGL_SCANCODE_H
Definition sbgl_input.h:30
@ SBGL_SCANCODE_5
Definition sbgl_input.h:54
@ SBGL_SCANCODE_J
Definition sbgl_input.h:32
@ SBGL_SCANCODE_LSHIFT
Definition sbgl_input.h:75
@ SBGL_SCANCODE_ESCAPE
Definition sbgl_input.h:62
@ SBGL_SCANCODE_X
Definition sbgl_input.h:46
@ SBGL_SCANCODE_LCTRL
Definition sbgl_input.h:76
@ SBGL_SCANCODE_UP
Definition sbgl_input.h:73
@ SBGL_SCANCODE_B
Definition sbgl_input.h:24
@ SBGL_SCANCODE_DOWN
Definition sbgl_input.h:72
@ SBGL_SCANCODE_G
Definition sbgl_input.h:29
@ SBGL_SCANCODE_TAB
Definition sbgl_input.h:64
@ SBGL_SCANCODE_7
Definition sbgl_input.h:56
@ SBGL_SCANCODE_F
Definition sbgl_input.h:28
@ SBGL_SCANCODE_MINUS
Definition sbgl_input.h:67
@ SBGL_SCANCODE_RETURN
Definition sbgl_input.h:61
@ SBGL_SCANCODE_E
Definition sbgl_input.h:27
@ SBGL_SCANCODE_T
Definition sbgl_input.h:42
@ SBGL_SCANCODE_3
Definition sbgl_input.h:52
@ SBGL_SCANCODE_D
Definition sbgl_input.h:26
@ SBGL_SCANCODE_1
Definition sbgl_input.h:50
@ SBGL_SCANCODE_MAX
Definition sbgl_input.h:137
@ SBGL_SCANCODE_P
Definition sbgl_input.h:38
@ SBGL_SCANCODE_Q
Definition sbgl_input.h:39
@ SBGL_SCANCODE_BACKSPACE
Definition sbgl_input.h:63
@ SBGL_SCANCODE_L
Definition sbgl_input.h:34
@ SBGL_SCANCODE_6
Definition sbgl_input.h:55
@ SBGL_SCANCODE_S
Definition sbgl_input.h:41
@ SBGL_SCANCODE_U
Definition sbgl_input.h:43
@ SBGL_SCANCODE_N
Definition sbgl_input.h:36
@ SBGL_SCANCODE_EQUAL
Definition sbgl_input.h:68
@ SBGL_SCANCODE_I
Definition sbgl_input.h:31
@ SBGL_SCANCODE_W
Definition sbgl_input.h:45
@ SBGL_MOUSE_BUTTON_MIDDLE
Definition sbgl_input.h:146
@ SBGL_MOUSE_BUTTON_MAX
Definition sbgl_input.h:147
@ SBGL_MOUSE_BUTTON_LEFT
Definition sbgl_input.h:144
@ SBGL_MOUSE_BUTTON_RIGHT
Definition sbgl_input.h:145
static void keyboard_repeat_info(void *data, struct wl_keyboard *k, int32_t rate, int32_t delay)
void linux_init_input(struct wl_registry *registry, uint32_t name, uint32_t version, sbgl_Window *window)
static void pointer_motion(void *data, struct wl_pointer *p, uint32_t t, wl_fixed_t sx, wl_fixed_t sy)
static void pointer_button(void *data, struct wl_pointer *p, uint32_t s, uint32_t t, uint32_t button, uint32_t state)
static void keyboard_leave(void *data, struct wl_keyboard *k, uint32_t s, struct wl_surface *surf)
static void relative_motion(void *data, struct zwp_relative_pointer_v1 *p, uint32_t hi, uint32_t lo, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t dx_unaccel, wl_fixed_t dy_unaccel)
const struct zwp_relative_pointer_v1_listener relative_pointer_listener
static void keyboard_keymap(void *data, struct wl_keyboard *k, uint32_t format, int32_t fd, uint32_t size)
void linux_internal_update_input_states(sbgl_Window *window)
static void pointer_axis_stop(void *data, struct wl_pointer *p, uint32_t t, uint32_t axis)
static void pointer_leave(void *data, struct wl_pointer *p, uint32_t s, struct wl_surface *surf)
const struct wl_pointer_listener pointer_listener
static void pointer_enter(void *data, struct wl_pointer *p, uint32_t s, struct wl_surface *surf, wl_fixed_t sx, wl_fixed_t sy)
static void pointer_axis_source(void *data, struct wl_pointer *p, uint32_t src)
static SBGL_Scancode wayland_key_to_scancode(uint32_t key)
static void keyboard_key(void *data, struct wl_keyboard *k, uint32_t s, uint32_t t, uint32_t key, uint32_t state)
static void pointer_frame(void *data, struct wl_pointer *p)
static void keyboard_modifiers(void *data, struct wl_keyboard *k, uint32_t s, uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group)
static void pointer_axis_discrete(void *data, struct wl_pointer *p, uint32_t axis, int32_t discrete)
static void pointer_axis(void *data, struct wl_pointer *p, uint32_t t, uint32_t axis, wl_fixed_t value)
const struct wl_keyboard_listener keyboard_listener
static void keyboard_enter(void *data, struct wl_keyboard *k, uint32_t s, struct wl_surface *surf, struct wl_array *keys)
Represents the real-time state of physical inputs.
Definition sbgl_input.h:165
bool mouseDown[SBGL_MOUSE_BUTTON_MAX]
Definition sbgl_input.h:168
bool keysPressed[SBGL_SCANCODE_MAX]
Definition sbgl_input.h:167
bool keysDown[SBGL_SCANCODE_MAX]
Definition sbgl_input.h:166
Native X11 window state.
sbgl_InputState * input