blob: 6748645a7b1bd18af082dd35b2400da826284723 [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;
35 struct weston_process screenshooter_process;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040036};
37
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040038static void
Kristian Høgsberg85449032011-05-02 12:11:07 -040039screenshooter_shoot(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -040040 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040041 struct wl_resource *output_resource,
42 struct wl_resource *buffer_resource)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050044 struct weston_output *output = output_resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040045 struct wl_buffer *buffer = buffer_resource->data;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040046 uint8_t *tmp, *d, *s;
47 int32_t stride, i;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040048
Kristian Høgsberg85449032011-05-02 12:11:07 -040049 if (!wl_buffer_is_shm(buffer))
50 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040051
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040052 if (buffer->width < output->current->width ||
53 buffer->height < output->current->height)
Kristian Høgsberg85449032011-05-02 12:11:07 -040054 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040055
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040056 stride = wl_shm_buffer_get_stride(buffer);
57 tmp = malloc(stride * buffer->height);
58 if (tmp == NULL) {
59 wl_resource_post_no_memory(resource);
60 return;
61 }
62
Kristian Høgsberg85449032011-05-02 12:11:07 -040063 glPixelStorei(GL_PACK_ALIGNMENT, 1);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040064 glReadPixels(0, 0, output->current->width, output->current->height,
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040065 GL_RGBA, GL_UNSIGNED_BYTE, tmp);
66
67 d = wl_shm_buffer_get_data(buffer);
68 s = tmp + stride * (buffer->height - 1);
69
70 for (i = 0; i < buffer->height; i++) {
71 memcpy(d, s, stride);
72 d += stride;
73 s -= stride;
74 }
75
76 free(tmp);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040077}
78
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040079struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040080 screenshooter_shoot
81};
82
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040083static void
84bind_shooter(struct wl_client *client,
85 void *data, uint32_t version, uint32_t id)
86{
Scott Moreau56456d62012-03-23 16:42:04 -060087 struct screenshooter *shooter = data;
88 struct wl_resource *resource;
89
90 resource = wl_client_add_object(client, &screenshooter_interface,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040091 &screenshooter_implementation, id, data);
Scott Moreau56456d62012-03-23 16:42:04 -060092
93 if (client != shooter->client) {
94 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
95 "screenshooter failed: permission denied");
96 wl_resource_destroy(resource, 0);
97 }
98}
99
100static void
101screenshooter_sigchld(struct weston_process *process, int status)
102{
103 struct screenshooter *shooter =
104 container_of(process, struct screenshooter, screenshooter_process);
105
106 shooter->client = NULL;
107}
108
109static void
110screenshooter_binding(struct wl_input_device *device, uint32_t time,
111 uint32_t key, uint32_t button, uint32_t axis,
112 int32_t state, void *data)
113{
114 struct screenshooter *shooter = data;
115 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
116
117 if (!shooter->client)
118 shooter->client = weston_client_launch(shooter->ec,
119 &shooter->screenshooter_process,
120 screenshooter_exe, screenshooter_sigchld);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400121}
122
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200123struct screenshooter *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500124screenshooter_create(struct weston_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400125{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400126 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400127
128 shooter = malloc(sizeof *shooter);
129 if (shooter == NULL)
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200130 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400131
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400132 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400133 shooter->base.implementation =
134 (void(**)(void)) &screenshooter_implementation;
135 shooter->ec = ec;
Scott Moreau56456d62012-03-23 16:42:04 -0600136 shooter->client = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400137
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200138 shooter->global = wl_display_add_global(ec->wl_display,
139 &screenshooter_interface,
140 shooter, bind_shooter);
Scott Moreau56456d62012-03-23 16:42:04 -0600141 weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
142 screenshooter_binding, shooter);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200143
144 return shooter;
145}
146
147void
148screenshooter_destroy(struct screenshooter *shooter)
149{
150 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
151 free(shooter);
152}