source: global.c @ 7d4fbcd

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7d4fbcd was 7d4fbcd, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
Initial check in
  • Property mode set to 100644
File size: 12.0 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
11#ifndef MAXHOSTNAMELEN
12#define MAXHOSTNAMELEN 256
13#endif
14
15void owl_global_init(owl_global *g) {
16  struct hostent *hent;
17  char hostname[MAXHOSTNAMELEN];
18  char buff[MAXPATHLEN];
19
20  gethostname(hostname, MAXHOSTNAMELEN);
21  hent=gethostbyname(hostname);
22  if (!hent) {
23    strcpy(g->thishost, "localhost");
24  } else {
25    strcpy(g->thishost, hent->h_name);
26  }
27
28  owl_context_init(&g->ctx);
29  owl_context_set_startup(&g->ctx);
30  g->curmsg=0;
31  g->topmsg=0;
32  g->needrefresh=1;
33
34  owl_variable_dict_setup(&(g->vars));
35  owl_cmddict_setup(&(g->cmds));
36
37  g->lines=LINES;
38  g->cols=COLS;
39
40  g->rightshift=0;
41
42  owl_editwin_init(&(g->tw), NULL, owl_global_get_typwin_lines(g), g->cols, OWL_EDITWIN_STYLE_ONELINE);
43
44  owl_keyhandler_init(&g->kh);
45  owl_keys_setup_keymaps(&g->kh);
46
47  owl_list_create(&(g->filterlist));
48  owl_list_create(&(g->puntlist));
49  g->curmsg_vert_offset=0;
50  g->resizepending=0;
51  g->typwinactive=0;
52  g->direction=OWL_DIRECTION_DOWNWARDS;
53  g->zaway=0;
54  if (has_colors()) {
55    g->hascolors=1;
56  }
57  g->colorpairs=COLOR_PAIRS;
58  g->debug=OWL_DEBUG;
59  g->starttime=time(NULL); /* assumes we call init only a start time */
60  strcpy(g->buffercommand, "");
61
62  owl_global_set_config_format(g, 0);
63  owl_global_set_userclue(g, OWL_USERCLUE_NONE);
64  owl_global_set_no_have_config(g);
65  owl_history_init(&(g->hist));
66  g->nextmsgid=0;
67
68  owl_filterelement_create_true(&(g->fe_true));
69  owl_filterelement_create_false(&(g->fe_false));
70  owl_filterelement_create_null(&(g->fe_null));
71
72  _owl_global_setup_windows(g);
73
74  /* Fill in some variables which don't have constant defaults */
75  /* TODO: come back later and check passwd file first */
76  strcpy(g->homedir, getenv("HOME"));
77  sprintf(buff, "%s/zlog/people/", owl_global_get_homedir(g));
78  owl_global_set_logpath(g, buff);
79  sprintf(buff, "%s/zlog/class/", owl_global_get_homedir(g));
80  owl_global_set_classlogpath(g, buff);
81 
82
83  owl_messagelist_create(&(g->msglist));
84  owl_mainwin_init(&(g->mw));
85  owl_popwin_init(&(g->pw));
86}
87
88void _owl_global_setup_windows(owl_global *g) {
89  int lines, cols, typwin_lines;
90
91  lines=g->lines;
92  cols=g->cols;
93  typwin_lines = owl_global_get_typwin_lines(g);
94
95  /* set the new window sizes */
96  g->recwinlines=g->lines-(typwin_lines+2);
97
98  /* create the new windows */
99  g->recwin=newwin(g->recwinlines, cols, 0, 0);
100  g->sepwin=newwin(1, cols, g->recwinlines, 0);
101  g->msgwin=newwin(1, cols, g->recwinlines+1, 0);
102  g->typwin=newwin(typwin_lines, cols, g->recwinlines+2, 0);
103
104  owl_editwin_set_curswin(&(g->tw), g->typwin, typwin_lines, g->cols);
105
106  idlok(g->typwin, FALSE);
107  idlok(g->recwin, FALSE);
108  idlok(g->sepwin, FALSE);
109  idlok(g->msgwin, FALSE);
110
111  nodelay(g->typwin, 1);
112  keypad(g->typwin, TRUE);
113  wmove(g->typwin, 0, 0);
114
115  meta(g->typwin, TRUE);
116}
117
118owl_context *owl_global_get_context(owl_global *g) {
119  return(&g->ctx);
120}
121                         
122int owl_global_get_lines(owl_global *g) {
123  return(g->lines);
124}
125
126int owl_global_get_cols(owl_global *g) {
127  return(g->cols);
128}
129
130int owl_global_get_recwin_lines(owl_global *g) {
131  return(g->recwinlines);
132}
133
134/* curmsg */
135
136int owl_global_get_curmsg(owl_global *g) {
137  return(g->curmsg);
138}
139
140void owl_global_set_curmsg(owl_global *g, int i) {
141  g->curmsg=i;
142  /* we will reset the vertical offset from here */
143  /* we might want to move this out to the functions later */
144  owl_global_set_curmsg_vert_offset(g, 0);
145}
146
147/* topmsg */
148
149int owl_global_get_topmsg(owl_global *g) {
150  return(g->topmsg);
151}
152
153void owl_global_set_topmsg(owl_global *g, int i) {
154  g->topmsg=i;
155}
156
157/* windows */
158
159owl_mainwin *owl_global_get_mainwin(owl_global *g) {
160  return(&(g->mw));
161}
162
163owl_popwin *owl_global_get_popwin(owl_global *g) {
164  return(&(g->pw));
165}
166
167/* msglist */
168
169owl_messagelist *owl_global_get_msglist(owl_global *g) {
170  return(&(g->msglist));
171}
172
173/* keyhandler */
174
175owl_keyhandler *owl_global_get_keyhandler(owl_global *g) {
176  return(&(g->kh));
177}
178
179/* curses windows */
180
181WINDOW *owl_global_get_curs_recwin(owl_global *g) {
182  return(g->recwin);
183}
184
185WINDOW *owl_global_get_curs_sepwin(owl_global *g) {
186  return(g->sepwin);
187}
188
189WINDOW *owl_global_get_curs_msgwin(owl_global *g) {
190  return(g->msgwin);
191}
192
193WINDOW *owl_global_get_curs_typwin(owl_global *g) {
194  return(g->typwin);
195}
196
197/* typwin */
198
199owl_editwin *owl_global_get_typwin(owl_global *g) {
200  return(&(g->tw));
201}
202
203/* buffercommand */
204
205void owl_global_set_buffercommand(owl_global *g, char *command) {
206  strcpy(g->buffercommand, command);
207}
208
209char *owl_global_get_buffercommand(owl_global *g) {
210  return(g->buffercommand);
211}
212
213/* refresh */
214
215int owl_global_is_needrefresh(owl_global *g) {
216  if (g->needrefresh==1) return(1);
217  return(0);
218}
219
220void owl_global_set_needrefresh(owl_global *g) {
221  g->needrefresh=1;
222}
223
224void owl_global_set_noneedrefresh(owl_global *g) {
225  g->needrefresh=0;
226}
227
228/* variable dictionary */
229
230owl_vardict *owl_global_get_vardict(owl_global *g) {
231  return &(g->vars);
232}
233
234/* command dictionary */
235
236owl_cmddict *owl_global_get_cmddict(owl_global *g) {
237  return &(g->cmds);
238}
239
240/* rightshift */
241
242void owl_global_set_rightshift(owl_global *g, int i) {
243  g->rightshift=i;
244}
245
246int owl_global_get_rightshift(owl_global *g) {
247  return(g->rightshift);
248}
249
250/* typwin */
251
252int owl_global_is_typwin_active(owl_global *g) {
253  if (g->typwinactive==1) return(1);
254  return(0);
255}
256
257void owl_global_set_typwin_active(owl_global *g) {
258  g->typwinactive=1;
259}
260
261void owl_global_set_typwin_inactive(owl_global *g) {
262  g->typwinactive=0;
263}
264
265/* resize */
266
267void owl_global_set_resize_pending(owl_global *g) {
268  g->resizepending=1;
269}
270
271char *owl_global_get_homedir(owl_global *g) {
272  return(g->homedir);
273}
274
275int owl_global_get_direction(owl_global *g) {
276  return(g->direction);
277}
278
279void owl_global_set_direction_downwards(owl_global *g) {
280  g->direction=OWL_DIRECTION_DOWNWARDS;
281}
282
283void owl_global_set_direction_upwards(owl_global *g) {
284  g->direction=OWL_DIRECTION_UPWARDS;
285}
286
287/* perl stuff */
288
289void owl_global_set_perlinterp(owl_global *g, void *p) {
290  g->perl=p;
291}
292
293void *owl_global_get_perlinterp(owl_global *g) {
294  return(g->perl);
295}
296
297int owl_global_is_config_format(owl_global *g) {
298  if (g->config_format) return(1);
299  return(0);
300}
301
302void owl_global_set_config_format(owl_global *g, int state) {
303  if (state==1) {
304    g->config_format=1;
305  } else {
306    g->config_format=0;
307  }
308}
309
310void owl_global_set_have_config(owl_global *g) {
311  g->haveconfig=1;
312}
313
314void owl_global_set_no_have_config(owl_global *g) {
315  g->haveconfig=0;
316}
317
318int owl_global_have_config(owl_global *g) {
319  if (g->haveconfig) return(1);
320  return(0);
321}
322
323void owl_global_resize(owl_global *g, int x, int y) {
324  /* resize the screen.  If x or y is 0 use the terminal size */
325  struct winsize size;
326   
327  if (!g->resizepending) return;
328
329  /* delete the current windows */
330  delwin(g->recwin);
331  delwin(g->sepwin);
332  delwin(g->msgwin);
333  delwin(g->typwin);
334  if (!isendwin()) {
335    endwin();
336  }
337
338  refresh();
339
340  /* get the new size */
341  ioctl(STDIN_FILENO, TIOCGWINSZ, &size);
342  if (x==0) {
343    g->lines=size.ws_row;
344  } else {
345    g->lines=x;
346  }
347
348  if (y==0) {
349    g->cols=size.ws_col;
350  } else {
351    g->cols=y;
352  }
353
354  /* resizeterm(size.ws_row, size.ws_col); */
355
356  /* re-initialize the windows */
357  _owl_global_setup_windows(g);
358
359  /* refresh stuff */
360  g->needrefresh=1;
361  owl_mainwin_redisplay(&(g->mw));
362  sepbar(NULL);
363
364  if (owl_global_is_typwin_active(g)) {
365    owl_editwin_redisplay(&(g->tw), 0);
366  }     
367  /* TODO: this should handle other forms of popwins */
368  if (owl_popwin_is_active(owl_global_get_popwin(g)) 
369      && owl_global_get_viewwin(g)) {
370    owl_popwin_refresh(owl_global_get_popwin(g));
371    owl_viewwin_redisplay(owl_global_get_viewwin(g), 0);
372  }
373
374  /*
375  char buff[1024];
376  sprintf(buff, "New size is %i lines, %i cols.\n", size.ws_row, size.ws_col);
377  owl_function_makemsg(buff);
378  */
379  owl_function_makemsg("");
380
381  g->resizepending=0;
382}
383
384/* tty (not fully implemented yet) */
385
386void owl_global_set_tty(owl_global *g, char *tty) {
387  if (tty) {
388    strcpy(g->thistty, tty);
389  } else if (getenv("DISPLAY")) {
390    strcpy(g->thistty, getenv("DISPLAY"));
391  } else if (ttyname(fileno(stdout))) {
392    strcpy(g->thistty, ttyname(fileno(stdout)));
393    if (!strncmp(g->thistty, "/dev/", 5)) {
394      strcpy(g->thistty, g->thistty+5);
395    }
396  } else {
397    strcpy(g->thistty, "unknown");
398  }
399   
400#ifdef HAVE_LIBZEPHYR_ZINITLOCATIONINFO
401  ZInitLocationInfo(g->thishost, g->thistty); 
402#endif
403}
404
405
406/* debug */
407
408int owl_global_is_debug_fast(owl_global *g) {
409  if (g->debug) return(1);
410  return(0);
411}
412
413/* starttime */
414
415time_t owl_global_get_starttime(owl_global *g) {
416  return(g->starttime);
417}
418
419time_t owl_global_get_runtime(owl_global *g) {
420  return(time(NULL)-g->starttime);
421}
422
423void owl_global_get_runtime_string(owl_global *g, char *buff) {
424  time_t diff;
425
426  diff=time(NULL)-owl_global_get_starttime(g);
427
428  /* print something nicer later */   
429  sprintf(buff, "%i seconds", (int) diff);
430}
431
432/* userclue */
433
434void owl_global_set_userclue(owl_global *g, int clue) {
435  g->userclue=clue;
436}
437
438void owl_global_add_userclue(owl_global *g, int clue) {
439  g->userclue|=clue;
440}
441
442int owl_global_get_userclue(owl_global *g) {
443  return(g->userclue);
444}
445
446int owl_global_is_userclue(owl_global *g, int clue) {
447  if (g->userclue & clue) return(1);
448  return(0);
449}
450
451/* viewwin */
452
453owl_viewwin *owl_global_get_viewwin(owl_global *g) {
454  return(&(g->vw));
455}
456
457
458/* vert offset */
459
460int owl_global_get_curmsg_vert_offset(owl_global *g) {
461  return(g->curmsg_vert_offset);
462}
463
464void owl_global_set_curmsg_vert_offset(owl_global *g, int i) {
465  g->curmsg_vert_offset=i;
466}
467
468/* startup args */
469
470void owl_global_set_startupargs(owl_global *g, int argc, char **argv) {
471  int i;
472
473  strcpy(g->startupargs, "");
474  for (i=0; i<argc; i++) {
475    sprintf(g->startupargs, "%s%s ", g->startupargs, argv[i]);
476  }
477  g->startupargs[strlen(g->startupargs)-1]='\0';
478}
479
480char *owl_global_get_startupargs(owl_global *g) {
481  return(g->startupargs);
482}
483
484/* history */
485
486owl_history *owl_global_get_history(owl_global *g) {
487  return(&(g->hist));
488}
489
490/* filterlist */
491
492owl_list *owl_global_get_filterlist(owl_global *g) {
493  return(&(g->filterlist));
494}
495
496owl_filter *owl_global_get_filter(owl_global *g, char *name) {
497  int i, j;
498  owl_filter *f;
499
500  j=owl_list_get_size(&(g->filterlist));
501  for (i=0; i<j; i++) {
502    f=owl_list_get_element(&(g->filterlist), i);
503    if (!strcmp(name, owl_filter_get_name(f))) {
504      return(f);
505    }
506  }
507  return(NULL);
508}
509
510void owl_global_add_filter(owl_global *g, owl_filter *f) {
511  owl_list_append_element(&(g->filterlist), f);
512}
513
514void owl_global_remove_filter(owl_global *g, char *name) {
515  int i, j;
516  owl_filter *f;
517
518  j=owl_list_get_size(&(g->filterlist));
519  for (i=0; i<j; i++) {
520    f=owl_list_get_element(&(g->filterlist), i);
521    if (!strcmp(name, owl_filter_get_name(f))) {
522      owl_filter_free(f);
523      owl_list_remove_element(&(g->filterlist), i);
524      break;
525    }
526  }
527}
528
529/* nextmsgid */
530
531int owl_global_get_nextmsgid(owl_global *g) {
532  return(g->nextmsgid++);
533}
534
535/* current view */
536
537owl_view *owl_global_get_current_view(owl_global *g) {
538  return(&(g->current_view));
539}
540
541owl_filterelement *owl_global_get_filterelement_true(owl_global *g) {
542  return(&(g->fe_true));
543}
544
545owl_filterelement *owl_global_get_filterelement_false(owl_global *g) {
546  return(&(g->fe_false));
547}
548
549owl_filterelement *owl_global_get_filterelement_null(owl_global *g) {
550  return(&(g->fe_null));
551}
552
553/* has colors */
554
555int owl_global_get_hascolors(owl_global *g) {
556  if (g->hascolors) return(1);
557  return(0);
558}
559
560/* color pairs */
561
562int owl_global_get_colorpairs(owl_global *g) {
563  return(g->colorpairs);
564}
565
566/* puntlist */
567
568owl_list *owl_global_get_puntlist(owl_global *g) {
569  return(&(g->puntlist));
570}
571
572int owl_global_message_is_puntable(owl_global *g, owl_message *m) {
573  owl_list *pl;
574  int i, j;
575
576  pl=owl_global_get_puntlist(g);
577  j=owl_list_get_size(pl);
578  for (i=0; i<j; i++) {
579    if (owl_filter_message_match(owl_list_get_element(pl, i), m)) return(1);
580  }
581  return(0);
582}
583
584int owl_global_should_followlast(owl_global *g) {
585  owl_view *v;
586 
587  if (!owl_global_is__followlast(g)) return(0);
588 
589  v=owl_global_get_current_view(g);
590 
591  if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1);
592  return(0);
593}
Note: See TracBrowser for help on using the repository browser.