2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief Types used by terminal interfaces
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2011-07-05
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-01-10 21:44:47 +01:00
|
|
|
* Copyright (C) 2011-2013 Genode Labs GmbH
|
2011-12-22 16:19:25 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _TERMINAL__TYPES_H_
|
|
|
|
#define _TERMINAL__TYPES_H_
|
|
|
|
|
|
|
|
namespace Terminal {
|
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
struct Character;
|
|
|
|
struct Boundary;
|
|
|
|
struct Offset;
|
|
|
|
struct Position;
|
|
|
|
struct Character_array;
|
|
|
|
template <unsigned, unsigned> class Static_character_array;
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
/*
|
|
|
|
* The character definition is wrapped in a compound data structure to make
|
|
|
|
* the implementation easily changeable to unicode later.
|
|
|
|
*/
|
|
|
|
struct Terminal::Character
|
|
|
|
{
|
|
|
|
unsigned char c;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
Character() : c(0) { }
|
|
|
|
Character(unsigned char c) : c(c) { }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2016-05-11 18:21:47 +02:00
|
|
|
bool valid() const { return c != 0; }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
unsigned char ascii() const { return c; }
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
struct Terminal::Boundary
|
|
|
|
{
|
|
|
|
int const width, height;
|
|
|
|
Boundary(int width, int height) : width(width), height(height) { }
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
struct Terminal::Offset
|
|
|
|
{
|
|
|
|
int const x, y;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
Offset(int x, int y) : x(x), y(y) { }
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
struct Terminal::Position
|
|
|
|
{
|
|
|
|
int x, y;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
Position() : x(0), y(0) { }
|
|
|
|
Position(int x, int y) : x(x), y(y) { }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
Position operator + (Offset const &offset) {
|
|
|
|
return Position(x + offset.x, y + offset.y); }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
bool operator == (Position const &pos) const {
|
|
|
|
return (pos.x == x) && (pos.y == y); }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
bool operator != (Position const &pos) const {
|
|
|
|
return (pos.x != x) || (pos.y != y); }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
/**
|
|
|
|
* Return true if position lies within the specified boundaries
|
|
|
|
*/
|
|
|
|
bool lies_within(Boundary const &boundary) const
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
2015-03-04 21:12:14 +01:00
|
|
|
return x >= 0 && x < boundary.width
|
|
|
|
&& y >= 0 && y < boundary.height;
|
|
|
|
}
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
struct Terminal::Character_array
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Assign character to specified position
|
|
|
|
*/
|
|
|
|
virtual void set(Position const &pos, Character c) = 0;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
/**
|
|
|
|
* Request character at specified position
|
|
|
|
*/
|
|
|
|
virtual Character get(Position const &pos) const = 0;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
/**
|
|
|
|
* Return array boundary
|
|
|
|
*/
|
|
|
|
virtual Boundary boundary() const = 0;
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
template <unsigned WIDTH, unsigned HEIGHT>
|
|
|
|
class Terminal::Static_character_array : public Character_array
|
|
|
|
{
|
|
|
|
private:
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
Character _array[HEIGHT][WIDTH];
|
|
|
|
Boundary const _boundary;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
public:
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
Static_character_array() : _boundary(WIDTH, HEIGHT) { }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
void set(Position const &pos, Character c)
|
|
|
|
{
|
|
|
|
if (pos.lies_within(_boundary))
|
|
|
|
_array[pos.y][pos.x] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
Character get(Position const &pos) const
|
|
|
|
{
|
|
|
|
if (pos.lies_within(_boundary))
|
|
|
|
return _array[pos.y][pos.x];
|
|
|
|
else
|
|
|
|
return Character();
|
|
|
|
}
|
|
|
|
|
|
|
|
Boundary boundary() const { return _boundary; }
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
#endif /* _TERMINAL__TYPES_H_ */
|