From d16177c36d41c1141e580b71021efd98a514d9af Mon Sep 17 00:00:00 2001 From: Ivan Baravy Date: Tue, 31 Jan 2023 02:38:48 +0000 Subject: [PATCH] Implement common command buffer for the shell and os --- makefile | 11 +- shell.c | 316 ++++++++++++++++++++++++----------------- shell.h | 10 +- system.inc | 2 + umka.h | 51 +++++-- umka_fuse.c | 1 - umka_gen_devices_dat.c | 2 +- umka_os.c | 56 ++++---- umka_os.h | 51 ------- umka_os.us | 2 +- umka_shell.c | 3 +- util.c => umkart.c | 9 +- util.h => umkart.h | 5 + 13 files changed, 285 insertions(+), 234 deletions(-) delete mode 100644 umka_os.h rename util.c => umkart.c (95%) rename util.h => umkart.h (78%) diff --git a/makefile b/makefile index fbc61a6..9c69ff8 100644 --- a/makefile +++ b/makefile @@ -57,7 +57,7 @@ test: umka_shell umka_shell: umka_shell.o umka.o shell.o trace.o trace_lbr.o vdisk.o \ vdisk/raw.o vdisk/qcow2.o vdisk/miniz/miniz.a vnet.o lodepng.o \ - $(HOST)/pci.o $(HOST)/thread.o io.o $(HOST)/io_async.o util.o \ + $(HOST)/pci.o $(HOST)/thread.o io.o $(HOST)/io_async.o umkart.o \ optparse32.o bestline32.o $(CC) $(LDFLAGS_32) $^ -o $@ -T umka.ld @@ -68,11 +68,12 @@ umka_fuse: umka_fuse.o umka.o trace.o trace_lbr.o vdisk.o vdisk/raw.o \ umka_os: umka_os.o umka.o shell.o lodepng.o vdisk.o vdisk/raw.o vdisk/qcow2.o \ vdisk/miniz/miniz.a vnet.o trace.o trace_lbr.o $(HOST)/pci.o \ - $(HOST)/thread.o io.o $(HOST)/io_async.o util.o bestline32.o optparse32.o + $(HOST)/thread.o io.o $(HOST)/io_async.o umkart.o bestline32.o \ + optparse32.o $(CC) $(LDFLAGS_32) `sdl2-config --libs` $^ -o $@ -T umka.ld umka_gen_devices_dat: umka_gen_devices_dat.o umka.o $(HOST)/pci.o \ - $(HOST)/thread.o util.o + $(HOST)/thread.o umkart.o $(CC) $(LDFLAGS_32) $^ -o $@ -T umka.ld umka.o umka.fas: umka.asm @@ -108,7 +109,7 @@ optparse32.o: optparse.c optparse.h optparse.o: optparse.c optparse.h $(CC) $(CFLAGS) -c $< -o $@ -util.o: util.c util.h umka.h +umkart.o: umkart.c umkart.h umka.h $(CC) $(CFLAGS_32) -c $< default.skn: $(KOLIBRIOS)/skins/Leency/Shkvorka/default.asm colors.dtp @@ -168,7 +169,7 @@ umka_shell.o: umka_shell.c umka.h trace.h umka_fuse.o: umka_fuse.c umka.h $(CC) $(CFLAGS_32) `pkg-config fuse3 --cflags` -c $< -umka_os.o: umka_os.c umka.h umka_os.h +umka_os.o: umka_os.c umka.h $(CC) $(CFLAGS_32) `sdl2-config --cflags` -c $< umka_gen_devices_dat.o: umka_gen_devices_dat.c umka.h diff --git a/shell.c b/shell.c index 023dba4..fd32b62 100644 --- a/shell.c +++ b/shell.c @@ -8,18 +8,19 @@ Copyright (C) 2021 Magomed Kostoev */ -#include -#include -#include +#include +#include #include #include -#include +#include +#include #include +#include +#include +#include #include -#include -#include #include -#include +#include // TODO: Cleanup #ifndef _WIN32 @@ -32,7 +33,7 @@ #include "umka.h" #include "trace.h" #include "pci.h" -#include "util.h" +#include "umkart.h" #include "lodepng.h" #include "optparse.h" #include "bestline.h" @@ -57,6 +58,38 @@ typedef struct { void (*func) (struct shell_ctx *, int, char **); } func_table_t; +void +umka_run_cmd_sync(struct shell_ctx *ctx) { + struct umka_cmd *cmd = umka_cmd_buf; + if (atomic_load_explicit(&cmd->status, memory_order_acquire) != UMKA_CMD_STATUS_READY) { + fprintf(ctx->fout, "[!] command is not ready: %d: %u\n", cmd - umka_cmd_buf, cmd->type); + return; + } + switch (cmd->type) { + case UMKA_CMD_SET_MOUSE_DATA: { + struct cmd_set_mouse_data *c = &cmd->arg.set_mouse_data; + kos_set_mouse_data(c->btn_state, c->xmoving, c->ymoving, c->vscroll, + c->hscroll); + break; + } + default: + fprintf(ctx->fout, "[!] unknown command: %u\n", cmd->type); + break; + } + atomic_store_explicit(&cmd->status, UMKA_CMD_STATUS_DONE, + memory_order_release); + cnd_signal(&ctx->cmd_done); +} + +static void +umka_run_cmd(struct shell_ctx *ctx) { + if (atomic_load_explicit(ctx->running, memory_order_acquire)) { + cnd_wait(&ctx->cmd_done, &ctx->cmd_mutex); + } else { + umka_run_cmd_sync(ctx); + } +} + const char *f70_status_name[] = { "success", "disk_base", @@ -368,7 +401,7 @@ cmd_var(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: var <$name>\n" - " $name variable name, must start with $ sign"; + " $name variable name, must start with $ sign\n"; if (argc != 2) { fputs(usage, ctx->fout); return; @@ -384,7 +417,7 @@ cmd_send_scancode(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: send_scancode ...\n" - " code dec or hex number"; + " code dec or hex number\n"; if (argc < 2) { fputs(usage, ctx->fout); return; @@ -470,7 +503,7 @@ cmd_i40(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: i40 [ebx [ecx [edx [esi [edi [ebp]]]]]]...\n" - " see '/kernel/docs/sysfuncs.txt' for details"; + " see '/kernel/docs/sysfuncs.txt' for details\n"; if (argc < 2 || argc > 8) { fputs(usage, ctx->fout); return; @@ -577,7 +610,7 @@ cmd_ramdisk_init(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: ramdisk_init \n" - " absolute or relative path"; + " absolute or relative path\n"; if (argc != 2) { fputs(usage, ctx->fout); return; @@ -610,7 +643,7 @@ cmd_disk_add(struct shell_ctx *ctx, int argc, char **argv) { "usage: disk_add [option]...\n" " absolute or relative path\n" " disk name, e.g. hd0 or rd\n" - " -c cache size size of disk cache in bytes"; + " -c cache size size of disk cache in bytes\n"; if (argc < 3) { fputs(usage, ctx->fout); return; @@ -669,7 +702,7 @@ cmd_disk_del(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: disk_del \n" - " name disk name, i.e. rd or hd0"; + " name disk name, i.e. rd or hd0\n"; if (argc != 2) { fputs(usage, ctx->fout); return; @@ -683,7 +716,7 @@ static void cmd_pwd(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ - "usage: pwd"; + "usage: pwd\n"; if (argc != 1) { fputs(usage, ctx->fout); return; @@ -705,7 +738,7 @@ cmd_set_pixel(struct shell_ctx *ctx, int argc, char **argv) { " x x window coordinate\n" " y y window coordinate\n" " color argb in hex\n" - " -i inverted color"; + " -i inverted color\n"; if (argc < 4) { fputs(usage, ctx->fout); return; @@ -735,7 +768,7 @@ cmd_write_text(struct shell_ctx *ctx, int argc, char **argv) { " font_and_enc font size and string encoding\n" " draw_to_buf draw to the buffer pointed to by the next param\n" " length length of the string if it is non-asciiz\n" - " bg_color_or_buf argb or pointer"; + " bg_color_or_buf argb or pointer\n"; if (argc != 12) { fputs(usage, ctx->fout); return; @@ -763,7 +796,7 @@ cmd_get_key(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; (void)argv; const char *usage = \ - "usage: get_key"; + "usage: get_key\n"; if (argc > 1) { fputs(usage, ctx->fout); return; @@ -776,7 +809,7 @@ cmd_dump_key_buff(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: dump_key_buff [count]\n" - " count how many items to dump (all by default)"; + " count how many items to dump (all by default)\n"; if (argc > 2) { fputs(usage, ctx->fout); return; @@ -796,7 +829,7 @@ cmd_dump_win_stack(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: dump_win_stack [count]\n" - " count how many items to dump"; + " count how many items to dump\n"; if (argc > 2) { fputs(usage, ctx->fout); return; @@ -815,7 +848,7 @@ cmd_dump_win_pos(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: dump_win_pos [count]\n" - " count how many items to dump"; + " count how many items to dump\n"; if (argc < 1) { fputs(usage, ctx->fout); return; @@ -835,7 +868,7 @@ cmd_dump_win_map(struct shell_ctx *ctx, int argc, char **argv) { (void)argv; // TODO: area const char *usage = \ - "usage: dump_win_map"; + "usage: dump_win_map\n"; if (argc < 0) { fputs(usage, ctx->fout); return; @@ -855,7 +888,7 @@ cmd_dump_appdata(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: dump_appdata [-p]\n" " index index into appdata array to dump\n" - " -p print fields that are pointers"; + " -p print fields that are pointers\n"; if (argc < 2) { fputs(usage, ctx->fout); return; @@ -916,7 +949,7 @@ cmd_switch_to_thread(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: switch_to_thread \n" - " thread id to switch to"; + " thread id to switch to\n"; if (argc != 2) { fputs(usage, ctx->fout); return; @@ -931,7 +964,7 @@ cmd_get(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: get \n" - " variable to get"; + " variable to get\n"; if (argc != 2) { fputs(usage, ctx->fout); return; @@ -960,7 +993,7 @@ cmd_set(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: set \n" " variable to set\n" - " decimal or hex value"; + " decimal or hex value\n"; if (argc != 3) { fputs(usage, ctx->fout); return; @@ -994,7 +1027,7 @@ cmd_new_sys_thread(struct shell_ctx *ctx, int argc, char **argv) { (void)argv; // FIXME const char *usage = \ - "usage: new_sys_thread"; + "usage: new_sys_thread\n"; if (!argc) { fputs(usage, ctx->fout); return; @@ -1008,14 +1041,14 @@ cmd_mouse_move(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: mouse_move [-l] [-m] [-r] [-x {+|-|=}]" - "[-y {+|-|=}] [-h {+|-}] [-v {+|-}]\n" + " [-y {+|-|=}] [-h {+|-}] [-v {+|-}]\n" " -l left button is held\n" " -m middle button is held\n" " -r right button is held\n" " -x increase, decrease or set x coordinate\n" " -y increase, decrease or set y coordinate\n" " -h scroll horizontally\n" - " -v scroll vertically"; + " -v scroll vertically\n"; if (!argc) { fputs(usage, ctx->fout); return; @@ -1086,17 +1119,31 @@ cmd_mouse_move(struct shell_ctx *ctx, int argc, char **argv) { return; } } + uint32_t btn_state = lbheld + (rbheld << 1) + (mbheld << 2) + (yabs << 30) + + (xabs << 31); + struct umka_cmd *cmd = umka_cmd_buf; + struct cmd_set_mouse_data *c = &cmd->arg.set_mouse_data; + cmd->type = UMKA_CMD_SET_MOUSE_DATA; + c->btn_state = btn_state; + c->xmoving = xmoving; + c->ymoving = ymoving; + c->vscroll = vscroll; + c->hscroll = hscroll; + atomic_store_explicit(&cmd->status, UMKA_CMD_STATUS_READY, + memory_order_release); COVERAGE_ON(); - umka_mouse_move(lbheld, mbheld, rbheld, xabs, xmoving, yabs, ymoving, - hscroll, vscroll); + umka_run_cmd(ctx); COVERAGE_OFF(); + + atomic_store_explicit(&cmd->status, UMKA_CMD_STATUS_EMPTY, + memory_order_release); } static void cmd_process_info(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: process_info \n" - " pid process id to dump, -1 for self"; + " pid process id to dump, -1 for self\n"; if (argc != 2) { fputs(usage, ctx->fout); return; @@ -1128,7 +1175,7 @@ cmd_check_for_event(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; (void)argv; const char *usage = \ - "usage: check_for_event"; + "usage: check_for_event\n"; if (argc != 1) { fputs(usage, ctx->fout); return; @@ -1159,7 +1206,7 @@ cmd_display_number(struct shell_ctx *ctx, int argc, char **argv) { " font 0 = 6x9, 1 = 8x16\n" " draw_to_buf 0/1\n" " scale_factor 0 = x1, ..., 7 = x8\n" - " bg_color_or_buf depending on flags fill_bg and draw_to_buf"; + " bg_color_or_buf depending on flags fill_bg and draw_to_buf\n"; if (argc != 15) { fputs(usage, ctx->fout); return; @@ -1197,7 +1244,7 @@ cmd_set_window_colors(struct shell_ctx *ctx, int argc, char **argv) { "usage: set_window_colors " " " " \n" - " * all colors are in hex"; + " * all colors are in hex\n"; if (argc != (1 + sizeof(system_colors_t)/4)) { fputs(usage, ctx->fout); return; @@ -1223,7 +1270,7 @@ cmd_get_window_colors(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; (void)argv; const char *usage = \ - "usage: get_window_colors"; + "usage: get_window_colors\n"; if (argc != 1) { fputs(usage, ctx->fout); return; @@ -1251,7 +1298,7 @@ cmd_get_skin_height(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; (void)argv; const char *usage = \ - "usage: get_skin_height"; + "usage: get_skin_height\n"; if (argc != 1) { fputs(usage, ctx->fout); return; @@ -1267,7 +1314,7 @@ cmd_get_screen_area(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; (void)argv; const char *usage = \ - "usage: get_screen_area"; + "usage: get_screen_area\n"; if (argc != 1) { fputs(usage, ctx->fout); return; @@ -1290,7 +1337,7 @@ cmd_set_screen_area(struct shell_ctx *ctx, int argc, char **argv) { " left left x coord\n" " top top y coord\n" " right right x coord (not length!)\n" - " bottom bottom y coord"; + " bottom bottom y coord\n"; if (argc != 5) { fputs(usage, ctx->fout); return; @@ -1310,7 +1357,7 @@ cmd_get_skin_margins(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; (void)argv; const char *usage = \ - "usage: get_skin_margins"; + "usage: get_skin_margins\n"; if (argc != 1) { fputs(usage, ctx->fout); return; @@ -1330,7 +1377,7 @@ cmd_set_button_style(struct shell_ctx *ctx, int argc, char **argv) { (void)ctx; const char *usage = \ "usage: set_button_style