-
Notifications
You must be signed in to change notification settings - Fork 2
/
Atom.c
106 lines (86 loc) · 1.89 KB
/
Atom.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
#include <string.h>
#include "X11/Xatom.h"
#include "nxlib.h"
#define SZHASHTABLE 64
struct hash_t {
char *name;
Atom atom;
struct hash_t *next;
};
static struct hash_t *hash_list[SZHASHTABLE];
static unsigned long atom_id = XA_LAST_PREDEFINED + 32;
static unsigned char
hash_str(_Xconst char *name)
{
unsigned char ch = 0;
int i = 0;
for (i = 0; i < strlen(name); i++)
ch += name[i];
return (ch % SZHASHTABLE);
}
Atom
XInternAtom(Display * display, _Xconst char *atom_name, Bool only_if_exists)
{
unsigned char hash = hash_str(atom_name);
struct hash_t *val;
for (val = hash_list[hash]; val; val = val->next)
if (strcmp(val->name, atom_name) == 0) {
return val->atom;
}
if (only_if_exists == True)
return None;
if (!hash_list[hash])
val = hash_list[hash] =
(struct hash_t *) Xcalloc(1, sizeof(struct hash_t));
else {
struct hash_t *h = hash_list[hash];
while (h->next)
h = h->next;
val = h->next =
(struct hash_t *) Xcalloc(1, sizeof(struct hash_t));
}
val->name = strdup(atom_name);
val->atom = atom_id++;
return val->atom;
}
Status
XInternAtoms(Display * display, char **names, int count,
Bool only_if_exists, Atom * atoms_return)
{
int i;
int ret = 1;
for (i = 0; i < count; i++) {
atoms_return[i] =
XInternAtom(display, names[i], only_if_exists);
if (!atoms_return[i])
ret = 0;
}
return ret;
}
/* return copy of Atom name*/
char *
XGetAtomName(Display * display, Atom atom)
{
int i;
for (i = 0; i < SZHASHTABLE; i++) {
struct hash_t *val;
for (val = hash_list[i]; val; val = val->next)
if (val->atom == atom) {
char *name = strdup(val->name);
return name;
}
}
return NULL;
}
Status
XGetAtomNames(Display * display, Atom * atoms, int count, char **names_return)
{
int i;
int ret = 1;
for (i = 0; i < count; i++) {
names_return[i] = XGetAtomName(display, atoms[i]);
if (!names_return[i])
ret = 0;
}
return ret;
}