From 338ee076e5b016da89b73c23d01652ddaf842c2a Mon Sep 17 00:00:00 2001 From: Sam Mason Date: Thu, 2 Nov 2023 13:35:10 +0000 Subject: [PATCH] More options for querying and changing opacity Allow the opacity command to return the current opacity, as per #7173, e.g.: swaymsg -r opacity get and make multiplicative changes easy, e.g.: swaymsg opacity mul 1.25 swaymsg opacity div 1.25 Multiplicative options are due to human senses tending to work on a log-scale, e.g. sound is measured with decibels. --- sway/commands/opacity.c | 43 +++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/sway/commands/opacity.c b/sway/commands/opacity.c index 96e6228e..d7773bb1 100644 --- a/sway/commands/opacity.c +++ b/sway/commands/opacity.c @@ -17,19 +17,46 @@ struct cmd_results *cmd_opacity(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "No current container"); } + enum { SET, PLUS, MINUS, MUL, DIV } op = SET; + char *argval = argv[0]; + + if (argc == 1) { + if (!strcasecmp(argv[0], "get")) { + return cmd_results_new(CMD_SUCCESS, "%f", con->alpha); + } + } else { + if ((error = checkarg(argc, "opacity", EXPECTED_EQUAL_TO, 2))) { + return error; + } + if (!strcasecmp(argv[0], "set")) { + op = SET; + } else if (!strcasecmp(argv[0], "plus")) { + op = PLUS; + } else if (!strcasecmp(argv[0], "minus")) { + op = MINUS; + } else if (!strcasecmp(argv[0], "mul")) { + op = MUL; + } else if (!strcasecmp(argv[0], "div")) { + op = DIV; + } else { + return cmd_results_new(CMD_INVALID, + "Expected: get,set|plus|minus|mul|div <0..1>: %s", argv[0]); + } + argval = argv[1]; + } + char *err; - float val = strtof(argc == 1 ? argv[0] : argv[1], &err); + float val = strtof(argval, &err); if (*err) { return cmd_results_new(CMD_INVALID, "opacity float invalid"); } - if (!strcasecmp(argv[0], "plus")) { - val = con->alpha + val; - } else if (!strcasecmp(argv[0], "minus")) { - val = con->alpha - val; - } else if (argc > 1 && strcasecmp(argv[0], "set")) { - return cmd_results_new(CMD_INVALID, - "Expected: set|plus|minus <0..1>: %s", argv[0]); + switch (op) { + case SET: break; + case PLUS: val += con->alpha; break; + case MINUS: val -= con->alpha; break; + case MUL: val *= con->alpha; break; + case DIV: val = con->alpha / val; break; } if (val < 0 || val > 1) {