From 88fb0c8364f93b49883f8505fe06728ee320115d Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 17 Nov 2021 07:56:19 +0500 Subject: [PATCH] Add module "unit" --- Makefile | 1 + src/dwm.c | 29 +++++++++++++++++++---------- src/unit.c | 22 ++++++++++++++++++++++ src/unit.h | 20 ++++++++++++++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 src/unit.c create mode 100644 src/unit.h diff --git a/Makefile b/Makefile index ec94df4..012c2c7 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ MODULES_SRC = \ src/settings.c \ src/spawn.c \ src/tags.c \ + src/unit.c \ src/util.c DWM_SRC = \ diff --git a/src/dwm.c b/src/dwm.c index 2b51d65..59d172f 100644 --- a/src/dwm.c +++ b/src/dwm.c @@ -53,6 +53,7 @@ #include "settings.h" #include "spawn.h" #include "tags.h" +#include "unit.h" #include "util.h" /* macros */ @@ -142,6 +143,7 @@ typedef struct { } Layout; struct Monitor { + Unit unit; char ltsymbol[16]; float mfact; int nmaster; @@ -152,7 +154,6 @@ struct Monitor { unsigned int seltags; unsigned int sellt; unsigned int tagset[2]; - bool showbar; int topbar; Client *clients; Client *sel; @@ -628,6 +629,7 @@ cleanupmon(Monitor *mon) } XUnmapWindow(dpy, mon->barwin); XDestroyWindow(dpy, mon->barwin); + UNIT_DELETE(mon->unit); free(mon); } @@ -657,10 +659,15 @@ createmon(void) unsigned int i; m = ecalloc(1, sizeof(Monitor)); + + if (!(m->unit = unit_new())) { + free(m); + return NULL; + } + m->tagset[0] = m->tagset[1] = 1; m->mfact = mfact; m->nmaster = settings_get_default_clients_in_master(); - m->showbar = settings_get_show_bar_by_default(); m->topbar = topbar; m->lt[0] = &layouts[0]; m->lt[1] = &layouts[1 % LENGTH(layouts)]; @@ -684,7 +691,7 @@ createmon(void) m->pertag->ltidxs[i][1] = m->lt[1]; m->pertag->sellts[i] = m->sellt; - m->pertag->showbars[i] = m->showbar; + m->pertag->showbars[i] = m->unit->show_bar; } return m; @@ -1837,15 +1844,17 @@ tagmon(const Arg *arg) void togglebar(const Arg *arg) { - selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar; + selmon->unit->show_bar = + selmon->pertag->showbars[selmon->pertag->curtag] = + !selmon->unit->show_bar; updatebarpos(selmon); resizebarwin(selmon); if (showsystray) { XWindowChanges wc; - if (!selmon->showbar) + if (!selmon->unit->show_bar) wc.y = -bh; - else if (selmon->showbar) { + else if (selmon->unit->show_bar) { wc.y = 0; if (!selmon->topbar) wc.y = selmon->mh - bh; @@ -1922,7 +1931,7 @@ toggleview(const Arg *arg) selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; - if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag] && + if (selmon->unit->show_bar != selmon->pertag->showbars[selmon->pertag->curtag] && settings_get_show_bar_per_tag()) { togglebar(NULL); @@ -2021,7 +2030,7 @@ updatebarpos(Monitor *m) { m->wy = m->my; m->wh = m->mh; - if (m->showbar) { + if (m->unit->show_bar) { m->wh -= bh; m->by = m->topbar ? m->wy : m->wy + m->wh; m->wy = m->topbar ? m->wy + bh : m->wy; @@ -2269,7 +2278,7 @@ view(const Arg *arg) selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; selmon->lt[selmon->sellt ^ 1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt ^ 1]; - if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag] && + if (selmon->unit->show_bar != selmon->pertag->showbars[selmon->pertag->curtag] && settings_get_show_bar_per_tag()) { togglebar(NULL); @@ -2314,7 +2323,7 @@ viewrel(const Arg *arg) selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; selmon->lt[selmon->sellt ^ 1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt ^ 1]; - if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag] && + if (selmon->unit->show_bar != selmon->pertag->showbars[selmon->pertag->curtag] && settings_get_show_bar_per_tag()) { togglebar(NULL); diff --git a/src/unit.c b/src/unit.c new file mode 100644 index 0000000..6d3a024 --- /dev/null +++ b/src/unit.c @@ -0,0 +1,22 @@ +#include "unit.h" + +#include "settings.h" + +#include +#include + +Unit unit_new() +{ + Unit unit = malloc(sizeof(struct Unit)); + memset(unit, 0, sizeof(struct Unit)); + unit->show_bar = settings_get_show_bar_by_default(); + return unit; +} + +void unit_delete(const Unit unit) +{ + // TODO: maybe we should assert + if (unit == NULL) return; + + free(unit); +} diff --git a/src/unit.h b/src/unit.h new file mode 100644 index 0000000..ee00eaf --- /dev/null +++ b/src/unit.h @@ -0,0 +1,20 @@ +#ifndef _UNIT_H +#define _UNIT_H + +#include +#include + +#define UNIT_DELETE(unit) { \ + unit_delete(unit); \ + unit = NULL; \ +} + +typedef struct Unit { + bool show_bar; +} *Unit; + +Unit unit_new(); + +void unit_delete(Unit unit); + +#endif // _UNIT_H