summaryrefslogtreecommitdiff
path: root/libs/surfaces/wiimote/wiimote.cc
blob: 68f2125dcb5a6824768b26cf868eccba0a03cf1c (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
/*
    Copyright (C) 2009-2013 Paul Davis
    Authors: Sampo Savolainen, Jannis Pohlmann

    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., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <iostream>

#include "pbd/compose.h"
#include "pbd/error.h"
#include "ardour/debug.h"
#include "ardour/session.h"
#include "i18n.h"

#include "wiimote.h"

using namespace ARDOUR;
using namespace PBD;
using namespace std;

#include "pbd/abstract_ui.cc" // instantiate template

void wiimote_control_protocol_mesg_callback (cwiid_wiimote_t *wiimote, int mesg_count, union cwiid_mesg mesg[], timespec *t);

WiimoteControlProtocol::WiimoteControlProtocol (Session& session)
	: ControlProtocol (session, X_("Wiimote"))
	, AbstractUI<WiimoteControlUIRequest> ("wiimote")
	, wiimote (0)
	, idle_source (0)
	, button_state (0)
	, callback_thread_registered (false)
{
}

WiimoteControlProtocol::~WiimoteControlProtocol ()
{
	stop ();
}

bool
WiimoteControlProtocol::probe ()
{
	return true;
}

int
WiimoteControlProtocol::set_active (bool yn)
{
	int result;

	DEBUG_TRACE (DEBUG::WiimoteControl, string_compose ("WiimoteControlProtocol::set_active init with yn: '%1'\n", yn));

	/* do nothing if the active state is not changing */
	if (yn == _active) {
		return 0;
	}

	if (yn) {
		/* activate Wiimote control surface */
		result = start ();
	} else {
		/* deactivate Wiimote control surface */
		result = stop ();
	}

	/* remember new active state */
	_active = yn;

	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::set_active done\n");

	return result;
}

XMLNode&
WiimoteControlProtocol::get_state ()
{
	XMLNode *node = new XMLNode ("Protocol");
	node->add_property (X_("name"), ARDOUR::ControlProtocol::_name);
	node->add_property (X_("feedback"), "0");
	return *node;
}

int
WiimoteControlProtocol::set_state (const XMLNode&, int)
{
	return 0;
}

void
WiimoteControlProtocol::do_request (WiimoteControlUIRequest* req)
{
	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::do_request init\n");

	if (req->type == CallSlot) {
		call_slot (MISSING_INVALIDATOR, req->the_slot);
	} else if (req->type == Quit) {
		stop ();
	}

	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::do_request done\n");
}

int
WiimoteControlProtocol::start ()
{
	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::start init\n");

	// update LEDs whenever the transport or recording state changes
	session->TransportStateChange.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&WiimoteControlProtocol::update_led_state, this), this);
	session->RecordStateChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&WiimoteControlProtocol::update_led_state, this), this);

	// start the Wiimote control UI; it will run in its own thread context
	BaseUI::run ();

	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::start done\n");

	return 0;
}

int
WiimoteControlProtocol::stop ()
{
	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::stop init\n");

	// stop wiimote discovery, just in case
	stop_wiimote_discovery ();

	// close and reset the wiimote handle
	if (wiimote) {
		cwiid_close (wiimote);
		wiimote = 0;
		callback_thread_registered = false;
	}

	// stop the Wiimote control UI
	BaseUI::quit ();

	// no longer update the LEDs
	session_connections.drop_connections ();

	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::stop done\n");

	return 0;
}

void
WiimoteControlProtocol::thread_init ()
{
	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::thread_init init\n");

	pthread_set_name (X_("wiimote"));

	// allow to make requests to the GUI and RT thread(s)
	PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self (), X_("wiimote"), 2048);
	BasicUI::register_thread ("wiimote");

	// connect a Wiimote
	start_wiimote_discovery ();

	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::thread_init done\n");
}

void
WiimoteControlProtocol::start_wiimote_discovery ()
{
	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::start_wiimote_discovery init\n");

	// connect to the Wiimote using an idle source
	Glib::RefPtr<Glib::IdleSource> source = Glib::IdleSource::create ();
	source->connect (sigc::mem_fun (*this, &WiimoteControlProtocol::connect_idle));
	source->attach (_main_loop->get_context ());

	// grab a reference on the underlying idle source to keep it around
	idle_source = source->gobj ();
	g_source_ref (idle_source);

	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::start_wiimote_discovery done\n");
}

void
WiimoteControlProtocol::stop_wiimote_discovery ()
{
	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::stop_wiimote_discovery init\n");

	if (idle_source) {
		g_source_unref (idle_source);
		idle_source = 0;
	}

	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::stop_wiimote_discovery done\n");
}

bool
WiimoteControlProtocol::connect_idle ()
{
	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::connect_idle init\n");

	bool retry = true;

	if (connect_wiimote ()) {
		stop_wiimote_discovery ();
		retry = false;
	}

	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::connect_idle done\n");

	return retry;
}

bool
WiimoteControlProtocol::connect_wiimote ()
{
	// abort the discovery and do nothing else if we already have a Wiimote
	if (wiimote) {
		return true;
	}

	bool success = true;

	// if we don't have a Wiimote yet, try to discover it; if that
	// fails, wait for a short period of time and try again
	if (!wiimote) {
		cerr << "Wiimote: Not discovered yet, press 1+2 to connect" << endl;

		bdaddr_t bdaddr = {{ 0, 0, 0, 0, 0, 0 }};
		wiimote = cwiid_open (&bdaddr, 0);
		callback_thread_registered = false;
		if (!wiimote) {
			success = false;
		} else {
			// a Wiimote was discovered
			cerr << "Wiimote: Connected successfully" << endl;

			// attach the WiimoteControlProtocol object to the Wiimote handle
			if (cwiid_set_data (wiimote, this)) {
				cerr << "Wiimote: Failed to attach control protocol" << endl;
				success = false;
			}

			// clear the last button state to start processing events cleanly
			button_state = 0;
		}
	}

	// enable message based communication with the Wiimote
	if (success && cwiid_enable (wiimote, CWIID_FLAG_MESG_IFC)) {
		cerr << "Wiimote: Failed to enable message based communication" << endl;
		success = false;
	}

	// enable button events to be received from the Wiimote
	if (success && cwiid_command (wiimote, CWIID_CMD_RPT_MODE, CWIID_RPT_BTN)) {
		cerr << "Wiimote: Failed to enable button events" << endl;
		success = false;
	}

	// receive an event for every single button pressed, not just when
	// a different button was pressed than before
	if (success && cwiid_enable (wiimote, CWIID_FLAG_REPEAT_BTN)) {
		cerr << "Wiimote: Failed to enable repeated button events" << endl;
		success = false;
	}

	// be notified of new input events
	if (success && cwiid_set_mesg_callback (wiimote, wiimote_control_protocol_mesg_callback)) {
	}

	// reset Wiimote handle if the configuration failed
	if (!success && wiimote) {
		cwiid_close (wiimote);
		wiimote = 0;
		callback_thread_registered = false;
	}

	return success;
}

void
WiimoteControlProtocol::update_led_state ()
{
	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state init\n");

	uint8_t state = 0;

	// do nothing if we do not have a Wiimote
	if (!wiimote) {
		DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state no wiimote connected\n");
		return;
	}

	// enable LED1 if Ardour is playing
	if (session->transport_rolling ()) {
		DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state playing, activate LED1\n");
		state |= CWIID_LED1_ON;
	}

	// enable LED4 if Ardour is recording
	if (session->actively_recording ()) {
		DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state recording, activate LED4\n");
		state |= CWIID_LED4_ON;
	}

	// apply the LED state
	cwiid_set_led (wiimote, state);

	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state done\n");
}

void
WiimoteControlProtocol::wiimote_callback (int mesg_count, union cwiid_mesg mesg[])
{
	// register the cwiid callback thread if that hasn't happened yet
	if (!callback_thread_registered) {
		BasicUI::register_thread ("wiimote callback");
		callback_thread_registered = true;
	}

	for (int i = 0; i < mesg_count; i++) {
		// restart Wiimote discovery when receiving errors
		if (mesg[i].type == CWIID_MESG_ERROR) {
			cerr << "Wiimote: disconnected" << endl;
			cwiid_close (wiimote);
			wiimote = 0;
			callback_thread_registered = false;
			start_wiimote_discovery ();
			return;
		}

		// skip non-button events
		if (mesg[i].type != CWIID_MESG_BTN) {
			continue;
		}

		// drop buttons from the event that were already pressed before
		uint16_t b = mesg[i].btn_mesg.buttons & ~button_state;

		// remember new button state
		button_state = mesg[i].btn_mesg.buttons;

		if (button_state & CWIID_BTN_B) {
			// B + A = abort recording and jump back
			if (b & CWIID_BTN_A) {
				access_action ("Transport/ToggleRollForgetCapture");
			}

			// B + left = move playhead to previous region boundary
			if (b & CWIID_BTN_LEFT) {
				access_action ("Editor/playhead-to-previous-region-boundary");
			}

			// B + right = move playhead to next region boundary
			if (b & CWIID_BTN_RIGHT) {
				access_action ("Editor/playhead-to-next-region-boundary");
			}

			// B + up = move playhead to next marker
			if (b & CWIID_BTN_UP) {
				next_marker ();
			}

			// B + down = move playhead to prev marker
			if (b & CWIID_BTN_DOWN) {
				prev_marker ();
			}

			// B + Home = add marker at playhead
			if (b & CWIID_BTN_HOME) {
				access_action ("Editor/add-location-from-playhead");
			}

			// B + minus = move playhead to the start
			if (b & CWIID_BTN_MINUS) {
				access_action ("Transport/GotoStart");
			}

			// B + plus = move playhead to the end
			if (b & CWIID_BTN_PLUS) {
				access_action ("Transport/GotoEnd");
			}
		} else {
			// A = toggle playback
			if (b & CWIID_BTN_A) {
				access_action ("Transport/ToggleRoll");
			}

			// 1 = toggle recording on the current track
			if (b & CWIID_BTN_1) {
				access_action ("Editor/track-record-enable-toggle");
			}

			// 2 = enable recording in general
			if (b & CWIID_BTN_2) {
				rec_enable_toggle ();
			}

			// left = move playhead back a bit
			if (b & CWIID_BTN_LEFT) {
				access_action ("Editor/nudge-playhead-backward");
			}

			// right = move playhead forward a bit
			if (b & CWIID_BTN_RIGHT) {
				access_action ("Editor/nudge-playhead-forward");
			}

			// up = select previous track
			if (b & CWIID_BTN_UP) {
				access_action ("Editor/select-prev-route");
			}

			// down = select next track
			if (b & CWIID_BTN_DOWN) {
				access_action ("Editor/select-next-route");
			}

			// + = zoom in
			if (b & CWIID_BTN_PLUS) {
				access_action ("Editor/temporal-zoom-in");
			}

			// - = zoom out
			if (b & CWIID_BTN_MINUS) {
				access_action ("Editor/temporal-zoom-out");
			}

			// home = no-op
			if (b & CWIID_BTN_HOME) {
				access_action ("Editor/playhead-to-edit");
			}
		}
	}
}

void
wiimote_control_protocol_mesg_callback (cwiid_wiimote_t *wiimote, int mesg_count, union cwiid_mesg mesg[], timespec *)
{
	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::mesg_callback init\n");

	WiimoteControlProtocol *protocol = (WiimoteControlProtocol *)cwiid_get_data (wiimote);

	if (protocol) {
		protocol->wiimote_callback (mesg_count, mesg);
	}

	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::mesg_callback done\n");
}