summaryrefslogtreecommitdiff
path: root/tests/surface-screenshot.c
blob: a54ae22e2edd38bc62e1d888293cc3326902a3b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright © 2015 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <linux/input.h>

#include "compositor.h"
#include "file-util.h"

static char *
encode_PAM_comment_line(const char *comment)
{
	size_t len = strlen(comment);
	char *str = malloc(len + 2);
	char *dst = str;
	const char *src = comment;
	const char *end = src + len;

	*dst++ = '#';
	*dst++ = ' ';
	for (; src < end; src++, dst++) {
		if (*src == '\n' || !isprint(*src))
			*dst = '_';
		else
			*dst = *src;
	}

	return str;
}

/*
 * PAM image format:
 * http://en.wikipedia.org/wiki/Netpbm#PAM_graphics_format
 * RGBA is in byte address order.
 *
 * ImageMagick 'convert' can convert a PAM image to a more common format.
 * To view the image metadata: $ head -n7 image.pam
 */
static int
write_PAM_image_rgba(FILE *fp, int width, int height,
		     void *pixels, size_t size, const char *comment)
{
	char *str;
	int ret;

	assert(size == (size_t)4 * width * height);

	ret = fprintf(fp, "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\n"
		      "TUPLTYPE RGB_ALPHA\n", width, height);
	if (ret < 0)
		return -1;

	if (comment) {
		str = encode_PAM_comment_line(comment);
		ret = fprintf(fp, "%s\n", str);
		free(str);

		if (ret < 0)
			return -1;
	}

	ret = fprintf(fp, "ENDHDR\n");
	if (ret < 0)
		return -1;

	if (fwrite(pixels, 1, size, fp) != size)
		return -1;

	if (ferror(fp))
		return -1;

	return 0;
}

static uint32_t
unmult(uint32_t c, uint32_t a)
{
	return (c * 255 + a / 2) / a;
}

static void
unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(void *pixels, size_t size)
{
	uint32_t *p = pixels;
	uint32_t *end;

	for (end = p + size / 4; p < end; p++) {
		uint32_t v = *p;
		uint32_t a;

		a = (v & 0xff000000) >> 24;
		if (a == 0) {
			*p = 0;
		} else {
			uint8_t *dst = (uint8_t *)p;

			dst[0] = unmult((v & 0x000000ff) >> 0, a);
			dst[1] = unmult((v & 0x0000ff00) >> 8, a);
			dst[2] = unmult((v & 0x00ff0000) >> 16, a);
			dst[3] = a;
		}
	}
}

static void
trigger_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
		void *data)
{
	const char *prefix = "surfaceshot-";
	const char *suffix = ".pam";
	char fname[1024];
	struct weston_surface *surface;
	struct weston_seat *seat = keyboard->seat;
	int width, height;
	char desc[512];
	void *pixels;
	const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
	size_t sz;
	int ret;
	FILE *fp;

	if (seat->pointer_device_count == 0 ||
	    !seat->pointer ||
	    !seat->pointer->focus)
		return;

	surface = seat->pointer->focus->surface;

	weston_surface_get_content_size(surface, &width, &height);

	if (!surface->get_label ||
	    surface->get_label(surface, desc, sizeof(desc)) < 0)
		snprintf(desc, sizeof(desc), "(unknown)");

	weston_log("surface screenshot of %p: '%s', %dx%d\n",
		   surface, desc, width, height);

	sz = width * bytespp * height;
	if (sz == 0) {
		weston_log("no content for %p\n", surface);
		return;
	}

	pixels = malloc(sz);
	if (!pixels) {
		weston_log("%s: failed to malloc %zu B\n", __func__, sz);
		return;
	}

	ret = weston_surface_copy_content(surface, pixels, sz,
					  0, 0, width, height);
	if (ret < 0) {
		weston_log("shooting surface %p failed\n", surface);
		goto out;
	}

	unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(pixels, sz);

	fp = file_create_dated(prefix, suffix, fname, sizeof(fname));
	if (!fp) {
		const char *msg;

		switch (errno) {
		case ETIME:
			msg = "failure in datetime formatting";
			break;
		default:
			msg = strerror(errno);
		}

		weston_log("Cannot open '%s*%s' for writing: %s\n",
			   prefix, suffix, msg);
		goto out;
	}

	ret = write_PAM_image_rgba(fp, width, height, pixels, sz, desc);
	if (fclose(fp) != 0 || ret < 0)
		weston_log("writing surface %p screenshot failed.\n", surface);
	else
		weston_log("successfully shot surface %p into '%s'\n",
			   surface, fname);

out:
	free(pixels);
}

WL_EXPORT int
module_init(struct weston_compositor *ec,
	    int *argc, char *argv[])
{
	weston_compositor_add_debug_binding(ec, KEY_H, trigger_binding, ec);

	return 0;
}