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