The Pedigree Project 0.1
TextIO.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef TEXTIO_H
21#define TEXTIO_H
22
23#include "pedigree/kernel/Atomic.h"
24#include "pedigree/kernel/compiler.h"
25#include "pedigree/kernel/machine/InputManager.h"
26#include "pedigree/kernel/process/Mutex.h"
27#include "pedigree/kernel/process/OwnedThread.h"
28#include "pedigree/kernel/process/Semaphore.h"
29#include "pedigree/kernel/processor/MemoryRegion.h"
30#include "pedigree/kernel/processor/types.h"
31#include "pedigree/kernel/utilities/Buffer.h"
32#include "pedigree/kernel/utilities/String.h"
33
34#include <vterm.h>
35
36#include "modules/system/vfs/File.h"
37
38class Filesystem;
39class Vga;
40
41#define MAX_TEXTIO_PARAMS 16
42#define TEXTIO_BUFFER_SIZE 1024
43
44// Blink periods in milliseconds.
45// We show text for 2x as long as the text is hidden.
46#define BLINK_ON_PERIOD 1000
47#define BLINK_OFF_PERIOD (BLINK_ON_PERIOD / 2)
48
53class EXPORTED_PUBLIC TextIO : public File {
54 private:
55 static const int COLOUR_BRIGHT_ADDEND = 8;
56
57 enum VgaColour {
58 Black = 0,
59 Blue = 1,
60 Green = 2,
61 Cyan = 3,
62 Red = 4,
63 Magenta = 5,
64 Orange = 6,
65 LightGrey = 7,
66 DarkGrey = 8,
67 LightBlue = 9,
68 LightGreen = 10,
69 LightCyan = 11,
70 LightRed = 12,
71 LightMagenta = 13,
72 Yellow = 14,
73 White = 15
74 };
75
76 VgaColour adjustColour(int colour, bool up) {
77 if (up) {
78 return static_cast<VgaColour>(colour + COLOUR_BRIGHT_ADDEND);
79 } else {
80 return static_cast<VgaColour>(colour - COLOUR_BRIGHT_ADDEND);
81 }
82 }
83
85 LineFeedNewLine = 0x1,
86 CursorKey = 0x2,
87 AnsiVt52 = 0x4,
88 Column = 0x8,
89 Scrolling = 0x10,
90 Screen = 0x20,
91 Origin = 0x40,
92 AutoWrap = 0x80,
93 AutoRepeat = 0x100,
94 Interlace = 0x200,
95
97 Inverse = 0x100000,
98
100 Bright = 0x200000,
101
102 Blink = 0x400000,
103
104 // Character sets.
105 CharacterSetG0 = 0x1000000,
106 CharacterSetG1 = 0x2000000,
107 };
108
109 public:
110 enum InputMode {
111 Standard, // use built-in system keymap translation
112 Raw, // write scancodes directly
113 };
114
115 TextIO(String str, size_t inode, Filesystem* pParentFS, File* pParent);
116 virtual ~TextIO();
117
124 bool initialise(bool bClear = true);
125
131 void writeStr(const char* s, size_t len);
132
136 virtual uint64_t readBytewise(uint64_t location, uint64_t size, uintptr_t buffer,
137 bool bCanBlock = true);
138 virtual uint64_t writeBytewise(uint64_t location, uint64_t size, uintptr_t buffer,
139 bool bCanBlock = true);
140 virtual int select(bool bWriting = false, int timeout = 0);
141
142 virtual bool isSeekable() const {
143 return false;
144 }
145
146 virtual void flipThread();
147
148 static void inputCallback(InputManager::InputNotification& in);
149 void handleInput(InputManager::InputNotification& in);
150
155 void markPrimary();
156
161 void unmarkPrimary();
162
166 bool isPrimary() const;
167
171 void setMode(InputMode mode);
172
176 InputMode getMode() const;
177
178 private:
179 static const ssize_t BACKBUFFER_COLS_WIDE = 132;
180 static const ssize_t BACKBUFFER_COLS_NORMAL = 80;
181 static const ssize_t BACKBUFFER_ROWS = 25;
182
183 static const ssize_t BACKBUFFER_STRIDE = BACKBUFFER_COLS_WIDE;
184
185 virtual bool isBytewise() const {
186 return true;
187 }
188
189 void setColour(VgaColour* which, size_t param, bool bBright = false);
190
191 void doBackspace();
192 void doLinefeed();
193 void doCarriageReturn();
194 void doHorizontalTab();
195
196 void checkScroll();
197 void checkWrap();
198
199 void eraseSOS();
200 void eraseEOS();
201 void eraseEOL();
202 void eraseSOL();
203 void eraseLine();
204
205 void eraseScreen(uint8_t character);
206
207 void goHome(ssize_t xmove = 0, ssize_t ymove = 0);
208
210 void clearBackbuffer();
211
215 void flip(bool timer = false, bool hideState = false);
216
217 void initialiseVterm();
218 void destroyVterm();
219 void syncVtermRect(VTermRect rect);
220 void syncVtermCell(VTermPos pos);
221 void markVtermRect(VTermRect rect);
222 void markVtermCell(VTermPos pos);
223 void storeVtermScrollback(const VTermScreenCell* cells, int cols);
224 int popVtermScrollback(VTermScreenCell* cells, int cols);
225 VgaColour vtermColour(VTermColor colour, bool foreground) const;
226
227 static void vtermOutput(const char* bytes, size_t length, void* user);
228 static int vtermDamage(VTermRect rect, void* user);
229 static int vtermMoveRect(VTermRect dest, VTermRect src, void* user);
230 static int vtermMoveCursor(VTermPos pos, VTermPos oldPos, int visible, void* user);
231 static int vtermSetTermProp(VTermProp prop, VTermValue* value, void* user);
232 static int vtermBell(void* user);
233 static int vtermResize(int rows, int cols, void* user);
234 static int vtermScrollbackPush(int cols, const VTermScreenCell* cells, void* user);
235 static int vtermScrollbackPop(int cols, VTermScreenCell* cells, void* user);
236 static int vtermScrollbackClear(void* user);
237
239 uint8_t translate(uint32_t codepoint);
240
241 typedef struct {
242 uint8_t character;
243 VgaColour fore;
244 VgaColour back;
245 size_t flags;
246
248 bool hidden;
249 } VgaCell;
250
251 void copyVtermCell(const VTermScreenCell& source, VgaCell& destination);
252
253 static const size_t VTERM_SCROLLBACK_LINES = 64;
254
255 Atomic<bool> m_bInitialised;
256 bool m_bControlSeq;
257 bool m_bBracket;
258 bool m_bParenthesis;
259 bool m_bParams;
260 bool m_bQuestionMark;
261 ssize_t m_CursorX, m_CursorY;
262 ssize_t m_SavedCursorX, m_SavedCursorY;
263 ssize_t m_ScrollStart, m_ScrollEnd;
264 ssize_t m_LeftMargin, m_RightMargin;
265 size_t m_CurrentParam;
266 size_t m_Params[MAX_TEXTIO_PARAMS];
267 int m_CurrentModes;
268
269 VgaColour m_Fore, m_Back;
270
271 MemoryRegion m_Backbuffer;
272
273 uint16_t* m_pFramebuffer;
274 VgaCell* m_pBackbuffer;
275 Vga* m_pVga;
276
277 VTerm* m_pVterm;
278 VTermScreen* m_pVtermScreen;
279 VTermState* m_pVtermState;
280 uint8_t m_VtermDirty[BACKBUFFER_STRIDE * BACKBUFFER_ROWS];
281 VgaCell m_VtermScrollback[VTERM_SCROLLBACK_LINES][BACKBUFFER_STRIDE];
282 size_t m_VtermScrollbackLines;
283
284 char m_TabStops[BACKBUFFER_STRIDE];
285
291
296 uint8_t m_G0, m_G1;
297
300
303
305 uint32_t m_nCharacter;
306
309
315
322
327 OwnedThread m_FlipThread;
328
333
334 InputMode m_InputMode;
335};
336
337#endif
Definition File.h:75
virtual uint64_t readBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
Definition File.cc:1369
virtual uint64_t writeBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
Definition File.cc:1376
virtual int select(bool bWriting=false, int timeout=0)
Definition File.cc:1050
Special memory entity in the kernel's virtual address space.
Definition Mutex.h:56
uint8_t m_G0
Definition TextIO.h:296
bool m_bUtf8
Definition TextIO.h:302
Buffer< char > m_OutBuffer
Definition TextIO.h:290
virtual bool isBytewise() const
Definition TextIO.h:185
Mutex m_Lock
Definition TextIO.h:321
uint32_t m_nCharacter
Definition TextIO.h:305
bool m_bActive
Definition TextIO.h:314
virtual bool isSeekable() const
Definition TextIO.h:142
size_t m_nUtf8Handled
Definition TextIO.h:308
TerminalModes
Definition TextIO.h:84
bool m_bOwnsConsole
Definition TextIO.h:332
uint64_t m_NextInterval
Definition TextIO.h:299
Semaphore m_FlipWake
Definition TextIO.h:326