Skip to content

Instantly share code, notes, and snippets.

@staaldraad
Created March 21, 2017 13:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save staaldraad/ec3007ccb9289d407c70e63162d03c69 to your computer and use it in GitHub Desktop.
Save staaldraad/ec3007ccb9289d407c70e63162d03c69 to your computer and use it in GitHub Desktop.
Filters keycodes from R400 presenter in Linux
/* Grabs all input from Logitech R400 presenter and filters to ensure only certain keys are pressed.
* Ensures that only valid R400 keys are pressed and not rogue keys injected.
* Main logic for this found here: http://stackoverflow.com/questions/7668872/need-to-intercept-hid-keyboard-events-and-then-block-them
* Author: Etienne Stalmans <etienne@sensepost.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
#include <signal.h>
int main(int argc, char* argv[])
{
struct input_event ev[64];
int fevdev = -1;
int inputdev = -1;
int result = 0;
int size = sizeof(struct input_event);
int rd;
int value;
char name[256] = "Logitech USB Receiver";
char *device = "/dev/input/by-id/usb-Logitech_USB_Receiver-event-kbd";
//get handle to keyboard, so we can forward keys
inputdev = open("/dev/input/event3",O_WRONLY|O_NONBLOCK);
if (inputdev == -1) {
printf("Failed to open keyboard device.\n");
exit(1);
}
//open handle to our logitech receiver
fevdev = open(device, O_RDONLY);
if (fevdev == -1) {
printf("Failed to open event device.\n");
exit(1);
}
result = ioctl(fevdev, EVIOCGNAME(sizeof(name)), name);
printf ("Reading From : %s (%s)\n", device, name);
printf("Getting exclusive access: ");
result = ioctl(fevdev, EVIOCGRAB, 1);
printf("%s\n", (result == 0) ? "SUCCESS" : "FAILURE");
while (1)
{
if ((rd = read(fevdev, ev, size * 64)) < size) {
break;
}
value = ev[0].value;
//check whether key_press or key_release
if (value != ' ' && (ev[1].value == 1 || ev[1].value == 0) && ev[1].type == 1 ) {
//printf ("Code[%d] Type[%d]\n", (ev[1].code),(ev[1].type));
//check that it is a valid key
if (ev[1].code == 109 ||ev[1].code == 104||ev[1].code == 52 || ev[1].code== 63){
write(inputdev,&ev,sizeof(ev));
}
}
}
printf("Exiting.\n");
result = ioctl(fevdev, EVIOCGRAB, 0);
close(fevdev);
close(inputdev);
return 0;
}
@staaldraad
Copy link
Author

Replace https://gist.github.com/staaldraad/ec3007ccb9289d407c70e63162d03c69#file-ioctlfilter-c-L33 with your device location.
Replace https://gist.github.com/staaldraad/ec3007ccb9289d407c70e63162d03c69#file-ioctlfilter-c-L36 with your input event for your keyboard.

compile with gcc
gcc ioctlfilter.c -o ioctlfilter
run with root/sudo

sudo ./ioctlfilter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment