*/
#include <stdlib.h>
+#include <stdbool.h>
#include "bspwm.h"
+#include "tree.h"
#include "query.h"
history_t *make_history(monitor_t *m, desktop_t *d, node_t *n)
void history_add(monitor_t *m, desktop_t *d, node_t *n)
{
- if (!record_history)
+ if (!record_history) {
return;
+ }
history_needle = NULL;
history_t *h = make_history(m, d, n);
if (history_head == NULL) {
history_head = history_tail = h;
} else if ((n != NULL && history_tail->loc.node != n) || (n == NULL && d != history_tail->loc.desktop)) {
- for (history_t *hh = history_tail; hh != NULL; hh = hh->prev)
- if ((n != NULL && hh->loc.node == n) || (n == NULL && d == hh->loc.desktop))
+ for (history_t *hh = history_tail; hh != NULL; hh = hh->prev) {
+ if ((n != NULL && hh->loc.node == n) || (n == NULL && d == hh->loc.desktop)) {
hh->latest = false;
+ }
+ }
history_tail->next = h;
h->prev = history_tail;
history_tail = h;
void history_transfer_node(monitor_t *m, desktop_t *d, node_t *n)
{
- for (history_t *h = history_head; h != NULL; h = h->next)
- if (h->loc.node == n) {
+ for (history_t *h = history_head; h != NULL; h = h->next) {
+ if (is_descendant(h->loc.node, n)) {
h->loc.monitor = m;
h->loc.desktop = d;
}
+ }
}
void history_transfer_desktop(monitor_t *m, desktop_t *d)
{
- for (history_t *h = history_head; h != NULL; h = h->next)
- if (h->loc.desktop == d)
+ for (history_t *h = history_head; h != NULL; h = h->next) {
+ if (h->loc.desktop == d) {
h->loc.monitor = m;
+ }
+ }
}
void history_swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
{
- for (history_t *h = history_head; h != NULL; h = h->next)
- if (h->loc.node == n1) {
+ for (history_t *h = history_head; h != NULL; h = h->next) {
+ if (is_descendant(h->loc.node, n1)) {
h->loc.monitor = m2;
h->loc.desktop = d2;
- } else if (h->loc.node == n2) {
+ } else if (is_descendant(h->loc.node, n2)) {
h->loc.monitor = m1;
h->loc.desktop = d1;
}
+ }
}
void history_swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
{
- for (history_t *h = history_head; h != NULL; h = h->next)
- if (h->loc.desktop == d1)
+ for (history_t *h = history_head; h != NULL; h = h->next) {
+ if (h->loc.desktop == d1) {
h->loc.monitor = m2;
- else if (h->loc.desktop == d2)
+ } else if (h->loc.desktop == d2) {
h->loc.monitor = m1;
+ }
+ }
}
-void history_remove(desktop_t *d, node_t *n)
+void history_remove(desktop_t *d, node_t *n, bool deep)
{
- /* removing from the newest to the oldest is required */
- /* for maintaining the *latest* attribute */
+ /* removing from the newest to the oldest is required */
+ /* for maintaining the *latest* attribute */
history_t *b = history_tail;
while (b != NULL) {
- if ((n != NULL && n == b->loc.node) || (n == NULL && d == b->loc.desktop)) {
+ if ((n != NULL && ((deep && is_descendant(b->loc.node, n)) || (!deep && b->loc.node == n))) ||
+ (n == NULL && d == b->loc.desktop)) {
history_t *a = b->next;
history_t *c = b->prev;
if (a != NULL) {
while (c != NULL && ((a->loc.node != NULL && a->loc.node == c->loc.node) ||
(a->loc.node == NULL && a->loc.desktop == c->loc.desktop))) {
history_t *p = c->prev;
- if (history_head == c)
+ if (history_head == c) {
history_head = history_tail;
- if (history_needle == c)
+ }
+ if (history_needle == c) {
history_needle = history_tail;
+ }
free(c);
c = p;
}
a->prev = c;
}
- if (c != NULL)
+ if (c != NULL) {
c->next = a;
- if (history_tail == b)
+ }
+ if (history_tail == b) {
history_tail = c;
- if (history_head == b)
+ }
+ if (history_head == b) {
history_head = a;
- if (history_needle == b)
+ }
+ if (history_needle == b) {
history_needle = c;
+ }
free(b);
b = c;
} else {
history_head = history_tail = NULL;
}
-node_t *history_get_node(desktop_t *d, node_t *n)
+node_t *history_last_node(desktop_t *d, node_t *n)
{
- for (history_t *h = history_tail; h != NULL; h = h->prev)
- if (h->latest && h->loc.node != NULL && h->loc.node != n && h->loc.desktop == d)
+ for (history_t *h = history_tail; h != NULL; h = h->prev) {
+ if (h->latest && h->loc.node != NULL && !h->loc.node->hidden &&
+ !is_descendant(h->loc.node, n) && h->loc.desktop == d) {
return h->loc.node;
+ }
+ }
return NULL;
}
-desktop_t *history_get_desktop(monitor_t *m, desktop_t *d)
+desktop_t *history_last_desktop(monitor_t *m, desktop_t *d)
{
- for (history_t *h = history_tail; h != NULL; h = h->prev)
- if (h->latest && h->loc.desktop != d && h->loc.monitor == m)
+ for (history_t *h = history_tail; h != NULL; h = h->prev) {
+ if (h->latest && h->loc.desktop != d && h->loc.monitor == m) {
return h->loc.desktop;
+ }
+ }
return NULL;
}
-monitor_t *history_get_monitor(monitor_t *m)
+monitor_t *history_last_monitor(monitor_t *m)
{
- for (history_t *h = history_tail; h != NULL; h = h->prev)
- if (h->latest && h->loc.monitor != m)
+ for (history_t *h = history_tail; h != NULL; h = h->prev) {
+ if (h->latest && h->loc.monitor != m) {
return h->loc.monitor;
+ }
+ }
return NULL;
}
-bool history_find_node(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, client_select_t sel)
+bool history_find_node(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, node_select_t sel)
{
- if (history_needle == NULL || record_history)
+ if (history_needle == NULL || record_history) {
history_needle = history_tail;
+ }
history_t *h;
for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
if (!h->latest ||
h->loc.node == NULL ||
h->loc.node == ref->node ||
- !node_matches(&h->loc, ref, sel))
+ h->loc.node->hidden ||
+ !node_matches(&h->loc, ref, sel)) {
continue;
- if (!record_history)
+ }
+ if (!record_history) {
history_needle = h;
+ }
*dst = h->loc;
return true;
}
bool history_find_desktop(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, desktop_select_t sel)
{
- if (history_needle == NULL || record_history)
+ if (history_needle == NULL || record_history) {
history_needle = history_tail;
+ }
history_t *h;
for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
if (!h->latest ||
h->loc.desktop == ref->desktop ||
- !desktop_matches(&h->loc, ref, sel))
+ !desktop_matches(&h->loc, ref, sel)) {
continue;
- if (!record_history)
+ }
+ if (!record_history) {
history_needle = h;
+ }
*dst = h->loc;
return true;
}
return false;
}
-bool history_find_monitor(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, desktop_select_t sel)
+bool history_find_monitor(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, monitor_select_t sel)
{
- if (history_needle == NULL || record_history)
+ if (history_needle == NULL || record_history) {
history_needle = history_tail;
+ }
history_t *h;
+
for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
if (!h->latest ||
h->loc.monitor == ref->monitor ||
- !desktop_matches(&h->loc, ref, sel))
+ !monitor_matches(&h->loc, ref, sel)) {
continue;
- if (!record_history)
+ }
+ if (!record_history) {
history_needle = h;
+ }
*dst = h->loc;
return true;
}
+
return false;
}
-int history_rank(desktop_t *d, node_t *n)
+uint32_t history_rank(node_t *n)
{
- int i = 0;
+ uint32_t r = 0;
history_t *h = history_tail;
- while (h != NULL && (!h->latest || h->loc.node != n || h->loc.desktop != d)) {
+ while (h != NULL && (!h->latest || h->loc.node != n)) {
h = h->prev;
- i++;
+ r++;
+ }
+ if (h == NULL) {
+ return UINT32_MAX;
+ } else {
+ return r;
}
- if (h == NULL)
- return -1;
- else
- return i;
}