blob: aa7788e4ab506244e744e80c31b9ee5cfab2a41b [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"
24
25struct screenshooter {
26 struct wl_object base;
27 struct wlsc_compositor *ec;
28};
29
30struct screenshooter_interface {
31 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
32};
33
34static void
35screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
36{
37 struct wlsc_compositor *ec = shooter->ec;
38 struct wlsc_output *output;
39 char buffer[256];
40 GdkPixbuf *pixbuf, *normal;
41 GError *error = NULL;
42 unsigned char *data;
43 int i, j;
44
45 i = 0;
46 wl_list_for_each(output, &ec->output_list, link) {
47 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
48 data = malloc(output->width * output->height * 4);
49 if (data == NULL) {
50 fprintf(stderr, "couldn't allocate image buffer\n");
51 continue;
52 }
53
54 glPixelStorei(GL_PACK_ALIGNMENT, 1);
55 glReadPixels(0, 0, output->width, output->height,
56 GL_RGBA, GL_UNSIGNED_BYTE, data);
57
58 /* FIXME: We should just use a RGB visual for the frontbuffer. */
59 for (j = 3; j < output->width * output->height * 4; j += 4)
60 data[j] = 0xff;
61
62 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
63 8, output->width, output->height, output->width * 4,
64 NULL, NULL);
65 normal = gdk_pixbuf_flip(pixbuf, FALSE);
66 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
67 gdk_pixbuf_unref(normal);
68 gdk_pixbuf_unref(pixbuf);
69 free(data);
70 }
71}
72
73static const struct wl_message screenshooter_methods[] = {
74 { "shoot", "", NULL }
75};
76
77static const struct wl_interface screenshooter_interface = {
78 "screenshooter", 1,
79 ARRAY_LENGTH(screenshooter_methods),
80 screenshooter_methods,
81};
82
83struct screenshooter_interface screenshooter_implementation = {
84 screenshooter_shoot
85};
86
87void
88screenshooter_create(struct wlsc_compositor *ec)
89{
90 struct screenshooter *shooter;
91
92 shooter = malloc(sizeof *shooter);
93 if (shooter == NULL)
94 return;
95
96 shooter->base.interface = &screenshooter_interface;
97 shooter->base.implementation =
98 (void(**)(void)) &screenshooter_implementation;
99 shooter->ec = ec;
100
101 wl_display_add_object(ec->wl_display, &shooter->base);
102 wl_display_add_global(ec->wl_display, &shooter->base, NULL);
103};