aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/gaps.c
diff options
context:
space:
mode:
authorLibravatar Zandr Martin <zandrmartin@gmail.com>2016-09-01 21:39:08 -0500
committerLibravatar Zandr Martin <zandrmartin@gmail.com>2016-09-01 21:39:08 -0500
commitb374c35758777f98e5ddbe4b0dc43bd7c80f36d7 (patch)
tree04bb4cfc3da7d2e0de7fbc38db42f65c66d2df4c /sway/commands/gaps.c
parentMerge pull request #874 from yohanesu75/ipc-client-fix (diff)
downloadsway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.gz
sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.zst
sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.zip
refactor commands.c
Diffstat (limited to 'sway/commands/gaps.c')
-rw-r--r--sway/commands/gaps.c165
1 files changed, 165 insertions, 0 deletions
diff --git a/sway/commands/gaps.c b/sway/commands/gaps.c
new file mode 100644
index 00000000..81ca14f4
--- /dev/null
+++ b/sway/commands/gaps.c
@@ -0,0 +1,165 @@
1#include <ctype.h>
2#include <errno.h>
3#include <stdlib.h>
4#include <string.h>
5#include "commands.h"
6#include "container.h"
7#include "focus.h"
8#include "layout.h"
9
10struct cmd_results *cmd_gaps(int argc, char **argv) {
11 struct cmd_results *error = NULL;
12 if ((error = checkarg(argc, "gaps", EXPECTED_AT_LEAST, 1))) {
13 return error;
14 }
15 const char *expected_syntax =
16 "Expected 'gaps edge_gaps <on|off|toggle>' or "
17 "'gaps <inner|outer> <current|all|workspace> <set|plus|minus n>'";
18 const char *amount_str = argv[0];
19 // gaps amount
20 if (argc >= 1 && isdigit(*amount_str)) {
21 int amount = (int)strtol(amount_str, NULL, 10);
22 if (errno == ERANGE) {
23 errno = 0;
24 return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range.");
25 }
26 config->gaps_inner = config->gaps_outer = amount;
27 arrange_windows(&root_container, -1, -1);
28 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
29 }
30 // gaps inner|outer n
31 else if (argc >= 2 && isdigit((amount_str = argv[1])[0])) {
32 int amount = (int)strtol(amount_str, NULL, 10);
33 if (errno == ERANGE) {
34 errno = 0;
35 return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range.");
36 }
37 const char *target_str = argv[0];
38 if (strcasecmp(target_str, "inner") == 0) {
39 config->gaps_inner = amount;
40 } else if (strcasecmp(target_str, "outer") == 0) {
41 config->gaps_outer = amount;
42 }
43 arrange_windows(&root_container, -1, -1);
44 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
45 } else if (argc == 2 && strcasecmp(argv[0], "edge_gaps") == 0) {
46 // gaps edge_gaps <on|off|toggle>
47 if (strcasecmp(argv[1], "toggle") == 0) {
48 if (config->reading) {
49 return cmd_results_new(CMD_FAILURE, "gaps edge_gaps toggle",
50 "Can't be used in config file.");
51 }
52 config->edge_gaps = !config->edge_gaps;
53 } else {
54 config->edge_gaps =
55 (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "on") == 0);
56 }
57 arrange_windows(&root_container, -1, -1);
58 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
59 }
60 // gaps inner|outer current|all set|plus|minus n
61 if (argc < 4 || config->reading) {
62 return cmd_results_new(CMD_INVALID, "gaps", expected_syntax);
63 }
64 // gaps inner|outer ...
65 const char *inout_str = argv[0];
66 enum {INNER, OUTER} inout;
67 if (strcasecmp(inout_str, "inner") == 0) {
68 inout = INNER;
69 } else if (strcasecmp(inout_str, "outer") == 0) {
70 inout = OUTER;
71 } else {
72 return cmd_results_new(CMD_INVALID, "gaps", expected_syntax);
73 }
74
75 // gaps ... current|all ...
76 const char *target_str = argv[1];
77 enum {CURRENT, WORKSPACE, ALL} target;
78 if (strcasecmp(target_str, "current") == 0) {
79 target = CURRENT;
80 } else if (strcasecmp(target_str, "all") == 0) {
81 target = ALL;
82 } else if (strcasecmp(target_str, "workspace") == 0) {
83 if (inout == OUTER) {
84 target = CURRENT;
85 } else {
86 // Set gap for views in workspace
87 target = WORKSPACE;
88 }
89 } else {
90 return cmd_results_new(CMD_INVALID, "gaps", expected_syntax);
91 }
92
93 // gaps ... n
94 amount_str = argv[3];
95 int amount = (int)strtol(amount_str, NULL, 10);
96 if (errno == ERANGE) {
97 errno = 0;
98 return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range.");
99 }
100
101 // gaps ... set|plus|minus ...
102 const char *method_str = argv[2];
103 enum {SET, ADD} method;
104 if (strcasecmp(method_str, "set") == 0) {
105 method = SET;
106 } else if (strcasecmp(method_str, "plus") == 0) {
107 method = ADD;
108 } else if (strcasecmp(method_str, "minus") == 0) {
109 method = ADD;
110 amount *= -1;
111 } else {
112 return cmd_results_new(CMD_INVALID, "gaps", expected_syntax);
113 }
114
115 if (target == CURRENT) {
116 swayc_t *cont;
117 if (inout == OUTER) {
118 if ((cont = swayc_active_workspace()) == NULL) {
119 return cmd_results_new(CMD_FAILURE, "gaps", "There's no active workspace.");
120 }
121 } else {
122 if ((cont = get_focused_view(&root_container))->type != C_VIEW) {
123 return cmd_results_new(CMD_FAILURE, "gaps", "Currently focused item is not a view.");
124 }
125 }
126 cont->gaps = swayc_gap(cont);
127 if (method == SET) {
128 cont->gaps = amount;
129 } else if ((cont->gaps += amount) < 0) {
130 cont->gaps = 0;
131 }
132 arrange_windows(cont->parent, -1, -1);
133 } else if (inout == OUTER) {
134 //resize all workspace.
135 int i,j;
136 for (i = 0; i < root_container.children->length; ++i) {
137 swayc_t *op = root_container.children->items[i];
138 for (j = 0; j < op->children->length; ++j) {
139 swayc_t *ws = op->children->items[j];
140 if (method == SET) {
141 ws->gaps = amount;
142 } else if ((ws->gaps += amount) < 0) {
143 ws->gaps = 0;
144 }
145 }
146 }
147 arrange_windows(&root_container, -1, -1);
148 } else {
149 // Resize gaps for all views in workspace
150 swayc_t *top;
151 if (target == WORKSPACE) {
152 if ((top = swayc_active_workspace()) == NULL) {
153 return cmd_results_new(CMD_FAILURE, "gaps", "There's currently no active workspace.");
154 }
155 } else {
156 top = &root_container;
157 }
158 int top_gap = top->gaps;
159 container_map(top, method == SET ? set_gaps : add_gaps, &amount);
160 top->gaps = top_gap;
161 arrange_windows(top, -1, -1);
162 }
163
164 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
165}