blob: bd813513e58dac146e33b6ddfa793c925f91e6a5 [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
Kristian Høgsberg85449032011-05-02 12:11:07 -040040screenshooter_shoot(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -040041 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040042 struct wl_resource *output_resource,
43 struct wl_resource *buffer_resource)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040044{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050045 struct weston_output *output = output_resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040046 struct wl_buffer *buffer = buffer_resource->data;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040047 uint8_t *tmp, *d, *s;
Scott Moreau80d27b72012-04-04 11:49:21 -060048 int32_t buffer_stride, output_stride, i;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040049
Kristian Høgsberg85449032011-05-02 12:11:07 -040050 if (!wl_buffer_is_shm(buffer))
51 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040052
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040053 if (buffer->width < output->current->width ||
54 buffer->height < output->current->height)
Kristian Høgsberg85449032011-05-02 12:11:07 -040055 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040056
Scott Moreau80d27b72012-04-04 11:49:21 -060057 buffer_stride = wl_shm_buffer_get_stride(buffer);
58 output_stride = output->current->width * 4;
59 tmp = malloc(output_stride * output->current->height);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040060 if (tmp == NULL) {
61 wl_resource_post_no_memory(resource);
62 return;
63 }
64
Kristian Høgsberg85449032011-05-02 12:11:07 -040065 glPixelStorei(GL_PACK_ALIGNMENT, 1);
Scott Moreau80d27b72012-04-04 11:49:21 -060066 output->read_pixels(output, tmp);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040067
Scott Moreau80d27b72012-04-04 11:49:21 -060068 d = wl_shm_buffer_get_data(buffer) + output->y * buffer_stride +
69 output->x * 4;
70 s = tmp + output_stride * (output->current->height - 1);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040071
Scott Moreau80d27b72012-04-04 11:49:21 -060072 for (i = 0; i < output->current->height; i++) {
73 memcpy(d, s, output_stride);
74 d += buffer_stride;
75 s -= output_stride;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040076 }
77
78 free(tmp);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040079}
80
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040081struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040082 screenshooter_shoot
83};
84
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040085static void
86bind_shooter(struct wl_client *client,
87 void *data, uint32_t version, uint32_t id)
88{
Scott Moreau56456d62012-03-23 16:42:04 -060089 struct screenshooter *shooter = data;
90 struct wl_resource *resource;
91
92 resource = wl_client_add_object(client, &screenshooter_interface,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040093 &screenshooter_implementation, id, data);
Scott Moreau56456d62012-03-23 16:42:04 -060094
95 if (client != shooter->client) {
96 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
97 "screenshooter failed: permission denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040098 wl_resource_destroy(resource);
Scott Moreau56456d62012-03-23 16:42:04 -060099 }
100}
101
102static void
103screenshooter_sigchld(struct weston_process *process, int status)
104{
105 struct screenshooter *shooter =
Scott Moreau80d27b72012-04-04 11:49:21 -0600106 container_of(process, struct screenshooter, process);
Scott Moreau56456d62012-03-23 16:42:04 -0600107
108 shooter->client = NULL;
109}
110
111static void
112screenshooter_binding(struct wl_input_device *device, uint32_t time,
113 uint32_t key, uint32_t button, uint32_t axis,
114 int32_t state, void *data)
115{
116 struct screenshooter *shooter = data;
117 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
118
119 if (!shooter->client)
120 shooter->client = weston_client_launch(shooter->ec,
Scott Moreau80d27b72012-04-04 11:49:21 -0600121 &shooter->process,
Scott Moreau56456d62012-03-23 16:42:04 -0600122 screenshooter_exe, screenshooter_sigchld);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400123}
124
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400125static void
126screenshooter_destroy(struct wl_listener *listener, void *data)
127{
128 struct screenshooter *shooter =
129 container_of(listener, struct screenshooter, destroy_listener);
130
131 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
132 free(shooter);
133}
134
135void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500136screenshooter_create(struct weston_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400137{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400138 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400139
140 shooter = malloc(sizeof *shooter);
141 if (shooter == NULL)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400142 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400143
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400144 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400145 shooter->base.implementation =
146 (void(**)(void)) &screenshooter_implementation;
147 shooter->ec = ec;
Scott Moreau56456d62012-03-23 16:42:04 -0600148 shooter->client = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400149
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200150 shooter->global = wl_display_add_global(ec->wl_display,
151 &screenshooter_interface,
152 shooter, bind_shooter);
Scott Moreau56456d62012-03-23 16:42:04 -0600153 weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
154 screenshooter_binding, shooter);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200155
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400156 shooter->destroy_listener.notify = screenshooter_destroy;
157 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200158}