]> git.sesse.net Git - vlc/blob - src/misc/darwin_specific.c
0efdd6d75971bb0d8c5f232220486e0a504e2505
[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
34 #include <CoreFoundation/CoreFoundation.h>
35 #include <mach-o/dyld.h>
36
37 #ifdef HAVE_LOCALE_H
38 #   include <locale.h>
39 #endif
40
41 #ifndef MAXPATHLEN
42 # define MAXPATHLEN 1024
43 #endif
44
45 /*****************************************************************************
46  * system_Init: fill in program path & retrieve language
47  *****************************************************************************/
48 void system_Init( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
49 {
50     VLC_UNUSED(p_this);
51     char i_dummy;
52     char *p_char = NULL;
53     char *p_oldchar = &i_dummy;
54     unsigned int i;
55     (void)pi_argc;
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     }
82
83     if( !p_char )
84     {
85         char path[MAXPATHLEN+1];
86         uint32_t path_len = MAXPATHLEN;
87         if ( !_NSGetExecutablePath(path, &path_len) )
88             p_char = strdup(path);
89     }
90     if( !p_char )
91     {
92         /* We are not linked to the VLC.framework, return the executable path */
93         p_char = strdup( ppsz_argv[ 0 ] );
94     }
95
96     free(psz_vlcpath);
97     psz_vlcpath = p_char;
98
99     /* Remove trailing program name */
100     for( ; *p_char ; )
101     {
102         if( *p_char == '/' )
103         {
104             *p_oldchar = '/';
105             *p_char = '\0';
106             p_oldchar = p_char;
107         }
108         p_char++;
109     }
110
111 #ifdef ENABLE_NLS
112     /* Check if $LANG is set. */
113     if( NULL == getenv("LANG") )
114     {
115         /*
116            Retrieve the preferred language as chosen in  System Preferences.app
117            (note that CFLocaleCopyCurrent() is not used because it returns the
118             preferred locale not language)
119         */
120         CFArrayRef all_locales, preferred_locales;
121         char psz_locale[50];
122
123         all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
124
125         preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
126
127         if ( preferred_locales )
128         {
129             if ( CFArrayGetCount( preferred_locales ) )
130             {
131                 CFStringRef user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
132                 CFStringGetCString( user_language_string_ref, psz_locale, sizeof(psz_locale), kCFStringEncodingUTF8 );
133                 setenv( "LANG", psz_locale, 1 );
134             }
135             CFRelease( preferred_locales );
136         }
137         CFRelease( all_locales );
138     }
139 #endif
140 }
141
142 /*****************************************************************************
143  * system_Configure: check for system specific configuration options.
144  *****************************************************************************/
145 void system_Configure( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
146 {
147     (void)p_this;
148     (void)pi_argc;
149     (void)ppsz_argv;
150 }
151
152 /*****************************************************************************
153  * system_End: free the program path.
154  *****************************************************************************/
155 void system_End( libvlc_int_t *p_this )
156 {
157     (void)p_this;
158     free( psz_vlcpath );
159     psz_vlcpath = NULL;
160 }
161