blob: 02a8d210dda1c47ebaa48028cb2f4bb9cb805857 [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04004 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040013 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040014 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040021 */
22
23#include <stdlib.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040024
25#include "compositor.h"
Kristian Høgsbergc5dcb902010-09-14 15:53:32 -040026#include "screenshooter-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040027
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040028struct screenshooter {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040029 struct wl_object base;
30 struct wlsc_compositor *ec;
31};
32
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033static void
Kristian Høgsberg85449032011-05-02 12:11:07 -040034screenshooter_shoot(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -040035 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040036 struct wl_resource *output_resource,
37 struct wl_resource *buffer_resource)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040038{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040039 struct wlsc_output *output = output_resource->data;
40 struct wl_buffer *buffer = buffer_resource->data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040041
Kristian Høgsberg85449032011-05-02 12:11:07 -040042 if (!wl_buffer_is_shm(buffer))
43 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040044
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040045 if (buffer->width < output->current->width ||
46 buffer->height < output->current->height)
Kristian Høgsberg85449032011-05-02 12:11:07 -040047 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040048
Kristian Høgsberg85449032011-05-02 12:11:07 -040049 glPixelStorei(GL_PACK_ALIGNMENT, 1);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040050 glReadPixels(0, 0, output->current->width, output->current->height,
Kristian Høgsberg85449032011-05-02 12:11:07 -040051 GL_RGBA, GL_UNSIGNED_BYTE,
52 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040053}
54
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040055struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040056 screenshooter_shoot
57};
58
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040059static void
60bind_shooter(struct wl_client *client,
61 void *data, uint32_t version, uint32_t id)
62{
63 wl_client_add_object(client, &screenshooter_interface,
64 &screenshooter_implementation, id, data);
65}
66
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040067void
68screenshooter_create(struct wlsc_compositor *ec)
69{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040070 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040071
72 shooter = malloc(sizeof *shooter);
73 if (shooter == NULL)
74 return;
75
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040076 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040077 shooter->base.implementation =
78 (void(**)(void)) &screenshooter_implementation;
79 shooter->ec = ec;
80
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040081 wl_display_add_global(ec->wl_display,
82 &screenshooter_interface, shooter, bind_shooter);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040083};