Debian Codegraph @

src/terminal.c

raw
1 /* netToe Version 1.5.1
2 *
3 * Copyright 2000,2001 Gabriele Giorgetti <[email protected]>
4 * 2009-2014 Mats Erik Andersson <[email protected]>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #if defined USE_TERMINFO || defined USE_TERMCAP
30 # if (defined HAVE_NCURSES_H || defined HAVE_NCURSES_NCURSES_H) \
31 && (defined USE_TINFO || defined USE_NCURSES)
32 # if defined HAVE_NCURSES_NCURSES_H
33 # include <ncurses/ncurses.h>
34 # else
35 # include <ncurses.h>
36 # endif
37 # endif /* NCURSES */
38 # if defined HAVE_CURSES_H && defined USE_CURSES
39 # include <curses.h>
40 # endif /* CURSES */
41 # if defined HAVE_NCURSES_TERM_H && USE_NCURSES
42 # include <ncurses/term.h>
43 # elif defined HAVE_TERM_H && !USE_TERMCAP
44 # include <term.h>
45 # elif defined HAVE_TERMCAP_H && USE_TERMCAP
46 # include <termcap.h>
47 # endif /* TERM || TERMCAP */
48
49 # if !HAVE_PUTP
50 # define putp(x) tputs((x), 1, putchar)
51 # endif
52 #endif /* Terminal capabilities. */
53
54 #include "terminal.h"
55 #include "game.h"
56
57 #if defined USE_TERMINFO || defined USE_TERMCAP
58 static char *clear_str = NULL;
59 static char *bell_str = NULL;
60 static char *bold_str = NULL;
61 static char *rmso_str = NULL;
62 static char *smso_str = NULL;
63 static char *sgr0_str = NULL;
64 static char *setab_str = NULL;
65 static char *setaf_str = NULL;
66
67 static int using_terminfo = 0;
68
69 void setup_terminfo (void)
70 {
71 # if HAVE_SETUPTERM
72 int err;
73
74 /* Error can be ignored, since we fall back
75 * to ECMA/ANSI character sequences. Catching
76 * errors in ERR is needed to avoid error output. */
77 if (setupterm(NULL, fileno(stdout), &err) == OK)
78 using_terminfo = 1;
79 # elif HAVE_TGETENT
80 static char buf[1024];
81 char *p = buf, *sp;
82
83 if ((sp = getenv("TERM")) == NULL)
84 sp = "vt100"; /* Hoping for the best. */
85
86 /* Error can be ignored, since we fall back
87 * to ECMA/ANSI character sequences. */
88 if (tgetent(buf, sp) == 1)
89 using_terminfo = 1;
90 # endif /* !HAVE_SETUPTERM && HAVE_TGETENT */
91
92 # if USE_TERMINFO
93 if (using_terminfo)
94 {
95 clear_str = tigetstr("clear");
96 bell_str = tigetstr("bel");
97 bold_str = tigetstr("bold");
98 rmso_str = tigetstr("rmso");
99 smso_str = tigetstr("smso");
100 sgr0_str = tigetstr("sgr0");
101 setab_str = tigetstr("setab");
102 setaf_str = tigetstr("setaf");
103 }
104 # elif USE_TERMCAP
105 if (using_terminfo)
106 {
107 clear_str = tgetstr("cl", &p);
108 bell_str = tgetstr("bl", &p);
109 bold_str = tgetstr("md", &p);
110 rmso_str = tgetstr("se", &p);
111 smso_str = tgetstr("so", &p);
112 sgr0_str = tgetstr("me", &p);
113 setab_str = tgetstr("AB", &p);
114 setaf_str = tgetstr("AF", &p);
115 }
116 # endif /* USE_TERMCAP */
117 }
118 #endif /* Terminal capabilities. */
119
120
121 void nettoe_term_clear (void)
122 {
123 #if USE_TERMINFO || USE_TERMCAP
124 if (clear_str)
125 putp(clear_str);
126 else
127 #endif
128 {
129 if (system("clear 2>/dev/null"))
130 fprintf(stderr, "Terminal access failed.\n");
131 }
132 }
133
134
135 /* same as nettoe_term_reset_color ( void ) */
136 void nettoe_term_set_default_color (void)
137 {
138 if (NO_COLORS == 1)
139 return;
140
141 #if USE_TERMINFO || USE_TERMCAP
142 if (setaf_str)
143 {
144 /* Based on ECMA-48. */
145 # if USE_TERMINFO
146 putp(tparm(setaf_str, COLOR_DEFAULT));
147
148 if (setab_str)
149 putp(tparm(setab_str, COLOR_DEFAULT));
150 # else /* USE_TERMCAP */
151 printf(setaf_str, COLOR_DEFAULT);
152
153 if (setab_str)
154 printf(setab_str, COLOR_DEFAULT);
155 # endif /* USE_TERMCAP */
156
157 return;
158 }
159 #endif /* USE_TERMINFO || USE_TERMCAP */
160 /* nettoe_term_set_color (COLOR_WHITE, ATTRIB_RESET); */
161
162 return;
163 }
164
165
166 void nettoe_term_reset_color ( void )
167 {
168 if (NO_COLORS == 1)
169 return;
170
171 #if USE_TERMINFO || USE_TERMCAP
172 if (sgr0_str)
173 putp(sgr0_str);
174 else if (rmso_str)
175 putp(rmso_str);
176 else
177 #endif /* USE_TERMINFO || USE_TERMCAP */
178 printf("%c[0m", 0x1B);
179
180 return;
181 }
182
183
184 void nettoe_term_set_color (int fg, int attrib)
185 {
186 char cmd_str[13];
187
188 if (NO_COLORS == 1 )
189 return;
190
191 if ( fg != COLOR_WHITE)
192 {
193 /* cmd_str is the control command to the terminal */
194 sprintf(cmd_str, "%c[%d;%dm", 0x1B, attrib, fg + 30);
195 }
196 else
197 {
198 /* Text colour COLOR_WHITE is left for the terminal to decide. */
199 sprintf(cmd_str, "%c[%dm", 0x1B, attrib);
200 }
201
202 #if USE_TERMINFO || USE_TERMCAP
203 if (setaf_str && (fg != COLOR_WHITE))
204 {
205 if (bold_str)
206 putp(bold_str);
207 # if USE_TERMINFO
208 putp(tparm(setaf_str, fg));
209 # else /* USE_TERMCAP */
210 printf(setaf_str, fg);
211 # endif /* USE_TERMCAP */
212 }
213 else
214 #endif /* USE_TERMINFO || USE_TERMCAP */
215 printf("%s", cmd_str);
216
217 return;
218 }
219
220 void nettoe_beep (void)
221 {
222 if ( NO_BEEP == 1 )
223 return;
224
225 #if USE_TERMINFO || USE_TERMCAP
226 if (bell_str)
227 putp(bell_str);
228 else
229 #endif /* USE_TERMINFO || USE_TERMCAP */
230 printf("\a");
231 } /* beep(void) */