summaryrefslogtreecommitdiff
path: root/src/draglock.c
blob: 6157caedc4abe877f91a26ff0d01c57d5f549f1f (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
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
313
314
315
316
317
318
319
320
321
/*
 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
 * Copyright 1993 by David Dawes <dawes@xfree86.org>
 * Copyright 2002 by SuSE Linux AG, Author: Egbert Eich
 * Copyright 1994-2002 by The XFree86 Project, Inc.
 * Copyright 2002 by Paul Elliott
 * (Ported from xf86-input-mouse, above copyrights taken from there)
 * Copyright © 2008 University of South Australia
 * Copyright 2008 by Chris Salch
 * Copyright 2008 Red Hat, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of the authors
 * not be used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  The authors make no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL 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 PERFORMANCE OF THIS SOFTWARE.
 *
 */

/* Draglock code */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <xf86.h>
#include <xf86Xinput.h>
#include <X11/Xatom.h>
#include <exevents.h>

#include <evdev-properties.h>
#include "evdev.h"

#ifdef HAVE_PROPERTIES
static Atom prop_dlock     = 0; /* Drag lock buttons. */
#endif

void EvdevDragLockLockButton(InputInfoPtr pInfo, unsigned int button);


/* Setup and configuration code */
void
EvdevDragLockPreInit(InputInfoPtr pInfo)
{
    EvdevPtr pEvdev = (EvdevPtr)pInfo->private;
    char *option_string = NULL;
    int meta_button = 0;
    int lock_button = 0;
    char *next_num = NULL;
    char *end_str = NULL;
    BOOL pairs = FALSE;

    option_string = xf86CheckStrOption(pInfo->options, "DragLockButtons",NULL);

    if (!option_string)
        return;

    next_num = option_string;

    /* Loop until we hit the end of our option string */
    while (next_num != NULL) {
        lock_button = 0;
        meta_button = strtol(next_num, &end_str, 10);

        /* check to see if we found anything */
        if (next_num != end_str) {
            /* setup for the next number */
            next_num = end_str;
        } else {
            /* we have nothing more to parse, drop out of the loop */
            next_num = NULL;
        }

        /* Check for a button to lock if we have a meta button */
        if (meta_button != 0 && next_num != NULL ) {
            lock_button = strtol(next_num, &end_str, 10);

            /* check to see if we found anything */
            if (next_num != end_str) {
                /* setup for the next number */
                next_num = end_str;
            } else {
                /* we have nothing more to parse, drop out of the loop */
                next_num = NULL;
            }
        }

        /* Ok, let the user know what we found on this look */
        if (meta_button != 0) {
            if (lock_button == 0) {
                if (!pairs) {
                    /* We only have a meta button */
                    pEvdev->dragLock.meta = meta_button;

                    xf86Msg(X_CONFIG, "%s: DragLockButtons : "
                            "%i as meta\n",
                            pInfo->name, meta_button);
                } else {
                    xf86Msg(X_ERROR, "%s: DragLockButtons : "
                            "Incomplete pair specifying button pairs %s\n",
                            pInfo->name, option_string);
                }
            } else {

                /* Do bounds checking to make sure we don't crash */
                if ((meta_button <= EVDEV_MAXBUTTONS) && (meta_button >= 0 ) &&
                    (lock_button <= EVDEV_MAXBUTTONS) && (lock_button >= 0)) {

                    xf86Msg(X_CONFIG, "%s: DragLockButtons : %i -> %i\n",
                            pInfo->name, meta_button, lock_button);

                    pEvdev->dragLock.lock_pair[meta_button - 1] = lock_button;
                    pairs=TRUE;
                } else {
                    /* Let the user know something was wrong
                       with this pair of buttons */
                    xf86Msg(X_CONFIG, "%s: DragLockButtons : "
                            "Invalid button pair %i -> %i\n",
                            pInfo->name, meta_button, lock_button);
                }
            }
        } else {
            xf86Msg(X_ERROR, "%s: Found DragLockButtons "
                    "with  invalid lock button string : '%s'\n",
                    pInfo->name, option_string);

            /* This should be the case anyhow, just make sure */
            next_num = NULL;
        }

        /* Check for end of string, to avoid annoying error */
        if (next_num != NULL && *next_num == '\0')
            next_num = NULL;
    }
}

/* Updates DragLock button state and fires button event messges */
void
EvdevDragLockLockButton(InputInfoPtr pInfo, unsigned int button)
{
    EvdevPtr pEvdev = (EvdevPtr)pInfo->private;
    BOOL state = 0;

    /* update button state */
    state = pEvdev->dragLock.lock_state[button - 1] ? FALSE : TRUE;
    pEvdev->dragLock.lock_state[button - 1] = state;

    EvdevQueueButtonEvent(pInfo, button, state);
}

/* Filter button presses looking for either a meta button or the
 * control of a button pair.
 *
 * @param button button number (1 for left, 3 for right)
 * @param value TRUE if button press, FALSE if release
 *
 * @return TRUE if the event was swallowed here, FALSE otherwise.
 */
BOOL
EvdevDragLockFilterEvent(InputInfoPtr pInfo, unsigned int button, int value)
{
    EvdevPtr pEvdev = (EvdevPtr)pInfo->private;

    if (button == 0)
        return FALSE;

    /* Do we have a single meta key or
       several button pairings? */
    if (pEvdev->dragLock.meta != 0) {

        if (pEvdev->dragLock.meta == button) {

            /* setup up for button lock */
            if (value)
                pEvdev->dragLock.meta_state = TRUE;

            return TRUE;
        } else if (pEvdev->dragLock.meta_state) { /* waiting to lock */

            pEvdev->dragLock.meta_state = FALSE;

            EvdevDragLockLockButton(pInfo, button);

            return TRUE;
        }
    } else if (pEvdev->dragLock.lock_pair[button - 1] && value) {
        /* A meta button in a meta/lock pair was pressed */
        EvdevDragLockLockButton(pInfo, pEvdev->dragLock.lock_pair[button - 1]);
        return TRUE;
    }

    /* Eat events for buttons that are locked */
    if (pEvdev->dragLock.lock_state[button - 1])
        return TRUE;

    return FALSE;
}

#ifdef HAVE_PROPERTIES
/**
 * Set the drag lock property.
 * If only one value is supplied, then this is used as the meta button.
 * If more than one value is supplied, then each value is the drag lock button
 * for the pair. 0 disables a pair.
 * i.e. to set bt 3 to draglock button 1, supply 0,0,1
 */
static int
EvdevDragLockSetProperty(DeviceIntPtr dev, Atom atom, XIPropertyValuePtr val,
                         BOOL checkonly)
{
    InputInfoPtr pInfo  = dev->public.devicePrivate;
    EvdevPtr     pEvdev = pInfo->private;

    if (atom == prop_dlock)
    {
        int i;

        if (val->format != 8 || val->type != XA_INTEGER)
            return BadMatch;

        /* Don't allow changes while a lock is active */
        if (pEvdev->dragLock.meta)
        {
            if (pEvdev->dragLock.meta_state)
                return BadAccess;
        } else
        {
            for (i = 0; i < EVDEV_MAXBUTTONS; i++)
                if (pEvdev->dragLock.lock_state[i])
                    return BadValue;
        }

        if (val->size == 0)
            return BadMatch;
        else if (val->size == 1)
        {
            int meta = *((CARD8*)val->data);
            if (meta > EVDEV_MAXBUTTONS)
                return BadValue;

            if (!checkonly)
            {
                pEvdev->dragLock.meta = meta;
                memset(pEvdev->dragLock.lock_pair, 0, sizeof(pEvdev->dragLock.lock_pair));
            }
        } else if ((val->size % 2) == 0)
        {
            CARD8* vals = (CARD8*)val->data;

            for (i = 0; i < val->size && i < EVDEV_MAXBUTTONS; i++)
                if (vals[i] > EVDEV_MAXBUTTONS)
                    return BadValue;

            if (!checkonly)
            {
                pEvdev->dragLock.meta = 0;
                memset(pEvdev->dragLock.lock_pair, 0, sizeof(pEvdev->dragLock.lock_pair));

                for (i = 0; i < val->size && i < EVDEV_MAXBUTTONS; i += 2)
                    pEvdev->dragLock.lock_pair[vals[i] - 1] = vals[i + 1];
            }
        } else
            return BadMatch;
    }

    return Success;
}

/**
 * Initialise property for drag lock buttons setting.
 */
void
EvdevDragLockInitProperty(DeviceIntPtr dev)
{
    InputInfoPtr pInfo  = dev->public.devicePrivate;
    EvdevPtr     pEvdev = pInfo->private;

    if (!dev->button) /* don't init prop for keyboards */
        return;

    prop_dlock = MakeAtom(EVDEV_PROP_DRAGLOCK, strlen(EVDEV_PROP_DRAGLOCK), TRUE);
    if (pEvdev->dragLock.meta)
    {
        XIChangeDeviceProperty(dev, prop_dlock, XA_INTEGER, 8,
                               PropModeReplace, 1, &pEvdev->dragLock.meta,
                               FALSE);
    } else {
        int highest = 0;
        int i;
        CARD8 pair[EVDEV_MAXBUTTONS] = {0};

        for (i = 0; i < EVDEV_MAXBUTTONS; i++)
        {
            if (pEvdev->dragLock.lock_pair[i])
                highest = i;
            pair[i] = pEvdev->dragLock.lock_pair[i];
        }

        XIChangeDeviceProperty(dev, prop_dlock, XA_INTEGER, 8, PropModeReplace,
                               highest + 1, pair, FALSE);
    }

    XISetDevicePropertyDeletable(dev, prop_dlock, FALSE);

    XIRegisterPropertyHandler(dev, EvdevDragLockSetProperty, NULL, NULL);
}

#endif