Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2008-2011 Kristian Høgsberg |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 4 | * 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øgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 13 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 14 | * 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øgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 24 | #include <string.h> |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 25 | #include <linux/input.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 26 | |
| 27 | #include "compositor.h" |
Kristian Høgsberg | c5dcb90 | 2010-09-14 15:53:32 -0400 | [diff] [blame] | 28 | #include "screenshooter-server-protocol.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 29 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 30 | struct screenshooter { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 31 | struct wl_object base; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 32 | struct weston_compositor *ec; |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 33 | struct wl_global *global; |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 34 | struct wl_client *client; |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 35 | struct weston_process process; |
Kristian Høgsberg | 02e79dc | 2012-04-12 09:55:26 -0400 | [diff] [blame] | 36 | struct wl_listener destroy_listener; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 37 | }; |
| 38 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 39 | static void |
Pekka Paalanen | 45fab0e | 2012-04-17 11:55:41 +0300 | [diff] [blame^] | 40 | copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, |
| 41 | int dst_stride, int src_stride) |
| 42 | { |
| 43 | uint8_t *end; |
| 44 | |
| 45 | end = dst + height * dst_stride; |
| 46 | while (dst < end) { |
| 47 | memcpy(dst, src, src_stride); |
| 48 | dst += dst_stride; |
| 49 | src -= src_stride; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static void |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 54 | screenshooter_shoot(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 55 | struct wl_resource *resource, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 56 | struct wl_resource *output_resource, |
| 57 | struct wl_resource *buffer_resource) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 58 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 59 | struct weston_output *output = output_resource->data; |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 60 | struct wl_buffer *buffer = buffer_resource->data; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 61 | uint8_t *tmp, *d, *s; |
Pekka Paalanen | 45fab0e | 2012-04-17 11:55:41 +0300 | [diff] [blame^] | 62 | int32_t buffer_stride, output_stride; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 63 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 64 | if (!wl_buffer_is_shm(buffer)) |
| 65 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 66 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 67 | if (buffer->width < output->current->width || |
| 68 | buffer->height < output->current->height) |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 69 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 70 | |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 71 | buffer_stride = wl_shm_buffer_get_stride(buffer); |
| 72 | output_stride = output->current->width * 4; |
| 73 | tmp = malloc(output_stride * output->current->height); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 74 | if (tmp == NULL) { |
| 75 | wl_resource_post_no_memory(resource); |
| 76 | return; |
| 77 | } |
| 78 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 79 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 80 | output->read_pixels(output, tmp); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 81 | |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 82 | d = wl_shm_buffer_get_data(buffer) + output->y * buffer_stride + |
| 83 | output->x * 4; |
| 84 | s = tmp + output_stride * (output->current->height - 1); |
Pekka Paalanen | 45fab0e | 2012-04-17 11:55:41 +0300 | [diff] [blame^] | 85 | copy_bgra_yflip(d, s, output->current->height, |
| 86 | buffer_stride, output_stride); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 87 | |
| 88 | free(tmp); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 91 | struct screenshooter_interface screenshooter_implementation = { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 92 | screenshooter_shoot |
| 93 | }; |
| 94 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 95 | static void |
| 96 | bind_shooter(struct wl_client *client, |
| 97 | void *data, uint32_t version, uint32_t id) |
| 98 | { |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 99 | struct screenshooter *shooter = data; |
| 100 | struct wl_resource *resource; |
| 101 | |
| 102 | resource = wl_client_add_object(client, &screenshooter_interface, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 103 | &screenshooter_implementation, id, data); |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 104 | |
| 105 | if (client != shooter->client) { |
| 106 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 107 | "screenshooter failed: permission denied"); |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 108 | wl_resource_destroy(resource); |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | static void |
| 113 | screenshooter_sigchld(struct weston_process *process, int status) |
| 114 | { |
| 115 | struct screenshooter *shooter = |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 116 | container_of(process, struct screenshooter, process); |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 117 | |
| 118 | shooter->client = NULL; |
| 119 | } |
| 120 | |
| 121 | static void |
| 122 | screenshooter_binding(struct wl_input_device *device, uint32_t time, |
| 123 | uint32_t key, uint32_t button, uint32_t axis, |
| 124 | int32_t state, void *data) |
| 125 | { |
| 126 | struct screenshooter *shooter = data; |
| 127 | const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter"; |
| 128 | |
| 129 | if (!shooter->client) |
| 130 | shooter->client = weston_client_launch(shooter->ec, |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 131 | &shooter->process, |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 132 | screenshooter_exe, screenshooter_sigchld); |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Kristian Høgsberg | 02e79dc | 2012-04-12 09:55:26 -0400 | [diff] [blame] | 135 | static void |
| 136 | screenshooter_destroy(struct wl_listener *listener, void *data) |
| 137 | { |
| 138 | struct screenshooter *shooter = |
| 139 | container_of(listener, struct screenshooter, destroy_listener); |
| 140 | |
| 141 | wl_display_remove_global(shooter->ec->wl_display, shooter->global); |
| 142 | free(shooter); |
| 143 | } |
| 144 | |
| 145 | void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 146 | screenshooter_create(struct weston_compositor *ec) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 147 | { |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 148 | struct screenshooter *shooter; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 149 | |
| 150 | shooter = malloc(sizeof *shooter); |
| 151 | if (shooter == NULL) |
Kristian Høgsberg | 02e79dc | 2012-04-12 09:55:26 -0400 | [diff] [blame] | 152 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 153 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 154 | shooter->base.interface = &screenshooter_interface; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 155 | shooter->base.implementation = |
| 156 | (void(**)(void)) &screenshooter_implementation; |
| 157 | shooter->ec = ec; |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 158 | shooter->client = NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 159 | |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 160 | shooter->global = wl_display_add_global(ec->wl_display, |
| 161 | &screenshooter_interface, |
| 162 | shooter, bind_shooter); |
Scott Moreau | 56456d6 | 2012-03-23 16:42:04 -0600 | [diff] [blame] | 163 | weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER, |
| 164 | screenshooter_binding, shooter); |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 165 | |
Kristian Høgsberg | 02e79dc | 2012-04-12 09:55:26 -0400 | [diff] [blame] | 166 | shooter->destroy_listener.notify = screenshooter_destroy; |
| 167 | wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener); |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 168 | } |