aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar M Stoeckl <code@mstoeckl.com>2019-01-21 12:39:16 -0500
committerLibravatar M Stoeckl <code@mstoeckl.com>2019-01-21 12:39:16 -0500
commitd7ff776552bef524e905d85c2a5e7651c8408658 (patch)
treeae20feac64f93f776e9c9e136c62459705e97987
parentswaybar: fix setting floating watcher slots (diff)
downloadsway-d7ff776552bef524e905d85c2a5e7651c8408658.tar.gz
sway-d7ff776552bef524e905d85c2a5e7651c8408658.tar.zst
sway-d7ff776552bef524e905d85c2a5e7651c8408658.zip
Move sway-specific functions in common/util.c into sway/
Modifier handling functions were moved into sway/input/keyboard.c; opposite_direction for enum wlr_direction into sway/tree/output.c; and get_parent_pid into sway/tree/root.c .
-rw-r--r--common/util.c105
-rw-r--r--include/sway/input/keyboard.h21
-rw-r--r--include/sway/output.h2
-rw-r--r--include/util.h35
-rw-r--r--sway/commands/bar/modifier.c2
-rw-r--r--sway/commands/bind.c1
-rw-r--r--sway/commands/floating_modifier.c2
-rw-r--r--sway/config/bar.c4
-rw-r--r--sway/input/keyboard.c54
-rw-r--r--sway/ipc-server.c1
-rw-r--r--sway/tree/output.c16
-rw-r--r--sway/tree/root.c35
12 files changed, 135 insertions, 143 deletions
diff --git a/common/util.c b/common/util.c
index 27039dcb..60f2f160 100644
--- a/common/util.c
+++ b/common/util.c
@@ -1,17 +1,9 @@
1#define _POSIX_C_SOURCE 200809L 1#define _POSIX_C_SOURCE 200809L
2#include <assert.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <unistd.h>
6#include <float.h> 2#include <float.h>
7#include <math.h> 3#include <math.h>
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h> 4#include <stdlib.h>
11#include <string.h> 5#include <string.h>
12#include <strings.h> 6#include <strings.h>
13#include <xkbcommon/xkbcommon-names.h>
14#include <wlr/types/wlr_keyboard.h>
15#include "log.h" 7#include "log.h"
16#include "util.h" 8#include "util.h"
17 9
@@ -27,88 +19,6 @@ int numlen(int n) {
27 return log10(abs(n)) + (n > 0 ? 1 : 2); 19 return log10(abs(n)) + (n > 0 ? 1 : 2);
28} 20}
29 21
30static struct modifier_key {
31 char *name;
32 uint32_t mod;
33} modifiers[] = {
34 { XKB_MOD_NAME_SHIFT, WLR_MODIFIER_SHIFT },
35 { XKB_MOD_NAME_CAPS, WLR_MODIFIER_CAPS },
36 { XKB_MOD_NAME_CTRL, WLR_MODIFIER_CTRL },
37 { "Ctrl", WLR_MODIFIER_CTRL },
38 { XKB_MOD_NAME_ALT, WLR_MODIFIER_ALT },
39 { "Alt", WLR_MODIFIER_ALT },
40 { XKB_MOD_NAME_NUM, WLR_MODIFIER_MOD2 },
41 { "Mod3", WLR_MODIFIER_MOD3 },
42 { XKB_MOD_NAME_LOGO, WLR_MODIFIER_LOGO },
43 { "Mod5", WLR_MODIFIER_MOD5 },
44};
45
46uint32_t get_modifier_mask_by_name(const char *name) {
47 int i;
48 for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
49 if (strcasecmp(modifiers[i].name, name) == 0) {
50 return modifiers[i].mod;
51 }
52 }
53
54 return 0;
55}
56
57const char *get_modifier_name_by_mask(uint32_t modifier) {
58 int i;
59 for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
60 if (modifiers[i].mod == modifier) {
61 return modifiers[i].name;
62 }
63 }
64
65 return NULL;
66}
67
68int get_modifier_names(const char **names, uint32_t modifier_masks) {
69 int length = 0;
70 int i;
71 for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
72 if ((modifier_masks & modifiers[i].mod) != 0) {
73 names[length] = modifiers[i].name;
74 ++length;
75 modifier_masks ^= modifiers[i].mod;
76 }
77 }
78
79 return length;
80}
81
82pid_t get_parent_pid(pid_t child) {
83 pid_t parent = -1;
84 char file_name[100];
85 char *buffer = NULL;
86 char *token = NULL;
87 const char *sep = " ";
88 FILE *stat = NULL;
89 size_t buf_size = 0;
90
91 sprintf(file_name, "/proc/%d/stat", child);
92
93 if ((stat = fopen(file_name, "r"))) {
94 if (getline(&buffer, &buf_size, stat) != -1) {
95 token = strtok(buffer, sep); // pid
96 token = strtok(NULL, sep); // executable name
97 token = strtok(NULL, sep); // state
98 token = strtok(NULL, sep); // parent pid
99 parent = strtol(token, NULL, 10);
100 }
101 free(buffer);
102 fclose(stat);
103 }
104
105 if (parent) {
106 return (parent == child) ? -1 : parent;
107 }
108
109 return -1;
110}
111
112uint32_t parse_color(const char *color) { 22uint32_t parse_color(const char *color) {
113 if (color[0] == '#') { 23 if (color[0] == '#') {
114 ++color; 24 ++color;
@@ -152,18 +62,3 @@ float parse_float(const char *value) {
152 } 62 }
153 return flt; 63 return flt;
154} 64}
155
156enum wlr_direction opposite_direction(enum wlr_direction d) {
157 switch (d) {
158 case WLR_DIRECTION_UP:
159 return WLR_DIRECTION_DOWN;
160 case WLR_DIRECTION_DOWN:
161 return WLR_DIRECTION_UP;
162 case WLR_DIRECTION_RIGHT:
163 return WLR_DIRECTION_LEFT;
164 case WLR_DIRECTION_LEFT:
165 return WLR_DIRECTION_RIGHT;
166 }
167 assert(false);
168 return 0;
169}
diff --git a/include/sway/input/keyboard.h b/include/sway/input/keyboard.h
index 65137a08..0c8ada0f 100644
--- a/include/sway/input/keyboard.h
+++ b/include/sway/input/keyboard.h
@@ -5,6 +5,27 @@
5 5
6#define SWAY_KEYBOARD_PRESSED_KEYS_CAP 32 6#define SWAY_KEYBOARD_PRESSED_KEYS_CAP 32
7 7
8/**
9 * Get modifier mask from modifier name.
10 *
11 * Returns the modifer mask or 0 if the name isn't found.
12 */
13uint32_t get_modifier_mask_by_name(const char *name);
14
15/**
16 * Get modifier name from modifier mask.
17 *
18 * Returns the modifier name or NULL if it isn't found.
19 */
20const char *get_modifier_name_by_mask(uint32_t modifier);
21
22/**
23 * Get an array of modifier names from modifier_masks
24 *
25 * Populates the names array and return the number of names added.
26 */
27int get_modifier_names(const char **names, uint32_t modifier_masks);
28
8struct sway_shortcut_state { 29struct sway_shortcut_state {
9 /** 30 /**
10 * A list of pressed key ids (either keysyms or keycodes), 31 * A list of pressed key ids (either keysyms or keycodes),
diff --git a/include/sway/output.h b/include/sway/output.h
index 9ebdb6c1..479897ef 100644
--- a/include/sway/output.h
+++ b/include/sway/output.h
@@ -152,4 +152,6 @@ void premultiply_alpha(float color[4], float opacity);
152 152
153void scale_box(struct wlr_box *box, float scale); 153void scale_box(struct wlr_box *box, float scale);
154 154
155enum wlr_direction opposite_direction(enum wlr_direction d);
156
155#endif 157#endif
diff --git a/include/util.h b/include/util.h
index 84318fe7..e3269d6b 100644
--- a/include/util.h
+++ b/include/util.h
@@ -3,9 +3,6 @@
3 3
4#include <stdint.h> 4#include <stdint.h>
5#include <stdbool.h> 5#include <stdbool.h>
6#include <unistd.h>
7#include <wlr/types/wlr_output_layout.h>
8#include <xkbcommon/xkbcommon.h>
9 6
10/** 7/**
11 * Wrap i into the range [0, max[ 8 * Wrap i into the range [0, max[
@@ -13,39 +10,11 @@
13int wrap(int i, int max); 10int wrap(int i, int max);
14 11
15/** 12/**
16 * Count number of digits in int 13 * Count number of digits in int, including '-' sign if there is one
17 */ 14 */
18int numlen(int n); 15int numlen(int n);
19 16
20/** 17/**
21 * Get modifier mask from modifier name.
22 *
23 * Returns the modifer mask or 0 if the name isn't found.
24 */
25uint32_t get_modifier_mask_by_name(const char *name);
26
27/**
28 * Get modifier name from modifier mask.
29 *
30 * Returns the modifier name or NULL if it isn't found.
31 */
32const char *get_modifier_name_by_mask(uint32_t modifier);
33
34/**
35 * Get an array of modifier names from modifier_masks
36 *
37 * Populates the names array and return the number of names added.
38 */
39int get_modifier_names(const char **names, uint32_t modifier_masks);
40
41/**
42 * Get the pid of a parent process given the pid of a child process.
43 *
44 * Returns the parent pid or NULL if the parent pid cannot be determined.
45 */
46pid_t get_parent_pid(pid_t pid);
47
48/**
49 * Given a string that represents an RGB(A) color, return a uint32_t 18 * Given a string that represents an RGB(A) color, return a uint32_t
50 * version of the color. 19 * version of the color.
51 */ 20 */
@@ -65,6 +34,4 @@ bool parse_boolean(const char *boolean, bool current);
65 */ 34 */
66float parse_float(const char *value); 35float parse_float(const char *value);
67 36
68enum wlr_direction opposite_direction(enum wlr_direction d);
69
70#endif 37#endif
diff --git a/sway/commands/bar/modifier.c b/sway/commands/bar/modifier.c
index 62ec681f..c95250d1 100644
--- a/sway/commands/bar/modifier.c
+++ b/sway/commands/bar/modifier.c
@@ -1,8 +1,8 @@
1#include <string.h> 1#include <string.h>
2#include "sway/commands.h" 2#include "sway/commands.h"
3#include "sway/input/keyboard.h"
3#include "log.h" 4#include "log.h"
4#include "stringop.h" 5#include "stringop.h"
5#include "util.h"
6 6
7struct cmd_results *bar_cmd_modifier(int argc, char **argv) { 7struct cmd_results *bar_cmd_modifier(int argc, char **argv) {
8 struct cmd_results *error = NULL; 8 struct cmd_results *error = NULL;
diff --git a/sway/commands/bind.c b/sway/commands/bind.c
index ce087fd0..59116d67 100644
--- a/sway/commands/bind.c
+++ b/sway/commands/bind.c
@@ -8,6 +8,7 @@
8#include "sway/commands.h" 8#include "sway/commands.h"
9#include "sway/config.h" 9#include "sway/config.h"
10#include "sway/input/cursor.h" 10#include "sway/input/cursor.h"
11#include "sway/input/keyboard.h"
11#include "sway/ipc-server.h" 12#include "sway/ipc-server.h"
12#include "list.h" 13#include "list.h"
13#include "log.h" 14#include "log.h"
diff --git a/sway/commands/floating_modifier.c b/sway/commands/floating_modifier.c
index 3674971a..fd606281 100644
--- a/sway/commands/floating_modifier.c
+++ b/sway/commands/floating_modifier.c
@@ -1,7 +1,7 @@
1#include "strings.h" 1#include "strings.h"
2#include "sway/commands.h" 2#include "sway/commands.h"
3#include "sway/config.h" 3#include "sway/config.h"
4#include "util.h" 4#include "sway/input/keyboard.h"
5 5
6struct cmd_results *cmd_floating_modifier(int argc, char **argv) { 6struct cmd_results *cmd_floating_modifier(int argc, char **argv) {
7 struct cmd_results *error = NULL; 7 struct cmd_results *error = NULL;
diff --git a/sway/config/bar.c b/sway/config/bar.c
index 0fdac5d8..2e28fa1e 100644
--- a/sway/config/bar.c
+++ b/sway/config/bar.c
@@ -11,12 +11,12 @@
11#include <strings.h> 11#include <strings.h>
12#include <signal.h> 12#include <signal.h>
13#include "sway/config.h" 13#include "sway/config.h"
14#include "sway/input/keyboard.h"
14#include "sway/output.h" 15#include "sway/output.h"
15#include "config.h" 16#include "config.h"
16#include "stringop.h"
17#include "list.h" 17#include "list.h"
18#include "log.h" 18#include "log.h"
19#include "util.h" 19#include "stringop.h"
20 20
21static void terminate_swaybar(pid_t pid) { 21static void terminate_swaybar(pid_t pid) {
22 sway_log(SWAY_DEBUG, "Terminating swaybar %d", pid); 22 sway_log(SWAY_DEBUG, "Terminating swaybar %d", pid);
diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c
index c1fc60f7..12c57366 100644
--- a/sway/input/keyboard.c
+++ b/sway/input/keyboard.c
@@ -1,9 +1,11 @@
1#include <assert.h> 1#include <assert.h>
2#include <limits.h> 2#include <limits.h>
3#include <strings.h>
3#include <wlr/backend/multi.h> 4#include <wlr/backend/multi.h>
4#include <wlr/backend/session.h> 5#include <wlr/backend/session.h>
5#include <wlr/types/wlr_idle.h> 6#include <wlr/types/wlr_idle.h>
6#include <wlr/interfaces/wlr_keyboard.h> 7#include <wlr/interfaces/wlr_keyboard.h>
8#include <xkbcommon/xkbcommon-names.h>
7#include "sway/commands.h" 9#include "sway/commands.h"
8#include "sway/desktop/transaction.h" 10#include "sway/desktop/transaction.h"
9#include "sway/input/input-manager.h" 11#include "sway/input/input-manager.h"
@@ -12,6 +14,58 @@
12#include "sway/ipc-server.h" 14#include "sway/ipc-server.h"
13#include "log.h" 15#include "log.h"
14 16
17static struct modifier_key {
18 char *name;
19 uint32_t mod;
20} modifiers[] = {
21 { XKB_MOD_NAME_SHIFT, WLR_MODIFIER_SHIFT },
22 { XKB_MOD_NAME_CAPS, WLR_MODIFIER_CAPS },
23 { XKB_MOD_NAME_CTRL, WLR_MODIFIER_CTRL },
24 { "Ctrl", WLR_MODIFIER_CTRL },
25 { XKB_MOD_NAME_ALT, WLR_MODIFIER_ALT },
26 { "Alt", WLR_MODIFIER_ALT },
27 { XKB_MOD_NAME_NUM, WLR_MODIFIER_MOD2 },
28 { "Mod3", WLR_MODIFIER_MOD3 },
29 { XKB_MOD_NAME_LOGO, WLR_MODIFIER_LOGO },
30 { "Mod5", WLR_MODIFIER_MOD5 },
31};
32
33uint32_t get_modifier_mask_by_name(const char *name) {
34 int i;
35 for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
36 if (strcasecmp(modifiers[i].name, name) == 0) {
37 return modifiers[i].mod;
38 }
39 }
40
41 return 0;
42}
43
44const char *get_modifier_name_by_mask(uint32_t modifier) {
45 int i;
46 for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
47 if (modifiers[i].mod == modifier) {
48 return modifiers[i].name;
49 }
50 }
51
52 return NULL;
53}
54
55int get_modifier_names(const char **names, uint32_t modifier_masks) {
56 int length = 0;
57 int i;
58 for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
59 if ((modifier_masks & modifiers[i].mod) != 0) {
60 names[length] = modifiers[i].name;
61 ++length;
62 modifier_masks ^= modifiers[i].mod;
63 }
64 }
65
66 return length;
67}
68
15/** 69/**
16 * Remove all key ids associated to a keycode from the list of pressed keys 70 * Remove all key ids associated to a keycode from the list of pressed keys
17 */ 71 */
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index 82e144b7..c99f34a9 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -23,6 +23,7 @@
23#include "sway/output.h" 23#include "sway/output.h"
24#include "sway/server.h" 24#include "sway/server.h"
25#include "sway/input/input-manager.h" 25#include "sway/input/input-manager.h"
26#include "sway/input/keyboard.h"
26#include "sway/input/seat.h" 27#include "sway/input/seat.h"
27#include "sway/tree/root.h" 28#include "sway/tree/root.h"
28#include "sway/tree/view.h" 29#include "sway/tree/view.h"
diff --git a/sway/tree/output.c b/sway/tree/output.c
index 7fbeeebd..50a2c535 100644
--- a/sway/tree/output.c
+++ b/sway/tree/output.c
@@ -1,4 +1,5 @@
1#define _POSIX_C_SOURCE 200809L 1#define _POSIX_C_SOURCE 200809L
2#include <assert.h>
2#include <ctype.h> 3#include <ctype.h>
3#include <string.h> 4#include <string.h>
4#include <strings.h> 5#include <strings.h>
@@ -12,6 +13,21 @@
12#include "log.h" 13#include "log.h"
13#include "util.h" 14#include "util.h"
14 15
16enum wlr_direction opposite_direction(enum wlr_direction d) {
17 switch (d) {
18 case WLR_DIRECTION_UP:
19 return WLR_DIRECTION_DOWN;
20 case WLR_DIRECTION_DOWN:
21 return WLR_DIRECTION_UP;
22 case WLR_DIRECTION_RIGHT:
23 return WLR_DIRECTION_LEFT;
24 case WLR_DIRECTION_LEFT:
25 return WLR_DIRECTION_RIGHT;
26 }
27 assert(false);
28 return 0;
29}
30
15static void restore_workspaces(struct sway_output *output) { 31static void restore_workspaces(struct sway_output *output) {
16 // Workspace output priority 32 // Workspace output priority
17 for (int i = 0; i < root->outputs->length; i++) { 33 for (int i = 0; i < root->outputs->length; i++) {
diff --git a/sway/tree/root.c b/sway/tree/root.c
index 08ce7942..d4b97be3 100644
--- a/sway/tree/root.c
+++ b/sway/tree/root.c
@@ -169,6 +169,41 @@ struct pid_workspace {
169 169
170static struct wl_list pid_workspaces; 170static struct wl_list pid_workspaces;
171 171
172/**
173 * Get the pid of a parent process given the pid of a child process.
174 *
175 * Returns the parent pid or NULL if the parent pid cannot be determined.
176 */
177static pid_t get_parent_pid(pid_t child) {
178 pid_t parent = -1;
179 char file_name[100];
180 char *buffer = NULL;
181 char *token = NULL;
182 const char *sep = " ";
183 FILE *stat = NULL;
184 size_t buf_size = 0;
185
186 sprintf(file_name, "/proc/%d/stat", child);
187
188 if ((stat = fopen(file_name, "r"))) {
189 if (getline(&buffer, &buf_size, stat) != -1) {
190 token = strtok(buffer, sep); // pid
191 token = strtok(NULL, sep); // executable name
192 token = strtok(NULL, sep); // state
193 token = strtok(NULL, sep); // parent pid
194 parent = strtol(token, NULL, 10);
195 }
196 free(buffer);
197 fclose(stat);
198 }
199
200 if (parent) {
201 return (parent == child) ? -1 : parent;
202 }
203
204 return -1;
205}
206
172struct sway_workspace *root_workspace_for_pid(pid_t pid) { 207struct sway_workspace *root_workspace_for_pid(pid_t pid) {
173 if (!pid_workspaces.prev && !pid_workspaces.next) { 208 if (!pid_workspaces.prev && !pid_workspaces.next) {
174 wl_list_init(&pid_workspaces); 209 wl_list_init(&pid_workspaces);