From: Michael D. Lowis Date: Wed, 18 Mar 2020 02:58:49 +0000 (-0400) Subject: moved tiling logic to a separate file X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=41aa541f7e7803598d5def70e17d81407749a18a;p=proto%2Fanvil.git moved tiling logic to a separate file --- diff --git a/anvil.h b/anvil.h index 5ed948d..c5e0bc4 100644 --- a/anvil.h +++ b/anvil.h @@ -149,6 +149,14 @@ void client_readprops(Client* c); void mouse_click(XButtonEvent* ev, Location* loc); void mouse_drag(XMotionEvent* ev, Location* loc); +/* tile.c */ +void monocled_add(Monitor* mon, Column* col, Client* c); +void monocled_del(Monitor* mon, Column* col, Client* c); +void expanded_add(Monitor* mon, Column* col, Client* c); +void expanded_del(Monitor* mon, Column* col, Client* c); +void stacked_add(Monitor* mon, Column* col, Client* c); +void stacked_del(Monitor* mon, Column* col, Client* c); + /* error.c */ extern int (*error_default)(Display* disp, XErrorEvent* ev); int error_init(Display* disp, XErrorEvent* ev); @@ -162,3 +170,4 @@ void xfree(void* p); Atom atom(char* str); void sendmsg(Window win, Atom proto, Atom type); void warp_mouse(Client* c); +Client* delclient(Client* list, Client* dead); diff --git a/mons.c b/mons.c index ce63915..040cec9 100644 --- a/mons.c +++ b/mons.c @@ -4,7 +4,6 @@ static Monitor* pickmon(int ptrx, int ptry); static Column* pickcol(Column* cols, int relx); static Workspace* pickws(Monitor* mon, int wsid); -static Client* delclient(Client* list, Client* dead); static void client_visibility(Workspace* wspace, int show); static Client* client_find(Client* clients, Window win); @@ -41,15 +40,6 @@ void mons_init(void) xfree(mons); } -static void coldims(Monitor* mon, Column* col, int *x, int *y, int *w, int *h) -{ - *x = mon->x, *y = mon->y, *w = col->width-2, *h = mon->h-2; -// printf("bef: %d %d %d %d\n", mon->x, mon->y, col->width, mon->h); -// printf("mon: %d %d %d %d\n", mon->x, mon->y, mon->w, mon->h); - for (Column* c = mon->cspace->columns; c != col; *x += c->width, c = c->next); -// printf("%d %d %d %d\n", *x, *y, *w, *h); -} - void mons_layer(Monitor* mon) { /* lower all of the floating windows first */ @@ -69,54 +59,6 @@ void mons_layer(Monitor* mon) } } -static void add_monocle(Monitor* mon, Column* col, Client* c) -{ - coldims(mon, col, &(c->x), &(c->y), &(c->w), &(c->h)); - client_adjust(c); - c->next = col->clients; - col->clients = c; -} - -static void add_expand(Monitor* mon, Column* col, Client* client) -{ - /* - * stack all existing clients top to bottom only showing title bars - * move+resize new client to fill remaining space or place its title bar at bottom of screen - */ - (void)mon, (void)col; - client->next = col->clients; - col->clients = client; -} - -static void add_stacked(Monitor* mon, Column* col, Client* client) -{ - coldims(mon, col, &(client->x), &(client->y), &(client->w), &(client->h)); - if (col->clients) - { - Client* max = col->clients; - for (Client* c = max; c; c = c->next) - { - if (c->h > max->h) - { - max = c; - } - } - client->h = max->h/2; - client->y = max->y + client->h; - max->h -= max->h/2; - client->next = max->next; - max->next = client; - client_adjust(max); - client_adjust(client); - } - else - { - client->next = col->clients; - col->clients = client; - client_adjust(client); - } -} - /* adds a new client to the most appropriate monitor */ void mons_addclient(Client* c) { @@ -137,15 +79,15 @@ void mons_addclient(Client* c) Column* col = pickcol(mon->cspace->columns, ptrx - mon->x); if (col->mode == TILE_MONOCLE) { - add_monocle(mon, col, c); + monocled_add(mon, col, c); } else if (col->mode == TILE_EXPAND) { - add_expand(mon, col, c); + expanded_add(mon, col, c); } else if (col->mode == TILE_STACKED) { - add_stacked(mon, col, c); + stacked_add(mon, col, c); } } mons_layer(mon); @@ -157,13 +99,25 @@ void mons_delclient(Client* c) Location loc = {0}; if (mons_find(c->win, &loc)) { - if (loc.column) + if (!loc.column) + { + loc.workspace->floating = delclient(loc.workspace->floating, c); + } + else if (loc.column->mode == TILE_MONOCLE) + { + monocled_del(loc.monitor, loc.column, loc.client); + } + else if (loc.column->mode == TILE_EXPAND) + { + expanded_del(loc.monitor, loc.column, loc.client); + } + else if (loc.column->mode == TILE_STACKED) { - loc.column->clients = delclient(loc.column->clients, c); + stacked_del(loc.monitor, loc.column, loc.client); } else { - loc.workspace->floating = delclient(loc.workspace->floating, c); + assert(0); } xfree(c->name); XDestroyWindow(X.disp, c->frame); @@ -347,29 +301,19 @@ static Workspace* pickws(Monitor* mon, int wsid) return wspace; } -static Client* delclient(Client* list, Client* dead) -{ - if (!list) - { - return NULL; - } - else if (list == dead) - { - return list->next; - } - else - { - list->next = delclient(list->next, dead); - return list; - } -} - static void client_visibility(Workspace* wspace, int show) { for (Client* client = wspace->floating; client; client = client->next) { client_show(client, show); } + for (Column* col = wspace->columns; col; col = col->next) + { + for (Client* client = col->clients; client; client = client->next) + { + client_show(client, show); + } + } XSync(X.disp, False); } diff --git a/tile.c b/tile.c new file mode 100644 index 0000000..52963dc --- /dev/null +++ b/tile.c @@ -0,0 +1,90 @@ +#include "anvil.h" + +static void coldims(Monitor* mon, Column* col, int *x, int *y, int *w, int *h) +{ + *x = mon->x, *y = mon->y, *w = col->width-2, *h = mon->h-2; + for (Column* c = mon->cspace->columns; c != col; *x += c->width, c = c->next); +} + +void monocled_add(Monitor* mon, Column* col, Client* c) +{ + coldims(mon, col, &(c->x), &(c->y), &(c->w), &(c->h)); + client_adjust(c); + c->next = col->clients; + col->clients = c; +} + +void monocled_del(Monitor* mon, Column* col, Client* c) +{ + (void)mon; + col->clients = delclient(col->clients, c); +} + +void expanded_add(Monitor* mon, Column* col, Client* c) +{ + /* + * stack all existing clients top to bottom only showing title bars + * move+resize new client to fill remaining space or place its title bar at bottom of screen + */ + (void)mon; + c->next = col->clients; + col->clients = c; +} + +void expanded_del(Monitor* mon, Column* col, Client* c) +{ + (void)mon; + col->clients = delclient(col->clients, c); +} + +void stacked_add(Monitor* mon, Column* col, Client* c) +{ + coldims(mon, col, &(c->x), &(c->y), &(c->w), &(c->h)); + if (col->clients) + { + Client* max = col->clients; + for (Client* c = max; c; c = c->next) + { + if (c->h > max->h) + { + max = c; + } + } + c->h = max->h/2; + c->y = max->y + c->h; + max->h -= max->h/2; + c->next = max->next; + max->next = c; + client_adjust(max); + client_adjust(c); + } + else + { + c->next = col->clients; + col->clients = c; + client_adjust(c); + } +} + +void stacked_del(Monitor* mon, Column* col, Client* c) +{ + if (col->clients == c) + { + col->clients = c->next; + if (col->clients) + { + col->clients->h += col->clients->y - mon->y; + col->clients->y = mon->y; + client_adjust(col->clients); + } + } + else + { + Client* prev = col->clients; + for (; prev->next != c; prev = prev->next); + prev->next = c->next; + prev->h += c->h; + client_adjust(prev); + } +} + diff --git a/util.c b/util.c index d96af32..4c8efd8 100644 --- a/util.c +++ b/util.c @@ -60,3 +60,20 @@ void warp_mouse(Client* c) X.start_y = c->y + new_h; } +Client* delclient(Client* list, Client* dead) +{ + if (!list) + { + return NULL; + } + else if (list == dead) + { + return list->next; + } + else + { + list->next = delclient(list->next, dead); + return list; + } +} +