Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
13 KB
Referenced Files
None
Subscribers
None
diff --git a/util/joystick.cpp b/util/joystick.cpp
index f5a74242..631ef7f3 100644
--- a/util/joystick.cpp
+++ b/util/joystick.cpp
@@ -1,16 +1,19 @@
#include <stdlib.h>
#include "joystick.h"
#ifdef LINUX
#include "linux_joystick.h"
#endif
Joystick * Joystick::create(){
#ifdef LINUX
return new LinuxJoystick();
#endif
return NULL;
}
Joystick::Joystick(){
}
+
+Joystick::~Joystick(){
+}
diff --git a/util/joystick.h b/util/joystick.h
index ed60ddb2..88d2c4d7 100644
--- a/util/joystick.h
+++ b/util/joystick.h
@@ -1,15 +1,53 @@
#ifndef _paintown_joystick
#define _paintown_joystick
+#include <vector>
+
+struct JoystickInput{
+ JoystickInput():
+ left(false),
+ right(false),
+ up(false),
+ down(false),
+ button1(false),
+ button2(false),
+ button3(false),
+ button4(false){
+ }
+
+ JoystickInput(const JoystickInput & copy):
+ left(copy.left),
+ right(copy.right),
+ up(copy.up),
+ down(copy.down),
+ button1(copy.button1),
+ button2(copy.button2),
+ button3(copy.button3),
+ button4(copy.button4){
+ }
+
+ bool left;
+ bool right;
+ bool up;
+ bool down;
+ bool button1;
+ bool button2;
+ bool button3;
+ bool button4;
+};
+
class Joystick{
public:
virtual void poll() = 0;
+ virtual JoystickInput readAll() = 0;
+ virtual bool pressed() = 0;
+ virtual ~Joystick();
static Joystick * create();
protected:
Joystick();
};
#endif
diff --git a/util/linux_joystick.cpp b/util/linux_joystick.cpp
index 904c8d02..8b49fac9 100644
--- a/util/linux_joystick.cpp
+++ b/util/linux_joystick.cpp
@@ -1,289 +1,389 @@
#ifdef LINUX
-/*
+
+/* based on
* jstest.c Version 1.2
*
* Copyright (c) 1996-1999 Vojtech Pavlik
*
* Sponsored by SuSE
*/
-/*
- * This program can be used to test all the features of the Linux
- * joystick API, including non-blocking and select() access, as
- * well as version 0.x compatibility mode. It is also intended to
- * serve as an example implementation for those who wish to learn
- * how to write their own joystick using applications.
- */
-
-/*
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Should you need to contact me, the author, you can do so either by
- * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
- * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
- */
-
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
+#include <iostream>
+#include <sstream>
#include <linux/input.h>
#include <linux/joystick.h>
#include "linux_joystick.h"
+#include "globals.h"
+
+using namespace std;
static const char *axis_names[ABS_MAX + 1] = {
"X", "Y", "Z", "Rx", "Ry", "Rz", "Throttle", "Rudder",
"Wheel", "Gas", "Brake", "?", "?", "?", "?", "?",
"Hat0X", "Hat0Y", "Hat1X", "Hat1Y", "Hat2X", "Hat2Y", "Hat3X", "Hat3Y",
"?", "?", "?", "?", "?", "?", "?",
};
static const char *button_names[KEY_MAX - BTN_MISC + 1] = {
"Btn0", "Btn1", "Btn2", "Btn3", "Btn4", "Btn5", "Btn6", "Btn7", "Btn8", "Btn9", "?", "?", "?", "?", "?", "?",
"LeftBtn", "RightBtn", "MiddleBtn", "SideBtn", "ExtraBtn", "ForwardBtn", "BackBtn", "TaskBtn", "?", "?", "?", "?", "?", "?", "?", "?",
"Trigger", "ThumbBtn", "ThumbBtn2", "TopBtn", "TopBtn2", "PinkieBtn", "BaseBtn", "BaseBtn2", "BaseBtn3", "BaseBtn4", "BaseBtn5", "BaseBtn6", "BtnDead",
"BtnA", "BtnB", "BtnC", "BtnX", "BtnY", "BtnZ", "BtnTL", "BtnTR", "BtnTL2", "BtnTR2", "BtnSelect", "BtnStart", "BtnMode", "BtnThumbL", "BtnThumbR", "?",
"?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
"WheelBtn", "Gear up",
};
-#define NAME_LENGTH 128
+/* tries to open one /dev/input/jsX and return an open file descriptor */
+static int read_joystick(){
+ for (int num = 0; num < 100; num++){
+ ostringstream name;
+ name << "/dev/input/js" << num;
+ const char * cname = name.str().c_str();
+ int fd = open(cname, O_RDONLY);
+ if (fd >= 0){
+ return fd;
+ }
+ }
+ return -1;
+}
-void LinuxJoystick::poll(){
+LinuxJoystick::LinuxJoystick():
+button(0),
+axis(0){
+ file = read_joystick();
+ if (file == -1){
+ Global::debug(0) << "Could not open joystick" << endl;
+ } else {
+ Global::debug(1) << "Opened joystick " << endl;
+
+ /* grab useful info */
+ ioctl(file, JSIOCGVERSION, &version);
+ ioctl(file, JSIOCGAXES, &axes);
+ ioctl(file, JSIOCGBUTTONS, &buttons);
+ ioctl(file, JSIOCGNAME(NAME_LENGTH), name);
+ ioctl(file, JSIOCGAXMAP, axmap);
+ ioctl(file, JSIOCGBTNMAP, btnmap);
+
+ /* put joystick in nonblocking mode */
+ fcntl(file, F_SETFL, O_NONBLOCK);
+
+ axis = new int[axes];
+ memset(axis, 0, sizeof(int) * axes);
+ button = new char[buttons];
+ memset(button, 0, sizeof(char) * buttons);
+
+ Global::debug(1) << "Joystick axis: " << (int)axes << " buttons: " << (int)buttons << endl;
+ }
+}
+
+LinuxJoystick::~LinuxJoystick(){
+ delete[] axis;
+ delete[] button;
+ if (file != -1){
+ close(file);
+ }
}
-LinuxJoystick::LinuxJoystick(){
+void LinuxJoystick::poll(){
+
+ struct js_event js;
+
+ if (file == -1){
+ return;
+ }
+
+ int bytes = read(file, &js, sizeof(struct js_event));
+ if (bytes == sizeof(struct js_event)){
+ Global::debug(4) << "Event: type " << (int)js.type << " time " << (int)js.time << " number " << (int)js.number << " value " << (int)js.value << endl;
+
+ if (errno != EAGAIN) {
+ perror("joystick: error reading");
+ return;
+ }
+
+ switch (js.type & ~JS_EVENT_INIT) {
+ case JS_EVENT_BUTTON:
+ button[js.number] = js.value;
+ break;
+ case JS_EVENT_AXIS:
+ axis[js.number] = js.value;
+ break;
+ }
+ }
}
+bool LinuxJoystick::pressed(){
+ for (int i = 0; i < axes; i++){
+ if (axis[i]){
+ return true;
+ }
+ }
+ for (int i = 0; i < buttons; i++){
+ if (button[i]){
+ return true;
+ }
+ }
+ return false;
+}
+
+JoystickInput LinuxJoystick::readAll(){
+ JoystickInput input;
+ if (axis[0] < 0){
+ input.left = true;
+ }
+ if (axis[0] > 0){
+ input.right = true;
+ }
+ if (axis[1] < 0){
+ input.up = true;
+ }
+ if (axis[1] > 0){
+ input.down = true;
+ }
+ if (button[0]){
+ input.button1 = true;
+ }
+ if (button[1]){
+ input.button2 = true;
+ }
+ if (button[2]){
+ input.button3 = true;
+ }
+ if (button[3]){
+ input.button4 = true;
+ }
+ Global::debug(1) << "joystick input up " << input.up
+ << " down " << input.down
+ << " left " << input.left
+ << " right " << input.right
+ << " button1 " << input.button1
+ << " button2 " << input.button2
+ << " button3 " << input.button3
+ << " button4 " << input.button4
+ << endl;
+ return input;
+}
#if 0
int main (int argc, char **argv)
{
int fd, i;
unsigned char axes = 2;
unsigned char buttons = 2;
int version = 0x000800;
char name[NAME_LENGTH] = "Unknown";
uint16_t btnmap[KEY_MAX - BTN_MISC + 1];
uint8_t axmap[ABS_MAX + 1];
if (argc < 2 || argc > 3 || !strcmp("--help", argv[1])) {
puts("");
puts("Usage: jstest [<mode>] <device>");
puts("");
puts("Modes:");
puts(" --normal One-line mode showing immediate status");
puts(" --old Same as --normal, using 0.x interface");
puts(" --event Prints events as they come in");
puts(" --nonblock Same as --event, in nonblocking mode");
puts(" --select Same as --event, using select() call");
puts("");
return 1;
}
if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) {
perror("jstest");
return 1;
}
ioctl(fd, JSIOCGVERSION, &version);
ioctl(fd, JSIOCGAXES, &axes);
ioctl(fd, JSIOCGBUTTONS, &buttons);
ioctl(fd, JSIOCGNAME(NAME_LENGTH), name);
ioctl(fd, JSIOCGAXMAP, axmap);
ioctl(fd, JSIOCGBTNMAP, btnmap);
printf("Driver version is %d.%d.%d.\n",
version >> 16, (version >> 8) & 0xff, version & 0xff);
printf("Joystick (%s) has %d axes (", name, axes);
for (i = 0; i < axes; i++)
printf("%s%s", i > 0 ? ", " : "", axis_names[axmap[i]]);
puts(")");
printf("and %d buttons (", buttons);
for (i = 0; i < buttons; i++)
printf("%s%s", i > 0 ? ", " : "", button_names[btnmap[i] - BTN_MISC]);
puts(").");
printf("Testing ... (interrupt to exit)\n");
/*
* Old (0.x) interface.
*/
if ((argc == 2 && version < 0x010000) || !strcmp("--old", argv[1])) {
struct JS_DATA_TYPE js;
while (1) {
if (read(fd, &js, JS_RETURN) != JS_RETURN) {
perror("\njstest: error reading");
return 1;
}
printf("Axes: X:%3d Y:%3d Buttons: A:%s B:%s\r",
js.x, js.y, (js.buttons & 1) ? "on " : "off", (js.buttons & 2) ? "on " : "off");
fflush(stdout);
usleep(10000);
}
}
/*
* Event interface, single line readout.
*/
if (argc == 2 || !strcmp("--normal", argv[1])) {
int *axis;
char *button;
int i;
struct js_event js;
axis = calloc(axes, sizeof(int));
button = calloc(buttons, sizeof(char));
while (1) {
if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
perror("\njstest: error reading");
return 1;
}
switch(js.type & ~JS_EVENT_INIT) {
case JS_EVENT_BUTTON:
button[js.number] = js.value;
break;
case JS_EVENT_AXIS:
axis[js.number] = js.value;
break;
}
printf("\r");
if (axes) {
printf("Axes: ");
for (i = 0; i < axes; i++)
printf("%2d:%6d ", i, axis[i]);
}
if (buttons) {
printf("Buttons: ");
for (i = 0; i < buttons; i++)
printf("%2d:%s ", i, button[i] ? "on " : "off");
}
fflush(stdout);
}
}
/*
* Event interface, events being printed.
*/
if (!strcmp("--event", argv[1])) {
struct js_event js;
while (1) {
if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
perror("\njstest: error reading");
return 1;
}
printf("Event: type %d, time %d, number %d, value %d\n",
js.type, js.time, js.number, js.value);
fflush(stdout);
}
}
/*
* Reading in nonblocking mode.
*/
if (!strcmp("--nonblock", argv[1])) {
struct js_event js;
fcntl(fd, F_SETFL, O_NONBLOCK);
while (1) {
while (read(fd, &js, sizeof(struct js_event)) == sizeof(struct js_event)) {
printf("Event: type %d, time %d, number %d, value %d\n",
js.type, js.time, js.number, js.value);
}
if (errno != EAGAIN) {
perror("\njstest: error reading");
return 1;
}
usleep(10000);
}
}
/*
* Using select() on joystick fd.
*/
if (!strcmp("--select", argv[1])) {
struct js_event js;
struct timeval tv;
fd_set set;
tv.tv_sec = 1;
tv.tv_usec = 0;
while (1) {
FD_ZERO(&set);
FD_SET(fd, &set);
if (select(fd+1, &set, NULL, NULL, &tv)) {
if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
perror("\njstest: error reading");
return 1;
}
printf("Event: type %d, time %d, number %d, value %d\n",
js.type, js.time, js.number, js.value);
}
}
}
printf("jstest: unknown mode: %s\n", argv[1]);
return -1;
}
#endif
#endif
diff --git a/util/linux_joystick.h b/util/linux_joystick.h
index 5f3fb173..054f6caa 100644
--- a/util/linux_joystick.h
+++ b/util/linux_joystick.h
@@ -1,10 +1,32 @@
#include "joystick.h"
+#include <stdint.h>
+#include <vector>
+#include <linux/input.h>
+#include <linux/joystick.h>
+
+#define NAME_LENGTH 1024
+
class LinuxJoystick: public Joystick {
public:
virtual void poll();
+ virtual JoystickInput readAll();
+ virtual bool pressed();
+
+ virtual ~LinuxJoystick();
friend class Joystick;
protected:
LinuxJoystick();
+
+ /* file descriptor for /dev/input/jsX */
+ int file;
+ char name[NAME_LENGTH];
+ unsigned char axes;
+ unsigned char buttons;
+ int version;
+ uint16_t btnmap[KEY_MAX - BTN_MISC + 1];
+ uint8_t axmap[ABS_MAX + 1];
+ char * button;
+ int * axis;
};

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 1:48 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69320
Default Alt Text
(13 KB)

Event Timeline