blob: c3ee877933eb0f7ab6e20bdb0427020ff3a00948 [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,
113 uint32_t time, uint32_t button, uint32_t state)
114{
115}
116
117static void
Scott Moreau210d0792012-03-22 10:47:01 -0600118input_device_handle_axis(void *data, struct wl_input_device *input_device,
119 uint32_t time, uint32_t axis, int32_t value)
120{
121}
122
123static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500124input_device_handle_key(void *data, struct wl_input_device *input_device,
125 uint32_t time, uint32_t key, uint32_t state)
126{
127}
128
129static void
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500130input_device_handle_pointer_enter(void *data,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500131 struct wl_input_device *input_device,
132 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200133 int32_t sx, int32_t sy)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500134{
135}
136
137static void
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500138input_device_handle_pointer_leave(void *data,
139 struct wl_input_device *input_device,
140 uint32_t time, struct wl_surface *surface)
141{
142}
143
144static void
145input_device_handle_keyboard_enter(void *data,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500146 struct wl_input_device *input_device,
147 uint32_t time,
148 struct wl_surface *surface,
149 struct wl_array *keys)
150{
151}
152
153static void
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500154input_device_handle_keyboard_leave(void *data,
155 struct wl_input_device *input_device,
156 uint32_t time,
157 struct wl_surface *surface)
158{
159}
160
161static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500162touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
163{
164 uint32_t *p, c;
165 static const uint32_t colors[] = {
166 0xffff0000,
167 0xffffff00,
168 0xff0000ff,
169 0xffff00ff,
170 };
171
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400172 if (id < (int32_t) ARRAY_LENGTH(colors))
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500173 c = colors[id];
174 else
175 c = 0xffffffff;
176
177 if (x < 1 || touch->width - 1 < x ||
178 y < 1 || touch->height - 1 < y)
179 return;
180
181 p = (uint32_t *) touch->data + (x - 1) + (y -1 ) * touch->width;
182 p[1] = c;
183 p += touch->width;
184 p[0] = c;
185 p[1] = c;
186 p[2] = c;
187 p += touch->width;
188 p[1] = c;
189
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400190 wl_surface_damage(touch->surface, 0, 0, touch->width, touch->height);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500191}
192
193static void
194input_device_handle_touch_down(void *data,
195 struct wl_input_device *wl_input_device,
196 uint32_t time, struct wl_surface *surface,
197 int32_t id, int32_t x, int32_t y)
198{
199 struct touch *touch = data;
200
201 touch_paint(touch, x, y, id);
202}
203
204static void
205input_device_handle_touch_up(void *data,
206 struct wl_input_device *wl_input_device,
207 uint32_t time, int32_t id)
208{
209}
210
211static void
212input_device_handle_touch_motion(void *data,
213 struct wl_input_device *wl_input_device,
214 uint32_t time,
215 int32_t id, int32_t x, int32_t y)
216{
217 struct touch *touch = data;
218
219 touch_paint(touch, x, y, id);
220}
221
222static void
223input_device_handle_touch_frame(void *data,
224 struct wl_input_device *wl_input_device)
225{
226}
227
228static void
229input_device_handle_touch_cancel(void *data,
230 struct wl_input_device *wl_input_device)
231{
232}
233
234static const struct wl_input_device_listener input_device_listener = {
235 input_device_handle_motion,
236 input_device_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -0600237 input_device_handle_axis,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500238 input_device_handle_key,
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500239 input_device_handle_pointer_enter,
240 input_device_handle_pointer_leave,
241 input_device_handle_keyboard_enter,
242 input_device_handle_keyboard_leave,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500243 input_device_handle_touch_down,
244 input_device_handle_touch_up,
245 input_device_handle_touch_motion,
246 input_device_handle_touch_frame,
247 input_device_handle_touch_cancel,
248};
249
250static void
251handle_global(struct wl_display *display, uint32_t id,
252 const char *interface, uint32_t version, void *data)
253{
254 struct touch *touch = data;
255
256 if (strcmp(interface, "wl_compositor") == 0) {
257 touch->compositor =
258 wl_display_bind(display, id, &wl_compositor_interface);
259 } else if (strcmp(interface, "wl_shell") == 0) {
260 touch->shell =
261 wl_display_bind(display, id, &wl_shell_interface);
262 } else if (strcmp(interface, "wl_shm") == 0) {
263 touch->shm = wl_display_bind(display, id, &wl_shm_interface);
264 wl_shm_add_listener(touch->shm, &shm_listenter, touch);
265 } else if (strcmp(interface, "wl_input_device") == 0) {
266 touch->input_device =
267 wl_display_bind(display, id,
268 &wl_input_device_interface);
269 wl_input_device_add_listener(touch->input_device,
270 &input_device_listener, touch);
271 }
272}
273
274static int
275event_mask_update(uint32_t mask, void *data)
276{
277 struct touch *touch = data;
278
279 touch->mask = mask;
280
281 return 0;
282}
283
284static struct touch *
285touch_create(int width, int height)
286{
287 struct touch *touch;
288
289 touch = malloc(sizeof *touch);
290 touch->display = wl_display_connect(NULL);
291 assert(touch->display);
292
293 touch->has_argb = 0;
294 wl_display_add_global_listener(touch->display, handle_global, touch);
295 wl_display_iterate(touch->display, WL_DISPLAY_READABLE);
296 wl_display_roundtrip(touch->display);
297
298 if (!touch->has_argb) {
299 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
300 exit(1);
301 }
302
303 wl_display_get_fd(touch->display, event_mask_update, touch);
304
305 touch->width = width;
306 touch->height = height;
307 touch->surface = wl_compositor_create_surface(touch->compositor);
308 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
309 touch->surface);
310 create_shm_buffer(touch);
311
312 wl_shell_surface_set_toplevel(touch->shell_surface);
313 wl_surface_set_user_data(touch->surface, touch);
314
315 memset(touch->data, 64, width * height * 4);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500316 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
317 wl_surface_damage(touch->surface, 0, 0, width, height);
318
319 return touch;
320}
321
322int
323main(int argc, char **argv)
324{
325 struct touch *touch;
326
327 touch = touch_create(600, 500);
328
329 while (true)
330 wl_display_iterate(touch->display, touch->mask);
331
332 return 0;
333}