globals--, fmt string fix, foreeach++, better names++, close fds, tray icon activates/closes
This commit is contained in:
166
src/joypad.cpp
166
src/joypad.cpp
@ -1,21 +1,24 @@
|
||||
#include "unistd.h"
|
||||
#include "joypad.h"
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include<stdint.h>
|
||||
#include <stdint.h>
|
||||
#include <poll.h>
|
||||
#include <QApplication>
|
||||
|
||||
JoyPad::JoyPad( int i, int dev, QObject *parent ) : QObject(parent) {
|
||||
#include "joypad.h"
|
||||
|
||||
JoyPad::JoyPad( int i, int dev, QObject *parent )
|
||||
: QObject(parent), joydev(-1), axisCount(0), buttonCount(0), jpw(0), readNotifier(0), errorNotifier(0) {
|
||||
debug_mesg("Constructing the joypad device with index %d and fd %d\n", i, dev);
|
||||
//remember the index,
|
||||
index = i;
|
||||
|
||||
//load data from the joystick device, if available.
|
||||
joydevFileHandle = NULL;
|
||||
if(dev >= 0) {
|
||||
if (dev >= 0) {
|
||||
debug_mesg("Valid file handle, setting up handlers and reading axis configs...\n");
|
||||
resetToDev(dev);
|
||||
open(dev);
|
||||
debug_mesg("done resetting and setting up device index %d\n", i);
|
||||
char id[256];
|
||||
memset(id, 0, sizeof(id));
|
||||
@ -28,70 +31,96 @@ JoyPad::JoyPad( int i, int dev, QObject *parent ) : QObject(parent) {
|
||||
} else {
|
||||
debug_mesg("This joypad does not have a valid file handle, not setting up event listeners\n");
|
||||
}
|
||||
//there is no JoyPadWidget yet.
|
||||
jpw = NULL;
|
||||
debug_mesg("Done constructing the joypad device %d\n", i);
|
||||
}
|
||||
|
||||
void JoyPad::resetToDev(int dev ) {
|
||||
JoyPad::~JoyPad() {
|
||||
close();
|
||||
}
|
||||
|
||||
void JoyPad::close() {
|
||||
if (readNotifier) {
|
||||
disconnect(readNotifier, 0, 0, 0);
|
||||
|
||||
readNotifier->blockSignals(true);
|
||||
readNotifier->setEnabled(false);
|
||||
|
||||
delete readNotifier;
|
||||
readNotifier = 0;
|
||||
}
|
||||
if (errorNotifier) {
|
||||
disconnect(errorNotifier, 0, 0, 0);
|
||||
|
||||
errorNotifier->blockSignals(true);
|
||||
errorNotifier->setEnabled(false);
|
||||
|
||||
delete errorNotifier;
|
||||
errorNotifier = 0;
|
||||
}
|
||||
if (joydev >= 0) {
|
||||
if (::close(joydev) != 0) {
|
||||
debug_mesg("close(js%d %d): %s\n", index, joydev, strerror(errno));
|
||||
}
|
||||
joydev = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void JoyPad::open(int dev) {
|
||||
debug_mesg("resetting to dev\n");
|
||||
//remember the device file descriptor
|
||||
close();
|
||||
joydev = dev;
|
||||
|
||||
//read in the number of axes / buttons
|
||||
axes = 0;
|
||||
ioctl (joydev, JSIOCGAXES, &axes);
|
||||
buttons = 0;
|
||||
ioctl (joydev, JSIOCGBUTTONS, &buttons);
|
||||
axisCount = 0;
|
||||
ioctl (joydev, JSIOCGAXES, &axisCount);
|
||||
buttonCount = 0;
|
||||
ioctl (joydev, JSIOCGBUTTONS, &buttonCount);
|
||||
//make sure that we have the axes we need.
|
||||
//if one that we need doesn't yet exist, add it in.
|
||||
//Note: if the current layout has a key assigned to an axis that did not
|
||||
//have a real joystick axis mapped to it, and this function suddenly brings
|
||||
//that axis into use, the key assignment will not be lost because the axis
|
||||
//will already exist and no new axis will be created.
|
||||
for (int i = 0; i < axes; i++) {
|
||||
if (Axes[i] == 0) Axes.insert(i, new Axis( i, this ));
|
||||
for (int i = 0; i < axisCount; i++) {
|
||||
if (axes[i] == 0) axes.insert(i, new Axis( i, this ));
|
||||
}
|
||||
for (int i = 0; i < buttons; i++) {
|
||||
if (Buttons[i] == 0) Buttons.insert(i, new Button( i, this ));
|
||||
for (int i = 0; i < buttonCount; i++) {
|
||||
if (buttons[i] == 0) buttons.insert(i, new Button( i, this ));
|
||||
}
|
||||
struct pollfd read_struct;
|
||||
read_struct.fd = joydev;
|
||||
read_struct.events = POLLIN;
|
||||
char buf[10];
|
||||
while(poll(&read_struct, 1, 5)!=0) {
|
||||
while (poll(&read_struct, 1, 5) != 0) {
|
||||
debug_mesg("reading junk data\n");
|
||||
if (read(joydev, buf, 10) <= 0) break;
|
||||
}
|
||||
setupJoyDeviceListener(dev);
|
||||
debug_mesg("done resetting to dev\n");
|
||||
}
|
||||
|
||||
void JoyPad::setupJoyDeviceListener(int dev) {
|
||||
debug_mesg("Setting up joyDeviceListeners\n");
|
||||
joydevFileHandle = new QSocketNotifier(dev, QSocketNotifier::Read, this);
|
||||
connect(joydevFileHandle, SIGNAL(activated(int)), this, SLOT(handleJoyEvents(int)));
|
||||
joydevFileException = new QSocketNotifier(dev, QSocketNotifier::Exception, this);
|
||||
connect(joydevFileException, SIGNAL(activated(int)), this, SLOT(errorRead(int)));
|
||||
readNotifier = new QSocketNotifier(joydev, QSocketNotifier::Read, this);
|
||||
connect(readNotifier, SIGNAL(activated(int)), this, SLOT(handleJoyEvents()));
|
||||
errorNotifier = new QSocketNotifier(joydev, QSocketNotifier::Exception, this);
|
||||
connect(errorNotifier, SIGNAL(activated(int)), this, SLOT(handleJoyEvents()));
|
||||
debug_mesg("Done setting up joyDeviceListeners\n");
|
||||
debug_mesg("done resetting to dev\n");
|
||||
}
|
||||
|
||||
void JoyPad::toDefault() {
|
||||
//to reset the whole, reset all the parts.
|
||||
foreach (Axis *axis, Axes) {
|
||||
foreach (Axis *axis, axes) {
|
||||
axis->toDefault();
|
||||
}
|
||||
foreach (Button *button, Buttons) {
|
||||
foreach (Button *button, buttons) {
|
||||
button->toDefault();
|
||||
}
|
||||
}
|
||||
|
||||
bool JoyPad::isDefault() {
|
||||
//if any of the parts are not at default, then the whole isn't either.
|
||||
foreach (Axis *axis, Axes) {
|
||||
foreach (Axis *axis, axes) {
|
||||
if (!axis->isDefault()) return false;
|
||||
}
|
||||
foreach (Button *button, Buttons) {
|
||||
foreach (Button *button, buttons) {
|
||||
if (!button->isDefault()) return false;
|
||||
}
|
||||
return true;
|
||||
@ -115,10 +144,10 @@ bool JoyPad::readConfig( QTextStream &stream ) {
|
||||
error("Layout file error", QString("Expected ':', found '%1'.").arg(ch));
|
||||
return false;
|
||||
}
|
||||
if (Buttons[num-1] == 0) {
|
||||
Buttons.insert(num-1,new Button(num-1));
|
||||
if (buttons[num-1] == 0) {
|
||||
buttons.insert(num-1,new Button(num-1));
|
||||
}
|
||||
if (!Buttons[num-1]->read( stream )) {
|
||||
if (!buttons[num-1]->read( stream )) {
|
||||
error("Layout file error", QString("Error reading Button %1").arg(num));
|
||||
return false;
|
||||
}
|
||||
@ -135,10 +164,10 @@ bool JoyPad::readConfig( QTextStream &stream ) {
|
||||
error("Layout file error", QString("Expected ':', found '%1'.").arg(ch));
|
||||
return false;
|
||||
}
|
||||
if (Axes[num-1] == 0) {
|
||||
Axes.insert(num-1,new Axis(num-1));
|
||||
if (axes[num-1] == 0) {
|
||||
axes.insert(num-1,new Axis(num-1));
|
||||
}
|
||||
if (!Axes[num-1]->read(stream)) {
|
||||
if (!axes[num-1]->read(stream)) {
|
||||
error("Layout file error", QString("Error reading Axis %1").arg(num));
|
||||
return false;
|
||||
}
|
||||
@ -155,12 +184,12 @@ bool JoyPad::readConfig( QTextStream &stream ) {
|
||||
|
||||
//only actually writes something if this JoyPad is NON DEFAULT.
|
||||
void JoyPad::write( QTextStream &stream ) {
|
||||
if (!Axes.empty() || !Buttons.empty()) {
|
||||
if (!axes.empty() || !buttons.empty()) {
|
||||
stream << getName() << " {\n";
|
||||
foreach (Axis *axis, Axes) {
|
||||
foreach (Axis *axis, axes) {
|
||||
axis->write(stream);
|
||||
}
|
||||
foreach (Button *button, Buttons) {
|
||||
foreach (Button *button, buttons) {
|
||||
button->write(stream);
|
||||
}
|
||||
stream << "}\n\n";
|
||||
@ -168,15 +197,15 @@ void JoyPad::write( QTextStream &stream ) {
|
||||
}
|
||||
|
||||
void JoyPad::release() {
|
||||
foreach (Axis *axis, Axes) {
|
||||
foreach (Axis *axis, axes) {
|
||||
axis->release();
|
||||
}
|
||||
foreach (Button *button, Buttons) {
|
||||
foreach (Button *button, buttons) {
|
||||
button->release();
|
||||
}
|
||||
}
|
||||
|
||||
void JoyPad::jsevent( js_event msg ) {
|
||||
void JoyPad::jsevent(const js_event &msg) {
|
||||
//if there is a JoyPadWidget around, ie, if the joypad is being edited
|
||||
if (jpw != NULL && hasFocus) {
|
||||
//tell the dialog there was an event. It will use this to flash
|
||||
@ -190,15 +219,18 @@ void JoyPad::jsevent( js_event msg ) {
|
||||
|
||||
//otherwise, lets create us a fake event! Pass on the event to whichever
|
||||
//Button or Axis was pressed and let them decide what to do with it.
|
||||
if (msg.type & JS_EVENT_AXIS) {
|
||||
qulonglong type = msg.type & ~JS_EVENT_INIT;
|
||||
if (type == JS_EVENT_AXIS) {
|
||||
debug_mesg("DEBUG: passing on an axis event\n");
|
||||
debug_mesg("DEBUG: %d %d\n", msg.number, msg.value);
|
||||
Axes[msg.number]->jsevent(msg.value);
|
||||
Axis *axis = axes[msg.number];
|
||||
if (axis) axis->jsevent(msg.value);
|
||||
}
|
||||
else {
|
||||
else if (type == JS_EVENT_BUTTON) {
|
||||
debug_mesg("DEBUG: passing on a button event\n");
|
||||
debug_mesg("DEBUG: %d %d\n", msg.number, msg.value);
|
||||
Buttons[msg.number]->jsevent(msg.value);
|
||||
Button *button = buttons[msg.number];
|
||||
if (button) button->jsevent(msg.value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -208,15 +240,11 @@ JoyPadWidget* JoyPad::widget( QWidget* parent, int i) {
|
||||
return jpw;
|
||||
}
|
||||
|
||||
void JoyPad::handleJoyEvents(int fd) {
|
||||
Q_UNUSED(fd);
|
||||
|
||||
void JoyPad::handleJoyEvents() {
|
||||
js_event msg;
|
||||
int len;
|
||||
|
||||
len = read( joydev, &msg, sizeof(js_event));
|
||||
ssize_t len = read(joydev, &msg, sizeof(js_event));
|
||||
//if there was a real event waiting,
|
||||
if (len == (int) sizeof(js_event)) {
|
||||
if (len == sizeof(js_event)) {
|
||||
//pass that event on to the joypad!
|
||||
jsevent(msg);
|
||||
}
|
||||
@ -227,30 +255,10 @@ void JoyPad::releaseWidget() {
|
||||
jpw = NULL;
|
||||
}
|
||||
|
||||
void JoyPad::unsetDev() {
|
||||
close(joydev);
|
||||
joydev = -1;
|
||||
if(joydevFileHandle != NULL) {
|
||||
delete joydevFileHandle;
|
||||
}
|
||||
}
|
||||
|
||||
void JoyPad::errorRead(int fd) {
|
||||
debug_mesg("There was an error reading off of the device with fd %d, disabling\n", fd);
|
||||
joydevFileHandle->blockSignals(true);
|
||||
joydevFileHandle->setEnabled(false);
|
||||
close(joydev);
|
||||
if(disconnect(joydevFileHandle , 0, 0, 0)) {
|
||||
joydevFileHandle->deleteLater();
|
||||
joydevFileHandle = NULL;
|
||||
}
|
||||
if(disconnect(joydevFileException, 0, 0, 0)) {
|
||||
joydevFileException->setEnabled(false);
|
||||
joydevFileException->deleteLater();
|
||||
joydevFileException = NULL;
|
||||
}
|
||||
joydev = -1;
|
||||
debug_mesg("Done disabling device with fd %d\n", fd);
|
||||
void JoyPad::errorRead() {
|
||||
debug_mesg("There was an error reading off of the device with fd %d, disabling\n", joydev);
|
||||
close();
|
||||
debug_mesg("Done disabling device with fd %d\n", joydev);
|
||||
}
|
||||
|
||||
void JoyPad::focusChange(bool focusState) {
|
||||
|
Reference in New Issue
Block a user