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
|
/*
* Copyright © 2008 Kristian Høgsberg
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <cairo.h>
#include <wayland-client.h>
#include "screenshooter-client-protocol.h"
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
/* The screenshooter is a good example of a custom object exposed by
* the compositor and serves as a test bed for implementing client
* side marshalling outside libwayland.so */
static struct wl_shm *shm;
static struct screenshooter *screenshooter;
static struct wl_list output_list;
struct screenshooter_output {
struct wl_output *output;
struct wl_buffer *buffer;
int width, height, offset_x, offset_y;
struct wl_list link;
};
static void
display_handle_geometry(void *data,
struct wl_output *wl_output,
int x,
int y,
int physical_width,
int physical_height,
int subpixel,
const char *make,
const char *model)
{
struct screenshooter_output *output;
output = wl_output_get_user_data(wl_output);
if (wl_output == output->output) {
output->offset_x = x;
output->offset_y = y;
}
}
static void
display_handle_mode(void *data,
struct wl_output *wl_output,
uint32_t flags,
int width,
int height,
int refresh)
{
struct screenshooter_output *output;
output = wl_output_get_user_data(wl_output);
if (wl_output == output->output &&
(flags & WL_OUTPUT_MODE_CURRENT)) {
output->width = width;
output->height = height;
}
}
static const struct wl_output_listener output_listener = {
display_handle_geometry,
display_handle_mode
};
static void
handle_global(struct wl_display *display, uint32_t id,
const char *interface, uint32_t version, void *data)
{
static struct screenshooter_output *output;
if (strcmp(interface, "wl_output") == 0) {
output = malloc(sizeof *output);
output->output = wl_display_bind(display, id, &wl_output_interface);
wl_list_insert(&output_list, &output->link);
wl_output_add_listener(output->output, &output_listener, output);
} else if (strcmp(interface, "wl_shm") == 0) {
shm = wl_display_bind(display, id, &wl_shm_interface);
} else if (strcmp(interface, "screenshooter") == 0) {
screenshooter = wl_display_bind(display, id, &screenshooter_interface);
}
}
static struct wl_buffer *
create_shm_buffer(int width, int height, void **data_out)
{
char filename[] = "/tmp/wayland-shm-XXXXXX";
struct wl_shm_pool *pool;
struct wl_buffer *buffer;
int fd, size, stride;
void *data;
fd = mkstemp(filename);
if (fd < 0) {
fprintf(stderr, "open %s failed: %m\n", filename);
return NULL;
}
stride = width * 4;
size = stride * height;
if (ftruncate(fd, size) < 0) {
fprintf(stderr, "ftruncate failed: %m\n");
close(fd);
return NULL;
}
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
unlink(filename);
if (data == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
return NULL;
}
pool = wl_shm_create_pool(shm, fd, size);
close(fd);
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
WL_SHM_FORMAT_XRGB8888);
wl_shm_pool_destroy(pool);
*data_out = data;
return buffer;
}
static void
write_png(int width, int height, void *data)
{
cairo_surface_t *surface;
surface = cairo_image_surface_create_for_data(data,
CAIRO_FORMAT_ARGB32,
width, height, width * 4);
cairo_surface_write_to_png(surface, "wayland-screenshot.png");
cairo_surface_destroy(surface);
}
int main(int argc, char *argv[])
{
struct wl_display *display;
struct wl_buffer *buffer;
void *data = NULL;
struct screenshooter_output *output, *next;
int ss_area_width = 0, ss_area_height = 0, num_outputs = 0;
display = wl_display_connect(NULL);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
wl_list_init(&output_list);
wl_display_add_global_listener(display, handle_global, &screenshooter);
wl_display_iterate(display, WL_DISPLAY_READABLE);
wl_display_roundtrip(display);
if (screenshooter == NULL) {
fprintf(stderr, "display doesn't support screenshooter\n");
return -1;
}
wl_list_for_each(output, &output_list, link) {
if (!num_outputs) {
ss_area_width = output->width + output->offset_x;
ss_area_height = output->height + output->offset_y;
}
else {
ss_area_width = MAX(ss_area_width, output->offset_x + output->width);
ss_area_height = MAX(ss_area_height, output->offset_y + output->height);
}
num_outputs++;
}
buffer = create_shm_buffer(ss_area_width, ss_area_height, &data);
wl_list_for_each(output, &output_list, link) {
screenshooter_shoot(screenshooter, output->output, buffer);
}
wl_display_roundtrip(display);
write_png(ss_area_width, ss_area_height, data);
wl_list_for_each_safe(output, next, &output_list, link) {
free(output);
}
return 0;
}
|