2 Copyright (C) 2016 Paul Davis
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.
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.
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.
19 #ifndef __ardour_push2_h__
20 #define __ardour_push2_h__
30 #include <cairomm/refptr.h>
32 #define ABSTRACT_UI_EXPORTS
33 #include "pbd/abstract_ui.h"
35 #include "midi++/types.h"
37 #include "ardour/types.h"
39 #include "control_protocol/control_protocol.h"
40 #include "control_protocol/types.h"
42 #include "midi_byte_array.h"
66 namespace ArdourSurface {
68 struct Push2Request : public BaseUI::BaseRequestObject {
78 class Push2 : public ARDOUR::ControlProtocol
79 , public AbstractUI<Push2Request>
85 Upper1, Upper2, Upper3, Upper4, Upper5, Upper6, Upper7, Upper8,
99 Lower1, Lower2, Lower3, Lower4, Lower5, Lower6, Lower7, Lower8,
167 LED (uint8_t e) : _extra (e), _color_index (Black), _state (NoTransition) {}
170 uint8_t extra () const { return _extra; }
171 uint8_t color_index () const { return _color_index; }
172 State state () const { return _state; }
174 void set_color (uint8_t color_index);
175 void set_state (State state);
177 virtual MidiByteArray state_msg() const = 0;
181 uint8_t _color_index;
185 struct Pad : public LED {
192 Pad (int xx, int yy, uint8_t ex)
196 , do_when_pressed (FlashOn)
198 , perma_color (LED::Black)
201 MidiByteArray state_msg () const { return MidiByteArray (3, 0x90|_state, _extra, _color_index); }
203 int coord () const { return (y * 8) + x; }
204 int note_number() const { return extra(); }
213 struct Button : public LED {
214 Button (ButtonID bb, uint8_t ex)
217 , press_method (&Push2::relax)
218 , release_method (&Push2::relax)
219 , long_press_method (&Push2::relax)
222 Button (ButtonID bb, uint8_t ex, void (Push2::*press)())
225 , press_method (press)
226 , release_method (&Push2::relax)
227 , long_press_method (&Push2::relax)
230 Button (ButtonID bb, uint8_t ex, void (Push2::*press)(), void (Push2::*release)())
233 , press_method (press)
234 , release_method (release)
235 , long_press_method (&Push2::relax)
238 Button (ButtonID bb, uint8_t ex, void (Push2::*press)(), void (Push2::*release)(), void (Push2::*long_press)())
241 , press_method (press)
242 , release_method (release)
243 , long_press_method (long_press)
246 MidiByteArray state_msg () const { return MidiByteArray (3, 0xb0|_state, _extra, _color_index); }
247 int controller_number() const { return extra(); }
250 void (Push2::*press_method)();
251 void (Push2::*release_method)();
252 void (Push2::*long_press_method)();
253 sigc::connection timeout_connection;
256 struct ColorButton : public Button {
257 ColorButton (ButtonID bb, uint8_t ex)
261 ColorButton (ButtonID bb, uint8_t ex, void (Push2::*press)())
262 : Button (bb, ex, press) {}
264 ColorButton (ButtonID bb, uint8_t ex, void (Push2::*press)(), void (Push2::*release)())
265 : Button (bb, ex, press, release) {}
267 ColorButton (ButtonID bb, uint8_t ex, void (Push2::*press)(), void (Push2::*release)(), void (Push2::*long_press)())
268 : Button (bb, ex, press, release, long_press) {}
271 struct WhiteButton : public Button {
272 WhiteButton (ButtonID bb, uint8_t ex)
275 WhiteButton (ButtonID bb, uint8_t ex, void (Push2::*press)())
276 : Button (bb, ex, press) {}
278 WhiteButton (ButtonID bb, uint8_t ex, void (Push2::*press)(), void (Push2::*release)())
279 : Button (bb, ex, press, release) {}
281 WhiteButton (ButtonID bb, uint8_t ex, void (Push2::*press)(), void (Push2::*release)(), void (Push2::*long_press)())
282 : Button (bb, ex, press, release, long_press) {}
286 Push2 (ARDOUR::Session&);
289 static bool probe ();
290 static void* request_factory (uint32_t);
292 std::list<boost::shared_ptr<ARDOUR::Bundle> > bundles ();
294 bool has_editor () const { return true; }
295 void* get_gui () const;
296 void tear_down_gui ();
298 int set_active (bool yn);
299 XMLNode& get_state();
300 int set_state (const XMLNode & node, int version);
302 PBD::Signal0<void> ConnectionChange;
304 boost::shared_ptr<ARDOUR::Port> input_port();
305 boost::shared_ptr<ARDOUR::Port> output_port();
307 int pad_note (int row, int col) const;
308 PBD::Signal0<void> PadChange;
310 void set_pad_scale (int root, int octave, MusicalMode::Type mode, bool inkey);
312 MusicalMode::Type mode() const { return _mode; }
313 int scale_root() const { return _scale_root; }
314 int root_octave() const { return _root_octave; }
315 bool in_key() const { return _in_key; }
317 Push2Layout* current_layout() const;
325 ModifierState modifier_state() const { return _modifier_state; }
327 Button* button_by_id (ButtonID);
329 void write (const MidiByteArray&);
331 uint8_t get_color_index (uint32_t rgb);
333 static const int cols;
334 static const int rows;
337 libusb_device_handle *handle;
338 uint8_t frame_header[16];
339 uint16_t* device_frame_buffer;
341 Cairo::RefPtr<Cairo::ImageSurface> frame_buffer;
342 sigc::connection vblank_connection;
343 sigc::connection periodic_connection;
345 ModifierState _modifier_state;
347 static const int pixels_per_row;
349 void do_request (Push2Request*);
354 int blit_to_device_frame_buffer ();
359 /* map of Buttons by CC */
360 typedef std::map<int,Button*> CCButtonMap;
361 CCButtonMap cc_button_map;
362 /* map of Buttons by ButtonID */
363 typedef std::map<ButtonID,Button*> IDButtonMap;
364 IDButtonMap id_button_map;
365 std::set<ButtonID> buttons_down;
366 std::set<ButtonID> consumed;
368 bool button_long_press_timeout (ButtonID id);
369 void start_press_timeout (Button&, ButtonID);
371 void init_buttons (bool startup);
372 void init_touch_strip ();
374 /* map of Pads by note number */
375 typedef std::map<int,Pad*> NNPadMap;
378 void set_button_color (ButtonID, uint8_t color_index);
379 void set_button_state (ButtonID, LED::State);
380 void set_led_color (ButtonID, uint8_t color_index);
381 void set_led_state (ButtonID, LED::State);
385 // Bundle to represent our input ports
386 boost::shared_ptr<ARDOUR::Bundle> _input_bundle;
387 // Bundle to represent our output ports
388 boost::shared_ptr<ARDOUR::Bundle> _output_bundle;
390 MIDI::Port* _input_port;
391 MIDI::Port* _output_port;
392 boost::shared_ptr<ARDOUR::Port> _async_in;
393 boost::shared_ptr<ARDOUR::Port> _async_out;
395 void connect_to_parser ();
396 void handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t);
397 void handle_midi_controller_message (MIDI::Parser&, MIDI::EventTwoBytes*);
398 void handle_midi_note_on_message (MIDI::Parser&, MIDI::EventTwoBytes*);
399 void handle_midi_note_off_message (MIDI::Parser&, MIDI::EventTwoBytes*);
400 void handle_midi_sysex (MIDI::Parser&, MIDI::byte *, size_t count);
402 bool midi_input_handler (Glib::IOCondition ioc, MIDI::Port* port);
407 PBD::ScopedConnectionList session_connections;
408 void connect_session_signals ();
409 void notify_record_state_changed ();
410 void notify_transport_state_changed ();
411 void notify_loop_state_changed ();
412 void notify_parameter_changed (std::string);
413 void notify_solo_active_changed (bool);
417 void button_recenable ();
420 void button_right ();
422 void button_metronome ();
423 void button_repeat ();
425 void button_fixed_length ();
427 void button_browse ();
430 void button_fwd32t ();
431 void button_fwd32 ();
432 void button_fwd16t ();
433 void button_fwd16 ();
434 void button_fwd8t ();
436 void button_fwd4t ();
438 void button_add_track ();
440 void button_shift_press ();
441 void button_shift_release ();
442 void button_shift_long_press ();
443 void button_select_press ();
444 void button_select_release ();
445 void button_select_long_press ();
446 void button_page_left ();
447 void button_page_right ();
448 void button_octave_up ();
449 void button_octave_down ();
450 void button_layout_press ();
451 void button_scale_press ();
452 void button_mix_press ();
454 void button_upper (uint32_t n);
455 void button_lower (uint32_t n);
457 void button_upper_1 () { button_upper (0); }
458 void button_upper_2 () { button_upper (1); }
459 void button_upper_3 () { button_upper (2); }
460 void button_upper_4 () { button_upper (3); }
461 void button_upper_5 () { button_upper (4); }
462 void button_upper_6 () { button_upper (5); }
463 void button_upper_7 () { button_upper (6); }
464 void button_upper_8 () { button_upper (7); }
465 void button_lower_1 () { button_lower (0); }
466 void button_lower_2 () { button_lower (1); }
467 void button_lower_3 () { button_lower (2); }
468 void button_lower_4 () { button_lower (3); }
469 void button_lower_5 () { button_lower (4); }
470 void button_lower_6 () { button_lower (5); }
471 void button_lower_7 () { button_lower (6); }
472 void button_lower_8 () { button_lower (7); }
477 /* non-strip encoders */
479 void other_vpot (int, int);
480 void other_vpot_touch (int, bool);
482 /* special Stripables */
484 boost::shared_ptr<ARDOUR::Stripable> master;
485 boost::shared_ptr<ARDOUR::Stripable> monitor;
487 /* Cairo graphics context */
489 Cairo::RefPtr<Cairo::Context> context;
492 ARDOUR::microseconds_t splash_start;
496 mutable Glib::Threads::Mutex layout_lock;
497 Push2Layout* _current_layout;
498 Push2Layout* drawn_layout;
499 Push2Layout* mix_layout;
500 Push2Layout* scale_layout;
501 Push2Layout* track_mix_layout;
503 bool pad_filter (ARDOUR::MidiBuffer& in, ARDOUR::MidiBuffer& out) const;
505 boost::weak_ptr<ARDOUR::MidiTrack> current_pad_target;
507 PBD::ScopedConnection port_reg_connection;
508 void port_registration_handler ();
510 enum ConnectionState {
511 InputConnected = 0x1,
512 OutputConnected = 0x2
515 int connection_state;
516 bool connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1, boost::weak_ptr<ARDOUR::Port>, std::string name2, bool yn);
517 PBD::ScopedConnection port_connection;
526 PBD::ScopedConnection selection_connection;
527 void stripable_selection_change (ARDOUR::StripableNotificationListPtr);
529 MusicalMode::Type _mode;
537 void set_percussive_mode (bool);
541 typedef std::map<uint32_t,uint8_t> ColorMap;
542 typedef std::stack<uint8_t> ColorMapFreeList;
544 ColorMapFreeList color_map_free_list;
545 void build_color_map ();
550 #endif /* __ardour_push2_h__ */