diff options
Diffstat (limited to 'sway/commands/input/map_from_region.c')
-rw-r--r-- | sway/commands/input/map_from_region.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/sway/commands/input/map_from_region.c b/sway/commands/input/map_from_region.c new file mode 100644 index 00000000..80bb856d --- /dev/null +++ b/sway/commands/input/map_from_region.c | |||
@@ -0,0 +1,79 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
2 | #include <stdbool.h> | ||
3 | #include <string.h> | ||
4 | #include <strings.h> | ||
5 | #include "log.h" | ||
6 | #include "sway/commands.h" | ||
7 | #include "sway/config.h" | ||
8 | #include "sway/input/input-manager.h" | ||
9 | |||
10 | static bool parse_coords(const char *str, double *x, double *y, bool *mm) { | ||
11 | *mm = false; | ||
12 | |||
13 | char *end; | ||
14 | *x = strtod(str, &end); | ||
15 | if (end[0] != 'x') { | ||
16 | return false; | ||
17 | } | ||
18 | ++end; | ||
19 | |||
20 | *y = strtod(end, &end); | ||
21 | if (end[0] == 'm') { | ||
22 | // Expect mm | ||
23 | if (end[1] != 'm') { | ||
24 | return false; | ||
25 | } | ||
26 | *mm = true; | ||
27 | end = &end[2]; | ||
28 | } | ||
29 | if (end[0] != '\0') { | ||
30 | return false; | ||
31 | } | ||
32 | |||
33 | return true; | ||
34 | } | ||
35 | |||
36 | struct cmd_results *input_cmd_map_from_region(int argc, char **argv) { | ||
37 | struct cmd_results *error = NULL; | ||
38 | if ((error = checkarg(argc, "map_from_region", EXPECTED_EQUAL_TO, 2))) { | ||
39 | return error; | ||
40 | } | ||
41 | struct input_config *current_input_config = | ||
42 | config->handler_context.input_config; | ||
43 | if (!current_input_config) { | ||
44 | return cmd_results_new(CMD_FAILURE, "map_from_region", | ||
45 | "No input device defined"); | ||
46 | } | ||
47 | |||
48 | struct input_config *new_config = | ||
49 | new_input_config(current_input_config->identifier); | ||
50 | |||
51 | new_config->mapped_from_region = | ||
52 | calloc(1, sizeof(struct input_config_mapped_from_region)); | ||
53 | |||
54 | bool mm1, mm2; | ||
55 | if (!parse_coords(argv[0], &new_config->mapped_from_region->x1, | ||
56 | &new_config->mapped_from_region->y1, &mm1)) { | ||
57 | return cmd_results_new(CMD_FAILURE, "map_from_region", | ||
58 | "Invalid top-left coordinates"); | ||
59 | } | ||
60 | if (!parse_coords(argv[1], &new_config->mapped_from_region->x2, | ||
61 | &new_config->mapped_from_region->y2, &mm2)) { | ||
62 | return cmd_results_new(CMD_FAILURE, "map_from_region", | ||
63 | "Invalid bottom-right coordinates"); | ||
64 | } | ||
65 | if (new_config->mapped_from_region->x1 > new_config->mapped_from_region->x2 || | ||
66 | new_config->mapped_from_region->y1 > new_config->mapped_from_region->y2) { | ||
67 | return cmd_results_new(CMD_FAILURE, "map_from_region", | ||
68 | "Invalid rectangle"); | ||
69 | } | ||
70 | if (mm1 != mm2) { | ||
71 | return cmd_results_new(CMD_FAILURE, "map_from_region", | ||
72 | "Both coordinates must be in the same unit"); | ||
73 | } | ||
74 | new_config->mapped_from_region->mm = mm1; | ||
75 | |||
76 | apply_input_config(new_config); | ||
77 | |||
78 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
79 | } | ||