blob: 0e2bac60bf0486c88e78ab20e0fabc9ad1ba6cda [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{
53 char filename[] = "/tmp/wayland-shm-XXXXXX";
54 int fd, size, stride;
55
56 fd = mkstemp(filename);
57 if (fd < 0) {
58 fprintf(stderr, "open %s failed: %m\n", filename);
59 exit(1);
60 }
61 stride = touch->width * 4;
62 size = stride * touch->height;
63 if (ftruncate(fd, size) < 0) {
64 fprintf(stderr, "ftruncate failed: %m\n");
65 close(fd);
66 exit(1);
67 }
68
69 touch->data =
70 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
71 unlink(filename);
72
73 if (touch->data == MAP_FAILED) {
74 fprintf(stderr, "mmap failed: %m\n");
75 close(fd);
76 exit(1);
77 }
78
79 touch->buffer =
80 wl_shm_create_buffer(touch->shm, fd,
81 touch->width, touch->height, stride,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050082 WL_SHM_FORMAT_ARGB8888);
Kristian Høgsberg558949b2011-12-21 22:54:49 -050083
84 close(fd);
85}
86
87static void
88shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
89{
90 struct touch *touch = data;
91
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050092 if (format == WL_SHM_FORMAT_ARGB8888)
Kristian Høgsberg558949b2011-12-21 22:54:49 -050093 touch->has_argb = 1;
94}
95
96struct wl_shm_listener shm_listenter = {
97 shm_format
98};
99
100
101static void
102input_device_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200103 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500104{
105}
106
107static void
108input_device_handle_button(void *data,
109 struct wl_input_device *input_device,
110 uint32_t time, uint32_t button, uint32_t state)
111{
112}
113
114static void
Scott Moreau210d0792012-03-22 10:47:01 -0600115input_device_handle_axis(void *data, struct wl_input_device *input_device,
116 uint32_t time, uint32_t axis, int32_t value)
117{
118}
119
120static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500121input_device_handle_key(void *data, struct wl_input_device *input_device,
122 uint32_t time, uint32_t key, uint32_t state)
123{
124}
125
126static void
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500127input_device_handle_pointer_enter(void *data,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500128 struct wl_input_device *input_device,
129 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200130 int32_t sx, int32_t sy)
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_leave(void *data,
136 struct wl_input_device *input_device,
137 uint32_t time, struct wl_surface *surface)
138{
139}
140
141static void
142input_device_handle_keyboard_enter(void *data,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500143 struct wl_input_device *input_device,
144 uint32_t time,
145 struct wl_surface *surface,
146 struct wl_array *keys)
147{
148}
149
150static void
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500151input_device_handle_keyboard_leave(void *data,
152 struct wl_input_device *input_device,
153 uint32_t time,
154 struct wl_surface *surface)
155{
156}
157
158static void
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500159touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
160{
161 uint32_t *p, c;
162 static const uint32_t colors[] = {
163 0xffff0000,
164 0xffffff00,
165 0xff0000ff,
166 0xffff00ff,
167 };
168
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400169 if (id < (int32_t) ARRAY_LENGTH(colors))
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500170 c = colors[id];
171 else
172 c = 0xffffffff;
173
174 if (x < 1 || touch->width - 1 < x ||
175 y < 1 || touch->height - 1 < y)
176 return;
177
178 p = (uint32_t *) touch->data + (x - 1) + (y -1 ) * touch->width;
179 p[1] = c;
180 p += touch->width;
181 p[0] = c;
182 p[1] = c;
183 p[2] = c;
184 p += touch->width;
185 p[1] = c;
186
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400187 wl_surface_damage(touch->surface, 0, 0, touch->width, touch->height);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500188}
189
190static void
191input_device_handle_touch_down(void *data,
192 struct wl_input_device *wl_input_device,
193 uint32_t time, struct wl_surface *surface,
194 int32_t id, int32_t x, int32_t y)
195{
196 struct touch *touch = data;
197
198 touch_paint(touch, x, y, id);
199}
200
201static void
202input_device_handle_touch_up(void *data,
203 struct wl_input_device *wl_input_device,
204 uint32_t time, int32_t id)
205{
206}
207
208static void
209input_device_handle_touch_motion(void *data,
210 struct wl_input_device *wl_input_device,
211 uint32_t time,
212 int32_t id, int32_t x, int32_t y)
213{
214 struct touch *touch = data;
215
216 touch_paint(touch, x, y, id);
217}
218
219static void
220input_device_handle_touch_frame(void *data,
221 struct wl_input_device *wl_input_device)
222{
223}
224
225static void
226input_device_handle_touch_cancel(void *data,
227 struct wl_input_device *wl_input_device)
228{
229}
230
231static const struct wl_input_device_listener input_device_listener = {
232 input_device_handle_motion,
233 input_device_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -0600234 input_device_handle_axis,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500235 input_device_handle_key,
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500236 input_device_handle_pointer_enter,
237 input_device_handle_pointer_leave,
238 input_device_handle_keyboard_enter,
239 input_device_handle_keyboard_leave,
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500240 input_device_handle_touch_down,
241 input_device_handle_touch_up,
242 input_device_handle_touch_motion,
243 input_device_handle_touch_frame,
244 input_device_handle_touch_cancel,
245};
246
247static void
248handle_global(struct wl_display *display, uint32_t id,
249 const char *interface, uint32_t version, void *data)
250{
251 struct touch *touch = data;
252
253 if (strcmp(interface, "wl_compositor") == 0) {
254 touch->compositor =
255 wl_display_bind(display, id, &wl_compositor_interface);
256 } else if (strcmp(interface, "wl_shell") == 0) {
257 touch->shell =
258 wl_display_bind(display, id, &wl_shell_interface);
259 } else if (strcmp(interface, "wl_shm") == 0) {
260 touch->shm = wl_display_bind(display, id, &wl_shm_interface);
261 wl_shm_add_listener(touch->shm, &shm_listenter, touch);
262 } else if (strcmp(interface, "wl_input_device") == 0) {
263 touch->input_device =
264 wl_display_bind(display, id,
265 &wl_input_device_interface);
266 wl_input_device_add_listener(touch->input_device,
267 &input_device_listener, touch);
268 }
269}
270
271static int
272event_mask_update(uint32_t mask, void *data)
273{
274 struct touch *touch = data;
275
276 touch->mask = mask;
277
278 return 0;
279}
280
281static struct touch *
282touch_create(int width, int height)
283{
284 struct touch *touch;
285
286 touch = malloc(sizeof *touch);
287 touch->display = wl_display_connect(NULL);
288 assert(touch->display);
289
290 touch->has_argb = 0;
291 wl_display_add_global_listener(touch->display, handle_global, touch);
292 wl_display_iterate(touch->display, WL_DISPLAY_READABLE);
293 wl_display_roundtrip(touch->display);
294
295 if (!touch->has_argb) {
296 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
297 exit(1);
298 }
299
300 wl_display_get_fd(touch->display, event_mask_update, touch);
301
302 touch->width = width;
303 touch->height = height;
304 touch->surface = wl_compositor_create_surface(touch->compositor);
305 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
306 touch->surface);
307 create_shm_buffer(touch);
308
309 wl_shell_surface_set_toplevel(touch->shell_surface);
310 wl_surface_set_user_data(touch->surface, touch);
311
312 memset(touch->data, 64, width * height * 4);
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500313 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
314 wl_surface_damage(touch->surface, 0, 0, width, height);
315
316 return touch;
317}
318
319int
320main(int argc, char **argv)
321{
322 struct touch *touch;
323
324 touch = touch_create(600, 500);
325
326 while (true)
327 wl_display_iterate(touch->display, touch->mask);
328
329 return 0;
330}