Skip to content

Commit

Permalink
#164 add LayoutDelta to encapsulate changes in flight
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Dec 22, 2024
1 parent 0ae9f29 commit 861ad68
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 83 deletions.
12 changes: 8 additions & 4 deletions inc/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ extern struct Lid *lid;

extern struct SList *cfg_file_paths;

extern struct Head *head_changing_mode;
extern struct Head *head_changing_adaptive_sync;

extern char *deltas_brief;
// layout change in progress
// single operations for individual heads are sometimes set
struct LayoutDelta {
struct Head *head_mode;
struct Head *head_adaptive_sync;
char *brief;
};
extern struct LayoutDelta layout_delta;

#endif // GLOBAL_H
6 changes: 3 additions & 3 deletions inc/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ void info_user_mode_string(struct UserMode *user_mode, char *buf, size_t nbuf);
void info_mode_string(struct Mode *mode, char *buf, size_t nbuf);

// LEN_BRIEF, consumer frees
char *brief_deltas(const enum ConfigState config_state, const struct SList * const heads);
char *delta_brief(const enum ConfigState config_state, const struct SList * const heads);

// LEN_BRIEF, consumer frees
char *brief_delta_mode(const enum ConfigState config_state, const struct Head * const head);
char *delta_brief_mode(const enum ConfigState config_state, const struct Head * const head);

// LEN_BRIEF, consumer frees
char *brief_delta_adaptive_sync(const enum ConfigState config_state, const struct Head * const head);
char *delta_brief_adaptive_sync(const enum ConfigState config_state, const struct Head * const head);

#endif // INFO_H

11 changes: 7 additions & 4 deletions src/global.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include <stddef.h>

#include "global.h"

struct Displ *displ = NULL;
struct Lid *lid = NULL;
struct Cfg *cfg = NULL;

struct SList *cfg_file_paths = NULL;

struct Head *head_changing_mode = NULL;
struct Head *head_changing_adaptive_sync = NULL;

char *deltas_brief = NULL;
struct LayoutDelta layout_delta = {
.head_mode = NULL,
.head_adaptive_sync = NULL,
.brief = NULL,
};

6 changes: 3 additions & 3 deletions src/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ void print_heads(enum LogThreshold t, enum InfoEvent event, struct SList *heads)
}
}

char *brief_deltas(const enum ConfigState config_state, const struct SList * const heads) {
char *delta_brief(const enum ConfigState config_state, const struct SList * const heads) {
if (!heads) {
return NULL;
}
Expand Down Expand Up @@ -503,7 +503,7 @@ char *brief_deltas(const enum ConfigState config_state, const struct SList * con
return buf;
}

char *brief_delta_mode(const enum ConfigState config_state, const struct Head * const head) {
char *delta_brief_mode(const enum ConfigState config_state, const struct Head * const head) {
if (!head) {
return NULL;
}
Expand Down Expand Up @@ -539,7 +539,7 @@ char *brief_delta_mode(const enum ConfigState config_state, const struct Head *
}


char *brief_delta_adaptive_sync(const enum ConfigState config_state, const struct Head * const head) {
char *delta_brief_adaptive_sync(const enum ConfigState config_state, const struct Head * const head) {
if (!head) {
return NULL;
}
Expand Down
66 changes: 33 additions & 33 deletions src/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ void desire(void) {

void apply(void) {
struct SList *heads_changing = NULL;
head_changing_mode = NULL;
head_changing_adaptive_sync = NULL;
layout_delta.head_mode = NULL;
layout_delta.head_adaptive_sync = NULL;

// determine whether changes are needed before initiating output configuration
struct SList *i = heads;
Expand All @@ -281,25 +281,25 @@ void apply(void) {
struct zwlr_output_configuration_v1 *zwlr_config = zwlr_output_manager_v1_create_configuration(displ->zwlr_output_manager, displ->zwlr_output_manager_serial);
zwlr_output_configuration_v1_add_listener(zwlr_config, zwlr_output_configuration_listener(), displ);

if ((head_changing_mode = slist_find_val(heads, head_current_mode_not_desired))) {
if ((layout_delta.head_mode = slist_find_val(heads, head_current_mode_not_desired))) {

print_head(INFO, DELTA, head_changing_mode);
print_head(INFO, DELTA, layout_delta.head_mode);

// mode change in its own operation; mode change desire is always enabled
head_changing_mode->zwlr_config_head = zwlr_output_configuration_v1_enable_head(zwlr_config, head_changing_mode->zwlr_head);
zwlr_output_configuration_head_v1_set_mode(head_changing_mode->zwlr_config_head, head_changing_mode->desired.mode->zwlr_mode);
layout_delta.head_mode->zwlr_config_head = zwlr_output_configuration_v1_enable_head(zwlr_config, layout_delta.head_mode->zwlr_head);
zwlr_output_configuration_head_v1_set_mode(layout_delta.head_mode->zwlr_config_head, layout_delta.head_mode->desired.mode->zwlr_mode);

deltas_brief = brief_delta_mode(displ->config_state, head_changing_mode);
layout_delta.brief = delta_brief_mode(displ->config_state, layout_delta.head_mode);

} else if ((head_changing_adaptive_sync = slist_find_val(heads, head_current_adaptive_sync_not_desired))) {
} else if ((layout_delta.head_adaptive_sync = slist_find_val(heads, head_current_adaptive_sync_not_desired))) {

print_head(INFO, DELTA, head_changing_adaptive_sync);
print_head(INFO, DELTA, layout_delta.head_adaptive_sync);

// adaptive sync change in its own operation; adaptive sync change desire is always enabled
head_changing_adaptive_sync->zwlr_config_head = zwlr_output_configuration_v1_enable_head(zwlr_config, head_changing_adaptive_sync->zwlr_head);
zwlr_output_configuration_head_v1_set_adaptive_sync(head_changing_adaptive_sync->zwlr_config_head, head_changing_adaptive_sync->desired.adaptive_sync);
layout_delta.head_adaptive_sync->zwlr_config_head = zwlr_output_configuration_v1_enable_head(zwlr_config, layout_delta.head_adaptive_sync->zwlr_head);
zwlr_output_configuration_head_v1_set_adaptive_sync(layout_delta.head_adaptive_sync->zwlr_config_head, layout_delta.head_adaptive_sync->desired.adaptive_sync);

deltas_brief = brief_delta_adaptive_sync(displ->config_state, head_changing_adaptive_sync);
layout_delta.brief = delta_brief_adaptive_sync(displ->config_state, layout_delta.head_adaptive_sync);

} else {

Expand All @@ -319,7 +319,7 @@ void apply(void) {
}
}

deltas_brief = brief_deltas(displ->config_state, heads_changing);
layout_delta.brief = delta_brief(displ->config_state, heads_changing);
}

zwlr_output_configuration_v1_apply(zwlr_config);
Expand All @@ -338,17 +338,17 @@ void report_adaptive_sync_fail(struct Head *head) {
}

void handle_success(void) {
if (head_changing_mode) {
if (layout_delta.head_mode) {

// successful mode change is not always reported
head_changing_mode->current.mode = head_changing_mode->desired.mode;
layout_delta.head_mode->current.mode = layout_delta.head_mode->desired.mode;

head_changing_mode = NULL;
layout_delta.head_mode = NULL;

} else if (head_changing_adaptive_sync) {
} else if (layout_delta.head_adaptive_sync) {

struct Head *head = head_changing_adaptive_sync;
head_changing_adaptive_sync = NULL;
struct Head *head = layout_delta.head_adaptive_sync;
layout_delta.head_adaptive_sync = NULL;

// sway reports adaptive sync failure as success
if (head_current_adaptive_sync_not_desired(head)) {
Expand All @@ -358,12 +358,12 @@ void handle_success(void) {
}
}

if (!head_changing_adaptive_sync && cfg->change_success_cmd) {
if (cfg->change_success_cmd) {
log_info("\nExecuting CHANGE_SUCCESS_CMD:");
log_info(" %s", cfg->change_success_cmd);

const struct STable *env = stable_init(1, 1, false);
stable_put(env, "WD_CHANGE_SUCCESS_MSG", deltas_brief);
stable_put(env, "WD_CHANGE_SUCCESS_MSG", layout_delta.brief);
spawn_sh_cmd(cfg->change_success_cmd, env);
stable_free(env);
}
Expand All @@ -373,26 +373,26 @@ void handle_success(void) {

void handle_failure(void) {

if (head_changing_mode) {
if (layout_delta.head_mode) {
log_error("\nChanges failed");

// mode setting failure, try again
log_error(" %s:", head_changing_mode->name);
print_mode(ERROR, head_changing_mode->desired.mode);
slist_append(&head_changing_mode->modes_failed, head_changing_mode->desired.mode);
log_error(" %s:", layout_delta.head_mode->name);
print_mode(ERROR, layout_delta.head_mode->desired.mode);
slist_append(&layout_delta.head_mode->modes_failed, layout_delta.head_mode->desired.mode);

// current mode may be misreported
head_changing_mode->current.mode = NULL;
layout_delta.head_mode->current.mode = NULL;

head_changing_mode = NULL;
layout_delta.head_mode = NULL;

} else if (head_changing_adaptive_sync && head_current_adaptive_sync_not_desired(head_changing_adaptive_sync)) {
} else if (layout_delta.head_adaptive_sync && head_current_adaptive_sync_not_desired(layout_delta.head_adaptive_sync)) {

// river reports adaptive sync failure as failure
report_adaptive_sync_fail(head_changing_adaptive_sync);
head_changing_adaptive_sync->adaptive_sync_failed = true;
report_adaptive_sync_fail(layout_delta.head_adaptive_sync);
layout_delta.head_adaptive_sync->adaptive_sync_failed = true;

head_changing_adaptive_sync = NULL;
layout_delta.head_adaptive_sync = NULL;

} else {
log_error("\nChanges failed");
Expand Down Expand Up @@ -435,8 +435,8 @@ void layout(void) {
break;
}

free(deltas_brief);
deltas_brief = NULL;
free(layout_delta.brief);
layout_delta.brief = NULL;

desire();
apply();
Expand Down
42 changes: 21 additions & 21 deletions tst/tst-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ void print_head_deltas__enable(void **state) {
free(expected_log);
}

void brief_delta_mode__to_no(void **state) {
void delta_brief_mode__to_no(void **state) {
struct State *s = *state;

s->head1->desired.mode = NULL;

char *deltas = brief_delta_mode(SUCCEEDED, s->head1);
char *deltas = delta_brief_mode(SUCCEEDED, s->head1);

assert_string_equal(deltas, ""
"description1\n"
Expand All @@ -275,12 +275,12 @@ void brief_delta_mode__to_no(void **state) {
free(deltas);
}

void brief_delta_mode__from_no(void **state) {
void delta_brief_mode__from_no(void **state) {
struct State *s = *state;

s->head2->current.mode = NULL;

char *deltas = brief_delta_mode(SUCCEEDED, s->head2);
char *deltas = delta_brief_mode(SUCCEEDED, s->head2);

assert_string_equal(deltas, ""
"name2\n"
Expand All @@ -292,13 +292,13 @@ void brief_delta_mode__from_no(void **state) {
free(deltas);
}

void brief_delta_adaptive_sync__on(void **state) {
void delta_brief_adaptive_sync__on(void **state) {
struct State *s = *state;

s->head1->current.adaptive_sync = ZWLR_OUTPUT_HEAD_V1_ADAPTIVE_SYNC_STATE_DISABLED;
s->head1->desired.adaptive_sync = ZWLR_OUTPUT_HEAD_V1_ADAPTIVE_SYNC_STATE_ENABLED;

char *deltas = brief_delta_adaptive_sync(SUCCEEDED, s->head1);
char *deltas = delta_brief_adaptive_sync(SUCCEEDED, s->head1);

assert_string_equal(deltas, ""
"description1\n"
Expand All @@ -310,13 +310,13 @@ void brief_delta_adaptive_sync__on(void **state) {
free(deltas);
}

void brief_delta_adaptive_sync__off(void **state) {
void delta_brief_adaptive_sync__off(void **state) {
struct State *s = *state;

s->head2->current.adaptive_sync = ZWLR_OUTPUT_HEAD_V1_ADAPTIVE_SYNC_STATE_ENABLED;
s->head2->desired.adaptive_sync = ZWLR_OUTPUT_HEAD_V1_ADAPTIVE_SYNC_STATE_DISABLED;

char *deltas = brief_delta_adaptive_sync(SUCCEEDED, s->head2);
char *deltas = delta_brief_adaptive_sync(SUCCEEDED, s->head2);

assert_string_equal(deltas, ""
"name2\n"
Expand All @@ -328,10 +328,10 @@ void brief_delta_adaptive_sync__off(void **state) {
free(deltas);
}

void brief_deltas__all(void **state) {
void delta_brief__all(void **state) {
struct State *s = *state;

char *deltas = brief_deltas(SUCCEEDED, s->heads);
char *deltas = delta_brief(SUCCEEDED, s->heads);

assert_string_equal(deltas, ""
"description1\n"
Expand All @@ -349,7 +349,7 @@ void brief_deltas__all(void **state) {
free(deltas);
}

void brief_deltas__enabled(void **state) {
void delta_brief__enabled(void **state) {
struct State *s = *state;

s->head1->current.enabled = false;
Expand All @@ -358,7 +358,7 @@ void brief_deltas__enabled(void **state) {
s->head2->current.enabled = false;
s->head2->desired.enabled = true;

char *deltas = brief_deltas(SUCCEEDED, s->heads);
char *deltas = delta_brief(SUCCEEDED, s->heads);

assert_string_equal(deltas, ""
"description1 enabled\n"
Expand All @@ -370,7 +370,7 @@ void brief_deltas__enabled(void **state) {
free(deltas);
}

void brief_deltas__disabled(void **state) {
void delta_brief__disabled(void **state) {
struct State *s = *state;

s->head1->current.enabled = true;
Expand All @@ -379,7 +379,7 @@ void brief_deltas__disabled(void **state) {
s->head2->current.enabled = true;
s->head2->desired.enabled = false;

char *deltas = brief_deltas(SUCCEEDED, s->heads);
char *deltas = delta_brief(SUCCEEDED, s->heads);

assert_string_equal(deltas, ""
"description1 disabled\n"
Expand All @@ -406,15 +406,15 @@ int main(void) {
TEST(print_head_deltas__disable),
TEST(print_head_deltas__enable),

TEST(brief_delta_mode__to_no),
TEST(brief_delta_mode__from_no),
TEST(delta_brief_mode__to_no),
TEST(delta_brief_mode__from_no),

TEST(brief_delta_adaptive_sync__on),
TEST(brief_delta_adaptive_sync__off),
TEST(delta_brief_adaptive_sync__on),
TEST(delta_brief_adaptive_sync__off),

TEST(brief_deltas__all),
TEST(brief_deltas__enabled),
TEST(brief_deltas__disabled),
TEST(delta_brief__all),
TEST(delta_brief__enabled),
TEST(delta_brief__disabled),
};

return RUN(tests);
Expand Down
Loading

0 comments on commit 861ad68

Please sign in to comment.