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