blob: 835e175600177c19046d473a8a8e44c297c32800 [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
Kristian Høgsberg85449032011-05-02 12:11:07 -040054screenshooter_shoot(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -040055 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040056 struct wl_resource *output_resource,
57 struct wl_resource *buffer_resource)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040058{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050059 struct weston_output *output = output_resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040060 struct wl_buffer *buffer = buffer_resource->data;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040061 uint8_t *tmp, *d, *s;
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030062 int32_t buffer_stride, output_stride;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040063
Kristian Høgsberg85449032011-05-02 12:11:07 -040064 if (!wl_buffer_is_shm(buffer))
65 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040066
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040067 if (buffer->width < output->current->width ||
68 buffer->height < output->current->height)
Kristian Høgsberg85449032011-05-02 12:11:07 -040069 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040070
Scott Moreau80d27b72012-04-04 11:49:21 -060071 buffer_stride = wl_shm_buffer_get_stride(buffer);
72 output_stride = output->current->width * 4;
73 tmp = malloc(output_stride * output->current->height);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040074 if (tmp == NULL) {
75 wl_resource_post_no_memory(resource);
76 return;
77 }
78
Kristian Høgsberg85449032011-05-02 12:11:07 -040079 glPixelStorei(GL_PACK_ALIGNMENT, 1);
Scott Moreau80d27b72012-04-04 11:49:21 -060080 output->read_pixels(output, tmp);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040081
Scott Moreau80d27b72012-04-04 11:49:21 -060082 d = wl_shm_buffer_get_data(buffer) + output->y * buffer_stride +
83 output->x * 4;
84 s = tmp + output_stride * (output->current->height - 1);
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030085 copy_bgra_yflip(d, s, output->current->height,
86 buffer_stride, output_stride);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040087
88 free(tmp);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040089}
90
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040091struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040092 screenshooter_shoot
93};
94
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040095static void
96bind_shooter(struct wl_client *client,
97 void *data, uint32_t version, uint32_t id)
98{
Scott Moreau56456d62012-03-23 16:42:04 -060099 struct screenshooter *shooter = data;
100 struct wl_resource *resource;
101
102 resource = wl_client_add_object(client, &screenshooter_interface,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400103 &screenshooter_implementation, id, data);
Scott Moreau56456d62012-03-23 16:42:04 -0600104
105 if (client != shooter->client) {
106 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
107 "screenshooter failed: permission denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400108 wl_resource_destroy(resource);
Scott Moreau56456d62012-03-23 16:42:04 -0600109 }
110}
111
112static void
113screenshooter_sigchld(struct weston_process *process, int status)
114{
115 struct screenshooter *shooter =
Scott Moreau80d27b72012-04-04 11:49:21 -0600116 container_of(process, struct screenshooter, process);
Scott Moreau56456d62012-03-23 16:42:04 -0600117
118 shooter->client = NULL;
119}
120
121static void
122screenshooter_binding(struct wl_input_device *device, uint32_t time,
123 uint32_t key, uint32_t button, uint32_t axis,
124 int32_t state, void *data)
125{
126 struct screenshooter *shooter = data;
127 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
128
129 if (!shooter->client)
130 shooter->client = weston_client_launch(shooter->ec,
Scott Moreau80d27b72012-04-04 11:49:21 -0600131 &shooter->process,
Scott Moreau56456d62012-03-23 16:42:04 -0600132 screenshooter_exe, screenshooter_sigchld);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400133}
134
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400135static void
136screenshooter_destroy(struct wl_listener *listener, void *data)
137{
138 struct screenshooter *shooter =
139 container_of(listener, struct screenshooter, destroy_listener);
140
141 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
142 free(shooter);
143}
144
145void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500146screenshooter_create(struct weston_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400147{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400148 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400149
150 shooter = malloc(sizeof *shooter);
151 if (shooter == NULL)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400152 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400153
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400154 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400155 shooter->base.implementation =
156 (void(**)(void)) &screenshooter_implementation;
157 shooter->ec = ec;
Scott Moreau56456d62012-03-23 16:42:04 -0600158 shooter->client = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400159
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200160 shooter->global = wl_display_add_global(ec->wl_display,
161 &screenshooter_interface,
162 shooter, bind_shooter);
Scott Moreau56456d62012-03-23 16:42:04 -0600163 weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
164 screenshooter_binding, shooter);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200165
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400166 shooter->destroy_listener.notify = screenshooter_destroy;
167 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200168}