blob: 69e460c730a85b2510fd3942ec7740d74cc27058 [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
Kristian Høgsbergc7d2c4c2013-08-26 14:43:17 -070024#include <config.h>
25
Kristian Høgsberg558949b2011-12-21 22:54:49 -050026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdbool.h>
30#include <assert.h>
31#include <unistd.h>
32#include <sys/mman.h>
33
34#include <wayland-client.h>
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030035#include "../shared/os-compatibility.h"
Kristian Høgsberg558949b2011-12-21 22:54:49 -050036
Kristian Høgsberg5717b6d2012-10-19 17:12:38 -040037#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
38
Kristian Høgsberg558949b2011-12-21 22:54:49 -050039struct touch {
40 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040041 struct wl_registry *registry;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050042 struct wl_compositor *compositor;
43 struct wl_shell *shell;
44 struct wl_shm *shm;
Daniel Stone37816df2012-05-16 18:45:18 +010045 struct wl_seat *seat;
46 struct wl_touch *wl_touch;
47 struct wl_pointer *pointer;
48 struct wl_keyboard *keyboard;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050049 struct wl_surface *surface;
50 struct wl_shell_surface *shell_surface;
51 struct wl_buffer *buffer;
52 int has_argb;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050053 int width, height;
54 void *data;
55};
56
57static void
58create_shm_buffer(struct touch *touch)
59{
Kristian Høgsberg16626282012-04-03 11:21:27 -040060 struct wl_shm_pool *pool;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050061 int fd, size, stride;
62
Kristian Høgsberg558949b2011-12-21 22:54:49 -050063 stride = touch->width * 4;
64 size = stride * touch->height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030065
66 fd = os_create_anonymous_file(size);
67 if (fd < 0) {
68 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
69 size);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050070 exit(1);
71 }
72
73 touch->data =
74 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050075 if (touch->data == MAP_FAILED) {
76 fprintf(stderr, "mmap failed: %m\n");
77 close(fd);
78 exit(1);
79 }
80
Kristian Høgsberg16626282012-04-03 11:21:27 -040081 pool = wl_shm_create_pool(touch->shm, fd, size);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050082 touch->buffer =
Kristian Høgsberg16626282012-04-03 11:21:27 -040083 wl_shm_pool_create_buffer(pool, 0,
84 touch->width, touch->height, stride,
85 WL_SHM_FORMAT_ARGB8888);
86 wl_shm_pool_destroy(pool);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050087
88 close(fd);
89}
90
91static void
92shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
93{
94 struct touch *touch = data;
95
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050096 if (format == WL_SHM_FORMAT_ARGB8888)
Kristian Høgsberg558949b2011-12-21 22:54:49 -050097 touch->has_argb = 1;
98}
99
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100100struct wl_shm_listener shm_listener = {
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500101 shm_format
102};
103
104
105static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500106touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
107{
108 uint32_t *p, c;
109 static const uint32_t colors[] = {
110 0xffff0000,
111 0xffffff00,
112 0xff0000ff,
113 0xffff00ff,
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300114 0xff00ff00,
115 0xff00ffff,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500116 };
117
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400118 if (id < (int32_t) ARRAY_LENGTH(colors))
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500119 c = colors[id];
120 else
121 c = 0xffffffff;
122
Pekka Paalanen07684192012-07-31 13:21:12 +0300123 if (x < 2 || x >= touch->width - 2 ||
124 y < 2 || y >= touch->height - 2)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500125 return;
126
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300127 p = (uint32_t *) touch->data + (x - 2) + (y - 2) * touch->width;
128 p[2] = c;
129 p += touch->width;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500130 p[1] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300131 p[2] = c;
132 p[3] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500133 p += touch->width;
134 p[0] = c;
135 p[1] = c;
136 p[2] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300137 p[3] = c;
138 p[4] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500139 p += touch->width;
140 p[1] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300141 p[2] = c;
142 p[3] = c;
143 p += touch->width;
144 p[2] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500145
Kristian Høgsberg5addaa12013-07-22 14:32:41 -0700146 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
Pekka Paalanen76fc57e2012-07-31 13:21:13 +0300147 wl_surface_damage(touch->surface, x - 2, y - 2, 5, 5);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300148 /* todo: We could queue up more damage before committing, if there
149 * are more input events to handle.
150 */
151 wl_surface_commit(touch->surface);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500152}
153
154static void
Daniel Stone37816df2012-05-16 18:45:18 +0100155touch_handle_down(void *data, struct wl_touch *wl_touch,
156 uint32_t serial, uint32_t time, struct wl_surface *surface,
157 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500158{
159 struct touch *touch = data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400160 float x = wl_fixed_to_double(x_w);
161 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500162
163 touch_paint(touch, x, y, id);
164}
165
166static void
Daniel Stone37816df2012-05-16 18:45:18 +0100167touch_handle_up(void *data, struct wl_touch *wl_touch,
168 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500169{
170}
171
172static void
Daniel Stone37816df2012-05-16 18:45:18 +0100173touch_handle_motion(void *data, struct wl_touch *wl_touch,
174 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500175{
176 struct touch *touch = data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400177 float x = wl_fixed_to_double(x_w);
178 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500179
180 touch_paint(touch, x, y, id);
181}
182
183static void
Daniel Stone37816df2012-05-16 18:45:18 +0100184touch_handle_frame(void *data, struct wl_touch *wl_touch)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500185{
186}
187
188static void
Daniel Stone37816df2012-05-16 18:45:18 +0100189touch_handle_cancel(void *data, struct wl_touch *wl_touch)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500190{
191}
192
Daniel Stone37816df2012-05-16 18:45:18 +0100193static const struct wl_touch_listener touch_listener = {
194 touch_handle_down,
195 touch_handle_up,
196 touch_handle_motion,
197 touch_handle_frame,
198 touch_handle_cancel,
199};
200
201static void
202seat_handle_capabilities(void *data, struct wl_seat *seat,
203 enum wl_seat_capability caps)
204{
205 struct touch *touch = data;
206
207 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !touch->wl_touch) {
208 touch->wl_touch = wl_seat_get_touch(seat);
209 wl_touch_set_user_data(touch->wl_touch, touch);
210 wl_touch_add_listener(touch->wl_touch, &touch_listener, touch);
211 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && touch->wl_touch) {
212 wl_touch_destroy(touch->wl_touch);
213 touch->wl_touch = NULL;
214 }
215}
216
217static const struct wl_seat_listener seat_listener = {
218 seat_handle_capabilities,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500219};
220
221static void
Pekka Paalanen7e94a982012-07-31 13:21:10 +0300222handle_ping(void *data, struct wl_shell_surface *shell_surface,
223 uint32_t serial)
224{
225 wl_shell_surface_pong(shell_surface, serial);
226}
227
228static void
229handle_configure(void *data, struct wl_shell_surface *shell_surface,
230 uint32_t edges, int32_t width, int32_t height)
231{
232}
233
234static void
235handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
236{
237}
238
239static const struct wl_shell_surface_listener shell_surface_listener = {
240 handle_ping,
241 handle_configure,
242 handle_popup_done
243};
244
245static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400246handle_global(void *data, struct wl_registry *registry,
247 uint32_t name, const char *interface, uint32_t version)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500248{
249 struct touch *touch = data;
250
251 if (strcmp(interface, "wl_compositor") == 0) {
252 touch->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400253 wl_registry_bind(registry, name,
254 &wl_compositor_interface, 1);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500255 } else if (strcmp(interface, "wl_shell") == 0) {
256 touch->shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400257 wl_registry_bind(registry, name,
258 &wl_shell_interface, 1);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500259 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400260 touch->shm = wl_registry_bind(registry, name,
261 &wl_shm_interface, 1);
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100262 wl_shm_add_listener(touch->shm, &shm_listener, touch);
Daniel Stone37816df2012-05-16 18:45:18 +0100263 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400264 touch->seat = wl_registry_bind(registry, name,
265 &wl_seat_interface, 1);
Daniel Stone37816df2012-05-16 18:45:18 +0100266 wl_seat_add_listener(touch->seat, &seat_listener, touch);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500267 }
268}
269
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200270static void
271handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
272{
273}
274
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400275static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200276 handle_global,
277 handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400278};
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500279
280static struct touch *
281touch_create(int width, int height)
282{
283 struct touch *touch;
284
285 touch = malloc(sizeof *touch);
Kristian Høgsbergc85a9172013-08-15 11:40:30 -0700286 if (touch == NULL) {
287 fprintf(stderr, "out of memory\n");
288 exit(1);
289 }
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500290 touch->display = wl_display_connect(NULL);
291 assert(touch->display);
292
293 touch->has_argb = 0;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400294 touch->registry = wl_display_get_registry(touch->display);
295 wl_registry_add_listener(touch->registry, &registry_listener, touch);
296 wl_display_dispatch(touch->display);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500297 wl_display_roundtrip(touch->display);
298
299 if (!touch->has_argb) {
300 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
301 exit(1);
302 }
303
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400304 wl_display_get_fd(touch->display);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500305
306 touch->width = width;
307 touch->height = height;
308 touch->surface = wl_compositor_create_surface(touch->compositor);
309 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
310 touch->surface);
311 create_shm_buffer(touch);
312
Pekka Paalanen7e94a982012-07-31 13:21:10 +0300313 if (touch->shell_surface) {
314 wl_shell_surface_add_listener(touch->shell_surface,
315 &shell_surface_listener, touch);
316 wl_shell_surface_set_toplevel(touch->shell_surface);
317 }
318
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500319 wl_surface_set_user_data(touch->surface, touch);
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600320 wl_shell_surface_set_title(touch->shell_surface, "simple-touch");
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500321
322 memset(touch->data, 64, width * height * 4);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500323 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
324 wl_surface_damage(touch->surface, 0, 0, width, height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300325 wl_surface_commit(touch->surface);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500326
327 return touch;
328}
329
330int
331main(int argc, char **argv)
332{
333 struct touch *touch;
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400334 int ret = 0;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500335
336 touch = touch_create(600, 500);
337
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400338 while (ret != -1)
339 ret = wl_display_dispatch(touch->display);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500340
341 return 0;
342}