r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / cinelerra / mwindow.C
blobf6727983075a649bcaaf58e5a502fd456130e074
1 #include "asset.h"
2 #include "assets.h"
3 #include "awindowgui.h"
4 #include "awindow.h"
5 #include "batchrender.h"
6 #include "bcdisplayinfo.h"
7 #include "bcsignals.h"
8 #include "brender.h"
9 #include "cache.h"
10 #include "channel.h"
11 #include "channeldb.h"
12 #include "clip.h"
13 #include "colormodels.h"
14 #include "cplayback.h"
15 #include "ctimebar.h"
16 #include "cwindowgui.h"
17 #include "cwindow.h"
18 #include "bchash.h"
19 #include "devicedvbinput.inc"
20 #include "editpanel.h"
21 #include "edl.h"
22 #include "edlsession.h"
23 #include "errorbox.h"
24 #include "fileformat.h"
25 #include "file.h"
26 #include "filesystem.h"
27 #include "filexml.h"
28 #include "framecache.h"
29 #include "gwindow.h"
30 #include "gwindowgui.h"
31 #include "indexfile.h"
32 #include "interlacemodes.h"
33 #include "language.h"
34 #include "levelwindowgui.h"
35 #include "levelwindow.h"
36 #include "loadfile.inc"
37 #include "localsession.h"
38 #include "maincursor.h"
39 #include "mainerror.h"
40 #include "mainindexes.h"
41 #include "mainmenu.h"
42 #include "mainprogress.h"
43 #include "mainsession.h"
44 #include "mainundo.h"
45 #include "mbuttons.h"
46 #include "mutex.h"
47 #include "mwindowgui.h"
48 #include "mwindow.h"
49 #include "new.h"
50 #include "patchbay.h"
51 #include "playback3d.h"
52 #include "playbackengine.h"
53 #include "plugin.h"
54 #include "pluginserver.h"
55 #include "pluginset.h"
56 #include "preferences.h"
57 #include "record.h"
58 #include "recordlabel.h"
59 #include "removethread.h"
60 #include "render.h"
61 #include "samplescroll.h"
62 #include "sighandler.h"
63 #include "splashgui.h"
64 #include "statusbar.h"
65 #include "theme.h"
66 #include "threadloader.h"
67 #include "timebar.h"
68 #include "tipwindow.h"
69 #include "trackcanvas.h"
70 #include "track.h"
71 #include "tracking.h"
72 #include "trackscroll.h"
73 #include "tracks.h"
74 #include "transition.h"
75 #include "transportque.h"
76 #include "vframe.h"
77 #include "videodevice.inc"
78 #include "videowindow.h"
79 #include "vplayback.h"
80 #include "vwindowgui.h"
81 #include "vwindow.h"
82 #include "wavecache.h"
83 #include "zoombar.h"
84 #include "exportedl.h"
86 #include <string.h>
90 extern "C"
96 // Hack for libdv to remove glib dependancy
98 // void
99 // g_log (const char    *log_domain,
100 //        int  log_level,
101 //        const char    *format,
102 //        ...)
103 // {
104 // }
105 // 
106 // void
107 // g_logv (const char    *log_domain,
108 //        int  log_level,
109 //        const char    *format,
110 //        ...)
111 // {
112 // }
113 // 
116 // Hack for XFree86 4.1.0
118 int atexit(void (*function)(void))
120         return 0;
129 MWindow::MWindow()
130  : Thread(1, 0, 0)
132         plugin_gui_lock = new Mutex("MWindow::plugin_gui_lock");
133         brender_lock = new Mutex("MWindow::brender_lock");
134         brender = 0;
135         session = 0;
136         channeldb_buz = new ChannelDB;
137         channeldb_v4l2jpeg = new ChannelDB;
138         dvb_input = 0;
139         dvb_input_lock = new Mutex("MWindow::dvb_input_lock");
142 MWindow::~MWindow()
144         brender_lock->lock("MWindow::~MWindow");
145         if(brender) delete brender;
146         brender = 0;
147         brender_lock->unlock();
148         delete brender_lock;
150         delete mainindexes;
152 SET_TRACE
153         clean_indexes();
154 SET_TRACE
156         save_defaults();
157 SET_TRACE
158 // Give up and go to a movie
159         exit(0);
161 SET_TRACE
162         delete mainprogress;
163         delete audio_cache;             // delete the cache after the assets
164         delete video_cache;             // delete the cache after the assets
165         delete frame_cache;
166         if(gui) delete gui;
167         delete undo;
168         delete preferences;
169         delete defaults;
170         delete render;
171 //      delete renderlist;
172         delete awindow;
173         delete vwindow;
174         delete cwindow;
175         delete lwindow;
176         plugin_guis->remove_all_objects();
177         delete plugin_guis;
178         delete plugin_gui_lock;
181 void MWindow::init_error()
183         new MainError(this);
186 void MWindow::create_defaults_path(char *string)
188 // set the .bcast path
189         FileSystem fs;
191         sprintf(string, "%s", BCASTDIR);
192         fs.complete_path(string);
193         if(!fs.is_dir(string)) 
194         {
195                 fs.create_dir(string); 
196         }
198 // load the defaults
199         strcat(string, "Cinelerra_rc");
202 void MWindow::init_defaults(BC_Hash* &defaults, char *config_path)
204         char path[BCTEXTLEN];
205 // Use user supplied path
206         if(config_path[0])
207         {
208                 strcpy(path, config_path);
209         }
210         else
211         {
212                 create_defaults_path(path);
213         }
215         defaults = new BC_Hash(path);
216         defaults->load();
219 void MWindow::init_plugin_path(Preferences *preferences, 
220         ArrayList<PluginServer*>* &plugindb,
221         FileSystem *fs,
222         SplashGUI *splash_window,
223         int *counter)
225         int result = 0;
226         PluginServer *newplugin;
228         if(!result)
229         {
230                 for(int i = 0; i < fs->dir_list.total; i++)
231                 {
232                         char path[BCTEXTLEN];
233                         strcpy(path, fs->dir_list.values[i]->path);
235 // File is a directory
236                         if(fs->is_dir(path))
237                         {
238                                 continue;
239                         }
240                         else
241                         {
242 // Try to query the plugin
243                                 fs->complete_path(path);
244 //printf("MWindow::init_plugin_path %s\n", path);
245                                 PluginServer *new_plugin = new PluginServer(path);
246                                 int result = new_plugin->open_plugin(1, preferences, 0, 0, -1);
248                                 if(!result)
249                                 {
250                                         plugindb->append(new_plugin);
251                                         new_plugin->close_plugin();
252                                         if(splash_window)
253                                                 splash_window->operation->update(_(new_plugin->title));
254                                 }
255                                 else
256                                 if(result == PLUGINSERVER_IS_LAD)
257                                 {
258                                         delete new_plugin;
259 // Open LAD subplugins
260                                         int id = 0;
261                                         do
262                                         {
263                                                 new_plugin = new PluginServer(path);
264                                                 result = new_plugin->open_plugin(1,
265                                                         preferences,
266                                                         0,
267                                                         0,
268                                                         id);
269                                                 id++;
270                                                 if(!result)
271                                                 {
272                                                         plugindb->append(new_plugin);
273                                                         new_plugin->close_plugin();
274                                                         if(splash_window)
275                                                                 splash_window->operation->update(_(new_plugin->title));
276                                                         else
277                                                         {
278                                                         }
279                                                 }
280                                         }while(!result);
281                                 }
282                                 else
283                                 {
284 // Plugin failed to open
285                                         delete new_plugin;
286                                 }
287                         }
289                         if(splash_window) splash_window->progress->update((*counter)++);
290                 }
291         }
294 void MWindow::init_plugins(Preferences *preferences, 
295         ArrayList<PluginServer*>* &plugindb,
296         SplashGUI *splash_window)
298         plugindb = new ArrayList<PluginServer*>;
302         FileSystem cinelerra_fs;
303         ArrayList<FileSystem*> lad_fs;
304         int result = 0;
306 // Get directories
307         cinelerra_fs.set_filter("[*.plugin][*.so]");
308         result = cinelerra_fs.update(preferences->global_plugin_dir);
310         if(result)
311         {
312                 fprintf(stderr, 
313                         _("MWindow::init_plugins: couldn't open %s directory\n"),
314                         preferences->global_plugin_dir);
315         }
317 // Parse LAD environment variable
318         char *env = getenv("LADSPA_PATH");
319         if(env)
320         {
321                 char string[BCTEXTLEN];
322                 char *ptr1 = env;
323                 while(ptr1)
324                 {
325                         char *ptr = strchr(ptr1, ':');
326                         char *end;
327                         if(ptr)
328                         {
329                                 end = ptr;
330                         }
331                         else
332                         {
333                                 end = env + strlen(env);
334                         }
336                         if(end > ptr1)
337                         {
338                                 int len = end - ptr1;
339                                 memcpy(string, ptr1, len);
340                                 string[len] = 0;
343                                 FileSystem *fs = new FileSystem;
344                                 lad_fs.append(fs);
345                                 fs->set_filter("*.so");
346                                 result = fs->update(string);
348                                 if(result)
349                                 {
350                                         fprintf(stderr, 
351                                                 _("MWindow::init_plugins: couldn't open %s directory\n"),
352                                                 string);
353                                 }
354                         }
356                         if(ptr)
357                                 ptr1 = ptr + 1;
358                         else
359                                 ptr1 = ptr;
360                 };
361         }
363         int total = cinelerra_fs.total_files();
364         int counter = 0;
365         for(int i = 0; i < lad_fs.total; i++)
366                 total += lad_fs.values[i]->total_files();
367         if(splash_window) splash_window->progress->update_length(total);
370 // Cinelerra
371 #ifndef DO_STATIC
372         init_plugin_path(preferences,
373                 plugindb,
374                 &cinelerra_fs,
375                 splash_window,
376                 &counter);
377 #else
378 // Call automatically generated routine to get plugins
379 #endif
381 // LAD
382         for(int i = 0; i < lad_fs.total; i++)
383                 init_plugin_path(preferences,
384                         plugindb,
385                         lad_fs.values[i],
386                         splash_window,
387                         &counter);
389         lad_fs.remove_all_objects();
392 void MWindow::delete_plugins()
394         for(int i = 0; i < plugindb->total; i++)
395         {
396                 delete plugindb->values[i];
397         }
398         delete plugindb;
401 void MWindow::create_plugindb(int do_audio, 
402                 int do_video, 
403                 int is_realtime, 
404                 int is_transition,
405                 int is_theme,
406                 ArrayList<PluginServer*> &plugindb)
408 // Get plugins
409         for(int i = 0; i < this->plugindb->total; i++)
410         {
411                 PluginServer *current = this->plugindb->values[i];
413                 if(current->audio == do_audio &&
414                         current->video == do_video &&
415                         (current->realtime == is_realtime || is_realtime < 0) &&
416                         current->transition == is_transition &&
417                         current->theme == is_theme)
418                         plugindb.append(current);
419         }
421 // Alphabetize list by title
422         int done = 0;
423         while(!done)
424         {
425                 done = 1;
426                 
427                 for(int i = 0; i < plugindb.total - 1; i++)
428                 {
429                         PluginServer *value1 = plugindb.values[i];
430                         PluginServer *value2 = plugindb.values[i + 1];
431                         if(strcmp(_(value1->title), _(value2->title)) > 0)
432                         {
433                                 done = 0;
434                                 plugindb.values[i] = value2;
435                                 plugindb.values[i + 1] = value1;
436                         }
437                 }
438         }
441 PluginServer* MWindow::scan_plugindb(char *title,
442                 int data_type)
444         if(data_type < 0)
445         {
446                 printf("MWindow::scan_plugindb data_type < 0\n");
447                 return 0;
448         }
450         for(int i = 0; i < plugindb->total; i++)
451         {
452                 PluginServer *server = plugindb->values[i];
453                 if(!strcasecmp(server->title, title) &&
454                 ((data_type == TRACK_AUDIO && server->audio) ||
455                 (data_type == TRACK_VIDEO && server->video))) 
456                         return plugindb->values[i];
457         }
458         return 0;
461 void MWindow::init_preferences()
463         preferences = new Preferences;
464         preferences->load_defaults(defaults);
465         session = new MainSession(this);
466         session->load_defaults(defaults);
469 void MWindow::clean_indexes()
471         FileSystem fs;
472         int total_excess;
473         long oldest;
474         int oldest_item = -1;
475         int result;
476         char string[BCTEXTLEN];
477         char string2[BCTEXTLEN];
479 // Delete extra indexes
480         fs.set_filter("*.idx");
481         fs.complete_path(preferences->index_directory);
482         fs.update(preferences->index_directory);
483 //printf("MWindow::clean_indexes 1 %d\n", fs.dir_list.total);
485 // Eliminate directories
486         result = 1;
487         while(result)
488         {
489                 result = 0;
490                 for(int i = 0; i < fs.dir_list.total && !result; i++)
491                 {
492                         fs.join_names(string, preferences->index_directory, fs.dir_list.values[i]->name);
493                         if(fs.is_dir(string))
494                         {
495                                 delete fs.dir_list.values[i];
496                                 fs.dir_list.remove_number(i);
497                                 result = 1;
498                         }
499                 }
500         }
501         total_excess = fs.dir_list.total - preferences->index_count;
503 //printf("MWindow::clean_indexes 2 %d\n", fs.dir_list.total);
504         while(total_excess > 0)
505         {
506 // Get oldest
507                 for(int i = 0; i < fs.dir_list.total; i++)
508                 {
509                         fs.join_names(string, preferences->index_directory, fs.dir_list.values[i]->name);
511                         if(i == 0 || fs.get_date(string) <= oldest)
512                         {
513                                 oldest = fs.get_date(string);
514                                 oldest_item = i;
515                         }
516                 }
518                 if(oldest_item >= 0)
519                 {
520 // Remove index file
521                         fs.join_names(string, 
522                                 preferences->index_directory, 
523                                 fs.dir_list.values[oldest_item]->name);
524 //printf("MWindow::clean_indexes 1 %s\n", string);
525                         if(remove(string))
526                                 perror("delete_indexes");
527                         delete fs.dir_list.values[oldest_item];
528                         fs.dir_list.remove_number(oldest_item);
530 // Remove table of contents if it exists
531                         strcpy(string2, string);
532                         char *ptr = strrchr(string2, '.');
533                         if(ptr)
534                         {
535 //printf("MWindow::clean_indexes 2 %s\n", string2);
536                                 sprintf(ptr, ".toc");
537                                 remove(string2);
538                         }
539                 }
541                 total_excess--;
542         }
545 void MWindow::init_awindow()
547         awindow = new AWindow(this);
548         awindow->create_objects();
551 void MWindow::init_gwindow()
553         gwindow = new GWindow(this);
554         gwindow->create_objects();
557 void MWindow::init_tipwindow()
559         twindow = new TipWindow(this);
560         twindow->start();
563 void MWindow::init_theme()
565         theme = 0;
567 // Replace blond theme with SUV since it doesn't work
568         if(!strcasecmp(preferences->theme, "Blond"))
569                 strcpy(preferences->theme, DEFAULT_THEME);
571         for(int i = 0; i < plugindb->total; i++)
572         {
573                 if(plugindb->values[i]->theme &&
574                         !strcasecmp(preferences->theme, plugindb->values[i]->title))
575                 {
576                         PluginServer plugin = *plugindb->values[i];
577                         plugin.open_plugin(0, preferences, 0, 0, -1);
578                         theme = plugin.new_theme();
579                         theme->mwindow = this;
580                         strcpy(theme->path, plugin.path);
581                         plugin.close_plugin();
582                 }
583         }
585         if(!theme)
586         {
587                 fprintf(stderr, _("MWindow::init_theme: theme %s not found.\n"), preferences->theme);
588                 exit(1);
589         }
591 // Load images which may have been forgotten
592         theme->Theme::initialize();
593 // Load user images
594         theme->initialize();
595 // Create menus with user colors
596         theme->build_menus();
597         init_menus();
599         theme->check_used();
602 void MWindow::init_3d()
604         playback_3d = new Playback3D(this);
605         playback_3d->create_objects();
608 void MWindow::init_edl()
610         edl = new EDL;
611         edl->create_objects();
612     edl->load_defaults(defaults);
613         edl->create_default_tracks();
614         edl->tracks->update_y_pixels(theme);
617 void MWindow::init_compositor()
619         cwindow = new CWindow(this);
620     cwindow->create_objects();
623 void MWindow::init_levelwindow()
625         lwindow = new LevelWindow(this);
626         lwindow->create_objects();
629 void MWindow::init_viewer()
631         vwindow = new VWindow(this);
632         vwindow->load_defaults();
633         vwindow->create_objects();
636 void MWindow::init_cache()
638         audio_cache = new CICache(preferences, plugindb);
639         video_cache = new CICache(preferences, plugindb);
640         frame_cache = new FrameCache;
641         wave_cache = new WaveCache;
644 void MWindow::init_channeldb()
646         channeldb_buz->load("channeldb_buz");
647         channeldb_v4l2jpeg->load("channeldb_v4l2jpeg");
650 void MWindow::init_menus()
652         char string[BCTEXTLEN];
654         // Color Models
655         cmodel_to_text(string, BC_RGB888);
656         colormodels.append(new ColormodelItem(string, BC_RGB888));
657         cmodel_to_text(string, BC_RGBA8888);
658         colormodels.append(new ColormodelItem(string, BC_RGBA8888));
659 //      cmodel_to_text(string, BC_RGB161616);
660 //      colormodels.append(new ColormodelItem(string, BC_RGB161616));
661 //      cmodel_to_text(string, BC_RGBA16161616);
662 //      colormodels.append(new ColormodelItem(string, BC_RGBA16161616));
663         cmodel_to_text(string, BC_RGB_FLOAT);
664         colormodels.append(new ColormodelItem(string, BC_RGB_FLOAT));
665         cmodel_to_text(string, BC_RGBA_FLOAT);
666         colormodels.append(new ColormodelItem(string, BC_RGBA_FLOAT));
667         cmodel_to_text(string, BC_YUV888);
668         colormodels.append(new ColormodelItem(string, BC_YUV888));
669         cmodel_to_text(string, BC_YUVA8888);
670         colormodels.append(new ColormodelItem(string, BC_YUVA8888));
671 //      cmodel_to_text(string, BC_YUV161616);
672 //      colormodels.append(new ColormodelItem(string, BC_YUV161616));
673 //      cmodel_to_text(string, BC_YUVA16161616);
674 //      colormodels.append(new ColormodelItem(string, BC_YUVA16161616));
676 #define ILACEPROJECTMODELISTADD(x) ilacemode_to_text(string, x); \
677                            interlace_project_modes.append(new InterlacemodeItem(string, x));
679 #define ILACEASSETMODELISTADD(x) ilacemode_to_text(string, x); \
680                            interlace_asset_modes.append(new InterlacemodeItem(string, x));
682 #define ILACEFIXMETHODLISTADD(x) ilacefixmethod_to_text(string, x); \
683                            interlace_asset_fixmethods.append(new InterlacefixmethodItem(string, x));
685         // Interlacing Modes
686         ILACEASSETMODELISTADD(BC_ILACE_MODE_UNDETECTED); // Not included in the list for the project options.
688         ILACEASSETMODELISTADD(BC_ILACE_MODE_TOP_FIRST);
689         ILACEPROJECTMODELISTADD(BC_ILACE_MODE_TOP_FIRST);
691         ILACEASSETMODELISTADD(BC_ILACE_MODE_BOTTOM_FIRST);
692         ILACEPROJECTMODELISTADD(BC_ILACE_MODE_BOTTOM_FIRST);
694         ILACEASSETMODELISTADD(BC_ILACE_MODE_NOTINTERLACED);
695         ILACEPROJECTMODELISTADD(BC_ILACE_MODE_NOTINTERLACED);
697         // Interlacing Fixing Methods
698         ILACEFIXMETHODLISTADD(BC_ILACE_FIXMETHOD_NONE);
699         ILACEFIXMETHODLISTADD(BC_ILACE_FIXMETHOD_UPONE);
700         ILACEFIXMETHODLISTADD(BC_ILACE_FIXMETHOD_DOWNONE);
703 void MWindow::init_indexes()
705         mainindexes = new MainIndexes(this);
706         mainindexes->start_loop();
709 void MWindow::init_gui()
711         gui = new MWindowGUI(this);
712         gui->create_objects();
713         gui->load_defaults(defaults);
716 void MWindow::init_signals()
718         sighandler = new SigHandler;
719         sighandler->initialize();
720 ENABLE_BUFFER
723 void MWindow::init_render()
725         render = new Render(this);
726 //      renderlist = new Render(this);
727         batch_render = new BatchRenderThread(this);
730 void MWindow::init_exportedl()
732         exportedl = new ExportEDL(this);
735 void MWindow::init_brender()
737         if(preferences->use_brender && !brender)
738         {
739                 brender_lock->lock("MWindow::init_brender 1");
740                 brender = new BRender(this);
741                 brender->initialize();
742                 session->brender_end = 0;
743                 brender_lock->unlock();
744         }
745         else
746         if(!preferences->use_brender && brender)
747         {
748                 brender_lock->lock("MWindow::init_brender 2");
749                 delete brender;
750                 brender = 0;
751                 session->brender_end = 0;
752                 brender_lock->unlock();
753         }
754         if(brender) brender->restart(edl);
757 void MWindow::restart_brender()
759 //printf("MWindow::restart_brender 1\n");
760         if(brender) brender->restart(edl);
763 void MWindow::stop_brender()
765         if(brender) brender->stop();
768 int MWindow::brender_available(int position)
770         int result = 0;
771         brender_lock->lock("MWindow::brender_available 1");
772         if(brender)
773         {
774                 if(brender->map_valid)
775                 {
776                         brender->map_lock->lock("MWindow::brender_available 2");
777                         if(position < brender->map_size &&
778                                 position >= 0)
779                         {
780 //printf("MWindow::brender_available 1 %d %d\n", position, brender->map[position]);
781                                 if(brender->map[position] == BRender::RENDERED)
782                                         result = 1;
783                         }
784                         brender->map_lock->unlock();
785                 }
786         }
787         brender_lock->unlock();
788         return result;
791 void MWindow::set_brender_start()
793         edl->session->brender_start = edl->local_session->get_selectionstart();
794         restart_brender();
795         gui->canvas->draw_overlays();
796         gui->canvas->flash();
801 int MWindow::load_filenames(ArrayList<char*> *filenames, 
802         int load_mode,
803         int update_filename,
804         char *reel_name,
805         int reel_number,
806         int overwrite_reel)
808 SET_TRACE
809         ArrayList<EDL*> new_edls;
810         ArrayList<Asset*> new_assets;
811         ArrayList<File*> new_files;
813         save_defaults();
814         gui->start_hourglass();
816 // Need to stop playback since tracking depends on the EDL not getting
817 // deleted.
818         cwindow->playback_engine->que->send_command(STOP,
819                 CHANGE_NONE, 
820                 0,
821                 0);
822         vwindow->playback_engine->que->send_command(STOP,
823                 CHANGE_NONE, 
824                 0,
825                 0);
826         cwindow->playback_engine->interrupt_playback(0);
827         vwindow->playback_engine->interrupt_playback(0);
831 // Define new_edls and new_assets to load
832         int result = 0;
833         for(int i = 0; i < filenames->total; i++)
834         {
835 // Get type of file
836                 File *new_file = new File;
837                 Asset *new_asset = new Asset(filenames->values[i]);
838                 EDL *new_edl = new EDL;
839                 char string[BCTEXTLEN];
841 // Set reel name and number for the asset
842 // If the user wants to overwrite the last used reel number for the clip,
843 // we have to rebuild the index for the file
845                 if(overwrite_reel)
846                 {
847                         char source_filename[BCTEXTLEN];
848                         char index_filename[BCTEXTLEN];
849                         
850                         strcpy(new_asset->reel_name, reel_name);
851                         new_asset->reel_number = reel_number;
853                         IndexFile::get_index_filename(source_filename,
854                                 preferences->index_directory,
855                                 index_filename,
856                                 new_asset->path);
857                         remove(index_filename);
858                         new_asset->index_status = INDEX_NOTTESTED;
859                 }
860                 
861                 new_edl->create_objects();
862                 new_edl->copy_session(edl);
864                 sprintf(string, "Loading %s", new_asset->path);
865                 gui->show_message(string);
866 SET_TRACE
867                 result = new_file->open_file(preferences, new_asset, 1, 0, 0, 0);
868 SET_TRACE
870                 switch(result)
871                 {
872 // Convert media file to EDL
873                         case FILE_OK:
874 // Warn about odd image dimensions
875                                 if(new_asset->video_data &&
876                                         ((new_asset->width % 2) ||
877                                         (new_asset->height % 2)))
878                                 {
879                                         char string[BCTEXTLEN];
880                                         sprintf(string, "%s's resolution is %dx%d.\nImages with odd dimensions may not decode properly.",
881                                                 new_asset->path,
882                                                 new_asset->width,
883                                                 new_asset->height);
884                                         MainError::show_error(string);
885                                 }
888                                 if(load_mode != LOAD_RESOURCESONLY)
889                                 {
890 SET_TRACE
891                                         asset_to_edl(new_edl, new_asset);
892 SET_TRACE
893                                         new_edls.append(new_edl);
894 SET_TRACE
895                                         Garbage::delete_object(new_asset);
896                                         new_asset = 0;
897 SET_TRACE
898                                 }
899                                 else
900                                 {
901                                         new_assets.append(new_asset);
902                                 }
904 // Set filename to nothing for assets since save EDL would overwrite them.
905                                 if(load_mode == LOAD_REPLACE || 
906                                         load_mode == LOAD_REPLACE_CONCATENATE)
907                                 {
908                                         set_filename("");
909 // Reset timeline position
910                                         new_edl->local_session->view_start = 0;
911                                         new_edl->local_session->track_start = 0;
912                                 }
914                                 result = 0;
915                                 break;
917 // File not found
918                         case FILE_NOT_FOUND:
919                                 sprintf(string, _("Failed to open %s"), new_asset->path);
920                                 gui->show_message(string, theme->message_error);
921                                 result = 1;
922                                 break;
924 // Unknown format
925                         case FILE_UNRECOGNIZED_CODEC:
926                         {
927 // Test index file
928                                 IndexFile indexfile(this);
929                                 result = indexfile.open_index(this, new_asset);
930                                 if(!result)
931                                 {
932                                         indexfile.close_index();
933                                 }
935 // Test existing EDLs
936                                 if(result)
937                                 {
938                                         for(int j = 0; j < new_edls.total + 1; j++)
939                                         {
940                                                 Asset *old_asset;
941                                                 if(j == new_edls.total)
942                                                 {
943                                                         if(old_asset = edl->assets->get_asset(new_asset->path))
944                                                         {
945                                                                 *new_asset = *old_asset;
946                                                                 result = 0;
947                                                         }
948                                                 }
949                                                 else
950                                                 {
951                                                         if(old_asset = new_edls.values[j]->assets->get_asset(new_asset->path))
952                                                         {
953                                                                 *new_asset = *old_asset;
954                                                                 result = 0;
955                                                         }
956                                                 }
957                                         }
958                                 }
960 // Prompt user
961                                 if(result)
962                                 {
963                                         char string[BCTEXTLEN];
964                                         FileSystem fs;
965                                         fs.extract_name(string, new_asset->path);
967                                         strcat(string, _("'s format couldn't be determined."));
968                                         new_asset->audio_data = 1;
969                                         new_asset->format = FILE_PCM;
970                                         new_asset->channels = defaults->get("AUDIO_CHANNELS", 2);
971                                         new_asset->sample_rate = defaults->get("SAMPLE_RATE", 44100);
972                                         new_asset->bits = defaults->get("AUDIO_BITS", 16);
973                                         new_asset->byte_order = defaults->get("BYTE_ORDER", 1);
974                                         new_asset->signed_ = defaults->get("SIGNED_", 1);
975                                         new_asset->header = defaults->get("HEADER", 0);
977                                         FileFormat fwindow(this);
978                                         fwindow.create_objects(new_asset, string);
979                                         result = fwindow.run_window();
981                                         defaults->update("AUDIO_CHANNELS", new_asset->channels);
982                                         defaults->update("SAMPLE_RATE", new_asset->sample_rate);
983                                         defaults->update("AUDIO_BITS", new_asset->bits);
984                                         defaults->update("BYTE_ORDER", new_asset->byte_order);
985                                         defaults->update("SIGNED_", new_asset->signed_);
986                                         defaults->update("HEADER", new_asset->header);
987                                         save_defaults();
988                                 }
990 // Append to list
991                                 if(!result)
992                                 {
993 // Recalculate length
994                                         delete new_file;
995                                         new_file = new File;
996                                         result = new_file->open_file(preferences, new_asset, 1, 0, 0, 0);
998                                         if(load_mode != LOAD_RESOURCESONLY)
999                                         {
1000                                                 asset_to_edl(new_edl, new_asset);
1001                                                 new_edls.append(new_edl);
1002                                                 Garbage::delete_object(new_asset);
1003                                                 new_asset = 0;
1004                                         }
1005                                         else
1006                                         {
1007                                                 new_assets.append(new_asset);
1008                                         }
1009                                 }
1010                                 else
1011                                 {
1012                                         result = 1;
1013                                 }
1014                                 break;
1015                         }
1017                         case FILE_IS_XML:
1018                         {
1019                                 FileXML xml_file;
1020                                 xml_file.read_from_file(filenames->values[i]);
1021 // Load EDL for pasting
1022                                 new_edl->load_xml(plugindb, &xml_file, LOAD_ALL);
1023                                 test_plugins(new_edl, filenames->values[i]);
1025 // We don't want a valid reel name/number for projects
1026                                 strcpy(new_asset->reel_name, "");
1027                                 reel_number = -1;
1029                                 if(load_mode == LOAD_REPLACE || 
1030                                         load_mode == LOAD_REPLACE_CONCATENATE)
1031                                 {
1032                                         strcpy(session->filename, filenames->values[i]);
1033                                         strcpy(new_edl->local_session->clip_title, filenames->values[i]);
1034                                         if(update_filename)
1035                                                 set_filename(new_edl->local_session->clip_title);
1036                                 }
1038                                 new_edls.append(new_edl);
1039                                 result = 0;
1040                                 break;
1041                         }
1042                 }
1044 SET_TRACE
1045                 if(result)
1046                 {
1047                         delete new_edl;
1048                         Garbage::delete_object(new_asset);
1049                         new_edl = 0;
1050                         new_asset = 0;
1051                 }
1052 SET_TRACE
1054 // Store for testing index
1055                 new_files.append(new_file);
1056 SET_TRACE
1057         }
1061         if(!result) gui->statusbar->default_message();
1070 // Paste them.
1071 // Don't back up here.
1072         if(new_edls.total)
1073         {
1074 // For pasting, clear the active region
1075                 if(load_mode == LOAD_PASTE)
1076                 {
1077                         double start = edl->local_session->get_selectionstart();
1078                         double end = edl->local_session->get_selectionend();
1079                         if(!EQUIV(start, end))
1080                                 edl->clear(start, 
1081                                         end,
1082                                         edl->session->labels_follow_edits,
1083                                         edl->session->plugins_follow_edits);
1084                 }
1086                 paste_edls(&new_edls, 
1087                         load_mode,
1088                         0,
1089                         -1,
1090                         edl->session->labels_follow_edits, 
1091                         edl->session->plugins_follow_edits,
1092                         0); // overwrite
1093         }
1100 // Add new assets to EDL and schedule assets for index building.
1101 // Used for loading resources only.
1102         if(new_assets.total)
1103         {
1104                 for(int i = 0; i < new_assets.total; i++)
1105                 {
1106                         Asset *new_asset = new_assets.values[i];
1107                         File *new_file = 0;
1108                         File *index_file = 0;
1109                         int got_it = 0;
1110                         for(int j = 0; j < new_files.total; j++)
1111                         {
1112                                 new_file = new_files.values[j];
1113                                 if(!strcmp(new_file->asset->path,
1114                                         new_asset->path))
1115                                 {
1116                                         got_it = 1;
1117                                         break;
1118                                 }
1119                         }
1121                         mainindexes->add_next_asset(got_it ? new_file : 0, 
1122                                 new_asset);
1123                         edl->assets->update(new_asset);
1125                 }
1128 // Start examining next batch of index files
1129                 mainindexes->start_build();
1130         }
1133         update_project(load_mode);
1134 SET_TRACE
1138         new_edls.remove_all_objects();
1139 SET_TRACE
1140         for(int i = 0; i < new_assets.total; i++)
1141                 Garbage::delete_object(new_assets.values[i]);
1142 SET_TRACE
1143         new_assets.remove_all();
1144 SET_TRACE
1145         new_files.remove_all_objects();
1147 SET_TRACE
1148         undo->update_undo(_("load"), LOAD_ALL, 0);
1151 SET_TRACE
1152         gui->stop_hourglass();
1153 SET_TRACE
1155         return 0;
1161 void MWindow::test_plugins(EDL *new_edl, char *path)
1163         char string[BCTEXTLEN];
1164 // Do a check weather plugins exist
1165         for(Track *track = new_edl->tracks->first; track; track = track->next)
1166         {
1167                 for(int k = 0; k < track->plugin_set.total; k++)
1168                 {
1169                         PluginSet *plugin_set = track->plugin_set.values[k];
1170                         for(Plugin *plugin = (Plugin*)plugin_set->first; 
1171                         plugin; 
1172                         plugin = (Plugin*)plugin->next)
1173                         {
1174                                 if(plugin->plugin_type == PLUGIN_STANDALONE)
1175                                 {
1176                                         // ok we need to find it in plugindb
1177                                         int plugin_found = 0;
1178                                         for(int j = 0; j < plugindb->total; j++)
1179                                         {
1180                                                 PluginServer *server = plugindb->values[j];
1181                                                 if(!strcasecmp(server->title, plugin->title) &&
1182                                                         ((track->data_type == TRACK_AUDIO && server->audio) ||
1183                                                         (track->data_type == TRACK_VIDEO && server->video)) &&
1184                                                         (!server->transition))
1185                                                         plugin_found = 1;
1186                                         }
1187                                         if (!plugin_found) 
1188                                         {
1189                                                 sprintf(string, 
1190                                                         "The effect '%s' in file '%s' is not part of your installation of Cinelerra.\n"
1191                                                         "The project won't be rendered as it was meant and Cinelerra might crash.\n",
1192                                                         plugin->title, 
1193                                                         path); 
1194                                                 MainError::show_error(string);
1195                                         }
1196                                 }
1197                         }
1198                 }
1199                 for(Edit *edit = (Edit*)track->edits->first; 
1200                 edit; 
1201                 edit = (Edit*)edit->next)
1202                 {
1203                         if (edit->transition)
1204                         {
1205                                 // ok we need to find transition in plugindb
1206                                 int transition_found = 0;
1207                                 for(int j = 0; j < plugindb->total; j++)
1208                                 {
1209                                         PluginServer *server = plugindb->values[j];
1210                                         if(!strcasecmp(server->title, edit->transition->title) &&
1211                                                 ((track->data_type == TRACK_AUDIO && server->audio) ||
1212                                                 (track->data_type == TRACK_VIDEO && server->video)) &&
1213                                                 (server->transition))
1214                                                 transition_found = 1;
1215                                 }
1216                                 if (!transition_found) 
1217                                 {
1218                                         sprintf(string, 
1219                                                 "The transition '%s' in file '%s' is not part of your installation of Cinelerra.\n"
1220                                                 "The project won't be rendered as it was meant and Cinelerra might crash.\n",
1221                                                 edit->transition->title, 
1222                                                 path); 
1223                                         MainError::show_error(string);
1224                                 }
1225                         }
1226                 }
1227         }
1231 void MWindow::init_shm()
1233 // Fix shared memory
1234         FILE *fd = fopen("/proc/sys/kernel/shmmax", "w");
1235         if(fd)
1236         {
1237                 fprintf(fd, "0x7fffffff");
1238                 fclose(fd);
1239         }
1240         fd = 0;
1242         fd = fopen("/proc/sys/kernel/shmmax", "r");
1243         if(!fd)
1244         {
1245                 MainError::show_error("MWindow::init_shm: couldn't open /proc/sys/kernel/shmmax for reading.\n");
1246                 return;
1247         }
1249         int64_t result = 0;
1250         fscanf(fd, "%lld", &result);
1251         fclose(fd);
1252         fd = 0;
1253         if(result < 0x7fffffff)
1254         {
1255                 eprintf("WARNING: /proc/sys/kernel/shmmax is 0x%llx, which is too low.\n"
1256                         "Before running Cinelerra do the following as root:\n"
1257                         "echo \"0x7fffffff\" > /proc/sys/kernel/shmmax\n",
1258                         result);
1259         }
1264 void MWindow::create_objects(int want_gui, 
1265         int want_new,
1266         char *config_path)
1268         char string[BCTEXTLEN];
1269         FileSystem fs;
1270         edl = 0;
1274         init_3d();
1275         remove_thread = new RemoveThread;
1276         remove_thread->create_objects();
1277         show_splash();
1279 // For some reason, init_signals must come after show_splash or the signals won't
1280 // get trapped.
1281         init_signals();
1283         init_error();
1285 SET_TRACE
1287         init_defaults(defaults, config_path);
1288 SET_TRACE
1289         init_preferences();
1290 SET_TRACE
1291         init_plugins(preferences, plugindb, splash_window);
1292         if(splash_window) splash_window->operation->update(_("Initializing GUI"));
1293 SET_TRACE
1294         init_theme();
1295 // Default project created here
1296 SET_TRACE
1297         init_edl();
1299 SET_TRACE
1300         init_awindow();
1301 SET_TRACE
1302         init_compositor();
1303 SET_TRACE
1304         init_levelwindow();
1305 SET_TRACE
1306         init_viewer();
1307 SET_TRACE
1308         init_cache();
1309 SET_TRACE
1310         init_indexes();
1311 SET_TRACE
1312         init_channeldb();
1313 SET_TRACE
1315         init_gui();
1316         init_gwindow();
1317 SET_TRACE
1318         init_render();
1319         init_brender();
1320         init_exportedl();
1321         mainprogress = new MainProgress(this, gui);
1322         undo = new MainUndo(this);
1324         plugin_guis = new ArrayList<PluginServer*>;
1326 SET_TRACE
1327         if(session->show_vwindow) vwindow->gui->show_window();
1328         if(session->show_cwindow) cwindow->gui->show_window();
1329         if(session->show_awindow) awindow->gui->show_window();
1330         if(session->show_lwindow) lwindow->gui->show_window();
1331         if(session->show_gwindow) gwindow->gui->show_window();
1332 SET_TRACE
1335         gui->mainmenu->load_defaults(defaults);
1336 SET_TRACE
1337         gui->mainmenu->update_toggles(0);
1338 SET_TRACE
1339         gui->patchbay->update();
1340 SET_TRACE
1341         gui->canvas->draw();
1342 SET_TRACE
1343         gui->cursor->draw(1);
1344 SET_TRACE
1345         gui->show_window();
1346         gui->raise_window();
1348         if(preferences->use_tipwindow)
1349                 init_tipwindow();
1350                 
1351 SET_TRACE
1353         hide_splash();
1354 SET_TRACE
1355         init_shm();
1359 void MWindow::show_splash()
1361 #include "data/heroine_logo12_png.h"
1362         VFrame *frame = new VFrame(heroine_logo12_png);
1363         BC_DisplayInfo display_info;
1364         splash_window = new SplashGUI(frame, 
1365                 display_info.get_root_w() / 2 - frame->get_w() / 2,
1366                 display_info.get_root_h() / 2 - frame->get_h() / 2);
1367         splash_window->create_objects();
1370 void MWindow::hide_splash()
1372         if(splash_window)
1373                 delete splash_window;
1374         splash_window = 0;
1378 void MWindow::start()
1380 ENABLE_BUFFER
1381         vwindow->start();
1382         awindow->start();
1383         cwindow->start();
1384         lwindow->start();
1385         gwindow->start();
1386         Thread::start();
1387         playback_3d->start();
1390 void MWindow::run()
1392         gui->run_window();
1395 void MWindow::show_vwindow()
1397         session->show_vwindow = 1;
1398         vwindow->gui->lock_window("MWindow::show_vwindow");
1399         vwindow->gui->show_window();
1400         vwindow->gui->raise_window();
1401         vwindow->gui->flush();
1402         vwindow->gui->unlock_window();
1403         gui->mainmenu->show_vwindow->set_checked(1);
1406 void MWindow::show_awindow()
1408         session->show_awindow = 1;
1409         awindow->gui->lock_window("MWindow::show_awindow");
1410         awindow->gui->show_window();
1411         awindow->gui->raise_window();
1412         awindow->gui->flush();
1413         awindow->gui->unlock_window();
1414         gui->mainmenu->show_awindow->set_checked(1);
1417 void MWindow::show_cwindow()
1419         session->show_cwindow = 1;
1420         cwindow->show_window();
1421         gui->mainmenu->show_cwindow->set_checked(1);
1424 void MWindow::show_gwindow()
1426         session->show_gwindow = 1;
1428         gwindow->gui->lock_window("MWindow::show_gwindow");
1429         gwindow->gui->show_window();
1430         gwindow->gui->raise_window();
1431         gwindow->gui->flush();
1432         gwindow->gui->unlock_window();
1434         gui->mainmenu->show_gwindow->set_checked(1);
1437 void MWindow::show_lwindow()
1439         session->show_lwindow = 1;
1440         lwindow->gui->lock_window("MWindow::show_lwindow");
1441         lwindow->gui->show_window();
1442         lwindow->gui->raise_window();
1443         lwindow->gui->flush();
1444         lwindow->gui->unlock_window();
1445         gui->mainmenu->show_lwindow->set_checked(1);
1448 void MWindow::tile_windows()
1450         session->default_window_positions();
1451         gui->default_positions();
1454 void MWindow::toggle_loop_playback()
1456         edl->local_session->loop_playback = !edl->local_session->loop_playback;
1457         set_loop_boundaries();
1458         save_backup();
1460         gui->canvas->draw_overlays();
1461         gui->canvas->flash();
1462         sync_parameters(CHANGE_PARAMS);
1465 void MWindow::set_titles(int value)
1467         edl->session->show_titles = value;
1468         trackmovement(edl->local_session->track_start);
1471 void MWindow::set_auto_keyframes(int value)
1473         gui->lock_window("MWindow::set_auto_keyframes");
1474         edl->session->auto_keyframes = value;
1475         gui->mbuttons->edit_panel->keyframe->update(value);
1476         gui->flush();
1477         gui->unlock_window();
1478         cwindow->gui->lock_window("MWindow::set_auto_keyframes");
1479         cwindow->gui->edit_panel->keyframe->update(value);
1480         cwindow->gui->flush();
1481         cwindow->gui->unlock_window();
1484 int MWindow::set_editing_mode(int new_editing_mode)
1486         gui->lock_window("MWindow::set_editing_mode");
1487         edl->session->editing_mode = new_editing_mode;
1488         gui->mbuttons->edit_panel->editing_mode = edl->session->editing_mode;
1489         gui->mbuttons->edit_panel->update();
1490         gui->canvas->update_cursor();
1491         gui->unlock_window();
1492         cwindow->gui->lock_window("MWindow::set_editing_mode");
1493         cwindow->gui->edit_panel->update();
1494         cwindow->gui->edit_panel->editing_mode = edl->session->editing_mode;
1495         cwindow->gui->unlock_window();
1496         return 0;
1499 void MWindow::set_labels_follow_edits(int value)
1501         gui->lock_window("MWindow::set_labels_follow_edits");
1502         edl->session->labels_follow_edits = value;
1503         gui->mbuttons->edit_panel->locklabels->update(value);
1504         gui->mainmenu->labels_follow_edits->set_checked(value);
1505         gui->flush();
1506         gui->unlock_window();
1509 void MWindow::sync_parameters(int change_type)
1512 // Sync engines which are playing back
1513         if(cwindow->playback_engine->is_playing_back)
1514         {
1515                 if(change_type == CHANGE_PARAMS)
1516                 {
1517 // TODO: block keyframes until synchronization is done
1518                         cwindow->playback_engine->sync_parameters(edl);
1519                 }
1520                 else
1521 // Stop and restart
1522                 {
1523                         int command = cwindow->playback_engine->command->command;
1524                         cwindow->playback_engine->que->send_command(STOP,
1525                                 CHANGE_NONE, 
1526                                 0,
1527                                 0);
1528 // Waiting for tracking to finish would make the restart position more
1529 // accurate but it can't lock the window to stop tracking for some reason.
1530 // Not waiting for tracking gives a faster response but restart position is
1531 // only as accurate as the last tracking update.
1532                         cwindow->playback_engine->interrupt_playback(0);
1533                         cwindow->playback_engine->que->send_command(command,
1534                                         change_type, 
1535                                         edl,
1536                                         1,
1537                                         0);
1538                 }
1539         }
1540         else
1541         {
1542                 cwindow->playback_engine->que->send_command(CURRENT_FRAME, 
1543                                                         change_type,
1544                                                         edl,
1545                                                         1);
1546         }
1549 void MWindow::age_caches()
1551         int64_t prev_memory_usage;
1552         int64_t memory_usage;
1553         int result = 0;
1554         do
1555         {
1556                 memory_usage = audio_cache->get_memory_usage(1) +
1557                         video_cache->get_memory_usage(1) +
1558                         frame_cache->get_memory_usage() +
1559                         wave_cache->get_memory_usage();
1561                 if(memory_usage > preferences->cache_size)
1562                 {
1563                         int target = 1;
1564                         int oldest1 = audio_cache->get_oldest();
1565                         int oldest2 = video_cache->get_oldest();
1566                         if(oldest2 < oldest1) target = 2;
1567                         int oldest3 = frame_cache->get_oldest();
1568                         if(oldest3 < oldest1 && oldest3 < oldest2) target = 3;
1569                         int oldest4 = wave_cache->get_oldest();
1570                         if(oldest4 < oldest3 && oldest4 < oldest2 && oldest4 < oldest1) target = 4;
1571                         switch(target)
1572                         {
1573                                 case 1: audio_cache->delete_oldest(); break;
1574                                 case 2: video_cache->delete_oldest(); break;
1575                                 case 3: frame_cache->delete_oldest(); break;
1576                                 case 4: wave_cache->delete_oldest(); break;
1577                         }
1578                 }
1579                 prev_memory_usage = memory_usage;
1580                 memory_usage = audio_cache->get_memory_usage(1) +
1581                         video_cache->get_memory_usage(1) +
1582                         frame_cache->get_memory_usage() +
1583                         wave_cache->get_memory_usage();
1584         }while(!result && 
1585                 prev_memory_usage != memory_usage && 
1586                 memory_usage > preferences->cache_size);
1589 void MWindow::show_plugin(Plugin *plugin)
1591         int done = 0;
1592 //printf("MWindow::show_plugin 1\n");
1593         plugin_gui_lock->lock("MWindow::show_plugin");
1594         for(int i = 0; i < plugin_guis->total; i++)
1595         {
1596 // Pointer comparison
1597                 if(plugin_guis->values[i]->plugin == plugin)
1598                 {
1599                         plugin_guis->values[i]->raise_window();
1600                         done = 1;
1601                         break;
1602                 }
1603         }
1605 //printf("MWindow::show_plugin 1\n");
1606         if(!done)
1607         {
1608                 if(!plugin->track)
1609                 {
1610                         printf("MWindow::show_plugin track not defined.\n");
1611                 }
1612                 PluginServer *server = scan_plugindb(plugin->title,
1613                         plugin->track->data_type);
1615 //printf("MWindow::show_plugin %p %d\n", server, server->uses_gui);
1616                 if(server && server->uses_gui)
1617                 {
1618                         PluginServer *gui = plugin_guis->append(new PluginServer(*server));
1619 // Needs mwindow to do GUI
1620                         gui->set_mwindow(this);
1621                         gui->open_plugin(0, preferences, edl, plugin, -1);
1622                         gui->show_gui();
1623                         plugin->show = 1;
1624                 }
1625         }
1626         plugin_gui_lock->unlock();
1627 //printf("MWindow::show_plugin 2\n");
1630 void MWindow::hide_plugin(Plugin *plugin, int lock)
1632         plugin->show = 0;
1633         gui->lock_window("MWindow::hide_plugin");
1634         gui->update(0, 1, 0, 0, 0, 0, 0);
1635         gui->unlock_window();
1637         if(lock) plugin_gui_lock->lock("MWindow::hide_plugin");
1638         for(int i = 0; i < plugin_guis->total; i++)
1639         {
1640                 if(plugin_guis->values[i]->plugin == plugin)
1641                 {
1642                         PluginServer *ptr = plugin_guis->values[i];
1643                         plugin_guis->remove(ptr);
1644                         if(lock) plugin_gui_lock->unlock();
1645 // Last command executed in client side close
1646                         delete ptr;
1647                         return;
1648                 }
1649         }
1650         if(lock) plugin_gui_lock->unlock();
1654 void MWindow::hide_plugins()
1656         plugin_gui_lock->lock("MWindow::hide_plugins");
1657         plugin_guis->remove_all_objects();
1658         plugin_gui_lock->unlock();
1661 void MWindow::update_plugin_guis()
1663         plugin_gui_lock->lock("MWindow::update_plugin_guis");
1665         for(int i = 0; i < plugin_guis->total; i++)
1666         {
1667                 plugin_guis->values[i]->update_gui();
1668         }
1669         plugin_gui_lock->unlock();
1672 int MWindow::plugin_gui_open(Plugin *plugin)
1674         int result = 0;
1675         plugin_gui_lock->lock("MWindow::plugin_gui_open");
1676         for(int i = 0; i < plugin_guis->total; i++)
1677         {
1678                 if(plugin_guis->values[i]->plugin->identical_location(plugin))
1679                 {
1680                         result = 1;
1681                         break;
1682                 }
1683         }
1684         plugin_gui_lock->unlock();
1685         return result;
1688 void MWindow::render_plugin_gui(void *data, Plugin *plugin)
1690         plugin_gui_lock->lock("MWindow::render_plugin_gui");
1691         for(int i = 0; i < plugin_guis->total; i++)
1692         {
1693                 if(plugin_guis->values[i]->plugin->identical_location(plugin))
1694                 {
1695                         plugin_guis->values[i]->render_gui(data);
1696                         break;
1697                 }
1698         }
1699         plugin_gui_lock->unlock();
1702 void MWindow::render_plugin_gui(void *data, int size, Plugin *plugin)
1704         plugin_gui_lock->lock("MWindow::render_plugin_gui");
1705         for(int i = 0; i < plugin_guis->total; i++)
1706         {
1707                 if(plugin_guis->values[i]->plugin->identical_location(plugin))
1708                 {
1709                         plugin_guis->values[i]->render_gui(data, size);
1710                         break;
1711                 }
1712         }
1713         plugin_gui_lock->unlock();
1717 void MWindow::update_plugin_states()
1719         plugin_gui_lock->lock("MWindow::update_plugin_states");
1720         for(int i = 0; i < plugin_guis->total; i++)
1721         {
1722                 int result = 0;
1723 // Get a plugin GUI
1724                 Plugin *src_plugin = plugin_guis->values[i]->plugin;
1725                 PluginServer *src_plugingui = plugin_guis->values[i];
1727 // Search for plugin in EDL.  Only the master EDL shows plugin GUIs.
1728                 for(Track *track = edl->tracks->first; 
1729                         track && !result; 
1730                         track = track->next)
1731                 {
1732                         for(int j = 0; 
1733                                 j < track->plugin_set.total && !result; 
1734                                 j++)
1735                         {
1736                                 PluginSet *plugin_set = track->plugin_set.values[j];
1737                                 for(Plugin *plugin = (Plugin*)plugin_set->first; 
1738                                         plugin && !result; 
1739                                         plugin = (Plugin*)plugin->next)
1740                                 {
1741                                         if(plugin == src_plugin &&
1742                                                 !strcmp(plugin->title, src_plugingui->title)) result = 1;
1743                                 }
1744                         }
1745                 }
1748 // Doesn't exist anymore
1749                 if(!result)
1750                 {
1751                         hide_plugin(src_plugin, 0);
1752                         i--;
1753                 }
1754         }
1755         plugin_gui_lock->unlock();
1759 void MWindow::update_plugin_titles()
1761         for(int i = 0; i < plugin_guis->total; i++)
1762         {
1763                 plugin_guis->values[i]->update_title();
1764         }
1767 int MWindow::asset_to_edl(EDL *new_edl, 
1768         Asset *new_asset, 
1769         RecordLabels *labels)
1771 //printf("MWindow::asset_to_edl 1\n");
1772 //      new_edl->load_defaults(defaults);
1774 // Keep frame rate, sample rate, and output size unchanged.
1775 // These parameters would revert the project if VWindow displayed an asset
1776 // of different size than the project.
1777         if(new_asset->video_data)
1778         {
1779                 new_edl->session->video_tracks = new_asset->layers;
1780 //              new_edl->session->frame_rate = new_asset->frame_rate;
1781 //              new_edl->session->output_w = new_asset->width;
1782 //              new_edl->session->output_h = new_asset->height;
1783         }
1784         else
1785                 new_edl->session->video_tracks = 0;
1792         if(new_asset->audio_data)
1793         {
1794                 new_edl->session->audio_tracks = new_asset->channels;
1795 //              new_edl->session->sample_rate = new_asset->sample_rate;
1796         }
1797         else
1798                 new_edl->session->audio_tracks = 0;
1799 //printf("MWindow::asset_to_edl 2 %d %d\n", new_edl->session->video_tracks, new_edl->session->audio_tracks);
1801         new_edl->create_default_tracks();
1802 //printf("MWindow::asset_to_edl 2 %d %d\n", new_edl->session->video_tracks, new_edl->session->audio_tracks);
1804 // Disable drawing if the file format isn't fast enough.
1805 // MPEG is now faster than most other codecs.
1806 //      if(new_asset->format == FILE_MPEG)
1807 //      {
1808 //              for(Track *current = new_edl->tracks->first;
1809 //                      current;
1810 //                      current = NEXT)
1811 //              {
1812 //                      if(current->data_type == TRACK_VIDEO) current->draw = 0;
1813 //              }
1814 //      }
1818 //printf("MWindow::asset_to_edl 3\n");
1819         new_edl->insert_asset(new_asset, 0, 0, labels);
1820 //printf("MWindow::asset_to_edl 3\n");
1822 // Align cursor on frames:: clip the new_edl to the minimum of the last joint frame.
1823         if(edl->session->cursor_on_frames)
1824         {
1825                 double edl_length = new_edl->tracks->total_length_framealigned(edl->session->frame_rate);
1826                 new_edl->tracks->clear(edl_length, new_edl->tracks->total_length() + 100, 1);
1827                 new_edl->tracks->loaded_lengths_to_tracklengths(1);
1828         }
1833         char string[BCTEXTLEN];
1834         FileSystem fs;
1835         fs.extract_name(string, new_asset->path);
1836 //printf("MWindow::asset_to_edl 3\n");
1838         strcpy(new_edl->local_session->clip_title, string);
1839 //printf("MWindow::asset_to_edl 4 %s\n", string);
1841 //      new_edl->dump();
1842         return 0;
1845 // Reset everything after a load.
1846 void MWindow::update_project(int load_mode)
1848         restart_brender();
1849         edl->tracks->update_y_pixels(theme);
1852         gui->update(1, 1, 1, 1, 1, 1, 1);
1854         cwindow->update(0, 0, 1, 1, 1);
1857         if(load_mode == LOAD_REPLACE ||
1858                 load_mode == LOAD_REPLACE_CONCATENATE)
1859         {
1860                 vwindow->change_source();
1861         }
1862         else
1863         {
1864                 vwindow->update(1);
1865         }
1867         cwindow->gui->lock_window("Mwindow::update_project");
1868         cwindow->gui->slider->set_position();
1869         cwindow->gui->timebar->update(1, 1);
1870         cwindow->gui->unlock_window();
1871         cwindow->playback_engine->que->send_command(CURRENT_FRAME, 
1872                 CHANGE_ALL,
1873                 edl,
1874                 1);
1876         awindow->gui->async_update_assets();
1877         gui->flush();
1881 void MWindow::rebuild_indices()
1883         char source_filename[BCTEXTLEN], index_filename[BCTEXTLEN];
1885         for(int i = 0; i < session->drag_assets->total; i++)
1886         {
1887 //printf("MWindow::rebuild_indices 1 %s\n", session->drag_assets->values[i]->path);
1888 // Erase file
1889                 IndexFile::get_index_filename(source_filename, 
1890                         preferences->index_directory,
1891                         index_filename, 
1892                         session->drag_assets->values[i]->path);
1893                 remove(index_filename);
1894 // Schedule index build
1895                 session->drag_assets->values[i]->index_status = INDEX_NOTTESTED;
1896                 mainindexes->add_next_asset(0, session->drag_assets->values[i]);
1897         }
1898         mainindexes->start_build();
1902 void MWindow::save_backup()
1904         FileXML file;
1905         edl->set_project_path(session->filename);
1906         edl->save_xml(plugindb, 
1907                 &file, 
1908                 BACKUP_PATH,
1909                 0,
1910                 0);
1911         file.terminate_string();
1912         char path[BCTEXTLEN];
1913         FileSystem fs;
1914         strcpy(path, BACKUP_PATH);
1915         fs.complete_path(path);
1917         if(file.write_to_file(path))
1918         {
1919                 char string2[256];
1920                 sprintf(string2, _("Couldn't open %s for writing."), BACKUP_PATH);
1921                 gui->show_message(string2);
1922         }
1926 int MWindow::create_aspect_ratio(float &w, float &h, int width, int height)
1928         int denominator;
1929         if(!width || !height) return 1;
1930         float fraction = (float)width / height;
1932         for(denominator = 1; 
1933                 denominator < 100 && 
1934                         fabs(fraction * denominator - (int)(fraction * denominator)) > .001; 
1935                 denominator++)
1936                 ;
1938         w = denominator * width / height;
1939         h = denominator;
1940         return 0;
1943 void MWindow::reset_caches()
1945         frame_cache->remove_all();
1946         wave_cache->remove_all();
1947         audio_cache->remove_all();
1948         video_cache->remove_all();
1949         if(cwindow->playback_engine && cwindow->playback_engine->audio_cache)
1950                 cwindow->playback_engine->audio_cache->remove_all();
1951         if(cwindow->playback_engine && cwindow->playback_engine->video_cache)
1952                 cwindow->playback_engine->video_cache->remove_all();
1953         if(vwindow->playback_engine && vwindow->playback_engine->audio_cache)
1954                 vwindow->playback_engine->audio_cache->remove_all();
1955         if(vwindow->playback_engine && vwindow->playback_engine->video_cache)
1956                 vwindow->playback_engine->video_cache->remove_all();
1959 void MWindow::remove_asset_from_caches(Asset *asset)
1961         frame_cache->remove_asset(asset);
1962         wave_cache->remove_asset(asset);
1963         audio_cache->delete_entry(asset);
1964         video_cache->delete_entry(asset);
1965         if(cwindow->playback_engine && cwindow->playback_engine->audio_cache)
1966                 cwindow->playback_engine->audio_cache->delete_entry(asset);
1967         if(cwindow->playback_engine && cwindow->playback_engine->video_cache)
1968                 cwindow->playback_engine->video_cache->delete_entry(asset);
1969         if(vwindow->playback_engine && vwindow->playback_engine->audio_cache)
1970                 vwindow->playback_engine->audio_cache->delete_entry(asset);
1971         if(vwindow->playback_engine && vwindow->playback_engine->video_cache)
1972                 vwindow->playback_engine->video_cache->delete_entry(asset);
1977 void MWindow::remove_assets_from_project(int push_undo)
1979         for(int i = 0; i < session->drag_assets->total; i++)
1980         {
1981                 Asset *asset = session->drag_assets->values[i];
1982                 remove_asset_from_caches(asset);
1983         }
1985 // Remove from VWindow.
1986         for(int i = 0; i < session->drag_clips->total; i++)
1987         {
1988                 if(session->drag_clips->values[i] == vwindow->get_edl())
1989                 {
1990                         vwindow->gui->lock_window("MWindow::remove_assets_from_project 1");
1991                         vwindow->remove_source();
1992                         vwindow->gui->unlock_window();
1993                 }
1994         }
1995         
1996         for(int i = 0; i < session->drag_assets->total; i++)
1997         {
1998                 if(session->drag_assets->values[i] == vwindow->get_asset())
1999                 {
2000                         vwindow->gui->lock_window("MWindow::remove_assets_from_project 2");
2001                         vwindow->remove_source();
2002                         vwindow->gui->unlock_window();
2003                 }
2004         }
2005         
2006         edl->remove_from_project(session->drag_assets);
2007         edl->remove_from_project(session->drag_clips);
2008         save_backup();
2009         if(push_undo) undo->update_undo(_("remove assets"), LOAD_ALL);
2010         restart_brender();
2012         gui->lock_window("MWindow::remove_assets_from_project 3");
2013         gui->update(1,
2014                 1,
2015                 1,
2016                 1,
2017                 0, 
2018                 1,
2019                 0);
2020         gui->unlock_window();
2022         awindow->gui->async_update_assets();
2024 // Removes from playback here
2025         sync_parameters(CHANGE_ALL);
2028 void MWindow::remove_assets_from_disk()
2030 // Remove from disk
2031         for(int i = 0; i < session->drag_assets->total; i++)
2032         {
2033                 remove(session->drag_assets->values[i]->path);
2034         }
2036         remove_assets_from_project(1);
2039 void MWindow::dump_plugins()
2041         for(int i = 0; i < plugindb->total; i++)
2042         {
2043                 printf("audio=%d video=%d realtime=%d transition=%d theme=%d %s\n",
2044                         plugindb->values[i]->audio,
2045                         plugindb->values[i]->video,
2046                         plugindb->values[i]->realtime,
2047                         plugindb->values[i]->transition,
2048                         plugindb->values[i]->theme,
2049                         plugindb->values[i]->title);
2050         }
2077 int MWindow::save_defaults()
2079         gui->save_defaults(defaults);
2080         edl->save_defaults(defaults);
2081         session->save_defaults(defaults);
2082         preferences->save_defaults(defaults);
2084         defaults->save();
2085         return 0;
2088 int MWindow::run_script(FileXML *script)
2090         int result = 0, result2 = 0;
2091         while(!result && !result2)
2092         {
2093                 result = script->read_tag();
2094                 if(!result)
2095                 {
2096                         if(script->tag.title_is("new_project"))
2097                         {
2098 // Run new in immediate mode.
2099 //                              gui->mainmenu->new_project->run_script(script);
2100                         }
2101                         else
2102                         if(script->tag.title_is("record"))
2103                         {
2104 // Run record as a thread.  It is a terminal command.
2105                                 ;
2106 // Will read the complete scipt file without letting record read it if not
2107 // terminated.
2108                                 result2 = 1;
2109                         }
2110                         else
2111                         {
2112                                 printf("MWindow::run_script: Unrecognized command: %s\n",script->tag.get_title() );
2113                         }
2114                 }
2115         }
2116         return result2;
2119 // ================================= synchronization
2122 int MWindow::interrupt_indexes()
2124         mainindexes->interrupt_build();
2125         return 0; 
2130 void MWindow::next_time_format()
2132         switch(edl->session->time_format)
2133         {
2134                 case TIME_HMS: edl->session->time_format = TIME_HMSF; break;
2135                 case TIME_HMSF: edl->session->time_format = TIME_SAMPLES; break;
2136                 case TIME_SAMPLES: edl->session->time_format = TIME_SAMPLES_HEX; break;
2137                 case TIME_SAMPLES_HEX: edl->session->time_format = TIME_FRAMES; break;
2138                 case TIME_FRAMES: edl->session->time_format = TIME_FEET_FRAMES; break;
2139                 case TIME_FEET_FRAMES: edl->session->time_format = TIME_SECONDS; break;
2140                 case TIME_SECONDS: edl->session->time_format = TIME_HMS; break;
2141         }
2143         time_format_common();
2146 void MWindow::prev_time_format()
2148         switch(edl->session->time_format)
2149         {
2150                 case TIME_HMS: edl->session->time_format = TIME_SECONDS; break;
2151                 case TIME_SECONDS: edl->session->time_format = TIME_FEET_FRAMES; break;
2152                 case TIME_FEET_FRAMES: edl->session->time_format = TIME_FRAMES; break;
2153                 case TIME_FRAMES: edl->session->time_format = TIME_SAMPLES_HEX; break;
2154                 case TIME_SAMPLES_HEX: edl->session->time_format = TIME_SAMPLES; break;
2155                 case TIME_SAMPLES: edl->session->time_format = TIME_HMSF; break;
2156                 case TIME_HMSF: edl->session->time_format = TIME_HMS; break;
2157         }
2159         time_format_common();
2162 void MWindow::time_format_common()
2164         gui->lock_window("MWindow::next_time_format");
2165         gui->redraw_time_dependancies();
2166         char string[BCTEXTLEN], string2[BCTEXTLEN];
2167         sprintf(string, _("Using %s."), Units::print_time_format(edl->session->time_format, string2));
2168         gui->show_message(string);
2169         gui->flush();
2170         gui->unlock_window();
2174 int MWindow::set_filename(char *filename)
2176         strcpy(session->filename, filename);
2177         if(gui)
2178         {
2179                 if(filename[0] == 0)
2180                 {
2181                         gui->set_title(PROGRAM_NAME);
2182                 }
2183                 else
2184                 {
2185                         FileSystem dir;
2186                         char string[BCTEXTLEN], string2[BCTEXTLEN];
2187                         dir.extract_name(string, filename);
2188                         sprintf(string2, PROGRAM_NAME ": %s", string);
2189                         gui->set_title(string2);
2190                 }
2191         }
2192         return 0; 
2202 int MWindow::set_loop_boundaries()
2204         double start = edl->local_session->get_selectionstart();
2205         double end = edl->local_session->get_selectionend();
2206         
2207         if(start != 
2208                 end) 
2209         {
2210                 ;
2211         }
2212         else
2213         if(edl->tracks->total_length())
2214         {
2215                 start = 0;
2216                 end = edl->tracks->total_length();
2217         }
2218         else
2219         {
2220                 start = end = 0;
2221         }
2223         if(edl->local_session->loop_playback && start != end)
2224         {
2225                 edl->local_session->loop_start = start;
2226                 edl->local_session->loop_end = end;
2227         }
2228         return 0; 
2237 int MWindow::reset_meters()
2239         cwindow->gui->lock_window("MWindow::reset_meters 1");
2240         cwindow->gui->meters->reset_meters();
2241         cwindow->gui->unlock_window();
2243         vwindow->gui->lock_window("MWindow::reset_meters 2");
2244         vwindow->gui->meters->reset_meters();
2245         vwindow->gui->unlock_window();
2247         lwindow->gui->lock_window("MWindow::reset_meters 3");
2248         lwindow->gui->panel->reset_meters();
2249         lwindow->gui->unlock_window();
2251         gui->lock_window("MWindow::reset_meters 4");
2252         gui->patchbay->reset_meters();
2253         gui->unlock_window();
2254         return 0;