2 * Copyright © 2008 Kristian Høgsberg
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.
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.
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.
20 #include <GLES2/gl2.h>
21 #include <gdk-pixbuf/gdk-pixbuf.h>
23 #include "compositor.h"
24 #include "screenshooter-server-protocol.h"
26 struct wl_screenshooter {
27 struct wl_object base;
28 struct wlsc_compositor *ec;
32 screenshooter_shoot(struct wl_client *client, struct wl_screenshooter *shooter)
34 struct wlsc_compositor *ec = shooter->ec;
35 struct wlsc_output *output;
37 GdkPixbuf *pixbuf, *normal;
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);
47 fprintf(stderr, "couldn't allocate image buffer\n");
51 glPixelStorei(GL_PACK_ALIGNMENT, 1);
52 glReadPixels(0, 0, output->width, output->height,
53 GL_RGBA, GL_UNSIGNED_BYTE, data);
55 /* FIXME: We should just use a RGB visual for the frontbuffer. */
56 for (j = 3; j < output->width * output->height * 4; j += 4)
59 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
60 8, output->width, output->height, output->width * 4,
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);
70 struct wl_screenshooter_interface screenshooter_implementation = {
75 screenshooter_create(struct wlsc_compositor *ec)
77 struct wl_screenshooter *shooter;
79 shooter = malloc(sizeof *shooter);
83 shooter->base.interface = &wl_screenshooter_interface;
84 shooter->base.implementation =
85 (void(**)(void)) &screenshooter_implementation;
88 wl_display_add_object(ec->wl_display, &shooter->base);
89 wl_display_add_global(ec->wl_display, &shooter->base, NULL);