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 "compositor/weston.h"
38 #include "file-util.h"
41 encode_PAM_comment_line(const char *comment)
43 size_t len = strlen(comment);
44 char *str = malloc(len + 2);
46 const char *src = comment;
47 const char *end = src + len;
51 for (; src < end; src++, dst++) {
52 if (*src == '\n' || !isprint(*src))
63 * http://en.wikipedia.org/wiki/Netpbm#PAM_graphics_format
64 * RGBA is in byte address order.
66 * ImageMagick 'convert' can convert a PAM image to a more common format.
67 * To view the image metadata: $ head -n7 image.pam
70 write_PAM_image_rgba(FILE *fp, int width, int height,
71 void *pixels, size_t size, const char *comment)
76 assert(size == (size_t)4 * width * height);
78 ret = fprintf(fp, "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\n"
79 "TUPLTYPE RGB_ALPHA\n", width, height);
84 str = encode_PAM_comment_line(comment);
85 ret = fprintf(fp, "%s\n", str);
92 ret = fprintf(fp, "ENDHDR\n");
96 if (fwrite(pixels, 1, size, fp) != size)
106 unmult(uint32_t c, uint32_t a)
108 return (c * 255 + a / 2) / a;
112 unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(void *pixels, size_t size)
114 uint32_t *p = pixels;
117 for (end = p + size / 4; p < end; p++) {
121 a = (v & 0xff000000) >> 24;
125 uint8_t *dst = (uint8_t *)p;
127 dst[0] = unmult((v & 0x000000ff) >> 0, a);
128 dst[1] = unmult((v & 0x0000ff00) >> 8, a);
129 dst[2] = unmult((v & 0x00ff0000) >> 16, a);
136 trigger_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
139 const char *prefix = "surfaceshot-";
140 const char *suffix = ".pam";
142 struct weston_surface *surface;
143 struct weston_seat *seat = keyboard->seat;
144 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
148 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
153 if (!pointer || !pointer->focus)
156 surface = pointer->focus->surface;
158 weston_surface_get_content_size(surface, &width, &height);
160 if (!surface->get_label ||
161 surface->get_label(surface, desc, sizeof(desc)) < 0)
162 snprintf(desc, sizeof(desc), "(unknown)");
164 weston_log("surface screenshot of %p: '%s', %dx%d\n",
165 surface, desc, width, height);
167 sz = width * bytespp * height;
169 weston_log("no content for %p\n", surface);
175 weston_log("%s: failed to malloc %zu B\n", __func__, sz);
179 ret = weston_surface_copy_content(surface, pixels, sz,
180 0, 0, width, height);
182 weston_log("shooting surface %p failed\n", surface);
186 unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(pixels, sz);
188 fp = file_create_dated(prefix, suffix, fname, sizeof(fname));
194 msg = "failure in datetime formatting";
197 msg = strerror(errno);
200 weston_log("Cannot open '%s*%s' for writing: %s\n",
201 prefix, suffix, msg);
205 ret = write_PAM_image_rgba(fp, width, height, pixels, sz, desc);
206 if (fclose(fp) != 0 || ret < 0)
207 weston_log("writing surface %p screenshot failed.\n", surface);
209 weston_log("successfully shot surface %p into '%s'\n",
217 wet_module_init(struct weston_compositor *ec,
218 int *argc, char *argv[])
220 weston_compositor_add_debug_binding(ec, KEY_H, trigger_binding, ec);