summaryrefslogtreecommitdiff
path: root/shell/shell-helper.c
blob: 111d4831c7ee533785220f7e4dc00fb432952d56 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Copyright (C) 2014 Collabora Ltd.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 *
 * Author: Jonny Lamb <jonny.lamb@collabora.co.uk>
 */

#include <stdio.h>
#include <assert.h>

#include <weston/compositor.h>

#include "shell-helper-server-protocol.h"

struct shell_helper {
	struct weston_compositor *compositor;

	struct wl_listener destroy_listener;

	struct weston_layer *panel_layer;

	struct wl_list slide_list;
};

static void
shell_helper_move_surface(struct wl_client *client,
			  struct wl_resource *resource,
			  struct wl_resource *surface_resource,
			  int32_t x,
			  int32_t y)
{
	struct shell_helper *helper = wl_resource_get_user_data(resource);
	struct weston_surface *surface =
		wl_resource_get_user_data(surface_resource);
	struct weston_view *view;

	view = container_of(surface->views.next, struct weston_view, surface_link);

	if (!view)
		return;

	weston_view_set_position(view, x, y);
	weston_view_update_transform(view);
}

static void
configure_surface(struct weston_surface *es, int32_t sx, int32_t sy)
{
	struct weston_view *existing_view = es->configure_private;
	struct weston_view *new_view;

	new_view = container_of(es->views.next, struct weston_view, surface_link);

	if (wl_list_empty(&new_view->layer_link)) {
		/* be sure to append to the list, not insert */
		wl_list_insert(&existing_view->layer_link, &new_view->layer_link);
		weston_compositor_schedule_repaint(es->compositor);
	}
}

static void
shell_helper_add_surface_to_layer(struct wl_client *client,
				  struct wl_resource *resource,
				  struct wl_resource *new_surface_resource,
				  struct wl_resource *existing_surface_resource)
{
	struct shell_helper *helper = wl_resource_get_user_data(resource);
	struct weston_surface *new_surface =
		wl_resource_get_user_data(new_surface_resource);
	struct weston_surface *existing_surface =
		wl_resource_get_user_data(existing_surface_resource);
	struct weston_view *new_view, *existing_view, *next;
	struct wl_layer *layer;

	if (new_surface->configure) {
		wl_resource_post_error(new_surface_resource,
				       WL_DISPLAY_ERROR_INVALID_OBJECT,
				       "surface role already assigned");
		return;
	}

	existing_view = container_of(existing_surface->views.next,
				     struct weston_view,
				     surface_link);

	wl_list_for_each_safe(new_view, next, &new_surface->views, surface_link)
		weston_view_destroy(new_view);
	new_view = weston_view_create(new_surface);

	new_surface->configure = configure_surface;
	new_surface->configure_private = existing_view;
	new_surface->output = existing_view->output;
}

static void
configure_panel(struct weston_surface *es, int32_t sx, int32_t sy)
{
	struct shell_helper *helper = es->configure_private;
	struct weston_view *view;

	view = container_of(es->views.next, struct weston_view, surface_link);

	if (wl_list_empty(&view->layer_link)) {
		wl_list_insert(&helper->panel_layer->view_list, &view->layer_link);
		weston_compositor_schedule_repaint(es->compositor);
	}
}

static void
shell_helper_set_panel(struct wl_client *client,
		       struct wl_resource *resource,
		       struct wl_resource *surface_resource)
{
	struct shell_helper *helper = wl_resource_get_user_data(resource);
	struct weston_surface *surface =
		wl_resource_get_user_data(surface_resource);
	struct weston_view *view = container_of(surface->views.next,
						struct weston_view,
						surface_link);

	/* we need to save the panel's layer so we can use it later on, but
	 * it hasn't yet been defined because the original surface configure
	 * function hasn't yet been calld. if we call it here we will have
	 * access to the layer. */
	surface->configure(surface, 0, 0);

	helper->panel_layer = container_of(&view->layer_link,
					   struct weston_layer,
					   view_list);

	/* set new configure functions that only ensure the surface is in the
	 * correct layer. */
	surface->configure = configure_panel;
	surface->configure_private = helper;
}

enum SlideState {
	SLIDE_STATE_NONE,
	SLIDE_STATE_SLIDING_OUT,
	SLIDE_STATE_OUT,
	SLIDE_STATE_SLIDING_BACK,
	SLIDE_STATE_BACK
};

enum SlideRequest {
	SLIDE_REQUEST_NONE,
	SLIDE_REQUEST_OUT,
	SLIDE_REQUEST_BACK
};

struct slide {
	struct weston_surface *surface;
	struct weston_view *view;
	int x;
	int y;

	enum SlideState state;
	enum SlideRequest request;

	struct weston_transform transform;

	struct wl_list link;
};

static void slide_back(struct slide *slide);

static void
slide_done_cb(struct weston_view_animation *animation, void *data)
{
	struct slide *slide = data;

	slide->state = SLIDE_STATE_OUT;

	wl_list_insert(&slide->view->transform.position.link,
		       &slide->transform.link);
	weston_matrix_init(&slide->transform.matrix);
	weston_matrix_translate(&slide->transform.matrix,
				slide->x,
				slide->y,
				0);

	weston_view_geometry_dirty(slide->view);
	weston_compositor_schedule_repaint(slide->surface->compositor);

	if (slide->request == SLIDE_REQUEST_BACK) {
		slide->request = SLIDE_REQUEST_NONE;
		slide_back(slide);
	}
}

static void
slide_out(struct slide *slide)
{
	assert(slide->state == SLIDE_STATE_NONE || slide->state == SLIDE_STATE_BACK);

	slide->state = SLIDE_STATE_SLIDING_OUT;

	weston_move_scale_run(slide->view, slide->x, slide->y,
			      1.0, 1.0, 0,
			      slide_done_cb, slide);
}

static void
slide_back_done_cb(struct weston_view_animation *animation, void *data)
{
	struct slide *slide = data;

	slide->state = SLIDE_STATE_BACK;

	wl_list_remove(&slide->transform.link);
	weston_view_geometry_dirty(slide->view);

	if (slide->request == SLIDE_REQUEST_OUT) {
		slide->request = SLIDE_REQUEST_NONE;
		slide_out(slide);
	} else {
		wl_list_remove(&slide->link);
		free(slide);
	}
}

static void
slide_back(struct slide *slide)
{
	assert(slide->state == SLIDE_STATE_OUT);

	slide->state = SLIDE_STATE_SLIDING_BACK;

	weston_move_scale_run(slide->view, -slide->x, -slide->y,
			      1.0, 1.0, 0,
			      slide_back_done_cb, slide);
}

static void
shell_helper_slide_surface(struct wl_client *client,
			   struct wl_resource *resource,
			   struct wl_resource *surface_resource,
			   int32_t x,
			   int32_t y)
{
	struct shell_helper *helper = wl_resource_get_user_data(resource);
	struct weston_surface *surface =
		wl_resource_get_user_data(surface_resource);
	struct weston_view *view;
	struct slide *slide;

	wl_list_for_each(slide, &helper->slide_list, link) {
		if (slide->surface == surface) {
			if (slide->state == SLIDE_STATE_SLIDING_BACK)
				slide->request = SLIDE_REQUEST_OUT;
			return;
		}
	}

	view = container_of(surface->views.next, struct weston_view, surface_link);

	if (!view)
		return;

	slide = malloc(sizeof *slide);
	if (!slide)
		return;

	slide->surface = surface;
	slide->view = view;
	slide->x = x;
	slide->y = y;

	slide->state = SLIDE_STATE_NONE;
	slide->request = SLIDE_REQUEST_NONE;

	wl_list_insert(&helper->slide_list,
		       &slide->link);

	slide_out(slide);
}

static void
shell_helper_slide_surface_back(struct wl_client *client,
				struct wl_resource *resource,
				struct wl_resource *surface_resource)
{
	struct shell_helper *helper = wl_resource_get_user_data(resource);
	struct weston_surface *surface =
		wl_resource_get_user_data(surface_resource);
	struct weston_view *view;
	int found = 0;
	struct slide *slide;

	wl_list_for_each(slide, &helper->slide_list, link) {
		if (slide->surface == surface) {
			found = 1;
			break;
		}
	}

	if (!found || slide->state == SLIDE_STATE_SLIDING_BACK)
		return;

	if (slide->state == SLIDE_STATE_SLIDING_OUT)
		slide->request = SLIDE_REQUEST_BACK;
	else
		slide_back(slide);
}

static const struct shell_helper_interface helper_implementation = {
	shell_helper_move_surface,
	shell_helper_add_surface_to_layer,
	shell_helper_set_panel,
	shell_helper_slide_surface,
	shell_helper_slide_surface_back
};

static void
bind_helper(struct wl_client *client, void *data, uint32_t version, uint32_t id)
{
	struct shell_helper *helper = data;
	struct wl_resource *resource;

	resource = wl_resource_create(client, &shell_helper_interface, 1, id);
	if (resource)
		wl_resource_set_implementation(resource, &helper_implementation,
					       helper, NULL);
}

static void
helper_destroy(struct wl_listener *listener, void *data)
{
	struct shell_helper *helper =
		container_of(listener, struct shell_helper, destroy_listener);

	free(helper);
}

WL_EXPORT int
module_init(struct weston_compositor *ec,
	    int *argc, char *argv[])
{
	struct shell_helper *helper;

	helper = zalloc(sizeof *helper);
	if (helper == NULL)
		return -1;

	helper->compositor = ec;
	helper->panel_layer = NULL;

	wl_list_init(&helper->slide_list);

	helper->destroy_listener.notify = helper_destroy;
	wl_signal_add(&ec->destroy_signal, &helper->destroy_listener);

	if (wl_global_create(ec->wl_display, &shell_helper_interface, 1,
			     helper, bind_helper) == NULL)
		return -1;

	return 0;
}