tizen 2.4 release
[framework/uifw/libevdev.git] / libevdev / libevdev-uinput.h
1 /*
2  * Copyright © 2013 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #ifndef LIBEVDEV_UINPUT_H
24 #define LIBEVDEV_UINPUT_H
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #include <libevdev/libevdev.h>
31
32 struct libevdev_uinput;
33
34 /**
35  * @defgroup uinput uinput device creation
36  *
37  * Creation of uinput devices based on existing libevdev devices. These functions
38  * help to create uinput devices that emulate libevdev devices. In the simplest
39  * form it serves to duplicate an existing device:
40  *
41  @code
42  int err;
43  int new_fd;
44  struct libevdev *dev;
45  struct libevdev_uinput *uidev;
46  struct input_event ev[2];
47
48  err = libevdev_new_from_fd(&dev, fd);
49  if (err != 0)
50      return err;
51
52  uifd = open("/dev/uinput", O_RDWR);
53  if (uidev < 0)
54      return -errno;
55
56  err = libevdev_uinput_create_from_device(dev, uifd, &uidev);
57  if (err != 0)
58      return err;
59
60  // post a REL_X event
61  err = libevdev_uinput_write_event(uidev, EV_REL, REL_X, -1);
62  if (err != 0)
63      return err;
64  libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);
65  if (err != 0)
66      return err;
67
68  libevdev_uinput_destroy(uidev);
69  close(uifd);
70
71  @endcode
72  *
73  * Alternatively, a device can be constructed from scratch:
74  *
75  @code
76  int err;
77  struct libevdev *dev;
78  struct libevdev_uinput *uidev;
79
80  dev = libevdev_new();
81  libevdev_set_name(dev, "test device");
82  libevdev_enable_event_type(dev, EV_REL);
83  libevdev_enable_event_code(dev, EV_REL, REL_X);
84  libevdev_enable_event_code(dev, EV_REL, REL_Y);
85  libevdev_enable_event_type(dev, EV_KEY);
86  libevdev_enable_event_code(dev, EV_KEY, BTN_LEFT);
87  libevdev_enable_event_code(dev, EV_KEY, BTN_MIDDLE);
88  libevdev_enable_event_code(dev, EV_KEY, BTN_RIGHT);
89
90  err = libevdev_uinput_create_from_device(dev,
91                                           LIBEVDEV_UINPUT_OPEN_MANAGED,
92                                           &uidev);
93  if (err != 0)
94      return err;
95
96  // ... do something ...
97
98  libevdev_uinput_destroy(uidev);
99
100  @endcode
101  */
102
103 enum libevdev_uinput_open_mode {
104         /* intentionally -2 to avoid to avoid code like the below from accidentally working:
105                 fd = open("/dev/uinput", O_RDWR); // fails, fd is -1
106                 libevdev_uinput_create_from_device(dev, fd, &uidev); // may hide the error */
107         LIBEVDEV_UINPUT_OPEN_MANAGED = -2  /**< let libevdev open and close @c /dev/uinput */
108 };
109
110 /**
111  * @ingroup uinput
112  *
113  * Create a uinput device based on the given libevdev device. The uinput device
114  * will be an exact copy of the libevdev device, minus the bits that uinput doesn't
115  * allow to be set.
116  *
117  * If uinput_fd is @ref LIBEVDEV_UINPUT_OPEN_MANAGED, libevdev_uinput_create_from_device()
118  * will open @c /dev/uinput in read/write mode and manage the file descriptor.
119  * Otherwise, uinput_fd must be opened by the caller and opened with the
120  * appropriate permissions.
121  *
122  * The device's lifetime is tied to the uinput file descriptor, closing it will
123  * destroy the uinput device. You should call libevdev_uinput_destroy() before
124  * closing the file descriptor to free allocated resources.
125  * A file descriptor can only create one uinput device at a time; the second device
126  * will fail with -EINVAL.
127  *
128  * You don't need to keep the file descriptor variable around,
129  * libevdev_uinput_get_fd() will return it when needed.
130  *
131  * @note Due to limitations in the uinput kernel module, REP_DELAY and
132  * REP_PERIOD will default to the kernel defaults, not to the ones set in the
133  * source device.
134  *
135  * @param dev The device to duplicate
136  * @param uinput_fd @ref LIBEVDEV_UINPUT_OPEN_MANAGED or a file descriptor to @c /dev/uinput,
137  * @param[out] uinput_dev The newly created libevdev device.
138  *
139  * @return 0 on success or a negative errno on failure. On failure, the value of
140  * uinput_dev is unmodified.
141  *
142  * @see libevdev_uinput_destroy
143  */
144 int libevdev_uinput_create_from_device(const struct libevdev *dev,
145                                        int uinput_fd,
146                                        struct libevdev_uinput **uinput_dev);
147
148 /**
149  * @ingroup uinput
150  *
151  * Destroy a previously created uinput device and free associated memory.
152  *
153  * If the device was opened with @ref LIBEVDEV_UINPUT_OPEN_MANAGED,
154  * libevdev_uinput_destroy() also closes the file descriptor. Otherwise, the
155  * fd is left as-is and must be closed by the caller.
156  *
157  * @param uinput_dev A previously created uinput device.
158  */
159 void libevdev_uinput_destroy(struct libevdev_uinput *uinput_dev);
160
161 /**
162  * @ingroup uinput
163  *
164  * Return the file descriptor used to create this uinput device. This is the
165  * fd pointing to <strong>/dev/uinput</strong>. This file descriptor may be used to write
166  * events that are emitted by the uinput device.
167  * Closing this file descriptor will destroy the uinput device, you should
168  * call libevdev_uinput_destroy() first to free allocated resources.
169  *
170  * @param uinput_dev A previously created uinput device.
171  *
172  * @return The file descriptor used to create this device
173  */
174 int libevdev_uinput_get_fd(const struct libevdev_uinput *uinput_dev);
175
176 /**
177  * @ingroup uinput
178  *
179  * Return the syspath representing this uinput device.
180  * At the time of writing, the uinput kernel device does not
181  * provide a way to get the syspath directly through uinput so libevdev must guess.
182  * In some cases libevdev is unable to derive the syspath. If the running kernel
183  * supports the UI_GET_SYSNAME ioctl, the syspath is retrieved through that and will
184  * be reliable and not be NULL. The UI_GET_SYSNAME ioctl is currently
185  * scheduled for 3.15.
186  *
187  * @note This function may return NULL. libevdev currently uses ctime and
188  * the device name to guess devices. To avoid false positives, wait at least
189  * wait at least 1.5s between creating devices that have the same name.
190  * @param uinput_dev A previously created uinput device.
191  * @return The syspath for this device, including the preceding /sys
192  *
193  * @see libevdev_uinput_get_devnode
194  */
195 const char*libevdev_uinput_get_syspath(struct libevdev_uinput *uinput_dev);
196
197 /**
198  * @ingroup uinput
199  *
200  * Return the device node representing this uinput device.
201  *
202  * This relies on libevdev_uinput_get_syspath() to provide a valid syspath.
203  * See libevdev_uinput_get_syspath() for more details.
204  *
205  * @note This function may return NULL. libevdev currently has to guess the
206  * syspath and the device node. See libevdev_uinput_get_syspath() for details.
207  * @param uinput_dev A previously created uinput device.
208  * @return The device node for this device, in the form of /dev/input/eventN
209  *
210  * @see libevdev_uinput_get_syspath
211  */
212 const char* libevdev_uinput_get_devnode(struct libevdev_uinput *uinput_dev);
213
214 /**
215  * @ingroup uinput
216  *
217  * Post an event through the uinput device. It is the caller's responsibility
218  * that any event sequence is terminated with an EV_SYN/SYN_REPORT/0 event.
219  * Otherwise, listeners on the device node will not see the events until the
220  * next EV_SYN event is posted.
221  *
222  * @param uinput_dev A previously created uinput device.
223  * @param type Event type (EV_ABS, EV_REL, etc.)
224  * @param code Event code (ABS_X, REL_Y, etc.)
225  * @param value The event value
226  * @return 0 on success or a negative errno on error
227  */
228 int libevdev_uinput_write_event(const struct libevdev_uinput *uinput_dev,
229                                 unsigned int type,
230                                 unsigned int code,
231                                 int value);
232 #ifdef __cplusplus
233 }
234 #endif
235
236 #endif /* LIBEVDEV_UINPUT_H */