source: global.c @ 591e6aa7

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 591e6aa7 was 591e6aa7, checked in by James M. Kretchmar <kretch@mit.edu>, 17 years ago
memory bug fix from previous check in
  • Property mode set to 100644
File size: 15.7 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
21  g->malloced=0;
22  g->freed=0;
23
24  gethostname(hostname, MAXHOSTNAMELEN);
25  hent=gethostbyname(hostname);
26  if (!hent) {
27    g->thishost=owl_strdup("localhost");
28  } else {
29    g->thishost=owl_strdup(hent->h_name);
30  }
31
32  owl_context_init(&g->ctx);
33  owl_context_set_startup(&g->ctx);
34  g->curmsg=0;
35  g->topmsg=0;
36  g->needrefresh=1;
37  g->startupargs=NULL;
38
39  owl_variable_dict_setup(&(g->vars));
40  owl_cmddict_setup(&(g->cmds));
41
42  g->lines=LINES;
43  g->cols=COLS;
44
45  g->rightshift=0;
46
47  owl_editwin_init(&(g->tw), NULL, owl_global_get_typwin_lines(g), g->cols, OWL_EDITWIN_STYLE_ONELINE, NULL);
48
49  owl_keyhandler_init(&g->kh);
50  owl_keys_setup_keymaps(&g->kh);
51
52  owl_list_create(&(g->filterlist));
53  owl_list_create(&(g->puntlist));
54  owl_list_create(&(g->messagequeue));
55  owl_list_create(&(g->stylelist));
56  g->curmsg_vert_offset=0;
57  g->resizepending=0;
58  g->typwinactive=0;
59  g->direction=OWL_DIRECTION_DOWNWARDS;
60  g->zaway=0;
61  if (has_colors()) {
62    g->hascolors=1;
63  }
64  g->colorpairs=COLOR_PAIRS;
65  g->debug=OWL_DEBUG;
66  g->searchactive=0;
67  g->searchstring=NULL;
68  g->starttime=time(NULL); /* assumes we call init only a start time */
69  g->buffercommand=NULL;
70  g->newmsgproc_pid=0;
71 
72  owl_global_set_config_format(g, 0);
73  owl_global_set_userclue(g, OWL_USERCLUE_NONE);
74  owl_global_set_no_have_config(g);
75  owl_history_init(&(g->msghist));
76  owl_history_init(&(g->cmdhist));
77  g->nextmsgid=0;
78
79  owl_filterelement_create_true(&(g->fe_true));
80  owl_filterelement_create_false(&(g->fe_false));
81  owl_filterelement_create_null(&(g->fe_null));
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  owl_messagelist_create(&(g->msglist));
90  owl_mainwin_init(&(g->mw));
91  owl_popwin_init(&(g->pw));
92
93  g->aim_screenname=NULL;
94  g->aim_loggedin=0;
95  owl_timer_create_countdown(&(g->aim_noop_timer), 30);
96  owl_timer_create_countdown(&(g->aim_ignorelogin_timer), 0);
97  owl_buddylist_init(&(g->buddylist));
98}
99
100void _owl_global_setup_windows(owl_global *g) {
101  int cols, typwin_lines;
102
103  cols=g->cols;
104  typwin_lines=owl_global_get_typwin_lines(g);
105
106  /* set the new window sizes */
107  g->recwinlines=g->lines-(typwin_lines+2);
108  if (g->recwinlines<1) {
109    /* this will screw things up.  I'm not sure what to do yet,
110       but this is better than nothing */
111    /* g->recwinlines=1; */
112  }
113
114  /* create the new windows */
115  g->recwin=newwin(g->recwinlines, cols, 0, 0);
116  if (g->recwin==NULL) {
117    owl_function_debugmsg("\n\nI just received an error on creating a new receive window\n");
118    owl_function_debugmsg("newwin was called with arguments (%i, %i, 0, 0) and returned NULL\n",
119           g->recwinlines, cols);
120    endwin();
121
122    exit(50);
123  }
124     
125  g->sepwin=newwin(1, cols, g->recwinlines, 0);
126  g->msgwin=newwin(1, cols, g->recwinlines+1, 0);
127  g->typwin=newwin(typwin_lines, cols, g->recwinlines+2, 0);
128
129  owl_editwin_set_curswin(&(g->tw), g->typwin, typwin_lines, g->cols);
130
131  idlok(g->typwin, FALSE);
132  idlok(g->recwin, FALSE);
133  idlok(g->sepwin, FALSE);
134  idlok(g->msgwin, FALSE);
135
136  nodelay(g->typwin, 1);
137  keypad(g->typwin, TRUE);
138  wmove(g->typwin, 0, 0);
139
140  meta(g->typwin, TRUE);
141}
142
143owl_context *owl_global_get_context(owl_global *g) {
144  return(&g->ctx);
145}
146                         
147int owl_global_get_lines(owl_global *g) {
148  return(g->lines);
149}
150
151int owl_global_get_cols(owl_global *g) {
152  return(g->cols);
153}
154
155int owl_global_get_recwin_lines(owl_global *g) {
156  return(g->recwinlines);
157}
158
159/* curmsg */
160
161int owl_global_get_curmsg(owl_global *g) {
162  return(g->curmsg);
163}
164
165void owl_global_set_curmsg(owl_global *g, int i) {
166  g->curmsg=i;
167  /* we will reset the vertical offset from here */
168  /* we might want to move this out to the functions later */
169  owl_global_set_curmsg_vert_offset(g, 0);
170}
171
172/* topmsg */
173
174int owl_global_get_topmsg(owl_global *g) {
175  return(g->topmsg);
176}
177
178void owl_global_set_topmsg(owl_global *g, int i) {
179  g->topmsg=i;
180}
181
182/* windows */
183
184owl_mainwin *owl_global_get_mainwin(owl_global *g) {
185  return(&(g->mw));
186}
187
188owl_popwin *owl_global_get_popwin(owl_global *g) {
189  return(&(g->pw));
190}
191
192/* msglist */
193
194owl_messagelist *owl_global_get_msglist(owl_global *g) {
195  return(&(g->msglist));
196}
197
198/* keyhandler */
199
200owl_keyhandler *owl_global_get_keyhandler(owl_global *g) {
201  return(&(g->kh));
202}
203
204/* curses windows */
205
206WINDOW *owl_global_get_curs_recwin(owl_global *g) {
207  return(g->recwin);
208}
209
210WINDOW *owl_global_get_curs_sepwin(owl_global *g) {
211  return(g->sepwin);
212}
213
214WINDOW *owl_global_get_curs_msgwin(owl_global *g) {
215  return(g->msgwin);
216}
217
218WINDOW *owl_global_get_curs_typwin(owl_global *g) {
219  return(g->typwin);
220}
221
222/* typwin */
223
224owl_editwin *owl_global_get_typwin(owl_global *g) {
225  return(&(g->tw));
226}
227
228/* buffercommand */
229
230void owl_global_set_buffercommand(owl_global *g, char *command) {
231  if (g->buffercommand) owl_free(g->buffercommand);
232  g->buffercommand=owl_strdup(command);
233}
234
235char *owl_global_get_buffercommand(owl_global *g) {
236  if (g->buffercommand) return(g->buffercommand);
237  return("");
238}
239
240/* refresh */
241
242int owl_global_is_needrefresh(owl_global *g) {
243  if (g->needrefresh==1) return(1);
244  return(0);
245}
246
247void owl_global_set_needrefresh(owl_global *g) {
248  g->needrefresh=1;
249}
250
251void owl_global_set_noneedrefresh(owl_global *g) {
252  g->needrefresh=0;
253}
254
255/* variable dictionary */
256
257owl_vardict *owl_global_get_vardict(owl_global *g) {
258  return &(g->vars);
259}
260
261/* command dictionary */
262
263owl_cmddict *owl_global_get_cmddict(owl_global *g) {
264  return &(g->cmds);
265}
266
267/* rightshift */
268
269void owl_global_set_rightshift(owl_global *g, int i) {
270  g->rightshift=i;
271}
272
273int owl_global_get_rightshift(owl_global *g) {
274  return(g->rightshift);
275}
276
277/* typwin */
278
279int owl_global_is_typwin_active(owl_global *g) {
280  if (g->typwinactive==1) return(1);
281  return(0);
282}
283
284void owl_global_set_typwin_active(owl_global *g) {
285  g->typwinactive=1;
286}
287
288void owl_global_set_typwin_inactive(owl_global *g) {
289  g->typwinactive=0;
290}
291
292/* resize */
293
294void owl_global_set_resize_pending(owl_global *g) {
295  g->resizepending=1;
296}
297
298char *owl_global_get_homedir(owl_global *g) {
299  if (g->homedir) return(g->homedir);
300  return("/");
301}
302
303int owl_global_get_direction(owl_global *g) {
304  return(g->direction);
305}
306
307void owl_global_set_direction_downwards(owl_global *g) {
308  g->direction=OWL_DIRECTION_DOWNWARDS;
309}
310
311void owl_global_set_direction_upwards(owl_global *g) {
312  g->direction=OWL_DIRECTION_UPWARDS;
313}
314
315/* perl stuff */
316
317void owl_global_set_perlinterp(owl_global *g, void *p) {
318  g->perl=p;
319}
320
321void *owl_global_get_perlinterp(owl_global *g) {
322  return(g->perl);
323}
324
325int owl_global_is_config_format(owl_global *g) {
326  if (g->config_format) return(1);
327  return(0);
328}
329
330void owl_global_set_config_format(owl_global *g, int state) {
331  if (state==1) {
332    g->config_format=1;
333  } else {
334    g->config_format=0;
335  }
336}
337
338void owl_global_set_have_config(owl_global *g) {
339  g->haveconfig=1;
340}
341
342void owl_global_set_no_have_config(owl_global *g) {
343  g->haveconfig=0;
344}
345
346int owl_global_have_config(owl_global *g) {
347  if (g->haveconfig) return(1);
348  return(0);
349}
350
351void owl_global_resize(owl_global *g, int x, int y) {
352  /* resize the screen.  If x or y is 0 use the terminal size */
353  struct winsize size;
354   
355  if (!g->resizepending) return;
356
357  /* delete the current windows */
358  delwin(g->recwin);
359  delwin(g->sepwin);
360  delwin(g->msgwin);
361  delwin(g->typwin);
362  if (!isendwin()) {
363    endwin();
364  }
365
366  refresh();
367
368  /* get the new size */
369  ioctl(STDIN_FILENO, TIOCGWINSZ, &size);
370  if (x==0) {
371    g->lines=size.ws_row;
372  } else {
373    g->lines=x;
374  }
375
376  if (y==0) {
377    g->cols=size.ws_col;
378  } else {
379    g->cols=y;
380  }
381
382  /* resizeterm(size.ws_row, size.ws_col); */
383
384  /* re-initialize the windows */
385  _owl_global_setup_windows(g);
386
387  /* refresh stuff */
388  g->needrefresh=1;
389  owl_mainwin_redisplay(&(g->mw));
390  sepbar(NULL);
391
392  if (owl_global_is_typwin_active(g)) {
393    owl_editwin_redisplay(&(g->tw), 0);
394  }     
395  /* TODO: this should handle other forms of popwins */
396  if (owl_popwin_is_active(owl_global_get_popwin(g)) 
397      && owl_global_get_viewwin(g)) {
398    owl_popwin_refresh(owl_global_get_popwin(g));
399    owl_viewwin_redisplay(owl_global_get_viewwin(g), 0);
400  }
401
402  owl_function_debugmsg("New size is %i lines, %i cols.", size.ws_row, size.ws_col);
403  owl_function_makemsg("");
404  g->resizepending=0;
405}
406
407/* debug */
408
409int owl_global_is_debug_fast(owl_global *g) {
410  if (g->debug) return(1);
411  return(0);
412}
413
414/* starttime */
415
416time_t owl_global_get_starttime(owl_global *g) {
417  return(g->starttime);
418}
419
420time_t owl_global_get_runtime(owl_global *g) {
421  return(time(NULL)-g->starttime);
422}
423
424void owl_global_get_runtime_string(owl_global *g, char *buff) {
425  time_t diff;
426
427  diff=time(NULL)-owl_global_get_starttime(g);
428
429  /* print something nicer later */   
430  sprintf(buff, "%i seconds", (int) diff);
431}
432
433char *owl_global_get_hostname(owl_global *g) {
434  if (g->thishost) return(g->thishost);
435  return("");
436}
437
438/* userclue */
439
440void owl_global_set_userclue(owl_global *g, int clue) {
441  g->userclue=clue;
442}
443
444void owl_global_add_userclue(owl_global *g, int clue) {
445  g->userclue|=clue;
446}
447
448int owl_global_get_userclue(owl_global *g) {
449  return(g->userclue);
450}
451
452int owl_global_is_userclue(owl_global *g, int clue) {
453  if (g->userclue & clue) return(1);
454  return(0);
455}
456
457/* viewwin */
458
459owl_viewwin *owl_global_get_viewwin(owl_global *g) {
460  return(&(g->vw));
461}
462
463
464/* vert offset */
465
466int owl_global_get_curmsg_vert_offset(owl_global *g) {
467  return(g->curmsg_vert_offset);
468}
469
470void owl_global_set_curmsg_vert_offset(owl_global *g, int i) {
471  g->curmsg_vert_offset=i;
472}
473
474/* startup args */
475
476void owl_global_set_startupargs(owl_global *g, int argc, char **argv) {
477  int i, len;
478
479  if (g->startupargs) owl_free(g->startupargs);
480 
481  len=0;
482  for (i=0; i<argc; i++) {
483    len+=strlen(argv[i])+5;
484  }
485  g->startupargs=malloc(len+5);
486
487  strcpy(g->startupargs, "");
488  for (i=0; i<argc; i++) {
489    sprintf(g->startupargs, "%s%s ", g->startupargs, argv[i]);
490  }
491  g->startupargs[strlen(g->startupargs)-1]='\0';
492}
493
494char *owl_global_get_startupargs(owl_global *g) {
495  if (g->startupargs) return(g->startupargs);
496  return("");
497}
498
499/* history */
500
501owl_history *owl_global_get_msg_history(owl_global *g) {
502  return(&(g->msghist));
503}
504
505owl_history *owl_global_get_cmd_history(owl_global *g) {
506  return(&(g->cmdhist));
507}
508
509/* filterlist */
510
511owl_list *owl_global_get_filterlist(owl_global *g) {
512  return(&(g->filterlist));
513}
514
515owl_filter *owl_global_get_filter(owl_global *g, char *name) {
516  int i, j;
517  owl_filter *f;
518
519  j=owl_list_get_size(&(g->filterlist));
520  for (i=0; i<j; i++) {
521    f=owl_list_get_element(&(g->filterlist), i);
522    if (!strcmp(name, owl_filter_get_name(f))) {
523      return(f);
524    }
525  }
526  return(NULL);
527}
528
529void owl_global_add_filter(owl_global *g, owl_filter *f) {
530  owl_list_append_element(&(g->filterlist), f);
531}
532
533void owl_global_remove_filter(owl_global *g, char *name) {
534  int i, j;
535  owl_filter *f;
536
537  j=owl_list_get_size(&(g->filterlist));
538  for (i=0; i<j; i++) {
539    f=owl_list_get_element(&(g->filterlist), i);
540    if (!strcmp(name, owl_filter_get_name(f))) {
541      owl_filter_free(f);
542      owl_list_remove_element(&(g->filterlist), i);
543      break;
544    }
545  }
546}
547
548/* nextmsgid */
549
550int owl_global_get_nextmsgid(owl_global *g) {
551  return(g->nextmsgid++);
552}
553
554/* current view */
555
556owl_view *owl_global_get_current_view(owl_global *g) {
557  return(&(g->current_view));
558}
559
560owl_filterelement *owl_global_get_filterelement_true(owl_global *g) {
561  return(&(g->fe_true));
562}
563
564owl_filterelement *owl_global_get_filterelement_false(owl_global *g) {
565  return(&(g->fe_false));
566}
567
568owl_filterelement *owl_global_get_filterelement_null(owl_global *g) {
569  return(&(g->fe_null));
570}
571
572/* has colors */
573
574int owl_global_get_hascolors(owl_global *g) {
575  if (g->hascolors) return(1);
576  return(0);
577}
578
579/* color pairs */
580
581int owl_global_get_colorpairs(owl_global *g) {
582  return(g->colorpairs);
583}
584
585/* puntlist */
586
587owl_list *owl_global_get_puntlist(owl_global *g) {
588  return(&(g->puntlist));
589}
590
591int owl_global_message_is_puntable(owl_global *g, owl_message *m) {
592  owl_list *pl;
593  int i, j;
594
595  pl=owl_global_get_puntlist(g);
596  j=owl_list_get_size(pl);
597  for (i=0; i<j; i++) {
598    if (owl_filter_message_match(owl_list_get_element(pl, i), m)) return(1);
599  }
600  return(0);
601}
602
603int owl_global_should_followlast(owl_global *g) {
604  owl_view *v;
605 
606  if (!owl_global_is__followlast(g)) return(0);
607 
608  v=owl_global_get_current_view(g);
609 
610  if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1);
611  return(0);
612}
613
614int owl_global_is_search_active(owl_global *g) {
615  if (g->searchactive) return(1);
616  return(0);
617}
618
619void owl_global_set_search_active(owl_global *g, char *string) {
620  g->searchactive=1;
621  if (g->searchstring != NULL) owl_free(g->searchstring);
622  g->searchstring=owl_strdup(string);
623}
624
625void owl_global_set_search_inactive(owl_global *g) {
626  g->searchactive=0;
627}
628
629char *owl_global_get_search_string(owl_global *g) {
630  if (g->searchstring==NULL) return("");
631  return(g->searchstring);
632}
633
634void owl_global_set_newmsgproc_pid(owl_global *g, int i) {
635  g->newmsgproc_pid=i;
636}
637
638int owl_global_get_newmsgproc_pid(owl_global *g) {
639  return(g->newmsgproc_pid);
640}
641
642void owl_global_add_to_malloced(owl_global *g, int i) {
643  g->malloced+=i;
644}
645
646void owl_global_add_to_freed(owl_global *g, int i) {
647  g->freed+=1;
648}
649
650int owl_global_get_malloced(owl_global *g) {
651  return(g->malloced);
652}
653
654int owl_global_get_freed(owl_global *g) {
655  return(g->freed);
656}
657
658int owl_global_get_meminuse(owl_global *g) {
659  return(g->malloced-g->freed);
660}
661
662/* AIM stuff */
663
664int owl_global_is_aimloggedin(owl_global *g)
665{
666  if (g->aim_loggedin) return(1);
667  return(0);
668}
669
670char *owl_global_get_aim_screenname(owl_global *g)
671{
672  return (g->aim_screenname);
673}
674
675void owl_global_set_aimloggedin(owl_global *g, char *screenname)
676{
677  g->aim_loggedin=1;
678  if (g->aim_screenname) owl_free(g->aim_screenname);
679  g->aim_screenname=owl_strdup(screenname);
680}
681
682void owl_global_set_aimnologgedin(owl_global *g)
683{
684  g->aim_loggedin=0;
685}
686
687aim_session_t *owl_global_get_aimsess(owl_global *g)
688{
689  return(&(g->aimsess));
690}
691
692aim_conn_t *owl_global_get_waitingconn(owl_global *g)
693{
694  return(&(g->waitingconn));
695}
696
697int owl_global_is_aimnop_time(owl_global *g)
698{
699  if (owl_timer_is_expired(&(g->aim_noop_timer))) return(1);
700  return(0);
701}
702
703void owl_global_aimnop_sent(owl_global *g)
704{
705  owl_timer_reset(&(g->aim_noop_timer));
706}
707
708owl_timer *owl_global_get_aim_login_timer(owl_global *g)
709{
710  return(&(g->aim_ignorelogin_timer));
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_messageuque_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  int i, j;
752  owl_style *s;
753 
754  j=owl_list_get_size(&(g->stylelist));
755  for (i=0; i<j; i++) {
756    s=owl_list_get_element(&(g->stylelist), i);
757    if (owl_style_matches_name(s, name)) {
758      return(s);
759    }
760  }
761  return(NULL);
762}
763
764void owl_global_add_style(owl_global *g, owl_style *s) {
765  owl_list_append_element(&(g->stylelist), s);
766}
Note: See TracBrowser for help on using the repository browser.