-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCConfigHandler.cpp
executable file
·208 lines (184 loc) · 5.55 KB
/
CConfigHandler.cpp
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
#include "CConfigHandler.h"
#include <stdio.h>
#include <string.h>
#include <memory>
#include <iostream>
CConfigHandler* CConfigHandler::getInstance() {
static CConfigHandler * singleInstance = new CConfigHandler;
return singleInstance;
}
bool CConfigHandler::ImportConfig(const char* par_cpFileName) {
borderlessEnableCmd = NULL;
borderlessDisableCmd = NULL;
FILE* fpConfig = 0;
fpConfig = fopen(par_cpFileName, "rb");
if (fpConfig == NULL) {
printf("Config file %s does not exist\n", par_cpFileName);
return false;
}
fseek(fpConfig, 0, SEEK_END);
int nFileSize = ftell(fpConfig);
fseek(fpConfig, 0, SEEK_SET);
//there are many return paths, best is to use a smart ptr.
std::shared_ptr<char> cpFileBuffer(new char[nFileSize]);
//char* cpFileBuffer = new char[nFileSize];
fread(cpFileBuffer.get(), 1, nFileSize, fpConfig);
fclose(fpConfig);
int deviceCount = 0;
char* cpTemp = strstr(cpFileBuffer.get(), "DEVICE_COUNT");
if(cpTemp)
sscanf(cpTemp, "DEVICE_COUNT=%d", &deviceCount);
else{
printf("Corrupted config!");
return false;
}
for(int i = 0; i < deviceCount; ++i){
char key[40];
sprintf(key, "\nDEVICE_ID=%d", i);
char* cpTemp = strstr(cpFileBuffer.get(), key);
if(cpTemp){
++cpTemp; //move beyond the newline.
HandlerProperties toRegister;
int res = sscanf(cpTemp, "DEVICE_ID=%d DEVICE_NAME=%s PORT=%d IP_ADDR=%s WIDTH=%d HEIGTH=%d;", &toRegister.DEVICE_ID, toRegister.DEVICE_NAME, &toRegister.PORT,
toRegister.IP_ADDR, &toRegister.width, &toRegister.heigth);
devices.push_back(toRegister);
}
else{
printf("Corrupted config!");
return false;
}
}
cpTemp = strstr(cpFileBuffer.get(), "HOTKEY_COUNT");
int hotkeyCount = 0;
if(cpTemp)
sscanf(cpTemp, "HOTKEY_COUNT=%d", &hotkeyCount);
else{
printf("Corrupted config!");
return false;
}
for(int i = 0; i < hotkeyCount; ++i){
char key[40];
sprintf(key, "\nHKEY_ID=%d", i);
char* cpTemp = strstr(cpFileBuffer.get(), key);
if(cpTemp){
++cpTemp;
int id = 0;
int type = 0;
const unsigned int keyMaxLen = 40;
const unsigned int argsMaxLen = 200;
char keys[keyMaxLen];
char args[argsMaxLen];
int res = sscanf(cpTemp, "HKEY_ID=%d KEYS=%s HKEY_TYPE=%d ARG=%[^\r\n];", &id, keys, &type, args);
HotkeyInfo toRegister;
toRegister.nHotkeyID = id;
switch (type)
{
case 1:
toRegister.setTypeAndArgs(SET_ACTIVE_SCREEN, args);
break;
case 2:
toRegister.setTypeAndArgs(TOGGLE_BORDERLESS_MODE, args);
break;
case 3:
if(args[strlen(args) - 1] == ';')
args[strlen(args) - 1] = 0; //omit the endofline ;
toRegister.setTypeAndArgs(EXECUTE_CMD, args);
break;
default:
break;
}
cpTemp = keys;
int commaCount = 0;
while(*cpTemp != 0){
if(*cpTemp == ','){++commaCount;}
++cpTemp;
}
toRegister.ucHotkeyLength = commaCount + 1;
toRegister.ucpHotkeySequence = new unsigned char[toRegister.ucHotkeyLength];
cpTemp = keys;
for(int i = 0; i < toRegister.ucHotkeyLength; ++i){
toRegister.ucpHotkeySequence[i] = (unsigned char)strtol(cpTemp, &cpTemp, 0);
++cpTemp;
}
hotkeys.push_back(toRegister);
}
else{
printf("Corrupted config!");
return false;
}
cpTemp = strstr(cpFileBuffer.get(), "BORDERLESS_SETTINGS_COUNT");
int blessSettingCount = 0;
if(cpTemp)
sscanf(cpTemp, "BORDERLESS_SETTINGS_COUNT=%d", &blessSettingCount);
else{
printf("Corrupted config!");
return false;
}
for(int i = 0; i < blessSettingCount; ++i){
char key[40];
sprintf(key, "\nBLESS_SET_ID=%d", i);
char* cpTemp = strstr(cpFileBuffer.get(), key);
if(cpTemp){
++cpTemp; //move beyond the newline.
BorderlessInfo temp;
int settingId;
int deviceId;
int res = sscanf(cpTemp, "BLESS_SET_ID=%d DEVICE_ID=%d LEFT=%d,%d RIGHT=%d,%d UP=%d,%d DOWN=%d,%d;",
&settingId, &deviceId, &temp.leftBorder, &temp.leftNeigh, &temp.rightBorder, &temp.rightNeigh,
&temp.topBorder, &temp.topNeigh, &temp.bottomBorder, &temp.bottomNeigh);
devices[deviceId].itsBorders = temp;
}
else{
printf("Corrupted config!");
return false;
}
}
cpTemp = strstr(cpFileBuffer.get(), "\nBLESS_ENABLE_CMD");
if(cpTemp != NULL){
const unsigned int argsMaxLen = 200;
char args[argsMaxLen];
sscanf(cpTemp+1, "BLESS_ENABLE_CMD=%[^\r\n]", args);
if(args[strlen(args) - 1] == ';')
args[strlen(args) - 1] = 0; //omit the endofline ;
borderlessEnableCmd = new char[strlen(args) + 1];
strcpy(borderlessEnableCmd, args);
}
cpTemp = strstr(cpFileBuffer.get(), "\nBLESS_DISABLE_CMD");
if(cpTemp != NULL){
const unsigned int argsMaxLen = 200;
char args[argsMaxLen];
sscanf(cpTemp+1, "BLESS_DISABLE_CMD=%[^\r\n]", args);
if(args[strlen(args) - 1] == ';')
args[strlen(args) - 1] = 0; //omit the endofline ;
borderlessDisableCmd = new char[strlen(args) + 1];
strcpy(borderlessDisableCmd, args);
}
}
PrintConfig();
return true;
}
bool CConfigHandler::ExportConfig(const char* par_cpFileName) {
FILE* filep = 0;
filep = fopen(par_cpFileName, "wb");
if(filep == NULL){
printf("Error: Can not create config file\n");
return false;
}
printf("Creating file %s\n", par_cpFileName);
fprintf(filep, "#MouseAroundConfigFile\r\n");
fprintf(filep, "#Sample Device Registirations:\r\n");
//TODO: add lines
fclose(filep);
return true;
}
void CConfigHandler::PrintConfig() {
for(const auto it: devices){
it.Print();
}
printf("BLESS_ENABLE_CMD:%s\n", borderlessEnableCmd);
printf("BLESS_ENABLE_CMD:%s\n", borderlessDisableCmd);
printf("HOTKEY_COUNT = %d\n", (int)hotkeys.size());
for(const auto& it : hotkeys){
it.Print();
}
}