r851: Merge 2.1:
[cinelerra_cv/ct.git] / cinelerra / main.C
blobfd5230741af442c95e1d3236c9c6cd68a1b3f50f
1 #include "arraylist.h"
2 #include "batchrender.h"
3 #include "bcsignals.h"
4 #include "builddate.h"
5 #include "edl.h"
6 #include "filexml.h"
7 #include "filesystem.h"
8 #include "language.h"
9 #include "loadfile.inc"
10 #include "mainmenu.h"
11 #include "mutex.h"
12 #include "mwindow.h"
13 #include "mwindowgui.h"
14 #include "pluginserver.h"
15 #include "preferences.h"
16 #include "renderfarmclient.h"
18 #include <locale.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #include <locale.h>
24 #define PACKAGE "cinelerra"
25 #define LOCALEDIR "/usr/share/locale"
28 enum
30         DO_GUI,
31         DO_DEAMON,
32         DO_DEAMON_FG,
33         DO_BRENDER,
34         DO_USAGE,
35         DO_BATCHRENDER
38 #include "thread.h"
40 int main(int argc, char *argv[])
42 // handle command line arguments first
43         srand(time(0));
44         ArrayList<char*> filenames;
45         FileSystem fs;
47         int operation = DO_GUI;
48         int deamon_port = DEAMON_PORT;
49         char deamon_path[BCTEXTLEN];
50         char config_path[BCTEXTLEN];
51         char batch_path[BCTEXTLEN];
52         int nice_value = 20;
53         config_path[0] = 0;
54         batch_path[0] = 0;
55         deamon_path[0] = 0;
56         EDL::id_lock = new Mutex("EDL::id_lock");
58 // detect an UTF-8 locale and try to use a non-Unicode locale instead
59 // <---Beginning of dirty hack
60 // This hack will be removed as soon as Cinelerra is UTF-8 compliant
61     char *s, *language;
63 // Query user locale
64     if ((s = getenv("LC_ALL"))  || 
65                 (s = getenv("LC_MESSAGES")) || 
66                 (s = getenv("LC_CTYPE")) || 
67                 (s = getenv ("LANG")))
68     {
69 // Test if user locale is set to Unicode        
70         if (strstr(s, ".UTF-8"))
71         {
72 // extract language  from language-charset@variant
73           language = strtok (s, ".@");
74 // set language as the default locale
75           setenv("LANG", language, 1);
76         }
77     }
78 // End of dirty hack --->
83         bindtextdomain (PACKAGE, LOCALEDIR);
84         textdomain (PACKAGE);
85         setlocale (LC_MESSAGES, "");
86         setlocale (LC_CTYPE, "");
88         for(int i = 1; i < argc; i++)
89         {
90                 if(!strcmp(argv[i], "-h"))
91                 {
92                         operation = DO_USAGE;
93                 }
94                 else
95                 if(!strcmp(argv[i], "-r"))
96                 {
97                         operation = DO_BATCHRENDER;
98                         if(argc > i + 1)
99                         {
100                                 if(argv[i + 1][0] != '-')
101                                 {
102                                         strcpy(batch_path, argv[i + 1]);
103                                         i++;
104                                 }
105                         }
106                 }
107                 else
108                 if(!strcmp(argv[i], "-c"))
109                 {
110                         if(argc > i + 1)
111                         {
112                                 strcpy(config_path, argv[i + 1]);
113                                 i++;
114                         }
115                         else
116                         {
117                                 fprintf(stderr, "%s: -c needs a filename.\n", argv[0]);
118                         }
119                 }
120                 else
121                 if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "-f"))
122                 {
123                         if(!strcmp(argv[i], "-d"))
124                                 operation = DO_DEAMON;
125                         else
126                                 operation = DO_DEAMON_FG;
128                         if(argc > i + 1)
129                         {
130                                 if(atol(argv[i + 1]) > 0)
131                                 {
132                                         deamon_port = atol(argv[i + 1]);
133                                         i++;
134                                 }
135                         }
136                 }
137                 else
138                 if(!strcmp(argv[i], "-b"))
139                 {
140                         operation = DO_BRENDER;
141                         if(i > argc - 2)
142                         {
143                                 fprintf(stderr, "-b may not be used by the user.\n");
144                                 exit(1);
145                         }
146                         else
147                                 strcpy(deamon_path, argv[i + 1]);
148                 }
149                 else
150                 if(!strcmp(argv[i], "-n"))
151                 {
152                         if(argc > i + 1)
153                         {
154                                 nice_value = atol(argv[i + 1]);
155                                 i++;
156                         }
157                 }
158                 else
159                 {
160                         char *new_filename;
161                         new_filename = new char[1024];
162                         strcpy(new_filename, argv[i]);
163             fs.complete_path(new_filename);
165                         filenames.append(new_filename);
166                 }
167         }
172         if(operation == DO_GUI || 
173                 operation == DO_DEAMON || 
174                 operation == DO_DEAMON_FG || 
175                 operation == DO_USAGE ||
176                 operation == DO_BATCHRENDER)
177         fprintf(stderr, 
178                 PROGRAM_NAME " " 
179                 CINELERRA_VERSION " " 
180                 BUILDDATE 
181                 " (C)2006 Heroine Virtual Ltd.\n\n"
183 PROGRAM_NAME " is free software, covered by the GNU General Public License,\n"
184 "and you are welcome to change it and/or distribute copies of it under\n"
185 "certain conditions. There is absolutely no warranty for " PROGRAM_NAME ".\n");
191         switch(operation)
192         {
193                 case DO_USAGE:
194                         printf(_("\nUsage:\n"));
195                         printf(_("%s [-f] [-c configuration] [-d port] [-n nice] [-r batch file] [filenames]\n\n"), argv[0]);
196                         printf(_("-d = Run in the background as renderfarm client.  The port (400) is optional.\n"));
197                         printf(_("-f = Run in the foreground as renderfarm client.  Substitute for -d.\n"));
198                         printf(_("-n = Nice value if running as renderfarm client. (20)\n"));
199                         printf(_("-c = Configuration file to use instead of %s%s.\n"), 
200                                 BCASTDIR, 
201                                 CONFIG_FILE);
202                         printf(_("-r = batch render the contents of the batch file (%s%s) with no GUI.  batch file is optional.\n"), 
203                                 BCASTDIR, 
204                                 BATCH_PATH);
205                         printf(_("filenames = files to load\n\n\n"));
206                         exit(0);
207                         break;
209                 case DO_DEAMON:
210                 case DO_DEAMON_FG:
211                 {
212                         if(operation == DO_DEAMON)
213                         {
214                                 int pid = fork();
216                                 if(pid)
217                                 {
218 // Redhat 9 requires _exit instead of exit here.
219                                         _exit(0);
220                                 }
221                         }
223                         RenderFarmClient client(deamon_port, 
224                                 0, 
225                                 nice_value, 
226                                 config_path);
227                         client.main_loop();
228                         break;
229                 }
231 // Same thing without detachment
232                 case DO_BRENDER:
233                 {
234                         RenderFarmClient client(0, 
235                                 deamon_path, 
236                                 20,
237                                 config_path);
238                         client.main_loop();
239                         break;
240                 }
242                 case DO_BATCHRENDER:
243                 {
244                         BatchRenderThread *thread = new BatchRenderThread;
245                         thread->start_rendering(config_path, 
246                                 batch_path);
247                         break;
248                 }
250                 case DO_GUI:
251                 {
252                         MWindow mwindow;
253                         mwindow.create_objects(1, 
254                                 !filenames.total,
255                                 config_path);
257 // load the initial files on seperate tracks
258                         if(filenames.total)
259                         {
260                                 mwindow.gui->lock_window("main");
261                                 mwindow.load_filenames(&filenames, LOAD_REPLACE);
262                                 if(filenames.total == 1)
263                                         mwindow.gui->mainmenu->add_load(filenames.values[0]);
264                                 mwindow.gui->unlock_window();
265                         }
267 // run the program
268                         mwindow.start();
269                         mwindow.save_defaults();
270 DISABLE_BUFFER
271                         break;
272                 }
273         }
275         filenames.remove_all_objects();
276         return 0;