blob: b4b341b4a166bb803e80a84e1cb8fdc0ce2e4da5 [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
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040039static void
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030040copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height,
41 int dst_stride, int src_stride)
42{
43 uint8_t *end;
44
45 end = dst + height * dst_stride;
46 while (dst < end) {
47 memcpy(dst, src, src_stride);
48 dst += dst_stride;
49 src -= src_stride;
50 }
51}
52
53static void
Pekka Paalanena1d57db2012-04-17 15:02:08 +030054copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
55{
56 uint32_t *dst = vdst;
57 uint32_t *src = vsrc;
58 uint32_t *end = dst + bytes / 4;
59
60 while (dst < end) {
61 uint32_t v = *src++;
62 /* A R G B */
63 uint32_t tmp = v & 0xff00ff00;
64 tmp |= (v >> 16) & 0x000000ff;
65 tmp |= (v << 16) & 0x00ff0000;
66 *dst++ = tmp;
67 }
68}
69
70static void
71copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height,
72 int dst_stride, int src_stride)
73{
74 uint8_t *end;
75
76 end = dst + height * dst_stride;
77 while (dst < end) {
78 copy_row_swap_RB(dst, src, src_stride);
79 dst += dst_stride;
80 src -= src_stride;
81 }
82}
83
84static void
Kristian Høgsberg85449032011-05-02 12:11:07 -040085screenshooter_shoot(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -040086 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040087 struct wl_resource *output_resource,
88 struct wl_resource *buffer_resource)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040089{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050090 struct weston_output *output = output_resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040091 struct wl_buffer *buffer = buffer_resource->data;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040092 uint8_t *tmp, *d, *s;
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030093 int32_t buffer_stride, output_stride;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040094
Kristian Høgsberg85449032011-05-02 12:11:07 -040095 if (!wl_buffer_is_shm(buffer))
96 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040097
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040098 if (buffer->width < output->current->width ||
99 buffer->height < output->current->height)
Kristian Høgsberg85449032011-05-02 12:11:07 -0400100 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400101
Scott Moreau80d27b72012-04-04 11:49:21 -0600102 buffer_stride = wl_shm_buffer_get_stride(buffer);
103 output_stride = output->current->width * 4;
104 tmp = malloc(output_stride * output->current->height);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400105 if (tmp == NULL) {
106 wl_resource_post_no_memory(resource);
107 return;
108 }
109
Kristian Høgsberg85449032011-05-02 12:11:07 -0400110 glPixelStorei(GL_PACK_ALIGNMENT, 1);
Scott Moreau80d27b72012-04-04 11:49:21 -0600111 output->read_pixels(output, tmp);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400112
Scott Moreau80d27b72012-04-04 11:49:21 -0600113 d = wl_shm_buffer_get_data(buffer) + output->y * buffer_stride +
114 output->x * 4;
115 s = tmp + output_stride * (output->current->height - 1);
Pekka Paalanena1d57db2012-04-17 15:02:08 +0300116
117 switch (output->compositor->read_format) {
118 case GL_BGRA_EXT:
119 copy_bgra_yflip(d, s, output->current->height,
120 buffer_stride, output_stride);
121 break;
122 case GL_RGBA:
123 copy_rgba_yflip(d, s, output->current->height,
124 buffer_stride, output_stride);
125 break;
126 default:
127 break;
128 }
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400129
130 free(tmp);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400131}
132
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400133struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400134 screenshooter_shoot
135};
136
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400137static void
138bind_shooter(struct wl_client *client,
139 void *data, uint32_t version, uint32_t id)
140{
Scott Moreau56456d62012-03-23 16:42:04 -0600141 struct screenshooter *shooter = data;
142 struct wl_resource *resource;
143
144 resource = wl_client_add_object(client, &screenshooter_interface,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400145 &screenshooter_implementation, id, data);
Scott Moreau56456d62012-03-23 16:42:04 -0600146
147 if (client != shooter->client) {
148 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
149 "screenshooter failed: permission denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400150 wl_resource_destroy(resource);
Scott Moreau56456d62012-03-23 16:42:04 -0600151 }
152}
153
154static void
155screenshooter_sigchld(struct weston_process *process, int status)
156{
157 struct screenshooter *shooter =
Scott Moreau80d27b72012-04-04 11:49:21 -0600158 container_of(process, struct screenshooter, process);
Scott Moreau56456d62012-03-23 16:42:04 -0600159
160 shooter->client = NULL;
161}
162
163static void
164screenshooter_binding(struct wl_input_device *device, uint32_t time,
165 uint32_t key, uint32_t button, uint32_t axis,
166 int32_t state, void *data)
167{
168 struct screenshooter *shooter = data;
169 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
170
171 if (!shooter->client)
172 shooter->client = weston_client_launch(shooter->ec,
Scott Moreau80d27b72012-04-04 11:49:21 -0600173 &shooter->process,
Scott Moreau56456d62012-03-23 16:42:04 -0600174 screenshooter_exe, screenshooter_sigchld);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400175}
176
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400177static void
178screenshooter_destroy(struct wl_listener *listener, void *data)
179{
180 struct screenshooter *shooter =
181 container_of(listener, struct screenshooter, destroy_listener);
182
183 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
184 free(shooter);
185}
186
187void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500188screenshooter_create(struct weston_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400189{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400190 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400191
192 shooter = malloc(sizeof *shooter);
193 if (shooter == NULL)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400194 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400195
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400196 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400197 shooter->base.implementation =
198 (void(**)(void)) &screenshooter_implementation;
199 shooter->ec = ec;
Scott Moreau56456d62012-03-23 16:42:04 -0600200 shooter->client = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400201
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200202 shooter->global = wl_display_add_global(ec->wl_display,
203 &screenshooter_interface,
204 shooter, bind_shooter);
Scott Moreau56456d62012-03-23 16:42:04 -0600205 weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
206 screenshooter_binding, shooter);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200207
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400208 shooter->destroy_listener.notify = screenshooter_destroy;
209 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200210}