aboutsummaryrefslogtreecommitdiffstats
path: root/common
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 /common
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 .
Diffstat (limited to 'common')
-rw-r--r--common/util.c105
1 files changed, 0 insertions, 105 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}