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
|
#include "config.h"
#include <X11/Xlib.h>
#include <string>
#include <map>
//Just for testing
#include <iostream>
#include <toml++/toml.hpp>
using std::map, std::string;
// For testing
using std::cout, std::endl, std::cerr;
void exit(const KeyArg arg)
{
cout << "exit called" << endl;
}
void spawn(const KeyArg arg)
{
cout << "spawn called " << arg.str << endl;
}
void changeWS(const KeyArg arg)
{
cout << "changeWS called" << endl;
}
void wToWS(const KeyArg arg)
{
cout << "wToWS called" << endl;
}
void focChange(const KeyArg arg)
{
cout << "focChange called" << endl;
}
void reload(const KeyArg arg)
{
cout << "reload called" << endl;
}
map<string, void(*) (const KeyArg arg)> funcNameMap = {
{"exit", exit},
{"spawn", spawn},
{"changeWS", changeWS},
{"wToWS", wToWS},
{"focChange", focChange},
{"reload", reload}
};
Config::Config()
{
}
std::vector<string> split (const string &s, char delim) {
std::vector<string> result;
std::stringstream ss (s);
string item;
while (getline (ss, item, delim)) {
result.push_back (item);
}
return result;
}
void Config::loadFromFile(string path)
{
//free();
toml::table tbl;
try
{
tbl = toml::parse_file(path);
}
catch (const toml::parse_error& err)
{
throw err;
}
//Startup
startupBashc = tbl["Startup"]["startupBash"].as_array()->size();
startupBash = new string[startupBashc];
for(int i = 0; i < startupBashc; i++)
{
auto element = tbl["Startup"]["startupBash"][i].value<string>();
if(element)
{
startupBash[i] = *element;
}
else
{
cerr << "Element " << i << " in `startupBash` is not a string" << endl;
startupBash[i] = "";
}
}
//Main
gaps = tbl["Main"]["gaps"].value_or<int>(3);
outerGaps = tbl["Main"]["gaps"].value_or<int>(3);
logFile = tbl["Main"]["gaps"].value_or<string>("/tmp/yatlog.txt");
//Workspaces
numWS = tbl["Workspaces"]["numWS"].value_or<int>(10);
workspaceNamesc = tbl["Workspaces"]["workspaceNames"].as_array()->size();
workspaceNames = new string[workspaceNamesc];
for(int i = 0; i < workspaceNamesc; i++)
{
auto element = tbl["Workspaces"]["workspaceNames"][i].value<string>();
if(element)
{
workspaceNames[i] = *element;
}
else
{
cerr << "Element " << i << " in `workspaceNames` is not a string" << endl;
workspaceNames[i] = "";
}
}
maxMonitors = tbl["Workspaces"]["maxMonitors"].value_or<int>(2);
screenPreferencesc = tbl["Workspaces"]["screenPreferences"].as_array()->size();
screenPreferences = new int*[screenPreferencesc];
for(int i = 0; i < screenPreferencesc; i++)
{
int* wsScreens = new int[maxMonitors];
for(int j = 0; j < maxMonitors; j++)
{
if(tbl["Workspaces"]["screenPreferences"][i].as_array()->size() <= j)
{
wsScreens[j] = 0;
continue;
}
auto element = tbl["Workspaces"]["screenPreferences"][i][j].value<int>();
if(element)
wsScreens[j] = *element;
else
{
cerr << "Element " << i << " " << j << " int `screenPreferences` is not an int" << endl;
wsScreens[j] = 0;
}
}
screenPreferences[i] = wsScreens;
}
//Keybinds
bindsc = tbl["Keybinds"]["key"].as_array()->size();
binds = new KeyBind[bindsc];
for(int i = 0; i < bindsc; i++)
{
KeyBind bind;
const string bindString = *tbl["Keybinds"]["key"][i]["bind"].value<string>();
std::vector<string> keys = split(bindString, '+');
for(string key : keys)
{
if(key == "mod")
{
bind.modifiers |= Mod4Mask;
}
else if(key == "alt")
{
bind.modifiers |= Mod1Mask;
}
else if(key == "shift")
{
bind.modifiers |= ShiftMask;
}
else if(key == "control")
{
bind.modifiers |= ControlMask;
}
else
{
bind.keysym = XStringToKeysym(key.c_str());
}
}
string funcString = *tbl["Keybinds"]["key"][i]["func"].value<string>();
void(* func) (const KeyArg arg) = funcNameMap.find(funcString)->second;
bind.func = func;
auto args = tbl["Keybinds"]["key"][i]["args"];
if(args.is<int64_t>())
{
int num = *args.value<int>();
bind.args = {.num = num};
}
else if(args.is<string>())
{
string str = (string)*args.value<string>();
if(str == "Up")
bind.args = {.dir = Up};
else if (str == "Down")
bind.args = {.dir = Down};
else if (str == "Left")
bind.args = {.dir = Left};
else if (str == "Right")
bind.args = {.dir = Right};
else
{
bind.args = {.str = strdup(str.c_str())};
}
}
else
{
bind.args = {NULL};
}
binds[i] = bind;
}
}
Config::~Config()
{
free();
}
void Config::free()
{
delete[] startupBash;
delete[] workspaceNames;
for(int i = 0; i < screenPreferencesc; i++)
{
delete[] screenPreferences[i];
}
delete[] screenPreferences;
}
|