view get_path.c @ 27148:858c01b81117

r26502: Document rgbtest arguments r26057: Fix copy&paste typo in rgbtest documentation r26198: Grayscale encoding/decoding with FFmpeg is no longer enabled, remove references r26221: Try to fix the description of what mbcmp influences, please fix if I misunderstood the code. r26231: better syntax for A key r26232: added missing escapes r26260: Experimental support for -framedrop with -correct-pts. r26271: Mention that '-frames 0' is useful with -identify, closes bug #1046. r26273: add "ipod" to the list of formats handled by lavf r26297: compacted new libavformat's 'ipod' description r26402: Enable runtime control for colorful and/or module name output r26427: Restore grayscale decoding support with FFmpeg. r26449: 10L, forgot to commit the documentation for the -noconfig options. r26460: restore options alphabetical order r26650: Update documentation for the gl2 driver to make clear gl is usually preferred. r26674: add h264 to list of supported codecs r26732: Mark new options Michael committed as undocumented. r26739: Oops, remove stray .TP. r26749: -psprobe can be used in mpeg-pes streams, too r26762: Add a new suboption to -vo xv and -vo xvmc that allows selection r26763: Remove '(pass 1/2)' from some lavcopts. These options really worked on r26795: Add support for AppleIR Remote as an input under Linux systems. r26798: Document the -noar command-line option in en/fr manpages. r26806: Document x264's AQ options r26853: Update gl vo section with the new force-pbo suboption. r26909: Add a slave command to stop stream playback. r26979: small spelling/wording fixes r26986: Document VIDIXIVTVALPHA environment variable. r26997: Fix codec-specific options syntax declaration to be less confusing and wrong. r27057: Ability for specifying TV standard individually for each TV channel. r27132: Fix/restore the description of the rectangle video filter. previously applied: r27169: add missing escapes and full stops for scaletempo filter r27179: remove two trailing whitespaces
author kraymer
date Mon, 30 Jun 2008 19:35:45 +0000
parents 5ce6348b1a6e
children 3a1a148a9633
line wrap: on
line source

/*
 * Get path to config dir/file.
 *
 * Return Values:
 *   Returns the pointer to the ALLOCATED buffer containing the
 *   zero terminated path string. This buffer has to be FREED
 *   by the caller.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mp_msg.h"

#ifdef MACOSX_BUNDLE
#include <CoreFoundation/CoreFoundation.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

#ifdef WIN32
#include <windows.h>
#endif

#ifdef __OS2__
#define INCL_DOS
#include <os2.h>
#endif

char *get_path(const char *filename){
	char *homedir;
	char *buff;
#ifdef __MINGW32__
	static char *config_dir = "/mplayer";
#else
	static char *config_dir = "/.mplayer";
#endif
	int len;
#ifdef MACOSX_BUNDLE
	struct stat dummy;
	CFIndex maxlen=256;
	CFURLRef res_url_ref=NULL;
	CFURLRef bdl_url_ref=NULL;
	char *res_url_path = NULL;
	char *bdl_url_path = NULL;
#endif

	if ((homedir = getenv("MPLAYER_HOME")) != NULL)
		config_dir = "";
	else if ((homedir = getenv("HOME")) == NULL)
#if defined(__MINGW32__) || defined(__CYGWIN__)
	/* Hack to get fonts etc. loaded outside of Cygwin environment. */
	{
		int i,imax=0;
		char exedir[260];
		GetModuleFileNameA(NULL, exedir, 260);
		for (i=0; i< strlen(exedir); i++)
			if (exedir[i] =='\\')
				{exedir[i]='/'; imax=i;}
		exedir[imax]='\0';
		homedir = exedir;
	}
#elif defined(__OS2__)
    {
        PPIB ppib;
        char path[260];

        // Get process info blocks
        DosGetInfoBlocks(NULL, &ppib);

        // Get full path of the executable
        DosQueryModuleName(ppib->pib_hmte, sizeof( path ), path);

        // Truncate name part including last backslash
        *strrchr(path, '\\') = 0;

        // Convert backslash to slash
        _fnslashify(path);

        homedir = path;
    }
#else
	return NULL;
#endif
	len = strlen(homedir) + strlen(config_dir) + 1;
	if (filename == NULL) {
		if ((buff = malloc(len)) == NULL)
			return NULL;
		sprintf(buff, "%s%s", homedir, config_dir);
	} else {
		len += strlen(filename) + 1;
		if ((buff = malloc(len)) == NULL)
			return NULL;
		sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
	}

#ifdef MACOSX_BUNDLE
	if (stat(buff, &dummy)) {

		res_url_ref=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
		bdl_url_ref=CFBundleCopyBundleURL(CFBundleGetMainBundle());

		if (res_url_ref&&bdl_url_ref) {

			res_url_path=malloc(maxlen);
			bdl_url_path=malloc(maxlen);

			while (!CFURLGetFileSystemRepresentation(res_url_ref, true, res_url_path, maxlen)) {
				maxlen*=2;
				res_url_path=realloc(res_url_path, maxlen);
			}
			CFRelease(res_url_ref);

			while (!CFURLGetFileSystemRepresentation(bdl_url_ref, true, bdl_url_path, maxlen)) {
				maxlen*=2;
				bdl_url_path=realloc(bdl_url_path, maxlen);
			}
			CFRelease(bdl_url_ref);

			if (strcmp(res_url_path, bdl_url_path) == 0)
				res_url_path = NULL;
		}

		if (res_url_path&&filename) {
			if ((strlen(filename)+strlen(res_url_path)+2)>maxlen) {
				maxlen=strlen(filename)+strlen(res_url_path)+2;
			}
			free(buff);
			buff = malloc(maxlen);
			strcpy(buff, res_url_path);

			strcat(buff,"/");
			strcat(buff, filename);
		}
	}
#endif
	mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
	return buff;
}

#if defined(WIN32) && defined(USE_WIN32DLL)
void set_path_env()
{
	/*make our codec dirs available for LoadLibraryA()*/
	char tmppath[MAX_PATH*2 + 1];
	char win32path[MAX_PATH];
	char realpath[MAX_PATH];
#ifdef __CYGWIN__
	cygwin_conv_to_full_win32_path(WIN32_PATH,win32path);
	strcpy(tmppath,win32path);
#ifdef USE_REALCODECS
	cygwin_conv_to_full_win32_path(REALCODEC_PATH,realpath);
	sprintf(tmppath,"%s;%s",win32path,realpath);
#endif /*USE_REALCODECS*/
#else /*__CYGWIN__*/
	/* Expand to absolute path unless it's already absolute */
	if (!strstr(WIN32_PATH,":") && WIN32_PATH[0] != '\\'){
		GetModuleFileNameA(NULL, win32path, MAX_PATH);
		strcpy(strrchr(win32path, '\\') + 1, WIN32_PATH);
	}
	else strcpy(win32path,WIN32_PATH);
	strcpy(tmppath,win32path);
#ifdef USE_REALCODECS
	/* Expand to absolute path unless it's already absolute */
	if (!strstr(REALCODEC_PATH,":") && REALCODEC_PATH[0] != '\\'){
		GetModuleFileNameA(NULL, realpath, MAX_PATH);
		strcpy(strrchr(realpath, '\\') + 1, REALCODEC_PATH);
	}
	else strcpy(realpath,REALCODEC_PATH);
	sprintf(tmppath,"%s;%s",win32path,realpath);
#endif /*USE_REALCODECS*/
#endif /*__CYGWIN__*/
	mp_msg(MSGT_WIN32, MSGL_V,"Setting PATH to %s\n",tmppath);
	if (!SetEnvironmentVariableA("PATH", tmppath))
		mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
}
#endif /*WIN32 && USE_WIN32DLL*/