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> |
| 20 | #include <GLES2/gl2.h> |
| 21 | #include <gdk-pixbuf/gdk-pixbuf.h> |
| 22 | |
| 23 | #include "compositor.h" |
Kristian Høgsberg | c5dcb90 | 2010-09-14 15:53:32 -0400 | [diff] [blame^] | 24 | #include "screenshooter-server-protocol.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 25 | |
Kristian Høgsberg | c5dcb90 | 2010-09-14 15:53:32 -0400 | [diff] [blame^] | 26 | struct wl_screenshooter { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 27 | struct wl_object base; |
| 28 | struct wlsc_compositor *ec; |
| 29 | }; |
| 30 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 31 | static void |
Kristian Høgsberg | c5dcb90 | 2010-09-14 15:53:32 -0400 | [diff] [blame^] | 32 | screenshooter_shoot(struct wl_client *client, struct wl_screenshooter *shooter) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | { |
| 34 | struct wlsc_compositor *ec = shooter->ec; |
| 35 | struct wlsc_output *output; |
| 36 | char buffer[256]; |
| 37 | GdkPixbuf *pixbuf, *normal; |
| 38 | GError *error = NULL; |
| 39 | unsigned char *data; |
| 40 | int i, j; |
| 41 | |
| 42 | i = 0; |
| 43 | wl_list_for_each(output, &ec->output_list, link) { |
| 44 | snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++); |
| 45 | data = malloc(output->width * output->height * 4); |
| 46 | if (data == NULL) { |
| 47 | fprintf(stderr, "couldn't allocate image buffer\n"); |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 52 | glReadPixels(0, 0, output->width, output->height, |
| 53 | GL_RGBA, GL_UNSIGNED_BYTE, data); |
| 54 | |
| 55 | /* FIXME: We should just use a RGB visual for the frontbuffer. */ |
| 56 | for (j = 3; j < output->width * output->height * 4; j += 4) |
| 57 | data[j] = 0xff; |
| 58 | |
| 59 | pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE, |
| 60 | 8, output->width, output->height, output->width * 4, |
| 61 | NULL, NULL); |
| 62 | normal = gdk_pixbuf_flip(pixbuf, FALSE); |
| 63 | gdk_pixbuf_save(normal, buffer, "png", &error, NULL); |
| 64 | gdk_pixbuf_unref(normal); |
| 65 | gdk_pixbuf_unref(pixbuf); |
| 66 | free(data); |
| 67 | } |
| 68 | } |
| 69 | |
Kristian Høgsberg | c5dcb90 | 2010-09-14 15:53:32 -0400 | [diff] [blame^] | 70 | struct wl_screenshooter_interface screenshooter_implementation = { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 71 | screenshooter_shoot |
| 72 | }; |
| 73 | |
| 74 | void |
| 75 | screenshooter_create(struct wlsc_compositor *ec) |
| 76 | { |
Kristian Høgsberg | c5dcb90 | 2010-09-14 15:53:32 -0400 | [diff] [blame^] | 77 | struct wl_screenshooter *shooter; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 78 | |
| 79 | shooter = malloc(sizeof *shooter); |
| 80 | if (shooter == NULL) |
| 81 | return; |
| 82 | |
Kristian Høgsberg | c5dcb90 | 2010-09-14 15:53:32 -0400 | [diff] [blame^] | 83 | shooter->base.interface = &wl_screenshooter_interface; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 84 | shooter->base.implementation = |
| 85 | (void(**)(void)) &screenshooter_implementation; |
| 86 | shooter->ec = ec; |
| 87 | |
| 88 | wl_display_add_object(ec->wl_display, &shooter->base); |
| 89 | wl_display_add_global(ec->wl_display, &shooter->base, NULL); |
| 90 | }; |