-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathgeneral.c
163 lines (138 loc) · 6.14 KB
/
general.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
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
#include "fastfetch.h"
#include "common/jsonconfig.h"
#include "common/processing.h"
#include "options/general.h"
#include "util/stringUtils.h"
#include <unistd.h>
const char* ffOptionsParseGeneralJsonConfig(FFOptionsGeneral* options, yyjson_val* root)
{
yyjson_val* object = yyjson_obj_get(root, "general");
if (!object) return NULL;
if (!yyjson_is_obj(object)) return "Property 'general' must be an object";
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(object, idx, max, key_, val)
{
const char* key = yyjson_get_str(key_);
if (ffStrEqualsIgnCase(key, "thread") || ffStrEqualsIgnCase(key, "multithreading"))
options->multithreading = yyjson_get_bool(val);
else if (ffStrEqualsIgnCase(key, "processingTimeout"))
options->processingTimeout = (int32_t) yyjson_get_int(val);
else if (ffStrEqualsIgnCase(key, "preRun"))
{
if (system(yyjson_get_str(val)) < 0)
return "Failed to execute preRun command";
}
else if (ffStrEqualsIgnCase(key, "detectVersion"))
options->detectVersion = yyjson_get_bool(val);
#if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__)
else if (ffStrEqualsIgnCase(key, "playerName"))
ffStrbufSetS(&options->playerName, yyjson_get_str(val));
else if (ffStrEqualsIgnCase(key, "dsForceDrm"))
{
if (yyjson_is_str(val))
{
int value;
const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) {
{ "sysfs-only", FF_DS_FORCE_DRM_TYPE_SYSFS_ONLY },
{ "false", FF_DS_FORCE_DRM_TYPE_FALSE },
{ "true", FF_DS_FORCE_DRM_TYPE_TRUE },
{},
});
if (error)
return "Invalid enum value of `dsForceDrm`";
else
options->dsForceDrm = (FFDsForceDrmType) value;
}
else
options->dsForceDrm = yyjson_get_bool(val) ? FF_DS_FORCE_DRM_TYPE_TRUE : FF_DS_FORCE_DRM_TYPE_FALSE;
}
#elif defined(_WIN32)
else if (ffStrEqualsIgnCase(key, "wmiTimeout"))
options->wmiTimeout = (int32_t) yyjson_get_int(val);
#endif
else
return "Unknown general property";
}
return NULL;
}
bool ffOptionsParseGeneralCommandLine(FFOptionsGeneral* options, const char* key, const char* value)
{
if(ffStrEqualsIgnCase(key, "--thread") || ffStrEqualsIgnCase(key, "--multithreading"))
options->multithreading = ffOptionParseBoolean(value);
else if(ffStrEqualsIgnCase(key, "--processing-timeout"))
options->processingTimeout = ffOptionParseInt32(key, value);
else if(ffStrEqualsIgnCase(key, "--detect-version"))
options->detectVersion = ffOptionParseBoolean(value);
#if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__)
else if(ffStrEqualsIgnCase(key, "--player-name"))
ffOptionParseString(key, value, &options->playerName);
else if(ffStrEqualsIgnCase(key, "--ds-force-drm"))
{
if (ffOptionParseBoolean(value))
options->dsForceDrm = FF_DS_FORCE_DRM_TYPE_TRUE;
else if (ffStrEqualsIgnCase(value, "sysfs-only"))
options->dsForceDrm = FF_DS_FORCE_DRM_TYPE_SYSFS_ONLY;
else
options->dsForceDrm = FF_DS_FORCE_DRM_TYPE_FALSE;
}
#elif defined(_WIN32)
else if (ffStrEqualsIgnCase(key, "--wmi-timeout"))
options->wmiTimeout = ffOptionParseInt32(key, value);
#endif
else
return false;
return true;
}
void ffOptionsInitGeneral(FFOptionsGeneral* options)
{
options->processingTimeout = 5000;
options->multithreading = true;
options->detectVersion = true;
#if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__)
ffStrbufInit(&options->playerName);
options->dsForceDrm = FF_DS_FORCE_DRM_TYPE_FALSE;
#elif defined(_WIN32)
options->wmiTimeout = 5000;
#endif
}
void ffOptionsDestroyGeneral(FF_MAYBE_UNUSED FFOptionsGeneral* options)
{
#if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__)
ffStrbufDestroy(&options->playerName);
#endif
}
void ffOptionsGenerateGeneralJsonConfig(FFOptionsGeneral* options, yyjson_mut_doc* doc)
{
__attribute__((__cleanup__(ffOptionsDestroyGeneral))) FFOptionsGeneral defaultOptions;
ffOptionsInitGeneral(&defaultOptions);
yyjson_mut_val* obj = yyjson_mut_obj(doc);
if (options->multithreading != defaultOptions.multithreading)
yyjson_mut_obj_add_bool(doc, obj, "thread", options->multithreading);
if (options->processingTimeout != defaultOptions.processingTimeout)
yyjson_mut_obj_add_int(doc, obj, "processingTimeout", options->processingTimeout);
#if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__)
if (!ffStrbufEqual(&options->playerName, &defaultOptions.playerName))
yyjson_mut_obj_add_strbuf(doc, obj, "playerName", &options->playerName);
if (options->dsForceDrm != defaultOptions.dsForceDrm)
{
switch (options->dsForceDrm)
{
case FF_DS_FORCE_DRM_TYPE_FALSE:
yyjson_mut_obj_add_bool(doc, obj, "dsForceDrm", false);
break;
case FF_DS_FORCE_DRM_TYPE_SYSFS_ONLY:
yyjson_mut_obj_add_str(doc, obj, "dsForceDrm", "sysfs-only");
break;
case FF_DS_FORCE_DRM_TYPE_TRUE:
yyjson_mut_obj_add_bool(doc, obj, "dsForceDrm", true);
break;
}
}
#elif defined(_WIN32)
if (options->wmiTimeout != defaultOptions.wmiTimeout)
yyjson_mut_obj_add_int(doc, obj, "wmiTimeout", options->wmiTimeout);
#endif
if (yyjson_mut_obj_size(obj) > 0)
yyjson_mut_obj_add_val(doc, doc->root, "general", obj);
}