summaryrefslogtreecommitdiff
path: root/src/user/app/vterm/vterm.c
blob: 43180b2352b055f1b8c0f8e613f3d7974adc1b52 (plain)
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
#include "vterm.h"
#include <camellia/syscalls.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct point cursor = {0};

void in_char(char c) {
	switch (c) {
		case '\n':
			cursor.x = 0;
			cursor.y++;
			break;
		case '\b':
			if (cursor.x > 0) cursor.x--;
			break;
		default:
			font_blit(c, cursor.x, cursor.y);
			cursor.x++;
	}

	if (cursor.x * font.w >= fb.width) {
		cursor.x = 0;
		cursor.y++;
	}
	while (cursor.y * font.h >= fb.height) scroll();
}

int main();

void fb_setup(void) {
	#define BASEPATH "/kdev/video/"
	char path[64], *spec;
	size_t pos;
	FILE *f;

	f = fopen(BASEPATH, "r");
	if (!f) {
		eprintf("couldn't open %s", BASEPATH);
		exit(1);
	}

	pos = strlen(BASEPATH);
	memcpy(path, BASEPATH, pos);
	spec = path + pos;
	fread(spec, 1, sizeof(path) - pos, f);
	/* assumes the read went correctly */
	fclose(f);

	fb_fd = _syscall_open(path, strlen(path), 0);
	if (fb_fd < 0) {
		eprintf("failed to open %s", path);
		exit(1);
	}

	fb.width  = strtol(spec, &spec, 0);
	if (*spec++ != 'x') { eprintf("bad filename format"); exit(1); }
	fb.height = strtol(spec, &spec, 0);
	if (*spec++ != 'x') { eprintf("bad filename format"); exit(1); }
	fb.bpp = strtol(spec, &spec, 0);
	fb.len = _syscall_getsize(fb_fd);
	fb.pitch = fb.len / fb.height;
	fb.b = malloc(fb.len);

	if (fb.bpp != 32) {
		eprintf("unsupported format %ux%ux%u", fb.width, fb.height, fb.bpp);
		exit(1);
	}
}

int main(void) {
	fb_setup();
	font_load("/init/font.psf");

	static char buf[512];
	struct fs_wait_response res;
	while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
		switch (res.op) {
			case VFSOP_OPEN:
				// TODO check path
				_syscall_fs_respond(NULL, 0, 0);
				break;

			case VFSOP_WRITE:
				if (res.flags) {
					_syscall_fs_respond(NULL, -1, 0);
				} else {
					for (size_t i = 0; i < res.len; i++)
						in_char(buf[i]);
					flush();
					_syscall_fs_respond(NULL, res.len, 0);
				}
				break;

			default:
				_syscall_fs_respond(NULL, -1, 0);
				break;
		}
	}

	return 1;
}