summaryrefslogtreecommitdiff
path: root/libs/surfaces/faderport8/fp8_button.h
blob: 4b27714cd199f20789884eda977d429f99a560f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/* FaderPort8 Button Interface
 *
 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef _ardour_surfaces_fp8button_h_
#define _ardour_surfaces_fp8button_h_

#include <stdint.h>

#include "pbd/base_ui.h"
#include "pbd/signals.h"

#include "fp8_base.h"

namespace ArdourSurface {

/* virtual base-class and interface */
class FP8ButtonInterface
{
public:
	FP8ButtonInterface () {}
	virtual ~FP8ButtonInterface () {}

	/* user API */
	PBD::Signal0<void> pressed;
	PBD::Signal0<void> released;

	virtual bool is_pressed () const { return false; }
	virtual bool is_active () const { return false; }

	virtual void ignore_release () {}

	/* internal API - called from midi thread,
	 * user pressed/released button the device
	 */
	virtual bool midi_event (bool) = 0;

	/* internal API - called from surface thread
	 * set Light on the button
	 */
	virtual void set_active (bool a) = 0;
	virtual void set_color (uint32_t rgba) {}
	virtual void set_blinking (bool) {}

	static bool force_change; // used during init
};

/* ****************************************************************************
 * Implementations
 */

class FP8DummyButton : public FP8ButtonInterface
{
public:
	virtual void set_active (bool a) {}
	virtual bool midi_event (bool) { return false; }
};


/* common implementation */
class FP8ButtonBase : public FP8ButtonInterface
{
public:
	FP8ButtonBase (FP8Base& b)
		: _base (b)
		, _pressed (false)
		, _active (false)
		, _ignore_release (false)
		, _rgba (0)
		, _blinking (false)
	{ }

	bool is_pressed () const { return _pressed; }
	bool is_active () const { return _active; }

	virtual bool midi_event (bool a)
	{
		if (a == _pressed) {
			return false;
		}
		_pressed = a;
		if (a) {
			pressed (); /* EMIT SIGNAL */
		} else {
			if (_ignore_release) {
				_ignore_release = false;
			} else {
				released (); /* EMIT SIGNAL */
			}
		}
		return true;
	}

	virtual void ignore_release () {
		if (_pressed) {
			_ignore_release = true;
		}
	}

	bool blinking () const { return _blinking; }

	void set_blinking (bool yes) {
		if (yes && !_blinking) {
			_blinking = true;
			_base.BlinkIt.connect_same_thread (_blink_connection, boost::bind (&FP8ButtonBase::blink, this, _1));
		} else if (!yes && _blinking) {
			_blink_connection.disconnect ();
			_blinking = false;
			blink (true);
		}
	}

protected:
	FP8Base&              _base;
	bool                  _pressed;
	bool                  _active;
	bool                  _ignore_release;
	uint32_t              _rgba;
	virtual void blink (bool onoff) = 0;

private:
	PBD::ScopedConnection _blink_connection;
	bool _blinking;
};

/* A basic LED or RGB button, not shift sensitive */
class FP8Button : public FP8ButtonBase
{
public:
	FP8Button (FP8Base& b, uint8_t id, bool color = false)
		: FP8ButtonBase (b)
		, _midi_id (id)
		, _has_color (color)
	{ }

	virtual void set_active (bool a)
	{
		if (_active == a && !force_change) {
			return;
		}
		_active = a;
		_base.tx_midi3 (0x90, _midi_id, a ? 0x7f : 0x00);
	}

	void set_color (uint32_t rgba)
	{
		if (!_has_color || _rgba == rgba) {
			return;
		}
		_rgba = rgba;
		_base.tx_midi3 (0x91, _midi_id, (_rgba >> 25) & 0x7f);
		_base.tx_midi3 (0x92, _midi_id, (_rgba >> 17) & 0x7f);
		_base.tx_midi3 (0x93, _midi_id, (_rgba >>  9) & 0x7f);
	}

protected:
	void blink (bool onoff)
	{
		if (!_active) { return; }
		_base.tx_midi3 (0x90, _midi_id, onoff ? 0x7f : 0x00);
	}

	uint8_t  _midi_id; // MIDI-note
	bool     _has_color;
};

/* footswitch and encoder-press buttons */
class FP8ReadOnlyButton : public FP8Button
{
public:
	FP8ReadOnlyButton (FP8Base& b, uint8_t id, bool color = false)
		: FP8Button (b, id, color)
	{}

	void set_active (bool) { }
};

/* virtual button. used for shift toggle. */
class ShadowButton : public FP8ButtonBase
{
public:
	ShadowButton (FP8Base& b)
		: FP8ButtonBase (b)
	{}

	PBD::Signal1<void, bool> ActiveChanged;
	PBD::Signal0<void> ColourChanged;

	uint32_t color () const { return _rgba; }

	bool midi_event (bool a)
	{
		assert (0);
		return false;
	}

	bool set_pressed (bool a)
	{
		return FP8ButtonBase::midi_event (a);
	}

	void set_active (bool a)
	{
		if (_active == a && !force_change) {
			return;
		}
		_active = a;
		ActiveChanged (a); /* EMIT SIGNAL */
	}

	void set_color (uint32_t rgba)
	{
		if (_rgba == rgba) {
			return;
		}
		_rgba = rgba;
		ColourChanged ();
	}

protected:
	void blink (bool onoff) {
		if (!_active) { return; }
		ActiveChanged (onoff);
	}
};

/* Wraps 2 buttons with the same physical MIDI ID */
class FP8DualButton : public FP8ButtonInterface
{
public:
	FP8DualButton (FP8Base& b, uint8_t id, bool color = false)
		: _base (b)
		, _b0 (b)
		, _b1 (b)
		, _midi_id (id)
		, _has_color (color)
		, _rgba (0)
		, _shift (false)
	{
		_b0.ActiveChanged.connect_same_thread (_button_connections, boost::bind (&FP8DualButton::active_changed, this, false, _1));
		_b1.ActiveChanged.connect_same_thread (_button_connections, boost::bind (&FP8DualButton::active_changed, this, true, _1));
		if (_has_color) {
			_b0.ColourChanged.connect_same_thread (_button_connections, boost::bind (&FP8DualButton::colour_changed, this, false));
			_b1.ColourChanged.connect_same_thread (_button_connections, boost::bind (&FP8DualButton::colour_changed, this, true));
		}
	}

	bool midi_event (bool a) {
		return (_shift ? _b1 : _b0).set_pressed (a);
	}

	void set_active (bool a) {
		/* This button is never directly used
		 * by the libardour side API.
		 */
		assert (0);
	}

	void active_changed (bool s, bool a) {
		if (s != _shift) {
			return;
		}
		_base.tx_midi3 (0x90, _midi_id, a ? 0x7f : 0x00);
	}

	void colour_changed (bool s) {
		if (s != _shift || !_has_color) {
			return;
		}
		uint32_t rgba = (_shift ? _b1 : _b0).color ();
		if (rgba == _rgba) {
			return;
		}
		_rgba = rgba;
		_base.tx_midi3 (0x91, _midi_id, (rgba >> 25) & 0x7f);
		_base.tx_midi3 (0x92, _midi_id, (rgba >> 17) & 0x7f);
		_base.tx_midi3 (0x93, _midi_id, (rgba >>  9) & 0x7f);
	}

	FP8ButtonInterface* button () { return &_b0; }
	FP8ButtonInterface* button_shift () { return &_b1; }

protected:
	FP8Base&     _base;

	virtual void connect_toggle () = 0;

	void shift_changed (bool shift) {
		if (_shift == shift) {
			return;
		}
		(_shift ? _b1 : _b0).set_pressed (false);
		_shift = shift;
		active_changed (_shift, (_shift ? _b1 : _b0).is_active());
		colour_changed (_shift);
	}

private:
	ShadowButton _b0;
	ShadowButton _b1;
	uint8_t      _midi_id; // MIDI-note
	bool         _has_color;
	uint32_t     _rgba;
	bool         _shift;
	PBD::ScopedConnectionList _button_connections;
};

class FP8ShiftSensitiveButton : public FP8DualButton
{
public:
	FP8ShiftSensitiveButton (FP8Base& b, uint8_t id, bool color = false)
		:FP8DualButton (b, id, color)
	{
		connect_toggle ();
	}

protected:
	void connect_toggle ()
	{
		_base.ShiftButtonChange.connect_same_thread (_shift_connection, boost::bind (&FP8ShiftSensitiveButton::shift_changed, this, _1));
	}

private:
	PBD::ScopedConnection _shift_connection;
};

class FP8ARMSensitiveButton : public FP8DualButton
{
public:
	FP8ARMSensitiveButton (FP8Base& b, uint8_t id, bool color = false)
		:FP8DualButton (b, id, color)
	{
		connect_toggle ();
	}

protected:
	void connect_toggle ()
	{
		_base.ARMButtonChange.connect_same_thread (_arm_connection, boost::bind (&FP8ARMSensitiveButton::shift_changed, this, _1));
	}

private:
	PBD::ScopedConnection _arm_connection;
};


// short press: activate in press, deactivate on release,
// long press + hold, activate on press, de-activate directly on release
// e.g. mute/solo  press + hold => changed()
class FP8MomentaryButton : public FP8ButtonBase
{
public:
	FP8MomentaryButton (FP8Base& b, uint8_t id)
		: FP8ButtonBase (b)
		, _midi_id (id)
	{}

	~FP8MomentaryButton () {
		_hold_connection.disconnect ();
	}

	PBD::Signal1<void, bool> StateChange;

	void set_active (bool a)
	{
		if (_active == a && !force_change) {
			return;
		}
		_active = a;
		_base.tx_midi3 (0x90, _midi_id, a ? 0x7f : 0x00);
	}

	void reset ()
	{
		_was_active_on_press = false;
		_hold_connection.disconnect ();
	}

	void ignore_release () { }

	bool midi_event (bool a)
	{
		if (a == _pressed) {
			return false;
		}

		_pressed = a;

		if (a) {
			_was_active_on_press = _active;
		}

		if (a && !_active) {
			_momentaty = false;
			StateChange (true); /* EMIT SIGNAL */
			Glib::RefPtr<Glib::TimeoutSource> hold_timer =
				Glib::TimeoutSource::create (500);
			hold_timer->attach (fp8_loop()->get_context());
			_hold_connection = hold_timer->connect (sigc::mem_fun (*this, &FP8MomentaryButton::hold_timeout));
		} else if (!a && _was_active_on_press) {
			_hold_connection.disconnect ();
			_momentaty = false;
			StateChange (false); /* EMIT SIGNAL */
		} else if (!a && _momentaty) {
			_hold_connection.disconnect ();
			_momentaty = false;
			StateChange (false); /* EMIT SIGNAL */
		}
		return true;
	}

protected:
	void blink (bool onoff)
	{
		if (!blinking ()) {
			_base.tx_midi3 (0x90, _midi_id, _active ? 0x7f : 0x00);
			return;
		}
		_base.tx_midi3 (0x90, _midi_id, onoff ? 0x7f : 0x00);
	}

	uint8_t  _midi_id; // MIDI-note
	bool     _momentaty;
	bool     _was_active_on_press;

private:
	bool hold_timeout ()
	{
		_momentaty = true;
		return false;
	}
	sigc::connection _hold_connection;
};

/* an auto-repeat button.
 * press + hold emits continuous "press" events.
 */
class FP8RepeatButton : public FP8Button
{
public:
	FP8RepeatButton (FP8Base& b, uint8_t id, bool color = false)
		: FP8Button (b, id, color)
		, _skip (0)
	{}

	~FP8RepeatButton ()
	{
		stop_repeat ();
	}

	bool midi_event (bool a)
	{
		bool rv = FP8Button::midi_event (a);
		if (rv && a) {
			start_repeat ();
		}
		return rv;
	}

	void stop_repeat ()
	{
		_press_timeout_connection.disconnect ();
	}

private:
	void start_repeat ()
	{
		stop_repeat ();
		_skip = 5;
		Glib::RefPtr<Glib::TimeoutSource> press_timer =
			Glib::TimeoutSource::create (100);
		press_timer->attach (fp8_loop()->get_context());
		_press_timeout_connection = press_timer->connect (sigc::mem_fun (*this, &FP8RepeatButton::repeat_press));
	}

	bool repeat_press ()
	{
		if (!_pressed) {
			return false;
		}
		if (_skip > 0) {
			--_skip;
			return true;
		}
		pressed ();
		return true;
	}

	int _skip;
	sigc::connection _press_timeout_connection;
};

} /* namespace */
#endif /* _ardour_surfaces_fp8button_h_ */