1 /*
2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2011 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "config.h"
26
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdbool.h>
32 #include <assert.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <sys/mman.h>
36
37 #include <wayland-client.h>
38 #include "shared/helpers.h"
39 #include "shared/os-compatibility.h"
40
41 struct seat {
42 struct touch *touch;
43 struct wl_seat *seat;
44 struct wl_touch *wl_touch;
45 };
46
47 struct touch {
48 struct wl_display *display;
49 struct wl_registry *registry;
50 struct wl_compositor *compositor;
51 struct wl_shell *shell;
52 struct wl_shm *shm;
53 struct wl_pointer *pointer;
54 struct wl_keyboard *keyboard;
55 struct wl_surface *surface;
56 struct wl_shell_surface *shell_surface;
57 struct wl_buffer *buffer;
58 int has_argb;
59 int width, height;
60 void *data;
61 };
62
63 static void
create_shm_buffer(struct touch * touch)64 create_shm_buffer(struct touch *touch)
65 {
66 struct wl_shm_pool *pool;
67 int fd, size, stride;
68
69 stride = touch->width * 4;
70 size = stride * touch->height;
71
72 fd = os_create_anonymous_file(size);
73 if (fd < 0) {
74 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
75 size, strerror(errno));
76 exit(1);
77 }
78
79 touch->data =
80 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
81 if (touch->data == MAP_FAILED) {
82 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
83 close(fd);
84 exit(1);
85 }
86
87 pool = wl_shm_create_pool(touch->shm, fd, size);
88 touch->buffer =
89 wl_shm_pool_create_buffer(pool, 0,
90 touch->width, touch->height, stride,
91 WL_SHM_FORMAT_ARGB8888);
92 wl_shm_pool_destroy(pool);
93
94 close(fd);
95 }
96
97 static void
shm_format(void * data,struct wl_shm * wl_shm,uint32_t format)98 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
99 {
100 struct touch *touch = data;
101
102 if (format == WL_SHM_FORMAT_ARGB8888)
103 touch->has_argb = 1;
104 }
105
106 struct wl_shm_listener shm_listener = {
107 shm_format
108 };
109
110
111 static void
touch_paint(struct touch * touch,int32_t x,int32_t y,int32_t id)112 touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
113 {
114 uint32_t *p, c;
115 static const uint32_t colors[] = {
116 0xffff0000,
117 0xffffff00,
118 0xff0000ff,
119 0xffff00ff,
120 0xff00ff00,
121 0xff00ffff,
122 };
123
124 if (id < (int32_t) ARRAY_LENGTH(colors))
125 c = colors[id];
126 else
127 c = 0xffffffff;
128
129 if (x < 2 || x >= touch->width - 2 ||
130 y < 2 || y >= touch->height - 2)
131 return;
132
133 p = (uint32_t *) touch->data + (x - 2) + (y - 2) * touch->width;
134 p[2] = c;
135 p += touch->width;
136 p[1] = c;
137 p[2] = c;
138 p[3] = c;
139 p += touch->width;
140 p[0] = c;
141 p[1] = c;
142 p[2] = c;
143 p[3] = c;
144 p[4] = c;
145 p += touch->width;
146 p[1] = c;
147 p[2] = c;
148 p[3] = c;
149 p += touch->width;
150 p[2] = c;
151
152 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
153 wl_surface_damage(touch->surface, x - 2, y - 2, 5, 5);
154 /* todo: We could queue up more damage before committing, if there
155 * are more input events to handle.
156 */
157 wl_surface_commit(touch->surface);
158 }
159
160 static void
touch_handle_down(void * data,struct wl_touch * wl_touch,uint32_t serial,uint32_t time,struct wl_surface * surface,int32_t id,wl_fixed_t x_w,wl_fixed_t y_w)161 touch_handle_down(void *data, struct wl_touch *wl_touch,
162 uint32_t serial, uint32_t time, struct wl_surface *surface,
163 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
164 {
165 struct touch *touch = data;
166 float x = wl_fixed_to_double(x_w);
167 float y = wl_fixed_to_double(y_w);
168
169 touch_paint(touch, x, y, id);
170 }
171
172 static void
touch_handle_up(void * data,struct wl_touch * wl_touch,uint32_t serial,uint32_t time,int32_t id)173 touch_handle_up(void *data, struct wl_touch *wl_touch,
174 uint32_t serial, uint32_t time, int32_t id)
175 {
176 }
177
178 static void
touch_handle_motion(void * data,struct wl_touch * wl_touch,uint32_t time,int32_t id,wl_fixed_t x_w,wl_fixed_t y_w)179 touch_handle_motion(void *data, struct wl_touch *wl_touch,
180 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
181 {
182 struct touch *touch = data;
183 float x = wl_fixed_to_double(x_w);
184 float y = wl_fixed_to_double(y_w);
185
186 touch_paint(touch, x, y, id);
187 }
188
189 static void
touch_handle_frame(void * data,struct wl_touch * wl_touch)190 touch_handle_frame(void *data, struct wl_touch *wl_touch)
191 {
192 }
193
194 static void
touch_handle_cancel(void * data,struct wl_touch * wl_touch)195 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
196 {
197 }
198
199 static const struct wl_touch_listener touch_listener = {
200 touch_handle_down,
201 touch_handle_up,
202 touch_handle_motion,
203 touch_handle_frame,
204 touch_handle_cancel,
205 };
206
207 static void
seat_handle_capabilities(void * data,struct wl_seat * wl_seat,enum wl_seat_capability caps)208 seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
209 enum wl_seat_capability caps)
210 {
211 struct seat *seat = data;
212 struct touch *touch = seat->touch;
213
214 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !seat->wl_touch) {
215 seat->wl_touch = wl_seat_get_touch(wl_seat);
216 wl_touch_set_user_data(seat->wl_touch, touch);
217 wl_touch_add_listener(seat->wl_touch, &touch_listener, touch);
218 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && seat->wl_touch) {
219 wl_touch_destroy(seat->wl_touch);
220 seat->wl_touch = NULL;
221 }
222 }
223
224 static const struct wl_seat_listener seat_listener = {
225 seat_handle_capabilities,
226 };
227
228 static void
add_seat(struct touch * touch,uint32_t name,uint32_t version)229 add_seat(struct touch *touch, uint32_t name, uint32_t version)
230 {
231 struct seat *seat;
232
233 seat = malloc(sizeof *seat);
234 assert(seat);
235
236 seat->touch = touch;
237 seat->wl_touch = NULL;
238 seat->seat = wl_registry_bind(touch->registry, name,
239 &wl_seat_interface, 1);
240 wl_seat_add_listener(seat->seat, &seat_listener, seat);
241 }
242
243 static void
handle_ping(void * data,struct wl_shell_surface * shell_surface,uint32_t serial)244 handle_ping(void *data, struct wl_shell_surface *shell_surface,
245 uint32_t serial)
246 {
247 wl_shell_surface_pong(shell_surface, serial);
248 }
249
250 static void
handle_configure(void * data,struct wl_shell_surface * shell_surface,uint32_t edges,int32_t width,int32_t height)251 handle_configure(void *data, struct wl_shell_surface *shell_surface,
252 uint32_t edges, int32_t width, int32_t height)
253 {
254 }
255
256 static void
handle_popup_done(void * data,struct wl_shell_surface * shell_surface)257 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
258 {
259 }
260
261 static const struct wl_shell_surface_listener shell_surface_listener = {
262 handle_ping,
263 handle_configure,
264 handle_popup_done
265 };
266
267 static void
handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)268 handle_global(void *data, struct wl_registry *registry,
269 uint32_t name, const char *interface, uint32_t version)
270 {
271 struct touch *touch = data;
272
273 if (strcmp(interface, "wl_compositor") == 0) {
274 touch->compositor =
275 wl_registry_bind(registry, name,
276 &wl_compositor_interface, 1);
277 } else if (strcmp(interface, "wl_shell") == 0) {
278 touch->shell =
279 wl_registry_bind(registry, name,
280 &wl_shell_interface, 1);
281 } else if (strcmp(interface, "wl_shm") == 0) {
282 touch->shm = wl_registry_bind(registry, name,
283 &wl_shm_interface, 1);
284 wl_shm_add_listener(touch->shm, &shm_listener, touch);
285 } else if (strcmp(interface, "wl_seat") == 0) {
286 add_seat(touch, name, version);
287 }
288 }
289
290 static void
handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)291 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
292 {
293 }
294
295 static const struct wl_registry_listener registry_listener = {
296 handle_global,
297 handle_global_remove
298 };
299
300 static struct touch *
touch_create(int width,int height)301 touch_create(int width, int height)
302 {
303 struct touch *touch;
304
305 touch = malloc(sizeof *touch);
306 if (touch == NULL) {
307 fprintf(stderr, "out of memory\n");
308 exit(1);
309 }
310 touch->display = wl_display_connect(NULL);
311 assert(touch->display);
312
313 touch->has_argb = 0;
314 touch->registry = wl_display_get_registry(touch->display);
315 wl_registry_add_listener(touch->registry, ®istry_listener, touch);
316 wl_display_dispatch(touch->display);
317 wl_display_roundtrip(touch->display);
318
319 if (!touch->has_argb) {
320 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
321 exit(1);
322 }
323
324 touch->width = width;
325 touch->height = height;
326 touch->surface = wl_compositor_create_surface(touch->compositor);
327 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
328 touch->surface);
329 create_shm_buffer(touch);
330
331 if (touch->shell_surface) {
332 wl_shell_surface_add_listener(touch->shell_surface,
333 &shell_surface_listener, touch);
334 wl_shell_surface_set_toplevel(touch->shell_surface);
335 }
336
337 wl_surface_set_user_data(touch->surface, touch);
338 wl_shell_surface_set_title(touch->shell_surface, "simple-touch");
339
340 memset(touch->data, 64, width * height * 4);
341 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
342 wl_surface_damage(touch->surface, 0, 0, width, height);
343 wl_surface_commit(touch->surface);
344
345 return touch;
346 }
347
348 int
main(int argc,char ** argv)349 main(int argc, char **argv)
350 {
351 struct touch *touch;
352 int ret = 0;
353
354 touch = touch_create(600, 500);
355
356 while (ret != -1)
357 ret = wl_display_dispatch(touch->display);
358
359 return 0;
360 }
361