Add -stop-screensaver flag for Windows
[mplayer/kovensky.git] / get_path.c
blob18cedb18744d99089a20e2988dff99789fde0f92
1 /*
2 * Get path to config dir/file.
4 * Return Values:
5 * Returns the pointer to the ALLOCATED buffer containing the
6 * zero terminated path string. This buffer has to be FREED
7 * by the caller.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "mp_msg.h"
14 #include "get_path.h"
16 #ifdef CONFIG_MACOSX_BUNDLE
17 #include <CoreFoundation/CoreFoundation.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #elif defined(_WIN32)
22 #define _WIN32_IE 0x0400
23 #include <windows.h>
24 #include <shlobj.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #if defined(__CYGWIN__)
28 #include <sys/cygwin.h>
29 #endif
30 #elif defined(__OS2__)
31 #define INCL_DOS
32 #include <os2.h>
33 #endif
35 char *get_path(const char *filename){
36 char *homedir;
37 char *buff;
38 #ifdef _WIN32
39 static char *config_dir = "/mplayer";
40 #else
41 static char *config_dir = "/.mplayer";
42 #endif
43 int len;
44 #ifdef CONFIG_MACOSX_BUNDLE
45 struct stat dummy;
46 CFIndex maxlen=256;
47 CFURLRef res_url_ref=NULL;
48 CFURLRef bdl_url_ref=NULL;
49 char *res_url_path = NULL;
50 char *bdl_url_path = NULL;
51 #endif
53 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
54 config_dir = "";
55 else if ((homedir = getenv("HOME")) == NULL)
56 #if _WIN32
57 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
59 int i,imax=0;
60 struct _stat statBuffer;
61 char exedir[4096], config_path[4096], appdata[4096];
62 if (SHGetSpecialFolderPathA(NULL, appdata, CSIDL_APPDATA, 1)) {
63 strncpy(exedir, appdata, strlen(appdata));
64 } else {
65 GetModuleFileNameA(NULL, exedir, 4096);
67 int len = strlen(exedir);
68 for (i=0; i< len; i++)
69 if (exedir[i] =='\\')
70 exedir[i]='/';
72 sprintf(config_path, "%s%s", exedir, config_dir);
73 int stat_res;
74 if ((stat_res = stat(config_path, &statBuffer)) < 0 ||
75 !(statBuffer.st_mode & S_IFDIR)) {
76 GetModuleFileNameA(NULL, exedir, 4096);
77 len = strlen(exedir);
78 for (i=0; i< len; i++)
79 if (exedir[i] =='\\')
80 {exedir[i]='/'; imax=i;}
81 exedir[imax]='\0';
84 homedir = exedir;
86 #elif defined(__OS2__)
88 PPIB ppib;
89 char path[260];
91 // Get process info blocks
92 DosGetInfoBlocks(NULL, &ppib);
94 // Get full path of the executable
95 DosQueryModuleName(ppib->pib_hmte, sizeof( path ), path);
97 // Truncate name part including last backslash
98 *strrchr(path, '\\') = 0;
100 // Convert backslash to slash
101 _fnslashify(path);
103 homedir = path;
105 #else
106 return NULL;
107 #endif
108 len = strlen(homedir) + strlen(config_dir) + 1;
109 if (filename == NULL) {
110 if ((buff = malloc(len)) == NULL)
111 return NULL;
112 sprintf(buff, "%s%s", homedir, config_dir);
113 } else {
114 len += strlen(filename) + 1;
115 if ((buff = malloc(len)) == NULL)
116 return NULL;
117 sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
120 #ifdef CONFIG_MACOSX_BUNDLE
121 if (stat(buff, &dummy)) {
123 res_url_ref=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
124 bdl_url_ref=CFBundleCopyBundleURL(CFBundleGetMainBundle());
126 if (res_url_ref&&bdl_url_ref) {
128 res_url_path=malloc(maxlen);
129 bdl_url_path=malloc(maxlen);
131 while (!CFURLGetFileSystemRepresentation(res_url_ref, true, res_url_path, maxlen)) {
132 maxlen*=2;
133 res_url_path=realloc(res_url_path, maxlen);
135 CFRelease(res_url_ref);
137 while (!CFURLGetFileSystemRepresentation(bdl_url_ref, true, bdl_url_path, maxlen)) {
138 maxlen*=2;
139 bdl_url_path=realloc(bdl_url_path, maxlen);
141 CFRelease(bdl_url_ref);
143 if (strcmp(res_url_path, bdl_url_path) == 0)
144 res_url_path = NULL;
147 if (res_url_path&&filename) {
148 if ((strlen(filename)+strlen(res_url_path)+2)>maxlen) {
149 maxlen=strlen(filename)+strlen(res_url_path)+2;
151 free(buff);
152 buff = malloc(maxlen);
153 strcpy(buff, res_url_path);
155 strcat(buff,"/");
156 strcat(buff, filename);
159 #endif
160 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
161 return buff;
164 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
165 void set_path_env(void)
167 /*make our codec dirs available for LoadLibraryA()*/
168 char tmppath[MAX_PATH*2 + 1];
169 char win32path[MAX_PATH];
170 char realpath[MAX_PATH];
171 #ifdef __CYGWIN__
172 cygwin_conv_to_full_win32_path(WIN32_PATH,win32path);
173 strcpy(tmppath,win32path);
174 #ifdef CONFIG_REALCODECS
175 cygwin_conv_to_full_win32_path(REALCODEC_PATH,realpath);
176 sprintf(tmppath,"%s;%s",win32path,realpath);
177 #endif /*CONFIG_REALCODECS*/
178 #else /*__CYGWIN__*/
179 /* Expand to absolute path unless it's already absolute */
180 if (!strstr(WIN32_PATH,":") && WIN32_PATH[0] != '\\'){
181 GetModuleFileNameA(NULL, win32path, MAX_PATH);
182 strcpy(strrchr(win32path, '\\') + 1, WIN32_PATH);
184 else strcpy(win32path,WIN32_PATH);
185 strcpy(tmppath,win32path);
186 #ifdef CONFIG_REALCODECS
187 /* Expand to absolute path unless it's already absolute */
188 if (!strstr(REALCODEC_PATH,":") && REALCODEC_PATH[0] != '\\'){
189 GetModuleFileNameA(NULL, realpath, MAX_PATH);
190 strcpy(strrchr(realpath, '\\') + 1, REALCODEC_PATH);
192 else strcpy(realpath,REALCODEC_PATH);
193 sprintf(tmppath,"%s;%s",win32path,realpath);
194 #endif /*CONFIG_REALCODECS*/
195 #endif /*__CYGWIN__*/
196 mp_msg(MSGT_WIN32, MSGL_V,"Setting PATH to %s\n",tmppath);
197 if (!SetEnvironmentVariableA("PATH", tmppath))
198 mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
200 #endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */