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])
40 struct wl_display *display;
41 struct wl_registry *registry;
42 struct wl_compositor *compositor;
43 struct wl_shell *shell;
46 struct wl_touch *wl_touch;
47 struct wl_pointer *pointer;
48 struct wl_keyboard *keyboard;
49 struct wl_surface *surface;
50 struct wl_shell_surface *shell_surface;
51 struct wl_buffer *buffer;
58 create_shm_buffer(struct touch *touch)
60 struct wl_shm_pool *pool;
63 stride = touch->width * 4;
64 size = stride * touch->height;
66 fd = os_create_anonymous_file(size);
68 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
74 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
75 if (touch->data == MAP_FAILED) {
76 fprintf(stderr, "mmap failed: %m\n");
81 pool = wl_shm_create_pool(touch->shm, fd, size);
83 wl_shm_pool_create_buffer(pool, 0,
84 touch->width, touch->height, stride,
85 WL_SHM_FORMAT_ARGB8888);
86 wl_shm_pool_destroy(pool);
92 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
94 struct touch *touch = data;
96 if (format == WL_SHM_FORMAT_ARGB8888)
100 struct wl_shm_listener shm_listener = {
106 touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
109 static const uint32_t colors[] = {
118 if (id < (int32_t) ARRAY_LENGTH(colors))
123 if (x < 2 || x >= touch->width - 2 ||
124 y < 2 || y >= touch->height - 2)
127 p = (uint32_t *) touch->data + (x - 2) + (y - 2) * touch->width;
146 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
147 wl_surface_damage(touch->surface, x - 2, y - 2, 5, 5);
148 /* todo: We could queue up more damage before committing, if there
149 * are more input events to handle.
151 wl_surface_commit(touch->surface);
155 touch_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)
159 struct touch *touch = data;
160 float x = wl_fixed_to_double(x_w);
161 float y = wl_fixed_to_double(y_w);
163 touch_paint(touch, x, y, id);
167 touch_handle_up(void *data, struct wl_touch *wl_touch,
168 uint32_t serial, uint32_t time, int32_t id)
173 touch_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)
176 struct touch *touch = data;
177 float x = wl_fixed_to_double(x_w);
178 float y = wl_fixed_to_double(y_w);
180 touch_paint(touch, x, y, id);
184 touch_handle_frame(void *data, struct wl_touch *wl_touch)
189 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
193 static const struct wl_touch_listener touch_listener = {
202 seat_handle_capabilities(void *data, struct wl_seat *seat,
203 enum wl_seat_capability caps)
205 struct touch *touch = data;
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;
217 static const struct wl_seat_listener seat_listener = {
218 seat_handle_capabilities,
222 handle_ping(void *data, struct wl_shell_surface *shell_surface,
225 wl_shell_surface_pong(shell_surface, serial);
229 handle_configure(void *data, struct wl_shell_surface *shell_surface,
230 uint32_t edges, int32_t width, int32_t height)
235 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
239 static const struct wl_shell_surface_listener shell_surface_listener = {
246 handle_global(void *data, struct wl_registry *registry,
247 uint32_t name, const char *interface, uint32_t version)
249 struct touch *touch = data;
251 if (strcmp(interface, "wl_compositor") == 0) {
253 wl_registry_bind(registry, name,
254 &wl_compositor_interface, 1);
255 } else if (strcmp(interface, "wl_shell") == 0) {
257 wl_registry_bind(registry, name,
258 &wl_shell_interface, 1);
259 } else if (strcmp(interface, "wl_shm") == 0) {
260 touch->shm = wl_registry_bind(registry, name,
261 &wl_shm_interface, 1);
262 wl_shm_add_listener(touch->shm, &shm_listener, touch);
263 } else if (strcmp(interface, "wl_seat") == 0) {
264 touch->seat = wl_registry_bind(registry, name,
265 &wl_seat_interface, 1);
266 wl_seat_add_listener(touch->seat, &seat_listener, touch);
271 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
275 static const struct wl_registry_listener registry_listener = {
280 static struct touch *
281 touch_create(int width, int height)
285 touch = malloc(sizeof *touch);
287 fprintf(stderr, "out of memory\n");
290 touch->display = wl_display_connect(NULL);
291 assert(touch->display);
294 touch->registry = wl_display_get_registry(touch->display);
295 wl_registry_add_listener(touch->registry, ®istry_listener, touch);
296 wl_display_dispatch(touch->display);
297 wl_display_roundtrip(touch->display);
299 if (!touch->has_argb) {
300 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
304 touch->width = width;
305 touch->height = height;
306 touch->surface = wl_compositor_create_surface(touch->compositor);
307 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
309 create_shm_buffer(touch);
311 if (touch->shell_surface) {
312 wl_shell_surface_add_listener(touch->shell_surface,
313 &shell_surface_listener, touch);
314 wl_shell_surface_set_toplevel(touch->shell_surface);
317 wl_surface_set_user_data(touch->surface, touch);
318 wl_shell_surface_set_title(touch->shell_surface, "simple-touch");
320 memset(touch->data, 64, width * height * 4);
321 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
322 wl_surface_damage(touch->surface, 0, 0, width, height);
323 wl_surface_commit(touch->surface);
329 main(int argc, char **argv)
334 touch = touch_create(600, 500);
337 ret = wl_display_dispatch(touch->display);