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 <GLES2/gl2.h>
33 #include <wayland-client.h>
34 #include <wayland-egl.h>
35 #include "../shared/os-compatibility.h"
38 struct wl_display *display;
39 struct wl_compositor *compositor;
40 struct wl_shell *shell;
43 struct wl_touch *wl_touch;
44 struct wl_pointer *pointer;
45 struct wl_keyboard *keyboard;
46 struct wl_surface *surface;
47 struct wl_shell_surface *shell_surface;
48 struct wl_buffer *buffer;
56 create_shm_buffer(struct touch *touch)
58 struct wl_shm_pool *pool;
61 stride = touch->width * 4;
62 size = stride * touch->height;
64 fd = os_create_anonymous_file(size);
66 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
72 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
73 if (touch->data == MAP_FAILED) {
74 fprintf(stderr, "mmap failed: %m\n");
79 pool = wl_shm_create_pool(touch->shm, fd, size);
81 wl_shm_pool_create_buffer(pool, 0,
82 touch->width, touch->height, stride,
83 WL_SHM_FORMAT_ARGB8888);
84 wl_shm_pool_destroy(pool);
90 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
92 struct touch *touch = data;
94 if (format == WL_SHM_FORMAT_ARGB8888)
98 struct wl_shm_listener shm_listenter = {
104 touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
107 static const uint32_t colors[] = {
114 if (id < (int32_t) ARRAY_LENGTH(colors))
119 if (x < 1 || touch->width - 1 < x ||
120 y < 1 || touch->height - 1 < y)
123 p = (uint32_t *) touch->data + (x - 1) + (y -1 ) * touch->width;
132 wl_surface_damage(touch->surface, 0, 0, touch->width, touch->height);
136 touch_handle_down(void *data, struct wl_touch *wl_touch,
137 uint32_t serial, uint32_t time, struct wl_surface *surface,
138 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
140 struct touch *touch = data;
141 float x = wl_fixed_to_double(x_w);
142 float y = wl_fixed_to_double(y_w);
144 touch_paint(touch, x, y, id);
148 touch_handle_up(void *data, struct wl_touch *wl_touch,
149 uint32_t serial, uint32_t time, int32_t id)
154 touch_handle_motion(void *data, struct wl_touch *wl_touch,
155 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
157 struct touch *touch = data;
158 float x = wl_fixed_to_double(x_w);
159 float y = wl_fixed_to_double(y_w);
161 touch_paint(touch, x, y, id);
165 touch_handle_frame(void *data, struct wl_touch *wl_touch)
170 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
174 static const struct wl_touch_listener touch_listener = {
183 seat_handle_capabilities(void *data, struct wl_seat *seat,
184 enum wl_seat_capability caps)
186 struct touch *touch = data;
188 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !touch->wl_touch) {
189 touch->wl_touch = wl_seat_get_touch(seat);
190 wl_touch_set_user_data(touch->wl_touch, touch);
191 wl_touch_add_listener(touch->wl_touch, &touch_listener, touch);
192 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && touch->wl_touch) {
193 wl_touch_destroy(touch->wl_touch);
194 touch->wl_touch = NULL;
198 static const struct wl_seat_listener seat_listener = {
199 seat_handle_capabilities,
203 handle_global(struct wl_display *display, uint32_t id,
204 const char *interface, uint32_t version, void *data)
206 struct touch *touch = data;
208 if (strcmp(interface, "wl_compositor") == 0) {
210 wl_display_bind(display, id, &wl_compositor_interface);
211 } else if (strcmp(interface, "wl_shell") == 0) {
213 wl_display_bind(display, id, &wl_shell_interface);
214 } else if (strcmp(interface, "wl_shm") == 0) {
215 touch->shm = wl_display_bind(display, id, &wl_shm_interface);
216 wl_shm_add_listener(touch->shm, &shm_listenter, touch);
217 } else if (strcmp(interface, "wl_seat") == 0) {
218 touch->seat = wl_display_bind(display, id, &wl_seat_interface);
219 wl_seat_add_listener(touch->seat, &seat_listener, touch);
224 event_mask_update(uint32_t mask, void *data)
226 struct touch *touch = data;
233 static struct touch *
234 touch_create(int width, int height)
238 touch = malloc(sizeof *touch);
239 touch->display = wl_display_connect(NULL);
240 assert(touch->display);
243 wl_display_add_global_listener(touch->display, handle_global, touch);
244 wl_display_iterate(touch->display, WL_DISPLAY_READABLE);
245 wl_display_roundtrip(touch->display);
247 if (!touch->has_argb) {
248 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
252 wl_display_get_fd(touch->display, event_mask_update, touch);
254 touch->width = width;
255 touch->height = height;
256 touch->surface = wl_compositor_create_surface(touch->compositor);
257 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
259 create_shm_buffer(touch);
261 wl_shell_surface_set_toplevel(touch->shell_surface);
262 wl_surface_set_user_data(touch->surface, touch);
264 memset(touch->data, 64, width * height * 4);
265 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
266 wl_surface_damage(touch->surface, 0, 0, width, height);
272 main(int argc, char **argv)
276 touch = touch_create(600, 500);
279 wl_display_iterate(touch->display, touch->mask);