Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

73 DISABLED, SCALE, MODE regex #74

Merged
merged 4 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion inc/head.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ bool head_matches_name_desc_exact(const void *head, const void *name_desc);

bool head_matches_name_desc_regex(const void *head, const void *name_desc);

bool head_matches_name_desc_fuzzy(const void *h, const void *name_desc);

bool head_matches_name_desc_partial(const void *head, const void *name_desc);
bool head_name_desc_partial_matches_head(const void *name_desc, const void *head);

bool head_matches_name_desc(const void *head, const void *name_desc);

bool head_name_desc_matches_head(const void *name_desc, const void *head);

wl_fixed_t head_auto_scale(struct Head *head);

Expand Down
4 changes: 4 additions & 0 deletions inc/mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ struct ModesResRefresh {
struct SList *modes;
};

struct Mode *mode_preferred(struct Head *head);

struct Mode *mode_max_preferred(struct Head *head);

int32_t mhz_to_hz(int32_t mhz);

double mode_dpi(struct Mode *mode);
Expand Down
71 changes: 14 additions & 57 deletions src/head.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,64 +23,15 @@ bool head_is_max_preferred_refresh(struct Head *head) {
return false;

for (struct SList *i = cfg->max_preferred_refresh_name_desc; i; i = i->nex) {
if (head_matches_name_desc_partial(head, i->val)) {
if (head_matches_name_desc(head, i->val)) {
return true;
}
}
return false;
}

bool head_matches_user_mode(const void *user_mode, const void *head) {
return user_mode && head && head_matches_name_desc_partial((struct Head*)head, ((struct UserMode*)user_mode)->name_desc);
}

struct Mode *preferred_mode(struct Head *head) {
if (!head)
return NULL;

struct Mode *mode = NULL;
for (struct SList *i = head->modes; i; i = i->nex) {
if (!i->val)
continue;
mode = i->val;

if (mode->preferred && !slist_find_equal(head->modes_failed, NULL, mode)) {
return mode;
}
}

return NULL;
}

struct Mode *max_preferred_mode(struct Head *head) {
struct Mode *preferred = preferred_mode(head);

if (!preferred)
return NULL;

struct Mode *mode = NULL, *max = NULL;

for (struct SList *i = head->modes; i; i = i->nex) {
if (!i->val)
continue;
mode = i->val;

if (slist_find_equal(head->modes_failed, NULL, mode)) {
continue;
}

if (mode->width != preferred->width || mode->height != preferred->height) {
continue;
}

if (!max) {
max = mode;
} else if (mode->refresh_mhz > max->refresh_mhz) {
max = mode;
}
}

return max;
return user_mode && head && head_matches_name_desc((struct Head*)head, ((struct UserMode*)user_mode)->name_desc);
}

struct Mode *max_mode(struct Head *head) {
Expand Down Expand Up @@ -155,7 +106,7 @@ bool head_matches_name_desc_regex(const void *h, const void *n) {
return !result;
}

bool head_matches_name_desc_partial(const void *h, const void *n) {
bool head_matches_name_desc_fuzzy(const void *h, const void *n) {
const struct Head *head = h;
const char *name_desc = n;

Expand All @@ -168,8 +119,14 @@ bool head_matches_name_desc_partial(const void *h, const void *n) {
);
}

bool head_name_desc_partial_matches_head(const void *n, const void *h) {
return head_matches_name_desc_partial(h, n);
bool head_matches_name_desc(const void *h, const void *n) {
return head_matches_name_desc_exact(h, n) ||
head_matches_name_desc_regex(h, n) ||
head_matches_name_desc_fuzzy(h, n);
}

bool head_name_desc_matches_head(const void *n, const void *h) {
return head_matches_name_desc(h, n);
}

bool head_matches_name_desc_exact(const void *h, const void *n) {
Expand Down Expand Up @@ -244,17 +201,17 @@ struct Mode *head_find_mode(struct Head *head) {
// always preferred
if (!mode) {
if (head_is_max_preferred_refresh(head)) {
mode = max_preferred_mode(head);
mode = mode_max_preferred(head);
} else {
mode = preferred_mode(head);
mode = mode_preferred(head);
}
if (!mode && !head->warned_no_preferred) {
head->warned_no_preferred = true;
log_info("\n%s: No preferred mode, falling back to maximum available", head->name);
}
}

// last change maximum
// last chance maximum
if (!mode) {
mode = max_mode(head);
}
Expand Down
8 changes: 4 additions & 4 deletions src/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ struct SList *order_heads(struct SList *order_name_desc, struct SList *heads) {
i++;
}

// partial
// fuzzy
i = 0;
for (struct SList *o = order_name_desc; o; o = o->nex) {
slist_move(&order_heads[i], &sorting, head_matches_name_desc_partial, o->val);
slist_move(&order_heads[i], &sorting, head_matches_name_desc_fuzzy, o->val);
i++;
}

Expand Down Expand Up @@ -153,7 +153,7 @@ void desire_enabled(struct Head *head) {
head->desired.enabled |= slist_length(heads) == 1;

// explicitly disabled
head->desired.enabled &= slist_find_equal(cfg->disabled_name_desc, head_name_desc_partial_matches_head, head) == NULL;
head->desired.enabled &= slist_find_equal(cfg->disabled_name_desc, head_name_desc_matches_head, head) == NULL;
}

void desire_mode(struct Head *head) {
Expand Down Expand Up @@ -191,7 +191,7 @@ void desire_scale(struct Head *head) {
struct UserScale *user_scale;
for (struct SList *i = cfg->user_scales; i; i = i->nex) {
user_scale = (struct UserScale*)i->val;
if (head_matches_name_desc_partial(head, user_scale->name_desc)) {
if (head_matches_name_desc(head, user_scale->name_desc)) {
head->desired.scale = wl_fixed_from_double(user_scale->scale);
return;
}
Expand Down
69 changes: 45 additions & 24 deletions src/marshalling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ extern "C" {
#include "server.h"
}

// If this is a regex pattern, attempt to compile it before including it in configuration.
bool validate_regex(const char *pattern, enum CfgElement element) {
bool rc = true;
if (pattern[0] == '!') {
regex_t regex;
int result = regcomp(&regex, pattern + 1, REG_EXTENDED);
if (result) {
char err[1024];
regerror(result, &regex, err, 1024);
log_warn("Ignoring bad %s regex '%s': %s", cfg_element_name(element), pattern + 1, err);
rc = false;
}
regfree(&regex);
}
return rc;
}

bool parse_node_val_bool(const YAML::Node &node, const char *key, bool *val, const char *desc1, const char *desc2) {
if (node[key]) {
try {
Expand Down Expand Up @@ -264,20 +281,8 @@ void cfg_parse_node(struct Cfg *cfg, const YAML::Node &node) {
const std::string &order_str = order.as<std::string>();
const char *order_cstr = order_str.c_str();
if (!slist_find_equal(cfg->order_name_desc, slist_equal_strcmp, order_cstr)) {
// If this is a regex pattern, attempt to compile it before
// including it in order configuration.
if (order_cstr[0] == '!') {
regex_t regex;
int result = regcomp(&regex, order_cstr + 1, REG_EXTENDED);
if (result) {
char err[1024];
regerror(result, &regex, err, 1024);
log_warn("\nCould not compile ORDER regex '%s': %s", order_cstr + 1, err);
regfree(&regex);
continue;
} else {
regfree(&regex);
}
if (!validate_regex(order_cstr, ORDER)) {
continue;
}
slist_append(&cfg->order_name_desc, strdup(order_cstr));
}
Expand Down Expand Up @@ -321,6 +326,10 @@ void cfg_parse_node(struct Cfg *cfg, const YAML::Node &node) {
cfg_user_scale_free(user_scale);
continue;
}
if (!validate_regex(user_scale->name_desc, SCALE)) {
cfg_user_mode_free(user_scale);
continue;
}
if (!parse_node_val_float(scale, "SCALE", &user_scale->scale, "SCALE", user_scale->name_desc)) {
cfg_user_scale_free(user_scale);
continue;
Expand All @@ -339,6 +348,10 @@ void cfg_parse_node(struct Cfg *cfg, const YAML::Node &node) {
cfg_user_mode_free(user_mode);
continue;
}
if (!validate_regex(user_mode->name_desc, MODE)) {
cfg_user_mode_free(user_mode);
continue;
}
if (mode["MAX"] && !parse_node_val_bool(mode, "MAX", &user_mode->max, "MODE", user_mode->name_desc)) {
cfg_user_mode_free(user_mode);
continue;
Expand All @@ -362,21 +375,29 @@ void cfg_parse_node(struct Cfg *cfg, const YAML::Node &node) {
}

if (node["MAX_PREFERRED_REFRESH"]) {
const auto &name_desc = node["MAX_PREFERRED_REFRESH"];
for (const auto &name_desc : name_desc) {
const std::string &name_desc_str = name_desc.as<std::string>();
if (!slist_find_equal(cfg->max_preferred_refresh_name_desc, slist_equal_strcmp, name_desc_str.c_str())) {
slist_append(&cfg->max_preferred_refresh_name_desc, strdup(name_desc_str.c_str()));
const auto &maxes = node["MAX_PREFERRED_REFRESH"];
for (const auto &max : maxes) {
const std::string &max_str = max.as<std::string>();
const char *max_cstr = max_str.c_str();
if (!slist_find_equal(cfg->max_preferred_refresh_name_desc, slist_equal_strcmp, max_cstr)) {
if (!validate_regex(max_cstr, MAX_PREFERRED_REFRESH)) {
continue;
}
slist_append(&cfg->max_preferred_refresh_name_desc, strdup(max_cstr));
}
}
}

if (node["DISABLED"]) {
const auto &name_desc = node["DISABLED"];
for (const auto &name_desc : name_desc) {
const std::string &name_desc_str = name_desc.as<std::string>();
if (!slist_find_equal(cfg->disabled_name_desc, slist_equal_strcmp, name_desc_str.c_str())) {
slist_append(&cfg->disabled_name_desc, strdup(name_desc_str.c_str()));
const auto &disableds = node["DISABLED"];
for (const auto &disabled : disableds) {
const std::string &disabled_str = disabled.as<std::string>();
const char *disabled_cstr = disabled_str.c_str();
if (!slist_find_equal(cfg->disabled_name_desc, slist_equal_strcmp, disabled_cstr)) {
if (!validate_regex(disabled_cstr, DISABLED)) {
continue;
}
slist_append(&cfg->disabled_name_desc, strdup(disabled_cstr));
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions src/mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,55 @@
#include "head.h"
#include "list.h"

struct Mode *mode_preferred(struct Head *head) {
if (!head)
return NULL;

struct Mode *mode = NULL;
for (struct SList *i = head->modes; i; i = i->nex) {
if (!i->val)
continue;
mode = i->val;

if (mode->preferred && !slist_find_equal(head->modes_failed, NULL, mode)) {
return mode;
}
}

return NULL;
}

struct Mode *mode_max_preferred(struct Head *head) {
struct Mode *preferred = mode_preferred(head);

if (!preferred)
return NULL;

struct Mode *mode = NULL, *max = NULL;

for (struct SList *i = head->modes; i; i = i->nex) {
if (!i->val)
continue;
mode = i->val;

if (slist_find_equal(head->modes_failed, NULL, mode)) {
continue;
}

if (mode->width != preferred->width || mode->height != preferred->height) {
continue;
}

if (!max) {
max = mode;
} else if (mode->refresh_mhz > max->refresh_mhz) {
max = mode;
}
}

return max;
}

int32_t mhz_to_hz(int32_t mhz) {
return (mhz + 500) / 1000;
}
Expand Down
2 changes: 1 addition & 1 deletion tst/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tst-cli: tst/tst-cli.o $(OBJS)
$(CXX) -o $(@) $(^) $(LDFLAGS) $(LDLIBS) $(WRAPS)

tst-head: tst/tst-head.o $(OBJS)
$(CXX) -o $(@) $(^) $(LDFLAGS) $(LDLIBS) $(WRAPS),--wrap=mode_dpi,--wrap=mode_user_mode
$(CXX) -o $(@) $(^) $(LDFLAGS) $(LDLIBS) $(WRAPS),--wrap=mode_dpi,--wrap=mode_user_mode,--wrap=mode_max_preferred

tst-layout: tst/tst-layout.o $(OBJS)
$(CXX) -o $(@) $(^) $(LDFLAGS) $(LDLIBS) $(WRAPS),--wrap=lid_is_closed,--wrap=head_find_mode,--wrap=head_auto_scale
Expand Down
8 changes: 7 additions & 1 deletion tst/marshalling/cfg-bad.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ ARRANGE: BAD_ARRANGE
ALIGN: BAD_ALIGN
AUTO_SCALE: BAD_AUTO_SCALE
ORDER:
- '!('
- '!(order'
SCALE:
-
- NAME_DESC: BAD_SCALE_NAME
SCALE: BAD_SCALE_VAL
- NAME_DESC: MISSING_SCALE_VALUE
- NAME_DESC: '!(scale'
MODE:
-
- NAME_DESC: BAD_MODE_MAX
Expand All @@ -19,4 +20,9 @@ MODE:
HEIGHT: BAD_HEIGHT
- NAME_DESC: BAD_MODE_HZ
HZ: BAD_HZ
- NAME_DESC: '!(mode'
MAX_PREFERRED_REFRESH:
- '!(max'
DISABLED:
- '!(disabled'

Loading