View Full Version : QSocketNotifier or QLocalSocket Usage

15th December 2011, 23:36
We're trying to program a joystick reader on Red Hat Enterprise Linux 6.1.

The file being read is /dev/input/js0, it's a file that is created by Linux and joystick drivers when a joystick is plugged in. If you have multiple joysticks, there'd be js1, js2, js3... etc. etc. We're only ever going to have 1 joystick plugged in.

We're trying to avoid a while(true) loop that just constantly reads the file in a blocking state. The last time I posted something about this I got the suggestion of:

"Just use QSocketNotifier or QLocalSocket on /dev/input/js0"

We've been reading the documentation on these 2 classes, but we can't seem to get these to work.

So we have these members in a class:

FILE* fileReader;

QLocalSocket* fileSocket;

QSocketNotifier* fileSocketNotifier;

And in the constructor, they get initialized to:

// Try to open the file
fileReader = fopen( FILE_STRING.toStdString().c_str(), "r" ); // Where FILE_STRING = /dev/input/js0
int fd = fileno( fileReader );
fileSocket = new QLocalSocket(); // What does this class do?
fileSocket->setSocketDescriptor( ( quintptr ) fileReader ); // What is this class for? How do I use this with the Notifier?
fileSocketNotifier = new QSocketNotifier( fd, QSocketNotifier::Read ); // Is this how you initialize this class?

So... is there any good examples of simply opening/connecting to a socket and being able to read from it? I've Googled this but I usually end up back in the QT Documentation for it, which doesn't really provide examples for opening a socket, it just says socketNotifier( YOUR_MAGIC_SOCKET_NUMBER_HERE, otherStuff );

Yea.. I'm lost.

Any help would be appreciated.