2 * Copyright © 2008-2011 Kristian Høgsberg
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <linux/input.h>
31 #include "compositor.h"
33 #include "weston-screenshooter-server-protocol.h"
34 #include "shared/helpers.h"
36 struct screenshooter {
37 struct weston_compositor *ec;
38 struct wl_global *global;
39 struct wl_client *client;
40 struct weston_process process;
41 struct wl_listener destroy_listener;
42 struct weston_recorder *recorder;
46 screenshooter_done(void *data, enum weston_screenshooter_outcome outcome)
48 struct wl_resource *resource = data;
51 case WESTON_SCREENSHOOTER_SUCCESS:
52 weston_screenshooter_send_done(resource);
54 case WESTON_SCREENSHOOTER_NO_MEMORY:
55 wl_resource_post_no_memory(resource);
63 screenshooter_shoot(struct wl_client *client,
64 struct wl_resource *resource,
65 struct wl_resource *output_resource,
66 struct wl_resource *buffer_resource)
68 struct weston_output *output =
69 wl_resource_get_user_data(output_resource);
70 struct weston_buffer *buffer =
71 weston_buffer_from_resource(buffer_resource);
74 wl_resource_post_no_memory(resource);
78 weston_screenshooter_shoot(output, buffer, screenshooter_done, resource);
81 struct weston_screenshooter_interface screenshooter_implementation = {
86 bind_shooter(struct wl_client *client,
87 void *data, uint32_t version, uint32_t id)
89 struct screenshooter *shooter = data;
90 struct wl_resource *resource;
92 resource = wl_resource_create(client,
93 &weston_screenshooter_interface, 1, id);
95 if (client != shooter->client) {
96 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
97 "screenshooter failed: permission denied");
101 wl_resource_set_implementation(resource, &screenshooter_implementation,
106 screenshooter_sigchld(struct weston_process *process, int status)
108 struct screenshooter *shooter =
109 container_of(process, struct screenshooter, process);
111 shooter->client = NULL;
115 screenshooter_binding(struct weston_keyboard *keyboard, uint32_t time,
116 uint32_t key, void *data)
118 struct screenshooter *shooter = data;
119 char *screenshooter_exe;
122 ret = asprintf(&screenshooter_exe, "%s/%s",
123 weston_config_get_libexec_dir(),
124 "/weston-screenshooter");
126 weston_log("Could not construct screenshooter path.\n");
130 if (!shooter->client)
131 shooter->client = weston_client_launch(shooter->ec,
133 screenshooter_exe, screenshooter_sigchld);
134 free(screenshooter_exe);
138 recorder_binding(struct weston_keyboard *keyboard, uint32_t time,
139 uint32_t key, void *data)
141 struct weston_compositor *ec = keyboard->seat->compositor;
142 struct weston_output *output;
143 struct screenshooter *shooter = data;
144 struct weston_recorder *recorder = shooter->recorder;;
145 static const char filename[] = "capture.wcap";
148 weston_recorder_stop(recorder);
149 shooter->recorder = NULL;
151 if (keyboard->focus && keyboard->focus->output)
152 output = keyboard->focus->output;
154 output = container_of(ec->output_list.next,
155 struct weston_output, link);
157 shooter->recorder = weston_recorder_start(output, filename);
162 screenshooter_destroy(struct wl_listener *listener, void *data)
164 struct screenshooter *shooter =
165 container_of(listener, struct screenshooter, destroy_listener);
167 wl_global_destroy(shooter->global);
172 screenshooter_create(struct weston_compositor *ec)
174 struct screenshooter *shooter;
176 shooter = zalloc(sizeof *shooter);
182 shooter->global = wl_global_create(ec->wl_display,
183 &weston_screenshooter_interface, 1,
184 shooter, bind_shooter);
185 weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
186 screenshooter_binding, shooter);
187 weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
188 recorder_binding, shooter);
190 shooter->destroy_listener.notify = screenshooter_destroy;
191 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);