11/*
22 libsuinput - A set of uinput helper functions
3- Copyright © 2010 Tuomas Jorma Juhani Räsänen <tuomas.j.j.rasanen@tjjr.fi>
3+ Copyright © 2011 Tuomas Jorma Juhani Räsänen <tuomas.j.j.rasanen@tjjr.fi>
44
55 This program is free software: you can redistribute it and/or modify
66 it under the terms of the GNU General Public License as published by
1515 You should have received a copy of the GNU General Public License
1616 along with this program. If not, see <http://www.gnu.org/licenses/>.
1717*/
18+
1819#include <errno.h>
1920#include <string.h>
2021#include <fcntl.h>
2122#include <unistd.h>
22- #include <limits.h>
2323#include <stdlib.h>
2424
2525#include <linux/limits.h>
2828
2929#include "suinput.h"
3030
31- int suinput_write_event (int uinput_fd , const struct input_event * event )
31+ int suinput_write_event (int uinput_fd , const struct input_event * event_p )
3232{
3333 ssize_t bytes ;
34- bytes = write (uinput_fd , event , sizeof (struct input_event ));
34+ bytes = write (uinput_fd , event_p , sizeof (struct input_event ));
3535 if (bytes != sizeof (struct input_event ))
3636 return -1 ;
3737 return 0 ;
@@ -54,13 +54,12 @@ int suinput_syn(int uinput_fd)
5454 return suinput_write (uinput_fd , EV_SYN , SYN_REPORT , 0 );
5555}
5656
57- const char * suinput_get_uinput_path (void )
57+ static char * suinput_get_uinput_path (void )
5858{
59- static char uinput_devnode [PATH_MAX + 1 ];
6059 struct udev * udev ;
6160 struct udev_device * udev_dev ;
6261 const char * devnode ;
63- const char * retval = NULL ;
62+ char * retval = NULL ;
6463 int orig_errno ;
6564
6665 if ((udev = udev_new ()) == NULL )
@@ -73,14 +72,10 @@ const char *suinput_get_uinput_path(void)
7372 if ((devnode = udev_device_get_devnode (udev_dev )) == NULL )
7473 goto out ;
7574
76- /* I'm on very defensive mood.. it's due the ignorance. :P */
77- if (strlen (devnode ) > PATH_MAX ) {
78- errno = ENAMETOOLONG ;
75+ if ((retval = malloc (strlen (devnode ) + 1 )) == NULL )
7976 goto out ;
80- }
8177
82- strncpy (uinput_devnode , devnode , PATH_MAX );
83- retval = uinput_devnode ;
78+ strcpy (retval , devnode );
8479 out :
8580 orig_errno = errno ;
8681 udev_device_unref (udev_dev );
@@ -92,46 +87,33 @@ const char *suinput_get_uinput_path(void)
9287int suinput_open (void )
9388{
9489 int uinput_fd ;
95- const char * uinput_devnode ;
96-
97- if ((uinput_devnode = suinput_get_uinput_path ()) == NULL )
98- return -1 ;
90+ char * uinput_path ;
9991
100- if ((uinput_fd = open ( uinput_devnode , O_WRONLY | O_NONBLOCK )) == -1 )
92+ if ((uinput_path = suinput_get_uinput_path ( )) == NULL )
10193 return -1 ;
10294
95+ uinput_fd = open (uinput_path , O_WRONLY | O_NONBLOCK );
96+ free (uinput_path );
10397 return uinput_fd ;
10498}
10599
106- int suinput_create (int uinput_fd , const struct uinput_user_dev * user_dev )
100+ int suinput_create (int uinput_fd , const struct uinput_user_dev * user_dev_p )
107101{
108102 ssize_t bytes ;
109103
110- bytes = write (uinput_fd , user_dev , sizeof (struct uinput_user_dev ));
104+ bytes = write (uinput_fd , user_dev_p , sizeof (struct uinput_user_dev ));
111105 if (bytes != sizeof (struct uinput_user_dev ))
112106 return -1 ;
113107
114108 if (ioctl (uinput_fd , UI_DEV_CREATE ) == -1 )
115109 return -1 ;
116110
117- /**
118- This magic sleep needs to be taken under X because of the way
119- how X assigns handlers for new devices. It goes somehow like
120- this:
121-
122- 1. Kernel creates an evdev device.
123-
124- 2. A dbus signal "DeviceAdded" is sent.
125-
126- 3. X's handler assigner catches the dbus event and assigns a
127- handler for the new device.
128-
129- Now the "problem" is in the asynchronous nature of this whole
130- process. Kernel is totally happy once it has created the evdev
131- device, but X cannot assign a handler for the new device before
132- it receives the dbus signal. Without this delay, kernel would
133- generate events before a handler is assigned. Perhaps a better
134- way would be to listen to the dbus?
111+ /*
112+ This magic sleep needs to be taken under X due to asynchronous
113+ nature of X's device handler assignement. Without this delay,
114+ kernel would generate events before X has assigned a handler
115+ for the newly created device. Perhaps a better way would be to
116+ wait for that event somehow?
135117 */
136118 if (getenv ("DISPLAY" ))
137119 sleep (1 );
@@ -154,7 +136,7 @@ int suinput_destroy(int uinput_fd)
154136}
155137
156138int suinput_set_capabilities (int uinput_fd , uint16_t ev_type ,
157- const int * ev_codes , size_t ev_codes_len )
139+ const int * ev_code_v , size_t ev_code_c )
158140{
159141 size_t i ;
160142 unsigned long io ;
@@ -191,8 +173,8 @@ int suinput_set_capabilities(int uinput_fd, uint16_t ev_type,
191173 return -2 ;
192174 }
193175
194- for (i = 0 ; i < ev_codes_len ; ++ i ) {
195- int ev_code = ev_codes [i ];
176+ for (i = 0 ; i < ev_code_c ; ++ i ) {
177+ int ev_code = ev_code_v [i ];
196178 if (ioctl (uinput_fd , io , ev_code ) == -1 )
197179 return -1 ;
198180 }
0 commit comments