blob: c1716ec2f5a785cb2792394922c59daea4f8a493 [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
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øgsbergc5dcb902010-09-14 15:53:32 -040024#include "screenshooter-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040025
Kristian Høgsbergc5dcb902010-09-14 15:53:32 -040026struct wl_screenshooter {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040027 struct wl_object base;
28 struct wlsc_compositor *ec;
29};
30
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031static void
Kristian Høgsbergc5dcb902010-09-14 15:53:32 -040032screenshooter_shoot(struct wl_client *client, struct wl_screenshooter *shooter)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033{
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øgsbergc5dcb902010-09-14 15:53:32 -040070struct wl_screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040071 screenshooter_shoot
72};
73
74void
75screenshooter_create(struct wlsc_compositor *ec)
76{
Kristian Høgsbergc5dcb902010-09-14 15:53:32 -040077 struct wl_screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040078
79 shooter = malloc(sizeof *shooter);
80 if (shooter == NULL)
81 return;
82
Kristian Høgsbergc5dcb902010-09-14 15:53:32 -040083 shooter->base.interface = &wl_screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040084 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};