diff options
Diffstat (limited to 'common/util.c')
-rw-r--r-- | common/util.c | 114 |
1 files changed, 5 insertions, 109 deletions
diff --git a/common/util.c b/common/util.c index 27039dcb..bd7bed2d 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 | ||
@@ -20,93 +12,12 @@ int wrap(int i, int max) { | |||
20 | } | 12 | } |
21 | 13 | ||
22 | int numlen(int n) { | 14 | int numlen(int n) { |
23 | if (n == 0) { | 15 | int j = n <= 0 ? 1 : 0; |
24 | return 1; | 16 | while (n) { |
17 | j++; | ||
18 | n /= 10; | ||
25 | } | 19 | } |
26 | // Account for the '-' in negative numbers. | 20 | return j; |
27 | return log10(abs(n)) + (n > 0 ? 1 : 2); | ||
28 | } | ||
29 | |||
30 | static 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 | |||
46 | uint32_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 | |||
57 | const 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 | |||
68 | int 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 | |||
82 | pid_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 | } | 21 | } |
111 | 22 | ||
112 | uint32_t parse_color(const char *color) { | 23 | uint32_t parse_color(const char *color) { |
@@ -152,18 +63,3 @@ float parse_float(const char *value) { | |||
152 | } | 63 | } |
153 | return flt; | 64 | return flt; |
154 | } | 65 | } |
155 | |||
156 | enum 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 | } | ||