Pekka Paalanen | 312fe5f | 2015-02-09 11:23:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2015 Collabora, Ltd. |
| 3 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
Pekka Paalanen | 312fe5f | 2015-02-09 11:23:48 +0200 | [diff] [blame] | 11 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
Pekka Paalanen | 312fe5f | 2015-02-09 11:23:48 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame^] | 28 | #include <stdint.h> |
Pekka Paalanen | 312fe5f | 2015-02-09 11:23:48 +0200 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <assert.h> |
| 31 | #include <ctype.h> |
| 32 | #include <string.h> |
| 33 | #include <errno.h> |
| 34 | #include <linux/input.h> |
| 35 | |
| 36 | #include "compositor.h" |
| 37 | #include "file-util.h" |
| 38 | |
| 39 | static char * |
| 40 | encode_PAM_comment_line(const char *comment) |
| 41 | { |
| 42 | size_t len = strlen(comment); |
| 43 | char *str = malloc(len + 2); |
| 44 | char *dst = str; |
| 45 | const char *src = comment; |
| 46 | const char *end = src + len; |
| 47 | |
| 48 | *dst++ = '#'; |
| 49 | *dst++ = ' '; |
| 50 | for (; src < end; src++, dst++) { |
| 51 | if (*src == '\n' || !isprint(*src)) |
| 52 | *dst = '_'; |
| 53 | else |
| 54 | *dst = *src; |
| 55 | } |
| 56 | |
| 57 | return str; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * PAM image format: |
| 62 | * http://en.wikipedia.org/wiki/Netpbm#PAM_graphics_format |
| 63 | * RGBA is in byte address order. |
| 64 | * |
| 65 | * ImageMagick 'convert' can convert a PAM image to a more common format. |
| 66 | * To view the image metadata: $ head -n7 image.pam |
| 67 | */ |
| 68 | static int |
| 69 | write_PAM_image_rgba(FILE *fp, int width, int height, |
| 70 | void *pixels, size_t size, const char *comment) |
| 71 | { |
| 72 | char *str; |
| 73 | int ret; |
| 74 | |
| 75 | assert(size == (size_t)4 * width * height); |
| 76 | |
| 77 | ret = fprintf(fp, "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\n" |
| 78 | "TUPLTYPE RGB_ALPHA\n", width, height); |
| 79 | if (ret < 0) |
| 80 | return -1; |
| 81 | |
| 82 | if (comment) { |
| 83 | str = encode_PAM_comment_line(comment); |
| 84 | ret = fprintf(fp, "%s\n", str); |
| 85 | free(str); |
| 86 | |
| 87 | if (ret < 0) |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | ret = fprintf(fp, "ENDHDR\n"); |
| 92 | if (ret < 0) |
| 93 | return -1; |
| 94 | |
| 95 | if (fwrite(pixels, 1, size, fp) != size) |
| 96 | return -1; |
| 97 | |
| 98 | if (ferror(fp)) |
| 99 | return -1; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static uint32_t |
| 105 | unmult(uint32_t c, uint32_t a) |
| 106 | { |
| 107 | return (c * 255 + a / 2) / a; |
| 108 | } |
| 109 | |
| 110 | static void |
| 111 | unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(void *pixels, size_t size) |
| 112 | { |
| 113 | uint32_t *p = pixels; |
| 114 | uint32_t *end; |
| 115 | |
| 116 | for (end = p + size / 4; p < end; p++) { |
| 117 | uint32_t v = *p; |
| 118 | uint32_t a; |
| 119 | |
| 120 | a = (v & 0xff000000) >> 24; |
| 121 | if (a == 0) { |
| 122 | *p = 0; |
| 123 | } else { |
| 124 | uint8_t *dst = (uint8_t *)p; |
| 125 | |
| 126 | dst[0] = unmult((v & 0x000000ff) >> 0, a); |
| 127 | dst[1] = unmult((v & 0x0000ff00) >> 8, a); |
| 128 | dst[2] = unmult((v & 0x00ff0000) >> 16, a); |
| 129 | dst[3] = a; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 135 | trigger_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key, |
Pekka Paalanen | 312fe5f | 2015-02-09 11:23:48 +0200 | [diff] [blame] | 136 | void *data) |
| 137 | { |
| 138 | const char *prefix = "surfaceshot-"; |
| 139 | const char *suffix = ".pam"; |
| 140 | char fname[1024]; |
| 141 | struct weston_surface *surface; |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 142 | struct weston_seat *seat = keyboard->seat; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 143 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Pekka Paalanen | 312fe5f | 2015-02-09 11:23:48 +0200 | [diff] [blame] | 144 | int width, height; |
| 145 | char desc[512]; |
| 146 | void *pixels; |
| 147 | const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */ |
| 148 | size_t sz; |
| 149 | int ret; |
| 150 | FILE *fp; |
| 151 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 152 | if (!pointer || !pointer->focus) |
Pekka Paalanen | 312fe5f | 2015-02-09 11:23:48 +0200 | [diff] [blame] | 153 | return; |
| 154 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 155 | surface = pointer->focus->surface; |
Pekka Paalanen | 312fe5f | 2015-02-09 11:23:48 +0200 | [diff] [blame] | 156 | |
| 157 | weston_surface_get_content_size(surface, &width, &height); |
| 158 | |
| 159 | if (!surface->get_label || |
| 160 | surface->get_label(surface, desc, sizeof(desc)) < 0) |
| 161 | snprintf(desc, sizeof(desc), "(unknown)"); |
| 162 | |
| 163 | weston_log("surface screenshot of %p: '%s', %dx%d\n", |
| 164 | surface, desc, width, height); |
| 165 | |
| 166 | sz = width * bytespp * height; |
| 167 | if (sz == 0) { |
| 168 | weston_log("no content for %p\n", surface); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | pixels = malloc(sz); |
| 173 | if (!pixels) { |
| 174 | weston_log("%s: failed to malloc %zu B\n", __func__, sz); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | ret = weston_surface_copy_content(surface, pixels, sz, |
| 179 | 0, 0, width, height); |
| 180 | if (ret < 0) { |
| 181 | weston_log("shooting surface %p failed\n", surface); |
| 182 | goto out; |
| 183 | } |
| 184 | |
| 185 | unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(pixels, sz); |
| 186 | |
| 187 | fp = file_create_dated(prefix, suffix, fname, sizeof(fname)); |
| 188 | if (!fp) { |
| 189 | const char *msg; |
| 190 | |
| 191 | switch (errno) { |
| 192 | case ETIME: |
| 193 | msg = "failure in datetime formatting"; |
| 194 | break; |
| 195 | default: |
| 196 | msg = strerror(errno); |
| 197 | } |
| 198 | |
| 199 | weston_log("Cannot open '%s*%s' for writing: %s\n", |
| 200 | prefix, suffix, msg); |
| 201 | goto out; |
| 202 | } |
| 203 | |
| 204 | ret = write_PAM_image_rgba(fp, width, height, pixels, sz, desc); |
| 205 | if (fclose(fp) != 0 || ret < 0) |
| 206 | weston_log("writing surface %p screenshot failed.\n", surface); |
| 207 | else |
| 208 | weston_log("successfully shot surface %p into '%s'\n", |
| 209 | surface, fname); |
| 210 | |
| 211 | out: |
| 212 | free(pixels); |
| 213 | } |
| 214 | |
| 215 | WL_EXPORT int |
| 216 | module_init(struct weston_compositor *ec, |
| 217 | int *argc, char *argv[]) |
| 218 | { |
| 219 | weston_compositor_add_debug_binding(ec, KEY_H, trigger_binding, ec); |
| 220 | |
| 221 | return 0; |
| 222 | } |