I am working on one "practical" program I need and it needs to be written in something like Xlib. One thing the program will do is read ALL key presses made by the user, to do this I must "open" the XDevices that are XI_KEYBOARDs.
So I have a loop which "opens" the XDevices that need to be opened
for(i = 0; i < devicesN; i++) {
if(deviceInfo[i].type == keyboardAtom) {
printf("%s %d %d %d\n", deviceInfo[i].name, deviceInfo[i].type, deviceInfo[i].id, keyboardAtom);
device = XOpenDevice(display, deviceInfo[i].id);
inputInfo = device->classes;
for(j = 0; j < deviceInfo->num_classes; j++, inputInfo++) {
printf("%d %d\n", inputInfo->input_class, KeyClass);
if(inputInfo->input_class == KeyClass) {
DeviceKeyPress(device, keypressType, events[eventsN++]);
}
}
if(XSelectExtensionEvent(display, RootWindow(display, screen), events, eventsN))
return 1;
}
}
Now the first time the loops executes everything goes fine. It first prints out Power Button 101 6 101
meaning name of device i: Power Button, type: 101, id: 6 and finally the keyboard atom 101, meaning that this device is something we should open and "listen" to. Well this works it does open this first device. Now the second time it executes this loop something odd happens,it again prints out Power Button 101 7 101
everything is fine and the id is 7 this time which is good. But after printing that out this error occurs:
X Error of failed request: XI_BadClass (invalid Class parameter)
Major opcode of failed request: 131 (XInputExtension)
Minor opcode of failed request: 6 (X_SelectExtensionEvent)
Class id in failed request: 0x6bf
Serial number of failed request: 19
Current serial number in output stream: 20
According to my debugging this error happens during the opening of the device so on this line: device = XOpenDevice(display, deviceInfo[i].id);
.
I have tried skipping the 7th device and going straight to the next device and it the error still occurs. It looks like it always happens on the second device "opening".
All help is appreciated!
PS: If you think this can be done more easily and less painfully on xcb then you can also suggest a way of doing it with xcb.