diff options
Diffstat (limited to 'sway/container.c')
-rw-r--r-- | sway/container.c | 118 |
1 files changed, 91 insertions, 27 deletions
diff --git a/sway/container.c b/sway/container.c index 7ccc2e09..5f1510a9 100644 --- a/sway/container.c +++ b/sway/container.c | |||
@@ -57,10 +57,6 @@ static void free_swayc(swayc_t *cont) { | |||
57 | 57 | ||
58 | // New containers | 58 | // New containers |
59 | 59 | ||
60 | static bool workspace_test(swayc_t *view, void *name) { | ||
61 | return strcasecmp(view->name, (char *)name) == 0; | ||
62 | } | ||
63 | |||
64 | swayc_t *new_output(wlc_handle handle) { | 60 | swayc_t *new_output(wlc_handle handle) { |
65 | const struct wlc_size* size = wlc_output_get_resolution(handle); | 61 | const struct wlc_size* size = wlc_output_get_resolution(handle); |
66 | const char *name = wlc_output_get_name(handle); | 62 | const char *name = wlc_output_get_name(handle); |
@@ -84,7 +80,7 @@ swayc_t *new_output(wlc_handle handle) { | |||
84 | if (strcasecmp(wso->output, name) == 0) { | 80 | if (strcasecmp(wso->output, name) == 0) { |
85 | sway_log(L_DEBUG, "Matched workspace to output: %s for %s", wso->workspace, wso->output); | 81 | sway_log(L_DEBUG, "Matched workspace to output: %s for %s", wso->workspace, wso->output); |
86 | // Check if any other workspaces are using this name | 82 | // Check if any other workspaces are using this name |
87 | if (find_container(&root_container, workspace_test, wso->workspace)) { | 83 | if (workspace_by_name(wso->workspace)) { |
88 | sway_log(L_DEBUG, "But it's already taken"); | 84 | sway_log(L_DEBUG, "But it's already taken"); |
89 | break; | 85 | break; |
90 | } | 86 | } |
@@ -128,7 +124,8 @@ swayc_t *new_workspace(swayc_t *output, const char *name) { | |||
128 | } | 124 | } |
129 | 125 | ||
130 | swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) { | 126 | swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) { |
131 | if (!ASSERT_NONNULL(child)) { | 127 | if (!ASSERT_NONNULL(child) |
128 | && !sway_assert(!child->is_floating, "cannot create container around floating window")) { | ||
132 | return NULL; | 129 | return NULL; |
133 | } | 130 | } |
134 | swayc_t *cont = new_swayc(C_CONTAINER); | 131 | swayc_t *cont = new_swayc(C_CONTAINER); |
@@ -207,6 +204,9 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { | |||
207 | } | 204 | } |
208 | 205 | ||
209 | swayc_t *new_floating_view(wlc_handle handle) { | 206 | swayc_t *new_floating_view(wlc_handle handle) { |
207 | if (swayc_active_workspace() == NULL) { | ||
208 | return NULL; | ||
209 | } | ||
210 | const char *title = wlc_view_get_title(handle); | 210 | const char *title = wlc_view_get_title(handle); |
211 | swayc_t *view = new_swayc(C_VIEW); | 211 | swayc_t *view = new_swayc(C_VIEW); |
212 | sway_log(L_DEBUG, "Adding new view %lu:%x:%s as a floating view", | 212 | sway_log(L_DEBUG, "Adding new view %lu:%x:%s as a floating view", |
@@ -220,8 +220,8 @@ swayc_t *new_floating_view(wlc_handle handle) { | |||
220 | const struct wlc_geometry* geometry = wlc_view_get_geometry(handle); | 220 | const struct wlc_geometry* geometry = wlc_view_get_geometry(handle); |
221 | 221 | ||
222 | // give it requested geometry, but place in center | 222 | // give it requested geometry, but place in center |
223 | view->x = (active_workspace->width - geometry->size.w) / 2; | 223 | view->x = (swayc_active_workspace()->width - geometry->size.w) / 2; |
224 | view->y = (active_workspace->height- geometry->size.h) / 2; | 224 | view->y = (swayc_active_workspace()->height- geometry->size.h) / 2; |
225 | view->width = geometry->size.w; | 225 | view->width = geometry->size.w; |
226 | view->height = geometry->size.h; | 226 | view->height = geometry->size.h; |
227 | 227 | ||
@@ -231,10 +231,10 @@ swayc_t *new_floating_view(wlc_handle handle) { | |||
231 | view->is_floating = true; | 231 | view->is_floating = true; |
232 | 232 | ||
233 | // Case of focused workspace, just create as child of it | 233 | // Case of focused workspace, just create as child of it |
234 | list_add(active_workspace->floating, view); | 234 | list_add(swayc_active_workspace()->floating, view); |
235 | view->parent = active_workspace; | 235 | view->parent = swayc_active_workspace(); |
236 | if (active_workspace->focused == NULL) { | 236 | if (swayc_active_workspace()->focused == NULL) { |
237 | set_focused_container_for(active_workspace, view); | 237 | set_focused_container_for(swayc_active_workspace(), view); |
238 | } | 238 | } |
239 | return view; | 239 | return view; |
240 | } | 240 | } |
@@ -306,6 +306,35 @@ swayc_t *destroy_view(swayc_t *view) { | |||
306 | 306 | ||
307 | // Container lookup | 307 | // Container lookup |
308 | 308 | ||
309 | |||
310 | swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) { | ||
311 | if (!container->children) { | ||
312 | return NULL; | ||
313 | } | ||
314 | // Special case for checking floating stuff | ||
315 | int i; | ||
316 | if (container->type == C_WORKSPACE) { | ||
317 | for (i = 0; i < container->floating->length; ++i) { | ||
318 | swayc_t *child = container->floating->items[i]; | ||
319 | if (test(child, data)) { | ||
320 | return child; | ||
321 | } | ||
322 | } | ||
323 | } | ||
324 | for (i = 0; i < container->children->length; ++i) { | ||
325 | swayc_t *child = container->children->items[i]; | ||
326 | if (test(child, data)) { | ||
327 | return child; | ||
328 | } else { | ||
329 | swayc_t *res = swayc_by_test(child, test, data); | ||
330 | if (res) { | ||
331 | return res; | ||
332 | } | ||
333 | } | ||
334 | } | ||
335 | return NULL; | ||
336 | } | ||
337 | |||
309 | swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { | 338 | swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { |
310 | if (!ASSERT_NONNULL(container)) { | 339 | if (!ASSERT_NONNULL(container)) { |
311 | return NULL; | 340 | return NULL; |
@@ -332,27 +361,30 @@ swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts layout) { | |||
332 | return container; | 361 | return container; |
333 | } | 362 | } |
334 | 363 | ||
335 | swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) { | 364 | static swayc_t *_swayc_by_handle_helper(wlc_handle handle, swayc_t *parent) { |
336 | if (!container->children) { | 365 | if (!parent || !parent->children) { |
337 | return NULL; | 366 | return NULL; |
338 | } | 367 | } |
339 | // Special case for checking floating stuff | 368 | int i, len; |
340 | int i; | 369 | swayc_t **child; |
341 | if (container->type == C_WORKSPACE) { | 370 | if (parent->type == C_WORKSPACE) { |
342 | for (i = 0; i < container->floating->length; ++i) { | 371 | len = parent->floating->length; |
343 | swayc_t *child = container->floating->items[i]; | 372 | child = (swayc_t **)parent->floating->items; |
344 | if (test(child, data)) { | 373 | for (i = 0; i < len; ++i, ++child) { |
345 | return child; | 374 | if ((*child)->handle == handle) { |
375 | return *child; | ||
346 | } | 376 | } |
347 | } | 377 | } |
348 | } | 378 | } |
349 | for (i = 0; i < container->children->length; ++i) { | 379 | |
350 | swayc_t *child = container->children->items[i]; | 380 | len = parent->children->length; |
351 | if (test(child, data)) { | 381 | child = (swayc_t**)parent->children->items; |
352 | return child; | 382 | for (i = 0; i < len; ++i, ++child) { |
383 | if ((*child)->handle == handle) { | ||
384 | return *child; | ||
353 | } else { | 385 | } else { |
354 | swayc_t *res = find_container(child, test, data); | 386 | swayc_t *res; |
355 | if (res) { | 387 | if ((res = _swayc_by_handle_helper(handle, *child))) { |
356 | return res; | 388 | return res; |
357 | } | 389 | } |
358 | } | 390 | } |
@@ -360,6 +392,38 @@ swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *da | |||
360 | return NULL; | 392 | return NULL; |
361 | } | 393 | } |
362 | 394 | ||
395 | swayc_t *swayc_by_handle(wlc_handle handle) { | ||
396 | return _swayc_by_handle_helper(handle, &root_container); | ||
397 | } | ||
398 | |||
399 | swayc_t *swayc_active_output(void) { | ||
400 | return root_container.focused; | ||
401 | } | ||
402 | |||
403 | swayc_t *swayc_active_workspace(void) { | ||
404 | return root_container.focused ? root_container.focused->focused : NULL; | ||
405 | } | ||
406 | |||
407 | swayc_t *swayc_active_workspace_for(swayc_t *cont) { | ||
408 | if (! cont) { | ||
409 | return NULL; | ||
410 | } | ||
411 | switch (cont->type) { | ||
412 | case C_ROOT: cont = cont->focused; | ||
413 | case C_OUTPUT: cont = cont->focused; | ||
414 | case C_WORKSPACE: return cont; | ||
415 | default: return swayc_parent_by_type(cont, C_WORKSPACE); | ||
416 | } | ||
417 | } | ||
418 | |||
419 | // Container information | ||
420 | |||
421 | bool swayc_is_fullscreen(swayc_t *view) { | ||
422 | return view && view->type == C_VIEW && (wlc_view_get_state(view->handle) & WLC_BIT_FULLSCREEN); | ||
423 | } | ||
424 | |||
425 | // Mapping | ||
426 | |||
363 | void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { | 427 | void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { |
364 | if (container && container->children && container->children->length) { | 428 | if (container && container->children && container->children->length) { |
365 | int i; | 429 | int i; |