luxos/Kernel/debug/console-out.c

156 lines
2.8 KiB
C
Raw Normal View History

#include <debugio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
extern uint8* VideoPtr;
extern UPoint ConsoleCursor;
extern UPoint ConsoleSize;
extern uint8 ConsoleDefaultColor;
/**************************************
* Write operations*
**************************************/
void _write_char(char c)
{
switch (c)
{
case '\n':
ConsoleCursorNewline(); break;
case '\r':
ConsoleCursor.X = 0; break;
case '\t':
ConsoleCursorIncreasePos(6 - ConsoleCursor.X % 6); break;
case '\b':
ConsoleCursorIncreasePos(-1);
VideoPtr[2 * (ConsoleCursor.Y * ConsoleSize.X + ConsoleCursor.X)] = 0;
break;
default:
VideoPtr[2 * (ConsoleCursor.Y * ConsoleSize.X + ConsoleCursor.X)] = c;
VideoPtr[2 * (ConsoleCursor.Y * ConsoleSize.X + ConsoleCursor.X) + 1] = ConsoleDefaultColor;
ConsoleCursorIncreasePos(1);
break;
}
}
void _write_string (string s)
{
int32 len = strlen(s), i;
for (i = 0; i < len; i++)
_write_char(s[i]);
}
void ConsoleWriteChar (char c)
{
_write_char(c);
ConsoleCursorUpdateHardware();
}
void ConsoleWriteString (string s)
{
_write_string(s);
ConsoleCursorUpdateHardware();
}
int32 ConsoleWrite (string format, ...)
{
if (!format || !*format) return 0;
va_list args;
va_start (args, format);
uint32 i, len = strlen(format);
uint8 temp_color = ConsoleDefaultColor;
for (i = 0; i < len; i++)
if (format[i] != '%') _write_char(format[i]);
else
{
++i;
switch (format[i]) {
// Character
case 'c': {
char c = va_arg (args, char);
_write_char(c);
break;
}
// String
case 's': {
int32* c = (int32*) va_arg (args, string);
_write_string((string)c);
break;
}
// Integers
case 'd':
case 'i': {
int32 c = va_arg(args, int32); char temp[32];
ConvertIntToString(temp, c, 10);
_write_string(temp);
break;
}
// Integers - hex
case 'X':
case 'x': {
int32 c = va_arg(args, int32); char temp[32];
ConvertUIntToString(temp, c, 16);
_write_string(temp);
break;
}
// Integers - unsigned
case 'u': {
int32 c = va_arg(args, uint32); char temp[32];
ConvertUIntToString (temp, c, 10);
_write_string(temp);
break;
}
// Colors
case '#': {
uint8 c = va_arg(args, uint8);
ConsoleDefaultColor = c;
break; }
default: va_end(args); return 1;
};
}
va_end(args);
ConsoleDefaultColor = temp_color;
ConsoleCursorUpdateHardware();
return i;
}
void ConsoleSetChar(Point pos, char c)
{
VideoPtr[2 * (pos.Y * ConsoleSize.X + pos.X)] = c;
}
void ConsoleSetColor(Point pos, uint8 color)
{
VideoPtr[2 * (pos.Y * ConsoleSize.X + pos.X) + 1] = color;
}
char ConsoleGetChar(Point pos)
{
return (char)VideoPtr[2 * (pos.Y * ConsoleSize.X + pos.X)];
}
uint8 ConsoleGetColor (Point pos)
{
return VideoPtr[2 * (pos.Y * ConsoleSize.X + pos.X) + 1];
}