1
#include "tuntitko-common.h"
3
#include <linux/input.h>
8
#include <extnsionst.h>
15
tunProcInit (DeviceIntPtr pTun)
17
LocalDevicePtr local = (LocalDevicePtr) pTun->public.devicePrivate;
18
TunDevicePtr tun = (TunDevicePtr) local -> private;
19
TunDeviceIDInfo device_ID;
21
int smin, swidth, sheight, smax;
24
if ((fd = open (tun->input_device, O_RDONLY)) < 0)
26
ErrorF ("Tuntitko: unable to open file %s", tun->input_device);
30
tunQueryDeviceID (fd, device_ID);
31
if (device_ID [ID_BUS] != tun->device_ID [ID_BUS] ||
32
device_ID [ID_VENDOR] != tun->device_ID [ID_VENDOR] ||
33
device_ID [ID_PRODUCT] != tun->device_ID [ID_PRODUCT] ||
34
device_ID [ID_VERSION] != tun->device_ID [ID_VERSION])
36
ErrorF ("Tuntitko: something bad happend - device ID is not the same as configured device ID");
41
swidth = screenInfo.screens [0]->width;
42
sheight = screenInfo.screens [0]->height;
43
smax = (swidth < sheight) ? sheight : swidth;
45
TLOG ("xvaluators %d", tun->nof_xvaluators);
47
if (tun->nof_xvaluators)
48
{ /* register valuators */
49
TunValuatorPtr valptr;
51
if (InitValuatorClassDeviceStruct (pTun, tun->nof_xvaluators, xf86GetMotionEvents,
52
local->history_size, tun->is_absolute) == FALSE)
54
ErrorF("unable to allocate Valuator class device\n");
58
for (i = 0; i < tun->nof_xvaluators; i++)
59
if (tun->xval_to_lval_tbl [i] == NULL) /* register fake valuator */
60
InitValuatorAxisStruct (pTun, i, -1, 1, 1,0,1);
63
valptr = tun->xval_to_lval_tbl [i];
65
if (valptr -> is_absolute == FALSE &&
66
valptr->min >= valptr->max)
71
else if (i == 1) /* Y */
72
valptr->max = sheight;
73
else valptr->max = smax;
76
valptr->value = (valptr->max + valptr->min) / 2;
77
InitValuatorAxisStruct (pTun, i,
80
1,0,1 /* fake resolution */);
83
/* allocate the motion history buffer if needed */
84
xf86MotionHistoryAllocate (local);
86
valptr = tun->xval_to_lval_tbl [0];
88
tun->factorX = ((double) screenInfo.screens[0]->width)
89
/ (double) (valptr->max - valptr->min);
90
else tun->factorX = 1.0;
92
valptr = (tun->nof_xvaluators > 1)
93
? tun->xval_to_lval_tbl [1]
97
tun->factorY = ((double) screenInfo.screens[0]->height)
98
/ (double) (valptr->max - valptr->min);
100
TLOG ("FX: %g, FY: %g", tun->factorX, tun->factorY);
103
if (tun->has_proximity)
104
if (InitProximityClassDeviceStruct (pTun) == FALSE)
106
ErrorF ("unable to init proximity class device\n");
110
if (tun->max_xbutton)
112
CARD8 map [BUTTON_MAX];
114
for (i = 0; i <= tun->max_xbutton; i++)
117
if (InitButtonClassDeviceStruct (pTun, tun->max_xbutton, map) == FALSE)
119
ErrorF ("unable to init Button class device\n");
125
AssignTypeAndName (pTun, local->atom, local->name);
128
close (fd); /* let's hope that nobody change our device behind our back :*) */
129
TLOG ("[%s] Init OK.", local->name);
141
tunProc(DeviceIntPtr pTun,
144
LocalDevicePtr local = (LocalDevicePtr) pTun->public.devicePrivate;
145
TunDevicePtr tun = (TunDevicePtr) local -> private;
150
TLOG ("DEVICE_INIT (%s)", local -> name);
151
return tunProcInit (pTun);
154
TLOG ("DEVICE_ON (%s)", local -> name);
157
if ((local->fd = open (tun->input_device, O_RDONLY)) == -1)
159
TLOG ("Can not open device %s file %s",
160
local->name, tun->input_device);
165
xf86AddEnabledDevice(local);
167
AddEnabledDevice(local->fd);
169
pTun->public.on = TRUE;
171
TLOG ("DEVICE_ON (%s): OK.", local -> name);
176
TLOG ("DEVICE_%s (%s)",
177
what == DEVICE_OFF ? "OFF" : "CLOSE",
182
xf86RemoveEnabledDevice(local);
184
RemoveEnabledDevice(local->fd);
189
pTun->public.on = FALSE;
193
ErrorF("unsupported mode=%d\n", what);
200
/** Read the new events from device and enqueue them.
204
tunReadInput (LocalDevicePtr local)
206
TunDevicePtr tun = (TunDevicePtr) local -> private;
207
TunValuatorPtr valptr;
208
struct input_event ebuf [64], *ebufptr;
211
rd = read (local->fd, ebuf, sizeof (ebuf));
212
if (rd < sizeof (struct input_event))
214
TLOG ("[%s] Error reading event device :(", local->name);
218
for (i = 0, ebufptr = ebuf;
219
i < rd / sizeof (struct input_event);
222
TLOG ("[%s] Event %s ", local -> name,
223
tunGetEventName (ebufptr->type));
225
if (tun->xval_to_lval_tbl &&
226
(ebufptr->time.tv_sec - tun->last_event_time.tv_sec > 1 ||
227
((ebufptr->time.tv_sec - tun->last_event_time.tv_sec) * 1000000 +
228
(ebufptr->time.tv_usec - tun->last_event_time.tv_usec)) > tun->delta_time))
230
TLOG ("Pred postem");
231
tunPostMotionEvent (local->dev);
233
tun->last_event_time = ebufptr->time;
236
switch (ebufptr->type)
239
if (ebufptr->code < tun->first_avaluator ||
240
ebufptr->code > (tun->first_avaluator + tun->nof_avaluators) ||
241
tun->avaluators [ebufptr->code - tun->first_avaluator].lid < 0)
243
ErrorF ("Unknown evaluator %d", ebufptr->code);
246
valptr = tun->avaluators + (ebufptr->code - tun->first_avaluator);
248
valptr->value = (valptr->upsidedown)
249
? (valptr->max - ebufptr->value + valptr->min)
254
if (IS_BUTTON (ebufptr->code))
256
int lbut = ebufptr->code - tun->first_lbutton;
258
if (lbut < 0 || lbut > tun->nof_lbuttons ||
259
(tun->lbut_to_xbut_tbl [lbut] == -1))
261
ErrorF ("[%s] Unknown button %d (%s)", local->name,
262
lbut, tunGetKeyName (lbut));
266
if (tun->lbut_to_xbut_tbl [lbut] == TUN_BUTTON_PROXIMITY)
267
tunPostProximityEvent (local->dev, ebufptr->value);
268
else if (tun->lbut_to_xbut_tbl [lbut] > 0)
269
tunPostButtonEvent (local->dev, tun->lbut_to_xbut_tbl [lbut], ebufptr->value);
274
TLOG ("[%s] Unhandled event %d (%s)",
275
local->name, ebufptr->type, tunGetEventName (ebufptr->type));
280
TunDevicePtr tun = (TunDevicePtr) local -> private;
281
TunAbsValuatorPtr absptr;
282
struct input_event ebuf [64], *ebufptr;
286
rd = read (local->fd, ebuf, sizeof (struct input_event) * 64);
287
if (rd < sizeof (struct input_event))
289
TLOG ("[%s] Error reading :(", local->name);
293
for (i = 0; i < rd / sizeof (struct input_event); i++)
295
TLOG ("[%s] Event %s )", local -> name,
296
tun_names_events [ebuf[i].type]? tun_names_events [ebuf[i].type] : "?");
300
switch (ebufptr -> type)
303
if (IS_BUTTON (ebufptr->code))
308
lbutton = KEY_TO_BUTTON (ebufptr->code);
310
if (lbutton < tun->nof_alloc_buttons)
311
button = tun->buttons_trans_tbl [lbutton];
316
tunPostButtonEvent (local->dev, tun, button, ebufptr->value);
320
xdev = tun->abs_trans_tbl [ebufptr->code];
322
break; /* ignored... */
324
absptr = tun->abs_valuators + xdev;
325
value = absptr->upsidedown
326
? (absptr->max - ebufptr->value)
330
if (tun->abs_bitmap [xdev] == 1) /* already got it... */
332
tunPostMotionEvent (local->dev, tun);
333
tun->abs_bitmap [xdev] = 1;
335
tun->abs_valuators [xdev].value = value;
339
tun->abs_valuators [xdev].value = value;
340
if (++(tun->nof_ev_abs) >= tun->nof_abs)
341
tunPostMotionEvent (local->dev,tun);
352
tunClose (LocalDevicePtr local)
354
TLOG ("Close (%s)", local -> name);
359
* \todo tunChangeControl: Figure out what does it mean.
362
tunChangeControl(LocalDevicePtr local, xDeviceCtl *control)
364
xDeviceResolutionCtl *res;
366
TLOG ("ChangeControl (%s)", local -> name);
367
res = (xDeviceResolutionCtl *)control;
369
if ((control->control != DEVICE_RESOLUTION) ||
370
(res->num_valuators < 1))
379
tunControlProc(LocalDevicePtr local,
382
TLOG ("ControlProc (%s)", local -> name);
389
tunSwitchMode(ClientPtr client,
393
LocalDevicePtr local = (LocalDevicePtr)dev->public.devicePrivate;
394
TLOG ("SwitchMode (%s)", local -> name);
399
/** Convert valuators to X and Y.
403
tunConvert(LocalDevicePtr local,
415
TunDevicePtr tun = (TunDevicePtr) local->private;
417
if (tun -> nof_xvaluators < 2)
422
TunValuatorPtr valptr;
424
valptr = tun->xval_to_lval_tbl [0];
426
tun->factorX = ((double) screenInfo.screens[0]->width)
427
/ (double) (valptr->max - valptr->min);
428
else tun->factorX = 1.0;
430
valptr = (tun->nof_xvaluators > 1)
431
? tun->xval_to_lval_tbl [1]
435
tun->factorY = ((double) screenInfo.screens[0]->height)
436
/ (double) (valptr->max - valptr->min);
442
*x = v0 * tun->factorX;
443
*y = v1 * tun->factorY;
445
TLOG ("Convert (%s) (%d %d), NEW (%d %d)", local -> name, v0, v1, *x, *y);
450
/** Convert X and Y to valuators.
454
tunReverseConvert(LocalDevicePtr local,
459
TLOG ("ReverseConvert (%s)", local -> name);
464
tunAllocate (int id, char *name)
466
LocalDevicePtr local = (LocalDevicePtr) xalloc (sizeof (LocalDeviceRec));
467
TunDevicePtr priv = (TunDevicePtr) xalloc (sizeof (TunDeviceRec));
469
TLOG ("Allocating linput%d (name %s)", id, name);
471
local->name = strdup (name);
472
local->flags = XI86_NO_OPEN_ON_INIT;
474
local->device_config = tunConfig; /* parsovatko */
475
local->device_control = tunProc; /* DEVICE_CLOSE apod */
476
local->read_input = tunReadInput; /* handluje cteni ze souboru */
477
local->control_proc = tunChangeControl;
478
local->close_proc = tunClose; /* pozavira soubory */
479
local->control_proc = tunControlProc; /* ??? */
480
local->switch_mode = tunSwitchMode; /* swica relativni a absolutni flazek... */
481
local->conversion_proc = tunConvert; /* convert valuators to X and Y */
482
local->reverse_conversion_proc = tunReverseConvert; /* convert valuators to X and Y */
487
local->private = priv; /* FIXME !!! */
488
local->private_flags = 0;
490
local->history_size = 0;
494
priv->input_device = (char*) xalloc (TUN_DEFAULT_INPUT_PATH_LENGTH);
495
sprintf (priv->input_device, TUN_DEFAULT_INPUT_PATH, id);
500
priv->last_event_time.tv_sec = 0;
501
priv->last_event_time.tv_usec = 0;
503
priv->delta_time = 50;
504
priv->last_valuator = -1;
505
priv->num_of_recieved_valuators = 0;
507
priv->nof_avaluators = 0;
508
priv->first_avaluator = 0;
509
priv->avaluators = NULL;
511
priv->nof_rvaluators = 0;
512
priv->first_rvaluator = 0;
513
priv->rvaluators = NULL;
515
priv->xval_to_lval_tbl = NULL;
517
priv->nof_lbuttons = 0;
518
priv->first_lbutton = 0;
519
priv->lbut_to_xbut_tbl = NULL;
521
priv->is_absolute = 1;
522
priv->has_proximity = 0;
523
priv->has_mouse_wheel_hack = 0;