source: global.c @ 61abb18

barnowl_perlaim
Last change on this file since 61abb18 was 61abb18, checked in by Geoffrey Thomas <geofft@mit.edu>, 16 years ago
Now it compiles. Highly likely to leak or crash, although it seems to work for me...
  • Property mode set to 100644
File size: 17.4 KB
Line 
1#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <string.h>
5#include <netdb.h>
6#include <termios.h>
7#include <sys/ioctl.h>
8#include <time.h>
9#include "owl.h"
10
11static const char fileIdent[] = "$Id$";
12
13#ifndef MAXHOSTNAMELEN
14#define MAXHOSTNAMELEN 256
15#endif
16
17void owl_global_init(owl_global *g) {
18  struct hostent *hent;
19  char hostname[MAXHOSTNAMELEN];
20  char *cd;
21
22  g->malloced=0;
23  g->freed=0;
24
25  gethostname(hostname, MAXHOSTNAMELEN);
26  hent=gethostbyname(hostname);
27  if (!hent) {
28    g->thishost=owl_strdup("localhost");
29  } else {
30    g->thishost=owl_strdup(hent->h_name);
31  }
32
33  owl_context_init(&g->ctx);
34  owl_context_set_startup(&g->ctx);
35  g->curmsg=0;
36  g->topmsg=0;
37  g->needrefresh=1;
38  g->startupargs=NULL;
39
40  owl_variable_dict_setup(&(g->vars));
41  owl_cmddict_setup(&(g->cmds));
42
43  g->lines=LINES;
44  g->cols=COLS;
45
46  g->rightshift=0;
47
48  owl_editwin_init(&(g->tw), NULL, owl_global_get_typwin_lines(g), g->cols, OWL_EDITWIN_STYLE_ONELINE, NULL);
49
50  owl_keyhandler_init(&g->kh);
51  owl_keys_setup_keymaps(&g->kh);
52
53  owl_list_create(&(g->muxevents));
54  owl_list_create(&(g->filterlist));
55  owl_list_create(&(g->puntlist));
56  owl_list_create(&(g->messagequeue));
57  owl_dict_create(&(g->styledict));
58  g->curmsg_vert_offset=0;
59  g->resizepending=0;
60  g->typwinactive=0;
61  g->direction=OWL_DIRECTION_DOWNWARDS;
62  g->zaway=0;
63  if (has_colors()) {
64    g->hascolors=1;
65  }
66  g->colorpairs=COLOR_PAIRS;
67  owl_fmtext_init_colorpair_mgr(&(g->cpmgr));
68  g->debug=OWL_DEBUG;
69  g->searchactive=0;
70  g->searchstring=NULL;
71  g->starttime=time(NULL); /* assumes we call init only a start time */
72  g->lastinputtime=g->starttime;
73  g->newmsgproc_pid=0;
74 
75  owl_global_set_config_format(g, 0);
76  owl_global_set_userclue(g, OWL_USERCLUE_NONE);
77  owl_global_set_no_have_config(g);
78  owl_history_init(&(g->msghist));
79  owl_history_init(&(g->cmdhist));
80  owl_history_set_norepeats(&(g->cmdhist));
81  g->nextmsgid=0;
82
83  _owl_global_setup_windows(g);
84
85  /* Fill in some variables which don't have constant defaults */
86  /* TODO: come back later and check passwd file first */
87  g->homedir=owl_strdup(getenv("HOME"));
88
89  g->confdir = NULL;
90  g->startupfile = NULL;
91  cd = owl_sprintf("%s/%s", g->homedir, OWL_CONFIG_DIR);
92  owl_global_set_confdir(g, cd);
93  owl_free(cd);
94
95  owl_messagelist_create(&(g->msglist));
96  owl_mainwin_init(&(g->mw));
97  owl_popwin_init(&(g->pw));
98
99  g->havezephyr=0;
100
101  owl_errqueue_init(&(g->errqueue));
102  g->got_err_signal=0;
103
104  owl_zbuddylist_create(&(g->zbuddies));
105  owl_timer_create_countdown(&(g->zephyr_buddycheck_timer), 60*3);
106
107  owl_obarray_init(&(g->obarray));
108
109  owl_message_init_fmtext_cache();
110  owl_list_create(&(g->dispatchlist));
111}
112
113void _owl_global_setup_windows(owl_global *g) {
114  int cols, typwin_lines;
115
116  cols=g->cols;
117  typwin_lines=owl_global_get_typwin_lines(g);
118
119  /* set the new window sizes */
120  g->recwinlines=g->lines-(typwin_lines+2);
121  if (g->recwinlines<0) {
122    /* gotta deal with this */
123    g->recwinlines=0;
124  }
125
126  owl_function_debugmsg("_owl_global_setup_windows: about to call newwin(%i, %i, 0, 0)\n", g->recwinlines, cols);
127
128  /* create the new windows */
129  g->recwin=newwin(g->recwinlines, cols, 0, 0);
130  if (g->recwin==NULL) {
131    owl_function_debugmsg("_owl_global_setup_windows: newwin returned NULL\n", g->recwinlines, cols);
132    endwin();
133    exit(50);
134  }
135     
136  g->sepwin=newwin(1, cols, g->recwinlines, 0);
137  g->msgwin=newwin(1, cols, g->recwinlines+1, 0);
138  g->typwin=newwin(typwin_lines, cols, g->recwinlines+2, 0);
139
140  owl_editwin_set_curswin(&(g->tw), g->typwin, typwin_lines, g->cols);
141
142  idlok(g->typwin, FALSE);
143  idlok(g->recwin, FALSE);
144  idlok(g->sepwin, FALSE);
145  idlok(g->msgwin, FALSE);
146
147  nodelay(g->typwin, 1);
148  keypad(g->typwin, TRUE);
149  wmove(g->typwin, 0, 0);
150
151  meta(g->typwin, TRUE);
152}
153
154owl_context *owl_global_get_context(owl_global *g) {
155  return(&g->ctx);
156}
157                         
158int owl_global_get_lines(owl_global *g) {
159  return(g->lines);
160}
161
162int owl_global_get_cols(owl_global *g) {
163  return(g->cols);
164}
165
166int owl_global_get_recwin_lines(owl_global *g) {
167  return(g->recwinlines);
168}
169
170/* curmsg */
171
172int owl_global_get_curmsg(owl_global *g) {
173  return(g->curmsg);
174}
175
176void owl_global_set_curmsg(owl_global *g, int i) {
177  g->curmsg=i;
178  /* we will reset the vertical offset from here */
179  /* we might want to move this out to the functions later */
180  owl_global_set_curmsg_vert_offset(g, 0);
181}
182
183/* topmsg */
184
185int owl_global_get_topmsg(owl_global *g) {
186  return(g->topmsg);
187}
188
189void owl_global_set_topmsg(owl_global *g, int i) {
190  g->topmsg=i;
191}
192
193/* windows */
194
195owl_mainwin *owl_global_get_mainwin(owl_global *g) {
196  return(&(g->mw));
197}
198
199owl_popwin *owl_global_get_popwin(owl_global *g) {
200  return(&(g->pw));
201}
202
203/* msglist */
204
205owl_messagelist *owl_global_get_msglist(owl_global *g) {
206  return(&(g->msglist));
207}
208
209/* keyhandler */
210
211owl_keyhandler *owl_global_get_keyhandler(owl_global *g) {
212  return(&(g->kh));
213}
214
215/* curses windows */
216
217WINDOW *owl_global_get_curs_recwin(owl_global *g) {
218  return(g->recwin);
219}
220
221WINDOW *owl_global_get_curs_sepwin(owl_global *g) {
222  return(g->sepwin);
223}
224
225WINDOW *owl_global_get_curs_msgwin(owl_global *g) {
226  return(g->msgwin);
227}
228
229WINDOW *owl_global_get_curs_typwin(owl_global *g) {
230  return(g->typwin);
231}
232
233/* typwin */
234
235owl_editwin *owl_global_get_typwin(owl_global *g) {
236  return(&(g->tw));
237}
238
239/* buffercallback */
240
241void owl_global_set_buffercallback(owl_global *g, owl_callback_t cb, char *line) {
242  owl_editwin_set_callback(owl_global_get_typwin(g), cb, line);
243}
244
245/* refresh */
246
247int owl_global_is_needrefresh(owl_global *g) {
248  if (g->needrefresh==1) return(1);
249  return(0);
250}
251
252void owl_global_set_needrefresh(owl_global *g) {
253  g->needrefresh=1;
254}
255
256void owl_global_set_noneedrefresh(owl_global *g) {
257  g->needrefresh=0;
258}
259
260/* variable dictionary */
261
262owl_vardict *owl_global_get_vardict(owl_global *g) {
263  return &(g->vars);
264}
265
266/* command dictionary */
267
268owl_cmddict *owl_global_get_cmddict(owl_global *g) {
269  return &(g->cmds);
270}
271
272/* rightshift */
273
274void owl_global_set_rightshift(owl_global *g, int i) {
275  g->rightshift=i;
276}
277
278int owl_global_get_rightshift(owl_global *g) {
279  return(g->rightshift);
280}
281
282/* typwin */
283
284int owl_global_is_typwin_active(owl_global *g) {
285  if (g->typwinactive==1) return(1);
286  return(0);
287}
288
289void owl_global_set_typwin_active(owl_global *g) {
290  g->typwinactive=1;
291}
292
293void owl_global_set_typwin_inactive(owl_global *g) {
294  g->typwinactive=0;
295}
296
297/* resize */
298
299void owl_global_set_resize_pending(owl_global *g) {
300  g->resizepending=1;
301}
302
303char *owl_global_get_homedir(owl_global *g) {
304  if (g->homedir) return(g->homedir);
305  return("/");
306}
307
308char *owl_global_get_confdir(owl_global *g) {
309  if (g->confdir) return(g->confdir);
310  return("/");
311}
312
313/*
314 * Setting this also sets startupfile to confdir/startup
315 */
316void owl_global_set_confdir(owl_global *g, char *cd) {
317  free(g->confdir);
318  g->confdir = owl_strdup(cd);
319  free(g->startupfile);
320  g->startupfile = owl_sprintf("%s/startup", cd);
321}
322
323char *owl_global_get_startupfile(owl_global *g) {
324  if(g->startupfile) return(g->startupfile);
325  return("/");
326}
327
328int owl_global_get_direction(owl_global *g) {
329  return(g->direction);
330}
331
332void owl_global_set_direction_downwards(owl_global *g) {
333  g->direction=OWL_DIRECTION_DOWNWARDS;
334}
335
336void owl_global_set_direction_upwards(owl_global *g) {
337  g->direction=OWL_DIRECTION_UPWARDS;
338}
339
340/* perl stuff */
341
342void owl_global_set_perlinterp(owl_global *g, void *p) {
343  g->perl=p;
344}
345
346void *owl_global_get_perlinterp(owl_global *g) {
347  return(g->perl);
348}
349
350int owl_global_is_config_format(owl_global *g) {
351  if (g->config_format) return(1);
352  return(0);
353}
354
355void owl_global_set_config_format(owl_global *g, int state) {
356  if (state==1) {
357    g->config_format=1;
358  } else {
359    g->config_format=0;
360  }
361}
362
363void owl_global_set_have_config(owl_global *g) {
364  g->haveconfig=1;
365}
366
367void owl_global_set_no_have_config(owl_global *g) {
368  g->haveconfig=0;
369}
370
371int owl_global_have_config(owl_global *g) {
372  if (g->haveconfig) return(1);
373  return(0);
374}
375
376void owl_global_resize(owl_global *g, int x, int y) {
377  /* resize the screen.  If x or y is 0 use the terminal size */
378  struct winsize size;
379   
380  if (!g->resizepending) return;
381
382  /* delete the current windows */
383  delwin(g->recwin);
384  delwin(g->sepwin);
385  delwin(g->msgwin);
386  delwin(g->typwin);
387  if (!isendwin()) {
388    endwin();
389  }
390
391  refresh();
392
393  /* get the new size */
394  ioctl(STDIN_FILENO, TIOCGWINSZ, &size);
395  if (x==0) {
396    if (size.ws_row) {
397      g->lines=size.ws_row;
398    } else {
399      g->lines=LINES;
400    } 
401  } else {
402      g->lines=x;
403  }
404
405  if (y==0) {
406    if (size.ws_col) {
407      g->cols=size.ws_col;
408    } else {
409      g->cols=COLS;
410    } 
411  } else {
412    g->cols=y;
413  }
414
415#ifdef HAVE_RESIZETERM
416  resizeterm(size.ws_row, size.ws_col);
417#endif
418
419  /* re-initialize the windows */
420  _owl_global_setup_windows(g);
421
422  /* in case any styles rely on the current width */
423  owl_messagelist_invalidate_formats(owl_global_get_msglist(g));
424
425  /* recalculate the topmsg to make sure the current message is on
426   * screen */
427  owl_function_calculate_topmsg(OWL_DIRECTION_NONE);
428
429  /* refresh stuff */
430  g->needrefresh=1;
431  owl_mainwin_redisplay(&(g->mw));
432  sepbar(NULL);
433  owl_editwin_redisplay(&(g->tw), 0);
434  owl_function_full_redisplay(&g);
435
436  /* TODO: this should handle other forms of popwins */
437  if (owl_popwin_is_active(owl_global_get_popwin(g)) 
438      && owl_global_get_viewwin(g)) {
439    owl_popwin_refresh(owl_global_get_popwin(g));
440    owl_viewwin_redisplay(owl_global_get_viewwin(g), 0);
441  }
442
443  owl_function_debugmsg("New size is %i lines, %i cols.", size.ws_row, size.ws_col);
444  owl_function_makemsg("");
445  g->resizepending=0;
446}
447
448/* debug */
449
450int owl_global_is_debug_fast(owl_global *g) {
451  if (g->debug) return(1);
452  return(0);
453}
454
455/* starttime */
456
457time_t owl_global_get_starttime(owl_global *g) {
458  return(g->starttime);
459}
460
461time_t owl_global_get_runtime(owl_global *g) {
462  return(time(NULL)-g->starttime);
463}
464
465time_t owl_global_get_lastinputtime(owl_global *g) {
466  return(g->lastinputtime);
467}
468
469void owl_global_set_lastinputtime(owl_global *g, time_t time) {
470  g->lastinputtime = time;
471}
472
473time_t owl_global_get_idletime(owl_global *g) {
474  return(time(NULL)-g->lastinputtime);
475}
476
477void owl_global_get_runtime_string(owl_global *g, char *buff) {
478  time_t diff;
479
480  diff=time(NULL)-owl_global_get_starttime(g);
481
482  /* print something nicer later */   
483  sprintf(buff, "%i seconds", (int) diff);
484}
485
486char *owl_global_get_hostname(owl_global *g) {
487  if (g->thishost) return(g->thishost);
488  return("");
489}
490
491/* userclue */
492
493void owl_global_set_userclue(owl_global *g, int clue) {
494  g->userclue=clue;
495}
496
497void owl_global_add_userclue(owl_global *g, int clue) {
498  g->userclue|=clue;
499}
500
501int owl_global_get_userclue(owl_global *g) {
502  return(g->userclue);
503}
504
505int owl_global_is_userclue(owl_global *g, int clue) {
506  if (g->userclue & clue) return(1);
507  return(0);
508}
509
510/* viewwin */
511
512owl_viewwin *owl_global_get_viewwin(owl_global *g) {
513  return(&(g->vw));
514}
515
516
517/* vert offset */
518
519int owl_global_get_curmsg_vert_offset(owl_global *g) {
520  return(g->curmsg_vert_offset);
521}
522
523void owl_global_set_curmsg_vert_offset(owl_global *g, int i) {
524  g->curmsg_vert_offset=i;
525}
526
527/* startup args */
528
529void owl_global_set_startupargs(owl_global *g, int argc, char **argv) {
530  int i, len;
531
532  if (g->startupargs) owl_free(g->startupargs);
533 
534  len=0;
535  for (i=0; i<argc; i++) {
536    len+=strlen(argv[i])+5;
537  }
538  g->startupargs=owl_malloc(len+5);
539
540  strcpy(g->startupargs, "");
541  for (i=0; i<argc; i++) {
542    sprintf(g->startupargs, "%s%s ", g->startupargs, argv[i]);
543  }
544  g->startupargs[strlen(g->startupargs)-1]='\0';
545}
546
547char *owl_global_get_startupargs(owl_global *g) {
548  if (g->startupargs) return(g->startupargs);
549  return("");
550}
551
552/* history */
553
554owl_history *owl_global_get_msg_history(owl_global *g) {
555  return(&(g->msghist));
556}
557
558owl_history *owl_global_get_cmd_history(owl_global *g) {
559  return(&(g->cmdhist));
560}
561
562/* muxevents */
563
564owl_muxevents *owl_global_get_muxevents(owl_global *g) {
565  return(&(g->muxevents));
566}
567
568/* filterlist */
569
570owl_list *owl_global_get_filterlist(owl_global *g) {
571  return(&(g->filterlist));
572}
573
574owl_filter *owl_global_get_filter(owl_global *g, char *name) {
575  int i, j;
576  owl_filter *f;
577
578  j=owl_list_get_size(&(g->filterlist));
579  for (i=0; i<j; i++) {
580    f=owl_list_get_element(&(g->filterlist), i);
581    if (!strcmp(name, owl_filter_get_name(f))) {
582      return(f);
583    }
584  }
585  return(NULL);
586}
587
588void owl_global_add_filter(owl_global *g, owl_filter *f) {
589  owl_list_append_element(&(g->filterlist), f);
590}
591
592void owl_global_remove_filter(owl_global *g, char *name) {
593  int i, j;
594  owl_filter *f;
595
596  j=owl_list_get_size(&(g->filterlist));
597  for (i=0; i<j; i++) {
598    f=owl_list_get_element(&(g->filterlist), i);
599    if (!strcmp(name, owl_filter_get_name(f))) {
600      owl_filter_free(f);
601      owl_list_remove_element(&(g->filterlist), i);
602      break;
603    }
604  }
605}
606
607/* nextmsgid */
608
609int owl_global_get_nextmsgid(owl_global *g) {
610  return(g->nextmsgid++);
611}
612
613/* current view */
614
615owl_view *owl_global_get_current_view(owl_global *g) {
616  return(&(g->current_view));
617}
618
619/* has colors */
620
621int owl_global_get_hascolors(owl_global *g) {
622  if (g->hascolors) return(1);
623  return(0);
624}
625
626/* color pairs */
627
628int owl_global_get_colorpairs(owl_global *g) {
629  return(g->colorpairs);
630}
631
632owl_colorpair_mgr *owl_global_get_colorpair_mgr(owl_global *g) {
633  return(&(g->cpmgr));
634}
635
636/* puntlist */
637
638owl_list *owl_global_get_puntlist(owl_global *g) {
639  return(&(g->puntlist));
640}
641
642int owl_global_message_is_puntable(owl_global *g, owl_message *m) {
643  owl_list *pl;
644  int i, j;
645
646  pl=owl_global_get_puntlist(g);
647  j=owl_list_get_size(pl);
648  for (i=0; i<j; i++) {
649    if (owl_filter_message_match(owl_list_get_element(pl, i), m)) return(1);
650  }
651  return(0);
652}
653
654int owl_global_should_followlast(owl_global *g) {
655  owl_view *v;
656 
657  if (!owl_global_is__followlast(g)) return(0);
658 
659  v=owl_global_get_current_view(g);
660 
661  if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1);
662  return(0);
663}
664
665int owl_global_is_search_active(owl_global *g) {
666  if (g->searchactive) return(1);
667  return(0);
668}
669
670void owl_global_set_search_active(owl_global *g, char *string) {
671  g->searchactive=1;
672  if (g->searchstring != NULL) owl_free(g->searchstring);
673  g->searchstring=owl_strdup(string);
674}
675
676void owl_global_set_search_inactive(owl_global *g) {
677  g->searchactive=0;
678}
679
680char *owl_global_get_search_string(owl_global *g) {
681  if (g->searchstring==NULL) return("");
682  return(g->searchstring);
683}
684
685void owl_global_set_newmsgproc_pid(owl_global *g, int i) {
686  g->newmsgproc_pid=i;
687}
688
689int owl_global_get_newmsgproc_pid(owl_global *g) {
690  return(g->newmsgproc_pid);
691}
692
693void owl_global_add_to_malloced(owl_global *g, int i) {
694  g->malloced+=i;
695}
696
697void owl_global_add_to_freed(owl_global *g, int i) {
698  g->freed+=1;
699}
700
701int owl_global_get_malloced(owl_global *g) {
702  return(g->malloced);
703}
704
705int owl_global_get_freed(owl_global *g) {
706  return(g->freed);
707}
708
709int owl_global_get_meminuse(owl_global *g) {
710  return(g->malloced-g->freed);
711}
712
713/* message queue */
714
715void owl_global_messagequeue_addmsg(owl_global *g, owl_message *m)
716{
717  owl_list_append_element(&(g->messagequeue), m);
718}
719
720/* pop off the first message and return it.  Return NULL if the queue
721 * is empty.  The caller should free the message after using it, if
722 * necessary.
723 */
724owl_message *owl_global_messagequeue_popmsg(owl_global *g)
725{
726  owl_message *out;
727
728  if (owl_list_get_size(&(g->messagequeue))==0) return(NULL);
729  out=owl_list_get_element(&(g->messagequeue), 0);
730  owl_list_remove_element(&(g->messagequeue), 0);
731  return(out);
732}
733
734int owl_global_messagequeue_pending(owl_global *g)
735{
736  if (owl_list_get_size(&(g->messagequeue))==0) return(0);
737  return(1);
738}
739
740owl_buddylist *owl_global_get_buddylist(owl_global *g)
741{
742  return(&(g->buddylist));
743}
744 
745/* style */
746
747/* Return the style with name 'name'.  If it does not exist return
748 * NULL */
749owl_style *owl_global_get_style_by_name(owl_global *g, char *name)
750{
751  return owl_dict_find_element(&(g->styledict), name);
752}
753
754/* creates a list and fills it in with keys.  duplicates the keys,
755 * so they will need to be freed by the caller. */
756int owl_global_get_style_names(owl_global *g, owl_list *l) {
757  return owl_dict_get_keys(&(g->styledict), l);
758}
759
760void owl_global_add_style(owl_global *g, owl_style *s)
761{
762  /*
763   * If we're redefining the current style, make sure to update
764   * pointers to it.
765   */
766  if(g->current_view.style
767     && !strcmp(owl_style_get_name(g->current_view.style),
768                owl_style_get_name(s)))
769    g->current_view.style = s;
770  owl_dict_insert_element(&(g->styledict), owl_style_get_name(s),
771                          s, (void(*)(void*))owl_style_free);
772}
773
774void owl_global_set_havezephyr(owl_global *g)
775{
776  g->havezephyr=1;
777}
778
779int owl_global_is_havezephyr(owl_global *g)
780{
781  if (g->havezephyr) return(1);
782  return(0);
783}
784
785owl_errqueue *owl_global_get_errqueue(owl_global *g)
786{
787  return(&(g->errqueue));
788}
789
790void owl_global_set_errsignal(owl_global *g, int signum, siginfo_t *siginfo)
791{
792  g->got_err_signal = signum;
793  if (siginfo) {
794    g->err_signal_info = *siginfo;
795  } else {
796    memset(&(g->err_signal_info), 0, sizeof(siginfo_t));
797  }
798}
799
800int owl_global_get_errsignal_and_clear(owl_global *g, siginfo_t *siginfo)
801{
802  int signum;
803  if (siginfo && g->got_err_signal) {
804    *siginfo = g->err_signal_info;
805  } 
806  signum = g->got_err_signal;
807  g->got_err_signal = 0;
808  return signum;
809}
810
811owl_timer *owl_global_get_zephyr_buddycheck_timer(owl_global *g)
812{
813  return(&(g->zephyr_buddycheck_timer));
814}
815
816owl_zbuddylist *owl_global_get_zephyr_buddylist(owl_global *g)
817{
818  return(&(g->zbuddies));
819}
820
821struct termios *owl_global_get_startup_tio(owl_global *g)
822{
823  return(&(g->startup_tio));
824}
825
826char * owl_global_intern(owl_global *g, char * string)
827{
828  return owl_obarray_insert(&(g->obarray), string);
829}
830
831owl_list *owl_global_get_dispatchlist(owl_global *g)
832{
833  return &(g->dispatchlist);
834}
Note: See TracBrowser for help on using the repository browser.