xref: /haiku/src/apps/terminal/TerminalLine.h (revision c0b591c58f38815fa8030e8116879fb08a30226e)
14c9d4b02SIngo Weinhold /*
25b41331fSSiarzhuk Zharski  * Copyright 2013, Haiku, Inc. All rights reserved.
34c9d4b02SIngo Weinhold  * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
44c9d4b02SIngo Weinhold  * Distributed under the terms of the MIT License.
55b41331fSSiarzhuk Zharski  *
65b41331fSSiarzhuk Zharski  * Authors:
75b41331fSSiarzhuk Zharski  *		Ingo Weinhold, ingo_weinhold@gmx.de
85b41331fSSiarzhuk Zharski  *		Siarzhuk Zharski, zharik@gmx.li
94c9d4b02SIngo Weinhold  */
104c9d4b02SIngo Weinhold #ifndef TERMINAL_LINE_H
114c9d4b02SIngo Weinhold #define TERMINAL_LINE_H
124c9d4b02SIngo Weinhold 
13*c0b591c5SJessica Hamilton #include <GraphicsDefs.h>
144c9d4b02SIngo Weinhold #include <SupportDefs.h>
154c9d4b02SIngo Weinhold 
16627ced12SAdrien Destugues #include "TermConst.h"
17627ced12SAdrien Destugues 
184c9d4b02SIngo Weinhold #include "UTF8Char.h"
194c9d4b02SIngo Weinhold 
204c9d4b02SIngo Weinhold 
21*c0b591c5SJessica Hamilton struct Attributes {
22*c0b591c5SJessica Hamilton 	uint32 state;
23*c0b591c5SJessica Hamilton 	uint32 foreground;
24*c0b591c5SJessica Hamilton 	uint32 background;
25*c0b591c5SJessica Hamilton 
26*c0b591c5SJessica Hamilton 	Attributes() : state(0), foreground(0), background(0) {}
27*c0b591c5SJessica Hamilton 
28*c0b591c5SJessica Hamilton 	inline void Reset() { state = 0; foreground = 0; background = 0; }
29*c0b591c5SJessica Hamilton 
30*c0b591c5SJessica Hamilton 	inline bool IsWidth() const { return (state & A_WIDTH) == A_WIDTH; }
31*c0b591c5SJessica Hamilton 	inline bool IsBold() const { return (state & BOLD) == BOLD; }
32*c0b591c5SJessica Hamilton 	inline bool IsUnder() const { return (state & UNDERLINE) == UNDERLINE; }
33*c0b591c5SJessica Hamilton 	inline bool IsInverse() const { return (state & INVERSE) == INVERSE; }
34*c0b591c5SJessica Hamilton 	inline bool IsMouse() const { return (state & MOUSE) == MOUSE; }
35*c0b591c5SJessica Hamilton 	inline bool IsForeSet() const { return (state & FORESET) == FORESET; }
36*c0b591c5SJessica Hamilton 	inline bool IsBackSet() const { return (state & BACKSET) == BACKSET; }
37*c0b591c5SJessica Hamilton 	inline bool IsFont() const { return (state & FONT) == FONT; }
38*c0b591c5SJessica Hamilton 	inline bool IsCR() const { return (state & DUMPCR) == DUMPCR; }
39*c0b591c5SJessica Hamilton 
40*c0b591c5SJessica Hamilton 	inline void SetDirectForeground(uint8 red, uint8 green, uint8 blue)
41*c0b591c5SJessica Hamilton 	{
42*c0b591c5SJessica Hamilton 		foreground = 0x80000000 | (red << 16) | (green << 8) | blue;
43*c0b591c5SJessica Hamilton 		state &= ~FORECOLOR;
44*c0b591c5SJessica Hamilton 		state |= FORESET;
45*c0b591c5SJessica Hamilton 	}
46*c0b591c5SJessica Hamilton 
47*c0b591c5SJessica Hamilton 	inline void SetDirectBackground(uint8 red, uint8 green, uint8 blue)
48*c0b591c5SJessica Hamilton 	{
49*c0b591c5SJessica Hamilton 		background = 0x80000000 | (red << 16) | (green << 8) | blue;
50*c0b591c5SJessica Hamilton 		state &= ~BACKCOLOR;
51*c0b591c5SJessica Hamilton 		state |= BACKSET;
52*c0b591c5SJessica Hamilton 	}
53*c0b591c5SJessica Hamilton 
54*c0b591c5SJessica Hamilton 	inline void SetIndexedForeground(uint32 index)
55*c0b591c5SJessica Hamilton 	{
56*c0b591c5SJessica Hamilton 		state &= ~FORECOLOR;
57*c0b591c5SJessica Hamilton 		state |= FORESET;
58*c0b591c5SJessica Hamilton 		state |= FORECOLORED(index);
59*c0b591c5SJessica Hamilton 		foreground = 0;
60*c0b591c5SJessica Hamilton 	}
61*c0b591c5SJessica Hamilton 
62*c0b591c5SJessica Hamilton 	inline void SetIndexedBackground(uint32 index)
63*c0b591c5SJessica Hamilton 	{
64*c0b591c5SJessica Hamilton 		state &= ~BACKCOLOR;
65*c0b591c5SJessica Hamilton 		state |= BACKSET;
66*c0b591c5SJessica Hamilton 		state |= BACKCOLORED(index);
67*c0b591c5SJessica Hamilton 		background = 0;
68*c0b591c5SJessica Hamilton 	}
69*c0b591c5SJessica Hamilton 
70*c0b591c5SJessica Hamilton 	inline void UnsetForeground()
71*c0b591c5SJessica Hamilton 	{
72*c0b591c5SJessica Hamilton 		state &= ~FORESET;
73*c0b591c5SJessica Hamilton 		foreground = 0;
74*c0b591c5SJessica Hamilton 	}
75*c0b591c5SJessica Hamilton 
76*c0b591c5SJessica Hamilton 	inline void UnsetBackground()
77*c0b591c5SJessica Hamilton 	{
78*c0b591c5SJessica Hamilton 		state &= ~BACKSET;
79*c0b591c5SJessica Hamilton 		background = 0;
80*c0b591c5SJessica Hamilton 	}
81*c0b591c5SJessica Hamilton 
82*c0b591c5SJessica Hamilton 	inline rgb_color
83*c0b591c5SJessica Hamilton 	ForegroundColor(const rgb_color* indexedColors) const
84*c0b591c5SJessica Hamilton 	{
85*c0b591c5SJessica Hamilton 		if ((foreground & 0x80000000) != 0)
86*c0b591c5SJessica Hamilton 			return make_color((foreground >> 16) & 0xFF,
87*c0b591c5SJessica Hamilton 				(foreground >> 8) & 0xFF,
88*c0b591c5SJessica Hamilton 				foreground & 0xFF);
89*c0b591c5SJessica Hamilton 		else
90*c0b591c5SJessica Hamilton 			return indexedColors[(state & FORECOLOR) >> 16];
91*c0b591c5SJessica Hamilton 	}
92*c0b591c5SJessica Hamilton 
93*c0b591c5SJessica Hamilton 	inline rgb_color
94*c0b591c5SJessica Hamilton 	BackgroundColor(const rgb_color* indexedColors) const
95*c0b591c5SJessica Hamilton 	{
96*c0b591c5SJessica Hamilton 		if ((background & 0x80000000) != 0)
97*c0b591c5SJessica Hamilton 			return make_color((background >> 16) & 0xFF,
98*c0b591c5SJessica Hamilton 				(background >> 8) & 0xFF,
99*c0b591c5SJessica Hamilton 				background & 0xFF);
100*c0b591c5SJessica Hamilton 		else
101*c0b591c5SJessica Hamilton 			return indexedColors[(state & BACKCOLOR) >> 24];
102*c0b591c5SJessica Hamilton 	}
103*c0b591c5SJessica Hamilton 
104*c0b591c5SJessica Hamilton 	inline Attributes&
105*c0b591c5SJessica Hamilton 	operator&=(uint32 value) { state &= value; return *this; }
106*c0b591c5SJessica Hamilton 
107*c0b591c5SJessica Hamilton 	inline Attributes&
108*c0b591c5SJessica Hamilton 	operator|=(uint32 value) { state |= value; return *this; }
109*c0b591c5SJessica Hamilton 
110*c0b591c5SJessica Hamilton 	inline uint32
111*c0b591c5SJessica Hamilton 	operator|(uint32 value) { return state | value; }
112*c0b591c5SJessica Hamilton 
113*c0b591c5SJessica Hamilton 	inline uint32
114*c0b591c5SJessica Hamilton 	operator&(uint32 value) { return state & value; }
115*c0b591c5SJessica Hamilton 
116*c0b591c5SJessica Hamilton 	inline bool
117*c0b591c5SJessica Hamilton 	operator==(const Attributes& other) const
118*c0b591c5SJessica Hamilton 	{
119*c0b591c5SJessica Hamilton 		return state == other.state
120*c0b591c5SJessica Hamilton 			&& foreground == other.foreground
121*c0b591c5SJessica Hamilton 			&& background == other.background;
122*c0b591c5SJessica Hamilton 	}
123*c0b591c5SJessica Hamilton 
124*c0b591c5SJessica Hamilton 	inline bool
125*c0b591c5SJessica Hamilton 	operator!=(const Attributes& other) const
126*c0b591c5SJessica Hamilton 	{
127*c0b591c5SJessica Hamilton 		return state != other.state
128*c0b591c5SJessica Hamilton 			|| foreground != other.foreground
129*c0b591c5SJessica Hamilton 			|| background != other.background;
130*c0b591c5SJessica Hamilton 	}
131*c0b591c5SJessica Hamilton };
132*c0b591c5SJessica Hamilton 
133*c0b591c5SJessica Hamilton 
1344c9d4b02SIngo Weinhold struct TerminalCell {
1354c9d4b02SIngo Weinhold 	UTF8Char			character;
136*c0b591c5SJessica Hamilton 	Attributes			attributes;
1374c9d4b02SIngo Weinhold };
1384c9d4b02SIngo Weinhold 
1394c9d4b02SIngo Weinhold 
1404c9d4b02SIngo Weinhold struct TerminalLine {
14117b889deSIngo Weinhold 	uint16			length;
1424c9d4b02SIngo Weinhold 	bool			softBreak;	// soft line break
143*c0b591c5SJessica Hamilton 	Attributes		attributes;
1444c9d4b02SIngo Weinhold 	TerminalCell	cells[1];
1454c9d4b02SIngo Weinhold 
146*c0b591c5SJessica Hamilton 	inline void Clear()
147*c0b591c5SJessica Hamilton 	{
148*c0b591c5SJessica Hamilton 		Clear(Attributes());
149*c0b591c5SJessica Hamilton 	}
150*c0b591c5SJessica Hamilton 
151*c0b591c5SJessica Hamilton 	inline void Clear(size_t count)
152*c0b591c5SJessica Hamilton 	{
153*c0b591c5SJessica Hamilton 		Clear(Attributes(), count);
154*c0b591c5SJessica Hamilton 	}
155*c0b591c5SJessica Hamilton 
156*c0b591c5SJessica Hamilton 	inline void Clear(Attributes attr, size_t count = 0)
1574c9d4b02SIngo Weinhold 	{
1584c9d4b02SIngo Weinhold 		length = 0;
159b512213bSSiarzhuk Zharski 		attributes = attr;
1604c9d4b02SIngo Weinhold 		softBreak = false;
161b512213bSSiarzhuk Zharski 		for (size_t i = 0; i < count; i++)
162b512213bSSiarzhuk Zharski 			cells[i].attributes = attr;
1634c9d4b02SIngo Weinhold 	}
1644c9d4b02SIngo Weinhold };
1654c9d4b02SIngo Weinhold 
1664c9d4b02SIngo Weinhold 
1674c9d4b02SIngo Weinhold struct AttributesRun {
168*c0b591c5SJessica Hamilton 	Attributes	attributes;
1694c9d4b02SIngo Weinhold 	uint16	offset;			// character offset
1704c9d4b02SIngo Weinhold 	uint16	length;			// length of the run in characters
1714c9d4b02SIngo Weinhold };
1724c9d4b02SIngo Weinhold 
1734c9d4b02SIngo Weinhold 
1744c9d4b02SIngo Weinhold struct HistoryLine {
1754c9d4b02SIngo Weinhold 	AttributesRun*	attributesRuns;
1764c9d4b02SIngo Weinhold 	uint16			attributesRunCount;	// number of attribute runs
1774c9d4b02SIngo Weinhold 	uint16			byteLength : 15;	// number of bytes in the line
1784c9d4b02SIngo Weinhold 	bool			softBreak : 1;		// soft line break;
179*c0b591c5SJessica Hamilton 	Attributes		attributes;
1804c9d4b02SIngo Weinhold 
1814c9d4b02SIngo Weinhold 	AttributesRun* AttributesRuns() const
1824c9d4b02SIngo Weinhold 	{
1834c9d4b02SIngo Weinhold 		return attributesRuns;
1844c9d4b02SIngo Weinhold 	}
1854c9d4b02SIngo Weinhold 
1864c9d4b02SIngo Weinhold 	char* Chars() const
1874c9d4b02SIngo Weinhold 	{
1884c9d4b02SIngo Weinhold 		return (char*)(attributesRuns + attributesRunCount);
1894c9d4b02SIngo Weinhold 	}
1904c9d4b02SIngo Weinhold 
1914c9d4b02SIngo Weinhold 	int32 BufferSize() const
1924c9d4b02SIngo Weinhold 	{
1934c9d4b02SIngo Weinhold 		return attributesRunCount * sizeof(AttributesRun) + byteLength;
1944c9d4b02SIngo Weinhold 	}
1954c9d4b02SIngo Weinhold };
1964c9d4b02SIngo Weinhold 
1974c9d4b02SIngo Weinhold 
1984c9d4b02SIngo Weinhold #endif	// TERMINAL_LINE_H
199