summaryrefslogtreecommitdiff
path: root/libs/surfaces/wiimote/wiimote.cc
blob: b3f891172a40ff8285340b995806ae7cbb7edab3 (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
#include "wiimote.h"

#include <iostream>
#include <sigc++/bind.h>

#include <pbd/xml++.h>
#include <ardour/session.h>

#include "i18n.h"


using namespace ARDOUR;
using namespace PBD;

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

uint16_t WiimoteControlProtocol::button_state = 0;

WiimoteControlProtocol::WiimoteControlProtocol ( Session & session) 
	: ControlProtocol ( session, "Wiimote"),
	  thread_quit (false),
	  thread_registered_for_ardour (false)
{
	std::cerr << "WiimoteControlProtocol()" << std::endl;
	thread = Glib::Thread::create( sigc::mem_fun(*this, &WiimoteControlProtocol::main_thread), true);
}

WiimoteControlProtocol::~WiimoteControlProtocol()
{
	thread_quit = true;
	thread->join();
	// TODO: you con't delete the thread, but is join() still enough?
	std::cerr << "~WiimoteControlProtocol()" << std::endl;
}


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

void
WiimoteControlProtocol::wiimote_callback(cwiid_wiimote_t *wiimote, int mesg_count, union cwiid_mesg mesg[], struct timespec *t)
{
	int i;
	uint16_t b;

	if (!thread_registered_for_ardour) {
		register_thread("Wiimote Control Protocol");
		thread_registered_for_ardour = true;
	}

        for (i=0; i < mesg_count; i++)
	{
		if (mesg[i].type != CWIID_MESG_BTN) continue;

		b = (mesg[i].btn_mesg.buttons ^ button_state) & mesg[i].btn_mesg.buttons;

		button_state = mesg[i].btn_mesg.buttons;

		if (b & CWIID_BTN_2) {
			rec_enable_toggle();
			//std::cerr << "2" << std::endl;
		}
		if (b & CWIID_BTN_1) {
			access_action("Editor/track-record-enable-toggle");			
			//std::cerr << "1" << std::endl;
		}
		if (b & CWIID_BTN_B) {
			// just a B doesn't do anything
			//std::cerr << "B" << std::endl;
		}
		if (b & CWIID_BTN_A && button_state & CWIID_BTN_B) {
			// B pressed down and then A
			access_action("Transport/ToggleRollForgetCapture");
			//std::cerr << "B+A" << std::endl;
		}

		if (b & CWIID_BTN_A && !(button_state & CWIID_BTN_B)) {
			// Just A pressed
			access_action("Transport/ToggleRoll");
			//std::cerr << "A" << std::endl;
		}
		if (b & CWIID_BTN_MINUS) {
			access_action("Editor/temporal-zoom-out");
			//std::cerr << "-" << std::endl;
		}
		if (b & CWIID_BTN_HOME) {
			//std::cerr << "home" << std::endl;
		}
		if (b & CWIID_BTN_LEFT) {
			access_action("Editor/nudge-playhead-backward");
			//std::cerr << "<" << std::endl;
		}
		if (b & CWIID_BTN_RIGHT) {
			access_action("Editor/nudge-playhead-forward");
			//std::cerr << ">" << std::endl;
		}
		if (b & CWIID_BTN_DOWN) {
			access_action("Editor/select-next-route");
			//std::cerr << "_" << std::endl;
		}
		if (b & CWIID_BTN_UP) {
			access_action("Editor/select-prev-route");
			//std::cerr << "^" << std::endl;
		}
		if (b & CWIID_BTN_PLUS) {
			access_action("Editor/temporal-zoom-in");
			//std::cerr << "+" << std::endl;
		}

	}
}

void
WiimoteControlProtocol::main_thread()
{
	cwiid_wiimote_t *wiimote_handle = 0;
	bdaddr_t bdaddr;
	unsigned char rpt_mode = 0;

	std::cerr << "wiimote: discovering, press 1+2" << std::endl;

 	while (!wiimote_handle && !thread_quit) {
		bdaddr = *BDADDR_ANY;
		wiimote_handle = cwiid_open(&bdaddr, 0);

		if (!wiimote_handle) {
			sleep(1); 
			// We don't know whether the issue was a timeout or a configuration 
			// issue
		}
	}

	if (thread_quit) {
		// The corner case where the wiimote is bound at the same time as
		// the control protocol is destroyed
		if (wiimote_handle) {
			cwiid_close(wiimote_handle);
		}
		std::cerr << "Wiimote Control Protocol stopped before connected to a wiimote" << std::endl;
		return;
	}

	std::cerr << "Wiimote: connected" << std::endl;
	WiimoteControlProtocol::button_state = 0;

	if (cwiid_enable(wiimote_handle, CWIID_FLAG_REPEAT_BTN)) {
		std::cerr << "cwiid_enable(), error" << std::endl;
		cwiid_close(wiimote_handle);
		return;
	}
	if (cwiid_set_mesg_callback(wiimote_handle, wiimote_control_protocol_cwiid_callback)) {
		std::cerr << "cwiid_set_mesg_callback(), couldn't connect callback" << std::endl;
		cwiid_close(wiimote_handle);
		return;
	} 
	if (cwiid_command(wiimote_handle, CWIID_CMD_RPT_MODE, CWIID_RPT_BTN)) {
		std::cerr << "cwiid_command(), RPT_MODE error" << std::endl;
		cwiid_close(wiimote_handle);
		return;
	}

	rpt_mode |= CWIID_RPT_BTN;
	cwiid_enable(wiimote_handle, CWIID_FLAG_MESG_IFC);
	cwiid_set_rpt_mode(wiimote_handle, rpt_mode);
	
	while (!thread_quit) {
		sleep(1);
	}	

	cwiid_close(wiimote_handle);
	std::cerr << "Wiimote: stopped" << std::endl;
}


int
WiimoteControlProtocol::set_active (bool yn)
{
	// Let's not care about this just yet
	return 0;

}

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

	return *node;
}

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