-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwmblocksctl.c
249 lines (225 loc) · 4.94 KB
/
dwmblocksctl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* See LICENSE file for copyright and license details.
*
* dwmblocksctl is just a helper for updating blocks, usefull
* when combined with commands that change the blocks' text,
* thus eliminating the need for them to constantly upgrade.
*
* It can send the corresponding update signal to dwm of a given
* block name, print the update signals along with the period and
* restart dwmblocks.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <signal.h>
#define CMDLENGTH 70
#define LENGTH(X) (int)(sizeof(X) / sizeof(X[0]))
#define MAX(a, b) (a > b ? a : b)
typedef const struct {
const char *command;
const unsigned int interval;
const unsigned int signal;
} Block;
#include "config.h"
char cmds[LENGTH(blocks)][CMDLENGTH];
static pid_t dwmblocks_pID = -1;
void
structinit()
{
char temp[CMDLENGTH];
char *ptr;
for (int i = 0; i < LENGTH(blocks); i++) {
ptr = strrchr(blocks[i].command,'/');
strcpy(temp, ++ptr);
if ((ptr = strrchr(temp,'"')) != NULL)
*ptr = '\0';
if ((ptr = strchr(temp,'"')) == NULL)
ptr = temp;
else
ptr++;
strcpy(cmds[i], ptr);
}
}
int
getdigits(int num)
{
int ret = 0;
int tempnum = num;
do {
++ret;
tempnum /= 10;
} while (tempnum);
return ret;
}
void
structstat()
{
int cmdl = 0, intl = 0, sigl = 0;
int cmdt, intt, sigt;
for (int i = 0; i < LENGTH(blocks); i++) {
cmdt = strlen(cmds[i]);
intt = getdigits(blocks[i].interval);
sigt = getdigits(blocks[i].signal);
cmdl = MAX(cmdt, cmdl);
intl = MAX(intt, intl);
sigl = MAX(sigt, sigl);
}
cmdl = MAX(9, cmdl+2);
intl = MAX(16, intl+2);
sigl = MAX(14, sigl+2);
for(int i = 0; i < cmdl + intl + sigl + 4; i++)
putchar('-');
putchar('\n');
printf("|%*s|%*s|%*s|\n", cmdl, "Command", intl, "Update interval", sigl, "Update signal");
for(int i = 0; i < cmdl + intl + sigl + 4; i++)
putchar('-');
putchar('\n');
for (int i = 0; i < LENGTH(blocks); i++) {
printf("|%*s|%*d|%*d|\n", cmdl, cmds[i], intl, blocks[i].interval, sigl, blocks[i].signal);
}
for(int i = 0; i < cmdl + intl + sigl + 4; i++)
putchar('-');
putchar('\n');
}
int
stringisnum(char *str)
{
for (int i = 0; i < strlen(str); i++)
if (str[i] > '9' || str[i] < '0')
return 0;
return 1;
}
void
freedirstruct(struct dirent** input, int n)
{
while (n--) {
if (input[n] != NULL)
free(input[n]);
}
if (input != NULL)
free(input);
}
int
getsignalnum(char *name)
{
for(int i = 0; i < LENGTH(blocks); i++) {
if(strcmp(name, cmds[i]) == 0) {
return blocks[i].signal;
}
}
return -1;
}
void
getpidofdwmblocks()
{
struct dirent** proclist;
FILE *fp;
char buffer[128];
char filename[256];
int proccesscount;
proccesscount = scandir("/proc", &proclist, NULL, NULL);
if (proccesscount == -1) {
perror("dwmblocksctl:Failed to scan '/proc' directory");
exit(EXIT_FAILURE);
}
for (int i = 0 ; i < proccesscount; i++) {
if (stringisnum(proclist[i]->d_name)) {
strcpy(filename, "/proc/");
strcat(filename, proclist[i]->d_name);
strcat(filename, "/cmdline");
fp = fopen(filename, "r");
if (fp == NULL) {
continue;
}
fgets(buffer, sizeof(buffer), fp);
fclose(fp);
if (strstr(buffer, "dwmblocks") != NULL) {
dwmblocks_pID = strtol(proclist[i]->d_name, NULL, 10);
break;
}
}
}
freedirstruct(proclist, proccesscount);
}
void
pkill(char *name)
{
if (dwmblocks_pID == -1) {
getpidofdwmblocks();
if(dwmblocks_pID == -1) {
perror("dwmblocksctl:Cannot get pId of dwmblocks");
exit(EXIT_FAILURE);
}
}
if (!strcmp(name,"dwmblocks-all")) {
kill(dwmblocks_pID, SIGUSR1);
return;
}
if (!strcmp(name,"dwmblocks")) {
kill(dwmblocks_pID, SIGTERM);
return;
}
int signalnum = getsignalnum(name);
kill(dwmblocks_pID, SIGRTMIN + signalnum);
}
void
usage()
{
puts("usage: dwmblocksctl [-arth] [-s block]");
}
void
executedwmblocks()
{
pid_t pID;
pID = fork();
if (pID < 0) {
perror("dwmblocksctl:Failed in forking");
exit(EXIT_FAILURE);
} else if (pID == 0) {
setsid();
execl("/usr/local/bin/dwmblocks", "dwmblocks", NULL);
exit(EXIT_SUCCESS);
} else {
exit(EXIT_SUCCESS);
}
}
void
help()
{
puts("Program that controls the dwmblocks status bar.");
puts("usage: dwmblocksctl [-arth] [-s block]");
puts("");
puts("Options:");
puts("\t-a Update all blocks.");
puts("\t-r Restart dwmblocks.");
puts("\t-t Show the active commands, along with their details.");
puts("\t-h Prints this help prompt.");
puts("\t-s [block] Specify a block to update.");
}
int
main(int argc, char *argv[])
{
int i = 0;
if (argc < 2)
usage();
structinit();
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-a")) {
pkill("dwmblocks-all");
} else if (!strcmp(argv[i], "-r")) {
pkill("dwmblocks");
executedwmblocks();
} else if (!strcmp(argv[i], "-t")) {
structstat();
} else if (!strcmp(argv[i], "-h")) {
help();
} else if (!strcmp(argv[i], "-s")) {
pkill(argv[++i]);
} else {
usage();
}
}
return 0;
}