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