source: global.c @ 2c8d62e

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2c8d62e was 700c712, checked in by James M. Kretchmar <kretch@mit.edu>, 22 years ago
Started adding code for newmsgproc. It doesn't fully work yet! Don't use it!
  • Property mode set to 100644
File size: 12.2 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  gethostname(hostname, MAXHOSTNAMELEN);
22  hent=gethostbyname(hostname);
23  if (!hent) {
24    strcpy(g->thishost, "localhost");
25  } else {
26    strcpy(g->thishost, hent->h_name);
27  }
28
29  owl_context_init(&g->ctx);
30  owl_context_set_startup(&g->ctx);
31  g->curmsg=0;
32  g->topmsg=0;
33  g->needrefresh=1;
34
35  owl_variable_dict_setup(&(g->vars));
36  owl_cmddict_setup(&(g->cmds));
37
38  g->lines=LINES;
39  g->cols=COLS;
40
41  g->rightshift=0;
42
43  owl_editwin_init(&(g->tw), NULL, owl_global_get_typwin_lines(g), g->cols, OWL_EDITWIN_STYLE_ONELINE, NULL);
44
45  owl_keyhandler_init(&g->kh);
46  owl_keys_setup_keymaps(&g->kh);
47
48  owl_list_create(&(g->filterlist));
49  owl_list_create(&(g->puntlist));
50  g->curmsg_vert_offset=0;
51  g->resizepending=0;
52  g->typwinactive=0;
53  g->direction=OWL_DIRECTION_DOWNWARDS;
54  g->zaway=0;
55  if (has_colors()) {
56    g->hascolors=1;
57  }
58  g->colorpairs=COLOR_PAIRS;
59  g->debug=OWL_DEBUG;
60  g->searchactive=0;
61  g->searchstring=NULL;
62  g->starttime=time(NULL); /* assumes we call init only a start time */
63  strcpy(g->buffercommand, "");
64  g->newmsgproc_pid=0;
65 
66  owl_global_set_config_format(g, 0);
67  owl_global_set_userclue(g, OWL_USERCLUE_NONE);
68  owl_global_set_no_have_config(g);
69  owl_history_init(&(g->msghist));
70  owl_history_init(&(g->cmdhist));
71  g->nextmsgid=0;
72
73  owl_filterelement_create_true(&(g->fe_true));
74  owl_filterelement_create_false(&(g->fe_false));
75  owl_filterelement_create_null(&(g->fe_null));
76
77  _owl_global_setup_windows(g);
78
79  /* Fill in some variables which don't have constant defaults */
80  /* TODO: come back later and check passwd file first */
81  strcpy(g->homedir, getenv("HOME"));
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/* debug */
385
386int owl_global_is_debug_fast(owl_global *g) {
387  if (g->debug) return(1);
388  return(0);
389}
390
391/* starttime */
392
393time_t owl_global_get_starttime(owl_global *g) {
394  return(g->starttime);
395}
396
397time_t owl_global_get_runtime(owl_global *g) {
398  return(time(NULL)-g->starttime);
399}
400
401void owl_global_get_runtime_string(owl_global *g, char *buff) {
402  time_t diff;
403
404  diff=time(NULL)-owl_global_get_starttime(g);
405
406  /* print something nicer later */   
407  sprintf(buff, "%i seconds", (int) diff);
408}
409
410char *owl_global_get_hostname(owl_global *g) {
411  return(g->thishost);
412}
413
414/* userclue */
415
416void owl_global_set_userclue(owl_global *g, int clue) {
417  g->userclue=clue;
418}
419
420void owl_global_add_userclue(owl_global *g, int clue) {
421  g->userclue|=clue;
422}
423
424int owl_global_get_userclue(owl_global *g) {
425  return(g->userclue);
426}
427
428int owl_global_is_userclue(owl_global *g, int clue) {
429  if (g->userclue & clue) return(1);
430  return(0);
431}
432
433/* viewwin */
434
435owl_viewwin *owl_global_get_viewwin(owl_global *g) {
436  return(&(g->vw));
437}
438
439
440/* vert offset */
441
442int owl_global_get_curmsg_vert_offset(owl_global *g) {
443  return(g->curmsg_vert_offset);
444}
445
446void owl_global_set_curmsg_vert_offset(owl_global *g, int i) {
447  g->curmsg_vert_offset=i;
448}
449
450/* startup args */
451
452void owl_global_set_startupargs(owl_global *g, int argc, char **argv) {
453  int i;
454
455  strcpy(g->startupargs, "");
456  for (i=0; i<argc; i++) {
457    sprintf(g->startupargs, "%s%s ", g->startupargs, argv[i]);
458  }
459  g->startupargs[strlen(g->startupargs)-1]='\0';
460}
461
462char *owl_global_get_startupargs(owl_global *g) {
463  return(g->startupargs);
464}
465
466/* history */
467
468owl_history *owl_global_get_msg_history(owl_global *g) {
469  return(&(g->msghist));
470}
471
472owl_history *owl_global_get_cmd_history(owl_global *g) {
473  return(&(g->cmdhist));
474}
475
476/* filterlist */
477
478owl_list *owl_global_get_filterlist(owl_global *g) {
479  return(&(g->filterlist));
480}
481
482owl_filter *owl_global_get_filter(owl_global *g, char *name) {
483  int i, j;
484  owl_filter *f;
485
486  j=owl_list_get_size(&(g->filterlist));
487  for (i=0; i<j; i++) {
488    f=owl_list_get_element(&(g->filterlist), i);
489    if (!strcmp(name, owl_filter_get_name(f))) {
490      return(f);
491    }
492  }
493  return(NULL);
494}
495
496void owl_global_add_filter(owl_global *g, owl_filter *f) {
497  owl_list_append_element(&(g->filterlist), f);
498}
499
500void owl_global_remove_filter(owl_global *g, char *name) {
501  int i, j;
502  owl_filter *f;
503
504  j=owl_list_get_size(&(g->filterlist));
505  for (i=0; i<j; i++) {
506    f=owl_list_get_element(&(g->filterlist), i);
507    if (!strcmp(name, owl_filter_get_name(f))) {
508      owl_filter_free(f);
509      owl_list_remove_element(&(g->filterlist), i);
510      break;
511    }
512  }
513}
514
515/* nextmsgid */
516
517int owl_global_get_nextmsgid(owl_global *g) {
518  return(g->nextmsgid++);
519}
520
521/* current view */
522
523owl_view *owl_global_get_current_view(owl_global *g) {
524  return(&(g->current_view));
525}
526
527owl_filterelement *owl_global_get_filterelement_true(owl_global *g) {
528  return(&(g->fe_true));
529}
530
531owl_filterelement *owl_global_get_filterelement_false(owl_global *g) {
532  return(&(g->fe_false));
533}
534
535owl_filterelement *owl_global_get_filterelement_null(owl_global *g) {
536  return(&(g->fe_null));
537}
538
539/* has colors */
540
541int owl_global_get_hascolors(owl_global *g) {
542  if (g->hascolors) return(1);
543  return(0);
544}
545
546/* color pairs */
547
548int owl_global_get_colorpairs(owl_global *g) {
549  return(g->colorpairs);
550}
551
552/* puntlist */
553
554owl_list *owl_global_get_puntlist(owl_global *g) {
555  return(&(g->puntlist));
556}
557
558int owl_global_message_is_puntable(owl_global *g, owl_message *m) {
559  owl_list *pl;
560  int i, j;
561
562  pl=owl_global_get_puntlist(g);
563  j=owl_list_get_size(pl);
564  for (i=0; i<j; i++) {
565    if (owl_filter_message_match(owl_list_get_element(pl, i), m)) return(1);
566  }
567  return(0);
568}
569
570int owl_global_should_followlast(owl_global *g) {
571  owl_view *v;
572 
573  if (!owl_global_is__followlast(g)) return(0);
574 
575  v=owl_global_get_current_view(g);
576 
577  if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1);
578  return(0);
579}
580
581int owl_global_is_search_active(owl_global *g) {
582  if (g->searchactive) return(1);
583  return(0);
584}
585
586void owl_global_set_search_active(owl_global *g, char *string) {
587  g->searchactive=1;
588  if (g->searchstring != NULL) owl_free(g->searchstring);
589  g->searchstring=owl_strdup(string);
590}
591
592void owl_global_set_search_inactive(owl_global *g) {
593  g->searchactive=0;
594}
595
596char *owl_global_get_search_string(owl_global *g) {
597  if (g->searchstring==NULL) return("");
598  return(g->searchstring);
599}
600
601void owl_global_set_newmsgproc_pid(owl_global *g, int i) {
602  g->newmsgproc_pid=i;
603}
604
605int owl_global_get_newmsgproc_pid(owl_global *g) {
606  return(g->newmsgproc_pid);
607}
608
Note: See TracBrowser for help on using the repository browser.