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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
#pragma once
#include <vector>
#include <string>
#include <string.h>
#include <functional>
#include <map>
#include <unistd.h>
#include <limits.h>
#include <inttypes.h>
#include <libgen.h>
#include <stdio.h>
#include <stdint.h>
#include <pwd.h>
#include <sys/stat.h>
struct ConfigHotkey {
int64_t keysym = 0;
uint32_t modifiers = 0;
};
struct MainConfig {
std::string record_area_option;
int32_t record_area_width = 0;
int32_t record_area_height = 0;
int32_t fps = 60;
bool merge_audio_tracks = true;
std::vector<std::string> audio_input;
std::string color_range;
std::string quality;
std::string codec; // Video codec
std::string audio_codec;
std::string framerate_mode;
bool advanced_view = false;
bool overclock = false;
bool show_notifications = true;
bool record_cursor = true;
bool hide_window_when_recording = false;
bool software_encoding_warning_shown = false;
bool restore_portal_session = true;
};
struct YoutubeStreamConfig {
std::string stream_key;
};
struct TwitchStreamConfig {
std::string stream_key;
};
struct CustomStreamConfig {
std::string url;
std::string container;
};
struct StreamingConfig {
std::string streaming_service;
YoutubeStreamConfig youtube;
TwitchStreamConfig twitch;
CustomStreamConfig custom;
ConfigHotkey start_recording_hotkey;
ConfigHotkey stop_recording_hotkey;
};
struct RecordConfig {
std::string save_directory;
std::string container;
ConfigHotkey start_recording_hotkey;
ConfigHotkey stop_recording_hotkey;
ConfigHotkey pause_recording_hotkey;
ConfigHotkey unpause_recording_hotkey;
};
struct ReplayConfig {
std::string save_directory;
std::string container;
int32_t replay_time = 30;
ConfigHotkey start_recording_hotkey;
ConfigHotkey stop_recording_hotkey;
ConfigHotkey save_recording_hotkey;
};
struct Config {
MainConfig main_config;
StreamingConfig streaming_config;
RecordConfig record_config;
ReplayConfig replay_config;
};
typedef enum {
CONFIG_TYPE_BOOL,
CONFIG_TYPE_STRING, // std::string
CONFIG_TYPE_I32,
CONFIG_TYPE_HOTKEY, // ConfigHotkey
CONFIG_TYPE_STRING_ARRAY, // std::vector<std::string>
} ConfigValueType;
struct ConfigValue {
ConfigValueType type;
void *data;
};
static std::string get_home_dir() {
const char *home_dir = getenv("HOME");
if(!home_dir) {
passwd *pw = getpwuid(getuid());
home_dir = pw->pw_dir;
}
if(!home_dir) {
fprintf(stderr, "Error: Failed to get home directory of user, using /tmp directory\n");
home_dir = "/tmp";
}
return home_dir;
}
static std::string get_config_dir() {
std::string config_dir;
const char *xdg_config_home = getenv("XDG_CONFIG_HOME");
if(xdg_config_home) {
config_dir = xdg_config_home;
} else {
config_dir = get_home_dir() + "/.config";
}
config_dir += "/gpu-screen-recorder";
return config_dir;
}
// Whoever designed xdg-user-dirs is retarded. Why are some XDG variables environment variables
// while others are in this pseudo shell config file ~/.config/user-dirs.dirs
static std::map<std::string, std::string> get_xdg_variables() {
std::string user_dirs_filepath;
const char *xdg_config_home = getenv("XDG_CONFIG_HOME");
if(xdg_config_home) {
user_dirs_filepath = xdg_config_home;
} else {
user_dirs_filepath = get_home_dir() + "/.config";
}
user_dirs_filepath += "/user-dirs.dirs";
std::map<std::string, std::string> result;
FILE *f = fopen(user_dirs_filepath.c_str(), "rb");
if(!f)
return result;
char line[PATH_MAX];
while(fgets(line, sizeof(line), f)) {
int len = strlen(line);
if(len < 2)
continue;
if(line[0] == '#')
continue;
if(line[len - 1] == '\n') {
line[len - 1] = '\0';
len--;
}
if(line[len - 1] != '"')
continue;
line[len - 1] = '\0';
len--;
const char *sep = strchr(line, '=');
if(!sep)
continue;
if(sep[1] != '\"')
continue;
std::string value(sep + 2);
if(strncmp(value.c_str(), "$HOME/", 6) == 0)
value = get_home_dir() + value.substr(5);
std::string key(line, sep - line);
result[std::move(key)] = std::move(value);
}
fclose(f);
return result;
}
static std::string get_videos_dir() {
auto xdg_vars = get_xdg_variables();
std::string xdg_videos_dir = xdg_vars["XDG_VIDEOS_DIR"];
if(xdg_videos_dir.empty())
xdg_videos_dir = get_home_dir() + "/Videos";
return xdg_videos_dir;
}
static int create_directory_recursive(char *path) {
int path_len = strlen(path);
char *p = path;
char *end = path + path_len;
for(;;) {
char *slash_p = strchr(p, '/');
// Skips first '/', we don't want to try and create the root directory
if(slash_p == path) {
++p;
continue;
}
if(!slash_p)
slash_p = end;
char prev_char = *slash_p;
*slash_p = '\0';
int err = mkdir(path, S_IRWXU);
*slash_p = prev_char;
if(err == -1 && errno != EEXIST)
return err;
if(slash_p == end)
break;
else
p = slash_p + 1;
}
return 0;
}
static bool file_get_content(const char *filepath, std::string &file_content) {
file_content.clear();
bool success = false;
FILE *file = fopen(filepath, "rb");
if(!file)
return success;
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
if(file_size != -1) {
file_content.resize(file_size);
fseek(file, 0, SEEK_SET);
if((long)fread(&file_content[0], 1, file_size, file) == file_size)
success = true;
}
fclose(file);
return success;
}
struct StringView {
const char *str = nullptr;
size_t size = 0;
bool operator == (const char *other) const {
int len = strlen(other);
return (size_t)len == size && memcmp(str, other, size) == 0;
}
size_t find(char c) const {
const void *p = memchr(str, c, size);
if(!p)
return std::string::npos;
return (const char*)p - str;
}
};
using StringSplitCallback = std::function<bool(StringView line)>;
static void string_split_char(const std::string &str, char delimiter, StringSplitCallback callback_func) {
size_t index = 0;
while(index < str.size()) {
size_t new_index = str.find(delimiter, index);
if(new_index == std::string::npos)
new_index = str.size();
if(!callback_func({str.data() + index, new_index - index}))
break;
index = new_index + 1;
}
}
static bool config_split_key_value(const StringView str, StringView &key, StringView &value) {
key.str = nullptr;
key.size = 0;
value.str = nullptr;
value.size = 0;
size_t index = str.find(' ');
if(index == std::string::npos)
return std::string::npos;
key.str = str.str;
key.size = index;
value.str = str.str + index + 1;
value.size = str.size - (index + 1);
return true;
}
static std::map<std::string, ConfigValue> get_config_options(Config &config) {
return {
{"main.record_area_option", {CONFIG_TYPE_STRING, &config.main_config.record_area_option}},
{"main.record_area_width", {CONFIG_TYPE_I32, &config.main_config.record_area_width}},
{"main.record_area_height", {CONFIG_TYPE_I32, &config.main_config.record_area_height}},
{"main.fps", {CONFIG_TYPE_I32, &config.main_config.fps}},
{"main.merge_audio_tracks", {CONFIG_TYPE_BOOL, &config.main_config.merge_audio_tracks}},
{"main.audio_input", {CONFIG_TYPE_STRING_ARRAY, &config.main_config.audio_input}},
{"main.color_range", {CONFIG_TYPE_STRING, &config.main_config.color_range}},
{"main.quality", {CONFIG_TYPE_STRING, &config.main_config.quality}},
{"main.codec", {CONFIG_TYPE_STRING, &config.main_config.codec}},
{"main.audio_codec", {CONFIG_TYPE_STRING, &config.main_config.audio_codec}},
{"main.framerate_mode", {CONFIG_TYPE_STRING, &config.main_config.framerate_mode}},
{"main.advanced_view", {CONFIG_TYPE_BOOL, &config.main_config.advanced_view}},
{"main.overclock", {CONFIG_TYPE_BOOL, &config.main_config.overclock}},
{"main.show_notifications", {CONFIG_TYPE_BOOL, &config.main_config.show_notifications}},
{"main.record_cursor", {CONFIG_TYPE_BOOL, &config.main_config.record_cursor}},
{"main.hide_window_when_recording", {CONFIG_TYPE_BOOL, &config.main_config.hide_window_when_recording}},
{"main.software_encoding_warning_shown", {CONFIG_TYPE_BOOL, &config.main_config.software_encoding_warning_shown}},
{"main.restore_portal_session", {CONFIG_TYPE_BOOL, &config.main_config.restore_portal_session}},
{"streaming.service", {CONFIG_TYPE_STRING, &config.streaming_config.streaming_service}},
{"streaming.youtube.key", {CONFIG_TYPE_STRING, &config.streaming_config.youtube.stream_key}},
{"streaming.twitch.key", {CONFIG_TYPE_STRING, &config.streaming_config.twitch.stream_key}},
{"streaming.custom.url", {CONFIG_TYPE_STRING, &config.streaming_config.custom.url}},
{"streaming.custom.container", {CONFIG_TYPE_STRING, &config.streaming_config.custom.container}},
{"streaming.start_recording_hotkey", {CONFIG_TYPE_HOTKEY, &config.streaming_config.start_recording_hotkey}},
{"streaming.stop_recording_hotkey", {CONFIG_TYPE_HOTKEY, &config.streaming_config.stop_recording_hotkey}},
{"record.save_directory", {CONFIG_TYPE_STRING, &config.record_config.save_directory}},
{"record.container", {CONFIG_TYPE_STRING, &config.record_config.container}},
{"record.start_recording_hotkey", {CONFIG_TYPE_HOTKEY, &config.record_config.start_recording_hotkey}},
{"record.stop_recording_hotkey", {CONFIG_TYPE_HOTKEY, &config.record_config.stop_recording_hotkey}},
{"record.pause_recording_hotkey", {CONFIG_TYPE_HOTKEY, &config.record_config.pause_recording_hotkey}},
{"record.unpause_recording_hotkey", {CONFIG_TYPE_HOTKEY, &config.record_config.unpause_recording_hotkey}},
{"replay.save_directory", {CONFIG_TYPE_STRING, &config.replay_config.save_directory}},
{"replay.container", {CONFIG_TYPE_STRING, &config.replay_config.container}},
{"replay.time", {CONFIG_TYPE_I32, &config.replay_config.replay_time}},
{"replay.start_recording_hotkey", {CONFIG_TYPE_HOTKEY, &config.replay_config.start_recording_hotkey}},
{"replay.stop_recording_hotkey", {CONFIG_TYPE_HOTKEY, &config.replay_config.stop_recording_hotkey}},
{"replay.save_recording_hotkey", {CONFIG_TYPE_HOTKEY, &config.replay_config.save_recording_hotkey}}
};
}
#define FORMAT_I32 "%" PRIi32
#define FORMAT_I64 "%" PRIi64
#define FORMAT_U32 "%" PRIu32
static Config read_config(bool &config_empty) {
Config config;
const std::string config_path = get_config_dir() + "/config";
std::string file_content;
if(!file_get_content(config_path.c_str(), file_content)) {
fprintf(stderr, "Warning: Failed to read config file: %s\n", config_path.c_str());
config_empty = true;
return config;
}
auto config_options = get_config_options(config);
string_split_char(file_content, '\n', [&](StringView line) {
StringView key, value;
if(!config_split_key_value(line, key, value)) {
fprintf(stderr, "Warning: Invalid config option format: %.*s\n", (int)line.size, line.str);
return true;
}
if(key.size == 0 || value.size == 0)
return true;
auto it = config_options.find(std::string(key.str, key.size));
if(it == config_options.end())
return true;
switch(it->second.type) {
case CONFIG_TYPE_BOOL: {
*(bool*)it->second.data = value == "true";
break;
}
case CONFIG_TYPE_STRING: {
((std::string*)it->second.data)->assign(value.str, value.size);
break;
}
case CONFIG_TYPE_I32: {
std::string value_str(value.str, value.size);
int32_t *value = (int32_t*)it->second.data;
if(sscanf(value_str.c_str(), FORMAT_I32, value) != 1) {
fprintf(stderr, "Warning: Invalid config option value for %.*s\n", (int)key.size, key.str);
*value = 0;
}
break;
}
case CONFIG_TYPE_HOTKEY: {
std::string value_str(value.str, value.size);
ConfigHotkey *config_hotkey = (ConfigHotkey*)it->second.data;
if(sscanf(value_str.c_str(), FORMAT_I64 " " FORMAT_U32, &config_hotkey->keysym, &config_hotkey->modifiers) != 2) {
fprintf(stderr, "Warning: Invalid config option value for %.*s\n", (int)key.size, key.str);
config_hotkey->keysym = 0;
config_hotkey->modifiers = 0;
}
break;
}
case CONFIG_TYPE_STRING_ARRAY: {
std::string array_value(value.str, value.size);
((std::vector<std::string>*)it->second.data)->push_back(std::move(array_value));
break;
}
}
return true;
});
return config;
}
static void save_config(Config &config) {
const std::string config_path = get_config_dir() + "/config";
char dir_tmp[PATH_MAX];
strcpy(dir_tmp, config_path.c_str());
char *dir = dirname(dir_tmp);
if(create_directory_recursive(dir) != 0) {
fprintf(stderr, "Warning: Failed to create config directory: %s\n", dir);
return;
}
FILE *file = fopen(config_path.c_str(), "wb");
if(!file) {
fprintf(stderr, "Warning: Failed to create config file: %s\n", config_path.c_str());
return;
}
const auto config_options = get_config_options(config);
for(auto it : config_options) {
switch(it.second.type) {
case CONFIG_TYPE_BOOL: {
fprintf(file, "%s %s\n", it.first.c_str(), *(bool*)it.second.data ? "true" : "false");
break;
}
case CONFIG_TYPE_STRING: {
fprintf(file, "%s %s\n", it.first.c_str(), ((std::string*)it.second.data)->c_str());
break;
}
case CONFIG_TYPE_I32: {
fprintf(file, "%s " FORMAT_I32 "\n", it.first.c_str(), *(int32_t*)it.second.data);
break;
}
case CONFIG_TYPE_HOTKEY: {
const ConfigHotkey *config_hotkey = (const ConfigHotkey*)it.second.data;
fprintf(file, "%s " FORMAT_I64 " " FORMAT_U32 "\n", it.first.c_str(), config_hotkey->keysym, config_hotkey->modifiers);
break;
}
case CONFIG_TYPE_STRING_ARRAY: {
for(const std::string &value : *(std::vector<std::string>*)it.second.data) {
fprintf(file, "%s %s\n", it.first.c_str(), value.c_str());
}
break;
}
}
}
fclose(file);
}
|