Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2008-2011 Kristian Høgsberg |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 13 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 24 | #include <stdio.h> |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 25 | #include <string.h> |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 26 | #include <linux/input.h> |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | #include <sys/uio.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 30 | |
| 31 | #include "compositor.h" |
Kristian Høgsberg | c5dcb90 | 2010-09-14 15:53:32 -0400 | [diff] [blame] | 32 | #include "screenshooter-server-protocol.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | |
Kristian Høgsberg | 2bcb2fc | 2012-05-25 18:03:52 -0400 | [diff] [blame] | 34 | #include "../wcap/wcap-decode.h" |
Kristian Høgsberg | 894e0b5 | 2012-05-25 17:55:20 -0400 | [diff] [blame] | 35 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 36 | struct screenshooter { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 37 | struct wl_object base; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 38 | struct weston_compositor *ec; |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 39 | struct wl_global *global; |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 40 | struct wl_client *client; |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 41 | struct weston_process process; |
Kristian Høgsberg | 02e79dc | 2012-04-12 09:55:26 -0400 | [diff] [blame] | 42 | struct wl_listener destroy_listener; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 43 | }; |
| 44 | |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 45 | struct screenshooter_read_pixels { |
| 46 | struct weston_read_pixels base; |
| 47 | struct wl_buffer *buffer; |
| 48 | struct wl_resource *resource; |
| 49 | }; |
| 50 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 51 | static void |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 52 | copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride) |
Pekka Paalanen | 45fab0e | 2012-04-17 11:55:41 +0300 | [diff] [blame] | 53 | { |
| 54 | uint8_t *end; |
| 55 | |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 56 | end = dst + height * stride; |
Pekka Paalanen | 45fab0e | 2012-04-17 11:55:41 +0300 | [diff] [blame] | 57 | while (dst < end) { |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 58 | memcpy(dst, src, stride); |
| 59 | dst += stride; |
| 60 | src -= stride; |
Pekka Paalanen | 45fab0e | 2012-04-17 11:55:41 +0300 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | static void |
Pekka Paalanen | a1d57db | 2012-04-17 15:02:08 +0300 | [diff] [blame] | 65 | copy_row_swap_RB(void *vdst, void *vsrc, int bytes) |
| 66 | { |
| 67 | uint32_t *dst = vdst; |
| 68 | uint32_t *src = vsrc; |
| 69 | uint32_t *end = dst + bytes / 4; |
| 70 | |
| 71 | while (dst < end) { |
| 72 | uint32_t v = *src++; |
| 73 | /* A R G B */ |
| 74 | uint32_t tmp = v & 0xff00ff00; |
| 75 | tmp |= (v >> 16) & 0x000000ff; |
| 76 | tmp |= (v << 16) & 0x00ff0000; |
| 77 | *dst++ = tmp; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 82 | copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride) |
Pekka Paalanen | a1d57db | 2012-04-17 15:02:08 +0300 | [diff] [blame] | 83 | { |
| 84 | uint8_t *end; |
| 85 | |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 86 | end = dst + height * stride; |
Pekka Paalanen | a1d57db | 2012-04-17 15:02:08 +0300 | [diff] [blame] | 87 | while (dst < end) { |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 88 | copy_row_swap_RB(dst, src, stride); |
| 89 | dst += stride; |
| 90 | src -= stride; |
Pekka Paalanen | a1d57db | 2012-04-17 15:02:08 +0300 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | static void |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 95 | screenshooter_read_pixels_done(struct weston_read_pixels *base, |
| 96 | struct weston_output *output) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 97 | { |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 98 | struct screenshooter_read_pixels *r = |
| 99 | (struct screenshooter_read_pixels *) base; |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 100 | int32_t stride; |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 101 | uint8_t *d, *s; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 102 | |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 103 | stride = wl_shm_buffer_get_stride(r->buffer); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 104 | |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 105 | d = wl_shm_buffer_get_data(r->buffer); |
| 106 | s = r->base.data + stride * (r->buffer->height - 1); |
Pekka Paalanen | a1d57db | 2012-04-17 15:02:08 +0300 | [diff] [blame] | 107 | |
| 108 | switch (output->compositor->read_format) { |
| 109 | case GL_BGRA_EXT: |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 110 | copy_bgra_yflip(d, s, output->current->height, stride); |
Pekka Paalanen | a1d57db | 2012-04-17 15:02:08 +0300 | [diff] [blame] | 111 | break; |
| 112 | case GL_RGBA: |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 113 | copy_rgba_yflip(d, s, output->current->height, stride); |
Pekka Paalanen | a1d57db | 2012-04-17 15:02:08 +0300 | [diff] [blame] | 114 | break; |
| 115 | default: |
| 116 | break; |
| 117 | } |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 118 | |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 119 | wl_list_remove(&r->base.link); |
| 120 | |
| 121 | screenshooter_send_done(r->resource); |
| 122 | free(r->base.data); |
| 123 | free(r); |
| 124 | |
| 125 | } |
| 126 | |
| 127 | static void |
| 128 | screenshooter_shoot(struct wl_client *client, |
| 129 | struct wl_resource *resource, |
| 130 | struct wl_resource *output_resource, |
| 131 | struct wl_resource *buffer_resource) |
| 132 | { |
| 133 | struct weston_output *output = output_resource->data; |
| 134 | struct screenshooter_read_pixels *r; |
| 135 | struct wl_buffer *buffer = buffer_resource->data; |
| 136 | int32_t stride; |
| 137 | |
| 138 | if (!wl_buffer_is_shm(buffer)) |
| 139 | return; |
| 140 | |
| 141 | if (buffer->width < output->current->width || |
| 142 | buffer->height < output->current->height) |
| 143 | return; |
| 144 | |
| 145 | r = malloc(sizeof *r); |
| 146 | if (r == NULL) { |
| 147 | wl_resource_post_no_memory(resource); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | r->base.x = 0; |
| 152 | r->base.y = 0; |
| 153 | r->base.width = output->current->width; |
| 154 | r->base.height = output->current->height; |
| 155 | r->base.done = screenshooter_read_pixels_done; |
| 156 | r->buffer = buffer; |
| 157 | r->resource = resource; |
| 158 | stride = buffer->width * 4; |
| 159 | r->base.data = malloc(stride * buffer->height); |
| 160 | |
| 161 | if (r->base.data == NULL) { |
| 162 | free(r); |
| 163 | wl_resource_post_no_memory(resource); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | wl_list_insert(output->read_pixels_list.prev, &r->base.link); |
| 168 | weston_compositor_schedule_repaint(output->compositor); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 171 | struct screenshooter_interface screenshooter_implementation = { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 172 | screenshooter_shoot |
| 173 | }; |
| 174 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 175 | static void |
| 176 | bind_shooter(struct wl_client *client, |
| 177 | void *data, uint32_t version, uint32_t id) |
| 178 | { |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 179 | struct screenshooter *shooter = data; |
| 180 | struct wl_resource *resource; |
| 181 | |
| 182 | resource = wl_client_add_object(client, &screenshooter_interface, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 183 | &screenshooter_implementation, id, data); |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 184 | |
| 185 | if (client != shooter->client) { |
| 186 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 187 | "screenshooter failed: permission denied"); |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 188 | wl_resource_destroy(resource); |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | screenshooter_sigchld(struct weston_process *process, int status) |
| 194 | { |
| 195 | struct screenshooter *shooter = |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 196 | container_of(process, struct screenshooter, process); |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 197 | |
| 198 | shooter->client = NULL; |
| 199 | } |
| 200 | |
| 201 | static void |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 202 | screenshooter_binding(struct wl_seat *seat, uint32_t time, uint32_t key, |
| 203 | void *data) |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 204 | { |
| 205 | struct screenshooter *shooter = data; |
| 206 | const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter"; |
| 207 | |
| 208 | if (!shooter->client) |
| 209 | shooter->client = weston_client_launch(shooter->ec, |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 210 | &shooter->process, |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 211 | screenshooter_exe, screenshooter_sigchld); |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 214 | struct weston_recorder { |
| 215 | struct weston_output *output; |
| 216 | uint32_t *frame, *rect; |
| 217 | uint32_t total; |
| 218 | int fd; |
| 219 | struct wl_listener frame_listener; |
Kristian Høgsberg | e012c82 | 2012-05-25 17:50:42 -0400 | [diff] [blame] | 220 | int count; |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | static uint32_t * |
| 224 | output_run(uint32_t *p, uint32_t delta, int run) |
| 225 | { |
| 226 | int i; |
| 227 | |
| 228 | while (run > 0) { |
| 229 | if (run <= 0xe0) { |
| 230 | *p++ = delta | ((run - 1) << 24); |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | i = 24 - __builtin_clz(run); |
| 235 | *p++ = delta | ((i + 0xe0) << 24); |
| 236 | run -= 1 << (7 + i); |
| 237 | } |
| 238 | |
| 239 | return p; |
| 240 | } |
| 241 | |
Kristian Høgsberg | 053be42 | 2012-05-29 12:15:47 -0400 | [diff] [blame] | 242 | static uint32_t |
| 243 | component_delta(uint32_t next, uint32_t prev) |
| 244 | { |
| 245 | unsigned char dr, dg, db; |
| 246 | |
| 247 | dr = (next >> 16) - (prev >> 16); |
| 248 | dg = (next >> 8) - (prev >> 8); |
| 249 | db = (next >> 0) - (prev >> 0); |
| 250 | |
| 251 | return (dr << 16) | (dg << 8) | (db << 0); |
| 252 | } |
| 253 | |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 254 | static void |
| 255 | weston_recorder_frame_notify(struct wl_listener *listener, void *data) |
| 256 | { |
| 257 | struct weston_recorder *recorder = |
| 258 | container_of(listener, struct weston_recorder, frame_listener); |
| 259 | struct weston_output *output = recorder->output; |
| 260 | uint32_t msecs = * (uint32_t *) data; |
| 261 | pixman_box32_t *r; |
| 262 | pixman_region32_t damage; |
| 263 | int i, j, k, n, width, height, run, stride; |
| 264 | uint32_t delta, prev, *d, *s, *p, next; |
| 265 | struct { |
| 266 | uint32_t msecs; |
| 267 | uint32_t nrects; |
| 268 | } header; |
| 269 | struct iovec v[2]; |
| 270 | |
| 271 | pixman_region32_init(&damage); |
| 272 | pixman_region32_intersect(&damage, &recorder->output->region, |
| 273 | &recorder->output->previous_damage); |
| 274 | |
| 275 | r = pixman_region32_rectangles(&damage, &n); |
| 276 | if (n == 0) |
| 277 | return; |
| 278 | |
| 279 | header.msecs = msecs; |
| 280 | header.nrects = n; |
| 281 | v[0].iov_base = &header; |
| 282 | v[0].iov_len = sizeof header; |
| 283 | v[1].iov_base = r; |
| 284 | v[1].iov_len = n * sizeof *r; |
| 285 | recorder->total += writev(recorder->fd, v, 2); |
| 286 | stride = output->current->width; |
| 287 | |
| 288 | for (i = 0; i < n; i++) { |
| 289 | width = r[i].x2 - r[i].x1; |
| 290 | height = r[i].y2 - r[i].y1; |
| 291 | glReadPixels(r[i].x1, output->current->height - r[i].y2, |
| 292 | width, height, |
| 293 | output->compositor->read_format, |
| 294 | GL_UNSIGNED_BYTE, recorder->rect); |
| 295 | |
| 296 | s = recorder->rect; |
| 297 | p = recorder->rect; |
| 298 | run = prev = 0; /* quiet gcc */ |
| 299 | for (j = 0; j < height; j++) { |
| 300 | d = recorder->frame + |
| 301 | stride * (r[i].y2 - j - 1) + r[i].x1; |
| 302 | for (k = 0; k < width; k++) { |
| 303 | next = *s++; |
Kristian Høgsberg | 053be42 | 2012-05-29 12:15:47 -0400 | [diff] [blame] | 304 | delta = component_delta(next, *d); |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 305 | *d++ = next; |
| 306 | if (run == 0 || delta == prev) { |
| 307 | run++; |
| 308 | } else { |
| 309 | p = output_run(p, prev, run); |
| 310 | run = 1; |
| 311 | prev = delta; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | p = output_run(p, prev, run); |
| 317 | |
| 318 | recorder->total += write(recorder->fd, |
| 319 | recorder->rect, |
| 320 | (p - recorder->rect) * 4); |
| 321 | |
| 322 | #if 0 |
| 323 | fprintf(stderr, |
| 324 | "%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n", |
| 325 | width, height, r[i].x1, r[i].y1, |
Kristian Høgsberg | f6f69d3 | 2012-06-18 17:10:19 -0400 | [diff] [blame] | 326 | width * height * 4, (int) (p - recorder->rect) * 4, |
| 327 | (float) (p - recorder->rect) / (width * height), |
| 328 | recorder->total / 1024 / 1024); |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 329 | #endif |
| 330 | } |
| 331 | |
| 332 | pixman_region32_fini(&damage); |
| 333 | } |
| 334 | |
| 335 | static void |
| 336 | weston_recorder_create(struct weston_output *output, const char *filename) |
| 337 | { |
| 338 | struct weston_recorder *recorder; |
| 339 | int stride, size; |
Kristian Høgsberg | 3b96960 | 2012-05-25 17:45:39 -0400 | [diff] [blame] | 340 | struct { uint32_t magic, format, width, height; } header; |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 341 | |
| 342 | recorder = malloc(sizeof *recorder); |
| 343 | recorder->output = output; |
| 344 | |
| 345 | stride = output->current->width; |
| 346 | size = stride * 4 * output->current->height; |
| 347 | recorder->frame = malloc(size); |
| 348 | recorder->rect = malloc(size); |
| 349 | recorder->total = 0; |
Kristian Høgsberg | e012c82 | 2012-05-25 17:50:42 -0400 | [diff] [blame] | 350 | recorder->count = 0; |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 351 | memset(recorder->frame, 0, size); |
| 352 | |
| 353 | recorder->fd = open(filename, |
| 354 | O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); |
| 355 | |
Kristian Høgsberg | 3b96960 | 2012-05-25 17:45:39 -0400 | [diff] [blame] | 356 | header.magic = WCAP_HEADER_MAGIC; |
| 357 | |
| 358 | switch (output->compositor->read_format) { |
| 359 | case GL_BGRA_EXT: |
| 360 | header.format = WCAP_FORMAT_XRGB8888; |
| 361 | break; |
| 362 | case GL_RGBA: |
| 363 | header.format = WCAP_FORMAT_XBGR8888; |
| 364 | break; |
| 365 | } |
| 366 | |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 367 | header.width = output->current->width; |
| 368 | header.height = output->current->height; |
| 369 | recorder->total += write(recorder->fd, &header, sizeof header); |
| 370 | |
| 371 | recorder->frame_listener.notify = weston_recorder_frame_notify; |
| 372 | wl_signal_add(&output->frame_signal, &recorder->frame_listener); |
| 373 | weston_output_damage(output); |
| 374 | } |
| 375 | |
| 376 | static void |
| 377 | weston_recorder_destroy(struct weston_recorder *recorder) |
| 378 | { |
| 379 | wl_list_remove(&recorder->frame_listener.link); |
| 380 | close(recorder->fd); |
| 381 | free(recorder->frame); |
| 382 | free(recorder->rect); |
| 383 | free(recorder); |
| 384 | } |
| 385 | |
| 386 | static void |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 387 | recorder_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data) |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 388 | { |
| 389 | struct weston_seat *ws = (struct weston_seat *) seat; |
| 390 | struct weston_compositor *ec = ws->compositor; |
| 391 | struct weston_output *output = |
| 392 | container_of(ec->output_list.next, |
| 393 | struct weston_output, link); |
| 394 | struct wl_listener *listener; |
| 395 | struct weston_recorder *recorder; |
| 396 | static const char filename[] = "capture.wcap"; |
| 397 | |
| 398 | listener = wl_signal_get(&output->frame_signal, |
| 399 | weston_recorder_frame_notify); |
| 400 | if (listener) { |
| 401 | recorder = container_of(listener, struct weston_recorder, |
| 402 | frame_listener); |
| 403 | |
Kristian Høgsberg | e012c82 | 2012-05-25 17:50:42 -0400 | [diff] [blame] | 404 | fprintf(stderr, |
| 405 | "stopping recorder, total file size %dM, %d frames\n", |
| 406 | recorder->total / (1024 * 1024), recorder->count); |
Kristian Høgsberg | 5fb70bf | 2012-05-24 12:29:46 -0400 | [diff] [blame] | 407 | |
| 408 | weston_recorder_destroy(recorder); |
| 409 | } else { |
| 410 | fprintf(stderr, "starting recorder, file %s\n", filename); |
| 411 | weston_recorder_create(output, filename); |
| 412 | } |
| 413 | } |
| 414 | |
Kristian Høgsberg | 02e79dc | 2012-04-12 09:55:26 -0400 | [diff] [blame] | 415 | static void |
| 416 | screenshooter_destroy(struct wl_listener *listener, void *data) |
| 417 | { |
| 418 | struct screenshooter *shooter = |
| 419 | container_of(listener, struct screenshooter, destroy_listener); |
| 420 | |
| 421 | wl_display_remove_global(shooter->ec->wl_display, shooter->global); |
| 422 | free(shooter); |
| 423 | } |
| 424 | |
| 425 | void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 426 | screenshooter_create(struct weston_compositor *ec) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 427 | { |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 428 | struct screenshooter *shooter; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 429 | |
| 430 | shooter = malloc(sizeof *shooter); |
| 431 | if (shooter == NULL) |
Kristian Høgsberg | 02e79dc | 2012-04-12 09:55:26 -0400 | [diff] [blame] | 432 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 433 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 434 | shooter->base.interface = &screenshooter_interface; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 435 | shooter->base.implementation = |
| 436 | (void(**)(void)) &screenshooter_implementation; |
| 437 | shooter->ec = ec; |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 438 | shooter->client = NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 439 | |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 440 | shooter->global = wl_display_add_global(ec->wl_display, |
| 441 | &screenshooter_interface, |
| 442 | shooter, bind_shooter); |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 443 | weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER, |
| 444 | screenshooter_binding, shooter); |
| 445 | weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER, |
| 446 | recorder_binding, shooter); |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 447 | |
Kristian Høgsberg | 02e79dc | 2012-04-12 09:55:26 -0400 | [diff] [blame] | 448 | shooter->destroy_listener.notify = screenshooter_destroy; |
| 449 | wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener); |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 450 | } |