]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/drivers/console.cpp
update
[l4.git] / kernel / fiasco / src / drivers / console.cpp
1 INTERFACE:
2
3 #include <cstddef>
4 #include "l4_types.h"
5
6 /**
7  * The abstract interface for a text I/O console.
8  *
9  * This abstract interface can be implemented for virtually every
10  * text input or output device.
11  */
12 class Console
13 {
14 public:
15   enum Console_state
16   {
17     DISABLED    =     0,
18     INENABLED   =     1, ///< output channel of console enabled
19     OUTENABLED  =     2, ///< input channel of console enabled
20     ENABLED     =     INENABLED | OUTENABLED ///< console fully enabled
21   };
22
23   enum Console_attr
24   {
25     // universal attributes
26     INVALID     =     0,
27     OUT         =   0x1, ///< output to console is possible
28     IN          =   0x2, ///< input from console is possible
29     // attributes to identify a specific console
30     DIRECT      =   0x4, ///< output to screen or input from keyboard
31     UART        =   0x8, ///< output to/input from serial serial line
32     UX          =  0x10, ///< filtered input console for UX
33     PUSH        =  0x20, ///< input console
34     GZIP        =  0x40, ///< gzip+uuencode output and sent to uart console
35     BUFFER      =  0x80, ///< ring buffer
36     DEBUG       = 0x100, ///< kdb interface
37     FAILED      = 0x200, ///< initialization failed
38   };
39
40   /**
41    * modify console state
42    */
43   virtual void state(Mword new_state);
44
45   /**
46    * Write a string of len chacters to the output.
47    * @param str the string to write (no zero termination is needed)
48    * @param len the number of chacters to write.
49    *
50    * This method must be implemented in every implementation, but
51    * can simply do nothing for input only consoles.
52    */
53   virtual int write(char const *str, size_t len);
54
55   /**
56    * read a charcater from the input.
57    * @param blocking if true getchar blocks til a charcater is available.
58    *
59    * This method must be implemented in every implementation, but
60    * can simply return -1 for output only consoles.
61    */
62   virtual int getchar(bool blocking = true);
63
64   /**
65    * Is input available?
66    *
67    * This method can be implemented.
68    * It must return -1 if no information is available, 
69    * 1 if at least one character is avialable, and 0 if
70    * no charachter is available.
71    */
72   virtual int char_avail() const;
73
74   /**
75    * Console attributes.
76    */
77   virtual Mword get_attributes() const;
78
79   virtual ~Console();
80
81   explicit Console(Console_state state) : _state(state) {}
82
83   void add_state(Console_state state)
84   { _state |= state; }
85
86   void del_state(Console_state state)
87   { _state &= ~state; }
88
89 public:
90   /**
91    * Disables the stdout, stdin, and stderr console.
92    */
93   static void disable_all();
94
95   /// stdout for libc glue.
96   static Console *stdout;
97   /// stderr for libc glue.
98   static Console *stderr;
99   /// stdin for libc glue.
100   static Console *stdin;
101
102 protected:
103   Mword  _state;
104 };
105
106
107
108 IMPLEMENTATION:
109
110 #include <cstring>
111 #include <cctype>
112
113 Console *Console::stdout;
114 Console *Console::stderr;
115 Console *Console::stdin;
116
117 IMPLEMENT Console::~Console()
118 {}
119
120 /**
121  * get current console state
122  */
123 PUBLIC inline
124 Mword
125 Console::state() const
126 {
127   return _state;
128 }
129
130 IMPLEMENT
131 void Console::state(Mword new_state)
132 {
133   _state = new_state;
134 }
135
136 PUBLIC inline
137 bool
138 Console::failed() const
139 {
140   return _state & FAILED;
141 }
142
143 PUBLIC inline
144 void
145 Console::fail()
146 {
147   _state |= FAILED;
148 }
149
150 IMPLEMENT
151 void Console::disable_all()
152 {
153   stdout = 0;
154   stderr = 0;
155   stdin  = 0;
156 }
157
158 IMPLEMENT
159 int Console::write(char const *, size_t len)
160 {
161   return len;
162 }
163
164 IMPLEMENT
165 int Console::getchar(bool /* blocking */)
166 {
167   return -1; /* no input */
168 }
169
170 IMPLEMENT
171 int Console::char_avail() const
172 {
173   return -1; /* unknown */
174 }
175
176 IMPLEMENT
177 Mword Console::get_attributes() const
178 {
179   return 0;
180 }
181
182 IMPLEMENTATION[debug]:
183
184 PUBLIC
185 const char*
186 Console::str_mode() const
187 {
188   static char const * const mode_str[] =
189     { "      ", "Output", "Input ", "InOut " };
190   return mode_str[get_attributes() & (OUT|IN)];
191 }
192
193 PUBLIC
194 const char*
195 Console::str_state() const
196 {
197   static char const * const state_str[] =
198     { "Disabled       ", "Output disabled",
199       "Input disabled ", "Enabled        " };
200   return state_str[state() & ENABLED];
201 }
202
203 PUBLIC
204 const char*
205 Console::str_attr(Mword bit) const
206 {
207   static char const * const attr_str[] =
208     { "Direct", "Uart", "UX", "Push", "Gzip", "Buffer", "Kdb", "FAILED!" };
209
210   return (bit < 2 || bit >= (sizeof(attr_str)/sizeof(attr_str[0]))+2)
211     ? "???"
212     : attr_str[bit-2];
213 }
214