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