From 05f969074e39c5adeb5d7390d8255b83cf351866 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Mon, 17 Aug 2015 00:38:34 -0500 Subject: Added in basic floating toggling --- include/container.h | 5 ++++ sway/children | 26 ++++++++++++++++++++ sway/commands.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ sway/container.c | 21 +++++++++++++---- sway/layout.c | 47 ++++++++++++++++++++++++++++++++++++ sway/workspace.c | 13 ++++++++++ 6 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 sway/children diff --git a/include/container.h b/include/container.h index dd934be6..7560ddb8 100644 --- a/include/container.h +++ b/include/container.h @@ -40,12 +40,17 @@ struct sway_container { bool visible; + bool is_floating; + int weight; char *name; list_t *children; + // Special list for floating windows in workspaces + list_t *floating; + struct sway_container *parent; struct sway_container *focused; }; diff --git a/sway/children b/sway/children new file mode 100644 index 00000000..55ef43c9 --- /dev/null +++ b/sway/children @@ -0,0 +1,26 @@ +workspace.c:90:35: swayc_t *current_output = active_workspace->parent; +workspace.c:93:78: if (strcmp((((swayc_t *)current_output->children->items[i])->name), active_workspace->name) == 0) { +workspace.c:105:35: swayc_t *current_output = active_workspace->parent; +workspace.c:108:78: if (strcmp((((swayc_t *)current_output->children->items[i])->name), active_workspace->name) == 0) { +workspace.c:133:35: swayc_t *current_output = active_workspace->parent; +workspace.c:136:78: if (strcmp((((swayc_t *)current_output->children->items[i])->name), active_workspace->name) == 0) { +workspace.c:149:35: swayc_t *current_output = active_workspace->parent; +workspace.c:152:78: if (strcmp((((swayc_t *)current_output->children->items[i])->name), active_workspace->name) == 0) { +workspace.c:176:23: swayc_t *ws_output = workspace->parent; +workspace.c:183:70: sway_log(L_DEBUG, "workspace: changing from '%s' to '%s'", focused_workspace->name, workspace->name); +workspace.c:199:20: swayc_t *output = workspace->parent; + +container.c:79:2: workspace->layout = L_HORIZ; // TODO:default layout +container.c:80:2: workspace->width = output->width; +container.c:81:2: workspace->height = output->height; +container.c:82:2: workspace->name = strdup(name); +container.c:83:2: workspace->visible = true; +container.c:105:19: cont->focused = workspace->focused; +container.c:106:3: workspace->focused = cont; +container.c:108:24: list_t *tmp_list = workspace->children; +container.c:109:3: workspace->children = cont->children; +container.c:114:18: cont->layout = workspace->layout; +container.c:115:3: workspace->layout = layout; +container.c:162:6: if (workspace->children->length == 0) { +container.c:163:61: sway_log(L_DEBUG, "Workspace: Destroying workspace '%s'", workspace->name); +container.c:164:21: swayc_t *parent = workspace->parent; diff --git a/sway/commands.c b/sway/commands.c index f0db4ed2..91dfa2b2 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -13,6 +13,7 @@ #include "workspace.h" #include "commands.h" #include "container.h" +#include "handlers.h" struct modifier_key { char *name; @@ -169,6 +170,72 @@ static bool cmd_exit(struct sway_config *config, int argc, char **argv) { return true; } +static bool cmd_floating(struct sway_config *config, int argc, char **argv) { + if (strcasecmp(argv[0], "toggle") == 0) { + swayc_t *view = get_focused_container(&root_container); + // Prevent running floating commands on things like workspaces + if (view->type != C_VIEW) { + return true; + } + int i; + // Change from nonfloating to floating + if (!view->is_floating) { + view->is_floating = true; + for (i = 0; i < view->parent->children->length; i++) { + if (view->parent->children->items[i] == view) { + // Cut down on width/height so it's obvious that you've gone floating + // if this is the only view + view->width = view->width - 30; + view->height = view->height - 30; + + // Swap from the list of whatever container the view was in + // to the workspace->floating list + // TODO: Destroy any remaining empty containers + list_del(view->parent->children, i); + list_add(active_workspace->floating, view); + + // Set the new position of the container and arrange windows + view->x = (active_workspace->width - view->width)/2; + view->y = (active_workspace->height - view->height)/2; + sway_log(L_INFO, "Setting container %p to floating at coordinates X:%d Y:%d, W:%d, H:%d", view, view->x, view->y, view->width, view->height); + // Change parent to active_workspace + view->parent = active_workspace; + arrange_windows(active_workspace, -1, -1); + return true; + } + } + } else { + // Delete the view from the floating list and unset its is_floating flag + // Using length-1 as the index is safe because the view must be the currently + // focused floating output + list_del(active_workspace->floating, active_workspace->floating->length - 1); + view->is_floating = false; + active_workspace->focused = NULL; + // Get the properly focused container, and add in the view there + swayc_t *focused = focus_pointer(); + // If focused is null, it's because the currently focused container is a workspace + if (focused == NULL) { + focused = active_workspace; + } + + sway_log(L_DEBUG, "Non-floating focused container is %p", focused); + + //Case of focused workspace, just create as child of it + if (focused->type == C_WORKSPACE) { + add_child(focused, view); + } + //Regular case, create as sibling of current container + else { + add_sibling(focused, view); + } + arrange_windows(active_workspace, -1, -1); + return true; + } + } + + return true; +} + static bool cmd_focus(struct sway_config *config, int argc, char **argv) { if (!checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1)) { return false; @@ -378,6 +445,7 @@ static struct cmd_handler handlers[] = { { "exec", cmd_exec }, { "exec_always", cmd_exec_always }, { "exit", cmd_exit }, + { "floating", cmd_floating }, { "focus", cmd_focus }, { "focus_follows_mouse", cmd_focus_follows_mouse }, { "fullscreen", cmd_fullscreen }, diff --git a/sway/container.c b/sway/container.c index 4321b6ce..87e48e91 100644 --- a/sway/container.c +++ b/sway/container.c @@ -81,6 +81,7 @@ swayc_t *new_workspace(swayc_t * output, const char *name) { workspace->height = output->height; workspace->name = strdup(name); workspace->visible = true; + workspace->floating = create_list(); add_child(output, workspace); return workspace; @@ -134,6 +135,9 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { view->name = strdup(title); view->visible = true; + // TODO: properly set this + view->is_floating = false; + //Case of focused workspace, just create as child of it if (sibling->type == C_WORKSPACE) { add_child(sibling, view); @@ -192,23 +196,32 @@ swayc_t *destroy_view(swayc_t *view) { if (parent->type == C_CONTAINER) { return destroy_container(parent); } + return parent; } - swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) { if (!container->children) { return NULL; } + // Special case for checking floating stuff int i; + if (container->type == C_WORKSPACE) { + for (i = 0; i < container->floating->length; ++i) { + swayc_t *child = container->floating->items[i]; + if (test(child, data)) { + return child; + } + } + } for (i = 0; i < container->children->length; ++i) { swayc_t *child = container->children->items[i]; if (test(child, data)) { return child; } else { - swayc_t *_ = find_container(child, test, data); - if (_) { - return _; + swayc_t *res = find_container(child, test, data); + if (res) { + return res; } } } diff --git a/sway/layout.c b/sway/layout.c index 4407742a..c53b9dad 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -65,6 +65,15 @@ swayc_t *replace_child(swayc_t *child, swayc_t *new_child) { swayc_t *remove_child(swayc_t *parent, swayc_t *child) { int i; + // Special case for floating views + if (child->is_floating) { + for (i = 0; i < parent->floating->length; ++i) { + if (parent->floating->items[i] == child) { + list_del(parent->floating, i); + break; + } + } + } for (i = 0; i < parent->children->length; ++i) { if (parent->children->items[i] == child) { list_del(parent->children, i); @@ -192,6 +201,33 @@ void arrange_windows(swayc_t *container, int width, int height) { } break; } + + // Arrage floating layouts for workspaces last + if (container->type == C_WORKSPACE) { + for (i = 0; i < container->floating->length; ++i) { + swayc_t *view = ((swayc_t *)container->floating->items[i]); + // Set the geometry + struct wlc_geometry geometry = { + .origin = { + .x = view->x, + .y = view->y + }, + .size = { + .w = view->width, + .h = view->height + } + }; + wlc_view_set_geometry(view->handle, &geometry); + + // Bring the views to the front in order of the list, the list + // will be kept up to date so that more recently focused views + // have higher indexes + // This is conditional on there not being a fullscreen view in the workspace + if (!(wlc_view_get_state(container->focused->handle) & WLC_BIT_FULLSCREEN)) { + wlc_view_bring_to_front(view->handle); + } + } + } layout_log(&root_container, 0); } @@ -199,7 +235,18 @@ swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) { if (parent->children == NULL) { return NULL; } + + // Search for floating workspaces int i; + if (parent->type == C_WORKSPACE) { + for (i = 0; i < parent->floating->length; ++i) { + swayc_t *child = parent->floating->items[i]; + if (child->handle == handle) { + return child; + } + } + } + for (i = 0; i < parent->children->length; ++i) { swayc_t *child = parent->children->items[i]; if (child->handle == handle) { diff --git a/sway/workspace.c b/sway/workspace.c index 4db75f48..df646445 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -255,5 +255,18 @@ void layout_log(const swayc_t *c, int depth) { for (i = 0; i < depth; ++i) fputc(' ', stderr); fprintf(stderr,")\n"); } + if (c->type == C_WORKSPACE) { + e = c->floating?c->floating->length:0; + for (i = 0; i < depth; ++i) fputc(' ', stderr); + if (e) { + for (i = 0; i < depth; ++i) fputc(' ', stderr); + fprintf(stderr,"(\n"); + for (i = 0; i < e; ++i) { + layout_log(c->floating->items[i], depth + 1); + } + for (i = 0; i < depth; ++i) fputc(' ', stderr); + fprintf(stderr,")\n"); + } + } } /* XXX:DEBUG:XXX */ -- cgit v1.2.3 From da77dc45a9fe8fd4c2d7db474d14257501dd1813 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Mon, 17 Aug 2015 00:40:14 -0500 Subject: Deleted file --- sway/children | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 sway/children diff --git a/sway/children b/sway/children deleted file mode 100644 index 55ef43c9..00000000 --- a/sway/children +++ /dev/null @@ -1,26 +0,0 @@ -workspace.c:90:35: swayc_t *current_output = active_workspace->parent; -workspace.c:93:78: if (strcmp((((swayc_t *)current_output->children->items[i])->name), active_workspace->name) == 0) { -workspace.c:105:35: swayc_t *current_output = active_workspace->parent; -workspace.c:108:78: if (strcmp((((swayc_t *)current_output->children->items[i])->name), active_workspace->name) == 0) { -workspace.c:133:35: swayc_t *current_output = active_workspace->parent; -workspace.c:136:78: if (strcmp((((swayc_t *)current_output->children->items[i])->name), active_workspace->name) == 0) { -workspace.c:149:35: swayc_t *current_output = active_workspace->parent; -workspace.c:152:78: if (strcmp((((swayc_t *)current_output->children->items[i])->name), active_workspace->name) == 0) { -workspace.c:176:23: swayc_t *ws_output = workspace->parent; -workspace.c:183:70: sway_log(L_DEBUG, "workspace: changing from '%s' to '%s'", focused_workspace->name, workspace->name); -workspace.c:199:20: swayc_t *output = workspace->parent; - -container.c:79:2: workspace->layout = L_HORIZ; // TODO:default layout -container.c:80:2: workspace->width = output->width; -container.c:81:2: workspace->height = output->height; -container.c:82:2: workspace->name = strdup(name); -container.c:83:2: workspace->visible = true; -container.c:105:19: cont->focused = workspace->focused; -container.c:106:3: workspace->focused = cont; -container.c:108:24: list_t *tmp_list = workspace->children; -container.c:109:3: workspace->children = cont->children; -container.c:114:18: cont->layout = workspace->layout; -container.c:115:3: workspace->layout = layout; -container.c:162:6: if (workspace->children->length == 0) { -container.c:163:61: sway_log(L_DEBUG, "Workspace: Destroying workspace '%s'", workspace->name); -container.c:164:21: swayc_t *parent = workspace->parent; -- cgit v1.2.3 From 47ec999e7184d29c2911af20ce696d1f4e948e29 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Mon, 17 Aug 2015 10:18:06 -0500 Subject: Fixes to floating and umanaged views --- include/container.h | 5 +++++ sway/commands.c | 13 ++++++++----- sway/container.c | 35 +++++++++++++++++++++++++++++++++++ sway/handlers.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- sway/layout.c | 11 ++++++----- 5 files changed, 99 insertions(+), 14 deletions(-) diff --git a/include/container.h b/include/container.h index 7560ddb8..3136e565 100644 --- a/include/container.h +++ b/include/container.h @@ -36,6 +36,9 @@ struct sway_container { // Not including borders or margins int width, height; + // Used for setting floating geometry + int desired_width, desired_height; + int x, y; bool visible; @@ -62,6 +65,8 @@ swayc_t *new_workspace(swayc_t * output, const char *name); swayc_t *new_container(swayc_t *child, enum swayc_layouts layout); //Creates view as a sibling of current focused container, or as child of a workspace swayc_t *new_view(swayc_t *sibling, wlc_handle handle); +//Creates view as a new floating view which is in the active workspace +swayc_t *new_floating_view(wlc_handle handle); swayc_t *destroy_output(swayc_t *output); diff --git a/sway/commands.c b/sway/commands.c index 91dfa2b2..8a81cd76 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -183,16 +183,19 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) { view->is_floating = true; for (i = 0; i < view->parent->children->length; i++) { if (view->parent->children->items[i] == view) { - // Cut down on width/height so it's obvious that you've gone floating - // if this is the only view - view->width = view->width - 30; - view->height = view->height - 30; + // Try to use desired geometry to set w/h + if (view->desired_width != -1) { + view->width = view->desired_width; + } + if (view->desired_height != -1) { + view->height = view->desired_height; + } // Swap from the list of whatever container the view was in // to the workspace->floating list - // TODO: Destroy any remaining empty containers list_del(view->parent->children, i); list_add(active_workspace->floating, view); + destroy_container(view->parent); // Set the new position of the container and arrange windows view->x = (active_workspace->width - view->width)/2; diff --git a/sway/container.c b/sway/container.c index 87e48e91..1f93d4dc 100644 --- a/sway/container.c +++ b/sway/container.c @@ -135,6 +135,9 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { view->name = strdup(title); view->visible = true; + view->desired_width = -1; + view->desired_height = -1; + // TODO: properly set this view->is_floating = false; @@ -149,6 +152,38 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { return view; } +swayc_t *new_floating_view(wlc_handle handle) { + const char *title = wlc_view_get_title(handle); + swayc_t *view = new_swayc(C_VIEW); + sway_log(L_DEBUG, "Adding new view %u:%s as a floating view", + (unsigned int)handle, title); + //Setup values + view->handle = handle; + view->name = strdup(title); + view->visible = true; + + // Set the geometry of the floating view + const struct wlc_geometry* geometry = wlc_view_get_geometry(handle); + + view->x = geometry->origin.x; + view->y = geometry->origin.y; + view->width = geometry->size.w; + view->height = geometry->size.h; + + view->desired_width = -1; + view->desired_height = -1; + + view->is_floating = true; + + //Case of focused workspace, just create as child of it + list_add(active_workspace->floating, view); + view->parent = active_workspace; + if (active_workspace->focused == NULL) { + active_workspace->focused = view; + } + return view; +} + swayc_t *destroy_output(swayc_t *output) { if (output->children->length == 0) { diff --git a/sway/handlers.c b/sway/handlers.c index 3e83b850..85df09f7 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -10,6 +10,7 @@ #include "handlers.h" #include "stringop.h" #include "workspace.h" +#include "container.h" static struct wlc_origin mouse_origin; @@ -88,17 +89,24 @@ static void handle_output_focused(wlc_handle output, bool focus) { static bool handle_view_created(wlc_handle handle) { swayc_t *focused = get_focused_container(&root_container); uint32_t type = wlc_view_get_type(handle); - //If override_redirect/unmanaged/popup/modal/splach + // If override_redirect/unmanaged/popup/modal/splach if (type) { sway_log(L_DEBUG,"Unmanaged window of type %x left alone", type); wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); if (type & WLC_BIT_UNMANAGED) { return true; } - //for things like Dmenu + // For things like Dmenu if (type & WLC_BIT_OVERRIDE_REDIRECT) { wlc_view_focus(handle); } + + // Float popups + if (type & WLC_BIT_POPUP) { + swayc_t *view = new_floating_view(handle); + focus_view(view); + arrange_windows(active_workspace, -1, -1); + } } else { swayc_t *view = new_view(focused, handle); //Set maximize flag for windows. @@ -118,6 +126,24 @@ static bool handle_view_created(wlc_handle handle) { static void handle_view_destroyed(wlc_handle handle) { sway_log(L_DEBUG, "Destroying window %u", (unsigned int)handle); + + // Properly handle unmanaged views + uint32_t type = wlc_view_get_type(handle); + if (type) { + wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); + sway_log(L_DEBUG,"Unmanaged window of type %x was destroyed", type); + if (type & WLC_BIT_UNMANAGED) { + focus_view(focus_pointer()); + arrange_windows(active_workspace, -1, -1); + return; + } + + if (type & WLC_BIT_OVERRIDE_REDIRECT) { + focus_view(focus_pointer()); + arrange_windows(active_workspace, -1, -1); + return; + } + } swayc_t *view = get_swayc_for_handle(handle, &root_container); swayc_t *parent; swayc_t *focused = get_focused_container(&root_container); @@ -135,8 +161,23 @@ static void handle_view_focus(wlc_handle view, bool focus) { return; } -static void handle_view_geometry_request(wlc_handle view, const struct wlc_geometry* geometry) { - // deny that shit +static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geometry* geometry) { + // If the view is floating, then apply the geometry. + // Otherwise save the desired width/height for the view. + // This will not do anything for the time being as WLC improperly sends geometry requests + swayc_t *view = get_swayc_for_handle(handle, &root_container); + if (view) { + if (view->is_floating) { + view->width = geometry->size.w; + view->height = geometry->size.h; + view->x = geometry->origin.x; + view->y = geometry->origin.y; + arrange_windows(view->parent, -1, -1); + } else { + view->desired_width = geometry->size.w; + view->desired_height = geometry->size.h; + } + } } static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit state, bool toggle) { diff --git a/sway/layout.c b/sway/layout.c index c53b9dad..1bc65050 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -73,11 +73,12 @@ swayc_t *remove_child(swayc_t *parent, swayc_t *child) { break; } } - } - for (i = 0; i < parent->children->length; ++i) { - if (parent->children->items[i] == child) { - list_del(parent->children, i); - break; + } else { + for (i = 0; i < parent->children->length; ++i) { + if (parent->children->items[i] == child) { + list_del(parent->children, i); + break; + } } } if (parent->focused == child) { -- cgit v1.2.3 From 3f475f5009974a5dcdfe12f578208a4fd92c84ba Mon Sep 17 00:00:00 2001 From: Luminarys Date: Mon, 17 Aug 2015 10:22:00 -0500 Subject: Minor fix to do proper floating refocuses --- sway/commands.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sway/commands.c b/sway/commands.c index 8a81cd76..5035316e 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -231,6 +231,8 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) { else { add_sibling(focused, view); } + // Refocus on the view once its been put back into the layout + focus_view(view); arrange_windows(active_workspace, -1, -1); return true; } -- cgit v1.2.3 From c9ce8bf1bd0f4cfb459bcb7d3ec45429c0a81293 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Mon, 17 Aug 2015 10:34:39 -0500 Subject: Style and other minor fixes --- include/container.h | 8 ++++---- sway/container.c | 4 ++-- sway/handlers.c | 12 ++++++------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/container.h b/include/container.h index 3136e565..e395a55b 100644 --- a/include/container.h +++ b/include/container.h @@ -36,14 +36,14 @@ struct sway_container { // Not including borders or margins int width, height; - // Used for setting floating geometry - int desired_width, desired_height; + // Used for setting floating geometry + int desired_width, desired_height; int x, y; bool visible; - bool is_floating; + bool is_floating; int weight; @@ -51,7 +51,7 @@ struct sway_container { list_t *children; - // Special list for floating windows in workspaces + // Special list for floating windows in workspaces list_t *floating; struct sway_container *parent; diff --git a/sway/container.c b/sway/container.c index 1f93d4dc..231876c5 100644 --- a/sway/container.c +++ b/sway/container.c @@ -170,8 +170,8 @@ swayc_t *new_floating_view(wlc_handle handle) { view->width = geometry->size.w; view->height = geometry->size.h; - view->desired_width = -1; - view->desired_height = -1; + view->desired_width = view->width; + view->desired_height = view->height; view->is_floating = true; diff --git a/sway/handlers.c b/sway/handlers.c index 85df09f7..77e8f237 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -167,16 +167,16 @@ static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geo // This will not do anything for the time being as WLC improperly sends geometry requests swayc_t *view = get_swayc_for_handle(handle, &root_container); if (view) { + view->desired_width = geometry->size.w; + view->desired_height = geometry->size.h; + if (view->is_floating) { - view->width = geometry->size.w; - view->height = geometry->size.h; + view->width = view->desired_width; + view->height = view->desired_height; view->x = geometry->origin.x; view->y = geometry->origin.y; arrange_windows(view->parent, -1, -1); - } else { - view->desired_width = geometry->size.w; - view->desired_height = geometry->size.h; - } + } } } -- cgit v1.2.3