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