]> git.sesse.net Git - vlc/blob - src/misc/darwin_specific.c
libvlc os-specific path discovery
[vlc] / src / misc / darwin_specific.c
1 /*****************************************************************************
2  * darwin_specific.m: Darwin specific features
3  *****************************************************************************
4  * Copyright (C) 2001-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Pierre d'Herbemont <pdherbemont@free.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include "../libvlc.h"
32 #include <dirent.h>                                                /* *dir() */
33 #include <libgen.h>
34 #include <dlfcn.h>
35 #include <CoreFoundation/CoreFoundation.h>
36 #include <mach-o/dyld.h>
37
38 #ifdef HAVE_LOCALE_H
39 #   include <locale.h>
40 #endif
41
42 #ifndef MAXPATHLEN
43 # define MAXPATHLEN 1024
44 #endif
45
46 /*****************************************************************************
47  * system_Init: fill in program path & retrieve language
48  *****************************************************************************/
49 void system_Init( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
50 {
51     VLC_UNUSED(p_this);
52     char i_dummy;
53     char *p_char = NULL;
54     char *p_oldchar = &i_dummy;
55     unsigned int i;
56     (void)pi_argc;
57
58     /* Get the full program path and name */
59     /* First try to see if we are linked to the framework */
60     for (i = 0; i < _dyld_image_count(); i++)
61     {
62         const char * psz_img_name = _dyld_get_image_name(i);
63         /* Check for "VLCKit.framework/Versions/Current/VLCKit",
64          * as well as "VLCKit.framework/Versions/A/VLCKit" and
65          * "VLC.framework/Versions/B/VLCKit" */
66         if( (p_char = strstr( psz_img_name, "VLCKit.framework/Versions/" )) )
67         {
68             /* Look for the next forward slash */
69             p_char += 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
70             while( *p_char != '\0' && *p_char != '/')
71                 p_char++;
72             
73             /* If the string ends with VLC then we've found a winner */
74             if ( !strcmp( p_char, "/VLCKit" ) )
75             {
76                 p_char = strdup( psz_img_name );
77                 break;
78             }
79             else
80                 p_char = NULL;
81         }
82     }
83     if ( !p_char )
84     {
85         /* We are not linked to the VLC.framework, let's use dladdr to figure
86          * libvlc path */
87         Dl_info info;
88         if( dladdr(system_Init, &info) )
89             p_char = strdup(dirname( info.dli_fname ));
90     }
91     if( !p_char )
92     {
93         char path[MAXPATHLEN+1];
94         uint32_t path_len = MAXPATHLEN;
95         if ( !_NSGetExecutablePath(path, &path_len) )
96             p_char = strdup(path);
97     }
98     if( !p_char )
99     {
100         /* We are not linked to the VLC.framework, return the executable path */
101         p_char = strdup( ppsz_argv[ 0 ] );
102     }
103
104     free(psz_vlcpath);
105     psz_vlcpath = p_char;
106
107     /* Remove trailing program name */
108     for( ; *p_char ; )
109     {
110         if( *p_char == '/' )
111         {
112             *p_oldchar = '/';
113             *p_char = '\0';
114             p_oldchar = p_char;
115         }
116         p_char++;
117     }
118
119 #ifdef ENABLE_NLS
120     /* Check if $LANG is set. */
121     if( NULL == getenv("LANG") )
122     {
123         /*
124            Retrieve the preferred language as chosen in  System Preferences.app
125            (note that CFLocaleCopyCurrent() is not used because it returns the
126             preferred locale not language)
127         */
128         CFArrayRef all_locales, preferred_locales;
129         char psz_locale[50];
130
131         all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
132
133         preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
134
135         if ( preferred_locales )
136         {
137             if ( CFArrayGetCount( preferred_locales ) )
138             {
139                 CFStringRef user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
140                 CFStringGetCString( user_language_string_ref, psz_locale, sizeof(psz_locale), kCFStringEncodingUTF8 );
141                 setenv( "LANG", psz_locale, 1 );
142             }
143             CFRelease( preferred_locales );
144         }
145         CFRelease( all_locales );
146     }
147 #endif
148 }
149
150 /*****************************************************************************
151  * system_Configure: check for system specific configuration options.
152  *****************************************************************************/
153 void system_Configure( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
154 {
155     (void)p_this;
156     (void)pi_argc;
157     (void)ppsz_argv;
158 }
159
160 /*****************************************************************************
161  * system_End: free the program path.
162  *****************************************************************************/
163 void system_End( libvlc_int_t *p_this )
164 {
165     (void)p_this;
166     free( psz_vlcpath );
167     psz_vlcpath = NULL;
168 }
169