/
os_config_init.c
2700 lines (2404 loc) · 89.4 KB
/
os_config_init.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
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
/*
* GPAC - Multimedia Framework C SDK
*
* Authors: Jean Le Feuvre
* Copyright (c) Telecom ParisTech 2000-2023
* All rights reserved
*
* This file is part of GPAC / common tools sub-project
*
* GPAC is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GPAC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <gpac/config_file.h>
#if defined(WIN32) || defined(_WIN32_WCE)
#include <windows.h> /*for GetModuleFileName*/
#ifndef _WIN32_WCE
#include <direct.h> /*for _mkdir*/
#include <gpac/utf.h>
#include <shlobj.h> /*for getting user-dir*/
#ifndef SHGFP_TYPE_CURRENT
#define SHGFP_TYPE_CURRENT 0 /*needed for MinGW*/
#endif
#endif
#define CFG_FILE_NAME "GPAC.cfg"
#define TEST_MODULE "gm_"
#elif (defined(__DARWIN__) || defined(__APPLE__) )
#include <mach-o/dyld.h> /*for _NSGetExecutablePath */
#ifdef GPAC_CONFIG_IOS
#define TEST_MODULE "gpac4ios"
#else
#define TEST_MODULE "gm_"
#endif
#define CFG_FILE_NAME "GPAC.cfg"
#else
#ifdef GPAC_CONFIG_LINUX
#include <unistd.h>
#endif
#define CFG_FILE_NAME "GPAC.cfg"
#if defined(GPAC_CONFIG_WIN32)
#define TEST_MODULE "gm_"
#else
#define TEST_MODULE "gm_"
#endif
#endif
#if !defined(GPAC_STATIC_MODULES) && !defined(GPAC_MP4BOX_MINI)
static Bool mod_enum(void *cbck, char *item_name, char *item_path, GF_FileEnumInfo *file_info)
{
if (!strncmp(item_name, "gm_", 3) || !strncmp(item_name, "gf_", 3)) {
*(Bool*)cbck = GF_TRUE;
return GF_TRUE;
}
return GF_FALSE;
}
#endif
static Bool check_file_exists(char *name, char *path, char *outPath)
{
char szPath[GF_MAX_PATH];
FILE *f;
int concatres;
if (! gf_dir_exists(path)) return 0;
if (!strcmp(name, TEST_MODULE)) {
Bool res = GF_FALSE;
#if defined(GPAC_STATIC_MODULES) || defined(GPAC_MP4BOX_MINI)
res = GF_TRUE;
#else
gf_enum_directory(path, GF_FALSE, mod_enum, &res, NULL);
#endif
if (!res) return GF_FALSE;
if (outPath != path) strcpy(outPath, path);
return 1;
}
concatres = snprintf(szPath, GF_MAX_PATH, "%s%c%s", path, GF_PATH_SEPARATOR, name);
if (concatres<0) {
GF_LOG(GF_LOG_WARNING, GF_LOG_CORE, ("Path too long (limit %d) when trying to concatenate %s and %s\n", GF_MAX_PATH, path, name));
}
//do not use gf_fopen here, we don't want to throw en error if failure
f = fopen(szPath, "rb");
if (!f) return GF_FALSE;
fclose(f);
if (outPath != path) strcpy(outPath, path);
return GF_TRUE;
}
enum
{
GF_PATH_APP,
GF_PATH_CFG,
//were we store gui/%, shaders/*, scripts/*
GF_PATH_SHARE,
GF_PATH_MODULES,
GF_PATH_LIB
};
#if defined(WIN32) || defined(_WIN32_WCE)
static Bool get_default_install_path(char *file_path, u32 path_type)
{
FILE *f;
char szPath[GF_MAX_PATH];
#ifdef _WIN32_WCE
TCHAR w_szPath[GF_MAX_PATH];
GetModuleFileName(NULL, w_szPath, GF_MAX_PATH);
CE_WideToChar((u16 *) w_szPath, file_path);
#else
wchar_t wtmp_file_path[GF_MAX_PATH];
char* tmp_file_path;
GetModuleFileNameA(NULL, file_path, GF_MAX_PATH);
#endif
/*remove exe name*/
if (strstr(file_path, ".exe")) {
char *sep = strrchr(file_path, '\\');
if (sep) sep[0] = 0;
}
strcpy(szPath, file_path);
strlwr(szPath);
/*if this is run from a browser, we do not get our app path - fortunately on Windows, we always use 'GPAC' in the
installation path*/
if (!strstr(file_path, "gpac") && !strstr(file_path, "GPAC") ) {
HKEY hKey = NULL;
DWORD dwSize = GF_MAX_PATH;
file_path[0] = 0;
/*locate the key in current user, then in local machine*/
#ifdef _WIN32_WCE
DWORD dwType = REG_SZ;
u16 w_path[1024];
RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\GPAC"), 0, KEY_READ, &hKey);
#ifdef _DEBUG
if (RegQueryValueEx(hKey, TEXT("DebugDir"), 0, &dwType, (LPBYTE) w_path, &dwSize) != ERROR_SUCCESS)
#endif
RegQueryValueEx(hKey, TEXT("InstallDir"), 0, &dwType, (LPBYTE) w_path, &dwSize);
CE_WideToChar(w_path, (char *)file_path);
RegCloseKey(hKey);
#else
if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\GPAC", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\GPAC", 0, KEY_READ, &hKey);
dwSize = GF_MAX_PATH;
#ifdef _DEBUG
if (RegQueryValueEx(hKey, "DebugDir", NULL, NULL,(unsigned char*) file_path, &dwSize) != ERROR_SUCCESS)
#endif
RegQueryValueEx(hKey, "InstallDir", NULL, NULL,(unsigned char*) file_path, &dwSize);
RegCloseKey(hKey);
#endif
}
//empty path, try DLL
if (!file_path[0] && (path_type != GF_PATH_LIB)) {
get_default_install_path(file_path, GF_PATH_LIB);
}
if (path_type==GF_PATH_APP) return GF_TRUE;
if (path_type==GF_PATH_SHARE) {
char *sep;
strcat(file_path, "\\share");
if (check_file_exists("gui\\gui.bt", file_path, file_path)) return GF_TRUE;
sep = strstr(file_path, "\\bin\\");
if (sep) {
sep[0] = 0;
strcat(file_path, "\\share");
if (check_file_exists("gui\\gui.bt", file_path, file_path)) return GF_TRUE;
}
return GF_FALSE;
}
/*modules are stored in the GPAC directory (should be changed to GPAC/modules)*/
if (path_type==GF_PATH_MODULES) return GF_TRUE;
if (path_type == GF_PATH_LIB) {
HMODULE hm=NULL;
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)&get_default_install_path, &hm) == 0) {
return 0;
}
if (GetModuleFileName(hm, file_path, GF_MAX_PATH) == 0) {
return 0;
}
char *sep = strrchr(file_path, '\\');
if (!sep) sep = strrchr(file_path, '/');
if (sep) sep[0] = 0;
return 1;
}
/*we are looking for the config file path - make sure it is writable*/
gf_assert(path_type == GF_PATH_CFG);
strcpy(szPath, file_path);
strcat(szPath, "\\gpaccfgtest.txt");
//do not use gf_fopen here, we don't want to through any error if failure
f = fopen(szPath, "wb");
if (f != NULL) {
fclose(f);
gf_file_delete(szPath);
return GF_TRUE;
}
#ifdef _WIN32_WCE
return 0;
#else
/*no write access, get user home directory*/
SHGetSpecialFolderPathW(NULL, wtmp_file_path, CSIDL_APPDATA, 1);
tmp_file_path = gf_wcs_to_utf8(wtmp_file_path);
strncpy(file_path, tmp_file_path, GF_MAX_PATH);
file_path[GF_MAX_PATH-1] = 0;
gf_free(tmp_file_path);
if (file_path[strlen(file_path)-1] != '\\') strcat(file_path, "\\");
strcat(file_path, "GPAC");
/*create GPAC dir*/
gf_mkdir(file_path);
strcpy(szPath, file_path);
strcat(szPath, "\\gpaccfgtest.txt");
f = gf_fopen(szPath, "wb");
/*COMPLETE FAILURE*/
if (!f) return GF_FALSE;
gf_fclose(f);
gf_file_delete(szPath);
return GF_TRUE;
#endif
}
#elif defined(GPAC_CONFIG_ANDROID)
static char android_app_data[512];
static char android_external_storage[512];
/*
Called by GPAC JNI wrappers. If not set, default to app_data=/data/data/io.gpac.gpac and ext_storage=/sdcard
*/
GF_EXPORT
void gf_sys_set_android_paths(const char *app_data, const char *ext_storage)
{
if (app_data && (strlen(app_data)<512)) {
strcpy(android_app_data, app_data);
}
if (ext_storage && (strlen(ext_storage)<512)) {
strcpy(android_external_storage, ext_storage);
}
}
static Bool get_default_install_path(char *file_path, u32 path_type)
{
if (!file_path) return 0;
if (path_type==GF_PATH_APP) {
strcpy(file_path, android_app_data[0] ? android_app_data : "/data/data/io.gpac.gpac");
return 1;
} else if (path_type==GF_PATH_CFG) {
const char *res = android_external_storage[0] ? android_external_storage : getenv("EXTERNAL_STORAGE");
if (!res) res = "/sdcard";
strcpy(file_path, res);
strcat(file_path, "/GPAC");
//GPAC folder exists in external storage, use profile from this location
if (gf_dir_exists(file_path)) {
return 1;
}
//otherwise use profile in app data store
strcpy(file_path, android_app_data[0] ? android_app_data : "/data/data/io.gpac.gpac");
strcat(file_path, "/GPAC");
return 1;
} else if (path_type==GF_PATH_SHARE) {
if (!get_default_install_path(file_path, GF_PATH_APP))
return 0;
strcat(file_path, "/share");
return 1;
} else if (path_type==GF_PATH_MODULES) {
if (!get_default_install_path(file_path, GF_PATH_APP))
return 0;
strcat(file_path, "/lib");
return 1;
}
return 0;
}
#elif defined(__SYMBIAN__)
#if defined(__SERIES60_3X__)
#define SYMBIAN_GPAC_CFG_DIR "\\private\\F01F9075"
#define SYMBIAN_GPAC_GUI_DIR "\\private\\F01F9075\\gui"
#define SYMBIAN_GPAC_MODULES_DIR "\\sys\\bin"
#else
#define SYMBIAN_GPAC_CFG_DIR "\\system\\apps\\GPAC"
#define SYMBIAN_GPAC_GUI_DIR "\\system\\apps\\GPAC\\gui"
#define SYMBIAN_GPAC_MODULES_DIR GPAC_CFG_DIR
#endif
static Bool get_default_install_path(char *file_path, u32 path_type)
{
if (path_type==GF_PATH_APP) strcpy(file_path, SYMBIAN_GPAC_MODULES_DIR);
else if (path_type==GF_PATH_CFG) strcpy(file_path, SYMBIAN_GPAC_CFG_DIR);
else if (path_type==GF_PATH_GUI) strcpy(file_path, SYMBIAN_GPAC_GUI_DIR);
else if (path_type==GF_PATH_MODULES) strcpy(file_path, SYMBIAN_GPAC_MODULES_DIR);
return 1;
}
/*Linux, OSX, iOS*/
#else
//dlinfo
#if defined(__DARWIN__) || defined(__APPLE__)
#include <dlfcn.h>
typedef Dl_info _Dl_info;
#elif defined(GPAC_CONFIG_LINUX)
typedef struct
{
const char *dli_fname;
void *dli_fbase;
const char *dli_sname;
void *dli_saddr;
} _Dl_info;
int dladdr(void *, _Dl_info *);
#endif
static Bool get_default_install_path(char *file_path, u32 path_type)
{
char app_path[GF_MAX_PATH];
char *sep;
#if (defined(__DARWIN__) || defined(__APPLE__) || defined(GPAC_CONFIG_LINUX))
u32 size;
#endif
/*on OSX, Linux & co, user home is where we store the cfg file*/
if (path_type==GF_PATH_CFG) {
#ifdef GPAC_CONFIG_EMSCRIPTEN
if (gf_dir_exists("/idbfs")) {
if (!gf_dir_exists("/idbfs/.gpac")) {
gf_mkdir("/idbfs/.gpac");
}
strcpy(file_path, "/idbfs/.gpac");
return 1;
}
#endif
char *user_home = getenv("HOME");
#ifdef GPAC_CONFIG_IOS
char buf[PATH_MAX];
char *res;
#endif
if (!user_home) {
GF_LOG(GF_LOG_INFO, GF_LOG_CORE, ("Couldn't find HOME directory\n"));
return 0;
}
#ifdef GPAC_CONFIG_IOS
res = realpath(user_home, buf);
if (res) {
strcpy(file_path, buf);
strcat(file_path, "/Documents");
} else
#endif
strcpy(file_path, user_home);
if (file_path[strlen(file_path)-1] == '/') file_path[strlen(file_path)-1] = 0;
//cleanup of old install in .gpacrc
if (check_file_exists(".gpacrc", file_path, file_path)) {
strcpy(app_path, file_path);
strcat(app_path, "/.gpacrc");
gf_file_delete(app_path);
}
strcat(file_path, "/.gpac");
if (!gf_dir_exists(file_path)) {
gf_mkdir(file_path);
}
return 1;
}
if (path_type==GF_PATH_APP) {
#if (defined(__DARWIN__) || defined(__APPLE__) )
size = GF_MAX_PATH-1;
if (_NSGetExecutablePath(app_path, &size) ==0) {
realpath(app_path, file_path);
sep = strrchr(file_path, '/');
if (sep) sep[0] = 0;
return 1;
}
#elif defined(GPAC_CONFIG_LINUX)
size = readlink("/proc/self/exe", file_path, GF_MAX_PATH-1);
if (size>0) {
file_path[size] = 0;
sep = strrchr(file_path, '/');
if (sep) sep[0] = 0;
return 1;
}
#elif defined(GPAC_CONFIG_WIN32)
GetModuleFileNameA(NULL, file_path, GF_MAX_PATH);
if (strstr(file_path, ".exe")) {
sep = strrchr(file_path, '\\');
if (sep) sep[0] = 0;
if ((file_path[1]==':') && (file_path[2]=='\\')) {
strcpy(file_path, &file_path[2]);
}
sep = file_path;
while ( sep[0] ) {
if (sep[0]=='\\') sep[0]='/';
sep++;
}
//get rid of /mingw32 or /mingw64
sep = strstr(file_path, "/usr/");
if (sep) {
strcpy(file_path, sep);
}
return 1;
}
#elif defined(GPAC_CONFIG_EMSCRIPTEN)
return 0;
#endif
GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("Unknown arch, cannot find executable path\n"));
return 0;
}
if (path_type==GF_PATH_LIB) {
#if defined(__DARWIN__) || defined(__APPLE__) || defined(GPAC_CONFIG_LINUX)
_Dl_info dl_info;
dl_info.dli_fname = NULL;
if (dladdr((void *)get_default_install_path, &dl_info)
&& dl_info.dli_fname
) {
strcpy(file_path, dl_info.dli_fname);
sep = strrchr(file_path, '/');
if (sep) sep[0] = 0;
return 1;
}
return 0;
#endif
//for emscripten we use a static load for now
#if !defined(GPAC_CONFIG_EMSCRIPTEN)
GF_LOG(GF_LOG_DEBUG, GF_LOG_CORE, ("Unknown arch, cannot find library path\n"));
#endif
return 0;
}
/*check system install:
- share is PREFIX/share/gpac/
- mod_dir is PREFIX/LIBDIR/gpac/
*/
if ((path_type==GF_PATH_SHARE) || (path_type==GF_PATH_MODULES)) {
char lib_arch_scheme[100];
lib_arch_scheme[0] = 0;
#if defined(GPAC_CONFIG_EMSCRIPTEN)
strcpy(app_path, "/usr/");
#else
/*locate libgpac path first in case we are not run using gpac apps (eg python bindings)
if failure (static build) locate app
*/
if (!get_default_install_path(app_path, GF_PATH_LIB)) {
if (!get_default_install_path(app_path, GF_PATH_APP)) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("Couldn't find GPAC binaries install directory\n"));
return 0;
}
}
#endif
//check if we are a lib path and have a .gpac/ here
if ((path_type==GF_PATH_MODULES) && strstr(app_path, "/lib")) {
strcat(app_path, "/gpac");
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
char *sep = strrchr(app_path, '/');
if (sep) sep[0]=0;
}
//extract PREFIX
//app_path is either PREFIX/lib or PREFIX/lib/arch-specific (shared lib) or PREFIX/bin (if app)
//without trailing slash
char *root = strrchr(app_path, '/');
if (root) {
strncpy(lib_arch_scheme, root+1, 99);
lib_arch_scheme[99]=0;
root[0]=0;
}
//check if we have a /lib/arch-specific scheme
char *lib = strrchr(app_path, '/');
if (lib && !strncmp(lib, "/lib/", 4)) {
root = lib;
root[0] = 0;
} else {
lib_arch_scheme[0]=0;
}
if ((path_type==GF_PATH_SHARE) && root) {
strcat(app_path, "/share/gpac");
if (check_file_exists("gui/gui.bt", app_path, file_path)) return 1;
/*failsafe - look in possible install dirs ...*/
if (check_file_exists("gui/gui.bt", "/usr/share/gpac", file_path)) return 1;
if (check_file_exists("gui/gui.bt", "/usr/local/share/gpac", file_path)) return 1;
if (check_file_exists("gui/gui.bt", "/opt/share/gpac", file_path)) return 1;
if (check_file_exists("gui/gui.bt", "/opt/local/share/gpac", file_path)) return 1;
} else if ((path_type==GF_PATH_MODULES) && root) {
//if arch-specific scheme try it first (to avoid loading 64bit versions for 32bit shared...)
if (lib_arch_scheme[0]) {
strcat(app_path, "/");
strcat(app_path, lib_arch_scheme);
strcat(app_path, "/gpac");
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
root[0]=0;
}
strcat(app_path, "/lib64/gpac");
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
root[0]=0;
strcat(app_path, "/lib/gpac");
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
root[0]=0;
strcat(app_path, "/lib/x86_64-linux-gnu/gpac");
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
root[0]=0;
strcat(app_path, "/lib/i386-linux-gnu/gpac");
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
/*failsafe - look in possible install dirs ...*/
if (check_file_exists(TEST_MODULE, "/usr/lib64/gpac", file_path)) return 1;
if (check_file_exists(TEST_MODULE, "/usr/lib/gpac", file_path)) return 1;
if (check_file_exists(TEST_MODULE, "/usr/local/lib/gpac", file_path)) return 1;
if (check_file_exists(TEST_MODULE, "/opt/lib/gpac", file_path)) return 1;
if (check_file_exists(TEST_MODULE, "/opt/local/lib/gpac", file_path)) return 1;
if (check_file_exists(TEST_MODULE, "/usr/lib/x86_64-linux-gnu/gpac", file_path)) return 1;
if (check_file_exists(TEST_MODULE, "/usr/lib/i386-linux-gnu/gpac", file_path)) return 1;
}
}
if (path_type==GF_PATH_SHARE) {
Bool try_lib=GF_TRUE;
if (get_default_install_path(app_path, GF_PATH_CFG)) {
sep = strstr(app_path, ".gpac/");
if (sep) sep[5]=0;
/*GUI not found, look in ~/.gpac/share/gui/ */
strcat(app_path, "/share");
if (check_file_exists("gui/gui.bt", app_path, file_path)) return 1;
}
/*GUI not found, look in gpac distribution if any */
if (get_default_install_path(app_path, GF_PATH_APP)) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_CORE, ("[core] trying to locate share from application dir %s\n", app_path));
strcat(app_path, "/");
retry_lib:
sep = strstr(app_path, "/bin/");
if (sep) {
sep[0] = 0;
strcat(app_path, "/share");
if (check_file_exists("gui/gui.bt", app_path, file_path)) return 1;
strcat(app_path, "/gpac");
if (check_file_exists("gui/gui.bt", app_path, file_path)) return 1;
}
sep = strstr(app_path, "/build/");
if (sep) {
sep[0] = 0;
strcat(app_path, "/share");
if (check_file_exists("gui/gui.bt", app_path, file_path)) return 1;
}
}
if (get_default_install_path(app_path, GF_PATH_LIB)) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_CORE, ("[core] trying to locate share from dynamic libgpac dir %s\n", app_path));
sep = strstr(app_path, "/lib");
if (sep) {
sep[0] = 0;
strcat(app_path, "/share");
if (check_file_exists("gui/gui.bt", app_path, file_path)) return 1;
}
if (try_lib) {
try_lib = GF_FALSE;
goto retry_lib;
}
}
/*GUI not found, look in .app for OSX case*/
}
if (path_type==GF_PATH_MODULES) {
/*look in gpac compilation tree (modules are output in the same folder as apps) and in distrib tree */
if (get_default_install_path(app_path, GF_PATH_APP)) {
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
/*on OSX check modules subdirectory */
strcat(app_path, "/modules");
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
get_default_install_path(app_path, GF_PATH_APP);
strcat(app_path, "/");
sep = strstr(app_path, "/bin/");
if (sep) {
sep[0] = 0;
strcat(app_path, "/lib/gpac");
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
}
/*modules not found*/
GF_LOG(GF_LOG_DEBUG, GF_LOG_CORE, ("Couldn't find any modules in standard path (app path %s)\n", app_path));
}
/*look in lib install */
if (get_default_install_path(app_path, GF_PATH_LIB)) {
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
strcat(app_path, "/gpac");
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("Couldn't find any modules in lib path %s\n", app_path));
}
/*modules not found, look in ~/.gpac/modules/ */
if (get_default_install_path(app_path, GF_PATH_CFG)) {
strcat(app_path, "/modules");
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
}
/*modules not found, failure*/
#ifndef GPAC_STATIC_MODULES
GF_LOG(GF_LOG_WARNING, GF_LOG_CORE, ("Couldn't find any modules in HOME path (app path %s)\n", app_path));
#endif
return 0;
}
/*OSX way vs iPhone*/
get_default_install_path(app_path, GF_PATH_APP);
sep = strstr(app_path, ".app/");
if (sep) sep[4] = 0;
/*we are looking for .app install path, or GUI */
if (path_type==GF_PATH_SHARE) {
#ifndef GPAC_CONFIG_IOS
strcat(app_path, "/Contents/MacOS/share");
if (check_file_exists("gui/gui.bt", app_path, file_path)) return 1;
#else /*iOS: for now, everything is set flat within the package*/
/*iOS app is distributed with embedded GUI*/
get_default_install_path(app_path, GF_PATH_APP);
if (check_file_exists("gui/gui.bt", app_path, file_path)) return 1;
strcat(app_path, "/share");
if (check_file_exists("gui/gui.bt", app_path, file_path)) return 1;
#endif
}
else { // (path_type==GF_PATH_MODULES)
strcat(app_path, "/Contents/MacOS/modules");
if (check_file_exists(TEST_MODULE, app_path, file_path)) return 1;
}
/*not found ...*/
return 0;
}
#endif
//get real path where the .gpac dir has been created, and use this as the default path
//for cache (tmp/ dir of ios app) and last working fir
#ifdef GPAC_CONFIG_IOS
static void gf_ios_refresh_cache_directory( GF_Config *cfg, const char *file_path)
{
char *cache_dir, *old_cache_dir;
char buf[GF_MAX_PATH], *res, *sep;
res = realpath(file_path, buf);
if (!res) return;
sep = strstr(res, ".gpac");
gf_assert(sep);
sep[0] = 0;
gf_cfg_set_key(cfg, "core", "docs-dir", res);
if (!gf_cfg_get_key(cfg, "core", "last-dir"))
gf_cfg_set_key(cfg, "core", "last-dir", res);
strcat(res, "cache/");
cache_dir = res;
old_cache_dir = (char*) gf_opts_get_key("core", "cache");
if (!gf_dir_exists(cache_dir)) {
if (old_cache_dir && strcmp(old_cache_dir, cache_dir)) {
GF_LOG(GF_LOG_WARNING, GF_LOG_CORE, ("Cache dir changed: old %d -> new %s\n\n", old_cache_dir, cache_dir ));
}
gf_mkdir(cache_dir);
}
gf_cfg_set_key(cfg, "core", "cache", cache_dir);
}
#endif
const char * gf_get_default_cache_directory_ex(Bool do_create);
GF_EXPORT
void gf_get_default_font_dir(char szPath[GF_MAX_PATH])
{
#if defined(_WIN32_WCE)
strcpy(szPath, "\\Windows");
#elif defined(WIN32)
GetWindowsDirectory((char*)szPath, MAX_PATH);
if (szPath[strlen((char*)szPath)-1] != '\\') strcat((char*)szPath, "\\");
strcat((char *)szPath, "Fonts");
#elif defined(__APPLE__) && defined(GPAC_CONFIG_IOS)
strcpy(szPath, "/System/Library/Fonts/Cache,/System/Library/Fonts/AppFonts,/System/Library/Fonts/Core,/System/Library/Fonts/Extra");
#elif defined(__APPLE__)
strcpy(szPath, "/System/Library/Fonts,/Library/Fonts");
#elif defined(GPAC_CONFIG_ANDROID)
strcpy(szPath, "/system/fonts/");
#else
//scan all /usr/share/fonts, not just /usr/share/fonts/truetype/ which does not exist in some distrros
strcpy(szPath, "/usr/share/fonts/");
#endif
}
static GF_Config *create_default_config(char *file_path, const char *profile)
{
Bool moddir_found;
GF_Config *cfg;
char szPath[GF_MAX_PATH];
if (file_path && ! get_default_install_path(file_path, GF_PATH_CFG)) {
profile = "0";
}
/*Create temp config file*/
if (profile && (!strcmp(profile, "0") || !stricmp(profile, "n"))) {
cfg = gf_cfg_new(NULL, NULL);
if (!stricmp(profile, "n")) {
gf_cfg_discard_changes(cfg);
return cfg;
}
} else {
FILE *f;
/*create config file from disk*/
if (profile) {
sprintf(szPath, "%s%cprofiles%c%s%c%s", file_path, GF_PATH_SEPARATOR, GF_PATH_SEPARATOR, profile, GF_PATH_SEPARATOR, CFG_FILE_NAME);
} else {
sprintf(szPath, "%s%c%s", file_path, GF_PATH_SEPARATOR, CFG_FILE_NAME);
}
GF_LOG(GF_LOG_INFO, GF_LOG_CORE, ("Trying to create config file: %s\n", szPath ));
f = gf_fopen(szPath, "wt");
if (!f) return NULL;
gf_fclose(f);
cfg = gf_cfg_new(NULL, szPath);
}
if (!cfg) return NULL;
#ifndef GPAC_CONFIG_IOS
moddir_found = get_default_install_path(szPath, GF_PATH_MODULES);
#else
moddir_found = get_default_install_path(szPath, GF_PATH_APP);
#endif
#if defined(GPAC_CONFIG_IOS)
gf_cfg_set_key(cfg, "core", "devclass", "ios");
#elif defined(GPAC_CONFIG_ANDROID)
gf_cfg_set_key(cfg, "core", "devclass", "android");
#elif defined(GPAC_CONFIG_EMSCRIPTEN)
gf_cfg_set_key(cfg, "core", "devclass", "wasm");
#else
gf_cfg_set_key(cfg, "core", "devclass", "desktop");
#endif
if (!moddir_found) {
#if !defined(GPAC_CONFIG_EMSCRIPTEN)
GF_LOG(GF_LOG_WARNING, GF_LOG_CORE, ("[Core] default modules directory not found\n"));
#endif
} else {
gf_cfg_set_key(cfg, "core", "module-dir", szPath);
}
#if defined(GPAC_CONFIG_IOS)
gf_ios_refresh_cache_directory(cfg, file_path);
#elif defined(GPAC_CONFIG_ANDROID)
if (get_default_install_path(szPath, GF_PATH_APP)) {
strcat(szPath, "/cache");
gf_cfg_set_key(cfg, "core", "cache", szPath);
strcat(szPath, "/.nomedia");
//create .nomedia in cache and in app dir
if (!gf_file_exists(szPath)) {
FILE *f = gf_fopen(szPath, "w");
if (f) gf_fclose(f);
}
get_default_install_path(szPath, GF_PATH_APP);
strcat(szPath, "/.nomedia");
if (!gf_file_exists(szPath)) {
FILE *f = gf_fopen(szPath, "w");
if (f) gf_fclose(f);
}
//add a tmp as well, tmpfile() does not work on android
get_default_install_path(szPath, GF_PATH_APP);
strcat(szPath, "/gpac_tmp");
gf_cfg_set_key(cfg, "core", "tmp", szPath);
strcat(szPath, "/.nomedia");
if (!gf_file_exists(szPath)) {
FILE *f = gf_fopen(szPath, "w");
if (f) gf_fclose(f);
}
}
#else
/*get default temporary directoy */
gf_cfg_set_key(cfg, "core", "cache", gf_get_default_cache_directory_ex(GF_FALSE));
#endif
/*Setup font engine to FreeType by default, and locate TrueType font directory on the system*/
gf_cfg_set_key(cfg, "core", "font-reader", "freetype");
gf_cfg_set_key(cfg, "core", "rescan-fonts", "yes");
gf_get_default_font_dir(szPath);
gf_cfg_set_key(cfg, "core", "font-dirs", szPath);
gf_cfg_set_key(cfg, "core", "cache-size", "100M");
#if defined(_WIN32_WCE)
gf_cfg_set_key(cfg, "core", "video-output", "gapi");
#elif defined(WIN32)
gf_cfg_set_key(cfg, "core", "video-output", "directx");
#elif defined(__DARWIN__) || defined(__APPLE__)
gf_cfg_set_key(cfg, "core", "video-output", "sdl");
#elif defined(GPAC_CONFIG_ANDROID)
gf_cfg_set_key(cfg, "core", "video-output", "android");
gf_cfg_set_key(cfg, "core", "audio-output", "android");
#else
//use SDL by default, will fallback to X11 if not found (our X11 wrapper is old and does not have all features of the SDL one)
gf_cfg_set_key(cfg, "core", "video-output", "sdl");
gf_cfg_set_key(cfg, "core", "audio-output", "sdla");
#endif
#if !defined(GPAC_CONFIG_IOS) && !defined(GPAC_CONFIG_ANDROID)
gf_cfg_set_key(cfg, "core", "switch-vres", "no");
gf_cfg_set_key(cfg, "core", "hwvmem", "auto");
#endif
#ifdef GPAC_CONFIG_ANDROID
const char *opt = android_external_storage[0] ? android_external_storage : getenv("EXTERNAL_STORAGE");
if (!opt) opt = "/sdcard";
gf_cfg_set_key(cfg, "core", "docs-dir", opt);
gf_cfg_set_key(cfg, "core", "last-dir", opt);
#endif
/*locate GUI*/
if ( get_default_install_path(szPath, GF_PATH_SHARE) ) {
char gui_path[GF_MAX_PATH+100];
sprintf(gui_path, "%s%cgui%cgui.bt", szPath, GF_PATH_SEPARATOR, GF_PATH_SEPARATOR);
if (gf_file_exists(gui_path)) {
gf_cfg_set_key(cfg, "core", "startup-file", gui_path);
}
/*shaders are at the same location*/
sprintf(gui_path, "%s%cshaders%cvertex.glsl", szPath, GF_PATH_SEPARATOR, GF_PATH_SEPARATOR);
gf_cfg_set_key(cfg, "filter@compositor", "vertshader", gui_path);
sprintf(gui_path, "%s%cshaders%cfragment.glsl", szPath, GF_PATH_SEPARATOR, GF_PATH_SEPARATOR);
gf_cfg_set_key(cfg, "filter@compositor", "fragshader", gui_path);
//aliases and other defaults
sprintf(gui_path, "%s%cdefault.cfg", szPath, GF_PATH_SEPARATOR);
if (gf_file_exists(gui_path)) {
GF_Config *aliases = gf_cfg_new(NULL, gui_path);
if (aliases) {
u32 i, count = gf_cfg_get_section_count(aliases);
for (i=0; i<count; i++) {
u32 j, count2;
const char *sec_name = gf_cfg_get_section_name(aliases, i);
if (!sec_name) continue;
count2 = gf_cfg_get_key_count(aliases, sec_name);
for (j=0; j<count2; j++) {
const char *name = gf_cfg_get_key_name(aliases, sec_name, j);
const char *val = gf_cfg_get_key(aliases, sec_name, name);
gf_cfg_set_key(cfg, sec_name, name, val);
}
}
}
gf_cfg_del(aliases);
}
}
if (profile && !strcmp(profile, "0")) {
GF_Err gf_cfg_set_filename(GF_Config *iniFile, const char * fileName);
// sprintf(szPath, "%s%c%s", gf_get_default_cache_directory(), GF_PATH_SEPARATOR, CFG_FILE_NAME);
gf_cfg_set_filename(cfg, CFG_FILE_NAME);
gf_cfg_discard_changes(cfg);
return cfg;
}
/*store and reload*/
strcpy(szPath, gf_cfg_get_filename(cfg));
gf_cfg_del(cfg);
return gf_cfg_new(NULL, szPath);
}
/*check if modules directory has changed in the config file
*/
static void check_modules_dir(GF_Config *cfg)
{
char path[GF_MAX_PATH];
#ifdef GPAC_CONFIG_IOS
const char *cfg_path;
if ( get_default_install_path(path, GF_PATH_SHARE) ) {
char *sep;
char shader_path[GF_MAX_PATH];
strcat(path, "/gui/gui.bt");
gf_cfg_set_key(cfg, "core", "startup-file", path);
//get rid of "/gui/gui.bt"
sep = strrchr(path, '/');
sep[0] = 0;
sep = strrchr(path, '/');
sep[0] = 0;
sprintf(shader_path, "%s%cshaders%cvertex.glsl", path, GF_PATH_SEPARATOR, GF_PATH_SEPARATOR);
gf_cfg_set_key(cfg, "filter@compositor", "vertshader", shader_path);
sprintf(shader_path, "%s%cshaders%cfragment.glsl", path, GF_PATH_SEPARATOR, GF_PATH_SEPARATOR);
gf_cfg_set_key(cfg, "filter@compositor", "fragshader", shader_path);
}
cfg_path = gf_cfg_get_filename(cfg);
gf_ios_refresh_cache_directory(cfg, cfg_path);
#else
const char *opt;
if ( get_default_install_path(path, GF_PATH_MODULES) ) {
opt = gf_cfg_get_key(cfg, "core", "module-dir");
//for OSX, we can have an install in /usr/... and an install in /Applications/GPAC.app - always change
#if defined(__DARWIN__) || defined(__APPLE__)
if (!opt || strcmp(opt, path))
gf_cfg_set_key(cfg, "core", "module-dir", path);
#else
//otherwise only check we didn't switch between a 64 bit version and a 32 bit version
if (!opt) {
gf_cfg_set_key(cfg, "core", "module-dir", path);
} else {
Bool erase_modules_dir = GF_FALSE;
const char *opt64 = gf_cfg_get_key(cfg, "core", "64bits");
if (!opt64) {
//first run or old versions, erase
erase_modules_dir = GF_TRUE;
} else if (!strcmp(opt64, "yes") ) {
#ifndef GPAC_64_BITS
erase_modules_dir = GF_TRUE;
#endif
} else {
#ifdef GPAC_64_BITS
erase_modules_dir = GF_TRUE;
#endif
}
#ifdef GPAC_64_BITS
opt64 = "yes";
#else
opt64 = "no";
#endif
gf_cfg_set_key(cfg, "core", "64bits", opt64);
if (erase_modules_dir) {
gf_cfg_set_key(cfg, "core", "module-dir", path);
}
}
#endif
}
/*if startup file was disabled, do not attempt to correct it*/
if (gf_cfg_get_key(cfg, "core", "startup-file")==NULL) return;
if ( get_default_install_path(path, GF_PATH_SHARE) ) {