dev_uinput.c

Go to the documentation of this file.
00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00015  *
00016  */
00017 
00024 #include <stdio.h>
00025 #include <errno.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <unistd.h>
00029 
00030 #include <sys/types.h>
00031 #include <sys/stat.h>
00032 #include <sys/ioctl.h>
00033 
00034 #include <fcntl.h>
00035 
00036 #include <linux/input.h>
00037 #include <linux/uinput.h>
00038 
00039 #define KEY_PRESSED     1
00040 #define KEY_RELEASED    0
00041 
00042 #define X_AXIS_MAX              240
00043 #define X_AXIS_MIN              0
00044 #define Y_AXIS_MAX              320
00045 #define Y_AXIS_MIN              0
00046 #define PRESSURE_MIN            0
00047 #define PRESSURE_MAX            15000
00048 
00049 
00050 static char *devs[] = {
00051         "/dev/misc/uinput",
00052         "/dev/input/uinput",
00053         "/dev/devfs/misc/uinput",
00054         "/dev/uinput",
00055         NULL
00056 };
00057 
00058 int uinput_open_device(void) {
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]
00080 
00081 int dev_uinput_init_mouse (char *name) {
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 };
00124 
00125 int dev_uinput_init_kbd(char* name) {
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 }
00154 
00155 int dev_uinput_key(int fd, unsigned short code, int pressed) {
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 }
00165 
00166 void ptr_abs(int fd, int x, int y) {
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 }
00182 
00183 void dev_uinput_sync(int fd) {
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 }
00192 
00193 void button_click(int fd, int down) {
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 }
00214 
00215 void dev_uinput_close(int fd) {
00216         ioctl(fd, UI_DEV_DESTROY);
00217         close(fd);
00218 
00219 }