expand display contents to include TC & BBT clocks, plus button function and track...
[ardour.git] / libs / surfaces / push2 / push2.cc
1 /*
2         Copyright (C) 2016 Paul Davis
3
4         This program is free software; you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation; either version 2 of the License, or
7         (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with this program; if not, write to the Free Software
16         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cairomm/context.h>
20 #include <cairomm/surface.h>
21 #include <pangomm/layout.h>
22
23 #include "pbd/compose.h"
24 #include "pbd/debug.h"
25 #include "pbd/failed_constructor.h"
26
27 #include "midi++/parser.h"
28 #include "timecode/time.h"
29 #include "timecode/bbt_time.h"
30
31 #include "ardour/async_midi_port.h"
32 #include "ardour/audioengine.h"
33 #include "ardour/debug.h"
34 #include "ardour/midiport_manager.h"
35 #include "ardour/session.h"
36 #include "ardour/tempo.h"
37
38 #include "push2.h"
39
40 using namespace ARDOUR;
41 using namespace std;
42 using namespace PBD;
43 using namespace Glib;
44 using namespace ArdourSurface;
45
46 #include "i18n.h"
47
48 #include "pbd/abstract_ui.cc" // instantiate template
49
50 const int Push2::cols = 960;
51 const int Push2::rows = 160;
52 const int Push2::pixels_per_row = 1024;
53
54 #define ABLETON 0x2982
55 #define PUSH2   0x1967
56
57 Push2::Push2 (ARDOUR::Session& s)
58         : ControlProtocol (s, string (X_("Ableton Push 2")))
59         , AbstractUI<Push2Request> (name())
60         , handle (0)
61         , device_buffer (0)
62         , frame_buffer (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, cols, rows))
63 {
64         context = Cairo::Context::create (frame_buffer);
65         tc_clock_layout = Pango::Layout::create (context);
66         bbt_clock_layout = Pango::Layout::create (context);
67
68         Pango::FontDescription fd ("Sans Bold 24");
69         tc_clock_layout->set_font_description (fd);
70         bbt_clock_layout->set_font_description (fd);
71
72         Pango::FontDescription fd2 ("Sans Bold 10");
73         for (int n = 0; n < 8; ++n) {
74                 upper_layout[n] = Pango::Layout::create (context);
75                 upper_layout[n]->set_font_description (fd2);
76                 upper_layout[n]->set_text ("solo");
77                 lower_layout[n] = Pango::Layout::create (context);
78                 lower_layout[n]->set_font_description (fd2);
79                 lower_layout[n]->set_text ("mute");
80                 mid_layout[n] = Pango::Layout::create (context);
81                 mid_layout[n]->set_font_description (fd2);
82                 mid_layout[n]->set_text (string_compose ("Inst %1", n));
83         }
84
85         if (open ()) {
86                 throw failed_constructor ();
87         }
88
89         build_maps ();
90 }
91
92 Push2::~Push2 ()
93 {
94         close ();
95 }
96
97 int
98 Push2::open ()
99 {
100         int err;
101
102         if (handle) {
103                 /* already open */
104                 return 0;
105         }
106
107         if ((handle = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
108                 return -1;
109         }
110
111         if ((err = libusb_claim_interface (handle, 0x00))) {
112                 return -1;
113         }
114
115         device_frame_buffer = new uint16_t[rows*pixels_per_row];
116
117         memset (device_frame_buffer, 0, sizeof (uint16_t) * rows * pixels_per_row);
118
119         frame_header[0] = 0xef;
120         frame_header[1] = 0xcd;
121         frame_header[2] = 0xab;
122         frame_header[3] = 0x89;
123         memset (&frame_header[4], 0, 12);
124
125         /* setup ports */
126
127         _async_in  = AudioEngine::instance()->register_input_port (DataType::MIDI, X_("push2 in"), true);
128         _async_out = AudioEngine::instance()->register_output_port (DataType::MIDI, X_("push2 out"), true);
129
130         if (_async_in == 0 || _async_out == 0) {
131                 return -1;
132         }
133
134         _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in).get();
135         _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out).get();
136
137         connect_to_parser ();
138
139         return 0;
140 }
141
142 int
143 Push2::close ()
144 {
145         AudioEngine::instance()->unregister_port (_async_in);
146         AudioEngine::instance()->unregister_port (_async_out);
147
148         _async_in.reset ((ARDOUR::Port*) 0);
149         _async_out.reset ((ARDOUR::Port*) 0);
150         _input_port = 0;
151         _output_port = 0;
152
153         vblank_connection.disconnect ();
154         periodic_connection.disconnect ();
155         session_connections.drop_connections ();
156
157         if (handle) {
158                 libusb_release_interface (handle, 0x00);
159                 libusb_close (handle);
160                 handle = 0;
161         }
162
163         delete [] device_frame_buffer;
164         device_frame_buffer = 0;
165
166         return 0;
167 }
168
169 bool
170 Push2::probe ()
171 {
172         libusb_device_handle *h;
173         libusb_init (NULL);
174
175         if ((h = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
176                 DEBUG_TRACE (DEBUG::Push2, "no Push2 device found\n");
177                 return false;
178         }
179
180         libusb_close (h);
181         DEBUG_TRACE (DEBUG::Push2, "Push2 device located\n");
182         return true;
183 }
184
185 void*
186 Push2::request_factory (uint32_t num_requests)
187 {
188         /* AbstractUI<T>::request_buffer_factory() is a template method only
189            instantiated in this source module. To provide something visible for
190            use in the interface/descriptor, we have this static method that is
191            template-free.
192         */
193         return request_buffer_factory (num_requests);
194 }
195
196 void
197 Push2::do_request (Push2Request * req)
198 {
199         DEBUG_TRACE (DEBUG::Push2, string_compose ("doing request type %1\n", req->type));
200         if (req->type == CallSlot) {
201
202                 call_slot (MISSING_INVALIDATOR, req->the_slot);
203
204         } else if (req->type == Quit) {
205
206                 stop ();
207         }
208 }
209
210 int
211 Push2::stop ()
212 {
213         BaseUI::quit ();
214         close ();
215         return 0;
216 }
217
218 /** render host-side frame buffer (a Cairo ImageSurface) to the current
219  * device-side frame buffer. The device frame buffer will be pushed to the
220  * device on the next call to vblank()
221  */
222
223 int
224 Push2::bitblt_to_device_frame_buffer ()
225 {
226         /* ensure that all drawing has been done before we fetch pixel data */
227
228         frame_buffer->flush ();
229
230         const int stride = 3840; /* bytes per row for Cairo::FORMAT_ARGB32 */
231         const uint8_t* data = frame_buffer->get_data ();
232
233         /* fill frame buffer (320kB) */
234
235         uint16_t* fb = (uint16_t*) device_frame_buffer;
236
237         for (int row = 0; row < rows; ++row) {
238
239                 const uint8_t* dp = data + row * stride;
240
241                 for (int col = 0; col < cols; ++col) {
242
243                         /* fetch r, g, b (range 0..255). Ignore alpha */
244
245                         const int r = (*((const uint32_t*)dp) >> 16) & 0xff;
246                         const int g = (*((const uint32_t*)dp) >> 8) & 0xff;
247                         const int b = *((const uint32_t*)dp) & 0xff;
248
249                         /* convert to 5 bits, 6 bits, 5 bits, respectively */
250                         /* generate 16 bit BGB565 value */
251
252                         *fb++ = (r >> 3) | ((g & 0xfc) << 3) | ((b & 0xf8) << 8);
253
254                         dp += 4;
255                 }
256
257                 /* skip 128 bytes to next line. This is filler, used to avoid line borders occuring in the middle of 512
258                    byte USB buffers
259                 */
260
261                 fb += 64; /* 128 bytes = 64 int16_t */
262         }
263
264         return 0;
265 }
266
267 bool
268 Push2::redraw ()
269 {
270         string tc_clock_text;
271         string bbt_clock_text;
272
273         if (session) {
274                 framepos_t audible = session->audible_frame();
275                 Timecode::Time TC;
276                 bool negative = false;
277
278                 if (audible < 0) {
279                         audible = -audible;
280                         negative = true;
281                 }
282
283                 session->timecode_time (audible, TC);
284
285                 TC.negative = TC.negative || negative;
286
287                 tc_clock_text = Timecode::timecode_format_time(TC);
288
289                 Timecode::BBT_Time bbt = session->tempo_map().bbt_at_frame (audible);
290                 char buf[16];
291
292 #define BBT_BAR_CHAR "|"
293
294                 if (negative) {
295                         snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
296                                   bbt.bars, bbt.beats, bbt.ticks);
297                 } else {
298                         snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
299                                   bbt.bars, bbt.beats, bbt.ticks);
300                 }
301
302                 bbt_clock_text = buf;
303         }
304
305         bool dirty = false;
306
307         if (tc_clock_text != tc_clock_layout->get_text()) {
308                 dirty = true;
309                 tc_clock_layout->set_text (tc_clock_text);
310         }
311
312         if (bbt_clock_text != tc_clock_layout->get_text()) {
313                 dirty = true;
314                 bbt_clock_layout->set_text (bbt_clock_text);
315         }
316
317         if (!dirty) {
318                 return false;
319         }
320
321         context->set_source_rgb (0.764, 0.882, 0.882);
322         context->rectangle (0, 0, 960, 160);
323         context->fill ();
324         context->set_source_rgb (0.23, 0.0, 0.349);
325         context->move_to (650, 25);
326         tc_clock_layout->update_from_cairo_context (context);
327         tc_clock_layout->show_in_cairo_context (context);
328         context->move_to (650, 60);
329         bbt_clock_layout->update_from_cairo_context (context);
330         bbt_clock_layout->show_in_cairo_context (context);
331
332         for (int n = 0; n < 8; ++n) {
333                 context->move_to (10 + (n*120), 2);
334                 upper_layout[n]->update_from_cairo_context (context);
335                 upper_layout[n]->show_in_cairo_context (context);
336         }
337
338         for (int n = 0; n < 8; ++n) {
339                 context->move_to (10 + (n*120), 140);
340                 lower_layout[n]->update_from_cairo_context (context);
341                 lower_layout[n]->show_in_cairo_context (context);
342         }
343
344         for (int n = 0; n < 8; ++n) {
345                 context->move_to (10 + (n*120), 120);
346                 context->set_source_rgb (0.0, 0.0, 0.0);
347                 mid_layout[n]->update_from_cairo_context (context);
348                 mid_layout[n]->show_in_cairo_context (context);
349         }
350
351         /* render clock */
352         /* render foo */
353         /* render bar */
354
355         return true;
356 }
357
358 bool
359 Push2::vblank ()
360 {
361         int transferred = 0;
362         const int timeout_msecs = 1000;
363         int err;
364
365         if ((err = libusb_bulk_transfer (handle, 0x01, frame_header, sizeof (frame_header), &transferred, timeout_msecs))) {
366                 return false;
367         }
368
369         if (redraw()) {
370                 /* things changed */
371                 bitblt_to_device_frame_buffer ();
372         }
373
374         if ((err = libusb_bulk_transfer (handle, 0x01, (uint8_t*) device_frame_buffer , 2 * rows * pixels_per_row, &transferred, timeout_msecs))) {
375                 return false;
376         }
377
378         return true;
379 }
380
381 int
382 Push2::set_active (bool yn)
383 {
384         DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active init with yn: '%1'\n", yn));
385
386         if (yn == active()) {
387                 return 0;
388         }
389
390         if (yn) {
391
392                 /* start event loop */
393
394                 BaseUI::run ();
395
396                 if (open ()) {
397                         DEBUG_TRACE (DEBUG::Push2, "device open failed\n");
398                         close ();
399                         return -1;
400                 }
401
402                 /* Connect input port to event loop */
403
404                 AsyncMIDIPort* asp;
405
406                 asp = dynamic_cast<AsyncMIDIPort*> (_input_port);
407                 asp->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &Push2::midi_input_handler), _input_port));
408                 asp->xthread().attach (main_loop()->get_context());
409
410                 connect_session_signals ();
411
412                 /* set up periodic task used to push a frame buffer to the
413                  * device (25fps). The device can handle 60fps, but we don't
414                  * need that frame rate.
415                  */
416
417                 Glib::RefPtr<Glib::TimeoutSource> vblank_timeout = Glib::TimeoutSource::create (40); // milliseconds
418                 vblank_connection = vblank_timeout->connect (sigc::mem_fun (*this, &Push2::vblank));
419                 vblank_timeout->attach (main_loop()->get_context());
420
421
422                 Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (1000); // milliseconds
423                 periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &Push2::periodic));
424                 periodic_timeout->attach (main_loop()->get_context());
425
426         } else {
427
428                 stop ();
429
430         }
431
432         ControlProtocol::set_active (yn);
433
434         DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active done with yn: '%1'\n", yn));
435
436         return 0;
437 }
438
439 void
440 Push2::write (const MidiByteArray& data)
441 {
442         cerr << data << endl;
443         /* immediate delivery */
444         _output_port->write (&data[0], data.size(), 0);
445 }
446
447 bool
448 Push2::midi_input_handler (IOCondition ioc, MIDI::Port* port)
449 {
450         if (ioc & ~IO_IN) {
451                 DEBUG_TRACE (DEBUG::Push2, "MIDI port closed\n");
452                 return false;
453         }
454
455         if (ioc & IO_IN) {
456
457                 // DEBUG_TRACE (DEBUG::Push2, string_compose ("something happend on  %1\n", port->name()));
458
459                 AsyncMIDIPort* asp = dynamic_cast<AsyncMIDIPort*>(port);
460                 if (asp) {
461                         asp->clear ();
462                 }
463
464                 //DEBUG_TRACE (DEBUG::Push2, string_compose ("data available on %1\n", port->name()));
465                 framepos_t now = AudioEngine::instance()->sample_time();
466                 port->parse (now);
467         }
468
469         return true;
470 }
471
472 bool
473 Push2::periodic ()
474 {
475         return true;
476 }
477
478 void
479 Push2::connect_to_parser ()
480 {
481         DEBUG_TRACE (DEBUG::Push2, string_compose ("Connecting to signals on port %2\n", _input_port->name()));
482
483         MIDI::Parser* p = _input_port->parser();
484
485         /* Incoming sysex */
486         p->sysex.connect_same_thread (*this, boost::bind (&Push2::handle_midi_sysex, this, _1, _2, _3));
487         /* V-Pot messages are Controller */
488         p->controller.connect_same_thread (*this, boost::bind (&Push2::handle_midi_controller_message, this, _1, _2));
489         /* Button messages are NoteOn */
490         p->note_on.connect_same_thread (*this, boost::bind (&Push2::handle_midi_note_on_message, this, _1, _2));
491         /* Button messages are NoteOn but libmidi++ sends note-on w/velocity = 0 as note-off so catch them too */
492         p->note_off.connect_same_thread (*this, boost::bind (&Push2::handle_midi_note_on_message, this, _1, _2));
493         /* Fader messages are Pitchbend */
494         p->channel_pitchbend[0].connect_same_thread (*this, boost::bind (&Push2::handle_midi_pitchbend_message, this, _1, _2));
495 }
496
497 void
498 Push2::handle_midi_sysex (MIDI::Parser&, MIDI::byte* raw_bytes, size_t sz)
499 {
500         cerr << "sysex, " << sz << " bytes\n";
501 }
502
503 void
504 Push2::handle_midi_controller_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
505 {
506         CCButtonMap::iterator b = cc_button_map.find (ev->controller_number);
507         if (b != cc_button_map.end()) {
508                 if (ev->value == 0) {
509                         (this->*b->second->release_method)();
510                 } else {
511                         (this->*b->second->press_method)();
512                 }
513         }
514 }
515
516 void
517 Push2::handle_midi_note_on_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
518 {
519         cerr << "note on" << (int) ev->note_number << ", velocity " << (int) ev->velocity << endl;
520 }
521
522 void
523 Push2::handle_midi_note_off_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
524 {
525         cerr << "note on" << (int) ev->note_number << ", velocity " << (int) ev->velocity << endl;
526 }
527
528 void
529 Push2::handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t pb)
530 {
531         cerr << "pitchbend @ " << pb << endl;
532 }
533
534 void
535 Push2::build_maps ()
536 {
537         /* Pads */
538
539         Pad* pad;
540
541 #define MAKE_PAD(x,y,nn) \
542         pad = new Pad ((x), (y), (nn)); \
543         nn_pad_map.insert (make_pair (pad->extra(), pad)); \
544         coord_pad_map.insert (make_pair (pad->coord(), pad));
545
546         MAKE_PAD (0, 1, 93);
547         MAKE_PAD (0, 2, 94);
548         MAKE_PAD (0, 3, 95);
549         MAKE_PAD (0, 4, 96);
550         MAKE_PAD (0, 5, 97);
551         MAKE_PAD (0, 6, 98);
552         MAKE_PAD (0, 7, 90);
553         MAKE_PAD (1, 0, 84);
554         MAKE_PAD (1, 1, 85);
555         MAKE_PAD (1, 2, 86);
556         MAKE_PAD (1, 3, 87);
557         MAKE_PAD (1, 4, 88);
558         MAKE_PAD (1, 5, 89);
559         MAKE_PAD (1, 6, 90);
560         MAKE_PAD (1, 7, 91);
561         MAKE_PAD (2, 0, 76);
562         MAKE_PAD (2, 1, 77);
563         MAKE_PAD (2, 2, 78);
564         MAKE_PAD (2, 3, 79);
565         MAKE_PAD (2, 4, 80);
566         MAKE_PAD (2, 5, 81);
567         MAKE_PAD (2, 6, 82);
568         MAKE_PAD (2, 7, 83);
569         MAKE_PAD (3, 0, 68);
570         MAKE_PAD (3, 1, 69);
571         MAKE_PAD (3, 2, 70);
572         MAKE_PAD (3, 3, 71);
573         MAKE_PAD (3, 4, 72);
574         MAKE_PAD (3, 5, 73);
575         MAKE_PAD (3, 6, 74);
576         MAKE_PAD (3, 7, 75);
577         MAKE_PAD (4, 0, 60);
578         MAKE_PAD (4, 1, 61);
579         MAKE_PAD (4, 2, 62);
580         MAKE_PAD (4, 3, 63);
581         MAKE_PAD (4, 4, 64);
582         MAKE_PAD (4, 5, 65);
583         MAKE_PAD (4, 6, 66);
584         MAKE_PAD (4, 7, 67);
585         MAKE_PAD (5, 0, 52);
586         MAKE_PAD (5, 1, 53);
587         MAKE_PAD (5, 2, 54);
588         MAKE_PAD (5, 3, 56);
589         MAKE_PAD (5, 4, 56);
590         MAKE_PAD (5, 5, 57);
591         MAKE_PAD (5, 6, 58);
592         MAKE_PAD (5, 7, 59);
593         MAKE_PAD (6, 0, 44);
594         MAKE_PAD (6, 1, 45);
595         MAKE_PAD (6, 2, 46);
596         MAKE_PAD (6, 3, 47);
597         MAKE_PAD (6, 4, 48);
598         MAKE_PAD (6, 5, 49);
599         MAKE_PAD (6, 6, 50);
600         MAKE_PAD (6, 7, 51);
601         MAKE_PAD (7, 0, 36);
602         MAKE_PAD (7, 1, 37);
603         MAKE_PAD (7, 2, 38);
604         MAKE_PAD (7, 3, 39);
605         MAKE_PAD (7, 4, 40);
606         MAKE_PAD (7, 5, 41);
607         MAKE_PAD (7, 6, 42);
608         MAKE_PAD (7, 7, 43);
609
610         /* Now color buttons */
611
612         Button *button;
613
614 #define MAKE_COLOR_BUTTON(i,cc) \
615         button = new ColorButton ((i), (cc)); \
616         cc_button_map.insert (make_pair (button->controller_number(), button)); \
617         id_button_map.insert (make_pair (button->id, button));
618 #define MAKE_COLOR_BUTTON_PRESS(i,cc,p)\
619         button = new ColorButton ((i), (cc), (p)); \
620         cc_button_map.insert (make_pair (button->controller_number(), button)); \
621         id_button_map.insert (make_pair (button->id, button))
622
623         MAKE_COLOR_BUTTON (Upper1, 102);
624         MAKE_COLOR_BUTTON (Upper2, 103);
625         MAKE_COLOR_BUTTON (Upper3, 104);
626         MAKE_COLOR_BUTTON (Upper4, 105);
627         MAKE_COLOR_BUTTON (Upper5, 106);
628         MAKE_COLOR_BUTTON (Upper6, 107);
629         MAKE_COLOR_BUTTON (Upper7, 108);
630         MAKE_COLOR_BUTTON (Upper8, 109);
631         MAKE_COLOR_BUTTON (Lower1, 21);
632         MAKE_COLOR_BUTTON (Lower2, 22);
633         MAKE_COLOR_BUTTON (Lower3, 23);
634         MAKE_COLOR_BUTTON (Lower4, 24);
635         MAKE_COLOR_BUTTON (Lower5, 25);
636         MAKE_COLOR_BUTTON (Lower6, 26);
637         MAKE_COLOR_BUTTON (Lower7, 27);
638         MAKE_COLOR_BUTTON (Mute, 60);
639         MAKE_COLOR_BUTTON (Solo, 61);
640         MAKE_COLOR_BUTTON (Stop, 29);
641         MAKE_COLOR_BUTTON (Fwd32ndT, 43);
642         MAKE_COLOR_BUTTON (Fwd32nd,42 );
643         MAKE_COLOR_BUTTON (Fwd16thT, 41);
644         MAKE_COLOR_BUTTON (Fwd16th, 40);
645         MAKE_COLOR_BUTTON (Fwd8thT, 39 );
646         MAKE_COLOR_BUTTON (Fwd8th, 38);
647         MAKE_COLOR_BUTTON (Fwd4trT, 37);
648         MAKE_COLOR_BUTTON (Fwd4tr, 36);
649         MAKE_COLOR_BUTTON (Automate, 89);
650         MAKE_COLOR_BUTTON_PRESS (RecordEnable, 86, &Push2::button_recenable);
651         MAKE_COLOR_BUTTON_PRESS (Play, 85, &Push2::button_play);
652
653 #define MAKE_WHITE_BUTTON(i,cc)\
654         button = new WhiteButton ((i), (cc)); \
655         cc_button_map.insert (make_pair (button->controller_number(), button)); \
656         id_button_map.insert (make_pair (button->id, button))
657 #define MAKE_WHITE_BUTTON_PRESS(i,cc,p)\
658         button = new WhiteButton ((i), (cc), (p)); \
659         cc_button_map.insert (make_pair (button->controller_number(), button)); \
660         id_button_map.insert (make_pair (button->id, button))
661
662         MAKE_WHITE_BUTTON (TapTempo, 3);
663         MAKE_WHITE_BUTTON_PRESS (Metronome, 9, &Push2::button_metronome);
664         MAKE_WHITE_BUTTON (Setup, 30);
665         MAKE_WHITE_BUTTON (User, 59);
666         MAKE_WHITE_BUTTON (Delete, 118);
667         MAKE_WHITE_BUTTON (AddDevice, 52);
668         MAKE_WHITE_BUTTON (Device, 110);
669         MAKE_WHITE_BUTTON (Mix, 112);
670         MAKE_WHITE_BUTTON (Undo, 119);
671         MAKE_WHITE_BUTTON (AddTrack, 53);
672         MAKE_WHITE_BUTTON (Browse, 113);
673         MAKE_WHITE_BUTTON (Convert, 35);
674         MAKE_WHITE_BUTTON (DoubleLoop, 117);
675         MAKE_WHITE_BUTTON (Quantize, 116);
676         MAKE_WHITE_BUTTON (Duplicate, 88);
677         MAKE_WHITE_BUTTON (New, 87);
678         MAKE_WHITE_BUTTON (FixedLength, 90);
679         MAKE_WHITE_BUTTON_PRESS (Up, 46, &Push2::button_up);
680         MAKE_WHITE_BUTTON_PRESS (Right, 45, &Push2::button_right);
681         MAKE_WHITE_BUTTON_PRESS (Down, 47, &Push2::button_down);
682         MAKE_WHITE_BUTTON_PRESS (Left, 44, &Push2::button_left);
683         MAKE_WHITE_BUTTON_PRESS (Repeat, 56, &Push2::button_repeat);
684         MAKE_WHITE_BUTTON (Accent, 57);
685         MAKE_WHITE_BUTTON (Scale, 58);
686         MAKE_WHITE_BUTTON (Layout, 31);
687         MAKE_WHITE_BUTTON (OctaveUp, 55);
688         MAKE_WHITE_BUTTON (PageRight, 63);
689         MAKE_WHITE_BUTTON (OctaveDown, 54);
690         MAKE_WHITE_BUTTON (PageLeft, 62);
691         MAKE_WHITE_BUTTON (Shift, 49);
692         MAKE_WHITE_BUTTON (Select, 48);
693 }
694
695 void
696 Push2::thread_init ()
697 {
698         struct sched_param rtparam;
699
700         pthread_set_name (event_loop_name().c_str());
701
702         PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
703         ARDOUR::SessionEvent::create_per_thread_pool (event_loop_name(), 128);
704
705         memset (&rtparam, 0, sizeof (rtparam));
706         rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
707
708         if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
709                 // do we care? not particularly.
710         }
711 }
712
713 void
714 Push2::connect_session_signals()
715 {
716         // receive routes added
717         //session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_routes_added, this, _1), this);
718         // receive VCAs added
719         //session->vca_manager().VCAAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_vca_added, this, _1), this);
720
721         // receive record state toggled
722         session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_record_state_changed, this), this);
723         // receive transport state changed
724         session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_transport_state_changed, this), this);
725         session->TransportLooped.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_loop_state_changed, this), this);
726         // receive punch-in and punch-out
727         Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_parameter_changed, this, _1), this);
728         session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_parameter_changed, this, _1), this);
729         // receive rude solo changed
730         session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_solo_active_changed, this, _1), this);
731 }
732
733 void
734 Push2::notify_record_state_changed ()
735 {
736         IDButtonMap::iterator b = id_button_map.find (RecordEnable);
737
738         if (b == id_button_map.end()) {
739                 return;
740         }
741
742         b->second->set_color (LED::Red);
743
744         switch (session->record_status ()) {
745         case Session::Disabled:
746                 b->second->set_state (LED::Off);
747                 break;
748         case Session::Enabled:
749                 b->second->set_state (LED::Blinking4th);
750                 break;
751         case Session::Recording:
752                 b->second->set_state (LED::OneShot24th);
753                 break;
754         }
755
756         write (b->second->state_msg());
757 }
758
759 void
760 Push2::notify_transport_state_changed ()
761 {
762         IDButtonMap::iterator b = id_button_map.find (Play);
763
764         if (b == id_button_map.end()) {
765                 return;
766         }
767
768         if (session->transport_rolling()) {
769                 b->second->set_state (LED::OneShot24th);
770                 b->second->set_color (LED::Blue);
771         } else {
772                 b->second->set_state (LED::Off);
773         }
774
775         write (b->second->state_msg());
776 }
777
778 void
779 Push2::notify_loop_state_changed ()
780 {
781 }
782
783 void
784 Push2::notify_parameter_changed (std::string)
785 {
786 }
787
788 void
789 Push2::notify_solo_active_changed (bool yn)
790 {
791         IDButtonMap::iterator b = id_button_map.find (Solo);
792
793         if (b == id_button_map.end()) {
794                 return;
795         }
796
797         if (yn) {
798                 b->second->set_state (LED::Blinking24th);
799         } else {
800                 b->second->set_state (LED::Off);
801         }
802
803         write (b->second->state_msg());
804 }
805
806 XMLNode&
807 Push2::get_state()
808 {
809         XMLNode& node (ControlProtocol::get_state());
810         XMLNode* child;
811
812         child = new XMLNode (X_("Input"));
813         child->add_child_nocopy (_async_in->get_state());
814         node.add_child_nocopy (*child);
815         child = new XMLNode (X_("Output"));
816         child->add_child_nocopy (_async_out->get_state());
817         node.add_child_nocopy (*child);
818
819         return node;
820 }
821
822 int
823 Push2::set_state (const XMLNode & node, int version)
824 {
825         DEBUG_TRACE (DEBUG::Push2, string_compose ("Push2::set_state: active %1\n", active()));
826
827         int retval = 0;
828
829         if (ControlProtocol::set_state (node, version)) {
830                 return -1;
831         }
832
833         XMLNode* child;
834
835         if ((child = node.child (X_("Input"))) != 0) {
836                 XMLNode* portnode = child->child (Port::state_node_name.c_str());
837                 if (portnode) {
838                         _async_in->set_state (*portnode, version);
839                 }
840         }
841
842         if ((child = node.child (X_("Output"))) != 0) {
843                 XMLNode* portnode = child->child (Port::state_node_name.c_str());
844                 if (portnode) {
845                         _async_out->set_state (*portnode, version);
846                 }
847         }
848
849         return retval;
850 }