SDL  2.0
SDL_waylandtouch.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 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 /* Contributed by Thomas Perl <thomas.perl@jollamobile.com> */
23 
24 #include "../../SDL_internal.h"
25 
26 #ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
27 
28 #include "SDL_log.h"
29 #include "SDL_mouse.h"
30 #include "SDL_keyboard.h"
31 #include "SDL_waylandtouch.h"
32 #include "../../events/SDL_touch_c.h"
33 
34 struct SDL_WaylandTouch {
35  struct qt_touch_extension *touch_extension;
36 };
37 
38 
39 /**
40  * Qt TouchPointState
41  * adapted from qtbase/src/corelib/global/qnamespace.h
42  **/
43 enum QtWaylandTouchPointState {
44  QtWaylandTouchPointPressed = 0x01,
45  QtWaylandTouchPointMoved = 0x02,
46  /*
47  Never sent by the server:
48  QtWaylandTouchPointStationary = 0x04,
49  */
50  QtWaylandTouchPointReleased = 0x08,
51 };
52 
53 static void
54 touch_handle_touch(void *data,
55  struct qt_touch_extension *qt_touch_extension,
56  uint32_t time,
57  uint32_t id,
59  int32_t x,
60  int32_t y,
61  int32_t normalized_x,
62  int32_t normalized_y,
63  int32_t width,
65  uint32_t pressure,
66  int32_t velocity_x,
67  int32_t velocity_y,
69  struct wl_array *rawdata)
70 {
71  /**
72  * Event is assembled in QtWayland in TouchExtensionGlobal::postTouchEvent
73  * (src/compositor/wayland_wrapper/qwltouch.cpp)
74  **/
75 
76  float FIXED_TO_FLOAT = 1. / 10000.;
77  float xf = FIXED_TO_FLOAT * x;
78  float yf = FIXED_TO_FLOAT * y;
79 
80  float PRESSURE_TO_FLOAT = 1. / 255.;
81  float pressuref = PRESSURE_TO_FLOAT * pressure;
82 
83  uint32_t touchState = state & 0xFFFF;
84  /*
85  Other fields that are sent by the server (qwltouch.cpp),
86  but not used at the moment can be decoded in this way:
87 
88  uint32_t sentPointCount = state >> 16;
89  uint32_t touchFlags = flags & 0xFFFF;
90  uint32_t capabilities = flags >> 16;
91  */
92 
94 
95  SDL_TouchID deviceId = 1;
96  if (SDL_AddTouch(deviceId, SDL_TOUCH_DEVICE_DIRECT, "qt_touch_extension") < 0) {
97  SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
98  }
99 
100  /* FIXME: This should be the window the given wayland surface is associated
101  * with, but how do we get the wayland surface? */
103  if (window == NULL) {
105  }
106 
107  switch (touchState) {
108  case QtWaylandTouchPointPressed:
109  case QtWaylandTouchPointReleased:
110  SDL_SendTouch(deviceId, (SDL_FingerID)id, window,
111  (touchState == QtWaylandTouchPointPressed) ? SDL_TRUE : SDL_FALSE,
112  xf, yf, pressuref);
113  break;
114  case QtWaylandTouchPointMoved:
115  SDL_SendTouchMotion(deviceId, (SDL_FingerID)id, window, xf, yf, pressuref);
116  break;
117  default:
118  /* Should not happen */
119  break;
120  }
121 }
122 
123 static void
124 touch_handle_configure(void *data,
125  struct qt_touch_extension *qt_touch_extension,
126  uint32_t flags)
127 {
128 }
129 
130 
131 /* wayland-qt-touch-extension.c BEGINS */
132 
133 static const struct qt_touch_extension_listener touch_listener = {
134  touch_handle_touch,
135  touch_handle_configure,
136 };
137 
138 static const struct wl_interface *qt_touch_extension_types[] = {
139  NULL,
140  NULL,
141  NULL,
142  NULL,
143  NULL,
144  NULL,
145  NULL,
146  NULL,
147  NULL,
148  NULL,
149  NULL,
150  NULL,
151  NULL,
152  NULL,
153 };
154 
155 static const struct wl_message qt_touch_extension_requests[] = {
156  { "dummy", "", qt_touch_extension_types + 0 },
157 };
158 
159 static const struct wl_message qt_touch_extension_events[] = {
160  { "touch", "uuuiiiiiiuiiua", qt_touch_extension_types + 0 },
161  { "configure", "u", qt_touch_extension_types + 0 },
162 };
163 
164 WL_EXPORT const struct wl_interface qt_touch_extension_interface = {
165  "qt_touch_extension", 1,
166  1, qt_touch_extension_requests,
167  2, qt_touch_extension_events,
168 };
169 
170 /* wayland-qt-touch-extension.c ENDS */
171 
172 /* wayland-qt-windowmanager.c BEGINS */
173 static const struct wl_interface *qt_windowmanager_types[] = {
174  NULL,
175  NULL,
176 };
177 
178 static const struct wl_message qt_windowmanager_requests[] = {
179  { "open_url", "us", qt_windowmanager_types + 0 },
180 };
181 
182 static const struct wl_message qt_windowmanager_events[] = {
183  { "hints", "i", qt_windowmanager_types + 0 },
184  { "quit", "", qt_windowmanager_types + 0 },
185 };
186 
187 WL_EXPORT const struct wl_interface qt_windowmanager_interface = {
188  "qt_windowmanager", 1,
189  1, qt_windowmanager_requests,
190  2, qt_windowmanager_events,
191 };
192 /* wayland-qt-windowmanager.c ENDS */
193 
194 /* wayland-qt-surface-extension.c BEGINS */
195 #ifndef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
196 extern const struct wl_interface wl_surface_interface;
197 #endif
198 
199 static const struct wl_interface *qt_surface_extension_types[] = {
200  NULL,
201  NULL,
202  &qt_extended_surface_interface,
203 #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
204  /* FIXME: Set this dynamically to (*WAYLAND_wl_surface_interface) ?
205  * The value comes from auto generated code and does
206  * not appear to actually be used anywhere
207  */
208  NULL,
209 #else
211 #endif
212 };
213 
214 static const struct wl_message qt_surface_extension_requests[] = {
215  { "get_extended_surface", "no", qt_surface_extension_types + 2 },
216 };
217 
218 WL_EXPORT const struct wl_interface qt_surface_extension_interface = {
219  "qt_surface_extension", 1,
220  1, qt_surface_extension_requests,
221  0, NULL,
222 };
223 
224 static const struct wl_message qt_extended_surface_requests[] = {
225  { "update_generic_property", "sa", qt_surface_extension_types + 0 },
226  { "set_content_orientation", "i", qt_surface_extension_types + 0 },
227  { "set_window_flags", "i", qt_surface_extension_types + 0 },
228 };
229 
230 static const struct wl_message qt_extended_surface_events[] = {
231  { "onscreen_visibility", "i", qt_surface_extension_types + 0 },
232  { "set_generic_property", "sa", qt_surface_extension_types + 0 },
233  { "close", "", qt_surface_extension_types + 0 },
234 };
235 
236 WL_EXPORT const struct wl_interface qt_extended_surface_interface = {
237  "qt_extended_surface", 1,
238  3, qt_extended_surface_requests,
239  3, qt_extended_surface_events,
240 };
241 
242 /* wayland-qt-surface-extension.c ENDS */
243 
244 void
245 Wayland_touch_create(SDL_VideoData *data, uint32_t id)
246 {
247  struct SDL_WaylandTouch *touch;
248 
249  if (data->touch) {
250  Wayland_touch_destroy(data);
251  }
252 
253  /* !!! FIXME: check for failure, call SDL_OutOfMemory() */
254  data->touch = SDL_malloc(sizeof(struct SDL_WaylandTouch));
255 
256  touch = data->touch;
257  touch->touch_extension = wl_registry_bind(data->registry, id, &qt_touch_extension_interface, 1);
258  qt_touch_extension_add_listener(touch->touch_extension, &touch_listener, data);
259 }
260 
261 void
262 Wayland_touch_destroy(SDL_VideoData *data)
263 {
264  if (data->touch) {
265  struct SDL_WaylandTouch *touch = data->touch;
266  if (touch->touch_extension) {
267  qt_touch_extension_destroy(touch->touch_extension);
268  }
269 
270  SDL_free(data->touch);
271  data->touch = NULL;
272  }
273 }
274 
275 #endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
SDL_TOUCH_DEVICE_DIRECT
@ SDL_TOUCH_DEVICE_DIRECT
Definition: SDL_touch.h:47
wl_surface_interface
const struct wl_interface wl_surface_interface
Definition: wayland-protocol.c:367
time
EGLSurface EGLnsecsANDROID time
Definition: eglext.h:518
SDL_mouse.h
NULL
#define NULL
Definition: begin_code.h:167
width
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
SDL_log.h
SDL_FingerID
Sint64 SDL_FingerID
Definition: SDL_touch.h:42
SDL_SendTouch
int SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid, SDL_Window *window, SDL_bool down, float x, float y, float pressure)
Definition: SDL_touch.c:242
SDL_keyboard.h
SDL_TouchID
Sint64 SDL_TouchID
Definition: SDL_touch.h:41
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:74
SDL_GetKeyboardFocus
#define SDL_GetKeyboardFocus
Definition: SDL_dynapi_overrides.h:216
wl_registry_bind
static void * wl_registry_bind(struct wl_registry *wl_registry, uint32_t name, const struct wl_interface *interface, uint32_t version)
Definition: wayland-client-protocol.h:1075
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_AddTouch
int SDL_AddTouch(SDL_TouchID touchID, SDL_TouchDeviceType type, const char *name)
Definition: SDL_touch.c:155
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
int32_t
signed int int32_t
Definition: SDL_config_windows.h:62
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
height
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:63
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_waylandtouch.h
SDL_GetMouseFocus
#define SDL_GetMouseFocus
Definition: SDL_dynapi_overrides.h:245
SDL_SendTouchMotion
int SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, SDL_Window *window, float x, float y, float pressure)
Definition: SDL_touch.c:356
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
flags
GLbitfield flags
Definition: SDL_opengl_glext.h:1483
state
struct xkb_state * state
Definition: SDL_waylandsym.h:114
SDL_VideoData
Definition: SDL_androidvideo.h:36