1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#include "SDL_internal.h"
23
24#ifndef SDL_waylandevents_h_
25#define SDL_waylandevents_h_
26
27#include "../../events/SDL_mouse_c.h"
28#include "../../events/SDL_pen_c.h"
29
30#include "SDL_waylandvideo.h"
31#include "SDL_waylandwindow.h"
32#include "SDL_waylanddatamanager.h"
33#include "SDL_waylandkeyboard.h"
34
35enum SDL_WaylandAxisEvent
36{
37 AXIS_EVENT_CONTINUOUS = 0,
38 AXIS_EVENT_DISCRETE,
39 AXIS_EVENT_VALUE120
40};
41
42struct SDL_WaylandTabletSeat;
43
44typedef struct SDL_WaylandTabletInput
45{
46 struct SDL_WaylandInput *input;
47 struct zwp_tablet_seat_v2 *seat;
48} SDL_WaylandTabletInput;
49
50typedef struct
51{
52 int32_t repeat_rate; // Repeat rate in range of [1, 1000] character(s) per second
53 int32_t repeat_delay_ms; // Time to first repeat event in milliseconds
54 Uint32 keyboard_id; // ID of the source keyboard.
55 bool is_initialized;
56
57 bool is_key_down;
58 uint32_t key;
59 Uint64 wl_press_time_ns; // Key press time as reported by the Wayland API
60 Uint64 sdl_press_time_ns; // Key press time expressed in SDL ticks
61 Uint64 next_repeat_ns; // Next repeat event in nanoseconds
62 uint32_t scancode;
63 char text[8];
64} SDL_WaylandKeyboardRepeat;
65
66struct SDL_WaylandInput
67{
68 SDL_VideoData *display;
69 struct wl_seat *seat;
70 struct wl_pointer *pointer;
71 struct wl_touch *touch;
72 struct wl_keyboard *keyboard;
73 SDL_WaylandDataDevice *data_device;
74 SDL_WaylandPrimarySelectionDevice *primary_selection_device;
75 SDL_WaylandTextInput *text_input;
76 struct wp_cursor_shape_device_v1 *cursor_shape;
77 struct zwp_relative_pointer_v1 *relative_pointer;
78 struct zwp_input_timestamps_v1 *keyboard_timestamps;
79 struct zwp_input_timestamps_v1 *pointer_timestamps;
80 struct zwp_input_timestamps_v1 *touch_timestamps;
81 SDL_WindowData *pointer_focus;
82 SDL_WindowData *keyboard_focus;
83 SDL_CursorData *current_cursor;
84 SDL_KeyboardID keyboard_id;
85 SDL_MouseID pointer_id;
86 uint32_t pointer_enter_serial;
87
88 // High-resolution event timestamps
89 Uint64 keyboard_timestamp_ns;
90 Uint64 pointer_timestamp_ns;
91 Uint64 touch_timestamp_ns;
92
93 // Last motion location
94 wl_fixed_t sx_w;
95 wl_fixed_t sy_w;
96
97 SDL_MouseButtonFlags buttons_pressed;
98
99 // The serial of the last implicit grab event for window activation and selection data.
100 Uint32 last_implicit_grab_serial;
101
102 struct
103 {
104 struct xkb_keymap *keymap;
105 struct xkb_state *state;
106 struct xkb_compose_table *compose_table;
107 struct xkb_compose_state *compose_state;
108
109 // Keyboard layout "group"
110 uint32_t current_group;
111
112 // Modifier bitshift values
113 uint32_t idx_shift;
114 uint32_t idx_ctrl;
115 uint32_t idx_alt;
116 uint32_t idx_gui;
117 uint32_t idx_mod3;
118 uint32_t idx_mod5;
119 uint32_t idx_num;
120 uint32_t idx_caps;
121
122 // Current system modifier flags
123 uint32_t wl_pressed_modifiers;
124 uint32_t wl_locked_modifiers;
125 } xkb;
126
127 // information about axis events on current frame
128 struct
129 {
130 enum SDL_WaylandAxisEvent x_axis_type;
131 float x;
132
133 enum SDL_WaylandAxisEvent y_axis_type;
134 float y;
135
136 // Event timestamp in nanoseconds
137 Uint64 timestamp_ns;
138 SDL_MouseWheelDirection direction;
139 } pointer_curr_axis_info;
140
141 SDL_WaylandKeyboardRepeat keyboard_repeat;
142
143 SDL_WaylandTabletInput *tablet_input;
144
145 bool keyboard_is_virtual;
146
147 // Current SDL modifier flags
148 SDL_Keymod pressed_modifiers;
149 SDL_Keymod locked_modifiers;
150};
151
152
153extern Uint64 Wayland_GetTouchTimestamp(struct SDL_WaylandInput *input, Uint32 wl_timestamp_ms);
154
155extern void Wayland_PumpEvents(SDL_VideoDevice *_this);
156extern void Wayland_SendWakeupEvent(SDL_VideoDevice *_this, SDL_Window *window);
157extern int Wayland_WaitEventTimeout(SDL_VideoDevice *_this, Sint64 timeoutNS);
158
159extern void Wayland_create_data_device(SDL_VideoData *d);
160extern void Wayland_create_primary_selection_device(SDL_VideoData *d);
161
162extern void Wayland_create_text_input_manager(SDL_VideoData *d, uint32_t id);
163
164extern void Wayland_input_initialize_seat(SDL_VideoData *d);
165extern void Wayland_display_destroy_input(SDL_VideoData *d);
166
167extern void Wayland_input_init_relative_pointer(SDL_VideoData *d);
168extern bool Wayland_input_enable_relative_pointer(struct SDL_WaylandInput *input);
169extern bool Wayland_input_disable_relative_pointer(struct SDL_WaylandInput *input);
170
171extern bool Wayland_input_lock_pointer(struct SDL_WaylandInput *input, SDL_Window *window);
172extern bool Wayland_input_unlock_pointer(struct SDL_WaylandInput *input, SDL_Window *window);
173
174extern bool Wayland_input_confine_pointer(struct SDL_WaylandInput *input, SDL_Window *window);
175extern bool Wayland_input_unconfine_pointer(struct SDL_WaylandInput *input, SDL_Window *window);
176
177extern bool Wayland_input_grab_keyboard(SDL_Window *window, struct SDL_WaylandInput *input);
178extern bool Wayland_input_ungrab_keyboard(SDL_Window *window);
179
180extern void Wayland_input_init_tablet_support(struct SDL_WaylandInput *input, struct zwp_tablet_manager_v2 *tablet_manager);
181extern void Wayland_input_quit_tablet_support(struct SDL_WaylandInput *input);
182
183extern void Wayland_RegisterTimestampListeners(struct SDL_WaylandInput *input);
184extern void Wayland_CreateCursorShapeDevice(struct SDL_WaylandInput *input);
185
186/* The implicit grab serial needs to be updated on:
187 * - Keyboard key down/up
188 * - Mouse button down
189 * - Touch event down
190 * - Tablet tool down
191 * - Tablet tool button down/up
192 */
193extern void Wayland_UpdateImplicitGrabSerial(struct SDL_WaylandInput *input, Uint32 serial);
194
195#endif // SDL_waylandevents_h_
196