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