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
32 #include <wayland-client.h>
33 #include <wayland-egl.h>
36 struct wl_display *display;
37 struct wl_compositor *compositor;
38 struct wl_shell *shell;
40 struct wl_input_device *input_device;
41 struct wl_surface *surface;
42 struct wl_shell_surface *shell_surface;
43 struct wl_buffer *buffer;
51 create_shm_buffer(struct touch *touch)
53 char filename[] = "/tmp/wayland-shm-XXXXXX";
56 fd = mkstemp(filename);
58 fprintf(stderr, "open %s failed: %m\n", filename);
61 stride = touch->width * 4;
62 size = stride * touch->height;
63 if (ftruncate(fd, size) < 0) {
64 fprintf(stderr, "ftruncate failed: %m\n");
70 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
73 if (touch->data == MAP_FAILED) {
74 fprintf(stderr, "mmap failed: %m\n");
80 wl_shm_create_buffer(touch->shm, fd,
81 touch->width, touch->height, stride,
82 WL_SHM_FORMAT_ARGB8888);
88 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
90 struct touch *touch = data;
92 if (format == WL_SHM_FORMAT_ARGB8888)
96 struct wl_shm_listener shm_listenter = {
102 input_device_handle_motion(void *data, struct wl_input_device *input_device,
103 uint32_t time, int32_t sx, int32_t sy)
108 input_device_handle_button(void *data,
109 struct wl_input_device *input_device,
110 uint32_t time, uint32_t button, uint32_t state)
115 input_device_handle_axis(void *data, struct wl_input_device *input_device,
116 uint32_t time, uint32_t axis, int32_t value)
121 input_device_handle_key(void *data, struct wl_input_device *input_device,
122 uint32_t time, uint32_t key, uint32_t state)
127 input_device_handle_pointer_enter(void *data,
128 struct wl_input_device *input_device,
129 uint32_t time, struct wl_surface *surface,
130 int32_t sx, int32_t sy)
135 input_device_handle_pointer_leave(void *data,
136 struct wl_input_device *input_device,
137 uint32_t time, struct wl_surface *surface)
142 input_device_handle_keyboard_enter(void *data,
143 struct wl_input_device *input_device,
145 struct wl_surface *surface,
146 struct wl_array *keys)
151 input_device_handle_keyboard_leave(void *data,
152 struct wl_input_device *input_device,
154 struct wl_surface *surface)
159 touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
162 static const uint32_t colors[] = {
169 if (id < ARRAY_LENGTH(colors))
174 if (x < 1 || touch->width - 1 < x ||
175 y < 1 || touch->height - 1 < y)
178 p = (uint32_t *) touch->data + (x - 1) + (y -1 ) * touch->width;
187 wl_buffer_damage(touch->buffer, 0, 0, touch->width, touch->height);
188 wl_surface_damage(touch->surface,
189 0, 0, touch->width, touch->height);
193 input_device_handle_touch_down(void *data,
194 struct wl_input_device *wl_input_device,
195 uint32_t time, struct wl_surface *surface,
196 int32_t id, int32_t x, int32_t y)
198 struct touch *touch = data;
200 touch_paint(touch, x, y, id);
204 input_device_handle_touch_up(void *data,
205 struct wl_input_device *wl_input_device,
206 uint32_t time, int32_t id)
211 input_device_handle_touch_motion(void *data,
212 struct wl_input_device *wl_input_device,
214 int32_t id, int32_t x, int32_t y)
216 struct touch *touch = data;
218 touch_paint(touch, x, y, id);
222 input_device_handle_touch_frame(void *data,
223 struct wl_input_device *wl_input_device)
228 input_device_handle_touch_cancel(void *data,
229 struct wl_input_device *wl_input_device)
233 static const struct wl_input_device_listener input_device_listener = {
234 input_device_handle_motion,
235 input_device_handle_button,
236 input_device_handle_axis,
237 input_device_handle_key,
238 input_device_handle_pointer_enter,
239 input_device_handle_pointer_leave,
240 input_device_handle_keyboard_enter,
241 input_device_handle_keyboard_leave,
242 input_device_handle_touch_down,
243 input_device_handle_touch_up,
244 input_device_handle_touch_motion,
245 input_device_handle_touch_frame,
246 input_device_handle_touch_cancel,
250 handle_global(struct wl_display *display, uint32_t id,
251 const char *interface, uint32_t version, void *data)
253 struct touch *touch = data;
255 if (strcmp(interface, "wl_compositor") == 0) {
257 wl_display_bind(display, id, &wl_compositor_interface);
258 } else if (strcmp(interface, "wl_shell") == 0) {
260 wl_display_bind(display, id, &wl_shell_interface);
261 } else if (strcmp(interface, "wl_shm") == 0) {
262 touch->shm = wl_display_bind(display, id, &wl_shm_interface);
263 wl_shm_add_listener(touch->shm, &shm_listenter, touch);
264 } else if (strcmp(interface, "wl_input_device") == 0) {
265 touch->input_device =
266 wl_display_bind(display, id,
267 &wl_input_device_interface);
268 wl_input_device_add_listener(touch->input_device,
269 &input_device_listener, touch);
274 event_mask_update(uint32_t mask, void *data)
276 struct touch *touch = data;
283 static struct touch *
284 touch_create(int width, int height)
288 touch = malloc(sizeof *touch);
289 touch->display = wl_display_connect(NULL);
290 assert(touch->display);
293 wl_display_add_global_listener(touch->display, handle_global, touch);
294 wl_display_iterate(touch->display, WL_DISPLAY_READABLE);
295 wl_display_roundtrip(touch->display);
297 if (!touch->has_argb) {
298 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
302 wl_display_get_fd(touch->display, event_mask_update, touch);
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 wl_shell_surface_set_toplevel(touch->shell_surface);
312 wl_surface_set_user_data(touch->surface, touch);
314 memset(touch->data, 64, width * height * 4);
315 wl_buffer_damage(touch->buffer, 0, 0, width, height);
316 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
317 wl_surface_damage(touch->surface, 0, 0, width, height);
323 main(int argc, char **argv)
327 touch = touch_create(600, 500);
330 wl_display_iterate(touch->display, touch->mask);