source: select.c @ 2be605a

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2be605a was 5a35c708, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Fix the build for 'make test' The tester build doesn't include owl.c, so owl.c cannot define any functions called from outside owl.c.
  • Property mode set to 100644
File size: 10.2 KB
Line 
1#include "owl.h"
2
3static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $";
4
5static int dispatch_active = 0;
6
7int _owl_select_timer_cmp(owl_timer *t1, owl_timer *t2) {
8  return t1->time - t2->time;
9}
10
11int _owl_select_timer_eq(owl_timer *t1, owl_timer *t2) {
12  return t1 == t2;
13}
14
15owl_timer *owl_select_add_timer(int after, int interval, void (*cb)(owl_timer *, void *), void (*destroy)(owl_timer*), void *data)
16{
17  owl_timer *t = owl_malloc(sizeof(owl_timer));
18  GList **timers = owl_global_get_timerlist(&g);
19
20  t->time = time(NULL) + after;
21  t->interval = interval;
22  t->callback = cb;
23  t->destroy = destroy;
24  t->data = data;
25
26  *timers = g_list_insert_sorted(*timers, t,
27                                 (GCompareFunc)_owl_select_timer_cmp);
28  return t;
29}
30
31void owl_select_remove_timer(owl_timer *t)
32{
33  GList **timers = owl_global_get_timerlist(&g);
34  if (t && g_list_find(*timers, t)) {
35    *timers = g_list_remove(*timers, t);
36    if(t->destroy) {
37      t->destroy(t);
38    }
39    owl_free(t);
40  }
41}
42
43void owl_select_process_timers(struct timespec *timeout)
44{
45  time_t now = time(NULL);
46  GList **timers = owl_global_get_timerlist(&g);
47
48  while(*timers) {
49    owl_timer *t = (*timers)->data;
50    int remove = 0;
51
52    if(t->time > now)
53      break;
54
55    /* Reschedule if appropriate */
56    if(t->interval > 0) {
57      t->time = now + t->interval;
58      *timers = g_list_remove(*timers, t);
59      *timers = g_list_insert_sorted(*timers, t,
60                                     (GCompareFunc)_owl_select_timer_cmp);
61    } else {
62      remove = 1;
63    }
64
65    /* Do the callback */
66    t->callback(t, t->data);
67    if(remove) {
68      owl_select_remove_timer(t);
69    }
70  }
71
72  if(*timers) {
73    owl_timer *t = (*timers)->data;
74    timeout->tv_sec = t->time - now;
75    if (timeout->tv_sec > 60)
76      timeout->tv_sec = 60;
77  } else {
78    timeout->tv_sec = 60;
79  }
80
81  timeout->tv_nsec = 0;
82}
83
84/* Returns the index of the dispatch for the file descriptor. */
85int owl_select_find_dispatch(int fd)
86{
87  int i, len;
88  owl_list *dl;
89  owl_dispatch *d;
90 
91  dl = owl_global_get_dispatchlist(&g);
92  len = owl_list_get_size(dl);
93  for(i = 0; i < len; i++) {
94    d = (owl_dispatch*)owl_list_get_element(dl, i);
95    if (d->fd == fd) return i;
96  }
97  return -1;
98}
99
100void owl_select_remove_dispatch_at(int elt) /* noproto */
101{
102  owl_list *dl;
103  owl_dispatch *d;
104
105  dl = owl_global_get_dispatchlist(&g);
106  d = (owl_dispatch*)owl_list_get_element(dl, elt);
107  owl_list_remove_element(dl, elt);
108  if (d->destroy) {
109    d->destroy(d);
110  }
111}
112
113/* Adds a new owl_dispatch to the list, replacing existing ones if needed. */
114void owl_select_add_dispatch(owl_dispatch *d)
115{
116  int elt;
117  owl_list *dl;
118
119  d->needs_gc = 0;
120
121  elt = owl_select_find_dispatch(d->fd);
122  dl = owl_global_get_dispatchlist(&g);
123 
124  if (elt != -1) {  /* If we have a dispatch for this FD */
125    owl_dispatch *d_old;
126    d_old = (owl_dispatch*)owl_list_get_element(dl, elt);
127    /* Ignore if we're adding the same dispatch again.  Otherwise
128       replace the old dispatch. */
129    if (d_old != d) {
130      owl_select_remove_dispatch_at(elt);
131    }
132  }
133  owl_list_append_element(dl, d);
134}
135
136/* Removes an owl_dispatch to the list, based on it's file descriptor. */
137void owl_select_remove_dispatch(int fd)
138{
139  int elt;
140  owl_list *dl;
141  owl_dispatch *d;
142
143  elt = owl_select_find_dispatch(fd);
144  if(elt == -1) {
145    return;
146  } else if(dispatch_active) {
147    /* Defer the removal until dispatch is done walking the list */
148    dl = owl_global_get_dispatchlist(&g);
149    d = (owl_dispatch*)owl_list_get_element(dl, elt);
150    d->needs_gc = 1;
151  } else {
152    owl_select_remove_dispatch_at(elt);
153  }
154}
155
156int owl_select_dispatch_count()
157{
158  return owl_list_get_size(owl_global_get_dispatchlist(&g));
159}
160
161int owl_select_add_perl_dispatch(int fd, SV *cb)
162{
163  int elt;
164  owl_dispatch *d;
165  elt = owl_select_find_dispatch(fd);
166  if (elt != -1) {
167    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
168    if (d->cfunc != owl_perlconfig_dispatch) {
169      /* don't mess with non-perl dispatch functions from here. */
170      return 1;
171    }
172  }
173
174  d = malloc(sizeof(owl_dispatch));
175  d->fd = fd;
176  d->cfunc = owl_perlconfig_dispatch;
177  d->destroy = owl_perlconfig_dispatch_free;
178  d->data = cb;
179  owl_select_add_dispatch(d);
180  return 0;
181}
182
183int owl_select_remove_perl_dispatch(int fd)
184{
185  int elt;
186  owl_dispatch *d;
187 
188  elt = owl_select_find_dispatch(fd);
189  if (elt != -1) {
190    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
191    if (d->cfunc == owl_perlconfig_dispatch) {
192      owl_select_remove_dispatch_at(elt);
193      return 0;
194    }
195  }
196  return 1;
197}
198
199int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e)
200{
201  int i, len, max_fd;
202  owl_dispatch *d;
203  owl_list *dl;
204
205  dl = owl_global_get_dispatchlist(&g);
206  FD_ZERO(r);
207  FD_ZERO(e);
208  max_fd = 0;
209  len = owl_select_dispatch_count(g);
210  for(i = 0; i < len; i++) {
211    d = (owl_dispatch*)owl_list_get_element(dl, i);
212    FD_SET(d->fd, r);
213    FD_SET(d->fd, e);
214    if (max_fd < d->fd) max_fd = d->fd;
215  }
216  return max_fd + 1;
217}
218
219void owl_select_gc()
220{
221  int i;
222  owl_list *dl;
223
224  dl = owl_global_get_dispatchlist(&g);
225  /*
226   * Count down so we aren't set off by removing items from the list
227   * during the iteration.
228   */
229  for(i = owl_list_get_size(dl) - 1; i >= 0; i--) {
230    owl_dispatch *d = owl_list_get_element(dl, i);
231    if(d->needs_gc) {
232      owl_select_remove_dispatch_at(i);
233    }
234  }
235}
236
237void owl_select_dispatch(fd_set *fds, int max_fd)
238{
239  int i, len;
240  owl_dispatch *d;
241  owl_list *dl;
242
243  dl = owl_global_get_dispatchlist(&g);
244  len = owl_select_dispatch_count();
245
246  dispatch_active = 1;
247
248  for(i = 0; i < len; i++) {
249    d = (owl_dispatch*)owl_list_get_element(dl, i);
250    /* While d shouldn't normally be null, the list may be altered by
251     * functions we dispatch to. */
252    if (d != NULL && !d->needs_gc && FD_ISSET(d->fd, fds)) {
253      if (d->cfunc != NULL) {
254        d->cfunc(d);
255      }
256    }
257  }
258
259  dispatch_active = 0;
260  owl_select_gc();
261}
262
263int owl_select_aim_hack(fd_set *rfds, fd_set *wfds)
264{
265  aim_conn_t *cur;
266  aim_session_t *sess;
267  int max_fd;
268
269  FD_ZERO(rfds);
270  FD_ZERO(wfds);
271  max_fd = 0;
272  sess = owl_global_get_aimsess(&g);
273  for (cur = sess->connlist, max_fd = 0; cur; cur = cur->next) {
274    if (cur->fd != -1) {
275      FD_SET(cur->fd, rfds);
276      if (cur->status & AIM_CONN_STATUS_INPROGRESS) {
277        /* Yes, we're checking writable sockets here. Without it, AIM
278           login is really slow. */
279        FD_SET(cur->fd, wfds);
280      }
281     
282      if (cur->fd > max_fd)
283        max_fd = cur->fd;
284    }
285  }
286  return max_fd;
287}
288
289void owl_process_input_char(owl_input j)
290{
291  int ret;
292  owl_popwin *pw;
293  owl_editwin *tw;
294
295  owl_global_set_lastinputtime(&g, time(NULL));
296  pw=owl_global_get_popwin(&g);
297  tw=owl_global_get_typwin(&g);
298
299  owl_global_set_lastinputtime(&g, time(NULL));
300  /* find and activate the current keymap.
301   * TODO: this should really get fixed by activating
302   * keymaps as we switch between windows...
303   */
304  if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
305    owl_context_set_popless(owl_global_get_context(&g), 
306                            owl_global_get_viewwin(&g));
307    owl_function_activate_keymap("popless");
308  } else if (owl_global_is_typwin_active(&g) 
309             && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
310    /*
311      owl_context_set_editline(owl_global_get_context(&g), tw);
312      owl_function_activate_keymap("editline");
313    */
314  } else if (owl_global_is_typwin_active(&g) 
315             && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
316    owl_context_set_editmulti(owl_global_get_context(&g), tw);
317    owl_function_activate_keymap("editmulti");
318  } else {
319    owl_context_set_recv(owl_global_get_context(&g));
320    owl_function_activate_keymap("recv");
321  }
322  /* now actually handle the keypress */
323  ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
324  if (ret!=0 && ret!=1) {
325    owl_function_makemsg("Unable to handle keypress");
326  }
327}
328
329void owl_select_handle_intr()
330{
331  owl_input in;
332
333  owl_global_unset_interrupted(&g);
334  owl_function_unmask_sigint(NULL);
335
336  in.ch = in.uch = owl_global_get_startup_tio(&g)->c_cc[VINTR];
337  owl_process_input_char(in);
338}
339
340void owl_select()
341{
342  int i, max_fd, aim_max_fd, aim_done, ret;
343  fd_set r;
344  fd_set e;
345  fd_set aim_rfds, aim_wfds;
346  struct timespec timeout;
347  sigset_t mask;
348
349  owl_select_process_timers(&timeout);
350
351  owl_function_mask_sigint(&mask);
352  if(owl_global_is_interrupted(&g)) {
353    owl_select_handle_intr();
354    return;
355  }
356
357  max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e);
358
359  /* AIM HACK:
360   *
361   *  The problem - I'm not sure where to hook into the owl/faim
362   *  interface to keep track of when the AIM socket(s) open and
363   *  close. In particular, the bosconn thing throws me off. So,
364   *  rather than register particular dispatchers for AIM, I look up
365   *  the relevant FDs and add them to select's watch lists, then
366   *  check for them individually before moving on to the other
367   *  dispatchers. --asedeno
368   */
369  aim_done = 1;
370  FD_ZERO(&aim_rfds);
371  FD_ZERO(&aim_wfds);
372  if (owl_global_is_doaimevents(&g)) {
373    aim_done = 0;
374    aim_max_fd = owl_select_aim_hack(&aim_rfds, &aim_wfds);
375    if (max_fd < aim_max_fd) max_fd = aim_max_fd;
376    for(i = 0; i <= aim_max_fd; i++) {
377      if (FD_ISSET(i, &aim_rfds)) {
378        FD_SET(i, &r);
379        FD_SET(i, &e);
380      }
381    }
382  }
383  /* END AIM HACK */
384
385
386  ret = pselect(max_fd+1, &r, &aim_wfds, &e, &timeout, &mask);
387
388  if(ret < 0 && errno == EINTR) {
389    if(owl_global_is_interrupted(&g)) {
390      owl_select_handle_intr();
391    }
392    return;
393  }
394
395  owl_function_unmask_sigint(NULL);
396
397  if(ret > 0) {
398    /* Merge fd_sets and clear AIM FDs. */
399    for(i = 0; i <= max_fd; i++) {
400      /* Merge all interesting FDs into one set, since we have a
401         single dispatch per FD. */
402      if (FD_ISSET(i, &r) || FD_ISSET(i, &aim_wfds) || FD_ISSET(i, &e)) {
403        /* AIM HACK: no separate dispatch, just process here if
404           needed, and only once per run through. */
405        if (!aim_done && (FD_ISSET(i, &aim_rfds) || FD_ISSET(i, &aim_wfds))) {
406          owl_process_aim();
407          aim_done = 1;
408        }
409        else {
410          FD_SET(i, &r);
411        }
412      }
413    }
414    /* NOTE: the same dispatch function is called for both exceptional
415       and read ready FDs. */
416    owl_select_dispatch(&r, max_fd);
417  }
418}
Note: See TracBrowser for help on using the repository browser.