+
+ if (current_midi_track == new_pad_target) {
+ /* nothing to do */
+ return;
+ }
+
+ if (!new_pad_target) {
+ /* leave existing connection alone */
+ return;
+ }
+
+ /* disconnect from pad port, if appropriate */
+
+ if (current_midi_track && pad_port) {
+
+ /* XXX this could possibly leave dangling MIDI notes.
+ *
+ * A general libardour fix is required. It isn't obvious
+ * how note resolution can be done unless disconnecting
+ * becomes "slow" (i.e. deferred for as long as it takes
+ * to resolve notes).
+ */
+ current_midi_track->input()->disconnect (current_midi_track->input()->nth(0), pad_port->name(), this);
+ }
+
+ /* now connect the pad port to this (newly) selected midi
+ * track, if indeed there is one.
+ */
+
+ if (new_pad_target && pad_port) {
+ new_pad_target->input()->connect (new_pad_target->input()->nth (0), pad_port->name(), this);
+ current_pad_target = new_pad_target;
+ selection_color = get_color_index (new_pad_target->presentation_info().color());
+ contrast_color = get_color_index (ArdourCanvas::HSV (new_pad_target->presentation_info().color()).opposite().color());
+ } else {
+ current_pad_target.reset ();
+ selection_color = LED::Green;
+ contrast_color = LED::Green;
+ }
+
+ reset_pad_colors ();
+}
+
+Push2::Button*
+Push2::button_by_id (ButtonID bid)
+{
+ return id_button_map[bid];
+}
+
+uint8_t
+Push2::get_color_index (ArdourCanvas::Color rgba)
+{
+ ColorMap::iterator i = color_map.find (rgba);
+
+ if (i != color_map.end()) {
+ return i->second;
+ }
+
+ double dr, dg, db, da;
+ int r, g, b;
+ ArdourCanvas::color_to_rgba (rgba, dr, dg, db, da);
+ int w = 126; /* not sure where/when we should get this value */
+
+
+ r = (int) floor (255.0 * dr);
+ g = (int) floor (255.0 * dg);
+ b = (int) floor (255.0 * db);
+
+ /* get a free index */
+
+ uint8_t index;
+
+ if (color_map_free_list.empty()) {
+ /* random replacement of any entry above zero and below 122 (where the
+ * Ableton standard colors live)
+ */
+ index = 1 + (random() % 121);
+ } else {
+ index = color_map_free_list.top();
+ color_map_free_list.pop();
+ }
+
+ MidiByteArray palette_msg (17,
+ 0xf0,
+ 0x00 , 0x21, 0x1d, 0x01, 0x01, 0x03, /* reset palette header */
+ 0x00, /* index = 7 */
+ 0x00, 0x00, /* r = 8 & 9 */
+ 0x00, 0x00, /* g = 10 & 11 */
+ 0x00, 0x00, /* b = 12 & 13 */
+ 0x00, 0x00, /* w (a?) = 14 & 15*/
+ 0xf7);
+ palette_msg[7] = index;
+ palette_msg[8] = r & 0x7f;
+ palette_msg[9] = (r & 0x80) >> 7;
+ palette_msg[10] = g & 0x7f;
+ palette_msg[11] = (g & 0x80) >> 7;
+ palette_msg[12] = b & 0x7f;
+ palette_msg[13] = (b & 0x80) >> 7;
+ palette_msg[14] = w & 0x7f;
+ palette_msg[15] = w & 0x80;
+
+ write (palette_msg);
+
+ MidiByteArray update_pallette_msg (8, 0xf0, 0x00, 0x21, 0x1d, 0x01, 0x01, 0x05, 0xF7);
+ write (update_pallette_msg);
+
+ color_map[rgba] = index;
+
+ return index;