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.
This commit is contained in:
Sam Mason 2023-11-02 13:35:10 +00:00
parent 6f6b82793d
commit 338ee076e5
No known key found for this signature in database
GPG key ID: 0D059A3A7ECA31DA

View file

@ -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) {