Skip to content

Latest commit

 

History

History
313 lines (260 loc) · 9.75 KB

SDL_mirevents.c

File metadata and controls

313 lines (260 loc) · 9.75 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/*
Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_MIR
#include "../../events/SDL_events_c.h"
#include "../../events/SDL_keyboard_c.h"
#include "../../events/SDL_touch_c.h"
#include "../../events/scancodes_xfree86.h"
#include "SDL_mirevents.h"
#include "SDL_mirwindow.h"
#include <xkbcommon/xkbcommon.h>
#include "SDL_mirdyn.h"
static void
HandleKeyText(int32_t key_code)
{
char text[8];
int size = 0;
size = MIR_xkb_keysym_to_utf8(key_code, text, sizeof text);
if (size > 0) {
text[size] = '\0';
SDL_SendKeyboardText(text);
}
}
/* FIXME
Mir still needs to implement its IM API, for now we assume
a single key press produces a character.
*/
static void
Feb 21, 2016
Feb 21, 2016
61
HandleKeyEvent(MirKeyboardEvent const* key_event, SDL_Window* window)
Feb 21, 2016
Feb 21, 2016
63
64
65
66
67
68
69
70
71
72
73
74
75
xkb_keysym_t key_code;
Uint8 key_state;
int event_scancode;
uint32_t sdl_scancode = SDL_SCANCODE_UNKNOWN;
MirKeyboardAction action = MIR_mir_keyboard_event_action(key_event);
key_state = SDL_PRESSED;
key_code = MIR_mir_keyboard_event_key_code(key_event);
event_scancode = MIR_mir_keyboard_event_scan_code(key_event);
if (action == mir_keyboard_action_up)
key_state = SDL_RELEASED;
Feb 21, 2016
Feb 21, 2016
77
78
if (event_scancode < SDL_arraysize(xfree86_scancode_table2))
sdl_scancode = xfree86_scancode_table2[event_scancode];
Feb 21, 2016
Feb 21, 2016
80
81
if (sdl_scancode != SDL_SCANCODE_UNKNOWN)
SDL_SendKeyboardKey(key_state, sdl_scancode);
82
83
if (key_state == SDL_PRESSED)
Feb 21, 2016
Feb 21, 2016
84
HandleKeyText(key_code);
Feb 21, 2016
Feb 21, 2016
88
HandleMouseButton(SDL_Window* sdl_window, Uint8 state, MirPointerEvent const* pointer)
Feb 21, 2016
Feb 21, 2016
90
91
92
93
94
95
96
97
uint32_t sdl_button = SDL_BUTTON_LEFT;
MirPointerButton button_state = mir_pointer_button_primary;
static uint32_t old_button_states = 0;
uint32_t new_button_states = MIR_mir_pointer_event_buttons(pointer);
// XOR on our old button states vs our new states to get the newley pressed/released button
button_state = new_button_states ^ old_button_states;
98
99
switch (button_state) {
Feb 21, 2016
Feb 21, 2016
100
case mir_pointer_button_primary:
101
102
sdl_button = SDL_BUTTON_LEFT;
break;
Feb 21, 2016
Feb 21, 2016
103
case mir_pointer_button_secondary:
104
105
sdl_button = SDL_BUTTON_RIGHT;
break;
Feb 21, 2016
Feb 21, 2016
106
case mir_pointer_button_tertiary:
107
108
sdl_button = SDL_BUTTON_MIDDLE;
break;
Feb 21, 2016
Feb 21, 2016
109
case mir_pointer_button_forward:
110
111
sdl_button = SDL_BUTTON_X1;
break;
Feb 21, 2016
Feb 21, 2016
112
case mir_pointer_button_back:
113
114
115
116
117
118
sdl_button = SDL_BUTTON_X2;
break;
default:
break;
}
Feb 21, 2016
Feb 21, 2016
119
120
old_button_states = new_button_states;
121
122
123
124
125
126
SDL_SendMouseButton(sdl_window, 0, state, sdl_button);
}
static void
HandleMouseMotion(SDL_Window* sdl_window, int x, int y)
{
Jun 7, 2016
Jun 7, 2016
127
128
SDL_Mouse* mouse = SDL_GetMouse();
SDL_SendMouseMotion(sdl_window, 0, mouse->relative_mode, x, y);
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
}
static void
HandleTouchPress(int device_id, int source_id, SDL_bool down, float x, float y, float pressure)
{
SDL_SendTouch(device_id, source_id, down, x, y, pressure);
}
static void
HandleTouchMotion(int device_id, int source_id, float x, float y, float pressure)
{
SDL_SendTouchMotion(device_id, source_id, x, y, pressure);
}
static void
HandleMouseScroll(SDL_Window* sdl_window, int hscroll, int vscroll)
{
SDL_SendMouseWheel(sdl_window, 0, hscroll, vscroll, SDL_MOUSEWHEEL_NORMAL);
}
static void
AddTouchDevice(int device_id)
{
if (SDL_AddTouch(device_id, "") < 0)
SDL_SetError("Error: can't add touch %s, %d", __FILE__, __LINE__);
}
static void
Feb 21, 2016
Feb 21, 2016
157
HandleTouchEvent(MirTouchEvent const* touch, int device_id, SDL_Window* sdl_window)
Feb 21, 2016
Feb 21, 2016
159
160
int i, point_count;
point_count = MIR_mir_touch_event_point_count(touch);
Feb 21, 2016
Feb 21, 2016
162
AddTouchDevice(device_id);
Feb 21, 2016
Feb 21, 2016
164
165
for (i = 0; i < point_count; i++) {
int id = MIR_mir_touch_event_id(touch, i);
Feb 21, 2016
Feb 21, 2016
167
168
int width = sdl_window->w;
int height = sdl_window->h;
Feb 21, 2016
Feb 21, 2016
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
float x = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_x);
float y = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_y);
float n_x = x / width;
float n_y = y / height;
float pressure = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_pressure);
switch (MIR_mir_touch_event_action(touch, i)) {
case mir_touch_action_up:
HandleTouchPress(device_id, id, SDL_FALSE, n_x, n_y, pressure);
break;
case mir_touch_action_down:
HandleTouchPress(device_id, id, SDL_TRUE, n_x, n_y, pressure);
break;
case mir_touch_action_change:
HandleTouchMotion(device_id, id, n_x, n_y, pressure);
break;
Sep 21, 2016
Sep 21, 2016
188
189
case mir_touch_actions:
break;
Feb 21, 2016
Feb 21, 2016
190
}
191
192
193
194
}
}
static void
Feb 21, 2016
Feb 21, 2016
195
HandleMouseEvent(MirPointerEvent const* pointer, SDL_Window* sdl_window)
196
197
198
{
SDL_SetMouseFocus(sdl_window);
Feb 21, 2016
Feb 21, 2016
199
200
201
switch (MIR_mir_pointer_event_action(pointer)) {
case mir_pointer_action_button_down:
HandleMouseButton(sdl_window, SDL_PRESSED, pointer);
Feb 21, 2016
Feb 21, 2016
203
204
case mir_pointer_action_button_up:
HandleMouseButton(sdl_window, SDL_RELEASED, pointer);
Feb 21, 2016
Feb 21, 2016
206
207
208
209
210
211
case mir_pointer_action_motion: {
int x, y;
int hscroll, vscroll;
SDL_Mouse* mouse = SDL_GetMouse();
x = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_x);
y = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_y);
Jun 7, 2016
Jun 7, 2016
212
Aug 30, 2016
Aug 30, 2016
213
if (mouse) {
Jun 7, 2016
Jun 7, 2016
214
215
216
217
218
if (mouse->relative_mode) {
int relative_x = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_relative_x);
int relative_y = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_relative_y);
HandleMouseMotion(sdl_window, relative_x, relative_y);
}
Aug 30, 2016
Aug 30, 2016
219
else if (mouse->x != x || mouse->y != y) {
Jun 7, 2016
Jun 7, 2016
220
221
222
223
HandleMouseMotion(sdl_window, x, y);
}
}
Feb 21, 2016
Feb 21, 2016
224
225
226
227
228
hscroll = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_hscroll);
vscroll = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_vscroll);
if (vscroll != 0 || hscroll != 0)
HandleMouseScroll(sdl_window, hscroll, vscroll);
}
Feb 21, 2016
Feb 21, 2016
230
case mir_pointer_action_leave:
231
232
SDL_SetMouseFocus(NULL);
break;
Feb 21, 2016
Feb 21, 2016
233
234
235
236
237
238
239
case mir_pointer_action_enter:
default:
break;
}
}
static void
Mar 1, 2017
Mar 1, 2017
240
HandleInput(MirInputEvent const* input_event, SDL_Window* window)
Feb 21, 2016
Feb 21, 2016
241
242
243
244
245
246
247
{
switch (MIR_mir_input_event_get_type(input_event)) {
case (mir_input_event_type_key):
HandleKeyEvent(MIR_mir_input_event_get_keyboard_event(input_event), window);
break;
case (mir_input_event_type_pointer):
HandleMouseEvent(MIR_mir_input_event_get_pointer_event(input_event), window);
Feb 21, 2016
Feb 21, 2016
249
250
251
252
case (mir_input_event_type_touch):
HandleTouchEvent(MIR_mir_input_event_get_touch_event(input_event),
MIR_mir_input_event_get_device_id(input_event),
window);
253
254
255
256
257
258
259
break;
default:
break;
}
}
static void
Mar 1, 2017
Mar 1, 2017
260
HandleResize(MirResizeEvent const* resize_event, SDL_Window* window)
Feb 21, 2016
Feb 21, 2016
262
263
264
265
266
267
268
269
int new_w = MIR_mir_resize_event_get_width (resize_event);
int new_h = MIR_mir_resize_event_get_height(resize_event);
int old_w = window->w;
int old_h = window->h;
if (new_w != old_w || new_h != old_h)
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, new_w, new_h);
Aug 30, 2016
Aug 30, 2016
272
static void
Mar 1, 2017
Mar 1, 2017
273
HandleWindow(MirWindowEvent const* event, SDL_Window* window)
Aug 30, 2016
Aug 30, 2016
274
{
Feb 27, 2017
Feb 27, 2017
275
276
MirWindowAttrib attrib = MIR_mir_window_event_get_attribute(event);
int value = MIR_mir_window_event_get_attribute_value(event);
Aug 30, 2016
Aug 30, 2016
277
Feb 27, 2017
Feb 27, 2017
278
279
if (attrib == mir_window_attrib_focus) {
if (value == mir_window_focus_state_focused) {
Aug 30, 2016
Aug 30, 2016
280
281
SDL_SetKeyboardFocus(window);
}
Feb 27, 2017
Feb 27, 2017
282
else if (value == mir_window_focus_state_unfocused) {
Aug 30, 2016
Aug 30, 2016
283
284
285
286
287
SDL_SetKeyboardFocus(NULL);
}
}
}
Feb 27, 2017
Feb 27, 2017
289
MIR_HandleEvent(MirWindow* mirwindow, MirEvent const* ev, void* context)
Feb 21, 2016
Feb 21, 2016
291
292
293
294
295
296
MirEventType event_type = MIR_mir_event_get_type(ev);
SDL_Window* window = (SDL_Window*)context;
if (window) {
switch (event_type) {
case (mir_event_type_input):
Mar 1, 2017
Mar 1, 2017
297
HandleInput(MIR_mir_event_get_input_event(ev), window);
Feb 21, 2016
Feb 21, 2016
298
299
break;
case (mir_event_type_resize):
Mar 1, 2017
Mar 1, 2017
300
HandleResize(MIR_mir_event_get_resize_event(ev), window);
Feb 21, 2016
Feb 21, 2016
301
break;
Feb 27, 2017
Feb 27, 2017
302
case (mir_event_type_window):
Mar 1, 2017
Mar 1, 2017
303
HandleWindow(MIR_mir_event_get_window_event(ev), window);
Aug 30, 2016
Aug 30, 2016
304
break;
Feb 21, 2016
Feb 21, 2016
305
306
307
default:
break;
}
308
309
310
311
312
313
}
}
#endif /* SDL_VIDEO_DRIVER_MIR */
/* vi: set ts=4 sw=4 expandtab: */