Exploring /dev/input

Linux is one of those operating systems in which once you know your way around, you are unstoppable. In this post, we will explore what you can possibly do with access to /dev/input directory. Now you typically need to have superuser privileges to access this directory but there is a way around it. ( check this stackexchange )

The /dev directory

This is the directory in Linux that contains all the device files for all the devices that are on your system.

/dev/input – The input is a subdirectory that holds the device files for various input devices such as mouse, keyboard, joystick and so on.

Screenshot from 2017-04-18 22:10:46

This is a screenshot of my input directory and as you can see there is the mice/mouse0/mouse1 which are files corresponding to the touchpad/wired mouse/wireless mouse respectively. And then you have these ‘eventX’ files. Yours might look similar but depending on the nature of your device you might have more or less options available.

eventX

With so many event files, how can one possibly know which one corresponds to which ? This script is what you need:

cat /proc/bus/input/devices

This script would display relevant information about all the input devices connected to your system. Here’s a sample output for the Lid switch :

Screenshot from 2017-04-18 13:04:47

Now what we want is the event handler number for the lid switch which we can see from the information as event0. And through this one can map all the events with their corresponding devices.

Alternative method:

An alternate approach to doing this is to look at /dev/input/by-id or /dev/input/by-path, they have symbolic links to /dev/input/event<x>. But the only downside to this is that it does not display all the possible devices. We will discuss how to read from both in this post.

Let’s read the data stream !

Say you want to read the mouse file, you only need to open the file using ‘cat’ and start moving the mouse.

(sudo) cat /dev/input/event5

Screenshot from 2017-04-18 22:57:25

You would observe that as you are moving the mouse a stream of data is being dumped onto your screen. This data although seems crazy contains the information for the movement of the mouse.

Interpreting the input stream

The format for the input stream is given in the Linux documentation as follows:

struct input_event {
	struct timeval time;
	unsigned short type;
	unsigned short code;
	unsigned int value;
};

struct timeval – 16

unsigned short – 2 ( x 2 )

unsigned int – 4

Total size = 24 bytes

What this means is the first 16 bytes of data contain the system time, which in our case is not essential. The rest 8 bytes of data contain the actual relevant values. This is the output format of all eventX files.

Reading the mouse

Reading the mouse is probably the simplest to start with because one does not need to go through the trouble of accessing any eventX files. The mouse file in the /dev/input directory outputs a stream of 3/4 bytes ( Button value, x , y ).

You would get a 4 byte stream if the mouse is configured with the scroll wheel. Unfortunately in my case, even though I use a mouse with a scroll wheel, it was configured as a PS2 mouse. ( If you are in a similar situation – read this)

mouse_usb

Map of the (x, y) data with the corresponding directions

My touchpad and wireless mouse do not have any special buttons and therefore when the button values are mapped with the corresponding clicks, I got the following :

Left_Button – 9

Right_Button – 10

Scroll_Button – 12

When I was going through this stackexchange, the code seemed to suggest different values for the each of these buttons.

Python Code for reading mouse:

import struct 
f = open( "/dev/input/mice", "rb" ); 
# Open the file in the read-binary mode
while 1:
  data = f.read(3)  # Reads the 3 bytes 
  print struct.unpack('3b',data)  #Unpacks the bytes to integers

Video Demo:

Reading the keyboard

Much like the mouse, one can opt for an easier way by just reading from the /dev/input/by-id or /dev/input/by-path, but for the love of it let’s try to play around with eventX files. Let’s go about this step by step.

  1.  Find the event handler number for the keyboard ( Mine was event5 )
  2.  Reading the byte data and converting it to the format specified in the Linux Documentation using the struct module in python
  3.  Extracting the relevant information from the converted data.

 

Python code for reading keyboard:

import struct 
f = open( "/dev/input/event4", "rb" ); # Open the file in the read-binary mode
while 1:
  data = f.read(24)
  print struct.unpack('4IHHI',data)
  ###### PRINT FORMAL = ( Time Stamp_INT , 0 , Time Stamp_DEC , 0 , 
  ######   type , code ( key pressed ) , value (press/release) )

Video Demo:

Other eventX ?

What should you do if you would like to read from another eventX file ? Well, from my experience I can share with you this. : The first 16 bytes in the file are always almost the time stamp and you can read only that data by:

struct.unpack('4I',data[:16])

The rest depends on the type of output which you can find out by using the evtest program.

sudo evtest /dev/input/eventX

 

** Now in theory, using uinput one can reverse engineer to emulate events. But we are still currently working on that. If you know anything about this, please ping us at 153armstrong@gmail.com

 

Advertisements

4 thoughts on “Exploring /dev/input

  1. Pingback: Headphone jack plugged in or not ? | The hacker Diary

  2. Pingback: cron.weekly issue #84: Hadoop, UUID, Evil.sh, GDB, rl, Monica, SFTP, Containers & more

  3. Pingback: Collection of Linux tutorials for 14/June/2017 - Linux News Site

  4. Pingback: Links 18/6/2017: New Debian Release, Catchup With a Lot of News | Techrights

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s