-
Notifications
You must be signed in to change notification settings - Fork 2
/
xfds.c
85 lines (72 loc) · 1.69 KB
/
xfds.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
bool xfd_add(X_FDS* set, int fd){
unsigned i;
if(!set->fds){
set->fds = malloc(sizeof(int));
if(!set->fds){
fprintf(stderr, "xfd_add: Initial alloc failed\n");
return false;
}
set->size = 1;
set->fds[0] = fd;
return true;
}
for(i = 0; i < set->size; i++){
if(set->fds[i] == fd){
fprintf(stderr, "xfd_add: Not pushing duplicate entry\n");
return false;
}
}
set->fds = realloc(set->fds, (set->size + 1) * sizeof(int));
if(!set->fds){
fprintf(stderr, "xfd_add: Failed to realloc fd set\n");
return false;
}
set->fds[set->size] = fd;
set->size++;
return true;
}
bool xfd_remove(X_FDS* set, int fd){
unsigned i, c;
for(i = 0; i < set->size; i++){
if(set->fds[i] == fd){
for(c = i; c < set->size - 1; c++){
set->fds[c] = set->fds[c + 1];
}
set->size--;
set->fds = realloc(set->fds, set->size * sizeof(int));
if(!set->fds && set->size > 0){
fprintf(stderr, "xfd_remove: Failed to realloc\n");
return false;
}
return true;
}
}
fprintf(stderr, "xfd_remove: FD not in set\n");
return false;
}
void xfd_free(X_FDS* set){
if(set->fds){
free(set->fds);
set->fds = NULL;
}
set->size = 0;
}
void xfds_select(X_FDS* set, fd_set* fds, int* maxfd){
unsigned u;
if(set->fds){
for(u = 0; u < set->size; u++){
FD_SET(set->fds[u], fds);
*maxfd = (set->fds[u] > *maxfd) ? set->fds[u]:*maxfd;
}
}
}
void xconn_watch(Display* dpy, XPointer client_data, int fd, Bool opening, XPointer* watch_data){
if(opening){
fprintf(stderr, "xconn_watch: Internal connection registered\n");
xfd_add((X_FDS*)client_data, fd);
}
else{
fprintf(stderr, "xconn_watch: Internal connection closed\n");
xfd_remove((X_FDS*)client_data, fd);
}
}