blob: 3d93ac58921a9b63a15d222f92fbce5ec34469f9 [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
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020036#include <libweston/libweston.h>
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +010037#include "compositor/weston.h"
Pekka Paalanenc232f8d2019-04-05 16:09:45 +030038#include "shared/file-util.h"
Marius Vlad56f3a682019-07-10 14:48:39 +030039#include "libweston-internal.h"
Pekka Paalanen312fe5f2015-02-09 11:23:48 +020040
41static char *
42encode_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 */
70static int
71write_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
106static uint32_t
107unmult(uint32_t c, uint32_t a)
108{
109 return (c * 255 + a / 2) / a;
110}
111
112static void
113unpremultiply_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
136static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200137trigger_binding(struct weston_keyboard *keyboard, const struct timespec *time,
138 uint32_t key, void *data)
Pekka Paalanen312fe5f2015-02-09 11:23:48 +0200139{
140 const char *prefix = "surfaceshot-";
141 const char *suffix = ".pam";
142 char fname[1024];
143 struct weston_surface *surface;
Derek Foreman8ae2db52015-07-15 13:00:36 -0500144 struct weston_seat *seat = keyboard->seat;
Derek Foreman1281a362015-07-31 16:55:32 -0500145 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Pekka Paalanen312fe5f2015-02-09 11:23:48 +0200146 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
Derek Foreman1281a362015-07-31 16:55:32 -0500154 if (!pointer || !pointer->focus)
Pekka Paalanen312fe5f2015-02-09 11:23:48 +0200155 return;
156
Derek Foreman1281a362015-07-31 16:55:32 -0500157 surface = pointer->focus->surface;
Pekka Paalanen312fe5f2015-02-09 11:23:48 +0200158
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
Aleksander Morgado72032ac2018-01-23 01:05:21 +0100189 fp = file_create_dated(NULL, prefix, suffix, fname, sizeof(fname));
Pekka Paalanen312fe5f2015-02-09 11:23:48 +0200190 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
213out:
214 free(pixels);
215}
216
217WL_EXPORT int
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100218wet_module_init(struct weston_compositor *ec,
219 int *argc, char *argv[])
Pekka Paalanen312fe5f2015-02-09 11:23:48 +0200220{
221 weston_compositor_add_debug_binding(ec, KEY_H, trigger_binding, ec);
222
223 return 0;
224}