Skip to content

Commit

Permalink
Split types.c into {monitor,desktop,history}.c
Browse files Browse the repository at this point in the history
  • Loading branch information
baskerville committed Sep 19, 2013
1 parent ff03ce7 commit d168e91
Show file tree
Hide file tree
Showing 23 changed files with 734 additions and 690 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ MANPREFIX = $(PREFIX)/share/man
BASHCPL = $(PREFIX)/share/bash-completion/completions
ZSHCPL = $(PREFIX)/share/zsh/site-functions

WM_SRC = bspwm.c helpers.c settings.c types.c tree.c events.c window.c messages.c query.c restore.c rules.c ewmh.c
WM_SRC = bspwm.c helpers.c settings.c monitor.c desktop.c tree.c history.c \
events.c window.c messages.c query.c restore.c rules.c ewmh.c
WM_OBJ = $(WM_SRC:.c=.o)
CL_SRC = bspc.c helpers.c
CL_OBJ = $(CL_SRC:.c=.o)
Expand Down
16 changes: 9 additions & 7 deletions Sourcedeps
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
bspc.o: bspc.c common.h helpers.h
bspwm.o: bspwm.c bspwm.h common.h events.h ewmh.h helpers.h messages.h rules.h settings.h tree.h types.h window.h
events.o: events.c bspwm.h events.h ewmh.h helpers.h query.h rules.h settings.h tree.h types.h window.h
bspwm.o: bspwm.c bspwm.h common.h desktop.h events.h ewmh.h helpers.h messages.h monitor.h rules.h settings.h tree.h types.h window.h
desktop.o: desktop.c bspwm.h desktop.h ewmh.h helpers.h history.h tree.h types.h window.h
events.o: events.c bspwm.h events.h ewmh.h helpers.h monitor.h query.h rules.h settings.h tree.h types.h window.h
ewmh.o: ewmh.c bspwm.h ewmh.h helpers.h settings.h tree.h types.h
helpers.o: helpers.c bspwm.h helpers.h types.h
messages.o: messages.c bspwm.h common.h events.h ewmh.h helpers.h messages.h query.h restore.h rules.h settings.h tree.h types.h window.h
query.o: query.c bspwm.h helpers.h messages.h query.h settings.h tree.h types.h
restore.o: restore.c bspwm.h ewmh.h helpers.h query.h restore.h settings.h tree.h types.h
history.o: history.c helpers.h history.h query.h types.h
messages.o: messages.c bspwm.h common.h desktop.h events.h ewmh.h helpers.h messages.h monitor.h query.h restore.h rules.h settings.h tree.h types.h window.h
monitor.o: monitor.c bspwm.h desktop.h ewmh.h helpers.h monitor.h tree.h types.h window.h
query.o: query.c bspwm.h helpers.h history.h messages.h query.h settings.h tree.h types.h
restore.o: restore.c bspwm.h desktop.h ewmh.h helpers.h history.h monitor.h query.h restore.h settings.h tree.h types.h
rules.o: rules.c bspwm.h ewmh.h helpers.h query.h rules.h types.h window.h
settings.o: settings.c bspwm.h common.h helpers.h settings.h types.h
tree.o: tree.c bspwm.h ewmh.h helpers.h settings.h tree.h types.h window.h
types.o: types.c bspwm.h ewmh.h helpers.h rules.h settings.h tree.h types.h window.h
tree.o: tree.c bspwm.h desktop.h ewmh.h helpers.h history.h query.h settings.h tree.h types.h window.h
window.o: window.c bspwm.h ewmh.h helpers.h query.h rules.h settings.h tree.h types.h window.h
89 changes: 2 additions & 87 deletions bspwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <xcb/xcb_ewmh.h>
#include <xcb/randr.h>
#include "types.h"
#include "desktop.h"
#include "monitor.h"
#include "settings.h"
#include "messages.h"
#include "rules.h"
Expand Down Expand Up @@ -258,93 +260,6 @@ void register_events(void)
}
}

bool import_monitors(void)
{
PUTS("import monitors");
xcb_randr_get_screen_resources_current_reply_t *sres = xcb_randr_get_screen_resources_current_reply(dpy, xcb_randr_get_screen_resources_current(dpy, root), NULL);
if (sres == NULL)
return false;

int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);

xcb_randr_get_output_info_cookie_t cookies[len];
for (int i = 0; i < len; i++)
cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);

for (monitor_t *m = mon_head; m != NULL; m = m->next)
m->wired = false;

monitor_t *mm = NULL;
unsigned int num = 0;

for (int i = 0; i < len; i++) {
xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
if (info != NULL && info->crtc != XCB_NONE) {

xcb_randr_get_crtc_info_reply_t *cir = xcb_randr_get_crtc_info_reply(dpy, xcb_randr_get_crtc_info(dpy, info->crtc, XCB_CURRENT_TIME), NULL);
if (cir != NULL) {
xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
mm = get_monitor_by_id(outputs[i]);
if (mm != NULL) {
mm->rectangle = rect;
for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
fit_monitor(mm, n->client);
arrange(mm, mm->desk);
mm->wired = true;
PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
} else {
mm = add_monitor(rect);
char *name = (char *)xcb_randr_get_output_info_name(info);
size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
strncpy(mm->name, name, name_len);
mm->name[name_len] = '\0';
mm->id = outputs[i];
PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
}
num++;
}
free(cir);
}
free(info);
}

/* initially focus the primary monitor and add the first desktop to it */
xcb_randr_get_output_primary_reply_t *gpo = xcb_randr_get_output_primary_reply(dpy, xcb_randr_get_output_primary(dpy, root), NULL);
if (gpo != NULL) {
pri_mon = get_monitor_by_id(gpo->output);
if (!running && pri_mon != NULL) {
if (mon != pri_mon)
mon = pri_mon;
add_desktop(pri_mon, make_desktop(NULL));
ewmh_update_current_desktop();
}
}
free(gpo);

/* add one desktop to each new monitor */
for (monitor_t *m = mon_head; m != NULL; m = m->next)
if (m->desk == NULL && (running || pri_mon == NULL || m != pri_mon))
add_desktop(m, make_desktop(NULL));

/* merge and remove disconnected monitors */
monitor_t *m = mon_head;
while (m != NULL) {
monitor_t *next = m->next;
if (!m->wired) {
PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
merge_monitors(m, mm);
remove_monitor(m);
}
m = next;
}

free(sres);
update_motion_recorder();
return (num_monitors > 0);
}

void quit(void)
{
running = false;
Expand Down
3 changes: 1 addition & 2 deletions bspwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ bool auto_raise;
bool running;
bool randr;

void register_events(void);
bool import_monitors(void);
void init(void);
void setup(void);
void register_events(void);
void put_status(void);
void cleanup(void);
void quit(void);
Expand Down
171 changes: 171 additions & 0 deletions desktop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#include <stdlib.h>
#include <string.h>
#include "bspwm.h"
#include "types.h"
#include "desktop.h"
#include "history.h"
#include "tree.h"
#include "window.h"
#include "ewmh.h"

void transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d)
{
if (ms == md)
return;

desktop_t *dd = ms->desk;
unlink_desktop(ms, d);
insert_desktop(md, d);

if (d == dd) {
if (ms->desk != NULL)
show_desktop(ms->desk);
if (md->desk != d)
hide_desktop(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) {
show_desktop(d);
}

ewmh_update_wm_desktops();
ewmh_update_desktop_names();
ewmh_update_current_desktop();
put_status();
}

desktop_t *make_desktop(const char *name)
{
desktop_t *d = malloc(sizeof(desktop_t));
if (name == NULL)
snprintf(d->name, sizeof(d->name), "%s%02d", DEFAULT_DESK_NAME, ++desktop_uid);
else
strncpy(d->name, name, sizeof(d->name));
d->layout = LAYOUT_TILED;
d->prev = d->next = NULL;
d->root = d->focus = NULL;
d->history = make_focus_history();
d->window_gap = WINDOW_GAP;
return d;
}

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)
{
PRINTF("add desktop %s\n", d->name);

insert_desktop(m, d);
num_desktops++;
ewmh_update_number_of_desktops();
ewmh_update_desktop_names();
put_status();
}

void empty_desktop(desktop_t *d)
{
destroy_tree(d->root);
d->root = d->focus = NULL;
empty_history(d->history);
}

void unlink_desktop(monitor_t *m, desktop_t *d)
{
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)
m->desk_head = next;
if (m->desk_tail == d)
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)
{
PRINTF("remove desktop %s\n", d->name);

unlink_desktop(m, d);
empty_desktop(d);
free(d);
num_desktops--;
ewmh_update_number_of_desktops();
ewmh_update_desktop_names();
put_status();
}

void swap_desktops(monitor_t *m, desktop_t *d1, desktop_t *d2)
{
if (d1 == NULL || d2 == NULL || d1 == d2)
return;

if (m->desk_head == d1)
m->desk_head = d2;
else if (m->desk_head == d2)
m->desk_head = d1;
if (m->desk_tail == d1)
m->desk_tail = d2;
else if (m->desk_tail == d2)
m->desk_tail = d1;

desktop_t *p1 = d1->prev;
desktop_t *n1 = d1->next;
desktop_t *p2 = d2->prev;
desktop_t *n2 = d2->next;

if (p1 != NULL && p1 != d2)
p1->next = d2;
if (n1 != NULL && n1 != d2)
n1->prev = d2;
if (p2 != NULL && p2 != d1)
p2->next = d1;
if (n2 != NULL && n2 != d1)
n2->prev = d1;

d1->prev = p2 == d1 ? d2 : p2;
d1->next = n2 == d1 ? d2 : n2;
d2->prev = p1 == d2 ? d1 : p1;
d2->next = n1 == d2 ? d1 : n1;

ewmh_update_wm_desktops();
ewmh_update_desktop_names();
ewmh_update_current_desktop();
put_status();
}

void show_desktop(desktop_t *d)
{
if (!visible)
return;
for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
window_show(n->client->window);
}

void hide_desktop(desktop_t *d)
{
if (!visible)
return;
for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
window_hide(n->client->window);
}
18 changes: 18 additions & 0 deletions desktop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _DESKTOP_H
#define _DESKTOP_H

#define DEFAULT_DESK_NAME "Desktop"
#define WINDOW_GAP 6

desktop_t *make_desktop(const char *);
void insert_desktop(monitor_t *, desktop_t *);
void add_desktop(monitor_t *, desktop_t *);
void empty_desktop(desktop_t *);
void unlink_desktop(monitor_t *, desktop_t *);
void remove_desktop(monitor_t *, desktop_t *);
void swap_desktops(monitor_t *, desktop_t *, desktop_t *);
void transfer_desktop(monitor_t *, monitor_t *, desktop_t *);
void show_desktop(desktop_t *);
void hide_desktop(desktop_t *);

#endif
1 change: 1 addition & 0 deletions events.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <xcb/xcb_event.h>
#include <xcb/xcb_icccm.h>
#include "types.h"
#include "monitor.h"
#include "bspwm.h"
#include "settings.h"
#include "helpers.h"
Expand Down
Loading

0 comments on commit d168e91

Please sign in to comment.