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