2 * Copyright © 2015 Collabora, Ltd.
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:
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.
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
34 #include <linux/input.h>
36 #include "compositor.h"
37 #include "file-util.h"
40 encode_PAM_comment_line(const char *comment)
42 size_t len = strlen(comment);
43 char *str = malloc(len + 2);
45 const char *src = comment;
46 const char *end = src + len;
50 for (; src < end; src++, dst++) {
51 if (*src == '\n' || !isprint(*src))
62 * http://en.wikipedia.org/wiki/Netpbm#PAM_graphics_format
63 * RGBA is in byte address order.
65 * ImageMagick 'convert' can convert a PAM image to a more common format.
66 * To view the image metadata: $ head -n7 image.pam
69 write_PAM_image_rgba(FILE *fp, int width, int height,
70 void *pixels, size_t size, const char *comment)
75 assert(size == (size_t)4 * width * height);
77 ret = fprintf(fp, "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\n"
78 "TUPLTYPE RGB_ALPHA\n", width, height);
83 str = encode_PAM_comment_line(comment);
84 ret = fprintf(fp, "%s\n", str);
91 ret = fprintf(fp, "ENDHDR\n");
95 if (fwrite(pixels, 1, size, fp) != size)
105 unmult(uint32_t c, uint32_t a)
107 return (c * 255 + a / 2) / a;
111 unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(void *pixels, size_t size)
113 uint32_t *p = pixels;
116 for (end = p + size / 4; p < end; p++) {
120 a = (v & 0xff000000) >> 24;
124 uint8_t *dst = (uint8_t *)p;
126 dst[0] = unmult((v & 0x000000ff) >> 0, a);
127 dst[1] = unmult((v & 0x0000ff00) >> 8, a);
128 dst[2] = unmult((v & 0x00ff0000) >> 16, a);
135 trigger_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
138 const char *prefix = "surfaceshot-";
139 const char *suffix = ".pam";
141 struct weston_surface *surface;
142 struct weston_seat *seat = keyboard->seat;
143 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
147 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
152 if (!pointer || !pointer->focus)
155 surface = pointer->focus->surface;
157 weston_surface_get_content_size(surface, &width, &height);
159 if (!surface->get_label ||
160 surface->get_label(surface, desc, sizeof(desc)) < 0)
161 snprintf(desc, sizeof(desc), "(unknown)");
163 weston_log("surface screenshot of %p: '%s', %dx%d\n",
164 surface, desc, width, height);
166 sz = width * bytespp * height;
168 weston_log("no content for %p\n", surface);
174 weston_log("%s: failed to malloc %zu B\n", __func__, sz);
178 ret = weston_surface_copy_content(surface, pixels, sz,
179 0, 0, width, height);
181 weston_log("shooting surface %p failed\n", surface);
185 unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(pixels, sz);
187 fp = file_create_dated(prefix, suffix, fname, sizeof(fname));
193 msg = "failure in datetime formatting";
196 msg = strerror(errno);
199 weston_log("Cannot open '%s*%s' for writing: %s\n",
200 prefix, suffix, msg);
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);
208 weston_log("successfully shot surface %p into '%s'\n",
216 module_init(struct weston_compositor *ec,
217 int *argc, char *argv[])
219 weston_compositor_add_debug_binding(ec, KEY_H, trigger_binding, ec);