blob: 1df413f4f46de27d9a94aa9ca544cbb26ad03d06 [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ø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;
Scott Moreau80d27b72012-04-04 11:49:21 -060047 int32_t buffer_stride, output_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
Scott Moreau80d27b72012-04-04 11:49:21 -060056 buffer_stride = wl_shm_buffer_get_stride(buffer);
57 output_stride = output->current->width * 4;
58 tmp = malloc(output_stride * output->current->height);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040059 if (tmp == NULL) {
60 wl_resource_post_no_memory(resource);
61 return;
62 }
63
Kristian Høgsberg85449032011-05-02 12:11:07 -040064 glPixelStorei(GL_PACK_ALIGNMENT, 1);
Scott Moreau80d27b72012-04-04 11:49:21 -060065 output->read_pixels(output, tmp);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040066
Scott Moreau80d27b72012-04-04 11:49:21 -060067 d = wl_shm_buffer_get_data(buffer) + output->y * buffer_stride +
68 output->x * 4;
69 s = tmp + output_stride * (output->current->height - 1);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040070
Scott Moreau80d27b72012-04-04 11:49:21 -060071 for (i = 0; i < output->current->height; i++) {
72 memcpy(d, s, output_stride);
73 d += buffer_stride;
74 s -= output_stride;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040075 }
76
77 free(tmp);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040078}
79
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040080struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040081 screenshooter_shoot
82};
83
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040084static void
85bind_shooter(struct wl_client *client,
86 void *data, uint32_t version, uint32_t id)
87{
Scott Moreau56456d62012-03-23 16:42:04 -060088 struct screenshooter *shooter = data;
89 struct wl_resource *resource;
90
91 resource = wl_client_add_object(client, &screenshooter_interface,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040092 &screenshooter_implementation, id, data);
Scott Moreau56456d62012-03-23 16:42:04 -060093
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
101static void
102screenshooter_sigchld(struct weston_process *process, int status)
103{
104 struct screenshooter *shooter =
Scott Moreau80d27b72012-04-04 11:49:21 -0600105 container_of(process, struct screenshooter, process);
Scott Moreau56456d62012-03-23 16:42:04 -0600106
107 shooter->client = NULL;
108}
109
110static void
111screenshooter_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,
Scott Moreau80d27b72012-04-04 11:49:21 -0600120 &shooter->process,
Scott Moreau56456d62012-03-23 16:42:04 -0600121 screenshooter_exe, screenshooter_sigchld);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400122}
123
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200124struct screenshooter *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500125screenshooter_create(struct weston_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400126{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400127 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400128
129 shooter = malloc(sizeof *shooter);
130 if (shooter == NULL)
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200131 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400132
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400133 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400134 shooter->base.implementation =
135 (void(**)(void)) &screenshooter_implementation;
136 shooter->ec = ec;
Scott Moreau56456d62012-03-23 16:42:04 -0600137 shooter->client = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400138
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200139 shooter->global = wl_display_add_global(ec->wl_display,
140 &screenshooter_interface,
141 shooter, bind_shooter);
Scott Moreau56456d62012-03-23 16:42:04 -0600142 weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
143 screenshooter_binding, shooter);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200144
145 return shooter;
146}
147
148void
149screenshooter_destroy(struct screenshooter *shooter)
150{
151 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
152 free(shooter);
153}