Skip to content

Commit

Permalink
#164 render a brief change message to set as $WD_MESSAGE for the command
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Nov 11, 2024
1 parent 9f10e79 commit 791b892
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 79 deletions.
2 changes: 2 additions & 0 deletions inc/displ.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ struct Displ {
char *zxdg_output_manager_interface;

enum ConfigState config_state;

char *delta_message;
};

void displ_init(void);
Expand Down
4 changes: 4 additions & 0 deletions inc/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stddef.h>

#include "cfg.h"
#include "displ.h"
#include "head.h"
#include "slist.h"
#include "log.h"
Expand Down Expand Up @@ -35,5 +36,8 @@ 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);

// length 1024 * 64, consumer frees
char *render_deltas_brief(const enum ConfigState config_state, const struct SList * const heads);

#endif // INFO_H

82 changes: 82 additions & 0 deletions src/info.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <wayland-util.h>

#include "info.h"
Expand Down Expand Up @@ -443,3 +444,84 @@ void print_heads(enum LogThreshold t, enum InfoEvent event, struct SList *heads)
}
}

char *render_deltas_brief(const enum ConfigState config_state, const struct SList * const heads) {
if (!heads) {
return NULL;
}

static const int len = 1024 * 64;

char *buf = (char*)calloc(len, sizeof(char));
char *bufp = buf;

for (const struct SList *i = heads; i; i = i->nex) {
const struct Head * head = i->val;

if (head->current.enabled && !head->desired.enabled) {
bufp += snprintf(bufp, len - (bufp - buf), "%s: disabled\n", head->name);
continue;
}

// mode changes happen in their own operation
if (head_current_mode_not_desired(head)) {
bufp += snprintf(bufp, len - (bufp - buf), "%s%s:\n mode: %dx%d@%dHz -> %dx%d@%dHz\n", // line up with vrr
head->name,
(!head->current.enabled && head->desired.enabled) ? ": enabled" : "",
head->current.mode->width,
head->current.mode->height,
mhz_to_hz_rounded(head->current.mode->refresh_mhz),
head->desired.mode->width,
head->desired.mode->height,
mhz_to_hz_rounded(head->desired.mode->refresh_mhz)
);
continue;
}

if (!head->current.enabled && head->desired.enabled) {
bufp += snprintf(bufp, len - (bufp - buf), "%s: enabled\n", head->name);
continue;
}

// adaptive sync changes happen in their own operation
if (head_current_adaptive_sync_not_desired(head)) {
bufp += snprintf(bufp, len - (bufp - buf), "%s:\n VRR: %s\n", // line up with mode
head->name,
head->desired.adaptive_sync == ZWLR_OUTPUT_HEAD_V1_ADAPTIVE_SYNC_STATE_ENABLED ? "on" : "off"
);
continue;
}

if (head_current_not_desired(head)) {
bufp += snprintf(bufp, len - (bufp - buf), "%s:\n", head->name);

if (head->current.scale != head->desired.scale) {
bufp += snprintf(bufp, len - (bufp - buf), " scale: %.3f -> %.3f\n",
wl_fixed_to_double(head->current.scale),
wl_fixed_to_double(head->desired.scale)
);
}

if (head->current.transform != head->desired.transform) {
bufp += snprintf(bufp, len - (bufp - buf), " transform: %s -> %s\n",
head->current.transform ? transform_name(head->current.transform) : "none",
head->desired.transform ? transform_name(head->desired.transform) : "none"
);
}

if (head->current.x != head->desired.x || head->current.y != head->desired.y) {
bufp += snprintf(bufp, len - (bufp - buf), " position: %d,%d -> %d,%d\n",
head->current.x, head->current.y,
head->desired.x, head->desired.y
);
}
}
}

// strip trailing newline
if (bufp > buf) {
*(bufp - 1) = '\0';
}

return buf;
}

8 changes: 7 additions & 1 deletion src/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ void apply(void) {

zwlr_output_configuration_v1_apply(zwlr_config);

free(displ->delta_message);
displ->delta_message = render_deltas_brief(displ->config_state, heads_changing);

displ->config_state = OUTSTANDING;

slist_free(&heads_changing);
Expand Down Expand Up @@ -365,7 +368,7 @@ void handle_success(void) {
log_info("\nExecuting CHANGE_SUCCESS_CMD:");
log_info(" %s", cfg->change_success_cmd);

spawn_sh_cmd(cfg->change_success_cmd, "handle_success");
spawn_sh_cmd(cfg->change_success_cmd, displ->delta_message);
}

log_info("\nChanges successful");
Expand Down Expand Up @@ -435,6 +438,9 @@ void layout(void) {
break;
}

free(displ->delta_message);
displ->delta_message = NULL;

desire();
apply();
}
Expand Down
7 changes: 6 additions & 1 deletion src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
Expand Down Expand Up @@ -105,9 +106,13 @@ void pid_file_create(void) {
}

void spawn_sh_cmd(const char * const command, char * const message) {
if (!command || !message)
return;

// experiments show that environment variable length tops out at 128k: variable itself plus contents
// message[1024 * 120] = '\0';
if (strlen(message) > 1024 * 120) {
message[1024 * 120] = '\0';
}

pid_t pid = fork();
if (pid < 0) {
Expand Down
12 changes: 6 additions & 6 deletions tst/info/print-head-arrived-all.log
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

name Arrived:
name1 Arrived:
info:
name: 'name'
make: 'make'
model: 'model'
serial: 'serial_number'
desc: 'description'
name: 'name1'
make: 'make1'
model: 'model1'
serial: 'serial_number1'
desc: 'description1'
width: 1mm
height: 2mm
dpi: 2540.00 @ 100x200
Expand Down
2 changes: 1 addition & 1 deletion tst/info/print-head-deltas-disable.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

name Changing:
name1 Changing:
from:
scale: 2.000 (26.458)
position: 700,800
Expand Down
2 changes: 1 addition & 1 deletion tst/info/print-head-deltas-enable.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

name Changing:
name1 Changing:
from:
mode: 100x200@30Hz (30,000mHz) (preferred)
VRR: off
Expand Down
2 changes: 1 addition & 1 deletion tst/info/print-head-deltas-mode.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

name Changing:
name1 Changing:
from:
scale: 2.000 (26.458)
position: 700,800
Expand Down
2 changes: 1 addition & 1 deletion tst/info/print-head-deltas-other.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

name Changing:
name1 Changing:
from:
scale: 2.000 (26.458)
position: 700,800
Expand Down
2 changes: 1 addition & 1 deletion tst/info/print-head-deltas-vrr.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

name Changing:
name1 Changing:
from:
scale: 2.000 (26.458)
position: 700,800
Expand Down
6 changes: 3 additions & 3 deletions tst/info/print-head-departed-ok.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

name Departed:
name: 'name'
desc: 'description'
name1 Departed:
name: 'name1'
desc: 'description1'

Loading

0 comments on commit 791b892

Please sign in to comment.