4

I'm working on a project on a raspberry pi in Qt. I have a usb keyboard and a usb magnetic card reader (which reads as a keyboard) plugged in. I need to be able to isolate the card reader input so that it can't be used to fill a regular text box and is read differently for credit card info. Both seem to have their files in /dev as hidraw items, though their ordering is random. Is there a way I can isolate one from the other programmatically? Thanks in advance for any help!

Nick
  • 401
  • 5
  • 18
  • Try `evrouter`. It can filter input events based on the device name. (if you use full X environment) – Velkan Feb 16 '17 at 06:58
  • Darn, I'm not in an X environment since I'm using EGLFS. Thanks though! – Nick Feb 16 '17 at 14:14
  • 1
    Maybe it uses `evdev` anyways. And I've seen that you can pass in an environment variable to set which device to use. Or maybe EGLFS uses `libinput`. Either way there are command-line tools in `evdev` and `libinput` to figure out the names of the devices and read the input from them. There is `QT_LOGGING_RULES=qt.qpa.input=true` variable to be able to see what Qt uses. – Velkan Feb 17 '17 at 12:34

1 Answers1

1

So as far as I can find, there's no way to use Qt to figure out the source of an event. Unfortunately there's also no way to use udev to change a kernel node so there's no way to prevent Qt from using it's input file. The only thing I could do was grab the input file and gain exclusive access, thus cutting Qt out. I did this in a separate thread that waits for input from the device and then reports it using a signal. I'll post pieces of some code for the QThread for those interested.

#include <QObject>
#include <QThread>
#include <QDebug>
#include <qplatformdefs.h>
#include "stdio.h"
#include "constants.h"
#include "linux/input.h"    

namespace KeyboardConstants {
        static const QString keys[] = {"","","1","2","3","4","5","6","7","8","9","0",
                                       "-","=","","","q","w","e","r","t","y","u",
                                      "i","o","p","[","]","","","a","s","d","f",
                                      "g","h","j","k","l",";","'","`","","\\","z",
                                      "x","c","v","B","n","m",",",".","/","","","",
                                      " ","",""};
        static const QString shiftKeys[] = {"","","!","@","#","$","%","^","&","*","(",")",
                                           "_","+","","","Q","W","E","R","T","Y","U",
                                           "I","O","P","{","}","","","A","S","D","F",
                                           "G","H","J","K","L",":","\"","~","","|","Z",
                                           "X","C","V","B","N","M","<",">","?","","",""};
    }

QString input = "";

void ccWatcher::run()
{
    struct input_event ev[1];
    int fevdev = -1;
    int size = sizeof(struct input_event);
    int rd;
    char name[256] = "Unknown";
    bool shift = false;
    QString device = "/dev/input/by-id/usb-XXXX";


    fevdev = open(device.toStdString().c_str(), O_RDONLY);

    if (fevdev >= 0) {
        ioctl(fevdev, EVIOCGNAME(sizeof(name)), name);
        // Gain exclusive access to the input_event file
        ioctl(fevdev, EVIOCGRAB, 1);
        while (1)
        {
            // Shouldn't happen, but you never know
            if ((rd = read(fevdev, ev, size)) < size) {
                break;
            }
            // Make sure the type is "key" and the value is 1
            if (ev[0].type == 1 && ev[0].value == 1) {
                // 28 and 96 are the codes for 'enter'
                if (ev[0].code != 28 && ev[0].code != 96) {
                    // 42 and 54 are the codes for shift
                    if (ev[0].code == 42 || ev[0].code == 54) {
                        shift = true;
                    } else {
                        if (shift) {
                            input.append(KeyboardConstants::shiftKeys[ev[0].code]);
                            shift = false;
                        } else input.append(KeyboardConstants::keys[ev[0].code]);
                    }
                } else {
                    emit ccReadin(input);
                    input = "";
                }
            }
        }
    }
}
Nick
  • 401
  • 5
  • 18