Merge pull request #2 from dylanaraps/rewrite

Rewrite
This commit is contained in:
dylan
2019-10-12 15:05:33 +00:00
committed by GitHub
2 changed files with 84 additions and 89 deletions

View File

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

165
sowm.c
View File

@@ -33,10 +33,7 @@ struct client{
};
typedef struct desktop desktop;
struct desktop{
client *head, *current;
int mode;
};
struct desktop{client *head;};
static void notify_motion(XEvent *e);
static void notify_destroy(XEvent *e);
@@ -50,12 +47,14 @@ static void button_release();
static void win_add(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_center();
static void win_center(Window w);
static void win_center_current();
static void win_next();
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_save(int i);
@@ -68,10 +67,10 @@ static void run(const Arg arg);
static void wm_init();
static void wm_setup();
static client *cur, *head;
static client *head;
static desktop desktops[10];
static int curr_desk, mode, screen, sh, sw;
static int curr_desk, screen, sh, sw;
static Display *dis;
static Window root;
@@ -99,23 +98,22 @@ void win_add(Window w) {
exit(1);
if (head == NULL) {
c->next = NULL;
c->prev = NULL;
c->win = w;
head = c;
}
else {
for (t=head;t->next;t=t->next);
c->next = NULL;
c->prev = t;
c->win = w;
t->next = c;
}
c->next = NULL;
c->win = w;
cur = c;
XSelectInput(dis, w, PropertyChangeMask|StructureNotifyMask|
EnterWindowMask|FocusChangeMask);
ws_save(curr_desk);
}
void ws_go(const Arg arg) {
@@ -125,66 +123,82 @@ void ws_go(const Arg arg) {
return;
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_sel(arg.i);
if (head != NULL)
for(c=head;c;c=c->next) XMapWindow(dis,c->win);
win_update();
for(c=head;c;c=c->next) XMapWindow(dis, c->win);
}
void win_center() {
XGetWindowAttributes(dis, cur->win, &attr);
Window win_current() {
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 y = (sh / 2) - (attr.height / 2);
XMoveWindow(dis, cur->win, x, y);
XMoveWindow(dis, w, x, y);
}
void win_fs() {
if (cur == NULL) return;
void win_center_current() {
win_center(win_current());
}
if (cur->f != 1) {
XGetWindowAttributes(dis, cur->win, &attr);
void win_fs_current() {
win_fs(win_current());
}
cur->f = 1;
cur->x = attr.x;
cur->y = attr.y;
cur->w = attr.width;
cur->h = attr.height;
void win_fs(Window w) {
client *c;
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 {
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) {
client *tmp = cur;
int tmp2 = curr_desk;
int tmp = curr_desk;
Window current = win_current();
if (arg.i == tmp2 || cur == NULL)
if (arg.i == tmp)
return;
// Add client to desktop
ws_sel(arg.i);
win_add(tmp->win);
win_add(current);
ws_save(arg.i);
// Remove client from current desktop
ws_sel(tmp2);
XUnmapWindow(dis,tmp->win);
win_del(tmp->win);
ws_save(tmp2);
win_update();
ws_sel(tmp);
XUnmapWindow(dis, current);
win_del(current);
ws_save(tmp);
}
void notify_destroy(XEvent *e) {
@@ -200,7 +214,6 @@ void notify_destroy(XEvent *e) {
return;
win_del(ev->window);
win_update();
}
void configure_request(XEvent *e) {
@@ -217,12 +230,8 @@ void configure_request(XEvent *e) {
XConfigureWindow(dis, ev->window, ev->value_mask, &wc);
}
void win_update() {
client *c;
for(c=head;c;c=c->next)
if (cur == c)
XSetInputFocus(dis, c->win, RevertToParent, CurrentTime);
void win_update(Window w) {
XSetInputFocus(dis, w, RevertToParent, CurrentTime);
}
void notify_enter(XEvent *e) {
@@ -269,8 +278,6 @@ void notify_motion(XEvent *e) {
attr.y + (start.button==1 ? ydiff : 0),
MAX(1, attr.width + (start.button==3 ? xdiff : 0)),
MAX(1, attr.height + (start.button==3 ? ydiff : 0)));
cur->f = 0;
}
}
@@ -279,36 +286,36 @@ void button_release() {
}
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) {
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_center(ev->window);
XMapWindow(dis, ev->window);
win_update(ev->window);
win_add(ev->window);
win_center();
XMapWindow(dis,ev->window);
win_update();
}
void win_next() {
Window current = win_current();
client *c;
if (cur != NULL && head != NULL) {
c = cur->next;
if (head != NULL) {
for(c=head;c;c=c->next)
if (c->win == current) break;
c = c->next;
if (c == NULL) c = head;
cur = c;
win_update();
win_update(c->win);
XRaiseWindow(dis, c->win);
}
}
@@ -323,7 +330,6 @@ void win_del(Window w) {
free(head);
head = NULL;
cur = NULL;
ws_save(curr_desk);
return;
@@ -332,37 +338,29 @@ void win_del(Window w) {
if (c->prev == NULL) {
head = c->next;
c->next->prev = NULL;
cur = c->next;
}
else if (c->next == NULL) {
c->prev->next = NULL;
cur = c->prev;
}
else {
c->prev->next = c->next;
c->next->prev = c->prev;
cur = c->prev;
}
free(c);
ws_save(curr_desk);
win_update();
return;
}
}
void ws_save(int i) {
desktops[i].mode = mode;
desktops[i].head = head;
desktops[i].current = cur;
desktops[i].head = head;
}
void ws_sel(int i) {
head = desktops[i].head;
cur = desktops[i].current;
mode = desktops[i].mode;
curr_desk = i;
}
@@ -377,11 +375,8 @@ void wm_setup() {
key_grab();
for(int i=0; i < TABLENGTH(desktops); ++i) {
desktops[i].mode = 0;
desktops[i].head = NULL;
desktops[i].current = NULL;
}
for(int i=0; i < TABLENGTH(desktops); ++i)
desktops[i].head = NULL;
const Arg arg = {.i = 1};
curr_desk = arg.i;