bspwm/types.c

330 lines
7.7 KiB
C
Raw Normal View History

2012-08-20 22:38:29 +02:00
#include <stdlib.h>
#include <string.h>
2012-08-20 22:38:29 +02:00
#include <xcb/xcb.h>
#include <xcb/xcb_event.h>
2012-10-17 16:18:40 +02:00
#include "bspwm.h"
#include "window.h"
2013-06-01 12:00:26 +02:00
#include "rules.h"
#include "ewmh.h"
#include "settings.h"
2012-08-18 11:18:19 +02:00
#include "types.h"
2012-12-26 14:39:46 +01:00
#include "tree.h"
2012-08-18 11:18:19 +02:00
2012-08-29 18:37:31 +02:00
node_t *make_node(void)
2012-08-18 11:18:19 +02:00
{
2012-08-29 18:37:31 +02:00
node_t *n = malloc(sizeof(node_t));
2012-09-04 11:14:01 +02:00
n->parent = n->first_child = n->second_child = NULL;
2013-04-03 12:19:01 +02:00
n->split_ratio = split_ratio;
n->split_type = TYPE_VERTICAL;
n->birth_rotation = ROTATE_IDENTITY;
2012-09-04 11:14:01 +02:00
n->client = NULL;
n->vacant = false;
2012-08-18 11:18:19 +02:00
return n;
}
2012-10-17 16:18:40 +02:00
monitor_t *make_monitor(xcb_rectangle_t *rect)
{
monitor_t *m = malloc(sizeof(monitor_t));
snprintf(m->name, sizeof(m->name), "%s%02d", DEFAULT_MON_NAME, ++monitor_uid);
m->prev = m->next = NULL;
m->desk = m->last_desk = NULL;
if (rect != NULL)
m->rectangle = *rect;
else
warn("no rectangle was given for monitor '%s'\n", m->name);
2012-12-09 12:01:45 +01:00
m->top_padding = m->right_padding = m->bottom_padding = m->left_padding = 0;
2013-05-28 17:31:35 +02:00
m->wired = true;
2012-10-17 16:18:40 +02:00
return m;
}
monitor_t *find_monitor(char *name)
{
for (monitor_t *m = mon_head; m != NULL; m = m->next)
if (strcmp(m->name, name) == 0)
return m;
return NULL;
}
2013-05-28 17:31:35 +02:00
monitor_t *get_monitor_by_id(xcb_randr_output_t id)
{
for (monitor_t *m = mon_head; m != NULL; m = m->next)
if (m->id == id)
return m;
return NULL;
}
monitor_t *add_monitor(xcb_rectangle_t *rect)
{
monitor_t *m = make_monitor(rect);
if (mon == NULL) {
mon = m;
mon_head = m;
mon_tail = m;
} else {
mon_tail->next = m;
m->prev = mon_tail;
mon_tail = m;
}
num_monitors++;
2013-05-28 17:31:35 +02:00
return m;
}
2012-12-26 14:39:46 +01:00
void remove_monitor(monitor_t *m)
{
while (m->desk_head != NULL)
remove_desktop(m, m->desk_head);
monitor_t *prev = m->prev;
monitor_t *next = m->next;
if (prev != NULL)
prev->next = next;
if (next != NULL)
next->prev = prev;
2013-05-29 10:37:52 +02:00
if (mon_head == m)
2012-12-26 14:39:46 +01:00
mon_head = next;
2013-05-29 10:37:52 +02:00
if (mon_tail == m)
2012-12-26 14:39:46 +01:00
mon_tail = prev;
2013-05-29 10:37:52 +02:00
if (last_mon == m)
last_mon = NULL;
2013-05-29 10:37:52 +02:00
if (mon == m)
mon = (last_mon == NULL ? mon_head : last_mon);
2012-12-26 15:22:36 +01:00
free(m);
2012-12-27 22:36:06 +01:00
num_monitors--;
2012-12-26 14:39:46 +01:00
}
void transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d)
2013-05-28 17:31:35 +02:00
{
2013-06-07 11:17:41 +02:00
desktop_t *dd = ms->desk;
unlink_desktop(ms, d);
insert_desktop(md, d);
2013-06-07 11:17:41 +02:00
if (d == dd) {
desktop_show(ms->desk);
if (md->desk != d)
desktop_hide(d);
}
for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
fit_monitor(md, n->client);
arrange(md, d);
if (d != dd && md->desk == d) {
desktop_show(d);
}
put_status();
ewmh_update_desktop_names();
}
void merge_monitors(monitor_t *ms, monitor_t *md)
{
PRINTF("merge %s into %s\n", ms->name, md->name);
desktop_t *d = ms->desk_head;
while (d != NULL) {
desktop_t *next = d->next;
2013-05-31 21:24:39 +02:00
transfer_desktop(ms, md, d);
d = next;
}
2013-05-28 17:31:35 +02:00
}
desktop_t *make_desktop(const char *name)
2012-08-18 11:18:19 +02:00
{
2012-08-29 18:37:31 +02:00
desktop_t *d = malloc(sizeof(desktop_t));
2012-10-17 16:18:40 +02:00
if (name == NULL)
snprintf(d->name, sizeof(d->name), "%s%02d", DEFAULT_DESK_NAME, ++desktop_uid);
else
strncpy(d->name, name, sizeof(d->name));
2012-08-25 15:24:35 +02:00
d->layout = LAYOUT_TILED;
2012-08-23 15:32:20 +02:00
d->prev = d->next = NULL;
2012-11-04 12:27:39 +01:00
d->root = d->focus = NULL;
d->history = make_focus_history();
2012-08-18 11:18:19 +02:00
return d;
}
2012-08-25 15:24:35 +02:00
void insert_desktop(monitor_t *m, desktop_t *d)
{
if (m->desk == NULL) {
m->desk = d;
m->desk_head = d;
m->desk_tail = d;
} else {
m->desk_tail->next = d;
d->prev = m->desk_tail;
m->desk_tail = d;
}
}
void add_desktop(monitor_t *m, desktop_t *d)
{
2013-05-31 21:24:39 +02:00
PRINTF("add desktop %s\n", d->name);
insert_desktop(m, d);
num_desktops++;
ewmh_update_number_of_desktops();
ewmh_update_desktop_names();
put_status();
}
2012-12-27 22:36:06 +01:00
void empty_desktop(desktop_t *d)
{
destroy_tree(d->root);
d->root = d->focus = NULL;
empty_history(d->history);
2012-12-27 22:36:06 +01:00
}
void unlink_desktop(monitor_t *m, desktop_t *d)
2012-12-26 14:39:46 +01:00
{
desktop_t *prev = d->prev;
desktop_t *next = d->next;
if (prev != NULL)
prev->next = next;
if (next != NULL)
next->prev = prev;
if (m->desk_head == d)
2012-12-26 14:39:46 +01:00
m->desk_head = next;
if (m->desk_tail == d)
2012-12-26 14:39:46 +01:00
m->desk_tail = prev;
if (m->last_desk == d)
m->last_desk = NULL;
if (m->desk == d)
m->desk = (m->last_desk == NULL ? (prev == NULL ? next : prev) : m->last_desk);
d->prev = d->next = NULL;
}
void remove_desktop(monitor_t *m, desktop_t *d)
{
2013-05-31 21:24:39 +02:00
PRINTF("remove desktop %s\n", d->name);
2013-06-01 12:00:26 +02:00
prune_rules(d);
unlink_desktop(m, d);
empty_desktop(d);
2012-12-26 15:22:36 +01:00
free(d);
2012-12-27 22:36:06 +01:00
num_desktops--;
ewmh_update_number_of_desktops();
ewmh_update_desktop_names();
put_status();
2012-12-26 14:39:46 +01:00
}
2012-09-05 14:07:06 +02:00
client_t *make_client(xcb_window_t win)
2012-08-25 15:24:35 +02:00
{
2012-08-29 18:37:31 +02:00
client_t *c = malloc(sizeof(client_t));
strncpy(c->class_name, MISSING_VALUE, sizeof(c->class_name));
c->uid = ++client_uid;
c->border_width = border_width;
2012-09-05 14:07:06 +02:00
c->window = win;
c->floating = c->transient = c->fullscreen = c->locked = c->urgent = false;
2012-08-25 15:24:35 +02:00
return c;
}
2012-09-10 13:47:51 +02:00
rule_t *make_rule(void)
{
rule_t *r = malloc(sizeof(rule_t));
2012-12-25 19:03:35 +01:00
r->uid = ++rule_uid;
2012-09-18 17:21:04 +02:00
r->effect.floating = false;
r->effect.follow = false;
r->effect.monitor = NULL;
r->effect.desktop = NULL;
r->prev = NULL;
2012-09-18 17:21:04 +02:00
r->next = NULL;
2012-09-10 13:47:51 +02:00
return r;
}
pointer_state_t *make_pointer_state(void)
{
pointer_state_t *p = malloc(sizeof(pointer_state_t));
p->monitor = NULL;
p->desktop = NULL;
p->node = p->vertical_fence = p->horizontal_fence = NULL;
p->client = NULL;
p->window = XCB_NONE;
return p;
}
2012-11-04 12:27:39 +01:00
focus_history_t *make_focus_history(void)
{
focus_history_t *f = malloc(sizeof(focus_history_t));
f->head = f->tail = NULL;
return f;
}
node_list_t *make_node_list(void)
{
node_list_t *n = malloc(sizeof(node_list_t));
n->node = NULL;
n->prev = n->next = NULL;
2013-05-06 18:31:53 +02:00
n->latest = true;
return n;
}
2012-11-04 12:27:39 +01:00
void history_add(focus_history_t *f, node_t *n)
{
node_list_t *a = make_node_list();
a->node = n;
if (f->head == NULL) {
f->head = f->tail = a;
} else if (f->head->node != n) {
2013-05-07 14:21:15 +02:00
for (node_list_t *b = f->head; b != NULL; b = b->next)
if (b->node == n)
b->latest = false;
f->head->prev = a;
a->next = f->head;
f->head = a;
2012-11-04 12:27:39 +01:00
} else {
free(a);
2012-11-04 12:27:39 +01:00
}
}
void history_remove(focus_history_t *f, node_t *n)
{
/* in order to maintain the `latest` node list state,
we remove node lists from head to tail */
node_list_t *b = f->head;
while (b != NULL) {
2012-11-04 12:27:39 +01:00
if (b->node == n) {
node_list_t *a = b->prev;
node_list_t *c = b->next;
if (a != NULL) {
/* remove duplicate entries */
2013-05-07 14:15:15 +02:00
while (c != NULL && c->node == a->node) {
node_list_t *d = c->next;
2013-03-03 13:10:52 +01:00
if (f->tail == c)
2013-05-07 14:15:15 +02:00
f->tail = f->head;
free(c);
c = d;
}
2012-11-04 12:27:39 +01:00
a->next = c;
}
2012-11-04 12:27:39 +01:00
if (c != NULL)
c->prev = a;
if (f->head == b)
f->head = c;
if (f->tail == b)
f->tail = a;
2012-11-04 12:27:39 +01:00
free(b);
b = c;
} else {
b = b->next;
2012-11-04 12:27:39 +01:00
}
}
}
void empty_history(focus_history_t *f)
{
node_list_t *a = f->head;
while (a != NULL) {
node_list_t *b = a->next;
free(a);
a = b;
}
f->head = f->tail = NULL;
}
node_t *history_get(focus_history_t *f, int i)
{
node_list_t *a = f->head;
while (a != NULL && i > 0) {
a = a->next;
i--;
}
if (a == NULL)
return NULL;
else
return a->node;
2012-11-04 12:27:39 +01:00
}