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