1 /*
2 * Copyright © 2015 Collabora, Ltd.
3 *
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:
11 *
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.
24 */
25
26 #include "config.h"
27
28 #include <stdint.h>
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 <libweston/libweston.h>
37 #include "compositor/weston.h"
38 #include "shared/file-util.h"
39 #include "libweston-internal.h"
40
41 static char *
encode_PAM_comment_line(const char * comment)42 encode_PAM_comment_line(const char *comment)
43 {
44 size_t len = strlen(comment);
45 char *str = malloc(len + 2);
46 char *dst = str;
47 const char *src = comment;
48 const char *end = src + len;
49
50 *dst++ = '#';
51 *dst++ = ' ';
52 for (; src < end; src++, dst++) {
53 if (*src == '\n' || !isprint(*src))
54 *dst = '_';
55 else
56 *dst = *src;
57 }
58
59 return str;
60 }
61
62 /*
63 * PAM image format:
64 * http://en.wikipedia.org/wiki/Netpbm#PAM_graphics_format
65 * RGBA is in byte address order.
66 *
67 * ImageMagick 'convert' can convert a PAM image to a more common format.
68 * To view the image metadata: $ head -n7 image.pam
69 */
70 static int
write_PAM_image_rgba(FILE * fp,int width,int height,void * pixels,size_t size,const char * comment)71 write_PAM_image_rgba(FILE *fp, int width, int height,
72 void *pixels, size_t size, const char *comment)
73 {
74 char *str;
75 int ret;
76
77 assert(size == (size_t)4 * width * height);
78
79 ret = fprintf(fp, "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\n"
80 "TUPLTYPE RGB_ALPHA\n", width, height);
81 if (ret < 0)
82 return -1;
83
84 if (comment) {
85 str = encode_PAM_comment_line(comment);
86 ret = fprintf(fp, "%s\n", str);
87 free(str);
88
89 if (ret < 0)
90 return -1;
91 }
92
93 ret = fprintf(fp, "ENDHDR\n");
94 if (ret < 0)
95 return -1;
96
97 if (fwrite(pixels, 1, size, fp) != size)
98 return -1;
99
100 if (ferror(fp))
101 return -1;
102
103 return 0;
104 }
105
106 static uint32_t
unmult(uint32_t c,uint32_t a)107 unmult(uint32_t c, uint32_t a)
108 {
109 return (c * 255 + a / 2) / a;
110 }
111
112 static void
unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(void * pixels,size_t size)113 unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(void *pixels, size_t size)
114 {
115 uint32_t *p = pixels;
116 uint32_t *end;
117
118 for (end = p + size / 4; p < end; p++) {
119 uint32_t v = *p;
120 uint32_t a;
121
122 a = (v & 0xff000000) >> 24;
123 if (a == 0) {
124 *p = 0;
125 } else {
126 uint8_t *dst = (uint8_t *)p;
127
128 dst[0] = unmult((v & 0x000000ff) >> 0, a);
129 dst[1] = unmult((v & 0x0000ff00) >> 8, a);
130 dst[2] = unmult((v & 0x00ff0000) >> 16, a);
131 dst[3] = a;
132 }
133 }
134 }
135
136 static void
trigger_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)137 trigger_binding(struct weston_keyboard *keyboard, const struct timespec *time,
138 uint32_t key, void *data)
139 {
140 const char *prefix = "surfaceshot-";
141 const char *suffix = ".pam";
142 char fname[1024];
143 struct weston_surface *surface;
144 struct weston_seat *seat = keyboard->seat;
145 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
146 int width, height;
147 char desc[512];
148 void *pixels;
149 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
150 size_t sz;
151 int ret;
152 FILE *fp;
153
154 if (!pointer || !pointer->focus)
155 return;
156
157 surface = pointer->focus->surface;
158
159 weston_surface_get_content_size(surface, &width, &height);
160
161 if (!surface->get_label ||
162 surface->get_label(surface, desc, sizeof(desc)) < 0)
163 snprintf(desc, sizeof(desc), "(unknown)");
164
165 weston_log("surface screenshot of %p: '%s', %dx%d\n",
166 surface, desc, width, height);
167
168 sz = width * bytespp * height;
169 if (sz == 0) {
170 weston_log("no content for %p\n", surface);
171 return;
172 }
173
174 pixels = malloc(sz);
175 if (!pixels) {
176 weston_log("%s: failed to malloc %zu B\n", __func__, sz);
177 return;
178 }
179
180 ret = weston_surface_copy_content(surface, pixels, sz,
181 0, 0, width, height);
182 if (ret < 0) {
183 weston_log("shooting surface %p failed\n", surface);
184 goto out;
185 }
186
187 unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(pixels, sz);
188
189 fp = file_create_dated(NULL, prefix, suffix, fname, sizeof(fname));
190 if (!fp) {
191 const char *msg;
192
193 switch (errno) {
194 case ETIME:
195 msg = "failure in datetime formatting";
196 break;
197 default:
198 msg = strerror(errno);
199 }
200
201 weston_log("Cannot open '%s*%s' for writing: %s\n",
202 prefix, suffix, msg);
203 goto out;
204 }
205
206 ret = write_PAM_image_rgba(fp, width, height, pixels, sz, desc);
207 if (fclose(fp) != 0 || ret < 0)
208 weston_log("writing surface %p screenshot failed.\n", surface);
209 else
210 weston_log("successfully shot surface %p into '%s'\n",
211 surface, fname);
212
213 out:
214 free(pixels);
215 }
216
217 WL_EXPORT int
wet_module_init(struct weston_compositor * ec,int * argc,char * argv[])218 wet_module_init(struct weston_compositor *ec,
219 int *argc, char *argv[])
220 {
221 weston_compositor_add_debug_binding(ec, KEY_H, trigger_binding, ec);
222
223 return 0;
224 }
225