2015-08-18 09:28:44 +02:00
|
|
|
#include <wlc/wlc.h>
|
|
|
|
|
|
|
|
#include "focus.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "workspace.h"
|
2015-08-20 05:22:15 +02:00
|
|
|
#include "layout.h"
|
2015-08-18 09:28:44 +02:00
|
|
|
|
|
|
|
bool locked_container_focus = false;
|
|
|
|
bool locked_view_focus = false;
|
|
|
|
|
2015-08-18 13:19:20 +02:00
|
|
|
// switches parent focus to c. will switch it accordingly
|
|
|
|
// TODO: Everything needs a handle, so we can set front/back position properly
|
2015-08-18 09:28:44 +02:00
|
|
|
static void update_focus(swayc_t *c) {
|
2015-08-18 13:19:20 +02:00
|
|
|
// Handle if focus switches
|
2015-08-18 09:28:44 +02:00
|
|
|
swayc_t *parent = c->parent;
|
|
|
|
if (parent->focused != c) {
|
|
|
|
switch (c->type) {
|
2015-08-18 20:22:52 +02:00
|
|
|
// Shouldnt happen
|
2015-08-18 09:28:44 +02:00
|
|
|
case C_ROOT: return;
|
2015-08-18 20:22:52 +02:00
|
|
|
|
|
|
|
// Case where output changes
|
2015-08-18 09:28:44 +02:00
|
|
|
case C_OUTPUT:
|
2015-08-18 14:20:59 +02:00
|
|
|
wlc_output_focus(c->handle);
|
2015-08-20 00:22:55 +02:00
|
|
|
//Set new workspace to the outputs focused workspace
|
|
|
|
active_workspace = c->focused;
|
2015-08-18 09:28:44 +02:00
|
|
|
break;
|
2015-08-18 20:22:52 +02:00
|
|
|
|
|
|
|
// Case where workspace changes
|
2015-08-18 09:28:44 +02:00
|
|
|
case C_WORKSPACE:
|
|
|
|
if (parent->focused) {
|
|
|
|
swayc_t *ws = parent->focused;
|
2015-08-18 13:19:20 +02:00
|
|
|
// hide visibility of old workspace
|
2015-08-18 09:28:44 +02:00
|
|
|
uint32_t mask = 1;
|
|
|
|
container_map(ws, set_view_visibility, &mask);
|
2015-08-18 13:19:20 +02:00
|
|
|
// set visibility of new workspace
|
2015-08-18 09:28:44 +02:00
|
|
|
mask = 2;
|
|
|
|
container_map(c, set_view_visibility, &mask);
|
|
|
|
wlc_output_set_mask(parent->handle, 2);
|
2015-08-18 20:22:52 +02:00
|
|
|
c->parent->focused = c;
|
2015-08-18 09:28:44 +02:00
|
|
|
destroy_workspace(ws);
|
|
|
|
}
|
|
|
|
active_workspace = c;
|
|
|
|
break;
|
2015-08-18 20:22:52 +02:00
|
|
|
|
2015-08-18 12:48:41 +02:00
|
|
|
default:
|
2015-08-18 09:28:44 +02:00
|
|
|
case C_VIEW:
|
|
|
|
case C_CONTAINER:
|
2015-08-18 13:19:20 +02:00
|
|
|
// TODO whatever to do when container changes
|
|
|
|
// for example, stacked and tabbing change stuff.
|
2015-08-18 09:28:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->parent->focused = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool move_focus(enum movement_direction direction) {
|
2015-08-20 05:22:15 +02:00
|
|
|
swayc_t *view = get_swayc_in_direction(
|
|
|
|
get_focused_container(&root_container), direction);
|
|
|
|
if (view) {
|
2015-08-20 05:28:05 +02:00
|
|
|
set_focused_container(get_focused_view(view));
|
2015-08-20 05:22:15 +02:00
|
|
|
return true;
|
2015-08-18 09:28:44 +02:00
|
|
|
}
|
2015-08-20 05:22:15 +02:00
|
|
|
return false;
|
2015-08-18 09:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *get_focused_container(swayc_t *parent) {
|
|
|
|
while (parent && !parent->is_focused) {
|
|
|
|
parent = parent->focused;
|
|
|
|
}
|
2015-08-18 13:19:20 +02:00
|
|
|
// just incase
|
2015-08-18 12:48:41 +02:00
|
|
|
if (parent == NULL) {
|
|
|
|
sway_log(L_DEBUG, "get_focused_container unable to find container");
|
|
|
|
return active_workspace;
|
|
|
|
}
|
2015-08-18 09:28:44 +02:00
|
|
|
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);
|
2015-08-18 20:22:52 +02:00
|
|
|
|
|
|
|
// Find previous focused view, and the new focused view, if they are the same return
|
|
|
|
swayc_t *focused = get_focused_view(&root_container);
|
|
|
|
swayc_t *workspace = active_workspace;
|
|
|
|
|
|
|
|
// update container focus from here to root, making necessary changes along
|
|
|
|
// the way
|
2015-08-18 09:28:44 +02:00
|
|
|
swayc_t *p = c;
|
2015-08-19 10:06:15 +02:00
|
|
|
if (p->type != C_OUTPUT && p->type != C_ROOT) {
|
|
|
|
p->is_focused = true;
|
|
|
|
}
|
2015-08-18 09:28:44 +02:00
|
|
|
while (p != &root_container) {
|
|
|
|
update_focus(p);
|
|
|
|
p = p->parent;
|
|
|
|
p->is_focused = false;
|
|
|
|
}
|
2015-08-18 20:22:52 +02:00
|
|
|
|
|
|
|
// if the workspace is the same, and previous focus is fullscreen, dont
|
|
|
|
// change focus
|
|
|
|
if (workspace == active_workspace
|
|
|
|
&& wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get new focused view and set focus to it.
|
|
|
|
p = get_focused_view(c);
|
|
|
|
if (p->type == C_VIEW && !(wlc_view_get_type(p->handle) & WLC_BIT_POPUP)) {
|
|
|
|
// unactivate previous focus
|
|
|
|
if (focused->type == C_VIEW) {
|
|
|
|
wlc_view_set_state(focused->handle, WLC_BIT_ACTIVATED, false);
|
|
|
|
}
|
|
|
|
// activate current focus
|
|
|
|
if (p->type == C_VIEW) {
|
2015-08-18 09:28:44 +02:00
|
|
|
wlc_view_set_state(p->handle, WLC_BIT_ACTIVATED, true);
|
2015-08-19 08:52:42 +02:00
|
|
|
//set focus if view_focus is unlocked
|
|
|
|
if (!locked_view_focus) {
|
|
|
|
wlc_view_focus(p->handle);
|
|
|
|
}
|
2015-08-18 09:28:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_focused_container_for(swayc_t *a, swayc_t *c) {
|
|
|
|
if (locked_container_focus || !c) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
swayc_t *find = c;
|
2015-08-18 20:22:52 +02:00
|
|
|
// Ensure that a is an ancestor of c
|
2015-08-18 09:28:44 +02:00
|
|
|
while (find != a && (find = find->parent)) {
|
|
|
|
if (find == &root_container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 20:22:52 +02:00
|
|
|
// Check if we changing a parent container that will see chnage
|
|
|
|
bool effective = true;
|
|
|
|
while (find != &root_container) {
|
|
|
|
if (find->parent->focused != find) {
|
|
|
|
effective = false;
|
|
|
|
}
|
|
|
|
find = find->parent;
|
|
|
|
}
|
|
|
|
if (effective) {
|
|
|
|
// Go to set_focused_container
|
|
|
|
set_focused_container(c);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-18 09:28:44 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *get_focused_view(swayc_t *parent) {
|
|
|
|
while (parent && parent->type != C_VIEW) {
|
2015-08-18 20:22:52 +02:00
|
|
|
if (parent->type == C_WORKSPACE && parent->focused == NULL) {
|
|
|
|
return parent;
|
|
|
|
}
|
2015-08-18 09:28:44 +02:00
|
|
|
parent = parent->focused;
|
|
|
|
}
|
2015-08-18 20:22:52 +02:00
|
|
|
if (parent == NULL) {
|
|
|
|
return active_workspace;
|
|
|
|
}
|
2015-08-18 09:28:44 +02:00
|
|
|
return parent;
|
|
|
|
}
|