The Pedigree Project  0.1
kernel/machine/hosted/Vga.cc
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 #include "Vga.h"
21 #include "pedigree/kernel/core/BootIO.h"
22 
23 #include <stdio.h>
24 
25 HostedVga::HostedVga()
26  : m_nWidth(80), m_nHeight(25), m_CursorX(0), m_CursorY(0), m_ModeStack(0),
27  m_nMode(3), m_nControls(0), m_pBackbuffer(0)
28 {
29 }
30 
31 HostedVga::~HostedVga()
32 {
33  delete[] m_pBackbuffer;
34 }
35 
36 void HostedVga::setControl(Vga::VgaControl which)
37 {
38  m_nControls |= 1 << static_cast<uint8_t>(which);
39 }
40 
41 void HostedVga::clearControl(Vga::VgaControl which)
42 {
43  m_nControls &= ~(1 << static_cast<uint8_t>(which));
44 }
45 
46 bool HostedVga::setMode(int mode)
47 {
48  m_nMode = mode;
49  return true;
50 }
51 
53 {
54  return true;
55 }
56 
57 bool HostedVga::isMode(size_t nCols, size_t nRows, bool bIsText, size_t nBpp)
58 {
59  if (bIsText)
60  return true;
61  return false;
62 }
63 
65 {
66  return true;
67 }
68 
70 {
71 }
72 
74 {
75 }
76 
77 void HostedVga::pokeBuffer(uint8_t *pBuffer, size_t nBufLen)
78 {
79  if (!pBuffer)
80  return;
81  if (!m_pBackbuffer)
82  return;
83 
84  size_t thisLen = (m_nWidth * m_nWidth * 2);
85  if (nBufLen > thisLen)
86  nBufLen = thisLen;
87 
88  MemoryCopy(m_pBackbuffer, pBuffer, nBufLen);
89 
90  size_t savedCursorX = m_CursorX;
91  size_t savedCursorY = m_CursorY;
92 
93  moveCursor(0, 0);
94  for (size_t y = 0; y < m_nHeight; ++y)
95  {
96  for (size_t x = 0; x < m_nWidth; ++x)
97  {
98  size_t offset = (y * m_nWidth) + x;
99  char c = static_cast<char>(m_pBackbuffer[offset] & 0xFF);
100  uint8_t attr = (m_pBackbuffer[offset] >> 8) & 0xFF;
102  putchar(c);
103  }
104 
105  putchar('\n');
106  }
107 
108  moveCursor(savedCursorX, savedCursorY);
109 }
110 
111 void HostedVga::peekBuffer(uint8_t *pBuffer, size_t nBufLen)
112 {
113  if (!m_pBackbuffer)
114  return;
115 
116  size_t thisLen = (m_nWidth * m_nWidth * 2);
117  if (nBufLen > thisLen)
118  nBufLen = thisLen;
119  MemoryCopy(pBuffer, m_pBackbuffer, nBufLen);
120 }
121 
122 void HostedVga::moveCursor(size_t nX, size_t nY)
123 {
124  if (!m_pBackbuffer)
125  return;
126 
127  m_CursorX = nX;
128  m_CursorY = nY;
129  printf("\033[%zu;%zuH", nY, nX);
130 }
131 
132 bool HostedVga::initialise()
133 {
134  if (m_pBackbuffer)
135  {
136  // Already initialised.
137  return true;
138  }
139 
140  m_pBackbuffer = new uint16_t[m_nHeight * m_nWidth];
141  m_nControls = 0;
142 
143  return true;
144 }
145 
146 uint8_t HostedVga::ansiColourFixup(uint8_t colour)
147 {
148  switch (colour)
149  {
150  case BootIO::Black:
151  case BootIO::DarkGrey:
152  return 0;
153  case BootIO::Blue:
154  case BootIO::LightBlue:
155  return 4;
156  case BootIO::Green:
157  case BootIO::LightGreen:
158  return 2;
159  case BootIO::Cyan:
160  case BootIO::LightCyan:
161  return 6;
162  case BootIO::Red:
163  case BootIO::LightRed:
164  return 1;
165  case BootIO::Magenta:
166  case BootIO::LightMagenta:
167  return 5;
168  case BootIO::Orange:
169  case BootIO::Yellow:
170  return 3;
171  case BootIO::LightGrey:
172  case BootIO::White:
173  return 7;
174  default:
175  // Default colour.
176  return 9;
177  }
178 }
179 
180 void HostedVga::printAttrAsAnsi(uint8_t attr)
181 {
182  uint8_t fore = ansiColourFixup(attr & 0xF);
183  uint8_t back = ansiColourFixup((attr >> 4) & 0xF);
184 
185  uint32_t fore_param = fore > 7 ? 90 + fore : 30 + fore;
186  uint32_t back_param = back > 7 ? 100 + back : 40 + back;
187  printf("\033[%d;%dm", fore_param, back_param);
188 }
virtual void setControl(VgaControl which)
virtual void clearControl(VgaControl which)
void moveCursor(size_t nX, size_t nY)
virtual bool isMode(size_t nCols, size_t nRows, bool bIsText, size_t nBpp=0)
virtual void peekBuffer(uint8_t *pBuffer, size_t nBufLen)
virtual void rememberMode()
static uint8_t ansiColourFixup(uint8_t colour)
virtual void moveCursor(size_t nX, size_t nY)
virtual void pokeBuffer(uint8_t *pBuffer, size_t nBufLen)
virtual bool setMode(int mode)
virtual bool setLargestTextMode()
virtual bool isLargestTextMode()
virtual void restoreMode()
static void printAttrAsAnsi(uint8_t attr)