ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/backend/ndk/terminal.cpp
Revision: 1.2
Committed: Sun Oct 24 20:31:51 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -1 lines
Log Message:
use EV and plain sockets instead of asio

File Contents

# Content
1 /* terminal.cc
2 * This file is part of ncursesxx library
3 * Copyright (c) 2003,2004 by Stanislav Ievlev
4 *
5 * This file is covered by the GNU Library General Public License,
6 * which should be included with libncursesxx as the file COPYING.
7 */
8 #include <ndk/terminal.hh>
9
10 #include <iostream>
11
12 #include <stdexcept>
13
14 using ndk::terminal;
15
16 static TERMINAL *
17 topen (int fd, char const *termname)
18 {
19 /**< open new terminal descriptor */
20
21 TERMINAL *new_term = 0;
22 TERMINAL *prev_term = cur_term;
23
24 cur_term = 0;
25 int erret;
26 /* mutex up here */
27 if (setupterm (const_cast<char *> (termname), fd, &erret) != OK)
28 {
29 switch (erret)
30 {
31 case 1: throw std::runtime_error ("terminal is hardcopy");
32 case 0: throw std::runtime_error ("unknown terminal");
33 case -1: throw std::runtime_error ("terminfo database not found");
34 default: throw std::runtime_error ("unknown error code");
35 }
36 }
37 new_term = cur_term;
38 set_curterm (prev_term);
39 /* mutex down here */
40 return new_term;
41 }
42
43 static void
44 tclose (TERMINAL *term)
45 {
46 /**< close terminal descriptor */
47 if (!term)
48 return;
49 del_curterm (term);
50 }
51
52 static char const *
53 ttigetstr (TERMINAL *term, char const *capname)
54 {
55 /**< get string capability for the specified terminal */
56 /* mutex up here */
57 TERMINAL *prev_term = set_curterm (term);
58 char const *value = tigetstr (const_cast<char *> (capname));
59
60 set_curterm (prev_term);
61 /* mutex down here */
62
63 if (value == reinterpret_cast<char const *> (-1)) /* FIXME: made assertion here */
64 throw std::runtime_error ("wrong string capability");
65 return value;
66 }
67
68 static int
69 ttigetnum (TERMINAL *term, char const *capname)
70 {
71 /**< get numeric capability for the specified terminal */
72 /* mutex up here */
73 TERMINAL *prev_term = set_curterm (term);
74 int value = tigetnum (const_cast<char *> (capname));
75
76 set_curterm (prev_term);
77 /* mutex down here */
78
79 if (value == -2) /* FIXME: made assertion here */
80 throw std::runtime_error ("wrong numeric capability");
81 return value;
82 }
83
84 static int
85 tvidputs (TERMINAL *term, chtype mode, int (*putc)(int))
86 {
87 /**< change attributes of the specified terminal */
88 /* mutex up here */
89 TERMINAL *prev_term = set_curterm (term);
90 int ret = vidputs (mode, putc);
91
92 set_curterm (prev_term);
93 /* mutex down here */
94 return ret;
95 }
96
97 /* special hack from ncurses library: there was changes in ncurses library some years ago */
98 static int
99 toggled_colors (int c)
100 {
101 if (c < 16)
102 {
103 int const table[] = { 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15 };
104 c = table[c];
105 }
106 return c;
107 }
108
109 static terminal::buffer_type *out_buf = 0; /**< output buffer */
110
111 static int
112 stream_put (int ch)
113 {
114 return out_buf->sputc (ch);
115 }
116
117 terminal::terminal (terminal::buffer_type &buf, char const *termname)
118 : buf_ (buf)
119 , attr_ (A_NORMAL)
120 , color_ (-1)
121 {
122 term_ = topen (buf_.fd (), termname);
123 colors_num_ = ttigetnum (term_, "colors");
124 if (colors_num_ < 0)
125 colors_num_ = 0; /*< black and white*/
126 }
127
128 terminal::~terminal ()
129 {
130 if (term_)
131 {
132 set_attr (); /* reset attributes to default */
133 reset_colors (); /* return orig colors */
134 tclose (term_);
135 }
136 reset_shell_mode ();
137 }
138
139 void
140 terminal::reset_colors ()
141 {
142 if (!colors_num_)
143 return;
144
145 /* mutex up here */
146 out_buf = &buf_;
147 char const *orig_pair_cap = ttigetstr (term_, "op");
148 char const *orig_colors_cap = ttigetstr (term_, "oc");
149 if (orig_pair_cap)
150 tputs (tparm (const_cast<char *> (orig_pair_cap)),
151 1, stream_put);
152 if (orig_colors_cap)
153 tputs (tparm (const_cast<char *> (orig_colors_cap)),
154 1, stream_put);
155 /* mutex down here */
156 }
157
158 void
159 terminal::set_attr (attr_t mode)
160 {
161 if (!term_)
162 return;
163
164 /* mutex up here */
165 out_buf = &buf_;
166 tvidputs (term_, (colors_num_ ? (mode | A_COLOR) : mode), stream_put);
167 /* mutex down here */
168 attr_ = mode; /* save current mode */
169 }
170
171 void
172 terminal::set_fg_color (color_t color)
173 {
174 if (!term_ || !colors_num_)
175 return;
176
177 /* mutex up here */
178 char const *set_a_foreground_cap = ttigetstr (term_, "setaf");
179 char const *set_foreground_cap = ttigetstr (term_, "setf");
180 out_buf = &buf_;
181 if (set_a_foreground_cap)
182 tputs (tparm (const_cast<char *> (set_a_foreground_cap), color),
183 1, stream_put);
184 else if (set_foreground_cap)
185 tputs (tparm (const_cast<char *> (set_foreground_cap), toggled_colors (color)),
186 1, stream_put);
187 /* mutex down here */
188 color_ = color; /* save current color */
189 }
190
191 /* todo read previous attributes before, termattrs() */
192 void
193 terminal::set_fg (color_t color, attr_t mode)
194 {
195 set_attr (mode);
196 set_fg_color (color);
197 }
198
199 ndk::attr_t
200 terminal::attr () const
201 {
202 return attr_;
203 }
204
205 ndk::color_t
206 terminal::color () const
207 {
208 return color_;
209 }
210
211 int
212 terminal::colors_num () const
213 {
214 return colors_num_;
215 }