1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/* $Id$
******************************************************************************
Linux evdev parser
Copyright (C) 2000 Marcus Sundberg [marcus@ggi-project.org]
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************
*/
#include "config.h"
#include "linux_evdev.h"
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
static gii_event_mask
dispatch_pmove(struct gii_input *inp, struct input_event *event,
gii_event *ev)
{
_giiEventBlank(ev, sizeof(gii_pmove_event));
switch (event->code) {
case REL_X:
ev->pmove.x = event->value;
break;
case REL_Y:
ev->pmove.y = event->value;
break;
case REL_Z:
ev->pmove.z = event->value;
break;
case REL_WHEEL:
ev->pmove.wheel = event->value;
break;
default:
return 0;
}
ev->any.size = sizeof(gii_pmove_event);
ev->any.type = evPtrRelative;
ev->any.origin = inp->origin;
return emPtrRelative;
}
static gii_event_mask
dispatch_pbutton(struct gii_input *inp, struct input_event *event,
gii_event *ev)
{
int ret;
_giiEventBlank(ev, sizeof(gii_pbutton_event));
switch (event->value) {
case 0:
ev->any.type = evPtrButtonRelease;
ret = emPtrButtonRelease;
break;
case 1:
ev->any.type = evPtrButtonPress;
ret = emPtrButtonPress;
break;
case 2:
/* Should not happen... */
ev->any.type = evPtrButtonPress;
ret = emPtrButtonPress;
break;
default:
return 0;
}
ev->any.size = sizeof(gii_pbutton_event);
ev->any.origin = inp->origin;
ev->pbutton.button = event->code + 1 - BTN_MOUSE;
return ret;
}
static gii_event_mask
dispatch_key(struct gii_input *inp, struct input_event *event, gii_event *ev)
{
int ret;
_giiEventBlank(ev, sizeof(gii_key_event));
switch (event->value) {
case 0:
ev->any.type = evKeyRelease;
ret = emKeyRelease;
break;
case 1:
ev->any.type = evKeyPress;
ret = emKeyPress;
break;
case 2:
ev->any.type = evKeyRepeat;
ret = emKeyRepeat;
break;
default:
return 0;
}
ev->any.size = sizeof(gii_key_event);
ev->any.origin = inp->origin;
ev->key.modifiers = 0;
ev->key.button = event->code;
if (event->code < 0x100) {
/* Keyboard key. */
/* Attention: gcc bug:
* passing arg 2 of `GII_levdev_key2label' with different
* width due to prototype
*/
ev->key.label = GII_levdev_key2label(inp, event->code);
ev->key.sym = ev->key.label;
} else {
/* Other button. */
ev->key.label = ev->key.sym = GIIK_VOID;
}
return ret;
}
static gii_event_mask
dispatch_rel(struct gii_input *inp, struct input_event *event, gii_event *ev)
{
_giiEventBlank(ev, sizeof(gii_val_event));
ev->any.type = evValRelative;
ev->any.size = sizeof(gii_val_event);
ev->any.origin = inp->origin;
ev->val.first = event->code;
ev->val.count = 1;
ev->val.value[0] = event->value;
return emValRelative;
}
static gii_event_mask
dispatch_abs(struct gii_input *inp, struct input_event *event, gii_event *ev)
{
_giiEventBlank(ev, sizeof(gii_val_event));
ev->any.type = evValAbsolute;
ev->any.size = sizeof(gii_val_event);
ev->any.origin = inp->origin;
ev->val.first = event->code;
ev->val.count = 1;
ev->val.value[0] = event->value;
return emValAbsolute;
}
typedef gii_event_mask (func_dispatcher)(struct gii_input *inp,
struct input_event *event,
gii_event *ev);
static gii_event_mask
dispatch_event(struct gii_input *inp, struct input_event *event)
{
func_dispatcher *dispatcher;
gii_event ev;
int ret;
switch (event->type) {
case EV_KEY:
if (event->code >= BTN_MOUSE && event->code < BTN_JOYSTICK) {
/* Pointer button */
dispatcher = dispatch_pbutton;
} else {
/* Other button/key */
dispatcher = dispatch_key;
}
break;
case EV_REL:
switch (event->code) {
case REL_X:
case REL_Y:
case REL_Z:
case REL_WHEEL:
/* Pointer move */
dispatcher = dispatch_pmove;
break;
default:
dispatcher = dispatch_rel;
break;
}
break;
case EV_ABS:
dispatcher = dispatch_abs;
break;
#ifdef EV_SYN
case EV_SYN:
#endif
#ifdef EV_MSC
case EV_MSC:
#endif
#ifdef EV_LED
case EV_LED:
#endif
#ifdef EV_SND
case EV_SND:
#endif
#ifdef EV_REP
case EV_REP:
#endif
#ifdef EV_FF
case EV_FF:
#endif
#ifdef EV_PWR
case EV_PWR:
#endif
#ifdef EV_FF_STATUS
case EV_FF_STATUS:
#endif
default:
return 0;
}
ret = dispatcher(inp, event, &ev);
if (ret) _giiEvQueueAdd(inp, &ev);
return ret;
}
#define MAX_EVENTS 64
gii_event_mask
GII_levdev_poll(struct gii_input *inp, void *arg)
{
gii_levdev_priv *priv = inp->priv;
struct input_event evbuf[MAX_EVENTS];
unsigned int i;
int read_len;
int ret;
DPRINT_EVENTS("GII_levdev_poll(%p, %p) called\n", inp, arg);
if (priv->eof) {
/* End-of-file, don't do any polling */
return 0;
}
if (arg == NULL) {
fd_set fds = inp->fdset;
struct timeval tv = { 0, 0 };
if (select(inp->maxfd, &fds, NULL, NULL, &tv) <= 0) {
return 0;
}
} else {
if (! FD_ISSET(priv->fd, ((fd_set*)arg))) {
/* Nothing to read on our fd */
DPRINT_EVENTS("GII_levdev_poll: dummypoll\n");
return 0;
}
}
/* The evdev docs guarantee that only complete events will be read. */
read_len = read(priv->fd, evbuf,
MAX_EVENTS*sizeof(struct input_event));
if (read_len <= 0) {
if (read_len == 0) {
/* End-of-file occured */
priv->eof = 1;
DPRINT_EVENTS("Levdev: EOF occured on fd: %d\n",
priv->fd);
return 0;
}
/* Error, we try again next time */
if (errno == EAGAIN) return 0; /* Don't print warning here */
perror("Levdev: Error reading events");
return 0;
}
/* Dispatch all events and return queued events. */
ret = 0;
for (i = 0; i < read_len / sizeof(struct input_event); i++) {
ret |= dispatch_event(inp, evbuf+i);
}
return ret;
}
|