459.1.6
by Matthew Fuller
Start putting together some util funcs to turn X event id's into names |
1 |
/*
|
2 |
* Utils for translating X events between their names and numbers
|
|
3 |
*/
|
|
4 |
||
5 |
#include "ctwm.h" |
|
6 |
||
7 |
#include <stddef.h> |
|
8 |
#include <strings.h> |
|
9 |
||
10 |
#include "event_names.h" |
|
11 |
||
462
by Matthew Fuller
Generate event_names_table.h rather than event_names.c. |
12 |
/* num->name lookup table (generated build-time) */
|
13 |
#include "event_names_table.h" |
|
459.1.6
by Matthew Fuller
Start putting together some util funcs to turn X event id's into names |
14 |
|
15 |
||
16 |
||
17 |
/* Need this for any iteration */
|
|
18 |
size_t
|
|
19 |
event_names_size(void) |
|
20 |
{
|
|
21 |
return(sizeof(event_names) / sizeof(*event_names)); |
|
22 |
}
|
|
23 |
||
24 |
||
25 |
/* Return the name for any event number */
|
|
26 |
const char * |
|
27 |
event_name_by_num(int evt) |
|
28 |
{
|
|
29 |
/* Bounds */
|
|
30 |
if(evt < 0 || evt >= event_names_size()) { |
|
31 |
return NULL; |
|
32 |
}
|
|
33 |
||
34 |
/* Else it's whatever is[n't] there */
|
|
35 |
return event_names[evt]; |
|
36 |
}
|
|
37 |
||
38 |
||
39 |
/*
|
|
40 |
* Find the number for a name. Technically, the index of the array is a
|
|
41 |
* size_t. We'd need a ssize_t to allow -1 to mean "not found". But the
|
|
42 |
* numbers in the X definition are ints, so that's what we'll return.
|
|
43 |
*/
|
|
44 |
int
|
|
45 |
event_num_by_name(const char *ename) |
|
46 |
{
|
|
47 |
int i; |
|
48 |
||
49 |
if(ename == NULL) { |
|
50 |
return -1; |
|
51 |
}
|
|
52 |
||
53 |
for(i = 0 ; i < event_names_size() ; i++) { |
|
54 |
if(event_names[i] != NULL && strcasecmp(ename, event_names[i]) == 0) { |
|
55 |
return i; |
|
56 |
}
|
|
57 |
}
|
|
58 |
||
59 |
/* Not found */
|
|
60 |
return -1; |
|
61 |
}
|