weston-editor --help works
[profile/ivi/weston-ivi-shell.git] / clients / simple-shm.c
1 /*
2  * Copyright © 2011 Benjamin Franzke
3  * Copyright © 2010 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 #include <signal.h>
34
35 #include <wayland-client.h>
36 #include "../shared/os-compatibility.h"
37 #include "xdg-shell-client-protocol.h"
38 #include "fullscreen-shell-client-protocol.h"
39
40 struct display {
41         struct wl_display *display;
42         struct wl_registry *registry;
43         struct wl_compositor *compositor;
44         struct xdg_shell *shell;
45         struct _wl_fullscreen_shell *fshell;
46         struct wl_shm *shm;
47         uint32_t formats;
48 };
49
50 struct buffer {
51         struct wl_buffer *buffer;
52         void *shm_data;
53         int busy;
54 };
55
56 struct window {
57         struct display *display;
58         int width, height;
59         struct wl_surface *surface;
60         struct xdg_surface *xdg_surface;
61         struct buffer buffers[2];
62         struct buffer *prev_buffer;
63         struct wl_callback *callback;
64 };
65
66 static int running = 1;
67
68 static void
69 buffer_release(void *data, struct wl_buffer *buffer)
70 {
71         struct buffer *mybuf = data;
72
73         mybuf->busy = 0;
74 }
75
76 static const struct wl_buffer_listener buffer_listener = {
77         buffer_release
78 };
79
80 static int
81 create_shm_buffer(struct display *display, struct buffer *buffer,
82                   int width, int height, uint32_t format)
83 {
84         struct wl_shm_pool *pool;
85         int fd, size, stride;
86         void *data;
87
88         stride = width * 4;
89         size = stride * height;
90
91         fd = os_create_anonymous_file(size);
92         if (fd < 0) {
93                 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
94                         size);
95                 return -1;
96         }
97
98         data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
99         if (data == MAP_FAILED) {
100                 fprintf(stderr, "mmap failed: %m\n");
101                 close(fd);
102                 return -1;
103         }
104
105         pool = wl_shm_create_pool(display->shm, fd, size);
106         buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
107                                                    width, height,
108                                                    stride, format);
109         wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
110         wl_shm_pool_destroy(pool);
111         close(fd);
112
113         buffer->shm_data = data;
114
115         return 0;
116 }
117
118 static void
119 handle_configure(void *data, struct xdg_surface *surface,
120                  int32_t width, int32_t height,
121                  struct wl_array *states, uint32_t serial)
122 {
123         xdg_surface_ack_configure(surface, serial);
124 }
125
126 static void
127 handle_delete(void *data, struct xdg_surface *xdg_surface)
128 {
129         running = 0;
130 }
131
132 static const struct xdg_surface_listener xdg_surface_listener = {
133         handle_configure,
134         handle_delete,
135 };
136
137 static struct window *
138 create_window(struct display *display, int width, int height)
139 {
140         struct window *window;
141
142         window = calloc(1, sizeof *window);
143         if (!window)
144                 return NULL;
145
146         window->callback = NULL;
147         window->display = display;
148         window->width = width;
149         window->height = height;
150         window->surface = wl_compositor_create_surface(display->compositor);
151
152         if (display->shell) {
153                 window->xdg_surface =
154                         xdg_shell_get_xdg_surface(display->shell,
155                                                   window->surface);
156
157                 assert(window->xdg_surface);
158
159                 xdg_surface_add_listener(window->xdg_surface,
160                                          &xdg_surface_listener, window);
161
162                 xdg_surface_set_title(window->xdg_surface, "simple-shm");
163         } else if (display->fshell) {
164                 _wl_fullscreen_shell_present_surface(display->fshell,
165                                                      window->surface,
166                                                      _WL_FULLSCREEN_SHELL_PRESENT_METHOD_DEFAULT,
167                                                      NULL);
168         } else {
169                 assert(0);
170         }
171
172         return window;
173 }
174
175 static void
176 destroy_window(struct window *window)
177 {
178         if (window->callback)
179                 wl_callback_destroy(window->callback);
180
181         if (window->buffers[0].buffer)
182                 wl_buffer_destroy(window->buffers[0].buffer);
183         if (window->buffers[1].buffer)
184                 wl_buffer_destroy(window->buffers[1].buffer);
185
186         if (window->xdg_surface)
187                 xdg_surface_destroy(window->xdg_surface);
188         wl_surface_destroy(window->surface);
189         free(window);
190 }
191
192 static struct buffer *
193 window_next_buffer(struct window *window)
194 {
195         struct buffer *buffer;
196         int ret = 0;
197
198         if (!window->buffers[0].busy)
199                 buffer = &window->buffers[0];
200         else if (!window->buffers[1].busy)
201                 buffer = &window->buffers[1];
202         else
203                 return NULL;
204
205         if (!buffer->buffer) {
206                 ret = create_shm_buffer(window->display, buffer,
207                                         window->width, window->height,
208                                         WL_SHM_FORMAT_XRGB8888);
209
210                 if (ret < 0)
211                         return NULL;
212
213                 /* paint the padding */
214                 memset(buffer->shm_data, 0xff,
215                        window->width * window->height * 4);
216         }
217
218         return buffer;
219 }
220
221 static void
222 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
223 {
224         const int halfh = padding + (height - padding * 2) / 2;
225         const int halfw = padding + (width  - padding * 2) / 2;
226         int ir, or;
227         uint32_t *pixel = image;
228         int y;
229
230         /* squared radii thresholds */
231         or = (halfw < halfh ? halfw : halfh) - 8;
232         ir = or - 32;
233         or *= or;
234         ir *= ir;
235
236         pixel += padding * width;
237         for (y = padding; y < height - padding; y++) {
238                 int x;
239                 int y2 = (y - halfh) * (y - halfh);
240
241                 pixel += padding;
242                 for (x = padding; x < width - padding; x++) {
243                         uint32_t v;
244
245                         /* squared distance from center */
246                         int r2 = (x - halfw) * (x - halfw) + y2;
247
248                         if (r2 < ir)
249                                 v = (r2 / 32 + time / 64) * 0x0080401;
250                         else if (r2 < or)
251                                 v = (y + time / 32) * 0x0080401;
252                         else
253                                 v = (x + time / 16) * 0x0080401;
254                         v &= 0x00ffffff;
255
256                         /* cross if compositor uses X from XRGB as alpha */
257                         if (abs(x - y) > 6 && abs(x + y - height) > 6)
258                                 v |= 0xff000000;
259
260                         *pixel++ = v;
261                 }
262
263                 pixel += padding;
264         }
265 }
266
267 static const struct wl_callback_listener frame_listener;
268
269 static void
270 redraw(void *data, struct wl_callback *callback, uint32_t time)
271 {
272         struct window *window = data;
273         struct buffer *buffer;
274
275         buffer = window_next_buffer(window);
276         if (!buffer) {
277                 fprintf(stderr,
278                         !callback ? "Failed to create the first buffer.\n" :
279                         "Both buffers busy at redraw(). Server bug?\n");
280                 abort();
281         }
282
283         paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
284
285         wl_surface_attach(window->surface, buffer->buffer, 0, 0);
286         wl_surface_damage(window->surface,
287                           20, 20, window->width - 40, window->height - 40);
288
289         if (callback)
290                 wl_callback_destroy(callback);
291
292         window->callback = wl_surface_frame(window->surface);
293         wl_callback_add_listener(window->callback, &frame_listener, window);
294         wl_surface_commit(window->surface);
295         buffer->busy = 1;
296 }
297
298 static const struct wl_callback_listener frame_listener = {
299         redraw
300 };
301
302 static void
303 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
304 {
305         struct display *d = data;
306
307         d->formats |= (1 << format);
308 }
309
310 struct wl_shm_listener shm_listener = {
311         shm_format
312 };
313
314 static void
315 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
316 {
317         xdg_shell_pong(shell, serial);
318 }
319
320 static const struct xdg_shell_listener xdg_shell_listener = {
321         xdg_shell_ping,
322 };
323
324 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
325 #ifdef static_assert
326 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
327               "Interface version doesn't match implementation version");
328 #endif
329
330 static void
331 registry_handle_global(void *data, struct wl_registry *registry,
332                        uint32_t id, const char *interface, uint32_t version)
333 {
334         struct display *d = data;
335
336         if (strcmp(interface, "wl_compositor") == 0) {
337                 d->compositor =
338                         wl_registry_bind(registry,
339                                          id, &wl_compositor_interface, 1);
340         } else if (strcmp(interface, "xdg_shell") == 0) {
341                 d->shell = wl_registry_bind(registry,
342                                             id, &xdg_shell_interface, 1);
343                 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
344                 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
345         } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
346                 d->fshell = wl_registry_bind(registry,
347                                              id, &_wl_fullscreen_shell_interface, 1);
348         } else if (strcmp(interface, "wl_shm") == 0) {
349                 d->shm = wl_registry_bind(registry,
350                                           id, &wl_shm_interface, 1);
351                 wl_shm_add_listener(d->shm, &shm_listener, d);
352         }
353 }
354
355 static void
356 registry_handle_global_remove(void *data, struct wl_registry *registry,
357                               uint32_t name)
358 {
359 }
360
361 static const struct wl_registry_listener registry_listener = {
362         registry_handle_global,
363         registry_handle_global_remove
364 };
365
366 static struct display *
367 create_display(void)
368 {
369         struct display *display;
370
371         display = malloc(sizeof *display);
372         if (display == NULL) {
373                 fprintf(stderr, "out of memory\n");
374                 exit(1);
375         }
376         display->display = wl_display_connect(NULL);
377         assert(display->display);
378
379         display->formats = 0;
380         display->registry = wl_display_get_registry(display->display);
381         wl_registry_add_listener(display->registry,
382                                  &registry_listener, display);
383         wl_display_roundtrip(display->display);
384         if (display->shm == NULL) {
385                 fprintf(stderr, "No wl_shm global\n");
386                 exit(1);
387         }
388
389         wl_display_roundtrip(display->display);
390
391         if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
392                 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
393                 exit(1);
394         }
395
396         wl_display_get_fd(display->display);
397         
398         return display;
399 }
400
401 static void
402 destroy_display(struct display *display)
403 {
404         if (display->shm)
405                 wl_shm_destroy(display->shm);
406
407         if (display->shell)
408                 xdg_shell_destroy(display->shell);
409
410         if (display->fshell)
411                 _wl_fullscreen_shell_release(display->fshell);
412
413         if (display->compositor)
414                 wl_compositor_destroy(display->compositor);
415
416         wl_registry_destroy(display->registry);
417         wl_display_flush(display->display);
418         wl_display_disconnect(display->display);
419         free(display);
420 }
421
422 static void
423 signal_int(int signum)
424 {
425         running = 0;
426 }
427
428 int
429 main(int argc, char **argv)
430 {
431         struct sigaction sigint;
432         struct display *display;
433         struct window *window;
434         int ret = 0;
435
436         display = create_display();
437         window = create_window(display, 250, 250);
438         if (!window)
439                 return 1;
440
441         sigint.sa_handler = signal_int;
442         sigemptyset(&sigint.sa_mask);
443         sigint.sa_flags = SA_RESETHAND;
444         sigaction(SIGINT, &sigint, NULL);
445
446         /* Initialise damage to full surface, so the padding gets painted */
447         wl_surface_damage(window->surface, 0, 0,
448                           window->width, window->height);
449
450         redraw(window, NULL, 0);
451
452         while (running && ret != -1)
453                 ret = wl_display_dispatch(display->display);
454
455         fprintf(stderr, "simple-shm exiting\n");
456         destroy_window(window);
457         destroy_display(display);
458
459         return 0;
460 }