darwin_specific.m 5.31 KB
Newer Older
Sam Hocevar's avatar
   
Sam Hocevar committed
1
/*****************************************************************************
2
 * darwin_specific.m: Darwin specific features
Sam Hocevar's avatar
   
Sam Hocevar committed
3
 *****************************************************************************
4
 * Copyright (C) 2001-2007 the VideoLAN team
hartman's avatar
-    
hartman committed
5
 * $Id$
Sam Hocevar's avatar
   
Sam Hocevar committed
6
 *
7
 * Authors: Sam Hocevar <sam@zoy.org>
8
 *          Christophe Massiot <massiot@via.ecp.fr>
9
 *          Pierre d'Herbemont <pdherbemont@free.fr>
Sam Hocevar's avatar
   
Sam Hocevar committed
10
11
12
13
14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
15
 *
Sam Hocevar's avatar
   
Sam Hocevar committed
16
17
18
19
20
21
22
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Sam Hocevar's avatar
   
Sam Hocevar committed
24
 *****************************************************************************/
25
#include <string.h>                                              /* strdup(), strstr() */
Sam Hocevar's avatar
   
Sam Hocevar committed
26
#include <stdlib.h>                                                /* free() */
27
#include <dirent.h>                                                /* *dir() */
Sam Hocevar's avatar
   
Sam Hocevar committed
28

29
#include <vlc/vlc.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
30

31
#include <CoreFoundation/CoreFoundation.h>
32

33
34
35
36
#ifdef HAVE_LOCALE_H
#   include <locale.h>
#endif

37
38
39
40
41
/* CFLocaleCopyAvailableLocaleIdentifiers is present only on post-10.4 */
extern CFArrayRef CFLocaleCopyAvailableLocaleIdentifiers(void) __attribute__((weak_import));

/* emulate CFLocaleCopyAvailableLocaleIdentifiers on pre-10.4 */
static CFArrayRef copy_all_locale_indentifiers(void)
42
{
43
44
45
46
47
48
49
50
    CFMutableArrayRef available_locales;
    DIR * dir;
    struct dirent *file;

    dir = opendir( "/usr/share/locale" );
    available_locales = CFArrayCreateMutable( kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks );

    while ( (file = readdir(dir)) )
51
    {
52
        /* we should probably filter out garbage */
Felix Paul Kühne's avatar
Felix Paul Kühne committed
53
54
        /* we can't use CFStringCreateWithFileSystemRepresentation as it is 
         * supported only on post-10.4 (and this function is only for pre-10.4) */
55
56
57
        CFStringRef locale = CFStringCreateWithCString( kCFAllocatorDefault, file->d_name, kCFStringEncodingUTF8 );
        CFArrayAppendValue( available_locales, (void*)locale );
        CFRelease( locale );
58
59
    }

60
61
    closedir( dir );
    return available_locales;
62
63
}

64
65
66
/*****************************************************************************
 * system_Init: fill in program path & retrieve language
 *****************************************************************************/
67
void system_Init( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
Sam Hocevar's avatar
   
Sam Hocevar committed
68
{
Sam Hocevar's avatar
   
Sam Hocevar committed
69
70
    char i_dummy;
    char *p_char, *p_oldchar = &i_dummy;
Sam Hocevar's avatar
   
Sam Hocevar committed
71
72

    /* Get the full program path and name */
73
    p_char = p_this->p_libvlc_global->psz_vlcpath = strdup( ppsz_argv[ 0 ] );
Sam Hocevar's avatar
   
Sam Hocevar committed
74
75
76
77
78

    /* Remove trailing program name */
    for( ; *p_char ; )
    {
        if( *p_char == '/' )
Sam Hocevar's avatar
   
Sam Hocevar committed
79
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
80
            *p_oldchar = '/';
Sam Hocevar's avatar
   
Sam Hocevar committed
81
82
83
            *p_char = '\0';
            p_oldchar = p_char;
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
84

Sam Hocevar's avatar
   
Sam Hocevar committed
85
        p_char++;
Sam Hocevar's avatar
   
Sam Hocevar committed
86
    }
87
88
89
90

    /* Check if $LANG is set. */
    if ( (p_char = getenv("LANG")) == NULL )
    {
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
        /*
           Retrieve the preferred language as chosen in  System Preferences.app
           (note that CFLocaleCopyCurrent() is not used because it returns the
            prefered locale not language) 
        */
        CFArrayRef all_locales, preferred_locales;
        char psz_locale[50];

        if( CFLocaleCopyAvailableLocaleIdentifiers )
            all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
        else
            all_locales = copy_all_locale_indentifiers();

        preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );

        if ( preferred_locales )
107
        {
108
            if ( CFArrayGetCount( preferred_locales ) )
109
            {
110
111
112
                CFStringRef user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
                CFStringGetCString( user_language_string_ref, psz_locale, sizeof(psz_locale), kCFStringEncodingUTF8 );
                setenv( "LANG", psz_locale, 1 );
113
            }
114
            CFRelease( preferred_locales );
115
        }
116
        CFRelease( all_locales );
117
    }
118

119
120
    vlc_mutex_init( p_this, &p_this->p_libvlc_global->iconv_lock );
    p_this->p_libvlc_global->iconv_macosx = vlc_iconv_open( "UTF-8", "UTF-8-MAC" );
Sam Hocevar's avatar
   
Sam Hocevar committed
121
122
}

123
124
125
/*****************************************************************************
 * system_Configure: check for system specific configuration options.
 *****************************************************************************/
126
void system_Configure( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
127
128
129
130
{

}

Sam Hocevar's avatar
   
Sam Hocevar committed
131
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
132
 * system_End: free the program path.
Sam Hocevar's avatar
   
Sam Hocevar committed
133
 *****************************************************************************/
134
void system_End( libvlc_int_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
135
{
136
    free( p_this->p_libvlc_global->psz_vlcpath );
137

138
139
140
    if ( p_this->p_libvlc_global->iconv_macosx != (vlc_iconv_t)-1 )
        vlc_iconv_close( p_this->p_libvlc_global->iconv_macosx );
    vlc_mutex_destroy( &p_this->p_libvlc_global->iconv_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
141
142
}