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
|
#include "config.h"
#include "commands.h"
#include "error.h"
#include <X11/Xlib.h>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <ios>
#include <string>
#include <vector>
#include <sstream>
#include <unistd.h>
#include <fcntl.h>
//Just for testing
#include <iostream>
using std::string;
// For testing
using std::cout, std::endl;
const void Config::gapsCmd(const CommandArg* argv)
{
gaps = argv[0].num;
}
const void Config::outerGapsCmd(const CommandArg* argv)
{
outerGaps = argv[0].num;
}
const void Config::logFileCmd(const CommandArg* argv)
{
logFile = argv[0].str;
}
const void Config::addWorkspaceCmd(const CommandArg* argv)
{
int* prefs = new int[argv[1].numArr.size];
memcpy(prefs, argv[1].numArr.arr, argv[1].numArr.size * sizeof(int));
workspaces.push_back({argv[0].str, prefs, argv[1].numArr.size});
numWS++;
}
Config::Config(CommandsModule& commandsModule)
: commandsModule(commandsModule)
{
//Register commands for config
commandsModule.addCommand("gaps", &Config::gapsCmd, 1, {NUM}, this);
commandsModule.addCommand("outergaps", &Config::outerGapsCmd, 1, {NUM}, this);
commandsModule.addCommand("logfile", &Config::logFileCmd, 1, {STR_REST}, this);
commandsModule.addCommand("addworkspace", &Config::addWorkspaceCmd, 2, {STR, NUM_ARR_REST}, this);
}
std::vector<Err> Config::reloadFile()
{
if(!loaded)
return {{CFG_ERR_NON_FATAL, "Not loaded config yet"}};
return loadFromFile(file);
}
std::vector<Err> Config::loadFromFile(std::string path)
{
std::vector<Err> errs;
file = path;
//Set defaults
gaps = 3;
outerGaps = 3;
logFile = "/tmp/yatlog.txt";
numWS = 0;
//Probably need something for workspaces and binds too...
string cmd;
int line = 0;
std::ifstream config(path);
while(getline(config, cmd))
{
if(cmd.at(0) == '#')
continue;
try
{
commandsModule.runCommand(cmd);
}
catch (Err e)
{
errs.push_back({e.code, "Error in config (line " + std::to_string(line) + "): " + std::to_string(e.code) + "\n\tMessage: " + e.message});
}
line++;
}
loaded = true;
return errs;
}
Config::~Config()
{
free();
}
void Config::free()
{
if(!loaded)
return;
for(Workspace w : workspaces)
{
delete [] w.screenPreferences;
}
}
|