added clang format

This commit is contained in:
Morten Delenk 2017-04-26 10:27:27 +00:00
parent ba8f73277c
commit dfd6e90200
13 changed files with 366 additions and 390 deletions

22
.clang-format Normal file
View file

@ -0,0 +1,22 @@
---
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
DerivePointerAlignment: false
PointerAlignment: Right
AccessModifierOffset: 0
AlignAfterOpenBracket: true
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakTemplateDeclarations: true
ColumnLimit: 120
NamespaceIndentation: Inner
Standard: Cpp11
UseTab: Never

View file

@ -14,9 +14,11 @@
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY
* DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/ */
#ifndef MULTIBOOT_HEADER #ifndef MULTIBOOT_HEADER
@ -96,8 +98,7 @@ typedef unsigned short multiboot_uint16_t;
typedef unsigned int multiboot_uint32_t; typedef unsigned int multiboot_uint32_t;
typedef unsigned long long multiboot_uint64_t; typedef unsigned long long multiboot_uint64_t;
struct multiboot_header struct multiboot_header {
{
/* Must be MULTIBOOT_MAGIC - see above. */ /* Must be MULTIBOOT_MAGIC - see above. */
multiboot_uint32_t magic; multiboot_uint32_t magic;
@ -122,8 +123,7 @@ struct multiboot_header
}; };
/* The symbol table for a.out. */ /* The symbol table for a.out. */
struct multiboot_aout_symbol_table struct multiboot_aout_symbol_table {
{
multiboot_uint32_t tabsize; multiboot_uint32_t tabsize;
multiboot_uint32_t strsize; multiboot_uint32_t strsize;
multiboot_uint32_t addr; multiboot_uint32_t addr;
@ -132,8 +132,7 @@ struct multiboot_aout_symbol_table
typedef struct multiboot_aout_symbol_table multiboot_aout_symbol_table_t; typedef struct multiboot_aout_symbol_table multiboot_aout_symbol_table_t;
/* The section header table for ELF. */ /* The section header table for ELF. */
struct multiboot_elf_section_header_table struct multiboot_elf_section_header_table {
{
multiboot_uint32_t num; multiboot_uint32_t num;
multiboot_uint32_t size; multiboot_uint32_t size;
multiboot_uint32_t addr; multiboot_uint32_t addr;
@ -141,8 +140,7 @@ struct multiboot_elf_section_header_table
}; };
typedef struct multiboot_elf_section_header_table multiboot_elf_section_header_table_t; typedef struct multiboot_elf_section_header_table multiboot_elf_section_header_table_t;
struct multiboot_info struct multiboot_info {
{
/* Multiboot info version number */ /* Multiboot info version number */
multiboot_uint32_t flags; multiboot_uint32_t flags;
@ -160,8 +158,7 @@ struct multiboot_info
multiboot_uint32_t mods_count; multiboot_uint32_t mods_count;
multiboot_uint32_t mods_addr; multiboot_uint32_t mods_addr;
union union {
{
multiboot_aout_symbol_table_t aout_sym; multiboot_aout_symbol_table_t aout_sym;
multiboot_elf_section_header_table_t elf_sec; multiboot_elf_section_header_table_t elf_sec;
} u; } u;
@ -200,15 +197,12 @@ struct multiboot_info
#define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1 #define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1
#define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2 #define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2
multiboot_uint8_t framebuffer_type; multiboot_uint8_t framebuffer_type;
union union {
{ struct {
struct
{
multiboot_uint32_t framebuffer_palette_addr; multiboot_uint32_t framebuffer_palette_addr;
multiboot_uint16_t framebuffer_palette_num_colors; multiboot_uint16_t framebuffer_palette_num_colors;
}; };
struct struct {
{
multiboot_uint8_t framebuffer_red_field_position; multiboot_uint8_t framebuffer_red_field_position;
multiboot_uint8_t framebuffer_red_mask_size; multiboot_uint8_t framebuffer_red_mask_size;
multiboot_uint8_t framebuffer_green_field_position; multiboot_uint8_t framebuffer_green_field_position;
@ -220,15 +214,13 @@ struct multiboot_info
}; };
typedef struct multiboot_info multiboot_info_t; typedef struct multiboot_info multiboot_info_t;
struct multiboot_color struct multiboot_color {
{
multiboot_uint8_t red; multiboot_uint8_t red;
multiboot_uint8_t green; multiboot_uint8_t green;
multiboot_uint8_t blue; multiboot_uint8_t blue;
}; };
struct multiboot_mmap_entry struct multiboot_mmap_entry {
{
multiboot_uint32_t size; multiboot_uint32_t size;
multiboot_uint64_t addr; multiboot_uint64_t addr;
multiboot_uint64_t len; multiboot_uint64_t len;
@ -241,8 +233,7 @@ struct multiboot_mmap_entry
} GRUB_PACKED; } GRUB_PACKED;
typedef struct multiboot_mmap_entry multiboot_memory_map_t; typedef struct multiboot_mmap_entry multiboot_memory_map_t;
struct multiboot_mod_list struct multiboot_mod_list {
{
/* the memory used goes from bytes 'mod_start' to 'mod_end-1' inclusive */ /* the memory used goes from bytes 'mod_start' to 'mod_end-1' inclusive */
multiboot_uint32_t mod_start; multiboot_uint32_t mod_start;
multiboot_uint32_t mod_end; multiboot_uint32_t mod_end;
@ -256,8 +247,7 @@ struct multiboot_mod_list
typedef struct multiboot_mod_list multiboot_module_t; typedef struct multiboot_mod_list multiboot_module_t;
/* APM BIOS info. */ /* APM BIOS info. */
struct multiboot_apm_info struct multiboot_apm_info {
{
grub_uint16_t version; grub_uint16_t version;
grub_uint16_t cseg; grub_uint16_t cseg;
grub_uint32_t offset; grub_uint32_t offset;

View file

@ -1,10 +1,8 @@
#include <base.hpp>
#include "../../../hw/pc/cgaterm/cgaterm.hpp" #include "../../../hw/pc/cgaterm/cgaterm.hpp"
#include <base.hpp>
CGATerm term; CGATerm term;
void main(); void main();
extern "C" void start() { extern "C" void start() { main(); }
main();
}
void drivers_init() { void drivers_init() {
setMainTTY(&term); setMainTTY(&term);
--term; --term;

View file

@ -1,69 +1,36 @@
#include "cgaterm.hpp" #include "cgaterm.hpp"
#include "cp437.hpp" #include "cp437.hpp"
CGATerm::CGATerm(): TTY(80, 25) {} CGATerm::CGATerm() : TTY(80, 25) {}
CGATerm::~CGATerm() {} CGATerm::~CGATerm() {}
auto CGATerm::rgbSupport() -> bool {return false;} auto CGATerm::rgbSupport() -> bool { return false; }
struct ScreenChar { struct ScreenChar {
unsigned char ch; unsigned char ch;
char fgcolor:4; char fgcolor : 4;
char bgcolor:4; char bgcolor : 4;
}__attribute__((packed)); } __attribute__((packed));
ScreenChar *scr = (ScreenChar*)0xB8000; ScreenChar *scr = (ScreenChar *)0xB8000;
auto CGATerm::plotChar(int x, int y, int c) -> void { auto CGATerm::plotChar(int x, int y, int c) -> void {
char ch = unicodeToCP437(c); char ch = unicodeToCP437(c);
scr[y*80+x].ch = ch; scr[y * 80 + x].ch = ch;
scr[y*80+x].bgcolor = 0; scr[y * 80 + x].bgcolor = 0;
char col = 0; char col = 0;
switch(curColor) { switch (curColor) {
case Color::BLACK: case Color::BLACK: col = 0; break;
col = 0; case Color::BLUE: col = 1; break;
break; case Color::GREEN: col = 2; break;
case Color::BLUE: case Color::CYAN: col = 3; break;
col = 1; case Color::RED: col = 4; break;
break; case Color::MAGENTA: col = 5; break;
case Color::GREEN: case Color::BROWN: col = 6; break;
col = 2; case Color::LIGHT_GRAY: col = 7; break;
break; case Color::GRAY: col = 8; break;
case Color::CYAN: case Color::LIGHT_BLUE: col = 9; break;
col = 3; case Color::LIGHT_GREEN: col = 10; break;
break; case Color::LIGHT_CYAN: col = 11; break;
case Color::RED: case Color::LIGHT_RED: col = 12; break;
col = 4; case Color::LIGHT_MAGENTA: col = 13; break;
break; case Color::YELLOW: col = 14; break;
case Color::MAGENTA: case Color::WHITE: col = 15; break;
col = 5;
break;
case Color::BROWN:
col = 6;
break;
case Color::LIGHT_GRAY:
col = 7;
break;
case Color::GRAY:
col = 8;
break;
case Color::LIGHT_BLUE:
col = 9;
break;
case Color::LIGHT_GREEN:
col = 10;
break;
case Color::LIGHT_CYAN:
col = 11;
break;
case Color::LIGHT_RED:
col = 12;
break;
case Color::LIGHT_MAGENTA:
col = 13;
break;
case Color::YELLOW:
col = 14;
break;
case Color::WHITE:
col = 15;
break;
} }
scr[y*80+x].fgcolor = col; scr[y * 80 + x].fgcolor = col;
} }

View file

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <tty.hpp> #include <tty.hpp>
class CGATerm: public TTY { class CGATerm : public TTY {
protected: protected:
virtual auto plotChar(int x, int y, int c) -> void; virtual auto plotChar(int x, int y, int c) -> void;
public: public:
CGATerm(); CGATerm();
virtual ~CGATerm(); virtual ~CGATerm();

View file

@ -1,10 +1,21 @@
int tbl[] = {0, 9786, 9787, 9829, 9830, 9827, 9824, 8226, 9688, 9675, 9689, 9794, 9792, 9834, 9835, 9788, 9658, 9668, 8597, 8252, 182, 167, 9644, 8616, 8593, 8595, 8594, 8592, 8735, 8596, 9652, 9660, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 8962, 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197, 201, 230, 198, 244, 246, 242, 251, 249, 255, 214, 220, 162, 163, 165, 8359, 402, 225, 237, 243, 250, 241, 209, 170, 186, 191, 8976, 172, 189, 188, 161, 171, 187, 9617, 9618, 9619, 9474, 9508, 9569, 9570, 9558, 9557, 9571, 9553, 9559, 9565, 9564, 9563, 9488, 9492, 9524, 9516, 9500, 9472, 9532, 9566, 9567, 9562, 9556, 9577, 9574, 9568, 9552, 9580, 9575, 9576, 9572, 9573, 9561, 9560, 9554, 9555, 9579, 9578, 9496, 9484, 9608, 9604, 9612, 9616, 9600, 945, 223, 915, 960, 931, 963, 181, 964, 934, 920, 937, 948, 8734, 966, 949, 8745, 8801, 177, 8805, 8804, 8992, 8993, 247, 8776, 176, 8729, 183, 8730, 8319, 178, 9632, 160}; int tbl[] = {0, 9786, 9787, 9829, 9830, 9827, 9824, 8226, 9688, 9675, 9689, 9794, 9792, 9834, 9835, 9788, 9658, 9668,
8597, 8252, 182, 167, 9644, 8616, 8593, 8595, 8594, 8592, 8735, 8596, 9652, 9660, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
126, 8962, 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197,
201, 230, 198, 244, 246, 242, 251, 249, 255, 214, 220, 162, 163, 165, 8359, 402, 225, 237,
243, 250, 241, 209, 170, 186, 191, 8976, 172, 189, 188, 161, 171, 187, 9617, 9618, 9619, 9474,
9508, 9569, 9570, 9558, 9557, 9571, 9553, 9559, 9565, 9564, 9563, 9488, 9492, 9524, 9516, 9500, 9472, 9532,
9566, 9567, 9562, 9556, 9577, 9574, 9568, 9552, 9580, 9575, 9576, 9572, 9573, 9561, 9560, 9554, 9555, 9579,
9578, 9496, 9484, 9608, 9604, 9612, 9616, 9600, 945, 223, 915, 960, 931, 963, 181, 964, 934, 920,
937, 948, 8734, 966, 949, 8745, 8801, 177, 8805, 8804, 8992, 8993, 247, 8776, 176, 8729, 183, 8730,
8319, 178, 9632, 160};
char unicodeToCP437(int c) { char unicodeToCP437(int c) {
for(int i=0; i<256; i++) for (int i = 0; i < 256; i++)
if(tbl[i]==c) if (tbl[i] == c) return (char)i;
return (char)i;
return 0; return 0;
} }
int CP437ToUnicode(char c) { int CP437ToUnicode(char c) { return tbl[(unsigned char)c]; }
return tbl[(unsigned char)c];
}

View file

@ -1,20 +1,15 @@
#include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
extern "C" void __cxa_pure_virtual() { extern "C" void __cxa_pure_virtual() {
//panic("Pure virtual function called."); // panic("Pure virtual function called.");
} }
void * operator new(size_t s) { void *operator new(size_t s) { return (void *)1; }
return (void*)1; void *operator new[](size_t s) { return (void *)1; }
} void operator delete(void *p) {}
void * operator new[](size_t s) { void operator delete[](void *p) {}
return (void*)1; void operator delete(void *p, size_t s) {}
} void operator delete[](void *p, size_t s) {}
void operator delete(void* p) {} void *operator new(size_t s, void *p) { return p; }
void operator delete[](void* p) {} void *operator new[](size_t s, void *p) { return p; }
void operator delete(void* p, size_t s) {}
void operator delete[](void* p, size_t s) {}
void * operator new(size_t s, void* p) {return p;}
void * operator new[](size_t s, void* p) {return p;}
void operator delete(void *, void *p) {} void operator delete(void *, void *p) {}
void operator delete[](void *, void *p) {} void operator delete[](void *, void *p) {}

View file

@ -1,5 +1,5 @@
#pragma once #pragma once
#include <tty.hpp>
#include <kobject.hpp> #include <kobject.hpp>
#include <tty.hpp>
extern TTY *out; extern TTY *out;
void setMainTTY(Kobject *obj); void setMainTTY(Kobject *obj);

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
enum class kobjectType { enum class kobjectType {
POINTER, //Pointer to non-object POINTER, // Pointer to non-object
KOBJECT, //garbage collected kobject KOBJECT, // garbage collected kobject
TTY, TTY,
}; };
class Kref { class Kref {
@ -15,32 +15,33 @@ class Kref {
refctr = 1; refctr = 1;
mset = false; mset = false;
} }
template<typename T> template <typename T>
auto set(T *data) -> Kref & { auto set(T *data) -> Kref & {
if(mset) if (mset) return *this;
return *this; ptr = (void *)data;
ptr = (void*)data; mset = true;
mset=true;
return *this; return *this;
} }
template<typename T> template <typename T>
T *get() { T *get() {
return (T*)ptr; return (T *)ptr;
} }
auto operator++(int) -> Kref & { auto operator++(int) -> Kref & {
unsigned int old = refctr; unsigned int old = refctr;
refctr++; refctr++;
if(old > refctr) { if (old > refctr) {
//uh-oh this shouldn't happen // uh-oh this shouldn't happen
//Put panic() here, because this object could lead to kernel execution when the refctr overflows. // Put panic() here, because this object could lead to kernel execution
for(;;); // when the refctr overflows.
for (;;)
;
} }
return *this; return *this;
} }
auto operator--(int) -> Kref & { auto operator--(int) -> Kref & {
refctr--; refctr--;
if(!refctr) { if (!refctr) {
//delete ptr; // delete ptr;
mset = false; mset = false;
ptr = nullptr; ptr = nullptr;
} }
@ -51,24 +52,23 @@ class Kobject {
public: public:
kobjectType type; kobjectType type;
unsigned int refctr; unsigned int refctr;
Kobject(kobjectType type):type(type),refctr(1) {} Kobject(kobjectType type) : type(type), refctr(1) {}
virtual ~Kobject() { virtual ~Kobject() { refctr = 0; }
refctr=0;
}
virtual auto operator++() -> Kobject & { virtual auto operator++() -> Kobject & {
unsigned int tmp=refctr; unsigned int tmp = refctr;
refctr++; refctr++;
if(refctr < tmp) { if (refctr < tmp) {
//TODO panic("Refcounter overflow"); // TODO panic("Refcounter overflow");
for(;;); for (;;)
;
} }
return *this; return *this;
} }
virtual auto operator--() -> Kobject & { virtual auto operator--() -> Kobject & {
refctr--; refctr--;
if(refctr == 0) { if (refctr == 0) {
//TODO delete this; // TODO delete this;
return *((Kobject*)nullptr); return *((Kobject *)nullptr);
} }
return *this; return *this;
} }

View file

@ -18,8 +18,8 @@ enum class Color {
YELLOW, YELLOW,
WHITE WHITE
}; };
class TTY: public Kobject { class TTY : public Kobject {
protected: protected:
int x; int x;
int y; int y;
int width; int width;
@ -28,7 +28,8 @@ protected:
unsigned int rgbColor; unsigned int rgbColor;
bool useRGB; bool useRGB;
virtual auto plotChar(int x, int y, int c) -> void; virtual auto plotChar(int x, int y, int c) -> void;
public:
public:
TTY(int width, int height); TTY(int width, int height);
virtual ~TTY(); virtual ~TTY();
virtual auto rgbSupport() -> bool; virtual auto rgbSupport() -> bool;
@ -36,26 +37,25 @@ public:
virtual auto setColor(unsigned int c) -> void; virtual auto setColor(unsigned int c) -> void;
virtual auto putChar(int c) -> void; virtual auto putChar(int c) -> void;
virtual auto puts(const char *s) -> void; virtual auto puts(const char *s) -> void;
template<typename T> template <typename T>
auto puti(T x) -> TTY & { auto puti(T x) -> TTY & {
T output = x; T output = x;
const char* chars="0123456789ABCDEF"; const char *chars = "0123456789ABCDEF";
char buf[sizeof(T)*8+1]; char buf[sizeof(T) * 8 + 1];
buf[sizeof(T)*8]='\0'; buf[sizeof(T) * 8] = '\0';
char* ptr=buf+sizeof(T)*8-1; char *ptr = buf + sizeof(T) * 8 - 1;
do { do {
*(ptr--)=chars[output&0xF]; *(ptr--) = chars[output & 0xF];
output>>=4; output >>= 4;
} while(output && (ptr != buf)); } while (output && (ptr != buf));
puts(ptr+1); puts(ptr + 1);
return *this; return *this;
} }
template<typename T> template <typename T>
auto operator<<(T x) -> TTY & { auto operator<<(T x) -> TTY & {
puts(x); puts(x);
return *this; return *this;
} }
}; };
template <> template <>
auto TTY::operator<<<int>(int) -> TTY &; auto TTY::operator<<<int>(int) -> TTY &;
@ -63,4 +63,3 @@ template <>
auto TTY::operator<<<unsigned int>(unsigned int) -> TTY &; auto TTY::operator<<<unsigned int>(unsigned int) -> TTY &;
template <> template <>
auto TTY::operator<<<long long>(long long) -> TTY &; auto TTY::operator<<<long long>(long long) -> TTY &;

View file

@ -1,14 +1,12 @@
#include <base.hpp> #include <base.hpp>
void drivers_init(); void drivers_init();
extern "C" void(*start_ctors)(); extern "C" void (*start_ctors)();
extern "C" void(*end_ctors)(); extern "C" void (*end_ctors)();
extern "C" void(*start_dtors)(); extern "C" void (*start_dtors)();
extern "C" void(*end_dtors)(); extern "C" void (*end_dtors)();
void main() { void main() {
for(auto ctor=&start_ctors;ctor<&end_ctors;ctor++) for (auto ctor = &start_ctors; ctor < &end_ctors; ctor++) (**ctor)();
(**ctor)();
drivers_init(); drivers_init();
*out << "Hallo!\n"; *out << "Hallo!\n";
for(auto dtor=&start_dtors;dtor!=&end_dtors;dtor++) for (auto dtor = &start_dtors; dtor != &end_dtors; dtor++) (**dtor)();
(**dtor)();
} }

View file

@ -2,11 +2,11 @@
TTY *out; TTY *out;
bool tty_set = false; bool tty_set = false;
void setMainTTY(Kobject *obj) { void setMainTTY(Kobject *obj) {
if(obj->type == kobjectType::TTY) { if (obj->type == kobjectType::TTY) {
++*obj; ++*obj;
// if(tty_set) // if(tty_set)
// --*out; // --*out;
out=(TTY*)obj; out = (TTY *)obj;
tty_set=true; tty_set = true;
} }
} }

View file

@ -1,78 +1,73 @@
#include <tty.hpp> #include <tty.hpp>
TTY::TTY(int width, int height): x(0), y(0), width(width), height(height), curColor(Color::WHITE), rgbColor(0xFFFFFF), useRGB(false), Kobject(kobjectType::TTY) {} TTY::TTY(int width, int height)
: x(0), y(0), width(width), height(height), curColor(Color::WHITE), rgbColor(0xFFFFFF), useRGB(false),
Kobject(kobjectType::TTY) {}
TTY::~TTY() {} TTY::~TTY() {}
auto TTY::plotChar(int x, int y, int c) -> void {} auto TTY::plotChar(int x, int y, int c) -> void {}
auto TTY::rgbSupport() -> bool {return true;} auto TTY::rgbSupport() -> bool { return true; }
auto TTY::setColor(Color c) -> void { auto TTY::setColor(Color c) -> void {
curColor = c; curColor = c;
useRGB=false; useRGB = false;
} }
auto TTY::setColor(unsigned int c) -> void { auto TTY::setColor(unsigned int c) -> void {
rgbColor = c; rgbColor = c;
useRGB=true; useRGB = true;
} }
auto TTY::putChar(int c) -> void { auto TTY::putChar(int c) -> void {
auto scroll = [this]()->void { auto scroll = [this]() -> void {
for(int x=0;x<this->width;x++) for (int x = 0; x < this->width; x++)
for(int y=0;y<this->height;y++) for (int y = 0; y < this->height; y++) this->plotChar(x, y, 0);
this->plotChar(x,y,0); this->x = this->y = 0;
this->x=this->y=0;
}; };
switch(c) { switch (c) {
case '\n': case '\n':
x=0; x = 0;
y++; y++;
if(y>=height) if (y >= height) scroll();
scroll();
break; break;
default: default:
plotChar(x,y,c); plotChar(x, y, c);
x++; x++;
if(x>width) if (x > width) y++;
y++; if (y >= height) scroll();
if(y>=height)
scroll();
} }
} }
auto TTY::puts(const char *str) -> void { auto TTY::puts(const char *str) -> void {
for(int i=0;str[i];i++) { for (int i = 0; str[i]; i++) {
//Decode UTF-8 // Decode UTF-8
int character=0; int character = 0;
if(str[i]<0x80) { if (str[i] < 0x80) { character = str[i]; }
character=str[i]; // UTF-8 character
if ((str[i] & 0b11000000) == 0b10000000) {
character = 0xFFFD; // ILLEGAL SEQUENCE
} }
//UTF-8 character // Now count how many bytes follow
if((str[i]&0b11000000)==0b10000000) { int bytesToFollow = 0;
character=0xFFFD; //ILLEGAL SEQUENCE if ((str[i] & 0b11100000) == 0b11000000) {
} bytesToFollow = 1;
//Now count how many bytes follow character = str[i] & 0b11111;
int bytesToFollow=0; } else if ((str[i] & 0b11110000) == 0b11100000) {
if((str[i]&0b11100000)==0b11000000) { bytesToFollow = 2;
bytesToFollow=1; character = str[i] & 0b1111;
character=str[i]&0b11111; } else if ((str[i] & 0b11111000) == 0b11110000) {
} else if((str[i]&0b11110000)==0b11100000) { bytesToFollow = 3;
bytesToFollow=2; character = str[i] & 0b111;
character=str[i]&0b1111; } else if (!character)
} else if((str[i]&0b11111000)==0b11110000) { character = 0xFFFD; // ILLEGAL SEQUENCE
bytesToFollow=3; while (bytesToFollow) {
character=str[i]&0b111;
} else if(!character)
character=0xFFFD; //ILLEGAL SEQUENCE
while(bytesToFollow) {
i++; i++;
if(!str[i]) { if (!str[i]) {
putChar(0xFFFD); //Incomplete putChar(0xFFFD); // Incomplete
return; return;
} }
if((str[i]&0b11000000)!=0b10000000) { if ((str[i] & 0b11000000) != 0b10000000) {
putChar(0xFFFD); //ILLEGAL SEQUENCE putChar(0xFFFD); // ILLEGAL SEQUENCE
break; break;
} }
character <<= 6; character <<= 6;
character |= str[i] & 0b111111; character |= str[i] & 0b111111;
bytesToFollow--; bytesToFollow--;
} }
if(!bytesToFollow) if (!bytesToFollow) putChar(character);
putChar(character);
} }
} }