sowm: rewrite

This commit is contained in:
Dylan Araps
2019-10-12 16:41:35 +03:00
parent 85ac75583f
commit 99901b15a6
2 changed files with 76 additions and 88 deletions

View File

@@ -24,10 +24,10 @@ const char* volmute[] = {"amixer", "sset", "Master", "toggle", NULL};
const char* colors[] = {"bud", "/home/goldie/Pictures/Wallpapers", NULL}; const char* colors[] = {"bud", "/home/goldie/Pictures/Wallpapers", NULL};
static struct key keys[] = { static struct key keys[] = {
{MOD, XK_q, win_kill, {NULL}}, {MOD, XK_q, win_kill, {NULL}},
{MOD, XK_c, win_center, {NULL}}, {MOD, XK_c, win_center_current, {NULL}},
{MOD, XK_f, win_fs, {NULL}}, {MOD, XK_f, win_fs_current, {NULL}},
{Mod1Mask, XK_Tab, win_next, {NULL}}, {Mod1Mask, XK_Tab, win_next, {NULL}},
{MOD, XK_d, run, {.com = menu}}, {MOD, XK_d, run, {.com = menu}},
{MOD, XK_w, run, {.com = colors}}, {MOD, XK_w, run, {.com = colors}},

156
sowm.c
View File

@@ -35,7 +35,6 @@ struct client{
typedef struct desktop desktop; typedef struct desktop desktop;
struct desktop{ struct desktop{
client *head, *current; client *head, *current;
int mode;
}; };
static void notify_motion(XEvent *e); static void notify_motion(XEvent *e);
@@ -50,12 +49,14 @@ static void button_release();
static void win_add(Window w); static void win_add(Window w);
static void win_del(Window w); static void win_del(Window w);
static void win_fs(); static void win_fs(Window w);
static void win_fs_current();
static void win_kill(); static void win_kill();
static void win_center(); static void win_center(Window w);
static void win_center_current();
static void win_next(); static void win_next();
static void win_to_ws(const Arg arg); static void win_to_ws(const Arg arg);
static void win_update(); static void win_update(Window w);
static void ws_go(const Arg arg); static void ws_go(const Arg arg);
static void ws_save(int i); static void ws_save(int i);
@@ -68,10 +69,10 @@ static void run(const Arg arg);
static void wm_init(); static void wm_init();
static void wm_setup(); static void wm_setup();
static client *cur, *head; static client *head;
static desktop desktops[10]; static desktop desktops[10];
static int curr_desk, mode, screen, sh, sw; static int curr_desk, screen, sh, sw;
static Display *dis; static Display *dis;
static Window root; static Window root;
@@ -112,10 +113,6 @@ void win_add(Window w) {
c->next = NULL; c->next = NULL;
c->win = w; c->win = w;
cur = c;
XSelectInput(dis, w, PropertyChangeMask|StructureNotifyMask|
EnterWindowMask|FocusChangeMask);
} }
void ws_go(const Arg arg) { void ws_go(const Arg arg) {
@@ -125,66 +122,81 @@ void ws_go(const Arg arg) {
return; return;
if (head != NULL) if (head != NULL)
for(c=head;c;c=c->next) XUnmapWindow(dis,c->win); for(c=head;c;c=c->next) XUnmapWindow(dis, c->win);
ws_save(curr_desk); ws_save(curr_desk);
ws_sel(arg.i); ws_sel(arg.i);
if (head != NULL) if (head != NULL)
for(c=head;c;c=c->next) XMapWindow(dis,c->win); for(c=head;c;c=c->next) XMapWindow(dis, c->win);
win_update();
} }
void win_center() { Window win_current() {
XGetWindowAttributes(dis, cur->win, &attr); Window focused;
int revert_to;
XGetInputFocus(dis, &focused, &revert_to);
return focused;
}
void win_center(Window w) {
XGetWindowAttributes(dis, w, &attr);
int x = (sw / 2) - (attr.width / 2); int x = (sw / 2) - (attr.width / 2);
int y = (sh / 2) - (attr.height / 2); int y = (sh / 2) - (attr.height / 2);
XMoveWindow(dis, cur->win, x, y); XMoveWindow(dis, w, x, y);
} }
void win_fs() { void win_center_current() {
if (cur == NULL) return; win_center(win_current());
}
if (cur->f != 1) { void win_fs_current() {
XGetWindowAttributes(dis, cur->win, &attr); win_fs(win_current());
}
cur->f = 1; void win_fs(Window w) {
cur->x = attr.x; client *c;
cur->y = attr.y;
cur->w = attr.width;
cur->h = attr.height;
XMoveResizeWindow(dis, cur->win, 0, 0, sw, sh); for(c=head;c;c=c->next)
if (c->win == w) break;
if (c == NULL) return;
if (c->f != 1) {
XGetWindowAttributes(dis, w, &attr);
c->f = 1;
c->x = attr.x;
c->y = attr.y;
c->w = attr.width;
c->h = attr.height;
XMoveResizeWindow(dis, w, 0, 0, sw, sh);
} }
else { else {
cur->f = 0; c->f = 0;
XMoveResizeWindow(dis, cur->win, cur->x, cur->y, cur->w, cur->h); XMoveResizeWindow(dis, c->win, c->x, c->y, c->w, c->h);
} }
} }
void win_to_ws(const Arg arg) { void win_to_ws(const Arg arg) {
client *tmp = cur; int tmp = curr_desk;
int tmp2 = curr_desk; Window current = win_current();
if (arg.i == tmp2 || cur == NULL) if (arg.i == tmp)
return; return;
// Add client to desktop
ws_sel(arg.i); ws_sel(arg.i);
win_add(tmp->win); win_add(current);
ws_save(arg.i); ws_save(arg.i);
// Remove client from current desktop ws_sel(tmp);
ws_sel(tmp2); XUnmapWindow(dis, current);
XUnmapWindow(dis,tmp->win); win_del(current);
win_del(tmp->win);
ws_save(tmp2);
win_update();
} }
void notify_destroy(XEvent *e) { void notify_destroy(XEvent *e) {
@@ -200,7 +212,6 @@ void notify_destroy(XEvent *e) {
return; return;
win_del(ev->window); win_del(ev->window);
win_update();
} }
void configure_request(XEvent *e) { void configure_request(XEvent *e) {
@@ -217,23 +228,12 @@ void configure_request(XEvent *e) {
XConfigureWindow(dis, ev->window, ev->value_mask, &wc); XConfigureWindow(dis, ev->window, ev->value_mask, &wc);
} }
void win_update() { void win_update(Window w) {
client *c; XSetInputFocus(dis, w, RevertToParent, CurrentTime);
for(c=head;c;c=c->next)
if (cur == c)
XSetInputFocus(dis, c->win, RevertToParent, CurrentTime);
} }
void notify_enter(XEvent *e) { void notify_enter(XEvent *e) {
client *c; XSetInputFocus(dis, e->xcrossing.window, RevertToParent, CurrentTime);
for (c=head;c;c=c->next)
if (e->xcrossing.window == c->win)
cur = c;
if (cur != NULL && head != NULL)
win_update();
} }
void key_grab() { void key_grab() {
@@ -276,8 +276,6 @@ void notify_motion(XEvent *e) {
attr.y + (start.button==1 ? ydiff : 0), attr.y + (start.button==1 ? ydiff : 0),
MAX(1, attr.width + (start.button==3 ? xdiff : 0)), MAX(1, attr.width + (start.button==3 ? xdiff : 0)),
MAX(1, attr.height + (start.button==3 ? ydiff : 0))); MAX(1, attr.height + (start.button==3 ? ydiff : 0)));
cur->f = 0;
} }
} }
@@ -286,36 +284,36 @@ void button_release() {
} }
void win_kill() { void win_kill() {
if (cur != NULL) XKillClient(dis, cur->win); Window current = win_current();
if (current != root)
XKillClient(dis, current);
} }
void map_request(XEvent *e) { void map_request(XEvent *e) {
XMapRequestEvent *ev = &e->xmaprequest; XMapRequestEvent *ev = &e->xmaprequest;
client *c;
// For fullscreen mplayer (and maybe some other program)
for (c=head;c;c=c->next)
if(ev->window == c->win) {
XMapWindow(dis,ev->window);
return;
}
XSelectInput(dis, ev->window, PropertyChangeMask|StructureNotifyMask|
EnterWindowMask|FocusChangeMask);
win_add(ev->window); win_add(ev->window);
win_center(); win_center(ev->window);
XMapWindow(dis,ev->window); XMapWindow(dis, ev->window);
win_update(); win_update(ev->window);
} }
void win_next() { void win_next() {
Window current = win_current();
client *c; client *c;
if (cur != NULL && head != NULL) { if (head != NULL) {
c = cur->next; for(c=head;c;c=c->next)
if (c->win == current) break;
c = c->next;
if (c == NULL) c = head; if (c == NULL) c = head;
cur = c; win_update(c->win);
win_update();
XRaiseWindow(dis, c->win); XRaiseWindow(dis, c->win);
} }
} }
@@ -330,7 +328,6 @@ void win_del(Window w) {
free(head); free(head);
head = NULL; head = NULL;
cur = NULL;
ws_save(curr_desk); ws_save(curr_desk);
return; return;
@@ -339,37 +336,29 @@ void win_del(Window w) {
if (c->prev == NULL) { if (c->prev == NULL) {
head = c->next; head = c->next;
c->next->prev = NULL; c->next->prev = NULL;
cur = c->next;
} }
else if (c->next == NULL) { else if (c->next == NULL) {
c->prev->next = NULL; c->prev->next = NULL;
cur = c->prev;
} }
else { else {
c->prev->next = c->next; c->prev->next = c->next;
c->next->prev = c->prev; c->next->prev = c->prev;
cur = c->prev;
} }
free(c); free(c);
ws_save(curr_desk); ws_save(curr_desk);
win_update();
return; return;
} }
} }
void ws_save(int i) { void ws_save(int i) {
desktops[i].mode = mode; desktops[i].head = head;
desktops[i].head = head;
desktops[i].current = cur;
} }
void ws_sel(int i) { void ws_sel(int i) {
head = desktops[i].head; head = desktops[i].head;
cur = desktops[i].current;
mode = desktops[i].mode;
curr_desk = i; curr_desk = i;
} }
@@ -385,7 +374,6 @@ void wm_setup() {
key_grab(); key_grab();
for(int i=0; i < TABLENGTH(desktops); ++i) { for(int i=0; i < TABLENGTH(desktops); ++i) {
desktops[i].mode = 0;
desktops[i].head = NULL; desktops[i].head = NULL;
desktops[i].current = NULL; desktops[i].current = NULL;
} }