diff --git a/include/container.h b/include/container.h index dd934be6..e395a55b 100644 --- a/include/container.h +++ b/include/container.h @@ -36,16 +36,24 @@ 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; + 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; }; @@ -57,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 f0db4ed2..5035316e 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,77 @@ 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) { + // 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 + 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; + 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); + } + // Refocus on the view once its been put back into the layout + focus_view(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 +450,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..231876c5 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,12 @@ 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; + //Case of focused workspace, just create as child of it if (sibling->type == C_WORKSPACE) { add_child(sibling, view); @@ -145,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 = view->width; + view->desired_height = view->height; + + 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) { @@ -192,23 +231,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/handlers.c b/sway/handlers.c index 3e83b850..77e8f237 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) { + view->desired_width = geometry->size.w; + view->desired_height = geometry->size.h; + + if (view->is_floating) { + 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); + } + } } 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 4407742a..1bc65050 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -65,10 +65,20 @@ swayc_t *replace_child(swayc_t *child, swayc_t *new_child) { swayc_t *remove_child(swayc_t *parent, swayc_t *child) { int i; - for (i = 0; i < parent->children->length; ++i) { - if (parent->children->items[i] == child) { - list_del(parent->children, i); - break; + // 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; + } + } + } 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) { @@ -192,6 +202,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 +236,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 */