aboutsummaryrefslogtreecommitdiffstats
path: root/sway/tree/view.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/tree/view.c')
-rw-r--r--sway/tree/view.c813
1 files changed, 357 insertions, 456 deletions
diff --git a/sway/tree/view.c b/sway/tree/view.c
index 7afcdf31..35b4b73f 100644
--- a/sway/tree/view.c
+++ b/sway/tree/view.c
@@ -1,11 +1,13 @@
1#define _POSIX_C_SOURCE 200809L
2#include <stdlib.h> 1#include <stdlib.h>
3#include <strings.h> 2#include <strings.h>
4#include <wayland-server-core.h> 3#include <wayland-server-core.h>
5#include <wlr/render/wlr_renderer.h> 4#include <wlr/render/wlr_renderer.h>
6#include <wlr/types/wlr_buffer.h> 5#include <wlr/types/wlr_buffer.h>
6#include <wlr/types/wlr_ext_foreign_toplevel_list_v1.h>
7#include <wlr/types/wlr_foreign_toplevel_management_v1.h>
7#include <wlr/types/wlr_output_layout.h> 8#include <wlr/types/wlr_output_layout.h>
8#include <wlr/types/wlr_server_decoration.h> 9#include <wlr/types/wlr_server_decoration.h>
10#include <wlr/types/wlr_subcompositor.h>
9#include <wlr/types/wlr_xdg_decoration_v1.h> 11#include <wlr/types/wlr_xdg_decoration_v1.h>
10#include "config.h" 12#include "config.h"
11#if HAVE_XWAYLAND 13#if HAVE_XWAYLAND
@@ -15,14 +17,16 @@
15#include "log.h" 17#include "log.h"
16#include "sway/criteria.h" 18#include "sway/criteria.h"
17#include "sway/commands.h" 19#include "sway/commands.h"
18#include "sway/desktop.h"
19#include "sway/desktop/transaction.h" 20#include "sway/desktop/transaction.h"
20#include "sway/desktop/idle_inhibit_v1.h" 21#include "sway/desktop/idle_inhibit_v1.h"
22#include "sway/desktop/launcher.h"
21#include "sway/input/cursor.h" 23#include "sway/input/cursor.h"
22#include "sway/ipc-server.h" 24#include "sway/ipc-server.h"
23#include "sway/output.h" 25#include "sway/output.h"
24#include "sway/input/seat.h" 26#include "sway/input/seat.h"
27#include "sway/scene_descriptor.h"
25#include "sway/server.h" 28#include "sway/server.h"
29#include "sway/sway_text_node.h"
26#include "sway/tree/arrange.h" 30#include "sway/tree/arrange.h"
27#include "sway/tree/container.h" 31#include "sway/tree/container.h"
28#include "sway/tree/view.h" 32#include "sway/tree/view.h"
@@ -32,15 +36,29 @@
32#include "pango.h" 36#include "pango.h"
33#include "stringop.h" 37#include "stringop.h"
34 38
35void view_init(struct sway_view *view, enum sway_view_type type, 39bool view_init(struct sway_view *view, enum sway_view_type type,
36 const struct sway_view_impl *impl) { 40 const struct sway_view_impl *impl) {
41 bool failed = false;
42 view->scene_tree = alloc_scene_tree(root->staging, &failed);
43 view->content_tree = alloc_scene_tree(view->scene_tree, &failed);
44
45 if (!failed && !scene_descriptor_assign(&view->scene_tree->node,
46 SWAY_SCENE_DESC_VIEW, view)) {
47 failed = true;
48 }
49
50 if (failed) {
51 wlr_scene_node_destroy(&view->scene_tree->node);
52 return false;
53 }
54
37 view->type = type; 55 view->type = type;
38 view->impl = impl; 56 view->impl = impl;
39 view->executed_criteria = create_list(); 57 view->executed_criteria = create_list();
40 wl_list_init(&view->saved_buffers);
41 view->allow_request_urgent = true; 58 view->allow_request_urgent = true;
42 view->shortcuts_inhibit = SHORTCUTS_INHIBIT_DEFAULT; 59 view->shortcuts_inhibit = SHORTCUTS_INHIBIT_DEFAULT;
43 wl_signal_init(&view->events.unmap); 60 wl_signal_init(&view->events.unmap);
61 return true;
44} 62}
45 63
46void view_destroy(struct sway_view *view) { 64void view_destroy(struct sway_view *view) {
@@ -56,11 +74,11 @@ void view_destroy(struct sway_view *view) {
56 "(might have a pending transaction?)")) { 74 "(might have a pending transaction?)")) {
57 return; 75 return;
58 } 76 }
59 if (!wl_list_empty(&view->saved_buffers)) { 77 wl_list_remove(&view->events.unmap.listener_list);
60 view_remove_saved_buffer(view);
61 }
62 list_free(view->executed_criteria); 78 list_free(view->executed_criteria);
63 79
80 view_assign_ctx(view, NULL);
81 wlr_scene_node_destroy(&view->scene_tree->node);
64 free(view->title_format); 82 free(view->title_format);
65 83
66 if (view->impl->destroy) { 84 if (view->impl->destroy) {
@@ -206,7 +224,7 @@ bool view_ancestor_is_only_visible(struct sway_view *view) {
206 } else { 224 } else {
207 only_visible = true; 225 only_visible = true;
208 } 226 }
209 con = con->parent; 227 con = con->pending.parent;
210 } 228 }
211 return only_visible; 229 return only_visible;
212} 230}
@@ -222,72 +240,73 @@ static bool view_is_only_visible(struct sway_view *view) {
222 } 240 }
223 } 241 }
224 242
225 con = con->parent; 243 con = con->pending.parent;
226 } 244 }
227 245
228 return true; 246 return true;
229} 247}
230 248
231static bool gaps_to_edge(struct sway_view *view) { 249static bool gaps_to_edge(struct sway_view *view) {
232 struct side_gaps gaps = view->container->workspace->current_gaps; 250 struct side_gaps gaps = view->container->pending.workspace->current_gaps;
233 return gaps.top > 0 || gaps.right > 0 || gaps.bottom > 0 || gaps.left > 0; 251 return gaps.top > 0 || gaps.right > 0 || gaps.bottom > 0 || gaps.left > 0;
234} 252}
235 253
236void view_autoconfigure(struct sway_view *view) { 254void view_autoconfigure(struct sway_view *view) {
237 struct sway_container *con = view->container; 255 struct sway_container *con = view->container;
238 struct sway_workspace *ws = con->workspace; 256 struct sway_workspace *ws = con->pending.workspace;
239 257
240 if (container_is_scratchpad_hidden(con) && 258 if (container_is_scratchpad_hidden(con) &&
241 con->fullscreen_mode != FULLSCREEN_GLOBAL) { 259 con->pending.fullscreen_mode != FULLSCREEN_GLOBAL) {
242 return; 260 return;
243 } 261 }
244 struct sway_output *output = ws ? ws->output : NULL; 262 struct sway_output *output = ws ? ws->output : NULL;
245 263
246 if (con->fullscreen_mode == FULLSCREEN_WORKSPACE) { 264 if (con->pending.fullscreen_mode == FULLSCREEN_WORKSPACE) {
247 con->content_x = output->lx; 265 con->pending.content_x = output->lx;
248 con->content_y = output->ly; 266 con->pending.content_y = output->ly;
249 con->content_width = output->width; 267 con->pending.content_width = output->width;
250 con->content_height = output->height; 268 con->pending.content_height = output->height;
251 return; 269 return;
252 } else if (con->fullscreen_mode == FULLSCREEN_GLOBAL) { 270 } else if (con->pending.fullscreen_mode == FULLSCREEN_GLOBAL) {
253 con->content_x = root->x; 271 con->pending.content_x = root->x;
254 con->content_y = root->y; 272 con->pending.content_y = root->y;
255 con->content_width = root->width; 273 con->pending.content_width = root->width;
256 con->content_height = root->height; 274 con->pending.content_height = root->height;
257 return; 275 return;
258 } 276 }
259 277
260 con->border_top = con->border_bottom = true; 278 con->pending.border_top = con->pending.border_bottom = true;
261 con->border_left = con->border_right = true; 279 con->pending.border_left = con->pending.border_right = true;
262 double y_offset = 0; 280 double y_offset = 0;
263 281
264 if (!container_is_floating(con) && ws) { 282 if (!container_is_floating_or_child(con) && ws) {
265 if (config->hide_edge_borders == E_BOTH 283 if (config->hide_edge_borders == E_BOTH
266 || config->hide_edge_borders == E_VERTICAL) { 284 || config->hide_edge_borders == E_VERTICAL) {
267 con->border_left = con->x != ws->x; 285 con->pending.border_left = con->pending.x != ws->x;
268 int right_x = con->x + con->width; 286 int right_x = con->pending.x + con->pending.width;
269 con->border_right = right_x != ws->x + ws->width; 287 con->pending.border_right = right_x != ws->x + ws->width;
270 } 288 }
271 289
272 if (config->hide_edge_borders == E_BOTH 290 if (config->hide_edge_borders == E_BOTH
273 || config->hide_edge_borders == E_HORIZONTAL) { 291 || config->hide_edge_borders == E_HORIZONTAL) {
274 con->border_top = con->y != ws->y; 292 con->pending.border_top = con->pending.y != ws->y;
275 int bottom_y = con->y + con->height; 293 int bottom_y = con->pending.y + con->pending.height;
276 con->border_bottom = bottom_y != ws->y + ws->height; 294 con->pending.border_bottom = bottom_y != ws->y + ws->height;
277 } 295 }
278 296
279 bool smart = config->hide_edge_borders_smart == ESMART_ON || 297 bool smart = config->hide_edge_borders_smart == ESMART_ON ||
280 (config->hide_edge_borders_smart == ESMART_NO_GAPS && 298 (config->hide_edge_borders_smart == ESMART_NO_GAPS &&
281 !gaps_to_edge(view)); 299 !gaps_to_edge(view));
282 if (smart) { 300 if (smart) {
283 bool show_border = container_is_floating_or_child(con) || 301 bool show_border = !view_is_only_visible(view);
284 !view_is_only_visible(view); 302 con->pending.border_left &= show_border;
285 con->border_left &= show_border; 303 con->pending.border_right &= show_border;
286 con->border_right &= show_border; 304 con->pending.border_top &= show_border;
287 con->border_top &= show_border; 305 con->pending.border_bottom &= show_border;
288 con->border_bottom &= show_border;
289 } 306 }
307 }
290 308
309 if (!container_is_floating(con)) {
291 // In a tabbed or stacked container, the container's y is the top of the 310 // In a tabbed or stacked container, the container's y is the top of the
292 // title area. We have to offset the surface y by the height of the title, 311 // title area. We have to offset the surface y by the height of the title,
293 // bar, and disable any top border because we'll always have the title bar. 312 // bar, and disable any top border because we'll always have the title bar.
@@ -298,56 +317,56 @@ void view_autoconfigure(struct sway_view *view) {
298 enum sway_container_layout layout = container_parent_layout(con); 317 enum sway_container_layout layout = container_parent_layout(con);
299 if (layout == L_TABBED) { 318 if (layout == L_TABBED) {
300 y_offset = container_titlebar_height(); 319 y_offset = container_titlebar_height();
301 con->border_top = false; 320 con->pending.border_top = false;
302 } else if (layout == L_STACKED) { 321 } else if (layout == L_STACKED) {
303 y_offset = container_titlebar_height() * siblings->length; 322 y_offset = container_titlebar_height() * siblings->length;
304 con->border_top = false; 323 con->pending.border_top = false;
305 } 324 }
306 } 325 }
307 } 326 }
308 327
309 double x, y, width, height; 328 double x, y, width, height;
310 switch (con->border) { 329 switch (con->pending.border) {
311 default: 330 default:
312 case B_CSD: 331 case B_CSD:
313 case B_NONE: 332 case B_NONE:
314 x = con->x; 333 x = con->pending.x;
315 y = con->y + y_offset; 334 y = con->pending.y + y_offset;
316 width = con->width; 335 width = con->pending.width;
317 height = con->height - y_offset; 336 height = con->pending.height - y_offset;
318 break; 337 break;
319 case B_PIXEL: 338 case B_PIXEL:
320 x = con->x + con->border_thickness * con->border_left; 339 x = con->pending.x + con->pending.border_thickness * con->pending.border_left;
321 y = con->y + con->border_thickness * con->border_top + y_offset; 340 y = con->pending.y + con->pending.border_thickness * con->pending.border_top + y_offset;
322 width = con->width 341 width = con->pending.width
323 - con->border_thickness * con->border_left 342 - con->pending.border_thickness * con->pending.border_left
324 - con->border_thickness * con->border_right; 343 - con->pending.border_thickness * con->pending.border_right;
325 height = con->height - y_offset 344 height = con->pending.height - y_offset
326 - con->border_thickness * con->border_top 345 - con->pending.border_thickness * con->pending.border_top
327 - con->border_thickness * con->border_bottom; 346 - con->pending.border_thickness * con->pending.border_bottom;
328 break; 347 break;
329 case B_NORMAL: 348 case B_NORMAL:
330 // Height is: 1px border + 3px pad + title height + 3px pad + 1px border 349 // Height is: 1px border + 3px pad + title height + 3px pad + 1px border
331 x = con->x + con->border_thickness * con->border_left; 350 x = con->pending.x + con->pending.border_thickness * con->pending.border_left;
332 width = con->width 351 width = con->pending.width
333 - con->border_thickness * con->border_left 352 - con->pending.border_thickness * con->pending.border_left
334 - con->border_thickness * con->border_right; 353 - con->pending.border_thickness * con->pending.border_right;
335 if (y_offset) { 354 if (y_offset) {
336 y = con->y + y_offset; 355 y = con->pending.y + y_offset;
337 height = con->height - y_offset 356 height = con->pending.height - y_offset
338 - con->border_thickness * con->border_bottom; 357 - con->pending.border_thickness * con->pending.border_bottom;
339 } else { 358 } else {
340 y = con->y + container_titlebar_height(); 359 y = con->pending.y + container_titlebar_height();
341 height = con->height - container_titlebar_height() 360 height = con->pending.height - container_titlebar_height()
342 - con->border_thickness * con->border_bottom; 361 - con->pending.border_thickness * con->pending.border_bottom;
343 } 362 }
344 break; 363 break;
345 } 364 }
346 365
347 con->content_x = x; 366 con->pending.content_x = x;
348 con->content_y = y; 367 con->pending.content_y = y;
349 con->content_width = width; 368 con->pending.content_width = width;
350 con->content_height = height; 369 con->pending.content_height = height;
351} 370}
352 371
353void view_set_activated(struct sway_view *view, bool activated) { 372void view_set_activated(struct sway_view *view, bool activated) {
@@ -360,17 +379,17 @@ void view_set_activated(struct sway_view *view, bool activated) {
360 } 379 }
361} 380}
362 381
363void view_request_activate(struct sway_view *view) { 382void view_request_activate(struct sway_view *view, struct sway_seat *seat) {
364 struct sway_workspace *ws = view->container->workspace; 383 struct sway_workspace *ws = view->container->pending.workspace;
365 if (!ws) { // hidden scratchpad container 384 if (!seat) {
366 return; 385 seat = input_manager_current_seat();
367 } 386 }
368 struct sway_seat *seat = input_manager_current_seat();
369 387
370 switch (config->focus_on_window_activation) { 388 switch (config->focus_on_window_activation) {
371 case FOWA_SMART: 389 case FOWA_SMART:
372 if (workspace_is_visible(ws)) { 390 if (ws && workspace_is_visible(ws)) {
373 seat_set_focus_container(seat, view->container); 391 seat_set_focus_container(seat, view->container);
392 container_raise_floating(view->container);
374 } else { 393 } else {
375 view_set_urgent(view, true); 394 view_set_urgent(view, true);
376 } 395 }
@@ -379,11 +398,23 @@ void view_request_activate(struct sway_view *view) {
379 view_set_urgent(view, true); 398 view_set_urgent(view, true);
380 break; 399 break;
381 case FOWA_FOCUS: 400 case FOWA_FOCUS:
382 seat_set_focus_container(seat, view->container); 401 if (container_is_scratchpad_hidden_or_child(view->container)) {
402 root_scratchpad_show(view->container);
403 } else {
404 seat_set_focus_container(seat, view->container);
405 container_raise_floating(view->container);
406 }
383 break; 407 break;
384 case FOWA_NONE: 408 case FOWA_NONE:
385 break; 409 break;
386 } 410 }
411 transaction_commit_dirty();
412}
413
414void view_request_urgent(struct sway_view *view) {
415 if (config->focus_on_window_activation != FOWA_NONE) {
416 view_set_urgent(view, true);
417 }
387} 418}
388 419
389void view_set_csd_from_server(struct sway_view *view, bool enabled) { 420void view_set_csd_from_server(struct sway_view *view, bool enabled) {
@@ -401,13 +432,13 @@ void view_set_csd_from_server(struct sway_view *view, bool enabled) {
401void view_update_csd_from_client(struct sway_view *view, bool enabled) { 432void view_update_csd_from_client(struct sway_view *view, bool enabled) {
402 sway_log(SWAY_DEBUG, "View %p updated CSD to %i", view, enabled); 433 sway_log(SWAY_DEBUG, "View %p updated CSD to %i", view, enabled);
403 struct sway_container *con = view->container; 434 struct sway_container *con = view->container;
404 if (enabled && con && con->border != B_CSD) { 435 if (enabled && con && con->pending.border != B_CSD) {
405 con->saved_border = con->border; 436 con->saved_border = con->pending.border;
406 if (container_is_floating(con)) { 437 if (container_is_floating(con)) {
407 con->border = B_CSD; 438 con->pending.border = B_CSD;
408 } 439 }
409 } else if (!enabled && con && con->border == B_CSD) { 440 } else if (!enabled && con && con->pending.border == B_CSD) {
410 con->border = con->saved_border; 441 con->pending.border = con->saved_border;
411 } 442 }
412 view->using_csd = enabled; 443 view->using_csd = enabled;
413} 444}
@@ -430,49 +461,6 @@ void view_close_popups(struct sway_view *view) {
430 } 461 }
431} 462}
432 463
433void view_damage_from(struct sway_view *view) {
434 for (int i = 0; i < root->outputs->length; ++i) {
435 struct sway_output *output = root->outputs->items[i];
436 output_damage_from_view(output, view);
437 }
438}
439
440void view_for_each_surface(struct sway_view *view,
441 wlr_surface_iterator_func_t iterator, void *user_data) {
442 if (!view->surface) {
443 return;
444 }
445 if (view->impl->for_each_surface) {
446 view->impl->for_each_surface(view, iterator, user_data);
447 } else {
448 wlr_surface_for_each_surface(view->surface, iterator, user_data);
449 }
450}
451
452void view_for_each_popup_surface(struct sway_view *view,
453 wlr_surface_iterator_func_t iterator, void *user_data) {
454 if (!view->surface) {
455 return;
456 }
457 if (view->impl->for_each_popup_surface) {
458 view->impl->for_each_popup_surface(view, iterator, user_data);
459 }
460}
461
462static void view_subsurface_create(struct sway_view *view,
463 struct wlr_subsurface *subsurface);
464
465static void view_init_subsurfaces(struct sway_view *view,
466 struct wlr_surface *surface);
467
468static void view_handle_surface_new_subsurface(struct wl_listener *listener,
469 void *data) {
470 struct sway_view *view =
471 wl_container_of(listener, view, surface_new_subsurface);
472 struct wlr_subsurface *subsurface = data;
473 view_subsurface_create(view, subsurface);
474}
475
476static bool view_has_executed_criteria(struct sway_view *view, 464static bool view_has_executed_criteria(struct sway_view *view,
477 struct criteria *criteria) { 465 struct criteria *criteria) {
478 for (int i = 0; i < view->executed_criteria->length; ++i) { 466 for (int i = 0; i < view->executed_criteria->length; ++i) {
@@ -514,7 +502,7 @@ static void view_populate_pid(struct sway_view *view) {
514#if HAVE_XWAYLAND 502#if HAVE_XWAYLAND
515 case SWAY_VIEW_XWAYLAND:; 503 case SWAY_VIEW_XWAYLAND:;
516 struct wlr_xwayland_surface *surf = 504 struct wlr_xwayland_surface *surf =
517 wlr_xwayland_surface_from_wlr_surface(view->surface); 505 wlr_xwayland_surface_try_from_wlr_surface(view->surface);
518 pid = surf->pid; 506 pid = surf->pid;
519 break; 507 break;
520#endif 508#endif
@@ -527,6 +515,20 @@ static void view_populate_pid(struct sway_view *view) {
527 view->pid = pid; 515 view->pid = pid;
528} 516}
529 517
518void view_assign_ctx(struct sway_view *view, struct launcher_ctx *ctx) {
519 if (view->ctx) {
520 // This ctx has been replaced
521 launcher_ctx_destroy(view->ctx);
522 view->ctx = NULL;
523 }
524 if (ctx == NULL) {
525 return;
526 }
527 launcher_ctx_consume(ctx);
528
529 view->ctx = ctx;
530}
531
530static struct sway_workspace *select_workspace(struct sway_view *view) { 532static struct sway_workspace *select_workspace(struct sway_view *view) {
531 struct sway_seat *seat = input_manager_current_seat(); 533 struct sway_seat *seat = input_manager_current_seat();
532 534
@@ -562,13 +564,14 @@ static struct sway_workspace *select_workspace(struct sway_view *view) {
562 } 564 }
563 list_free(criterias); 565 list_free(criterias);
564 if (ws) { 566 if (ws) {
565 root_remove_workspace_pid(view->pid); 567 view_assign_ctx(view, NULL);
566 return ws; 568 return ws;
567 } 569 }
568 570
569 // Check if there's a PID mapping 571 // Check if there's a PID mapping
570 ws = root_workspace_for_pid(view->pid); 572 ws = view->ctx ? launcher_ctx_get_workspace(view->ctx) : NULL;
571 if (ws) { 573 if (ws) {
574 view_assign_ctx(view, NULL);
572 return ws; 575 return ws;
573 } 576 }
574 577
@@ -577,7 +580,7 @@ static struct sway_workspace *select_workspace(struct sway_view *view) {
577 if (node && node->type == N_WORKSPACE) { 580 if (node && node->type == N_WORKSPACE) {
578 return node->sway_workspace; 581 return node->sway_workspace;
579 } else if (node && node->type == N_CONTAINER) { 582 } else if (node && node->type == N_CONTAINER) {
580 return node->sway_container->workspace; 583 return node->sway_container->pending.workspace;
581 } 584 }
582 585
583 // When there's no outputs connected, the above should match a workspace on 586 // When there's no outputs connected, the above should match a workspace on
@@ -586,16 +589,29 @@ static struct sway_workspace *select_workspace(struct sway_view *view) {
586 return NULL; 589 return NULL;
587} 590}
588 591
592static void update_ext_foreign_toplevel(struct sway_view *view) {
593 struct wlr_ext_foreign_toplevel_handle_v1_state toplevel_state = {
594 .app_id = view_get_app_id(view),
595 .title = view_get_title(view),
596 };
597 wlr_ext_foreign_toplevel_handle_v1_update_state(view->ext_foreign_toplevel, &toplevel_state);
598}
599
589static bool should_focus(struct sway_view *view) { 600static bool should_focus(struct sway_view *view) {
590 struct sway_seat *seat = input_manager_current_seat(); 601 struct sway_seat *seat = input_manager_current_seat();
591 struct sway_container *prev_con = seat_get_focused_container(seat); 602 struct sway_container *prev_con = seat_get_focused_container(seat);
592 struct sway_workspace *prev_ws = seat_get_focused_workspace(seat); 603 struct sway_workspace *prev_ws = seat_get_focused_workspace(seat);
593 struct sway_workspace *map_ws = view->container->workspace; 604 struct sway_workspace *map_ws = view->container->pending.workspace;
594 605
595 if (view->container->fullscreen_mode == FULLSCREEN_GLOBAL) { 606 if (view->container->pending.fullscreen_mode == FULLSCREEN_GLOBAL) {
596 return true; 607 return true;
597 } 608 }
598 609
610 // View opened "under" fullscreen view should not be given focus.
611 if (root->fullscreen_global || !map_ws || map_ws->fullscreen) {
612 return false;
613 }
614
599 // Views can only take focus if they are mapped into the active workspace 615 // Views can only take focus if they are mapped into the active workspace
600 if (prev_ws != map_ws) { 616 if (prev_ws != map_ws) {
601 return false; 617 return false;
@@ -603,9 +619,9 @@ static bool should_focus(struct sway_view *view) {
603 619
604 // If the view is the only one in the focused workspace, it'll get focus 620 // If the view is the only one in the focused workspace, it'll get focus
605 // regardless of any no_focus criteria. 621 // regardless of any no_focus criteria.
606 if (!view->container->parent && !prev_con) { 622 if (!view->container->pending.parent && !prev_con) {
607 size_t num_children = view->container->workspace->tiling->length + 623 size_t num_children = view->container->pending.workspace->tiling->length +
608 view->container->workspace->floating->length; 624 view->container->pending.workspace->floating->length;
609 if (num_children == 1) { 625 if (num_children == 1) {
610 return true; 626 return true;
611 } 627 }
@@ -635,6 +651,7 @@ static void handle_foreign_activate_request(
635 break; 651 break;
636 } 652 }
637 } 653 }
654 transaction_commit_dirty();
638} 655}
639 656
640static void handle_foreign_fullscreen_request( 657static void handle_foreign_fullscreen_request(
@@ -645,9 +662,21 @@ static void handle_foreign_fullscreen_request(
645 662
646 // Match fullscreen command behavior for scratchpad hidden views 663 // Match fullscreen command behavior for scratchpad hidden views
647 struct sway_container *container = view->container; 664 struct sway_container *container = view->container;
648 if (!container->workspace) { 665 if (!container->pending.workspace) {
649 while (container->parent) { 666 while (container->pending.parent) {
650 container = container->parent; 667 container = container->pending.parent;
668 }
669 }
670
671 if (event->fullscreen && event->output && event->output->data) {
672 struct sway_output *output = event->output->data;
673 struct sway_workspace *ws = output_get_active_workspace(output);
674 if (ws && !container_is_scratchpad_hidden(view->container)) {
675 if (container_is_floating(view->container)) {
676 workspace_add_floating(ws, view->container);
677 } else {
678 workspace_add_tiling(ws, view->container);
679 }
651 } 680 }
652 } 681 }
653 682
@@ -656,12 +685,13 @@ static void handle_foreign_fullscreen_request(
656 if (event->fullscreen) { 685 if (event->fullscreen) {
657 arrange_root(); 686 arrange_root();
658 } else { 687 } else {
659 if (container->parent) { 688 if (container->pending.parent) {
660 arrange_container(container->parent); 689 arrange_container(container->pending.parent);
661 } else if (container->workspace) { 690 } else if (container->pending.workspace) {
662 arrange_workspace(container->workspace); 691 arrange_workspace(container->pending.workspace);
663 } 692 }
664 } 693 }
694 transaction_commit_dirty();
665} 695}
666 696
667static void handle_foreign_close_request( 697static void handle_foreign_close_request(
@@ -692,6 +722,13 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface,
692 view_populate_pid(view); 722 view_populate_pid(view);
693 view->container = container_create(view); 723 view->container = container_create(view);
694 724
725 if (view->ctx == NULL) {
726 struct launcher_ctx *ctx = launcher_ctx_find_pid(view->pid);
727 if (ctx != NULL) {
728 view_assign_ctx(view, ctx);
729 }
730 }
731
695 // If there is a request to be opened fullscreen on a specific output, try 732 // If there is a request to be opened fullscreen on a specific output, try
696 // to honor that request. Otherwise, fallback to assigns, pid mappings, 733 // to honor that request. Otherwise, fallback to assigns, pid mappings,
697 // focused workspace, etc 734 // focused workspace, etc
@@ -705,10 +742,36 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface,
705 } 742 }
706 743
707 struct sway_seat *seat = input_manager_current_seat(); 744 struct sway_seat *seat = input_manager_current_seat();
708 struct sway_node *node = ws ? seat_get_focus_inactive(seat, &ws->node) 745 struct sway_node *node =
709 : seat_get_focus_inactive(seat, &root->node); 746 seat_get_focus_inactive(seat, ws ? &ws->node : &root->node);
710 struct sway_container *target_sibling = node->type == N_CONTAINER ? 747 struct sway_container *target_sibling = NULL;
711 node->sway_container : NULL; 748 if (node && node->type == N_CONTAINER) {
749 if (container_is_floating(node->sway_container)) {
750 // If we're about to launch the view into the floating container, then
751 // launch it as a tiled view instead.
752 if (ws) {
753 target_sibling = seat_get_focus_inactive_tiling(seat, ws);
754 if (target_sibling) {
755 struct sway_container *con =
756 seat_get_focus_inactive_view(seat, &target_sibling->node);
757 if (con) {
758 target_sibling = con;
759 }
760 }
761 } else {
762 ws = seat_get_last_known_workspace(seat);
763 }
764 } else {
765 target_sibling = node->sway_container;
766 }
767 }
768
769 struct wlr_ext_foreign_toplevel_handle_v1_state foreign_toplevel_state = {
770 .app_id = view_get_app_id(view),
771 .title = view_get_title(view),
772 };
773 view->ext_foreign_toplevel =
774 wlr_ext_foreign_toplevel_handle_v1_create(server.foreign_toplevel_list, &foreign_toplevel_state);
712 775
713 view->foreign_toplevel = 776 view->foreign_toplevel =
714 wlr_foreign_toplevel_handle_v1_create(server.foreign_toplevel_manager); 777 wlr_foreign_toplevel_handle_v1_create(server.foreign_toplevel_manager);
@@ -725,13 +788,6 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface,
725 wl_signal_add(&view->foreign_toplevel->events.destroy, 788 wl_signal_add(&view->foreign_toplevel->events.destroy,
726 &view->foreign_destroy); 789 &view->foreign_destroy);
727 790
728 // If we're about to launch the view into the floating container, then
729 // launch it as a tiled view in the root of the workspace instead.
730 if (target_sibling && container_is_floating(target_sibling)) {
731 target_sibling = NULL;
732 ws = seat_get_last_known_workspace(seat);
733 }
734
735 struct sway_container *container = view->container; 791 struct sway_container *container = view->container;
736 if (target_sibling) { 792 if (target_sibling) {
737 container_add_sibling(target_sibling, container, 1); 793 container_add_sibling(target_sibling, container, 1);
@@ -740,30 +796,25 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface,
740 } 796 }
741 ipc_event_window(view->container, "new"); 797 ipc_event_window(view->container, "new");
742 798
743 view_init_subsurfaces(view, wlr_surface);
744 wl_signal_add(&wlr_surface->events.new_subsurface,
745 &view->surface_new_subsurface);
746 view->surface_new_subsurface.notify = view_handle_surface_new_subsurface;
747
748 if (decoration) { 799 if (decoration) {
749 view_update_csd_from_client(view, decoration); 800 view_update_csd_from_client(view, decoration);
750 } 801 }
751 802
752 if (view->impl->wants_floating && view->impl->wants_floating(view)) { 803 if (view->impl->wants_floating && view->impl->wants_floating(view)) {
753 view->container->border = config->floating_border; 804 view->container->pending.border = config->floating_border;
754 view->container->border_thickness = config->floating_border_thickness; 805 view->container->pending.border_thickness = config->floating_border_thickness;
755 container_set_floating(view->container, true); 806 container_set_floating(view->container, true);
756 } else { 807 } else {
757 view->container->border = config->border; 808 view->container->pending.border = config->border;
758 view->container->border_thickness = config->border_thickness; 809 view->container->pending.border_thickness = config->border_thickness;
759 view_set_tiled(view, true); 810 view_set_tiled(view, true);
760 } 811 }
761 812
762 if (config->popup_during_fullscreen == POPUP_LEAVE && 813 if (config->popup_during_fullscreen == POPUP_LEAVE &&
763 container->workspace && 814 container->pending.workspace &&
764 container->workspace->fullscreen && 815 container->pending.workspace->fullscreen &&
765 container->workspace->fullscreen->view) { 816 container->pending.workspace->fullscreen->view) {
766 struct sway_container *fs = container->workspace->fullscreen; 817 struct sway_container *fs = container->pending.workspace->fullscreen;
767 if (view_is_transient_for(view, fs->view)) { 818 if (view_is_transient_for(view, fs->view)) {
768 container_set_fullscreen(fs, false); 819 container_set_fullscreen(fs, false);
769 } 820 }
@@ -774,12 +825,12 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface,
774 825
775 if (fullscreen) { 826 if (fullscreen) {
776 container_set_fullscreen(view->container, true); 827 container_set_fullscreen(view->container, true);
777 arrange_workspace(view->container->workspace); 828 arrange_workspace(view->container->pending.workspace);
778 } else { 829 } else {
779 if (container->parent) { 830 if (container->pending.parent) {
780 arrange_container(container->parent); 831 arrange_container(container->pending.parent);
781 } else if (container->workspace) { 832 } else if (container->pending.workspace) {
782 arrange_workspace(container->workspace); 833 arrange_workspace(container->pending.workspace);
783 } 834 }
784 } 835 }
785 836
@@ -788,11 +839,10 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface,
788 bool set_focus = should_focus(view); 839 bool set_focus = should_focus(view);
789 840
790#if HAVE_XWAYLAND 841#if HAVE_XWAYLAND
791 if (wlr_surface_is_xwayland_surface(wlr_surface)) { 842 struct wlr_xwayland_surface *xsurface;
792 struct wlr_xwayland_surface *xsurface = 843 if ((xsurface = wlr_xwayland_surface_try_from_wlr_surface(wlr_surface))) {
793 wlr_xwayland_surface_from_wlr_surface(wlr_surface); 844 set_focus &= wlr_xwayland_icccm_input_model(xsurface) !=
794 set_focus = (wlr_xwayland_icccm_input_model(xsurface) != 845 WLR_ICCCM_INPUT_MODEL_NONE;
795 WLR_ICCCM_INPUT_MODEL_NONE) && set_focus;
796 } 846 }
797#endif 847#endif
798 848
@@ -800,34 +850,41 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface,
800 input_manager_set_focus(&view->container->node); 850 input_manager_set_focus(&view->container->node);
801 } 851 }
802 852
853 if (view->ext_foreign_toplevel) {
854 update_ext_foreign_toplevel(view);
855 }
856
803 const char *app_id; 857 const char *app_id;
804 const char *class; 858 const char *class;
805 if ((app_id = view_get_app_id(view)) != NULL) { 859 if ((app_id = view_get_app_id(view)) != NULL) {
806 wlr_foreign_toplevel_handle_v1_set_app_id( 860 wlr_foreign_toplevel_handle_v1_set_app_id(view->foreign_toplevel, app_id);
807 view->foreign_toplevel, app_id);
808 } else if ((class = view_get_class(view)) != NULL) { 861 } else if ((class = view_get_class(view)) != NULL) {
809 wlr_foreign_toplevel_handle_v1_set_app_id( 862 wlr_foreign_toplevel_handle_v1_set_app_id(view->foreign_toplevel, class);
810 view->foreign_toplevel, class);
811 } 863 }
812} 864}
813 865
814void view_unmap(struct sway_view *view) { 866void view_unmap(struct sway_view *view) {
815 wl_signal_emit(&view->events.unmap, view); 867 wl_signal_emit_mutable(&view->events.unmap, view);
816 868
817 wl_list_remove(&view->surface_new_subsurface.link); 869 view->executed_criteria->length = 0;
818 870
819 if (view->urgent_timer) { 871 if (view->urgent_timer) {
820 wl_event_source_remove(view->urgent_timer); 872 wl_event_source_remove(view->urgent_timer);
821 view->urgent_timer = NULL; 873 view->urgent_timer = NULL;
822 } 874 }
823 875
876 if (view->ext_foreign_toplevel) {
877 wlr_ext_foreign_toplevel_handle_v1_destroy(view->ext_foreign_toplevel);
878 view->ext_foreign_toplevel = NULL;
879 }
880
824 if (view->foreign_toplevel) { 881 if (view->foreign_toplevel) {
825 wlr_foreign_toplevel_handle_v1_destroy(view->foreign_toplevel); 882 wlr_foreign_toplevel_handle_v1_destroy(view->foreign_toplevel);
826 view->foreign_toplevel = NULL; 883 view->foreign_toplevel = NULL;
827 } 884 }
828 885
829 struct sway_container *parent = view->container->parent; 886 struct sway_container *parent = view->container->pending.parent;
830 struct sway_workspace *ws = view->container->workspace; 887 struct sway_workspace *ws = view->container->pending.workspace;
831 container_begin_destroy(view->container); 888 container_begin_destroy(view->container);
832 if (parent) { 889 if (parent) {
833 container_reap_empty(parent); 890 container_reap_empty(parent);
@@ -860,262 +917,54 @@ void view_unmap(struct sway_view *view) {
860 view->surface = NULL; 917 view->surface = NULL;
861} 918}
862 919
863void view_update_size(struct sway_view *view, int width, int height) { 920void view_update_size(struct sway_view *view) {
864 struct sway_container *con = view->container; 921 struct sway_container *con = view->container;
865 922 con->pending.content_width = view->geometry.width;
866 if (container_is_floating(con)) { 923 con->pending.content_height = view->geometry.height;
867 con->content_width = width; 924 container_set_geometry_from_content(con);
868 con->content_height = height;
869 container_set_geometry_from_content(con);
870 } else {
871 con->surface_x = con->content_x + (con->content_width - width) / 2;
872 con->surface_y = con->content_y + (con->content_height - height) / 2;
873 con->surface_x = fmax(con->surface_x, con->content_x);
874 con->surface_y = fmax(con->surface_y, con->content_y);
875 }
876} 925}
877 926
878static const struct sway_view_child_impl subsurface_impl; 927void view_center_and_clip_surface(struct sway_view *view) {
928 struct sway_container *con = view->container;
879 929
880static void subsurface_get_root_coords(struct sway_view_child *child, 930 if (container_is_floating(con)) {
881 int *root_sx, int *root_sy) { 931 // We always center the current coordinates rather than the next, as the
882 struct wlr_surface *surface = child->surface; 932 // geometry immediately affects the currently active rendering.
883 *root_sx = -child->view->geometry.x; 933 int x = (int) fmax(0, (con->current.content_width - view->geometry.width) / 2);
884 *root_sy = -child->view->geometry.y; 934 int y = (int) fmax(0, (con->current.content_height - view->geometry.height) / 2);
885 935
886 if (child->parent && child->parent->impl && 936 wlr_scene_node_set_position(&view->content_tree->node, x, y);
887 child->parent->impl->get_root_coords) {
888 int sx, sy;
889 child->parent->impl->get_root_coords(child->parent, &sx, &sy);
890 *root_sx += sx;
891 *root_sy += sy;
892 } else { 937 } else {
893 while (surface && wlr_surface_is_subsurface(surface)) { 938 wlr_scene_node_set_position(&view->content_tree->node, 0, 0);
894 struct wlr_subsurface *subsurface =
895 wlr_subsurface_from_wlr_surface(surface);
896 if (subsurface == NULL) {
897 break;
898 }
899 *root_sx += subsurface->current.x;
900 *root_sy += subsurface->current.y;
901 surface = subsurface->parent;
902 }
903 } 939 }
904}
905 940
906static void subsurface_destroy(struct sway_view_child *child) { 941 // only make sure to clip the content if there is content to clip
907 if (!sway_assert(child->impl == &subsurface_impl, 942 if (!wl_list_empty(&con->view->content_tree->children)) {
908 "Expected a subsurface")) { 943 wlr_scene_subsurface_tree_set_clip(&con->view->content_tree->node, &(struct wlr_box){
909 return; 944 .x = con->view->geometry.x,
910 } 945 .y = con->view->geometry.y,
911 struct sway_subsurface *subsurface = (struct sway_subsurface *)child; 946 .width = con->current.content_width,
912 wl_list_remove(&subsurface->destroy.link); 947 .height = con->current.content_height,
913 free(subsurface); 948 });
914}
915
916static const struct sway_view_child_impl subsurface_impl = {
917 .get_root_coords = subsurface_get_root_coords,
918 .destroy = subsurface_destroy,
919};
920
921static void subsurface_handle_destroy(struct wl_listener *listener,
922 void *data) {
923 struct sway_subsurface *subsurface =
924 wl_container_of(listener, subsurface, destroy);
925 struct sway_view_child *child = &subsurface->child;
926 view_child_destroy(child);
927}
928
929static void view_child_damage(struct sway_view_child *child, bool whole);
930
931static void view_subsurface_create(struct sway_view *view,
932 struct wlr_subsurface *wlr_subsurface) {
933 struct sway_subsurface *subsurface =
934 calloc(1, sizeof(struct sway_subsurface));
935 if (subsurface == NULL) {
936 sway_log(SWAY_ERROR, "Allocation failed");
937 return;
938 }
939 view_child_init(&subsurface->child, &subsurface_impl, view,
940 wlr_subsurface->surface);
941
942 wl_signal_add(&wlr_subsurface->events.destroy, &subsurface->destroy);
943 subsurface->destroy.notify = subsurface_handle_destroy;
944
945 subsurface->child.mapped = true;
946
947 view_child_damage(&subsurface->child, true);
948}
949
950static void view_child_subsurface_create(struct sway_view_child *child,
951 struct wlr_subsurface *wlr_subsurface) {
952 struct sway_subsurface *subsurface =
953 calloc(1, sizeof(struct sway_subsurface));
954 if (subsurface == NULL) {
955 sway_log(SWAY_ERROR, "Allocation failed");
956 return;
957 }
958 subsurface->child.parent = child;
959 wl_list_insert(&child->children, &subsurface->child.link);
960 view_child_init(&subsurface->child, &subsurface_impl, child->view,
961 wlr_subsurface->surface);
962
963 wl_signal_add(&wlr_subsurface->events.destroy, &subsurface->destroy);
964 subsurface->destroy.notify = subsurface_handle_destroy;
965
966 subsurface->child.mapped = true;
967
968 view_child_damage(&subsurface->child, true);
969}
970
971static void view_child_damage(struct sway_view_child *child, bool whole) {
972 if (!child || !child->mapped || !child->view || !child->view->container) {
973 return;
974 }
975 int sx, sy;
976 child->impl->get_root_coords(child, &sx, &sy);
977 desktop_damage_surface(child->surface,
978 child->view->container->content_x + sx,
979 child->view->container->content_y + sy, whole);
980}
981
982static void view_child_handle_surface_commit(struct wl_listener *listener,
983 void *data) {
984 struct sway_view_child *child =
985 wl_container_of(listener, child, surface_commit);
986 view_child_damage(child, false);
987}
988
989static void view_child_handle_surface_new_subsurface(
990 struct wl_listener *listener, void *data) {
991 struct sway_view_child *child =
992 wl_container_of(listener, child, surface_new_subsurface);
993 struct wlr_subsurface *subsurface = data;
994 view_child_subsurface_create(child, subsurface);
995}
996
997static void view_child_handle_surface_destroy(struct wl_listener *listener,
998 void *data) {
999 struct sway_view_child *child =
1000 wl_container_of(listener, child, surface_destroy);
1001 view_child_destroy(child);
1002}
1003
1004static void view_init_subsurfaces(struct sway_view *view,
1005 struct wlr_surface *surface) {
1006 struct wlr_subsurface *subsurface;
1007 wl_list_for_each(subsurface, &surface->subsurfaces, parent_link) {
1008 view_subsurface_create(view, subsurface);
1009 }
1010}
1011
1012static void view_child_handle_surface_map(struct wl_listener *listener,
1013 void *data) {
1014 struct sway_view_child *child =
1015 wl_container_of(listener, child, surface_map);
1016 child->mapped = true;
1017 view_child_damage(child, true);
1018}
1019
1020static void view_child_handle_surface_unmap(struct wl_listener *listener,
1021 void *data) {
1022 struct sway_view_child *child =
1023 wl_container_of(listener, child, surface_unmap);
1024 view_child_damage(child, true);
1025 child->mapped = false;
1026}
1027
1028static void view_child_handle_view_unmap(struct wl_listener *listener,
1029 void *data) {
1030 struct sway_view_child *child =
1031 wl_container_of(listener, child, view_unmap);
1032 view_child_damage(child, true);
1033 child->mapped = false;
1034}
1035
1036void view_child_init(struct sway_view_child *child,
1037 const struct sway_view_child_impl *impl, struct sway_view *view,
1038 struct wlr_surface *surface) {
1039 child->impl = impl;
1040 child->view = view;
1041 child->surface = surface;
1042 wl_list_init(&child->children);
1043
1044 wl_signal_add(&surface->events.commit, &child->surface_commit);
1045 child->surface_commit.notify = view_child_handle_surface_commit;
1046 wl_signal_add(&surface->events.new_subsurface,
1047 &child->surface_new_subsurface);
1048 child->surface_new_subsurface.notify =
1049 view_child_handle_surface_new_subsurface;
1050 wl_signal_add(&surface->events.destroy, &child->surface_destroy);
1051 child->surface_destroy.notify = view_child_handle_surface_destroy;
1052
1053 // Not all child views have a map/unmap event
1054 child->surface_map.notify = view_child_handle_surface_map;
1055 wl_list_init(&child->surface_map.link);
1056 child->surface_unmap.notify = view_child_handle_surface_unmap;
1057 wl_list_init(&child->surface_unmap.link);
1058
1059 wl_signal_add(&view->events.unmap, &child->view_unmap);
1060 child->view_unmap.notify = view_child_handle_view_unmap;
1061
1062 struct sway_workspace *workspace = child->view->container->workspace;
1063 if (workspace) {
1064 wlr_surface_send_enter(child->surface, workspace->output->wlr_output);
1065 }
1066
1067 view_init_subsurfaces(child->view, surface);
1068}
1069
1070void view_child_destroy(struct sway_view_child *child) {
1071 if (child->mapped && child->view->container != NULL) {
1072 view_child_damage(child, true);
1073 }
1074
1075 if (child->parent != NULL) {
1076 wl_list_remove(&child->link);
1077 child->parent = NULL;
1078 }
1079
1080 struct sway_view_child *subchild, *tmpchild;
1081 wl_list_for_each_safe(subchild, tmpchild, &child->children, link) {
1082 wl_list_remove(&subchild->link);
1083 subchild->parent = NULL;
1084 }
1085
1086 wl_list_remove(&child->surface_commit.link);
1087 wl_list_remove(&child->surface_destroy.link);
1088 wl_list_remove(&child->surface_map.link);
1089 wl_list_remove(&child->surface_unmap.link);
1090 wl_list_remove(&child->view_unmap.link);
1091 wl_list_remove(&child->surface_new_subsurface.link);
1092
1093 if (child->impl && child->impl->destroy) {
1094 child->impl->destroy(child);
1095 } else {
1096 free(child);
1097 } 949 }
1098} 950}
1099 951
1100struct sway_view *view_from_wlr_surface(struct wlr_surface *wlr_surface) { 952struct sway_view *view_from_wlr_surface(struct wlr_surface *wlr_surface) {
1101 if (wlr_surface_is_xdg_surface(wlr_surface)) { 953 struct wlr_xdg_surface *xdg_surface;
1102 struct wlr_xdg_surface *xdg_surface = 954 if ((xdg_surface = wlr_xdg_surface_try_from_wlr_surface(wlr_surface))) {
1103 wlr_xdg_surface_from_wlr_surface(wlr_surface);
1104 return view_from_wlr_xdg_surface(xdg_surface); 955 return view_from_wlr_xdg_surface(xdg_surface);
1105 } 956 }
1106#if HAVE_XWAYLAND 957#if HAVE_XWAYLAND
1107 if (wlr_surface_is_xwayland_surface(wlr_surface)) { 958 struct wlr_xwayland_surface *xsurface;
1108 struct wlr_xwayland_surface *xsurface = 959 if ((xsurface = wlr_xwayland_surface_try_from_wlr_surface(wlr_surface))) {
1109 wlr_xwayland_surface_from_wlr_surface(wlr_surface);
1110 return view_from_wlr_xwayland_surface(xsurface); 960 return view_from_wlr_xwayland_surface(xsurface);
1111 } 961 }
1112#endif 962#endif
1113 if (wlr_surface_is_subsurface(wlr_surface)) { 963 struct wlr_subsurface *subsurface;
1114 struct wlr_subsurface *subsurface = 964 if ((subsurface = wlr_subsurface_try_from_wlr_surface(wlr_surface))) {
1115 wlr_subsurface_from_wlr_surface(wlr_surface);
1116 return view_from_wlr_surface(subsurface->parent); 965 return view_from_wlr_surface(subsurface->parent);
1117 } 966 }
1118 if (wlr_surface_is_layer_surface(wlr_surface)) { 967 if (wlr_layer_surface_v1_try_from_wlr_surface(wlr_surface) != NULL) {
1119 return NULL; 968 return NULL;
1120 } 969 }
1121 970
@@ -1196,6 +1045,18 @@ static size_t parse_title_format(struct sway_view *view, char *buffer) {
1196 return len; 1045 return len;
1197} 1046}
1198 1047
1048void view_update_app_id(struct sway_view *view) {
1049 const char *app_id = view_get_app_id(view);
1050
1051 if (view->foreign_toplevel && app_id) {
1052 wlr_foreign_toplevel_handle_v1_set_app_id(view->foreign_toplevel, app_id);
1053 }
1054
1055 if (view->ext_foreign_toplevel) {
1056 update_ext_foreign_toplevel(view);
1057 }
1058}
1059
1199void view_update_title(struct sway_view *view, bool force) { 1060void view_update_title(struct sway_view *view, bool force) {
1200 const char *title = view_get_title(view); 1061 const char *title = view_get_title(view);
1201 1062
@@ -1211,46 +1072,56 @@ void view_update_title(struct sway_view *view, bool force) {
1211 1072
1212 free(view->container->title); 1073 free(view->container->title);
1213 free(view->container->formatted_title); 1074 free(view->container->formatted_title);
1214 if (title) { 1075
1215 size_t len = parse_title_format(view, NULL); 1076 size_t len = parse_title_format(view, NULL);
1077
1078 if (len) {
1216 char *buffer = calloc(len + 1, sizeof(char)); 1079 char *buffer = calloc(len + 1, sizeof(char));
1217 if (!sway_assert(buffer, "Unable to allocate title string")) { 1080 if (!sway_assert(buffer, "Unable to allocate title string")) {
1218 return; 1081 return;
1219 } 1082 }
1220 parse_title_format(view, buffer);
1221 1083
1222 view->container->title = strdup(title); 1084 parse_title_format(view, buffer);
1223 view->container->formatted_title = buffer; 1085 view->container->formatted_title = buffer;
1224 } else { 1086 } else {
1225 view->container->title = NULL;
1226 view->container->formatted_title = NULL; 1087 view->container->formatted_title = NULL;
1227 } 1088 }
1228 container_calculate_title_height(view->container); 1089
1229 config_update_font_height(false); 1090 view->container->title = title ? strdup(title) : NULL;
1230 1091
1231 // Update title after the global font height is updated 1092 // Update title after the global font height is updated
1232 container_update_title_textures(view->container); 1093 if (view->container->title_bar.title_text && len) {
1094 sway_text_node_set_text(view->container->title_bar.title_text,
1095 view->container->formatted_title);
1096 container_arrange_title_bar(view->container);
1097 } else {
1098 container_update_title_bar(view->container);
1099 }
1233 1100
1234 ipc_event_window(view->container, "title"); 1101 ipc_event_window(view->container, "title");
1235 1102
1236 if (view->foreign_toplevel && title) { 1103 if (view->foreign_toplevel && title) {
1237 wlr_foreign_toplevel_handle_v1_set_title(view->foreign_toplevel, title); 1104 wlr_foreign_toplevel_handle_v1_set_title(view->foreign_toplevel, title);
1238 } 1105 }
1106
1107 if (view->ext_foreign_toplevel) {
1108 update_ext_foreign_toplevel(view);
1109 }
1239} 1110}
1240 1111
1241bool view_is_visible(struct sway_view *view) { 1112bool view_is_visible(struct sway_view *view) {
1242 if (view->container->node.destroying) { 1113 if (view->container->node.destroying) {
1243 return false; 1114 return false;
1244 } 1115 }
1245 struct sway_workspace *workspace = view->container->workspace; 1116 struct sway_workspace *workspace = view->container->pending.workspace;
1246 if (!workspace && view->container->fullscreen_mode != FULLSCREEN_GLOBAL) { 1117 if (!workspace && view->container->pending.fullscreen_mode != FULLSCREEN_GLOBAL) {
1247 bool fs_global_descendant = false; 1118 bool fs_global_descendant = false;
1248 struct sway_container *parent = view->container->parent; 1119 struct sway_container *parent = view->container->pending.parent;
1249 while (parent) { 1120 while (parent) {
1250 if (parent->fullscreen_mode == FULLSCREEN_GLOBAL) { 1121 if (parent->pending.fullscreen_mode == FULLSCREEN_GLOBAL) {
1251 fs_global_descendant = true; 1122 fs_global_descendant = true;
1252 } 1123 }
1253 parent = parent->parent; 1124 parent = parent->pending.parent;
1254 } 1125 }
1255 if (!fs_global_descendant) { 1126 if (!fs_global_descendant) {
1256 return false; 1127 return false;
@@ -1268,13 +1139,13 @@ bool view_is_visible(struct sway_view *view) {
1268 enum sway_container_layout layout = container_parent_layout(con); 1139 enum sway_container_layout layout = container_parent_layout(con);
1269 if ((layout == L_TABBED || layout == L_STACKED) 1140 if ((layout == L_TABBED || layout == L_STACKED)
1270 && !container_is_floating(con)) { 1141 && !container_is_floating(con)) {
1271 struct sway_node *parent = con->parent ? 1142 struct sway_node *parent = con->pending.parent ?
1272 &con->parent->node : &con->workspace->node; 1143 &con->pending.parent->node : &con->pending.workspace->node;
1273 if (seat_get_active_tiling_child(seat, parent) != &con->node) { 1144 if (seat_get_active_tiling_child(seat, parent) != &con->node) {
1274 return false; 1145 return false;
1275 } 1146 }
1276 } 1147 }
1277 con = con->parent; 1148 con = con->pending.parent;
1278 } 1149 }
1279 // Check view isn't hidden by another fullscreen view 1150 // Check view isn't hidden by another fullscreen view
1280 struct sway_container *fs = root->fullscreen_global ? 1151 struct sway_container *fs = root->fullscreen_global ?
@@ -1296,6 +1167,7 @@ void view_set_urgent(struct sway_view *view, bool enable) {
1296 return; 1167 return;
1297 } 1168 }
1298 clock_gettime(CLOCK_MONOTONIC, &view->urgent); 1169 clock_gettime(CLOCK_MONOTONIC, &view->urgent);
1170 container_update_itself_and_parents(view->container);
1299 } else { 1171 } else {
1300 view->urgent = (struct timespec){ 0 }; 1172 view->urgent = (struct timespec){ 0 };
1301 if (view->urgent_timer) { 1173 if (view->urgent_timer) {
@@ -1303,12 +1175,11 @@ void view_set_urgent(struct sway_view *view, bool enable) {
1303 view->urgent_timer = NULL; 1175 view->urgent_timer = NULL;
1304 } 1176 }
1305 } 1177 }
1306 container_damage_whole(view->container);
1307 1178
1308 ipc_event_window(view->container, "urgent"); 1179 ipc_event_window(view->container, "urgent");
1309 1180
1310 if (!container_is_scratchpad_hidden(view->container)) { 1181 if (!container_is_scratchpad_hidden(view->container)) {
1311 workspace_detect_urgent(view->container->workspace); 1182 workspace_detect_urgent(view->container->pending.workspace);
1312 } 1183 }
1313} 1184}
1314 1185
@@ -1317,40 +1188,54 @@ bool view_is_urgent(struct sway_view *view) {
1317} 1188}
1318 1189
1319void view_remove_saved_buffer(struct sway_view *view) { 1190void view_remove_saved_buffer(struct sway_view *view) {
1320 if (!sway_assert(!wl_list_empty(&view->saved_buffers), "Expected a saved buffer")) { 1191 if (!sway_assert(view->saved_surface_tree, "Expected a saved buffer")) {
1321 return; 1192 return;
1322 } 1193 }
1323 struct sway_saved_buffer *saved_buf, *tmp; 1194
1324 wl_list_for_each_safe(saved_buf, tmp, &view->saved_buffers, link) { 1195 wlr_scene_node_destroy(&view->saved_surface_tree->node);
1325 wlr_buffer_unlock(&saved_buf->buffer->base); 1196 view->saved_surface_tree = NULL;
1326 wl_list_remove(&saved_buf->link); 1197 wlr_scene_node_set_enabled(&view->content_tree->node, true);
1327 free(saved_buf);
1328 }
1329} 1198}
1330 1199
1331static void view_save_buffer_iterator(struct wlr_surface *surface, 1200static void view_save_buffer_iterator(struct wlr_scene_buffer *buffer,
1332 int sx, int sy, void *data) { 1201 int sx, int sy, void *data) {
1333 struct sway_view *view = data; 1202 struct wlr_scene_tree *tree = data;
1334 1203
1335 if (surface && wlr_surface_has_buffer(surface)) { 1204 struct wlr_scene_buffer *sbuf = wlr_scene_buffer_create(tree, NULL);
1336 wlr_buffer_lock(&surface->buffer->base); 1205 if (!sbuf) {
1337 struct sway_saved_buffer *saved_buffer = calloc(1, sizeof(struct sway_saved_buffer)); 1206 sway_log(SWAY_ERROR, "Could not allocate a scene buffer when saving a surface");
1338 saved_buffer->buffer = surface->buffer; 1207 return;
1339 saved_buffer->width = surface->current.width;
1340 saved_buffer->height = surface->current.height;
1341 saved_buffer->x = sx;
1342 saved_buffer->y = sy;
1343 saved_buffer->transform = surface->current.transform;
1344 wlr_surface_get_buffer_source_box(surface, &saved_buffer->source_box);
1345 wl_list_insert(&view->saved_buffers, &saved_buffer->link);
1346 } 1208 }
1209
1210 wlr_scene_buffer_set_dest_size(sbuf,
1211 buffer->dst_width, buffer->dst_height);
1212 wlr_scene_buffer_set_opaque_region(sbuf, &buffer->opaque_region);
1213 wlr_scene_buffer_set_source_box(sbuf, &buffer->src_box);
1214 wlr_scene_node_set_position(&sbuf->node, sx, sy);
1215 wlr_scene_buffer_set_transform(sbuf, buffer->transform);
1216 wlr_scene_buffer_set_buffer(sbuf, buffer->buffer);
1347} 1217}
1348 1218
1349void view_save_buffer(struct sway_view *view) { 1219void view_save_buffer(struct sway_view *view) {
1350 if (!sway_assert(wl_list_empty(&view->saved_buffers), "Didn't expect saved buffer")) { 1220 if (!sway_assert(!view->saved_surface_tree, "Didn't expect saved buffer")) {
1351 view_remove_saved_buffer(view); 1221 view_remove_saved_buffer(view);
1352 } 1222 }
1353 view_for_each_surface(view, view_save_buffer_iterator, view); 1223
1224 view->saved_surface_tree = wlr_scene_tree_create(view->scene_tree);
1225 if (!view->saved_surface_tree) {
1226 sway_log(SWAY_ERROR, "Could not allocate a scene tree node when saving a surface");
1227 return;
1228 }
1229
1230 // Enable and disable the saved surface tree like so to atomitaclly update
1231 // the tree. This will prevent over damaging or other weirdness.
1232 wlr_scene_node_set_enabled(&view->saved_surface_tree->node, false);
1233
1234 wlr_scene_node_for_each_buffer(&view->content_tree->node,
1235 view_save_buffer_iterator, view->saved_surface_tree);
1236
1237 wlr_scene_node_set_enabled(&view->content_tree->node, false);
1238 wlr_scene_node_set_enabled(&view->saved_surface_tree->node, true);
1354} 1239}
1355 1240
1356bool view_is_transient_for(struct sway_view *child, 1241bool view_is_transient_for(struct sway_view *child,
@@ -1358,3 +1243,19 @@ bool view_is_transient_for(struct sway_view *child,
1358 return child->impl->is_transient_for && 1243 return child->impl->is_transient_for &&
1359 child->impl->is_transient_for(child, ancestor); 1244 child->impl->is_transient_for(child, ancestor);
1360} 1245}
1246
1247static void send_frame_done_iterator(struct wlr_scene_buffer *scene_buffer,
1248 int x, int y, void *data) {
1249 struct timespec *when = data;
1250 wl_signal_emit_mutable(&scene_buffer->events.frame_done, when);
1251}
1252
1253void view_send_frame_done(struct sway_view *view) {
1254 struct timespec when;
1255 clock_gettime(CLOCK_MONOTONIC, &when);
1256
1257 struct wlr_scene_node *node;
1258 wl_list_for_each(node, &view->content_tree->children, link) {
1259 wlr_scene_node_for_each_buffer(node, send_frame_done_iterator, &when);
1260 }
1261}