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