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 | |
| 28 | #include <linux/input.h> |
| 29 | |
| 30 | #include "compositor.h" |
| 31 | #include "weston.h" |
| 32 | #include "weston-screenshooter-server-protocol.h" |
| 33 | #include "shared/helpers.h" |
| 34 | |
| 35 | struct screenshooter { |
| 36 | struct weston_compositor *ec; |
| 37 | struct wl_global *global; |
| 38 | struct wl_client *client; |
| 39 | struct weston_process process; |
| 40 | struct wl_listener destroy_listener; |
| 41 | struct weston_recorder *recorder; |
| 42 | }; |
| 43 | |
| 44 | static void |
| 45 | screenshooter_done(void *data, enum weston_screenshooter_outcome outcome) |
| 46 | { |
| 47 | struct wl_resource *resource = data; |
| 48 | |
| 49 | switch (outcome) { |
| 50 | case WESTON_SCREENSHOOTER_SUCCESS: |
| 51 | weston_screenshooter_send_done(resource); |
| 52 | break; |
| 53 | case WESTON_SCREENSHOOTER_NO_MEMORY: |
| 54 | wl_resource_post_no_memory(resource); |
| 55 | break; |
| 56 | default: |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static void |
| 62 | screenshooter_shoot(struct wl_client *client, |
| 63 | struct wl_resource *resource, |
| 64 | struct wl_resource *output_resource, |
| 65 | struct wl_resource *buffer_resource) |
| 66 | { |
| 67 | struct weston_output *output = |
| 68 | wl_resource_get_user_data(output_resource); |
| 69 | struct weston_buffer *buffer = |
| 70 | weston_buffer_from_resource(buffer_resource); |
| 71 | |
| 72 | if (buffer == NULL) { |
| 73 | wl_resource_post_no_memory(resource); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | weston_screenshooter_shoot(output, buffer, screenshooter_done, resource); |
| 78 | } |
| 79 | |
| 80 | struct weston_screenshooter_interface screenshooter_implementation = { |
| 81 | screenshooter_shoot |
| 82 | }; |
| 83 | |
| 84 | static void |
| 85 | bind_shooter(struct wl_client *client, |
| 86 | void *data, uint32_t version, uint32_t id) |
| 87 | { |
| 88 | struct screenshooter *shooter = data; |
| 89 | struct wl_resource *resource; |
| 90 | |
| 91 | resource = wl_resource_create(client, |
| 92 | &weston_screenshooter_interface, 1, id); |
| 93 | |
| 94 | if (client != shooter->client) { |
| 95 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 96 | "screenshooter failed: permission denied"); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | wl_resource_set_implementation(resource, &screenshooter_implementation, |
| 101 | data, NULL); |
| 102 | } |
| 103 | |
| 104 | static void |
| 105 | screenshooter_sigchld(struct weston_process *process, int status) |
| 106 | { |
| 107 | struct screenshooter *shooter = |
| 108 | container_of(process, struct screenshooter, process); |
| 109 | |
| 110 | shooter->client = NULL; |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | screenshooter_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 115 | uint32_t key, void *data) |
| 116 | { |
| 117 | struct screenshooter *shooter = data; |
| 118 | char *screenshooter_exe; |
| 119 | int ret; |
| 120 | |
| 121 | ret = asprintf(&screenshooter_exe, "%s/%s", |
| 122 | weston_config_get_libexec_dir(), |
| 123 | "/weston-screenshooter"); |
| 124 | if (ret < 0) { |
| 125 | weston_log("Could not construct screenshooter path.\n"); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | if (!shooter->client) |
| 130 | shooter->client = weston_client_launch(shooter->ec, |
| 131 | &shooter->process, |
| 132 | screenshooter_exe, screenshooter_sigchld); |
| 133 | free(screenshooter_exe); |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | recorder_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 138 | uint32_t key, void *data) |
| 139 | { |
| 140 | struct weston_compositor *ec = keyboard->seat->compositor; |
| 141 | struct weston_output *output; |
| 142 | struct screenshooter *shooter = data; |
| 143 | struct weston_recorder *recorder = shooter->recorder;; |
| 144 | static const char filename[] = "capture.wcap"; |
| 145 | |
| 146 | if (recorder) { |
| 147 | weston_recorder_stop(recorder); |
| 148 | shooter->recorder = NULL; |
| 149 | } else { |
| 150 | if (keyboard->focus && keyboard->focus->output) |
| 151 | output = keyboard->focus->output; |
| 152 | else |
| 153 | output = container_of(ec->output_list.next, |
| 154 | struct weston_output, link); |
| 155 | |
| 156 | shooter->recorder = weston_recorder_start(output, filename); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static void |
| 161 | screenshooter_destroy(struct wl_listener *listener, void *data) |
| 162 | { |
| 163 | struct screenshooter *shooter = |
| 164 | container_of(listener, struct screenshooter, destroy_listener); |
| 165 | |
| 166 | wl_global_destroy(shooter->global); |
| 167 | free(shooter); |
| 168 | } |
| 169 | |
| 170 | WL_EXPORT void |
| 171 | screenshooter_create(struct weston_compositor *ec) |
| 172 | { |
| 173 | struct screenshooter *shooter; |
| 174 | |
| 175 | shooter = zalloc(sizeof *shooter); |
| 176 | if (shooter == NULL) |
| 177 | return; |
| 178 | |
| 179 | shooter->ec = ec; |
| 180 | |
| 181 | shooter->global = wl_global_create(ec->wl_display, |
| 182 | &weston_screenshooter_interface, 1, |
| 183 | shooter, bind_shooter); |
| 184 | weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER, |
| 185 | screenshooter_binding, shooter); |
| 186 | weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER, |
| 187 | recorder_binding, shooter); |
| 188 | |
| 189 | shooter->destroy_listener.notify = screenshooter_destroy; |
| 190 | wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener); |
| 191 | } |