-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathEventMapping.cpp
125 lines (98 loc) · 2.98 KB
/
EventMapping.cpp
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
/*
* This file is part of hid_mapper.
*
* hid_mapper 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.
*
* hid_mapper 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 hid_mapper. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Thibault Kummer <[email protected]>
*/
#include <EventMapping.h>
#include <string.h>
EventMapping::EventMapping(void)
{
for(int i=0;i<EVENT_MAXLENGTH;i++)
{
map_hash[i] = 0;
map_hash_size[i] = 0;
}
event_list = new LinkedList<event_mapping>;
enum_current_hash_size = 0;
enum_current_position = -1;
}
EventMapping::~EventMapping(void)
{
for(int i=0;i<EVENT_MAXLENGTH;i++)
if(map_hash)
delete[] map_hash[i];
delete event_list;
}
bool EventMapping::AddEvent(int type,const void *event,const void *mask,unsigned int length,int value)
{
if(length==0 || length>EVENT_MAXLENGTH)
return false;
int newpos = map_hash_size[length-1];
event_mapping *new_map = new event_mapping[map_hash_size[length-1]+1];
memcpy(new_map,map_hash[length-1],newpos*sizeof(event_mapping));
memcpy(new_map+newpos+1,map_hash[length-1]+newpos,(map_hash_size[length-1]-newpos)*sizeof(event_mapping));
new_map[newpos].type = type;
new_map[newpos].length = length;
memcpy(new_map[newpos].event,event,length);
memcpy(new_map[newpos].mask,mask,length);
new_map[newpos].value = value;
if(map_hash[length-1])
delete[] map_hash[length-1];
map_hash[length-1] = new_map;
map_hash_size[length-1]++;
return true;
}
LinkedList<event_mapping> *EventMapping::LookupEvent(const void *event,unsigned int event_length)
{
event_list->Reset();
if(event_length==0 || event_length>EVENT_MAXLENGTH)
return event_list;
if(map_hash_size[event_length-1]==0)
return event_list;
int i,j;
for(i=0;i<map_hash_size[event_length-1];i++)
{
for(j=0;j<event_length;j++)
{
if((unsigned char)map_hash[event_length-1][i].mask[j]!=0xFF && map_hash[event_length-1][i].event[j]!=((char *)event)[j])
break;
}
if(j==event_length)
event_list->AddItem(&map_hash[event_length-1][i]);
}
return event_list;
}
void EventMapping::EnumReset(void)
{
enum_current_hash_size = 0;
enum_current_position = -1;
}
const event_mapping *EventMapping::EnumEvent(void)
{
if(enum_current_hash_size>=EVENT_MAXLENGTH)
return 0;
for(;enum_current_hash_size<EVENT_MAXLENGTH;enum_current_hash_size++)
{
if(enum_current_position+1>=map_hash_size[enum_current_hash_size])
{
enum_current_position = -1;
continue;
}
enum_current_position++;
return &map_hash[enum_current_hash_size][enum_current_position];
}
return 0;
}