source: select.c @ 72ab15f

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 72ab15f 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
RevLine 
[9c7a701]1#include "owl.h"
2
3static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $";
4
[1895c29]5static int dispatch_active = 0;
6
[58d1f8a]7int _owl_select_timer_cmp(owl_timer *t1, owl_timer *t2) {
[ca1fc33a]8  return t1->time - t2->time;
[b7bb454]9}
10
[58d1f8a]11int _owl_select_timer_eq(owl_timer *t1, owl_timer *t2) {
[ca1fc33a]12  return t1 == t2;
[b7bb454]13}
14
[c675b39]15owl_timer *owl_select_add_timer(int after, int interval, void (*cb)(owl_timer *, void *), void (*destroy)(owl_timer*), void *data)
[b7bb454]16{
[ca1fc33a]17  owl_timer *t = owl_malloc(sizeof(owl_timer));
[58d1f8a]18  GList **timers = owl_global_get_timerlist(&g);
[b7bb454]19
[ca1fc33a]20  t->time = time(NULL) + after;
21  t->interval = interval;
22  t->callback = cb;
[c675b39]23  t->destroy = destroy;
[ca1fc33a]24  t->data = data;
[b7bb454]25
[58d1f8a]26  *timers = g_list_insert_sorted(*timers, t,
27                                 (GCompareFunc)_owl_select_timer_cmp);
[ca1fc33a]28  return t;
[b7bb454]29}
30
31void owl_select_remove_timer(owl_timer *t)
32{
[58d1f8a]33  GList **timers = owl_global_get_timerlist(&g);
34  if (t && g_list_find(*timers, t)) {
35    *timers = g_list_remove(*timers, t);
[c675b39]36    if(t->destroy) {
37      t->destroy(t);
38    }
[ca1fc33a]39    owl_free(t);
40  }
[b7bb454]41}
42
[3a84694]43void owl_select_process_timers(struct timespec *timeout)
[b7bb454]44{
[ca1fc33a]45  time_t now = time(NULL);
[58d1f8a]46  GList **timers = owl_global_get_timerlist(&g);
[b7bb454]47
[58d1f8a]48  while(*timers) {
49    owl_timer *t = (*timers)->data;
[c675b39]50    int remove = 0;
[b7bb454]51
[ca1fc33a]52    if(t->time > now)
53      break;
[b7bb454]54
[ca1fc33a]55    /* Reschedule if appropriate */
56    if(t->interval > 0) {
57      t->time = now + t->interval;
[58d1f8a]58      *timers = g_list_remove(*timers, t);
59      *timers = g_list_insert_sorted(*timers, t,
60                                     (GCompareFunc)_owl_select_timer_cmp);
[ca1fc33a]61    } else {
[c675b39]62      remove = 1;
[ca1fc33a]63    }
[b7bb454]64
[ca1fc33a]65    /* Do the callback */
[c675b39]66    t->callback(t, t->data);
67    if(remove) {
68      owl_select_remove_timer(t);
69    }
[ca1fc33a]70  }
[b7bb454]71
[58d1f8a]72  if(*timers) {
73    owl_timer *t = (*timers)->data;
[ca1fc33a]74    timeout->tv_sec = t->time - now;
[58d1f8a]75    if (timeout->tv_sec > 60)
76      timeout->tv_sec = 60;
77  } else {
78    timeout->tv_sec = 60;
[ca1fc33a]79  }
[b7bb454]80
[3a84694]81  timeout->tv_nsec = 0;
[b7bb454]82}
83
[9c7a701]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
[1895c29]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
[9c7a701]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
[1895c29]119  d->needs_gc = 0;
120
[9c7a701]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) {
[1895c29]130      owl_select_remove_dispatch_at(elt);
[9c7a701]131    }
132  }
[1895c29]133  owl_list_append_element(dl, d);
[9c7a701]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;
[1895c29]141  owl_dispatch *d;
[9c7a701]142
143  elt = owl_select_find_dispatch(fd);
[1895c29]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);
[9c7a701]149    d = (owl_dispatch*)owl_list_get_element(dl, elt);
[1895c29]150    d->needs_gc = 1;
151  } else {
152    owl_select_remove_dispatch_at(elt);
[9c7a701]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);
[f36cd97]168    if (d->cfunc != owl_perlconfig_dispatch) {
[9c7a701]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;
[f36cd97]176  d->cfunc = owl_perlconfig_dispatch;
177  d->destroy = owl_perlconfig_dispatch_free;
178  d->data = cb;
[9c7a701]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);
[f36cd97]191    if (d->cfunc == owl_perlconfig_dispatch) {
[1895c29]192      owl_select_remove_dispatch_at(elt);
[9c7a701]193      return 0;
194    }
195  }
196  return 1;
197}
198
[2f69081]199int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e)
[9c7a701]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
[1895c29]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
[9c7a701]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();
[1895c29]245
246  dispatch_active = 1;
247
[9c7a701]248  for(i = 0; i < len; i++) {
249    d = (owl_dispatch*)owl_list_get_element(dl, i);
[18a54ee]250    /* While d shouldn't normally be null, the list may be altered by
251     * functions we dispatch to. */
[1895c29]252    if (d != NULL && !d->needs_gc && FD_ISSET(d->fd, fds)) {
[9c7a701]253      if (d->cfunc != NULL) {
[f36cd97]254        d->cfunc(d);
[9c7a701]255      }
256    }
257  }
[1895c29]258
259  dispatch_active = 0;
260  owl_select_gc();
[9c7a701]261}
262
[2f69081]263int owl_select_aim_hack(fd_set *rfds, fd_set *wfds)
[9c7a701]264{
265  aim_conn_t *cur;
266  aim_session_t *sess;
267  int max_fd;
268
[2f69081]269  FD_ZERO(rfds);
270  FD_ZERO(wfds);
[9c7a701]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) {
[2f69081]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     
[9c7a701]282      if (cur->fd > max_fd)
283        max_fd = cur->fd;
284    }
285  }
286  return max_fd;
287}
288
[5a35c708]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
[3a84694]329void owl_select_handle_intr()
330{
331  owl_input in;
[0cb6c26]332
333  owl_global_unset_interrupted(&g);
334  owl_function_unmask_sigint(NULL);
335
[3a84694]336  in.ch = in.uch = owl_global_get_startup_tio(&g)->c_cc[VINTR];
337  owl_process_input_char(in);
338}
339
[9c7a701]340void owl_select()
341{
[3a84694]342  int i, max_fd, aim_max_fd, aim_done, ret;
[9c7a701]343  fd_set r;
344  fd_set e;
[2f69081]345  fd_set aim_rfds, aim_wfds;
[3a84694]346  struct timespec timeout;
[0cb6c26]347  sigset_t mask;
[9c7a701]348
[b7bb454]349  owl_select_process_timers(&timeout);
[9c7a701]350
[0cb6c26]351  owl_function_mask_sigint(&mask);
[3a84694]352  if(owl_global_is_interrupted(&g)) {
[0cb6c26]353    owl_select_handle_intr();
[3a84694]354    return;
355  }
356
[2f69081]357  max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e);
[9c7a701]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;
[2f69081]370  FD_ZERO(&aim_rfds);
371  FD_ZERO(&aim_wfds);
[9c7a701]372  if (owl_global_is_doaimevents(&g)) {
373    aim_done = 0;
[2f69081]374    aim_max_fd = owl_select_aim_hack(&aim_rfds, &aim_wfds);
[9c7a701]375    if (max_fd < aim_max_fd) max_fd = aim_max_fd;
376    for(i = 0; i <= aim_max_fd; i++) {
[2f69081]377      if (FD_ISSET(i, &aim_rfds)) {
[9c7a701]378        FD_SET(i, &r);
379        FD_SET(i, &e);
380      }
381    }
382  }
383  /* END AIM HACK */
[2f69081]384
[3a84694]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
[0cb6c26]395  owl_function_unmask_sigint(NULL);
[3a84694]396
397  if(ret > 0) {
[9c7a701]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. */
[2f69081]402      if (FD_ISSET(i, &r) || FD_ISSET(i, &aim_wfds) || FD_ISSET(i, &e)) {
[9c7a701]403        /* AIM HACK: no separate dispatch, just process here if
404           needed, and only once per run through. */
[2f69081]405        if (!aim_done && (FD_ISSET(i, &aim_rfds) || FD_ISSET(i, &aim_wfds))) {
[9c7a701]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.