[9c7a701] | 1 | #include "owl.h" |
---|
| 2 | |
---|
[1895c29] | 3 | static int dispatch_active = 0; |
---|
| 4 | |
---|
[9cf96c8] | 5 | int _owl_select_timer_cmp(const owl_timer *t1, const owl_timer *t2) { |
---|
[ca1fc33a] | 6 | return t1->time - t2->time; |
---|
[b7bb454] | 7 | } |
---|
| 8 | |
---|
[9cf96c8] | 9 | int _owl_select_timer_eq(const owl_timer *t1, const owl_timer *t2) { |
---|
[ca1fc33a] | 10 | return t1 == t2; |
---|
[b7bb454] | 11 | } |
---|
| 12 | |
---|
[c675b39] | 13 | owl_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 | |
---|
| 29 | void 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] | 41 | void 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. */ |
---|
| 83 | int 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] | 98 | void 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. */ |
---|
| 112 | void 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. */ |
---|
| 135 | void 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] | 154 | int owl_select_dispatch_count(void) |
---|
[9c7a701] | 155 | { |
---|
| 156 | return owl_list_get_size(owl_global_get_dispatchlist(&g)); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | int 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 | |
---|
| 181 | int 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] | 197 | int 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] | 217 | void 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] | 235 | void 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] | 261 | int 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] | 287 | void 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 | |
---|
[c79a047] | 327 | void owl_select_handle_intr(void) |
---|
[3a84694] | 328 | { |
---|
| 329 | owl_input in; |
---|
[0cb6c26] | 330 | |
---|
| 331 | owl_global_unset_interrupted(&g); |
---|
| 332 | owl_function_unmask_sigint(NULL); |
---|
| 333 | |
---|
[3a84694] | 334 | in.ch = in.uch = owl_global_get_startup_tio(&g)->c_cc[VINTR]; |
---|
| 335 | owl_process_input_char(in); |
---|
| 336 | } |
---|
| 337 | |
---|
[c79a047] | 338 | void owl_select(void) |
---|
[9c7a701] | 339 | { |
---|
[3a84694] | 340 | int i, max_fd, aim_max_fd, aim_done, ret; |
---|
[9c7a701] | 341 | fd_set r; |
---|
| 342 | fd_set e; |
---|
[2f69081] | 343 | fd_set aim_rfds, aim_wfds; |
---|
[3a84694] | 344 | struct timespec timeout; |
---|
[0cb6c26] | 345 | sigset_t mask; |
---|
[9c7a701] | 346 | |
---|
[b7bb454] | 347 | owl_select_process_timers(&timeout); |
---|
[9c7a701] | 348 | |
---|
[0cb6c26] | 349 | owl_function_mask_sigint(&mask); |
---|
[3a84694] | 350 | if(owl_global_is_interrupted(&g)) { |
---|
[0cb6c26] | 351 | owl_select_handle_intr(); |
---|
[3a84694] | 352 | return; |
---|
| 353 | } |
---|
| 354 | |
---|
[2f69081] | 355 | max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e); |
---|
[9c7a701] | 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; |
---|
[2f69081] | 368 | FD_ZERO(&aim_rfds); |
---|
| 369 | FD_ZERO(&aim_wfds); |
---|
[9c7a701] | 370 | if (owl_global_is_doaimevents(&g)) { |
---|
| 371 | aim_done = 0; |
---|
[2f69081] | 372 | aim_max_fd = owl_select_aim_hack(&aim_rfds, &aim_wfds); |
---|
[9c7a701] | 373 | if (max_fd < aim_max_fd) max_fd = aim_max_fd; |
---|
| 374 | for(i = 0; i <= aim_max_fd; i++) { |
---|
[2f69081] | 375 | if (FD_ISSET(i, &aim_rfds)) { |
---|
[9c7a701] | 376 | FD_SET(i, &r); |
---|
| 377 | FD_SET(i, &e); |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | } |
---|
| 381 | /* END AIM HACK */ |
---|
[2f69081] | 382 | |
---|
[3a84694] | 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 | |
---|
[0cb6c26] | 393 | owl_function_unmask_sigint(NULL); |
---|
[3a84694] | 394 | |
---|
| 395 | if(ret > 0) { |
---|
[9c7a701] | 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. */ |
---|
[2f69081] | 400 | if (FD_ISSET(i, &r) || FD_ISSET(i, &aim_wfds) || FD_ISSET(i, &e)) { |
---|
[9c7a701] | 401 | /* AIM HACK: no separate dispatch, just process here if |
---|
| 402 | needed, and only once per run through. */ |
---|
[2f69081] | 403 | if (!aim_done && (FD_ISSET(i, &aim_rfds) || FD_ISSET(i, &aim_wfds))) { |
---|
[9c7a701] | 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 | } |
---|