| 1 | #include "owl.h" |
|---|
| 2 | #include <stdio.h> |
|---|
| 3 | #include <sys/ioctl.h> |
|---|
| 4 | |
|---|
| 5 | static void _owl_global_init_windows(owl_global *g); |
|---|
| 6 | |
|---|
| 7 | void owl_global_init(owl_global *g) { |
|---|
| 8 | char *cd; |
|---|
| 9 | const char *homedir; |
|---|
| 10 | |
|---|
| 11 | #if !GLIB_CHECK_VERSION(2, 35, 0) |
|---|
| 12 | g_type_init(); |
|---|
| 13 | #endif |
|---|
| 14 | #if !GLIB_CHECK_VERSION(2, 31, 0) |
|---|
| 15 | g_thread_init(NULL); |
|---|
| 16 | #endif |
|---|
| 17 | |
|---|
| 18 | owl_select_init(); |
|---|
| 19 | |
|---|
| 20 | g->lines=LINES; |
|---|
| 21 | g->cols=COLS; |
|---|
| 22 | /* We shouldn't need this if we initialize lines and cols before the first |
|---|
| 23 | * owl_window_get_screen, but to be safe, we synchronize. */ |
|---|
| 24 | owl_window_resize(owl_window_get_screen(), g->lines, g->cols); |
|---|
| 25 | |
|---|
| 26 | g->context_stack = NULL; |
|---|
| 27 | owl_global_push_context(g, OWL_CTX_STARTUP, NULL, NULL, NULL); |
|---|
| 28 | |
|---|
| 29 | g->curmsg=0; |
|---|
| 30 | g->topmsg=0; |
|---|
| 31 | g->markedmsgid=-1; |
|---|
| 32 | g->startupargs=NULL; |
|---|
| 33 | |
|---|
| 34 | owl_variable_dict_setup(&(g->vars)); |
|---|
| 35 | |
|---|
| 36 | g->rightshift=0; |
|---|
| 37 | |
|---|
| 38 | g->pw = NULL; |
|---|
| 39 | g->vw = NULL; |
|---|
| 40 | g->tw = NULL; |
|---|
| 41 | |
|---|
| 42 | owl_keyhandler_init(&g->kh); |
|---|
| 43 | owl_keys_setup_keymaps(&g->kh); |
|---|
| 44 | |
|---|
| 45 | owl_dict_create(&(g->filters)); |
|---|
| 46 | g->filterlist = NULL; |
|---|
| 47 | g->puntlist = g_ptr_array_new(); |
|---|
| 48 | g->messagequeue = g_queue_new(); |
|---|
| 49 | owl_dict_create(&(g->styledict)); |
|---|
| 50 | g->curmsg_vert_offset=0; |
|---|
| 51 | g->resizepending=0; |
|---|
| 52 | g->direction=OWL_DIRECTION_DOWNWARDS; |
|---|
| 53 | owl_fmtext_init_colorpair_mgr(&(g->cpmgr)); |
|---|
| 54 | g->debug=OWL_DEBUG; |
|---|
| 55 | owl_regex_init(&g->search_re); |
|---|
| 56 | g->starttime=time(NULL); /* assumes we call init only a start time */ |
|---|
| 57 | g->lastinputtime=g->starttime; |
|---|
| 58 | g->newmsgproc_pid=0; |
|---|
| 59 | |
|---|
| 60 | owl_global_set_config_format(g, 0); |
|---|
| 61 | owl_global_set_no_have_config(g); |
|---|
| 62 | owl_history_init(&(g->msghist)); |
|---|
| 63 | owl_history_init(&(g->cmdhist)); |
|---|
| 64 | g->nextmsgid=0; |
|---|
| 65 | |
|---|
| 66 | /* Fill in some variables which don't have constant defaults */ |
|---|
| 67 | |
|---|
| 68 | /* glib's g_get_home_dir prefers passwd entries to $HOME, so we |
|---|
| 69 | * explicitly check getenv first. */ |
|---|
| 70 | homedir = getenv("HOME"); |
|---|
| 71 | if (!homedir) |
|---|
| 72 | homedir = g_get_home_dir(); |
|---|
| 73 | g->homedir = g_strdup(homedir); |
|---|
| 74 | |
|---|
| 75 | g->confdir = NULL; |
|---|
| 76 | g->startupfile = NULL; |
|---|
| 77 | cd = g_build_filename(g->homedir, OWL_CONFIG_DIR, NULL); |
|---|
| 78 | owl_global_set_confdir(g, cd); |
|---|
| 79 | g_free(cd); |
|---|
| 80 | |
|---|
| 81 | g->msglist = owl_messagelist_new(); |
|---|
| 82 | |
|---|
| 83 | _owl_global_init_windows(g); |
|---|
| 84 | |
|---|
| 85 | g->aim_screenname=NULL; |
|---|
| 86 | g->aim_screenname_for_filters=NULL; |
|---|
| 87 | g->aim_loggedin=0; |
|---|
| 88 | owl_buddylist_init(&(g->buddylist)); |
|---|
| 89 | |
|---|
| 90 | g->havezephyr=0; |
|---|
| 91 | g->haveaim=0; |
|---|
| 92 | g->ignoreaimlogin=0; |
|---|
| 93 | owl_global_set_no_doaimevents(g); |
|---|
| 94 | |
|---|
| 95 | owl_errqueue_init(&(g->errqueue)); |
|---|
| 96 | |
|---|
| 97 | owl_zbuddylist_create(&(g->zbuddies)); |
|---|
| 98 | |
|---|
| 99 | g->zaldlist = NULL; |
|---|
| 100 | g->pseudologin_notify = 0; |
|---|
| 101 | |
|---|
| 102 | owl_message_init_fmtext_cache(); |
|---|
| 103 | g->kill_buffer = NULL; |
|---|
| 104 | |
|---|
| 105 | g->interrupt_count = 0; |
|---|
| 106 | #if GLIB_CHECK_VERSION(2, 31, 0) |
|---|
| 107 | g_mutex_init(&g->interrupt_lock); |
|---|
| 108 | #else |
|---|
| 109 | g->interrupt_lock = g_mutex_new(); |
|---|
| 110 | #endif |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | static void _owl_global_init_windows(owl_global *g) |
|---|
| 114 | { |
|---|
| 115 | /* Create the main window */ |
|---|
| 116 | owl_mainpanel_init(&(g->mainpanel)); |
|---|
| 117 | |
|---|
| 118 | /* Create the widgets */ |
|---|
| 119 | g->mw = owl_mainwin_new(g->mainpanel.recwin); |
|---|
| 120 | owl_msgwin_init(&(g->msgwin), g->mainpanel.msgwin); |
|---|
| 121 | owl_sepbar_init(g->mainpanel.sepwin); |
|---|
| 122 | |
|---|
| 123 | owl_window_set_default_cursor(g->mainpanel.sepwin); |
|---|
| 124 | |
|---|
| 125 | /* set up a pad for input */ |
|---|
| 126 | g->input_pad = newpad(1, 1); |
|---|
| 127 | nodelay(g->input_pad, 1); |
|---|
| 128 | keypad(g->input_pad, 1); |
|---|
| 129 | meta(g->input_pad, 1); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | void owl_global_sepbar_dirty(owl_global *g) |
|---|
| 133 | { |
|---|
| 134 | owl_window_dirty(g->mainpanel.sepwin); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /* Called once perl has been initialized */ |
|---|
| 138 | void owl_global_complete_setup(owl_global *g) |
|---|
| 139 | { |
|---|
| 140 | owl_cmddict_setup(&(g->cmds)); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | owl_context *owl_global_get_context(const owl_global *g) { |
|---|
| 144 | if (!g->context_stack) |
|---|
| 145 | return NULL; |
|---|
| 146 | return g->context_stack->data; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | static void owl_global_activate_context(owl_global *g, owl_context *c) { |
|---|
| 150 | if (!c) |
|---|
| 151 | return; |
|---|
| 152 | |
|---|
| 153 | if (c->keymap) { |
|---|
| 154 | if (!owl_keyhandler_activate(owl_global_get_keyhandler(g), c->keymap)) { |
|---|
| 155 | owl_function_error("Unable to activate keymap '%s'", c->keymap); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | owl_window_set_cursor(c->cursor); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | void owl_global_push_context(owl_global *g, int mode, void *data, const char *keymap, owl_window *cursor) { |
|---|
| 162 | owl_context *c; |
|---|
| 163 | c = owl_context_new(mode, data, keymap, cursor); |
|---|
| 164 | owl_global_push_context_obj(g, c); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | void owl_global_push_context_obj(owl_global *g, owl_context *c) |
|---|
| 168 | { |
|---|
| 169 | g->context_stack = g_list_prepend(g->context_stack, c); |
|---|
| 170 | owl_global_activate_context(g, owl_global_get_context(g)); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /* Pops the current context from the context stack and returns it. Caller is |
|---|
| 174 | * responsible for freeing. */ |
|---|
| 175 | CALLER_OWN owl_context *owl_global_pop_context_no_delete(owl_global *g) |
|---|
| 176 | { |
|---|
| 177 | owl_context *c; |
|---|
| 178 | if (!g->context_stack) |
|---|
| 179 | return NULL; |
|---|
| 180 | c = owl_global_get_context(g); |
|---|
| 181 | owl_context_deactivate(c); |
|---|
| 182 | g->context_stack = g_list_delete_link(g->context_stack, |
|---|
| 183 | g->context_stack); |
|---|
| 184 | owl_global_activate_context(g, owl_global_get_context(g)); |
|---|
| 185 | return c; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /* Pops the current context from the context stack and deletes it. */ |
|---|
| 189 | void owl_global_pop_context(owl_global *g) { |
|---|
| 190 | owl_context *c; |
|---|
| 191 | c = owl_global_pop_context_no_delete(g); |
|---|
| 192 | if (c) |
|---|
| 193 | owl_context_delete(c); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | int owl_global_get_lines(const owl_global *g) { |
|---|
| 197 | return(g->lines); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | int owl_global_get_cols(const owl_global *g) { |
|---|
| 201 | return(g->cols); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | int owl_global_get_recwin_lines(const owl_global *g) { |
|---|
| 205 | return g->mainpanel.recwinlines; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | /* curmsg */ |
|---|
| 209 | |
|---|
| 210 | int owl_global_get_curmsg(const owl_global *g) { |
|---|
| 211 | return(g->curmsg); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | void owl_global_set_curmsg(owl_global *g, int i) { |
|---|
| 215 | g->curmsg=i; |
|---|
| 216 | /* we will reset the vertical offset from here */ |
|---|
| 217 | /* we might want to move this out to the functions later */ |
|---|
| 218 | owl_global_set_curmsg_vert_offset(g, 0); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | /* topmsg */ |
|---|
| 222 | |
|---|
| 223 | int owl_global_get_topmsg(const owl_global *g) { |
|---|
| 224 | return(g->topmsg); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | void owl_global_set_topmsg(owl_global *g, int i) { |
|---|
| 228 | g->topmsg=i; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | /* markedmsgid */ |
|---|
| 232 | |
|---|
| 233 | int owl_global_get_markedmsgid(const owl_global *g) { |
|---|
| 234 | return(g->markedmsgid); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | void owl_global_set_markedmsgid(owl_global *g, int i) { |
|---|
| 238 | g->markedmsgid=i; |
|---|
| 239 | /* i; index of message in the current view. |
|---|
| 240 | const owl_message *m; |
|---|
| 241 | owl_view *v; |
|---|
| 242 | |
|---|
| 243 | v = owl_global_get_current_view(&g); |
|---|
| 244 | m = owl_view_get_element(v, i); |
|---|
| 245 | g->markedmsgid = m ? owl_message_get_id(m) : 0; |
|---|
| 246 | */ |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | /* windows */ |
|---|
| 250 | |
|---|
| 251 | owl_mainwin *owl_global_get_mainwin(owl_global *g) |
|---|
| 252 | { |
|---|
| 253 | return g->mw; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | owl_popwin *owl_global_get_popwin(owl_global *g) { |
|---|
| 257 | return g->pw; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | void owl_global_set_popwin(owl_global *g, owl_popwin *pw) { |
|---|
| 261 | g->pw = pw; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | /* msglist */ |
|---|
| 265 | |
|---|
| 266 | owl_messagelist *owl_global_get_msglist(owl_global *g) { |
|---|
| 267 | return g->msglist; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | /* keyhandler */ |
|---|
| 271 | |
|---|
| 272 | owl_keyhandler *owl_global_get_keyhandler(owl_global *g) { |
|---|
| 273 | return(&(g->kh)); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | /* Gets the currently active typwin out of the current context. */ |
|---|
| 277 | owl_editwin *owl_global_current_typwin(const owl_global *g) { |
|---|
| 278 | owl_context *ctx = owl_global_get_context(g); |
|---|
| 279 | return owl_editcontext_get_editwin(ctx); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | /* variable dictionary */ |
|---|
| 283 | |
|---|
| 284 | owl_vardict *owl_global_get_vardict(owl_global *g) { |
|---|
| 285 | return &(g->vars); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | /* command dictionary */ |
|---|
| 289 | |
|---|
| 290 | owl_cmddict *owl_global_get_cmddict(owl_global *g) { |
|---|
| 291 | return &(g->cmds); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | /* rightshift */ |
|---|
| 295 | |
|---|
| 296 | void owl_global_set_rightshift(owl_global *g, int i) { |
|---|
| 297 | g->rightshift = i; |
|---|
| 298 | owl_mainwin_redisplay(owl_global_get_mainwin(g)); |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | int owl_global_get_rightshift(const owl_global *g) { |
|---|
| 302 | return(g->rightshift); |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | /* typwin */ |
|---|
| 306 | |
|---|
| 307 | owl_editwin *owl_global_set_typwin_active(owl_global *g, int style, owl_history *hist) { |
|---|
| 308 | int d; |
|---|
| 309 | d = owl_global_get_typewindelta(g); |
|---|
| 310 | if (d > 0 && style == OWL_EDITWIN_STYLE_MULTILINE) |
|---|
| 311 | owl_function_resize_typwin(owl_global_get_typwin_lines(g) + d); |
|---|
| 312 | |
|---|
| 313 | if (g->typwin_erase_id) { |
|---|
| 314 | g_signal_handler_disconnect(g->mainpanel.typwin, g->typwin_erase_id); |
|---|
| 315 | g->typwin_erase_id = 0; |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | g->tw = owl_editwin_new(g->mainpanel.typwin, |
|---|
| 319 | owl_global_get_typwin_lines(g), |
|---|
| 320 | g->cols, |
|---|
| 321 | style, |
|---|
| 322 | hist); |
|---|
| 323 | return g->tw; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | void owl_global_deactivate_editcontext(owl_context *ctx) { |
|---|
| 327 | owl_global *g = ctx->cbdata; |
|---|
| 328 | owl_global_set_typwin_inactive(g); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | void owl_global_set_typwin_inactive(owl_global *g) { |
|---|
| 332 | int d = owl_global_get_typewindelta(g); |
|---|
| 333 | if (d > 0 && owl_editwin_get_style(g->tw) == OWL_EDITWIN_STYLE_MULTILINE) |
|---|
| 334 | owl_function_resize_typwin(owl_global_get_typwin_lines(g) - d); |
|---|
| 335 | |
|---|
| 336 | if (!g->typwin_erase_id) { |
|---|
| 337 | g->typwin_erase_id = |
|---|
| 338 | g_signal_connect(g->mainpanel.typwin, "redraw", G_CALLBACK(owl_window_erase_cb), NULL); |
|---|
| 339 | } |
|---|
| 340 | owl_window_dirty(g->mainpanel.typwin); |
|---|
| 341 | |
|---|
| 342 | owl_editwin_unref(g->tw); |
|---|
| 343 | g->tw = NULL; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | /* resize */ |
|---|
| 347 | |
|---|
| 348 | void owl_global_set_resize_pending(owl_global *g) { |
|---|
| 349 | g->resizepending = true; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | const char *owl_global_get_homedir(const owl_global *g) { |
|---|
| 353 | if (g->homedir) return(g->homedir); |
|---|
| 354 | return("/"); |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | const char *owl_global_get_confdir(const owl_global *g) { |
|---|
| 358 | if (g->confdir) return(g->confdir); |
|---|
| 359 | return("/"); |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | /* |
|---|
| 363 | * Setting this also sets startupfile to confdir/startup |
|---|
| 364 | */ |
|---|
| 365 | void owl_global_set_confdir(owl_global *g, const char *cd) { |
|---|
| 366 | g_free(g->confdir); |
|---|
| 367 | g->confdir = g_strdup(cd); |
|---|
| 368 | g_free(g->startupfile); |
|---|
| 369 | g->startupfile = g_build_filename(cd, "startup", NULL); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | const char *owl_global_get_startupfile(const owl_global *g) { |
|---|
| 373 | if(g->startupfile) return(g->startupfile); |
|---|
| 374 | return("/"); |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | int owl_global_get_direction(const owl_global *g) { |
|---|
| 378 | return(g->direction); |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | void owl_global_set_direction_downwards(owl_global *g) { |
|---|
| 382 | g->direction=OWL_DIRECTION_DOWNWARDS; |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | void owl_global_set_direction_upwards(owl_global *g) { |
|---|
| 386 | g->direction=OWL_DIRECTION_UPWARDS; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | /* perl stuff */ |
|---|
| 390 | |
|---|
| 391 | void owl_global_set_perlinterp(owl_global *g, void *p) { |
|---|
| 392 | g->perl=p; |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | void *owl_global_get_perlinterp(const owl_global *g) { |
|---|
| 396 | return(g->perl); |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | int owl_global_is_config_format(const owl_global *g) { |
|---|
| 400 | if (g->config_format) return(1); |
|---|
| 401 | return(0); |
|---|
| 402 | } |
|---|
| 403 | |
|---|
| 404 | void owl_global_set_config_format(owl_global *g, int state) { |
|---|
| 405 | if (state==1) { |
|---|
| 406 | g->config_format=1; |
|---|
| 407 | } else { |
|---|
| 408 | g->config_format=0; |
|---|
| 409 | } |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | void owl_global_set_have_config(owl_global *g) { |
|---|
| 413 | g->haveconfig=1; |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | void owl_global_set_no_have_config(owl_global *g) { |
|---|
| 417 | g->haveconfig=0; |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | int owl_global_have_config(owl_global *g) { |
|---|
| 421 | if (g->haveconfig) return(1); |
|---|
| 422 | return(0); |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | /* |
|---|
| 426 | * Compute the size of the terminal. Try a ioctl, fallback to other stuff on |
|---|
| 427 | * fail. |
|---|
| 428 | */ |
|---|
| 429 | void owl_global_get_terminal_size(int *lines, int *cols) { |
|---|
| 430 | struct winsize size; |
|---|
| 431 | /* get the new size */ |
|---|
| 432 | ioctl(STDIN_FILENO, TIOCGWINSZ, &size); |
|---|
| 433 | if (size.ws_row) { |
|---|
| 434 | *lines = size.ws_row; |
|---|
| 435 | } else { |
|---|
| 436 | *lines = LINES; |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | if (size.ws_col) { |
|---|
| 440 | *cols = size.ws_col; |
|---|
| 441 | } else { |
|---|
| 442 | *cols = COLS; |
|---|
| 443 | } |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | void owl_global_check_resize(owl_global *g) { |
|---|
| 447 | /* resize the screen. If lines or cols is 0 use the terminal size */ |
|---|
| 448 | if (!g->resizepending) return; |
|---|
| 449 | g->resizepending = false; |
|---|
| 450 | |
|---|
| 451 | owl_global_get_terminal_size(&g->lines, &g->cols); |
|---|
| 452 | owl_window_resize(owl_window_get_screen(), g->lines, g->cols); |
|---|
| 453 | |
|---|
| 454 | owl_function_debugmsg("New size is %i lines, %i cols.", g->lines, g->cols); |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | /* debug */ |
|---|
| 458 | |
|---|
| 459 | int owl_global_is_debug_fast(const owl_global *g) { |
|---|
| 460 | if (g->debug) return(1); |
|---|
| 461 | return(0); |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | /* starttime */ |
|---|
| 465 | |
|---|
| 466 | time_t owl_global_get_starttime(const owl_global *g) { |
|---|
| 467 | return(g->starttime); |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | time_t owl_global_get_runtime(const owl_global *g) { |
|---|
| 471 | return(time(NULL)-g->starttime); |
|---|
| 472 | } |
|---|
| 473 | |
|---|
| 474 | time_t owl_global_get_lastinputtime(const owl_global *g) { |
|---|
| 475 | return(g->lastinputtime); |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | void owl_global_set_lastinputtime(owl_global *g, time_t time) { |
|---|
| 479 | g->lastinputtime = time; |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | time_t owl_global_get_idletime(const owl_global *g) { |
|---|
| 483 | return(time(NULL)-g->lastinputtime); |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | /* viewwin */ |
|---|
| 487 | |
|---|
| 488 | owl_viewwin *owl_global_get_viewwin(owl_global *g) { |
|---|
| 489 | return g->vw; |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | void owl_global_set_viewwin(owl_global *g, owl_viewwin *vw) { |
|---|
| 493 | g->vw = vw; |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | /* vert offset */ |
|---|
| 498 | |
|---|
| 499 | int owl_global_get_curmsg_vert_offset(const owl_global *g) { |
|---|
| 500 | return(g->curmsg_vert_offset); |
|---|
| 501 | } |
|---|
| 502 | |
|---|
| 503 | void owl_global_set_curmsg_vert_offset(owl_global *g, int i) { |
|---|
| 504 | g->curmsg_vert_offset = i; |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | /* startup args */ |
|---|
| 508 | |
|---|
| 509 | void owl_global_set_startupargs(owl_global *g, int argc, char **argv) { |
|---|
| 510 | g_free(g->startupargs); |
|---|
| 511 | g->startupargs = g_strjoinv(" ", argv); |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | const char *owl_global_get_startupargs(const owl_global *g) { |
|---|
| 515 | if (g->startupargs) return(g->startupargs); |
|---|
| 516 | return(""); |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | /* history */ |
|---|
| 520 | |
|---|
| 521 | owl_history *owl_global_get_msg_history(owl_global *g) { |
|---|
| 522 | return(&(g->msghist)); |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | owl_history *owl_global_get_cmd_history(owl_global *g) { |
|---|
| 526 | return(&(g->cmdhist)); |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | /* filterlist */ |
|---|
| 530 | typedef struct _owl_global_filter_ent { /* noproto */ |
|---|
| 531 | owl_global *g; |
|---|
| 532 | owl_filter *f; |
|---|
| 533 | } owl_global_filter_ent; |
|---|
| 534 | |
|---|
| 535 | owl_filter *owl_global_get_filter(const owl_global *g, const char *name) { |
|---|
| 536 | owl_global_filter_ent *e = owl_dict_find_element(&(g->filters), name); |
|---|
| 537 | if (e) return e->f; |
|---|
| 538 | return NULL; |
|---|
| 539 | } |
|---|
| 540 | |
|---|
| 541 | static void owl_global_delete_filter_ent(void *data) |
|---|
| 542 | { |
|---|
| 543 | owl_global_filter_ent *e = data; |
|---|
| 544 | e->g->filterlist = g_list_remove(e->g->filterlist, e->f); |
|---|
| 545 | owl_filter_delete(e->f); |
|---|
| 546 | g_free(e); |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | void owl_global_add_filter(owl_global *g, owl_filter *f) { |
|---|
| 550 | owl_global_filter_ent *e = g_new(owl_global_filter_ent, 1); |
|---|
| 551 | e->g = g; |
|---|
| 552 | e->f = f; |
|---|
| 553 | |
|---|
| 554 | owl_dict_insert_element(&(g->filters), owl_filter_get_name(f), |
|---|
| 555 | e, owl_global_delete_filter_ent); |
|---|
| 556 | g->filterlist = g_list_append(g->filterlist, f); |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | void owl_global_remove_filter(owl_global *g, const char *name) { |
|---|
| 560 | owl_global_filter_ent *e = owl_dict_remove_element(&(g->filters), name); |
|---|
| 561 | if (e) |
|---|
| 562 | owl_global_delete_filter_ent(e); |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | /* nextmsgid */ |
|---|
| 566 | |
|---|
| 567 | int owl_global_get_nextmsgid(owl_global *g) { |
|---|
| 568 | return(g->nextmsgid++); |
|---|
| 569 | } |
|---|
| 570 | |
|---|
| 571 | /* current view */ |
|---|
| 572 | |
|---|
| 573 | owl_view *owl_global_get_current_view(owl_global *g) { |
|---|
| 574 | return(&(g->current_view)); |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | owl_colorpair_mgr *owl_global_get_colorpair_mgr(owl_global *g) { |
|---|
| 578 | return(&(g->cpmgr)); |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | /* puntlist */ |
|---|
| 582 | |
|---|
| 583 | GPtrArray *owl_global_get_puntlist(owl_global *g) { |
|---|
| 584 | return g->puntlist; |
|---|
| 585 | } |
|---|
| 586 | |
|---|
| 587 | int owl_global_message_is_puntable(owl_global *g, const owl_message *m) { |
|---|
| 588 | const GPtrArray *pl; |
|---|
| 589 | int i; |
|---|
| 590 | |
|---|
| 591 | pl = owl_global_get_puntlist(g); |
|---|
| 592 | for (i = 0; i < pl->len; i++) { |
|---|
| 593 | if (owl_filter_message_match(pl->pdata[i], m)) return 1; |
|---|
| 594 | } |
|---|
| 595 | return 0; |
|---|
| 596 | } |
|---|
| 597 | |
|---|
| 598 | int owl_global_should_followlast(owl_global *g) { |
|---|
| 599 | const owl_view *v; |
|---|
| 600 | |
|---|
| 601 | if (!owl_global_is__followlast(g)) return(0); |
|---|
| 602 | |
|---|
| 603 | v=owl_global_get_current_view(g); |
|---|
| 604 | |
|---|
| 605 | if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1); |
|---|
| 606 | return(0); |
|---|
| 607 | } |
|---|
| 608 | |
|---|
| 609 | int owl_global_is_search_active(const owl_global *g) { |
|---|
| 610 | if (owl_regex_is_set(&g->search_re)) return(1); |
|---|
| 611 | return(0); |
|---|
| 612 | } |
|---|
| 613 | |
|---|
| 614 | void owl_global_set_search_re(owl_global *g, const owl_regex *re) { |
|---|
| 615 | if (owl_regex_is_set(&g->search_re)) { |
|---|
| 616 | owl_regex_cleanup(&g->search_re); |
|---|
| 617 | owl_regex_init(&g->search_re); |
|---|
| 618 | } |
|---|
| 619 | if (re != NULL) |
|---|
| 620 | owl_regex_copy(re, &g->search_re); |
|---|
| 621 | /* TODO: Emit a signal so we don't depend on the viewwin and mainwin */ |
|---|
| 622 | if (owl_global_get_viewwin(g)) |
|---|
| 623 | owl_viewwin_dirty(owl_global_get_viewwin(g)); |
|---|
| 624 | owl_mainwin_redisplay(owl_global_get_mainwin(g)); |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | const owl_regex *owl_global_get_search_re(const owl_global *g) { |
|---|
| 628 | return &g->search_re; |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | void owl_global_set_newmsgproc_pid(owl_global *g, pid_t i) { |
|---|
| 632 | g->newmsgproc_pid=i; |
|---|
| 633 | } |
|---|
| 634 | |
|---|
| 635 | pid_t owl_global_get_newmsgproc_pid(const owl_global *g) { |
|---|
| 636 | return(g->newmsgproc_pid); |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | /* AIM stuff */ |
|---|
| 640 | |
|---|
| 641 | int owl_global_is_aimloggedin(const owl_global *g) |
|---|
| 642 | { |
|---|
| 643 | if (g->aim_loggedin) return(1); |
|---|
| 644 | return(0); |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | const char *owl_global_get_aim_screenname(const owl_global *g) |
|---|
| 648 | { |
|---|
| 649 | if (owl_global_is_aimloggedin(g)) { |
|---|
| 650 | return (g->aim_screenname); |
|---|
| 651 | } |
|---|
| 652 | return(""); |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | const char *owl_global_get_aim_screenname_for_filters(const owl_global *g) |
|---|
| 656 | { |
|---|
| 657 | if (owl_global_is_aimloggedin(g)) { |
|---|
| 658 | return (g->aim_screenname_for_filters); |
|---|
| 659 | } |
|---|
| 660 | return(""); |
|---|
| 661 | } |
|---|
| 662 | |
|---|
| 663 | void owl_global_set_aimloggedin(owl_global *g, const char *screenname) |
|---|
| 664 | { |
|---|
| 665 | char *sn_escaped; |
|---|
| 666 | g->aim_loggedin=1; |
|---|
| 667 | if (g->aim_screenname) g_free(g->aim_screenname); |
|---|
| 668 | if (g->aim_screenname_for_filters) g_free(g->aim_screenname_for_filters); |
|---|
| 669 | g->aim_screenname=g_strdup(screenname); |
|---|
| 670 | sn_escaped = owl_text_quote(screenname, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH); |
|---|
| 671 | g->aim_screenname_for_filters = owl_arg_quote(sn_escaped); |
|---|
| 672 | g_free(sn_escaped); |
|---|
| 673 | } |
|---|
| 674 | |
|---|
| 675 | void owl_global_set_aimnologgedin(owl_global *g) |
|---|
| 676 | { |
|---|
| 677 | g->aim_loggedin=0; |
|---|
| 678 | } |
|---|
| 679 | |
|---|
| 680 | bool owl_global_is_doaimevents(const owl_global *g) |
|---|
| 681 | { |
|---|
| 682 | return g->aim_event_source != NULL; |
|---|
| 683 | } |
|---|
| 684 | |
|---|
| 685 | void owl_global_set_doaimevents(owl_global *g) |
|---|
| 686 | { |
|---|
| 687 | if (g->aim_event_source) |
|---|
| 688 | return; |
|---|
| 689 | g->aim_event_source = owl_aim_event_source_new(owl_global_get_aimsess(g)); |
|---|
| 690 | g_source_attach(g->aim_event_source, NULL); |
|---|
| 691 | } |
|---|
| 692 | |
|---|
| 693 | void owl_global_set_no_doaimevents(owl_global *g) |
|---|
| 694 | { |
|---|
| 695 | if (!g->aim_event_source) |
|---|
| 696 | return; |
|---|
| 697 | g_source_destroy(g->aim_event_source); |
|---|
| 698 | g_source_unref(g->aim_event_source); |
|---|
| 699 | g->aim_event_source = NULL; |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | aim_session_t *owl_global_get_aimsess(owl_global *g) |
|---|
| 703 | { |
|---|
| 704 | return(&(g->aimsess)); |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | aim_conn_t *owl_global_get_bosconn(owl_global *g) |
|---|
| 708 | { |
|---|
| 709 | return(&(g->bosconn)); |
|---|
| 710 | } |
|---|
| 711 | |
|---|
| 712 | void owl_global_set_bossconn(owl_global *g, aim_conn_t *conn) |
|---|
| 713 | { |
|---|
| 714 | g->bosconn=*conn; |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | /* message queue */ |
|---|
| 718 | |
|---|
| 719 | void owl_global_messagequeue_addmsg(owl_global *g, owl_message *m) |
|---|
| 720 | { |
|---|
| 721 | g_queue_push_tail(g->messagequeue, m); |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | /* pop off the first message and return it. Return NULL if the queue |
|---|
| 725 | * is empty. The caller should free the message after using it, if |
|---|
| 726 | * necessary. |
|---|
| 727 | */ |
|---|
| 728 | CALLER_OWN owl_message *owl_global_messagequeue_popmsg(owl_global *g) |
|---|
| 729 | { |
|---|
| 730 | owl_message *out; |
|---|
| 731 | |
|---|
| 732 | if (g_queue_is_empty(g->messagequeue)) |
|---|
| 733 | return NULL; |
|---|
| 734 | out = g_queue_pop_head(g->messagequeue); |
|---|
| 735 | return out; |
|---|
| 736 | } |
|---|
| 737 | |
|---|
| 738 | int owl_global_messagequeue_pending(owl_global *g) |
|---|
| 739 | { |
|---|
| 740 | return !g_queue_is_empty(g->messagequeue); |
|---|
| 741 | } |
|---|
| 742 | |
|---|
| 743 | owl_buddylist *owl_global_get_buddylist(owl_global *g) |
|---|
| 744 | { |
|---|
| 745 | return(&(g->buddylist)); |
|---|
| 746 | } |
|---|
| 747 | |
|---|
| 748 | /* style */ |
|---|
| 749 | |
|---|
| 750 | /* Return the style with name 'name'. If it does not exist return |
|---|
| 751 | * NULL */ |
|---|
| 752 | const owl_style *owl_global_get_style_by_name(const owl_global *g, const char *name) |
|---|
| 753 | { |
|---|
| 754 | return owl_dict_find_element(&(g->styledict), name); |
|---|
| 755 | } |
|---|
| 756 | |
|---|
| 757 | CALLER_OWN GPtrArray *owl_global_get_style_names(const owl_global *g) |
|---|
| 758 | { |
|---|
| 759 | return owl_dict_get_keys(&g->styledict); |
|---|
| 760 | } |
|---|
| 761 | |
|---|
| 762 | void owl_global_add_style(owl_global *g, owl_style *s) |
|---|
| 763 | { |
|---|
| 764 | /* |
|---|
| 765 | * If we're redefining the current style, make sure to update |
|---|
| 766 | * pointers to it. |
|---|
| 767 | */ |
|---|
| 768 | if(g->current_view.style |
|---|
| 769 | && !strcmp(owl_style_get_name(g->current_view.style), |
|---|
| 770 | owl_style_get_name(s))) |
|---|
| 771 | g->current_view.style = s; |
|---|
| 772 | owl_dict_insert_element(&(g->styledict), owl_style_get_name(s), |
|---|
| 773 | s, (void (*)(void *))owl_style_delete); |
|---|
| 774 | } |
|---|
| 775 | |
|---|
| 776 | void owl_global_set_haveaim(owl_global *g) |
|---|
| 777 | { |
|---|
| 778 | g->haveaim=1; |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | int owl_global_is_haveaim(const owl_global *g) |
|---|
| 782 | { |
|---|
| 783 | if (g->haveaim) return(1); |
|---|
| 784 | return(0); |
|---|
| 785 | } |
|---|
| 786 | |
|---|
| 787 | void owl_global_set_ignore_aimlogin(owl_global *g) |
|---|
| 788 | { |
|---|
| 789 | g->ignoreaimlogin = 1; |
|---|
| 790 | } |
|---|
| 791 | |
|---|
| 792 | void owl_global_unset_ignore_aimlogin(owl_global *g) |
|---|
| 793 | { |
|---|
| 794 | g->ignoreaimlogin = 0; |
|---|
| 795 | } |
|---|
| 796 | |
|---|
| 797 | int owl_global_is_ignore_aimlogin(const owl_global *g) |
|---|
| 798 | { |
|---|
| 799 | return g->ignoreaimlogin; |
|---|
| 800 | } |
|---|
| 801 | |
|---|
| 802 | void owl_global_set_havezephyr(owl_global *g) |
|---|
| 803 | { |
|---|
| 804 | g->havezephyr=1; |
|---|
| 805 | } |
|---|
| 806 | |
|---|
| 807 | int owl_global_is_havezephyr(const owl_global *g) |
|---|
| 808 | { |
|---|
| 809 | if (g->havezephyr) return(1); |
|---|
| 810 | return(0); |
|---|
| 811 | } |
|---|
| 812 | |
|---|
| 813 | owl_errqueue *owl_global_get_errqueue(owl_global *g) |
|---|
| 814 | { |
|---|
| 815 | return(&(g->errqueue)); |
|---|
| 816 | } |
|---|
| 817 | |
|---|
| 818 | owl_zbuddylist *owl_global_get_zephyr_buddylist(owl_global *g) |
|---|
| 819 | { |
|---|
| 820 | return(&(g->zbuddies)); |
|---|
| 821 | } |
|---|
| 822 | |
|---|
| 823 | GList **owl_global_get_zaldlist(owl_global *g) |
|---|
| 824 | { |
|---|
| 825 | return &(g->zaldlist); |
|---|
| 826 | } |
|---|
| 827 | |
|---|
| 828 | int owl_global_get_pseudologin_notify(owl_global *g) |
|---|
| 829 | { |
|---|
| 830 | return g->pseudologin_notify; |
|---|
| 831 | } |
|---|
| 832 | |
|---|
| 833 | void owl_global_set_pseudologin_notify(owl_global *g, int notify) |
|---|
| 834 | { |
|---|
| 835 | g->pseudologin_notify = notify; |
|---|
| 836 | } |
|---|
| 837 | |
|---|
| 838 | struct termios *owl_global_get_startup_tio(owl_global *g) |
|---|
| 839 | { |
|---|
| 840 | return(&(g->startup_tio)); |
|---|
| 841 | } |
|---|
| 842 | |
|---|
| 843 | void owl_global_setup_default_filters(owl_global *g) |
|---|
| 844 | { |
|---|
| 845 | int i; |
|---|
| 846 | static const struct { |
|---|
| 847 | const char *name; |
|---|
| 848 | const char *desc; |
|---|
| 849 | } filters[] = { |
|---|
| 850 | { "personal", |
|---|
| 851 | "isprivate ^true$ and ( not type ^zephyr$ or ( class ^message ) )" }, |
|---|
| 852 | { "trash", |
|---|
| 853 | "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )" }, |
|---|
| 854 | { "wordwrap", "not ( type ^admin$ or type ^zephyr$ )" }, |
|---|
| 855 | { "ping", "opcode ^ping$" }, |
|---|
| 856 | { "auto", "opcode ^auto$" }, |
|---|
| 857 | { "login", "not login ^none$" }, |
|---|
| 858 | { "reply-lockout", "class ^mail$" }, |
|---|
| 859 | { "out", "direction ^out$" }, |
|---|
| 860 | { "aim", "type ^aim$" }, |
|---|
| 861 | { "zephyr", "type ^zephyr$" }, |
|---|
| 862 | { "none", "false" }, |
|---|
| 863 | { "all", "true" }, |
|---|
| 864 | { NULL, NULL } |
|---|
| 865 | }; |
|---|
| 866 | |
|---|
| 867 | owl_function_debugmsg("startup: creating default filters"); |
|---|
| 868 | |
|---|
| 869 | for (i = 0; filters[i].name != NULL; i++) |
|---|
| 870 | owl_global_add_filter(g, owl_filter_new_fromstring(filters[i].name, |
|---|
| 871 | filters[i].desc)); |
|---|
| 872 | } |
|---|
| 873 | |
|---|
| 874 | FILE *owl_global_get_debug_file_handle(owl_global *g) { |
|---|
| 875 | static char *open_file = NULL; |
|---|
| 876 | const char *filename = owl_global_get_debug_file(g); |
|---|
| 877 | if (g->debug_file == NULL || |
|---|
| 878 | (open_file && strcmp(filename, open_file) != 0)) { |
|---|
| 879 | char *path; |
|---|
| 880 | int fd; |
|---|
| 881 | |
|---|
| 882 | if (g->debug_file) |
|---|
| 883 | fclose(g->debug_file); |
|---|
| 884 | |
|---|
| 885 | g->debug_file = NULL; |
|---|
| 886 | |
|---|
| 887 | path = g_strdup_printf("%s.%d", filename, getpid()); |
|---|
| 888 | fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0600); |
|---|
| 889 | g_free(path); |
|---|
| 890 | |
|---|
| 891 | if (fd >= 0) |
|---|
| 892 | g->debug_file = fdopen(fd, "a"); |
|---|
| 893 | |
|---|
| 894 | g_free(open_file); |
|---|
| 895 | open_file = g_strdup(filename); |
|---|
| 896 | } |
|---|
| 897 | return g->debug_file; |
|---|
| 898 | } |
|---|
| 899 | |
|---|
| 900 | const char *owl_global_get_kill_buffer(owl_global *g) { |
|---|
| 901 | return g->kill_buffer; |
|---|
| 902 | } |
|---|
| 903 | |
|---|
| 904 | void owl_global_set_kill_buffer(owl_global *g, const char *kill, int len) { |
|---|
| 905 | g_free(g->kill_buffer); |
|---|
| 906 | g->kill_buffer = g_strndup(kill, len); |
|---|
| 907 | } |
|---|
| 908 | |
|---|
| 909 | static GMutex *owl_global_get_interrupt_lock(owl_global *g) |
|---|
| 910 | { |
|---|
| 911 | #if GLIB_CHECK_VERSION(2, 31, 0) |
|---|
| 912 | return &g->interrupt_lock; |
|---|
| 913 | #else |
|---|
| 914 | return g->interrupt_lock; |
|---|
| 915 | #endif |
|---|
| 916 | } |
|---|
| 917 | |
|---|
| 918 | void owl_global_add_interrupt(owl_global *g) { |
|---|
| 919 | /* TODO: This can almost certainly be done with atomic |
|---|
| 920 | * operations. Whatever. */ |
|---|
| 921 | g_mutex_lock(owl_global_get_interrupt_lock(g)); |
|---|
| 922 | g->interrupt_count++; |
|---|
| 923 | g_mutex_unlock(owl_global_get_interrupt_lock(g)); |
|---|
| 924 | } |
|---|
| 925 | |
|---|
| 926 | bool owl_global_take_interrupt(owl_global *g) { |
|---|
| 927 | bool ans = false; |
|---|
| 928 | g_mutex_lock(owl_global_get_interrupt_lock(g)); |
|---|
| 929 | if (g->interrupt_count > 0) { |
|---|
| 930 | ans = true; |
|---|
| 931 | g->interrupt_count--; |
|---|
| 932 | } |
|---|
| 933 | g_mutex_unlock(owl_global_get_interrupt_lock(g)); |
|---|
| 934 | return ans; |
|---|
| 935 | } |
|---|