source: select.c @ 7a1c90d

barnowl_perlaim
Last change on this file since 7a1c90d was c453ada, checked in by Geoffrey Thomas <geofft@mit.edu>, 16 years ago
Remove aim.c. buddylist.c, buddy.c, libfaim, and everything that uses them.
  • Property mode set to 100644
File size: 3.7 KB
Line 
1#include "owl.h"
2
3static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $";
4
5/* Returns the index of the dispatch for the file descriptor. */
6int owl_select_find_dispatch(int fd)
7{
8  int i, len;
9  owl_list *dl;
10  owl_dispatch *d;
11 
12  dl = owl_global_get_dispatchlist(&g);
13  len = owl_list_get_size(dl);
14  for(i = 0; i < len; i++) {
15    d = (owl_dispatch*)owl_list_get_element(dl, i);
16    if (d->fd == fd) return i;
17  }
18  return -1;
19}
20
21/* Adds a new owl_dispatch to the list, replacing existing ones if needed. */
22void owl_select_add_dispatch(owl_dispatch *d)
23{
24  int elt;
25  owl_list *dl;
26
27  elt = owl_select_find_dispatch(d->fd);
28  dl = owl_global_get_dispatchlist(&g);
29 
30  if (elt != -1) {  /* If we have a dispatch for this FD */
31    owl_dispatch *d_old;
32    d_old = (owl_dispatch*)owl_list_get_element(dl, elt);
33    /* Ignore if we're adding the same dispatch again.  Otherwise
34       replace the old dispatch. */
35    if (d_old != d) {
36      owl_list_replace_element(dl, elt, d);
37      owl_free(d_old);
38    }
39  }
40  else {
41    owl_list_append_element(dl, d);
42  }
43}
44
45/* Removes an owl_dispatch to the list, based on it's file descriptor. */
46void owl_select_remove_dispatch(int fd)
47{
48  int elt;
49  owl_list *dl;
50
51  elt = owl_select_find_dispatch(fd);
52  dl = owl_global_get_dispatchlist(&g);
53 
54  if (elt != -1) {
55    owl_dispatch *d;
56    d = (owl_dispatch*)owl_list_get_element(dl, elt);
57    owl_list_remove_element(dl, elt);
58    if (d->pfunc) {
59      owl_perlconfig_dispatch_free(d);
60    }
61    owl_free(d);
62  }
63}
64
65int owl_select_dispatch_count()
66{
67  return owl_list_get_size(owl_global_get_dispatchlist(&g));
68}
69
70int owl_select_add_perl_dispatch(int fd, SV *cb)
71{
72  int elt;
73  owl_dispatch *d;
74  elt = owl_select_find_dispatch(fd);
75  if (elt != -1) {
76    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
77    if (d->pfunc == NULL) {
78      /* don't mess with non-perl dispatch functions from here. */
79      return 1;
80    }
81  }
82
83  d = malloc(sizeof(owl_dispatch));
84  d->fd = fd;
85  d->cfunc = NULL;
86  d->pfunc = cb;
87  owl_select_add_dispatch(d);
88  return 0;
89}
90
91int owl_select_remove_perl_dispatch(int fd)
92{
93  int elt;
94  owl_dispatch *d;
95 
96  elt = owl_select_find_dispatch(fd);
97  if (elt != -1) {
98    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
99    if (d->pfunc != NULL) {
100      owl_select_remove_dispatch(fd);
101      return 0;
102    }
103  }
104  return 1;
105}
106
107int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e)
108{
109  int i, len, max_fd;
110  owl_dispatch *d;
111  owl_list *dl;
112
113  dl = owl_global_get_dispatchlist(&g);
114  FD_ZERO(r);
115  FD_ZERO(e);
116  max_fd = 0;
117  len = owl_select_dispatch_count(g);
118  for(i = 0; i < len; i++) {
119    d = (owl_dispatch*)owl_list_get_element(dl, i);
120    FD_SET(d->fd, r);
121    FD_SET(d->fd, e);
122    if (max_fd < d->fd) max_fd = d->fd;
123  }
124  return max_fd + 1;
125}
126
127void owl_select_dispatch(fd_set *fds, int max_fd)
128{
129  int i, len;
130  owl_dispatch *d;
131  owl_list *dl;
132
133  dl = owl_global_get_dispatchlist(&g);
134  len = owl_select_dispatch_count();
135  for(i = 0; i < len; i++) {
136    d = (owl_dispatch*)owl_list_get_element(dl, i);
137    /* While d shouldn't normally be null, the list may be altered by
138     * functions we dispatch to. */
139    if (d != NULL && FD_ISSET(d->fd, fds)) {
140      if (d->cfunc != NULL) {
141        (d->cfunc)();
142      }
143      else if (d->pfunc != NULL) {
144        owl_perlconfig_do_dispatch(d);
145      }
146    }
147  }
148}
149
150void owl_select()
151{
152  int max_fd;
153  fd_set r;
154  fd_set e;
155  struct timeval timeout;
156
157  timeout.tv_sec = 1;
158  timeout.tv_usec = 0;
159
160  max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e);
161
162  if ( select(max_fd+1, &r, NULL, &e, &timeout) ) {
163    /* NOTE: the same dispatch function is called for both exceptional
164       and read ready FDs. */
165    owl_select_dispatch(&r, max_fd);
166  }
167}
Note: See TracBrowser for help on using the repository browser.