2008-06-02 17:41:35 -04:00
|
|
|
/*
|
|
|
|
powermate.cc
|
|
|
|
Ben Loftis
|
|
|
|
Created: 03/26/07 20:07:56
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <linux/input.h>
|
2008-09-10 11:03:30 -04:00
|
|
|
#include <cstring>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
2008-06-02 17:41:35 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <glibmm.h>
|
|
|
|
|
2009-12-04 14:24:09 -05:00
|
|
|
#include "pbd/pthread_utils.h"
|
|
|
|
#include "pbd/xml++.h"
|
|
|
|
#include "pbd/error.h"
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
#include "powermate.h"
|
2009-12-04 14:24:09 -05:00
|
|
|
#include "i18n.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
using namespace ARDOUR;
|
|
|
|
using namespace std;
|
|
|
|
using namespace sigc;
|
|
|
|
using namespace PBD;
|
|
|
|
|
|
|
|
#define NUM_VALID_PREFIXES 2
|
|
|
|
|
|
|
|
static const char *valid_prefix[NUM_VALID_PREFIXES] = {
|
|
|
|
"Griffin PowerMate",
|
|
|
|
"Griffin SoundKnob"
|
|
|
|
};
|
|
|
|
|
|
|
|
#define NUM_EVENT_DEVICES 16
|
|
|
|
|
2009-08-04 18:01:28 -04:00
|
|
|
int open_powermate (const char *dev, int mode)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
if (!Glib::file_test (dev, Glib::FILE_TEST_EXISTS)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-04 18:01:28 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
int fd = open(dev, mode);
|
|
|
|
int i;
|
|
|
|
char name[255];
|
|
|
|
|
2009-08-04 18:01:28 -04:00
|
|
|
if (fd < 0) {
|
2008-06-02 17:41:35 -04:00
|
|
|
if (errno != EACCES) {
|
|
|
|
error << string_compose ("Unable to open \"%1\": %2", dev, strerror(errno)) << endmsg;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-08-04 18:01:28 -04:00
|
|
|
/* placate valgrind */
|
|
|
|
name[0] = '\0';
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-08-04 18:01:28 -04:00
|
|
|
if (ioctl (fd, EVIOCGNAME (sizeof(name)), name) < 0) {
|
|
|
|
error << string_compose ("\"%1\": EVIOCGNAME failed: %2", dev, strerror(errno)) << endmsg;
|
|
|
|
close (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-08-04 18:01:28 -04:00
|
|
|
// it's the correct device if the prefix matches what we expect it to be:
|
|
|
|
for (i = 0; i < NUM_VALID_PREFIXES; ++i) {
|
|
|
|
if (!strncasecmp (name, valid_prefix[i], strlen (valid_prefix[i]))) {
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
return -1;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int find_powermate(int mode)
|
|
|
|
{
|
2009-12-04 14:24:09 -05:00
|
|
|
char devname[256];
|
|
|
|
int i, r;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_EVENT_DEVICES; i++) {
|
|
|
|
sprintf (devname, "/dev/input/event%d", i);
|
|
|
|
r = open_powermate (devname, mode);
|
|
|
|
if (r >= 0) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PowermateControlProtocol::PowermateControlProtocol (Session& s)
|
2009-12-21 13:23:07 -05:00
|
|
|
: ControlProtocol (s, "powermate", 0 /* XXX need an event loop here */)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PowermateControlProtocol::~PowermateControlProtocol ()
|
|
|
|
{
|
|
|
|
set_active (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PowermateControlProtocol::probe ()
|
|
|
|
{
|
|
|
|
int port = find_powermate( O_RDONLY );
|
|
|
|
|
|
|
|
if (port < 0) {
|
2009-09-02 21:06:45 -04:00
|
|
|
if (errno == ENOENT) {
|
2009-09-02 21:08:39 -04:00
|
|
|
info << "Powermate device not found; perhaps you have no powermate connected" << endmsg;
|
2009-09-02 21:06:45 -04:00
|
|
|
} else {
|
|
|
|
printf ("powermate: Opening of powermate failed - %s\n", strerror(errno));
|
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
close (port);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PowermateControlProtocol::set_active (bool inActivate)
|
|
|
|
{
|
|
|
|
if (inActivate != _active) {
|
|
|
|
|
|
|
|
if (inActivate) {
|
|
|
|
|
|
|
|
mPort = find_powermate(O_RDONLY);
|
|
|
|
|
|
|
|
if ( mPort < 0 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
if (pthread_create_and_store ("Powermate", &mThread, SerialThreadEntry, this) == 0) {
|
2008-06-02 17:41:35 -04:00
|
|
|
_active = true;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Powermate Control Protocol activated\n");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
pthread_cancel (mThread);
|
|
|
|
close (mPort);
|
|
|
|
_active = false;
|
|
|
|
printf("Powermate Control Protocol deactivated\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode&
|
|
|
|
PowermateControlProtocol::get_state ()
|
|
|
|
{
|
|
|
|
XMLNode* node = new XMLNode (X_("Protocol"));
|
|
|
|
node->add_property (X_("name"), _name);
|
|
|
|
return *node;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-10-14 20:57:55 -04:00
|
|
|
PowermateControlProtocol::set_state (const XMLNode& /*node*/, int /*version*/)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void*
|
|
|
|
PowermateControlProtocol::SerialThreadEntry (void* arg)
|
|
|
|
{
|
2009-12-04 14:24:09 -05:00
|
|
|
static_cast<PowermateControlProtocol*>(arg)->register_thread ("Powermate");
|
2008-06-02 17:41:35 -04:00
|
|
|
return static_cast<PowermateControlProtocol*>(arg)->SerialThread ();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 32
|
|
|
|
|
|
|
|
bool held = false;
|
|
|
|
bool skippingMarkers = false;
|
|
|
|
|
|
|
|
void
|
|
|
|
PowermateControlProtocol::ProcessEvent(struct input_event *ev)
|
|
|
|
{
|
|
|
|
#ifdef VERBOSE
|
|
|
|
fprintf(stderr, "type=0x%04x, code=0x%04x, value=%d\n",
|
|
|
|
ev->type, ev->code, (int)ev->value);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch(ev->type){
|
|
|
|
case EV_MSC:
|
|
|
|
printf("The LED pulse settings were changed; code=0x%04x, value=0x%08x\n", ev->code, ev->value);
|
|
|
|
break;
|
|
|
|
case EV_REL:
|
|
|
|
if(ev->code != REL_DIAL)
|
|
|
|
fprintf(stderr, "Warning: unexpected rotation event; ev->code = 0x%04x\n", ev->code);
|
|
|
|
else{
|
|
|
|
if (held) {
|
|
|
|
//click and hold to skip forward and back by markers
|
|
|
|
skippingMarkers = true;;
|
|
|
|
if (ev->value > 0)
|
|
|
|
next_marker();
|
|
|
|
else
|
|
|
|
prev_marker();
|
|
|
|
} else {
|
|
|
|
//scale the range so that we can go from +/-8x within 180 degrees, with less precision at the higher speeds
|
|
|
|
float speed = get_transport_speed();
|
|
|
|
speed += (float)ev->value * 0.05;
|
|
|
|
if (speed > 1.5 || speed < -1.5 )
|
|
|
|
speed += ev->value;
|
|
|
|
set_transport_speed( speed );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EV_KEY:
|
|
|
|
if(ev->code != BTN_0)
|
|
|
|
fprintf(stderr, "Warning: unexpected key event; ev->code = 0x%04x\n", ev->code);
|
|
|
|
else
|
|
|
|
if (ev->value)
|
|
|
|
held = true;
|
|
|
|
else {
|
|
|
|
held = false;
|
|
|
|
if (skippingMarkers) {
|
|
|
|
skippingMarkers = false;
|
|
|
|
} else {
|
|
|
|
if (get_transport_speed() == 0.0) {
|
|
|
|
set_transport_speed(1.0);
|
|
|
|
} else {
|
|
|
|
set_transport_speed(0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
PowermateControlProtocol::SerialThread ()
|
|
|
|
{
|
|
|
|
struct input_event ibuffer[BUFFER_SIZE];
|
|
|
|
int r, events, i;
|
|
|
|
|
|
|
|
while(1){
|
|
|
|
r = read(mPort, ibuffer, sizeof(struct input_event) * BUFFER_SIZE);
|
|
|
|
if( r > 0 ){
|
|
|
|
events = r / sizeof(struct input_event);
|
|
|
|
for(i=0; i<events; i++)
|
|
|
|
ProcessEvent(&ibuffer[i]);
|
|
|
|
}else{
|
|
|
|
fprintf(stderr, "read() failed: %s\n", strerror(errno));
|
|
|
|
return (void*) 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (void*) 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|