eloop: move prefix to "ev_" instead of "kmscon_"
[platform/upstream/kmscon.git] / src / input.h
1 /*
2  * kmscon - udev input hotplug and evdev handling
3  *
4  * Copyright (c) 2011 Ran Benita <ran234@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 /*
27  * This module provides an input object which can deliver all useful input
28  * events to the program.
29  *
30  * Its use should be as simple as the following (but also see below):
31  * - Create a new input object.
32  * - Provide a callback function to receive the events.
33  * - Connect the input object to a ev_eloop.
34  * - Wake up the input object to begin receiving input events through the
35  *   event loop.
36  *
37  * A few things to note:
38  * - This module uses evdev for input, and reads from input devices directly.
39  *   This requires root privileges; waking up the input object will fail
40  *   without them.
41  * - evdev  has no inhert notion of "focus" like tty input. In other words,
42  *   it will deliver input events whether they are intended for the program
43  *   or not. This may also pose a security risk. Therefore, make sure to put
44  *   the object to sleep when the program is not active, for example by
45  *   reacting to VT changes.
46  */
47
48 #ifndef KMSCON_INPUT_H
49 #define KMSCON_INPUT_H
50
51 #include <inttypes.h>
52 #include <limits.h>
53 #include <stdbool.h>
54 #include "eloop.h"
55
56 struct kmscon_input;
57
58 enum kmscon_modifier {
59         KMSCON_SHIFT_MASK       = (1 << 0),
60         KMSCON_LOCK_MASK        = (1 << 1),
61         KMSCON_CONTROL_MASK     = (1 << 2),
62         KMSCON_MOD1_MASK        = (1 << 3),
63         KMSCON_MOD2_MASK        = (1 << 4),
64         KMSCON_MOD3_MASK        = (1 << 5),
65         KMSCON_MOD4_MASK        = (1 << 6),
66         KMSCON_MOD5_MASK        = (1 << 7),
67 };
68
69 #define KMSCON_INPUT_INVALID 0xffffffff
70
71 struct kmscon_input_event {
72         uint16_t keycode;  /* linux keycode - KEY_* - linux/input.h */
73         uint32_t keysym;   /* X keysym - XK_* - X11/keysym.h */
74         unsigned int mods; /* active modifiers - kmscon_modifier mask */
75         uint32_t unicode;  /* UCS-4 unicode value or KMSCON_INPUT_INVALID */
76 };
77
78 typedef void (*kmscon_input_cb) (struct kmscon_input *input,
79                                 struct kmscon_input_event *ev, void *data);
80
81 int kmscon_input_new(struct kmscon_input **out);
82 void kmscon_input_ref(struct kmscon_input *input);
83 void kmscon_input_unref(struct kmscon_input *input);
84
85 int kmscon_input_connect_eloop(struct kmscon_input *input,
86                 struct ev_eloop *eloop, kmscon_input_cb cb, void *data);
87 void kmscon_input_disconnect_eloop(struct kmscon_input *input);
88
89 void kmscon_input_sleep(struct kmscon_input *input);
90 void kmscon_input_wake_up(struct kmscon_input *input);
91 bool kmscon_input_is_asleep(struct kmscon_input *input);
92
93 /* Querying the results of evdev ioctl's. Also used by kbd backends. */
94 static inline bool kmscon_evdev_bit_is_set(const unsigned long *array, int bit)
95 {
96         return !!(array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT)));
97 }
98
99 #endif /* KMSCON_INPUT_H */