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> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 25 | |
| 26 | #include "compositor.h" |
Kristian Høgsberg | c5dcb90 | 2010-09-14 15:53:32 -0400 | [diff] [blame] | 27 | #include "screenshooter-server-protocol.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 28 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 29 | struct screenshooter { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 30 | struct wl_object base; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 31 | struct weston_compositor *ec; |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 32 | struct wl_global *global; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | }; |
| 34 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 35 | static void |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 36 | screenshooter_shoot(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 37 | struct wl_resource *resource, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 38 | struct wl_resource *output_resource, |
| 39 | struct wl_resource *buffer_resource) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 40 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 41 | struct weston_output *output = output_resource->data; |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 42 | struct wl_buffer *buffer = buffer_resource->data; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame^] | 43 | uint8_t *tmp, *d, *s; |
| 44 | int32_t stride, i; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 45 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 46 | if (!wl_buffer_is_shm(buffer)) |
| 47 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 48 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 49 | if (buffer->width < output->current->width || |
| 50 | buffer->height < output->current->height) |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 51 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 52 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame^] | 53 | stride = wl_shm_buffer_get_stride(buffer); |
| 54 | tmp = malloc(stride * buffer->height); |
| 55 | if (tmp == NULL) { |
| 56 | wl_resource_post_no_memory(resource); |
| 57 | return; |
| 58 | } |
| 59 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 60 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 61 | glReadPixels(0, 0, output->current->width, output->current->height, |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame^] | 62 | GL_RGBA, GL_UNSIGNED_BYTE, tmp); |
| 63 | |
| 64 | d = wl_shm_buffer_get_data(buffer); |
| 65 | s = tmp + stride * (buffer->height - 1); |
| 66 | |
| 67 | for (i = 0; i < buffer->height; i++) { |
| 68 | memcpy(d, s, stride); |
| 69 | d += stride; |
| 70 | s -= stride; |
| 71 | } |
| 72 | |
| 73 | free(tmp); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 76 | struct screenshooter_interface screenshooter_implementation = { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 77 | screenshooter_shoot |
| 78 | }; |
| 79 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 80 | static void |
| 81 | bind_shooter(struct wl_client *client, |
| 82 | void *data, uint32_t version, uint32_t id) |
| 83 | { |
| 84 | wl_client_add_object(client, &screenshooter_interface, |
| 85 | &screenshooter_implementation, id, data); |
| 86 | } |
| 87 | |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 88 | struct screenshooter * |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 89 | screenshooter_create(struct weston_compositor *ec) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 90 | { |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 91 | struct screenshooter *shooter; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 92 | |
| 93 | shooter = malloc(sizeof *shooter); |
| 94 | if (shooter == NULL) |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 95 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 96 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 97 | shooter->base.interface = &screenshooter_interface; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 98 | shooter->base.implementation = |
| 99 | (void(**)(void)) &screenshooter_implementation; |
| 100 | shooter->ec = ec; |
| 101 | |
Pekka Paalanen | 35ce06d | 2012-01-03 11:39:13 +0200 | [diff] [blame] | 102 | shooter->global = wl_display_add_global(ec->wl_display, |
| 103 | &screenshooter_interface, |
| 104 | shooter, bind_shooter); |
| 105 | |
| 106 | return shooter; |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | screenshooter_destroy(struct screenshooter *shooter) |
| 111 | { |
| 112 | wl_display_remove_global(shooter->ec->wl_display, shooter->global); |
| 113 | free(shooter); |
| 114 | } |