aboutsummaryrefslogtreecommitdiff
path: root/sway/commands/input/tool_mode.c
blob: 04316857fae7bd199ec9d4ae8cc968ddca893f8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <strings.h>
#include "sway/commands.h"
#include "sway/config.h"

static void set_tool_mode(struct input_config *ic,
		enum wlr_tablet_tool_type type, enum sway_tablet_tool_mode mode) {
	for (int i = 0; i < ic->tools->length; i++) {
		struct input_config_tool *tool = ic->tools->items[i];
		if (tool->type == type) {
			tool->mode = mode;
			return;
		}
	}

	struct input_config_tool *tool = calloc(1, sizeof(*tool));
	if (tool) {
		tool->type = type;
		tool->mode = mode;
		list_add(ic->tools, tool);
	}
}

struct cmd_results *input_cmd_tool_mode(int argc, char **argv) {
	struct cmd_results *error;
	if ((error = checkarg(argc, "tool_mode", EXPECTED_AT_LEAST, 2))) {
		return error;
	}

	struct input_config *ic = config->handler_context.input_config;
	if (!ic) {
		return cmd_results_new(CMD_FAILURE, "No input device defined.");
	}

	enum sway_tablet_tool_mode tool_mode;
	if (!strcasecmp(argv[1], "absolute")) {
		tool_mode = SWAY_TABLET_TOOL_MODE_ABSOLUTE;
	} else if (!strcasecmp(argv[1], "relative")) {
		tool_mode = SWAY_TABLET_TOOL_MODE_RELATIVE;
	} else {
		goto invalid_command;
	}

	if (!strcasecmp(argv[0], "*")) {
		set_tool_mode(ic, WLR_TABLET_TOOL_TYPE_PEN, tool_mode);
		set_tool_mode(ic, WLR_TABLET_TOOL_TYPE_ERASER, tool_mode);
		set_tool_mode(ic, WLR_TABLET_TOOL_TYPE_BRUSH, tool_mode);
		set_tool_mode(ic, WLR_TABLET_TOOL_TYPE_PENCIL, tool_mode);
		set_tool_mode(ic, WLR_TABLET_TOOL_TYPE_AIRBRUSH, tool_mode);
	} else {
		enum wlr_tablet_tool_type tool_type;
		if (!strcasecmp(argv[0], "pen")) {
			tool_type = WLR_TABLET_TOOL_TYPE_PEN;
		} else if (!strcasecmp(argv[0], "eraser")) {
			tool_type = WLR_TABLET_TOOL_TYPE_ERASER;
		} else if (!strcasecmp(argv[0], "brush")) {
			tool_type = WLR_TABLET_TOOL_TYPE_BRUSH;
		} else if (!strcasecmp(argv[0], "pencil")) {
			tool_type = WLR_TABLET_TOOL_TYPE_PENCIL;
		} else if (!strcasecmp(argv[0], "airbrush")) {
			tool_type = WLR_TABLET_TOOL_TYPE_AIRBRUSH;
		} else {
			goto invalid_command;
		}

		set_tool_mode(ic, tool_type, tool_mode);
	}

	return cmd_results_new(CMD_SUCCESS, NULL);

invalid_command:
	return cmd_results_new(CMD_INVALID,
		"Expected 'tool_mode <tool> <absolute|relative>'");
}