summaryrefslogtreecommitdiff
path: root/src/user
diff options
context:
space:
mode:
authordzwdz2022-08-06 00:12:51 +0200
committerdzwdz2022-08-06 00:12:51 +0200
commit03c6f3cf458c89df17b02557cd232f9cde73ed54 (patch)
treea89076307f32d9df6eec550f175accaa73d72408 /src/user
parent641b857dcfe1cfca625fbd75646b743142a9069c (diff)
make snprintf shared; dynamic resolution support
Diffstat (limited to 'src/user')
-rw-r--r--src/user/app/vterm/vterm.c52
-rw-r--r--src/user/app/vterm/vterm.h1
-rw-r--r--src/user/lib/include/stdio.h2
-rw-r--r--src/user/lib/printf.c27
4 files changed, 46 insertions, 36 deletions
diff --git a/src/user/app/vterm/vterm.c b/src/user/app/vterm/vterm.c
index 0468477..43180b2 100644
--- a/src/user/app/vterm/vterm.c
+++ b/src/user/app/vterm/vterm.c
@@ -1,7 +1,10 @@
#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};
@@ -26,15 +29,50 @@ void in_char(char c) {
while (cursor.y * font.h >= fb.height) scroll();
}
-int main(void) {
- fb_fd = _syscall_open("/kdev/video/b", 13, 0);
- // TODO don't hardcode size
- fb.len = 640 * 480 * 4;
- fb.width = 640;
- fb.height = 480;
- fb.pitch = 640 * 4;
+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];
diff --git a/src/user/app/vterm/vterm.h b/src/user/app/vterm/vterm.h
index 5e09105..5cfc609 100644
--- a/src/user/app/vterm/vterm.h
+++ b/src/user/app/vterm/vterm.h
@@ -24,6 +24,7 @@ void font_blit(uint32_t glyph, int x, int y);
struct framebuf {
size_t len, width, height, pitch;
+ uint8_t bpp;
char *b;
};
extern struct framebuf fb;
diff --git a/src/user/lib/include/stdio.h b/src/user/lib/include/stdio.h
index 063c81f..4d9cf94 100644
--- a/src/user/lib/include/stdio.h
+++ b/src/user/lib/include/stdio.h
@@ -11,11 +11,9 @@
int printf(const char *restrict fmt, ...);
int fprintf(FILE *restrict f, const char *restrict fmt, ...);
-int snprintf(char *restrict str, size_t len, const char *restrict fmt, ...);
int vprintf(const char *restrict fmt, va_list ap);
int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap);
-int vsnprintf(char *restrict str, size_t len, const char *restrict fmt, va_list ap);
int _klogf(const char *fmt, ...); // for kernel debugging only
diff --git a/src/user/lib/printf.c b/src/user/lib/printf.c
index 53b1140..cb6561b 100644
--- a/src/user/lib/printf.c
+++ b/src/user/lib/printf.c
@@ -12,24 +12,6 @@ int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap) {
return __printf_internal(fmt, ap, backend_file, f);
}
-static void backend_buf(void *arg, const char *buf, size_t len) {
- char **ptrs = arg;
- size_t space = ptrs[1] - ptrs[0];
- if (len > space) len = space;
-
- memcpy(ptrs[0], buf, len);
- ptrs[0] += len;
- /* ptrs[1] is the last byte of the buffer, it must be 0.
- * on overflow:
- * ptrs[0] + (ptrs[1] - ptrs[0]) = ptrs[1] */
- *ptrs[0] = '\0';
-}
-
-int vsnprintf(char *restrict str, size_t len, const char *restrict fmt, va_list ap) {
- char *ptrs[2] = {str, str + len - 1};
- return __printf_internal(fmt, ap, backend_buf, &ptrs);
-}
-
int printf(const char *restrict fmt, ...) {
int ret;
@@ -49,15 +31,6 @@ int fprintf(FILE *restrict f, const char *restrict fmt, ...) {
return ret;
}
-int snprintf(char *restrict str, size_t len, const char *restrict fmt, ...) {
- int ret;
- va_list argp;
- va_start(argp, fmt);
- ret = vsnprintf(str, len, fmt, argp);
- va_end(argp);
- return ret;
-}
-
int vprintf(const char *restrict fmt, va_list ap) {
return vfprintf(stdout, fmt, ap);
}