#include "VirtualConsole.h"
#include <idl4glue.h>
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <new>
#include <assert.h>
#include <string.h>
#include <l4/thread.h>
#include <l4/ipc.h>
#include <l4/kip.h>
#include <sdi/panic.h>
#include <sdi/log.h>
#include <sdi/locator.h>
#include <if/iflocator.h>
#include <if/iflogging.h>
#include <if/ifsyscall.h>
VirtualConsole::VirtualConsole(int consoleId, char* videoMemStart) {
myConsoleId = consoleId;
videoBuffer = videoMemStart;
hasFocus = 0;
inputBuffer = new uint8_t[INPUT_BUFFER_LENGTH];
nextFreeIn = 0;
keyStatus = 0;
consoleBuffer = new ConsoleBuffer(CONSOLE_BUFFER_LENGTH, videoMemStart, &hasFocus);
consoleBuffer->ClearScreen();
buffer_t buf;
buf._buffer = new char[128];
sprintf(buf._buffer, "Hello to console%d", consoleId);
buf._length = strlen(buf._buffer);
consoleBuffer->appendString(&buf);
consoleBuffer->NewLine();
};
VirtualConsole::~VirtualConsole() {
}
idlsize_t VirtualConsole::write(const buffer_t *buffer, const uint32_t pos) {
for(CORBA_unsigned_long i=0; i<buffer->_length; i++) {
char c = buffer->_buffer[i];
if((32 <= c) && (c <= 126) ) {
consoleBuffer->appendChar(c);
} else if('\b' == c) {
consoleBuffer->DeleteLast();
} else if('\n' == c) {
consoleBuffer->NewLine();
} else if(127 == c) {
consoleBuffer->Delete();
} else if(27 == c) {
if(i+2 < buffer->_length) {
if(buffer->_buffer[i+1] == '[') {
if(buffer->_buffer[i+2] == 's') {
consoleBuffer->SaveCursor();
i += 2;
} else if(buffer->_buffer[i+2] == 'u') {
consoleBuffer->LoadCursor();
i += 2;
} else if(i+3 < buffer->_length && 48 <= buffer->_buffer[i+2] && buffer->_buffer[i+2] <= 57) {
uint32_t n = 0;
int k=i+2;
while(k < buffer->_length && 48 <= buffer->_buffer[k] && buffer->_buffer[k] <= 57) {
n = n * 10 + (buffer->_buffer[k] - 48);
k++;
}
if(k < buffer->_length) {
if(buffer->_buffer[k] == 'm') {
consoleBuffer->SetGraphic(n);
} else if(buffer->_buffer[k] == 'C') {
consoleBuffer->CursorRight(n);
consoleBuffer->update();
} else if(buffer->_buffer[k] == 'D') {
consoleBuffer->CursorLeft(n);
consoleBuffer->update();
} else if(buffer->_buffer[k] == 'K') {
consoleBuffer->ClearLine(n);
} else if(buffer->_buffer[k] == 'J' && 2 == buffer->_buffer[k-1]) {
consoleBuffer->ClearScreen();
ClearInputBuffer();
}
i = k;
}
}
}
}
}
}
consoleBuffer->update();
return buffer->_length;
};
CORBA_unsigned_long VirtualConsole::read(idlsize_t readsize, CORBA_char* buf) {
if(nextFreeIn == 0) {
return 0;
} else if(nextFreeIn <= readsize) {
memcpy(buf, inputBuffer, nextFreeIn);
int filled = nextFreeIn;
nextFreeIn = 0;
return filled;
} else {
memcpy(buf, inputBuffer, readsize);
int size = nextFreeIn - readsize;
memcpy(inputBuffer, inputBuffer + readsize, size);
nextFreeIn = size;
return readsize;
}
};
TODO:
void VirtualConsole::addInput(uint8_t c) {
if(nextFreeIn < INPUT_BUFFER_LENGTH) {
inputBuffer[nextFreeIn] = c;
nextFreeIn++;
} else {
LogMessage("InputBuffer full in console %d", myConsoleId);
}
};
void VirtualConsole::ClearInputBuffer() {
nextFreeIn = 0;
}