2 * Copyright © 2008-2011 Kristian Høgsberg
4 * 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.
14 * 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.
25 #include <linux/input.h>
27 #include "compositor.h"
28 #include "screenshooter-server-protocol.h"
30 struct screenshooter {
31 struct wl_object base;
32 struct weston_compositor *ec;
33 struct wl_global *global;
34 struct wl_client *client;
35 struct weston_process process;
36 struct wl_listener destroy_listener;
39 struct screenshooter_read_pixels {
40 struct weston_read_pixels base;
41 struct wl_buffer *buffer;
42 struct wl_resource *resource;
46 copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height,
47 int dst_stride, int src_stride)
51 end = dst + height * dst_stride;
53 memcpy(dst, src, src_stride);
60 copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
64 uint32_t *end = dst + bytes / 4;
69 uint32_t tmp = v & 0xff00ff00;
70 tmp |= (v >> 16) & 0x000000ff;
71 tmp |= (v << 16) & 0x00ff0000;
77 copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height,
78 int dst_stride, int src_stride)
82 end = dst + height * dst_stride;
84 copy_row_swap_RB(dst, src, src_stride);
91 screenshooter_read_pixels_done(struct weston_read_pixels *base,
92 struct weston_output *output)
94 struct screenshooter_read_pixels *r =
95 (struct screenshooter_read_pixels *) base;
96 int32_t buffer_stride, output_stride;
99 buffer_stride = wl_shm_buffer_get_stride(r->buffer);
100 output_stride = output->current->width * 4;
102 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);
106 switch (output->compositor->read_format) {
108 copy_bgra_yflip(d, s, output->current->height,
109 buffer_stride, output_stride);
112 copy_rgba_yflip(d, s, output->current->height,
113 buffer_stride, output_stride);
119 wl_list_remove(&r->base.link);
121 screenshooter_send_done(r->resource);
128 screenshooter_shoot(struct wl_client *client,
129 struct wl_resource *resource,
130 struct wl_resource *output_resource,
131 struct wl_resource *buffer_resource)
133 struct weston_output *output = output_resource->data;
134 struct screenshooter_read_pixels *r;
135 struct wl_buffer *buffer = buffer_resource->data;
138 if (!wl_buffer_is_shm(buffer))
141 if (buffer->width < output->current->width ||
142 buffer->height < output->current->height)
145 r = malloc(sizeof *r);
147 wl_resource_post_no_memory(resource);
153 r->base.width = output->current->width;
154 r->base.height = output->current->height;
155 r->base.done = screenshooter_read_pixels_done;
157 r->resource = resource;
158 stride = buffer->width * 4;
159 r->base.data = malloc(stride * buffer->height);
161 if (r->base.data == NULL) {
163 wl_resource_post_no_memory(resource);
167 wl_list_insert(output->read_pixels_list.prev, &r->base.link);
168 weston_compositor_schedule_repaint(output->compositor);
171 struct screenshooter_interface screenshooter_implementation = {
176 bind_shooter(struct wl_client *client,
177 void *data, uint32_t version, uint32_t id)
179 struct screenshooter *shooter = data;
180 struct wl_resource *resource;
182 resource = wl_client_add_object(client, &screenshooter_interface,
183 &screenshooter_implementation, id, data);
185 if (client != shooter->client) {
186 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
187 "screenshooter failed: permission denied");
188 wl_resource_destroy(resource);
193 screenshooter_sigchld(struct weston_process *process, int status)
195 struct screenshooter *shooter =
196 container_of(process, struct screenshooter, process);
198 shooter->client = NULL;
202 screenshooter_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)
206 struct screenshooter *shooter = data;
207 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
209 if (!shooter->client)
210 shooter->client = weston_client_launch(shooter->ec,
212 screenshooter_exe, screenshooter_sigchld);
216 screenshooter_destroy(struct wl_listener *listener, void *data)
218 struct screenshooter *shooter =
219 container_of(listener, struct screenshooter, destroy_listener);
221 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
226 screenshooter_create(struct weston_compositor *ec)
228 struct screenshooter *shooter;
230 shooter = malloc(sizeof *shooter);
234 shooter->base.interface = &screenshooter_interface;
235 shooter->base.implementation =
236 (void(**)(void)) &screenshooter_implementation;
238 shooter->client = NULL;
240 shooter->global = wl_display_add_global(ec->wl_display,
241 &screenshooter_interface,
242 shooter, bind_shooter);
243 weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
244 screenshooter_binding, shooter);
246 shooter->destroy_listener.notify = screenshooter_destroy;
247 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);