blob: 4f6fee9a71c2742cb5c96a2484852f99b517a71d [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>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040020
21#include "compositor.h"
Kristian Høgsbergc5dcb902010-09-14 15:53:32 -040022#include "screenshooter-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040023
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040024struct screenshooter {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040025 struct wl_object base;
26 struct wlsc_compositor *ec;
27};
28
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040029static void
Kristian Høgsberg85449032011-05-02 12:11:07 -040030screenshooter_shoot(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -040031 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040032 struct wl_resource *output_resource,
33 struct wl_resource *buffer_resource)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040034{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040035 struct wlsc_output *output = output_resource->data;
36 struct wl_buffer *buffer = buffer_resource->data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040037
Kristian Høgsberg85449032011-05-02 12:11:07 -040038 if (!wl_buffer_is_shm(buffer))
39 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040040
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040041 if (buffer->width < output->current->width ||
42 buffer->height < output->current->height)
Kristian Høgsberg85449032011-05-02 12:11:07 -040043 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040044
Kristian Høgsberg85449032011-05-02 12:11:07 -040045 glPixelStorei(GL_PACK_ALIGNMENT, 1);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040046 glReadPixels(0, 0, output->current->width, output->current->height,
Kristian Høgsberg85449032011-05-02 12:11:07 -040047 GL_RGBA, GL_UNSIGNED_BYTE,
48 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040049}
50
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040051struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040052 screenshooter_shoot
53};
54
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040055static void
56bind_shooter(struct wl_client *client,
57 void *data, uint32_t version, uint32_t id)
58{
59 wl_client_add_object(client, &screenshooter_interface,
60 &screenshooter_implementation, id, data);
61}
62
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040063void
64screenshooter_create(struct wlsc_compositor *ec)
65{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040066 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040067
68 shooter = malloc(sizeof *shooter);
69 if (shooter == NULL)
70 return;
71
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040072 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040073 shooter->base.implementation =
74 (void(**)(void)) &screenshooter_implementation;
75 shooter->ec = ec;
76
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040077 wl_display_add_global(ec->wl_display,
78 &screenshooter_interface, shooter, bind_shooter);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040079};