blob: 534c77caed291ef9954c1e019b997bb665ccd0b1 [file] [log] [blame]
Kristian Høgsberg558949b2011-12-21 22:54:49 -05001/*
2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2011 Intel Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdbool.h>
28#include <assert.h>
29#include <unistd.h>
30#include <sys/mman.h>
31
32#include <wayland-client.h>
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030033#include "../shared/os-compatibility.h"
Kristian Høgsberg558949b2011-12-21 22:54:49 -050034
Kristian Høgsberg5717b6d2012-10-19 17:12:38 -040035#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
36
Kristian Høgsberg558949b2011-12-21 22:54:49 -050037struct touch {
38 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040039 struct wl_registry *registry;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050040 struct wl_compositor *compositor;
41 struct wl_shell *shell;
42 struct wl_shm *shm;
Daniel Stone37816df2012-05-16 18:45:18 +010043 struct wl_seat *seat;
44 struct wl_touch *wl_touch;
45 struct wl_pointer *pointer;
46 struct wl_keyboard *keyboard;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050047 struct wl_surface *surface;
48 struct wl_shell_surface *shell_surface;
49 struct wl_buffer *buffer;
50 int has_argb;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050051 int width, height;
52 void *data;
53};
54
55static void
56create_shm_buffer(struct touch *touch)
57{
Kristian Høgsberg16626282012-04-03 11:21:27 -040058 struct wl_shm_pool *pool;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050059 int fd, size, stride;
60
Kristian Høgsberg558949b2011-12-21 22:54:49 -050061 stride = touch->width * 4;
62 size = stride * touch->height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030063
64 fd = os_create_anonymous_file(size);
65 if (fd < 0) {
66 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
67 size);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050068 exit(1);
69 }
70
71 touch->data =
72 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050073 if (touch->data == MAP_FAILED) {
74 fprintf(stderr, "mmap failed: %m\n");
75 close(fd);
76 exit(1);
77 }
78
Kristian Høgsberg16626282012-04-03 11:21:27 -040079 pool = wl_shm_create_pool(touch->shm, fd, size);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050080 touch->buffer =
Kristian Høgsberg16626282012-04-03 11:21:27 -040081 wl_shm_pool_create_buffer(pool, 0,
82 touch->width, touch->height, stride,
83 WL_SHM_FORMAT_ARGB8888);
84 wl_shm_pool_destroy(pool);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050085
86 close(fd);
87}
88
89static void
90shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
91{
92 struct touch *touch = data;
93
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050094 if (format == WL_SHM_FORMAT_ARGB8888)
Kristian Høgsberg558949b2011-12-21 22:54:49 -050095 touch->has_argb = 1;
96}
97
98struct wl_shm_listener shm_listenter = {
99 shm_format
100};
101
102
103static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500104touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
105{
106 uint32_t *p, c;
107 static const uint32_t colors[] = {
108 0xffff0000,
109 0xffffff00,
110 0xff0000ff,
111 0xffff00ff,
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300112 0xff00ff00,
113 0xff00ffff,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500114 };
115
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400116 if (id < (int32_t) ARRAY_LENGTH(colors))
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500117 c = colors[id];
118 else
119 c = 0xffffffff;
120
Pekka Paalanen07684192012-07-31 13:21:12 +0300121 if (x < 2 || x >= touch->width - 2 ||
122 y < 2 || y >= touch->height - 2)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500123 return;
124
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300125 p = (uint32_t *) touch->data + (x - 2) + (y - 2) * touch->width;
126 p[2] = c;
127 p += touch->width;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500128 p[1] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300129 p[2] = c;
130 p[3] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500131 p += touch->width;
132 p[0] = c;
133 p[1] = c;
134 p[2] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300135 p[3] = c;
136 p[4] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500137 p += touch->width;
138 p[1] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300139 p[2] = c;
140 p[3] = c;
141 p += touch->width;
142 p[2] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500143
Kristian Høgsberg5addaa12013-07-22 14:32:41 -0700144 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
Pekka Paalanen76fc57e2012-07-31 13:21:13 +0300145 wl_surface_damage(touch->surface, x - 2, y - 2, 5, 5);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300146 /* todo: We could queue up more damage before committing, if there
147 * are more input events to handle.
148 */
149 wl_surface_commit(touch->surface);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500150}
151
152static void
Daniel Stone37816df2012-05-16 18:45:18 +0100153touch_handle_down(void *data, struct wl_touch *wl_touch,
154 uint32_t serial, uint32_t time, struct wl_surface *surface,
155 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500156{
157 struct touch *touch = data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400158 float x = wl_fixed_to_double(x_w);
159 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500160
161 touch_paint(touch, x, y, id);
162}
163
164static void
Daniel Stone37816df2012-05-16 18:45:18 +0100165touch_handle_up(void *data, struct wl_touch *wl_touch,
166 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500167{
168}
169
170static void
Daniel Stone37816df2012-05-16 18:45:18 +0100171touch_handle_motion(void *data, struct wl_touch *wl_touch,
172 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500173{
174 struct touch *touch = data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400175 float x = wl_fixed_to_double(x_w);
176 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500177
178 touch_paint(touch, x, y, id);
179}
180
181static void
Daniel Stone37816df2012-05-16 18:45:18 +0100182touch_handle_frame(void *data, struct wl_touch *wl_touch)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500183{
184}
185
186static void
Daniel Stone37816df2012-05-16 18:45:18 +0100187touch_handle_cancel(void *data, struct wl_touch *wl_touch)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500188{
189}
190
Daniel Stone37816df2012-05-16 18:45:18 +0100191static const struct wl_touch_listener touch_listener = {
192 touch_handle_down,
193 touch_handle_up,
194 touch_handle_motion,
195 touch_handle_frame,
196 touch_handle_cancel,
197};
198
199static void
200seat_handle_capabilities(void *data, struct wl_seat *seat,
201 enum wl_seat_capability caps)
202{
203 struct touch *touch = data;
204
205 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !touch->wl_touch) {
206 touch->wl_touch = wl_seat_get_touch(seat);
207 wl_touch_set_user_data(touch->wl_touch, touch);
208 wl_touch_add_listener(touch->wl_touch, &touch_listener, touch);
209 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && touch->wl_touch) {
210 wl_touch_destroy(touch->wl_touch);
211 touch->wl_touch = NULL;
212 }
213}
214
215static const struct wl_seat_listener seat_listener = {
216 seat_handle_capabilities,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500217};
218
219static void
Pekka Paalanen7e94a982012-07-31 13:21:10 +0300220handle_ping(void *data, struct wl_shell_surface *shell_surface,
221 uint32_t serial)
222{
223 wl_shell_surface_pong(shell_surface, serial);
224}
225
226static void
227handle_configure(void *data, struct wl_shell_surface *shell_surface,
228 uint32_t edges, int32_t width, int32_t height)
229{
230}
231
232static void
233handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
234{
235}
236
237static const struct wl_shell_surface_listener shell_surface_listener = {
238 handle_ping,
239 handle_configure,
240 handle_popup_done
241};
242
243static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400244handle_global(void *data, struct wl_registry *registry,
245 uint32_t name, const char *interface, uint32_t version)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500246{
247 struct touch *touch = data;
248
249 if (strcmp(interface, "wl_compositor") == 0) {
250 touch->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400251 wl_registry_bind(registry, name,
252 &wl_compositor_interface, 1);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500253 } else if (strcmp(interface, "wl_shell") == 0) {
254 touch->shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400255 wl_registry_bind(registry, name,
256 &wl_shell_interface, 1);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500257 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400258 touch->shm = wl_registry_bind(registry, name,
259 &wl_shm_interface, 1);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500260 wl_shm_add_listener(touch->shm, &shm_listenter, touch);
Daniel Stone37816df2012-05-16 18:45:18 +0100261 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400262 touch->seat = wl_registry_bind(registry, name,
263 &wl_seat_interface, 1);
Daniel Stone37816df2012-05-16 18:45:18 +0100264 wl_seat_add_listener(touch->seat, &seat_listener, touch);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500265 }
266}
267
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200268static void
269handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
270{
271}
272
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400273static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200274 handle_global,
275 handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400276};
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500277
278static struct touch *
279touch_create(int width, int height)
280{
281 struct touch *touch;
282
283 touch = malloc(sizeof *touch);
284 touch->display = wl_display_connect(NULL);
285 assert(touch->display);
286
287 touch->has_argb = 0;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400288 touch->registry = wl_display_get_registry(touch->display);
289 wl_registry_add_listener(touch->registry, &registry_listener, touch);
290 wl_display_dispatch(touch->display);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500291 wl_display_roundtrip(touch->display);
292
293 if (!touch->has_argb) {
294 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
295 exit(1);
296 }
297
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400298 wl_display_get_fd(touch->display);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500299
300 touch->width = width;
301 touch->height = height;
302 touch->surface = wl_compositor_create_surface(touch->compositor);
303 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
304 touch->surface);
305 create_shm_buffer(touch);
306
Pekka Paalanen7e94a982012-07-31 13:21:10 +0300307 if (touch->shell_surface) {
308 wl_shell_surface_add_listener(touch->shell_surface,
309 &shell_surface_listener, touch);
310 wl_shell_surface_set_toplevel(touch->shell_surface);
311 }
312
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500313 wl_surface_set_user_data(touch->surface, touch);
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600314 wl_shell_surface_set_title(touch->shell_surface, "simple-touch");
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500315
316 memset(touch->data, 64, width * height * 4);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500317 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
318 wl_surface_damage(touch->surface, 0, 0, width, height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300319 wl_surface_commit(touch->surface);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500320
321 return touch;
322}
323
324int
325main(int argc, char **argv)
326{
327 struct touch *touch;
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400328 int ret = 0;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500329
330 touch = touch_create(600, 500);
331
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400332 while (ret != -1)
333 ret = wl_display_dispatch(touch->display);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500334
335 return 0;
336}