source: global.c @ 217a43e

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