blob: 27967c46a2f7d8367a7fbcc96af679cec14e1d0d [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;
Pekka Paalanen35ce06d2012-01-03 11:39:13 +020031 struct wl_global *global;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040032};
33
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040034static void
Kristian Høgsberg85449032011-05-02 12:11:07 -040035screenshooter_shoot(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -040036 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040037 struct wl_resource *output_resource,
38 struct wl_resource *buffer_resource)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040039{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040040 struct wlsc_output *output = output_resource->data;
41 struct wl_buffer *buffer = buffer_resource->data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040042
Kristian Høgsberg85449032011-05-02 12:11:07 -040043 if (!wl_buffer_is_shm(buffer))
44 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040046 if (buffer->width < output->current->width ||
47 buffer->height < output->current->height)
Kristian Høgsberg85449032011-05-02 12:11:07 -040048 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040049
Kristian Høgsberg85449032011-05-02 12:11:07 -040050 glPixelStorei(GL_PACK_ALIGNMENT, 1);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040051 glReadPixels(0, 0, output->current->width, output->current->height,
Kristian Høgsberg85449032011-05-02 12:11:07 -040052 GL_RGBA, GL_UNSIGNED_BYTE,
53 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040054}
55
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040056struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040057 screenshooter_shoot
58};
59
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040060static void
61bind_shooter(struct wl_client *client,
62 void *data, uint32_t version, uint32_t id)
63{
64 wl_client_add_object(client, &screenshooter_interface,
65 &screenshooter_implementation, id, data);
66}
67
Pekka Paalanen35ce06d2012-01-03 11:39:13 +020068struct screenshooter *
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040069screenshooter_create(struct wlsc_compositor *ec)
70{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040071 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040072
73 shooter = malloc(sizeof *shooter);
74 if (shooter == NULL)
Pekka Paalanen35ce06d2012-01-03 11:39:13 +020075 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040076
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040077 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040078 shooter->base.implementation =
79 (void(**)(void)) &screenshooter_implementation;
80 shooter->ec = ec;
81
Pekka Paalanen35ce06d2012-01-03 11:39:13 +020082 shooter->global = wl_display_add_global(ec->wl_display,
83 &screenshooter_interface,
84 shooter, bind_shooter);
85
86 return shooter;
87}
88
89void
90screenshooter_destroy(struct screenshooter *shooter)
91{
92 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
93 free(shooter);
94}