blob: fefdd4747b68d3830b6bb10904e52e6bb61c7300 [file] [log] [blame]
Kristian Høgsberg558949b2011-12-21 22:54:49 -05001/*
2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2011 Intel Corporation
4 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07005 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg558949b2011-12-21 22:54:49 -050011 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070012 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg558949b2011-12-21 22:54:49 -050023 */
24
Kristian Høgsbergc7d2c4c2013-08-26 14:43:17 -070025#include <config.h>
26
Kristian Høgsberg558949b2011-12-21 22:54:49 -050027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdbool.h>
31#include <assert.h>
32#include <unistd.h>
33#include <sys/mman.h>
34
35#include <wayland-client.h>
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030036#include "../shared/os-compatibility.h"
Kristian Høgsberg558949b2011-12-21 22:54:49 -050037
Kristian Høgsberg5717b6d2012-10-19 17:12:38 -040038#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
39
Ander Conselvan de Oliveirac3f03f52014-05-07 11:57:27 +030040struct seat {
41 struct touch *touch;
42 struct wl_seat *seat;
43 struct wl_touch *wl_touch;
44};
45
Kristian Høgsberg558949b2011-12-21 22:54:49 -050046struct touch {
47 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040048 struct wl_registry *registry;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050049 struct wl_compositor *compositor;
50 struct wl_shell *shell;
51 struct wl_shm *shm;
Daniel Stone37816df2012-05-16 18:45:18 +010052 struct wl_pointer *pointer;
53 struct wl_keyboard *keyboard;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050054 struct wl_surface *surface;
55 struct wl_shell_surface *shell_surface;
56 struct wl_buffer *buffer;
57 int has_argb;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050058 int width, height;
59 void *data;
60};
61
62static void
63create_shm_buffer(struct touch *touch)
64{
Kristian Høgsberg16626282012-04-03 11:21:27 -040065 struct wl_shm_pool *pool;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050066 int fd, size, stride;
67
Kristian Høgsberg558949b2011-12-21 22:54:49 -050068 stride = touch->width * 4;
69 size = stride * touch->height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030070
71 fd = os_create_anonymous_file(size);
72 if (fd < 0) {
73 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
74 size);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050075 exit(1);
76 }
77
78 touch->data =
79 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050080 if (touch->data == MAP_FAILED) {
81 fprintf(stderr, "mmap failed: %m\n");
82 close(fd);
83 exit(1);
84 }
85
Kristian Høgsberg16626282012-04-03 11:21:27 -040086 pool = wl_shm_create_pool(touch->shm, fd, size);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050087 touch->buffer =
Kristian Høgsberg16626282012-04-03 11:21:27 -040088 wl_shm_pool_create_buffer(pool, 0,
89 touch->width, touch->height, stride,
90 WL_SHM_FORMAT_ARGB8888);
91 wl_shm_pool_destroy(pool);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050092
93 close(fd);
94}
95
96static void
97shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
98{
99 struct touch *touch = data;
100
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500101 if (format == WL_SHM_FORMAT_ARGB8888)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500102 touch->has_argb = 1;
103}
104
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100105struct wl_shm_listener shm_listener = {
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500106 shm_format
107};
108
109
110static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500111touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
112{
113 uint32_t *p, c;
114 static const uint32_t colors[] = {
115 0xffff0000,
116 0xffffff00,
117 0xff0000ff,
118 0xffff00ff,
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300119 0xff00ff00,
120 0xff00ffff,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500121 };
122
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400123 if (id < (int32_t) ARRAY_LENGTH(colors))
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500124 c = colors[id];
125 else
126 c = 0xffffffff;
127
Pekka Paalanen07684192012-07-31 13:21:12 +0300128 if (x < 2 || x >= touch->width - 2 ||
129 y < 2 || y >= touch->height - 2)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500130 return;
131
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300132 p = (uint32_t *) touch->data + (x - 2) + (y - 2) * touch->width;
133 p[2] = c;
134 p += touch->width;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500135 p[1] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300136 p[2] = c;
137 p[3] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500138 p += touch->width;
139 p[0] = c;
140 p[1] = c;
141 p[2] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300142 p[3] = c;
143 p[4] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500144 p += touch->width;
145 p[1] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300146 p[2] = c;
147 p[3] = c;
148 p += touch->width;
149 p[2] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500150
Kristian Høgsberg5addaa12013-07-22 14:32:41 -0700151 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
Pekka Paalanen76fc57e2012-07-31 13:21:13 +0300152 wl_surface_damage(touch->surface, x - 2, y - 2, 5, 5);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300153 /* todo: We could queue up more damage before committing, if there
154 * are more input events to handle.
155 */
156 wl_surface_commit(touch->surface);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500157}
158
159static void
Daniel Stone37816df2012-05-16 18:45:18 +0100160touch_handle_down(void *data, struct wl_touch *wl_touch,
161 uint32_t serial, uint32_t time, struct wl_surface *surface,
162 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500163{
164 struct touch *touch = data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400165 float x = wl_fixed_to_double(x_w);
166 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500167
168 touch_paint(touch, x, y, id);
169}
170
171static void
Daniel Stone37816df2012-05-16 18:45:18 +0100172touch_handle_up(void *data, struct wl_touch *wl_touch,
173 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500174{
175}
176
177static void
Daniel Stone37816df2012-05-16 18:45:18 +0100178touch_handle_motion(void *data, struct wl_touch *wl_touch,
179 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500180{
181 struct touch *touch = data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400182 float x = wl_fixed_to_double(x_w);
183 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500184
185 touch_paint(touch, x, y, id);
186}
187
188static void
Daniel Stone37816df2012-05-16 18:45:18 +0100189touch_handle_frame(void *data, struct wl_touch *wl_touch)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500190{
191}
192
193static void
Daniel Stone37816df2012-05-16 18:45:18 +0100194touch_handle_cancel(void *data, struct wl_touch *wl_touch)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500195{
196}
197
Daniel Stone37816df2012-05-16 18:45:18 +0100198static const struct wl_touch_listener touch_listener = {
199 touch_handle_down,
200 touch_handle_up,
201 touch_handle_motion,
202 touch_handle_frame,
203 touch_handle_cancel,
204};
205
206static void
Ander Conselvan de Oliveirac3f03f52014-05-07 11:57:27 +0300207seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
Daniel Stone37816df2012-05-16 18:45:18 +0100208 enum wl_seat_capability caps)
209{
Ander Conselvan de Oliveirac3f03f52014-05-07 11:57:27 +0300210 struct seat *seat = data;
211 struct touch *touch = seat->touch;
Daniel Stone37816df2012-05-16 18:45:18 +0100212
Ander Conselvan de Oliveirac3f03f52014-05-07 11:57:27 +0300213 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !seat->wl_touch) {
214 seat->wl_touch = wl_seat_get_touch(wl_seat);
215 wl_touch_set_user_data(seat->wl_touch, touch);
216 wl_touch_add_listener(seat->wl_touch, &touch_listener, touch);
217 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && seat->wl_touch) {
218 wl_touch_destroy(seat->wl_touch);
219 seat->wl_touch = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100220 }
221}
222
223static const struct wl_seat_listener seat_listener = {
224 seat_handle_capabilities,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500225};
226
227static void
Ander Conselvan de Oliveirac3f03f52014-05-07 11:57:27 +0300228add_seat(struct touch *touch, uint32_t name, uint32_t version)
229{
230 struct seat *seat;
231
232 seat = malloc(sizeof *seat);
233 assert(seat);
234
235 seat->touch = touch;
236 seat->wl_touch = NULL;
237 seat->seat = wl_registry_bind(touch->registry, name,
238 &wl_seat_interface, 1);
239 wl_seat_add_listener(seat->seat, &seat_listener, seat);
240}
241
242static void
Pekka Paalanen7e94a982012-07-31 13:21:10 +0300243handle_ping(void *data, struct wl_shell_surface *shell_surface,
244 uint32_t serial)
245{
246 wl_shell_surface_pong(shell_surface, serial);
247}
248
249static void
250handle_configure(void *data, struct wl_shell_surface *shell_surface,
251 uint32_t edges, int32_t width, int32_t height)
252{
253}
254
255static void
256handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
257{
258}
259
260static const struct wl_shell_surface_listener shell_surface_listener = {
261 handle_ping,
262 handle_configure,
263 handle_popup_done
264};
265
266static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400267handle_global(void *data, struct wl_registry *registry,
268 uint32_t name, const char *interface, uint32_t version)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500269{
270 struct touch *touch = data;
271
272 if (strcmp(interface, "wl_compositor") == 0) {
273 touch->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400274 wl_registry_bind(registry, name,
275 &wl_compositor_interface, 1);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500276 } else if (strcmp(interface, "wl_shell") == 0) {
277 touch->shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400278 wl_registry_bind(registry, name,
279 &wl_shell_interface, 1);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500280 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400281 touch->shm = wl_registry_bind(registry, name,
282 &wl_shm_interface, 1);
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100283 wl_shm_add_listener(touch->shm, &shm_listener, touch);
Daniel Stone37816df2012-05-16 18:45:18 +0100284 } else if (strcmp(interface, "wl_seat") == 0) {
Ander Conselvan de Oliveirac3f03f52014-05-07 11:57:27 +0300285 add_seat(touch, name, version);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500286 }
287}
288
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200289static void
290handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
291{
292}
293
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400294static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200295 handle_global,
296 handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400297};
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500298
299static struct touch *
300touch_create(int width, int height)
301{
302 struct touch *touch;
303
304 touch = malloc(sizeof *touch);
Kristian Høgsbergc85a9172013-08-15 11:40:30 -0700305 if (touch == NULL) {
306 fprintf(stderr, "out of memory\n");
307 exit(1);
308 }
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500309 touch->display = wl_display_connect(NULL);
310 assert(touch->display);
311
312 touch->has_argb = 0;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400313 touch->registry = wl_display_get_registry(touch->display);
314 wl_registry_add_listener(touch->registry, &registry_listener, touch);
315 wl_display_dispatch(touch->display);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500316 wl_display_roundtrip(touch->display);
317
318 if (!touch->has_argb) {
319 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
320 exit(1);
321 }
322
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500323 touch->width = width;
324 touch->height = height;
325 touch->surface = wl_compositor_create_surface(touch->compositor);
326 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
327 touch->surface);
328 create_shm_buffer(touch);
329
Pekka Paalanen7e94a982012-07-31 13:21:10 +0300330 if (touch->shell_surface) {
331 wl_shell_surface_add_listener(touch->shell_surface,
332 &shell_surface_listener, touch);
333 wl_shell_surface_set_toplevel(touch->shell_surface);
334 }
335
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500336 wl_surface_set_user_data(touch->surface, touch);
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600337 wl_shell_surface_set_title(touch->shell_surface, "simple-touch");
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500338
339 memset(touch->data, 64, width * height * 4);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500340 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
341 wl_surface_damage(touch->surface, 0, 0, width, height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300342 wl_surface_commit(touch->surface);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500343
344 return touch;
345}
346
347int
348main(int argc, char **argv)
349{
350 struct touch *touch;
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400351 int ret = 0;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500352
353 touch = touch_create(600, 500);
354
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400355 while (ret != -1)
356 ret = wl_display_dispatch(touch->display);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500357
358 return 0;
359}