-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathsystemdetector.pas
More file actions
1281 lines (1172 loc) · 33.6 KB
/
Copy pathsystemdetector.pas
File metadata and controls
1281 lines (1172 loc) · 33.6 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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit systemdetector;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Process, StrUtils, FileUtil, IniFiles, Dialogs, configmanager;
type
/// <summary>
/// GPU vendor types
/// </summary>
TGPUVendor = (gpuUnknown, gpuAMD, gpuNVIDIA, gpuIntel);
/// <summary>
/// Session type (X11 or Wayland)
/// </summary>
TSessionType = (sessionUnknown, sessionX11, sessionWayland);
/// <summary>
/// Detects if the application is running inside a Flatpak sandbox
/// </summary>
/// <returns>True if running in Flatpak, False otherwise</returns>
function IsRunningInFlatpak: Boolean;
/// <summary>
/// Checks if a command is available in the system PATH
/// </summary>
/// <param name="CommandName">Name of the command to check</param>
/// <returns>True if command exists, False otherwise</returns>
function IsCommandAvailable(const CommandName: string): Boolean;
/// <summary>
/// Checks if a command is available on the host system (for Flatpak)
/// This is used for commands that are never inside the Flatpak sandbox
/// </summary>
/// <param name="CommandName">Name of the command to check</param>
/// <returns>True if command exists on host, False otherwise</returns>
function IsHostCommandAvailable(const CommandName: string): Boolean;
/// <summary>
/// Detects GPU vendor by reading /sys/bus/pci/devices (Flatpak-compatible)
/// </summary>
/// <returns>GPU vendor type</returns>
function DetectGPUVendorFromSys: TGPUVendor;
/// <summary>
/// Detects GPU vendor using lspci command (traditional method)
/// </summary>
/// <returns>GPU vendor type</returns>
function DetectGPUVendorFromLspci: TGPUVendor;
/// <summary>
/// Detects GPU vendor (automatically chooses method based on environment)
/// </summary>
/// <returns>GPU vendor type</returns>
function DetectGPUVendor: TGPUVendor;
/// <summary>
/// Converts GPU vendor enum to string
/// </summary>
/// <param name="Vendor">GPU vendor type</param>
/// <returns>Vendor name as string (AMD, NVIDIA, Intel, unknown)</returns>
function GPUVendorToString(Vendor: TGPUVendor): string;
/// <summary>
/// Checks if NVIDIA kernel module is loaded
/// </summary>
/// <returns>True if nvidia module is loaded, False otherwise</returns>
function IsNvidiaDriverLoaded: Boolean;
/// <summary>
/// Gets network interfaces from /sys/class/net (Flatpak-compatible)
/// </summary>
/// <returns>List of network interface names</returns>
function GetNetworkInterfacesFromSys: TStringList;
/// <summary>
/// Gets network interfaces using 'ip link' command (traditional method)
/// </summary>
/// <returns>List of network interface names</returns>
function GetNetworkInterfacesFromCommand: TStringList;
/// <summary>
/// Gets network interfaces (automatically chooses method based on environment)
/// </summary>
/// <returns>List of network interface names</returns>
function GetNetworkInterfaces: TStringList;
/// <summary>
/// Gets standard font directories for different distributions
/// </summary>
/// <returns>List of existing font directory paths</returns>
function GetStandardFontDirectories: TStringList;
/// <summary>
/// Detects the current session type (X11 or Wayland)
/// </summary>
/// <returns>Session type</returns>
function DetectSessionType: TSessionType;
/// <summary>
/// Converts session type enum to string
/// </summary>
/// <param name="SessionType">Session type</param>
/// <returns>Session type as string (x11, wayland, unknown)</returns>
function SessionTypeToString(Session: TSessionType): string;
/// <summary>
/// Finds the default executable path for a command
/// </summary>
/// <param name="ExecutableName">Name of the executable</param>
/// <returns>Full path to executable, or just the name if not found</returns>
function FindDefaultExecutablePath(const ExecutableName: string): string;
/// <summary>
/// Gets Linux distribution name
/// </summary>
function GetSysLinuxDistribution: string;
/// <summary>
/// Gets CPU model name
/// </summary>
function GetSysCPUModel: string;
/// <summary>
/// Gets GPU model name
/// </summary>
function GetSysGPUModel: string;
/// <summary>
/// Gets GPU Driver info
/// </summary>
function GetSysGPUDriver: string;
/// <summary>
/// Gets or generates the Goverlay Client ID
/// </summary>
function GetGoverlayClientID: string;
function GetUserNickname: string;
procedure SaveUserNickname(const ANickname: string);
function GetPasCubeNicknameParam: string;
procedure CheckPromptUserNickname;
/// <summary>
/// Checks whether a shared library (e.g. 'libqt6pas') is available on the
/// current system. Works across Ubuntu, Debian, Fedora, OpenSUSE, Arch and
/// NixOS by first querying ldconfig and then scanning the standard lib dirs.
/// </summary>
/// <param name="LibName">Library base name without extension (e.g. 'libqt6pas')</param>
/// <returns>True if any matching .so file is found</returns>
function IsLibraryAvailable(const LibName: string): Boolean;
/// <summary>
/// Checks if a Nerd Font is installed in the system using fc-list
/// </summary>
/// <returns>True if at least one Nerd Font is found</returns>
function IsNerdFontInstalled: Boolean;
/// <summary>
/// Gets GOverlay installation type (Flatpak, AppImage, or Native)
/// </summary>
/// <returns>String indicating installation type</returns>
function GetGOverlayInstallationType: string;
/// <summary>
/// Gets GOverlay package type environment variable prefix
/// </summary>
function GetGOverlayPackageEnv: string;
/// <summary>
/// Checks if the pascube Vulkan test binary is available locally or on PATH.
/// </summary>
function IsPasCubeAvailable: Boolean;
/// <summary>
/// Gets the command string to launch pascube.
/// </summary>
function GetPasCubeCommand: string;
implementation
function RunCommand(const Executable: string; const Parameters: array of string; out Output: string): Boolean;
var
AProcess: TProcess;
i: Integer;
SL: TStringList;
begin
Result := False;
Output := '';
AProcess := TProcess.Create(nil);
SL := TStringList.Create;
try
try
AProcess.Executable := Executable;
for i := Low(Parameters) to High(Parameters) do
AProcess.Parameters.Add(Parameters[i]);
AProcess.Options := [poWaitOnExit, poUsePipes];
AProcess.Execute;
SL.LoadFromStream(AProcess.Output);
Output := Trim(SL.Text);
Result := AProcess.ExitStatus = 0;
except
Result := False;
end;
finally
SL.Free;
AProcess.Free;
end;
end;
function IsRunningInFlatpak: Boolean;
begin
Result := GetEnvironmentVariable('FLATPAK_ID') <> '';
end;
function IsCommandAvailable(const CommandName: string): Boolean;
var
Output: string;
begin
// We use 'which' to check if command exists
Result := RunCommand('which', [CommandName], Output);
if not Result and (CommandName = '7z') then
Result := RunCommand('which', ['7zz'], Output);
end;
function IsHostCommandAvailable(const CommandName: string): Boolean;
const
// Common binary paths on Linux systems
HostPaths: array[0..5] of string = (
'/usr/bin/',
'/usr/local/bin/',
'/bin/',
'/usr/games/',
'/usr/local/games/',
'/opt/bin/'
);
var
i: Integer;
HostPath: string;
begin
Result := False;
// If not in Flatpak, use regular command check
if not IsRunningInFlatpak then
begin
Result := IsCommandAvailable(CommandName);
Exit;
end;
// In Flatpak: check common host paths directly via filesystem
// The host /usr is typically mounted at /run/host/usr in Flatpak
for i := Low(HostPaths) to High(HostPaths) do
begin
HostPath := '/run/host' + HostPaths[i] + CommandName;
if FileExists(HostPath) then
begin
Result := True;
Exit;
end;
end;
end;
function DetectGPUVendorFromSys: TGPUVendor;
var
SearchRec: TSearchRec;
VendorFile, DeviceClassFile: string;
VendorID: string;
DeviceClass: string;
VendorText: TStringList;
begin
Result := gpuUnknown;
// Search for VGA devices in /sys/bus/pci/devices/
if FindFirst('/sys/bus/pci/devices/*', faDirectory, SearchRec) = 0 then
begin
try
repeat
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
begin
DeviceClassFile := '/sys/bus/pci/devices/' + SearchRec.Name + '/class';
VendorFile := '/sys/bus/pci/devices/' + SearchRec.Name + '/vendor';
// Check if this is a VGA device (class 0x03xxxx)
if FileExists(DeviceClassFile) and FileExists(VendorFile) then
begin
VendorText := TStringList.Create;
try
VendorText.LoadFromFile(DeviceClassFile);
if VendorText.Count > 0 then
begin
DeviceClass := Trim(VendorText[0]);
// VGA controller class starts with 0x03
if (Length(DeviceClass) >= 4) and (Copy(DeviceClass, 1, 4) = '0x03') then
begin
VendorText.Clear;
VendorText.LoadFromFile(VendorFile);
if VendorText.Count > 0 then
begin
VendorID := Trim(VendorText[0]);
// Check vendor IDs
case VendorID of
'0x1002': Result := gpuAMD; // AMD/ATI
'0x10de': Result := gpuNVIDIA; // NVIDIA
'0x8086': Result := gpuIntel; // Intel
end;
if Result <> gpuUnknown then
Break; // Found a GPU, stop searching
end;
end;
end;
finally
VendorText.Free;
end;
end;
end;
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
end;
end;
end;
function DetectGPUVendorFromLspci: TGPUVendor;
var
Output: string;
Executable: string;
begin
Result := gpuUnknown;
Executable := FindDefaultExecutablePath('lspci');
if RunCommand(Executable, ['-nn'], Output) then
begin
// Search for VGA or 3D controller in Output
if (Pos('VGA', Output) > 0) or (Pos('3D controller', Output) > 0) then
begin
// Check for vendor identifiers
if (Pos('AMD', Output) > 0) or (Pos('ATI', Output) > 0) or (Pos('[1002:', Output) > 0) then
Result := gpuAMD
else if (Pos('NVIDIA', Output) > 0) or (Pos('[10de:', Output) > 0) then
Result := gpuNVIDIA
else if (Pos('Intel', Output) > 0) or (Pos('[8086:', Output) > 0) then
Result := gpuIntel;
end;
end;
end;
function DetectGPUVendor: TGPUVendor;
begin
// Use Flatpak-compatible detection if running in sandbox or lspci not available
if IsRunningInFlatpak or not IsCommandAvailable('lspci') then
Result := DetectGPUVendorFromSys
else
Result := DetectGPUVendorFromLspci;
end;
function GPUVendorToString(Vendor: TGPUVendor): string;
begin
case Vendor of
gpuAMD: Result := 'AMD';
gpuNVIDIA: Result := 'NVIDIA';
gpuIntel: Result := 'Intel';
else Result := 'unknown';
end;
end;
function IsNvidiaDriverLoaded: Boolean;
var
SL: TStringList;
begin
Result := False;
// Direct file read from /proc/modules is faster and works in sandboxes
if FileExists('/proc/modules') then
begin
SL := TStringList.Create;
try
SL.LoadFromFile('/proc/modules');
Result := Pos('nvidia', SL.Text) > 0;
finally
SL.Free;
end;
end;
end;
function GetNetworkInterfacesFromSys: TStringList;
var
SearchRec: TSearchRec;
InterfaceName: string;
begin
Result := TStringList.Create;
Result.Sorted := True;
Result.Duplicates := dupIgnore;
// Read directly from /sys/class/net/
if FindFirst('/sys/class/net/*', faAnyFile, SearchRec) = 0 then
begin
try
repeat
InterfaceName := SearchRec.Name;
// Filter out . and .. and loopback
if (InterfaceName <> '.') and (InterfaceName <> '..') and (InterfaceName <> 'lo') then
begin
// Add common network interface types
if (Pos('eth', InterfaceName) = 1) or
(Pos('enp', InterfaceName) = 1) or
(Pos('wlan', InterfaceName) = 1) or
(Pos('wlp', InterfaceName) = 1) or
(Pos('wlo', InterfaceName) = 1) then
begin
Result.Add(InterfaceName);
end;
end;
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
end;
end;
end;
function GetNetworkInterfacesFromCommand: TStringList;
var
OutputStr: string;
Output: TStringList;
i: Integer;
Line, InterfaceName: string;
Executable: string;
begin
Result := TStringList.Create;
Result.Sorted := True;
Result.Duplicates := dupIgnore;
Executable := FindDefaultExecutablePath('ip');
if RunCommand(Executable, ['link'], OutputStr) then
begin
Output := TStringList.Create;
try
Output.Text := OutputStr;
// Parse ip link output
for i := 0 to Output.Count - 1 do
begin
Line := Output[i];
// Lines with interface names start with a number
if (Length(Line) > 0) and (Line[1] in ['0'..'9']) then
begin
// Extract interface name (format: "2: enp0s3: <BROADCAST,...")
Delete(Line, 1, Pos(':', Line)); // Remove number and first colon
Line := Trim(Line);
InterfaceName := Copy(Line, 1, Pos(':', Line) - 1);
// Filter relevant interfaces
if (InterfaceName <> 'lo') and
((Pos('eth', InterfaceName) = 1) or
(Pos('enp', InterfaceName) = 1) or
(Pos('wlan', InterfaceName) = 1) or
(Pos('wlp', InterfaceName) = 1) or
(Pos('wlo', InterfaceName) = 1)) then
begin
Result.Add(InterfaceName);
end;
end;
end;
finally
Output.Free;
end;
end;
end;
function GetNetworkInterfaces: TStringList;
begin
// Use Flatpak-compatible detection if running in sandbox or ip command not available
if IsRunningInFlatpak or not IsCommandAvailable('ip') then
Result := GetNetworkInterfacesFromSys
else
Result := GetNetworkInterfacesFromCommand;
end;
function GetStandardFontDirectories: TStringList;
var
Dir: String;
i: Integer;
begin
Result := TStringList.Create;
Result.Duplicates := dupIgnore;
Result.Sorted := True;
// Standard Linux font directories
Result.Add('/usr/share/fonts');
Result.Add('/usr/local/share/fonts');
Result.Add(GetUserDir + '.local/share/fonts');
Result.Add(GetUserDir + '.fonts');
// NixOS-specific directories
Result.Add('/run/current-system/sw/share/fonts');
Result.Add(GetEnvironmentVariable('HOME') + '/.nix-profile/share/fonts');
// Flatpak font directories
Result.Add('/var/lib/flatpak/exports/share/fonts');
Result.Add(GetUserDir + '.local/share/flatpak/exports/share/fonts');
// Remove directories that don't exist
i := Result.Count - 1;
while i >= 0 do
begin
if not DirectoryExists(Result[i]) then
Result.Delete(i);
Dec(i);
end;
end;
function DetectSessionType: TSessionType;
var
SessionTypeStr: string;
begin
SessionTypeStr := LowerCase(GetEnvironmentVariable('XDG_SESSION_TYPE'));
if SessionTypeStr = 'wayland' then
Result := sessionWayland
else if SessionTypeStr = 'x11' then
Result := sessionX11
else
Result := sessionUnknown;
end;
function SessionTypeToString(Session: TSessionType): string;
begin
case Session of
sessionX11: Result := 'x11';
sessionWayland: Result := 'wayland';
else Result := 'unknown';
end;
end;
function FindDefaultExecutablePath(const ExecutableName: string): string;
var
Output: string;
begin
Result := ExecutableName; // Default fallback
if RunCommand('which', [ExecutableName], Output) then
begin
if Output <> '' then
Result := Output;
end
else if ExecutableName = '7z' then
begin
if RunCommand('which', ['7zz'], Output) then
begin
if Output <> '' then
Result := Output;
end;
end;
end;
function GetSysLinuxDistribution: string;
var
SL: TStringList;
Line: string;
begin
Result := 'Unknown Linux';
if FileExists('/etc/os-release') then
begin
SL := TStringList.Create;
try
SL.LoadFromFile('/etc/os-release');
for Line in SL do
begin
if Pos('PRETTY_NAME=', Line) = 1 then
begin
Result := StringReplace(Line, 'PRETTY_NAME=', '', []);
Result := StringReplace(Result, '"', '', [rfReplaceAll]);
Break;
end;
end;
finally
SL.Free;
end;
end;
end;
function GetSysCPUModel: string;
var
SL: TStringList;
Line: string;
begin
Result := 'Unknown CPU';
if FileExists('/proc/cpuinfo') then
begin
SL := TStringList.Create;
try
SL.LoadFromFile('/proc/cpuinfo');
for Line in SL do
begin
if Pos('model name', Line) = 1 then
begin
Result := Trim(Copy(Line, Pos(':', Line) + 1, Length(Line)));
Break;
end;
end;
finally
SL.Free;
end;
end;
end;
function GetGPUModelFromSysfs: string;
var
SR: TSearchRec;
Path, VendorFile, DeviceFile, BootVgaFile: string;
VendorId, DeviceId: string;
SL: TStringList;
FoundCard: string;
begin
Result := 'Unknown GPU';
FoundCard := '';
SL := TStringList.Create;
try
if FindFirst('/sys/class/drm/card*', faAnyFile, SR) = 0 then
begin
repeat
if (SR.Name <> '.') and (SR.Name <> '..') then
begin
Path := '/sys/class/drm/' + SR.Name + '/device/';
if DirectoryExists(Path) then
begin
BootVgaFile := Path + 'boot_vga';
if FileExists(BootVgaFile) then
begin
try
SL.LoadFromFile(BootVgaFile);
if (SL.Count > 0) and (Trim(SL[0]) = '1') then
begin
FoundCard := Path;
Break;
end;
except
end;
end;
if FoundCard = '' then
FoundCard := Path;
end;
end;
until FindNext(SR) <> 0;
FindClose(SR);
end;
if FoundCard <> '' then
begin
VendorFile := FoundCard + 'vendor';
DeviceFile := FoundCard + 'device';
VendorId := '';
DeviceId := '';
if FileExists(VendorFile) then
begin
try
SL.LoadFromFile(VendorFile);
if SL.Count > 0 then VendorId := LowerCase(Trim(SL[0]));
except
end;
end;
if FileExists(DeviceFile) then
begin
try
SL.LoadFromFile(DeviceFile);
if SL.Count > 0 then DeviceId := LowerCase(Trim(SL[0]));
except
end;
end;
if VendorId <> '' then
begin
if (Pos('1002', VendorId) > 0) or (Pos('0x1002', VendorId) > 0) then
Result := 'AMD GPU'
else if (Pos('10de', VendorId) > 0) or (Pos('0x10de', VendorId) > 0) then
Result := 'NVIDIA GPU'
else if (Pos('8086', VendorId) > 0) or (Pos('0x8086', VendorId) > 0) then
Result := 'Intel GPU'
else
Result := 'Generic GPU (' + VendorId + ':' + DeviceId + ')';
end;
end;
finally
SL.Free;
end;
end;
function GetGPUDriverFromSysfs: string;
var
SR: TSearchRec;
Path, UeventFile, BootVgaFile, Line: string;
SL: TStringList;
FoundCard: string;
begin
Result := 'Unknown Driver';
FoundCard := '';
SL := TStringList.Create;
try
if FindFirst('/sys/class/drm/card*', faAnyFile, SR) = 0 then
begin
repeat
if (SR.Name <> '.') and (SR.Name <> '..') then
begin
Path := '/sys/class/drm/' + SR.Name + '/device/';
if DirectoryExists(Path) then
begin
BootVgaFile := Path + 'boot_vga';
if FileExists(BootVgaFile) then
begin
try
SL.LoadFromFile(BootVgaFile);
if (SL.Count > 0) and (Trim(SL[0]) = '1') then
begin
FoundCard := Path;
Break;
end;
except
end;
end;
if FoundCard = '' then
FoundCard := Path;
end;
end;
until FindNext(SR) <> 0;
FindClose(SR);
end;
if FoundCard <> '' then
begin
UeventFile := FoundCard + 'uevent';
if FileExists(UeventFile) then
begin
try
SL.LoadFromFile(UeventFile);
for Line in SL do
begin
if Pos('DRIVER=', Line) = 1 then
begin
Result := Trim(Copy(Line, 8, Length(Line)));
Break;
end;
end;
except
end;
end;
end;
finally
SL.Free;
end;
end;
function GetSysGPUModel: string;
var
Output, Line, CleanName: string;
SL: TStringList;
begin
Result := 'Unknown GPU';
if RunCommand('glxinfo', ['-B'], Output) then
begin
SL := TStringList.Create;
try
SL.Text := Output;
for Line in SL do
begin
if Pos('OpenGL renderer string:', Trim(Line)) = 1 then
begin
CleanName := Trim(Copy(Trim(Line), 24, Length(Line)));
if Pos('(', CleanName) > 0 then
CleanName := Trim(Copy(CleanName, 1, Pos('(', CleanName) - 1));
Result := CleanName;
Break;
end;
end;
finally
SL.Free;
end;
end;
if Result = 'Unknown GPU' then
Result := GetGPUModelFromSysfs;
end;
function GetSysGPUDriver: string;
var
Output, Line, CleanStr: string;
SL: TStringList;
begin
Result := 'Unknown Driver';
if RunCommand('glxinfo', ['-B'], Output) then
begin
SL := TStringList.Create;
try
SL.Text := Output;
for Line in SL do
begin
if Pos('OpenGL core profile version string:', Trim(Line)) = 1 then
begin
CleanStr := Trim(Copy(Trim(Line), Length('OpenGL core profile version string:') + 1, Length(Line)));
if Pos('Mesa', CleanStr) > 0 then
Result := Trim(Copy(CleanStr, Pos('Mesa', CleanStr), Length(CleanStr)))
else if Pos('NVIDIA', CleanStr) > 0 then
Result := Trim(Copy(CleanStr, Pos('NVIDIA', CleanStr), Length(CleanStr)))
else
Result := CleanStr;
Break;
end;
end;
finally
SL.Free;
end;
end;
if Result = 'Unknown Driver' then
Result := GetGPUDriverFromSysfs;
end;
procedure CleanProcessEnvironment(AProcess: TProcess);
var
i: Integer;
EnvVar: string;
begin
i := 1;
while GetEnvironmentString(i) <> '' do begin
EnvVar := GetEnvironmentString(i);
if (Pos('LD_PRELOAD=', EnvVar) <> 1) and
(Pos('MANGOHUD=', EnvVar) <> 1) and
(Pos('MANGOHUD_CONFIGFILE=', EnvVar) <> 1) and
(Pos('ENABLE_VKBASALT=', EnvVar) <> 1) and
(Pos('VKBASALT_CONFIG_FILE=', EnvVar) <> 1) and
(Pos('ENABLE_VKSUMI=', EnvVar) <> 1) and
(Pos('VKSUMI_CONFIG_FILE=', EnvVar) <> 1) then begin
AProcess.Environment.Add(EnvVar);
end;
Inc(i);
end;
end;
function GetSHA256Hash(const AInput: string): string;
var
AProcess: TProcess;
Buffer: array[0..255] of Char;
BytesRead: LongInt;
OutputStr: string;
LoopCount: Integer;
begin
Result := '';
AProcess := TProcess.Create(nil);
try
CleanProcessEnvironment(AProcess);
AProcess.Executable := 'sha256sum';
AProcess.Options := [poUsePipes, poNoConsole];
try
AProcess.Execute;
if Length(AInput) > 0 then
AProcess.Input.Write(AInput[1], Length(AInput));
AProcess.CloseInput;
OutputStr := '';
LoopCount := 0;
while AProcess.Running or (AProcess.Output.NumBytesAvailable > 0) do begin
Inc(LoopCount);
if LoopCount > 200 then begin // 1 second timeout
try
AProcess.Terminate(1);
except
end;
Break;
end;
if AProcess.Output.NumBytesAvailable > 0 then begin
BytesRead := AProcess.Output.Read(Buffer[0], SizeOf(Buffer) - 1);
if BytesRead > 0 then begin
Buffer[BytesRead] := #0;
OutputStr := OutputStr + StrPas(Buffer);
end;
end;
Sleep(5);
end;
OutputStr := Trim(OutputStr);
if Length(OutputStr) >= 64 then
Result := Copy(OutputStr, 1, 64);
except
// ignore
end;
finally
AProcess.Free;
end;
end;
function GetNvidiaUUID: string;
var
AProcess: TProcess;
Buffer: array[0..255] of Char;
BytesRead: LongInt;
OutputStr: string;
LoopCount: Integer;
begin
Result := '';
AProcess := TProcess.Create(nil);
try
CleanProcessEnvironment(AProcess);
AProcess.Executable := 'nvidia-smi';
AProcess.Parameters.Add('--query-gpu=uuid');
AProcess.Parameters.Add('--format=csv,noheader');
AProcess.Options := [poUsePipes, poNoConsole];
try
AProcess.Execute;
AProcess.CloseInput;
OutputStr := '';
LoopCount := 0;
while AProcess.Running or (AProcess.Output.NumBytesAvailable > 0) do begin
Inc(LoopCount);
if LoopCount > 200 then begin // 1 second timeout
try
AProcess.Terminate(1);
except
end;
Break;
end;
if AProcess.Output.NumBytesAvailable > 0 then begin
BytesRead := AProcess.Output.Read(Buffer[0], SizeOf(Buffer) - 1);
if BytesRead > 0 then begin
Buffer[BytesRead] := #0;
OutputStr := OutputStr + StrPas(Buffer);
end;
end;
Sleep(5);
end;
Result := Trim(OutputStr);
except
// ignore
end;
finally
AProcess.Free;
end;
end;
function GetAmdUniqueID: string;
var
SL: TStringList;
FilePath: string;
i: Integer;
begin
Result := '';
for i := 0 to 8 do begin
FilePath := '/sys/class/drm/card' + IntToStr(i) + '/device/unique_id';
if FileExists(FilePath) then begin
SL := TStringList.Create;
try
try
SL.LoadFromFile(FilePath);
if SL.Count > 0 then
Result := Trim(SL[0]);
except
// ignore
end;
finally
SL.Free;
end;
if Result <> '' then Exit;
end;
end;
end;
function GetPersistentUUID: string;
var
ConfigDir, FilePath: string;
SL: TStringList;
Guid: TGUID;
GuidStr: string;
begin
Result := '';
ConfigDir := GetAppConfigDir(False);
FilePath := IncludeTrailingPathDelimiter(ConfigDir) + 'client-id';
// Try reading existing file
if FileExists(FilePath) then begin
SL := TStringList.Create;
try
try
SL.LoadFromFile(FilePath);
if SL.Count > 0 then
Result := Trim(SL[0]);
except
// ignore
end;
finally
SL.Free;
end;
end;
// If empty or not found, generate new one
if Result = '' then begin
if CreateGUID(Guid) = 0 then begin
GuidStr := GUIDToString(Guid);
// Strip out '{' and '}'
if (Length(GuidStr) >= 2) and (GuidStr[1] = '{') and (GuidStr[Length(GuidStr)] = '}') then