<commit_before>#include "include/video.h"
#include "include/font.h"
#include "../include/common.h"
#include "../system/ipc/include/mutex.h"

//TODO: Delete of characters if fucked up, should be fixed

#define ROW(cursor_offset) ((cursor_offset)/WIDTH)
#define COL(cursor_offset) ((cursor_offset)%WIDTH)

static void reDrawMatrix();
static void iPrintNewLine();
static char isValidColor(char color);
static ColorRGB getRGBColor(char color);
static char isValidOffset(int row, int col);
static void printChar(char c, int row, int col, char color);
static void printBase(uint64_t value, uint32_t base, char color);
static void iPrintPixel(uint16_t x, uint64_t y, ColorRGB *color);
static void printCharPixelLine(char line, int x, int y, char color);
static uint32_t uintToBase(uint64_t value, char * buffer, uint32_t base);

static char
	defaultColor = COLOR_DEFAULT,
	defaultBgColor = COLOR_BG_DEFAULT;
static int offset = 0;
static bool must_lock; 
mutex v_mutex;
static charStructure charMatrix[HEIGHT][WIDTH];
static ColorRGB 
	baseColors[16] = {
		{0,		0,		0},
		{0,		0,		255},
		{0,		128,	0},
		{0,		255,	255},
		{255,	0,		0},
		{255,	0,		255},
		{165,	42,		42},
		{211,	211,	211},
		{128,	128,	128},
		{0,		128,	255},
		{144,	238,	144},
		{224,	255,	255},
		{255,	77,		77},
		{237,	83,		147},
		{255,	255,	0},
		{255,	255,	255}
	};

bool videoBuild() {
	v_mutex = lockBuild();

	if(v_mutex == NULL)
		return false;

	must_lock = false;

	return true;
}

void videoDestroy() {
	lockDestroy(v_mutex);
}

void videoStartLocking() {
	must_lock = true;
}

void videoStopLocking() {
	must_lock = false;
}

void print(const char* str, char color) {
	if(must_lock == true)
		lock(v_mutex);

	for(int i = 0; str[i] != '\0' && isValidOffset(ROW(offset), COL(offset)); i++) {
		if(str[i] == '\n')
			iPrintNewLine();
		else {
			printChar(str[i], ROW(offset), COL(offset), color);
			offset++;
		}
	}

	if(must_lock == true)
		unlock(v_mutex);
}

void printToast(const char* str, int size) {
	if(must_lock == true)
		lock(v_mutex);

	int
		bOffset = offset;
	offset = (HEIGHT - 1)*WIDTH + WIDTH/3;
	print(str, COLOR_NULL);
	offset = bOffset;

	if(must_lock == true)
		unlock(v_mutex);
}

void printDec(uint64_t value, char color) {
	if(must_lock == true)
		lock(v_mutex);

	printBase(value, 10, color);

	if(must_lock == true)
		unlock(v_mutex);
}

void printHex(uint64_t value, char color) {
	if(must_lock == true)
		lock(v_mutex);

	printBase(value, 16, color);

	if(must_lock == true)
		unlock(v_mutex);
}

void putChar(char c, char color) {
	if(must_lock == true)
		lock(v_mutex);

	if(c == '\b') {
		offset--;
		printChar(' ', ROW(offset), COL(offset), color);
	} else if(c == '\n') {
		iPrintNewLine();
		if(ROW(offset) == HEIGHT)
			reDrawMatrix();
	}
	else {
		printChar(c, ROW(offset), COL(offset), color);
		offset++;
		if(ROW(offset) == HEIGHT)
			reDrawMatrix();
	}

	if(must_lock == true)
		unlock(v_mutex);
}

void printNewLine() {
	if(must_lock == true)
		lock(v_mutex);


	iPrintNewLine();

	if(must_lock == true)
		unlock(v_mutex);
}

void clear() {
	if(must_lock == true)
		lock(v_mutex);

	ColorRGB bgcolor = getRGBColor(defaultBgColor);
	for(int i = 0; i < 1025; i ++)
		for(int j=0; j<769; j++)
			iPrintPixel(i, j, &bgcolor);

	offset = 0;

	if(must_lock == true)
		unlock(v_mutex);
}

void setDefaultColor(char color) {
	if(must_lock == true)
		lock(v_mutex);

	defaultColor = color;

	if(must_lock == true)<commit_msg>Fixed clear bug<commit_after>#include "include/video.h"
#include "include/font.h"
#include "../include/common.h"
#include "../system/ipc/include/mutex.h"

//TODO: Delete of characters if fucked up, should be fixed

#define ROW(cursor_offset) ((cursor_offset)/WIDTH)
#define COL(cursor_offset) ((cursor_offset)%WIDTH)

static void reDrawMatrix();
static void iPrintNewLine();
static char isValidColor(char color);
static ColorRGB getRGBColor(char color);
static char isValidOffset(int row, int col);
static void printChar(char c, int row, int col, char color);
static void printBase(uint64_t value, uint32_t base, char color);
static void iPrintPixel(uint16_t x, uint64_t y, ColorRGB *color);
static void printCharPixelLine(char line, int x, int y, char color);
static uint32_t uintToBase(uint64_t value, char * buffer, uint32_t base);

static char
	defaultColor = COLOR_DEFAULT,
	defaultBgColor = COLOR_BG_DEFAULT;
static int offset = 0;
static bool must_lock; 
mutex v_mutex;
static charStructure charMatrix[HEIGHT][WIDTH];
static ColorRGB 
	baseColors[16] = {
		{0,		0,		0},
		{0,		0,		255},
		{0,		128,	0},
		{0,		255,	255},
		{255,	0,		0},
		{255,	0,		255},
		{165,	42,		42},
		{211,	211,	211},
		{128,	128,	128},
		{0,		128,	255},
		{144,	238,	144},
		{224,	255,	255},
		{255,	77,		77},
		{237,	83,		147},
		{255,	255,	0},
		{255,	255,	255}
	};

bool videoBuild() {
	v_mutex = lockBuild();

	if(v_mutex == NULL)
		return false;

	must_lock = false;

	for(int i=0; i<HEIGHT; i++) {
		for(int j=0; j<WIDTH; j++) {
			charMatrix[i][j].c = EMPTY;
			charMatrix[i][j].color = defaultColor;
		}
	}

	return true;
}

void videoDestroy() {
	lockDestroy(v_mutex);
}

void videoStartLocking() {
	must_lock = true;
}

void videoStopLocking() {
	must_lock = false;
}

void print(const char* str, char color) {
	if(must_lock == true)
		lock(v_mutex);

	for(int i = 0; str[i] != '\0' && isValidOffset(ROW(offset), COL(offset)); i++) {
		if(str[i] == '\n')
			iPrintNewLine();
		else {
			printChar(str[i], ROW(offset), COL(offset), color);
			offset++;
		}
	}

	if(must_lock == true)
		unlock(v_mutex);
}

void printToast(const char* str, int size) {
	if(must_lock == true)
		lock(v_mutex);

	int
		bOffset = offset;
	offset = (HEIGHT - 1)*WIDTH + WIDTH/3;
	print(str, COLOR_NULL);
	offset = bOffset;

	if(must_lock == true)
		unlock(v_mutex);
}

void printDec(uint64_t value, char color) {
	if(must_lock == true)
		lock(v_mutex);

	printBase(value, 10, color);

	if(must_lock == true)
		unlock(v_mutex);
}

void printHex(uint64_t value, char color) {
	if(must_lock == true)
		lock(v_mutex);

	printBase(value, 16, color);

	if(must_lock == true)
		unlock(v_mutex);
}

void putChar(char c, char color) {
	if(must_lock == true)
		lock(v_mutex);

	if(c == '\b') {
		offset--;
		printChar(' ', ROW(offset), COL(offset), color);
	} else if(c == '\n') {
		iPrintNewLine();
		if(ROW(offset) == HEIGHT)
			reDrawMatrix();
	}
	else {
		printChar(c, ROW(offset), COL(offset), color);
		offset++;
		if(ROW(offset) == HEIGHT)
			reDrawMatrix();
	}

	if(must_lock == true)
		unlock(v_mutex);
}

void printNewLine() {
	if(must_lock == true)
		lock(v_mutex);

	iPrintNewLine();

	if(must_lock == true)
		unlock(v_mutex);
}

void clear() {
	if(must_lock == true)
		lock(v_mutex);

	ColorRGB bgcolor = getRGBColor(defaultBgColor);
	for(int i=0; i<1025; i++)
		for(int j=0; j<769; j++)
			iPrintPixel(i, j, &bgcolor);

	for(int i=0; i<HEIGHT; i++) {
		for(int j=0; j<WIDTH; j++) {
			charMatrix[i][j].c = EMPTY;
			charMatrix[i][j].color = defaultColor;
		}
	}

	offset = 0;

	if(must_lock == true)
		unlock(v_mutex);
}

void setDefaultColor(char color) {
	if(must_lock == true)
		lock(v_mutex);

	defaultColor = color;

	if(must_lock == true)
{'fraction_non_alphanumeric': 0.16554809843400448, 'fraction_numerical': 0.043064876957494405, 'mean_word_length': 4.263428991905813, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5733788', 'n_tokens_mistral': 3237, 'n_tokens_neox': 2886, 'n_words': 701}