~ctwm/ctwm/trunk

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
/*
 * Utils for translating X events between their names and numbers
 */

#include "ctwm.h"

#include <stddef.h>
#include <strings.h>

#include "event_names.h"

/* num->name lookup table (generated build-time) */
#include "event_names_table.h"



/* Need this for any iteration */
size_t
event_names_size(void)
{
	return(sizeof(event_names) / sizeof(*event_names));
}


/* Return the name for any event number */
const char *
event_name_by_num(int evt)
{
	/* Bounds */
	if(evt < 0 || evt >= event_names_size()) {
		return NULL;
	}

	/* Else it's whatever is[n't] there */
	return event_names[evt];
}


/*
 * Find the number for a name.  Technically, the index of the array is a
 * size_t.  We'd need a ssize_t to allow -1 to mean "not found".  But the
 * numbers in the X definition are ints, so that's what we'll return.
 */
int
event_num_by_name(const char *ename)
{
	int i;

	if(ename == NULL) {
		return -1;
	}

	for(i = 0 ; i < event_names_size() ; i++) {
		if(event_names[i] != NULL && strcasecmp(ename, event_names[i]) == 0) {
			return i;
		}
	}

	/* Not found */
	return -1;
}