2021-11-17 02:56:19 +00:00
|
|
|
#include "unit.h"
|
|
|
|
|
2021-11-18 15:37:49 +00:00
|
|
|
#include "constraints.h"
|
2021-11-17 02:56:19 +00:00
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-11-17 04:05:32 +00:00
|
|
|
struct Unit {
|
|
|
|
UnitKind kind;
|
2021-11-17 04:35:21 +00:00
|
|
|
struct Unit *parent;
|
2021-11-17 04:05:32 +00:00
|
|
|
bool show_bar;
|
2021-11-18 15:18:27 +00:00
|
|
|
float master_area_factor;
|
2021-11-17 04:05:32 +00:00
|
|
|
};
|
|
|
|
|
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-18 15:18:27 +00:00
|
|
|
|
|
|
|
unit->show_bar = settings_get_show_bar_by_default();
|
|
|
|
unit->master_area_factor = settings_get_default_master_area_factor();
|
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);
|
|
|
|
}
|
2021-11-17 04:05:32 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2021-11-17 04:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2021-11-17 04:05:32 +00:00
|
|
|
}
|
2021-11-18 15:18:27 +00:00
|
|
|
|
|
|
|
float unit_get_master_area_factor(const Unit unit)
|
|
|
|
{
|
|
|
|
const UnitKind master_area_factor_per_unit =
|
|
|
|
settings_get_master_area_factor_per_unit();
|
|
|
|
|
|
|
|
if (unit->kind == master_area_factor_per_unit) {
|
|
|
|
return unit->master_area_factor;
|
|
|
|
} else if (unit->kind > master_area_factor_per_unit) {
|
|
|
|
return unit_get_master_area_factor(unit->parent);
|
|
|
|
} else {
|
|
|
|
// TODO: maybe we should assert here
|
|
|
|
return settings_get_default_master_area_factor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float unit_inc_master_area_factor(const Unit unit, const float delta)
|
|
|
|
{
|
|
|
|
const UnitKind master_area_factor_per_unit =
|
|
|
|
settings_get_master_area_factor_per_unit();
|
|
|
|
|
|
|
|
if (unit->kind == master_area_factor_per_unit) {
|
2021-11-18 15:37:49 +00:00
|
|
|
return unit->master_area_factor =
|
|
|
|
constraints_master_area_factor(unit->master_area_factor + delta);
|
2021-11-18 15:18:27 +00:00
|
|
|
} else if (unit->kind > master_area_factor_per_unit) {
|
|
|
|
return unit_inc_master_area_factor(unit->parent, delta);
|
|
|
|
} else {
|
|
|
|
// TODO: maybe we should assert here
|
|
|
|
return settings_get_default_master_area_factor();
|
|
|
|
}
|
|
|
|
}
|