diff options
Diffstat (limited to 'sway/output.c')
-rw-r--r-- | sway/output.c | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/sway/output.c b/sway/output.c new file mode 100644 index 00000000..39c933f8 --- /dev/null +++ b/sway/output.c | |||
@@ -0,0 +1,95 @@ | |||
1 | #include <strings.h> | ||
2 | #include "output.h" | ||
3 | #include "log.h" | ||
4 | |||
5 | swayc_t *output_by_name(const char* name) { | ||
6 | if (strcasecmp(name, "left") == 0) { | ||
7 | return swayc_adjacent_output(NULL, MOVE_LEFT); | ||
8 | } | ||
9 | else if (strcasecmp(name, "right") == 0) { | ||
10 | return swayc_adjacent_output(NULL, MOVE_RIGHT); | ||
11 | } | ||
12 | else if (strcasecmp(name, "up") == 0) { | ||
13 | return swayc_adjacent_output(NULL, MOVE_UP); | ||
14 | } | ||
15 | else if (strcasecmp(name, "down") == 0) { | ||
16 | return swayc_adjacent_output(NULL, MOVE_DOWN); | ||
17 | } | ||
18 | else { | ||
19 | for(int i = 0; i < root_container.children->length; ++i) { | ||
20 | swayc_t *c = root_container.children->items[i]; | ||
21 | if (c->type == C_OUTPUT && strcasecmp(c->name, name) == 0) { | ||
22 | return c; | ||
23 | } | ||
24 | } | ||
25 | } | ||
26 | return NULL; | ||
27 | } | ||
28 | |||
29 | swayc_t *swayc_adjacent_output(swayc_t *output, enum movement_direction dir) { | ||
30 | // TODO: This implementation is naïve: We assume all outputs are | ||
31 | // perfectly aligned (ie. only a single output per edge which covers | ||
32 | // the whole edge). | ||
33 | if (!output) { | ||
34 | output = swayc_active_output(); | ||
35 | } | ||
36 | swayc_t *adjacent = NULL; | ||
37 | switch(dir) { | ||
38 | case MOVE_LEFT: | ||
39 | for(int i = 0; i < root_container.children->length; ++i) { | ||
40 | swayc_t *c = root_container.children->items[i]; | ||
41 | if (c == output || c->type != C_OUTPUT) { | ||
42 | continue; | ||
43 | } | ||
44 | if (c->y == output->y && c->x + c->width == output->x) { | ||
45 | sway_log(L_DEBUG, "%s is left of current output %s", c->name, output->name); | ||
46 | adjacent = c; | ||
47 | break; | ||
48 | } | ||
49 | } | ||
50 | break; | ||
51 | case MOVE_RIGHT: | ||
52 | for(int i = 0; i < root_container.children->length; ++i) { | ||
53 | swayc_t *c = root_container.children->items[i]; | ||
54 | if (c == output || c->type != C_OUTPUT) { | ||
55 | continue; | ||
56 | } | ||
57 | if (c->y == output->y && output->x + output->width == c->x) { | ||
58 | sway_log(L_DEBUG, "%s is right of current output %s", c->name, output->name); | ||
59 | adjacent = c; | ||
60 | break; | ||
61 | } | ||
62 | } | ||
63 | break; | ||
64 | case MOVE_UP: | ||
65 | for(int i = 0; i < root_container.children->length; ++i) { | ||
66 | swayc_t *c = root_container.children->items[i]; | ||
67 | if (c == output || c->type != C_OUTPUT) { | ||
68 | continue; | ||
69 | } | ||
70 | if (output->x == c->x && c->y + c->height == output->y) { | ||
71 | sway_log(L_DEBUG, "%s is above current output %s", c->name, output->name); | ||
72 | adjacent = c; | ||
73 | break; | ||
74 | } | ||
75 | } | ||
76 | break; | ||
77 | case MOVE_DOWN: | ||
78 | for(int i = 0; i < root_container.children->length; ++i) { | ||
79 | swayc_t *c = root_container.children->items[i]; | ||
80 | if (c == output || c->type != C_OUTPUT) { | ||
81 | continue; | ||
82 | } | ||
83 | if (output->x == c->x && output->y + output->height == c->y) { | ||
84 | sway_log(L_DEBUG, "%s is below current output %s", c->name, output->name); | ||
85 | adjacent = c; | ||
86 | break; | ||
87 | } | ||
88 | } | ||
89 | break; | ||
90 | default: | ||
91 | sway_abort("Function called with invalid argument."); | ||
92 | break; | ||
93 | } | ||
94 | return adjacent; | ||
95 | } | ||