dev_uinput.c File Reference

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>

Include dependency graph for dev_uinput.c:

Go to the source code of this file.

Defines

#define KEY_PRESSED   1
#define KEY_RELEASED   0
#define PRESSURE_MAX   15000
#define PRESSURE_MIN   0
#define X_AXIS_MAX   240
#define X_AXIS_MIN   0
#define Y_AXIS_MAX   320
#define Y_AXIS_MIN   0

Functions

void button_click (int fd, int down)
void dev_uinput_close (int fd)
int dev_uinput_init_kbd (char *name)
 create a new keyboard event node
int dev_uinput_init_mouse (char *name)
 create a new mouse event node
int dev_uinput_key (int fd, unsigned short code, int pressed)
void dev_uinput_sync (int fd)
void ptr_abs (int fd, int x, int y)
int uinput_open_device (void)


Define Documentation

#define KEY_PRESSED   1

Author:
Patrik Gfeller <gfellerpatrik@gmx.net>

Copyright (C) 2004,2005,2006 Nils Faerber <nils.faerber@kernelconcepts.de>

Definition at line 39 of file dev_uinput.c.

#define KEY_RELEASED   0

Definition at line 40 of file dev_uinput.c.

#define PRESSURE_MAX   15000

Definition at line 47 of file dev_uinput.c.

Referenced by dev_uinput_init_mouse().

#define PRESSURE_MIN   0

Definition at line 46 of file dev_uinput.c.

Referenced by dev_uinput_init_mouse().

#define X_AXIS_MAX   240

Definition at line 42 of file dev_uinput.c.

Referenced by dev_uinput_init_mouse().

#define X_AXIS_MIN   0

Definition at line 43 of file dev_uinput.c.

Referenced by dev_uinput_init_mouse().

#define Y_AXIS_MAX   320

Definition at line 44 of file dev_uinput.c.

Referenced by dev_uinput_init_mouse().

#define Y_AXIS_MIN   0

Definition at line 45 of file dev_uinput.c.

Referenced by dev_uinput_init_mouse().


Function Documentation

void button_click ( int  fd,
int  down 
)

Definition at line 193 of file dev_uinput.c.

Referenced by ptrevent(), and write_to_input().

00193                                     {
00194         struct input_event ev;
00195         int
00196           pressure = 0;
00197 
00198         if (down) { pressure = 10000; }
00199 
00200         memset(&ev, 0, sizeof(ev));
00201         gettimeofday(&ev.time, NULL);
00202 
00203         ev.type  = EV_KEY;
00204         ev.value = down;
00205         ev.code  = BTN_TOUCH;
00206         // ev.code  = BTN_LEFT;
00207         write(fd, &ev, sizeof(ev));
00208 
00209         ev.type = EV_ABS;
00210         ev.value = pressure;
00211         ev.code = ABS_PRESSURE;
00212         write(fd, &ev, sizeof(ev));
00213 }

void dev_uinput_close ( int  fd  ) 

Definition at line 215 of file dev_uinput.c.

Referenced by main().

00215                               {
00216         ioctl(fd, UI_DEV_DESTROY);
00217         close(fd);
00218 
00219 }

int dev_uinput_init_kbd ( char *  name  ) 

create a new keyboard event node

Parameters:
[in] name this name will be assigend to the event node
Returns:
handle to the uinput device | false

Todo:
check what values I have to use for uinput id.vendor, id.product, id.version

Definition at line 125 of file dev_uinput.c.

References uinput_open_device().

Referenced by main().

00125                                     {
00126   struct uinput_user_dev dev;
00127   int
00128     fd_keyb,   // FileHandle for Key-Events
00129 
00130     aux;
00131 
00132   fd_keyb =  uinput_open_device();
00133 
00134   if (fd_keyb) {
00135     memset(&dev, 0, sizeof(dev));
00136     strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE);
00137 
00138     dev.id.bustype = BUS_HOST;
00140     dev.id.vendor  = 0x1F;
00141     dev.id.product = 0x1F;
00142     dev.id.version = 0x00;
00143     write(fd_keyb, &dev, sizeof(dev));
00144 
00145     if (ioctl(fd_keyb, UI_SET_EVBIT, EV_KEY) != 0) { perror("set evbit: EV_KEY"); close(fd_keyb); return -1; }
00146     if (ioctl(fd_keyb, UI_SET_EVBIT, EV_REP) != 0) { perror("set evbit: EV_REP"); close(fd_keyb); return -1; }
00147     for (aux = KEY_RESERVED; aux <= KEY_UNKNOWN; aux++)
00148       if (ioctl(fd_keyb, UI_SET_KEYBIT, aux) != 0) { perror("set evbit: UI_SET_KEYBIT"); close(fd_keyb); return -1; }
00149 
00150     if (ioctl(fd_keyb, UI_DEV_CREATE) != 0) { perror("set evbit: UI_DEV_CREATE"); close(fd_keyb); return -1; }
00151   } // end of [if]
00152   return fd_keyb;
00153 }

Here is the call graph for this function:

int dev_uinput_init_mouse ( char *  name  ) 

create a new mouse event node

Parameters:
[in] name this name will be assigend to the event node
Returns:
handle to the uinput device | false

Todo:
check what values I have to use for uinput id.vendor, id.product, id.version

Definition at line 81 of file dev_uinput.c.

References EV_SYN, PRESSURE_MAX, PRESSURE_MIN, uinput_open_device(), X_AXIS_MAX, X_AXIS_MIN, Y_AXIS_MAX, and Y_AXIS_MIN.

Referenced by main().

00081                                        {
00082   struct uinput_user_dev dev;
00083   int fd_mouse;  // FileHandle for Mouse-Events
00084 
00085   fd_mouse = uinput_open_device();
00086 
00087   if (fd_mouse) {
00088     memset(&dev, 0, sizeof(dev));
00089     strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE);
00090 
00091     dev.id.bustype = BUS_HOST;
00093     dev.id.vendor  = 0x1F;
00094     dev.id.product = 0x1E;
00095     dev.id.version = 0x00;
00096 
00097     dev.absmax[ABS_X] = X_AXIS_MAX;
00098     dev.absmin[ABS_X] = X_AXIS_MIN;
00099     dev.absmax[ABS_Y] = Y_AXIS_MAX;
00100     dev.absmin[ABS_Y] = Y_AXIS_MIN;
00101     dev.absmax[ABS_PRESSURE] = PRESSURE_MAX;
00102     dev.absmin[ABS_PRESSURE] = PRESSURE_MIN;
00103     write(fd_mouse, &dev, sizeof(dev));
00104 
00105     ioctl(fd_mouse, UI_SET_EVBIT, EV_KEY);
00106     ioctl(fd_mouse, UI_SET_EVBIT, EV_ABS);
00107     ioctl(fd_mouse, UI_SET_EVBIT, EV_SYN);
00108 
00109     ioctl(fd_mouse, UI_SET_ABSBIT, ABS_X);
00110     ioctl(fd_mouse, UI_SET_ABSBIT, ABS_Y);
00111     ioctl(fd_mouse, UI_SET_ABSBIT, ABS_PRESSURE);
00112 // ioctl(fd_mouse, UI_SET_RELBIT, REL_WHEEL);
00113 
00114 // ioctl(fd_mouse, UI_SET_KEYBIT, BTN_LEFT);
00115 // ioctl(fd_mouse, UI_SET_KEYBIT, BTN_RIGHT);
00116 // ioctl(fd_mouse, UI_SET_KEYBIT, BTN_MIDDLE);
00117     ioctl(fd_mouse, UI_SET_KEYBIT, BTN_TOUCH);
00118 
00119     if (ioctl(fd_mouse, UI_DEV_CREATE) != 0) { perror("set evbit: UI_DEV_CREATE"); close(fd_mouse); return -1; }
00120   } // end of [if]
00121 
00122   return fd_mouse;
00123 };

Here is the call graph for this function:

int dev_uinput_key ( int  fd,
unsigned short  code,
int  pressed 
)

Parameters:
[in] fd the filedescriptor handle to the uinput device returned by int dev_uinput_init();
[in] code The "code" is the keycode, this is the raw keycode as you can read them when starting "dumpkeys" on a text console.
[in] pressed ... kind of a bool, i.e. != 0 is a key press and == 0 is a release.

Definition at line 155 of file dev_uinput.c.

Referenced by emit_scancode().

00155                                                              {
00156   struct input_event event;
00157 
00158         memset(&event, 0, sizeof(event));
00159         event.type  = EV_KEY;
00160         event.code  = code;
00161         event.value = pressed;
00162 
00163   return (write(fd, &event, sizeof(event)));
00164 }

void dev_uinput_sync ( int  fd  ) 

Definition at line 183 of file dev_uinput.c.

References EV_SYN.

Referenced by ptrevent(), and write_to_input().

00183                              {
00184         struct input_event ev;
00185 
00186         gettimeofday(&ev.time, NULL);
00187         ev.type  = EV_SYN;
00188         ev.code  = SYN_REPORT;
00189         ev.value = 0;
00190         write(fd, &ev, sizeof(ev));
00191 }

void ptr_abs ( int  fd,
int  x,
int  y 
)

Definition at line 166 of file dev_uinput.c.

Referenced by ptrevent(), and write_to_input().

00166                                    {
00167  struct input_event event;
00168         memset(&event, 0, sizeof(event));
00169 
00170         gettimeofday(&event.time, NULL);
00171         event.type  = EV_ABS;
00172         event.code  = ABS_X;
00173         event.value = x;
00174         write(fd, &event, sizeof(event));
00175 
00176         gettimeofday(&event.time, NULL);
00177         event.type  = EV_ABS;
00178         event.code  = ABS_Y;
00179         event.value = y;
00180         write(fd, &event, sizeof(event));
00181 }

int uinput_open_device ( void   ) 

Definition at line 58 of file dev_uinput.c.

Referenced by dev_uinput_init_kbd(), and dev_uinput_init_mouse().

00058                              {
00059   struct uinput_user_dev dev;
00060   int fd = -1;
00061   int i  =  0;
00062 
00063   while (devs[i] != NULL) {
00064     if ( (fd = open(devs[i++], O_RDWR)) >= 0) {
00065       break;
00066     } // end of [if]
00067   } // end of [while]
00068 
00069   if (fd < 0) { perror("failed to open uinput device"); return -1; }
00070 
00071   memset(&dev, 0, sizeof(dev));
00072   if (write(fd, &dev, sizeof(dev)) < 0) {
00073     fprintf (stderr,"failed to write uinputdev: %s\n", strerror(errno));
00074     close(fd);
00075     return -1;
00076   }
00077 
00078   return fd;
00079 } // end of [uinput_open_device]