2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2011 Intel Corporation
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.
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
34 #include <wayland-client.h>
35 #include "../shared/os-compatibility.h"
37 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
42 struct wl_touch *wl_touch;
46 struct wl_display *display;
47 struct wl_registry *registry;
48 struct wl_compositor *compositor;
49 struct wl_shell *shell;
51 struct wl_pointer *pointer;
52 struct wl_keyboard *keyboard;
53 struct wl_surface *surface;
54 struct wl_shell_surface *shell_surface;
55 struct wl_buffer *buffer;
62 create_shm_buffer(struct touch *touch)
64 struct wl_shm_pool *pool;
67 stride = touch->width * 4;
68 size = stride * touch->height;
70 fd = os_create_anonymous_file(size);
72 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
78 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
79 if (touch->data == MAP_FAILED) {
80 fprintf(stderr, "mmap failed: %m\n");
85 pool = wl_shm_create_pool(touch->shm, fd, size);
87 wl_shm_pool_create_buffer(pool, 0,
88 touch->width, touch->height, stride,
89 WL_SHM_FORMAT_ARGB8888);
90 wl_shm_pool_destroy(pool);
96 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
98 struct touch *touch = data;
100 if (format == WL_SHM_FORMAT_ARGB8888)
104 struct wl_shm_listener shm_listener = {
110 touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
113 static const uint32_t colors[] = {
122 if (id < (int32_t) ARRAY_LENGTH(colors))
127 if (x < 2 || x >= touch->width - 2 ||
128 y < 2 || y >= touch->height - 2)
131 p = (uint32_t *) touch->data + (x - 2) + (y - 2) * touch->width;
150 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
151 wl_surface_damage(touch->surface, x - 2, y - 2, 5, 5);
152 /* todo: We could queue up more damage before committing, if there
153 * are more input events to handle.
155 wl_surface_commit(touch->surface);
159 touch_handle_down(void *data, struct wl_touch *wl_touch,
160 uint32_t serial, uint32_t time, struct wl_surface *surface,
161 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
163 struct touch *touch = data;
164 float x = wl_fixed_to_double(x_w);
165 float y = wl_fixed_to_double(y_w);
167 touch_paint(touch, x, y, id);
171 touch_handle_up(void *data, struct wl_touch *wl_touch,
172 uint32_t serial, uint32_t time, int32_t id)
177 touch_handle_motion(void *data, struct wl_touch *wl_touch,
178 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
180 struct touch *touch = data;
181 float x = wl_fixed_to_double(x_w);
182 float y = wl_fixed_to_double(y_w);
184 touch_paint(touch, x, y, id);
188 touch_handle_frame(void *data, struct wl_touch *wl_touch)
193 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
197 static const struct wl_touch_listener touch_listener = {
206 seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
207 enum wl_seat_capability caps)
209 struct seat *seat = data;
210 struct touch *touch = seat->touch;
212 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !seat->wl_touch) {
213 seat->wl_touch = wl_seat_get_touch(wl_seat);
214 wl_touch_set_user_data(seat->wl_touch, touch);
215 wl_touch_add_listener(seat->wl_touch, &touch_listener, touch);
216 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && seat->wl_touch) {
217 wl_touch_destroy(seat->wl_touch);
218 seat->wl_touch = NULL;
222 static const struct wl_seat_listener seat_listener = {
223 seat_handle_capabilities,
227 add_seat(struct touch *touch, uint32_t name, uint32_t version)
231 seat = malloc(sizeof *seat);
235 seat->wl_touch = NULL;
236 seat->seat = wl_registry_bind(touch->registry, name,
237 &wl_seat_interface, 1);
238 wl_seat_add_listener(seat->seat, &seat_listener, seat);
242 handle_ping(void *data, struct wl_shell_surface *shell_surface,
245 wl_shell_surface_pong(shell_surface, serial);
249 handle_configure(void *data, struct wl_shell_surface *shell_surface,
250 uint32_t edges, int32_t width, int32_t height)
255 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
259 static const struct wl_shell_surface_listener shell_surface_listener = {
266 handle_global(void *data, struct wl_registry *registry,
267 uint32_t name, const char *interface, uint32_t version)
269 struct touch *touch = data;
271 if (strcmp(interface, "wl_compositor") == 0) {
273 wl_registry_bind(registry, name,
274 &wl_compositor_interface, 1);
275 } else if (strcmp(interface, "wl_shell") == 0) {
277 wl_registry_bind(registry, name,
278 &wl_shell_interface, 1);
279 } else if (strcmp(interface, "wl_shm") == 0) {
280 touch->shm = wl_registry_bind(registry, name,
281 &wl_shm_interface, 1);
282 wl_shm_add_listener(touch->shm, &shm_listener, touch);
283 } else if (strcmp(interface, "wl_seat") == 0) {
284 add_seat(touch, name, version);
289 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
293 static const struct wl_registry_listener registry_listener = {
298 static struct touch *
299 touch_create(int width, int height)
303 touch = malloc(sizeof *touch);
305 fprintf(stderr, "out of memory\n");
308 touch->display = wl_display_connect(NULL);
309 assert(touch->display);
312 touch->registry = wl_display_get_registry(touch->display);
313 wl_registry_add_listener(touch->registry, ®istry_listener, touch);
314 wl_display_dispatch(touch->display);
315 wl_display_roundtrip(touch->display);
317 if (!touch->has_argb) {
318 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
322 touch->width = width;
323 touch->height = height;
324 touch->surface = wl_compositor_create_surface(touch->compositor);
325 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
327 create_shm_buffer(touch);
329 if (touch->shell_surface) {
330 wl_shell_surface_add_listener(touch->shell_surface,
331 &shell_surface_listener, touch);
332 wl_shell_surface_set_toplevel(touch->shell_surface);
335 wl_surface_set_user_data(touch->surface, touch);
336 wl_shell_surface_set_title(touch->shell_surface, "simple-touch");
338 memset(touch->data, 64, width * height * 4);
339 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
340 wl_surface_damage(touch->surface, 0, 0, width, height);
341 wl_surface_commit(touch->surface);
347 main(int argc, char **argv)
352 touch = touch_create(600, 500);
355 ret = wl_display_dispatch(touch->display);