-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathoverlay_utils.pas
More file actions
368 lines (331 loc) · 8.76 KB
/
Copy pathoverlay_utils.pas
File metadata and controls
368 lines (331 loc) · 8.76 KB
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
unit overlay_utils;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, StdCtrls, Graphics, Types, FileUtil, StrUtils, process,
constants, configmanager, systemdetector, apputils;
var
FONTS: TStringList; // Available system fonts cache
function GetUserConfigDir(): String;
function GetMangoHudConfigDir(): String;
function GetVkBasaltConfigDir(): String;
function GetVkSumiConfigDir(): String;
function GetGOverlayConfigDir(): String;
function GetGOverlayDataDir(): String;
function GetEnvironmentDConfigDir(): String;
function IsMangoHudGloballyEnabled(): Boolean;
procedure EnableMangoHudGlobally();
procedure DisableMangoHudGlobally();
function LoadFont(const Parametro: string; out Valor: TStringList): Boolean;
function GetStandardFontDirectories: TStringList;
procedure ListarFontesNoDiretorio(ComboBox: TComboBox);
procedure ListFontDirectories(out Dirs: TStringList);
procedure GetNetworkInterfaces(ComboBox: TComboBox);
implementation
function GetUserConfigDir(): String;
var
UserConfig: String;
begin
UserConfig := GetEnvironmentVariable('XDG_CONFIG_HOME');
if not DirectoryExists(UserConfig) then
begin
UserConfig := GetUserDir + '.config';
end;
Result := UserConfig;
end;
function GetMangoHudConfigDir(): String;
var
ConfigHome: String;
begin
if IsRunningInFlatpak then
begin
ConfigHome := GetUserDir + '.config';
end
else
begin
ConfigHome := GetEnvironmentVariable('HOST_XDG_CONFIG_HOME');
if ConfigHome = '' then
ConfigHome := GetEnvironmentVariable('XDG_CONFIG_HOME');
if ConfigHome = '' then
ConfigHome := GetUserDir + '.config';
end;
Result := IncludeTrailingPathDelimiter(ConfigHome) + 'MangoHud';
end;
function GetVkBasaltConfigDir(): String;
var
ConfigHome: String;
begin
if IsRunningInFlatpak then
begin
ConfigHome := GetUserDir + '.config';
end
else
begin
ConfigHome := GetEnvironmentVariable('XDG_CONFIG_HOME');
if ConfigHome = '' then
ConfigHome := GetUserDir + '.config';
end;
Result := IncludeTrailingPathDelimiter(ConfigHome) + 'vkBasalt';
end;
// Function to get vkSumi config directory with proper XDG support
function GetVkSumiConfigDir(): String;
var
ConfigHome: String;
begin
if IsRunningInFlatpak then
begin
ConfigHome := GetUserDir + '.config';
end
else
begin
ConfigHome := GetEnvironmentVariable('XDG_CONFIG_HOME');
if ConfigHome = '' then
ConfigHome := GetUserDir + '.config';
end;
Result := IncludeTrailingPathDelimiter(ConfigHome) + 'vkSumi';
end;
function GetGOverlayConfigDir(): String;
var
ConfigHome: String;
begin
ConfigHome := GetEnvironmentVariable('HOST_XDG_CONFIG_HOME');
if ConfigHome = '' then
ConfigHome := GetEnvironmentVariable('XDG_CONFIG_HOME');
if ConfigHome = '' then
ConfigHome := GetUserDir + '.config';
Result := IncludeTrailingPathDelimiter(ConfigHome) + 'goverlay';
end;
function GetGOverlayDataDir(): String;
var
DataHome: String;
UserName: String;
begin
DataHome := GetEnvironmentVariable('HOST_XDG_DATA_HOME');
if (DataHome = '') and IsRunningInFlatpak then
begin
UserName := GetEnvironmentVariable('USER');
if UserName <> '' then
DataHome := '/home/' + UserName + '/.local/share'
else
DataHome := GetUserDir + '.local/share';
end;
if DataHome = '' then
DataHome := GetEnvironmentVariable('XDG_DATA_HOME');
if DataHome = '' then
DataHome := GetUserDir + '.local/share';
Result := IncludeTrailingPathDelimiter(DataHome) + 'goverlay';
end;
function GetEnvironmentDConfigDir(): String;
var
ConfigHome: String;
begin
ConfigHome := GetEnvironmentVariable('HOST_XDG_CONFIG_HOME');
if ConfigHome = '' then
ConfigHome := GetEnvironmentVariable('XDG_CONFIG_HOME');
if ConfigHome = '' then
ConfigHome := GetUserDir + '.config';
Result := IncludeTrailingPathDelimiter(ConfigHome) + 'environment.d';
end;
function IsMangoHudGloballyEnabled(): Boolean;
var
ConfigFile: String;
FileContent: TStringList;
i: Integer;
begin
Result := False;
ConfigFile := IncludeTrailingPathDelimiter(GetEnvironmentDConfigDir()) + 'mangohud.conf';
if not FileExists(ConfigFile) then
Exit;
FileContent := TStringList.Create;
try
try
FileContent.LoadFromFile(ConfigFile);
for i := 0 to FileContent.Count - 1 do
begin
if Trim(FileContent[i]) = 'MANGOHUD=1' then
begin
Result := True;
Break;
end;
end;
except
Result := False;
end;
finally
FileContent.Free;
end;
end;
procedure EnableMangoHudGlobally();
var
ConfigDir, ConfigFile: String;
FileContent: TStringList;
begin
ConfigDir := GetEnvironmentDConfigDir();
ConfigFile := IncludeTrailingPathDelimiter(ConfigDir) + 'mangohud.conf';
if not DirectoryExists(ConfigDir) then
ForceDirectories(ConfigDir);
FileContent := TStringList.Create;
try
FileContent.Add('MANGOHUD=1');
FileContent.SaveToFile(ConfigFile);
finally
FileContent.Free;
end;
end;
procedure DisableMangoHudGlobally();
var
ConfigFile: String;
begin
ConfigFile := IncludeTrailingPathDelimiter(GetEnvironmentDConfigDir()) + 'mangohud.conf';
if FileExists(ConfigFile) then
DeleteFile(ConfigFile);
end;
function LoadFont(const Parametro: string; out Valor: TStringList): Boolean;
const
BUF_SIZE = 2048;
var
Process: TProcess;
Output: TStream;
BytesRead: longint;
Buffer: array[1..BUF_SIZE] of byte;
TempList: TStringList;
begin
if Assigned(Valor) then
FreeAndNil(Valor);
Process := TProcess.Create(nil);
Output := TMemoryStream.Create;
Valor := TStringList.Create;
Valor.Sorted := True;
Valor.Duplicates := dupIgnore;
Process.Executable := FindDefaultExecutablePath('sh');
Process.Parameters.Add('-c');
Process.Parameters.Add('fc-list -f "%{file}\n" :lang=en:fontformat=TrueType');
Process.Options := [poUsePipes];
Process.Execute;
repeat
BytesRead := Process.Output.Read(Buffer, BUF_SIZE);
Output.Write(Buffer, BytesRead);
until BytesRead = 0;
Process.Free;
TempList := TStringList.Create;
try
Output.Position := 0;
TempList.LoadFromStream(Output);
Valor.Text := StringReplace(TempList.Text, '\n', LineEnding, [rfReplaceAll, rfIgnoreCase]);
finally
TempList.Free;
end;
Output.Free;
Result := Valor.Text <> '';
end;
function GetStandardFontDirectories: TStringList;
var
Dir: String;
i: Integer;
begin
Result := TStringList.Create;
Result.Duplicates := dupIgnore;
Result.Sorted := True;
Result.Add('/usr/share/fonts');
Result.Add('/usr/local/share/fonts');
Result.Add(GetUserConfigDir + '/../.local/share/fonts');
Result.Add(GetUserConfigDir + '/../.fonts');
Result.Add('/run/current-system/sw/share/fonts');
Result.Add(GetEnvironmentVariable('HOME') + '/.nix-profile/share/fonts');
Result.Add('/var/lib/flatpak/exports/share/fonts');
Result.Add(GetUserConfigDir + '/../.local/share/flatpak/exports/share/fonts');
for i := Result.Count - 1 downto 0 do
begin
if not DirectoryExists(Result[i]) then
Result.Delete(i);
end;
end;
procedure ListarFontesNoDiretorio(ComboBox: TComboBox);
var
Arquivos, AllFonts, FontDirs: TStringList;
Arquivo, FontDir: String;
begin
AllFonts := TStringList.Create;
AllFonts.Duplicates := dupIgnore;
AllFonts.Sorted := True;
if LoadFont('fonts', FONTS) then
begin
for Arquivo in FONTS do
AllFonts.Add(Arquivo);
end
else
begin
FontDirs := GetStandardFontDirectories;
try
for FontDir in FontDirs do
begin
if DirectoryExists(FontDir) then
begin
Arquivos := FindAllFiles(FontDir, '*.ttf', True);
try
for Arquivo in Arquivos do
AllFonts.Add(Arquivo);
finally
Arquivos.Free;
end;
end;
end;
finally
FontDirs.Free;
end;
end;
try
for Arquivo in AllFonts do
begin
ComboBox.Items.Add(ExtractFileName(Arquivo));
end;
finally
AllFonts.Free;
end;
end;
procedure ListFontDirectories(out Dirs: TStringList);
var
FontDirs: TStringList;
Arquivo, FontDir: String;
begin
if LoadFont('fonts', FONTS) then
begin
for Arquivo in FONTS do
begin
Dirs.Add(ExtractFileDir(Arquivo));
end;
end
else
begin
FontDirs := GetStandardFontDirectories;
try
for FontDir in FontDirs do
begin
if DirectoryExists(FontDir) then
Dirs.Add(FontDir);
end;
finally
FontDirs.Free;
end;
end;
end;
procedure GetNetworkInterfaces(ComboBox: TComboBox);
var
Interfaces: TStringList;
i: Integer;
begin
ComboBox.Items.Clear;
Interfaces := systemdetector.GetNetworkInterfaces;
try
for i := 0 to Interfaces.Count - 1 do
ComboBox.Items.Add(Interfaces[i]);
finally
Interfaces.Free;
end;
end;
initialization
FONTS := nil;
finalization
if Assigned(FONTS) then
FreeAndNil(FONTS);
end.