The Pedigree Project  0.1
Xterm.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 XTERM_H
21 #define XTERM_H
22 
23 #include "environment.h"
24 #include "tui.h"
25 
26 #include <string>
27 
28 #include "pedigree/native/graphics/Graphics.h"
29 
30 #include <cairo/cairo.h>
31 
32 #define XTERM_MAX_PARAMS 16
33 
34 // We must always support at least 132 columns. Normally, the margins would be
35 // set to whatever the screen supports, but for DECCOLM support we will need to
36 // be able to expand to 132 columns and only render the visible portion.
37 #define XTERM_WIDE 132
38 #define XTERM_STANDARD 80
39 #define XTERM_MIN_WIDTH 132
40 
41 // #define XTERM_DEBUG
42 // #define XTERM_DEBUG_EXTRA
43 
44 class Xterm
45 {
46  public:
47  Xterm(
48  PedigreeGraphics::Framebuffer *pFramebuffer, size_t nWidth,
49  size_t nHeight, size_t offsetLeft, size_t offsetTop, class Terminal *pT,
50  class Widget *pWidget, Tui *pTui, class Font *pNormalFont,
51  class Font *pBoldFont);
52  ~Xterm();
53 
55  void write(uint32_t utf32, DirtyRectangle &rect);
56 
58  void renderAll(DirtyRectangle &rect);
59 
60  size_t getRows() const
61  {
62  return m_pWindows[0]->m_Height;
63  }
64  size_t getCols() const
65  {
66  return m_pWindows[0]->m_Width;
67  }
68  size_t getStride() const
69  {
70  return m_pWindows[0]->m_Stride;
71  }
72 
73  void showCursor(DirtyRectangle &rect)
74  {
75  m_pWindows[m_ActiveBuffer]->showCursor(rect);
76  }
77  void hideCursor(DirtyRectangle &rect)
78  {
79  m_pWindows[m_ActiveBuffer]->hideCursor(rect);
80  }
81 
82  size_t getModes() const
83  {
84  return m_Modes;
85  }
86 
87  void resize(size_t w, size_t h, PedigreeGraphics::Framebuffer *pFb)
88  {
89  m_pWindows[(m_ActiveBuffer + 1) % 2]->resize(w, h, false);
90  m_pWindows[m_ActiveBuffer]->resize(w, h, true);
91  }
92 
93  void setCursorStyle(bool bFilled)
94  {
95  m_pWindows[m_ActiveBuffer]->setCursorStyle(bFilled);
96  }
97 
100  void processKey(uint64_t key);
101 
102  void setCairo(cairo_t *pCairo, cairo_surface_t *pSurface)
103  {
104  m_pCairo = pCairo;
105  m_pCairoSurface = pSurface;
106  }
107 
108  void setFonts(Font *pNormalFont, Font *pBoldFont)
109  {
110  m_pNormalFont = pNormalFont;
111  m_pBoldFont = pBoldFont;
112  }
113 
114  private:
115  class Window
116  {
117  friend class Xterm;
118 
119  private:
120  class TermChar
121  {
122  public:
123  bool operator==(const TermChar &other) const
124  {
125  return (
126  flags == other.flags && fore == other.fore &&
127  back == other.back && utf32 == other.utf32);
128  }
129  bool operator!=(const TermChar &other) const
130  {
131  return !(
132  flags == other.flags && fore == other.fore &&
133  back == other.back && utf32 == other.utf32);
134  }
135  uint8_t flags;
136  uint8_t fore, back;
137  uint32_t utf32;
138  };
139 
140  public:
141  Window(
142  size_t nRows, size_t nCols, PedigreeGraphics::Framebuffer *pFb,
143  size_t nMaxScrollback, size_t offsetLeft, size_t offsetTop,
144  size_t fbWidth, Xterm *parent);
145  ~Window();
146 
147  void showCursor(DirtyRectangle &rect);
148  void hideCursor(DirtyRectangle &rect);
149 
150  void resize(size_t nRows, size_t nCols, bool bActive);
151 
152  void setScrollRegion(int start, int end);
153  void setForeColour(uint8_t fgColour);
154  void setBackColour(uint8_t bgColour);
155  void setFlags(uint8_t flags);
156  uint8_t getFlags();
157 
158  void setMargins(size_t left, size_t right);
159 
160  void setChar(uint32_t utf32, size_t x, size_t y);
161  TermChar getChar(size_t x = ~0UL, size_t y = ~0UL);
162 
163  void fillChar(uint32_t utf32, DirtyRectangle &rect);
164  void addChar(uint32_t utf32, DirtyRectangle &rect);
165 
166  void setCursorRelOrigin(size_t x, size_t y, DirtyRectangle &rect);
167  void setCursor(size_t x, size_t y, DirtyRectangle &rect);
168  void setCursorX(size_t x, DirtyRectangle &rect);
169  void setCursorY(size_t y, DirtyRectangle &rect);
170  ssize_t getCursorX() const;
171  ssize_t getCursorY() const;
174  ssize_t getCursorXRelOrigin() const;
175  ssize_t getCursorYRelOrigin() const;
176  void setCursorStyle(bool bFilled)
177  {
178  m_bCursorFilled = bFilled;
179  }
180 
181  void cursorToOrigin();
182 
183  void cursorLeft(DirtyRectangle &rect);
184  void cursorLeftNum(size_t n, DirtyRectangle &rect);
185  void cursorLeftToMargin(DirtyRectangle &rect);
186  void cursorDown(size_t n, DirtyRectangle &);
187  void cursorUp(size_t n, DirtyRectangle &);
188 
189  void backspace(DirtyRectangle &rect);
190 
191  void cursorUpWithinMargin(size_t n, DirtyRectangle &);
192  void cursorLeftWithinMargin(size_t n, DirtyRectangle &);
193  void cursorRightWithinMargin(size_t n, DirtyRectangle &);
194  void cursorDownWithinMargin(size_t n, DirtyRectangle &);
195 
196  void cursorDownAndLeftToMargin(DirtyRectangle &rect);
197  void cursorDownAndLeft(DirtyRectangle &rect);
198  void cursorTab(DirtyRectangle &rect);
199  void cursorTabBack(DirtyRectangle &rect);
200 
201  void renderAll(DirtyRectangle &rect, Window *pPrevious);
202  void render(
203  DirtyRectangle &rect, size_t flags = 0, size_t x = ~0UL,
204  size_t y = ~0UL);
205  void renderArea(
206  DirtyRectangle &rect, size_t x = ~0UL, size_t y = ~0UL,
207  size_t w = ~0UL, size_t h = ~0UL);
208 
209  void scrollRegionUp(size_t n, DirtyRectangle &rect);
210  void scrollRegionDown(size_t n, DirtyRectangle &rect);
211 
212  void scrollUp(size_t n, DirtyRectangle &rect);
213  void scrollDown(size_t n, DirtyRectangle &rect);
214 
216  void eraseEOL(DirtyRectangle &rect);
218  void eraseSOL(DirtyRectangle &rect);
220  void eraseLine(DirtyRectangle &rect);
222  void eraseChars(size_t n, DirtyRectangle &rect);
224  void eraseUp(DirtyRectangle &rect);
226  void eraseDown(DirtyRectangle &rect);
228  void eraseScreen(DirtyRectangle &rect);
229 
231  void deleteCharacters(size_t n, DirtyRectangle &rect);
233  void insertCharacters(size_t n, DirtyRectangle &rect);
234 
236  void insertLines(size_t n, DirtyRectangle &rect);
238  void deleteLines(size_t n, DirtyRectangle &rect);
239 
241  void setTabStop();
243  void clearTabStop();
245  void clearAllTabStops();
246 
248  void checkWrap(DirtyRectangle &rect);
249 
251  void checkScroll(DirtyRectangle &rect);
252 
253  /* Inverts the display, if the character has the default colours. */
254  void invert(DirtyRectangle &rect);
255 
256  void setLineRenderMode(bool b)
257  {
258  m_bLineRender = b;
259  }
260 
261  bool getLineRenderMode()
262  {
263  return m_bLineRender;
264  }
265 
266  void lineRender(uint32_t utf32, DirtyRectangle &rect);
267 
268  private:
269  Window(const Window &);
270  Window &operator=(const Window &);
271 
272  TermChar *m_pBuffer;
273  size_t m_BufferLength;
274 
275  PedigreeGraphics::Framebuffer *m_pFramebuffer;
276 
277  size_t m_FbWidth;
278  size_t m_Width, m_Height, m_Stride;
279 
280  size_t m_OffsetLeft, m_OffsetTop;
281 
282  size_t m_nMaxScrollback;
283  ssize_t m_CursorX, m_CursorY;
284 
285  ssize_t m_ScrollStart, m_ScrollEnd;
286  ssize_t m_LeftMargin, m_RightMargin;
287 
288  TermChar *m_pInsert;
289  TermChar *m_pView;
290 
291  uint8_t m_Fg, m_Bg;
292  uint8_t m_Flags;
293 
294  bool m_bCursorFilled;
295 
296  bool m_bLineRender;
297 
298  Xterm *m_pParentXterm;
299  };
300 
301  Xterm(const Xterm &);
302  Xterm &operator=(const Xterm &);
303 
306 
307  Window *m_pWindows[2];
308 
310  typedef struct
311  {
312  int params[XTERM_MAX_PARAMS];
313  int cur_param;
314  bool has_param;
315  } XtermCmd;
316  XtermCmd m_Cmd;
317 
318  typedef struct
319  {
320  std::string params[XTERM_MAX_PARAMS];
321  int cur_param;
322  bool has_param;
323  } XtermOsControl;
324  XtermOsControl m_OsCtl;
325 
328 
332  bool m_bContainedParen;
333  bool m_bIsOsControl;
334 
335  enum TerminalModes
336  {
337  LineFeedNewLine = 0x1,
338  CursorKey = 0x2,
339  AnsiVt52 = 0x4, // ANSI vs VT52 mode toggle.
340  Column = 0x8,
341  Scrolling = 0x10,
342  Screen = 0x20,
343  Origin = 0x40,
344  AutoWrap = 0x80,
345  AutoRepeat = 0x100,
346  Interlace = 0x200,
347  AppKeypad = 0x400,
348  Margin = 0x800,
349  Insert = 0x1000,
350  };
351 
353  {
355  Question = 0x1,
356 
358  LeftAngle = 0x2,
359 
361  RightAngle = 0x4,
362 
364  LeftSquare = 0x8,
365 
367  RightSquare = 0x10,
368 
370  LeftRound = 0x20,
371 
373  RightRound = 0x40,
374 
376  Dollar = 0x80,
377 
379  Apostrophe = 0x100,
380 
382  Quote = 0x200,
383 
385  Space = 0x400,
386 
388  Bang = 0x800,
389 
391  Escape = 0x1000,
392 
394  Percent = 0x2000,
395 
397  Asterisk = 0x4000,
398 
400  Plus = 0x8000,
401 
403  Minus = 0x10000,
404 
406  Period = 0x20000,
407 
409  Slash = 0x40000,
410 
412  Hash = 0x80000,
413 
415  Backslash = 0x100000,
416 
418  Underscore = 0x200000,
419 
421  Vt52SetCursorWaitY = 0x400000,
422 
424  Vt52SetCursorWaitX = 0x800000,
425  };
426 
427  bool setFlagsForUtf32(uint32_t utf32);
428 
430  size_t m_Flags;
431 
433  size_t m_Modes;
434 
436  char *m_TabStops;
437 
439  uint32_t m_SavedX, m_SavedY;
440 
441  class Terminal *m_pT;
442 
443  bool m_bFbMode;
444 
445  class Widget *m_pWidget;
446 
447  Tui *m_pTui;
448 
449  cairo_t *m_pCairo;
450  cairo_surface_t *m_pCairoSurface;
451 
452  Font *m_pNormalFont;
453  Font *m_pBoldFont;
454 };
455 
456 #endif
Definition: tui.h:36
void processKey(uint64_t key)
Definition: Xterm.cc:152
void write(uint32_t utf32, DirtyRectangle &rect)
Definition: Xterm.cc:354
Seen a &#39;[&#39; character.
Definition: Xterm.h:364
void eraseLine(DirtyRectangle &rect)
Definition: Xterm.cc:2590
ssize_t getCursorXRelOrigin() const
Definition: Xterm.cc:2258
void renderAll(DirtyRectangle &rect)
Definition: Xterm.cc:1554
Seen a &#39;+&#39; character.
Definition: Xterm.h:400
Definition: Font.h:33
uint32_t m_SavedX
Saved cursor position.
Definition: Xterm.h:439
void insertLines(size_t n, DirtyRectangle &rect)
Definition: Xterm.cc:2935
void eraseScreen(DirtyRectangle &rect)
Definition: Xterm.cc:2442
Seen a &#39; &#39; character.
Definition: Xterm.h:385
Seen a &#39;/&#39; character.
Definition: Xterm.h:409
bool m_bContainedBracket
Definition: Xterm.h:331
char * m_TabStops
Tab stops.
Definition: Xterm.h:436
void eraseDown(DirtyRectangle &rect)
Definition: Xterm.cc:2743
Seen a &#39;?&#39; character.
Definition: Xterm.h:355
void clearAllTabStops()
Definition: Xterm.cc:3329
size_t m_ActiveBuffer
Definition: Xterm.h:305
void setTabStop()
Definition: Xterm.cc:3311
Seen a &#39;.&#39; character.
Definition: Xterm.h:406
Seen a &#39;&#39; (ESC) character.
Definition: Xterm.h:391
void insertCharacters(size_t n, DirtyRectangle &rect)
Definition: Xterm.cc:2866
Seen a &#39;$&#39; character.
Definition: Xterm.h:376
void eraseChars(size_t n, DirtyRectangle &rect)
Definition: Xterm.cc:2635
Definition: Xterm.h:44
void eraseUp(DirtyRectangle &rect)
Definition: Xterm.cc:2691
TerminalFlags
Definition: Xterm.h:352
void eraseEOL(DirtyRectangle &rect)
Definition: Xterm.cc:2490
Seen a &#39;-&#39; character.
Definition: Xterm.h:403
Seen a &#39;>&#39; character.
Definition: Xterm.h:361
size_t m_Modes
Currently active modes.
Definition: Xterm.h:433
Seen a &#39;)&#39; character.
Definition: Xterm.h:373
Definition: Widget.h:63
size_t m_Flags
Flags for this particular sequence.
Definition: Xterm.h:430
VT52: set cursor, waiting for Y.
Definition: Xterm.h:421
Seen a &#39;(&#39; character.
Definition: Xterm.h:370
Seen a &#39;"&#39; character.
Definition: Xterm.h:382
Seen a &#39;!&#39; character.
Definition: Xterm.h:388
Seen a &#39;&#39; character.
Definition: Xterm.h:394
bool m_bChangingState
Are we currently interpreting a state change?
Definition: Xterm.h:327
Set of parameters for the XTerm commands.
Definition: Xterm.h:310
void deleteLines(size_t n, DirtyRectangle &rect)
Definition: Xterm.cc:2950
void eraseSOL(DirtyRectangle &rect)
Definition: Xterm.cc:2543
void checkScroll(DirtyRectangle &rect)
Definition: Xterm.cc:3266
Seen a &#39;\&#39;&#39; character.
Definition: Xterm.h:379
void deleteCharacters(size_t n, DirtyRectangle &rect)
Definition: Xterm.cc:2800
void checkWrap(DirtyRectangle &rect)
Definition: Xterm.cc:3246
Seen a &#39;\&#39; character.
Definition: Xterm.h:415
Seen a &#39;_&#39; character.
Definition: Xterm.h:418
Seen a &#39;*&#39; character.
Definition: Xterm.h:397
void clearTabStop()
Definition: Xterm.cc:3320
Seen a &#39;]&#39; character.
Definition: Xterm.h:367
Seen a &#39;<&#39; character.
Definition: Xterm.h:358
VT52: set cursor, waiting for X.
Definition: Xterm.h:424