Giulio Camuffo | 26f62d4 | 2016-06-02 21:48:09 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * |
| 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: |
| 11 | * |
| 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. |
| 15 | * |
| 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 |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 28 | #include <stdint.h> |
Giulio Camuffo | 26f62d4 | 2016-06-02 21:48:09 +0300 | [diff] [blame] | 29 | #include <linux/input.h> |
| 30 | |
| 31 | #include "compositor.h" |
| 32 | #include "weston.h" |
| 33 | #include "weston-screenshooter-server-protocol.h" |
| 34 | #include "shared/helpers.h" |
| 35 | |
| 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; |
| 43 | }; |
| 44 | |
| 45 | static void |
| 46 | screenshooter_done(void *data, enum weston_screenshooter_outcome outcome) |
| 47 | { |
| 48 | struct wl_resource *resource = data; |
| 49 | |
| 50 | switch (outcome) { |
| 51 | case WESTON_SCREENSHOOTER_SUCCESS: |
| 52 | weston_screenshooter_send_done(resource); |
| 53 | break; |
| 54 | case WESTON_SCREENSHOOTER_NO_MEMORY: |
| 55 | wl_resource_post_no_memory(resource); |
| 56 | break; |
| 57 | default: |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static void |
| 63 | screenshooter_shoot(struct wl_client *client, |
| 64 | struct wl_resource *resource, |
| 65 | struct wl_resource *output_resource, |
| 66 | struct wl_resource *buffer_resource) |
| 67 | { |
| 68 | struct weston_output *output = |
Pekka Paalanen | 9ffb250 | 2017-03-27 15:14:32 +0300 | [diff] [blame] | 69 | weston_output_from_resource(output_resource); |
Giulio Camuffo | 26f62d4 | 2016-06-02 21:48:09 +0300 | [diff] [blame] | 70 | struct weston_buffer *buffer = |
| 71 | weston_buffer_from_resource(buffer_resource); |
| 72 | |
| 73 | if (buffer == NULL) { |
| 74 | wl_resource_post_no_memory(resource); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | weston_screenshooter_shoot(output, buffer, screenshooter_done, resource); |
| 79 | } |
| 80 | |
| 81 | struct weston_screenshooter_interface screenshooter_implementation = { |
| 82 | screenshooter_shoot |
| 83 | }; |
| 84 | |
| 85 | static void |
| 86 | bind_shooter(struct wl_client *client, |
| 87 | void *data, uint32_t version, uint32_t id) |
| 88 | { |
| 89 | struct screenshooter *shooter = data; |
| 90 | struct wl_resource *resource; |
| 91 | |
| 92 | resource = wl_resource_create(client, |
| 93 | &weston_screenshooter_interface, 1, id); |
| 94 | |
| 95 | if (client != shooter->client) { |
| 96 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 97 | "screenshooter failed: permission denied"); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | wl_resource_set_implementation(resource, &screenshooter_implementation, |
| 102 | data, NULL); |
| 103 | } |
| 104 | |
| 105 | static void |
| 106 | screenshooter_sigchld(struct weston_process *process, int status) |
| 107 | { |
| 108 | struct screenshooter *shooter = |
| 109 | container_of(process, struct screenshooter, process); |
| 110 | |
| 111 | shooter->client = NULL; |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | screenshooter_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 116 | uint32_t key, void *data) |
| 117 | { |
| 118 | struct screenshooter *shooter = data; |
| 119 | char *screenshooter_exe; |
| 120 | int ret; |
| 121 | |
| 122 | ret = asprintf(&screenshooter_exe, "%s/%s", |
| 123 | weston_config_get_libexec_dir(), |
| 124 | "/weston-screenshooter"); |
| 125 | if (ret < 0) { |
| 126 | weston_log("Could not construct screenshooter path.\n"); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | if (!shooter->client) |
| 131 | shooter->client = weston_client_launch(shooter->ec, |
| 132 | &shooter->process, |
| 133 | screenshooter_exe, screenshooter_sigchld); |
| 134 | free(screenshooter_exe); |
| 135 | } |
| 136 | |
| 137 | static void |
| 138 | recorder_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 139 | uint32_t key, void *data) |
| 140 | { |
| 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"; |
| 146 | |
| 147 | if (recorder) { |
| 148 | weston_recorder_stop(recorder); |
| 149 | shooter->recorder = NULL; |
| 150 | } else { |
| 151 | if (keyboard->focus && keyboard->focus->output) |
| 152 | output = keyboard->focus->output; |
| 153 | else |
| 154 | output = container_of(ec->output_list.next, |
| 155 | struct weston_output, link); |
| 156 | |
| 157 | shooter->recorder = weston_recorder_start(output, filename); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static void |
| 162 | screenshooter_destroy(struct wl_listener *listener, void *data) |
| 163 | { |
| 164 | struct screenshooter *shooter = |
| 165 | container_of(listener, struct screenshooter, destroy_listener); |
| 166 | |
| 167 | wl_global_destroy(shooter->global); |
| 168 | free(shooter); |
| 169 | } |
| 170 | |
| 171 | WL_EXPORT void |
| 172 | screenshooter_create(struct weston_compositor *ec) |
| 173 | { |
| 174 | struct screenshooter *shooter; |
| 175 | |
| 176 | shooter = zalloc(sizeof *shooter); |
| 177 | if (shooter == NULL) |
| 178 | return; |
| 179 | |
| 180 | shooter->ec = ec; |
| 181 | |
| 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); |
| 189 | |
| 190 | shooter->destroy_listener.notify = screenshooter_destroy; |
| 191 | wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener); |
| 192 | } |