Add module "unit"

This commit is contained in:
Alex Kotov 2021-11-17 07:56:19 +05:00
parent 6fceae2d49
commit 88fb0c8364
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 62 additions and 10 deletions

View file

@ -26,6 +26,7 @@ MODULES_SRC = \
src/settings.c \ src/settings.c \
src/spawn.c \ src/spawn.c \
src/tags.c \ src/tags.c \
src/unit.c \
src/util.c src/util.c
DWM_SRC = \ DWM_SRC = \

View file

@ -53,6 +53,7 @@
#include "settings.h" #include "settings.h"
#include "spawn.h" #include "spawn.h"
#include "tags.h" #include "tags.h"
#include "unit.h"
#include "util.h" #include "util.h"
/* macros */ /* macros */
@ -142,6 +143,7 @@ typedef struct {
} Layout; } Layout;
struct Monitor { struct Monitor {
Unit unit;
char ltsymbol[16]; char ltsymbol[16];
float mfact; float mfact;
int nmaster; int nmaster;
@ -152,7 +154,6 @@ struct Monitor {
unsigned int seltags; unsigned int seltags;
unsigned int sellt; unsigned int sellt;
unsigned int tagset[2]; unsigned int tagset[2];
bool showbar;
int topbar; int topbar;
Client *clients; Client *clients;
Client *sel; Client *sel;
@ -628,6 +629,7 @@ cleanupmon(Monitor *mon)
} }
XUnmapWindow(dpy, mon->barwin); XUnmapWindow(dpy, mon->barwin);
XDestroyWindow(dpy, mon->barwin); XDestroyWindow(dpy, mon->barwin);
UNIT_DELETE(mon->unit);
free(mon); free(mon);
} }
@ -657,10 +659,15 @@ createmon(void)
unsigned int i; unsigned int i;
m = ecalloc(1, sizeof(Monitor)); m = ecalloc(1, sizeof(Monitor));
if (!(m->unit = unit_new())) {
free(m);
return NULL;
}
m->tagset[0] = m->tagset[1] = 1; m->tagset[0] = m->tagset[1] = 1;
m->mfact = mfact; m->mfact = mfact;
m->nmaster = settings_get_default_clients_in_master(); m->nmaster = settings_get_default_clients_in_master();
m->showbar = settings_get_show_bar_by_default();
m->topbar = topbar; m->topbar = topbar;
m->lt[0] = &layouts[0]; m->lt[0] = &layouts[0];
m->lt[1] = &layouts[1 % LENGTH(layouts)]; m->lt[1] = &layouts[1 % LENGTH(layouts)];
@ -684,7 +691,7 @@ createmon(void)
m->pertag->ltidxs[i][1] = m->lt[1]; m->pertag->ltidxs[i][1] = m->lt[1];
m->pertag->sellts[i] = m->sellt; m->pertag->sellts[i] = m->sellt;
m->pertag->showbars[i] = m->showbar; m->pertag->showbars[i] = m->unit->show_bar;
} }
return m; return m;
@ -1837,15 +1844,17 @@ tagmon(const Arg *arg)
void void
togglebar(const Arg *arg) 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); updatebarpos(selmon);
resizebarwin(selmon); resizebarwin(selmon);
if (showsystray) { if (showsystray) {
XWindowChanges wc; XWindowChanges wc;
if (!selmon->showbar) if (!selmon->unit->show_bar)
wc.y = -bh; wc.y = -bh;
else if (selmon->showbar) { else if (selmon->unit->show_bar) {
wc.y = 0; wc.y = 0;
if (!selmon->topbar) if (!selmon->topbar)
wc.y = selmon->mh - bh; 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] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; 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()) settings_get_show_bar_per_tag())
{ {
togglebar(NULL); togglebar(NULL);
@ -2021,7 +2030,7 @@ updatebarpos(Monitor *m)
{ {
m->wy = m->my; m->wy = m->my;
m->wh = m->mh; m->wh = m->mh;
if (m->showbar) { if (m->unit->show_bar) {
m->wh -= bh; m->wh -= bh;
m->by = m->topbar ? m->wy : m->wy + m->wh; m->by = m->topbar ? m->wy : m->wy + m->wh;
m->wy = m->topbar ? m->wy + bh : m->wy; 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] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
selmon->lt[selmon->sellt ^ 1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt ^ 1]; 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()) settings_get_show_bar_per_tag())
{ {
togglebar(NULL); 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] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
selmon->lt[selmon->sellt ^ 1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt ^ 1]; 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()) settings_get_show_bar_per_tag())
{ {
togglebar(NULL); togglebar(NULL);

22
src/unit.c Normal file
View file

@ -0,0 +1,22 @@
#include "unit.h"
#include "settings.h"
#include <stdlib.h>
#include <string.h>
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);
}

20
src/unit.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef _UNIT_H
#define _UNIT_H
#include <stdbool.h>
#include <stddef.h>
#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