button-test: Use wl_display_roundtrip instead of yield()
[profile/ivi/weston.git] / clients / simple-touch.c
1 /*
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 "../shared/os-compatibility.h"
34
35 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
36
37 struct touch {
38         struct wl_display *display;
39         struct wl_registry *registry;
40         struct wl_compositor *compositor;
41         struct wl_shell *shell;
42         struct wl_shm *shm;
43         struct wl_seat *seat;
44         struct wl_touch *wl_touch;
45         struct wl_pointer *pointer;
46         struct wl_keyboard *keyboard;
47         struct wl_surface *surface;
48         struct wl_shell_surface *shell_surface;
49         struct wl_buffer *buffer;
50         int has_argb;
51         int width, height;
52         void *data;
53 };
54
55 static void
56 create_shm_buffer(struct touch *touch)
57 {
58         struct wl_shm_pool *pool;
59         int fd, size, stride;
60
61         stride = touch->width * 4;
62         size = stride * touch->height;
63
64         fd = os_create_anonymous_file(size);
65         if (fd < 0) {
66                 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
67                         size);
68                 exit(1);
69         }
70
71         touch->data =
72                 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
73         if (touch->data == MAP_FAILED) {
74                 fprintf(stderr, "mmap failed: %m\n");
75                 close(fd);
76                 exit(1);
77         }
78
79         pool = wl_shm_create_pool(touch->shm, fd, size);
80         touch->buffer =
81                 wl_shm_pool_create_buffer(pool, 0,
82                                           touch->width, touch->height, stride,
83                                           WL_SHM_FORMAT_ARGB8888);
84         wl_shm_pool_destroy(pool);
85
86         close(fd);
87 }
88
89 static void
90 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
91 {
92         struct touch *touch = data;
93
94         if (format == WL_SHM_FORMAT_ARGB8888)
95                 touch->has_argb = 1;
96 }
97
98 struct wl_shm_listener shm_listenter = {
99         shm_format
100 };
101
102
103 static void
104 touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
105 {
106         uint32_t *p, c;
107         static const uint32_t colors[] = {
108                 0xffff0000,
109                 0xffffff00,
110                 0xff0000ff,
111                 0xffff00ff,
112                 0xff00ff00,
113                 0xff00ffff,
114         };
115
116         if (id < (int32_t) ARRAY_LENGTH(colors))
117                 c = colors[id];
118         else
119                 c = 0xffffffff;
120
121         if (x < 2 || x >= touch->width - 2 ||
122             y < 2 || y >= touch->height - 2)
123                 return;
124
125         p = (uint32_t *) touch->data + (x - 2) + (y - 2) * touch->width;
126         p[2] = c;
127         p += touch->width;
128         p[1] = c;
129         p[2] = c;
130         p[3] = c;
131         p += touch->width;
132         p[0] = c;
133         p[1] = c;
134         p[2] = c;
135         p[3] = c;
136         p[4] = c;
137         p += touch->width;
138         p[1] = c;
139         p[2] = c;
140         p[3] = c;
141         p += touch->width;
142         p[2] = c;
143
144         wl_surface_damage(touch->surface, x - 2, y - 2, 5, 5);
145         /* todo: We could queue up more damage before committing, if there
146          * are more input events to handle.
147          */
148         wl_surface_commit(touch->surface);
149 }
150
151 static void
152 touch_handle_down(void *data, struct wl_touch *wl_touch,
153                   uint32_t serial, uint32_t time, struct wl_surface *surface,
154                   int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
155 {
156         struct touch *touch = data;
157         float x = wl_fixed_to_double(x_w);
158         float y = wl_fixed_to_double(y_w);
159
160         touch_paint(touch, x, y, id);
161 }
162
163 static void
164 touch_handle_up(void *data, struct wl_touch *wl_touch,
165                 uint32_t serial, uint32_t time, int32_t id)
166 {
167 }
168
169 static void
170 touch_handle_motion(void *data, struct wl_touch *wl_touch,
171                     uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
172 {
173         struct touch *touch = data;
174         float x = wl_fixed_to_double(x_w);
175         float y = wl_fixed_to_double(y_w);
176
177         touch_paint(touch, x, y, id);
178 }
179
180 static void
181 touch_handle_frame(void *data, struct wl_touch *wl_touch)
182 {
183 }
184
185 static void
186 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
187 {
188 }
189
190 static const struct wl_touch_listener touch_listener = {
191         touch_handle_down,
192         touch_handle_up,
193         touch_handle_motion,
194         touch_handle_frame,
195         touch_handle_cancel,
196 };
197
198 static void
199 seat_handle_capabilities(void *data, struct wl_seat *seat,
200                          enum wl_seat_capability caps)
201 {
202         struct touch *touch = data;
203
204         if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !touch->wl_touch) {
205                 touch->wl_touch = wl_seat_get_touch(seat);
206                 wl_touch_set_user_data(touch->wl_touch, touch);
207                 wl_touch_add_listener(touch->wl_touch, &touch_listener, touch);
208         } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && touch->wl_touch) {
209                 wl_touch_destroy(touch->wl_touch);
210                 touch->wl_touch = NULL;
211         }
212 }
213
214 static const struct wl_seat_listener seat_listener = {
215         seat_handle_capabilities,
216 };
217
218 static void
219 handle_ping(void *data, struct wl_shell_surface *shell_surface,
220             uint32_t serial)
221 {
222         wl_shell_surface_pong(shell_surface, serial);
223 }
224
225 static void
226 handle_configure(void *data, struct wl_shell_surface *shell_surface,
227                  uint32_t edges, int32_t width, int32_t height)
228 {
229 }
230
231 static void
232 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
233 {
234 }
235
236 static const struct wl_shell_surface_listener shell_surface_listener = {
237         handle_ping,
238         handle_configure,
239         handle_popup_done
240 };
241
242 static void
243 handle_global(void *data, struct wl_registry *registry,
244               uint32_t name, const char *interface, uint32_t version)
245 {
246         struct touch *touch = data;
247
248         if (strcmp(interface, "wl_compositor") == 0) {
249                 touch->compositor =
250                         wl_registry_bind(registry, name,
251                                          &wl_compositor_interface, 1);
252         } else if (strcmp(interface, "wl_shell") == 0) {
253                 touch->shell =
254                         wl_registry_bind(registry, name,
255                                          &wl_shell_interface, 1);
256         } else if (strcmp(interface, "wl_shm") == 0) {
257                 touch->shm = wl_registry_bind(registry, name,
258                                               &wl_shm_interface, 1);
259                 wl_shm_add_listener(touch->shm, &shm_listenter, touch);
260         } else if (strcmp(interface, "wl_seat") == 0) {
261                 touch->seat = wl_registry_bind(registry, name,
262                                                &wl_seat_interface, 1);
263                 wl_seat_add_listener(touch->seat, &seat_listener, touch);
264         }
265 }
266
267 static const struct wl_registry_listener registry_listener = {
268         handle_global
269 };
270
271 static struct touch *
272 touch_create(int width, int height)
273 {
274         struct touch *touch;
275
276         touch = malloc(sizeof *touch);
277         touch->display = wl_display_connect(NULL);
278         assert(touch->display);
279
280         touch->has_argb = 0;
281         touch->registry = wl_display_get_registry(touch->display);
282         wl_registry_add_listener(touch->registry, &registry_listener, touch);
283         wl_display_dispatch(touch->display);
284         wl_display_roundtrip(touch->display);
285
286         if (!touch->has_argb) {
287                 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
288                 exit(1);
289         }
290
291         wl_display_get_fd(touch->display);
292         
293         touch->width = width;
294         touch->height = height;
295         touch->surface = wl_compositor_create_surface(touch->compositor);
296         touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
297                                                           touch->surface);
298         create_shm_buffer(touch);
299
300         if (touch->shell_surface) {
301                 wl_shell_surface_add_listener(touch->shell_surface,
302                                               &shell_surface_listener, touch);
303                 wl_shell_surface_set_toplevel(touch->shell_surface);
304         }
305
306         wl_surface_set_user_data(touch->surface, touch);
307         wl_shell_surface_set_title(touch->shell_surface, "simple-touch");
308
309         memset(touch->data, 64, width * height * 4);
310         wl_surface_attach(touch->surface, touch->buffer, 0, 0);
311         wl_surface_damage(touch->surface, 0, 0, width, height);
312         wl_surface_commit(touch->surface);
313
314         return touch;
315 }
316
317 int
318 main(int argc, char **argv)
319 {
320         struct touch *touch;
321         int ret = 0;
322
323         touch = touch_create(600, 500);
324
325         while (ret != -1)
326                 ret = wl_display_dispatch(touch->display);
327
328         return 0;
329 }