forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_type_a.c
164 lines (141 loc) · 3.82 KB
/
input_type_a.c
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
/*
* This file is part of MultiROM.
*
* MultiROM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MultiROM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultiROM. If not, see <http://www.gnu.org/licenses/>.
*/
// Implementation of "Protocol Example A" from kernel's
// Documentation/input/multi-touch-protocol.txt
// This protocol requires client to keep track of ids,
// I don't really like this implementation, but I can't
// come up with anything better :/
#include <linux/input.h>
#include <assert.h>
#include <stdlib.h>
#include "input.h"
#include "input_priv.h"
#include "util.h"
static int *active_touches = NULL;
static int *curr_touches = NULL;
static void idlist_clear(int *list)
{
int i;
for(i = 0; i < MAX_FINGERS && list[i] != -1; ++i)
list[i] = -1;
}
static int *idlist_init(void)
{
int *res = malloc(MAX_FINGERS*sizeof(int));
int i;
for(i = 0; i < MAX_FINGERS; ++i)
res[i] = -1;
return res;
}
static void idlist_swap(int **list_a, int **list_b)
{
int *tmp = *list_a;
*list_a = *list_b;
*list_b = tmp;
}
static int idlist_add(int *list, int id)
{
int i;
for(i = 0; i < MAX_FINGERS; ++i)
{
if(list[i] == id)
return -1;
if(list[i] == -1)
{
list[i] = id;
return 0;
}
}
assert(0);
return -1;
}
static int idlist_rm(int *list, int id)
{
int i;
for(i = 0; i < MAX_FINGERS; ++i)
{
if(list[i] == -1)
return -1;
if(list[i] == id)
{
for(++i; i < MAX_FINGERS && list[i] != -1; ++i)
list[i-1] = list[i];
list[i-1] = -1;
return 0;
}
}
return -1;
}
void init_touch_specifics(void)
{
active_touches = idlist_init();
curr_touches = idlist_init();
}
void destroy_touch_specifics(void)
{
free(active_touches);
free(curr_touches);
active_touches = NULL;
curr_touches = NULL;
}
void handle_abs_event(struct input_event *ev)
{
switch(ev->code)
{
case ABS_MT_TRACKING_ID:
{
mt_events[mt_slot++].id = ev->value;
break;
}
case ABS_MT_POSITION_X:
case ABS_MT_POSITION_Y:
{
if((ev->code == ABS_MT_POSITION_X) ^ (mt_switch_xy != 0))
{
mt_events[mt_slot].orig_x = calc_mt_pos(ev->value, mt_range_x, mt_screen_res[0]);
if(mt_switch_xy)
mt_events[mt_slot].orig_x = mt_screen_res[0] - mt_events[mt_slot].orig_x;
}
else
mt_events[mt_slot].orig_y = calc_mt_pos(ev->value, mt_range_y, mt_screen_res[1]);
mt_events[mt_slot].changed |= TCHNG_POS;
break;
}
}
}
void handle_syn_event(struct input_event *ev)
{
if(ev->code != SYN_REPORT)
return;
idlist_swap(&curr_touches, &active_touches);
int i;
for(i = 0; i < mt_slot; ++i)
{
idlist_add(active_touches, mt_events[i].id);
if(idlist_rm(curr_touches, mt_events[i].id) == -1)
mt_events[i].changed |= TCHNG_ADDED;
}
for(i = 0; mt_slot < MAX_FINGERS && i < MAX_FINGERS && curr_touches[i] != -1; ++i)
{
mt_events[mt_slot].id = curr_touches[i];
mt_events[mt_slot].changed = TCHNG_REMOVED;
curr_touches[i] = -1;
++mt_slot;
}
mt_slot = 0;
touch_commit_events(ev->time);
}