ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/backend/ncxx/terminal.cpp
Revision: 1.1
Committed: Sun Oct 17 08:15:07 2010 UTC (16 years, 1 month ago) by sf-pippijn
Branch: MAIN
Log Message:
initial import

File Contents

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