OSDN Git Service

MacGui: Remove Target Size as a rate control option as it doesn't really work correct...
[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 char * str_printf(const char *fmt, ...)
33 {
34     /* Guess we need no more than 100 bytes. */
35     int len;
36     va_list ap;
37     int size = 100;
38     char *tmp, *str = NULL;
39
40     str = (char*)malloc(size);
41     while (1) 
42     {
43         /* Try to print in the allocated space. */
44         va_start(ap, fmt);
45         len = vsnprintf(str, size, fmt, ap);
46         va_end(ap);
47
48         /* If that worked, return the string. */
49         if (len > -1 && len < size) {
50             return str;
51         }
52
53         /* Else try again with more space. */
54         if (len > -1)    /* glibc 2.1 */
55             size = len+1; /* precisely what is needed */
56         else           /* glibc 2.0 */
57             size *= 2;  /* twice the old size */
58
59         tmp = (char*)realloc(str, size);
60         if (tmp == NULL) {
61             return str;
62         }
63         str = tmp;
64     }
65 }
66
67 #define EXTRA_VLC_DYLD_PATH "/Applications/VLC.app/Contents/MacOS/lib"
68 #define DEFAULT_DYLD_PATH "/usr/local/lib:/usr/lib"
69
70 int main( int argc, const char ** argv )
71 {
72     char *dylib_path;
73     int no_exec = 0;
74
75     // Check for flag that prevents exec bomb.  It
76     // incidentally can be used to prevent adding
77     // our modifications to the dyld env vars.
78     if ( argc > 1 && strncmp(argv[1], "-n", 2) == 0 )
79         no_exec = 1;
80
81     if ( !no_exec )
82     {
83         dylib_path = getenv("DYLD_FALLBACK_LIBRARY_PATH");
84         if ( dylib_path == NULL ||
85              strstr( dylib_path, "/Applications/VLC.app/Contents/MacOS/lib" ) == NULL )
86         {
87             char *path = NULL;
88             char *home;
89             int result = -1;
90
91             home = getenv("HOME");
92
93             if ( dylib_path == NULL )
94             {
95                 // Set the system default of $HOME/lib:/usr/local/lib:/usr/lib
96                 // And add our extra path
97                 if ( home != NULL )
98                 {
99                     path = str_printf("%s/lib:%s:%s:%s%s", home, 
100                                       DEFAULT_DYLD_PATH, 
101                                       EXTRA_VLC_DYLD_PATH, 
102                                       home, EXTRA_VLC_DYLD_PATH);
103                 }
104                 else
105                 {
106                     path = str_printf("%s:%s", DEFAULT_DYLD_PATH, EXTRA_VLC_DYLD_PATH);
107                 }
108                 if ( path != NULL )
109                     result = setenv("DYLD_FALLBACK_LIBRARY_PATH", path, 1);
110             }
111             else
112             {
113                 // add our extra path
114                 if ( home != NULL )
115                 {
116                     path = str_printf("%s:%s:%s%s", dylib_path, EXTRA_VLC_DYLD_PATH,
117                                                         home, EXTRA_VLC_DYLD_PATH);
118                 }
119                 else
120                 {
121                     path = str_printf("%s:%s", dylib_path, EXTRA_VLC_DYLD_PATH);
122                 }
123                 if ( path != NULL )
124                     result = setenv("DYLD_FALLBACK_LIBRARY_PATH", path, 1);
125             }
126             if ( result == 0 )
127             {
128                 const char ** new_argv;
129                 int i;
130
131                 new_argv = (const char**)malloc( (argc + 2) * sizeof(char*) );
132                 new_argv[0] = argv[0];
133                 new_argv[1] = "-n";
134                 for (i = 1; i < argc; i++)
135                     new_argv[i+1] = argv[i];
136                 new_argv[i+1] = NULL;
137                 execv(new_argv[0], (char* const*)new_argv);
138             }
139         }
140     }
141     signal( SIGINT, SigHandler );
142     hb_register_error_handler(&hb_error_handler);
143     return NSApplicationMain( argc, argv );
144 }