]> 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     DISABLED_INIT =   4, ///< the console remains disabled during boot
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 public:
82   /**
83    * Disables the stdout, stdin, and stderr console.
84    */
85   static void disable_all();
86
87   /// stdout for libc glue.
88   static Console *stdout;
89   /// stderr for libc glue.
90   static Console *stderr;
91   /// stdin for libc glue.
92   static Console *stdin;
93
94 protected:
95   Mword  _state;
96
97 public:
98   void *operator new (size_t, void *p) throw() { return p; }
99 };
100
101
102
103 IMPLEMENTATION:
104
105 #include <cstring>
106 #include <cctype>
107
108 Console *Console::stdout;
109 Console *Console::stderr;
110 Console *Console::stdin;
111
112 IMPLEMENT Console::~Console()
113 {}
114
115 /**
116  * get current console state
117  */
118 PUBLIC inline
119 Mword
120 Console::state() const
121 {
122   return _state;
123 }
124
125 IMPLEMENT
126 void Console::state(Mword new_state)
127 {
128   _state = new_state;
129 }
130
131 PUBLIC inline
132 bool
133 Console::failed() const
134 {
135   return _state & FAILED;
136 }
137
138 PUBLIC inline
139 void
140 Console::fail()
141 {
142   _state |= FAILED;
143 }
144
145 IMPLEMENT
146 void Console::disable_all()
147 {
148   stdout = 0;
149   stderr = 0;
150   stdin  = 0;
151 }
152
153 IMPLEMENT
154 int Console::write(char const *, size_t len)
155 {
156   return len;
157 }
158
159 IMPLEMENT
160 int Console::getchar(bool /* blocking */)
161 {
162   return -1; /* no input */
163 }
164
165 IMPLEMENT
166 int Console::char_avail() const
167 {
168   return -1; /* unknown */
169 }
170
171 IMPLEMENT
172 Mword Console::get_attributes() const
173 {
174   return 0;
175 }
176
177 IMPLEMENTATION[debug]:
178
179 PUBLIC
180 const char*
181 Console::str_mode() const
182 {
183   static char const * const mode_str[] =
184     { "      ", "Output", "Input ", "InOut " };
185   return mode_str[get_attributes() & (OUT|IN)];
186 }
187
188 PUBLIC
189 const char*
190 Console::str_state() const
191 {
192   static char const * const state_str[] =
193     { "Disabled       ", "Output disabled",
194       "Input disabled ", "Enabled        " };
195   return state_str[state() & (INENABLED|OUTENABLED)];
196 }
197
198 PUBLIC
199 const char*
200 Console::str_attr(Mword bit) const
201 {
202   static char const * const attr_str[] =
203     { "Direct", "Uart", "UX", "Push", "Gzip", "Buffer", "Kdb", "FAILED!" };
204
205   return (bit < 2 || bit >= (sizeof(attr_str)/sizeof(attr_str[0]))+2)
206     ? "???"
207     : attr_str[bit-2];
208 }
209