OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / macosx / main.mm
1 /* $Id: main.mm,v 1.3 2005/11/25 15:04:35 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.fr/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include <Cocoa/Cocoa.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <stdlib.h>
11
12 #import "hb.h"
13
14 void SigHandler( int signal )
15 {
16     [NSApp terminate: NULL];
17
18
19 /****************************************************************************
20  * hb_error_handler
21  * 
22  * Change this to display a dialog box - and maybe move it somewhere else,
23  * this is the only place I could find that looked like C :)
24 ****************************************************************************/
25 extern "C" {
26 void hb_error_handler( const char *errmsg )
27 {
28     fprintf(stderr, "GUI ERROR dialog: %s\n", errmsg );
29 }
30 }
31
32 extern "C" {
33 extern int mm_flags;
34 int mm_support();
35 }
36
37 char * str_printf(const char *fmt, ...)
38 {
39     /* Guess we need no more than 100 bytes. */
40     int len;
41     va_list ap;
42     int size = 100;
43     char *tmp, *str = NULL;
44
45     str = (char*)malloc(size);
46     while (1) 
47     {
48         /* Try to print in the allocated space. */
49         va_start(ap, fmt);
50         len = vsnprintf(str, size, fmt, ap);
51         va_end(ap);
52
53         /* If that worked, return the string. */
54         if (len > -1 && len < size) {
55             return str;
56         }
57
58         /* Else try again with more space. */
59         if (len > -1)    /* glibc 2.1 */
60             size = len+1; /* precisely what is needed */
61         else           /* glibc 2.0 */
62             size *= 2;  /* twice the old size */
63
64         tmp = (char*)realloc(str, size);
65         if (tmp == NULL) {
66             return str;
67         }
68         str = tmp;
69     }
70 }
71
72 #define EXTRA_VLC_DYLD_PATH "/Applications/VLC.app/Contents/MacOS/lib"
73 #define DEFAULT_DYLD_PATH "/usr/local/lib:/usr/lib"
74
75 int main( int argc, const char ** argv )
76 {
77     char *dylib_path;
78     int no_exec = 0;
79
80     // Check for flag that prevents exec bomb.  It
81     // incidentally can be used to prevent adding
82     // our modifications to the dyld env vars.
83     if ( argc > 1 && strncmp(argv[1], "-n", 2) == 0 )
84         no_exec = 1;
85
86     if ( !no_exec )
87     {
88         dylib_path = getenv("DYLD_FALLBACK_LIBRARY_PATH");
89         if ( dylib_path == NULL ||
90              strstr( dylib_path, "/Applications/VLC.app/Contents/MacOS/lib" ) == NULL )
91         {
92             char *path = NULL;
93             char *home;
94             int result = -1;
95
96             home = getenv("HOME");
97
98             if ( dylib_path == NULL )
99             {
100                 // Set the system default of $HOME/lib:/usr/local/lib:/usr/lib
101                 // And add our extra path
102                 if ( home != NULL )
103                 {
104                     path = str_printf("%s/lib:%s:%s:%s%s", home, 
105                                       DEFAULT_DYLD_PATH, 
106                                       EXTRA_VLC_DYLD_PATH, 
107                                       home, EXTRA_VLC_DYLD_PATH);
108                 }
109                 else
110                 {
111                     path = str_printf("%s:%s", DEFAULT_DYLD_PATH, EXTRA_VLC_DYLD_PATH);
112                 }
113                 if ( path != NULL )
114                     result = setenv("DYLD_FALLBACK_LIBRARY_PATH", path, 1);
115             }
116             else
117             {
118                 // add our extra path
119                 if ( home != NULL )
120                 {
121                     path = str_printf("%s:%s:%s%s", dylib_path, EXTRA_VLC_DYLD_PATH,
122                                                         home, EXTRA_VLC_DYLD_PATH);
123                 }
124                 else
125                 {
126                     path = str_printf("%s:%s", dylib_path, EXTRA_VLC_DYLD_PATH);
127                 }
128                 if ( path != NULL )
129                     result = setenv("DYLD_FALLBACK_LIBRARY_PATH", path, 1);
130             }
131             if ( result == 0 )
132             {
133                 const char ** new_argv;
134                 int i;
135
136                 new_argv = (const char**)malloc( (argc + 2) * sizeof(char*) );
137                 new_argv[0] = argv[0];
138                 new_argv[1] = "-n";
139                 for (i = 1; i < argc; i++)
140                     new_argv[i+1] = argv[i];
141                 new_argv[i+1] = NULL;
142                 execv(new_argv[0], (char* const*)new_argv);
143             }
144         }
145     }
146     mm_flags = mm_support();
147     signal( SIGINT, SigHandler );
148     hb_register_error_handler(&hb_error_handler);
149     return NSApplicationMain( argc, argv );
150 }