source: select.c @ 28cf94b

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