console vga stuff
This commit is contained in:
44
kernel/lib/font.c
Normal file
44
kernel/lib/font.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "vfs.h"
|
||||
#include <lib/font.h>
|
||||
#include <lib/memory.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
psf_font_t* load_font(const char* path) {
|
||||
int fd = vfs_open(path, 0);
|
||||
if (fd == -1) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
psf_hdr_t* header = kalloc(sizeof(psf_hdr_t));
|
||||
int rd = vfs_read(fd, header, sizeof(psf_hdr_t));
|
||||
if (rd <= 0) {
|
||||
kfree(header);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t num_glyphs = (header->mode & (1 << 0)) ? 512 : 256;
|
||||
uint32_t glyph_sz = header->charsz;
|
||||
uint32_t glyph_buf_sz = num_glyphs * glyph_sz;
|
||||
|
||||
void* glyphs = kalloc(glyph_buf_sz);
|
||||
|
||||
rd = 0;
|
||||
rd = vfs_read(fd, glyphs, glyph_buf_sz);
|
||||
if (rd <= 0) {
|
||||
kfree(header);
|
||||
kfree(glyphs);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
psf_font_t* font = kalloc(sizeof(psf_font_t));
|
||||
font->hdr = header;
|
||||
font->glyphs = glyphs;
|
||||
return font;
|
||||
}
|
||||
|
||||
void free_font(psf_font_t* font) {
|
||||
kfree(font->glyphs);
|
||||
kfree(font->hdr);
|
||||
kfree(font);
|
||||
}
|
||||
20
kernel/lib/font.h
Normal file
20
kernel/lib/font.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef KFONT_H
|
||||
#define KFONT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint8_t magic[2];
|
||||
uint8_t mode;
|
||||
uint8_t charsz;
|
||||
} psf_hdr_t;
|
||||
|
||||
typedef struct {
|
||||
psf_hdr_t* hdr;
|
||||
void* glyphs;
|
||||
} psf_font_t;
|
||||
|
||||
psf_font_t* load_font(const char* path);
|
||||
void free_font(psf_font_t* font);
|
||||
|
||||
#endif
|
||||
@@ -5,21 +5,24 @@
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <drivers/vga/vga.h>
|
||||
#include <drivers/console/console.h>
|
||||
|
||||
spinlock_t kwrite_lock = {0};
|
||||
|
||||
extern psf_font_t* kfont;
|
||||
|
||||
static char* hex_chars = "0123456789abcdef";
|
||||
|
||||
static void kprint(const char *str) {
|
||||
while (*str) {
|
||||
vga_putchar(*str);
|
||||
console_putchar(*str);
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
static void kputd(int d) {
|
||||
if (d == 0) {
|
||||
vga_putchar('0');
|
||||
console_putchar('0');
|
||||
return;
|
||||
}
|
||||
char buffer[12];
|
||||
@@ -29,13 +32,13 @@ static void kputd(int d) {
|
||||
d /= 10;
|
||||
}
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
vga_putchar(buffer[j]);
|
||||
console_putchar(buffer[j]);
|
||||
}
|
||||
}
|
||||
|
||||
static void kputx(unsigned int x) {
|
||||
if (x == 0) {
|
||||
vga_putchar('0');
|
||||
console_putchar('0');
|
||||
return;
|
||||
}
|
||||
char buffer[8];
|
||||
@@ -46,13 +49,13 @@ static void kputx(unsigned int x) {
|
||||
x >>= 4;
|
||||
}
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
vga_putchar(buffer[j]);
|
||||
console_putchar(buffer[j]);
|
||||
}
|
||||
}
|
||||
|
||||
void kputc(const char c) {
|
||||
spin_lock(&kwrite_lock);
|
||||
vga_putchar(c);
|
||||
console_putchar(c);
|
||||
spin_unlock(&kwrite_lock);
|
||||
}
|
||||
|
||||
@@ -82,20 +85,20 @@ void kprintf(const char* fmt, ...) {
|
||||
}
|
||||
case 'c': {
|
||||
int c = va_arg(args, int);
|
||||
vga_putchar((char)c);
|
||||
console_putchar((char)c);
|
||||
break;
|
||||
}
|
||||
case '%':
|
||||
vga_putchar('%');
|
||||
console_putchar('%');
|
||||
break;
|
||||
default:
|
||||
vga_putchar('%');
|
||||
vga_putchar(*fmt);
|
||||
console_putchar('%');
|
||||
console_putchar(*fmt);
|
||||
break;
|
||||
}
|
||||
fmt++;
|
||||
} else {
|
||||
vga_putchar(*fmt);
|
||||
console_putchar(*fmt);
|
||||
fmt++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user