blob: 0eeb8f9e75c4c0c7570e2a4164b9f7315b0eb4b6 [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
35struct touch {
36 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040037 struct wl_registry *registry;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050038 struct wl_compositor *compositor;
39 struct wl_shell *shell;
40 struct wl_shm *shm;
Daniel Stone37816df2012-05-16 18:45:18 +010041 struct wl_seat *seat;
42 struct wl_touch *wl_touch;
43 struct wl_pointer *pointer;
44 struct wl_keyboard *keyboard;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050045 struct wl_surface *surface;
46 struct wl_shell_surface *shell_surface;
47 struct wl_buffer *buffer;
48 int has_argb;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050049 int width, height;
50 void *data;
51};
52
53static void
54create_shm_buffer(struct touch *touch)
55{
Kristian Høgsberg16626282012-04-03 11:21:27 -040056 struct wl_shm_pool *pool;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050057 int fd, size, stride;
58
Kristian Høgsberg558949b2011-12-21 22:54:49 -050059 stride = touch->width * 4;
60 size = stride * touch->height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030061
62 fd = os_create_anonymous_file(size);
63 if (fd < 0) {
64 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
65 size);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050066 exit(1);
67 }
68
69 touch->data =
70 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050071 if (touch->data == MAP_FAILED) {
72 fprintf(stderr, "mmap failed: %m\n");
73 close(fd);
74 exit(1);
75 }
76
Kristian Høgsberg16626282012-04-03 11:21:27 -040077 pool = wl_shm_create_pool(touch->shm, fd, size);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050078 touch->buffer =
Kristian Høgsberg16626282012-04-03 11:21:27 -040079 wl_shm_pool_create_buffer(pool, 0,
80 touch->width, touch->height, stride,
81 WL_SHM_FORMAT_ARGB8888);
82 wl_shm_pool_destroy(pool);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050083
84 close(fd);
85}
86
87static void
88shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
89{
90 struct touch *touch = data;
91
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050092 if (format == WL_SHM_FORMAT_ARGB8888)
Kristian Høgsberg558949b2011-12-21 22:54:49 -050093 touch->has_argb = 1;
94}
95
96struct wl_shm_listener shm_listenter = {
97 shm_format
98};
99
100
101static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500102touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
103{
104 uint32_t *p, c;
105 static const uint32_t colors[] = {
106 0xffff0000,
107 0xffffff00,
108 0xff0000ff,
109 0xffff00ff,
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300110 0xff00ff00,
111 0xff00ffff,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500112 };
113
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400114 if (id < (int32_t) ARRAY_LENGTH(colors))
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500115 c = colors[id];
116 else
117 c = 0xffffffff;
118
Pekka Paalanen07684192012-07-31 13:21:12 +0300119 if (x < 2 || x >= touch->width - 2 ||
120 y < 2 || y >= touch->height - 2)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500121 return;
122
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300123 p = (uint32_t *) touch->data + (x - 2) + (y - 2) * touch->width;
124 p[2] = c;
125 p += touch->width;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500126 p[1] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300127 p[2] = c;
128 p[3] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500129 p += touch->width;
130 p[0] = c;
131 p[1] = c;
132 p[2] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300133 p[3] = c;
134 p[4] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500135 p += touch->width;
136 p[1] = c;
Pekka Paalanen55b7cb22012-07-31 13:21:11 +0300137 p[2] = c;
138 p[3] = c;
139 p += touch->width;
140 p[2] = c;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500141
Pekka Paalanen76fc57e2012-07-31 13:21:13 +0300142 wl_surface_damage(touch->surface, x - 2, y - 2, 5, 5);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300143 /* todo: We could queue up more damage before committing, if there
144 * are more input events to handle.
145 */
146 wl_surface_commit(touch->surface);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500147}
148
149static void
Daniel Stone37816df2012-05-16 18:45:18 +0100150touch_handle_down(void *data, struct wl_touch *wl_touch,
151 uint32_t serial, uint32_t time, struct wl_surface *surface,
152 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500153{
154 struct touch *touch = data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400155 float x = wl_fixed_to_double(x_w);
156 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500157
158 touch_paint(touch, x, y, id);
159}
160
161static void
Daniel Stone37816df2012-05-16 18:45:18 +0100162touch_handle_up(void *data, struct wl_touch *wl_touch,
163 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500164{
165}
166
167static void
Daniel Stone37816df2012-05-16 18:45:18 +0100168touch_handle_motion(void *data, struct wl_touch *wl_touch,
169 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500170{
171 struct touch *touch = data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400172 float x = wl_fixed_to_double(x_w);
173 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500174
175 touch_paint(touch, x, y, id);
176}
177
178static void
Daniel Stone37816df2012-05-16 18:45:18 +0100179touch_handle_frame(void *data, struct wl_touch *wl_touch)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500180{
181}
182
183static void
Daniel Stone37816df2012-05-16 18:45:18 +0100184touch_handle_cancel(void *data, struct wl_touch *wl_touch)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500185{
186}
187
Daniel Stone37816df2012-05-16 18:45:18 +0100188static const struct wl_touch_listener touch_listener = {
189 touch_handle_down,
190 touch_handle_up,
191 touch_handle_motion,
192 touch_handle_frame,
193 touch_handle_cancel,
194};
195
196static void
197seat_handle_capabilities(void *data, struct wl_seat *seat,
198 enum wl_seat_capability caps)
199{
200 struct touch *touch = data;
201
202 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !touch->wl_touch) {
203 touch->wl_touch = wl_seat_get_touch(seat);
204 wl_touch_set_user_data(touch->wl_touch, touch);
205 wl_touch_add_listener(touch->wl_touch, &touch_listener, touch);
206 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && touch->wl_touch) {
207 wl_touch_destroy(touch->wl_touch);
208 touch->wl_touch = NULL;
209 }
210}
211
212static const struct wl_seat_listener seat_listener = {
213 seat_handle_capabilities,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500214};
215
216static void
Pekka Paalanen7e94a982012-07-31 13:21:10 +0300217handle_ping(void *data, struct wl_shell_surface *shell_surface,
218 uint32_t serial)
219{
220 wl_shell_surface_pong(shell_surface, serial);
221}
222
223static void
224handle_configure(void *data, struct wl_shell_surface *shell_surface,
225 uint32_t edges, int32_t width, int32_t height)
226{
227}
228
229static void
230handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
231{
232}
233
234static const struct wl_shell_surface_listener shell_surface_listener = {
235 handle_ping,
236 handle_configure,
237 handle_popup_done
238};
239
240static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400241handle_global(void *data, struct wl_registry *registry,
242 uint32_t name, const char *interface, uint32_t version)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500243{
244 struct touch *touch = data;
245
246 if (strcmp(interface, "wl_compositor") == 0) {
247 touch->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400248 wl_registry_bind(registry, name,
249 &wl_compositor_interface, 1);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500250 } else if (strcmp(interface, "wl_shell") == 0) {
251 touch->shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400252 wl_registry_bind(registry, name,
253 &wl_shell_interface, 1);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500254 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400255 touch->shm = wl_registry_bind(registry, name,
256 &wl_shm_interface, 1);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500257 wl_shm_add_listener(touch->shm, &shm_listenter, touch);
Daniel Stone37816df2012-05-16 18:45:18 +0100258 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400259 touch->seat = wl_registry_bind(registry, name,
260 &wl_seat_interface, 1);
Daniel Stone37816df2012-05-16 18:45:18 +0100261 wl_seat_add_listener(touch->seat, &seat_listener, touch);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500262 }
263}
264
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400265static const struct wl_registry_listener registry_listener = {
266 handle_global
267};
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500268
269static struct touch *
270touch_create(int width, int height)
271{
272 struct touch *touch;
273
274 touch = malloc(sizeof *touch);
275 touch->display = wl_display_connect(NULL);
276 assert(touch->display);
277
278 touch->has_argb = 0;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400279 touch->registry = wl_display_get_registry(touch->display);
280 wl_registry_add_listener(touch->registry, &registry_listener, touch);
281 wl_display_dispatch(touch->display);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500282 wl_display_roundtrip(touch->display);
283
284 if (!touch->has_argb) {
285 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
286 exit(1);
287 }
288
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400289 wl_display_get_fd(touch->display);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500290
291 touch->width = width;
292 touch->height = height;
293 touch->surface = wl_compositor_create_surface(touch->compositor);
294 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
295 touch->surface);
296 create_shm_buffer(touch);
297
Pekka Paalanen7e94a982012-07-31 13:21:10 +0300298 if (touch->shell_surface) {
299 wl_shell_surface_add_listener(touch->shell_surface,
300 &shell_surface_listener, touch);
301 wl_shell_surface_set_toplevel(touch->shell_surface);
302 }
303
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500304 wl_surface_set_user_data(touch->surface, touch);
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600305 wl_shell_surface_set_title(touch->shell_surface, "simple-touch");
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500306
307 memset(touch->data, 64, width * height * 4);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500308 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
309 wl_surface_damage(touch->surface, 0, 0, width, height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300310 wl_surface_commit(touch->surface);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500311
312 return touch;
313}
314
315int
316main(int argc, char **argv)
317{
318 struct touch *touch;
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400319 int ret = 0;
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500320
321 touch = touch_create(600, 500);
322
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400323 while (ret != -1)
324 ret = wl_display_dispatch(touch->display);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500325
326 return 0;
327}