summaryrefslogtreecommitdiff
path: root/libs/surfaces/wiimote
diff options
context:
space:
mode:
authorPaul Davis <paul@linuxaudiosystems.com>2008-12-12 14:43:24 +0000
committerPaul Davis <paul@linuxaudiosystems.com>2008-12-12 14:43:24 +0000
commitbc89fe0147c04b67141936d109c00dfd4d69cc4b (patch)
tree544ff450c40fe1f43853a8420228a26f27f1eafb /libs/surfaces/wiimote
parent30daaebaa2d90d6b0e8673143ccc3cacd7bd1753 (diff)
most of the 2.X->3.0 commit (up to rev 4299) except for gtk2_ardour/editor_canvas.cc; builds and runs and does a few specific things but expect it to be buggy for a while yet
git-svn-id: svn://localhost/ardour2/branches/3.0@4313 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/surfaces/wiimote')
-rw-r--r--libs/surfaces/wiimote/SConscript60
-rw-r--r--libs/surfaces/wiimote/interface.cc73
-rw-r--r--libs/surfaces/wiimote/wiimote.cc289
-rw-r--r--libs/surfaces/wiimote/wiimote.h69
4 files changed, 491 insertions, 0 deletions
diff --git a/libs/surfaces/wiimote/SConscript b/libs/surfaces/wiimote/SConscript
new file mode 100644
index 0000000000..c530445887
--- /dev/null
+++ b/libs/surfaces/wiimote/SConscript
@@ -0,0 +1,60 @@
+# -*- python -*-
+
+import os
+import os.path
+import glob
+
+Import('env final_prefix install_prefix final_config_prefix libraries i18n')
+
+wiimote = env.Clone()
+
+#
+# this defines the version number of libardour_wiimote
+#
+
+domain = 'ardour_wiimote'
+
+wiimote.Append(DOMAIN = domain, MAJOR = 1, MINOR = 0, MICRO = 0)
+wiimote.Append(CXXFLAGS = "-DPACKAGE=\\\"" + domain + "\\\"")
+wiimote.Append(CXXFLAGS="-DLIBSIGC_DISABLE_DEPRECATED")
+wiimote.Append(PACKAGE = domain)
+wiimote.Append(POTFILE = domain + '.pot')
+
+wiimote_files=Split("""
+wiimote.cc
+interface.cc
+""")
+
+wiimote.Append(CCFLAGS="-D_REENTRANT -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE")
+wiimote.Append(CXXFLAGS="-DDATA_DIR=\\\""+final_prefix+"/share\\\"")
+wiimote.Append(CXXFLAGS="-DCONFIG_DIR=\\\""+final_config_prefix+"\\\"")
+wiimote.Append(CXXFLAGS="-DLOCALEDIR=\\\""+final_prefix+"/share/locale\\\"")
+wiimote.Append(LINKFLAGS="-lcwiid")
+#wiimote.Append(CPPPATH = libraries['jack'].get('CPPPATH', []))
+
+wiimote.Merge ([
+ libraries['ardour'],
+ libraries['ardour_cp'],
+ libraries['sndfile'],
+ libraries['midi++2'],
+ libraries['pbd'],
+ libraries['sigc2'],
+ libraries['usb'],
+ libraries['xml'],
+ libraries['glib2'],
+ libraries['glibmm2']
+ ])
+
+libardour_wiimote = wiimote.SharedLibrary('ardour_wiimote', wiimote_files)
+
+if wiimote['WIIMOTE']:
+ Default(libardour_wiimote)
+ env.Alias('install', env.Install(os.path.join(install_prefix, env['LIBDIR'], 'ardour2', 'surfaces'), libardour_wiimote))
+
+if env['NLS']:
+ i18n (wiimote, wiimote_files, env)
+
+env.Alias('tarball', env.Distribute (env['DISTTREE'],
+ [ 'SConscript' ] +
+ wiimote_files +
+ glob.glob('po/*.po') + glob.glob('*.h')))
diff --git a/libs/surfaces/wiimote/interface.cc b/libs/surfaces/wiimote/interface.cc
new file mode 100644
index 0000000000..0ef301dd6c
--- /dev/null
+++ b/libs/surfaces/wiimote/interface.cc
@@ -0,0 +1,73 @@
+#include <pbd/failed_constructor.h>
+
+#include <control_protocol/control_protocol.h>
+#include "wiimote.h"
+
+#include <ardour/session.h>
+
+using namespace ARDOUR;
+
+static WiimoteControlProtocol *foo;
+
+ControlProtocol*
+new_wiimote_protocol (ControlProtocolDescriptor* descriptor, Session* s)
+{
+ WiimoteControlProtocol* wmcp;
+
+ try {
+ wmcp = new WiimoteControlProtocol (*s);
+ } catch (failed_constructor& err) {
+ return 0;
+ }
+
+ if (wmcp-> set_active (true)) {
+ delete wmcp;
+ return 0;
+ }
+
+ foo = wmcp;
+
+ return wmcp;
+}
+
+void
+wiimote_control_protocol_cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count, union cwiid_mesg mesg[], struct timespec *t)
+{
+ assert(foo != 0);
+
+ foo->wiimote_callback(wiimote,mesg_count,mesg,t);
+}
+
+void
+delete_wiimote_protocol (ControlProtocolDescriptor* descriptor, ControlProtocol* cp)
+{
+ foo = 0;
+ delete cp;
+}
+
+bool
+probe_wiimote_protocol (ControlProtocolDescriptor* descriptor)
+{
+ return WiimoteControlProtocol::probe ();
+}
+
+static ControlProtocolDescriptor wiimote_descriptor = {
+ name : "Wiimote",
+ id : "uri://ardour.org/surfaces/wiimote:0",
+ ptr : 0,
+ module : 0,
+ mandatory : 0,
+ supports_feedback : false,
+ probe : probe_wiimote_protocol,
+ initialize : new_wiimote_protocol,
+ destroy : delete_wiimote_protocol
+};
+
+
+extern "C" {
+ControlProtocolDescriptor*
+protocol_descriptor () {
+ return &wiimote_descriptor;
+}
+}
+
diff --git a/libs/surfaces/wiimote/wiimote.cc b/libs/surfaces/wiimote/wiimote.cc
new file mode 100644
index 0000000000..af3bbc369d
--- /dev/null
+++ b/libs/surfaces/wiimote/wiimote.cc
@@ -0,0 +1,289 @@
+#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"),
+ main_thread_quit (false),
+ restart_discovery (false),
+ callback_thread_registered_for_ardour (false),
+ wiimote_handle (0)
+{
+ main_thread = Glib::Thread::create( sigc::mem_fun(*this, &WiimoteControlProtocol::wiimote_main), true);
+}
+
+WiimoteControlProtocol::~WiimoteControlProtocol()
+{
+ main_thread_quit = true;
+ slot_cond.signal();
+ main_thread->join();
+
+ if (wiimote_handle) {
+ cwiid_close(wiimote_handle);
+ }
+ std::cerr << "Wiimote: closed" << 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 (!callback_thread_registered_for_ardour) {
+ register_thread("Wiimote Control Protocol");
+ callback_thread_registered_for_ardour = true;
+ }
+
+ for (i=0; i < mesg_count; i++)
+ {
+ if (mesg[i].type == CWIID_MESG_ERROR) {
+ std::cerr << "Wiimote: disconnect" << std::endl;
+ restart_discovery = true;
+ slot_cond.signal();
+ return;
+ }
+
+ if (mesg[i].type != CWIID_MESG_BTN) continue;
+
+ // what buttons are pressed down which weren't pressed down last time
+ b = (mesg[i].btn_mesg.buttons ^ button_state) & mesg[i].btn_mesg.buttons;
+
+ button_state = mesg[i].btn_mesg.buttons;
+
+ // if B is pressed down
+ if (button_state & CWIID_BTN_B) {
+ if (b & CWIID_BTN_A) { // B is down and A is pressed
+ access_action("Transport/ToggleRollForgetCapture");
+ }
+
+ if (b & CWIID_BTN_LEFT) {
+ access_action("Editor/playhead-to-previous-region-boundary");
+ }
+ if (b & CWIID_BTN_RIGHT) {
+ access_action("Editor/playhead-to-next-region-boundary");
+ }
+ if (b & CWIID_BTN_UP) {
+ next_marker();
+ }
+ if (b & CWIID_BTN_DOWN) {
+ prev_marker();
+ }
+
+ if (b & CWIID_BTN_HOME) {
+ access_action("Editor/add-location-from-playhead");
+ }
+
+ if (b & CWIID_BTN_MINUS) {
+ access_action("Transport/GotoStart");
+ }
+
+ if (b & CWIID_BTN_PLUS) {
+ access_action("Transport/GotoEnd");
+ }
+
+ continue;
+ }
+
+
+ if (b & CWIID_BTN_A) {
+ access_action("Transport/ToggleRoll");
+ }
+
+ if (b & CWIID_BTN_1) { // 1
+ access_action("Editor/track-record-enable-toggle");
+ }
+ if (b & CWIID_BTN_2) { // 2
+ rec_enable_toggle();
+ }
+
+ // d-pad
+ if (b & CWIID_BTN_LEFT) { // left
+ access_action("Editor/nudge-playhead-backward");
+ }
+ if (b & CWIID_BTN_RIGHT) { // right
+ access_action("Editor/nudge-playhead-forward");
+ }
+ if (b & CWIID_BTN_DOWN) { // down
+ access_action("Editor/select-next-route");
+ }
+ if (b & CWIID_BTN_UP) { // up
+ access_action("Editor/select-prev-route");
+ }
+
+
+ if (b & CWIID_BTN_PLUS) { // +
+ access_action("Editor/temporal-zoom-in");
+ }
+ if (b & CWIID_BTN_MINUS) { // -
+ access_action("Editor/temporal-zoom-out");
+ }
+ if (b & CWIID_BTN_HOME) { // "home"
+ // no op, yet. any suggestions?
+ access_action("Editor/playhead-to-edit");
+ }
+
+ }
+}
+
+void
+WiimoteControlProtocol::update_led_state()
+{
+ ENSURE_WIIMOTE_THREAD(sigc::mem_fun(*this, &WiimoteControlProtocol::update_led_state));
+
+ uint8_t state = 0;
+
+ if (session->transport_rolling()) {
+ state |= CWIID_LED1_ON;
+ }
+
+ if (session->actively_recording()) {
+ state |= CWIID_LED4_ON;
+ }
+
+ cwiid_set_led(wiimote_handle, state);
+}
+
+void
+WiimoteControlProtocol::wiimote_main()
+{
+ bdaddr_t bdaddr;
+ unsigned char rpt_mode = 0;
+ register_thread("Wiimote Discovery and Callback Thread");
+
+wiimote_discovery:
+
+ std::cerr << "Wiimote: discovering, press 1+2" << std::endl;
+
+ while (!wiimote_handle && !main_thread_quit) {
+ bdaddr = *BDADDR_ANY;
+ callback_thread_registered_for_ardour = false;
+ wiimote_handle = cwiid_open(&bdaddr, 0);
+
+ if (!wiimote_handle && !main_thread_quit) {
+ sleep(1);
+ // We don't know whether the issue was a timeout or a configuration
+ // issue
+ }
+ }
+
+ if (main_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);
+ }
+ wiimote_handle = 0;
+
+ 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);
+ wiimote_handle = 0;
+ 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);
+ wiimote_handle = 0;
+ 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);
+ wiimote_handle = 0;
+ return;
+ }
+
+ rpt_mode |= CWIID_RPT_BTN;
+ cwiid_enable(wiimote_handle, CWIID_FLAG_MESG_IFC);
+ cwiid_set_rpt_mode(wiimote_handle, rpt_mode);
+
+ transport_state_conn = session->TransportStateChange.connect(sigc::mem_fun(*this, &WiimoteControlProtocol::update_led_state));
+ record_state_conn = session->RecordStateChanged.connect(sigc::mem_fun(*this, &WiimoteControlProtocol::update_led_state));
+
+ std::cerr << "Wiimote: initialization done, waiting for callbacks / quit" << std::endl;
+
+ while (!main_thread_quit) {
+ slot_mutex.lock();
+ while (slot_list.empty() && !main_thread_quit && !restart_discovery)
+ slot_cond.wait(slot_mutex);
+
+ if (main_thread_quit) {
+ slot_mutex.unlock();
+ break;
+ }
+
+ if (restart_discovery) {
+ std::cerr << "Wiimote: closing wiimote and restarting discovery" << std::endl;
+ if (wiimote_handle) {
+ cwiid_close(wiimote_handle);
+ wiimote_handle = 0;
+ }
+ slot_mutex.unlock();
+ restart_discovery = false;
+ goto wiimote_discovery;
+ }
+
+ sigc::slot<void> call_me = *slot_list.begin();
+ slot_list.pop_front();
+ slot_mutex.unlock();
+
+ call_me();
+ }
+
+
+ std::cerr << "Wiimote: main thread 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;
+}
diff --git a/libs/surfaces/wiimote/wiimote.h b/libs/surfaces/wiimote/wiimote.h
new file mode 100644
index 0000000000..a6680edd2e
--- /dev/null
+++ b/libs/surfaces/wiimote/wiimote.h
@@ -0,0 +1,69 @@
+#ifndef ardour_wiimote_control_protocol_h
+#define ardour_wiimote_control_protocol_h
+
+#include <ardour/types.h>
+#include <control_protocol/control_protocol.h>
+
+#include <glibmm/thread.h>
+
+#include <pbd/abstract_ui.h>
+
+#include <cwiid.h>
+
+
+namespace ARDOUR {
+ class Session;
+}
+
+#define ENSURE_WIIMOTE_THREAD(slot) \
+ if (Glib::Thread::self() != main_thread) {\
+ slot_mutex.lock();\
+ slot_list.push_back(slot);\
+ slot_cond.signal();\
+ slot_mutex.unlock();\
+ return;\
+ }
+
+
+class WiimoteControlProtocol : public ARDOUR::ControlProtocol {
+ public:
+ WiimoteControlProtocol (ARDOUR::Session &);
+ virtual ~WiimoteControlProtocol ();
+
+ static bool probe();
+
+ int set_active (bool yn);
+ XMLNode& get_state();
+ int set_state(const XMLNode&);
+
+ void wiimote_callback(cwiid_wiimote_t *, int, union cwiid_mesg [],
+ struct timespec *);
+
+ private:
+
+ void wiimote_main();
+ volatile bool main_thread_quit;
+ volatile bool restart_discovery;
+
+ Glib::Thread *main_thread;
+
+ void update_led_state();
+
+ bool callback_thread_registered_for_ardour;
+
+ static uint16_t button_state;
+
+ cwiid_wiimote_t *wiimote_handle;
+
+ Glib::Cond slot_cond;
+ Glib::Mutex slot_mutex;
+
+ std::list< sigc::slot<void> > slot_list;
+
+ sigc::connection transport_state_conn;
+ sigc::connection record_state_conn;
+};
+
+
+#endif /* ardour_wiimote_control_protocol_h */
+