summaryrefslogtreecommitdiff
path: root/compositor/weston-screenshooter.c
blob: 6c40124909a1b35b951abd02fb910669e175af8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright © 2008-2011 Kristian Høgsberg
 * Copyright 2022 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#include <stdint.h>
#include <linux/input.h>

#include <libweston/libweston.h>
#include "weston.h"
#include "shared/helpers.h"

struct screenshooter {
	struct weston_compositor *ec;
	struct wl_client *client;
	struct wl_listener client_destroy_listener;
	struct wl_listener compositor_destroy_listener;
	struct weston_recorder *recorder;
	struct wl_listener authorization;
};

static void
screenshooter_client_destroy(struct wl_listener *listener, void *data)
{
	struct screenshooter *shooter =
		container_of(listener, struct screenshooter,
			     client_destroy_listener);

	shooter->client = NULL;
}

static void
screenshooter_binding(struct weston_keyboard *keyboard,
		      const struct timespec *time, uint32_t key, void *data)
{
	struct screenshooter *shooter = data;
	char *screenshooter_exe;

	/* Don't start a screenshot whilst we already have one in progress */
	if (shooter->client)
		return;

	screenshooter_exe = wet_get_bindir_path("weston-screenshooter");
	if (!screenshooter_exe) {
		weston_log("Could not construct screenshooter path.\n");
		return;
	}

	shooter->client = weston_client_start(shooter->ec,
					      screenshooter_exe);
	free(screenshooter_exe);

	if (!shooter->client)
		return;

	shooter->client_destroy_listener.notify = screenshooter_client_destroy;
	wl_client_add_destroy_listener(shooter->client,
				       &shooter->client_destroy_listener);
}

static void
recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
		 uint32_t key, void *data)
{
	struct weston_compositor *ec = keyboard->seat->compositor;
	struct weston_output *output;
	struct screenshooter *shooter = data;
	struct weston_recorder *recorder = shooter->recorder;;
	static const char filename[] = "capture.wcap";

	if (recorder) {
		weston_recorder_stop(recorder);
		shooter->recorder = NULL;
	} else {
		if (keyboard->focus && keyboard->focus->output)
			output = keyboard->focus->output;
		else
			output = container_of(ec->output_list.next,
					      struct weston_output, link);

		shooter->recorder = weston_recorder_start(output, filename);
	}
}

static void
authorize_screenshooter(struct wl_listener *l,
			struct weston_output_capture_attempt *att)
{
	struct screenshooter *shooter = wl_container_of(l, shooter,
							authorization);

	if (shooter->client && att->who->client == shooter->client)
		att->authorized = true;
}

static void
screenshooter_destroy(struct wl_listener *listener, void *data)
{
	struct screenshooter *shooter =
		container_of(listener, struct screenshooter,
			     compositor_destroy_listener);

	wl_list_remove(&shooter->compositor_destroy_listener.link);
	wl_list_remove(&shooter->authorization.link);

	free(shooter);
}

WL_EXPORT void
screenshooter_create(struct weston_compositor *ec)
{
	struct screenshooter *shooter;

	shooter = zalloc(sizeof *shooter);
	if (shooter == NULL)
		return;

	shooter->ec = ec;

	weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
					  screenshooter_binding, shooter);
	weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
					  recorder_binding, shooter);

	shooter->compositor_destroy_listener.notify = screenshooter_destroy;
	wl_signal_add(&ec->destroy_signal,
		      &shooter->compositor_destroy_listener);

	weston_compositor_add_screenshot_authority(ec, &shooter->authorization,
						   authorize_screenshooter);
}