Screenshots were upside down
[profile/ivi/weston.git] / compositor / screenshooter.c
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"
24 #include "screenshooter-server-protocol.h"
25
26 struct wl_screenshooter {
27         struct wl_object base;
28         struct wlsc_compositor *ec;
29 };
30
31 static void
32 screenshooter_shoot(struct wl_client *client, struct wl_screenshooter *shooter)
33 {
34         struct wlsc_compositor *ec = shooter->ec;
35         struct wlsc_output *output;
36         char buffer[256];
37         GdkPixbuf *pixbuf;
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                 gdk_pixbuf_save(pixbuf, buffer, "png", &error, NULL);
63                 g_object_unref(pixbuf);
64                 free(data);
65         }
66 }
67
68 struct wl_screenshooter_interface screenshooter_implementation = {
69         screenshooter_shoot
70 };
71
72 void
73 screenshooter_create(struct wlsc_compositor *ec)
74 {
75         struct wl_screenshooter *shooter;
76
77         shooter = malloc(sizeof *shooter);
78         if (shooter == NULL)
79                 return;
80
81         shooter->base.interface = &wl_screenshooter_interface;
82         shooter->base.implementation =
83                 (void(**)(void)) &screenshooter_implementation;
84         shooter->ec = ec;
85
86         wl_display_add_object(ec->wl_display, &shooter->base);
87         wl_display_add_global(ec->wl_display, &shooter->base, NULL);
88 };