2015-08-18 17:28:44 +10:00
|
|
|
#include <wlc/wlc.h>
|
|
|
|
|
|
|
|
#include "focus.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "workspace.h"
|
2015-08-20 13:22:15 +10:00
|
|
|
#include "layout.h"
|
2015-10-26 22:20:32 +11:00
|
|
|
#include "config.h"
|
|
|
|
#include "input_state.h"
|
2015-11-28 01:50:04 +11:00
|
|
|
#include "ipc-server.h"
|
2016-03-29 23:47:30 +11:00
|
|
|
#include "border.h"
|
2015-08-18 17:28:44 +10:00
|
|
|
|
|
|
|
bool locked_container_focus = false;
|
2016-07-12 05:27:13 +10:00
|
|
|
bool suspend_workspace_cleanup = false;
|
2015-08-18 17:28:44 +10:00
|
|
|
|
2015-08-18 21:19:20 +10:00
|
|
|
// switches parent focus to c. will switch it accordingly
|
2015-08-18 17:28:44 +10:00
|
|
|
static void update_focus(swayc_t *c) {
|
2015-08-18 21:19:20 +10:00
|
|
|
// Handle if focus switches
|
2015-08-18 17:28:44 +10:00
|
|
|
swayc_t *parent = c->parent;
|
2015-12-02 10:09:37 +11:00
|
|
|
if (!parent) return;
|
2015-08-18 17:28:44 +10:00
|
|
|
if (parent->focused != c) {
|
2015-08-28 16:18:28 +10:00
|
|
|
// Get previous focus
|
|
|
|
swayc_t *prev = parent->focused;
|
|
|
|
// Set new focus
|
|
|
|
parent->focused = c;
|
2015-11-03 06:28:51 +11:00
|
|
|
|
2015-08-18 17:28:44 +10:00
|
|
|
switch (c->type) {
|
2016-04-03 02:00:05 +11:00
|
|
|
// Shouldn't happen
|
2015-08-18 17:28:44 +10:00
|
|
|
case C_ROOT: return;
|
2015-08-19 04:22:52 +10:00
|
|
|
|
|
|
|
// Case where output changes
|
2015-08-18 17:28:44 +10:00
|
|
|
case C_OUTPUT:
|
2015-08-18 22:20:59 +10:00
|
|
|
wlc_output_focus(c->handle);
|
2015-08-18 17:28:44 +10:00
|
|
|
break;
|
2015-08-19 04:22:52 +10:00
|
|
|
|
|
|
|
// Case where workspace changes
|
2015-08-18 17:28:44 +10:00
|
|
|
case C_WORKSPACE:
|
2015-08-28 16:18:28 +10:00
|
|
|
if (prev) {
|
2015-12-22 08:01:36 +11:00
|
|
|
ipc_event_workspace(prev, c, "focus");
|
2015-12-14 04:19:24 +11:00
|
|
|
|
|
|
|
// if the old workspace has no children, destroy it
|
2016-07-12 05:27:13 +10:00
|
|
|
if(prev->children->length == 0 && prev->floating->length == 0 && !suspend_workspace_cleanup) {
|
2015-12-14 04:19:24 +11:00
|
|
|
destroy_workspace(prev);
|
2016-01-22 08:05:03 +11:00
|
|
|
} else {
|
|
|
|
// update visibility of old workspace
|
|
|
|
update_visibility(prev);
|
2015-12-14 04:19:24 +11:00
|
|
|
}
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
2015-08-28 16:18:28 +10:00
|
|
|
// Update visibility of newly focused workspace
|
|
|
|
update_visibility(c);
|
2015-08-18 17:28:44 +10:00
|
|
|
break;
|
2015-08-19 04:22:52 +10:00
|
|
|
|
2015-08-18 20:48:41 +10:00
|
|
|
default:
|
2015-08-18 17:28:44 +10:00
|
|
|
case C_VIEW:
|
|
|
|
case C_CONTAINER:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool move_focus(enum movement_direction direction) {
|
2015-10-26 22:20:32 +11:00
|
|
|
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;
|
2016-07-28 12:06:16 +10:00
|
|
|
} else if (new_view->type == C_ROOT) {
|
2016-07-28 11:30:45 +10:00
|
|
|
sway_log(L_DEBUG, "Not setting focus above the workspace level");
|
|
|
|
return false;
|
2016-07-28 12:06:16 +10:00
|
|
|
} else if (new_view->type == C_OUTPUT) {
|
|
|
|
return set_focused_container(swayc_active_workspace_for(new_view));
|
2016-07-28 12:42:45 +10:00
|
|
|
} else if (direction == MOVE_PARENT || direction == MOVE_CHILD) {
|
2015-10-26 22:20:32 +11:00
|
|
|
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;
|
2015-08-20 13:29:24 +10:00
|
|
|
}
|
2015-10-26 22:20:32 +11:00
|
|
|
} else {
|
|
|
|
return set_focused_container(get_focused_view(new_view));
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
2015-08-20 13:22:15 +10:00
|
|
|
return false;
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *get_focused_container(swayc_t *parent) {
|
2015-08-22 03:28:37 +10:00
|
|
|
if (!parent) {
|
|
|
|
return swayc_active_workspace();
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
2016-05-24 07:05:44 +10:00
|
|
|
// get focused container
|
2015-08-22 03:28:37 +10:00
|
|
|
while (!parent->is_focused && parent->focused) {
|
|
|
|
parent = parent->focused;
|
2015-08-18 20:48:41 +10:00
|
|
|
}
|
2015-08-18 17:28:44 +10:00
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2015-10-23 23:32:17 +11:00
|
|
|
bool set_focused_container(swayc_t *c) {
|
2015-12-02 10:09:37 +11:00
|
|
|
if (locked_container_focus || !c || !c->parent) {
|
2015-10-23 23:32:17 +11:00
|
|
|
return false;
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
2015-12-14 08:59:05 +11:00
|
|
|
swayc_t *active_ws = swayc_active_workspace();
|
2015-12-18 13:33:42 +11:00
|
|
|
int active_ws_child_count = 0;
|
|
|
|
if (active_ws) {
|
|
|
|
active_ws_child_count = active_ws->children->length + active_ws->floating->length;
|
|
|
|
}
|
2015-12-14 08:59:05 +11:00
|
|
|
|
2015-08-22 03:28:37 +10:00
|
|
|
swayc_t *workspace = swayc_active_workspace_for(c);
|
2016-08-04 12:26:23 +10:00
|
|
|
swayc_t *focused = get_focused_container(&root_container);
|
2016-05-29 09:18:46 +10:00
|
|
|
|
2016-08-04 12:26:23 +10:00
|
|
|
if (swayc_is_fullscreen(get_focused_container(workspace))) {
|
2016-05-24 07:05:44 +10:00
|
|
|
// if switching to a workspace with a fullscreen view,
|
|
|
|
// focus on the fullscreen view
|
2016-08-04 12:26:23 +10:00
|
|
|
c = get_focused_container(workspace);
|
2015-08-22 03:28:37 +10:00
|
|
|
}
|
2015-08-19 04:22:52 +10:00
|
|
|
|
2016-08-04 12:26:23 +10:00
|
|
|
swayc_log(L_DEBUG, c, "Setting focus to %p:%" PRIuPTR, c, c->handle);
|
|
|
|
|
2016-07-12 23:58:24 +10:00
|
|
|
if (c->type == C_VIEW) {
|
|
|
|
// dispatch a window event
|
|
|
|
ipc_event_window(c, "focus");
|
|
|
|
}
|
2015-08-19 04:22:52 +10:00
|
|
|
// update container focus from here to root, making necessary changes along
|
|
|
|
// the way
|
2015-08-18 17:28:44 +10:00
|
|
|
swayc_t *p = c;
|
2015-08-19 18:06:15 +10:00
|
|
|
if (p->type != C_OUTPUT && p->type != C_ROOT) {
|
|
|
|
p->is_focused = true;
|
|
|
|
}
|
2015-08-18 17:28:44 +10:00
|
|
|
while (p != &root_container) {
|
|
|
|
update_focus(p);
|
|
|
|
p = p->parent;
|
|
|
|
p->is_focused = false;
|
|
|
|
}
|
2015-08-19 04:22:52 +10:00
|
|
|
|
|
|
|
// get new focused view and set focus to it.
|
2016-08-04 12:26:23 +10:00
|
|
|
if (!(wlc_view_get_type(p->handle) & WLC_BIT_POPUP)) {
|
2015-08-19 04:22:52 +10:00
|
|
|
// unactivate previous focus
|
|
|
|
if (focused->type == C_VIEW) {
|
|
|
|
wlc_view_set_state(focused->handle, WLC_BIT_ACTIVATED, false);
|
|
|
|
}
|
2016-08-01 08:10:33 +10:00
|
|
|
update_container_border(focused);
|
2015-08-19 04:22:52 +10:00
|
|
|
// activate current focus
|
2016-08-01 08:10:33 +10:00
|
|
|
if (c->type == C_VIEW) {
|
|
|
|
wlc_view_set_state(c->handle, WLC_BIT_ACTIVATED, true);
|
|
|
|
}
|
2016-08-04 12:26:23 +10:00
|
|
|
// set focus
|
2016-08-02 12:25:08 +10:00
|
|
|
wlc_view_focus(c->handle);
|
|
|
|
if (c->parent->layout != L_TABBED && c->parent->layout != L_STACKED) {
|
|
|
|
update_container_border(c);
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
2016-04-01 09:04:08 +11:00
|
|
|
|
|
|
|
// rearrange if parent container is tabbed/stacked
|
2016-08-01 08:10:33 +10:00
|
|
|
swayc_t *parent = swayc_tabbed_stacked_ancestor(c);
|
2016-04-20 08:22:15 +10:00
|
|
|
if (parent != NULL) {
|
2016-07-22 05:47:35 +10:00
|
|
|
arrange_backgrounds();
|
2016-04-20 08:22:15 +10:00
|
|
|
arrange_windows(parent, -1, -1);
|
2016-04-01 09:04:08 +11:00
|
|
|
}
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
2015-12-14 08:59:05 +11:00
|
|
|
|
|
|
|
if (active_ws != workspace) {
|
2015-12-22 08:01:36 +11:00
|
|
|
// active_ws might have been destroyed by now
|
|
|
|
// (focus swap away from empty ws = destroy ws)
|
|
|
|
if (active_ws_child_count == 0) {
|
|
|
|
active_ws = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc_event_workspace(active_ws, workspace, "focus");
|
2015-12-14 08:59:05 +11:00
|
|
|
}
|
2015-10-23 23:32:17 +11:00
|
|
|
return true;
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
|
|
|
|
2015-10-23 23:32:17 +11:00
|
|
|
bool set_focused_container_for(swayc_t *a, swayc_t *c) {
|
2015-08-18 17:28:44 +10:00
|
|
|
if (locked_container_focus || !c) {
|
2015-10-23 23:32:17 +11:00
|
|
|
return false;
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
|
|
|
swayc_t *find = c;
|
2015-08-19 04:22:52 +10:00
|
|
|
// Ensure that a is an ancestor of c
|
2015-08-18 17:28:44 +10:00
|
|
|
while (find != a && (find = find->parent)) {
|
|
|
|
if (find == &root_container) {
|
2015-10-23 23:32:17 +11:00
|
|
|
return false;
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
|
|
|
}
|
2015-08-22 03:28:37 +10:00
|
|
|
|
|
|
|
// 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) {
|
2015-10-23 23:32:17 +11:00
|
|
|
return false;
|
2015-08-22 03:28:37 +10:00
|
|
|
}
|
|
|
|
|
2016-05-24 07:05:44 +10:00
|
|
|
// Check if we are changing a parent container that will see change
|
2015-08-19 04:22:52 +10:00
|
|
|
bool effective = true;
|
|
|
|
while (find != &root_container) {
|
|
|
|
if (find->parent->focused != find) {
|
|
|
|
effective = false;
|
|
|
|
}
|
|
|
|
find = find->parent;
|
|
|
|
}
|
|
|
|
if (effective) {
|
|
|
|
// Go to set_focused_container
|
2015-10-23 23:32:17 +11:00
|
|
|
return set_focused_container(c);
|
2015-08-19 04:22:52 +10:00
|
|
|
}
|
2015-08-18 17:28:44 +10:00
|
|
|
|
2016-04-18 00:26:26 +10:00
|
|
|
sway_log(L_DEBUG, "Setting focus for %p:%" PRIuPTR " to %p:%" PRIuPTR,
|
2015-08-18 17:28:44 +10:00
|
|
|
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;
|
|
|
|
}
|
2015-10-23 23:32:17 +11:00
|
|
|
return true;
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *get_focused_view(swayc_t *parent) {
|
2015-08-26 06:29:33 +10:00
|
|
|
swayc_t *c = parent;
|
|
|
|
while (c && c->type != C_VIEW) {
|
|
|
|
if (c->type == C_WORKSPACE && c->focused == NULL) {
|
|
|
|
return c;
|
2015-08-19 04:22:52 +10:00
|
|
|
}
|
2015-08-26 06:29:33 +10:00
|
|
|
c = c->focused;
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
2015-08-26 06:29:33 +10:00
|
|
|
if (c == NULL) {
|
|
|
|
c = swayc_active_workspace_for(parent);
|
2015-08-19 04:22:52 +10:00
|
|
|
}
|
2015-08-26 06:29:33 +10:00
|
|
|
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;
|
2015-08-18 17:28:44 +10:00
|
|
|
}
|
2016-07-04 03:11:21 +10:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|