blob: bd5226a974e484417e1ad45338f12507323e2658 [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
Daniel Stone103db7f2012-05-08 17:17:55 +010032#include <GLES2/gl2.h>
Kristian Høgsberg558949b2011-12-21 22:54:49 -050033#include <wayland-client.h>
34#include <wayland-egl.h>
35
36struct touch {
37 struct wl_display *display;
38 struct wl_compositor *compositor;
39 struct wl_shell *shell;
40 struct wl_shm *shm;
41 struct wl_input_device *input_device;
42 struct wl_surface *surface;
43 struct wl_shell_surface *shell_surface;
44 struct wl_buffer *buffer;
45 int has_argb;
46 uint32_t mask;
47 int width, height;
48 void *data;
49};
50
51static void
52create_shm_buffer(struct touch *touch)
53{
Kristian Høgsberg16626282012-04-03 11:21:27 -040054 struct wl_shm_pool *pool;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050055 char filename[] = "/tmp/wayland-shm-XXXXXX";
56 int fd, size, stride;
57
58 fd = mkstemp(filename);
59 if (fd < 0) {
60 fprintf(stderr, "open %s failed: %m\n", filename);
61 exit(1);
62 }
63 stride = touch->width * 4;
64 size = stride * touch->height;
65 if (ftruncate(fd, size) < 0) {
66 fprintf(stderr, "ftruncate failed: %m\n");
67 close(fd);
68 exit(1);
69 }
70
71 touch->data =
72 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
73 unlink(filename);
74
75 if (touch->data == MAP_FAILED) {
76 fprintf(stderr, "mmap failed: %m\n");
77 close(fd);
78 exit(1);
79 }
80
Kristian Høgsberg16626282012-04-03 11:21:27 -040081 pool = wl_shm_create_pool(touch->shm, fd, size);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050082 touch->buffer =
Kristian Høgsberg16626282012-04-03 11:21:27 -040083 wl_shm_pool_create_buffer(pool, 0,
84 touch->width, touch->height, stride,
85 WL_SHM_FORMAT_ARGB8888);
86 wl_shm_pool_destroy(pool);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050087
88 close(fd);
89}
90
91static void
92shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
93{
94 struct touch *touch = data;
95
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050096 if (format == WL_SHM_FORMAT_ARGB8888)
Kristian Høgsberg558949b2011-12-21 22:54:49 -050097 touch->has_argb = 1;
98}
99
100struct wl_shm_listener shm_listenter = {
101 shm_format
102};
103
104
105static void
106input_device_handle_motion(void *data, struct wl_input_device *input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +0100107 uint32_t time,
108 wl_fixed_t sx_w,
109 wl_fixed_t sy_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500110{
111}
112
113static void
114input_device_handle_button(void *data,
115 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400116 uint32_t serial, uint32_t time,
117 uint32_t button, uint32_t state)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500118{
119}
120
121static void
Scott Moreau210d0792012-03-22 10:47:01 -0600122input_device_handle_axis(void *data, struct wl_input_device *input_device,
123 uint32_t time, uint32_t axis, int32_t value)
124{
125}
126
127static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500128input_device_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400129 uint32_t serial, uint32_t time,
130 uint32_t key, uint32_t state)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500131{
132}
133
134static void
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500135input_device_handle_pointer_enter(void *data,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500136 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400137 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +0100138 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500139{
140}
141
142static void
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500143input_device_handle_pointer_leave(void *data,
144 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400145 uint32_t serial, struct wl_surface *surface)
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500146{
147}
148
149static void
150input_device_handle_keyboard_enter(void *data,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500151 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400152 uint32_t serial,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500153 struct wl_surface *surface,
154 struct wl_array *keys)
155{
156}
157
158static void
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500159input_device_handle_keyboard_leave(void *data,
160 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400161 uint32_t serial,
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500162 struct wl_surface *surface)
163{
164}
165
166static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500167touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
168{
169 uint32_t *p, c;
170 static const uint32_t colors[] = {
171 0xffff0000,
172 0xffffff00,
173 0xff0000ff,
174 0xffff00ff,
175 };
176
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400177 if (id < (int32_t) ARRAY_LENGTH(colors))
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500178 c = colors[id];
179 else
180 c = 0xffffffff;
181
182 if (x < 1 || touch->width - 1 < x ||
183 y < 1 || touch->height - 1 < y)
184 return;
185
186 p = (uint32_t *) touch->data + (x - 1) + (y -1 ) * touch->width;
187 p[1] = c;
188 p += touch->width;
189 p[0] = c;
190 p[1] = c;
191 p[2] = c;
192 p += touch->width;
193 p[1] = c;
194
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400195 wl_surface_damage(touch->surface, 0, 0, touch->width, touch->height);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500196}
197
198static void
199input_device_handle_touch_down(void *data,
200 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400201 uint32_t serial, uint32_t time,
202 struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +0100203 int32_t id,
204 wl_fixed_t x_w,
205 wl_fixed_t y_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500206{
207 struct touch *touch = data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400208 float x = wl_fixed_to_double(x_w);
209 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500210
211 touch_paint(touch, x, y, id);
212}
213
214static void
215input_device_handle_touch_up(void *data,
216 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400217 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500218{
219}
220
221static void
222input_device_handle_touch_motion(void *data,
223 struct wl_input_device *wl_input_device,
224 uint32_t time,
Daniel Stone103db7f2012-05-08 17:17:55 +0100225 int32_t id,
226 wl_fixed_t x_w,
227 wl_fixed_t y_w)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500228{
229 struct touch *touch = data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400230 float x = wl_fixed_to_double(x_w);
231 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500232
233 touch_paint(touch, x, y, id);
234}
235
236static void
237input_device_handle_touch_frame(void *data,
238 struct wl_input_device *wl_input_device)
239{
240}
241
242static void
243input_device_handle_touch_cancel(void *data,
244 struct wl_input_device *wl_input_device)
245{
246}
247
248static const struct wl_input_device_listener input_device_listener = {
249 input_device_handle_motion,
250 input_device_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -0600251 input_device_handle_axis,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500252 input_device_handle_key,
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500253 input_device_handle_pointer_enter,
254 input_device_handle_pointer_leave,
255 input_device_handle_keyboard_enter,
256 input_device_handle_keyboard_leave,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500257 input_device_handle_touch_down,
258 input_device_handle_touch_up,
259 input_device_handle_touch_motion,
260 input_device_handle_touch_frame,
261 input_device_handle_touch_cancel,
262};
263
264static void
265handle_global(struct wl_display *display, uint32_t id,
266 const char *interface, uint32_t version, void *data)
267{
268 struct touch *touch = data;
269
270 if (strcmp(interface, "wl_compositor") == 0) {
271 touch->compositor =
272 wl_display_bind(display, id, &wl_compositor_interface);
273 } else if (strcmp(interface, "wl_shell") == 0) {
274 touch->shell =
275 wl_display_bind(display, id, &wl_shell_interface);
276 } else if (strcmp(interface, "wl_shm") == 0) {
277 touch->shm = wl_display_bind(display, id, &wl_shm_interface);
278 wl_shm_add_listener(touch->shm, &shm_listenter, touch);
279 } else if (strcmp(interface, "wl_input_device") == 0) {
280 touch->input_device =
281 wl_display_bind(display, id,
282 &wl_input_device_interface);
283 wl_input_device_add_listener(touch->input_device,
284 &input_device_listener, touch);
285 }
286}
287
288static int
289event_mask_update(uint32_t mask, void *data)
290{
291 struct touch *touch = data;
292
293 touch->mask = mask;
294
295 return 0;
296}
297
298static struct touch *
299touch_create(int width, int height)
300{
301 struct touch *touch;
302
303 touch = malloc(sizeof *touch);
304 touch->display = wl_display_connect(NULL);
305 assert(touch->display);
306
307 touch->has_argb = 0;
308 wl_display_add_global_listener(touch->display, handle_global, touch);
309 wl_display_iterate(touch->display, WL_DISPLAY_READABLE);
310 wl_display_roundtrip(touch->display);
311
312 if (!touch->has_argb) {
313 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
314 exit(1);
315 }
316
317 wl_display_get_fd(touch->display, event_mask_update, touch);
318
319 touch->width = width;
320 touch->height = height;
321 touch->surface = wl_compositor_create_surface(touch->compositor);
322 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
323 touch->surface);
324 create_shm_buffer(touch);
325
326 wl_shell_surface_set_toplevel(touch->shell_surface);
327 wl_surface_set_user_data(touch->surface, touch);
328
329 memset(touch->data, 64, width * height * 4);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500330 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
331 wl_surface_damage(touch->surface, 0, 0, width, height);
332
333 return touch;
334}
335
336int
337main(int argc, char **argv)
338{
339 struct touch *touch;
340
341 touch = touch_create(600, 500);
342
343 while (true)
344 wl_display_iterate(touch->display, touch->mask);
345
346 return 0;
347}