screenshooter: Properly handle multiple outputs.
[profile/ivi/weston.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 };
37
38 static void
39 screenshooter_shoot(struct wl_client *client,
40                     struct wl_resource *resource,
41                     struct wl_resource *output_resource,
42                     struct wl_resource *buffer_resource)
43 {
44         struct weston_output *output = output_resource->data;
45         struct wl_buffer *buffer = buffer_resource->data;
46         uint8_t *tmp, *d, *s;
47         int32_t buffer_stride, output_stride, i;
48
49         if (!wl_buffer_is_shm(buffer))
50                 return;
51
52         if (buffer->width < output->current->width ||
53             buffer->height < output->current->height)
54                 return;
55
56         buffer_stride = wl_shm_buffer_get_stride(buffer);
57         output_stride = output->current->width * 4;
58         tmp = malloc(output_stride * output->current->height);
59         if (tmp == NULL) {
60                 wl_resource_post_no_memory(resource);
61                 return;
62         }
63
64         glPixelStorei(GL_PACK_ALIGNMENT, 1);
65         output->read_pixels(output, tmp);
66
67         d = wl_shm_buffer_get_data(buffer) + output->y * buffer_stride +
68                                                         output->x * 4;
69         s = tmp + output_stride * (output->current->height - 1);
70
71         for (i = 0; i < output->current->height; i++) {
72                 memcpy(d, s, output_stride);
73                 d += buffer_stride;
74                 s -= output_stride;
75         }
76
77         free(tmp);
78 }
79
80 struct screenshooter_interface screenshooter_implementation = {
81         screenshooter_shoot
82 };
83
84 static void
85 bind_shooter(struct wl_client *client,
86              void *data, uint32_t version, uint32_t id)
87 {
88         struct screenshooter *shooter = data;
89         struct wl_resource *resource;
90
91         resource = wl_client_add_object(client, &screenshooter_interface,
92                              &screenshooter_implementation, id, data);
93
94         if (client != shooter->client) {
95                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
96                                        "screenshooter failed: permission denied");
97                 wl_resource_destroy(resource, 0);
98         }
99 }
100
101 static void
102 screenshooter_sigchld(struct weston_process *process, int status)
103 {
104         struct screenshooter *shooter =
105                 container_of(process, struct screenshooter, process);
106
107         shooter->client = NULL;
108 }
109
110 static void
111 screenshooter_binding(struct wl_input_device *device, uint32_t time,
112                  uint32_t key, uint32_t button, uint32_t axis,
113                  int32_t state, void *data)
114 {
115         struct screenshooter *shooter = data;
116         const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
117
118         if (!shooter->client)
119                 shooter->client = weston_client_launch(shooter->ec,
120                                         &shooter->process,
121                                         screenshooter_exe, screenshooter_sigchld);
122 }
123
124 struct screenshooter *
125 screenshooter_create(struct weston_compositor *ec)
126 {
127         struct screenshooter *shooter;
128
129         shooter = malloc(sizeof *shooter);
130         if (shooter == NULL)
131                 return NULL;
132
133         shooter->base.interface = &screenshooter_interface;
134         shooter->base.implementation =
135                 (void(**)(void)) &screenshooter_implementation;
136         shooter->ec = ec;
137         shooter->client = NULL;
138
139         shooter->global = wl_display_add_global(ec->wl_display,
140                                                 &screenshooter_interface,
141                                                 shooter, bind_shooter);
142         weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
143                                         screenshooter_binding, shooter);
144
145         return shooter;
146 }
147
148 void
149 screenshooter_destroy(struct screenshooter *shooter)
150 {
151         wl_display_remove_global(shooter->ec->wl_display, shooter->global);
152         free(shooter);
153 }