Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software Foundation, |
| 16 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 20 | |
| 21 | #include "compositor.h" |
Kristian Høgsberg | c5dcb90 | 2010-09-14 15:53:32 -0400 | [diff] [blame] | 22 | #include "screenshooter-server-protocol.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 23 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 24 | struct screenshooter { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 25 | struct wl_object base; |
| 26 | struct wlsc_compositor *ec; |
| 27 | }; |
| 28 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 29 | static void |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 30 | screenshooter_shoot(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame^] | 31 | struct wl_resource *resource, |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 32 | struct wl_output *output_base, struct wl_buffer *buffer) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | { |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 34 | struct wlsc_output *output = (struct wlsc_output *) output_base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 35 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 36 | if (!wl_buffer_is_shm(buffer)) |
| 37 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 38 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 39 | if (buffer->width < output->current->width || |
| 40 | buffer->height < output->current->height) |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 41 | return; |
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 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 44 | glReadPixels(0, 0, output->current->width, output->current->height, |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 45 | GL_RGBA, GL_UNSIGNED_BYTE, |
| 46 | wl_shm_buffer_get_data(buffer)); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 49 | struct screenshooter_interface screenshooter_implementation = { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 50 | screenshooter_shoot |
| 51 | }; |
| 52 | |
| 53 | void |
| 54 | screenshooter_create(struct wlsc_compositor *ec) |
| 55 | { |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 56 | struct screenshooter *shooter; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 57 | |
| 58 | shooter = malloc(sizeof *shooter); |
| 59 | if (shooter == NULL) |
| 60 | return; |
| 61 | |
Kristian Høgsberg | cf57dc5 | 2011-04-18 10:33:25 -0400 | [diff] [blame] | 62 | shooter->base.interface = &screenshooter_interface; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 63 | shooter->base.implementation = |
| 64 | (void(**)(void)) &screenshooter_implementation; |
| 65 | shooter->ec = ec; |
| 66 | |
| 67 | wl_display_add_object(ec->wl_display, &shooter->base); |
| 68 | wl_display_add_global(ec->wl_display, &shooter->base, NULL); |
| 69 | }; |