more ru xlats, doc updates, proxy tweaks
[goodguy/history.git] / cinelerra-5.1 / cinelerra / mwindow.C
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "aboutprefs.h"
22 #include "asset.h"
23 #include "assets.h"
24 #include "atrack.h"
25 #include "audioalsa.h"
26 #include "autos.h"
27 #include "awindowgui.h"
28 #include "awindow.h"
29 #include "batchrender.h"
30 #include "bcdisplayinfo.h"
31 #include "bcsignals.h"
32 #include "bctimer.h"
33 #include "bctrace.h"
34 #include "bdcreate.h"
35 #include "brender.h"
36 #include "cache.h"
37 #include "channel.h"
38 #include "channeldb.h"
39 #include "channelinfo.h"
40 #include "clip.h"
41 #include "clipedls.h"
42 #include "bccmodels.h"
43 #include "commercials.h"
44 #include "cplayback.h"
45 #include "ctimebar.h"
46 #include "cwindowgui.h"
47 #include "cwindow.h"
48 #include "cwindowtool.h"
49 #include "bchash.h"
50 #include "devicedvbinput.inc"
51 #include "dvdcreate.h"
52 #include "editpanel.h"
53 #include "edl.h"
54 #include "edlsession.h"
55 #include "errorbox.h"
56 #include "fileformat.h"
57 #include "file.h"
58 #include "filesystem.h"
59 #include "filexml.h"
60 #include "floatautos.h"
61 #include "framecache.h"
62 #include "gwindow.h"
63 #include "gwindowgui.h"
64 #include "keyframegui.h"
65 #include "indexfile.h"
66 #include "intautos.h"
67 #include "interlacemodes.h"
68 #include "language.h"
69 #include "levelwindowgui.h"
70 #include "levelwindow.h"
71 #include "loadfile.inc"
72 #include "localsession.h"
73 #include "maincursor.h"
74 #include "mainerror.h"
75 #include "mainindexes.h"
76 #include "mainmenu.h"
77 #include "mainprogress.h"
78 #include "mainsession.h"
79 #include "mainundo.h"
80 #include "mbuttons.h"
81 #include "mutex.h"
82 #include "mwindowgui.h"
83 #include "mwindow.h"
84 #include "new.h"
85 #include "panautos.h"
86 #include "patchbay.h"
87 #include "playback3d.h"
88 #include "playbackengine.h"
89 #include "plugin.h"
90 #include "pluginserver.h"
91 #include "pluginset.h"
92 #include "preferences.h"
93 #include "proxy.h"
94 #include "record.h"
95 #include "recordmonitor.h"
96 #include "recordlabel.h"
97 #include "removefile.h"
98 #include "render.h"
99 #include "resourcethread.h"
100 #include "samplescroll.h"
101 #include "sha1.h"
102 #include "sighandler.h"
103 #include "splashgui.h"
104 #include "statusbar.h"
105 #include "theme.h"
106 #include "threadloader.h"
107 #include "timebar.h"
108 #include "timelinepane.h"
109 #include "tipwindow.h"
110 #include "trackcanvas.h"
111 #include "track.h"
112 #include "tracking.h"
113 #include "trackscroll.h"
114 #include "tracks.h"
115 #include "transition.h"
116 #include "transportque.h"
117 #include "vframe.h"
118 #include "vtrack.h"
119 #include "versioninfo.h"
120 #include "videodevice.inc"
121 #include "videowindow.h"
122 #include "vplayback.h"
123 #include "vwindowgui.h"
124 #include "vwindow.h"
125 #include "wavecache.h"
126 #include "wwindow.h"
127 #include "zoombar.h"
128 #include "zwindow.h"
129 #include "zwindowgui.h"
130 #include "exportedl.h"
131
132 #include "defaultformats.h"
133 #include "ntsczones.h"
134
135 #include <stdint.h>
136 #include <stdlib.h>
137 #include <unistd.h>
138 #include <fcntl.h>
139 #include <string.h>
140 #include <sys/types.h>
141 #include <sys/stat.h>
142 #include <sys/time.h>
143 #include <sys/mman.h>
144 #include <limits.h>
145 #include <errno.h>
146
147
148 extern "C"
149 {
150
151
152
153
154 // Hack for libdv to remove glib dependancy
155
156 // void
157 // g_log (const char    *log_domain,
158 //        int  log_level,
159 //        const char    *format,
160 //        ...)
161 // {
162 // }
163 //
164 // void
165 // g_logv (const char    *log_domain,
166 //        int  log_level,
167 //        const char    *format,
168 //        ...)
169 // {
170 // }
171 //
172
173 }
174
175
176
177 ArrayList<PluginServer*>* MWindow::plugindb = 0;
178 Commercials* MWindow::commercials = 0;
179
180
181 MWindow::MWindow()
182  : Thread(1, 0, 0)
183 {
184         run_lock = new Mutex("MWindow::run_lock");
185         plugin_gui_lock = new Mutex("MWindow::plugin_gui_lock");
186         dead_plugin_lock = new Mutex("MWindow::dead_plugin_lock");
187         vwindows_lock = new Mutex("MWindow::vwindows_lock");
188         zwindows_lock = new Mutex("MWindow::zwindows_lock");
189         brender_lock = new Mutex("MWindow::brender_lock");
190         keyframe_gui_lock = new Mutex("MWindow::keyframe_gui_lock");
191
192         playback_3d = 0;
193         splash_window = 0;
194         undo = 0;
195         defaults = 0;
196         assets = 0;
197         //commercials = 0;
198         commercial_active = 0;
199         audio_cache = 0;
200         video_cache = 0;
201         frame_cache = 0;
202         wave_cache = 0;
203         preferences = 0;
204         session = 0;
205         theme = 0;
206         mainindexes = 0;
207         mainprogress = 0;
208         brender = 0;
209         brender_active = 0;
210         strcpy(cin_lang,"en");
211         channeldb_buz =  new ChannelDB;
212         channeldb_v4l2jpeg =  new ChannelDB;
213         plugin_guis = 0;
214         dead_plugins = 0;
215         keyframe_threads = 0;
216         create_bd = 0;
217         create_dvd = 0;
218         batch_render = 0;
219         render = 0;
220         edl = 0;
221         gui = 0;
222         cwindow = 0;
223         awindow = 0;
224         gwindow = 0;
225         twindow = 0;
226         wwindow = 0;
227         lwindow = 0;
228         sighandler = 0;
229         restart_status = 0;
230         screens = 1;
231         in_destructor = 0;
232         speed_edl = 0;
233         proxy_beep = 0;
234 }
235
236
237 // Need to delete brender temporary here.
238 MWindow::~MWindow()
239 {
240         run_lock->lock("MWindow::~MWindow");
241         in_destructor = 1;
242 //printf("MWindow::~MWindow %d\n", __LINE__);
243         gui->remote_control->deactivate();
244         gui->record->stop();
245 #ifdef HAVE_DVB
246         gui->channel_info->stop();
247 #endif
248         delete proxy_beep;
249         delete create_bd;       create_bd = 0;
250         delete create_dvd;      create_dvd = 0;
251         delete batch_render;    batch_render = 0;
252         delete render;          render = 0;
253         commit_commercial();
254         if( commercials && !commercials->remove_user() ) commercials = 0;
255         close_mixers();
256         if( speed_edl ) { speed_edl->remove_user();  speed_edl = 0; }
257 // Save defaults for open plugins
258         plugin_gui_lock->lock("MWindow::~MWindow");
259         for(int i = 0; i < plugin_guis->size(); i++) {
260                 plugin_guis->get(i)->hide_gui();
261                 delete_plugin(plugin_guis->get(i));
262         }
263         plugin_gui_lock->unlock();
264         hide_keyframe_guis();
265         clean_indexes();
266         save_defaults();
267 // Give up and go to a movie
268 //  cant run valgrind if this is used
269
270         gui->del_keyboard_listener(
271                 (int (BC_WindowBase::*)(BC_WindowBase *))
272                 &MWindowGUI::keyboard_listener);
273 #if 0
274 // release the hounds
275         if( awindow && awindow->gui ) awindow->gui->close(0);
276         if( cwindow && cwindow->gui ) cwindow->gui->close(0);
277         if( lwindow && lwindow->gui ) lwindow->gui->close(0);
278         if( gwindow && gwindow->gui ) gwindow->gui->close(0);
279         if( twindow && twindow->is_running() ) twindow->close_window();
280         if( wwindow && wwindow->is_running() ) wwindow->close_window();
281         vwindows.remove_all_objects();
282         zwindows.remove_all_objects();
283         gui->close(0);
284         if( awindow ) awindow->join();
285         if( cwindow ) cwindow->join();
286         if( lwindow ) lwindow->join();
287         if( twindow ) twindow->join();
288         if( wwindow ) wwindow->join();
289         if( gwindow ) gwindow->join();
290         join();
291 #else
292 // one at a time, or nouveau chokes
293 #define close_gui(win) if( win ) { \
294   if( win->gui ) win->gui->close(0); \
295   win->join(); }
296         close_gui(awindow);
297         close_gui(cwindow);
298         close_gui(lwindow);
299         close_gui(gwindow);
300         close_gui(twindow);
301         close_gui(wwindow);
302         vwindows.remove_all_objects();
303         zwindows.remove_all_objects();
304         gui->close(0);
305         join();
306 #endif
307         reset_caches();
308         dead_plugins->remove_all_objects();
309 // must delete theme before destroying plugindb
310 //  theme destructor will be deleted by delete_plugins
311         delete theme;           theme = 0;
312         delete_plugins();
313         finit_error();
314         keyframe_threads->remove_all_objects();
315         colormodels.remove_all_objects();
316         delete awindow;         awindow = 0;
317         delete lwindow;         lwindow = 0;
318         delete twindow;         twindow = 0;
319         delete wwindow;         wwindow = 0;
320         delete gwindow;         gwindow = 0;
321         delete cwindow;         cwindow = 0;
322         delete gui;             gui = 0;
323         delete mainindexes;     mainindexes = 0;
324         delete mainprogress;    mainprogress = 0;
325         delete audio_cache;     audio_cache = 0;  // delete the cache after the assets
326         delete video_cache;     video_cache = 0;  // delete the cache after the assets
327         delete frame_cache;     frame_cache = 0;
328         delete wave_cache;      wave_cache = 0;
329         delete plugin_guis;     plugin_guis = 0;
330         delete dead_plugins;    dead_plugins = 0;
331         delete keyframe_threads;  keyframe_threads = 0;
332         delete undo;            undo = 0;
333         delete preferences;     preferences = 0;
334         delete exportedl;       exportedl = 0;
335         delete session;         session = 0;
336         delete defaults;        defaults = 0;
337         delete assets;          assets = 0;
338         delete splash_window;   splash_window = 0;
339         if( !edl->Garbage::remove_user() ) edl = 0;
340         delete channeldb_buz;
341         delete channeldb_v4l2jpeg;
342 // This must be last thread to exit
343         delete playback_3d;     playback_3d = 0;
344         delete dead_plugin_lock;
345         delete plugin_gui_lock;
346         delete vwindows_lock;
347         delete zwindows_lock;
348         delete brender_lock;
349         delete keyframe_gui_lock;
350         colormodels.remove_all_objects();
351         interlace_project_modes.remove_all_objects();
352         interlace_asset_modes.remove_all_objects();
353         interlace_asset_fixmethods.remove_all_objects();
354         sighandler->terminate();
355         delete sighandler;
356         delete run_lock;
357 }
358
359
360 void MWindow::quit()
361 {
362         gui->set_done(0);
363 }
364
365 void MWindow::init_error()
366 {
367         MainError::init_error(this);
368 }
369
370 void MWindow::finit_error()
371 {
372         MainError::finit_error();
373 }
374
375 void MWindow::create_defaults_path(char *string, const char *config_file)
376 {
377 // set the .bcast path
378         FileSystem fs;
379
380         sprintf(string, "%s/", File::get_config_path());
381         fs.complete_path(string);
382         if(!fs.is_dir(string))
383                 fs.create_dir(string);
384
385 // load the defaults
386         strcat(string, config_file);
387 }
388 const char *MWindow::default_std()
389 {
390         char buf[BCTEXTLEN], *p = 0;
391
392         int fd = open(TIMEZONE_NAME, O_RDONLY);
393         if( fd >= 0 ) {
394                 int l = read(fd, buf, sizeof(buf)-1);
395                 close(fd);
396                 if( l > 0 ) {
397                         if( buf[l-1] == '\n' ) --l;
398                         buf[l] = 0;
399                         p = buf;
400                 }
401         }
402         if( !p ) {
403                 int l = readlink(LOCALTIME_LINK, buf, sizeof(buf)-1);
404                 if( l > 0 ) {
405                         buf[l] = 0;
406                         if( !(p=strstr(buf, ZONEINFO_STR)) != 0 ) return "PAL";
407                         p += strlen(ZONEINFO_STR);
408                 }
409         }
410
411         if( p ) {
412                 for( int i=0; ntsc_zones[i]; ++i ) {
413                         if( !strcmp(ntsc_zones[i], p) )
414                                 return "NTSC";
415                 }
416         }
417
418         p = getenv("TZ");
419         if( p ) {
420                 for( int i=0; ntsc_zones[i]; ++i ) {
421                         if( !strcmp(ntsc_zones[i], p) )
422                                 return "NTSC";
423                 }
424         }
425
426 //__timezone: Seconds west of UTC.  240sec/deg
427         double tz_deg = -__timezone / 240.;
428 // from Honolulu = -10, to New York = -5, 15deg/hr   lat -150..-75
429         return tz_deg >= -10*15 && tz_deg <= -5*15 ? "NTSC" : "PAL";
430 }
431
432 void MWindow::fill_preset_defaults(const char *preset, EDLSession *session)
433 {
434         struct formatpresets *fpr;
435
436         for(fpr = &format_presets[0]; fpr->name; fpr++)
437         {
438                 if(strcmp(_(fpr->name), preset) == 0)
439                 {
440                         session->audio_channels = fpr->audio_channels;
441                         session->audio_tracks = fpr->audio_tracks;
442                         session->sample_rate = fpr->sample_rate;
443                         session->video_channels = fpr->video_channels;
444                         session->video_tracks = fpr->video_tracks;
445                         session->frame_rate = fpr->frame_rate;
446                         session->output_w = fpr->output_w;
447                         session->output_h = fpr->output_h;
448                         session->aspect_w = fpr->aspect_w;
449                         session->aspect_h = fpr->aspect_h;
450                         session->interlace_mode = fpr->interlace_mode;
451                         session->color_model = fpr->color_model;
452                         return;
453                 }
454         }
455 }
456
457 const char *MWindow::get_preset_name(int index)
458 {
459         if(index < 0 || index >= (int)MAX_NUM_PRESETS)
460                 return "Error";
461         return _(format_presets[index].name);
462 }
463
464
465 void MWindow::init_defaults(BC_Hash* &defaults, char *config_path)
466 {
467         char path[BCTEXTLEN];
468 // Use user supplied path
469         if(config_path[0])
470                 strcpy(path, config_path);
471         else
472                 create_defaults_path(path, CONFIG_FILE);
473
474         delete defaults;
475         defaults = new BC_Hash(path);
476         defaults->load();
477 }
478
479
480 void MWindow::check_language()
481 {
482         char curr_lang[BCTEXTLEN]; curr_lang[0] = 0;
483         const char *env_lang = getenv("LANGUAGE");
484         if( !env_lang ) env_lang = getenv("LC_ALL");
485         if( !env_lang ) env_lang = getenv("LANG");
486         if( !env_lang ) {
487                 snprintf(curr_lang, sizeof(curr_lang), "%s_%s-%s",
488                         BC_Resources::language, BC_Resources::region, BC_Resources::encoding);
489                 env_lang = curr_lang;
490         }
491         char last_lang[BCTEXTLEN]; last_lang[0] = 0;
492         defaults->get("LAST_LANG",last_lang);
493         if( strcmp(env_lang,last_lang)) {
494                 printf("lang changed from '%s' to '%s'\n", last_lang, env_lang);
495                 defaults->update("LAST_LANG",env_lang);
496                 char plugin_path[BCTEXTLEN];
497                 create_defaults_path(plugin_path, PLUGIN_FILE);
498                 ::remove(plugin_path);
499                 char ladspa_path[BCTEXTLEN];
500                 create_defaults_path(ladspa_path, LADSPA_FILE);
501                 ::remove(ladspa_path);
502                 defaults->save();
503         }
504         if( strlen(env_lang) > 1 &&
505             ( env_lang[2] == 0 || env_lang[2] == '_'  || env_lang[2] == '.' ) ) {
506                 cin_lang[0] = env_lang[0];  cin_lang[1] = env_lang[1];  cin_lang[2] = 0;
507         }
508         else
509                 strcpy(cin_lang, "en");
510 }
511
512 void MWindow::get_plugin_path(char *path, const char *plug_dir, const char *fs_path)
513 {
514         char *base_path = FileSystem::basepath(fs_path), *bp = base_path;
515         if( plug_dir ) {
516                 const char *dp = plug_dir;
517                 while( *bp && *dp && *bp == *dp ) { ++bp; ++dp; }
518                 bp = !*dp && *bp == '/' ? bp+1 : base_path;
519         }
520         strcpy(path, bp);
521         delete [] base_path;
522 }
523
524 int MWindow::load_plugin_index(MWindow *mwindow, const char *index_path, const char *plugin_dir)
525 {
526 // load index
527         FILE *fp = fopen(index_path, "r");
528         if( !fp ) return -1;
529
530         int ret = 0;
531         char index_line[BCTEXTLEN];
532         int index_version = -1, len = strlen(plugin_dir);
533         if( !fgets(index_line, BCTEXTLEN, fp) ||
534             sscanf(index_line, "%d", &index_version) != 1 ||
535             index_version != PLUGIN_FILE_VERSION ||
536             !fgets(index_line, BCTEXTLEN, fp) ||
537             (int)strlen(index_line)-1 != len || index_line[len] != '\n' ||
538             strncmp(index_line, plugin_dir, len) != 0 ) ret = 1;
539
540         ArrayList<PluginServer*> plugins;
541         while( !ret && !feof(fp) && fgets(index_line, BCTEXTLEN, fp) ) {
542                 if( index_line[0] == ';' ) continue;
543                 if( index_line[0] == '#' ) continue;
544                 int type = PLUGIN_TYPE_UNKNOWN;
545                 char path[BCTEXTLEN], title[BCTEXTLEN];
546                 int64_t mtime = 0;
547                 if( PluginServer::scan_table(index_line, type, path, title, mtime) ) {
548                         ret = 1; continue;
549                 }
550                 PluginServer *server = 0;
551                 switch( type ) {
552                 case PLUGIN_TYPE_BUILTIN:
553                 case PLUGIN_TYPE_EXECUTABLE:
554                 case PLUGIN_TYPE_LADSPA: {
555                         char plugin_path[BCTEXTLEN];  struct stat st;
556                         sprintf(plugin_path, "%s/%s", plugin_dir, path);
557                         if( stat(plugin_path, &st) || st.st_mtime != mtime ) {
558                                 ret = 1; continue;
559                         }
560                         server = new PluginServer(mwindow, plugin_path, type);
561                         break; }
562                 case PLUGIN_TYPE_FFMPEG: {
563                         server = new_ffmpeg_server(mwindow, path);
564                         break; }
565                 case PLUGIN_TYPE_LV2: {
566                         server = new_lv2_server(mwindow, path);
567                         break; }
568                 }
569                 if( !server ) continue;
570                 plugins.append(server);
571 // Create plugin server from index entry
572                 server->set_title(title);
573                 if( server->read_table(index_line) ) {
574                         ret = 1;  continue;
575                 }
576         }
577         fclose(fp);
578
579         if( !ret )
580                 ret = check_plugin_index(plugins, plugin_dir, ".");
581
582         if( !ret )
583                 add_plugins(plugins);
584         else
585                 plugins.remove_all_objects();
586
587         return ret;
588 }
589
590 int MWindow::check_plugin_index(ArrayList<PluginServer*> &plugins,
591         const char *plug_dir, const char *plug_path)
592 {
593         char plugin_path[BCTEXTLEN];
594         sprintf(plugin_path, "%s/%s", plug_dir, plug_path);
595         FileSystem fs;
596         fs.set_filter( "[*.plugin][*.so]" );
597         if( fs.update(plugin_path) )
598                 return 1;
599
600         for( int i=0; i<fs.dir_list.total; ++i ) {
601                 char fs_path[BCTEXTLEN];
602                 get_plugin_path(fs_path, 0, fs.dir_list[i]->path);
603                 if( fs.is_dir(fs_path) ) {
604                         get_plugin_path(plugin_path, plug_dir, fs_path);
605                         if( check_plugin_index(plugins, plug_dir, plugin_path) )
606                                 return 1;
607                 }
608                 else if( !plugin_exists(fs_path, plugins) )
609                         return 1;
610         }
611         return 0;
612 }
613
614
615 int MWindow::init_plugins(MWindow *mwindow, Preferences *preferences)
616 {
617         if( !plugindb )
618                 plugindb = new ArrayList<PluginServer*>;
619         init_ffmpeg();
620         char index_path[BCTEXTLEN], plugin_path[BCTEXTLEN];
621         create_defaults_path(index_path, PLUGIN_FILE);
622         char *plugin_dir = FileSystem::basepath(preferences->plugin_dir);
623         strcpy(plugin_path, plugin_dir);  delete [] plugin_dir;
624         if( !load_plugin_index(mwindow, index_path, plugin_path) ) return 1;
625         printf("init plugin index: %s\n", plugin_path);
626         FILE *fp = fopen(index_path,"w");
627         if( !fp ) {
628                 fprintf(stderr,_("MWindow::init_plugins: "
629                         "can't create plugin index: %s\n"), index_path);
630                 return 1;
631         }
632         fprintf(fp, "%d\n", PLUGIN_FILE_VERSION);
633         fprintf(fp, "%s\n", plugin_path);
634         init_plugin_index(mwindow, preferences, fp, plugin_path);
635         init_ffmpeg_index(mwindow, preferences, fp);
636         init_lv2_index(mwindow, preferences, fp);
637         fclose(fp);
638         return load_plugin_index(mwindow, index_path, plugin_path);
639 }
640
641 int MWindow::init_ladspa_plugins(MWindow *mwindow, Preferences *preferences)
642 {
643         char *path = getenv("LADSPA_PATH");
644         char ladspa_path[BCTEXTLEN];
645         if( !path ) {
646                 strncpy(ladspa_path, File::get_ladspa_path(), sizeof(ladspa_path));
647                 path = ladspa_path;
648         }
649         for( int len=0; *path; path+=len ) {
650                 char *cp = strchr(path,':');
651                 len = !cp ? strlen(path) : cp-path;
652                 char index_path[BCTEXTLEN], plugin_path[BCTEXTLEN];
653                 memcpy(plugin_path, path, len);  plugin_path[len] = 0;
654                 if( cp ) ++len;
655                 char *plugin_dir = FileSystem::basepath(plugin_path);
656                 strcpy(plugin_path, plugin_dir);  delete [] plugin_dir;
657                 create_defaults_path(index_path, LADSPA_FILE);
658                 cp = index_path + strlen(index_path);
659                 for( char *bp=plugin_path; *bp!=0; ++bp )
660                         *cp++ = *bp=='/' ? '_' : *bp;
661                 *cp = 0;
662                 if( !load_plugin_index(mwindow, index_path, plugin_path) ) continue;
663                 if( init_ladspa_index(mwindow, preferences, index_path, plugin_path) ) continue;
664                 load_plugin_index(mwindow, index_path, plugin_path);
665         }
666         return 1;
667 }
668
669 void MWindow::init_plugin_index(MWindow *mwindow, Preferences *preferences,
670         FILE *fp, const char *plugin_dir)
671 {
672         int idx = PLUGIN_IDS;
673         if( plugindb ) {
674                 for( int i=0; i<plugindb->size(); ++i ) {
675                         PluginServer *server = plugindb->get(i);
676                         if( server->dir_idx >= idx )
677                                 idx = server->dir_idx+1;
678                 }
679         }
680         scan_plugin_index(mwindow, preferences, fp, plugin_dir, ".", idx);
681 }
682
683 void MWindow::scan_plugin_index(MWindow *mwindow, Preferences *preferences, FILE *fp,
684         const char *plug_dir, const char *plug_path, int &idx)
685 {
686         char plugin_path[BCTEXTLEN];
687         sprintf(plugin_path, "%s/%s", plug_dir, plug_path);
688         FileSystem fs;
689         fs.set_filter( "[*.plugin][*.so]" );
690         int result = fs.update(plugin_path);
691         if( result || !fs.dir_list.total ) return;
692         int vis_id = idx++;
693
694         for( int i=0; i<fs.dir_list.total; ++i ) {
695                 char fs_path[BCTEXTLEN], path[BCTEXTLEN];
696                 get_plugin_path(fs_path, 0, fs.dir_list[i]->path);
697                 get_plugin_path(path, plug_dir, fs_path);
698                 if( fs.is_dir(fs_path) ) {
699                         scan_plugin_index(mwindow, preferences, fp, plug_dir, path, idx);
700                         continue;
701                 }
702                 if( plugin_exists(path) ) continue;
703                 struct stat st;
704                 if( stat(fs_path, &st) ) continue;
705                 int64_t mtime = st.st_mtime;
706                 PluginServer server(mwindow, fs_path, PLUGIN_TYPE_UNKNOWN);
707                 result = server.open_plugin(1, preferences, 0, 0);
708                 if( !result ) {
709                         server.write_table(fp, path, vis_id, mtime);
710                         server.close_plugin();
711                 }
712                 else if( result == PLUGINSERVER_IS_LAD ) {
713                         int lad_index = 0;
714                         for(;;) {
715                                 PluginServer ladspa(mwindow, fs_path, PLUGIN_TYPE_LADSPA);
716                                 ladspa.set_lad_index(lad_index++);
717                                 result = ladspa.open_plugin(1, preferences, 0, 0);
718                                 if( result ) break;
719                                 ladspa.write_table(fp, path, PLUGIN_LADSPA_ID, mtime);
720                                 ladspa.close_plugin();
721                         }
722                 }
723         }
724 }
725
726 void MWindow::add_plugins(ArrayList<PluginServer*> &plugins)
727 {
728         for( int i=0; i<plugins.size(); ++i )
729                 plugindb->append(plugins[i]);
730         plugins.remove_all();
731 }
732
733 void MWindow::init_plugin_tips(ArrayList<PluginServer*> &plugins, const char *lang)
734 {
735         const char *dat_path = File::get_cindat_path();
736         char msg_path[BCTEXTLEN];
737         FILE *fp = 0;
738         if( BC_Resources::language[0] ) {
739                 snprintf(msg_path, sizeof(msg_path), "%s/info/plugins.%s",
740                         dat_path, lang);
741                 fp = fopen(msg_path, "r");
742         }
743         if( !fp ) {
744                 snprintf(msg_path, sizeof(msg_path), "%s/info/plugins.txt",
745                         dat_path);
746                 fp = fopen(msg_path, "r");
747         }
748         if( !fp ) return;
749         char text[BCTEXTLEN];
750         char *tp = text, *ep = tp + sizeof(text)-1;
751         char title[BCTEXTLEN];
752         title[0] = 0;
753         int no = 0;
754         for(;;) {
755                 ++no;  int done = 1;
756                 char line[BCTEXTLEN], *cp = line;
757                 if( fgets(line,sizeof(line)-1,fp) ) {
758                         if( *cp == '#' ) continue;
759                         done = *cp == ' ' || *cp == '\t' ? 0 : -1;
760                 }
761                 if( done ) {
762                         if( tp > text && *--tp == '\n' ) *tp = 0;
763                         if( title[0] ) {
764                                 int idx = plugins.size();
765                                 while( --idx>=0 && strcmp(plugins[idx]->title, title) );
766                                 if( idx >= 0 ) {
767                                         delete [] plugins[idx]->tip;
768                                         plugins[idx]->tip = cstrdup(text);
769                                 }
770                                 title[0] = 0;
771                         }
772                         if( done > 0 ) break;
773                         tp = text;  *tp = 0;
774                         char *dp = strchr(cp, ':');
775                         if( !dp ) {
776                                 printf("plugin tips: error on line %d\n", no);
777                                 continue;
778                         }
779                         char *bp = title;
780                         while( cp < dp ) *bp++ = *cp++;
781                         *bp = 0;
782                         ++cp;
783                 }
784
785                 while( *cp == ' ' || *cp == '\t' ) ++cp;
786                 for( ; tp<ep && (*tp=*cp)!=0; ++tp,++cp );
787         }
788         fclose(fp);
789 }
790
791 void MWindow::delete_plugins()
792 {
793         plugindb->remove_all_objects();
794         delete plugindb;
795         plugindb = 0;
796 }
797
798 void MWindow::search_plugindb(int do_audio,
799                 int do_video,
800                 int is_realtime,
801                 int is_transition,
802                 int is_theme,
803                 ArrayList<PluginServer*> &results)
804 {
805 // Get plugins
806         for(int i = 0; i < MWindow::plugindb->total; i++)
807         {
808                 PluginServer *current = MWindow::plugindb->get(i);
809
810                 if(current->audio == do_audio &&
811                         current->video == do_video &&
812                         (current->realtime == is_realtime || is_realtime < 0) &&
813                         current->transition == is_transition &&
814                         current->theme == is_theme)
815                         results.append(current);
816         }
817
818 // Alphabetize list by title
819         int done = 0;
820         while(!done)
821         {
822                 done = 1;
823
824                 for(int i = 0; i < results.total - 1; i++)
825                 {
826                         PluginServer *value1 = results[i];
827                         PluginServer *value2 = results[i + 1];
828                         if(strcmp(_(value1->title), _(value2->title)) > 0)
829                         {
830                                 done = 0;
831                                 results[i] = value2;
832                                 results[i + 1] = value1;
833                         }
834                 }
835         }
836 }
837
838 PluginServer* MWindow::scan_plugindb(char *title,
839                 int data_type)
840 {
841 //      if(data_type < 0)
842 //      {
843 //              printf("MWindow::scan_plugindb data_type < 0\n");
844 //              return 0;
845 //      }
846
847         for(int i = 0; i < plugindb->total; i++)
848         {
849                 PluginServer *server = plugindb->get(i);
850                 if(server->title &&
851                         !strcasecmp(server->title, title) &&
852                         (data_type < 0 ||
853                                 (data_type == TRACK_AUDIO && server->audio) ||
854                                 (data_type == TRACK_VIDEO && server->video)))
855                         return plugindb->get(i);
856         }
857         return 0;
858 }
859
860 // repair session files with xlated plugin titles
861 void MWindow::fix_plugin_title(char *title)
862 {
863         for(int i = 0; i < plugindb->total; i++) {
864                 PluginServer *server = plugindb->get(i);
865                 if( !server->title ) continue;
866                 const char *server_title = server->title;
867                 if( !bstrcasecmp(title, _(server_title)) ) {
868                         strcpy(title, server_title);
869                         return;
870                 }
871         }
872 }
873
874 int MWindow::plugin_exists(const char *plugin_path, ArrayList<PluginServer*> &plugins)
875 {
876         for( int i=0; i<plugins.size(); ++i )
877                 if( !strcmp(plugin_path, plugins[i]->get_path()) ) return 1;
878         return 0;
879 }
880
881 int MWindow::plugin_exists(char *plugin_path)
882 {
883         return !plugindb ? 0 : plugin_exists(plugin_path, *plugindb);
884 }
885
886 void MWindow::remove_plugin_index()
887 {
888         char index_path[BCTEXTLEN];
889         MWindow::create_defaults_path(index_path, PLUGIN_FILE);
890         ::remove(index_path);
891 }
892
893 void MWindow::init_preferences()
894 {
895         preferences = new Preferences;
896         preferences->load_defaults(defaults);
897         const char *lv2_path = getenv("LV2_PATH");
898         if( lv2_path && strcmp(lv2_path, preferences->lv2_path) ) {
899                 strncpy(preferences->lv2_path, lv2_path, sizeof(preferences->lv2_path));
900                 remove_plugin_index();
901         }
902         else if( !lv2_path && preferences->lv2_path[0] ) {
903                 File::setenv_path("LV2_PATH",preferences->lv2_path, 0);
904         }
905         session = new MainSession(this);
906         session->load_defaults(defaults);
907         // set x11_host, screens, window_config
908         screens = session->set_default_x11_host();
909         BC_Signals::set_trap_hook(trap_hook, this);
910         BC_Signals::set_catch_segv(preferences->trap_sigsegv);
911         BC_Signals::set_catch_intr(preferences->trap_sigintr);
912         if( preferences->trap_sigsegv || preferences->trap_sigintr ) {
913                 BC_Trace::enable_locks();
914         }
915         else {
916                 BC_Trace::disable_locks();
917         }
918         BC_WindowBase::get_resources()->popupmenu_btnup = preferences->popupmenu_btnup;
919         BC_WindowBase::get_resources()->textbox_focus_policy = preferences->textbox_focus_policy;
920         BC_WindowBase::get_resources()->grab_input_focus = preferences->grab_input_focus;
921         YUV::yuv.yuv_set_colors(preferences->yuv_color_space, preferences->yuv_color_range);
922 }
923
924 void MWindow::clean_indexes()
925 {
926         FileSystem fs;
927         int total_excess;
928         long oldest = 0;
929         int oldest_item = -1;
930         int result;
931         char string[BCTEXTLEN];
932         char string2[BCTEXTLEN];
933
934 // Delete extra indexes
935         fs.set_filter("*.idx");
936         fs.complete_path(preferences->index_directory);
937         fs.update(preferences->index_directory);
938 //printf("MWindow::clean_indexes 1 %d\n", fs.dir_list.total);
939
940 // Eliminate directories
941         result = 1;
942         while(result)
943         {
944                 result = 0;
945                 for(int i = 0; i < fs.dir_list.total && !result; i++)
946                 {
947                         fs.join_names(string, preferences->index_directory, fs.dir_list[i]->name);
948                         if(fs.is_dir(string))
949                         {
950                                 delete fs.dir_list[i];
951                                 fs.dir_list.remove_number(i);
952                                 result = 1;
953                         }
954                 }
955         }
956         total_excess = fs.dir_list.total - preferences->index_count;
957
958 //printf("MWindow::clean_indexes 2 %d\n", fs.dir_list.total);
959         while(total_excess > 0)
960         {
961 // Get oldest
962                 for(int i = 0; i < fs.dir_list.total; i++)
963                 {
964                         fs.join_names(string, preferences->index_directory, fs.dir_list[i]->name);
965
966                         if(i == 0 || fs.get_date(string) <= oldest)
967                         {
968                                 oldest = fs.get_date(string);
969                                 oldest_item = i;
970                         }
971                 }
972
973                 if(oldest_item >= 0)
974                 {
975 // Remove index file
976                         fs.join_names(string,
977                                 preferences->index_directory,
978                                 fs.dir_list[oldest_item]->name);
979 //printf("MWindow::clean_indexes 1 %s\n", string);
980                         if(remove(string))
981                                 perror("delete_indexes");
982                         delete fs.dir_list[oldest_item];
983                         fs.dir_list.remove_number(oldest_item);
984
985 // Remove table of contents if it exists
986                         strcpy(string2, string);
987                         char *ptr = strrchr(string2, '.');
988                         if(ptr)
989                         {
990 //printf("MWindow::clean_indexes 2 %s\n", string2);
991                                 sprintf(ptr, ".toc");
992                                 remove(string2);
993                                 sprintf(ptr, ".mkr");
994                                 remove(string2);
995                         }
996                 }
997
998                 total_excess--;
999         }
1000 }
1001
1002 void MWindow::init_awindow()
1003 {
1004         awindow = new AWindow(this);
1005         awindow->create_objects();
1006 }
1007
1008 void MWindow::init_gwindow()
1009 {
1010         gwindow = new GWindow(this);
1011         gwindow->create_objects();
1012 }
1013
1014 void MWindow::init_tipwindow()
1015 {
1016         TipWindow::load_tips(cin_lang);
1017         if( !twindow )
1018                 twindow = new TipWindow(this);
1019         twindow->start();
1020 }
1021
1022 void MWindow::show_warning(int *do_warning, const char *text)
1023 {
1024         if( do_warning && !*do_warning ) return;
1025         if( !wwindow )
1026                 wwindow = new WWindow(this);
1027         wwindow->show_warning(do_warning, text);
1028 }
1029
1030 int MWindow::wait_warning()
1031 {
1032         return wwindow->wait_result();
1033 }
1034
1035 void MWindow::init_theme()
1036 {
1037         Timer timer;
1038         theme = 0;
1039
1040         PluginServer *theme_plugin = 0;
1041         for(int i = 0; i < plugindb->total && !theme_plugin; i++) {
1042                 if( plugindb->get(i)->theme &&
1043                     !strcasecmp(preferences->theme, plugindb->get(i)->title) )
1044                         theme_plugin = plugindb->get(i);
1045         }
1046
1047         if( !theme_plugin )
1048                 fprintf(stderr, _("MWindow::init_theme: prefered theme %s not found.\n"),
1049                          preferences->theme);
1050
1051         const char *default_theme = DEFAULT_THEME;
1052         if( !theme_plugin && strcasecmp(preferences->theme, default_theme) ) {
1053                 fprintf(stderr, _("MWindow::init_theme: trying default theme %s\n"),
1054                         default_theme);
1055                 for(int i = 0; i < plugindb->total && !theme_plugin; i++) {
1056                         if( plugindb->get(i)->theme &&
1057                             !strcasecmp(default_theme, plugindb->get(i)->title) )
1058                                 theme_plugin = plugindb->get(i);
1059                 }
1060         }
1061
1062         if(!theme_plugin) {
1063                 fprintf(stderr, _("MWindow::init_theme: theme_plugin not found.\n"));
1064                 exit(1);
1065         }
1066
1067         PluginServer *plugin = new PluginServer(*theme_plugin);
1068         if( plugin->open_plugin(0, preferences, 0, 0) ) {
1069                 fprintf(stderr, _("MWindow::init_theme: unable to load theme %s\n"),
1070                         theme_plugin->title);
1071                 exit(1);
1072         }
1073
1074         theme = plugin->new_theme();
1075         theme->mwindow = this;
1076         strcpy(theme->path, plugin->path);
1077         delete plugin;
1078
1079 // Load default images & settings
1080         theme->Theme::initialize();
1081 // Load user images & settings
1082         theme->initialize();
1083 // Create menus with user colors
1084         theme->build_menus();
1085         init_menus();
1086
1087         theme->sort_image_sets();
1088         theme->check_used();
1089 //printf("MWindow::init_theme %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1090 }
1091
1092 void MWindow::init_3d()
1093 {
1094         playback_3d = new Playback3D(this);
1095         playback_3d->create_objects();
1096 }
1097
1098 void MWindow::init_edl()
1099 {
1100         edl = new EDL;
1101         edl->create_objects();
1102         fill_preset_defaults(default_standard, edl->session);
1103         edl->load_defaults(defaults);
1104         edl->session->brender_start = edl->session->brender_end = 0;
1105         edl->create_default_tracks();
1106         edl->tracks->update_y_pixels(theme);
1107 }
1108
1109 void MWindow::init_compositor()
1110 {
1111         cwindow = new CWindow(this);
1112         cwindow->create_objects();
1113 }
1114
1115 void MWindow::init_levelwindow()
1116 {
1117         lwindow = new LevelWindow(this);
1118         lwindow->create_objects();
1119 }
1120
1121 VWindow *MWindow::get_viewer(int start_it, int idx)
1122 {
1123         vwindows_lock->lock("MWindow::get_viewer");
1124         VWindow *vwindow = idx >= 0 && idx < vwindows.size() ? vwindows[idx] : 0;
1125         if( !vwindow ) idx = vwindows.size();
1126         while( !vwindow && --idx >= 0 ) {
1127                 VWindow *vwin = vwindows[idx];
1128                 if( !vwin->is_running() || !vwin->get_edl() )
1129                         vwindow = vwin;
1130         }
1131         if( !vwindow ) {
1132                 vwindow = new VWindow(this);
1133                 vwindow->load_defaults();
1134                 vwindow->create_objects();
1135                 vwindows.append(vwindow);
1136         }
1137         vwindows_lock->unlock();
1138         if( start_it ) vwindow->start();
1139         return vwindow;
1140 }
1141
1142 ZWindow *MWindow::get_mixer(Mixer *&mixer)
1143 {
1144         zwindows_lock->lock("MWindow::get_mixer");
1145         if( !mixer ) mixer = edl->mixers.new_mixer();
1146         ZWindow *zwindow = 0;
1147         for( int i=0; !zwindow && i<zwindows.size(); ++i )
1148                 if( zwindows[i]->idx < 0 ) zwindow = zwindows[i];
1149         if( !zwindow )
1150                 zwindows.append(zwindow = new ZWindow(this));
1151         zwindow->idx = mixer->idx;
1152         zwindows_lock->unlock();
1153         return zwindow;
1154 }
1155
1156 void MWindow::del_mixer(ZWindow *zwindow)
1157 {
1158         zwindows_lock->lock("MWindow::del_mixer 0");
1159         edl->mixers.del_mixer(zwindow->idx);
1160         if( session->selected_zwindow >= 0 ) {
1161                 int i = zwindows.number_of(zwindow);
1162                 if( i >= 0 && i < session->selected_zwindow )
1163                         --session->selected_zwindow;
1164                 else if( i == session->selected_zwindow )
1165                         session->selected_zwindow = -1;
1166         }
1167         zwindows_lock->unlock();
1168         gui->lock_window("MWindow::del_mixer 1");
1169         gui->update_mixers(0, -1);
1170         gui->unlock_window();
1171 }
1172
1173 void MWindow::start_mixer()
1174 {
1175         Mixer *mixer = 0;
1176         ZWindow *zwindow = get_mixer(mixer);
1177         const char *title = 0;
1178
1179         for( Track *track=edl->tracks->first; track!=0; track=track->next ) {
1180                 PatchGUI *patchgui = get_patchgui(track);
1181                 if( !patchgui || !patchgui->mixer ) continue;
1182                 mixer->mixer_ids.append(track->get_mixer_id());
1183                 if( !title ) title = track->title;
1184         }
1185
1186         session->selected_zwindow = -1;
1187         gui->lock_window("MWindow::start_mixer");
1188         gui->update_mixers(0, 0);
1189         gui->unlock_window();
1190
1191         zwindow->set_title(title);
1192         zwindow->start();
1193         refresh_mixers();
1194 }
1195
1196 int MWindow::mixer_track_active(Track *track)
1197 {
1198         int i = session->selected_zwindow;
1199         if( i < 0 || i >= zwindows.size() ) return 0;
1200         ZWindow *zwindow = zwindows[i];
1201         Mixer *mixer = edl->mixers.get_mixer(zwindow->idx);
1202         if( !mixer ) return 0;
1203         int n = mixer->mixer_ids.number_of(track->get_mixer_id());
1204         return n >= 0 ? 1 : 0;
1205 }
1206
1207 void MWindow::update_mixer_tracks()
1208 {
1209         zwindows_lock->lock("MixPatch::handle_event");
1210         int i = session->selected_zwindow;
1211         if( i >= 0 && i < zwindows.size() ) {
1212                 ZWindow *zwindow = zwindows[i];
1213                 zwindow->update_mixer_ids();
1214         }
1215         zwindows_lock->unlock();
1216 }
1217
1218 void MWindow::queue_mixers(EDL *edl, int command, int wait_tracking,
1219                 int use_inout, int update_refresh, int toggle_audio, int loop_play)
1220 {
1221         zwindows_lock->lock("MWindow::queue_mixers");
1222         for( int vidx=0; vidx<zwindows.size(); ++vidx ) {
1223                 ZWindow *zwindow = zwindows[vidx];
1224                 if( zwindow->idx < 0 ) continue;
1225                 Mixer *mixer = edl->mixers.get_mixer(zwindow->idx);
1226                 if( !mixer || !mixer->mixer_ids.size() ) continue;
1227                 int k = -1;
1228                 for( Track *track = edl->tracks->first; k<0 && track!=0; track=track->next ) {
1229                         if( track->data_type != TRACK_VIDEO ) continue;
1230                         int mixer_id = track->get_mixer_id();
1231                         k = mixer->mixer_ids.size();
1232                         while( --k >= 0 && mixer_id != mixer->mixer_ids[k] );
1233                 }
1234                 if( k < 0 ) continue;
1235                 EDL *mixer_edl = new EDL(this->edl);
1236                 mixer_edl->create_objects();
1237                 mixer_edl->copy_all(edl);
1238                 mixer_edl->remove_vwindow_edls();
1239                 for( Track *track = mixer_edl->tracks->first; track!=0; track=track->next ) {
1240                         k = mixer->mixer_ids.size();
1241                         while( --k >= 0 && track->get_mixer_id() != mixer->mixer_ids[k] );
1242                         if( k >= 0 ) {
1243                                 track->record = 1;
1244                                 track->play = track->data_type == TRACK_VIDEO ? 1 : 0;
1245                         }
1246                         else
1247                                 track->record = track->play = 0;
1248                 }
1249                 zwindow->change_source(mixer_edl);
1250                 zwindow->issue_command(command,
1251                         wait_tracking, use_inout, update_refresh, toggle_audio, loop_play);
1252         }
1253         zwindows_lock->unlock();
1254 }
1255
1256 void MWindow::refresh_mixers(int dir)
1257 {
1258         int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
1259         queue_mixers(edl,command,0,0,1,0,0);
1260 }
1261
1262 void MWindow::stop_mixers()
1263 {
1264         for( int vidx=0; vidx<zwindows.size(); ++vidx ) {
1265                 ZWindow *zwindow = zwindows[vidx];
1266                 if( zwindow->idx < 0 ) continue;
1267                 zwindow->issue_command(STOP, 0, 0, 0, 0, 0);
1268         }
1269 }
1270
1271 void MWindow::close_mixers(int destroy)
1272 {
1273         zwindows_lock->lock("MWindow::close_mixers");
1274         for( int i=zwindows.size(); --i>=0; ) {
1275                 ZWindow *zwindow = zwindows[i];
1276                 if( zwindow->idx < 0 ) continue;
1277                 zwindow->destroy = destroy;
1278                 ZWindowGUI *zgui = zwindow->zgui;
1279                 zgui->lock_window("MWindow::select_zwindow 0");
1280                 zgui->set_done(0);
1281                 zgui->unlock_window();
1282         }
1283         zwindows_lock->unlock();
1284         for( int i=0; i<zwindows.size(); ++i ) {
1285                 ZWindow *zwindow = zwindows[i];
1286                 if( zwindow->idx < 0 ) continue;
1287                 zwindow->close_window();
1288         }
1289 }
1290
1291 ZWindow *MWindow::create_mixer(Indexable *indexable)
1292 {
1293         ArrayList<Indexable*> new_assets;
1294         new_assets.append(indexable);
1295         Track *track = edl->tracks->last;
1296         load_assets(&new_assets, 0, LOADMODE_NEW_TRACKS, 0, 0, 0, 0, 0, 0);
1297         track = !track ? edl->tracks->first : track->next;
1298         Mixer *mixer = 0;
1299         ZWindow *zwindow = get_mixer(mixer);
1300         while( track ) {
1301                 track->play = track->record = 0;
1302                 if( track->data_type == TRACK_VIDEO ) {
1303                         sprintf(track->title, _("Mixer %d"), zwindow->idx);
1304                 }
1305                 mixer->mixer_ids.append(track->get_mixer_id());
1306                 track = track->next;
1307         }
1308         if(  indexable->is_asset ) {
1309                 char *path = indexable->path;
1310                 char *tp = strrchr(path, '/');
1311                 if( !tp ) tp = path; else ++tp;
1312                 zwindow->set_title(tp);
1313         }
1314         else {
1315                 char *title = ((EDL*)indexable)->local_session->clip_title;
1316                 zwindow->set_title(title);
1317         }
1318         return zwindow;
1319 }
1320
1321 void MWindow::create_mixers()
1322 {
1323         if( !session->drag_assets->size() &&
1324             !session->drag_clips->size() ) return;
1325         undo->update_undo_before();
1326
1327         select_zwindow(0);
1328         ArrayList<ZWindow *>new_mixers;
1329
1330         for( int i=0; i<session->drag_assets->size(); ++i ) {
1331                 Indexable *indexable = session->drag_assets->get(i);
1332                 if( !indexable->have_video() ) continue;
1333                 ZWindow *zwindow = create_mixer(indexable);
1334                 new_mixers.append(zwindow);
1335         }
1336         for( int i=0; i<session->drag_clips->size(); ++i ) {
1337                 Indexable *indexable = (Indexable*)session->drag_clips->get(i);
1338                 if( !indexable->have_video() ) continue;
1339                 ZWindow *zwindow = create_mixer(indexable);
1340                 new_mixers.append(zwindow);
1341         }
1342
1343         tile_mixers();
1344         for( int i=0; i<new_mixers.size(); ++i )
1345                 new_mixers[i]->start();
1346
1347         refresh_mixers();
1348         save_backup();
1349         undo->update_undo_after(_("create mixers"), LOAD_ALL);
1350         restart_brender();
1351         gui->update(1, 2, 1, 1, 1, 1, 0);
1352         sync_parameters(CHANGE_ALL);
1353 }
1354
1355 void MWindow::open_mixers()
1356 {
1357         for( int i=0; i<edl->mixers.size(); ++i ) {
1358                 Mixer *mixer = edl->mixers[i];
1359                 ZWindow *zwindow = get_mixer(mixer);
1360                 zwindow->set_title(mixer->title);
1361                 zwindow->start();
1362         }
1363         refresh_mixers();
1364 }
1365
1366 int MWindow::select_zwindow(ZWindow *zwindow)
1367 {
1368         int ret = 0, n = zwindows.number_of(zwindow);
1369         if( session->selected_zwindow != n ) {
1370                 session->selected_zwindow = n;
1371                 for( int i=0; i<zwindows.size(); ++i ) {
1372                         ZWindow *zwindow = zwindows[i];
1373                         if( zwindow->idx < 0 ) continue;
1374                         ZWindowGUI *zgui = zwindow->zgui;
1375                         zgui->lock_window("MWindow::select_zwindow 0");
1376                         zwindow->highlighted = i == n ? 1 : 0;
1377                         if( zgui->draw_overlays() )
1378                                 zgui->canvas->get_canvas()->flash(1);
1379                         zgui->unlock_window();
1380                 }
1381                 ret = 1;
1382                 gui->lock_window("MWindow::select_window 1");
1383                 gui->update_mixers(0, -1);
1384                 gui->unlock_window();
1385         }
1386         return ret;
1387 }
1388
1389 void MWindow::tile_mixers()
1390 {
1391         int nz = 0;
1392         for( int i=0; i<zwindows.size(); ++i ) {
1393                 ZWindow *zwindow = zwindows[i];
1394                 if( zwindow->idx < 0 ) continue;
1395                 ++nz;
1396         }
1397         if( !nz ) return;
1398         int zn = ceil(sqrt(nz));
1399         int x1 = 1 + gui->get_x(), x2 = cwindow->gui->get_x();
1400         int y1 = 1, y2 = gui->get_y();
1401         int rw = gui->get_root_w(0), rh = gui->get_root_h(0);
1402         if( x1 < 0 ) x1 = 0;
1403         if( y1 < 0 ) y1 = 0;
1404         if( x2 > rw ) x2 = rw;
1405         if( y2 > rh ) y2 = rh;
1406         int dx = x2 - x1, dy = y2 - y1;
1407         int zw = dx / zn;
1408         int lt = BC_DisplayInfo::get_left_border();
1409         int top = BC_DisplayInfo::get_top_border();
1410         int bw = lt + BC_DisplayInfo::get_right_border();  // borders
1411         int bh = top + BC_DisplayInfo::get_bottom_border();
1412         int zx = 0, zy = 0;  // window origins
1413         int mw = 10+10, mh = 10+10; // canvas margins
1414         int rsz = 0, n = 0, dz = 0;
1415         int ow = edl->session->output_w, oh = edl->session->output_h;
1416         for( int i=0; i<zwindows.size(); ++i ) {
1417                 ZWindow *zwindow = zwindows[i];
1418                 if( zwindow->idx < 0 ) continue;
1419                 int ww = zw - bw, hh = (ww - mw) * oh / ow + mh, zh = hh + bh;
1420                 if( rsz < hh ) rsz = hh;
1421                 int xx = zx + x1, yy = zy + y1;
1422                 int mx = x2 - zw, my = y2 - zh;
1423                 if( xx > mx ) xx = mx;
1424                 if( yy > my ) yy = my;
1425                 xx += lt + dz;  yy += top + dz;
1426                 zwindow->reposition(xx,yy, ww,hh);
1427                 if( zwindow->running() ) {
1428                         ZWindowGUI *gui = (ZWindowGUI *)zwindow->get_gui();
1429                         gui->lock_window("MWindow::tile_mixers");
1430                         gui->BC_WindowBase::reposition_window(xx,yy, ww,hh);
1431                         gui->unlock_window();
1432                 }
1433                 if( ++n >= zn ) {
1434                         n = 0;  rsz += bh;
1435                         if( (zy += rsz) > (dy - rsz) ) dz += 10;
1436                         rsz = 0;
1437                         zx = 0;
1438                 }
1439                 else
1440                         zx += zw;
1441         }
1442 }
1443
1444 void MWindow::init_cache()
1445 {
1446         audio_cache = new CICache(preferences);
1447         video_cache = new CICache(preferences);
1448         frame_cache = new FrameCache;
1449         wave_cache = new WaveCache;
1450 }
1451
1452 void MWindow::init_channeldb()
1453 {
1454         channeldb_buz->load("channeldb_buz");
1455         channeldb_v4l2jpeg->load("channeldb_v4l2jpeg");
1456 }
1457
1458 void MWindow::init_menus()
1459 {
1460         char string[BCTEXTLEN];
1461
1462         // Color Models
1463         BC_CModels::to_text(string, BC_RGB888);
1464         colormodels.append(new ColormodelItem(string, BC_RGB888));
1465         BC_CModels::to_text(string, BC_RGBA8888);
1466         colormodels.append(new ColormodelItem(string, BC_RGBA8888));
1467 //      BC_CModels::to_text(string, BC_RGB161616);
1468 //      colormodels.append(new ColormodelItem(string, BC_RGB161616));
1469 //      BC_CModels::to_text(string, BC_RGBA16161616);
1470 //      colormodels.append(new ColormodelItem(string, BC_RGBA16161616));
1471         BC_CModels::to_text(string, BC_RGB_FLOAT);
1472         colormodels.append(new ColormodelItem(string, BC_RGB_FLOAT));
1473         BC_CModels::to_text(string, BC_RGBA_FLOAT);
1474         colormodels.append(new ColormodelItem(string, BC_RGBA_FLOAT));
1475         BC_CModels::to_text(string, BC_YUV888);
1476         colormodels.append(new ColormodelItem(string, BC_YUV888));
1477         BC_CModels::to_text(string, BC_YUVA8888);
1478         colormodels.append(new ColormodelItem(string, BC_YUVA8888));
1479 //      BC_CModels::to_text(string, BC_YUV161616);
1480 //      colormodels.append(new ColormodelItem(string, BC_YUV161616));
1481 //      BC_CModels::to_text(string, BC_YUVA16161616);
1482 //      colormodels.append(new ColormodelItem(string, BC_YUVA16161616));
1483
1484 #define ILACEPROJECTMODELISTADD(x) ilacemode_to_text(string, x); \
1485                            interlace_project_modes.append(new InterlacemodeItem(string, x));
1486
1487 #define ILACEASSETMODELISTADD(x) ilacemode_to_text(string, x); \
1488                            interlace_asset_modes.append(new InterlacemodeItem(string, x));
1489
1490 #define ILACEFIXMETHODLISTADD(x) ilacefixmethod_to_text(string, x); \
1491                            interlace_asset_fixmethods.append(new InterlacefixmethodItem(string, x));
1492
1493         // Interlacing Modes
1494         ILACEASSETMODELISTADD(ILACE_MODE_UNDETECTED); // Not included in the list for the project options.
1495
1496         ILACEASSETMODELISTADD(ILACE_MODE_TOP_FIRST);
1497         ILACEPROJECTMODELISTADD(ILACE_MODE_TOP_FIRST);
1498
1499         ILACEASSETMODELISTADD(ILACE_MODE_BOTTOM_FIRST);
1500         ILACEPROJECTMODELISTADD(ILACE_MODE_BOTTOM_FIRST);
1501
1502         ILACEASSETMODELISTADD(ILACE_MODE_NOTINTERLACED);
1503         ILACEPROJECTMODELISTADD(ILACE_MODE_NOTINTERLACED);
1504
1505         // Interlacing Fixing Methods
1506         ILACEFIXMETHODLISTADD(ILACE_FIXMETHOD_NONE);
1507         ILACEFIXMETHODLISTADD(ILACE_FIXMETHOD_UPONE);
1508         ILACEFIXMETHODLISTADD(ILACE_FIXMETHOD_DOWNONE);
1509 }
1510
1511 void MWindow::init_indexes()
1512 {
1513         mainindexes = new MainIndexes(this);
1514         mainindexes->start_loop();
1515 }
1516
1517 void MWindow::init_gui()
1518 {
1519         gui = new MWindowGUI(this);
1520         gui->lock_window("MWindow::init_gui");
1521         gui->create_objects();
1522         gui->unlock_window();
1523         gui->load_defaults(defaults);
1524 }
1525
1526 void MWindow::init_signals()
1527 {
1528         sighandler = new SigHandler;
1529         sighandler->initialize("/tmp/cinelerra_%d.dmp");
1530 ENABLE_BUFFER
1531 }
1532
1533 void MWindow::init_render()
1534 {
1535         render = new Render(this);
1536         create_bd = new CreateBD_Thread(this);
1537         create_dvd = new CreateDVD_Thread(this);
1538         batch_render = new BatchRenderThread(this);
1539 }
1540
1541 void MWindow::init_exportedl()
1542 {
1543         exportedl = new ExportEDL(this);
1544 }
1545
1546 void MWindow::init_brender()
1547 {
1548         if(preferences->use_brender && !brender)
1549         {
1550                 brender_lock->lock("MWindow::init_brender 1");
1551                 brender = new BRender(this);
1552                 brender->initialize();
1553                 session->brender_end = 0;
1554                 brender_lock->unlock();
1555         }
1556         else
1557         if(!preferences->use_brender && brender)
1558         {
1559                 brender_lock->lock("MWindow::init_brender 2");
1560                 delete brender;
1561                 brender = 0;
1562                 session->brender_end = 0;
1563                 brender_lock->unlock();
1564         }
1565         brender_active = 0;
1566         stop_brender();
1567 }
1568
1569 void MWindow::restart_brender()
1570 {
1571 //printf("MWindow::restart_brender 1\n");
1572         if( !brender_active || !preferences->use_brender ) return;
1573         if( !brender ) return;
1574         brender->restart(edl);
1575 }
1576
1577 void MWindow::stop_brender()
1578 {
1579         if( !brender ) return;
1580 // cannot be holding mwindow->gui display lock
1581         brender->stop();
1582 }
1583
1584 int MWindow::brender_available(int position)
1585 {
1586         int result = 0;
1587         brender_lock->lock("MWindow::brender_available 1");
1588         if(brender && brender_active)
1589         {
1590                 if(brender->map_valid)
1591                 {
1592                         brender->map_lock->lock("MWindow::brender_available 2");
1593                         if(position < brender->map_size &&
1594                                 position >= 0)
1595                         {
1596 //printf("MWindow::brender_available 1 %d %d\n", position, brender->map[position]);
1597                                 if(brender->map[position] == BRender::RENDERED)
1598                                         result = 1;
1599                         }
1600                         brender->map_lock->unlock();
1601                 }
1602         }
1603         brender_lock->unlock();
1604         return result;
1605 }
1606
1607 void MWindow::set_brender_active(int v, int update)
1608 {
1609         if( !preferences->use_brender ) v = 0;
1610         brender_active = v;
1611         gui->mainmenu->brender_active->set_checked(v);
1612         if( v != 0 ) {
1613                 edl->session->brender_start = edl->local_session->get_selectionstart(1);
1614                 edl->session->brender_end = edl->local_session->get_selectionend(1);
1615
1616                 if(EQUIV(edl->session->brender_end, edl->session->brender_start)) {
1617                         edl->session->brender_end = edl->tracks->total_video_length();
1618                 }
1619                 restart_brender();
1620         }
1621         else {
1622                 edl->session->brender_start = edl->session->brender_end = 0;
1623                 gui->unlock_window();
1624                 stop_brender();
1625                 gui->lock_window("MWindow::set_brender_active");
1626         }
1627         if( update ) {
1628                 gui->update_timebar(0);
1629                 gui->draw_overlays(1);
1630         }
1631 }
1632
1633 int MWindow::has_commercials()
1634 {
1635 #ifdef HAVE_COMMERCIAL
1636         return theme->use_commercials;
1637 #else
1638         return 0;
1639 #endif
1640 }
1641
1642 void MWindow::init_commercials()
1643 {
1644 #ifdef HAVE_COMMERCIAL
1645         if( !commercials ) {
1646                 commercials = new Commercials(this);
1647                 commercial_active = 0;
1648         }
1649         else
1650                 commercials->add_user();
1651 #endif
1652 }
1653
1654 void MWindow::commit_commercial()
1655 {
1656 #ifdef HAVE_COMMERCIAL
1657         if( !commercial_active ) return;
1658         commercial_active = 0;
1659         if( !commercials ) return;
1660         commercials->commitDb();
1661 #endif
1662 }
1663
1664 void MWindow::undo_commercial()
1665 {
1666 #ifdef HAVE_COMMERCIAL
1667         if( !commercial_active ) return;
1668         commercial_active = 0;
1669         if( !commercials ) return;
1670         commercials->undoDb();
1671 #endif
1672 }
1673
1674 int MWindow::put_commercial()
1675 {
1676         int result = 0;
1677 #ifdef HAVE_COMMERCIAL
1678         double start = edl->local_session->get_selectionstart();
1679         double end = edl->local_session->get_selectionend();
1680         if( start >= end ) return 0;
1681
1682         const char *errmsg = 0;
1683         int count = 0;
1684         Tracks *tracks = edl->tracks;
1685         //check it
1686         for(Track *track=tracks->first; track && !errmsg; track=track->next) {
1687                 if( track->data_type != TRACK_VIDEO ) continue;
1688                 if( !track->record ) continue;
1689                 if( count > 0 ) { errmsg = _("multiple video tracks"); break; }
1690                 ++count;
1691                 int64_t units_start = track->to_units(start,0);
1692                 int64_t units_end = track->to_units(end,0);
1693                 Edits *edits = track->edits;
1694                 Edit *edit1 = edits->editof(units_start, PLAY_FORWARD, 0);
1695                 Edit *edit2 = edits->editof(units_end, PLAY_FORWARD, 0);
1696                 if(!edit1 && !edit2) continue;  // nothing selected
1697                 if(!edit2) {                    // edit2 beyond end of track
1698                         edit2 = edits->last;
1699                         units_end = edits->length();
1700                 }
1701                 if(edit1 != edit2) { errmsg = _("crosses edits"); break; }
1702                 Indexable *indexable = edit1->get_source();
1703                 if( !indexable->is_asset ) { errmsg = _("not asset"); break; }
1704         }
1705         //run it
1706         for(Track *track=tracks->first; track && !errmsg; track=track->next) {
1707                 if( track->data_type != TRACK_VIDEO ) continue;
1708                 if( !track->record ) continue;
1709                 int64_t units_start = track->to_units(start,0);
1710                 int64_t units_end = track->to_units(end,0);
1711                 Edits *edits = track->edits;
1712                 Edit *edit1 = edits->editof(units_start, PLAY_FORWARD, 0);
1713                 Edit *edit2 = edits->editof(units_end, PLAY_FORWARD, 0);
1714                 if(!edit1 && !edit2) continue;  // nothing selected
1715                 if(!edit2) {                    // edit2 beyond end of track
1716                         edit2 = edits->last;
1717                         units_end = edits->length();
1718                 }
1719                 Indexable *indexable = edit1->get_source();
1720                 Asset *asset = (Asset *)indexable;
1721                 File *file = video_cache->check_out(asset, edl);
1722                 if( !file ) { errmsg = _("no file"); break; }
1723                 int64_t edit_length = units_end - units_start;
1724                 int64_t edit_start = units_start - edit1->startproject + edit1->startsource;
1725                 result = commercials->put_clip(file, edit1->channel,
1726                         track->from_units(edit_start), track->from_units(edit_length));
1727                 video_cache->check_in(asset);
1728                 if( result ) { errmsg = _("db failed"); break; }
1729         }
1730         if( errmsg ) {
1731                 char string[BCTEXTLEN];
1732                 sprintf(string, _("put_commercial: %s"), errmsg);
1733                 MainError::show_error(string);
1734                 undo_commercial();
1735                 result = 1;
1736         }
1737 #endif
1738         return result;
1739 }
1740
1741 void MWindow::stop_playback(int wait)
1742 {
1743         gui->stop_drawing();
1744
1745         cwindow->stop_playback(wait);
1746
1747         for(int i = 0; i < vwindows.size(); i++) {
1748                 VWindow *vwindow = vwindows[i];
1749                 if( !vwindow->is_running() ) continue;
1750                 vwindow->stop_playback(wait);
1751         }
1752         for(int i = 0; i < zwindows.size(); i++) {
1753                 ZWindow *zwindow = zwindows[i];
1754                 if( zwindow->idx < 0 ) continue;
1755                 zwindow->stop_playback(wait);
1756         }
1757 }
1758
1759 void MWindow::stop_transport()
1760 {
1761         gui->stop_transport(gui->get_window_lock() ? "MWindow::stop_transport" : 0);
1762 }
1763
1764 void MWindow::beep(double freq, double secs, double gain)
1765 {
1766         if( !proxy_beep ) proxy_beep = new ProxyBeep(this);
1767         proxy_beep->tone(freq, secs, gain);
1768 }
1769
1770 int MWindow::load_filenames(ArrayList<char*> *filenames,
1771         int load_mode,
1772         int update_filename)
1773 {
1774         ArrayList<EDL*> new_edls;
1775         ArrayList<Asset*> new_assets;
1776         ArrayList<File*> new_files;
1777         const int debug = 0;
1778 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1779
1780 //      save_defaults();
1781         gui->start_hourglass();
1782
1783 // Need to stop playback since tracking depends on the EDL not getting
1784 // deleted.
1785         gui->unlock_window();
1786         stop_playback(1);
1787         gui->lock_window("MWindow::load_filenames 0");
1788
1789 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1790         undo->update_undo_before();
1791
1792
1793 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1794
1795 // Define new_edls and new_assets to load
1796         int result = 0, ftype = -1;
1797
1798         for( int i=0; i<filenames->size(); ++i ) {
1799 // Get type of file
1800                 File *new_file = new File;
1801                 Asset *new_asset = new Asset(filenames->get(i));
1802                 EDL *new_edl = new EDL;
1803                 char string[BCTEXTLEN];
1804
1805                 new_edl->create_objects();
1806                 new_edl->copy_session(edl);
1807                 new_file->set_program(edl->session->program_no);
1808
1809                 sprintf(string, _("Loading %s"), new_asset->path);
1810                 gui->show_message(string);
1811 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1812
1813                 ftype = new_file->open_file(preferences, new_asset, 1, 0);
1814 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1815
1816                 result = 1;
1817                 switch( ftype ) {
1818 // Convert media file to EDL
1819                 case FILE_OK:
1820 // Warn about odd image dimensions
1821                         if( new_asset->video_data &&
1822                             ((new_asset->width % 2) || (new_asset->height % 2)) ) {
1823                                 char string[BCTEXTLEN];
1824                                 sprintf(string, _("%s's resolution is %dx%d.\nImages with odd dimensions may not decode properly."),
1825                                         new_asset->path, new_asset->width, new_asset->height);
1826                                 MainError::show_error(string);
1827                         }
1828
1829                         if( new_asset->program >= 0 &&
1830                             edl->session->program_no != new_asset->program ) {
1831                                 char string[BCTEXTLEN];
1832                                 sprintf(string, _("%s's index was built for program number %d\n"
1833                                         "Playback preference is %d.\n  Using program %d."),
1834                                         new_asset->path, new_asset->program,
1835                                         edl->session->program_no, new_asset->program);
1836                                 MainError::show_error(string);
1837                         }
1838
1839                         if( load_mode != LOADMODE_RESOURCESONLY ) {
1840                                 RecordLabels *labels = edl->session->label_cells ?
1841                                         new RecordLabels(new_file) : 0;
1842                                 asset_to_edl(new_edl, new_asset, labels);
1843                                 new_edls.append(new_edl);
1844                                 new_edl->add_user();
1845                                 delete labels;
1846                         }
1847                         else {
1848                                 new_assets.append(new_asset);
1849                                 new_asset->add_user();
1850                         }
1851
1852 // Set filename to nothing for assets since save EDL would overwrite them.
1853                         if( load_mode == LOADMODE_REPLACE ||
1854                             load_mode == LOADMODE_REPLACE_CONCATENATE ) {
1855                                 set_filename("");
1856 // Reset timeline position
1857                                 for( int i=0; i<TOTAL_PANES; ++i ) {
1858                                         new_edl->local_session->view_start[i] = 0;
1859                                         new_edl->local_session->track_start[i] = 0;
1860                                 }
1861                         }
1862
1863                         result = 0;
1864                         break;
1865
1866 // File not found
1867                 case FILE_NOT_FOUND:
1868                         sprintf(string, _("Failed to open %s"), new_asset->path);
1869                         gui->show_message(string, theme->message_error);
1870                         gui->update_default_message();
1871                         break;
1872
1873 // Unknown format
1874                 case FILE_UNRECOGNIZED_CODEC: {
1875 // Test index file
1876                         { IndexFile indexfile(this, new_asset);
1877                         if( !(result = indexfile.open_index()) )
1878                                 indexfile.close_index(); }
1879
1880 // Test existing EDLs
1881                         for( int j=0; result && j<new_edls.total; ++j ) {
1882                                 Asset *old_asset = new_edls[j]->assets->get_asset(new_asset->path);
1883                                 if( old_asset ) {
1884                                         new_asset->copy_from(old_asset,1);
1885                                         result = 0;
1886                                 }
1887                         }
1888                         if( result ) {
1889                                 Asset *old_asset = edl->assets->get_asset(new_asset->path);
1890                                 if( old_asset ) {
1891                                         new_asset->copy_from(old_asset,1);
1892                                         result = 0;
1893                                 }
1894                         }
1895
1896 // Prompt user
1897                         if( result ) {
1898                                 char string[BCTEXTLEN];
1899                                 FileSystem fs;
1900                                 fs.extract_name(string, new_asset->path);
1901
1902                                 strcat(string, _("'s format couldn't be determined."));
1903                                 new_asset->audio_data = 1;
1904                                 new_asset->format = FILE_PCM;
1905                                 new_asset->channels = defaults->get("AUDIO_CHANNELS", 2);
1906                                 new_asset->sample_rate = defaults->get("SAMPLE_RATE", 44100);
1907                                 new_asset->bits = defaults->get("AUDIO_BITS", 16);
1908                                 new_asset->byte_order = defaults->get("BYTE_ORDER", 1);
1909                                 new_asset->signed_ = defaults->get("SIGNED_", 1);
1910                                 new_asset->header = defaults->get("HEADER", 0);
1911
1912                                 FileFormat fwindow(this);
1913                                 fwindow.create_objects(new_asset, string);
1914                                 result = fwindow.run_window();
1915
1916                                 defaults->update("AUDIO_CHANNELS", new_asset->channels);
1917                                 defaults->update("SAMPLE_RATE", new_asset->sample_rate);
1918                                 defaults->update("AUDIO_BITS", new_asset->bits);
1919                                 defaults->update("BYTE_ORDER", new_asset->byte_order);
1920                                 defaults->update("SIGNED_", new_asset->signed_);
1921                                 defaults->update("HEADER", new_asset->header);
1922                                 save_defaults();
1923                         }
1924
1925 // Append to list
1926                         if( !result ) {
1927 // Recalculate length
1928                                 delete new_file;
1929                                 new_file = new File;
1930                                 result = new_file->open_file(preferences, new_asset, 1, 0);
1931
1932                                 if( load_mode != LOADMODE_RESOURCESONLY ) {
1933                                         RecordLabels *labels = edl->session->label_cells ?
1934                                                 new RecordLabels(new_file) : 0;
1935                                         asset_to_edl(new_edl, new_asset, labels);
1936                                         new_edls.append(new_edl);
1937                                         new_edl->add_user();
1938                                         delete labels;
1939                                 }
1940                                 else {
1941                                         new_assets.append(new_asset);
1942                                         new_asset->add_user();
1943                                 }
1944                         }
1945                         else {
1946                                 result = 1;
1947                         }
1948                         break; }
1949
1950                 case FILE_IS_XML: {
1951                         FileXML xml_file;
1952 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1953                         xml_file.read_from_file(filenames->get(i));
1954 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1955                         const char *cin_version = 0;
1956                         while( !xml_file.read_tag() ) {
1957                                 if( xml_file.tag.title_is("EDL") ) {
1958                                         cin_version = xml_file.tag.get_property("VERSION");
1959                                         break;
1960                                 }
1961                         }
1962                         xml_file.rewind();
1963                         if( !cin_version ) {
1964                                 eprintf(_("XML file %s\n not from cinelerra."),filenames->get(i));
1965                                 char string[BCTEXTLEN];
1966                                 sprintf(string,_("Unknown %s"), filenames->get(i));
1967                                 gui->show_message(string);
1968                                 result = 1;
1969                                 break;
1970                         }
1971                         if( strcmp(cin_version, CINELERRA_VERSION) ) {
1972                                 char string[BCTEXTLEN];
1973                                 snprintf(string, sizeof(string),
1974                                          _("Warning: XML from cinelerra version %s\n"
1975                                         "Session data may be incompatible."), cin_version);
1976                                 show_warning(&preferences->warn_version, string);
1977                         }
1978                         if( load_mode == LOADMODE_NESTED ) {
1979 // Load temporary EDL for nesting.
1980                                 EDL *nested_edl = new EDL;
1981                                 nested_edl->create_objects();
1982                                 nested_edl->load_xml(&xml_file, LOAD_ALL);
1983 //printf("MWindow::load_filenames %p %s\n", nested_edl, nested_edl->project_path);
1984                                 new_edl->create_nested(nested_edl);
1985                                 new_edl->set_path(filenames->get(i));
1986                                 nested_edl->Garbage::remove_user();
1987                         }
1988                         else {
1989 // Load EDL for pasting
1990                                 new_edl->load_xml(&xml_file, LOAD_ALL);
1991 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1992                                 test_plugins(new_edl, filenames->get(i));
1993 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1994
1995                                 if( load_mode == LOADMODE_REPLACE ||
1996                                     load_mode == LOADMODE_REPLACE_CONCATENATE ) {
1997                                         strcpy(session->filename, filenames->get(i));
1998                                         strcpy(new_edl->local_session->clip_title,
1999                                                 filenames->get(i));
2000                                         if(update_filename)
2001                                                 set_filename(new_edl->local_session->clip_title);
2002                                 }
2003                                 else if( load_mode == LOADMODE_RESOURCESONLY ) {
2004                                         strcpy(new_edl->local_session->clip_title,
2005                                                 filenames->get(i));
2006                                         struct stat st;
2007                                         time_t t = !stat(filenames->get(i),&st) ?
2008                                                 st.st_mtime : time(&t);
2009                                         ctime_r(&t, new_edl->local_session->clip_notes);
2010                                 }
2011                         }
2012
2013                         new_edls.append(new_edl);
2014                         new_edl->add_user();
2015                         result = 0;
2016                         break; }
2017                 }
2018
2019                 new_edl->Garbage::remove_user();
2020                 new_asset->Garbage::remove_user();
2021
2022 // Store for testing index
2023                 new_files.append(new_file);
2024         }
2025
2026 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
2027
2028
2029         if(!result) {
2030                 gui->reset_default_message();
2031                 gui->default_message();
2032         }
2033
2034
2035 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
2036
2037 // Paste them.
2038 // Don't back up here.
2039         if(new_edls.size())
2040         {
2041 // For pasting, clear the active region
2042                 if(load_mode == LOADMODE_PASTE ||
2043                         load_mode == LOADMODE_NESTED)
2044                 {
2045                         double start = edl->local_session->get_selectionstart();
2046                         double end = edl->local_session->get_selectionend();
2047                         if(!EQUIV(start, end))
2048                                 edl->clear(start,
2049                                         end,
2050                                         edl->session->labels_follow_edits,
2051                                         edl->session->plugins_follow_edits,
2052                                         edl->session->autos_follow_edits);
2053                 }
2054
2055                 paste_edls(&new_edls, load_mode, 0, -1,
2056                         edl->session->labels_follow_edits,
2057                         edl->session->plugins_follow_edits,
2058                         edl->session->autos_follow_edits,
2059                         0); // overwrite
2060         }
2061
2062
2063
2064
2065 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
2066
2067 // Add new assets to EDL and schedule assets for index building.
2068         int got_indexes = 0;
2069         for( int i=0; i<new_edls.size(); ++i ) {
2070                 EDL *new_edl = new_edls[i];
2071                 for( int j=0; j<new_edl->nested_edls.size(); ++j ) {
2072                         mainindexes->add_next_asset(0, new_edl->nested_edls[j]);
2073                         edl->nested_edls.update_index(new_edl->nested_edls[j]);
2074                         got_indexes = 1;
2075                 }
2076
2077         }
2078
2079 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
2080         for( int i=0; i<new_assets.size(); ++i ) {
2081                 Asset *new_asset = new_assets[i];
2082
2083                 File *new_file = 0;
2084                 int got_it = 0;
2085                 for( int j=0; j<new_files.size(); ++j ) {
2086                         new_file = new_files[j];
2087                         if( !strcmp(new_file->asset->path, new_asset->path) ) {
2088                                 got_it = 1;
2089                                 break;
2090                         }
2091                 }
2092
2093                 mainindexes->add_next_asset(got_it ? new_file : 0, new_asset);
2094                 edl->assets->update(new_asset);
2095                 got_indexes = 1;
2096         }
2097 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
2098
2099 // Start examining next batch of index files
2100         if(got_indexes) mainindexes->start_build();
2101 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
2102
2103 // Open plugin GUIs
2104         Track *track = edl->tracks->first;
2105         while(track)
2106         {
2107                 for(int j = 0; j < track->plugin_set.size(); j++)
2108                 {
2109                         PluginSet *plugins = track->plugin_set[j];
2110                         Plugin *plugin = plugins->get_first_plugin();
2111
2112                         while(plugin)
2113                         {
2114                                 if(load_mode == LOADMODE_REPLACE ||
2115                                         load_mode == LOADMODE_REPLACE_CONCATENATE)
2116                                 {
2117                                         if(plugin->plugin_type == PLUGIN_STANDALONE &&
2118                                                 plugin->show)
2119                                         {
2120                                                 show_plugin(plugin);
2121                                         }
2122                                 }
2123
2124                                 plugin = (Plugin*)plugin->next;
2125                         }
2126                 }
2127
2128                 track = track->next;
2129         }
2130
2131         // if just opening one new resource in replace mode
2132         if( ftype != FILE_IS_XML &&
2133             ( load_mode == LOADMODE_REPLACE ||
2134               load_mode == LOADMODE_REPLACE_CONCATENATE ) ) {
2135                 select_asset(0, 0);
2136                 edl->session->proxy_scale = 1;
2137                 edl->session->proxy_use_scaler = 0;
2138                 edl->session->proxy_auto_scale = 0;
2139                 edl->session->proxy_beep = 0;
2140                 edl->local_session->preview_start = 0;
2141                 edl->local_session->preview_end = 0;
2142                 edl->local_session->loop_playback = 0;
2143                 edl->local_session->set_selectionstart(0);
2144                 edl->local_session->set_selectionend(0);
2145                 set_brender_active(0, 0);
2146                 fit_selection();
2147                 goto_start();
2148         }
2149
2150         if( ( edl->session->proxy_auto_scale && edl->session->proxy_scale != 1 ) &&
2151             ( load_mode != LOADMODE_REPLACE && load_mode != LOADMODE_REPLACE_CONCATENATE ) ) {
2152                 ArrayList<Indexable *> orig_idxbls;
2153                 for( int i=0; i<new_assets.size(); ++i )
2154                         orig_idxbls.append(new_assets.get(i));
2155                 for( int i=0; i<new_edls.size(); ++i ) {
2156                         EDL *new_edl = new_edls[i];
2157                         for( Track *track=new_edl->tracks->first; track; track=track->next ) {
2158                                 if( track->data_type != TRACK_VIDEO ) continue;
2159                                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
2160                                         Indexable *idxbl = (Indexable *)edit->asset;
2161                                         if( !idxbl ) continue;
2162                                         if( !idxbl->have_video() ) continue;
2163                                         if( edit->channel != 0 ) continue; // first layer only
2164                                         orig_idxbls.append(edit->asset);
2165                                 }
2166                         }
2167                 }
2168                 gui->unlock_window(); // to update progress bar
2169                 int ret = render_proxy(orig_idxbls);
2170                 gui->lock_window("MWindow::load_filenames");
2171                 if( ret >= 0 && edl->session->proxy_beep ) {
2172                         if( ret > 0 )
2173                                 beep(2000., 1.5, 0.5);
2174                         else
2175                                 beep(4000., 0.25, 0.5);
2176                 }
2177         }
2178
2179 // need to update undo before project, since mwindow is unlocked & a new load
2180 // can begin here.  Should really prevent loading until we're done.
2181 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
2182         undo->update_undo_after(_("load"), LOAD_ALL);
2183
2184         for(int i = 0; i < new_edls.size(); i++)
2185         {
2186                 new_edls[i]->remove_user();
2187         }
2188 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
2189
2190         new_edls.remove_all();
2191
2192         for(int i = 0; i < new_assets.size(); i++)
2193         {
2194                 new_assets[i]->Garbage::remove_user();
2195         }
2196
2197         new_assets.remove_all();
2198         new_files.remove_all_objects();
2199
2200
2201 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
2202         if(load_mode == LOADMODE_REPLACE ||
2203                 load_mode == LOADMODE_REPLACE_CONCATENATE)
2204         {
2205                 session->changes_made = 0;
2206         }
2207         else
2208         {
2209                 session->changes_made = 1;
2210         }
2211
2212         gui->stop_hourglass();
2213
2214
2215 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
2216         update_project(load_mode);
2217
2218 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
2219
2220         return 0;
2221 }
2222
2223 int MWindow::render_proxy(ArrayList<Indexable *> &new_idxbls)
2224 {
2225         Asset *format_asset = new Asset;
2226         format_asset->format = FILE_FFMPEG;
2227         format_asset->load_defaults(defaults, "PROXY_", 1, 1, 0, 0, 0);
2228         ProxyRender proxy_render(this, format_asset);
2229         int new_scale = edl->session->proxy_scale;
2230         int use_scaler = edl->session->proxy_use_scaler;
2231
2232         for( int i=0; i<new_idxbls.size(); ++i ) {
2233                 Indexable *orig = new_idxbls.get(i);
2234                 Asset *proxy = proxy_render.add_original(orig, new_scale);
2235                 if( !proxy ) continue;
2236                 FileSystem fs;
2237                 int exists = fs.get_size(proxy->path) > 0 ? 1 : 0;
2238                 int got_it = exists && // if proxy exists, and is newer than orig
2239                     fs.get_date(proxy->path) > fs.get_date(orig->path) ? 1 : 0;
2240                 if( got_it ) continue;
2241                 proxy_render.add_needed(orig, proxy);
2242         }
2243
2244 // render needed proxies
2245         int result = proxy_render.create_needed_proxies(new_scale);
2246         if( !result ) {
2247                 add_proxy(use_scaler,
2248                         &proxy_render.orig_idxbls, &proxy_render.orig_proxies);
2249         }
2250         format_asset->remove_user();
2251         return !result ? proxy_render.needed_proxies.size() : -1;
2252 }
2253
2254 void MWindow::test_plugins(EDL *new_edl, char *path)
2255 {
2256         char string[BCTEXTLEN];
2257
2258 // Do a check whether plugins exist
2259         for( Track *track=new_edl->tracks->first; track; track=track->next ) {
2260                 for( int k=0; k<track->plugin_set.total; ++k ) {
2261                         PluginSet *plugin_set = track->plugin_set[k];
2262                         for( Plugin *plugin = (Plugin*)plugin_set->first;
2263                                         plugin; plugin = (Plugin*)plugin->next ) {
2264                                 if( plugin->plugin_type != PLUGIN_STANDALONE ) continue;
2265 // ok we need to find it in plugindb
2266                                 PluginServer *server =
2267                                         scan_plugindb(plugin->title, track->data_type);
2268                                 if( !server || server->transition ) {
2269                                         sprintf(string,
2270         _("The %s '%s' in file '%s' is not part of your installation of Cinelerra.\n"
2271           "The project won't be rendered as it was meant and Cinelerra might crash.\n"),
2272                                                 "effect", _(plugin->title), path);
2273                                         MainError::show_error(string);
2274                                 }
2275                         }
2276                 }
2277
2278                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
2279                         if( !edit->transition ) continue;
2280 // ok we need to find transition in plugindb
2281                         PluginServer *server =
2282                                 scan_plugindb(edit->transition->title, track->data_type);
2283                         if( !server || !server->transition ) {
2284                                 sprintf(string,
2285         _("The %s '%s' in file '%s' is not part of your installation of Cinelerra.\n"
2286           "The project won't be rendered as it was meant and Cinelerra might crash.\n"),
2287                                         "transition", _(edit->transition->title), path);
2288                                 MainError::show_error(string);
2289                         }
2290                 }
2291         }
2292 }
2293
2294
2295 void MWindow::init_shm(const char *pfn, int64_t min)
2296 {
2297         int64_t result = 0;
2298 // Fix shared memory
2299         FILE *fd = fopen(pfn, "r");
2300         if( fd ) {
2301                 fscanf(fd, "%jd", &result);
2302                 fclose(fd);
2303                 if( result >= min ) return;
2304         }
2305
2306         fd = fopen(pfn, "w");
2307         if( !fd ) return;
2308         fprintf(fd, "0x%jx", min);
2309         fclose(fd);
2310
2311         fd = fopen(pfn, "r");
2312         if( !fd ) {
2313                 eprintf(_("MWindow::init_shm: couldn't open %s for reading.\n"), pfn);
2314                 return;
2315         }
2316
2317         fscanf(fd, "%jd", &result);
2318         fclose(fd);
2319         if( result < min ) {
2320                 eprintf(_("MWindow::init_shm: %s is %p.\n"
2321                         "you probably need to be root, or:\n"
2322                         "as root, run: echo 0x%jx > %s\n"
2323                         "before trying to start cinelerra.\n"
2324                         "It should be at least 0x%jx for Cinelerra.\n"),
2325                         pfn, (void *)result, min, pfn, min);
2326         }
2327 }
2328
2329 void MWindow::create_objects(int want_gui,
2330         int want_new,
2331         char *config_path)
2332 {
2333         const int debug = 0;
2334         if(debug) PRINT_TRACE
2335
2336 // For some reason, init_signals must come after show_splash or the signals won't
2337 // get trapped.
2338         init_signals();
2339         if(debug) PRINT_TRACE
2340         init_3d();
2341
2342         if(debug) PRINT_TRACE
2343         show_splash();
2344
2345         if(debug) PRINT_TRACE
2346         default_standard = default_std();
2347         init_defaults(defaults, config_path);
2348         check_language();
2349         init_preferences();
2350         if(splash_window)
2351                 splash_window->operation->update(_("Initializing Plugins"));
2352         init_plugins(this, preferences);
2353         if(debug) PRINT_TRACE
2354         init_ladspa_plugins(this, preferences);
2355         if(debug) PRINT_TRACE
2356         init_plugin_tips(*plugindb, cin_lang);
2357         if(splash_window)
2358                 splash_window->operation->update(_("Initializing GUI"));
2359         if(debug) PRINT_TRACE
2360         init_theme();
2361
2362         if(debug) PRINT_TRACE
2363         init_error();
2364
2365         if(splash_window)
2366                 splash_window->operation->update(_("Initializing Fonts"));
2367         char string[BCTEXTLEN];
2368         strcpy(string, preferences->plugin_dir);
2369         strcat(string, "/" FONT_SEARCHPATH);
2370         BC_Resources::init_fontconfig(string);
2371         if(debug) PRINT_TRACE
2372
2373 // Default project created here
2374         init_edl();
2375         if(debug) PRINT_TRACE
2376
2377         init_cache();
2378         if(debug) PRINT_TRACE
2379
2380         Timer timer;
2381
2382         init_awindow();
2383         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2384
2385         init_compositor();
2386         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2387
2388 //printf("MWindow::create_objects %d session->show_vwindow=%d\n", __LINE__, session->show_vwindow);
2389         if(session->show_vwindow)
2390                 get_viewer(1, DEFAULT_VWINDOW);
2391         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2392
2393         init_gui();
2394         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2395
2396         init_levelwindow();
2397         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2398
2399         init_indexes();
2400         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2401
2402         init_channeldb();
2403         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2404
2405         init_gwindow();
2406         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2407
2408         init_render();
2409         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2410
2411         init_brender();
2412         init_exportedl();
2413         init_commercials();
2414         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2415         mainprogress = new MainProgress(this, gui);
2416         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2417         undo = new MainUndo(this);
2418
2419         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2420
2421         plugin_guis = new ArrayList<PluginServer*>;
2422         dead_plugins = new ArrayList<PluginServer*>;
2423         keyframe_threads = new ArrayList<KeyFrameThread*>;
2424
2425         if(debug) printf("MWindow::create_objects %d vwindows=%d show_vwindow=%d\n",
2426                 __LINE__,
2427                 vwindows.size(),
2428                 session->show_vwindow);
2429
2430 // Show all vwindows
2431 //      if(session->show_vwindow) {
2432 //              for(int j = 0; j < vwindows.size(); j++) {
2433 //                      VWindow *vwindow = vwindows[j];
2434 //                      if( !vwindow->is_running() ) continue;
2435 //                      if(debug) printf("MWindow::create_objects %d vwindow=%p\n",
2436 //                              __LINE__,
2437 //                              vwindow);
2438 //                      if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2439 //                      vwindow->gui->lock_window("MWindow::create_objects 1");
2440 //                      if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2441 //                      vwindow->gui->show_window();
2442 //                      if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2443 //                      vwindow->gui->unlock_window();
2444 //              }
2445 //      }
2446
2447
2448         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2449
2450         if(session->show_cwindow)
2451         {
2452                 cwindow->gui->lock_window("MWindow::create_objects 1");
2453                 cwindow->gui->show_window();
2454                 cwindow->gui->unlock_window();
2455         }
2456
2457         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2458         if(session->show_awindow)
2459         {
2460                 awindow->gui->lock_window("MWindow::create_objects 1");
2461                 awindow->gui->show_window();
2462                 awindow->gui->unlock_window();
2463         }
2464
2465         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
2466         if(session->show_lwindow)
2467         {
2468                 lwindow->gui->lock_window("MWindow::create_objects 1");
2469                 lwindow->gui->show_window();
2470                 lwindow->gui->unlock_window();
2471         }
2472
2473         if(debug) printf("MWindow::create_objects %d total_time=%d gwindow=%p\n",
2474                 __LINE__,
2475                 (int)timer.get_difference(),
2476                 gwindow->gui);
2477         if(session->show_gwindow)
2478         {
2479                 gwindow->gui->lock_window("MWindow::create_objects 1");
2480                 gwindow->gui->show_window();
2481                 gwindow->gui->unlock_window();
2482         }
2483
2484         if(debug) PRINT_TRACE
2485
2486         gui->lock_window("MWindow::create_objects 1");
2487         gui->mainmenu->load_defaults(defaults);
2488         gui->mainmenu->update_toggles(0);
2489         gui->update_patchbay();
2490         gui->draw_canvas(0, 0);
2491         gui->draw_cursor(1);
2492         gui->show_window();
2493         gui->raise_window();
2494         gui->unlock_window();
2495
2496         if(debug) PRINT_TRACE
2497
2498         if(preferences->use_tipwindow)
2499                 init_tipwindow();
2500         if(debug) PRINT_TRACE
2501
2502         gui->add_keyboard_listener(
2503                 (int (BC_WindowBase::*)(BC_WindowBase *))
2504                 &MWindowGUI::keyboard_listener);
2505
2506         hide_splash();
2507         init_shm("/proc/sys/kernel/shmmax", 0x7fffffff);
2508         init_shm("/proc/sys/kernel/shmmni", 0x4000);
2509         if(debug) PRINT_TRACE
2510
2511         BC_WindowBase::get_resources()->vframe_shm = 1;
2512 }
2513
2514
2515 void MWindow::show_splash()
2516 {
2517 #include "data/heroine_logo9_png.h"
2518         VFrame *frame = new VFramePng(heroine_logo9_png);
2519         BC_DisplayInfo dpyi;
2520         int rw = dpyi.get_root_w(), rh = dpyi.get_root_h();
2521         int rr = (rw < rh ? rw : rh) / 4;
2522         splash_window = new SplashGUI(frame, rr, rr);
2523         splash_window->create_objects();
2524 }
2525
2526 void MWindow::hide_splash()
2527 {
2528         if(splash_window)
2529                 delete splash_window;
2530         splash_window = 0;
2531 }
2532
2533
2534 void MWindow::start()
2535 {
2536 ENABLE_BUFFER
2537 //PRINT_TRACE
2538 //      vwindows[DEFAULT_VWINDOW]->start();
2539         awindow->start();
2540 //PRINT_TRACE
2541         cwindow->start();
2542 //PRINT_TRACE
2543         lwindow->start();
2544 //PRINT_TRACE
2545         gwindow->start();
2546 //PRINT_TRACE
2547 //      Thread::start();
2548 //PRINT_TRACE
2549         playback_3d->start();
2550 //PRINT_TRACE
2551 }
2552
2553 void MWindow::run()
2554 {
2555         run_lock->lock("MWindow::run");
2556         gui->run_window();
2557         stop_playback(1);
2558
2559         brender_lock->lock("MWindow::run 1");
2560         delete brender;         brender = 0;
2561         brender_lock->unlock();
2562
2563         interrupt_indexes();
2564         clean_indexes();
2565         save_defaults();
2566         run_lock->unlock();
2567 }
2568
2569 void MWindow::show_vwindow()
2570 {
2571         int total_running = 0;
2572         session->show_vwindow = 1;
2573
2574 //printf("MWindow::show_vwindow %d %d\n", __LINE__, vwindows.size());
2575 // Raise all windows which are visible
2576         for(int j = 0; j < vwindows.size(); j++) {
2577                 VWindow *vwindow = vwindows[j];
2578                 if( !vwindow->is_running() ) continue;
2579                 vwindow->gui->lock_window("MWindow::show_vwindow");
2580                 vwindow->gui->show_window(0);
2581                 vwindow->gui->raise_window();
2582                 vwindow->gui->flush();
2583                 vwindow->gui->unlock_window();
2584                 total_running++;
2585         }
2586
2587 // If no windows visible
2588         if(!total_running)
2589         {
2590                 get_viewer(1, DEFAULT_VWINDOW);
2591         }
2592
2593         gui->mainmenu->show_vwindow->set_checked(1);
2594 }
2595
2596 void MWindow::show_awindow()
2597 {
2598         session->show_awindow = 1;
2599         awindow->gui->lock_window("MWindow::show_awindow");
2600         awindow->gui->show_window();
2601         awindow->gui->raise_window();
2602         awindow->gui->flush();
2603         awindow->gui->unlock_window();
2604         gui->mainmenu->show_awindow->set_checked(1);
2605 }
2606
2607 char *MWindow::get_cwindow_display()
2608 {
2609         char *x11_host = screens < 2 || session->window_config == 0 ?
2610                 session->a_x11_host : session->b_x11_host;
2611         return *x11_host ? x11_host : 0;
2612 }
2613
2614 void MWindow::show_cwindow()
2615 {
2616         session->show_cwindow = 1;
2617         cwindow->show_window();
2618         gui->mainmenu->show_cwindow->set_checked(1);
2619 }
2620
2621 void MWindow::show_gwindow()
2622 {
2623         session->show_gwindow = 1;
2624
2625         gwindow->gui->lock_window("MWindow::show_gwindow");
2626         gwindow->gui->show_window();
2627         gwindow->gui->raise_window();
2628         gwindow->gui->flush();
2629         gwindow->gui->unlock_window();
2630
2631         gui->mainmenu->show_gwindow->set_checked(1);
2632 }
2633 void MWindow::hide_gwindow()
2634 {
2635         session->show_gwindow = 0;
2636
2637         gwindow->gui->lock_window("MWindow::show_gwindow");
2638         gwindow->gui->hide_window();
2639         gwindow->gui->unlock_window();
2640 }
2641
2642 void MWindow::show_lwindow()
2643 {
2644         session->show_lwindow = 1;
2645         lwindow->gui->lock_window("MWindow::show_lwindow");
2646         lwindow->gui->show_window();
2647         lwindow->gui->raise_window();
2648         lwindow->gui->flush();
2649         lwindow->gui->unlock_window();
2650         gui->mainmenu->show_lwindow->set_checked(1);
2651 }
2652
2653 int MWindow::tile_windows(int window_config)
2654 {
2655         int need_reload = 0;
2656         int play_config = window_config==0 ? 0 : 1;
2657         edl->session->playback_config->load_defaults(defaults, play_config);
2658         session->default_window_positions(window_config);
2659         if( screens == 1 ) {
2660                 gui->default_positions();
2661                 sync_parameters(CHANGE_ALL);
2662         }
2663         else
2664                 need_reload = 1;
2665         return need_reload;
2666 }
2667
2668 void MWindow::toggle_loop_playback()
2669 {
2670         edl->local_session->loop_playback = !edl->local_session->loop_playback;
2671         set_loop_boundaries();
2672         save_backup();
2673
2674         gui->draw_overlays(1);
2675         sync_parameters(CHANGE_PARAMS);
2676 }
2677
2678 //void MWindow::set_titles(int value)
2679 //{
2680 //      edl->session->show_titles = value;
2681 //      trackmovement(edl->local_session->track_start);
2682 //}
2683
2684 void MWindow::set_screens(int value)
2685 {
2686         screens = value;
2687 }
2688
2689 void MWindow::set_auto_keyframes(int value, int lock_mwindow, int lock_cwindow)
2690 {
2691         if(lock_mwindow) gui->lock_window("MWindow::set_auto_keyframes");
2692         edl->session->auto_keyframes = value;
2693         gui->mbuttons->edit_panel->keyframe->update(value);
2694         gui->flush();
2695         if(lock_mwindow) gui->unlock_window();
2696
2697         if(lock_cwindow) cwindow->gui->lock_window("MWindow::set_auto_keyframes");
2698         cwindow->gui->edit_panel->keyframe->update(value);
2699         cwindow->gui->flush();
2700         if(lock_cwindow) cwindow->gui->unlock_window();
2701 }
2702
2703 void MWindow::set_auto_visibility(Autos *autos, int value)
2704 {
2705         if( autos->type == Autos::AUTOMATION_TYPE_PLUGIN )
2706                 edl->session->auto_conf->plugins = value;
2707         else if( autos->autoidx >= 0 )
2708                 edl->session->auto_conf->autos[autos->autoidx] = value;
2709         else
2710                 return;
2711
2712         gui->update(0, 1, 0, 0, 0, 0, 0);
2713         gui->mainmenu->update_toggles(1);
2714         gui->unlock_window();
2715         gwindow->gui->update_toggles(1);
2716         gui->lock_window("MWindow::set_auto_visibility");
2717 }
2718
2719 void MWindow::set_keyframe_type(int mode)
2720 {
2721         gui->lock_window("MWindow::set_keyframe_type");
2722         edl->local_session->floatauto_type = mode;
2723         gui->mainmenu->update_toggles(0);
2724         gui->unlock_window();
2725 }
2726
2727 int MWindow::set_editing_mode(int new_editing_mode, int lock_mwindow, int lock_cwindow)
2728 {
2729         if(lock_mwindow) gui->lock_window("MWindow::set_editing_mode");
2730         edl->session->editing_mode = new_editing_mode;
2731         gui->mbuttons->edit_panel->editing_mode = edl->session->editing_mode;
2732         gui->mbuttons->edit_panel->update();
2733         gui->set_editing_mode(1);
2734         if(lock_mwindow) gui->unlock_window();
2735
2736
2737         if(lock_cwindow) cwindow->gui->lock_window("MWindow::set_editing_mode");
2738         cwindow->gui->edit_panel->update();
2739         cwindow->gui->edit_panel->editing_mode = edl->session->editing_mode;
2740         if(lock_cwindow) cwindow->gui->unlock_window();
2741         return 0;
2742 }
2743
2744 void MWindow::toggle_editing_mode()
2745 {
2746         int mode = edl->session->editing_mode;
2747         if( mode == EDITING_ARROW )
2748                 set_editing_mode(EDITING_IBEAM, 0, 1);
2749         else
2750                 set_editing_mode(EDITING_ARROW, 0, 1);
2751 }
2752
2753
2754 void MWindow::set_labels_follow_edits(int value)
2755 {
2756         gui->lock_window("MWindow::set_labels_follow_edits");
2757         edl->session->labels_follow_edits = value;
2758         gui->mbuttons->edit_panel->locklabels->update(value);
2759         gui->mainmenu->labels_follow_edits->set_checked(value);
2760         gui->flush();
2761         gui->unlock_window();
2762 }
2763
2764 void MWindow::sync_parameters(int change_type)
2765 {
2766         if( in_destructor ) return;
2767
2768 // Sync engines which are playing back
2769         if(cwindow->playback_engine->is_playing_back)
2770         {
2771                 if(change_type == CHANGE_PARAMS)
2772                 {
2773 // TODO: block keyframes until synchronization is done
2774                         cwindow->playback_engine->sync_parameters(edl);
2775                 }
2776                 else
2777 // Stop and restart
2778                 {
2779                         int command = cwindow->playback_engine->command->command;
2780                         cwindow->playback_engine->que->send_command(STOP,
2781                                 CHANGE_NONE,
2782                                 0,
2783                                 0);
2784 // Waiting for tracking to finish would make the restart position more
2785 // accurate but it can't lock the window to stop tracking for some reason.
2786 // Not waiting for tracking gives a faster response but restart position is
2787 // only as accurate as the last tracking update.
2788                         cwindow->playback_engine->interrupt_playback(0);
2789                         cwindow->playback_engine->que->send_command(command,
2790                                         change_type,
2791                                         edl,
2792                                         1,
2793                                         0);
2794                 }
2795         }
2796         else
2797         {
2798                 cwindow->refresh_frame(change_type);
2799         }
2800 }
2801
2802 void MWindow::age_caches()
2803 {
2804 // printf("MWindow::age_caches %d %lld %lld %lld %lld\n",
2805 // __LINE__,
2806 // preferences->cache_size,
2807 // audio_cache->get_memory_usage(1),
2808 // video_cache->get_memory_usage(1),
2809 // frame_cache->get_memory_usage(),
2810 // wave_cache->get_memory_usage(),
2811 // memory_usage);
2812         frame_cache->age_cache(512);
2813         wave_cache->age_cache(8192);
2814
2815         int64_t memory_usage;
2816         int64_t prev_memory_usage = 0;
2817         int result = 0;
2818         int64_t video_size = (int64_t)(preferences->cache_size * 0.80);
2819         while( !result && prev_memory_usage !=
2820                 (memory_usage=video_cache->get_memory_usage(1)) &&
2821                 memory_usage > video_size ) {
2822                 video_cache->delete_oldest();
2823                 prev_memory_usage = memory_usage;
2824         }
2825
2826         prev_memory_usage = 0;
2827         result = 0;
2828         int64_t audio_size = (int64_t)(preferences->cache_size * 0.20);
2829         while( !result && prev_memory_usage !=
2830                 (memory_usage=audio_cache->get_memory_usage(1)) &&
2831                 memory_usage > audio_size ) {
2832                 audio_cache->delete_oldest();
2833                 prev_memory_usage = memory_usage;
2834         }
2835 }
2836
2837 void MWindow::reset_android_remote()
2838 {
2839         gui->use_android_remote(preferences->android_remote);
2840 }
2841
2842
2843 void MWindow::show_keyframe_gui(Plugin *plugin)
2844 {
2845         keyframe_gui_lock->lock("MWindow::show_keyframe_gui");
2846 // Find existing thread
2847         for(int i = 0; i < keyframe_threads->size(); i++)
2848         {
2849                 if(keyframe_threads->get(i)->plugin == plugin)
2850                 {
2851                         keyframe_threads->get(i)->start_window(plugin, 0);
2852                         keyframe_gui_lock->unlock();
2853                         return;
2854                 }
2855         }
2856
2857 // Find unused thread
2858         for(int i = 0; i < keyframe_threads->size(); i++)
2859         {
2860                 if(!keyframe_threads->get(i)->plugin)
2861                 {
2862                         keyframe_threads->get(i)->start_window(plugin, 0);
2863                         keyframe_gui_lock->unlock();
2864                         return;
2865                 }
2866         }
2867
2868 // Create new thread
2869         KeyFrameThread *thread = new KeyFrameThread(this);
2870         keyframe_threads->append(thread);
2871         thread->start_window(plugin, 0);
2872
2873         keyframe_gui_lock->unlock();
2874 }
2875
2876
2877
2878
2879
2880 void MWindow::show_plugin(Plugin *plugin)
2881 {
2882         int done = 0;
2883
2884 SET_TRACE
2885 // Remove previously deleted plugin GUIs
2886         dead_plugin_lock->lock("MWindow::show_plugin");
2887         dead_plugins->remove_all_objects();
2888         dead_plugin_lock->unlock();
2889
2890 //printf("MWindow::show_plugin %d\n", __LINE__);
2891 SET_TRACE
2892
2893
2894         plugin_gui_lock->lock("MWindow::show_plugin");
2895         for(int i = 0; i < plugin_guis->total; i++)
2896         {
2897 // Pointer comparison
2898                 if(plugin_guis->get(i)->plugin == plugin)
2899                 {
2900                         plugin_guis->get(i)->raise_window();
2901                         done = 1;
2902                         break;
2903                 }
2904         }
2905 SET_TRACE
2906
2907 //printf("MWindow::show_plugin 1\n");
2908         if( !done && !plugin->track ) {
2909                 printf("MWindow::show_plugin track not defined.\n");
2910                 done = 1;
2911         }
2912         if( !done ) {
2913                 PluginServer *server = scan_plugindb(plugin->title,
2914                         plugin->track->data_type);
2915
2916 //printf("MWindow::show_plugin %p %d\n", server, server->uses_gui);
2917                 if(server && server->uses_gui)
2918                 {
2919                         PluginServer *gui = new PluginServer(*server);
2920                         plugin_guis->append(gui);
2921 // Needs mwindow to do GUI
2922                         gui->set_mwindow(this);
2923                         gui->open_plugin(0, preferences, edl, plugin);
2924                         plugin->show = 1;
2925                         gui->show_gui();
2926                 }
2927         }
2928         plugin_gui_lock->unlock();
2929 //printf("MWindow::show_plugin %d\n", __LINE__);
2930 SET_TRACE
2931 //sleep(1);
2932 //printf("MWindow::show_plugin 2\n");
2933 }
2934
2935 void MWindow::hide_plugin(Plugin *plugin, int lock)
2936 {
2937         plugin->show = 0;
2938 // Update the toggle
2939         gui->lock_window("MWindow::hide_plugin");
2940         gui->update(0, 1, 0, 0, 0, 0, 0);
2941         gui->unlock_window();
2942
2943         if(lock) plugin_gui_lock->lock("MWindow::hide_plugin");
2944         for(int i = 0; i < plugin_guis->total; i++)
2945         {
2946                 if(plugin_guis->get(i)->plugin == plugin)
2947                 {
2948                         PluginServer *ptr = plugin_guis->get(i);
2949                         plugin_guis->remove(ptr);
2950                         if(lock) plugin_gui_lock->unlock();
2951 // Last command executed in client side close
2952 // Schedule for deletion
2953                         ptr->hide_gui();
2954                         delete_plugin(ptr);
2955 //sleep(1);
2956                         return;
2957                 }
2958         }
2959         if(lock) plugin_gui_lock->unlock();
2960 }
2961
2962 void MWindow::delete_plugin(PluginServer *plugin)
2963 {
2964         dead_plugin_lock->lock("MWindow::delete_plugin");
2965         dead_plugins->append(plugin);
2966         dead_plugin_lock->unlock();
2967 }
2968
2969 void MWindow::hide_plugins()
2970 {
2971         plugin_gui_lock->lock("MWindow::hide_plugins 1");
2972         while(plugin_guis->size())
2973         {
2974                 PluginServer *ptr = plugin_guis->get(0);
2975                 plugin_guis->remove(ptr);
2976                 plugin_gui_lock->unlock();
2977 // Last command executed in client side close
2978 // Schedule for deletion
2979                 ptr->hide_gui();
2980                 delete_plugin(ptr);
2981                 plugin_gui_lock->lock("MWindow::hide_plugins 2");
2982         }
2983         plugin_gui_lock->unlock();
2984
2985         hide_keyframe_guis();
2986 }
2987
2988 void MWindow::hide_keyframe_guis()
2989 {
2990         keyframe_gui_lock->lock("MWindow::hide_keyframe_guis");
2991         for(int i = 0; i < keyframe_threads->size(); i++)
2992         {
2993                 keyframe_threads->get(i)->close_window();
2994         }
2995         keyframe_gui_lock->unlock();
2996 }
2997
2998 void MWindow::hide_keyframe_gui(Plugin *plugin)
2999 {
3000         keyframe_gui_lock->lock("MWindow::hide_keyframe_gui");
3001         for(int i = 0; i < keyframe_threads->size(); i++)
3002         {
3003                 if(keyframe_threads->get(i)->plugin == plugin)
3004                 {
3005                         keyframe_threads->get(i)->close_window();
3006                         break;
3007                 }
3008         }
3009         keyframe_gui_lock->unlock();
3010 }
3011
3012 void MWindow::update_keyframe_guis()
3013 {
3014 // Send new configuration to keyframe GUI's
3015         keyframe_gui_lock->lock("MWindow::update_keyframe_guis");
3016         for(int i = 0; i < keyframe_threads->size(); i++)
3017         {
3018                 KeyFrameThread *ptr = keyframe_threads->get(i);
3019                 if(edl->tracks->plugin_exists(ptr->plugin))
3020                         ptr->update_gui(1);
3021                 else
3022                 {
3023                         ptr->close_window();
3024                 }
3025         }
3026         keyframe_gui_lock->unlock();
3027 }
3028
3029 void MWindow::update_plugin_guis(int do_keyframe_guis)
3030 {
3031 // Send new configuration to plugin GUI's
3032         plugin_gui_lock->lock("MWindow::update_plugin_guis");
3033
3034         for(int i = 0; i < plugin_guis->size(); i++)
3035         {
3036                 PluginServer *ptr = plugin_guis->get(i);
3037                 if(edl->tracks->plugin_exists(ptr->plugin))
3038                         ptr->update_gui();
3039                 else
3040                 {
3041 // Schedule for deletion if no plugin
3042                         plugin_guis->remove_number(i);
3043                         i--;
3044
3045                         ptr->hide_gui();
3046                         delete_plugin(ptr);
3047                 }
3048         }
3049
3050
3051 // Change plugin variable if not visible
3052         Track *track = edl->tracks->first;
3053         while(track)
3054         {
3055                 for(int i = 0; i < track->plugin_set.size(); i++)
3056                 {
3057                         Plugin *plugin = (Plugin*)track->plugin_set[i]->first;
3058                         while(plugin)
3059                         {
3060                                 int got_it = 0;
3061                                 for(int i = 0; i < plugin_guis->size(); i++)
3062                                 {
3063                                         PluginServer *server = plugin_guis->get(i);
3064                                         if(server->plugin == plugin)
3065                                         {
3066                                                 got_it = 1;
3067                                                 break;
3068                                         }
3069                                 }
3070
3071                                 if(!got_it) plugin->show = 0;
3072
3073                                 plugin = (Plugin*)plugin->next;
3074                         }
3075                 }
3076
3077                 track = track->next;
3078         }
3079
3080         plugin_gui_lock->unlock();
3081
3082
3083         if(do_keyframe_guis) update_keyframe_guis();
3084 }
3085
3086 int MWindow::plugin_gui_open(Plugin *plugin)
3087 {
3088         int result = 0;
3089         plugin_gui_lock->lock("MWindow::plugin_gui_open");
3090         for(int i = 0; i < plugin_guis->total; i++)
3091         {
3092                 if(plugin_guis->get(i)->plugin->identical_location(plugin))
3093                 {
3094                         result = 1;
3095                         break;
3096                 }
3097         }
3098         plugin_gui_lock->unlock();
3099         return result;
3100 }
3101
3102 void MWindow::render_plugin_gui(void *data, Plugin *plugin)
3103 {
3104         plugin_gui_lock->lock("MWindow::render_plugin_gui");
3105         for(int i = 0; i < plugin_guis->total; i++)
3106         {
3107                 if(plugin_guis->get(i)->plugin->identical_location(plugin))
3108                 {
3109                         plugin_guis->get(i)->render_gui(data);
3110                         break;
3111                 }
3112         }
3113         plugin_gui_lock->unlock();
3114 }
3115
3116 void MWindow::render_plugin_gui(void *data, int size, Plugin *plugin)
3117 {
3118         plugin_gui_lock->lock("MWindow::render_plugin_gui");
3119         for(int i = 0; i < plugin_guis->total; i++)
3120         {
3121                 if(plugin_guis->get(i)->plugin->identical_location(plugin))
3122                 {
3123                         plugin_guis->get(i)->render_gui(data, size);
3124                         break;
3125                 }
3126         }
3127         plugin_gui_lock->unlock();
3128 }
3129
3130
3131 void MWindow::update_plugin_states()
3132 {
3133         plugin_gui_lock->lock("MWindow::update_plugin_states");
3134         for(int i = 0; i < plugin_guis->total; i++)
3135         {
3136                 int result = 0;
3137 // Get a plugin GUI
3138                 Plugin *src_plugin = plugin_guis->get(i)->plugin;
3139                 PluginServer *src_plugingui = plugin_guis->get(i);
3140
3141 // Search for plugin in EDL.  Only the master EDL shows plugin GUIs.
3142                 for(Track *track = edl->tracks->first;
3143                         track && !result;
3144                         track = track->next)
3145                 {
3146                         for(int j = 0;
3147                                 j < track->plugin_set.total && !result;
3148                                 j++)
3149                         {
3150                                 PluginSet *plugin_set = track->plugin_set[j];
3151                                 for(Plugin *plugin = (Plugin*)plugin_set->first;
3152                                         plugin && !result;
3153                                         plugin = (Plugin*)plugin->next)
3154                                 {
3155                                         if(plugin == src_plugin &&
3156                                                 !strcmp(plugin->title, src_plugingui->title)) result = 1;
3157                                 }
3158                         }
3159                 }
3160
3161
3162 // Doesn't exist anymore
3163                 if(!result)
3164                 {
3165                         hide_plugin(src_plugin, 0);
3166                         i--;
3167                 }
3168         }