source: select.c @ 40bda84

release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 40bda84 was 40bda84, checked in by Nelson Elhage <nelhage@mit.edu>, 12 years ago
Catch ^Z and display a message instead of suspending.
  • Property mode set to 100644
File size: 10.6 KB
RevLine 
[9c7a701]1#include "owl.h"
2
[1895c29]3static int dispatch_active = 0;
4
[9cf96c8]5int _owl_select_timer_cmp(const owl_timer *t1, const owl_timer *t2) {
[ca1fc33a]6  return t1->time - t2->time;
[b7bb454]7}
8
[9cf96c8]9int _owl_select_timer_eq(const owl_timer *t1, const owl_timer *t2) {
[ca1fc33a]10  return t1 == t2;
[b7bb454]11}
12
[c675b39]13owl_timer *owl_select_add_timer(int after, int interval, void (*cb)(owl_timer *, void *), void (*destroy)(owl_timer*), void *data)
[b7bb454]14{
[ca1fc33a]15  owl_timer *t = owl_malloc(sizeof(owl_timer));
[58d1f8a]16  GList **timers = owl_global_get_timerlist(&g);
[b7bb454]17
[ca1fc33a]18  t->time = time(NULL) + after;
19  t->interval = interval;
20  t->callback = cb;
[c675b39]21  t->destroy = destroy;
[ca1fc33a]22  t->data = data;
[b7bb454]23
[58d1f8a]24  *timers = g_list_insert_sorted(*timers, t,
25                                 (GCompareFunc)_owl_select_timer_cmp);
[ca1fc33a]26  return t;
[b7bb454]27}
28
29void owl_select_remove_timer(owl_timer *t)
30{
[58d1f8a]31  GList **timers = owl_global_get_timerlist(&g);
32  if (t && g_list_find(*timers, t)) {
33    *timers = g_list_remove(*timers, t);
[c675b39]34    if(t->destroy) {
35      t->destroy(t);
36    }
[ca1fc33a]37    owl_free(t);
38  }
[b7bb454]39}
40
[3a84694]41void owl_select_process_timers(struct timespec *timeout)
[b7bb454]42{
[ca1fc33a]43  time_t now = time(NULL);
[58d1f8a]44  GList **timers = owl_global_get_timerlist(&g);
[b7bb454]45
[58d1f8a]46  while(*timers) {
47    owl_timer *t = (*timers)->data;
[c675b39]48    int remove = 0;
[b7bb454]49
[ca1fc33a]50    if(t->time > now)
51      break;
[b7bb454]52
[ca1fc33a]53    /* Reschedule if appropriate */
54    if(t->interval > 0) {
55      t->time = now + t->interval;
[58d1f8a]56      *timers = g_list_remove(*timers, t);
57      *timers = g_list_insert_sorted(*timers, t,
58                                     (GCompareFunc)_owl_select_timer_cmp);
[ca1fc33a]59    } else {
[c675b39]60      remove = 1;
[ca1fc33a]61    }
[b7bb454]62
[ca1fc33a]63    /* Do the callback */
[c675b39]64    t->callback(t, t->data);
65    if(remove) {
66      owl_select_remove_timer(t);
67    }
[ca1fc33a]68  }
[b7bb454]69
[58d1f8a]70  if(*timers) {
71    owl_timer *t = (*timers)->data;
[ca1fc33a]72    timeout->tv_sec = t->time - now;
[58d1f8a]73    if (timeout->tv_sec > 60)
74      timeout->tv_sec = 60;
75  } else {
76    timeout->tv_sec = 60;
[ca1fc33a]77  }
[b7bb454]78
[3a84694]79  timeout->tv_nsec = 0;
[b7bb454]80}
81
[9c7a701]82/* Returns the index of the dispatch for the file descriptor. */
83int owl_select_find_dispatch(int fd)
84{
85  int i, len;
[77bced3]86  const owl_list *dl;
[6249e137]87  const owl_dispatch *d;
[9c7a701]88 
89  dl = owl_global_get_dispatchlist(&g);
90  len = owl_list_get_size(dl);
91  for(i = 0; i < len; i++) {
[4d86e06]92    d = owl_list_get_element(dl, i);
[9c7a701]93    if (d->fd == fd) return i;
94  }
95  return -1;
96}
97
[1895c29]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);
[4d86e06]104  d = owl_list_get_element(dl, elt);
[1895c29]105  owl_list_remove_element(dl, elt);
106  if (d->destroy) {
107    d->destroy(d);
108  }
109}
110
[9c7a701]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
[1895c29]117  d->needs_gc = 0;
118
[9c7a701]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;
[4d86e06]124    d_old = owl_list_get_element(dl, elt);
[9c7a701]125    /* Ignore if we're adding the same dispatch again.  Otherwise
126       replace the old dispatch. */
127    if (d_old != d) {
[1895c29]128      owl_select_remove_dispatch_at(elt);
[9c7a701]129    }
130  }
[1895c29]131  owl_list_append_element(dl, d);
[9c7a701]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;
[1895c29]139  owl_dispatch *d;
[9c7a701]140
141  elt = owl_select_find_dispatch(fd);
[1895c29]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);
[4d86e06]147    d = owl_list_get_element(dl, elt);
[1895c29]148    d->needs_gc = 1;
149  } else {
150    owl_select_remove_dispatch_at(elt);
[9c7a701]151  }
152}
153
[c79a047]154int owl_select_dispatch_count(void)
[9c7a701]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) {
[4d86e06]165    d = owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
[f36cd97]166    if (d->cfunc != owl_perlconfig_dispatch) {
[9c7a701]167      /* don't mess with non-perl dispatch functions from here. */
168      return 1;
169    }
170  }
171
[27f6487]172  d = owl_malloc(sizeof(owl_dispatch));
[9c7a701]173  d->fd = fd;
[f36cd97]174  d->cfunc = owl_perlconfig_dispatch;
175  d->destroy = owl_perlconfig_dispatch_free;
176  d->data = cb;
[9c7a701]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) {
[4d86e06]188    d = owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
[f36cd97]189    if (d->cfunc == owl_perlconfig_dispatch) {
[1895c29]190      owl_select_remove_dispatch_at(elt);
[9c7a701]191      return 0;
192    }
193  }
194  return 1;
195}
196
[2f69081]197int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e)
[9c7a701]198{
199  int i, len, max_fd;
200  owl_dispatch *d;
[77bced3]201  const owl_list *dl;
[9c7a701]202
203  dl = owl_global_get_dispatchlist(&g);
204  FD_ZERO(r);
205  FD_ZERO(e);
206  max_fd = 0;
[69f47f8]207  len = owl_select_dispatch_count();
[9c7a701]208  for(i = 0; i < len; i++) {
[4d86e06]209    d = owl_list_get_element(dl, i);
[9c7a701]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
[c79a047]217void owl_select_gc(void)
[1895c29]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--) {
[6249e137]228    const owl_dispatch *d = owl_list_get_element(dl, i);
[1895c29]229    if(d->needs_gc) {
230      owl_select_remove_dispatch_at(i);
231    }
232  }
233}
234
[9c7a701]235void owl_select_dispatch(fd_set *fds, int max_fd)
236{
237  int i, len;
238  owl_dispatch *d;
[77bced3]239  const owl_list *dl;
[9c7a701]240
241  dl = owl_global_get_dispatchlist(&g);
242  len = owl_select_dispatch_count();
[1895c29]243
244  dispatch_active = 1;
245
[9c7a701]246  for(i = 0; i < len; i++) {
[4d86e06]247    d = owl_list_get_element(dl, i);
[18a54ee]248    /* While d shouldn't normally be null, the list may be altered by
249     * functions we dispatch to. */
[1895c29]250    if (d != NULL && !d->needs_gc && FD_ISSET(d->fd, fds)) {
[9c7a701]251      if (d->cfunc != NULL) {
[f36cd97]252        d->cfunc(d);
[9c7a701]253      }
254    }
255  }
[1895c29]256
257  dispatch_active = 0;
258  owl_select_gc();
[9c7a701]259}
260
[2f69081]261int owl_select_aim_hack(fd_set *rfds, fd_set *wfds)
[9c7a701]262{
263  aim_conn_t *cur;
264  aim_session_t *sess;
265  int max_fd;
266
[2f69081]267  FD_ZERO(rfds);
268  FD_ZERO(wfds);
[9c7a701]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) {
[2f69081]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     
[9c7a701]280      if (cur->fd > max_fd)
281        max_fd = cur->fd;
282    }
283  }
284  return max_fd;
285}
286
[5a35c708]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
[40bda84]327void owl_select_mask_signals(sigset_t *oldmask) {
328  sigset_t set;
329
330  sigemptyset(&set);
331  sigaddset(&set, SIGINT);
332  sigaddset(&set, SIGTSTP);
333  sigprocmask(SIG_BLOCK, &set, oldmask);
334}
335
336void owl_select_handle_intr(sigset_t *restore)
[3a84694]337{
338  owl_input in;
[0cb6c26]339
340  owl_global_unset_interrupted(&g);
[40bda84]341
342  sigprocmask(SIG_SETMASK, restore, NULL);
[0cb6c26]343
[3a84694]344  in.ch = in.uch = owl_global_get_startup_tio(&g)->c_cc[VINTR];
345  owl_process_input_char(in);
346}
347
[40bda84]348void owl_select_check_tstp() {
349  if(owl_global_is_sigstp(&g)) {
350    owl_function_makemsg("Use :suspend to suspend.");
351    owl_global_unset_got_sigstp(&g);
352  }
353}
354
[c79a047]355void owl_select(void)
[9c7a701]356{
[3a84694]357  int i, max_fd, aim_max_fd, aim_done, ret;
[9c7a701]358  fd_set r;
359  fd_set e;
[2f69081]360  fd_set aim_rfds, aim_wfds;
[3a84694]361  struct timespec timeout;
[0cb6c26]362  sigset_t mask;
[9c7a701]363
[b7bb454]364  owl_select_process_timers(&timeout);
[9c7a701]365
[40bda84]366  owl_select_mask_signals(&mask);
367
368  owl_select_check_tstp();
[3a84694]369  if(owl_global_is_interrupted(&g)) {
[40bda84]370    owl_select_handle_intr(&mask);
[3a84694]371    return;
372  }
373
[2f69081]374  max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e);
[9c7a701]375
376  /* AIM HACK:
377   *
378   *  The problem - I'm not sure where to hook into the owl/faim
379   *  interface to keep track of when the AIM socket(s) open and
380   *  close. In particular, the bosconn thing throws me off. So,
381   *  rather than register particular dispatchers for AIM, I look up
382   *  the relevant FDs and add them to select's watch lists, then
383   *  check for them individually before moving on to the other
384   *  dispatchers. --asedeno
385   */
386  aim_done = 1;
[2f69081]387  FD_ZERO(&aim_rfds);
388  FD_ZERO(&aim_wfds);
[9c7a701]389  if (owl_global_is_doaimevents(&g)) {
390    aim_done = 0;
[2f69081]391    aim_max_fd = owl_select_aim_hack(&aim_rfds, &aim_wfds);
[9c7a701]392    if (max_fd < aim_max_fd) max_fd = aim_max_fd;
393    for(i = 0; i <= aim_max_fd; i++) {
[2f69081]394      if (FD_ISSET(i, &aim_rfds)) {
[9c7a701]395        FD_SET(i, &r);
396        FD_SET(i, &e);
397      }
398    }
399  }
400  /* END AIM HACK */
[2f69081]401
[3a84694]402  ret = pselect(max_fd+1, &r, &aim_wfds, &e, &timeout, &mask);
403
404  if(ret < 0 && errno == EINTR) {
[40bda84]405    owl_select_check_tstp();
[3a84694]406    if(owl_global_is_interrupted(&g)) {
[40bda84]407      owl_select_handle_intr(NULL);
[3a84694]408    }
[40bda84]409    sigprocmask(SIG_SETMASK, &mask, NULL);
[3a84694]410    return;
411  }
412
[40bda84]413  sigprocmask(SIG_SETMASK, &mask, NULL);
[3a84694]414
415  if(ret > 0) {
[9c7a701]416    /* Merge fd_sets and clear AIM FDs. */
417    for(i = 0; i <= max_fd; i++) {
418      /* Merge all interesting FDs into one set, since we have a
419         single dispatch per FD. */
[2f69081]420      if (FD_ISSET(i, &r) || FD_ISSET(i, &aim_wfds) || FD_ISSET(i, &e)) {
[9c7a701]421        /* AIM HACK: no separate dispatch, just process here if
422           needed, and only once per run through. */
[2f69081]423        if (!aim_done && (FD_ISSET(i, &aim_rfds) || FD_ISSET(i, &aim_wfds))) {
[9c7a701]424          owl_process_aim();
425          aim_done = 1;
426        }
427        else {
428          FD_SET(i, &r);
429        }
430      }
431    }
432    /* NOTE: the same dispatch function is called for both exceptional
433       and read ready FDs. */
434    owl_select_dispatch(&r, max_fd);
435  }
436}
Note: See TracBrowser for help on using the repository browser.