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