You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
279 lines
7.0 KiB
279 lines
7.0 KiB
8 years ago
|
#include "stdbool.h"
|
||
9 years ago
|
#include <wlc/wlc.h>
|
||
8 years ago
|
#include "sway/focus.h"
|
||
|
#include "sway/workspace.h"
|
||
|
#include "sway/layout.h"
|
||
|
#include "sway/config.h"
|
||
|
#include "sway/input_state.h"
|
||
|
#include "sway/ipc-server.h"
|
||
|
#include "sway/border.h"
|
||
9 years ago
|
#include "log.h"
|
||
|
|
||
|
bool locked_container_focus = false;
|
||
9 years ago
|
bool suspend_workspace_cleanup = false;
|
||
9 years ago
|
|
||
9 years ago
|
// switches parent focus to c. will switch it accordingly
|
||
9 years ago
|
static void update_focus(swayc_t *c) {
|
||
9 years ago
|
// Handle if focus switches
|
||
9 years ago
|
swayc_t *parent = c->parent;
|
||
9 years ago
|
if (!parent) return;
|
||
9 years ago
|
if (parent->focused != c) {
|
||
9 years ago
|
// Get previous focus
|
||
|
swayc_t *prev = parent->focused;
|
||
|
// Set new focus
|
||
|
parent->focused = c;
|
||
9 years ago
|
|
||
9 years ago
|
switch (c->type) {
|
||
9 years ago
|
// Shouldn't happen
|
||
9 years ago
|
case C_ROOT: return;
|
||
9 years ago
|
|
||
|
// Case where output changes
|
||
9 years ago
|
case C_OUTPUT:
|
||
9 years ago
|
wlc_output_focus(c->handle);
|
||
9 years ago
|
break;
|
||
9 years ago
|
|
||
|
// Case where workspace changes
|
||
9 years ago
|
case C_WORKSPACE:
|
||
9 years ago
|
if (prev) {
|
||
9 years ago
|
ipc_event_workspace(prev, c, "focus");
|
||
9 years ago
|
|
||
|
// if the old workspace has no children, destroy it
|
||
9 years ago
|
if(prev->children->length == 0 && prev->floating->length == 0 && !suspend_workspace_cleanup) {
|
||
9 years ago
|
destroy_workspace(prev);
|
||
9 years ago
|
} else {
|
||
|
// update visibility of old workspace
|
||
|
update_visibility(prev);
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
// Update visibility of newly focused workspace
|
||
|
update_visibility(c);
|
||
9 years ago
|
break;
|
||
9 years ago
|
|
||
9 years ago
|
default:
|
||
9 years ago
|
case C_VIEW:
|
||
|
case C_CONTAINER:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool move_focus(enum movement_direction direction) {
|
||
9 years ago
|
swayc_t *old_view = get_focused_container(&root_container);
|
||
|
swayc_t *new_view = get_swayc_in_direction(old_view, direction);
|
||
|
if (!new_view) {
|
||
|
return false;
|
||
8 years ago
|
} else if (new_view->type == C_ROOT) {
|
||
8 years ago
|
sway_log(L_DEBUG, "Not setting focus above the workspace level");
|
||
|
return false;
|
||
8 years ago
|
} else if (new_view->type == C_OUTPUT) {
|
||
|
return set_focused_container(swayc_active_workspace_for(new_view));
|
||
8 years ago
|
} else if (direction == MOVE_PARENT || direction == MOVE_CHILD) {
|
||
9 years ago
|
return set_focused_container(new_view);
|
||
|
} else if (config->mouse_warping) {
|
||
|
swayc_t *old_op = old_view->type == C_OUTPUT ?
|
||
|
old_view : swayc_parent_by_type(old_view, C_OUTPUT);
|
||
|
swayc_t *focused = get_focused_view(new_view);
|
||
|
if (set_focused_container(focused)) {
|
||
|
if (old_op != swayc_active_output() && focused && focused->type == C_VIEW) {
|
||
|
center_pointer_on(focused);
|
||
|
}
|
||
|
return true;
|
||
9 years ago
|
}
|
||
9 years ago
|
} else {
|
||
|
return set_focused_container(get_focused_view(new_view));
|
||
9 years ago
|
}
|
||
9 years ago
|
return false;
|
||
9 years ago
|
}
|
||
|
|
||
|
swayc_t *get_focused_container(swayc_t *parent) {
|
||
9 years ago
|
if (!parent) {
|
||
|
return swayc_active_workspace();
|
||
9 years ago
|
}
|
||
9 years ago
|
while (!parent->is_focused && parent->focused) {
|
||
|
parent = parent->focused;
|
||
9 years ago
|
}
|
||
9 years ago
|
return parent;
|
||
|
}
|
||
|
|
||
9 years ago
|
bool set_focused_container(swayc_t *c) {
|
||
9 years ago
|
if (locked_container_focus || !c || !c->parent) {
|
||
9 years ago
|
return false;
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
|
// current ("old") workspace for sending workspace change event later
|
||
|
swayc_t *old_ws = swayc_active_workspace();
|
||
|
// keep track of child count so we can determine if it gets destroyed
|
||
|
int old_ws_child_count = 0;
|
||
|
if (old_ws) {
|
||
|
old_ws_child_count = old_ws->children->length + old_ws->floating->length;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
// current ("old") focused container
|
||
|
swayc_t *old_focus = get_focused_container(&root_container);
|
||
|
// if old_focus is a workspace, then it's the same workspace as
|
||
|
// old_ws, and we'll need to null its pointer too, since it will
|
||
|
// be destroyed in the update_focus() call
|
||
|
bool old_focus_was_ws = (old_focus->type == C_WORKSPACE);
|
||
|
|
||
|
// workspace of new focused container
|
||
9 years ago
|
swayc_t *workspace = swayc_active_workspace_for(c);
|
||
9 years ago
|
|
||
8 years ago
|
if (swayc_is_fullscreen(get_focused_container(workspace))) {
|
||
9 years ago
|
// if switching to a workspace with a fullscreen view,
|
||
|
// focus on the fullscreen view
|
||
8 years ago
|
c = get_focused_container(workspace);
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
swayc_log(L_DEBUG, c, "Setting focus to %p:%" PRIuPTR, c, c->handle);
|
||
8 years ago
|
|
||
9 years ago
|
if (c->type == C_VIEW) {
|
||
|
// dispatch a window event
|
||
|
ipc_event_window(c, "focus");
|
||
|
}
|
||
8 years ago
|
|
||
|
// update the global pointer
|
||
|
current_focus = c;
|
||
|
|
||
9 years ago
|
// update container focus from here to root, making necessary changes along
|
||
|
// the way
|
||
9 years ago
|
swayc_t *p = c;
|
||
9 years ago
|
if (p->type != C_OUTPUT && p->type != C_ROOT) {
|
||
|
p->is_focused = true;
|
||
|
}
|
||
9 years ago
|
while (p != &root_container) {
|
||
|
update_focus(p);
|
||
|
p = p->parent;
|
||
|
p->is_focused = false;
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if (old_focus_was_ws && old_ws_child_count == 0) {
|
||
|
// this workspace was destroyed in update_focus(), so null the pointers
|
||
|
old_focus = NULL;
|
||
|
old_ws = NULL;
|
||
|
}
|
||
|
|
||
8 years ago
|
if (!(wlc_view_get_type(p->handle) & WLC_BIT_POPUP)) {
|
||
8 years ago
|
if (old_focus) {
|
||
|
if (old_focus->type == C_VIEW) {
|
||
|
wlc_view_set_state(old_focus->handle, WLC_BIT_ACTIVATED, false);
|
||
|
}
|
||
|
update_container_border(old_focus);
|
||
9 years ago
|
}
|
||
8 years ago
|
if (c->type == C_VIEW) {
|
||
|
wlc_view_set_state(c->handle, WLC_BIT_ACTIVATED, true);
|
||
|
}
|
||
7 years ago
|
/* TODO WLR
|
||
8 years ago
|
if (!desktop_shell.is_locked) {
|
||
|
// If the system is locked, we do everything _but_ actually setting
|
||
|
// focus. This includes making our internals think that this view is
|
||
|
// focused.
|
||
|
wlc_view_focus(c->handle);
|
||
|
}
|
||
7 years ago
|
*/
|
||
8 years ago
|
if (c->parent->layout != L_TABBED && c->parent->layout != L_STACKED) {
|
||
|
update_container_border(c);
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
swayc_t *parent = swayc_tabbed_stacked_ancestor(c);
|
||
9 years ago
|
if (parent != NULL) {
|
||
8 years ago
|
arrange_backgrounds();
|
||
9 years ago
|
arrange_windows(parent, -1, -1);
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if (old_ws != workspace) {
|
||
|
// old_ws might be NULL here but that's ok
|
||
|
ipc_event_workspace(old_ws, workspace, "focus");
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
9 years ago
|
return true;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
bool set_focused_container_for(swayc_t *a, swayc_t *c) {
|
||
9 years ago
|
if (locked_container_focus || !c) {
|
||
9 years ago
|
return false;
|
||
9 years ago
|
}
|
||
|
swayc_t *find = c;
|
||
|
while (find != a && (find = find->parent)) {
|
||
|
if (find == &root_container) {
|
||
9 years ago
|
return false;
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
|
||
|
// Get workspace for c, get that workspaces current focused container.
|
||
|
swayc_t *workspace = swayc_active_workspace_for(c);
|
||
|
swayc_t *focused = get_focused_view(workspace);
|
||
|
// if the workspace we are changing focus to has a fullscreen view return
|
||
|
if (swayc_is_fullscreen(focused) && c != focused) {
|
||
9 years ago
|
return false;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Check if we are changing a parent container that will see change
|
||
9 years ago
|
bool effective = true;
|
||
|
while (find != &root_container) {
|
||
|
if (find->parent->focused != find) {
|
||
|
effective = false;
|
||
|
}
|
||
|
find = find->parent;
|
||
|
}
|
||
|
if (effective) {
|
||
|
// Go to set_focused_container
|
||
9 years ago
|
return set_focused_container(c);
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
sway_log(L_DEBUG, "Setting focus for %p:%" PRIuPTR " to %p:%" PRIuPTR,
|
||
9 years ago
|
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;
|
||
|
}
|
||
9 years ago
|
return true;
|
||
9 years ago
|
}
|
||
|
|
||
|
swayc_t *get_focused_view(swayc_t *parent) {
|
||
9 years ago
|
swayc_t *c = parent;
|
||
|
while (c && c->type != C_VIEW) {
|
||
|
if (c->type == C_WORKSPACE && c->focused == NULL) {
|
||
|
return c;
|
||
9 years ago
|
}
|
||
9 years ago
|
c = c->focused;
|
||
9 years ago
|
}
|
||
9 years ago
|
if (c == NULL) {
|
||
|
c = swayc_active_workspace_for(parent);
|
||
9 years ago
|
}
|
||
9 years ago
|
return c;
|
||
|
}
|
||
|
|
||
|
swayc_t *get_focused_float(swayc_t *ws) {
|
||
|
if(!sway_assert(ws->type == C_WORKSPACE, "must be of workspace type")) {
|
||
|
ws = swayc_active_workspace();
|
||
|
}
|
||
|
if (ws->floating->length) {
|
||
|
return ws->floating->items[ws->floating->length - 1];
|
||
|
}
|
||
|
return NULL;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
swayc_t *get_focused_view_include_floating(swayc_t *parent) {
|
||
|
swayc_t *c = parent;
|
||
|
swayc_t *f = NULL;
|
||
|
|
||
|
while (c && c->type != C_VIEW) {
|
||
|
if (c->type == C_WORKSPACE && c->focused == NULL) {
|
||
|
return ((f = get_focused_float(c))) ? f : c;
|
||
|
}
|
||
|
|
||
|
c = c->focused;
|
||
|
}
|
||
|
|
||
|
if (c == NULL) {
|
||
|
c = swayc_active_workspace_for(parent);
|
||
|
}
|
||
|
|
||
|
return c;
|
||
|
}
|