2015-08-10 10:10:26 +10:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "layout.h"
|
|
|
|
#include "movement.h"
|
|
|
|
|
2015-08-13 15:58:15 +10:00
|
|
|
bool move_focus(enum movement_direction direction) {
|
2015-08-10 10:10:26 +10:00
|
|
|
swayc_t *current = get_focused_container(&root_container);
|
|
|
|
swayc_t *parent = current->parent;
|
|
|
|
|
2015-08-11 00:06:54 +10:00
|
|
|
if (direction == MOVE_PARENT) {
|
2015-08-15 05:42:19 +10:00
|
|
|
if (parent->type == C_OUTPUT) {
|
2015-08-10 17:05:44 +10:00
|
|
|
sway_log(L_DEBUG, "Focus cannot move to parent");
|
2015-08-13 15:58:15 +10:00
|
|
|
return false;
|
2015-08-10 17:05:44 +10:00
|
|
|
} else {
|
2015-08-15 05:42:19 +10:00
|
|
|
sway_log(L_DEBUG, "Moving focus away from %p to %p", current, parent);
|
|
|
|
unfocus_all(parent->parent);
|
2015-08-11 00:06:54 +10:00
|
|
|
focus_view(parent);
|
2015-08-13 15:58:15 +10:00
|
|
|
return true;
|
2015-08-10 17:05:44 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 10:10:26 +10:00
|
|
|
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) {
|
2015-08-17 04:47:08 +10:00
|
|
|
if (parent->layout == L_HORIZ || parent->type == C_ROOT) {
|
2015-08-10 10:10:26 +10:00
|
|
|
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]);
|
2015-08-13 15:58:15 +10:00
|
|
|
return true;
|
2015-08-10 10:10:26 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!can_move) {
|
|
|
|
sway_log(L_DEBUG, "Can't move at current level, moving up tree");
|
|
|
|
current = parent;
|
|
|
|
parent = parent->parent;
|
2015-08-17 04:47:08 +10:00
|
|
|
if (!parent) {
|
2015-08-10 10:10:26 +10:00
|
|
|
// Nothing we can do
|
2015-08-13 15:58:15 +10:00
|
|
|
return false;
|
2015-08-10 10:10:26 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|