aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/chess/chess.c2
-rw-r--r--apps/wm/wm.c58
-rw-r--r--kernel/Makefile1
-rw-r--r--kernel/features/bus.c52
-rw-r--r--kernel/features/io.c22
-rw-r--r--kernel/features/logger.c42
-rw-r--r--kernel/features/proc.c57
-rw-r--r--kernel/features/syscall.c5
-rw-r--r--kernel/inc/io.h9
-rw-r--r--kernel/inc/logger.h8
-rw-r--r--kernel/inc/proc.h10
-rw-r--r--libs/libc/inc/assert.h17
-rw-r--r--libs/libc/inc/print.h5
-rw-r--r--libs/libc/inc/sys.h15
-rw-r--r--libs/libc/print.c59
-rw-r--r--libs/libc/rand.c (renamed from libs/libc/random.c)0
-rw-r--r--libs/libc/sys.c2
-rw-r--r--libs/libgui/gui.c52
-rw-r--r--libs/libgui/gui.h2
-rw-r--r--libs/libgui/msg.c31
-rw-r--r--libs/libgui/msg.h7
21 files changed, 289 insertions, 167 deletions
diff --git a/apps/chess/chess.c b/apps/chess/chess.c
index 833edb0..a8e5fae 100644
--- a/apps/chess/chess.c
+++ b/apps/chess/chess.c
@@ -241,7 +241,7 @@ static void draw_board(void)
int main(void)
{
- assert((win = gui_new_window()) > 0);
+ assert(gui_new_window(&win) == EOK);
fen_parse(START_FEN);
draw_board();
diff --git a/apps/wm/wm.c b/apps/wm/wm.c
index 00dedd0..7b88764 100644
--- a/apps/wm/wm.c
+++ b/apps/wm/wm.c
@@ -13,7 +13,7 @@
#include <rand.h>
struct client {
- u32 pid;
+ u32 conn; // Bus conn
};
struct window {
@@ -385,7 +385,11 @@ static void handle_event_mouse(struct event_mouse *event)
msg.id = win->id;
msg.pos = vec2_sub(mouse.pos, win->pos);
msg.bits.click = event->but1;
- msg_send(win->client.pid, GUI_MOUSE, &msg, sizeof(msg));
+
+ if (msg_connect_conn(win->client.conn) == EOK)
+ msg_send(GUI_MOUSE, &msg, sizeof(msg));
+ else
+ log("Failed to connect to window\n");
}
/**
@@ -394,50 +398,56 @@ static void handle_event_mouse(struct event_mouse *event)
static void handle_message_new_window(struct message_new_window *msg)
{
- struct window *win = window_new((struct client){ .pid = msg->header.src }, "idk",
+ struct window *win = window_new((struct client){ .conn = msg->header.bus.conn }, "idk",
vec2(500, 600), vec2(600, 400), 0);
msg->ctx = win->ctx;
msg->shid = win->shid;
msg->id = win->id;
- if (msg->header.state == MSG_NEED_ANSWER)
- msg_send(msg->header.src, GUI_NEW_WINDOW | MSG_SUCCESS, msg, sizeof(*msg));
+ if (msg->header.state == MSG_NEED_ANSWER) {
+ msg_connect_conn(msg->header.bus.conn);
+ msg_send(GUI_NEW_WINDOW | MSG_SUCCESS, msg, sizeof(*msg));
+ }
}
static void handle_message_redraw_window(struct message_redraw_window *msg)
{
u32 id = msg->id;
struct window *win = window_find(id);
- if (!win || win->client.pid != msg->header.src) {
- if (msg->header.state == MSG_NEED_ANSWER)
- msg_send(msg->header.src, GUI_REDRAW_WINDOW | MSG_FAILURE, msg,
- sizeof(msg->header));
+ if (!win || win->client.conn != msg->header.bus.conn) {
+ if (msg->header.state == MSG_NEED_ANSWER) {
+ msg_connect_conn(msg->header.bus.conn);
+ msg_send(GUI_REDRAW_WINDOW | MSG_FAILURE, msg, sizeof(msg->header));
+ }
return;
}
window_redraw(win);
- if (msg->header.state == MSG_NEED_ANSWER)
- msg_send(msg->header.src, GUI_REDRAW_WINDOW | MSG_SUCCESS, msg,
- sizeof(msg->header));
+ if (msg->header.state == MSG_NEED_ANSWER) {
+ msg_connect_conn(msg->header.bus.conn);
+ msg_send(GUI_REDRAW_WINDOW | MSG_SUCCESS, msg, sizeof(msg->header));
+ }
}
static void handle_message_destroy_window(struct message_destroy_window *msg)
{
u32 id = msg->id;
struct window *win = window_find(id);
- if (!win || win->client.pid != msg->header.src) {
- if (msg->header.state == MSG_NEED_ANSWER)
- msg_send(msg->header.src, GUI_DESTROY_WINDOW | MSG_FAILURE, msg,
- sizeof(msg->header));
+ if (!win || win->client.conn != msg->header.bus.conn) {
+ if (msg->header.state == MSG_NEED_ANSWER) {
+ msg_connect_conn(msg->header.bus.conn);
+ msg_send(GUI_DESTROY_WINDOW | MSG_FAILURE, msg, sizeof(msg->header));
+ }
return;
}
window_destroy(win);
- if (msg->header.state == MSG_NEED_ANSWER)
- msg_send(msg->header.src, GUI_DESTROY_WINDOW | MSG_SUCCESS, msg,
- sizeof(msg->header));
+ if (msg->header.state == MSG_NEED_ANSWER) {
+ msg_connect_conn(msg->header.bus.conn);
+ msg_send(GUI_DESTROY_WINDOW | MSG_SUCCESS, msg, sizeof(msg->header));
+ }
}
static void handle_message(void *msg)
@@ -456,13 +466,13 @@ static void handle_message(void *msg)
break;
default:
log("Message type %d not implemented!\n", header->type);
- msg_send(header->src, header->type | MSG_FAILURE, msg, sizeof(*header));
+ msg_connect_conn(header->bus.conn);
+ msg_send(GUI_DESTROY_WINDOW | MSG_SUCCESS, msg, sizeof(header));
}
}
static void handle_exit(void)
{
- log("Handle\n");
if (keymap)
free(keymap);
@@ -495,7 +505,7 @@ int main(int argc, char **argv)
assert(io_control(IO_FRAMEBUFFER, IOCTL_FB_GET, &screen) == EOK);
log("WM loaded: %dx%d\n", screen.width, screen.height);
- wm_client = (struct client){ .pid = getpid() };
+ wm_client = (struct client){ .conn = 0 };
bypp = (screen.bpp >> 3);
windows = list_new();
@@ -519,6 +529,8 @@ int main(int argc, char **argv)
assert(io_control(IO_BUS, IOCTL_BUS_REGISTER, "wm") == EOK);
+ assert(exec("chess", NULL) == EOK);
+
u8 msg[1024] = { 0 };
struct event_keyboard event_keyboard = { 0 };
struct event_mouse event_mouse = { 0 };
@@ -539,7 +551,7 @@ int main(int argc, char **argv)
continue;
}
} else if (poll_ret == IO_BUS) {
- if (io_read(IO_BUS, msg, 0, sizeof(msg)) > 0) {
+ if (msg_receive(msg, sizeof(msg)) > 0) {
handle_message(msg);
continue;
}
diff --git a/kernel/Makefile b/kernel/Makefile
index 2b75415..17a89b7 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -17,6 +17,7 @@ COBJS = main.o \
features/fs.o \
features/io.o \
features/bus.o \
+ features/logger.o \
features/load.o \
features/proc.o \
features/proc_asm.o \
diff --git a/kernel/features/bus.c b/kernel/features/bus.c
index f8a561c..4a09fc0 100644
--- a/kernel/features/bus.c
+++ b/kernel/features/bus.c
@@ -19,6 +19,7 @@
struct bus_conn {
u32 bus; // Bus name hash
u32 conn;
+ u32 pid; // Client pid
struct stack *in; // Queue of bus owner
struct stack *out; // Queue of other client
};
@@ -85,6 +86,7 @@ static void bus_add_conn(u32 hash, u32 conn)
{
struct bus_conn *bus_conn = zalloc(sizeof(*bus_conn));
bus_conn->bus = hash;
+ bus_conn->pid = proc_current()->pid;
bus_conn->conn = conn;
bus_conn->in = stack_new();
bus_conn->out = stack_new();
@@ -113,7 +115,7 @@ static res bus_register(const char *name)
return EOK;
}
-static res bus_connect(const char *bus, u32 *conn)
+static res bus_connect_bus(const char *bus, u32 *conn)
{
if (!memory_readable(bus) || !memory_writable(conn))
return -EFAULT;
@@ -137,7 +139,25 @@ static res bus_connect(const char *bus, u32 *conn)
return EOK;
}
-static res bus_send(u32 conn, void *buf, u32 count)
+static res bus_connect_conn(u32 conn)
+{
+ struct bus_conn *bus_conn = bus_find_conn(conn);
+ if (!bus_conn)
+ return -ENOENT;
+
+ struct bus *bus = bus_find_bus(bus_conn->bus);
+ if (!bus)
+ return -ENOENT;
+
+ if (bus_conn->pid != proc_current()->pid && bus->pid != proc_current()->pid)
+ return -EACCES;
+
+ proc_current()->bus_conn = conn;
+
+ return EOK;
+}
+
+static res bus_send(u32 conn, const void *buf, u32 count)
{
if (!count)
return EOK;
@@ -160,10 +180,13 @@ static res bus_send(u32 conn, void *buf, u32 count)
msg->conn = conn;
msg->size = count;
- if (bus->pid == proc_current()->pid)
- stack_push_bot(bus_conn->in, msg);
- else
+ if (bus->pid == proc_current()->pid) {
stack_push_bot(bus_conn->out, msg);
+ io_unblock_pid(bus_conn->pid);
+ } else {
+ stack_push_bot(bus_conn->in, msg);
+ io_unblock_pid(bus->pid);
+ }
return count;
}
@@ -176,16 +199,20 @@ static res bus_conn_receive(struct bus_conn *bus_conn, void *buf, u32 offset, u3
struct bus_message *msg = NULL;
if (bus->pid == proc_current()->pid)
- msg = stack_pop(bus_conn->out);
+ msg = stack_pop(bus_conn->in);
else
msg = stack_pop(bus_conn->out);
if (!msg)
return -EIO;
- memcpy_user(buf, (u8 *)msg->data + offset, MIN(count, msg->size));
+ struct bus_header h = { .conn = bus_conn->conn };
+ memcpy_user(buf, &h, sizeof(h));
+ memcpy_user((u8 *)buf + sizeof(h), (u8 *)msg->data + offset, MIN(count, msg->size));
+
free(msg->data);
free(msg);
+
return MIN(count, msg->size);
}
@@ -230,8 +257,11 @@ static res bus_control(u32 request, void *arg1, void *arg2, void *arg3)
UNUSED(arg3);
switch (request) {
- case IOCTL_BUS_CONNECT: {
- return bus_connect(arg1, arg2);
+ case IOCTL_BUS_CONNECT_BUS: {
+ return bus_connect_bus(arg1, arg2);
+ }
+ case IOCTL_BUS_CONNECT_CONN: {
+ return bus_connect_conn((u32)arg1);
}
case IOCTL_BUS_REGISTER: {
return bus_register(arg1);
@@ -242,7 +272,7 @@ static res bus_control(u32 request, void *arg1, void *arg2, void *arg3)
}
}
-static res bus_write(void *buf, u32 offset, u32 count)
+static res bus_write(const void *buf, u32 offset, u32 count)
{
if (offset)
return -EINVAL;
@@ -263,7 +293,7 @@ static res bus_conn_ready(struct bus_conn *bus_conn)
u8 ready = 0;
if (bus->pid == proc_current()->pid)
- ready = !stack_empty(bus_conn->out);
+ ready = !stack_empty(bus_conn->in);
else
ready = !stack_empty(bus_conn->out);
diff --git a/kernel/features/io.c b/kernel/features/io.c
index c48eb62..fbae5cf 100644
--- a/kernel/features/io.c
+++ b/kernel/features/io.c
@@ -8,6 +8,7 @@
#include <interrupts.h>
#include <io.h>
#include <list.h>
+#include <logger.h>
#include <mem.h>
#include <mm.h>
#include <proc.h>
@@ -117,7 +118,7 @@ res io_control(enum io_type io, u32 request, void *arg1, void *arg2, void *arg3)
return dev->control(request, arg1, arg2, arg3);
}
-res io_write(enum io_type io, void *buf, u32 offset, u32 count)
+res io_write(enum io_type io, const void *buf, u32 offset, u32 count)
{
if (!memory_readable(buf))
return -EFAULT;
@@ -182,6 +183,24 @@ void io_unblock(enum io_type io)
}
}
+void io_unblock_pid(u32 pid)
+{
+ for (u32 io = IO_MIN; io < IO_MAX; io++) {
+ struct node *iterator = io_listeners[io]->head;
+ while (iterator) {
+ struct io_listener *listener = iterator->data;
+ struct proc *proc = listener->proc;
+ proc_state(proc, PROC_RUNNING);
+ struct node *next = iterator->next;
+ if (proc->pid == pid) {
+ list_remove(io_listeners[io], iterator);
+ free(listener);
+ }
+ iterator = next;
+ }
+ }
+}
+
CLEAR void io_install(struct boot_info *boot)
{
for (u32 i = 0; i < IO_MAX; i++)
@@ -200,6 +219,7 @@ CLEAR void io_install(struct boot_info *boot)
}
timer_install();
+ logger_install();
fb_install(boot->vid);
bus_install();
}
diff --git a/kernel/features/logger.c b/kernel/features/logger.c
new file mode 100644
index 0000000..7db9a82
--- /dev/null
+++ b/kernel/features/logger.c
@@ -0,0 +1,42 @@
+// MIT License, Copyright (c) 2021 Marvin Borner
+
+#include <cpu.h>
+#include <def.h>
+#include <errno.h>
+#include <io.h>
+#include <logger.h>
+#include <mem.h>
+#include <print.h>
+#include <serial.h>
+
+static res logger_write(const void *buf, u32 offset, u32 count)
+{
+ if (offset)
+ return -EINVAL;
+
+ if (!count)
+ return EOK;
+
+ print_prefix();
+
+ u32 i;
+ stac();
+ for (i = 0; i < count; i++) {
+ if (!((const u8 *)buf)[i])
+ break;
+
+ serial_put(((const u8 *)buf)[i]);
+ }
+ clac();
+
+ serial_print("\x1B[0m");
+
+ return i;
+}
+
+void logger_install(void)
+{
+ struct io_dev *dev = zalloc(sizeof(*dev));
+ dev->write = logger_write;
+ io_add(IO_LOGGER, dev);
+}
diff --git a/kernel/features/proc.c b/kernel/features/proc.c
index cada97d..4cf4005 100644
--- a/kernel/features/proc.c
+++ b/kernel/features/proc.c
@@ -251,21 +251,6 @@ static const char *procfs_parse_path(const char **path, u32 *pid)
return *path;
}
-static enum stream_defaults procfs_stream(const char *path)
-{
- if (!memcmp_user(path, "in", 3)) {
- return STREAM_IN;
- } else if (!memcmp_user(path, "out", 4)) {
- return STREAM_OUT;
- } else if (!memcmp_user(path, "err", 4)) {
- return STREAM_ERR;
- } else if (!memcmp_user(path, "log", 4)) {
- return STREAM_LOG;
- } else {
- return STREAM_UNKNOWN;
- }
-}
-
struct procfs_message {
u8 *data;
u32 size;
@@ -273,39 +258,12 @@ struct procfs_message {
static res procfs_write(const char *path, void *buf, u32 offset, u32 count, struct vfs_dev *dev)
{
+ UNUSED(path);
+ UNUSED(buf);
UNUSED(offset);
+ UNUSED(count);
UNUSED(dev);
- u32 pid = 0;
- procfs_parse_path(&path, &pid);
- if (pid) {
- struct proc *p = proc_from_pid(pid);
- stac();
- if (!p || path[0] != '/') {
- clac();
- return -ENOENT;
- }
- clac();
-
- path++;
- if (!memcmp_user(path, "io/", 3)) {
- path += 3;
- enum stream_defaults id = procfs_stream(path);
- if (id == STREAM_UNKNOWN)
- return -ENOENT;
-
- // Put proc log/err messages to serial console for debugging
- if (id == STREAM_LOG || id == STREAM_ERR)
- print_app(id, p->name, (char *)buf);
-
- struct stream *stream = &p->streams[id];
- assert(stream->offset_write + count < STREAM_MAX_SIZE); // TODO: Resize
- memcpy_user((char *)(stream->data + stream->offset_write), buf, count);
- stream->offset_write += count;
- return count;
- }
- }
-
return -ENOENT;
}
@@ -338,15 +296,6 @@ static res procfs_read(const char *path, void *buf, u32 offset, u32 count, struc
const char *status = p->state == PROC_RUNNING ? "running" : "sleeping";
memcpy_user(buf, status + offset, count);
return count;
- } else if (!memcmp_user(path, "io/", 3)) {
- path += 3;
- enum stream_defaults id = procfs_stream(path);
- if (id == STREAM_UNKNOWN)
- return -ENOENT;
- struct stream *stream = &p->streams[id];
- memcpy_user(buf, stream->data + stream->offset_read, count);
- stream->offset_read += count;
- return count;
}
}
diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c
index 94ea778..23c8d37 100644
--- a/kernel/features/syscall.c
+++ b/kernel/features/syscall.c
@@ -132,6 +132,11 @@ static void syscall_handler(struct regs *r)
break;
}
+ case SYS_MIN:
+ case SYS_MAX:
+ r->eax = -EINVAL;
+ break;
+
// TODO: Reimplement network functions using VFS
default: {
r->eax = -EINVAL;
diff --git a/kernel/inc/io.h b/kernel/inc/io.h
index e0b817b..ed7920e 100644
--- a/kernel/inc/io.h
+++ b/kernel/inc/io.h
@@ -1,7 +1,7 @@
// MIT License, Copyright (c) 2021 Marvin Borner
-#ifndef DEV_H
-#define DEV_H
+#ifndef IO_H
+#define IO_H
#include <boot.h>
#include <def.h>
@@ -11,7 +11,7 @@
struct io_dev {
res (*read)(void *buf, u32 offset, u32 count) NONNULL;
- res (*write)(void *buf, u32 offset, u32 count) NONNULL;
+ res (*write)(const void *buf, u32 offset, u32 count) NONNULL;
res (*control)(u32 request, void *arg1, void *arg2, void *arg3);
res (*ready)(void);
};
@@ -21,12 +21,13 @@ void io_add(enum io_type io, struct io_dev *dev) NONNULL;
// No NONNULL on syscalls
res io_control(enum io_type io, u32 request, void *arg1, void *arg2, void *arg3);
-res io_write(enum io_type io, void *buf, u32 offset, u32 count);
+res io_write(enum io_type io, const void *buf, u32 offset, u32 count);
res io_read(enum io_type io, void *buf, u32 offset, u32 count);
res io_poll(u32 *devs);
res io_ready(enum io_type io);
void io_block(enum io_type io, struct proc *proc) NONNULL;
void io_unblock(enum io_type io);
+void io_unblock_pid(u32 pid);
#endif
diff --git a/kernel/inc/logger.h b/kernel/inc/logger.h
new file mode 100644
index 0000000..61ec576
--- /dev/null
+++ b/kernel/inc/logger.h
@@ -0,0 +1,8 @@
+// MIT License, Copyright (c) 2021 Marvin Borner
+
+#ifndef LOGGER_H
+#define LOGGER_H
+
+void logger_install(void);
+
+#endif
diff --git a/kernel/inc/proc.h b/kernel/inc/proc.h
index 1e3f037..5b8fbea 100644
--- a/kernel/inc/proc.h
+++ b/kernel/inc/proc.h
@@ -22,25 +22,15 @@
#define RING(regs) ((regs->cs) & 3)
-#define STREAM_MAX_SIZE 4096
-enum stream_defaults { STREAM_IN, STREAM_OUT, STREAM_ERR, STREAM_LOG, STREAM_UNKNOWN = -1 };
-
enum proc_priv { PROC_PRIV_NONE, PROC_PRIV_ROOT, PROC_PRIV_KERNEL };
enum proc_state { PROC_RUNNING, PROC_BLOCKED };
-struct stream {
- u32 offset_read;
- u32 offset_write;
- char data[STREAM_MAX_SIZE];
-};
-
struct proc {
u32 pid;
u32 entry;
char name[64];
char dir[64];
- struct stream streams[4];
struct page_dir *page_dir;
struct regs regs;
enum proc_priv priv;
diff --git a/libs/libc/inc/assert.h b/libs/libc/inc/assert.h
index 9621e36..fb5a857 100644
--- a/libs/libc/inc/assert.h
+++ b/libs/libc/inc/assert.h
@@ -8,15 +8,20 @@
#ifdef KERNEL
#include <proc.h>
#define assert(exp) \
- if (!(exp)) { \
- printf("%s:%d: %s: Kernel assertion '%s' failed\n", __FILE__, __LINE__, __func__, \
- #exp); \
- __asm__ volatile("cli\nhlt"); \
+ { \
+ if (!(exp)) { \
+ printf("%s:%d: %s: Kernel assertion '%s' failed\n", __FILE__, __LINE__, \
+ __func__, #exp); \
+ __asm__ volatile("cli\nhlt"); \
+ } \
}
#elif defined(USER)
#define assert(exp) \
- if (!(exp)) \
- err(1, "%s:%d: %s: Assertion '%s' failed\n", __FILE__, __LINE__, __func__, #exp);
+ { \
+ if (!(exp)) \
+ err(1, "%s:%d: %s: Assertion '%s' failed\n", __FILE__, __LINE__, __func__, \
+ #exp); \
+ }
#endif
#endif
diff --git a/libs/libc/inc/print.h b/libs/libc/inc/print.h
index 1d85c33..6d959c3 100644
--- a/libs/libc/inc/print.h
+++ b/libs/libc/inc/print.h
@@ -14,13 +14,16 @@ int print(const char *str) NONNULL;
NORETURN void panic(const char *format, ...) NONNULL;
#ifdef USER
+#include <sys.h>
int vfprintf(const char *path, const char *format, va_list ap) NONNULL;
+int viprintf(enum io_type io, const char *format, va_list ap) NONNULL;
int fprintf(const char *path, const char *format, ...) NONNULL;
+int iprintf(enum io_type io, const char *format, ...) NONNULL;
int log(const char *format, ...) NONNULL;
void err(int code, const char *format, ...) NONNULL;
#else
#include <proc.h>
-int print_app(enum stream_defaults id, const char *proc_name, const char *str) NONNULL;
+int print_prefix(void);
void print_trace(u32 count);
#endif
diff --git a/libs/libc/inc/sys.h b/libs/libc/inc/sys.h
index 72a8029..8c8e217 100644
--- a/libs/libc/inc/sys.h
+++ b/libs/libc/inc/sys.h
@@ -15,6 +15,7 @@
#define SYS_BOOT_SHUTDOWN 0xdead
enum sys {
+ SYS_MIN,
SYS_ALLOC, // Allocate memory
SYS_SHACCESS, // Access shared memory
SYS_FREE, // Free memory
@@ -29,10 +30,12 @@ enum sys {
SYS_EXIT, // Exit current process
SYS_BOOT, // Boot functions (e.g. reboot/shutdown)
SYS_YIELD, // Switch to next process
+ SYS_MAX,
};
enum io_type {
IO_MIN,
+ IO_LOGGER,
IO_FRAMEBUFFER,
IO_NETWORK,
IO_KEYBOARD,
@@ -44,8 +47,14 @@ enum io_type {
// I/O control declarations
#define IOCTL_FB_GET 0
-#define IOCTL_BUS_CONNECT 0
-#define IOCTL_BUS_REGISTER 1
+#define IOCTL_BUS_CONNECT_BUS 0
+#define IOCTL_BUS_CONNECT_CONN 1
+#define IOCTL_BUS_REGISTER 2
+
+struct bus_header {
+ u32 conn;
+ // Data starts here
+};
struct event_keyboard {
u32 magic;
@@ -84,7 +93,7 @@ res exec(const char *path, ...) ATTR((nonnull(1))) SENTINEL;
res io_poll(enum io_type *devs) NONNULL;
res io_read(enum io_type io, void *buf, u32 offset, u32 count) NONNULL;
-res io_write(enum io_type io, void *buf, u32 offset, u32 count) NONNULL;
+res io_write(enum io_type io, const void *buf, u32 offset, u32 count) NONNULL;
res io_control(enum io_type io, ...);
res yield(void);
diff --git a/libs/libc/print.c b/libs/libc/print.c
index 27048bd..2d08fdb 100644
--- a/libs/libc/print.c
+++ b/libs/libc/print.c
@@ -7,6 +7,15 @@
#include <mem.h>
#include <str.h>
+#define RED "\x1B[1;31m"
+#define GRN "\x1B[1;32m"
+#define YEL "\x1B[1;33m"
+#define BLU "\x1B[1;34m"
+#define MAG "\x1B[1;35m"
+#define CYN "\x1B[1;36m"
+#define WHT "\x1B[1;37m"
+#define RES "\x1B[0m"
+
static void append(char *dest, char *src, int index)
{
for (u32 i = index; i < strlen(src) + index; i++)
@@ -98,13 +107,10 @@ int snprintf(char *str, u32 size, const char *format, ...)
#ifdef USER
#include <sys.h>
-#define PATH_OUT "/proc/self/io/out"
-#define PATH_LOG "/proc/self/io/log"
-#define PATH_ERR "/proc/self/io/err"
int vprintf(const char *format, va_list ap)
{
- return vfprintf(PATH_OUT, format, ap);
+ return viprintf(IO_LOGGER, format, ap);
}
int vfprintf(const char *path, const char *format, va_list ap)
@@ -114,6 +120,13 @@ int vfprintf(const char *path, const char *format, va_list ap)
return write(path, buf, 0, len);
}
+int viprintf(enum io_type io, const char *format, va_list ap)
+{
+ char buf[1024] = { 0 };
+ int len = vsnprintf(buf, sizeof(buf), format, ap);
+ return io_write(io, buf, 0, len);
+}
+
int fprintf(const char *path, const char *format, ...)
{
va_list ap;
@@ -124,6 +137,16 @@ int fprintf(const char *path, const char *format, ...)
return len;
}
+int iprintf(enum io_type io, const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ int len = viprintf(io, format, ap);
+ va_end(ap);
+
+ return len;
+}
+
int printf(const char *format, ...)
{
va_list ap;
@@ -138,7 +161,7 @@ int log(const char *format, ...)
{
va_list ap;
va_start(ap, format);
- int len = vfprintf(PATH_LOG, format, ap);
+ int len = viprintf(IO_LOGGER, format, ap);
va_end(ap);
return len;
@@ -150,14 +173,14 @@ NORETURN void err(int code, const char *format, ...)
log("ERRNO: %d (%s)\n", errno, strerror(errno));
va_list ap;
va_start(ap, format);
- vfprintf(PATH_ERR, format, ap);
+ viprintf(IO_LOGGER, format, ap);
va_end(ap);
exit(code);
}
int print(const char *str)
{
- return write(PATH_OUT, str, 0, strlen(str));
+ return io_write(IO_LOGGER, str, 0, strlen(str));
}
#else
@@ -169,15 +192,6 @@ int print(const char *str)
#include <proc.h>
#include <serial.h>
-#define RED "\x1B[1;31m"
-#define GRN "\x1B[1;32m"
-#define YEL "\x1B[1;33m"
-#define BLU "\x1B[1;34m"
-#define MAG "\x1B[1;35m"
-#define CYN "\x1B[1;36m"
-#define WHT "\x1B[1;37m"
-#define RES "\x1B[0m"
-
static void print_kernel(const char *str)
{
serial_print(RED);
@@ -204,18 +218,11 @@ int printf(const char *format, ...)
return len;
}
-int print_app(enum stream_defaults id, const char *proc_name, const char *str)
+int print_prefix(void)
{
- if (id == STREAM_LOG)
- serial_print(CYN "[LOG] to ");
- else if (id == STREAM_ERR)
- serial_print(YEL "[ERR] to ");
- serial_print(proc_name);
+ serial_print(CYN "[LOG] to ");
+ serial_print(proc_current()->name);
serial_print(": ");
- stac();
- serial_print(str);
- clac();
- serial_print(RES);
return 1;
}
diff --git a/libs/libc/random.c b/libs/libc/rand.c
index 268a21f..268a21f 100644
--- a/libs/libc/random.c
+++ b/libs/libc/rand.c
diff --git a/libs/libc/sys.c b/libs/libc/sys.c
index b0e6e93..cf2165a 100644
--- a/libs/libc/sys.c
+++ b/libs/libc/sys.c
@@ -138,7 +138,7 @@ res io_read(enum io_type io, void *buf, u32 offset, u32 count)
return sys4(SYS_IOREAD, (int)io, (int)buf, (int)offset, (int)count);
}
-res io_write(enum io_type io, void *buf, u32 offset, u32 count)
+res io_write(enum io_type io, const void *buf, u32 offset, u32 count)
{
return sys4(SYS_IOWRITE, (int)io, (int)buf, (int)offset, (int)count);
}
diff --git a/libs/libgui/gui.c b/libs/libgui/gui.c
index 4e3b4cb..ef99d92 100644
--- a/libs/libgui/gui.c
+++ b/libs/libgui/gui.c
@@ -8,8 +8,6 @@
#include <list.h>
#include <print.h>
-#define WM_PATH "wm"
-
struct gui_widget {
u32 id;
vec2 pos;
@@ -105,6 +103,21 @@ static struct gui_widget *gui_widget_by_id(u32 win_id, u32 widget_id)
}
/**
+ * Bus stuff
+ */
+
+static u32 wm_conn = 0;
+static void gui_connect_wm(void)
+{
+ if (wm_conn) {
+ // TODO: Fix
+ assert(msg_connect_conn(wm_conn) == EOK);
+ } else {
+ assert(msg_connect_bus("wm", &wm_conn) == EOK);
+ }
+}
+
+/**
* GFX wrappers
*/
@@ -398,7 +411,7 @@ vec2 gui_window_size(u32 win_id)
* Window manager interfaces
*/
-res gui_new_window(void)
+res gui_new_window(u32 *id)
{
if (!windows)
windows = list_new();
@@ -406,8 +419,8 @@ res gui_new_window(void)
struct gui_window *win = zalloc(sizeof(*win));
struct message_new_window msg = { .header.state = MSG_NEED_ANSWER };
- if (msg_send(pidof(WM_PATH), GUI_NEW_WINDOW, &msg, sizeof(msg)) > 0 &&
- msg_receive(&msg, sizeof(msg)) > 0 &&
+ gui_connect_wm();
+ if (msg_send(GUI_NEW_WINDOW, &msg, sizeof(msg)) > 0 && msg_receive(&msg, sizeof(msg)) > 0 &&
msg.header.type == (GUI_NEW_WINDOW | MSG_SUCCESS)) {
win->id = msg.id;
win->ctx = msg.ctx;
@@ -418,7 +431,8 @@ res gui_new_window(void)
list_add(windows, win);
win->widgets = list_new();
- return win->id;
+ *id = win->id;
+ return_errno(EOK);
}
return_errno(EINVAL);
@@ -431,7 +445,8 @@ res gui_redraw_window(u32 id)
return_errno(ENOENT);
struct message_redraw_window msg = { .id = id, .header.state = MSG_NEED_ANSWER };
- if (msg_send(pidof(WM_PATH), GUI_REDRAW_WINDOW, &msg, sizeof(msg)) > 0 &&
+ gui_connect_wm();
+ if (msg_send(GUI_REDRAW_WINDOW, &msg, sizeof(msg)) > 0 &&
msg_receive(&msg, sizeof(msg)) > 0 &&
msg.header.type == (GUI_REDRAW_WINDOW | MSG_SUCCESS))
return EOK;
@@ -456,17 +471,20 @@ static res gui_handle_ping(struct message_ping *msg)
msg->header.type |= MSG_SUCCESS;
msg->ping = MSG_PING_RECV;
- msg_send(msg->header.src, GUI_PING, &msg, sizeof(msg));
-
- return errno;
+ if (msg_connect_conn(msg->header.bus.conn) == EOK &&
+ msg_send(GUI_PING, msg, sizeof(msg)) == EOK)
+ return EOK;
+ else
+ return errno;
}
static res gui_handle_mouse(struct message_mouse *msg)
{
if (msg->header.state == MSG_NEED_ANSWER) {
- msg_send(msg->header.src, msg->header.type | MSG_SUCCESS, msg, sizeof(*msg));
-
- if (errno != EOK)
+ if (msg_connect_conn(msg->header.bus.conn) == EOK &&
+ msg_send(msg->header.type | MSG_SUCCESS, msg, sizeof(msg)) == EOK)
+ return EOK;
+ else
return errno;
}
@@ -495,7 +513,8 @@ static void gui_handle_exit(void)
while (iterator) {
struct gui_window *win = iterator->data;
struct message_destroy_window msg = { .id = win->id };
- msg_send(pidof(WM_PATH), GUI_DESTROY_WINDOW, &msg, sizeof(msg));
+ gui_connect_wm();
+ msg_send(GUI_DESTROY_WINDOW, &msg, sizeof(msg));
iterator = iterator->next;
}
@@ -514,7 +533,7 @@ void gui_loop(void)
err(1, "Create some windows first\n");
void *msg = zalloc(4096);
- while (msg_receive(msg, 4096)) {
+ while (gui_connect_wm(), msg_receive(msg, 4096) > 0) {
struct message_header *head = msg;
switch (head->type) {
case GUI_PING:
@@ -528,4 +547,7 @@ void gui_loop(void)
gui_handle_error("loop", EINVAL);
}
}
+
+ free(msg);
+ err(1, "Gui loop failed\n");
}
diff --git a/libs/libgui/gui.h b/libs/libgui/gui.h
index f4c213b..abc50fb 100644
--- a/libs/libgui/gui.h
+++ b/libs/libgui/gui.h
@@ -18,7 +18,7 @@ enum gui_layer {
GUI_LAYER_FG,
};
-res gui_new_window(void);
+res gui_new_window(u32 *id);
res gui_redraw_window(u32 id);
res gui_fill(u32 win_id, u32 widget_id, enum gui_layer layer, u32 c);
diff --git a/libs/libgui/msg.c b/libs/libgui/msg.c
index 051072e..70a77a1 100644
--- a/libs/libgui/msg.c
+++ b/libs/libgui/msg.c
@@ -6,24 +6,39 @@
#include <print.h>
#include <sys.h>
-res msg_send(u32 pid, enum message_type type, void *data, u32 size)
+// TODO: Remove debug assertions?
+
+res msg_connect_bus(const char *bus, u32 *conn)
+{
+ res ret = io_control(IO_BUS, IOCTL_BUS_CONNECT_BUS, bus, conn);
+ assert(ret == EOK && *conn);
+ return EOK;
+}
+
+res msg_connect_conn(u32 conn)
+{
+ res ret = io_control(IO_BUS, IOCTL_BUS_CONNECT_CONN, conn);
+ assert(ret == EOK);
+ return ret;
+}
+
+res msg_send(enum message_type type, void *data, u32 size)
{
- assert((signed)pid != -1 && size >= sizeof(struct message_header));
- char path[32] = { 0 };
- snprintf(path, sizeof(path), "/proc/%d/msg", pid);
+ assert(size >= sizeof(struct message_header));
struct message_header *header = data;
header->magic = MSG_MAGIC;
- header->src = getpid();
header->type = type;
- return write(path, data, 0, size);
+ res ret = io_write(IO_BUS, (u8 *)data + sizeof(struct bus_header), 0, size);
+ assert(ret >= EOK);
+ return ret;
}
res msg_receive(void *buf, u32 size)
{
- int ret = read("/proc/self/msg", buf, 0, size);
+ res ret = io_read(IO_BUS, buf, 0, size);
struct message_header *header = buf;
if (header->magic == MSG_MAGIC)
return ret;
else
- return -1;
+ return -EINVAL;
}
diff --git a/libs/libgui/msg.h b/libs/libgui/msg.h
index c25e95e..857042c 100644
--- a/libs/libgui/msg.h
+++ b/libs/libgui/msg.h
@@ -5,6 +5,7 @@
#include <def.h>
#include <libgui/gfx.h>
+#include <sys.h>
#define MSG_PING_SEND 0x07734
#define MSG_PING_RECV 0x7474
@@ -19,8 +20,8 @@ enum message_state {
};
struct message_header {
+ struct bus_header bus;
u32 magic;
- u32 src;
u32 type;
enum message_state state;
};
@@ -66,7 +67,9 @@ enum message_type {
GUI_KEYBOARD,
};
-res msg_send(u32 pid, enum message_type type, void *data, u32 size) NONNULL;
+res msg_connect_bus(const char *bus, u32 *conn);
+res msg_connect_conn(u32 conn);
+res msg_send(enum message_type type, void *data, u32 size) NONNULL;
res msg_receive(void *buf, u32 size) NONNULL;
#endif