Friday, May 10, 2013

Listing Input devices in C / C++

         Without  input devices we can't do anything with our computer / system. Here Input device may be Mouse, Keyboard, Touch screen, External touch pad, Pen tablet, Mic, camera etc.

         When we are programatically dealing with input devices, It is unavoidable to List / scan all input devices to get information about those devices. For this purpose we can use a X function called XListInputDevices.

XDeviceInfo * XListInputDevices(Display *display, int *ndevices)
Display *display Specifies the connection to the X server.
int *ndevices Specifies the address of a variable into which the server can return the number of input devices available to the X server.

XListInputDevices allows a client to determine which devices are available for X input and information about those devices. An array of XDeviceInfo structures is returned, with one element in the array for each device. The number of devices is returned in the ndevices argument. The X pointer device and X keyboard device are reported, as well as all available extension input devices. The use member of the XDeviceInfo structure specifies the current use of the device.

Lets write a small C program to list All Xinput devices using XListInputDevices.

//File Name: ListInputDevices.c
#include <stdio.h>
#include <X11/Xutil.h>
#include <X11/extensions/XTest.h>
#include <X11/extensions/XInput.h>

void main()
        int n, i=0;
        XDeviceInfo *devList, *curDev;
        // Connecting to XServer
        Display *dpy = XOpenDisplay(0);
        // Getting Input Device list
        devList = XListInputDevices(dpy, &n);
        // Checking for more then 0 devices
       if (!devList)
        printf("\n No Input devices to List");
        // Listing devices ID and NAME
        while(i < n)
                curDev = devList + i;
                printf(" DEVICE ID: %d ",curDev->id);
                printf(" DEVICE NAME: %s \n",curDev->name);
To Run: gcc ListInputDevices.c -lX11 -lXi

Out Put: Your All Xinput Devices

1 comment:

  1. Required Packages to run above program in CentOS
    libX11, libX11-devel, libXi, libXi-devel
    We can install these packages using yum.