button-test: Use wl_display_roundtrip instead of yield()
[profile/ivi/weston.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 <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 #include <signal.h>
32
33 #include <wayland-client.h>
34 #include "../shared/os-compatibility.h"
35
36 struct display {
37         struct wl_display *display;
38         struct wl_registry *registry;
39         struct wl_compositor *compositor;
40         struct wl_shell *shell;
41         struct wl_shm *shm;
42         uint32_t formats;
43 };
44
45 struct window {
46         struct display *display;
47         int width, height;
48         struct wl_surface *surface;
49         struct wl_shell_surface *shell_surface;
50         struct wl_buffer *buffer;
51         void *shm_data;
52         struct wl_callback *callback;
53 };
54
55 static struct wl_buffer *
56 create_shm_buffer(struct display *display,
57                   int width, int height, uint32_t format, void **data_out)
58 {
59         struct wl_shm_pool *pool;
60         struct wl_buffer *buffer;
61         int fd, size, stride;
62         void *data;
63
64         stride = width * 4;
65         size = stride * height;
66
67         fd = os_create_anonymous_file(size);
68         if (fd < 0) {
69                 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
70                         size);
71                 return NULL;
72         }
73
74         data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
75         if (data == MAP_FAILED) {
76                 fprintf(stderr, "mmap failed: %m\n");
77                 close(fd);
78                 return NULL;
79         }
80
81         pool = wl_shm_create_pool(display->shm, fd, size);
82         buffer = wl_shm_pool_create_buffer(pool, 0,
83                                            width, height, stride, format);
84         wl_shm_pool_destroy(pool);
85         close(fd);
86
87         *data_out = data;
88
89         return buffer;
90 }
91
92 static void
93 handle_ping(void *data, struct wl_shell_surface *shell_surface,
94                                                         uint32_t serial)
95 {
96         wl_shell_surface_pong(shell_surface, serial);
97 }
98
99 static void
100 handle_configure(void *data, struct wl_shell_surface *shell_surface,
101                  uint32_t edges, int32_t width, int32_t height)
102 {
103 }
104
105 static void
106 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
107 {
108 }
109
110 static const struct wl_shell_surface_listener shell_surface_listener = {
111         handle_ping,
112         handle_configure,
113         handle_popup_done
114 };
115
116 static struct window *
117 create_window(struct display *display, int width, int height)
118 {
119         struct window *window;
120         
121         window = malloc(sizeof *window);
122
123         window->buffer = create_shm_buffer(display,
124                                            width, height,
125                                            WL_SHM_FORMAT_XRGB8888,
126                                            &window->shm_data);
127
128         if (!window->buffer) {
129                 free(window);
130                 return NULL;
131         }
132
133         window->callback = NULL;
134         window->display = display;
135         window->width = width;
136         window->height = height;
137         window->surface = wl_compositor_create_surface(display->compositor);
138         window->shell_surface = wl_shell_get_shell_surface(display->shell,
139                                                            window->surface);
140
141         if (window->shell_surface)
142                 wl_shell_surface_add_listener(window->shell_surface,
143                                               &shell_surface_listener, window);
144
145         wl_shell_surface_set_title(window->shell_surface, "simple-shm");
146
147         wl_shell_surface_set_toplevel(window->shell_surface);
148
149         return window;
150 }
151
152 static void
153 destroy_window(struct window *window)
154 {
155         if (window->callback)
156                 wl_callback_destroy(window->callback);
157
158         wl_buffer_destroy(window->buffer);
159         wl_shell_surface_destroy(window->shell_surface);
160         wl_surface_destroy(window->surface);
161         free(window);
162 }
163
164 static void
165 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
166 {
167         const int halfh = padding + (height - padding * 2) / 2;
168         const int halfw = padding + (width  - padding * 2) / 2;
169         int ir, or;
170         uint32_t *pixel = image;
171         int y;
172
173         /* squared radii thresholds */
174         or = (halfw < halfh ? halfw : halfh) - 8;
175         ir = or - 32;
176         or *= or;
177         ir *= ir;
178
179         pixel += padding * width;
180         for (y = padding; y < height - padding; y++) {
181                 int x;
182                 int y2 = (y - halfh) * (y - halfh);
183
184                 pixel += padding;
185                 for (x = padding; x < width - padding; x++) {
186                         uint32_t v;
187
188                         /* squared distance from center */
189                         int r2 = (x - halfw) * (x - halfw) + y2;
190
191                         if (r2 < ir)
192                                 v = (r2 / 32 + time / 64) * 0x0080401;
193                         else if (r2 < or)
194                                 v = (y + time / 32) * 0x0080401;
195                         else
196                                 v = (x + time / 16) * 0x0080401;
197                         v &= 0x00ffffff;
198
199                         /* cross if compositor uses X from XRGB as alpha */
200                         if (abs(x - y) > 6 && abs(x + y - height) > 6)
201                                 v |= 0xff000000;
202
203                         *pixel++ = v;
204                 }
205
206                 pixel += padding;
207         }
208 }
209
210 static const struct wl_callback_listener frame_listener;
211
212 static void
213 redraw(void *data, struct wl_callback *callback, uint32_t time)
214 {
215         struct window *window = data;
216
217         paint_pixels(window->shm_data, 20, window->width, window->height, time);
218         wl_surface_damage(window->surface,
219                           20, 20, window->width - 40, window->height - 40);
220
221         if (callback)
222                 wl_callback_destroy(callback);
223
224         window->callback = wl_surface_frame(window->surface);
225         wl_callback_add_listener(window->callback, &frame_listener, window);
226         wl_surface_commit(window->surface);
227 }
228
229 static const struct wl_callback_listener frame_listener = {
230         redraw
231 };
232
233 static void
234 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
235 {
236         struct display *d = data;
237
238         d->formats |= (1 << format);
239 }
240
241 struct wl_shm_listener shm_listenter = {
242         shm_format
243 };
244
245 static void
246 registry_handle_global(void *data, struct wl_registry *registry,
247                        uint32_t id, const char *interface, uint32_t version)
248 {
249         struct display *d = data;
250
251         if (strcmp(interface, "wl_compositor") == 0) {
252                 d->compositor =
253                         wl_registry_bind(registry,
254                                          id, &wl_compositor_interface, 1);
255         } else if (strcmp(interface, "wl_shell") == 0) {
256                 d->shell = wl_registry_bind(registry,
257                                             id, &wl_shell_interface, 1);
258         } else if (strcmp(interface, "wl_shm") == 0) {
259                 d->shm = wl_registry_bind(registry,
260                                           id, &wl_shm_interface, 1);
261                 wl_shm_add_listener(d->shm, &shm_listenter, d);
262         }
263 }
264
265 static const struct wl_registry_listener registry_listener = {
266         registry_handle_global
267 };
268
269 static struct display *
270 create_display(void)
271 {
272         struct display *display;
273
274         display = malloc(sizeof *display);
275         display->display = wl_display_connect(NULL);
276         assert(display->display);
277
278         display->formats = 0;
279         display->registry = wl_display_get_registry(display->display);
280         wl_registry_add_listener(display->registry,
281                                  &registry_listener, display);
282         wl_display_roundtrip(display->display);
283         if (display->shm == NULL) {
284                 fprintf(stderr, "No wl_shm global\n");
285                 exit(1);
286         }
287
288         wl_display_roundtrip(display->display);
289
290         if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
291                 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
292                 exit(1);
293         }
294
295         wl_display_get_fd(display->display);
296         
297         return display;
298 }
299
300 static void
301 destroy_display(struct display *display)
302 {
303         if (display->shm)
304                 wl_shm_destroy(display->shm);
305
306         if (display->shell)
307                 wl_shell_destroy(display->shell);
308
309         if (display->compositor)
310                 wl_compositor_destroy(display->compositor);
311
312         wl_display_flush(display->display);
313         wl_display_disconnect(display->display);
314         free(display);
315 }
316
317 static int running = 1;
318
319 static void
320 signal_int(int signum)
321 {
322         running = 0;
323 }
324
325 int
326 main(int argc, char **argv)
327 {
328         struct sigaction sigint;
329         struct display *display;
330         struct window *window;
331         int ret = 0;
332
333         display = create_display();
334         window = create_window(display, 250, 250);
335         if (!window)
336                 return 1;
337
338         sigint.sa_handler = signal_int;
339         sigemptyset(&sigint.sa_mask);
340         sigint.sa_flags = SA_RESETHAND;
341         sigaction(SIGINT, &sigint, NULL);
342
343         memset(window->shm_data, 0xff, window->width * window->height * 4);
344         wl_surface_attach(window->surface, window->buffer, 0, 0);
345
346         redraw(window, NULL, 0);
347
348         while (running && ret != -1)
349                 ret = wl_display_dispatch(display->display);
350
351         fprintf(stderr, "simple-shm exiting\n");
352         destroy_window(window);
353         destroy_display(display);
354
355         return 0;
356 }