-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctl.c
94 lines (85 loc) · 2.28 KB
/
ctl.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
/*
* ISC License
*
* Copyright (c) 2021, Tommi Leino <namhas@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "mxswm.h"
#include <stdio.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdlib.h>
#include <err.h>
#include <string.h>
void
run_ctl_lines()
{
FILE *fp;
char *home;
struct stat sb;
char path[PATH_MAX], buf[1024];
home = getenv("HOME");
if (home == NULL)
home = "/";
if (snprintf(path, sizeof(path),
"%s/.mxswmrc", home) >= sizeof(path)) {
warnx(".mxswmrc path truncated");
return;
}
if (stat(path, &sb) == 0) {
if (!S_ISREG(sb.st_mode)) {
warnx("%s is not regular file", path);
return;
}
}
fp = fopen(path, "r");
if (fp == NULL) {
warn("%s", path);
return;
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
buf[strcspn(buf, "\r\n")] = '\0';
run_ctl_line(buf);
}
if (ferror(fp))
warn("%s", path);
fclose(fp);
}
void
run_ctl_line(const char *str)
{
int stackno, width, sticky;
struct stack *stack;
TRACE_LOG("\"%s\"", str);
if (sscanf(str, "stack %d width %d", &stackno, &width) == 2) {
stack = find_stack(stackno);
if (stack != NULL)
stack->prefer_width = width;
resize_stacks();
focus_stack(stack);
} else if (sscanf(str, "stack %d sticky %d", &stackno, &sticky) == 2) {
stack = find_stack(stackno);
if (stack != NULL)
stack->sticky = sticky ? 1 : 0;
} else if (strncmp(str, "add stack", strlen("add stack")) == 0) {
add_stack(current_stack());
}
#ifdef TRACE
if (strncmp(str, "stacks", strlen("stacks")) == 0) {
dump_stacks();
} else if (strncmp(str, "clients", strlen("clients")) == 0) {
dump_clients();
}
#endif
}