The Pedigree Project 0.1
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
25HostedVga::HostedVga()
26 : m_nWidth(80), m_nHeight(25), m_CursorX(0), m_CursorY(0), m_pBackbuffer(0) {}
27
28HostedVga::~HostedVga() {
29 delete[] m_pBackbuffer;
30}
31
33 return true;
34}
35
36void HostedVga::pokeBuffer(uint8_t* pBuffer, size_t nBufLen) {
37 if (!pBuffer)
38 return;
39 if (!m_pBackbuffer)
40 return;
41
42 size_t thisLen = (m_nWidth * m_nWidth * 2);
43 if (nBufLen > thisLen)
44 nBufLen = thisLen;
45
46 MemoryCopy(m_pBackbuffer, pBuffer, nBufLen);
47
48 size_t savedCursorX = m_CursorX;
49 size_t savedCursorY = m_CursorY;
50
51 moveCursor(0, 0);
52 for (size_t y = 0; y < m_nHeight; ++y) {
53 for (size_t x = 0; x < m_nWidth; ++x) {
54 size_t offset = (y * m_nWidth) + x;
55 char c = static_cast<char>(m_pBackbuffer[offset] & 0xFF);
56 uint8_t attr = (m_pBackbuffer[offset] >> 8) & 0xFF;
58 putchar(c);
59 }
60
61 putchar('\n');
62 }
63
64 moveCursor(savedCursorX, savedCursorY);
65}
66
67void HostedVga::peekBuffer(uint8_t* pBuffer, size_t nBufLen) {
68 if (!m_pBackbuffer)
69 return;
70
71 size_t thisLen = (m_nWidth * m_nWidth * 2);
72 if (nBufLen > thisLen)
73 nBufLen = thisLen;
74 MemoryCopy(pBuffer, m_pBackbuffer, nBufLen);
75}
76
77void HostedVga::moveCursor(size_t nX, size_t nY) {
78 if (!m_pBackbuffer)
79 return;
80
81 m_CursorX = nX;
82 m_CursorY = nY;
83 printf("\033[%zu;%zuH", nY, nX);
84}
85
86bool HostedVga::initialise() {
87 if (m_pBackbuffer) {
88 // Already initialised.
89 return true;
90 }
91
92 m_pBackbuffer = new uint16_t[m_nHeight * m_nWidth];
93
94 return true;
95}
96
97uint8_t HostedVga::ansiColourFixup(uint8_t colour) {
98 switch (colour) {
99 case BootIO::Black:
100 case BootIO::DarkGrey:
101 return 0;
102 case BootIO::Blue:
103 case BootIO::LightBlue:
104 return 4;
105 case BootIO::Green:
106 case BootIO::LightGreen:
107 return 2;
108 case BootIO::Cyan:
109 case BootIO::LightCyan:
110 return 6;
111 case BootIO::Red:
112 case BootIO::LightRed:
113 return 1;
114 case BootIO::Magenta:
115 case BootIO::LightMagenta:
116 return 5;
117 case BootIO::Orange:
118 case BootIO::Yellow:
119 return 3;
120 case BootIO::LightGrey:
121 case BootIO::White:
122 return 7;
123 default:
124 // Default colour.
125 return 9;
126 }
127}
128
129void HostedVga::printAttrAsAnsi(uint8_t attr) {
130 uint8_t fore = ansiColourFixup(attr & 0xF);
131 uint8_t back = ansiColourFixup((attr >> 4) & 0xF);
132
133 uint32_t fore_param = fore > 7 ? 90 + fore : 30 + fore;
134 uint32_t back_param = back > 7 ? 100 + back : 40 + back;
135 printf("\033[%d;%dm", fore_param, back_param);
136}
static void printAttrAsAnsi(uint8_t attr)
static uint8_t ansiColourFixup(uint8_t colour)
Definition hosted/Vga.cc:97
virtual void moveCursor(size_t nX, size_t nY)
Definition hosted/Vga.cc:77
virtual void pokeBuffer(uint8_t *pBuffer, size_t nBufLen)
Definition hosted/Vga.cc:36
virtual bool setLargestTextMode()
Definition hosted/Vga.cc:32
virtual void peekBuffer(uint8_t *pBuffer, size_t nBufLen)
Definition hosted/Vga.cc:67