aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2021-01-09 23:19:53 +0100
committerMarvin Borner2021-01-09 23:19:53 +0100
commit836294b9232c7d63d26db4f87c32cf1420cd856d (patch)
tree54f1b28951c53b38dcde5866a74c082c8ef83b0f
parentf27a5f8af9fc5a16b80a7d6646e44d718b0efd7d (diff)
To be continued
-rw-r--r--apps/wm.c22
-rw-r--r--kernel/drivers/keyboard.c2
-rw-r--r--kernel/features/proc.c7
-rw-r--r--kernel/features/syscall.c3
-rw-r--r--kernel/inc/proc.h2
-rw-r--r--libc/inc/mem.h10
-rw-r--r--libc/inc/sys.h9
-rw-r--r--libc/mem.c17
-rw-r--r--libgui/gfx.c8
-rw-r--r--libgui/gui.c14
-rw-r--r--libgui/inc/gfx.h3
11 files changed, 56 insertions, 41 deletions
diff --git a/apps/wm.c b/apps/wm.c
index 104469b..e590d46 100644
--- a/apps/wm.c
+++ b/apps/wm.c
@@ -288,30 +288,30 @@ int main(int argc, char **argv)
event_register(EVENT_MOUSE);
event_register(EVENT_KEYBOARD);
- struct message *msg;
+ struct message msg = { 0 };
while (1) {
- if (!(msg = msg_receive())) {
+ if (!msg_receive(&msg)) {
yield();
continue;
}
- switch (msg->type) {
+ switch (msg.type) {
case GFX_NEW_CONTEXT: {
- struct context *ctx = msg->data;
+ struct context *ctx = msg.data;
int width = ctx->width;
int height = ctx->height;
int x = ctx->x;
int y = ctx->y;
- ctx->pid = msg->src;
- new_context(ctx, msg->src, x, y, width, height, ctx->flags);
+ ctx->pid = msg.src;
+ new_context(ctx, msg.src, x, y, width, height, ctx->flags);
list_add(contexts, ctx);
if (!(ctx->flags & WF_RELATIVE))
focused = ctx;
redraw_all();
- msg_send(msg->src, GFX_NEW_CONTEXT, ctx);
+ msg_send(msg.src, GFX_NEW_CONTEXT, ctx);
// Send mouse position
- struct gui_event_mouse *mouse = malloc(sizeof(*msg));
+ struct gui_event_mouse *mouse = malloc(sizeof(msg));
mouse->x = mouse_x - focused->x;
mouse->y = mouse_y - focused->y;
msg_send(focused->pid, GUI_MOUSE, mouse);
@@ -324,16 +324,14 @@ int main(int argc, char **argv)
redraw_focused();
break;
case EVENT_MOUSE:
- handle_mouse(msg->data);
+ handle_mouse(msg.data);
break;
case EVENT_KEYBOARD:
- handle_keyboard(msg->data);
+ handle_keyboard(msg.data);
break;
default:
break;
}
-
- free(msg);
};
return 0;
diff --git a/kernel/drivers/keyboard.c b/kernel/drivers/keyboard.c
index e8690f1..ac97d36 100644
--- a/kernel/drivers/keyboard.c
+++ b/kernel/drivers/keyboard.c
@@ -28,6 +28,8 @@ void keyboard_handler()
// TODO: "Merge" scancode to linux keycode?
/* printf("%x %x = %x\n", scancode, state ? 0xe0 : 0, merged); */
+ if (event)
+ free(event);
event = malloc(sizeof(*event));
event->magic = KEYBOARD_MAGIC;
event->press = (scancode & 0x80) == 0;
diff --git a/kernel/features/proc.c b/kernel/features/proc.c
index 76da36d..08f3b8e 100644
--- a/kernel/features/proc.c
+++ b/kernel/features/proc.c
@@ -98,14 +98,15 @@ void proc_send(struct proc *src, struct proc *dest, u32 type, void *data)
priority_proc = dest;
}
-struct proc_message *proc_receive(struct proc *proc)
+u32 proc_receive(struct proc *proc, struct message *buf)
{
if (proc->messages && proc->messages->head) {
struct proc_message *msg = proc->messages->head->data;
list_remove(proc->messages, proc->messages->head);
- return msg;
+ memcpy(buf, msg->msg, sizeof(*buf));
+ return 1;
} else {
- return NULL;
+ return 0;
}
}
diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c
index e2df79a..a6bfb2a 100644
--- a/kernel/features/syscall.c
+++ b/kernel/features/syscall.c
@@ -88,8 +88,7 @@ void syscall_handler(struct regs *r)
break;
}
case SYS_RECEIVE: {
- struct proc_message *msg = proc_receive(proc_current());
- r->eax = (u32)(msg ? msg->msg : NULL);
+ r->eax = proc_receive(proc_current(), (void *)r->ebx);
break;
}
case SYS_GETPID: {
diff --git a/kernel/inc/proc.h b/kernel/inc/proc.h
index 5fc217c..93d8d48 100644
--- a/kernel/inc/proc.h
+++ b/kernel/inc/proc.h
@@ -41,7 +41,7 @@ void proc_init(void);
void proc_print(void);
struct proc *proc_current(void);
void proc_send(struct proc *src, struct proc *dest, u32 type, void *data);
-struct proc_message *proc_receive(struct proc *proc);
+u32 proc_receive(struct proc *proc, struct message *buf);
struct proc *proc_from_pid(u32 pid);
void proc_exit(struct proc *proc, int status);
void proc_yield(struct regs *r);
diff --git a/libc/inc/mem.h b/libc/inc/mem.h
index 4750efb..a01c9a2 100644
--- a/libc/inc/mem.h
+++ b/libc/inc/mem.h
@@ -10,14 +10,14 @@ void free_debug(void *ptr, const char *file, int line, const char *func, const c
#define malloc(size) malloc_debug(size, __FILE__, __LINE__, __func__, #size)
#define free(ptr) free_debug(ptr, __FILE__, __LINE__, __func__, #ptr)
-// Huh
+/* void *_malloc(u32 size); */
+/* void _free(void *ptr); */
+/* #define malloc(size) _malloc(size) */
+/* #define free(ptr) _free(ptr) */
+
#ifdef kernel
void heap_init(u32 start);
-/* void *malloc(u32 size); */
-/* void free(void *ptr); */
#elif defined(userspace)
-/* void *malloc(u32 size); */
-/* void free(void *ptr); */
#else
#error "No lib target specified. Please use -Dkernel or -Duserspace"
#endif
diff --git a/libc/inc/sys.h b/libc/inc/sys.h
index 3145432..c846b4b 100644
--- a/libc/inc/sys.h
+++ b/libc/inc/sys.h
@@ -98,14 +98,13 @@ int sysv(enum sys num, ...);
#define event_unregister(id) sys1(SYS_UNREGISTER, (int)(id))
#define msg_send(pid, type, msg) sys3(SYS_SEND, (int)(pid), (int)(type), (int)(msg))
-#define msg_receive() (struct message *)sys0(SYS_RECEIVE)
+#define msg_receive(buf) (struct message *)sys1(SYS_RECEIVE, (int)(buf))
#define getpid() (int)sys0(SYS_GETPID)
-static inline struct message *msg_receive_loop()
+static inline struct message *msg_receive_loop(struct message *buf)
{
- struct message *msg;
- while (!(msg = msg_receive()))
+ while (!msg_receive(&buf))
yield();
- return msg;
+ return buf;
}
// Simple read wrapper
diff --git a/libc/mem.c b/libc/mem.c
index 0c0ad4d..17703d5 100644
--- a/libc/mem.c
+++ b/libc/mem.c
@@ -347,13 +347,24 @@ void _free(void *ptr)
void *malloc_debug(u32 size, const char *file, int line, const char *func, const char *inp)
{
- printf("MALLOC:\t%s:%d: %s: %dB (%s)\n", file, line, func, size, inp);
- return _malloc(size);
+ void *ret = _malloc(size);
+#ifdef kernel
+ printf("K");
+#else
+ printf("U");
+#endif
+ printf("MALLOC\t%s:%d: %s: 0x%x %dB (%s)\n", file, line, func, ret, size, inp);
+ return ret;
}
void free_debug(void *ptr, const char *file, int line, const char *func, const char *inp)
{
- printf("FREE:\t%s:%d: %s: 0x%x (%s)\n", file, line, func, ptr, inp);
+#ifdef kernel
+ printf("K");
+#else
+ printf("U");
+#endif
+ printf("FREE\t%s:%d: %s: 0x%x (%s)\n", file, line, func, ptr, inp);
if (ptr)
_free(ptr);
}
diff --git a/libgui/gfx.c b/libgui/gfx.c
index 3eb388a..b93ee62 100644
--- a/libgui/gfx.c
+++ b/libgui/gfx.c
@@ -96,6 +96,14 @@ static void draw_rectangle(struct context *ctx, int x1, int y1, int x2, int y2,
}
}
+struct context *gfx_new_ctx(struct context *ctx)
+{
+ struct message msg = { 0 };
+ msg_send(2, GFX_NEW_CONTEXT, ctx);
+ memcpy(ctx, msg_receive_loop(&msg)->data, sizeof(*ctx));
+ return ctx;
+}
+
// On-demand font loading
struct font *gfx_resolve_font(enum font_type font_type)
{
diff --git a/libgui/gui.c b/libgui/gui.c
index b675586..8ab6260 100644
--- a/libgui/gui.c
+++ b/libgui/gui.c
@@ -531,28 +531,28 @@ void gui_event_loop(struct element *container)
if (!container)
return;
- struct message *msg;
+ struct message msg = { 0 };
struct element *focused = NULL;
while (1) {
- if (!(msg = msg_receive())) {
+ if (!msg_receive(&msg)) {
yield();
continue;
}
- switch (msg->type) {
+ switch (msg.type) {
case GUI_KILL: {
remove_all();
exit(0);
}
case GUI_MOUSE: {
- struct gui_event_mouse *event = msg->data;
+ struct gui_event_mouse *event = msg.data;
focused = element_at(container, event->x, event->y);
if (focused && focused->event.on_click && event->but1)
focused->event.on_click(event, focused);
break;
}
case GUI_KEYBOARD: {
- struct gui_event_keyboard *event = msg->data;
+ struct gui_event_keyboard *event = msg.data;
if (focused && focused->type == GUI_TYPE_TEXT_INPUT && event->press) {
char *s = ((struct element_text_input *)focused->data)->text;
@@ -584,15 +584,13 @@ void gui_event_loop(struct element *container)
break;
}
case GUI_RESIZE: {
- struct gui_event_resize *event = msg->data;
+ struct gui_event_resize *event = msg.data;
struct element *root = get_root(container->window_id);
root->ctx = event->new_ctx;
gui_sync_window(container->window_id);
break;
}
}
-
- free(msg);
}
exit(1);
diff --git a/libgui/inc/gfx.h b/libgui/inc/gfx.h
index 0f4c631..d096005 100644
--- a/libgui/inc/gfx.h
+++ b/libgui/inc/gfx.h
@@ -64,6 +64,7 @@ struct context {
int flags;
};
+struct context *gfx_new_ctx(struct context *ctx);
struct font *gfx_resolve_font(enum font_type font_type);
void gfx_write_char(struct context *ctx, int x, int y, enum font_type font_type, u32 c, char ch);
void gfx_write(struct context *ctx, int x, int y, enum font_type font_type, u32 c,
@@ -83,8 +84,6 @@ int gfx_font_width(enum font_type);
* Wrappers
*/
-#define gfx_new_ctx(ctx) \
- (msg_send(2, GFX_NEW_CONTEXT, (ctx)), (struct context *)msg_receive_loop()->data)
#define gfx_redraw() (msg_send(2, GFX_REDRAW, NULL)) // TODO: Partial redraw (optimization)
#define gfx_redraw_focused() (msg_send(2, GFX_REDRAW_FOCUSED, NULL))