summaryrefslogtreecommitdiff
path: root/src/draglock.c
blob: e0a91d08f4dff10118df28970460949ed7edbf03 (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
/*
 * Copyright © 2015 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 Red Hat
 * not be used in advertising or publicity pertaining to distribution
 * of the software without specific, written prior permission.  Red
 * Hat makes 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.
 */

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

#include "draglock.h"

#include <string.h>
#include <stdlib.h>

#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))

static int
draglock_parse_config(struct draglock *dl, const char *config)
{
    int button = 0, target = 0;
    const char *str = NULL;
    char *end_str = NULL;
    int pairs[DRAGLOCK_MAX_BUTTONS] = {0};

    if (!config)
	    return 0;

    /* empty string disables drag lock */
    if (*config == '\0') {
	    dl->mode = DRAGLOCK_DISABLED;
	    return 0;
    }

    /* check for a single-number string first, config is "<int>" */
    button = strtol(config, &end_str, 10);
    if (*end_str == '\0') {
	    if (button < 0 || button >= DRAGLOCK_MAX_BUTTONS)
		    return 1;
	    /* we allow for button 0 so stacked xorg.conf.d snippets can
	     * disable the config again */
	    if (button == 0) {
		    dl->mode = DRAGLOCK_DISABLED;
		    return 0;
	    }

	    return draglock_set_meta(dl, button);
    }

    dl->mode = DRAGLOCK_DISABLED;

    /* check for a set of button pairs, config is
     * "<int> <int> <int> <int>..." */
    str = config;
    while (*str != '\0') {
	    button = strtol(str, &end_str, 10);
	    if (*end_str == '\0')
		    return 1;

	    str = end_str;
	    target = strtol(str, &end_str, 10);
	    if (end_str == str)
		    return 1;
	    if (button <= 0 || button >= DRAGLOCK_MAX_BUTTONS || target >= DRAGLOCK_MAX_BUTTONS)
		    return 1;

	    pairs[button] = target;
	    str = end_str;
    }

    return draglock_set_pairs(dl, pairs, ARRAY_SIZE(pairs));
}

int
draglock_init_from_string(struct draglock *dl, const char *config)
{
	dl->mode = DRAGLOCK_DISABLED;

	dl->meta_button = 0;
	dl->meta_state = false;
	memset(dl->lock_pair, 0, sizeof(dl->lock_pair));
	memset(dl->lock_state, 0, sizeof(dl->lock_state));

	return draglock_parse_config(dl, config);
}

enum draglock_mode
draglock_get_mode(const struct draglock *dl)
{
	return dl->mode;
}

int
draglock_get_meta(const struct draglock *dl)
{
	if (dl->mode == DRAGLOCK_META)
		return dl->meta_button;
	return 0;
}

size_t
draglock_get_pairs(const struct draglock *dl, int *array, size_t nelem)
{
	unsigned int i;
	size_t last = 0;

	if (dl->mode != DRAGLOCK_PAIRS)
		return 0;

	/* size 1 array with the meta button */
	if (dl->meta_button) {
		*array = dl->meta_button;
		return 1;
	}

	/* size N array with a[0] == 0, the rest ordered by button number */
	memset(array, 0, nelem * sizeof(array[0]));
	for (i = 0; i < nelem && i < ARRAY_SIZE(dl->lock_pair); i++) {
		array[i] = dl->lock_pair[i];
		if (array[i] != 0 && i > last)
			last = i;
	}
	return last;
}

int
draglock_set_meta(struct draglock *dl, int meta_button)
{
	if (meta_button < 0 || meta_button >= DRAGLOCK_MAX_BUTTONS)
		return 1;

	dl->meta_button = meta_button;
	dl->mode = meta_button ? DRAGLOCK_META : DRAGLOCK_DISABLED;

	return 0;
}

int
draglock_set_pairs(struct draglock *dl, const int *array, size_t nelem)
{
	unsigned int i;

	if (nelem == 0 || array[0] != 0)
		return 1;

	for (i = 0; i < nelem; i++) {
		if (array[i] < 0 || array[i] >= DRAGLOCK_MAX_BUTTONS)
			return 1;
	}

	dl->mode = DRAGLOCK_DISABLED;
	for (i = 0; i < nelem; i++) {
		dl->lock_pair[i] = array[i];
		if (dl->lock_pair[i])
			dl->mode = DRAGLOCK_PAIRS;
	}

	return 0;
}

static int
draglock_filter_meta(struct draglock *dl, int *button, int *press)
{
	int b = *button,
	    is_press = *press;

	if (b == dl->meta_button) {
		if (is_press)
			dl->meta_state = true;
		*button = 0;
		return 0;
	}

	switch (dl->lock_state[b]) {
	case DRAGLOCK_BUTTON_STATE_NONE:
		if (dl->meta_state && is_press) {
			dl->lock_state[b] = DRAGLOCK_BUTTON_STATE_DOWN_1;
			dl->meta_state = false;
		}
		break;
	case DRAGLOCK_BUTTON_STATE_DOWN_1:
		if (!is_press) {
			dl->lock_state[b] = DRAGLOCK_BUTTON_STATE_UP_1;
			b = 0;
		}
		break;
	case DRAGLOCK_BUTTON_STATE_UP_1:
		if (is_press) {
			dl->lock_state[b] = DRAGLOCK_BUTTON_STATE_DOWN_2;
			b = 0;
		}
		break;
	case DRAGLOCK_BUTTON_STATE_DOWN_2:
		if (!is_press) {
			dl->lock_state[b] = DRAGLOCK_BUTTON_STATE_NONE;
		}
		break;
	}

	*button = b;

	return 0;
}

static int
draglock_filter_pair(struct draglock *dl, int *button, int *press)
{
	int b = *button,
	    is_press = *press;

	if (dl->lock_pair[b] == 0)
		return 0;

	switch (dl->lock_state[b]) {
	case DRAGLOCK_BUTTON_STATE_NONE:
		if (is_press) {
			dl->lock_state[b] = DRAGLOCK_BUTTON_STATE_DOWN_1;
			b = dl->lock_pair[b];
		}
		break;
	case DRAGLOCK_BUTTON_STATE_DOWN_1:
		if (!is_press) {
			dl->lock_state[b] = DRAGLOCK_BUTTON_STATE_UP_1;
			b = 0;
		}
		break;
	case DRAGLOCK_BUTTON_STATE_UP_1:
		if (is_press) {
			dl->lock_state[b] = DRAGLOCK_BUTTON_STATE_DOWN_2;
			b = 0;
		}
		break;
	case DRAGLOCK_BUTTON_STATE_DOWN_2:
		if (!is_press) {
			dl->lock_state[b] = DRAGLOCK_BUTTON_STATE_NONE;
			b = dl->lock_pair[b];
		}
		break;
	}

	*button = b;

	return 0;
}

int
draglock_filter_button(struct draglock *dl, int *button, int *is_press)
{
	if (*button == 0)
		return 0;

	switch(dl->mode) {
	case DRAGLOCK_DISABLED:
		return 0;
	case DRAGLOCK_META:
		return draglock_filter_meta(dl, button, is_press);
	case DRAGLOCK_PAIRS:
		return draglock_filter_pair(dl, button, is_press);
	default:
		abort();
		break;
	}

	return 0;
}