summaryrefslogtreecommitdiffstats
path: root/lib/virtterm.c
blob: de43c514e1a0365b8656abfddd48d08e9fb18778 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
127
128
129
130
131
#include "virtterm.h"
#include "boot/cmdline.h"
#include "boot/initrd.h"
#include "hal/obj.h"
#include "init.h"
#include "jove.h"
#include "hal/framebuffer.h"
#include "lib/printf.h"
#include "lib/psf.h"
#include "lib/string.h"

struct psf1_header *s_font = NULL;

struct virtterm s_init_vtty = { 0 };

static void
vt_blit(struct virtterm *vtty, jove_owned_obj_t *fb, const uint8_t *glyph)
{
    uint32_t buffer[s_font->char_height << 3];
    size_t cursor_x = vtty->cursor_col * 9;
    size_t cursor_y = vtty->cursor_row * s_font->char_height;

    for(size_t y = 0; y < s_font->char_height; y++) {
        uint8_t mask = 1 << 7;
        for(size_t x = 0; x < 8; x++) {
            buffer[x + (y << 3)] = glyph[y] & mask ? vtty->cursor_fg : vtty->cursor_bg;
            mask >>=1;
        }
    }
    fb_blit(fb, buffer, cursor_x, cursor_y, 8, s_font->char_height);
    vtty->cursor_col++;
}

static void
vt_putc(struct virtterm *vtty, jove_owned_obj_t *fb, char c)
{
    const uint8_t *glyph = &s_font->bitmap[(size_t)c * s_font->char_height];
    
    switch(c) {
        case '\n':
            vtty->cursor_col = 0;
            vtty->cursor_row++;
        break;
        default:
            vt_blit(vtty, fb, glyph);
        break;
    }

    if(vtty->cursor_col >= vtty->width) {
        vtty->cursor_col = 0;
        vtty->cursor_row++;
    }
    if(vtty->cursor_row >= vtty->height) {
        vtty->cursor_row = 0;
    }
}

int
vt_write(jove_owned_obj_t *obj, const char *s, size_t n)
{
    if(obj_gettype(obj) != JOVE_OBJ_VIRTTERM) {
        reterr(EBADOBJ);
    }

    struct virtterm *vtty = obj_deref(obj);
    jove_owned_obj_t *fb_obj = obj_take(objdir_getlocal(_rootdir_obj, vtty->fb_obj));
    if(obj_gettype(fb_obj) != JOVE_OBJ_FRAMEBUFFER) {
        obj_release(fb_obj);
        reterr(EBADOBJ);
    }

    for(size_t i = 0; i < n; i++) {
        vt_putc(vtty, fb_obj, s[i]);
    }

    obj_release(fb_obj);
    return 0;
}

void
vt_init()
{
    const char *font_path = cmdline_getarg("font");
    if(font_path == NULL) {
        kprintf("Kernel commandline is missing 'font' argument. VTT is disabled.");
        return;
    }

    jove_owned_obj_t *initrd_obj = obj_take(objdir_getlocal(_rootdir_obj, ROOTDIR_INITRD_I));
    if(obj_gettype(initrd_obj) != JOVE_OBJ_OBJDIR) {
        kprintf("Initrd is not present. VTT is disabled.\n");
        obj_release(initrd_obj);
        return;
    }

    jove_owned_obj_t *file_obj;
    size_t file_i = 0;
    while((file_obj = obj_take(objdir_getlocal(initrd_obj, file_i++)))) {
        const char *filename = NULL;
        if(initrd_file_name(file_obj, &filename) != 0) {
            break;
        }
        if(strncmp(font_path, filename, strlen(font_path)) == 0) {
            initrd_file_data(file_obj, (uintptr_t*)&s_font);
            break;
        }
        obj_release(file_obj);
    }
    obj_release(file_obj);
    obj_release(initrd_obj);

    if(s_font == NULL) {
        kprintf("Could not find file %s in initrd. VTT is disabled.\n", font_path);
        return;
    }
    s_init_vtty.fb_obj = ROOTDIR_FRAMEBUFFER_I;
    s_init_vtty.cursor_fg = 0x00FFFFFF;


    {
        jove_owned_obj_t *fb_obj = obj_take(objdir_getlocal(_rootdir_obj, ROOTDIR_FRAMEBUFFER_I));
        s_init_vtty.width = fb_width(fb_obj) / 9;
        s_init_vtty.height = fb_height(fb_obj) / s_font->char_height;
        obj_release(fb_obj);

        jove_owned_obj_t *vt_obj = obj_take(objdir_getlocal(_rootdir_obj, ROOTDIR_VIRTTERM_I));
        obj_settype(vt_obj, JOVE_OBJ_VIRTTERM);
        obj_setvalue(vt_obj, (uintmax_t)&s_init_vtty);
        obj_release(vt_obj);
    }
}