summaryrefslogtreecommitdiff
path: root/overlay-plugin.c
blob: f0a5932ef0bc21abb77f4f2d5a4a7eba85d054f8 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <linux/input.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/uio.h>

#include <weston/compositor.h>
#include "overlay-server-protocol.h"

struct overlay {
	struct weston_compositor *compositor;
	struct wl_global *global;
	struct wl_resource *resource;
	struct wl_listener destroy_listener;
	struct wl_listener surface_destroy_listener;
	struct weston_surface *surface;
	struct weston_binding *binding;
	struct weston_layer layer;
	struct wl_seat *seat;
	struct wl_pointer_grab grab;
	struct weston_output *output;

	struct wl_surface *saved_focus;
	struct wl_listener saved_focus_destroy_listener;
};

static void
handle_surface_destroy(struct wl_listener *listener, void *data)
{
	struct overlay *overlay =
		container_of(listener, struct overlay,
			     surface_destroy_listener);

	overlay->surface = NULL;

	if (overlay->saved_focus) {
		weston_surface_activate((struct weston_surface *) overlay->saved_focus,
					(struct weston_seat *) overlay->seat);
		wl_list_remove(&overlay->saved_focus_destroy_listener.link);
	}
}

static void
overlay_close(struct overlay *overlay)
{
	overlay_send_done(overlay->resource);
	wl_pointer_end_grab(overlay->grab.pointer);

}

static void
overlay_grab_focus(struct wl_pointer_grab *grab,
		   struct wl_surface *surface,
		   wl_fixed_t x, wl_fixed_t y)
{
	struct wl_pointer *pointer = grab->pointer;
	struct overlay *overlay = container_of(grab, struct overlay, grab);

	if (surface == &overlay->surface->surface) {
		wl_pointer_set_focus(pointer, surface, x, y);
		grab->focus = surface;
	} else {
		wl_pointer_set_focus(pointer, NULL,
				     wl_fixed_from_int(0),
				     wl_fixed_from_int(0));
		grab->focus = NULL;
	}
}

static void
overlay_grab_motion(struct wl_pointer_grab *grab,
		    uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
{
	struct wl_resource *resource;

	resource = grab->pointer->focus_resource;
	if (resource)
		wl_pointer_send_motion(resource, time, sx, sy);
}

static void
overlay_grab_button(struct wl_pointer_grab *grab,
		    uint32_t time, uint32_t button, uint32_t state_w)
{
	struct wl_resource *resource;
	struct overlay *overlay = container_of(grab, struct overlay, grab);
	struct wl_display *display;
	enum wl_pointer_button_state state = state_w;
	uint32_t serial;

	resource = grab->pointer->focus_resource;
	if (resource) {
		display = wl_client_get_display(resource->client);
		serial = wl_display_get_serial(display);
		wl_pointer_send_button(resource, serial, time, button, state);
	} else {
		overlay_close(overlay);
	}
}

static const struct wl_pointer_grab_interface overlay_grab_interface = {
	overlay_grab_focus,
	overlay_grab_motion,
	overlay_grab_button,
};

static struct weston_output *
get_output_for_pointer(struct overlay *overlay)
{
	struct weston_output *output;
	int x, y;

	x = wl_fixed_to_int(overlay->seat->pointer->x);
	y = wl_fixed_to_int(overlay->seat->pointer->y);

	wl_list_for_each(output, &overlay->compositor->output_list, link) {
		if (pixman_region32_contains_point(&output->region,
						   x, y, NULL))
			return output;
	}

	return NULL;
}

static void
handle_saved_focus_destroy(struct wl_listener *listener, void *data)
{
	struct overlay *overlay =container_of(listener, struct overlay,
					      saved_focus_destroy_listener);

	overlay->saved_focus = NULL;
}

static void
center_on_output(struct overlay *overlay, int32_t width, int32_t height)
{
	struct weston_surface *surface = overlay->surface;
	struct weston_output *output = overlay->output;

	surface->geometry.x = output->x + (output->width - width) / 2;
	surface->geometry.y = output->y + (output->height - height) / 2;
	surface->geometry.width = width;
	surface->geometry.height = height;

	weston_surface_geometry_dirty(surface);

}

static void
overlay_map(struct overlay *overlay, int32_t width, int32_t height)
{
	struct weston_surface *surface = overlay->surface;

	wl_list_insert(&overlay->layer.surface_list, &surface->layer_link);

	overlay->saved_focus = overlay->seat->keyboard->focus;
	if (overlay->saved_focus) {
		overlay->saved_focus_destroy_listener.notify =
			handle_saved_focus_destroy;
		wl_signal_add(&overlay->saved_focus->resource.destroy_signal,
			      &overlay->saved_focus_destroy_listener);
	}

	weston_surface_activate(overlay->surface,
				(struct weston_seat *) overlay->seat);
	overlay->grab.interface = &overlay_grab_interface;
	wl_pointer_start_grab(overlay->seat->pointer, &overlay->grab);

	overlay->output = get_output_for_pointer(overlay);
	center_on_output(overlay, width, height);
	weston_surface_update_transform(surface);
	weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
}

static void
overlay_configure(struct weston_surface *surface,
		  int32_t sx, int32_t sy, int32_t width, int32_t height)
{
	struct overlay *overlay = surface->private;
	struct weston_output *output;

	if (wl_list_empty(&surface->layer_link))
		overlay_map(overlay, width, height);
	else
		center_on_output(overlay, width, height);
}

static void
overlay_handle_surface(struct wl_client *client,
		       struct wl_resource *resource,
		       struct wl_resource *surface_resource)
{
	struct overlay *overlay = resource->data;

	overlay->surface = surface_resource->data;
	overlay->surface_destroy_listener.notify = handle_surface_destroy;
	wl_signal_add(&surface_resource->destroy_signal,
		      &overlay->surface_destroy_listener);

	overlay->surface->configure = overlay_configure;
	overlay->surface->private = overlay;
}

static void
overlay_handle_close(struct wl_client *client,
		     struct wl_resource *resource)
{
	struct overlay *overlay = resource->data;

	overlay_close(overlay);	
}

static const struct overlay_interface overlay_implementation = {
	overlay_handle_surface,
	overlay_handle_close
};

static void
overlay_binding(struct wl_seat *_seat,
		uint32_t time, uint32_t key, void *data)
{
	struct overlay *overlay = data;

	fprintf(stderr, "overlay binding pressed\n");

	overlay->seat = _seat;
	if (overlay->resource)
		overlay_send_activate(overlay->resource);
}

static void
handle_overlay_destroy(struct wl_resource *resource)
{
	struct overlay *overlay = resource->data;

	overlay->resource = NULL;
	free(resource);
}

static void
bind_overlay(struct wl_client *client,
	     void *data, uint32_t version, uint32_t id)
{
	struct overlay *overlay = data;

	fprintf(stderr, "client %p binds to overlay global\n", client);

	overlay->resource =
		wl_client_add_object(client, &overlay_interface,
				     &overlay_implementation, id, overlay);
	overlay->resource->destroy = handle_overlay_destroy;
}

static void
overlay_destroy(struct wl_listener *listener, void *data)
{
	struct overlay *overlay =
		container_of(listener, struct overlay, destroy_listener);

	wl_display_remove_global(overlay->compositor->wl_display,
				 overlay->global);
	free(overlay);
}

int
module_init(struct weston_compositor *compositor,
	    int *argc, char *argv[], const char *config_file);

WL_EXPORT int
module_init(struct weston_compositor *compositor,
	    int *argc, char *argv[], const char *config_file)
{
	struct overlay *overlay;
	int major, minor, micro;

	weston_version(&major, &minor, &micro);
	if (major != 1 || minor != 0 || micro < 90) {
		weston_log("overlay plugin requires weston > 1.0.90\n");
		return -1;
	}

	overlay = malloc(sizeof *overlay);
	if (overlay == NULL)
		return -1;

	overlay->compositor = compositor;
	overlay->global = wl_display_add_global(compositor->wl_display,
					       &overlay_interface,
					       overlay, bind_overlay);

	overlay->destroy_listener.notify = overlay_destroy;
	wl_signal_add(&compositor->destroy_signal, &overlay->destroy_listener);

	overlay->binding =
		weston_compositor_add_key_binding(compositor, KEY_SPACE,
						  MODIFIER_SUPER,
						  overlay_binding, overlay);

	weston_layer_init(&overlay->layer, &compositor->cursor_layer.link);

	weston_log("overlay initialized\n");

	return 0;
}