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