From 140ce785fe52c511b220a0d74997466c995db695 Mon Sep 17 00:00:00 2001
From: Jeff Peeler <jpeeler@gmail.com>
Date: Mon, 15 Jul 2019 10:06:05 -0400
Subject: [PATCH] cmd_opacity: add relative opacity changes

This enhances the opacity command to support relative assignment as well
as the currently implemented absolute assignment. The syntax is copied
from the same format that gaps uses for relative and absolute setting.
An example usage in a sway config looks like:

// relative change (this feature)
bindsym button4 opacity plus .1
bindsym button5 opacity minus .1

// absolute change (this feature)
bindsym button4 opacity set 1
bindsym button5 opacity set .3

// old way, still supported
bindsym button4 opacity 1
bindsym button5 opacity .3
---
 sway/commands/opacity.c | 37 ++++++++++++++++++++-----------------
 sway/sway.5.scd         |  6 +++---
 2 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/sway/commands/opacity.c b/sway/commands/opacity.c
index 14a070518..96e6228ed 100644
--- a/sway/commands/opacity.c
+++ b/sway/commands/opacity.c
@@ -1,21 +1,13 @@
 #include <assert.h>
 #include <stdlib.h>
+#include <strings.h>
 #include "sway/commands.h"
 #include "sway/tree/view.h"
 #include "log.h"
 
-static bool parse_opacity(const char *opacity, float *val) {
-	char *err;
-	*val = strtof(opacity, &err);
-	if (*val < 0 || *val > 1 || *err) {
-		return false;
-	}
-	return true;
-}
-
 struct cmd_results *cmd_opacity(int argc, char **argv) {
 	struct cmd_results *error = NULL;
-	if ((error = checkarg(argc, "opacity", EXPECTED_EQUAL_TO, 1))) {
+	if ((error = checkarg(argc, "opacity", EXPECTED_AT_LEAST, 1))) {
 		return error;
 	}
 
@@ -25,15 +17,26 @@ struct cmd_results *cmd_opacity(int argc, char **argv) {
 		return cmd_results_new(CMD_FAILURE, "No current container");
 	}
 
-	float opacity = 0.0f;
-
-	if (!parse_opacity(argv[0], &opacity)) {
-		return cmd_results_new(CMD_INVALID,
-				"Invalid value (expected 0..1): %s", argv[0]);
+	char *err;
+	float val = strtof(argc == 1 ? argv[0] : argv[1], &err);
+	if (*err) {
+		return cmd_results_new(CMD_INVALID, "opacity float invalid");
 	}
 
-	con->alpha = opacity;
-	container_damage_whole(con);
+	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]);
+	}
 
+	if (val < 0 || val > 1) {
+		return cmd_results_new(CMD_FAILURE, "opacity value out of bounds");
+	}
+
+	con->alpha = val;
+	container_damage_whole(con);
 	return cmd_results_new(CMD_SUCCESS, NULL);
 }
diff --git a/sway/sway.5.scd b/sway/sway.5.scd
index 3e445e0e2..768f125ec 100644
--- a/sway/sway.5.scd
+++ b/sway/sway.5.scd
@@ -664,9 +664,9 @@ The default colors are:
 	Any mark that starts with an underscore will not be drawn even if
 	*show_marks* is yes. The default is _yes_.
 
-*opacity* <value>
-	Set the opacity of the window between 0 (completely transparent) and 1
-	(completely opaque).
+*opacity* [set|plus|minus] <value>
+	Adjusts the opacity of the window between 0 (completely transparent) and
+	1 (completely opaque). If the operation is omitted, _set_ will be used.
 
 *tiling_drag*  enable|disable|toggle
 	Sets whether or not tiling containers can be dragged with the mouse. If