blob: b8d915628e9b41e55dcaa3646efdacc48bcbbf97 [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
32#include <wayland-client.h>
33#include <wayland-egl.h>
34
35struct touch {
36 struct wl_display *display;
37 struct wl_compositor *compositor;
38 struct wl_shell *shell;
39 struct wl_shm *shm;
40 struct wl_input_device *input_device;
41 struct wl_surface *surface;
42 struct wl_shell_surface *shell_surface;
43 struct wl_buffer *buffer;
44 int has_argb;
45 uint32_t mask;
46 int width, height;
47 void *data;
48};
49
50static void
51create_shm_buffer(struct touch *touch)
52{
Kristian Høgsberg16626282012-04-03 11:21:27 -040053 struct wl_shm_pool *pool;
Kristian Høgsberg558949b2011-12-21 22:54:49 -050054 char filename[] = "/tmp/wayland-shm-XXXXXX";
55 int fd, size, stride;
56
57 fd = mkstemp(filename);
58 if (fd < 0) {
59 fprintf(stderr, "open %s failed: %m\n", filename);
60 exit(1);
61 }
62 stride = touch->width * 4;
63 size = stride * touch->height;
64 if (ftruncate(fd, size) < 0) {
65 fprintf(stderr, "ftruncate failed: %m\n");
66 close(fd);
67 exit(1);
68 }
69
70 touch->data =
71 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
72 unlink(filename);
73
74 if (touch->data == MAP_FAILED) {
75 fprintf(stderr, "mmap failed: %m\n");
76 close(fd);
77 exit(1);
78 }
79
Kristian Høgsberg16626282012-04-03 11:21:27 -040080 pool = wl_shm_create_pool(touch->shm, fd, size);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050081 touch->buffer =
Kristian Høgsberg16626282012-04-03 11:21:27 -040082 wl_shm_pool_create_buffer(pool, 0,
83 touch->width, touch->height, stride,
84 WL_SHM_FORMAT_ARGB8888);
85 wl_shm_pool_destroy(pool);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050086
87 close(fd);
88}
89
90static void
91shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
92{
93 struct touch *touch = data;
94
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050095 if (format == WL_SHM_FORMAT_ARGB8888)
Kristian Høgsberg558949b2011-12-21 22:54:49 -050096 touch->has_argb = 1;
97}
98
99struct wl_shm_listener shm_listenter = {
100 shm_format
101};
102
103
104static void
105input_device_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200106 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500107{
108}
109
110static void
111input_device_handle_button(void *data,
112 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400113 uint32_t serial, uint32_t time,
114 uint32_t button, uint32_t state)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500115{
116}
117
118static void
Scott Moreau210d0792012-03-22 10:47:01 -0600119input_device_handle_axis(void *data, struct wl_input_device *input_device,
120 uint32_t time, uint32_t axis, int32_t value)
121{
122}
123
124static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500125input_device_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400126 uint32_t serial, uint32_t time,
127 uint32_t key, uint32_t state)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500128{
129}
130
131static void
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500132input_device_handle_pointer_enter(void *data,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500133 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400134 uint32_t serial, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200135 int32_t sx, int32_t sy)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500136{
137}
138
139static void
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500140input_device_handle_pointer_leave(void *data,
141 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400142 uint32_t serial, struct wl_surface *surface)
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500143{
144}
145
146static void
147input_device_handle_keyboard_enter(void *data,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500148 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400149 uint32_t serial,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500150 struct wl_surface *surface,
151 struct wl_array *keys)
152{
153}
154
155static void
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500156input_device_handle_keyboard_leave(void *data,
157 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400158 uint32_t serial,
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500159 struct wl_surface *surface)
160{
161}
162
163static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500164touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
165{
166 uint32_t *p, c;
167 static const uint32_t colors[] = {
168 0xffff0000,
169 0xffffff00,
170 0xff0000ff,
171 0xffff00ff,
172 };
173
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400174 if (id < (int32_t) ARRAY_LENGTH(colors))
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500175 c = colors[id];
176 else
177 c = 0xffffffff;
178
179 if (x < 1 || touch->width - 1 < x ||
180 y < 1 || touch->height - 1 < y)
181 return;
182
183 p = (uint32_t *) touch->data + (x - 1) + (y -1 ) * touch->width;
184 p[1] = c;
185 p += touch->width;
186 p[0] = c;
187 p[1] = c;
188 p[2] = c;
189 p += touch->width;
190 p[1] = c;
191
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400192 wl_surface_damage(touch->surface, 0, 0, touch->width, touch->height);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500193}
194
195static void
196input_device_handle_touch_down(void *data,
197 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400198 uint32_t serial, uint32_t time,
199 struct wl_surface *surface,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500200 int32_t id, int32_t x, int32_t y)
201{
202 struct touch *touch = data;
203
204 touch_paint(touch, x, y, id);
205}
206
207static void
208input_device_handle_touch_up(void *data,
209 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400210 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500211{
212}
213
214static void
215input_device_handle_touch_motion(void *data,
216 struct wl_input_device *wl_input_device,
217 uint32_t time,
218 int32_t id, int32_t x, int32_t y)
219{
220 struct touch *touch = data;
221
222 touch_paint(touch, x, y, id);
223}
224
225static void
226input_device_handle_touch_frame(void *data,
227 struct wl_input_device *wl_input_device)
228{
229}
230
231static void
232input_device_handle_touch_cancel(void *data,
233 struct wl_input_device *wl_input_device)
234{
235}
236
237static const struct wl_input_device_listener input_device_listener = {
238 input_device_handle_motion,
239 input_device_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -0600240 input_device_handle_axis,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500241 input_device_handle_key,
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500242 input_device_handle_pointer_enter,
243 input_device_handle_pointer_leave,
244 input_device_handle_keyboard_enter,
245 input_device_handle_keyboard_leave,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500246 input_device_handle_touch_down,
247 input_device_handle_touch_up,
248 input_device_handle_touch_motion,
249 input_device_handle_touch_frame,
250 input_device_handle_touch_cancel,
251};
252
253static void
254handle_global(struct wl_display *display, uint32_t id,
255 const char *interface, uint32_t version, void *data)
256{
257 struct touch *touch = data;
258
259 if (strcmp(interface, "wl_compositor") == 0) {
260 touch->compositor =
261 wl_display_bind(display, id, &wl_compositor_interface);
262 } else if (strcmp(interface, "wl_shell") == 0) {
263 touch->shell =
264 wl_display_bind(display, id, &wl_shell_interface);
265 } else if (strcmp(interface, "wl_shm") == 0) {
266 touch->shm = wl_display_bind(display, id, &wl_shm_interface);
267 wl_shm_add_listener(touch->shm, &shm_listenter, touch);
268 } else if (strcmp(interface, "wl_input_device") == 0) {
269 touch->input_device =
270 wl_display_bind(display, id,
271 &wl_input_device_interface);
272 wl_input_device_add_listener(touch->input_device,
273 &input_device_listener, touch);
274 }
275}
276
277static int
278event_mask_update(uint32_t mask, void *data)
279{
280 struct touch *touch = data;
281
282 touch->mask = mask;
283
284 return 0;
285}
286
287static struct touch *
288touch_create(int width, int height)
289{
290 struct touch *touch;
291
292 touch = malloc(sizeof *touch);
293 touch->display = wl_display_connect(NULL);
294 assert(touch->display);
295
296 touch->has_argb = 0;
297 wl_display_add_global_listener(touch->display, handle_global, touch);
298 wl_display_iterate(touch->display, WL_DISPLAY_READABLE);
299 wl_display_roundtrip(touch->display);
300
301 if (!touch->has_argb) {
302 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
303 exit(1);
304 }
305
306 wl_display_get_fd(touch->display, event_mask_update, touch);
307
308 touch->width = width;
309 touch->height = height;
310 touch->surface = wl_compositor_create_surface(touch->compositor);
311 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
312 touch->surface);
313 create_shm_buffer(touch);
314
315 wl_shell_surface_set_toplevel(touch->shell_surface);
316 wl_surface_set_user_data(touch->surface, touch);
317
318 memset(touch->data, 64, width * height * 4);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500319 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
320 wl_surface_damage(touch->surface, 0, 0, width, height);
321
322 return touch;
323}
324
325int
326main(int argc, char **argv)
327{
328 struct touch *touch;
329
330 touch = touch_create(600, 500);
331
332 while (true)
333 wl_display_iterate(touch->display, touch->mask);
334
335 return 0;
336}