prefer recently used states.
authorRobin Gareus <robin@gareus.org>
Mon, 23 Nov 2015 21:44:13 +0000 (22:44 +0100)
committerRobin Gareus <robin@gareus.org>
Mon, 23 Nov 2015 21:45:16 +0000 (22:45 +0100)
gtk2_ardour/engine_dialog.cc
gtk2_ardour/engine_dialog.h

index d8f35b211aa25ea8895daeed12bd42d074bfd629..381a15b5ac05bee71e08b2eabfea22e8343ec118 100644 (file)
@@ -1630,8 +1630,6 @@ EngineControl::State
 EngineControl::get_matching_state (const string& backend)
 {
        for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
-               // TODO use LRU for every backend and prefer the active one
-               // uniqueness is only guaranteed for backend + driver + device(s)
                if ((*i)->backend == backend) {
                        return (*i);
                }
@@ -1708,6 +1706,19 @@ bool EngineControl::equivalent_states (const EngineControl::State& state1,
        return false;
 }
 
+bool
+EngineControl::state_sort_cmp (const State &a, const State &b) {
+       if (a->active) {
+               return true;
+       }
+       else if (b->active) {
+               return false;
+       }
+       else {
+               return a->lru < b->lru;
+       }
+}
+
 EngineControl::State
 EngineControl::save_state ()
 {
@@ -1716,6 +1727,7 @@ EngineControl::save_state ()
        if (!_have_control) {
                state = get_matching_state (backend_combo.get_active_text(), string(), string());
                if (state) {
+                       state->lru = time (NULL) ;
                        return state;
                }
                state.reset(new StateStruct);
@@ -1735,6 +1747,8 @@ EngineControl::save_state ()
 
        states.push_back (state);
 
+       states.sort (state_sort_cmp);
+
        return state;
 }
 
@@ -1754,6 +1768,7 @@ EngineControl::store_state (State state)
        state->output_channels = get_output_channels ();
        state->midi_option = get_midi_option ();
        state->midi_devices = _midi_devices;
+       state->lru = time (NULL) ;
 }
 
 void
@@ -1817,6 +1832,7 @@ EngineControl::get_state ()
                        node->add_property ("output-channels", (*i)->output_channels);
                        node->add_property ("active", (*i)->active ? "yes" : "no");
                        node->add_property ("midi-option", (*i)->midi_option);
+                       node->add_property ("lru", (*i)->active ? time (NULL) : (*i)->lru);
 
                        XMLNode* midi_devices = new XMLNode ("MIDIDevices");
                        for (std::vector<MidiDeviceSettings>::const_iterator p = (*i)->midi_devices.begin(); p != (*i)->midi_devices.end(); ++p) {
@@ -1980,6 +1996,10 @@ EngineControl::set_state (const XMLNode& root)
                                }
                        }
 
+                       if ((prop = grandchild->property ("lru"))) {
+                               state->lru = atoi (prop->value ());
+                       }
+
 #if 1
                        /* remove accumulated duplicates (due to bug in ealier version)
                         * this can be removed again before release
@@ -2016,6 +2036,8 @@ EngineControl::set_state (const XMLNode& root)
                }
        }
 
+       states.sort (state_sort_cmp);
+
        for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
 
                if ((*i)->active) {
@@ -2414,6 +2436,8 @@ EngineControl::post_push ()
                store_state(state);
        }
 
+       states.sort (state_sort_cmp);
+
        /* all off */
 
        for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
index ae2128b19be5e15dfa14a910e51b2ef5090a8940..bfb094deea36635b8139d0194fe765763de5d4e7 100644 (file)
@@ -217,6 +217,7 @@ class EngineControl : public ArdourDialog, public PBD::ScopedConnectionList {
        bool active;
        std::string midi_option;
        std::vector<MidiDeviceSettings> midi_devices;
+       time_t lru;
 
        StateStruct()
                : sample_rate (48000)
@@ -225,12 +226,14 @@ class EngineControl : public ArdourDialog, public PBD::ScopedConnectionList {
                , output_latency (0)
                , input_channels (0)
                , output_channels (0)
-               , active (false) {}
+               , active (false)
+               , lru (0) {}
 
     };
 
     typedef boost::shared_ptr<StateStruct> State;
     typedef std::list<State> StateList;
+    static bool state_sort_cmp (const State &a, const State &b);
 
     StateList states;