fix editpanel wdw lock issues, use undo bracketing to avoid stop playback deadlocks
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / mwindow.C
index 601f5c24e0fdfd7a01c26078bd35d01fbf25cd81..f62df89b82f2ea99718954b03de6636e5413b97f 100644 (file)
 #include "savefile.inc"
 #include "samplescroll.h"
 #include "sha1.h"
+#include "shuttle.h"
 #include "sighandler.h"
 #include "splashgui.h"
 #include "statusbar.h"
 #include "vframe.h"
 #include "vtrack.h"
 #include "versioninfo.h"
+#include "vicon.h"
 #include "videodevice.inc"
 #include "videowindow.h"
 #include "vplayback.h"
@@ -197,6 +199,7 @@ MWindow::MWindow()
        playback_3d = 0;
        splash_window = 0;
        undo = 0;
+       undo_command = COMMAND_NONE;
        defaults = 0;
        assets = 0;
        //commercials = 0;
@@ -236,6 +239,7 @@ MWindow::MWindow()
        in_destructor = 0;
        speed_edl = 0;
        proxy_beep = 0;
+       shuttle = 0;
 }
 
 
@@ -253,6 +257,7 @@ MWindow::~MWindow()
        delete proxy_beep;
        delete create_bd;       create_bd = 0;
        delete create_dvd;      create_dvd = 0;
+       delete shuttle;         shuttle = 0;
        delete batch_render;    batch_render = 0;
        delete render;          render = 0;
        commit_commercial();
@@ -1362,7 +1367,7 @@ void MWindow::create_mixers()
 {
        if( !session->drag_assets->size() &&
            !session->drag_clips->size() ) return;
-       undo->update_undo_before();
+       undo_before();
 
        select_zwindow(0);
        ArrayList<ZWindow *>new_mixers;
@@ -1386,9 +1391,9 @@ void MWindow::create_mixers()
 
        refresh_mixers();
        save_backup();
-       undo->update_undo_after(_("create mixers"), LOAD_ALL);
+       undo_after(_("create mixers"), LOAD_ALL);
        restart_brender();
-       gui->update(1, 2, 1, 1, 1, 1, 0);
+       gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
        sync_parameters(CHANGE_ALL);
 }
 
@@ -1581,6 +1586,17 @@ void MWindow::init_exportedl()
        exportedl = new ExportEDL(this);
 }
 
+void MWindow::init_shuttle()
+{
+#ifdef HAVE_SHUTTLE
+       const char *dev_name = Shuttle::probe();
+       if( dev_name ) {
+               shuttle = new Shuttle(this);
+               shuttle->start(dev_name);
+       }
+#endif
+}
+
 void MWindow::init_brender()
 {
        if(preferences->use_brender && !brender)
@@ -1799,6 +1815,28 @@ void MWindow::stop_transport()
        gui->stop_transport(gui->get_window_lock() ? "MWindow::stop_transport" : 0);
 }
 
+void MWindow::undo_before(const char *description, void *creator)
+{
+       if( cwindow->playback_engine->is_playing_back ) {
+               undo_command = cwindow->playback_engine->command->command;
+               cwindow->playback_engine->que->send_command(STOP, CHANGE_NONE, 0, 0);
+               gui->unlock_window();
+               cwindow->playback_engine->interrupt_playback(1);
+               gui->lock_window(description);
+       }
+       undo->update_undo_before(description, creator);
+}
+
+void MWindow::undo_after(const char *description, uint32_t load_flags, int changes_made)
+{
+       if( undo_command != COMMAND_NONE ) {
+               cwindow->playback_engine->que->send_command(undo_command, CHANGE_NONE, edl, 1, 0);
+               undo_command = COMMAND_NONE;
+       }
+
+       undo->update_undo_after(description, load_flags, changes_made);
+}
+
 void MWindow::beep(double freq, double secs, double gain)
 {
        if( !proxy_beep ) proxy_beep = new ProxyBeep(this);
@@ -1825,7 +1863,7 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
        gui->lock_window("MWindow::load_filenames 0");
 
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
-       undo->update_undo_before();
+       undo_before();
 
 
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
@@ -2007,6 +2045,7 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                                break;
                        }
                        if( strcmp(cin_version, CINELERRA_VERSION) &&
+                           strcmp(cin_version, "Unify") &&
                            strcmp(cin_version, "5.1") ) {
                                char string[BCTEXTLEN];
                                snprintf(string, sizeof(string),
@@ -2019,6 +2058,8 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                                EDL *nested_edl = new EDL;
                                nested_edl->create_objects();
                                nested_edl->load_xml(&xml_file, LOAD_ALL);
+                               int groups = nested_edl->regroup(session->group_number);
+                               session->group_number += groups;
 //printf("MWindow::load_filenames %p %s\n", nested_edl, nested_edl->project_path);
                                new_edl->create_nested(nested_edl);
                                new_edl->set_path(filenames->get(i));
@@ -2027,6 +2068,8 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                        else {
 // Load EDL for pasting
                                new_edl->load_xml(&xml_file, LOAD_ALL);
+                               int groups = new_edl->regroup(session->group_number);
+                               session->group_number += groups;
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                                test_plugins(new_edl, filenames->get(i));
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
@@ -2219,7 +2262,7 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 // need to update undo before project, since mwindow is unlocked & a new load
 // can begin here.  Should really prevent loading until we're done.
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
-       undo->update_undo_after(_("load"), LOAD_ALL);
+       undo_after(_("load"), LOAD_ALL);
 
        for(int i = 0; i < new_edls.size(); i++)
        {
@@ -2333,7 +2376,7 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
 
        edl->Garbage::add_user();
        save_backup();
-       undo->update_undo_before(_("proxy"), this);
+       undo_before(_("proxy"), this);
        ProxyRender proxy_render(this, asset);
 
 // revert project to original size from current size
@@ -2446,7 +2489,7 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
                edl->set_proxy(new_scale, new_use_scaler,
                        &proxy_render.orig_idxbls, &proxy_render.orig_proxies);
 
-       undo->update_undo_after(_("proxy"), LOAD_ALL);
+       undo_after(_("proxy"), LOAD_ALL);
        edl->Garbage::remove_user();
        restart_brender();
 
@@ -2614,6 +2657,7 @@ void MWindow::create_objects(int want_gui,
        init_render();
        if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
 
+       init_shuttle();
        init_brender();
        init_exportedl();
        init_commercials();
@@ -2861,32 +2905,67 @@ void MWindow::restore_windows()
        if( !session->show_vwindow ) {
                for( int i=0, n=vwindows.size(); i<n; ++i ) {
                        VWindow *vwindow = vwindows[i];
-                       if( !vwindow ) continue;
-                       vwindow->gui->close(0);
+                       if( !vwindow || !vwindow->is_running() ) continue;
+                       vwindow->gui->lock_window("MWindow::restore_windows");
+                       vwindow->gui->close(1);
+                       vwindow->gui->unlock_window();
                }
        }
        else
                show_vwindow();
 
-       if( !session->show_awindow )
-               awindow->gui->close(0);
-       else
+       if( !session->show_awindow && !awindow->gui->is_hidden() ) {
+               awindow->gui->lock_window("MWindow::restore_windows");
+               awindow->gui->close_event();
+               awindow->gui->unlock_window();
+       }
+       else if( session->show_awindow && awindow->gui->is_hidden() )
                show_awindow();
 
-       if( !session->show_cwindow )
-               cwindow->gui->close(0);
-       else
-               show_cwindow();
+       if( !session->show_cwindow && !cwindow->gui->is_hidden() ) {
+               cwindow->gui->lock_window("MWindow::restore_windows");
+               cwindow->hide_window();
+               cwindow->gui->unlock_window();
+       }
+       else if( session->show_cwindow && cwindow->gui->is_hidden() )
+               cwindow->show_window();
 
-       if( !session->show_gwindow )
-               gwindow->gui->close(0);
-       else
+       if( !session->show_gwindow && !gwindow->gui->is_hidden() ) {
+               gwindow->gui->lock_window("MWindow::restore_windows");
+               gwindow->gui->close_event();
+               gwindow->gui->unlock_window();
+       }
+       else if( session->show_gwindow && gwindow->gui->is_hidden() )
                show_gwindow();
 
-       if( !session->show_lwindow )
-               lwindow->gui->close(0);
-       else
+       if( !session->show_lwindow && !lwindow->gui->is_hidden() ) {
+               lwindow->gui->lock_window("MWindow::restore_windows");
+               lwindow->gui->close_event();
+               lwindow->gui->unlock_window();
+       }
+       else if( session->show_lwindow && lwindow->gui->is_hidden() )
                show_lwindow();
+
+       gui->focus();
+}
+
+void MWindow::save_layout(int no)
+{
+       char layout_path[BCTEXTLEN];
+       snprintf(layout_path, sizeof(layout_path), "%s/" LAYOUT_FILE,
+               File::get_config_path(), no);
+       session->save_file(layout_path);
+}
+
+void MWindow::load_layout(int no)
+{
+       char layout_path[BCTEXTLEN];
+       snprintf(layout_path, sizeof(layout_path), "%s/" LAYOUT_FILE,
+               File::get_config_path(), no);
+       session->load_file(layout_path);
+       restore_windows();
+       gui->default_positions();
+       save_defaults();
 }
 
 int MWindow::tile_windows(int window_config)
@@ -2919,18 +2998,15 @@ void MWindow::set_screens(int value)
        screens = value;
 }
 
-void MWindow::set_auto_keyframes(int value, int lock_mwindow, int lock_cwindow)
+void MWindow::set_auto_keyframes(int value)
 {
-       if(lock_mwindow) gui->lock_window("MWindow::set_auto_keyframes");
        edl->session->auto_keyframes = value;
        gui->mbuttons->edit_panel->keyframe->update(value);
        gui->flush();
-       if(lock_mwindow) gui->unlock_window();
-
-       if(lock_cwindow) cwindow->gui->lock_window("MWindow::set_auto_keyframes");
+       cwindow->gui->lock_window("MWindow::set_auto_keyframes");
        cwindow->gui->edit_panel->keyframe->update(value);
        cwindow->gui->flush();
-       if(lock_cwindow) cwindow->gui->unlock_window();
+       cwindow->gui->unlock_window();
 }
 
 void MWindow::set_auto_visibility(Autos *autos, int value)
@@ -2942,7 +3018,7 @@ void MWindow::set_auto_visibility(Autos *autos, int value)
        else
                return;
 
-       gui->update(0, 1, 0, 0, 0, 0, 0);
+       gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
        gui->mainmenu->update_toggles(1);
        gui->unlock_window();
        gwindow->gui->update_toggles(1);
@@ -2957,20 +3033,17 @@ void MWindow::set_keyframe_type(int mode)
        gui->unlock_window();
 }
 
-int MWindow::set_editing_mode(int new_editing_mode, int lock_mwindow, int lock_cwindow)
+int MWindow::set_editing_mode(int new_editing_mode)
 {
-       if(lock_mwindow) gui->lock_window("MWindow::set_editing_mode");
        edl->session->editing_mode = new_editing_mode;
        gui->mbuttons->edit_panel->editing_mode = edl->session->editing_mode;
        gui->mbuttons->edit_panel->update();
        gui->set_editing_mode(1);
-       if(lock_mwindow) gui->unlock_window();
-
 
-       if(lock_cwindow) cwindow->gui->lock_window("MWindow::set_editing_mode");
+       cwindow->gui->lock_window("MWindow::set_editing_mode");
        cwindow->gui->edit_panel->update();
        cwindow->gui->edit_panel->editing_mode = edl->session->editing_mode;
-       if(lock_cwindow) cwindow->gui->unlock_window();
+       cwindow->gui->unlock_window();
        return 0;
 }
 
@@ -2978,9 +3051,9 @@ void MWindow::toggle_editing_mode()
 {
        int mode = edl->session->editing_mode;
        if( mode == EDITING_ARROW )
-               set_editing_mode(EDITING_IBEAM, 0, 1);
+               set_editing_mode(EDITING_IBEAM);
        else
-               set_editing_mode(EDITING_ARROW, 0, 1);
+               set_editing_mode(EDITING_ARROW);
 }
 
 void MWindow::toggle_camera_xyz()
@@ -2999,12 +3072,10 @@ void MWindow::toggle_projector_xyz()
 
 void MWindow::set_labels_follow_edits(int value)
 {
-       gui->lock_window("MWindow::set_labels_follow_edits");
        edl->session->labels_follow_edits = value;
        gui->mbuttons->edit_panel->locklabels->update(value);
        gui->mainmenu->labels_follow_edits->set_checked(value);
        gui->flush();
-       gui->unlock_window();
 }
 
 void MWindow::sync_parameters(int change_type)
@@ -3183,7 +3254,7 @@ void MWindow::hide_plugin(Plugin *plugin, int lock)
        plugin->show = 0;
 // Update the toggle
        gui->lock_window("MWindow::hide_plugin");
-       gui->update(0, 1, 0, 0, 0, 0, 0);
+       gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
        gui->unlock_window();
 
        if(lock) plugin_gui_lock->lock("MWindow::hide_plugin");
@@ -3255,6 +3326,69 @@ void MWindow::hide_keyframe_gui(Plugin *plugin)
        keyframe_gui_lock->unlock();
 }
 
+int MWindow::get_hash_color(Edit *edit)
+{
+       Indexable *idxbl = edit->asset ?
+               (Indexable*)edit->asset : (Indexable*)edit->nested_edl;
+       if( !idxbl ) return 0;
+       char path[BCTEXTLEN];
+       if( !edit->asset || edit->track->data_type != TRACK_VIDEO ||
+           edl->session->proxy_scale == 1 ||
+           ProxyRender::from_proxy_path(path, idxbl, edl->session->proxy_scale) )
+               strcpy(path, idxbl->path);
+       char *cp = strrchr(path, '/');
+       cp = !cp ? path : cp+1;
+       uint8_t *bp = (uint8_t*)cp;
+       int v = 0;
+       while( *bp ) v += *bp++;
+       return get_hash_color(v);
+}
+
+int MWindow::get_hash_color(int v)
+{
+       int hash = 0x303030;
+       if( v & 0x01 ) hash ^= 0x000040;
+       if( v & 0x02 ) hash ^= 0x004000;
+       if( v & 0x04 ) hash ^= 0x400000;
+       if( v & 0x08 ) hash ^= 0x080000;
+       if( v & 0x10 ) hash ^= 0x000800;
+       if( v & 0x20 ) hash ^= 0x000008;
+       if( v & 0x40 ) hash ^= 0x404040;
+       if( v & 0x80 ) hash ^= 0x080808;
+       return hash;
+}
+
+int MWindow::get_group_color(int v)
+{
+       int color = 0x606060;
+       if( v & 0x01 ) color ^= 0x000080;
+       if( v & 0x02 ) color ^= 0x008000;
+       if( v & 0x04 ) color ^= 0x800000;
+       if( v & 0x08 ) color ^= 0x100000;
+       if( v & 0x10 ) color ^= 0x001000;
+       if( v & 0x20 ) color ^= 0x000010;
+       if( v & 0x40 ) color ^= 0x080808;
+       if( v & 0x80 ) color ^= 0x909090;
+       return color;
+}
+
+int MWindow::get_title_color(Edit *edit)
+{
+        unsigned color = edit->color & 0xffffff;
+       unsigned alpha = (~edit->color>>24) & 0xff;
+       if( !color ) {
+               if( edit->group_id )
+                       color = get_group_color(edit->group_id);
+               else if( preferences->autocolor_assets )
+                       color = get_hash_color(edit);
+               else
+                       return 0;
+       }
+       if( alpha == 0xff )
+               alpha = session->title_bar_alpha*255;
+       return color | (~alpha<<24);
+}
+
 void MWindow::update_keyframe_guis()
 {
 // Send new configuration to keyframe GUI's
@@ -3514,7 +3648,7 @@ void MWindow::update_project(int load_mode)
                gui->load_panes();
        }
 
-       gui->update(1, 1, 1, 1, 1, 1, 1);
+       gui->update(1, NORMAL_DRAW, 1, 1, 1, 1, 1);
        if(debug) PRINT_TRACE
        gui->unlock_window();
        init_brender();
@@ -3677,8 +3811,8 @@ void MWindow::load_backup()
 
 void MWindow::save_undo_data()
 {
-       undo->update_undo_before();
-       undo->update_undo_after(_("perpetual session"), LOAD_ALL);
+       undo_before();
+       undo_after(_("perpetual session"), LOAD_ALL);
        char perpetual_path[BCTEXTLEN];
        snprintf(perpetual_path, sizeof(perpetual_path), "%s/%s",
                File::get_config_path(), PERPETUAL_FILE);
@@ -3974,6 +4108,8 @@ void MWindow::remove_asset_from_caches(Asset *asset)
 void MWindow::remove_assets_from_project(int push_undo, int redraw,
                ArrayList<Indexable*> *drag_assets, ArrayList<EDL*> *drag_clips)
 {
+       awindow->gui->close_view_popup();
+
        for(int i = 0; i < drag_assets->total; i++) {
                Indexable *indexable = drag_assets->get(i);
                if(indexable->is_asset) remove_asset_from_caches((Asset*)indexable);
@@ -4010,16 +4146,16 @@ void MWindow::remove_assets_from_project(int push_undo, int redraw,
        }
 
 //printf("MWindow::rebuild_indices 1 %s\n", indexable->path);
-       if(push_undo) undo->update_undo_before();
+       if(push_undo) undo_before();
        if(drag_assets) edl->remove_from_project(drag_assets);
        if(drag_clips) edl->remove_from_project(drag_clips);
        if(redraw) save_backup();
-       if(push_undo) undo->update_undo_after(_("remove assets"), LOAD_ALL);
+       if(push_undo) undo_after(_("remove assets"), LOAD_ALL);
        if(redraw) {
                restart_brender();
 
                gui->lock_window("MWindow::remove_assets_from_project 3");
-               gui->update(1, 1, 1, 1, 0, 1, 0);
+               gui->update(1, NORMAL_DRAW, 1, 1, 0, 1, 0);
                gui->unlock_window();
 
        // Removes from playback here
@@ -4031,16 +4167,16 @@ void MWindow::remove_assets_from_project(int push_undo, int redraw,
 
 void MWindow::remove_assets_from_disk()
 {
+       remove_assets_from_project(1,
+               1,
+               session->drag_assets,
+               session->drag_clips);
+
 // Remove from disk
        for(int i = 0; i < session->drag_assets->total; i++)
        {
                remove(session->drag_assets->get(i)->path);
        }
-
-       remove_assets_from_project(1,
-               1,
-               session->drag_assets,
-               session->drag_clips);
 }
 
 void MWindow::dump_plugins(FILE *fp)
@@ -4345,7 +4481,7 @@ void MWindow::resync_guis()
 // Update GUIs
        restart_brender();
        gui->lock_window("MWindow::resync_guis");
-       gui->update(1, 1, 1, 1, 1, 1, 0);
+       gui->update(1, NORMAL_DRAW, 1, 1, 1, 1, 0);
        gui->unlock_window();
 
        cwindow->gui->lock_window("MWindow::resync_guis");
@@ -4397,7 +4533,7 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
        session->auto_keyframes = 0;
        int result = file->open_file(preferences, asset, 1, 0);
        if( !result && delete_tracks > 0 )
-               undo->update_undo_before();
+               undo_before();
        int video_layers = asset->get_video_layers();
        if( !result && asset->video_data && vstream < video_layers ) {
                // try to get asset up to date, may fail
@@ -4493,7 +4629,7 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
        session->auto_keyframes = old_auto_keyframes;
        if( !result && delete_tracks > 0 ) {
                save_backup();
-               undo->update_undo_after(_("select asset"), LOAD_ALL);
+               undo_after(_("select asset"), LOAD_ALL);
        }
        resync_guis();
        return result;