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