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 "modules/system/vfs/File.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/processor/MemoryRegion.h"
28 #include "pedigree/kernel/processor/types.h"
29 #include "pedigree/kernel/utilities/Buffer.h"
30 #include "pedigree/kernel/utilities/String.h"
31 
32 class Filesystem;
33 class Thread;
34 class Vga;
35 
36 #define MAX_TEXTIO_PARAMS 16
37 #define TEXTIO_BUFFER_SIZE 1024
38 
39 // Blink periods in milliseconds.
40 // We show text for 2x as long as the text is hidden.
41 #define BLINK_ON_PERIOD 1000
42 #define BLINK_OFF_PERIOD (BLINK_ON_PERIOD / 2)
43 
48 class EXPORTED_PUBLIC TextIO : public File
49 {
50  private:
51  static const int COLOUR_BRIGHT_ADDEND = 8;
52 
53  enum VgaColour
54  {
55  Black = 0,
56  Blue = 1,
57  Green = 2,
58  Cyan = 3,
59  Red = 4,
60  Magenta = 5,
61  Orange = 6,
62  LightGrey = 7,
63  DarkGrey = 8,
64  LightBlue = 9,
65  LightGreen = 10,
66  LightCyan = 11,
67  LightRed = 12,
68  LightMagenta = 13,
69  Yellow = 14,
70  White = 15
71  };
72 
73  VgaColour adjustColour(int colour, bool up)
74  {
75  if (up)
76  {
77  return static_cast<VgaColour>(colour + COLOUR_BRIGHT_ADDEND);
78  }
79  else
80  {
81  return static_cast<VgaColour>(colour - COLOUR_BRIGHT_ADDEND);
82  }
83  }
84 
86  {
87  LineFeedNewLine = 0x1,
88  CursorKey = 0x2,
89  AnsiVt52 = 0x4,
90  Column = 0x8,
91  Scrolling = 0x10,
92  Screen = 0x20,
93  Origin = 0x40,
94  AutoWrap = 0x80,
95  AutoRepeat = 0x100,
96  Interlace = 0x200,
97 
99  Inverse = 0x100000,
100 
102  Bright = 0x200000,
103 
104  Blink = 0x400000,
105 
106  // Character sets.
107  CharacterSetG0 = 0x1000000,
108  CharacterSetG1 = 0x2000000,
109  };
110 
111  public:
112  enum InputMode
113  {
114  Standard, // use built-in system keymap translation
115  Raw, // write scancodes directly
116  };
117 
118  TextIO(String str, size_t inode, Filesystem *pParentFS, File *pParent);
119  virtual ~TextIO();
120 
127  bool initialise(bool bClear = true);
128 
134  void writeStr(const char *s, size_t len);
135 
139  virtual uint64_t readBytewise(
140  uint64_t location, uint64_t size, uintptr_t buffer,
141  bool bCanBlock = true);
142  virtual uint64_t writeBytewise(
143  uint64_t location, uint64_t size, uintptr_t buffer,
144  bool bCanBlock = true);
145  virtual int select(bool bWriting = false, int timeout = 0);
146 
147  virtual void flipThread();
148 
149  static void inputCallback(InputManager::InputNotification &in);
150  void handleInput(InputManager::InputNotification &in);
151 
156  void markPrimary();
157 
162  void unmarkPrimary();
163 
167  bool isPrimary() const;
168 
172  void setMode(InputMode mode);
173 
177  InputMode getMode() const;
178 
179  private:
180  static const ssize_t BACKBUFFER_COLS_WIDE = 132;
181  static const ssize_t BACKBUFFER_COLS_NORMAL = 80;
182  static const ssize_t BACKBUFFER_ROWS = 25;
183 
184  static const ssize_t BACKBUFFER_STRIDE = BACKBUFFER_COLS_WIDE;
185 
186  virtual bool isBytewise() const
187  {
188  return true;
189  }
190 
191  void setColour(VgaColour *which, size_t param, bool bBright = false);
192 
193  void doBackspace();
194  void doLinefeed();
195  void doCarriageReturn();
196  void doHorizontalTab();
197 
198  void checkScroll();
199  void checkWrap();
200 
201  void eraseSOS();
202  void eraseEOS();
203  void eraseEOL();
204  void eraseSOL();
205  void eraseLine();
206 
207  void eraseScreen(uint8_t character);
208 
209  void goHome(ssize_t xmove = 0, ssize_t ymove = 0);
210 
212  void clearBackbuffer();
213 
217  void flip(bool timer = false, bool hideState = false);
218 
220  uint8_t translate(uint32_t codepoint);
221 
222  typedef struct
223  {
224  uint8_t character;
225  VgaColour fore;
226  VgaColour back;
227  size_t flags;
228 
230  bool hidden;
231  } VgaCell;
232 
233  bool m_bInitialised;
234  bool m_bControlSeq;
235  bool m_bBracket;
236  bool m_bParenthesis;
237  bool m_bParams;
238  bool m_bQuestionMark;
239  ssize_t m_CursorX, m_CursorY;
240  ssize_t m_SavedCursorX, m_SavedCursorY;
241  ssize_t m_ScrollStart, m_ScrollEnd;
242  ssize_t m_LeftMargin, m_RightMargin;
243  size_t m_CurrentParam;
244  size_t m_Params[MAX_TEXTIO_PARAMS];
245  int m_CurrentModes;
246 
247  VgaColour m_Fore, m_Back;
248 
249  MemoryRegion m_Backbuffer;
250 
251  uint16_t *m_pFramebuffer;
252  VgaCell *m_pBackbuffer;
253  Vga *m_pVga;
254 
255  char m_TabStops[BACKBUFFER_STRIDE];
256 
262 
267  uint8_t m_G0, m_G1;
268 
270  uint64_t m_NextInterval;
271 
273  bool m_bUtf8;
274 
276  uint32_t m_nCharacter;
277 
280 
285  bool m_bActive;
286 
293 
298 
303 
304  InputMode m_InputMode;
305 };
306 
307 #endif
virtual bool isBytewise() const
Definition: TextIO.h:186
Thread * m_pFlipThread
Definition: TextIO.h:297
virtual int select(bool bWriting=false, int timeout=0)
Definition: File.cc:534
uint8_t m_G0
Definition: TextIO.h:267
uint64_t m_NextInterval
Definition: TextIO.h:270
virtual uint64_t readBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
Definition: File.cc:582
Definition: Mutex.h:58
Definition: String.h:49
Definition: TextIO.h:48
Special memory entity in the kernel&#39;s virtual address space.
Definition: MemoryRegion.h:35
Buffer< char > m_OutBuffer
Definition: TextIO.h:261
TerminalModes
Definition: TextIO.h:85
bool m_bUtf8
Definition: TextIO.h:273
Mutex m_Lock
Definition: TextIO.h:292
Definition: Thread.h:54
bool m_bOwnsConsole
Definition: TextIO.h:302
size_t m_nUtf8Handled
Definition: TextIO.h:279
Definition: File.h:66
bool m_bActive
Definition: TextIO.h:285
virtual uint64_t writeBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
Definition: File.cc:592
uint32_t m_nCharacter
Definition: TextIO.h:276