Rename wayland-compositor to weston
[profile/ivi/weston-ivi-shell.git] / src / screenshooter.c
1 /*
2  * Copyright © 2008-2011 Kristian Høgsberg
3  *
4  * 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.
13  *
14  * 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.
21  */
22
23 #include <stdlib.h>
24
25 #include "compositor.h"
26 #include "screenshooter-server-protocol.h"
27
28 struct screenshooter {
29         struct wl_object base;
30         struct weston_compositor *ec;
31 };
32
33 static void
34 screenshooter_shoot(struct wl_client *client,
35                     struct wl_resource *resource,
36                     struct wl_resource *output_resource,
37                     struct wl_resource *buffer_resource)
38 {
39         struct weston_output *output = output_resource->data;
40         struct wl_buffer *buffer = buffer_resource->data;
41
42         if (!wl_buffer_is_shm(buffer))
43                 return;
44
45         if (buffer->width < output->current->width ||
46             buffer->height < output->current->height)
47                 return;
48
49         glPixelStorei(GL_PACK_ALIGNMENT, 1);
50         glReadPixels(0, 0, output->current->width, output->current->height,
51                      GL_RGBA, GL_UNSIGNED_BYTE,
52                      wl_shm_buffer_get_data(buffer));
53 }
54
55 struct screenshooter_interface screenshooter_implementation = {
56         screenshooter_shoot
57 };
58
59 static void
60 bind_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
67 void
68 screenshooter_create(struct weston_compositor *ec)
69 {
70         struct screenshooter *shooter;
71
72         shooter = malloc(sizeof *shooter);
73         if (shooter == NULL)
74                 return;
75
76         shooter->base.interface = &screenshooter_interface;
77         shooter->base.implementation =
78                 (void(**)(void)) &screenshooter_implementation;
79         shooter->ec = ec;
80
81         wl_display_add_global(ec->wl_display,
82                               &screenshooter_interface, shooter, bind_shooter);
83 };