b4b341b4a166bb803e80a84e1cb8fdc0ce2e4da5
[profile/ivi/weston-ivi-shell.git] / src / screenshooter.c
1 /*
2  * Copyright © 2008-2011 Kristian Høgsberg
3  *
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.
13  *
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.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <linux/input.h>
26
27 #include "compositor.h"
28 #include "screenshooter-server-protocol.h"
29
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;
37 };
38
39 static void
40 copy_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
53 static void
54 copy_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
70 static void
71 copy_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
84 static void
85 screenshooter_shoot(struct wl_client *client,
86                     struct wl_resource *resource,
87                     struct wl_resource *output_resource,
88                     struct wl_resource *buffer_resource)
89 {
90         struct weston_output *output = output_resource->data;
91         struct wl_buffer *buffer = buffer_resource->data;
92         uint8_t *tmp, *d, *s;
93         int32_t buffer_stride, output_stride;
94
95         if (!wl_buffer_is_shm(buffer))
96                 return;
97
98         if (buffer->width < output->current->width ||
99             buffer->height < output->current->height)
100                 return;
101
102         buffer_stride = wl_shm_buffer_get_stride(buffer);
103         output_stride = output->current->width * 4;
104         tmp = malloc(output_stride * output->current->height);
105         if (tmp == NULL) {
106                 wl_resource_post_no_memory(resource);
107                 return;
108         }
109
110         glPixelStorei(GL_PACK_ALIGNMENT, 1);
111         output->read_pixels(output, tmp);
112
113         d = wl_shm_buffer_get_data(buffer) + output->y * buffer_stride +
114                                                         output->x * 4;
115         s = tmp + output_stride * (output->current->height - 1);
116
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         }
129
130         free(tmp);
131 }
132
133 struct screenshooter_interface screenshooter_implementation = {
134         screenshooter_shoot
135 };
136
137 static void
138 bind_shooter(struct wl_client *client,
139              void *data, uint32_t version, uint32_t id)
140 {
141         struct screenshooter *shooter = data;
142         struct wl_resource *resource;
143
144         resource = wl_client_add_object(client, &screenshooter_interface,
145                              &screenshooter_implementation, id, data);
146
147         if (client != shooter->client) {
148                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
149                                        "screenshooter failed: permission denied");
150                 wl_resource_destroy(resource);
151         }
152 }
153
154 static void
155 screenshooter_sigchld(struct weston_process *process, int status)
156 {
157         struct screenshooter *shooter =
158                 container_of(process, struct screenshooter, process);
159
160         shooter->client = NULL;
161 }
162
163 static void
164 screenshooter_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,
173                                         &shooter->process,
174                                         screenshooter_exe, screenshooter_sigchld);
175 }
176
177 static void
178 screenshooter_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
187 void
188 screenshooter_create(struct weston_compositor *ec)
189 {
190         struct screenshooter *shooter;
191
192         shooter = malloc(sizeof *shooter);
193         if (shooter == NULL)
194                 return;
195
196         shooter->base.interface = &screenshooter_interface;
197         shooter->base.implementation =
198                 (void(**)(void)) &screenshooter_implementation;
199         shooter->ec = ec;
200         shooter->client = NULL;
201
202         shooter->global = wl_display_add_global(ec->wl_display,
203                                                 &screenshooter_interface,
204                                                 shooter, bind_shooter);
205         weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
206                                         screenshooter_binding, shooter);
207
208         shooter->destroy_listener.notify = screenshooter_destroy;
209         wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
210 }