summaryrefslogtreecommitdiffstats
path: root/macosx/main.mm
blob: 0d0b01a6a574f44b1c11336d98719fadc7e4a15a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* $Id: main.mm,v 1.3 2005/11/25 15:04:35 titer Exp $

   This file is part of the HandBrake source code.
   Homepage: <http://handbrake.fr/>.
   It may be used under the terms of the GNU General Public License. */

#include <Cocoa/Cocoa.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

#import "hb.h"

void SigHandler( int signal )
{
    [NSApp terminate: NULL];
} 

/****************************************************************************
 * hb_error_handler
 * 
 * Change this to display a dialog box - and maybe move it somewhere else,
 * this is the only place I could find that looked like C :)
****************************************************************************/
extern "C" {
void hb_error_handler( const char *errmsg )
{
    fprintf(stderr, "GUI ERROR dialog: %s\n", errmsg );
}
}

extern "C" {
extern int mm_flags;
int mm_support();
}

char * str_printf(const char *fmt, ...)
{
    /* Guess we need no more than 100 bytes. */
    int len;
    va_list ap;
    int size = 100;
    char *tmp, *str = NULL;

    str = (char*)malloc(size);
    while (1) 
    {
        /* Try to print in the allocated space. */
        va_start(ap, fmt);
        len = vsnprintf(str, size, fmt, ap);
        va_end(ap);

        /* If that worked, return the string. */
        if (len > -1 && len < size) {
            return str;
        }

        /* Else try again with more space. */
        if (len > -1)    /* glibc 2.1 */
            size = len+1; /* precisely what is needed */
        else           /* glibc 2.0 */
            size *= 2;  /* twice the old size */

        tmp = (char*)realloc(str, size);
        if (tmp == NULL) {
            return str;
        }
        str = tmp;
    }
}

#define EXTRA_VLC_DYLD_PATH "/Applications/VLC.app/Contents/MacOS/lib"
#define DEFAULT_DYLD_PATH "/usr/local/lib:/usr/lib"

int main( int argc, const char ** argv )
{
    char *dylib_path;
    int no_exec = 0;

    // Check for flag that prevents exec bomb.  It
    // incidentally can be used to prevent adding
    // our modifications to the dyld env vars.
    if ( argc > 1 && strncmp(argv[1], "-n", 2) == 0 )
        no_exec = 1;

    if ( !no_exec )
    {
        dylib_path = getenv("DYLD_FALLBACK_LIBRARY_PATH");
        if ( dylib_path == NULL ||
             strstr( dylib_path, "/Applications/VLC.app/Contents/MacOS/lib" ) == NULL )
        {
            char *path = NULL;
            char *home;
            int result = -1;

            home = getenv("HOME");

            if ( dylib_path == NULL )
            {
                // Set the system default of $HOME/lib:/usr/local/lib:/usr/lib
                // And add our extra path
                if ( home != NULL )
                {
                    path = str_printf("%s/lib:%s:%s:%s%s", home, 
                                      DEFAULT_DYLD_PATH, 
                                      EXTRA_VLC_DYLD_PATH, 
                                      home, EXTRA_VLC_DYLD_PATH);
                }
                else
                {
                    path = str_printf("%s:%s", DEFAULT_DYLD_PATH, EXTRA_VLC_DYLD_PATH);
                }
                if ( path != NULL )
                    result = setenv("DYLD_FALLBACK_LIBRARY_PATH", path, 1);
            }
            else
            {
                // add our extra path
                if ( home != NULL )
                {
                    path = str_printf("%s:%s:%s%s", dylib_path, EXTRA_VLC_DYLD_PATH,
                                                        home, EXTRA_VLC_DYLD_PATH);
                }
                else
                {
                    path = str_printf("%s:%s", dylib_path, EXTRA_VLC_DYLD_PATH);
                }
                if ( path != NULL )
                    result = setenv("DYLD_FALLBACK_LIBRARY_PATH", path, 1);
            }
            if ( result == 0 )
            {
                const char ** new_argv;
                int i;

                new_argv = (const char**)malloc( (argc + 2) * sizeof(char*) );
                new_argv[0] = argv[0];
                new_argv[1] = "-n";
                for (i = 1; i < argc; i++)
                    new_argv[i+1] = argv[i];
                new_argv[i+1] = NULL;
                execv(new_argv[0], (char* const*)new_argv);
            }
        }
    }
    mm_flags = mm_support();
    signal( SIGINT, SigHandler );
    hb_register_error_handler(&hb_error_handler);
    return NSApplicationMain( argc, argv );
}