diff --git a/include/config.h b/include/config.h index 38e93eb8..9243bf35 100644 --- a/include/config.h +++ b/include/config.h @@ -3,6 +3,7 @@ #include #include +#include #include "list.h" struct sway_variable { @@ -32,16 +33,17 @@ struct sway_config { list_t *cmd_queue; list_t *workspace_outputs; struct sway_mode *current_mode; + uint32_t floating_mod; // Flags bool focus_follows_mouse; bool mouse_warping; - bool active; - bool failed; + bool active; + bool failed; bool reloading; }; -bool load_config(); +bool load_config(void); bool read_config(FILE *file, bool is_active); char *do_var_replacement(struct sway_config *config, char *str); diff --git a/include/container.h b/include/container.h index e395a55b..5f386368 100644 --- a/include/container.h +++ b/include/container.h @@ -45,6 +45,8 @@ struct sway_container { bool is_floating; + bool is_focused; + int weight; char *name; diff --git a/include/focus.h b/include/focus.h new file mode 100644 index 00000000..185910f3 --- /dev/null +++ b/include/focus.h @@ -0,0 +1,36 @@ +#ifndef _SWAY_FOCUS_H +#define _SWAY_FOCUS_H +#include "container.h" + +enum movement_direction { + MOVE_LEFT, + MOVE_RIGHT, + MOVE_UP, + MOVE_DOWN, + MOVE_PARENT +}; + +//focused_container - the container found by following the `focused` pointer +//from a given container to a container with `is_focused` boolean set +//--- +//focused_view - the container found by following the `focused` pointer from a +//given container to a view. +//--- + +swayc_t *get_focused_container(swayc_t *parent); +swayc_t *get_focused_view(swayc_t *parent); + +void set_focused_container(swayc_t *container); +void set_focused_container_for(swayc_t *ancestor, swayc_t *container); + +//lock focused container/view. locked by windows with OVERRIDE attribute +//and unlocked when they are destroyed + +extern bool locked_container_focus; +extern bool locked_view_focus; + + +bool move_focus(enum movement_direction direction); + +#endif + diff --git a/include/handlers.h b/include/handlers.h index d1742cce..6b642419 100644 --- a/include/handlers.h +++ b/include/handlers.h @@ -1,12 +1,13 @@ #ifndef _SWAY_HANDLERS_H #define _SWAY_HANDLERS_H - +#include "container.h" #include #include extern struct wlc_interface interface; +extern uint32_t keys_pressed[32]; //set focus to current pointer location and return focused container -swayc_t *focus_pointer(void); +swayc_t *container_under_pointer(void); #endif diff --git a/include/layout.h b/include/layout.h index a7f43fda..26d00ce4 100644 --- a/include/layout.h +++ b/include/layout.h @@ -13,7 +13,7 @@ void add_child(swayc_t *parent, swayc_t *child); //Returns parent container which needs to be rearranged. swayc_t *add_sibling(swayc_t *sibling, swayc_t *child); swayc_t *replace_child(swayc_t *child, swayc_t *new_child); -swayc_t *remove_child(swayc_t *parent, swayc_t *child); +swayc_t *remove_child(swayc_t *child); //Layout void arrange_windows(swayc_t *container, int width, int height); diff --git a/include/movement.h b/include/movement.h deleted file mode 100644 index dd701877..00000000 --- a/include/movement.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef _SWAY_MOVEMENT_H -#define _SWAY_MOVEMENT_H - -#include -#include "list.h" - -enum movement_direction { - MOVE_LEFT, - MOVE_RIGHT, - MOVE_UP, - MOVE_DOWN, - MOVE_PARENT -}; - -bool move_focus(enum movement_direction direction); - -#endif diff --git a/sway/commands.c b/sway/commands.c index 642fa3ce..7e9169e8 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -8,7 +8,7 @@ #include #include "stringop.h" #include "layout.h" -#include "movement.h" +#include "focus.h" #include "log.h" #include "workspace.h" #include "commands.h" @@ -171,6 +171,10 @@ static bool cmd_exit(struct sway_config *config, int argc, char **argv) { } static bool cmd_floating(struct sway_config *config, int argc, char **argv) { + if (!checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1)) { + return false; + } + if (strcasecmp(argv[0], "toggle") == 0) { swayc_t *view = get_focused_container(&root_container); // Prevent running floating commands on things like workspaces @@ -215,11 +219,12 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) { view->is_floating = false; active_workspace->focused = NULL; // Get the properly focused container, and add in the view there - swayc_t *focused = focus_pointer(); + swayc_t *focused = container_under_pointer(); // If focused is null, it's because the currently focused container is a workspace if (focused == NULL) { focused = active_workspace; } + set_focused_container(focused); sway_log(L_DEBUG, "Non-floating focused container is %p", focused); @@ -232,7 +237,7 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) { add_sibling(focused, view); } // Refocus on the view once its been put back into the layout - focus_view(view); + set_focused_container(view); arrange_windows(active_workspace, -1, -1); return true; } @@ -241,6 +246,14 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) { return true; } +static bool cmd_floating_mod(struct sway_config *config, int argc, char **argv) { + if (!checkarg(argc, "floating_modifier", EXPECTED_EQUAL_TO, 1)) { + return false; + } + config->floating_mod = xkb_keysym_from_name(argv[0], XKB_KEYSYM_CASE_INSENSITIVE); + return true; +} + static bool cmd_focus(struct sway_config *config, int argc, char **argv) { if (!checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1)) { return false; @@ -345,7 +358,7 @@ static bool _do_split(struct sway_config *config, int argc, char **argv, int lay else { sway_log(L_DEBUG, "Adding new container around current focused container"); swayc_t *parent = new_container(focused, layout); - focus_view(focused); + set_focused_container(focused); arrange_windows(parent, -1, -1); } @@ -465,6 +478,7 @@ static struct cmd_handler handlers[] = { { "exec_always", cmd_exec_always }, { "exit", cmd_exit }, { "floating", cmd_floating }, + { "floating_modifier", cmd_floating_mod }, { "focus", cmd_focus }, { "focus_follows_mouse", cmd_focus_follows_mouse }, { "fullscreen", cmd_fullscreen }, diff --git a/sway/config.c b/sway/config.c index e2d10d28..95d605a3 100644 --- a/sway/config.c +++ b/sway/config.c @@ -126,7 +126,7 @@ static char* get_config_path() { return NULL; } -bool load_config() { +bool load_config(void) { sway_log(L_INFO, "Loading config"); char *path = get_config_path(); diff --git a/sway/container.c b/sway/container.c index 3534721d..2b9f7554 100644 --- a/sway/container.c +++ b/sway/container.c @@ -27,10 +27,7 @@ static void free_swayc(swayc_t *c) { list_free(c->children); } if (c->parent) { - if (c->parent->focused == c) { - c->parent->focused = NULL; - } - remove_child(c->parent, c); + remove_child(c); } if (c->name) { free(c->name); @@ -39,17 +36,11 @@ static void free_swayc(swayc_t *c) { } /* New containers */ -static void add_output_widths(swayc_t *container, void *_width) { - int *width = _width; - if (container->type == C_OUTPUT) { - *width += container->width; - } -} swayc_t *new_output(wlc_handle handle) { const struct wlc_size* size = wlc_output_get_resolution(handle); const char *name = wlc_output_get_name(handle); - sway_log(L_DEBUG, "Added output %u %s", (unsigned int)handle, name); + sway_log(L_DEBUG, "Added output %lu:%s", handle, name); swayc_t *output = new_swayc(C_OUTPUT); output->width = size->w; @@ -59,9 +50,12 @@ swayc_t *new_output(wlc_handle handle) { add_child(&root_container, output); - //TODO something with this - int total_width = 0; - container_map(&root_container, add_output_widths, &total_width); +//TODO still dont know why this is here? +// int total_width = 0; +// int i; +// for (i = 0; i < root_container.children->length; ++i) { +// total_width += ((swayc_t*)root_container.children->items[i])->width; +// } //Create workspace char *ws_name = NULL; @@ -79,7 +73,10 @@ swayc_t *new_output(wlc_handle handle) { if (!ws_name) { ws_name = workspace_next_name(); } - new_workspace(output, ws_name); + //create and initilize default workspace + swayc_t *ws = new_workspace(output, ws_name); + ws->is_focused = true; + free(ws_name); return output; @@ -118,6 +115,11 @@ swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) { //reorder focus cont->focused = workspace->focused; workspace->focused = cont; + //set all children focu to container + int i; + for (i = 0; i < workspace->children->length; ++i) { + ((swayc_t *)workspace->children->items[i])->parent = cont; + } //Swap children list_t *tmp_list = workspace->children; workspace->children = cont->children; @@ -139,14 +141,15 @@ swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) { } swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { - const char *title = wlc_view_get_title(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 to container %p %d", - (unsigned int)handle, title, sibling, sibling?sibling->type:0); + sway_log(L_DEBUG, "Adding new view %lu:%s to container %p %d", + handle, title, sibling, sibling ? sibling->type : 0); //Setup values view->handle = handle; view->name = title ? strdup(title) : NULL; view->visible = true; + view->is_focused = true; view->desired_width = -1; view->desired_height = -1; @@ -168,8 +171,8 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { 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); + sway_log(L_DEBUG, "Adding new view %lu:%x:%s as a floating view", + handle, wlc_view_get_type(handle), title); //Setup values view->handle = handle; view->name = title ? strdup(title) : NULL; @@ -197,12 +200,13 @@ swayc_t *new_floating_view(wlc_handle handle) { return view; } +/* Destroy container */ swayc_t *destroy_output(swayc_t *output) { if (output->children->length == 0) { //TODO move workspaces to other outputs } - sway_log(L_DEBUG, "OUTPUT: Destroying output '%u'", (unsigned int)output->handle); + sway_log(L_DEBUG, "OUTPUT: Destroying output '%lu'", output->handle); free_swayc(output); return &root_container; } @@ -244,7 +248,6 @@ swayc_t *destroy_view(swayc_t *view) { if (parent->type == C_CONTAINER) { return destroy_container(parent); } - return parent; } @@ -300,3 +303,5 @@ void set_view_visibility(swayc_t *view, void *data) { } view->visible = (*p == 2); } + + diff --git a/sway/focus.c b/sway/focus.c new file mode 100644 index 00000000..14d27184 --- /dev/null +++ b/sway/focus.c @@ -0,0 +1,192 @@ +#include + +#include "focus.h" +#include "log.h" +#include "workspace.h" + +bool locked_container_focus = false; +bool locked_view_focus = false; + +//switches parent focus to c. will switch it accordingly +//TODO, everything needs a handle, so we can set front/back position properly +static void update_focus(swayc_t *c) { + //Handle if focus switches + swayc_t *parent = c->parent; + if (parent->focused != c) { + switch (c->type) { + case C_ROOT: return; + case C_OUTPUT: + wlc_output_focus(c->parent->handle); + break; + //switching workspaces + case C_WORKSPACE: + if (parent->focused) { + swayc_t *ws = parent->focused; + //hide visibility of old workspace + uint32_t mask = 1; + container_map(ws, set_view_visibility, &mask); + //set visibility of new workspace + mask = 2; + container_map(c, set_view_visibility, &mask); + wlc_output_set_mask(parent->handle, 2); + destroy_workspace(ws); + } + active_workspace = c; + break; + default: + case C_VIEW: + case C_CONTAINER: + //TODO whatever to do when container changes + //for example, stacked and tabbing change stuff. + break; + } + } + c->parent->focused = c; +} + +bool move_focus(enum movement_direction direction) { + if (locked_container_focus) { + return false; + } + swayc_t *current = get_focused_container(&root_container); + swayc_t *parent = current->parent; + + if (direction == MOVE_PARENT) { + if (parent->type == C_OUTPUT) { + sway_log(L_DEBUG, "Focus cannot move to parent"); + return false; + } else { + sway_log(L_DEBUG, "Moving focus from %p:%ld to %p:%ld", + current, current->handle, parent, parent->handle); + set_focused_container(parent); + return true; + } + } + + while (true) { + sway_log(L_DEBUG, "Moving focus away from %p", current); + + // Test if we can even make a difference here + bool can_move = false; + int diff = 0; + if (direction == MOVE_LEFT || direction == MOVE_RIGHT) { + if (parent->layout == L_HORIZ || parent->type == C_ROOT) { + can_move = true; + diff = direction == MOVE_LEFT ? -1 : 1; + } + } else { + if (parent->layout == L_VERT) { + can_move = true; + diff = direction == MOVE_UP ? -1 : 1; + } + } + sway_log(L_DEBUG, "Can move? %s", can_move ? "yes" : "no"); + if (can_move) { + int i; + for (i = 0; i < parent->children->length; ++i) { + swayc_t *child = parent->children->items[i]; + if (child == current) { + break; + } + } + int desired = i + diff; + sway_log(L_DEBUG, "Moving from %d to %d", i, desired); + if (desired < 0 || desired >= parent->children->length) { + can_move = false; + } else { + swayc_t *newview = parent->children->items[desired]; + set_focused_container(get_focused_view(newview)); + return true; + } + } + if (!can_move) { + sway_log(L_DEBUG, "Can't move at current level, moving up tree"); + current = parent; + parent = parent->parent; + if (!parent) { + // Nothing we can do + return false; + } + } + } +} + +swayc_t *get_focused_container(swayc_t *parent) { + while (parent && !parent->is_focused) { + parent = parent->focused; + } + //just incase + if (parent == NULL) { + sway_log(L_DEBUG, "get_focused_container unable to find container"); + return active_workspace; + } + return parent; +} + +void set_focused_container(swayc_t *c) { + if (locked_container_focus || !c) { + return; + } + sway_log(L_DEBUG, "Setting focus to %p:%ld", c, c->handle); + if (c->type != C_ROOT && c->type != C_OUTPUT) { + c->is_focused = true; + } + swayc_t *prev_view = get_focused_view(&root_container); + swayc_t *p = c; + while (p != &root_container) { + update_focus(p); + p = p->parent; + p->is_focused = false; + } + if (!locked_view_focus) { + p = get_focused_view(c); + //Set focus to p + if (p && !(wlc_view_get_type(p->handle) & WLC_BIT_POPUP)) { + if (prev_view) { + wlc_view_set_state(prev_view->handle, WLC_BIT_ACTIVATED, false); + } + wlc_view_focus(p->handle); + wlc_view_set_state(p->handle, WLC_BIT_ACTIVATED, true); + } + } +} + +void set_focused_container_for(swayc_t *a, swayc_t *c) { + if (locked_container_focus || !c) { + return; + } + swayc_t *find = c; + //Ensure that a is an ancestor of c + while (find != a && (find = find->parent)) { + if (find == &root_container) { + return; + } + } + + sway_log(L_DEBUG, "Setting focus for %p:%ld to %p:%ld", + a, a->handle, c, c->handle); + + c->is_focused = true; + swayc_t *p = c; + while (p != a) { + update_focus(p); + p = p->parent; + p->is_focused = false; + } + if (!locked_view_focus) { + p = get_focused_view(c); + //Set focus to p + if (p) { + wlc_view_focus(p->handle); + wlc_view_set_state(p->handle, WLC_BIT_ACTIVATED, true); + } + } +} + +swayc_t *get_focused_view(swayc_t *parent) { + while (parent && parent->type != C_VIEW) { + parent = parent->focused; + } + return parent; +} + diff --git a/sway/handlers.c b/sway/handlers.c index e17aefee..0157d466 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -3,18 +3,23 @@ #include #include #include -#include "layout.h" + +#include "handlers.h" #include "log.h" +#include "layout.h" #include "config.h" #include "commands.h" -#include "handlers.h" #include "stringop.h" #include "workspace.h" #include "container.h" +#include "focus.h" + +uint32_t keys_pressed[32]; static struct wlc_origin mouse_origin; -//Keyboard input is being overrided by window (dmenu) -static bool override_redirect = false; + +static bool m1_held = false; +static bool m2_held = false; static bool pointer_test(swayc_t *view, void *_origin) { const struct wlc_origin *origin = _origin; @@ -23,27 +28,60 @@ static bool pointer_test(swayc_t *view, void *_origin) { while (parent->type != C_OUTPUT) { parent = parent->parent; } - if (view->type == C_VIEW && origin->x >= view->x && origin->y >= view->y - && origin->x < view->x + view->width && origin->y < view->y + view->height - && view->visible && parent == root_container.focused) { + if (origin->x >= view->x && origin->y >= view->y + && origin->x < view->x + view->width && origin->y < view->y + view->height + && view->visible && parent == root_container.focused) { return true; } return false; } -swayc_t *focus_pointer(void) { - swayc_t *focused = get_focused_container(&root_container); - if (!(wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN)) { - swayc_t *pointer = find_container(&root_container, pointer_test, &mouse_origin); - if (pointer && focused != pointer) { - unfocus_all(&root_container); - focus_view(pointer); - } else if (!focused) { - focus_view(active_workspace); +swayc_t *container_under_pointer(void) { + //root.output->workspace + if (!root_container.focused || !root_container.focused->focused) { + return NULL; + } + swayc_t *lookup = root_container.focused->focused; + //Case of empty workspace + if (lookup->children == 0) { + return NULL; + } + while (lookup->type != C_VIEW) { + int i; + int len; + //if tabbed/stacked go directly to focused container, otherwise search + //children + if (lookup->layout == L_TABBED || lookup->layout == L_STACKED) { + lookup = lookup->focused; + continue; + } + //if workspace, search floating + if (lookup->type == C_WORKSPACE) { + len = lookup->floating->length; + for (i = 0; i < len; ++i) { + if (pointer_test(lookup->floating->items[i], &mouse_origin)) { + lookup = lookup->floating->items[i]; + break; + } + } + if (i < len) { + continue; + } + } + //search children + len = lookup->children->length; + for (i = 0; i < len; ++i) { + if (pointer_test(lookup->children->items[i], &mouse_origin)) { + lookup = lookup->children->items[i]; + break; + } + } + //when border and titles are done, this could happen + if (i == len) { + break; } - focused = pointer; } - return focused; + return lookup; } static bool handle_output_created(wlc_handle output) { @@ -81,90 +119,77 @@ static void handle_output_resolution_change(wlc_handle output, const struct wlc_ static void handle_output_focused(wlc_handle output, bool focus) { swayc_t *c = get_swayc_for_handle(output, &root_container); - if (!c) return; + //if for some reason this output doesnt exist, create it. + if (!c) { + handle_output_created(output); + } if (focus) { - unfocus_all(&root_container); - focus_view(c); + set_focused_container(c); } } 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 (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 - if (type & WLC_BIT_OVERRIDE_REDIRECT) { - override_redirect = true; - wlc_view_focus(handle); - } - - // Float popups - if (type & WLC_BIT_POPUP) { - swayc_t *view = new_floating_view(handle); - wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, false); - focus_view(view); - arrange_windows(active_workspace, -1, -1); - } - } else { - swayc_t *view = new_view(focused, handle); - //Set maximize flag for windows. - //TODO: floating windows have this unset + swayc_t *newview = NULL; + switch (wlc_view_get_type(handle)) { + //regular view created regularly + case 0: + newview = new_view(focused, handle); wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, true); - unfocus_all(&root_container); - focus_view(view); - arrange_windows(view->parent, -1, -1); + break; + //takes keyboard focus + case WLC_BIT_OVERRIDE_REDIRECT: + sway_log(L_DEBUG, "view %ld with OVERRIDE_REDIRECT", handle); + locked_view_focus = true; + wlc_view_focus(handle); + wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); + wlc_view_bring_to_front(handle); + break; + //Takes container focus + case WLC_BIT_OVERRIDE_REDIRECT|WLC_BIT_UNMANAGED: + sway_log(L_DEBUG, "view %ld with OVERRIDE_REDIRECT|WLC_BIT_MANAGED", handle); + wlc_view_bring_to_front(handle); + locked_container_focus = true; + break; + //set modals as floating containers + case WLC_BIT_MODAL: + wlc_view_bring_to_front(handle); + newview = new_floating_view(handle); + case WLC_BIT_POPUP: + break; } - if (wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN) { - unfocus_all(&root_container); - focus_view(focused); - arrange_windows(focused, -1, -1); + if (newview) { + set_focused_container(newview); + arrange_windows(newview->parent, -1, -1); } return true; } 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) { - // We need to call focus_view() on focus_pointer because unmanaged windows - // do not alter the focus structure of the container tree. This makes focus_pointer() - // think that it doesn't need to do anything, so we manually focus the result. - focus_view(focus_pointer()); - return; - } - - if (type & WLC_BIT_OVERRIDE_REDIRECT) { - override_redirect = false; - focus_view(focus_pointer()); - return; - } - - // WLC_BIT_POPUP doesn't need to be dealt with since it's - // treated as a floating view. - } - + sway_log(L_DEBUG, "Destroying window %lu", handle); swayc_t *view = get_swayc_for_handle(handle, &root_container); - swayc_t *parent; swayc_t *focused = get_focused_container(&root_container); - if (view) { - parent = destroy_view(view); - arrange_windows(parent, -1, -1); - } - if (!focused || focused == view) { - focus_pointer(); + switch (wlc_view_get_type(handle)) { + //regular view created regularly + case 0: + case WLC_BIT_MODAL: + if (view) { + swayc_t *parent = destroy_view(view); + arrange_windows(parent, -1, -1); + } + break; + //takes keyboard focus + case WLC_BIT_OVERRIDE_REDIRECT: + locked_view_focus = false; + break; + //Takes container focus + case WLC_BIT_OVERRIDE_REDIRECT|WLC_BIT_UNMANAGED: + locked_container_focus = false; + case WLC_BIT_POPUP: + break; } + set_focused_container(get_focused_view(&root_container)); } static void handle_view_focus(wlc_handle view, bool focus) { @@ -172,6 +197,8 @@ static void handle_view_focus(wlc_handle view, bool focus) { } static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geometry* geometry) { + sway_log(L_DEBUG, "geometry request %d x %d : %d x %d", + geometry->origin.x, geometry->origin.y, geometry->size.w,geometry->size.h); // 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 @@ -186,33 +213,32 @@ static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geo 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) { + swayc_t *c = NULL; switch(state) { case WLC_BIT_FULLSCREEN: - { - //I3 just lets it become fullscreen - wlc_view_set_state(view,state,toggle); - swayc_t *c = get_swayc_for_handle(view, &root_container); - sway_log(L_DEBUG, "setting view %ld %s, fullscreen %d",view,c->name,toggle); - if (c) { - arrange_windows(c->parent, -1, -1); - //Set it as focused window for that workspace if its going - //fullscreen - if (toggle) { - swayc_t *ws = c; - while (ws->type != C_WORKSPACE) { - ws = ws->parent; - } - //Set ws focus to c - focus_view_for(ws, c); + //I3 just lets it become fullscreen + wlc_view_set_state(view, state, toggle); + c = get_swayc_for_handle(view, &root_container); + sway_log(L_DEBUG, "setting view %ld %s, fullscreen %d",view,c->name,toggle); + if (c) { + arrange_windows(c->parent, -1, -1); + //Set it as focused window for that workspace if its going + //fullscreen + if (toggle) { + swayc_t *ws = c; + while (ws->type != C_WORKSPACE) { + ws = ws->parent; } + //Set ws focus to c + set_focused_container_for(ws, c); } - break; } + break; case WLC_BIT_MAXIMIZED: case WLC_BIT_RESIZING: case WLC_BIT_MOVING: @@ -226,11 +252,10 @@ static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit s static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, uint32_t key, uint32_t sym, enum wlc_key_state state) { enum { QSIZE = 32 }; - if (override_redirect) { + if (locked_view_focus && state == WLC_KEY_STATE_PRESSED) { return false; } static uint8_t head = 0; - static uint32_t array[QSIZE]; bool cmd_success = false; struct sway_mode *mode = config->current_mode; @@ -239,13 +264,13 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier //Find key, if it has been pressed int mid = 0; - while (mid < head && array[mid] != sym) { + while (mid < head && keys_pressed[mid] != sym) { ++mid; } if (state == WLC_KEY_STATE_PRESSED && mid == head && head + 1 < QSIZE) { - array[head++] = sym; + keys_pressed[head++] = sym; } else if (state == WLC_KEY_STATE_RELEASED && mid < head) { - memmove(array + mid, array + mid + 1, sizeof*array * (--head - mid)); + memmove(keys_pressed + mid, keys_pressed + mid + 1, sizeof*keys_pressed * (--head - mid)); } // TODO: reminder to check conflicts with mod+q+a versus mod+q int i; @@ -260,7 +285,7 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier xkb_keysym_t *key = binding->keys->items[j]; uint8_t k; for (k = 0; k < head; ++k) { - if (array[k] == *key) { + if (keys_pressed[k] == *key) { match = true; break; } @@ -271,12 +296,12 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier } if (match) { - //Remove matched keys from array + //Remove matched keys from keys_pressed int j; for (j = 0; j < binding->keys->length; ++j) { uint8_t k; for (k = 0; k < head; ++k) { - memmove(array + k, array + k + 1, sizeof*array * (--head - k)); + memmove(keys_pressed + k, keys_pressed + k + 1, sizeof*keys_pressed * (--head - k)); break; } } @@ -291,13 +316,100 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier return cmd_success; } -static bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_origin *origin) { - static wlc_handle prev_view = 0; +static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct wlc_origin *origin) { + static struct wlc_origin prev_pos; + static wlc_handle prev_handle = 0; mouse_origin = *origin; - if (config->focus_follows_mouse && prev_view != view) { - focus_pointer(); + bool changed_floating = false; + int i = 0; + // Do checks to determine if proper keys are being held + swayc_t *view = active_workspace->focused; + if (m1_held) { + if (view->is_floating) { + while (keys_pressed[i++]) { + if (keys_pressed[i] == config->floating_mod) { + int dx = mouse_origin.x - prev_pos.x; + int dy = mouse_origin.y - prev_pos.y; + sway_log(L_DEBUG, "Moving from px: %d to cx: %d and from py: %d to cy: %d", prev_pos.x, mouse_origin.x, prev_pos.y, mouse_origin.y); + sway_log(L_DEBUG, "Moving: dx: %d, dy: %d", dx, dy); + + view->x += dx; + view->y += dy; + changed_floating = true; + break; + } + } + } + } else if (m2_held) { + if (view->is_floating) { + while (keys_pressed[i++]) { + if (keys_pressed[i] == config->floating_mod) { + int dx = mouse_origin.x - prev_pos.x; + int dy = mouse_origin.y - prev_pos.y; + sway_log(L_DEBUG, "Moving from px: %d to cx: %d and from py: %d to cy: %d", prev_pos.x, mouse_origin.x, prev_pos.y, mouse_origin.y); + sway_log(L_INFO, "Moving: dx: %d, dy: %d", dx, dy); + + // Move and resize the view based on the dx/dy and mouse position + int midway_x = view->x + view->width/2; + int midway_y = view->y + view->height/2; + + if (dx < 0) { + changed_floating = true; + if (mouse_origin.x > midway_x) { + sway_log(L_INFO, "Downsizing view to the left"); + view->width += dx; + } else { + sway_log(L_INFO, "Upsizing view to the left"); + view->x += dx; + view->width -= dx; + } + } else if (dx > 0){ + changed_floating = true; + if (mouse_origin.x > midway_x) { + sway_log(L_INFO, "Upsizing to the right"); + view->width += dx; + } else { + sway_log(L_INFO, "Downsizing to the right"); + view->x += dx; + view->width -= dx; + } + } + + if (dy < 0) { + changed_floating = true; + if (mouse_origin.y > midway_y) { + sway_log(L_INFO, "Downsizing view to the top"); + view->height += dy; + } else { + sway_log(L_INFO, "Upsizing the view to the top"); + view->y += dy; + view->height -= dy; + } + } else if (dy > 0) { + changed_floating = true; + if (mouse_origin.y > midway_y) { + sway_log(L_INFO, "Upsizing to the bottom"); + view->height += dy; + } else { + sway_log(L_INFO, "Downsizing to the bottom"); + view->y += dy; + view->height -= dy; + } + } + break; + } + } + } + } + if (config->focus_follows_mouse && prev_handle != handle) { + set_focused_container(container_under_pointer()); + } + prev_handle = handle; + prev_pos = mouse_origin; + if (changed_floating) { + arrange_windows(view, -1, -1); + return true; } - prev_view = view; return false; } @@ -305,8 +417,24 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w uint32_t button, enum wlc_button_state state) { swayc_t *focused = get_focused_container(&root_container); if (state == WLC_BUTTON_STATE_PRESSED) { - swayc_t *pointer = focus_pointer(); + sway_log(L_DEBUG, "Mouse button %u pressed", button); + if (button == 272) { + m1_held = true; + } + if (button == 273) { + m2_held = true; + } + swayc_t *pointer = container_under_pointer(); + set_focused_container(pointer); return (pointer && pointer != focused); + } else { + sway_log(L_DEBUG, "Mouse button %u released", button); + if (button == 272) { + m1_held = false; + } + if (button == 273) { + m2_held = false; + } } return false; } diff --git a/sway/layout.c b/sway/layout.c index f4079d4b..8ff5c4b7 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -31,9 +31,6 @@ void add_child(swayc_t *parent, swayc_t *child) { child->width, child->height, parent, parent->type, parent->width, parent->height); list_add(parent->children, child); child->parent = parent; - if (parent->focused == NULL) { - parent->focused = child; - } } swayc_t *add_sibling(swayc_t *sibling, swayc_t *child) { @@ -63,8 +60,9 @@ swayc_t *replace_child(swayc_t *child, swayc_t *new_child) { return parent; } -swayc_t *remove_child(swayc_t *parent, swayc_t *child) { +swayc_t *remove_child(swayc_t *child) { int i; + swayc_t *parent = child->parent; // Special case for floating views if (child->is_floating) { for (i = 0; i < parent->floating->length; ++i) { @@ -82,7 +80,11 @@ swayc_t *remove_child(swayc_t *parent, swayc_t *child) { } } if (parent->focused == child) { - parent->focused = NULL; + if (parent->children->length > 0) { + parent->focused = parent->children->items[i?i-1:0]; + } else { + parent->focused = NULL; + } } return parent; } @@ -206,7 +208,7 @@ void arrange_windows(swayc_t *container, int width, int height) { // 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]); + swayc_t *view = container->floating->items[i]; // Set the geometry struct wlc_geometry geometry = { .origin = { @@ -262,64 +264,3 @@ swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) { return NULL; } -swayc_t *get_focused_container(swayc_t *parent) { - if (parent->focused == NULL) { - return parent; - } - return get_focused_container(parent->focused); -} - -void unfocus_all(swayc_t *container) { - if (container->children == NULL) { - return; - } - int i; - for (i = 0; i < container->children->length; ++i) { - swayc_t *view = container->children->items[i]; - if (view->type == C_VIEW) { - wlc_view_set_state(view->handle, WLC_BIT_ACTIVATED, false); - } else { - unfocus_all(view); - } - } -} - -void focus_view(swayc_t *view) { - if (!view) { - return; - } - sway_log(L_DEBUG, "Setting focus for %p:%ld", view, view->handle); - swayc_t *c = view; - //Set focus from root to view - while (c != &root_container) { - c->parent->focused = c; - c = c->parent; - } - //Set output - wlc_output_focus(c->focused->handle); - //get focus for views focused window - while (view && view->type != C_VIEW) { - view = view->focused; - } - if (view) { - wlc_view_set_state(view->handle, WLC_BIT_ACTIVATED, true); - wlc_view_focus(view->handle); - } -} - -void focus_view_for(swayc_t *top, swayc_t *view) { - swayc_t *find = view; - //Make sure top is a ancestor of view - while (find != top) { - if (find == &root_container) { - return; - } - find = find->parent; - } - //Set focus for top to go to view - while (view != top) { - view->parent->focused = view; - view = view->parent; - } -} - diff --git a/sway/log.c b/sway/log.c index b9048b34..03639ae4 100644 --- a/sway/log.c +++ b/sway/log.c @@ -8,7 +8,7 @@ int colored = 1; int v = 0; -const char *verbosity_colors[] = { +static const char *verbosity_colors[] = { "", // L_SILENT "\x1B[1;31m", // L_ERROR "\x1B[1;34m", // L_INFO diff --git a/sway/movement.c b/sway/movement.c deleted file mode 100644 index 12726392..00000000 --- a/sway/movement.c +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include "list.h" -#include "log.h" -#include "layout.h" -#include "movement.h" - -bool move_focus(enum movement_direction direction) { - swayc_t *current = get_focused_container(&root_container); - swayc_t *parent = current->parent; - - if (direction == MOVE_PARENT) { - if (parent->type == C_OUTPUT) { - sway_log(L_DEBUG, "Focus cannot move to parent"); - return false; - } else { - sway_log(L_DEBUG, "Moving focus away from %p to %p", current, parent); - unfocus_all(parent->parent); - focus_view(parent); - return true; - } - } - - while (true) { - sway_log(L_DEBUG, "Moving focus away from %p", current); - - // Test if we can even make a difference here - bool can_move = false; - int diff = 0; - if (direction == MOVE_LEFT || direction == MOVE_RIGHT) { - if (parent->layout == L_HORIZ || parent->type == C_ROOT) { - can_move = true; - diff = direction == MOVE_LEFT ? -1 : 1; - } - } else { - if (parent->layout == L_VERT) { - can_move = true; - diff = direction == MOVE_UP ? -1 : 1; - } - } - sway_log(L_DEBUG, "Can move? %s", can_move ? "yes" : "no"); - if (can_move) { - int i; - for (i = 0; i < parent->children->length; ++i) { - swayc_t *child = parent->children->items[i]; - if (child == current) { - break; - } - } - int desired = i + diff; - sway_log(L_DEBUG, "Moving from %d to %d", i, desired); - if (desired < 0 || desired >= parent->children->length) { - can_move = false; - } else { - unfocus_all(&root_container); - focus_view(parent->children->items[desired]); - return true; - } - } - if (!can_move) { - sway_log(L_DEBUG, "Can't move at current level, moving up tree"); - current = parent; - parent = parent->parent; - if (!parent) { - // Nothing we can do - return false; - } - } - } -} diff --git a/sway/workspace.c b/sway/workspace.c index df646445..bc0fa2c8 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -9,6 +9,7 @@ #include "handlers.h" #include "config.h" #include "stringop.h" +#include "focus.h" swayc_t *active_workspace = NULL; @@ -173,64 +174,33 @@ void workspace_prev() { } void workspace_switch(swayc_t *workspace) { - swayc_t *ws_output = workspace->parent; - while (ws_output->type != C_OUTPUT) { - ws_output = ws_output->parent; - } - // The current workspace of the output our target workspace is in - swayc_t *focused_workspace = ws_output->focused; - if (workspace != focused_workspace && focused_workspace) { - sway_log(L_DEBUG, "workspace: changing from '%s' to '%s'", focused_workspace->name, workspace->name); - uint32_t mask = 1; - - // set all c_views in the old workspace to the invisible mask if the workspace - // is in the same output & c_views in the new workspace to the visible mask - container_map(focused_workspace, set_view_visibility, &mask); - mask = 2; - container_map(workspace, set_view_visibility, &mask); - wlc_output_set_mask(ws_output->handle, 2); - - destroy_workspace(focused_workspace); - } - unfocus_all(&root_container); - focus_view(workspace); - - // focus the output this workspace is on - swayc_t *output = workspace->parent; - sway_log(L_DEBUG, "Switching focus to output %p (%d)", output, output->type); - while (output && output->type != C_OUTPUT) { - output = output->parent; - } - if (output) { - sway_log(L_DEBUG, "Switching focus to output %p (%d)", output, output->type); - wlc_output_focus(output->handle); - } + set_focused_container(workspace); active_workspace = workspace; } /* XXX:DEBUG:XXX */ static void container_log(const swayc_t *c) { fprintf(stderr, "focus:%c|", - c == get_focused_container(&root_container) ? 'F' : //Focused + c->is_focused ? 'F' : //Focused c == active_workspace ? 'W' : //active workspace c == &root_container ? 'R' : //root 'X');//not any others fprintf(stderr,"(%p)",c); fprintf(stderr,"(p:%p)",c->parent); fprintf(stderr,"(f:%p)",c->focused); + fprintf(stderr,"(h:%ld)",c->handle); fprintf(stderr,"Type:"); fprintf(stderr, - c->type == C_ROOT ? "Root|" : - c->type == C_OUTPUT ? "Output|" : + c->type == C_ROOT ? "Root|" : + c->type == C_OUTPUT ? "Output|" : c->type == C_WORKSPACE ? "Workspace|" : c->type == C_CONTAINER ? "Container|" : - c->type == C_VIEW ? "View|" : - "Unknown|"); + c->type == C_VIEW ? "View|" : "Unknown|"); fprintf(stderr,"layout:"); fprintf(stderr, - c->layout == L_NONE ? "NONE|" : - c->layout == L_HORIZ ? "Horiz|": - c->layout == L_VERT ? "Vert|": + c->layout == L_NONE ? "NONE|" : + c->layout == L_HORIZ ? "Horiz|": + c->layout == L_VERT ? "Vert|": c->layout == L_STACKED ? "Stacked|": c->layout == L_FLOATING ? "Floating|": "Unknown|");