blob: 90dd49796809b2e7f2bc09a5512070e0c022ddc4 [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
Scott Moreau72c23722012-04-20 13:37:34 -060046copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030047{
48 uint8_t *end;
49
Scott Moreau72c23722012-04-20 13:37:34 -060050 end = dst + height * stride;
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030051 while (dst < end) {
Scott Moreau72c23722012-04-20 13:37:34 -060052 memcpy(dst, src, stride);
53 dst += stride;
54 src -= stride;
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030055 }
56}
57
58static void
Pekka Paalanena1d57db2012-04-17 15:02:08 +030059copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
60{
61 uint32_t *dst = vdst;
62 uint32_t *src = vsrc;
63 uint32_t *end = dst + bytes / 4;
64
65 while (dst < end) {
66 uint32_t v = *src++;
67 /* A R G B */
68 uint32_t tmp = v & 0xff00ff00;
69 tmp |= (v >> 16) & 0x000000ff;
70 tmp |= (v << 16) & 0x00ff0000;
71 *dst++ = tmp;
72 }
73}
74
75static void
Scott Moreau72c23722012-04-20 13:37:34 -060076copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
Pekka Paalanena1d57db2012-04-17 15:02:08 +030077{
78 uint8_t *end;
79
Scott Moreau72c23722012-04-20 13:37:34 -060080 end = dst + height * stride;
Pekka Paalanena1d57db2012-04-17 15:02:08 +030081 while (dst < end) {
Scott Moreau72c23722012-04-20 13:37:34 -060082 copy_row_swap_RB(dst, src, stride);
83 dst += stride;
84 src -= stride;
Pekka Paalanena1d57db2012-04-17 15:02:08 +030085 }
86}
87
88static void
Scott Moreau062be7e2012-04-20 13:37:33 -060089screenshooter_read_pixels_done(struct weston_read_pixels *base,
90 struct weston_output *output)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040091{
Scott Moreau062be7e2012-04-20 13:37:33 -060092 struct screenshooter_read_pixels *r =
93 (struct screenshooter_read_pixels *) base;
Scott Moreau72c23722012-04-20 13:37:34 -060094 int32_t stride;
Scott Moreau062be7e2012-04-20 13:37:33 -060095 uint8_t *d, *s;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040096
Scott Moreau72c23722012-04-20 13:37:34 -060097 stride = wl_shm_buffer_get_stride(r->buffer);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040098
Scott Moreau72c23722012-04-20 13:37:34 -060099 d = wl_shm_buffer_get_data(r->buffer);
100 s = r->base.data + stride * (r->buffer->height - 1);
Pekka Paalanena1d57db2012-04-17 15:02:08 +0300101
102 switch (output->compositor->read_format) {
103 case GL_BGRA_EXT:
Scott Moreau72c23722012-04-20 13:37:34 -0600104 copy_bgra_yflip(d, s, output->current->height, stride);
Pekka Paalanena1d57db2012-04-17 15:02:08 +0300105 break;
106 case GL_RGBA:
Scott Moreau72c23722012-04-20 13:37:34 -0600107 copy_rgba_yflip(d, s, output->current->height, stride);
Pekka Paalanena1d57db2012-04-17 15:02:08 +0300108 break;
109 default:
110 break;
111 }
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400112
Scott Moreau062be7e2012-04-20 13:37:33 -0600113 wl_list_remove(&r->base.link);
114
115 screenshooter_send_done(r->resource);
116 free(r->base.data);
117 free(r);
118
119}
120
121static void
122screenshooter_shoot(struct wl_client *client,
123 struct wl_resource *resource,
124 struct wl_resource *output_resource,
125 struct wl_resource *buffer_resource)
126{
127 struct weston_output *output = output_resource->data;
128 struct screenshooter_read_pixels *r;
129 struct wl_buffer *buffer = buffer_resource->data;
130 int32_t stride;
131
132 if (!wl_buffer_is_shm(buffer))
133 return;
134
135 if (buffer->width < output->current->width ||
136 buffer->height < output->current->height)
137 return;
138
139 r = malloc(sizeof *r);
140 if (r == NULL) {
141 wl_resource_post_no_memory(resource);
142 return;
143 }
144
145 r->base.x = 0;
146 r->base.y = 0;
147 r->base.width = output->current->width;
148 r->base.height = output->current->height;
149 r->base.done = screenshooter_read_pixels_done;
150 r->buffer = buffer;
151 r->resource = resource;
152 stride = buffer->width * 4;
153 r->base.data = malloc(stride * buffer->height);
154
155 if (r->base.data == NULL) {
156 free(r);
157 wl_resource_post_no_memory(resource);
158 return;
159 }
160
161 wl_list_insert(output->read_pixels_list.prev, &r->base.link);
162 weston_compositor_schedule_repaint(output->compositor);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400163}
164
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400165struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400166 screenshooter_shoot
167};
168
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400169static void
170bind_shooter(struct wl_client *client,
171 void *data, uint32_t version, uint32_t id)
172{
Scott Moreau56456d62012-03-23 16:42:04 -0600173 struct screenshooter *shooter = data;
174 struct wl_resource *resource;
175
176 resource = wl_client_add_object(client, &screenshooter_interface,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400177 &screenshooter_implementation, id, data);
Scott Moreau56456d62012-03-23 16:42:04 -0600178
179 if (client != shooter->client) {
180 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
181 "screenshooter failed: permission denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400182 wl_resource_destroy(resource);
Scott Moreau56456d62012-03-23 16:42:04 -0600183 }
184}
185
186static void
187screenshooter_sigchld(struct weston_process *process, int status)
188{
189 struct screenshooter *shooter =
Scott Moreau80d27b72012-04-04 11:49:21 -0600190 container_of(process, struct screenshooter, process);
Scott Moreau56456d62012-03-23 16:42:04 -0600191
192 shooter->client = NULL;
193}
194
195static void
196screenshooter_binding(struct wl_input_device *device, uint32_t time,
197 uint32_t key, uint32_t button, uint32_t axis,
198 int32_t state, void *data)
199{
200 struct screenshooter *shooter = data;
201 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
202
203 if (!shooter->client)
204 shooter->client = weston_client_launch(shooter->ec,
Scott Moreau80d27b72012-04-04 11:49:21 -0600205 &shooter->process,
Scott Moreau56456d62012-03-23 16:42:04 -0600206 screenshooter_exe, screenshooter_sigchld);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400207}
208
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400209static void
210screenshooter_destroy(struct wl_listener *listener, void *data)
211{
212 struct screenshooter *shooter =
213 container_of(listener, struct screenshooter, destroy_listener);
214
215 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
216 free(shooter);
217}
218
219void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500220screenshooter_create(struct weston_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400221{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400222 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400223
224 shooter = malloc(sizeof *shooter);
225 if (shooter == NULL)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400226 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400227
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400228 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400229 shooter->base.implementation =
230 (void(**)(void)) &screenshooter_implementation;
231 shooter->ec = ec;
Scott Moreau56456d62012-03-23 16:42:04 -0600232 shooter->client = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400233
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200234 shooter->global = wl_display_add_global(ec->wl_display,
235 &screenshooter_interface,
236 shooter, bind_shooter);
Scott Moreau56456d62012-03-23 16:42:04 -0600237 weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
238 screenshooter_binding, shooter);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200239
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400240 shooter->destroy_listener.notify = screenshooter_destroy;
241 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200242}