From d5eba48b366dc319b57f91b91d4e28d346a0f16d Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Thu, 18 Nov 2021 20:37:49 +0500 Subject: [PATCH] Add module "src/constraints.c" --- Makefile | 1 + src/constraints.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++ src/constraints.h | 12 +++++++++ src/settings.c | 47 +++++++++-------------------------- src/unit.c | 8 +++--- 5 files changed, 91 insertions(+), 40 deletions(-) create mode 100644 src/constraints.c create mode 100644 src/constraints.h diff --git a/Makefile b/Makefile index be7fe3f..6133dd3 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ VERSION = 6.2 MODULES_SRC = \ src/atoms.c \ + src/constraints.c \ src/drw.c \ src/helpers.c \ src/layouts.c \ diff --git a/src/constraints.c b/src/constraints.c new file mode 100644 index 0000000..1825f12 --- /dev/null +++ b/src/constraints.c @@ -0,0 +1,63 @@ +#include "constraints.h" + +#define MIN_BORDER_WIDTH 0 +#define MAX_BORDER_WIDTH 10000 +#define MIN_DEFAULT_CLIENTS_IN_MASTER 1 +#define MAX_DEFAULT_CLIENTS_IN_MASTER 10000 +#define MIN_DEFAULT_MASTER_AREA_FACTOR 0.05 +#define MAX_DEFAULT_MASTER_AREA_FACTOR 0.95 +#define MIN_GAP_SIZE 0 +#define MAX_GAP_SIZE 10000 +#define MIN_MASTER_AREA_FACTOR 0.05 +#define MAX_MASTER_AREA_FACTOR 0.95 +#define MIN_MAX_CLIENTS_IN_MASTER 1 +#define MAX_MAX_CLIENTS_IN_MASTER 10000 +#define MIN_SNAP_DISTANCE 1 +#define MAX_SNAP_DISTANCE 10000 + +int constraints_border_width(const int border_width) +{ + if (border_width < MIN_BORDER_WIDTH) return MIN_BORDER_WIDTH; + if (border_width > MAX_BORDER_WIDTH) return MAX_BORDER_WIDTH; + return border_width; +} + +int constraints_default_clients_in_master(const int default_clients_in_master) +{ + if (default_clients_in_master < MIN_DEFAULT_CLIENTS_IN_MASTER) return MIN_DEFAULT_CLIENTS_IN_MASTER; + if (default_clients_in_master > MAX_DEFAULT_CLIENTS_IN_MASTER) return MAX_DEFAULT_CLIENTS_IN_MASTER; + return default_clients_in_master; +} + +float constraints_default_master_area_factor(const float default_master_area_factor) +{ + return constraints_master_area_factor(default_master_area_factor); +} + +int constraints_gap_size(const int gap_size) +{ + if (gap_size < MIN_GAP_SIZE) return MIN_GAP_SIZE; + if (gap_size < MAX_GAP_SIZE) return MAX_GAP_SIZE; + return gap_size; +} + +float constraints_master_area_factor(const float master_area_factor) +{ + if (master_area_factor < MIN_MASTER_AREA_FACTOR) return MIN_MASTER_AREA_FACTOR; + if (master_area_factor > MAX_MASTER_AREA_FACTOR) return MAX_MASTER_AREA_FACTOR; + return master_area_factor; +} + +int constraints_max_clients_in_master(const int max_clients_in_master) +{ + if (max_clients_in_master < MIN_MAX_CLIENTS_IN_MASTER) return MIN_MAX_CLIENTS_IN_MASTER; + if (max_clients_in_master > MAX_MAX_CLIENTS_IN_MASTER) return MAX_MAX_CLIENTS_IN_MASTER; + return max_clients_in_master; +} + +unsigned int constraints_snap_distance(const unsigned int snap_distance) +{ + if (snap_distance < MIN_SNAP_DISTANCE) return MIN_SNAP_DISTANCE; + if (snap_distance > MAX_SNAP_DISTANCE) return MAX_SNAP_DISTANCE; + return snap_distance; +} diff --git a/src/constraints.h b/src/constraints.h new file mode 100644 index 0000000..7ba71ef --- /dev/null +++ b/src/constraints.h @@ -0,0 +1,12 @@ +#ifndef _CONSTRAINTS_H +#define _CONSTRAINTS_H + +int constraints_border_width(int border_width); +int constraints_default_clients_in_master(int default_clients_in_master); +float constraints_default_master_area_factor(float default_master_area_factor); +int constraints_gap_size(int gap_size); +float constraints_master_area_factor(float master_area_factor); +int constraints_max_clients_in_master(int max_clients_in_master); +unsigned int constraints_snap_distance(unsigned int snap_distance); + +#endif // _CONSTRAINTS_H diff --git a/src/settings.c b/src/settings.c index a3b5154..994958f 100644 --- a/src/settings.c +++ b/src/settings.c @@ -1,17 +1,6 @@ #include "settings.h" -#define MIN_BORDER_WIDTH 0 -#define MAX_BORDER_WIDTH 10000 -#define MIN_DEFAULT_CLIENTS_IN_MASTER 1 -#define MAX_DEFAULT_CLIENTS_IN_MASTER 10000 -#define MIN_DEFAULT_MASTER_AREA_FACTOR 0.05 -#define MAX_DEFAULT_MASTER_AREA_FACTOR 0.95 -#define MIN_GAP_SIZE 0 -#define MAX_GAP_SIZE 10000 -#define MIN_MAX_CLIENTS_IN_MASTER 1 -#define MAX_MAX_CLIENTS_IN_MASTER 10000 -#define MIN_SNAP_DISTANCE 1 -#define MAX_SNAP_DISTANCE 10000 +#include "constraints.h" static SettingsForSingleWindow border_for_single_window = SETTINGS_FOR_SINGLE_WINDOW_NOBODY_IS_FULLSCREEN; static int border_width = 2; @@ -46,11 +35,9 @@ int settings_get_border_width() return border_width; } -void settings_set_border_width(int new_border_width) +void settings_set_border_width(const int new_border_width) { - if (new_border_width < MIN_BORDER_WIDTH) new_border_width = MIN_BORDER_WIDTH; - if (new_border_width > MAX_BORDER_WIDTH) new_border_width = MAX_BORDER_WIDTH; - border_width = new_border_width; + border_width = constraints_border_width(new_border_width); // TODO: notify WM to rearrange clients } @@ -59,11 +46,9 @@ int settings_get_default_clients_in_master() return default_clients_in_master; } -void settings_set_default_clients_in_master(int new_default_clients_in_master) +void settings_set_default_clients_in_master(const int new_default_clients_in_master) { - if (new_default_clients_in_master < MIN_DEFAULT_CLIENTS_IN_MASTER) new_default_clients_in_master = MIN_DEFAULT_CLIENTS_IN_MASTER; - if (new_default_clients_in_master < MAX_DEFAULT_CLIENTS_IN_MASTER) new_default_clients_in_master = MAX_DEFAULT_CLIENTS_IN_MASTER; - default_clients_in_master = new_default_clients_in_master; + default_clients_in_master = constraints_default_clients_in_master(new_default_clients_in_master); } float settings_get_default_master_area_factor() @@ -71,11 +56,9 @@ float settings_get_default_master_area_factor() return default_master_area_factor; } -void settings_set_default_master_area_factor(float new_default_master_area_factor) +void settings_set_default_master_area_factor(const float new_default_master_area_factor) { - if (new_default_master_area_factor < MIN_DEFAULT_MASTER_AREA_FACTOR) new_default_master_area_factor = MIN_DEFAULT_MASTER_AREA_FACTOR; - if (new_default_master_area_factor > MAX_DEFAULT_MASTER_AREA_FACTOR) new_default_master_area_factor = MAX_DEFAULT_MASTER_AREA_FACTOR; - default_master_area_factor = new_default_master_area_factor; + default_master_area_factor = constraints_default_master_area_factor(new_default_master_area_factor); } SettingsForSingleWindow settings_get_gap_for_single_window() @@ -115,11 +98,9 @@ int settings_get_gap_size() return gap_size; } -void settings_set_gap_size(int new_gap_size) +void settings_set_gap_size(const int new_gap_size) { - if (new_gap_size < MIN_GAP_SIZE) new_gap_size = MIN_GAP_SIZE; - if (new_gap_size > MAX_GAP_SIZE) new_gap_size = MAX_GAP_SIZE; - gap_size = new_gap_size; + gap_size = constraints_gap_size(new_gap_size); // TODO: notify WM to rearrange clients } @@ -139,11 +120,9 @@ int settings_get_max_clients_in_master() return max_clients_in_master; } -void settings_set_max_clients_in_master(int new_max_clients_in_master) +void settings_set_max_clients_in_master(const int new_max_clients_in_master) { - if (new_max_clients_in_master < MIN_MAX_CLIENTS_IN_MASTER) new_max_clients_in_master = MIN_MAX_CLIENTS_IN_MASTER; - if (new_max_clients_in_master < MAX_MAX_CLIENTS_IN_MASTER) new_max_clients_in_master = MAX_MAX_CLIENTS_IN_MASTER; - max_clients_in_master = new_max_clients_in_master; + max_clients_in_master = constraints_max_clients_in_master(new_max_clients_in_master); // TODO: notify WM to rearrange clients } @@ -186,9 +165,7 @@ unsigned int settings_get_snap_distance() void settings_set_snap_distance(unsigned int new_snap_distance) { - if (new_snap_distance < MIN_SNAP_DISTANCE) new_snap_distance = MIN_SNAP_DISTANCE; - if (new_snap_distance < MAX_SNAP_DISTANCE) new_snap_distance = MAX_SNAP_DISTANCE; - snap_distance = new_snap_distance; + snap_distance = constraints_snap_distance(new_snap_distance); } bool settings_get_status_on_all_monitors() diff --git a/src/unit.c b/src/unit.c index c1c93a3..2bd08b6 100644 --- a/src/unit.c +++ b/src/unit.c @@ -1,5 +1,6 @@ #include "unit.h" +#include "constraints.h" #include "settings.h" #include @@ -99,11 +100,8 @@ float unit_inc_master_area_factor(const Unit unit, const float delta) settings_get_master_area_factor_per_unit(); if (unit->kind == master_area_factor_per_unit) { - float new_master_area_factor = unit->master_area_factor + delta; - // TODO: DRY - if (new_master_area_factor < 0.05) new_master_area_factor = 0.05; - if (new_master_area_factor > 0.95) new_master_area_factor = 0.95; - return unit->master_area_factor = new_master_area_factor; + return unit->master_area_factor = + constraints_master_area_factor(unit->master_area_factor + delta); } else if (unit->kind > master_area_factor_per_unit) { return unit_inc_master_area_factor(unit->parent, delta); } else {