9 * This implementation of the Console interface can be used to
10 * multiplex among some input, output, and in-out consoles.
12 class Mux_console : public Console
18 SIZE = 8 ///< The maximum number of consoles to be multiplexed.
21 int write( char const *str, size_t len );
22 int getchar( bool blocking = true );
23 int char_avail() const;
35 #include "processor.h"
38 Mux_console::Mux_console()
39 : _next_getchar(-1), _items(0)
44 Mux_console::write( char const *str, size_t len )
46 for(int i=0; i<_items; ++i)
47 if(_cons[i] && (_cons[i]->state() & OUTENABLED))
48 _cons[i]->write(str,len);
55 Mux_console::getchar( bool blocking )
57 if (_next_getchar != -1)
59 int c = _next_getchar;
66 for(int i=0; i<_items; ++i)
68 if(_cons[i] && (_cons[i]->state() & INENABLED))
70 ret = _cons[i]->getchar( false );
77 } while( blocking && ret==-1 );
83 * deliver attributes of all subconsoles.
87 Mux_console::get_attributes() const
91 for (int i=0; i<_items; i++)
93 attr |= _cons[i]->get_attributes();
100 Mux_console::getchar_chance ()
102 for (int i=0; i<_items; ++i)
104 if (_cons[i] && (_cons[i]->state() & INENABLED) &&
105 _cons[i]->char_avail() == 1)
107 int c = _cons[i]->getchar(false);
108 if (c != -1 && _next_getchar == -1)
116 Mux_console::char_avail() const
119 for(int i=0; i<_items; ++i)
120 if(_cons[i] && (_cons[i]->state() & INENABLED))
122 int tmp = _cons[i]->char_avail();
132 * Register a console to be multiplexed.
133 * @param cons the Console to add.
134 * @param pos the position of the console, normally not needed.
138 Mux_console::register_console( Console *c, int pos = 0)
146 if(pos>=SIZE || pos<0)
153 for(int i = _items-1; i>=pos; --i)
154 _cons[i+1] = _cons[i];
158 if (_cons[pos]->state() & DISABLED_INIT)
159 _cons[pos]->state(DISABLED);
161 _cons[pos]->state(INENABLED|OUTENABLED);
167 * Unregister a console from the multiplexer.
168 * @param cons the console to remove.
172 Mux_console::unregister_console( Console *c )
175 for(pos = 0; pos < _items && _cons[pos]!=c; pos++)
181 for(int i = pos; i<_items; ++i)
182 _cons[i] = _cons[i+1];
188 * Change the state of a group of consoles specified by
190 * @param any_true match if console has any of these attributes
191 * @param all_false match if console doesn't have any of these attributes
195 Mux_console::change_state(Mword any_true, Mword all_false,
196 Mword mask, Mword bits)
198 for (int i=0; i<_items; i++)
202 Mword attr = _cons[i]->get_attributes();
203 if ( // any bit of the any_true attributes must be set
204 (!any_true || (attr & any_true) != 0)
205 // all bits of the all_false attributes must be cleared
206 && (!all_false || (attr & all_false) == 0))
208 _cons[i]->state((_cons[i]->state() & mask) | bits);
215 * Find a console with a specific attribute.
216 * @param any_true match to console which has set any bit of this bitmask
220 Mux_console::find_console(Mword any_true)
222 for (int i=0; i<_items; i++)
224 if (_cons[i] && _cons[i]->get_attributes() & any_true)
232 * Start exclusive mode for a specific console. Only the one
233 * console which matches to any_true is enabled for input and
234 * output. All other consoles are disabled.
235 * @param any_true match to console which has set any bit of this bitmask
239 Mux_console::start_exclusive(Mword any_true)
241 // enable exclusive console
242 change_state(any_true, 0, ~0UL, (OUTENABLED|INENABLED));
243 // disable all other consoles
244 change_state(0, any_true, ~(OUTENABLED|INENABLED), 0);
248 * End exclusive mode for a specific console.
249 * @param any_true match to console which has set any bit of this bitmask
253 Mux_console::end_exclusive(Mword any_true)
255 // disable exclusive console
256 change_state(any_true, 0, ~(OUTENABLED|INENABLED), 0);
257 // enable all other consoles
258 change_state(0, any_true, ~0UL, (OUTENABLED|INENABLED));
262 IMPLEMENTATION[debug]:
266 Mux_console::list_consoles()
268 for (int i=0; i<_items; i++)
272 Mword attr = _cons[i]->get_attributes();
274 printf(" "L4_PTR_FMT" %s (%s) ",
275 attr, _cons[i]->str_mode(), _cons[i]->str_state());
276 for (unsigned bit=2; bit<sizeof(attr)*4; bit++)
278 printf("%s ", Console::str_attr(bit));