blob: 7ba475d9fb61420a82385681ef5969321432937c [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04004 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040013 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040014 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040021 */
22
23#include <stdlib.h>
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040024#include <string.h>
Scott Moreau56456d62012-03-23 16:42:04 -060025#include <linux/input.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040026
27#include "compositor.h"
Kristian Høgsbergc5dcb902010-09-14 15:53:32 -040028#include "screenshooter-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040029
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040030struct screenshooter {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031 struct wl_object base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050032 struct weston_compositor *ec;
Pekka Paalanen35ce06d2012-01-03 11:39:13 +020033 struct wl_global *global;
Scott Moreau56456d62012-03-23 16:42:04 -060034 struct wl_client *client;
Scott Moreau80d27b72012-04-04 11:49:21 -060035 struct weston_process process;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040036 struct wl_listener destroy_listener;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040037};
38
Scott Moreau062be7e2012-04-20 13:37:33 -060039struct screenshooter_read_pixels {
40 struct weston_read_pixels base;
41 struct wl_buffer *buffer;
42 struct wl_resource *resource;
43};
44
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045static void
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030046copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height,
47 int dst_stride, int src_stride)
48{
49 uint8_t *end;
50
51 end = dst + height * dst_stride;
52 while (dst < end) {
53 memcpy(dst, src, src_stride);
54 dst += dst_stride;
55 src -= src_stride;
56 }
57}
58
59static void
Pekka Paalanena1d57db2012-04-17 15:02:08 +030060copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
61{
62 uint32_t *dst = vdst;
63 uint32_t *src = vsrc;
64 uint32_t *end = dst + bytes / 4;
65
66 while (dst < end) {
67 uint32_t v = *src++;
68 /* A R G B */
69 uint32_t tmp = v & 0xff00ff00;
70 tmp |= (v >> 16) & 0x000000ff;
71 tmp |= (v << 16) & 0x00ff0000;
72 *dst++ = tmp;
73 }
74}
75
76static void
77copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height,
78 int dst_stride, int src_stride)
79{
80 uint8_t *end;
81
82 end = dst + height * dst_stride;
83 while (dst < end) {
84 copy_row_swap_RB(dst, src, src_stride);
85 dst += dst_stride;
86 src -= src_stride;
87 }
88}
89
90static void
Scott Moreau062be7e2012-04-20 13:37:33 -060091screenshooter_read_pixels_done(struct weston_read_pixels *base,
92 struct weston_output *output)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040093{
Scott Moreau062be7e2012-04-20 13:37:33 -060094 struct screenshooter_read_pixels *r =
95 (struct screenshooter_read_pixels *) base;
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030096 int32_t buffer_stride, output_stride;
Scott Moreau062be7e2012-04-20 13:37:33 -060097 uint8_t *d, *s;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040098
Scott Moreau062be7e2012-04-20 13:37:33 -060099 buffer_stride = wl_shm_buffer_get_stride(r->buffer);
Scott Moreau80d27b72012-04-04 11:49:21 -0600100 output_stride = output->current->width * 4;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400101
Scott Moreau062be7e2012-04-20 13:37:33 -0600102 d = wl_shm_buffer_get_data(r->buffer) +
103 output->y * buffer_stride + output->x * 4;
104 s = r->base.data + output_stride * (output->current->height - 1);
Pekka Paalanena1d57db2012-04-17 15:02:08 +0300105
106 switch (output->compositor->read_format) {
107 case GL_BGRA_EXT:
108 copy_bgra_yflip(d, s, output->current->height,
109 buffer_stride, output_stride);
110 break;
111 case GL_RGBA:
112 copy_rgba_yflip(d, s, output->current->height,
113 buffer_stride, output_stride);
114 break;
115 default:
116 break;
117 }
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400118
Scott Moreau062be7e2012-04-20 13:37:33 -0600119 wl_list_remove(&r->base.link);
120
121 screenshooter_send_done(r->resource);
122 free(r->base.data);
123 free(r);
124
125}
126
127static void
128screenshooter_shoot(struct wl_client *client,
129 struct wl_resource *resource,
130 struct wl_resource *output_resource,
131 struct wl_resource *buffer_resource)
132{
133 struct weston_output *output = output_resource->data;
134 struct screenshooter_read_pixels *r;
135 struct wl_buffer *buffer = buffer_resource->data;
136 int32_t stride;
137
138 if (!wl_buffer_is_shm(buffer))
139 return;
140
141 if (buffer->width < output->current->width ||
142 buffer->height < output->current->height)
143 return;
144
145 r = malloc(sizeof *r);
146 if (r == NULL) {
147 wl_resource_post_no_memory(resource);
148 return;
149 }
150
151 r->base.x = 0;
152 r->base.y = 0;
153 r->base.width = output->current->width;
154 r->base.height = output->current->height;
155 r->base.done = screenshooter_read_pixels_done;
156 r->buffer = buffer;
157 r->resource = resource;
158 stride = buffer->width * 4;
159 r->base.data = malloc(stride * buffer->height);
160
161 if (r->base.data == NULL) {
162 free(r);
163 wl_resource_post_no_memory(resource);
164 return;
165 }
166
167 wl_list_insert(output->read_pixels_list.prev, &r->base.link);
168 weston_compositor_schedule_repaint(output->compositor);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400169}
170
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400171struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400172 screenshooter_shoot
173};
174
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400175static void
176bind_shooter(struct wl_client *client,
177 void *data, uint32_t version, uint32_t id)
178{
Scott Moreau56456d62012-03-23 16:42:04 -0600179 struct screenshooter *shooter = data;
180 struct wl_resource *resource;
181
182 resource = wl_client_add_object(client, &screenshooter_interface,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400183 &screenshooter_implementation, id, data);
Scott Moreau56456d62012-03-23 16:42:04 -0600184
185 if (client != shooter->client) {
186 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
187 "screenshooter failed: permission denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400188 wl_resource_destroy(resource);
Scott Moreau56456d62012-03-23 16:42:04 -0600189 }
190}
191
192static void
193screenshooter_sigchld(struct weston_process *process, int status)
194{
195 struct screenshooter *shooter =
Scott Moreau80d27b72012-04-04 11:49:21 -0600196 container_of(process, struct screenshooter, process);
Scott Moreau56456d62012-03-23 16:42:04 -0600197
198 shooter->client = NULL;
199}
200
201static void
202screenshooter_binding(struct wl_input_device *device, uint32_t time,
203 uint32_t key, uint32_t button, uint32_t axis,
204 int32_t state, void *data)
205{
206 struct screenshooter *shooter = data;
207 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
208
209 if (!shooter->client)
210 shooter->client = weston_client_launch(shooter->ec,
Scott Moreau80d27b72012-04-04 11:49:21 -0600211 &shooter->process,
Scott Moreau56456d62012-03-23 16:42:04 -0600212 screenshooter_exe, screenshooter_sigchld);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400213}
214
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400215static void
216screenshooter_destroy(struct wl_listener *listener, void *data)
217{
218 struct screenshooter *shooter =
219 container_of(listener, struct screenshooter, destroy_listener);
220
221 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
222 free(shooter);
223}
224
225void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500226screenshooter_create(struct weston_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400227{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400228 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400229
230 shooter = malloc(sizeof *shooter);
231 if (shooter == NULL)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400232 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400233
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400234 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400235 shooter->base.implementation =
236 (void(**)(void)) &screenshooter_implementation;
237 shooter->ec = ec;
Scott Moreau56456d62012-03-23 16:42:04 -0600238 shooter->client = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400239
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200240 shooter->global = wl_display_add_global(ec->wl_display,
241 &screenshooter_interface,
242 shooter, bind_shooter);
Scott Moreau56456d62012-03-23 16:42:04 -0600243 weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
244 screenshooter_binding, shooter);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200245
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400246 shooter->destroy_listener.notify = screenshooter_destroy;
247 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200248}