aboutsummaryrefslogtreecommitdiff
path: root/src/evdev_btn.c
blob: a0e0dad818f1daf5696674f147e67fda635281f1 (plain)
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
/*
 * Copyright © 2006 Zephaniah E. Hull
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Soft-
 * ware"), to deal in the Software without restriction, including without
 * limitation the rights to use, copy, modify, merge, publish, distribute,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, provided that the above copyright
 * notice(s) and this permission notice appear in all copies of the Soft-
 * ware and that both the above copyright notice(s) and this permission
 * notice appear in supporting documentation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
 * MANCE OF THIS SOFTWARE.
 *
 * Except as contained in this notice, the name of a copyright holder shall
 * not be used in advertising or otherwise to promote the sale, use or
 * other dealings in this Software without prior written authorization of
 * the copyright holder.
 *
 * Author:  Zephaniah E. Hull (warp@aehallh.com)
 */
 
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
 
#include <X11/keysym.h>
#include <X11/XF86keysym.h>
#include <X11/extensions/XIproto.h>
 
#include <linux/input.h>
 
#include <misc.h>
#include <xf86.h>
#include <xf86str.h>
#include <xf86_OSproc.h>
#include <xf86Xinput.h>
#include <exevents.h>
#include <mipointer.h>
 
#include <xf86Module.h>
 
#include "evdev.h"
 
#define ArrayLength(a) (sizeof(a) / (sizeof((a)[0])))
 
#define BITS_PER_LONG (sizeof(long) * 8)
#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
#define OFF(x)  ((x)%BITS_PER_LONG)
#define LONG(x) ((x)/BITS_PER_LONG)
#define TestBit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
 
void
EvdevBtnPostFakeClicks(InputInfoPtr pInfo, int button, int count)
{
    int i;
 
    for (i = 0; i < count; i++) {
        xf86PostButtonEvent(pInfo->dev, 0, button, 100);
        xf86PostButtonEvent(pInfo->dev, 0, button, 000);
    }
}
 
int
EvdevBtnInit (DeviceIntPtr device)
{
    InputInfoPtr pInfo = device->public.devicePrivate;
    evdevDevicePtr pEvdev = pInfo->private;
    CARD8 *map;
    int i;
 
    if (!pEvdev->state.btn)
return Success;
 
    map = Xcalloc (sizeof (CARD8) * (pEvdev->state.btn->buttons + 1));
 
    for (i = 0; i <= pEvdev->state.btn->buttons; i++)
        map[i] = i;
 
    xf86Msg(X_ERROR, "%s (%d): Registering %d buttons.\n", __FILE__, __LINE__,
    pEvdev->state.btn->buttons);
    if (!InitButtonClassDeviceStruct (device, pEvdev->state.btn->buttons, map)) {
pEvdev->state.btn->buttons = 0;
 
        return !Success;
    }
 
    Xfree (map);
 
    return Success;
}
 
int
EvdevBtnOn (DeviceIntPtr device)
{
    InputInfoPtr pInfo = device->public.devicePrivate;
    evdevDevicePtr pEvdev = pInfo->private;
    int i, blocked;
 
    if (!pEvdev->state.btn)
return Success;
 
    blocked = xf86BlockSIGIO ();
    for (i = 1; i <= pEvdev->state.btn->buttons; i++)
xf86PostButtonEvent (device, 0, i, 000);
    xf86UnblockSIGIO (blocked);
 
    return Success;
}
 
int
EvdevBtnOff (DeviceIntPtr device)
{
    return Success;
}
 
/*
 * Warning, evil lives here.
 */
static void
EvdevBtnCalcRemap (InputInfoPtr pInfo)
{
    evdevDevicePtr pEvdev = pInfo->private;
    evdevStatePtr state = &pEvdev->state;
    int i, j, base, clear, fake;
 
    for (i = 0, base = 1, fake = 0; i < pEvdev->state.btn->real_buttons; i++) {
if (state->rel) {
    do {
clear = 1;
for (j = 0; j < REL_MAX; j++) {
    if (state->rel->btnMap[j][0] == (i + base)) {
base++;
clear = 0;
break;
    }
    if (state->rel->btnMap[j][1] == (i + base)) {
base++;
clear = 0;
break;
    }
}
    } while (!clear);
}
 
if (!fake && base != 1)
    fake = i;
 
state->btn->buttons = state->btn->map[i] = i + base;
    }
 
    if (state->btn->real_buttons >= 3 && (!fake || fake >= 3)) {
base = state->btn->map[1];
state->btn->map[1] = state->btn->map[2];
state->btn->map[2] = base;
    }
 
    if (state->rel) {
for (i = 0; i < REL_MAX; i++) {
    if (state->rel->btnMap[i][0] > state->btn->buttons)
state->btn->buttons = state->rel->btnMap[i][0];
    if (state->rel->btnMap[i][1] > state->btn->buttons)
state->btn->buttons = state->rel->btnMap[i][1];
}
    }
}
 
 
int
EvdevBtnNew(InputInfoPtr pInfo)
{
    evdevDevicePtr pEvdev = pInfo->private;
    evdevStatePtr state = &pEvdev->state;
    int i, bit;
 
    state->btn = Xcalloc (sizeof (evdevBtnRec));
 
    for (i = BTN_MISC; i < (KEY_OK - 1); i++)
if (TestBit (i, pEvdev->bits.key)) {
    bit = i;
    if ((bit >= BTN_MOUSE) && (bit < BTN_JOYSTICK)) {
bit -= BTN_MOUSE - BTN_MISC;
    } else if ((bit >= BTN_MISC) && (bit < BTN_MOUSE)) {
bit += BTN_MOUSE - BTN_MISC;
    }
    bit -= BTN_MISC;
    state->btn->real_buttons = bit + 1;
}
 
    if (state->btn->real_buttons)
        xf86Msg(X_INFO, "%s: Found %d mouse buttons\n", pInfo->name, state->btn->real_buttons);
 
    EvdevBtnCalcRemap (pInfo);
 
    if (state->btn->buttons)
xf86Msg(X_INFO, "%s: Configured %d mouse buttons\n", pInfo->name, state->btn->buttons);
    else {
Xfree (state->btn);
state->btn = NULL;
return !Success;
    }
 
    pInfo->flags |= XI86_SEND_DRAG_EVENTS | XI86_CONFIGURED;
    /*
     * FIXME: Mouse may not be accurate.
     * Check buttons to see if we're actually a joystick or something.
     */
    pInfo->type_name = XI_MOUSE;
 
    return Success;
}
 
void
EvdevBtnProcess (InputInfoPtr pInfo, struct input_event *ev)
{
    evdevDevicePtr pEvdev = pInfo->private;
    evdevStatePtr state = &pEvdev->state;
    int button;
 
    if (!state->btn)
return;
 
    button = ev->code - BTN_MISC;
 
    if ((ev->code >= BTN_MOUSE) && (ev->code < BTN_JOYSTICK)) {
button -= BTN_MOUSE - BTN_MISC;
    } else if ((ev->code >= BTN_MISC) && (ev->code < BTN_MOUSE)) {
button += BTN_MOUSE - BTN_MISC;
    }
 
    if (state->btn->state[button])
*state->btn->state[button] = ev->value;
 
    button = state->btn->map[button];
    xf86PostButtonEvent (pInfo->dev, 0, button, ev->value, 00);
}