-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonparser.c
149 lines (139 loc) · 2.96 KB
/
jsonparser.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
#include <strings.h>
typedef void (*ArgFunction)(const Arg*);
static ArgFunction texttofunction(const char *name) {
#define FUNC(fun) { #fun, fun }
const struct
{
const char *name;
ArgFunction func;
}
actions[] = {
FUNC(allnonfloat),
FUNC(decreasebright),
FUNC(focuslast),
FUNC(focusmon),
FUNC(focusstack),
FUNC(increasebright),
FUNC(killclient),
FUNC(mirrorlayout),
FUNC(movemouse),
FUNC(opacitychange),
FUNC(pushdown),
FUNC(pushup),
FUNC(quit),
FUNC(resizemouse),
FUNC(rotatelayoutaxis),
FUNC(rotatemonitor),
FUNC(sendselkey),
FUNC(setlayout),
FUNC(setmfact),
FUNC(shiftmastersplit),
FUNC(spawn),
FUNC(spawnterm),
FUNC(tabview),
FUNC(tag),
FUNC(tagmon),
FUNC(togglebar),
FUNC(toggledock),
FUNC(togglefloating),
FUNC(togglefoldtags),
FUNC(toggletag),
FUNC(togglevarilayout),
FUNC(toggleview),
FUNC(togglewindowgap),
FUNC(updatecolors),
FUNC(view),
FUNC(viewscroll),
FUNC(zoom),
FUNC(noop),
};
int i;
ArgFunction func = NULL;
for(i = 0; i < LENGTH(actions); ++i)
if (!strcmp(actions[i].name, name)) {
func = actions[i].func;
break;
}
return func;
}
static unsigned int jsontomodifier(const struct nx_json *json) {
#define MODIFIER(mod) { #mod, mod ## Mask }
const struct
{
const char* name;
unsigned int mod;
}
modifiers[] = {
MODIFIER(Shift),
MODIFIER(Lock),
MODIFIER(Control),
MODIFIER(Mod1),
MODIFIER(Mod2),
MODIFIER(Mod3),
MODIFIER(Mod4),
MODIFIER(Mod5),
};
int i;
const struct nx_json *js;
unsigned int mod = 0;
for(js = json->child; js; js = js->next)
for(i = 0; i < LENGTH(modifiers); ++i)
if(!strcasecmp(js->text_value, modifiers[i].name))
mod |= modifiers[i].mod;
return mod;
}
static const Layout *getlayout(const char *name) {
const struct
{
const char* name;
enum layout lt;
}
layoutnames[] = {
{ "tile" , TILE },
{ "spiral" , SPIRAL },
{ "dwindle" , DWINDLE },
{ "float" , FLOAT },
{ "monocle" , MONOCLE },
{ "varimono" , VARIMONO },
};
int i;
for(i = 0; i < LENGTH(layoutnames); ++i)
if(!strcmp(layoutnames[i].name, name))
return &layouts[layoutnames[i].lt];
fprintf(stderr, "layout not found: %s\n", name);
return NULL;
}
static void argparser(const struct nx_json *json, Arg *arg) {
switch(json->type) {
case NX_JSON_STRING:
arg->shcmd = calloc(strlen(json->text_value) + 1, sizeof(char));
strcpy(arg->shcmd, json->text_value);
break;
case NX_JSON_DOUBLE:
arg->f = (float)json->dbl_value;
break;
case NX_JSON_INTEGER:
case NX_JSON_BOOL:
arg->i = (int)json->int_value;
break;
case NX_JSON_NULL:
case NX_JSON_OBJECT:
case NX_JSON_ARRAY:
default:
break;
}
}
static Bool argislayout(void (*func)(const Arg *)) {
const struct {
void (*func)(const Arg *);
} layoutfuncs[] = {
{ &setlayout },
{ &togglevarilayout },
};
int i = 0;
Bool needslayout = False;
while(!needslayout && i < LENGTH(layoutfuncs))
if(layoutfuncs[i++].func == func)
needslayout = True;
return needslayout;
}