-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonkeysparser.c
97 lines (83 loc) · 2.24 KB
/
jsonkeysparser.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
static const char *pendingtagkeys = "tagkeys";
static void keymodifiersparser(const struct nx_json *json, Key *key) {
key->mod = jsontomodifier(json);
}
static void keyparser(const struct nx_json *json, Key *key) {
key->keysym = XStringToKeysym(json->text_value);
if(key->keysym == NoSymbol)
fprintf(stderr, "No such key: %s\n", json->text_value);
}
static void keyfunctionparser(const struct nx_json *json, Key *key) {
if(!strcmp(json->text_value, pendingtagkeys)) {
key->pending = calloc(strlen(pendingtagkeys) + 1, sizeof(char));
strcpy(key->pending, pendingtagkeys);
}
else
key->func = texttofunction(json->text_value);
}
static void keyargparser(const struct nx_json *json, Key *key) {
argparser(json, &key->arg);
}
static void keyactionparser(const struct nx_json *json, Key *key) {
const struct
{
const char *key;
void (*parse)(const struct nx_json *js, Key *key);
} parsers[] =
{
{ "function", keyfunctionparser },
{ "arg" , keyargparser },
};
int i;
const struct nx_json *js;
char *shcmd;
for (js = json->child; js; js = js->next)
for (i = 0; i < LENGTH(parsers); ++i)
if (!strcmp(js->key, parsers[i].key))
parsers[i].parse(js, key);
if(key->arg.shcmd && argislayout(key->func)) {
shcmd = key->arg.shcmd;
key->arg.v = (void*)getlayout(key->arg.shcmd);
free(shcmd);
}
}
static void readKeyAttribute(const struct nx_json *js, Key *key) {
const struct
{
const char *key;
void (*parse)(const struct nx_json *js, Key *key);
} parsers[] =
{
{ "modifiers", keymodifiersparser },
{ "key" , keyparser },
{ "action" , keyactionparser },
};
int i;
for (i = 0; i < LENGTH(parsers); ++i)
if (!strcmp(js->key, parsers[i].key)) {
parsers[i].parse(js, key);
break;
}
}
static Key *callockey() {
Key *key = calloc(1, sizeof(Key));
return key;
}
static Key *getNextKey (const struct nx_json *json) {
Key *key = callockey();
const struct nx_json *js;
for(js = json->child; js; js = js->next)
readKeyAttribute(js, key);
return key;
}
static void readkeys (const struct nx_json *jsonKeys) {
if (jsonKeys) {
const struct nx_json *js = jsonKeys;
Key* key;
for(js = jsonKeys->child; js; js = js->next) {
key = getNextKey(js);
key->next = keys;
keys = key;
}
}
}