BRL-CAD
cursor.c
Go to the documentation of this file.
1 /* C U R S O R . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 /* interface header */
24 #include "cursor.h"
25 
26 /* system headers */
27 
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #ifdef HAVE_TERMLIB_H
32 # include <termlib.h>
33 #else
34 # ifdef HAVE_NCURSES_H
35 # include <ncurses.h>
36 # else
37 # ifdef HAVE_CURSES_H
38 # include <curses.h>
39 # else
40 # ifdef HAVE_TERMCAP_H
41 # include <termcap.h>
42 # else
43 # ifdef HAVE_TERMINFO_H
44 # include <terminfo.h>
45 # else
46 # ifdef HAVE_TINFO_H
47 # include <tinfo.h>
48 # endif
49 # endif
50 # endif
51 # endif
52 # endif
53 # ifdef HAVE_TERM_H
54 # include <term.h>
55 # endif
56 #endif
57 
58 #ifdef HAVE_SYS_IOCTL_H
59 # include <sys/ioctl.h>
60 #endif
61 #define _winsize winsize /* For compat with _ioctl.h. */
62 
63 #define TBUFSIZ 1024
64 #define MAX_TERM_LEN 80
65 
66 static FILE *out_fp; /* Output stream. */
67 static char termCapBuf[TBUFSIZ]; /* Termcap entry. */
68 static char tstrings[TBUFSIZ]; /* Individual TCS. */
69 static char *tstr_addr = tstrings; /* Used by tgetstr(). */
70 #ifdef TIOCGWINSZ
71 static int fd_stdout = 1;
72 #endif
73 
74 static void LoadTP(void);
75 static void LoadTCS(void);
76 
77 
78 /* This is a global buffer for the name of the terminal. */
79 static char termName[MAX_TERM_LEN] = "UNKNOWN";
80 
81 /* from termcap/termlib library */
82 extern char *BC; /* Backspace. */
83 extern char *UP; /* Cursor up one line. */
84 
85 /* Individual terminal control strings (TCS). */
86 char *CS; /* Change scrolling region. */
87 char *SO; /* Begin standout mode. */
88 char *SE; /* End standout mode. */
89 char *CE; /* Clear to end of line. */
90 char *CL; /* Clear display and home cursor. */
91 char *HO; /* Home cursor. */
92 char *CM; /* Screen-relative cursor motion. */
93 char *TI; /* Initialize terminal. */
94 char *DL; /* Delete line. */
95 char *SR; /* Scroll text down. */
96 char *SF; /* Scroll text up. */
97 
98 /* Individual terminal parameters. */
99 int LI; /* Number of lines on screen. */
100 int CO; /* Number of columns on screen. */
101 
102 
103 /* This function must be accessible to the termcap library, but will
104  * not necessarily be needed by the application.
105  */
106 int PutChr(int c);
107 
108 
109 /*
110  * Get terminal name from environment and leave in 'termName' for
111  * external reference.
112  * Read termcap entry into 'termCapBuf'.
113  */
114 int
115 InitTermCap(FILE *fp)
116 {
117  char *term; /* Name of terminal from environment ($TERM).*/
118  out_fp = fp;
119 #ifdef TIOCGWINSZ
120  fd_stdout = fileno(out_fp);
121 #endif
122  if ((term = getenv("TERM")) == NULL) {
123  (void) fprintf(stderr, "TERM not set or exported!\n");
124  return 0;
125  } else {
126  (void) strncpy(termName, term, MAX_TERM_LEN); /* intentionally not bu_strlcpy to not add libbu dep */
127  termName[sizeof(termName) - 1] = '\0';
128  }
129 
130  /* Get terminal entry. */
131  switch (tgetent(termCapBuf, term)) {
132  case -1 :
133  (void) fprintf(stderr, "Can't open termcap file!\n");
134  return 0;
135  case 0 :
136  (void) fprintf(stderr,
137  "Terminal type not in termcap file!\n"
138  );
139  return 0;
140  }
141 
142  /* Get individual terminal parameters and control strings. */
143  LoadTP();
144  LoadTCS();
145 
146  tputs(TI, 1, PutChr); /* Initialize terminal. */
147  return 1; /* All is well. */
148 }
149 
150 
151 /*
152  * Get the terminal parameters.
153  */
154 static void
155 LoadTP(void)
156 {
157 #ifdef TIOCGWINSZ
158  /* Get window size for DMD layers support. */
159  struct _winsize window;
160 
161  if (ioctl(fd_stdout, TIOCGWINSZ, &window) == 0
162  && window.ws_row != 0 && window.ws_col != 0
163  )
164  {
165  LI = (int) window.ws_row;
166  CO = (int) window.ws_col;
167  return;
168  }
169 #endif
170 
171  LI = tgetnum("li");
172  CO = tgetnum("co");
173  return;
174 }
175 
176 
177 /*
178  * Get the terminal control strings.
179  */
180 static void
181 LoadTCS(void)
182 {
183  CS = tgetstr("cs", &tstr_addr);
184  SE = tgetstr("se", &tstr_addr);
185  SO = tgetstr("so", &tstr_addr);
186  CE = tgetstr("ce", &tstr_addr);
187  CL = tgetstr("cl", &tstr_addr);
188  HO = tgetstr("ho", &tstr_addr);
189  CM = tgetstr("cm", &tstr_addr);
190  BC = tgetstr("bc", &tstr_addr);
191  UP = tgetstr("up", &tstr_addr);
192  TI = tgetstr("ti", &tstr_addr);
193  DL = tgetstr("dl", &tstr_addr);
194  SR = tgetstr("sr", &tstr_addr);
195  SF = tgetstr("sf", &tstr_addr);
196  return;
197 }
198 
199 
200 int
201 HmCursor(void)
202 {
203  if (HO != NULL) {
204  tputs(HO, 1, PutChr);
205  return 1;
206  } else {
207  return 0;
208  }
209 }
210 
211 
212 int
213 ScrollUp(void)
214 {
215  if (SF != NULL) {
216  tputs(SF, 1, PutChr);
217  return 1;
218  } else {
219  return 0;
220  }
221 }
222 
223 
224 int
225 ScrollDn(void)
226 {
227  if (SR != NULL) {
228  tputs(SR, 1, PutChr);
229  return 1;
230  } else {
231  return 0;
232  }
233 }
234 
235 
236 int
237 DeleteLn(void)
238 {
239  if (DL != NULL) {
240  tputs(DL, 1, PutChr);
241  return 1;
242  } else {
243  return 0;
244  }
245 }
246 
247 
248 int
249 MvCursor(int x, int y)
250 {
251  /* Tgoto() adds 1 to each coordinate!? */
252  --x;
253  --y;
254 
255  if (CM != NULL) {
256  tputs(tgoto(CM, x, y), 1, PutChr);
257  return 1;
258  } else {
259  return 0;
260  }
261 }
262 
263 
264 int
265 ClrEOL(void)
266 {
267  if (CE != NULL) {
268  tputs(CE, 1, PutChr);
269  return 1;
270  } else {
271  return 0;
272  }
273 }
274 
275 
276 int
277 ClrText(void)
278 {
279  if (CL != NULL) {
280  tputs(CL, LI, PutChr);
281  return 1;
282  } else {
283  return 0;
284  }
285 }
286 
287 
288 int
289 SetScrlReg(int top, int btm)
290 {
291  if (CS != NULL) {
292  tputs(tgoto(CS, btm-1, top-1), 1, PutChr);
293  return 1;
294  } else {
295  return 0;
296  }
297 }
298 
299 
300 int
302 {
303  if (CS != NULL) {
304  tputs(tgoto(CS, LI-1, 0), 1, PutChr);
305  return 1;
306  } else {
307  return 0;
308  }
309 }
310 
311 
312 int
314 {
315  if (SE != NULL) {
316  tputs(SE, 1, PutChr);
317  return 1;
318  } else {
319  return 0;
320  }
321 }
322 
323 
324 int
326 {
327  if (SO != NULL) {
328  tputs(SO, 1, PutChr);
329  return 1;
330  } else {
331  return 0;
332  }
333 }
334 
335 
336 int
337 PutChr(int c)
338 {
339  return putc(c, out_fp);
340 }
341 
342 
343 /*
344  * Local Variables:
345  * mode: C
346  * tab-width: 8
347  * indent-tabs-mode: t
348  * c-file-style: "stroustrup"
349  * End:
350  * ex: shiftwidth=4 tabstop=8
351  */
char * UP
int LI
Definition: cursor.c:99
int ResetScrlReg(void)
Definition: cursor.c:301
char * HO
Definition: cursor.c:91
int ScrollDn(void)
Definition: cursor.c:225
#define _winsize
Definition: cursor.c:61
int ScrollUp(void)
Definition: cursor.c:213
Header file for the BRL-CAD common definitions.
int InitTermCap(FILE *fp)
Definition: cursor.c:115
int SetStandout(void)
Definition: cursor.c:325
int ClrText(void)
Definition: cursor.c:277
int ClrEOL(void)
Definition: cursor.c:265
int PutChr(int c)
Definition: cursor.c:337
int CO
Definition: cursor.c:100
int MvCursor(int x, int y)
Definition: cursor.c:249
static void top()
int HmCursor(void)
Definition: cursor.c:201
char * SF
Definition: cursor.c:96
char * SO
Definition: cursor.c:87
#define TBUFSIZ
Definition: cursor.c:63
char * CM
Definition: cursor.c:92
char * CE
Definition: cursor.c:89
char * DL
Definition: cursor.c:94
#define MAX_TERM_LEN
Definition: cursor.c:64
int DeleteLn(void)
Definition: cursor.c:237
char * SE
Definition: cursor.c:88
int SetScrlReg(int top, int btm)
Definition: cursor.c:289
int ClrStandout(void)
Definition: cursor.c:313
char * CS
Definition: cursor.c:86
char * SR
Definition: cursor.c:95
char * TI
Definition: cursor.c:93
char * BC
char * CL
Definition: cursor.c:90