polytreewm/src/unit.c

77 lines
1.6 KiB
C
Raw Normal View History

2021-11-17 02:56:19 +00:00
#include "unit.h"
#include "settings.h"
#include <stdlib.h>
#include <string.h>
struct Unit {
UnitKind kind;
2021-11-17 04:35:21 +00:00
struct Unit *parent;
bool show_bar;
};
2021-11-17 03:21:32 +00:00
Unit unit_new(const UnitKind kind, const Unit parent)
2021-11-17 02:56:19 +00:00
{
Unit unit = malloc(sizeof(struct Unit));
2021-11-17 03:21:32 +00:00
if (!unit) goto fail_without_unit;
2021-11-17 02:56:19 +00:00
memset(unit, 0, sizeof(struct Unit));
2021-11-17 03:21:32 +00:00
unit->kind = kind;
unit->parent = parent;
2021-11-17 02:56:19 +00:00
unit->show_bar = settings_get_show_bar_by_default();
2021-11-17 03:21:32 +00:00
if (unit->kind == UNIT_GLOBAL) {
// TODO: maybe we should assert here
if (unit->parent) goto fail_other;
} else {
if (!unit->parent || unit->kind != unit->parent->kind + 1) {
// TODO: maybe we should assert here
goto fail_other;
}
}
2021-11-17 02:56:19 +00:00
return unit;
2021-11-17 03:21:32 +00:00
fail_other:
free(unit);
fail_without_unit:
return NULL;
2021-11-17 02:56:19 +00:00
}
void unit_delete(const Unit unit)
{
// TODO: maybe we should assert
if (unit == NULL) return;
free(unit);
}
bool unit_get_show_bar(const Unit unit)
{
2021-11-17 04:35:21 +00:00
const UnitKind show_bar_per_unit = settings_get_show_bar_per_unit();
if (unit->kind == show_bar_per_unit) {
return unit->show_bar;
} else if (unit->kind > show_bar_per_unit) {
return unit_get_show_bar(unit->parent);
} else {
// TODO: maybe we should assert here
return settings_get_show_bar_by_default();
}
}
bool unit_toggle_show_bar(const Unit unit)
{
2021-11-17 04:35:21 +00:00
const UnitKind show_bar_per_unit = settings_get_show_bar_per_unit();
if (unit->kind == show_bar_per_unit) {
return unit->show_bar = !unit->show_bar;
} else if (unit->kind > show_bar_per_unit) {
return unit_toggle_show_bar(unit->parent);
} else {
// TODO: maybe we should assert here
return settings_get_show_bar_by_default();
}
}