From: Jon Langseth Date: Sat, 4 Dec 2010 20:20:12 +0000 (+0100) Subject: Working test with Qt GUI. Threaded, with no thread safety. X-Git-Url: https://git.defcon.no/?p=joysticktest;a=commitdiff_plain Working test with Qt GUI. Threaded, with no thread safety. --- 621cd2e86d109e7deeea934ae5cfb82c5915d3d7 diff --git a/JoystickView.pro b/JoystickView.pro new file mode 100644 index 0000000..de3dca8 --- /dev/null +++ b/JoystickView.pro @@ -0,0 +1,13 @@ +# ------------------------------------------------- +# Project created by QtCreator 2010-12-01T17:33:12 +# ------------------------------------------------- +TARGET = JoystickView +TEMPLATE = app +SOURCES += main.cpp \ + joystickview.cpp \ + joystickdata.cpp \ + joystickthread.cpp +HEADERS += joystickview.h \ + joystickdata.h \ + joystickthread.h +FORMS += joystickview.ui diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f6f7bfd --- /dev/null +++ b/Makefile @@ -0,0 +1,236 @@ +############################################################################# +# Makefile for building: JoystickView +# Generated by qmake (2.01a) (Qt 4.5.0) on: Wed Dec 1 19:48:45 2010 +# Project: JoystickView.pro +# Template: app +# Command: /opt/qtsdk-2009.01/qt/bin/qmake -spec /opt/qtsdk-2009.01/qt/mkspecs/linux-g++-64 -unix CONFIG+=debug -o Makefile JoystickView.pro +############################################################################# + +####### Compiler, tools and options + +CC = gcc +CXX = g++ +DEFINES = -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED +CFLAGS = -m64 -pipe -g -Wall -W -D_REENTRANT $(DEFINES) +CXXFLAGS = -m64 -pipe -g -Wall -W -D_REENTRANT $(DEFINES) +INCPATH = -I/opt/qtsdk-2009.01/qt/mkspecs/linux-g++-64 -I. -I/opt/qtsdk-2009.01/qt/include/QtCore -I/opt/qtsdk-2009.01/qt/include/QtGui -I/opt/qtsdk-2009.01/qt/include -I. -I. +LINK = g++ +LFLAGS = -m64 -Wl,-rpath,/opt/qtsdk-2009.01/qt/lib +LIBS = $(SUBLIBS) -L/opt/qtsdk-2009.01/qt/lib -lQtGui -L/opt/qtsdk-2009.01/qt/lib -L/usr/X11R6/lib64 -pthread -lfreetype -lgobject-2.0 -lSM -lICE -pthread -pthread -lXrender -lfontconfig -lXext -lX11 -lQtCore -lm -pthread -lgthread-2.0 -lrt -lglib-2.0 -ldl -lpthread +AR = ar cqs +RANLIB = +QMAKE = /opt/qtsdk-2009.01/qt/bin/qmake +TAR = tar -cf +COMPRESS = gzip -9f +COPY = cp -f +SED = sed +COPY_FILE = $(COPY) +COPY_DIR = $(COPY) -r +INSTALL_FILE = install -m 644 -p +INSTALL_DIR = $(COPY_DIR) +INSTALL_PROGRAM = install -m 755 -p +DEL_FILE = rm -f +SYMLINK = ln -sf +DEL_DIR = rmdir +MOVE = mv -f +CHK_DIR_EXISTS= test -d +MKDIR = mkdir -p + +####### Output directory + +OBJECTS_DIR = ./ + +####### Files + +SOURCES = main.cpp \ + joystickview.cpp \ + joystickdata.cpp \ + joystickthread.cpp moc_joystickview.cpp \ + moc_joystickdata.cpp +OBJECTS = main.o \ + joystickview.o \ + joystickdata.o \ + joystickthread.o \ + moc_joystickview.o \ + moc_joystickdata.o +DIST = /opt/qtsdk-2009.01/qt/mkspecs/common/g++.conf \ + /opt/qtsdk-2009.01/qt/mkspecs/common/unix.conf \ + /opt/qtsdk-2009.01/qt/mkspecs/common/linux.conf \ + /opt/qtsdk-2009.01/qt/mkspecs/qconfig.pri \ + /opt/qtsdk-2009.01/qt/mkspecs/features/qt_functions.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/qt_config.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/exclusive_builds.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/default_pre.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/debug.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/default_post.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/warn_on.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/qt.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/unix/thread.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/moc.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/resources.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/uic.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/yacc.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/lex.prf \ + JoystickView.pro +QMAKE_TARGET = JoystickView +DESTDIR = +TARGET = JoystickView + +first: all +####### Implicit rules + +.SUFFIXES: .o .c .cpp .cc .cxx .C + +.cpp.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cc.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cxx.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.C.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.c.o: + $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<" + +####### Build rules + +all: Makefile $(TARGET) + +$(TARGET): ui_joystickview.h $(OBJECTS) + $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS) + +Makefile: JoystickView.pro /opt/qtsdk-2009.01/qt/mkspecs/linux-g++-64/qmake.conf /opt/qtsdk-2009.01/qt/mkspecs/common/g++.conf \ + /opt/qtsdk-2009.01/qt/mkspecs/common/unix.conf \ + /opt/qtsdk-2009.01/qt/mkspecs/common/linux.conf \ + /opt/qtsdk-2009.01/qt/mkspecs/qconfig.pri \ + /opt/qtsdk-2009.01/qt/mkspecs/features/qt_functions.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/qt_config.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/exclusive_builds.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/default_pre.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/debug.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/default_post.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/warn_on.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/qt.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/unix/thread.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/moc.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/resources.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/uic.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/yacc.prf \ + /opt/qtsdk-2009.01/qt/mkspecs/features/lex.prf \ + /opt/qtsdk-2009.01/qt/lib/libQtGui.prl \ + /opt/qtsdk-2009.01/qt/lib/libQtCore.prl + $(QMAKE) -spec /opt/qtsdk-2009.01/qt/mkspecs/linux-g++-64 -unix CONFIG+=debug -o Makefile JoystickView.pro +/opt/qtsdk-2009.01/qt/mkspecs/common/g++.conf: +/opt/qtsdk-2009.01/qt/mkspecs/common/unix.conf: +/opt/qtsdk-2009.01/qt/mkspecs/common/linux.conf: +/opt/qtsdk-2009.01/qt/mkspecs/qconfig.pri: +/opt/qtsdk-2009.01/qt/mkspecs/features/qt_functions.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/qt_config.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/exclusive_builds.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/default_pre.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/debug.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/default_post.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/warn_on.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/qt.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/unix/thread.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/moc.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/resources.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/uic.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/yacc.prf: +/opt/qtsdk-2009.01/qt/mkspecs/features/lex.prf: +/opt/qtsdk-2009.01/qt/lib/libQtGui.prl: +/opt/qtsdk-2009.01/qt/lib/libQtCore.prl: +qmake: FORCE + @$(QMAKE) -spec /opt/qtsdk-2009.01/qt/mkspecs/linux-g++-64 -unix CONFIG+=debug -o Makefile JoystickView.pro + +dist: + @$(CHK_DIR_EXISTS) .tmp/JoystickView1.0.0 || $(MKDIR) .tmp/JoystickView1.0.0 + $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/JoystickView1.0.0/ && $(COPY_FILE) --parents joystickview.h joystickdata.h joystickthread.h .tmp/JoystickView1.0.0/ && $(COPY_FILE) --parents main.cpp joystickview.cpp joystickdata.cpp joystickthread.cpp .tmp/JoystickView1.0.0/ && $(COPY_FILE) --parents joystickview.ui .tmp/JoystickView1.0.0/ && (cd `dirname .tmp/JoystickView1.0.0` && $(TAR) JoystickView1.0.0.tar JoystickView1.0.0 && $(COMPRESS) JoystickView1.0.0.tar) && $(MOVE) `dirname .tmp/JoystickView1.0.0`/JoystickView1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/JoystickView1.0.0 + + +clean:compiler_clean + -$(DEL_FILE) $(OBJECTS) + -$(DEL_FILE) *~ core *.core + + +####### Sub-libraries + +distclean: clean + -$(DEL_FILE) $(TARGET) + -$(DEL_FILE) Makefile + + +mocclean: compiler_moc_header_clean compiler_moc_source_clean + +mocables: compiler_moc_header_make_all compiler_moc_source_make_all + +compiler_moc_header_make_all: moc_joystickview.cpp moc_joystickdata.cpp +compiler_moc_header_clean: + -$(DEL_FILE) moc_joystickview.cpp moc_joystickdata.cpp +moc_joystickview.cpp: joystickdata.h \ + joystickthread.h \ + joystickview.h + /opt/qtsdk-2009.01/qt/bin/moc $(DEFINES) $(INCPATH) joystickview.h -o moc_joystickview.cpp + +moc_joystickdata.cpp: joystickdata.h + /opt/qtsdk-2009.01/qt/bin/moc $(DEFINES) $(INCPATH) joystickdata.h -o moc_joystickdata.cpp + +compiler_rcc_make_all: +compiler_rcc_clean: +compiler_image_collection_make_all: qmake_image_collection.cpp +compiler_image_collection_clean: + -$(DEL_FILE) qmake_image_collection.cpp +compiler_moc_source_make_all: +compiler_moc_source_clean: +compiler_uic_make_all: ui_joystickview.h +compiler_uic_clean: + -$(DEL_FILE) ui_joystickview.h +ui_joystickview.h: joystickview.ui + /opt/qtsdk-2009.01/qt/bin/uic joystickview.ui -o ui_joystickview.h + +compiler_yacc_decl_make_all: +compiler_yacc_decl_clean: +compiler_yacc_impl_make_all: +compiler_yacc_impl_clean: +compiler_lex_make_all: +compiler_lex_clean: +compiler_clean: compiler_moc_header_clean compiler_uic_clean + +####### Compile + +main.o: main.cpp joystickview.h \ + joystickdata.h \ + joystickthread.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp + +joystickview.o: joystickview.cpp joystickview.h \ + joystickdata.h \ + joystickthread.h \ + ui_joystickview.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o joystickview.o joystickview.cpp + +joystickdata.o: joystickdata.cpp joystickdata.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o joystickdata.o joystickdata.cpp + +joystickthread.o: joystickthread.cpp joystickthread.h \ + joystickdata.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o joystickthread.o joystickthread.cpp + +moc_joystickview.o: moc_joystickview.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_joystickview.o moc_joystickview.cpp + +moc_joystickdata.o: moc_joystickdata.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_joystickdata.o moc_joystickdata.cpp + +####### Install + +install: FORCE + +uninstall: FORCE + +FORCE: + diff --git a/joystickdata.cpp b/joystickdata.cpp new file mode 100644 index 0000000..54d0c5b --- /dev/null +++ b/joystickdata.cpp @@ -0,0 +1,42 @@ +#include "joystickdata.h" + +JoystickData::JoystickData() +{ + for (int i = 0; i<6; i++) + { + this->setAxis(i, 512); + } + for (int i = 0; i<10; i++) + { + this->setButton(i, 0); + } + +} + +void JoystickData::setAxis( int axis, int value ) +{ + if (( axis < 0) || (axis > 5)) + return; + value = (value/64) + 512; // Conversion from +/-32768 to 0..1024 + axis_values[axis] = value; + emit valuechanged(); +} +void JoystickData::setButton( int button, bool value ) +{ + if (( button < 0) || (button > 9)) + return; + button_values[button] = value; + emit valuechanged(); +} +int JoystickData::getAxis( int axis ) +{ + if (( axis < 0) || (axis > 5)) + return -1; + return axis_values[axis]; +} +bool JoystickData::getButton( int button ) +{ + if (( button < 0) || (button > 9)) + return 0; + return button_values[button]; +} diff --git a/joystickdata.h b/joystickdata.h new file mode 100644 index 0000000..16de377 --- /dev/null +++ b/joystickdata.h @@ -0,0 +1,25 @@ +#ifndef JOYSTICKDATA_H +#define JOYSTICKDATA_H + +#include + +class JoystickData : public QObject +{ + Q_OBJECT + +private: + int axis_values[6]; + bool button_values[10]; + +public: + JoystickData(); + void setAxis( int axis, int value ); + void setButton( int button, bool value ); + int getAxis( int axis ); + bool getButton( int button ); + +signals: + void valuechanged( ); +}; + +#endif // JOYSTICKDATA_H diff --git a/joystickthread.cpp b/joystickthread.cpp new file mode 100644 index 0000000..bfd87ca --- /dev/null +++ b/joystickthread.cpp @@ -0,0 +1,79 @@ +#include "joystickthread.h" + +#include +#include +#include +#include +#include +#include +#include + +int JoystickThread::open_joystick(char *joystick_device) +{ + joystick_fd = open(joystick_device, O_RDONLY | O_NONBLOCK); + + return joystick_fd; +} +int JoystickThread::read_joystick_event(struct js_event *jse) +{ + int bytes; + + bytes = read(joystick_fd, jse, sizeof(*jse)); + + if (bytes == -1) + return 0; + + if (bytes == sizeof(*jse)) + return 1; + + return -1; +} + +JoystickThread::JoystickThread(JoystickData *data, QString device) +{ + this->device = device; + this->jd = data; + this->joystick_fd = -1; + this->done = 0; + +} + +void JoystickThread::run() +{ + int fd, rc; + + struct js_event jse; + fd = open_joystick(this->device.toLatin1().data()); + if (fd < 0) { + return; + } + while (!done) { + rc = read_joystick_event(&jse); + usleep(1000); + if (rc == 1) { + if (jse.type == JS_EVENT_AXIS) + { + if ( jse.number <= 5) + { + jd->setAxis(jse.number, jse.value); + } + } + if (jse.type == JS_EVENT_BUTTON) + { + if ( jse.number <= 9) + { + jd->setButton(jse.number, jse.value); + } + } + } + } + close(joystick_fd); + joystick_fd = -1; + done = 0; + return; +} +void JoystickThread::stop() +{ + this->done = 1; + this->wait(); +} diff --git a/joystickthread.h b/joystickthread.h new file mode 100644 index 0000000..ac89156 --- /dev/null +++ b/joystickthread.h @@ -0,0 +1,37 @@ +#ifndef JOYSTICKTHREAD_H +#define JOYSTICKTHREAD_H + +#include +#include "joystickdata.h" + +#define JS_EVENT_BUTTON 0x01 /* button pressed/released */ +#define JS_EVENT_AXIS 0x02 /* joystick moved */ +#define JS_EVENT_INIT 0x80 /* initial state of device */ +#define JS_EVENT_AXISENUM 0x81 /* enumeration of available axis */ +#define JS_EVENT_BUTTONENUM 0x81 /* enumeration of available buttons */ + +struct js_event { + unsigned int time; /* event timestamp in milliseconds */ + short value; /* value */ + unsigned char type; /* event type */ + unsigned char number; /* axis/button number */ +}; + +class JoystickThread : public QThread +{ +private: + JoystickData *jd; + int open_joystick(char *joystick_device); + int read_joystick_event(struct js_event *jse); + + int done; + int joystick_fd; + QString device; + +public: + JoystickThread(JoystickData *jd, const QString device); + void run(); + void stop(); +}; + +#endif // JOYSTICKTHREAD_H diff --git a/joystickview.cpp b/joystickview.cpp new file mode 100644 index 0000000..c2db695 --- /dev/null +++ b/joystickview.cpp @@ -0,0 +1,73 @@ +#include "joystickview.h" +#include "ui_joystickview.h" +#include "joystickdata.h" +#include "joystickthread.h" +#include +#include + +QSlider *sliders[6]; +QCheckBox *btns[10]; + +JoystickView::JoystickView(QWidget *parent) + : QMainWindow(parent), ui(new Ui::JoystickView) +{ + this->jd = new JoystickData(); + this->jthread = new JoystickThread(jd, "/dev/input/js0"); + + ui->setupUi(this); + ui->textbox->setPlainText("Starting"); + + QObject::connect(this, SIGNAL(forceupdate()), this, SLOT(updateValues())); + QObject::connect(jd, SIGNAL(valuechanged()), this, SLOT(updateValues())); + jthread->start(); + + sliders[0] = ui->axis1; + sliders[1] = ui->axis2; + sliders[2] = ui->axis3; + sliders[3] = ui->axis4; + sliders[4] = ui->axis5; + sliders[5] = ui->axis6; + + btns[0] = ui->button1; + btns[1] = ui->button2; + btns[2] = ui->button3; + btns[3] = ui->button4; + btns[4] = ui->button5; + btns[5] = ui->button6; + btns[6] = ui->button7; + btns[7] = ui->button8; + btns[8] = ui->button9; + btns[9] = ui->button10; + this->update(); +} + +JoystickView::~JoystickView() +{ + delete ui; +} + +void JoystickView::updateValues(void) +{ + QString output(""); + + output.append("Axis> "); + //ui->textbox->appendPlainText("Update\n"); + for ( int i = 0; i < 6; i++ ) + { + sliders[i]->setValue( jd->getAxis(i) ); + output.append(QString::number(i) + ":" + QString::number(jd->getAxis(i)) + " " ); + } + output.append("\nButtons>"); + for ( int i = 0; i < 10; i++ ) + { + btns[i]->setChecked( jd->getButton(i) ); + output.append(QString::number(i) + ":" + QString::number(jd->getButton(i)) + " " ); + } + ui->textbox->appendPlainText(output + "\n"); +} + +void JoystickView::on_pushButton_clicked() +{ + jthread->stop(); + QApplication::quit(); +} diff --git a/joystickview.h b/joystickview.h new file mode 100644 index 0000000..206d15c --- /dev/null +++ b/joystickview.h @@ -0,0 +1,34 @@ +#ifndef JOYSTICKVIEW_H +#define JOYSTICKVIEW_H + +#include +#include "joystickdata.h" +#include "joystickthread.h" + +namespace Ui +{ + class JoystickView; +} + +class JoystickView : public QMainWindow +{ + Q_OBJECT + +public: + JoystickView(QWidget *parent = 0); + ~JoystickView(); + +private: + Ui::JoystickView *ui; + JoystickData *jd; + JoystickThread *jthread; + +public slots: + void updateValues(void); +private slots: + void on_pushButton_clicked(); +signals: + void forceupdate(void); +}; + +#endif // JOYSTICKVIEW_H diff --git a/joystickview.png b/joystickview.png new file mode 100644 index 0000000..aa168ca Binary files /dev/null and b/joystickview.png differ diff --git a/joystickview.ui b/joystickview.ui new file mode 100644 index 0000000..e1efd42 --- /dev/null +++ b/joystickview.ui @@ -0,0 +1,290 @@ + + + JoystickView + + + + 0 + 0 + 521 + 507 + + + + JoystickView + + + + + + 90 + 13 + 20 + 171 + + + + 1024 + + + 512 + + + Qt::Vertical + + + + + + 150 + 13 + 20 + 171 + + + + 1024 + + + 512 + + + Qt::Vertical + + + + + + 210 + 13 + 20 + 171 + + + + 1024 + + + 512 + + + Qt::Vertical + + + + + + 270 + 13 + 20 + 171 + + + + 1024 + + + 512 + + + Qt::Vertical + + + + + + 390 + 10 + 20 + 171 + + + + 1024 + + + 512 + + + Qt::Vertical + + + + + + 330 + 13 + 20 + 171 + + + + 1024 + + + 512 + + + Qt::Vertical + + + + + + 150 + 190 + 16 + 22 + + + + + + + + + + 170 + 190 + 16 + 22 + + + + + + + + + + 190 + 190 + 16 + 22 + + + + + + + + + + 210 + 190 + 16 + 22 + + + + + + + + + + 230 + 190 + 16 + 22 + + + + + + + + + + 250 + 190 + 16 + 22 + + + + + + + + + + 270 + 190 + 16 + 22 + + + + + + + + + + 290 + 190 + 16 + 22 + + + + + + + + + + 310 + 190 + 16 + 22 + + + + + + + + + + 330 + 190 + 16 + 22 + + + + + + + + + + 40 + 220 + 441 + 271 + + + + + + + 400 + 190 + 83 + 26 + + + + EXIT + + + + + + + + + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a983708 --- /dev/null +++ b/main.cpp @@ -0,0 +1,10 @@ +#include +#include "joystickview.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + JoystickView w; + w.show(); + return a.exec(); +}