aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans-Christoph Steiner <eighthave@users.sourceforge.net>2003-04-22 05:04:49 +0000
committerHans-Christoph Steiner <eighthave@users.sourceforge.net>2003-04-22 05:04:49 +0000
commit3f365ef507d2cf16ddef435b96f690bb454253b5 (patch)
tree181b08a4d8dfe7d1cd1f3800f17fffbcfc834cec
parent451a11d5c75349db05cc26406ca947cd1c5b39c0 (diff)
changed the name from raw to linux, since they are linux-specific; put common stuff in linuxhid.h
svn path=/trunk/externals/hcs/; revision=583
-rw-r--r--Makefile4
-rw-r--r--linuxevent.c305
-rw-r--r--linuxevent.pd14
-rw-r--r--linuxhid.h61
-rw-r--r--linuxjoystick.c383
-rw-r--r--linuxjoystick.pd98
-rw-r--r--linuxmouse.c331
-rw-r--r--linuxmouse.pd16
8 files changed, 1210 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 5a8b48b..25d4f8f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
-pd_linux: pan_gogins~.pd_linux range.pd_linux rawmouse.pd_linux rawevent.pd_linux rawjoystick.pd_linux
+pd_linux: pan_gogins~.pd_linux range.pd_linux linuxmouse.pd_linux linuxevent.pd_linux linuxjoystick.pd_linux rawjoystick.pd_linux
-clean: ; rm -f *.pd_linux *.o
+clean: ; rm -f *.pd_linux *.o *~
# ----------------------- LINUX i386 -----------------------
diff --git a/linuxevent.c b/linuxevent.c
new file mode 100644
index 0000000..a112430
--- /dev/null
+++ b/linuxevent.c
@@ -0,0 +1,305 @@
+
+#include <m_imp.h>
+
+#ifdef NT
+#pragma warning( disable : 4244 )
+#pragma warning( disable : 4305 )
+#endif
+
+#include <linux/input.h>
+
+#include <sys/stat.h>
+
+#include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/fcntl.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <termios.h>
+
+#include "linuxhid.h"
+
+#define DEBUG(x)
+/*#define DEBUG(x) x */
+
+#define LINUXEVENT_DEVICE "/dev/input/event0"
+#define LINUXEVENT_OUTLETS 4
+
+/*------------------------------------------------------------------------------
+ * CLASS DEF
+ */
+static t_class *linuxevent_class;
+
+typedef struct _linuxevent {
+ t_object x_obj;
+ t_int x_fd;
+ t_symbol* x_devname;
+ int read_ok;
+ int started;
+ struct input_event x_input_event;
+ t_outlet *x_input_event_time_outlet;
+ t_outlet *x_input_event_type_outlet;
+ t_outlet *x_input_event_code_outlet;
+ t_outlet *x_input_event_value_outlet;
+}t_linuxevent;
+
+/*------------------------------------------------------------------------------
+ * IMPLEMENTATION
+ */
+
+//DONE
+static int linuxevent_close(t_linuxevent *x)
+{
+ DEBUG(post("linuxevent_close");)
+
+ if (x->x_fd <0) return 0;
+
+ close (x->x_fd);
+
+ return 1;
+}
+
+//DONE
+static int linuxevent_open(t_linuxevent *x,t_symbol* s)
+{
+ int eventType, eventCode, buttons, rel_axes, abs_axes, ff;
+ unsigned long bitmask[EV_MAX][NBITS(KEY_MAX)];
+ char devicename[256] = "Unknown";
+ DEBUG(post("linuxevent_open");)
+
+ linuxevent_close(x);
+
+ /* set obj device name to parameter
+ * otherwise set to default
+ */
+ if (s != &s_)
+ x->x_devname = s;
+ else {
+ post("You need to set a input device (i.e /dev/input/event0)");
+ }
+
+ /* open device */
+ if (x->x_devname) {
+ post("opening ...");
+ /* open the linuxevent device read-only, non-exclusive */
+ x->x_fd = open (x->x_devname->s_name, O_RDONLY | O_NONBLOCK);
+ if (x->x_fd >= 0 ) post("done");
+ else post("failed");
+ }
+ else {
+ return 1;
+ }
+
+ /* test if device open */
+ if (x->x_fd >= 0)
+ post("%s opened",x->x_devname->s_name);
+ else {
+ post("unable to open %s",x->x_devname->s_name);
+ x->x_fd = -1;
+ return 0;
+ }
+
+ /* read input_events from the LINUXEVENT_DEVICE stream
+ * It seems that is just there to flush the event input buffer?
+ */
+ while (read (x->x_fd, &(x->x_input_event), sizeof(struct input_event)) > -1);
+
+ /* get name of device */
+ ioctl(x->x_fd, EVIOCGNAME(sizeof(devicename)), devicename);
+ post ("configuring %s",devicename);
+
+ /* get bitmask representing supported events (axes, buttons, etc.) */
+ memset(bitmask, 0, sizeof(bitmask));
+ ioctl(x->x_fd, EVIOCGBIT(0, EV_MAX), bitmask[0]);
+ post("Supported events:");
+
+ rel_axes = 0;
+ abs_axes = 0;
+ buttons = 0;
+ ff = 0;
+
+ /* cycle through all possible event types */
+ for (eventType = 0; eventType < EV_MAX; eventType++) {
+ if (test_bit(eventType, bitmask[0])) {
+ post(" %s (type %d) ", events[eventType] ? events[eventType] : "?", eventType);
+ // post("Event type %d",eventType);
+
+ /* get bitmask representing supported button types */
+ ioctl(x->x_fd, EVIOCGBIT(eventType, KEY_MAX), bitmask[eventType]);
+
+ /* cycle through all possible event codes (axes, keys, etc.)
+ * testing to see which are supported
+ */
+ for (eventCode = 0; eventCode < KEY_MAX; eventCode++)
+ if (test_bit(eventCode, bitmask[eventType])) {
+ post(" Event code %d (%s)", eventCode, names[eventType] ? (names[eventType][eventCode] ? names[eventType][eventCode] : "?") : "?");
+
+ switch(eventType) {
+ case EV_RST:
+ break;
+ case EV_KEY:
+ buttons++;
+ break;
+ case EV_REL:
+ rel_axes++;
+ break;
+ case EV_ABS:
+ abs_axes++;
+ break;
+ case EV_MSC:
+ break;
+ case EV_LED:
+ break;
+ case EV_SND:
+ break;
+ case EV_REP:
+ break;
+ case EV_FF:
+ ff++;
+ break;
+ }
+ }
+ }
+ }
+
+ post ("\nUsing %d relative axes, %d absolute axes, and %d buttons.", rel_axes, abs_axes, buttons);
+ if (ff > 0) post ("Detected %d force feedback types",ff);
+ post ("");
+ post ("WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING");
+ post ("This object is under development! The interface could change at anytime!");
+ post ("As I write cross-platform versions, the interface might have to change.");
+ post ("WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING");
+
+ return 1;
+}
+
+
+
+static int linuxevent_read(t_linuxevent *x,int fd)
+{
+ int readBytes;
+ int axis_num = 0;
+ t_float button_num = 0;
+
+ if (x->x_fd < 0) return 0;
+ if (x->read_ok) {
+ readBytes = read(x->x_fd, &(x->x_input_event), sizeof(struct input_event));
+ DEBUG(post("reading %d",readBytes);)
+ if ( readBytes < 0 ) {
+ post("linuxevent: read failed");
+ x->read_ok = 0;
+ return 0;
+ }
+ }
+ /* input_event.time is a timeval struct from <sys/time.h> */
+ /* outlet_float (x->x_input_event_time_outlet, x->x_input_event.time); */
+ outlet_float (x->x_input_event_type_outlet, x->x_input_event.type);
+ outlet_float (x->x_input_event_code_outlet, x->x_input_event.code);
+ outlet_float (x->x_input_event_value_outlet, (int)x->x_input_event.value);
+
+ return 1;
+}
+
+/* Actions */
+
+static void linuxevent_bang(t_linuxevent* x)
+{
+ DEBUG(post("linuxevent_bang");)
+
+}
+
+static void linuxevent_float(t_linuxevent* x)
+{
+ DEBUG(post("linuxevent_float");)
+
+}
+
+// DONE
+void linuxevent_start(t_linuxevent* x)
+{
+ DEBUG(post("linuxevent_start");)
+
+ if (x->x_fd >= 0 && !x->started) {
+ sys_addpollfn(x->x_fd, (t_fdpollfn)linuxevent_read, x);
+ post("linuxevent: start");
+ x->started = 1;
+ }
+}
+
+
+// DONE
+void linuxevent_stop(t_linuxevent* x)
+{
+ DEBUG(post("linuxevent_stop");)
+
+ if (x->x_fd >= 0 && x->started) {
+ sys_rmpollfn(x->x_fd);
+ post("linuxevent: stop");
+ x->started = 0;
+ }
+}
+
+/* Misc setup functions */
+
+
+static void linuxevent_free(t_linuxevent* x)
+{
+ DEBUG(post("linuxevent_free");)
+
+ if (x->x_fd < 0) return;
+
+ linuxevent_stop(x);
+
+ close (x->x_fd);
+}
+
+static void *linuxevent_new(t_symbol *s)
+{
+ int i;
+ t_linuxevent *x = (t_linuxevent *)pd_new(linuxevent_class);
+
+ DEBUG(post("linuxevent_new");)
+
+ /* init vars */
+ x->x_fd = -1;
+ x->read_ok = 1;
+ x->started = 0;
+
+ /* create outlets for each axis */
+ x->x_input_event_time_outlet = outlet_new(&x->x_obj, &s_float);
+ x->x_input_event_type_outlet = outlet_new(&x->x_obj, &s_float);
+ x->x_input_event_code_outlet = outlet_new(&x->x_obj, &s_float);
+ x->x_input_event_value_outlet = outlet_new(&x->x_obj, &s_float);
+
+ if (s != &s_)
+ x->x_devname = s;
+
+ /* Open the device and save settings */
+
+ if (!linuxevent_open(x,s)) return x;
+
+ return (x);
+}
+
+
+void linuxevent_setup(void)
+{
+ DEBUG(post("linuxevent_setup");)
+ linuxevent_class = class_new(gensym("linuxevent"),
+ (t_newmethod)linuxevent_new,
+ (t_method)linuxevent_free,
+ sizeof(t_linuxevent),0,A_DEFSYM,0);
+
+ /* add inlet datatype methods */
+ class_addfloat(linuxevent_class,(t_method) linuxevent_float);
+ class_addbang(linuxevent_class,(t_method) linuxevent_bang);
+
+ /* add inlet message methods */
+ class_addmethod(linuxevent_class, (t_method) linuxevent_open,gensym("open"),A_DEFSYM);
+ class_addmethod(linuxevent_class,(t_method) linuxevent_close,gensym("close"),0);
+ class_addmethod(linuxevent_class,(t_method) linuxevent_start,gensym("start"),0);
+ class_addmethod(linuxevent_class,(t_method) linuxevent_stop,gensym("stop"),0);
+
+}
+
diff --git a/linuxevent.pd b/linuxevent.pd
new file mode 100644
index 0000000..9e6be44
--- /dev/null
+++ b/linuxevent.pd
@@ -0,0 +1,14 @@
+#N canvas 454 205 450 300 10;
+#X floatatom 244 176 5 0 0 3 code - -;
+#X floatatom 303 176 5 0 0 3 value - -;
+#X floatatom 185 176 4 0 0 3 type - -;
+#X floatatom 112 176 9 0 0 3 time - -;
+#X msg 176 53 start;
+#X msg 187 91 stop;
+#X obj 126 123 linuxevent /dev/input/event0;
+#X connect 4 0 6 0;
+#X connect 5 0 6 0;
+#X connect 6 0 3 0;
+#X connect 6 1 2 0;
+#X connect 6 2 0 0;
+#X connect 6 3 1 0;
diff --git a/linuxhid.h b/linuxhid.h
new file mode 100644
index 0000000..9707eec
--- /dev/null
+++ b/linuxhid.h
@@ -0,0 +1,61 @@
+/*------------------------------------------------------------------------------
+ * from evtest.c from the ff-utils package
+ */
+
+#define BITS_PER_LONG (sizeof(long) * 8)
+#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
+#define OFF(x) ((x)%BITS_PER_LONG)
+#define BIT(x) (1UL<<OFF(x))
+#define LONG(x) ((x)/BITS_PER_LONG)
+#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
+
+
+char *events[EV_MAX + 1] = { "Reset", "Key", "Relative", "Absolute", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+NULL, NULL, NULL, "LED", "Sound", NULL, "Repeat", "ForceFeedback", NULL, "ForceFeedbackStatus"};
+char *keys[KEY_MAX + 1] = { "Reserved", "Esc", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "Minus", "Equal", "Backspace",
+"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "LeftBrace", "RightBrace", "Enter", "LeftControl", "A", "S", "D", "F", "G",
+"H", "J", "K", "L", "Semicolon", "Apostrophe", "Grave", "LeftShift", "BackSlash", "Z", "X", "C", "V", "B", "N", "M", "Comma", "Dot",
+"Slash", "RightShift", "KPAsterisk", "LeftAlt", "Space", "CapsLock", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",
+"NumLock", "ScrollLock", "KP7", "KP8", "KP9", "KPMinus", "KP4", "KP5", "KP6", "KPPlus", "KP1", "KP2", "KP3", "KP0", "KPDot", "103rd",
+"F13", "102nd", "F11", "F12", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "KPEnter", "RightCtrl", "KPSlash", "SysRq",
+"RightAlt", "LineFeed", "Home", "Up", "PageUp", "Left", "Right", "End", "Down", "PageDown", "Insert", "Delete", "Macro", "Mute",
+"VolumeDown", "VolumeUp", "Power", "KPEqual", "KPPlusMinus", "Pause", "F21", "F22", "F23", "F24", "KPComma", "LeftMeta", "RightMeta",
+"Compose", "Stop", "Again", "Props", "Undo", "Front", "Copy", "Open", "Paste", "Find", "Cut", "Help", "Menu", "Calc", "Setup",
+"Sleep", "WakeUp", "File", "SendFile", "DeleteFile", "X-fer", "Prog1", "Prog2", "WWW", "MSDOS", "Coffee", "Direction",
+"CycleWindows", "Mail", "Bookmarks", "Computer", "Back", "Forward", "CloseCD", "EjectCD", "EjectCloseCD", "NextSong", "PlayPause",
+"PreviousSong", "StopCD", "Record", "Rewind", "Phone", "ISOKey", "Config", "HomePage", "Refresh", "Exit", "Move", "Edit", "ScrollUp",
+"ScrollDown", "KPLeftParenthesis", "KPRightParenthesis",
+"International1", "International2", "International3", "International4", "International5",
+"International6", "International7", "International8", "International9",
+"Language1", "Language2", "Language3", "Language4", "Language5", "Language6", "Language7", "Language8", "Language9",
+NULL,
+"PlayCD", "PauseCD", "Prog3", "Prog4", "Suspend", "Close",
+NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+"Btn0", "Btn1", "Btn2", "Btn3", "Btn4", "Btn5", "Btn6", "Btn7", "Btn8", "Btn9",
+NULL, NULL, NULL, NULL, NULL, NULL,
+"LeftBtn", "RightBtn", "MiddleBtn", "SideBtn", "ExtraBtn", "ForwardBtn", "BackBtn",
+NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+"Trigger", "ThumbBtn", "ThumbBtn2", "TopBtn", "TopBtn2", "PinkieBtn",
+"BaseBtn", "BaseBtn2", "BaseBtn3", "BaseBtn4", "BaseBtn5", "BaseBtn6",
+NULL, NULL, NULL, "BtnDead",
+"BtnA", "BtnB", "BtnC", "BtnX", "BtnY", "BtnZ", "BtnTL", "BtnTR", "BtnTL2", "BtnTR2", "BtnSelect", "BtnStart", "BtnMode",
+"BtnThumbL", "BtnThumbR", NULL,
+"ToolPen", "ToolRubber", "ToolBrush", "ToolPencil", "ToolAirbrush", "ToolFinger", "ToolMouse", "ToolLens", NULL, NULL,
+"Touch", "Stylus", "Stylus2" };
+
+char *absval[5] = { "Value", "Min ", "Max ", "Fuzz ", "Flat " };
+char *relatives[REL_MAX + 1] = { "X", "Y", "Z", NULL, NULL, NULL, "HWheel", "Dial", "Wheel" };
+char *absolutes[ABS_MAX + 1] = { "X", "Y", "Z", "Rx", "Ry", "Rz", "Throttle", "Rudder", "Wheel", "Gas", "Brake",
+NULL, NULL, NULL, NULL, NULL,
+"Hat0X", "Hat0Y", "Hat1X", "Hat1Y", "Hat2X", "Hat2Y", "Hat3X", "Hat 3Y", "Pressure", "Distance", "XTilt", "YTilt"};
+char *leds[LED_MAX + 1] = { "NumLock", "CapsLock", "ScrollLock", "Compose", "Kana", "Sleep", "Suspend", "Mute" };
+char *repeats[REP_MAX + 1] = { "Delay", "Period" };
+char *sounds[SND_MAX + 1] = { "Bell", "Click" };
+
+char **names[EV_MAX + 1] = { events, keys, relatives, absolutes, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+NULL, NULL, leds, sounds, NULL, repeats, NULL, NULL, NULL };
+
+/*------------------------------------------------------------------------------
+ */
diff --git a/linuxjoystick.c b/linuxjoystick.c
new file mode 100644
index 0000000..ceb4edd
--- /dev/null
+++ b/linuxjoystick.c
@@ -0,0 +1,383 @@
+
+#include <m_imp.h>
+
+#ifdef NT
+#pragma warning( disable : 4244 )
+#pragma warning( disable : 4305 )
+#endif
+
+#include <linux/input.h>
+
+#include <sys/stat.h>
+
+#include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/fcntl.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <termios.h>
+
+#include "linuxhid.h"
+
+#define DEBUG(x)
+/*#define DEBUG(x) x */
+
+
+#define LINUXJOYSTICK_DEVICE "/dev/input/event0"
+
+#define LINUXJOYSTICK_AXES 6
+#define LINUXJOYSTICK_BUTTONS 9
+
+/*------------------------------------------------------------------------------
+ * CLASS DEF
+ */
+static t_class *linuxjoystick_class;
+
+typedef struct _linuxjoystick {
+ t_object x_obj;
+ t_int x_fd;
+ t_symbol* x_devname;
+ int read_ok;
+ int started;
+ struct input_event x_input_event;
+ t_outlet *x_axis_out[LINUXJOYSTICK_AXES];
+ t_outlet *x_button_num_out;
+ t_outlet *x_button_val_out;
+ t_clock *x_clock;
+ double x_delaytime;
+ unsigned char x_buttons;
+ unsigned char x_axes;
+}t_linuxjoystick;
+
+/*------------------------------------------------------------------------------
+ * IMPLEMENTATION
+ */
+
+//DONE
+static int linuxjoystick_close(t_linuxjoystick *x)
+{
+ DEBUG(post("linuxjoystick_close");)
+
+ if (x->x_fd <0) return 0;
+
+ close (x->x_fd);
+
+ return 1;
+}
+
+//DONE
+static int linuxjoystick_open(t_linuxjoystick *x,t_symbol* s)
+{
+ int eventType, eventCode;
+ unsigned long bitmask[EV_MAX][NBITS(KEY_MAX)];
+ char devicename[256] = "Unknown";
+ DEBUG(post("linuxjoystick_open");)
+
+ linuxjoystick_close(x);
+
+ /* set obj device name to parameter
+ * otherwise set to default
+ */
+ if (s != &s_)
+ x->x_devname = s;
+ else {
+ post("You need to set a input device (i.e /dev/input/event0)");
+ }
+
+ /* open device */
+ if (x->x_devname) {
+ post("opening ...");
+ /* open the linuxjoystick device read-only, non-exclusive */
+ x->x_fd = open (x->x_devname->s_name, O_RDONLY | O_NONBLOCK);
+ if (x->x_fd >= 0 ) post("done");
+ else post("failed");
+ }
+ else {
+ return 1;
+ }
+
+ /* test if device open */
+ if (x->x_fd >= 0)
+ post("%s opened",x->x_devname->s_name);
+ else {
+ post("unable to open %s",x->x_devname->s_name);
+ x->x_fd = -1;
+ return 0;
+ }
+
+ /* read input_events from the LINUXJOYSTICK_DEVICE stream
+ * It seems that is just there to flush the event input buffer?
+ */
+ while (read (x->x_fd, &(x->x_input_event), sizeof(struct input_event)) > -1);
+
+ /* get name of device */
+ ioctl(x->x_fd, EVIOCGNAME(sizeof(devicename)), devicename);
+ post ("configuring %s",devicename);
+
+ /* get bitmask representing supported events (axes, buttons, etc.) */
+ memset(bitmask, 0, sizeof(bitmask));
+ ioctl(x->x_fd, EVIOCGBIT(0, EV_MAX), bitmask[0]);
+ post("Supported events:");
+
+ x->x_axes = 0;
+ x->x_buttons = 0;
+
+ /* cycle through all possible event types */
+ for (eventType = 0; eventType < EV_MAX; eventType++) {
+ if (test_bit(eventType, bitmask[0])) {
+ post(" %s (type %d) ", events[eventType] ? events[eventType] : "?", eventType);
+ // post("Event type %d",eventType);
+
+ /* get bitmask representing supported button types */
+ ioctl(x->x_fd, EVIOCGBIT(eventType, KEY_MAX), bitmask[eventType]);
+
+ /* cycle through all possible event codes (axes, keys, etc.)
+ * testing to see which are supported
+ */
+ for (eventCode = 0; eventCode < KEY_MAX; eventCode++)
+ if (test_bit(eventCode, bitmask[eventType])) {
+ post(" Event code %d (%s)", eventCode, names[eventType] ? (names[eventType][eventCode] ? names[eventType][eventCode] : "?") : "?");
+
+ if ( eventType == EV_KEY )
+ x->x_buttons++;
+ else if ( eventType == EV_ABS )
+ x->x_axes++;
+ }
+ }
+ }
+
+ post ("\nUsing %d axes and %d buttons.", x->x_axes, x->x_buttons);
+ post ("WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING");
+ post ("This object is under development! The interface could change at anytime!");
+ post ("As I write cross-platform versions, the interface might have to change.");
+ post ("WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING");
+
+ return 1;
+}
+
+
+
+static int linuxjoystick_read(t_linuxjoystick *x,int fd)
+{
+ int readBytes;
+ int axis_num = 0;
+ t_float button_num = 0;
+
+ if (x->x_fd < 0) return 0;
+ if (x->read_ok) {
+ readBytes = read(x->x_fd, &(x->x_input_event), sizeof(struct input_event));
+ DEBUG(post("reading %d",readBytes);)
+ if ( readBytes < 0 ) {
+ post("linuxjoystick: read failed");
+ x->read_ok = 0;
+ return 0;
+ }
+ }
+ if ( x->x_input_event.type == EV_KEY ) {
+ /* key/button event type */
+ switch ( x->x_input_event.code ) {
+ case BTN_0:
+ button_num = 0;
+ break;
+ case BTN_1:
+ button_num = 1;
+ break;
+ case BTN_2:
+ button_num = 2;
+ break;
+ case BTN_3:
+ button_num = 3;
+ break;
+ case BTN_4:
+ button_num = 4;
+ break;
+ case BTN_5:
+ button_num = 5;
+ break;
+ case BTN_6:
+ button_num = 6;
+ break;
+ case BTN_7:
+ button_num = 7;
+ break;
+ case BTN_8:
+ button_num = 8;
+ break;
+ case BTN_9:
+ button_num = 9;
+ break;
+ case BTN_TRIGGER:
+ button_num = 10;
+ break;
+ case BTN_THUMB:
+ button_num = 11;
+ break;
+ case BTN_THUMB2:
+ button_num = 12;
+ break;
+ case BTN_TOP:
+ button_num = 13;
+ break;
+ case BTN_TOP2:
+ button_num = 14;
+ break;
+ case BTN_PINKIE:
+ button_num = 15;
+ break;
+ case BTN_BASE:
+ button_num = 16;
+ break;
+ case BTN_BASE2:
+ button_num = 17;
+ break;
+ case BTN_BASE3:
+ button_num = 18;
+ break;
+ case BTN_BASE4:
+ button_num = 19;
+ break;
+ case BTN_BASE5:
+ button_num = 20;
+ break;
+ case BTN_BASE6:
+ button_num = 21;
+ break;
+ }
+ outlet_float (x->x_button_val_out, x->x_input_event.value);
+ outlet_float (x->x_button_num_out, button_num);
+ }
+ else if ( x->x_input_event.type == EV_ABS ) {
+ /* Relative Axes Event Type */
+ switch ( x->x_input_event.code ) {
+ case ABS_X:
+ axis_num = 0;
+ break;
+ case ABS_Y:
+ axis_num = 1;
+ break;
+ case ABS_Z:
+ axis_num = 2;
+ break;
+ case ABS_HAT0X:
+ axis_num = 3;
+ break;
+ case ABS_HAT0Y:
+ axis_num = 4;
+ break;
+ case ABS_THROTTLE:
+ axis_num = 5;
+ break;
+ }
+ outlet_float (x->x_axis_out[axis_num], (int)x->x_input_event.value);
+ }
+
+ return 1;
+}
+
+
+
+/* Actions */
+
+static void linuxjoystick_bang(t_linuxjoystick* x)
+{
+ DEBUG(post("linuxjoystick_bang");)
+
+}
+
+static void linuxjoystick_float(t_linuxjoystick* x)
+{
+ DEBUG(post("linuxjoystick_float");)
+
+}
+
+// DONE
+void linuxjoystick_start(t_linuxjoystick* x)
+{
+ DEBUG(post("linuxjoystick_start");)
+
+ if (x->x_fd >= 0 && !x->started) {
+ sys_addpollfn(x->x_fd, (t_fdpollfn)linuxjoystick_read, x);
+ post("linuxjoystick: start");
+ x->started = 1;
+ }
+}
+
+
+// DONE
+void linuxjoystick_stop(t_linuxjoystick* x)
+{
+ DEBUG(post("linuxjoystick_stop");)
+
+ if (x->x_fd >= 0 && x->started) {
+ sys_rmpollfn(x->x_fd);
+ post("linuxjoystick: stop");
+ x->started = 0;
+ }
+}
+
+/* Misc setup functions */
+
+
+static void linuxjoystick_free(t_linuxjoystick* x)
+{
+ DEBUG(post("linuxjoystick_free");)
+
+ if (x->x_fd < 0) return;
+
+ linuxjoystick_stop(x);
+
+ close (x->x_fd);
+}
+
+static void *linuxjoystick_new(t_symbol *s)
+{
+ int i;
+ t_linuxjoystick *x = (t_linuxjoystick *)pd_new(linuxjoystick_class);
+
+ DEBUG(post("linuxjoystick_new");)
+
+ /* init vars */
+ x->x_fd = -1;
+ x->read_ok = 1;
+ x->started = 0;
+
+ /* create outlets for each axis */
+ for (i = 0; i < LINUXJOYSTICK_AXES; i++)
+ x->x_axis_out[i] = outlet_new(&x->x_obj, &s_float);
+
+ /* create outlets for buttons */
+ x->x_button_num_out = outlet_new(&x->x_obj, &s_float);
+ x->x_button_val_out = outlet_new(&x->x_obj, &s_float);
+
+ if (s != &s_)
+ x->x_devname = s;
+
+ /* Open the device and save settings */
+
+ if (!linuxjoystick_open(x,s)) return x;
+
+ return (x);
+}
+
+
+void linuxjoystick_setup(void)
+{
+ DEBUG(post("linuxjoystick_setup");)
+ linuxjoystick_class = class_new(gensym("linuxjoystick"),
+ (t_newmethod)linuxjoystick_new,
+ (t_method)linuxjoystick_free,
+ sizeof(t_linuxjoystick),0,A_DEFSYM,0);
+
+ /* add inlet datatype methods */
+ class_addfloat(linuxjoystick_class,(t_method) linuxjoystick_float);
+ class_addbang(linuxjoystick_class,(t_method) linuxjoystick_bang);
+
+ /* add inlet message methods */
+ class_addmethod(linuxjoystick_class, (t_method) linuxjoystick_open,gensym("open"),A_DEFSYM);
+ class_addmethod(linuxjoystick_class,(t_method) linuxjoystick_close,gensym("close"),0);
+ class_addmethod(linuxjoystick_class,(t_method) linuxjoystick_start,gensym("start"),0);
+ class_addmethod(linuxjoystick_class,(t_method) linuxjoystick_stop,gensym("stop"),0);
+
+}
+
diff --git a/linuxjoystick.pd b/linuxjoystick.pd
new file mode 100644
index 0000000..0c37469
--- /dev/null
+++ b/linuxjoystick.pd
@@ -0,0 +1,98 @@
+#N canvas 61 386 886 505 10;
+#X msg 192 64 start;
+#X msg 203 95 stop;
+#X floatatom 397 180 2 0 0 3 button# - -;
+#X floatatom 454 179 2 0 0 3 button_value - -;
+#X floatatom 143 180 5 0 0 3 y-axis - -;
+#X floatatom 95 180 5 0 0 3 x-axis - -;
+#X floatatom 242 181 3 0 0 3 hat-X - -;
+#X floatatom 288 181 3 0 0 3 hat-Y - -;
+#X floatatom 192 181 5 0 0 3 twist - -;
+#X floatatom 331 180 5 0 0 3 throttle - -;
+#X obj 192 123 linuxjoystick /dev/input/event1;
+#X obj 299 309 select 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
+19 20 21;
+#X obj 297 354 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 313 355 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 333 354 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 351 354 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 370 353 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 389 354 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 409 353 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 734 354 bng 30 250 50 0 empty empty not_recognized 0 -6 0 8
+-261689 -1 -1;
+#X obj 689 352 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 667 350 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 647 351 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 631 354 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 613 352 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 596 350 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 579 352 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 561 351 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 541 349 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 523 351 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 503 350 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 485 351 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 466 351 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 447 353 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 428 353 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 439 220 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X connect 0 0 10 0;
+#X connect 1 0 10 0;
+#X connect 2 0 11 0;
+#X connect 3 0 35 0;
+#X connect 10 0 5 0;
+#X connect 10 1 4 0;
+#X connect 10 2 8 0;
+#X connect 10 3 6 0;
+#X connect 10 4 7 0;
+#X connect 10 5 9 0;
+#X connect 10 6 2 0;
+#X connect 10 7 3 0;
+#X connect 11 0 12 0;
+#X connect 11 1 13 0;
+#X connect 11 2 14 0;
+#X connect 11 3 15 0;
+#X connect 11 4 16 0;
+#X connect 11 5 17 0;
+#X connect 11 6 18 0;
+#X connect 11 7 34 0;
+#X connect 11 8 33 0;
+#X connect 11 9 32 0;
+#X connect 11 10 31 0;
+#X connect 11 11 30 0;
+#X connect 11 12 29 0;
+#X connect 11 13 28 0;
+#X connect 11 14 27 0;
+#X connect 11 15 26 0;
+#X connect 11 16 25 0;
+#X connect 11 17 24 0;
+#X connect 11 18 23 0;
+#X connect 11 19 22 0;
+#X connect 11 20 21 0;
+#X connect 11 21 20 0;
+#X connect 11 22 19 0;
+#X connect 35 0 2 0;
diff --git a/linuxmouse.c b/linuxmouse.c
new file mode 100644
index 0000000..913d35b
--- /dev/null
+++ b/linuxmouse.c
@@ -0,0 +1,331 @@
+
+#include <m_imp.h>
+
+#ifdef NT
+#pragma warning( disable : 4244 )
+#pragma warning( disable : 4305 )
+#endif
+
+
+#include <linux/input.h>
+
+#include <sys/stat.h>
+
+#include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/fcntl.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <termios.h>
+
+#include "linuxhid.h"
+
+#define DEBUG(x)
+/*#define DEBUG(x) x */
+
+
+#define LINUXMOUSE_DEVICE "/dev/input/event0"
+
+#define LINUXMOUSE_AXES 3
+#define LINUXMOUSE_BUTTONS 7
+
+
+/*------------------------------------------------------------------------------
+ * CLASS DEF
+ */
+static t_class *linuxmouse_class;
+
+typedef struct _linuxmouse {
+ t_object x_obj;
+ t_int x_fd;
+ t_symbol* x_devname;
+ int read_ok;
+ int started;
+ struct input_event x_input_event;
+ t_outlet *x_axis_out[LINUXMOUSE_AXES];
+ t_outlet *x_button_num_out;
+ t_outlet *x_button_val_out;
+ t_clock *x_clock;
+ double x_delaytime;
+ unsigned char x_buttons;
+ unsigned char x_axes;
+}t_linuxmouse;
+
+/*------------------------------------------------------------------------------
+ * IMPLEMENTATION
+ */
+
+//DONE
+static int linuxmouse_close(t_linuxmouse *x)
+{
+ DEBUG(post("linuxmouse_close");)
+
+ if (x->x_fd <0) return 0;
+
+ close (x->x_fd);
+
+ return 1;
+}
+
+//DONE
+static int linuxmouse_open(t_linuxmouse *x,t_symbol* s)
+{
+ int eventType, eventCode;
+ unsigned long bitmask[EV_MAX][NBITS(KEY_MAX)];
+ char devicename[256] = "Unknown";
+ DEBUG(post("linuxmouse_open");)
+
+ linuxmouse_close(x);
+
+ /* set obj device name to parameter
+ * otherwise set to default
+ */
+ if (s != &s_)
+ x->x_devname = s;
+ else {
+ post("You need to set a input device (i.e /dev/input/event0)");
+ }
+
+ /* open device */
+ if (x->x_devname) {
+ post("opening ...");
+ /* open the linuxmouse device read-only, non-exclusive */
+ x->x_fd = open (x->x_devname->s_name, O_RDONLY | O_NONBLOCK);
+ if (x->x_fd >= 0 ) post("done");
+ else post("failed");
+ }
+ else {
+ return 1;
+ }
+
+ /* test if device open */
+ if (x->x_fd >= 0)
+ post("%s opened",x->x_devname->s_name);
+ else {
+ post("unable to open %s",x->x_devname->s_name);
+ x->x_fd = -1;
+ return 0;
+ }
+
+ /* read input_events from the LINUXMOUSE_DEVICE stream
+ * It seems that is just there to flush the event input buffer?
+ */
+ while (read (x->x_fd, &(x->x_input_event), sizeof(struct input_event)) > -1);
+
+ /* get name of device */
+ ioctl(x->x_fd, EVIOCGNAME(sizeof(devicename)), devicename);
+ post ("configuring %s",devicename);
+
+ /* get bitmask representing supported events (axes, buttons, etc.) */
+ memset(bitmask, 0, sizeof(bitmask));
+ ioctl(x->x_fd, EVIOCGBIT(0, EV_MAX), bitmask[0]);
+ post("Supported events:");
+
+ x->x_axes = 0;
+ x->x_buttons = 0;
+
+ /* cycle through all possible event types */
+ for (eventType = 0; eventType < EV_MAX; eventType++) {
+ if (test_bit(eventType, bitmask[0])) {
+ post(" %s (type %d) ", events[eventType] ? events[eventType] : "?", eventType);
+ // post("Event type %d",eventType);
+
+ /* get bitmask representing supported button types */
+ ioctl(x->x_fd, EVIOCGBIT(eventType, KEY_MAX), bitmask[eventType]);
+
+ /* cycle through all possible event codes (axes, keys, etc.)
+ * testing to see which are supported
+ */
+ for (eventCode = 0; eventCode < KEY_MAX; eventCode++)
+ if (test_bit(eventCode, bitmask[eventType])) {
+ post(" Event code %d (%s)", eventCode, names[eventType] ? (names[eventType][eventCode] ? names[eventType][eventCode] : "?") : "?");
+
+ if ( eventType == EV_KEY )
+ x->x_buttons++;
+ else if ( eventType == EV_REL )
+ x->x_axes++;
+ }
+ }
+ }
+
+ post ("\nUsing %d axes and %d buttons.", x->x_axes, x->x_buttons);
+ post ("WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING");
+ post ("This object is under development! The interface could change at anytime!");
+ post ("As I write cross-platform versions, the interface might have to change.");
+ post ("WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING");
+
+ return 1;
+}
+
+
+
+static int linuxmouse_read(t_linuxmouse *x,int fd)
+{
+ int readBytes;
+ int axis_num = 0;
+ t_float button_num = 0;
+
+ if (x->x_fd < 0) return 0;
+ if (x->read_ok) {
+ readBytes = read(x->x_fd, &(x->x_input_event), sizeof(struct input_event));
+ DEBUG(post("reading %d",readBytes);)
+ if ( readBytes < 0 ) {
+ post("linuxmouse: read failed");
+ x->read_ok = 0;
+ return 0;
+ }
+ }
+ if ( x->x_input_event.type == EV_KEY ) {
+ /* key/button event type */
+ switch ( x->x_input_event.code ) {
+ case BTN_LEFT:
+ button_num = 0;
+ break;
+ case BTN_RIGHT:
+ button_num = 1;
+ break;
+ case BTN_MIDDLE:
+ button_num = 2;
+ break;
+ case BTN_SIDE:
+ button_num = 3;
+ break;
+ case BTN_EXTRA:
+ button_num = 4;
+ break;
+ case BTN_FORWARD:
+ button_num = 5;
+ break;
+ case BTN_BACK:
+ button_num = 6;
+ break;
+ }
+ outlet_float (x->x_button_val_out, x->x_input_event.value);
+ outlet_float (x->x_button_num_out, button_num);
+ }
+ else if ( x->x_input_event.type == EV_REL ) {
+ /* Relative Axes Event Type */
+ switch ( x->x_input_event.code ) {
+ case REL_X:
+ axis_num = 0;
+ break;
+ case REL_Y:
+ axis_num = 1;
+ break;
+ case REL_WHEEL:
+ axis_num = 2;
+ break;
+ }
+ outlet_float (x->x_axis_out[axis_num], (int)x->x_input_event.value);
+ }
+
+ return 1;
+}
+
+
+
+/* Actions */
+
+static void linuxmouse_bang(t_linuxmouse* x)
+{
+ DEBUG(post("linuxmouse_bang");)
+
+}
+
+static void linuxmouse_float(t_linuxmouse* x)
+{
+ DEBUG(post("linuxmouse_float");)
+
+}
+
+// DONE
+void linuxmouse_start(t_linuxmouse* x)
+{
+ DEBUG(post("linuxmouse_start");)
+
+ if (x->x_fd >= 0 && !x->started) {
+ sys_addpollfn(x->x_fd, (t_fdpollfn)linuxmouse_read, x);
+ post("linuxmouse: start");
+ x->started = 1;
+ }
+}
+
+
+// DONE
+void linuxmouse_stop(t_linuxmouse* x)
+{
+ DEBUG(post("linuxmouse_stop");)
+
+ if (x->x_fd >= 0 && x->started) {
+ sys_rmpollfn(x->x_fd);
+ post("linuxmouse: stop");
+ x->started = 0;
+ }
+}
+
+/* Misc setup functions */
+
+
+static void linuxmouse_free(t_linuxmouse* x)
+{
+ DEBUG(post("linuxmouse_free");)
+
+ if (x->x_fd < 0) return;
+
+ linuxmouse_stop(x);
+
+ close (x->x_fd);
+}
+
+static void *linuxmouse_new(t_symbol *s)
+{
+ int i;
+ t_linuxmouse *x = (t_linuxmouse *)pd_new(linuxmouse_class);
+
+ DEBUG(post("linuxmouse_new");)
+
+ /* init vars */
+ x->x_fd = -1;
+ x->read_ok = 1;
+ x->started = 0;
+
+ /* create outlets for each axis */
+ for (i = 0; i < LINUXMOUSE_AXES; i++)
+ x->x_axis_out[i] = outlet_new(&x->x_obj, &s_float);
+
+ /* create outlets for buttons */
+ x->x_button_num_out = outlet_new(&x->x_obj, &s_float);
+ x->x_button_val_out = outlet_new(&x->x_obj, &s_float);
+
+ if (s != &s_)
+ x->x_devname = s;
+
+ /* Open the device and save settings */
+
+ if (!linuxmouse_open(x,s)) return x;
+
+ return (x);
+}
+
+
+void linuxmouse_setup(void)
+{
+ DEBUG(post("linuxmouse_setup");)
+ linuxmouse_class = class_new(gensym("linuxmouse"),
+ (t_newmethod)linuxmouse_new,
+ (t_method)linuxmouse_free,
+ sizeof(t_linuxmouse),0,A_DEFSYM,0);
+
+ /* add inlet datatype methods */
+ class_addfloat(linuxmouse_class,(t_method) linuxmouse_float);
+ class_addbang(linuxmouse_class,(t_method) linuxmouse_bang);
+
+ /* add inlet message methods */
+ class_addmethod(linuxmouse_class, (t_method) linuxmouse_open,gensym("open"),A_DEFSYM);
+ class_addmethod(linuxmouse_class,(t_method) linuxmouse_close,gensym("close"),0);
+ class_addmethod(linuxmouse_class,(t_method) linuxmouse_start,gensym("start"),0);
+ class_addmethod(linuxmouse_class,(t_method) linuxmouse_stop,gensym("stop"),0);
+
+}
+
diff --git a/linuxmouse.pd b/linuxmouse.pd
new file mode 100644
index 0000000..e735296
--- /dev/null
+++ b/linuxmouse.pd
@@ -0,0 +1,16 @@
+#N canvas 454 205 450 300 10;
+#X floatatom 258 160 5 0 0 3 button# - -;
+#X floatatom 321 162 5 0 0 3 button_value - -;
+#X floatatom 175 160 2 0 0 3 wheel - -;
+#X floatatom 99 161 4 0 0 3 y-axis - -;
+#X floatatom 24 162 4 0 0 3 x-axis - -;
+#X obj 75 110 linuxmouse /dev/input/event0;
+#X msg 125 40 start;
+#X msg 136 78 stop;
+#X connect 5 0 4 0;
+#X connect 5 1 3 0;
+#X connect 5 2 2 0;
+#X connect 5 3 0 0;
+#X connect 5 4 1 0;
+#X connect 6 0 5 0;
+#X connect 7 0 5 0;