blob: 080427b7bf8d31ce1d0e5e5986a5f3c0f4d783e8 [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øgsbergf02a6492012-03-12 01:05:25 -040024#include <string.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040025
26#include "compositor.h"
Kristian Høgsbergc5dcb902010-09-14 15:53:32 -040027#include "screenshooter-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040028
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040029struct screenshooter {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040030 struct wl_object base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050031 struct weston_compositor *ec;
Pekka Paalanen35ce06d2012-01-03 11:39:13 +020032 struct wl_global *global;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033};
34
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040035static void
Kristian Høgsberg85449032011-05-02 12:11:07 -040036screenshooter_shoot(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -040037 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040038 struct wl_resource *output_resource,
39 struct wl_resource *buffer_resource)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040040{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050041 struct weston_output *output = output_resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040042 struct wl_buffer *buffer = buffer_resource->data;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040043 uint8_t *tmp, *d, *s;
44 int32_t stride, i;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045
Kristian Høgsberg85449032011-05-02 12:11:07 -040046 if (!wl_buffer_is_shm(buffer))
47 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040048
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040049 if (buffer->width < output->current->width ||
50 buffer->height < output->current->height)
Kristian Høgsberg85449032011-05-02 12:11:07 -040051 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040052
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040053 stride = wl_shm_buffer_get_stride(buffer);
54 tmp = malloc(stride * buffer->height);
55 if (tmp == NULL) {
56 wl_resource_post_no_memory(resource);
57 return;
58 }
59
Kristian Høgsberg85449032011-05-02 12:11:07 -040060 glPixelStorei(GL_PACK_ALIGNMENT, 1);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040061 glReadPixels(0, 0, output->current->width, output->current->height,
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040062 GL_RGBA, GL_UNSIGNED_BYTE, tmp);
63
64 d = wl_shm_buffer_get_data(buffer);
65 s = tmp + stride * (buffer->height - 1);
66
67 for (i = 0; i < buffer->height; i++) {
68 memcpy(d, s, stride);
69 d += stride;
70 s -= stride;
71 }
72
73 free(tmp);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040074}
75
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040076struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040077 screenshooter_shoot
78};
79
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040080static void
81bind_shooter(struct wl_client *client,
82 void *data, uint32_t version, uint32_t id)
83{
84 wl_client_add_object(client, &screenshooter_interface,
85 &screenshooter_implementation, id, data);
86}
87
Pekka Paalanen35ce06d2012-01-03 11:39:13 +020088struct screenshooter *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050089screenshooter_create(struct weston_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040090{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040091 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040092
93 shooter = malloc(sizeof *shooter);
94 if (shooter == NULL)
Pekka Paalanen35ce06d2012-01-03 11:39:13 +020095 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040096
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040097 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040098 shooter->base.implementation =
99 (void(**)(void)) &screenshooter_implementation;
100 shooter->ec = ec;
101
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200102 shooter->global = wl_display_add_global(ec->wl_display,
103 &screenshooter_interface,
104 shooter, bind_shooter);
105
106 return shooter;
107}
108
109void
110screenshooter_destroy(struct screenshooter *shooter)
111{
112 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
113 free(shooter);
114}