blob: 433f08aefb1fd21d5e3d5d2d6945648ed1fe9fdf [file] [log] [blame]
Quentin Glidic248dd102016-08-12 10:41:34 +02001/*
2 * Copyright © 2016 Quentin "Sardem FF7" Glidic
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include "config.h"
25
26#include <string.h>
27#include <assert.h>
28
29#include <wayland-server.h>
30
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020031#include <libweston/libweston.h>
Pekka Paalanenecbdcfd2019-04-04 14:46:00 +030032#include <libweston/zalloc.h>
Quentin Glidic248dd102016-08-12 10:41:34 +020033
Pekka Paalanen8ebd9812019-04-04 16:02:14 +030034#include <libweston-desktop/libweston-desktop.h>
Quentin Glidic248dd102016-08-12 10:41:34 +020035#include "internal.h"
36
37struct weston_desktop_view {
38 struct wl_list link;
39 struct weston_view *view;
40 struct weston_desktop_view *parent;
41 struct wl_list children_list;
42 struct wl_list children_link;
43};
44
45struct weston_desktop_surface {
46 struct weston_desktop *desktop;
47 struct weston_desktop_client *client;
48 struct wl_list client_link;
49 const struct weston_desktop_surface_implementation *implementation;
50 void *implementation_data;
51 void *user_data;
52 struct weston_surface *surface;
53 struct wl_list view_list;
Quentin Glidic003da882016-08-15 12:21:39 +020054 struct weston_position buffer_move;
Quentin Glidic248dd102016-08-12 10:41:34 +020055 struct wl_listener surface_commit_listener;
56 struct wl_listener surface_destroy_listener;
57 struct wl_listener client_destroy_listener;
58 struct wl_list children_list;
59
60 struct wl_list resource_list;
61 bool has_geometry;
62 struct weston_geometry geometry;
63 struct {
64 char *title;
65 char *app_id;
66 pid_t pid;
Matt Hoosier555c5482017-09-05 08:05:49 -050067 struct wl_signal metadata_signal;
Quentin Glidic248dd102016-08-12 10:41:34 +020068 };
69 struct {
70 struct weston_desktop_surface *parent;
71 struct wl_list children_link;
72 struct weston_position position;
73 bool use_geometry;
74 };
75 struct {
76 struct wl_list grab_link;
77 };
78};
79
80static void
81weston_desktop_surface_update_view_position(struct weston_desktop_surface *surface)
82{
83 struct weston_desktop_view *view;
84 int32_t x, y;
85
86 x = surface->position.x;
87 y = surface->position.y;
88
89 if (surface->use_geometry) {
90 struct weston_desktop_surface *parent =
91 weston_desktop_surface_get_parent(surface);
92 struct weston_geometry geometry, parent_geometry;
93
94 geometry = weston_desktop_surface_get_geometry(surface);
95 parent_geometry = weston_desktop_surface_get_geometry(parent);
96
97 x += parent_geometry.x - geometry.x;
98 y += parent_geometry.y - geometry.y;
99 }
100 wl_list_for_each(view, &surface->view_list, link)
101 weston_view_set_position(view->view, x, y);
102}
103
104
105static void
106weston_desktop_view_propagate_layer(struct weston_desktop_view *view);
107
108static void
Quentin Glidic248dd102016-08-12 10:41:34 +0200109weston_desktop_view_destroy(struct weston_desktop_view *view)
110{
111 struct weston_desktop_view *child_view, *tmp;
112
113 wl_list_for_each_safe(child_view, tmp, &view->children_list, children_link)
114 weston_desktop_view_destroy(child_view);
115
116 wl_list_remove(&view->children_link);
117 wl_list_remove(&view->link);
118
119 weston_view_damage_below(view->view);
120 if (view->parent != NULL)
121 weston_view_destroy(view->view);
122
123 free(view);
124}
125
126void
127weston_desktop_surface_destroy(struct weston_desktop_surface *surface)
128{
129 struct weston_desktop_view *view, *next_view;
130 struct weston_desktop_surface *child, *next_child;
131
132 wl_list_remove(&surface->surface_commit_listener.link);
133 wl_list_remove(&surface->surface_destroy_listener.link);
134 wl_list_remove(&surface->client_destroy_listener.link);
135
136 if (!wl_list_empty(&surface->resource_list)) {
137 struct wl_resource *resource, *tmp;
138 wl_resource_for_each_safe(resource, tmp, &surface->resource_list) {
139 wl_resource_set_user_data(resource, NULL);
140 wl_list_remove(wl_resource_get_link(resource));
141 }
142 }
143
144 surface->implementation->destroy(surface, surface->implementation_data);
145
146 surface->surface->committed = NULL;
147 surface->surface->committed_private = NULL;
148
149 weston_desktop_surface_unset_relative_to(surface);
150 wl_list_remove(&surface->client_link);
151
152 wl_list_for_each_safe(child, next_child,
153 &surface->children_list,
154 children_link)
155 weston_desktop_surface_unset_relative_to(child);
156
157 wl_list_for_each_safe(view, next_view, &surface->view_list, link)
158 weston_desktop_view_destroy(view);
159
160 free(surface->title);
161 free(surface->app_id);
162
163 free(surface);
164}
165
166static void
167weston_desktop_surface_surface_committed(struct wl_listener *listener,
168 void *data)
169{
170 struct weston_desktop_surface *surface =
171 wl_container_of(listener, surface, surface_commit_listener);
172
Quentin Glidic003da882016-08-15 12:21:39 +0200173 if (surface->implementation->committed != NULL)
174 surface->implementation->committed(surface,
175 surface->implementation_data,
176 surface->buffer_move.x,
177 surface->buffer_move.y);
178
179 if (surface->parent != NULL) {
180 struct weston_desktop_view *view;
181
182 wl_list_for_each(view, &surface->view_list, link) {
183 weston_view_set_transform_parent(view->view,
184 view->parent->view);
185 weston_desktop_view_propagate_layer(view->parent);
186 }
187 weston_desktop_surface_update_view_position(surface);
188 }
189
190 if (!wl_list_empty(&surface->children_list)) {
191 struct weston_desktop_surface *child;
192
193 wl_list_for_each(child, &surface->children_list, children_link)
194 weston_desktop_surface_update_view_position(child);
195 }
196
197 surface->buffer_move.x = 0;
198 surface->buffer_move.y = 0;
Quentin Glidic248dd102016-08-12 10:41:34 +0200199}
200
201static void
202weston_desktop_surface_surface_destroyed(struct wl_listener *listener,
203 void *data)
204{
205 struct weston_desktop_surface *surface =
206 wl_container_of(listener, surface, surface_destroy_listener);
207
208 weston_desktop_surface_destroy(surface);
209}
210
211void
212weston_desktop_surface_resource_destroy(struct wl_resource *resource)
213{
214 struct weston_desktop_surface *surface =
215 wl_resource_get_user_data(resource);
216
217 if (surface != NULL)
218 weston_desktop_surface_destroy(surface);
219}
220
221static void
222weston_desktop_surface_committed(struct weston_surface *wsurface,
223 int32_t sx, int32_t sy)
224{
225 struct weston_desktop_surface *surface = wsurface->committed_private;
226
Quentin Glidic003da882016-08-15 12:21:39 +0200227 surface->buffer_move.x = sx;
228 surface->buffer_move.y = sy;
Quentin Glidic248dd102016-08-12 10:41:34 +0200229}
230
231static void
232weston_desktop_surface_client_destroyed(struct wl_listener *listener,
233 void *data)
234{
235 struct weston_desktop_surface *surface =
236 wl_container_of(listener, surface, client_destroy_listener);
237
238 weston_desktop_surface_destroy(surface);
239}
240
241struct weston_desktop_surface *
242weston_desktop_surface_create(struct weston_desktop *desktop,
243 struct weston_desktop_client *client,
244 struct weston_surface *wsurface,
245 const struct weston_desktop_surface_implementation *implementation,
246 void *implementation_data)
247{
248 assert(implementation->destroy != NULL);
249
250 struct weston_desktop_surface *surface;
251
252 surface = zalloc(sizeof(struct weston_desktop_surface));
253 if (surface == NULL) {
254 if (client != NULL)
255 wl_client_post_no_memory(weston_desktop_client_get_client(client));
256 return NULL;
257 }
258
259 surface->desktop = desktop;
260 surface->implementation = implementation;
261 surface->implementation_data = implementation_data;
262 surface->surface = wsurface;
263
264 surface->client = client;
265 surface->client_destroy_listener.notify =
266 weston_desktop_surface_client_destroyed;
267 weston_desktop_client_add_destroy_listener(
268 client, &surface->client_destroy_listener);
269
270 wsurface->committed = weston_desktop_surface_committed;
271 wsurface->committed_private = surface;
272
Giulio Camuffof15320f2016-12-08 09:21:08 +0100273 surface->pid = -1;
274
Quentin Glidic248dd102016-08-12 10:41:34 +0200275 surface->surface_commit_listener.notify =
276 weston_desktop_surface_surface_committed;
277 wl_signal_add(&surface->surface->commit_signal,
278 &surface->surface_commit_listener);
279 surface->surface_destroy_listener.notify =
280 weston_desktop_surface_surface_destroyed;
281 wl_signal_add(&surface->surface->destroy_signal,
282 &surface->surface_destroy_listener);
283
284 wl_list_init(&surface->client_link);
285 wl_list_init(&surface->resource_list);
286 wl_list_init(&surface->children_list);
287 wl_list_init(&surface->children_link);
288 wl_list_init(&surface->view_list);
289 wl_list_init(&surface->grab_link);
290
Matt Hoosier555c5482017-09-05 08:05:49 -0500291 wl_signal_init(&surface->metadata_signal);
292
Quentin Glidic248dd102016-08-12 10:41:34 +0200293 return surface;
294}
295
296struct wl_resource *
297weston_desktop_surface_add_resource(struct weston_desktop_surface *surface,
298 const struct wl_interface *interface,
299 const void *implementation, uint32_t id,
300 wl_resource_destroy_func_t destroy)
301{
302 struct wl_resource *client_resource =
303 weston_desktop_client_get_resource(surface->client);
304 struct wl_client *wl_client =
305 weston_desktop_client_get_client(surface->client);
306 struct wl_resource *resource;
307
308 resource = wl_resource_create(wl_client,
309 interface,
310 wl_resource_get_version(client_resource),
311 id);
312 if (resource == NULL) {
313 wl_client_post_no_memory(wl_client);
314 weston_desktop_surface_destroy(surface);
315 return NULL;
316 }
317 if (destroy == NULL)
318 destroy = weston_desktop_surface_resource_destroy;
319 wl_resource_set_implementation(resource, implementation, surface, destroy);
320 wl_list_insert(&surface->resource_list, wl_resource_get_link(resource));
321
322 return resource;
323}
324
325struct weston_desktop_surface *
326weston_desktop_surface_from_grab_link(struct wl_list *grab_link)
327{
328 struct weston_desktop_surface *surface =
329 wl_container_of(grab_link, surface, grab_link);
330
331 return surface;
332}
333
334WL_EXPORT bool
335weston_surface_is_desktop_surface(struct weston_surface *wsurface)
336{
337 return wsurface->committed == weston_desktop_surface_committed;
338}
339
340WL_EXPORT struct weston_desktop_surface *
341weston_surface_get_desktop_surface(struct weston_surface *wsurface)
342{
343 if (!weston_surface_is_desktop_surface(wsurface))
344 return NULL;
345 return wsurface->committed_private;
346}
347
348WL_EXPORT void
349weston_desktop_surface_set_user_data(struct weston_desktop_surface *surface,
350 void *user_data)
351{
352 surface->user_data = user_data;
353}
354
355static struct weston_desktop_view *
356weston_desktop_surface_create_desktop_view(struct weston_desktop_surface *surface)
357{
358 struct wl_client *wl_client=
359 weston_desktop_client_get_client(surface->client);
360 struct weston_desktop_view *view, *child_view;
361 struct weston_view *wview;
362 struct weston_desktop_surface *child;
363
364 wview = weston_view_create(surface->surface);
365 if (wview == NULL) {
366 if (wl_client != NULL)
367 wl_client_post_no_memory(wl_client);
368 return NULL;
369 }
370
371 view = zalloc(sizeof(struct weston_desktop_view));
372 if (view == NULL) {
373 if (wl_client != NULL)
374 wl_client_post_no_memory(wl_client);
375 return NULL;
376 }
377
378 view->view = wview;
379 wl_list_init(&view->children_list);
380 wl_list_init(&view->children_link);
381 wl_list_insert(surface->view_list.prev, &view->link);
382
383 wl_list_for_each(child, &surface->children_list, children_link) {
384 child_view =
385 weston_desktop_surface_create_desktop_view(child);
386 if (child_view == NULL) {
387 weston_desktop_view_destroy(view);
388 return NULL;
389 }
390
391 child_view->parent = view;
392 wl_list_insert(view->children_list.prev,
393 &child_view->children_link);
394 }
395
396 return view;
397}
398
399WL_EXPORT struct weston_view *
400weston_desktop_surface_create_view(struct weston_desktop_surface *surface)
401{
402 struct weston_desktop_view *view;
403
404 view = weston_desktop_surface_create_desktop_view(surface);
405 if (view == NULL)
406 return NULL;
407
408 return view->view;
409}
410
411WL_EXPORT void
Quentin Glidicf01ecee2016-08-16 10:52:46 +0200412weston_desktop_surface_unlink_view(struct weston_view *wview)
Quentin Glidic248dd102016-08-12 10:41:34 +0200413{
414 struct weston_desktop_surface *surface;
415 struct weston_desktop_view *view;
416
417 if (!weston_surface_is_desktop_surface(wview->surface))
418 return;
419
420 surface = weston_surface_get_desktop_surface(wview->surface);
421 wl_list_for_each(view, &surface->view_list, link) {
422 if (view->view == wview) {
423 weston_desktop_view_destroy(view);
424 return;
425 }
426 }
427}
428
429static void
430weston_desktop_view_propagate_layer(struct weston_desktop_view *view)
431{
432 struct weston_desktop_view *child;
433 struct wl_list *link = &view->view->layer_link.link;
434
435 wl_list_for_each_reverse(child, &view->children_list, children_link) {
436 struct weston_layer_entry *prev =
437 wl_container_of(link->prev, prev, link);
438
439 if (prev == &child->view->layer_link)
440 continue;
441
442 child->view->is_mapped = true;
443 weston_view_damage_below(child->view);
444 weston_view_geometry_dirty(child->view);
445 weston_layer_entry_remove(&child->view->layer_link);
446 weston_layer_entry_insert(prev, &child->view->layer_link);
447 weston_view_geometry_dirty(child->view);
448 weston_surface_damage(child->view->surface);
449 weston_view_update_transform(child->view);
450
451 weston_desktop_view_propagate_layer(child);
452 }
453}
454
455WL_EXPORT void
456weston_desktop_surface_propagate_layer(struct weston_desktop_surface *surface)
457{
458 struct weston_desktop_view *view;
459
460 wl_list_for_each(view, &surface->view_list, link)
461 weston_desktop_view_propagate_layer(view);
462}
463
464WL_EXPORT void
465weston_desktop_surface_set_activated(struct weston_desktop_surface *surface, bool activated)
466{
467 if (surface->implementation->set_activated != NULL)
468 surface->implementation->set_activated(surface,
469 surface->implementation_data,
470 activated);
471}
472
473WL_EXPORT void
474weston_desktop_surface_set_fullscreen(struct weston_desktop_surface *surface, bool fullscreen)
475{
476 if (surface->implementation->set_fullscreen != NULL)
477 surface->implementation->set_fullscreen(surface,
478 surface->implementation_data,
479 fullscreen);
480}
481
482WL_EXPORT void
483weston_desktop_surface_set_maximized(struct weston_desktop_surface *surface, bool maximized)
484{
485 if (surface->implementation->set_maximized != NULL)
486 surface->implementation->set_maximized(surface,
487 surface->implementation_data,
488 maximized);
489}
490
491WL_EXPORT void
492weston_desktop_surface_set_resizing(struct weston_desktop_surface *surface, bool resizing)
493{
494 if (surface->implementation->set_resizing != NULL)
495 surface->implementation->set_resizing(surface,
496 surface->implementation_data,
497 resizing);
498}
499
500WL_EXPORT void
501weston_desktop_surface_set_size(struct weston_desktop_surface *surface, int32_t width, int32_t height)
502{
503 if (surface->implementation->set_size != NULL)
504 surface->implementation->set_size(surface,
505 surface->implementation_data,
506 width, height);
507}
508
509WL_EXPORT void
510weston_desktop_surface_close(struct weston_desktop_surface *surface)
511{
512 if (surface->implementation->close != NULL)
513 surface->implementation->close(surface,
514 surface->implementation_data);
515}
516
Matt Hoosier555c5482017-09-05 08:05:49 -0500517WL_EXPORT void
518weston_desktop_surface_add_metadata_listener(struct weston_desktop_surface *surface,
519 struct wl_listener *listener)
520{
521 wl_signal_add(&surface->metadata_signal, listener);
522}
523
Quentin Glidic248dd102016-08-12 10:41:34 +0200524struct weston_desktop_surface *
525weston_desktop_surface_from_client_link(struct wl_list *link)
526{
527 struct weston_desktop_surface *surface;
528
529 surface = wl_container_of(link, surface, client_link);
530 return surface;
531}
532
533struct wl_list *
534weston_desktop_surface_get_client_link(struct weston_desktop_surface *surface)
535{
536 return &surface->client_link;
537}
538
539bool
540weston_desktop_surface_has_implementation(struct weston_desktop_surface *surface,
541 const struct weston_desktop_surface_implementation *implementation)
542{
543 return surface->implementation == implementation;
544}
545
546const struct weston_desktop_surface_implementation *
547weston_desktop_surface_get_implementation(struct weston_desktop_surface *surface)
548{
549 return surface->implementation;
550}
551
552void *
553weston_desktop_surface_get_implementation_data(struct weston_desktop_surface *surface)
554{
555 return surface->implementation_data;
556}
557
558struct weston_desktop_surface *
559weston_desktop_surface_get_parent(struct weston_desktop_surface *surface)
560{
561 return surface->parent;
562}
563
564bool
565weston_desktop_surface_get_grab(struct weston_desktop_surface *surface)
566{
567 return !wl_list_empty(&surface->grab_link);
568}
569
570WL_EXPORT struct weston_desktop_client *
571weston_desktop_surface_get_client(struct weston_desktop_surface *surface)
572{
573 return surface->client;
574}
575
576WL_EXPORT void *
577weston_desktop_surface_get_user_data(struct weston_desktop_surface *surface)
578{
579 return surface->user_data;
580}
581
582WL_EXPORT struct weston_surface *
583weston_desktop_surface_get_surface(struct weston_desktop_surface *surface)
584{
585 return surface->surface;
586}
587
588WL_EXPORT const char *
589weston_desktop_surface_get_title(struct weston_desktop_surface *surface)
590{
591 return surface->title;
592}
593
594WL_EXPORT const char *
595weston_desktop_surface_get_app_id(struct weston_desktop_surface *surface)
596{
597 return surface->app_id;
598}
599
600WL_EXPORT pid_t
601weston_desktop_surface_get_pid(struct weston_desktop_surface *surface)
602{
603 pid_t pid;
604
Giulio Camuffof15320f2016-12-08 09:21:08 +0100605 if (surface->pid != -1) {
Quentin Glidic248dd102016-08-12 10:41:34 +0200606 pid = surface->pid;
607 } else {
608 struct weston_desktop_client *client =
609 weston_desktop_surface_get_client(surface);
610 struct wl_client *wl_client =
611 weston_desktop_client_get_client(client);
612
Giulio Camuffof15320f2016-12-08 09:21:08 +0100613 /* wl_client should always be valid, because only in the
614 * xwayland case it wouldn't be, but in that case we won't
615 * reach here, as the pid is initialized to 0. */
616 assert(wl_client);
Quentin Glidic248dd102016-08-12 10:41:34 +0200617 wl_client_get_credentials(wl_client, &pid, NULL, NULL);
618 }
619 return pid;
620}
621
622WL_EXPORT bool
623weston_desktop_surface_get_activated(struct weston_desktop_surface *surface)
624{
625 if (surface->implementation->get_activated == NULL)
626 return false;
627 return surface->implementation->get_activated(surface,
628 surface->implementation_data);
629}
630
631WL_EXPORT bool
632weston_desktop_surface_get_resizing(struct weston_desktop_surface *surface)
633{
634 if (surface->implementation->get_resizing == NULL)
635 return false;
636 return surface->implementation->get_resizing(surface,
637 surface->implementation_data);
638}
639
640WL_EXPORT bool
641weston_desktop_surface_get_maximized(struct weston_desktop_surface *surface)
642{
643 if (surface->implementation->get_maximized == NULL)
644 return false;
645 return surface->implementation->get_maximized(surface,
646 surface->implementation_data);
647}
648
649WL_EXPORT bool
650weston_desktop_surface_get_fullscreen(struct weston_desktop_surface *surface)
651{
652 if (surface->implementation->get_fullscreen == NULL)
653 return false;
654 return surface->implementation->get_fullscreen(surface,
655 surface->implementation_data);
656}
657
658WL_EXPORT struct weston_geometry
659weston_desktop_surface_get_geometry(struct weston_desktop_surface *surface)
660{
661 if (surface->has_geometry)
662 return surface->geometry;
663 return weston_surface_get_bounding_box(surface->surface);
664}
665
666WL_EXPORT struct weston_size
667weston_desktop_surface_get_max_size(struct weston_desktop_surface *surface)
668{
669 struct weston_size size = { 0, 0 };
670
671 if (surface->implementation->get_max_size == NULL)
672 return size;
673 return surface->implementation->get_max_size(surface,
674 surface->implementation_data);
675}
676
677WL_EXPORT struct weston_size
678weston_desktop_surface_get_min_size(struct weston_desktop_surface *surface)
679{
680 struct weston_size size = { 0, 0 };
681
682 if (surface->implementation->get_min_size == NULL)
683 return size;
684 return surface->implementation->get_min_size(surface,
685 surface->implementation_data);
686}
687
688void
689weston_desktop_surface_set_title(struct weston_desktop_surface *surface,
690 const char *title)
691{
Matt Hoosier555c5482017-09-05 08:05:49 -0500692 char *tmp, *old;
Quentin Glidic248dd102016-08-12 10:41:34 +0200693
694 tmp = strdup(title);
695 if (tmp == NULL)
696 return;
697
Matt Hoosier555c5482017-09-05 08:05:49 -0500698 old = surface->title;
Quentin Glidic248dd102016-08-12 10:41:34 +0200699 surface->title = tmp;
Matt Hoosier555c5482017-09-05 08:05:49 -0500700 wl_signal_emit(&surface->metadata_signal, surface);
701 free(old);
Quentin Glidic248dd102016-08-12 10:41:34 +0200702}
703
704void
705weston_desktop_surface_set_app_id(struct weston_desktop_surface *surface,
706 const char *app_id)
707{
Matt Hoosier555c5482017-09-05 08:05:49 -0500708 char *tmp, *old;
Quentin Glidic248dd102016-08-12 10:41:34 +0200709
710 tmp = strdup(app_id);
711 if (tmp == NULL)
712 return;
713
Matt Hoosier555c5482017-09-05 08:05:49 -0500714 old = surface->app_id;
Quentin Glidic248dd102016-08-12 10:41:34 +0200715 surface->app_id = tmp;
Matt Hoosier555c5482017-09-05 08:05:49 -0500716 wl_signal_emit(&surface->metadata_signal, surface);
717 free(old);
Quentin Glidic248dd102016-08-12 10:41:34 +0200718}
719
720void
721weston_desktop_surface_set_pid(struct weston_desktop_surface *surface,
722 pid_t pid)
723{
724 surface->pid = pid;
725}
726
727void
728weston_desktop_surface_set_geometry(struct weston_desktop_surface *surface,
729 struct weston_geometry geometry)
730{
731 surface->has_geometry = true;
732 surface->geometry = geometry;
733}
734
735void
736weston_desktop_surface_set_relative_to(struct weston_desktop_surface *surface,
737 struct weston_desktop_surface *parent,
738 int32_t x, int32_t y, bool use_geometry)
739{
740 struct weston_desktop_view *view, *parent_view;
741 struct wl_list *link, *tmp;
742
743 assert(parent);
744
745 surface->position.x = x;
746 surface->position.y = y;
747 surface->use_geometry = use_geometry;
748
749 if (surface->parent == parent)
750 return;
751
752 surface->parent = parent;
753 wl_list_remove(&surface->children_link);
754 wl_list_insert(surface->parent->children_list.prev,
755 &surface->children_link);
756
757 link = surface->view_list.next;
758 tmp = link->next;
759 wl_list_for_each(parent_view, &parent->view_list, link) {
760 if (link == &surface->view_list) {
761 view = weston_desktop_surface_create_desktop_view(surface);
762 if (view == NULL)
763 return;
764 tmp = &surface->view_list;
765 } else {
766 view = wl_container_of(link, view, link);
767 wl_list_remove(&view->children_link);
768 }
769
770 view->parent = parent_view;
771 wl_list_insert(parent_view->children_list.prev,
772 &view->children_link);
773 weston_desktop_view_propagate_layer(view);
774
775 link = tmp;
776 tmp = link->next;
777 }
778 for (; link != &surface->view_list; link = tmp, tmp = link->next) {
779 view = wl_container_of(link, view, link);
780 weston_desktop_view_destroy(view);
781 }
782}
783
784void
785weston_desktop_surface_unset_relative_to(struct weston_desktop_surface *surface)
786{
787 struct weston_desktop_view *view, *tmp;
788
789 if (surface->parent == NULL)
790 return;
791
792 surface->parent = NULL;
793 wl_list_remove(&surface->children_link);
794 wl_list_init(&surface->children_link);
795
796 wl_list_for_each_safe(view, tmp, &surface->view_list, link)
797 weston_desktop_view_destroy(view);
798}
799
800void
801weston_desktop_surface_popup_grab(struct weston_desktop_surface *surface,
802 struct weston_desktop_seat *seat,
803 uint32_t serial)
804{
805 struct wl_client *wl_client =
806 weston_desktop_client_get_client(surface->client);
807 if (weston_desktop_seat_popup_grab_start(seat, wl_client, serial))
808 weston_desktop_seat_popup_grab_add_surface(seat, &surface->grab_link);
809 else
810 weston_desktop_surface_popup_dismiss(surface);
811}
812
813void
814weston_desktop_surface_popup_ungrab(struct weston_desktop_surface *surface,
815 struct weston_desktop_seat *seat)
816{
817 weston_desktop_seat_popup_grab_remove_surface(seat, &surface->grab_link);
818}
819
820void
821weston_desktop_surface_popup_dismiss(struct weston_desktop_surface *surface)
822{
823 struct weston_desktop_view *view, *tmp;
824
825 wl_list_for_each_safe(view, tmp, &surface->view_list, link)
826 weston_desktop_view_destroy(view);
827 wl_list_remove(&surface->grab_link);
828 wl_list_init(&surface->grab_link);
829 weston_desktop_surface_close(surface);
830}