From 29aa5fda5734b83ac9148408aba34b8bdda10fa6 Mon Sep 17 00:00:00 2001 From: Nesbox Date: Wed, 26 May 2021 13:56:20 +0300 Subject: [PATCH] #1247: Added headless mode to run console commands (use `--cli` argument), also fixed batch commands processing --- src/studio/screens/console.c | 35 ++++++----- src/studio/studio.c | 5 ++ src/studio/studio.h | 1 + src/studio/system.h | 1 + src/system/sdl/main.c | 110 ++++++++++++++++++----------------- src/system/sokol/sokol.c | 18 ++++-- 6 files changed, 99 insertions(+), 71 deletions(-) diff --git a/src/studio/screens/console.c b/src/studio/screens/console.c index 7416a7b31..15c75e146 100644 --- a/src/studio/screens/console.c +++ b/src/studio/screens/console.c @@ -2012,7 +2012,7 @@ static void onExport_sfx(Console* console, const char* param, const char* name, if(params.id >= 0 && params.id < SFX_COUNT) error = studioExportSfx(params.id, filename) == NULL; - onFileExported(console, filename, error); + onFileExported(console, filename, !error); } static void onExport_music(Console* console, const char* type, const char* name, ExportParams params) @@ -2023,7 +2023,7 @@ static void onExport_music(Console* console, const char* type, const char* name, if(params.id >= 0 && params.id < MUSIC_TRACKS) error = studioExportMusic(params.id, filename) == NULL; - onFileExported(console, filename, error); + onFileExported(console, filename, !error); } static void onExport_screen(Console* console, const char* param, const char* name, ExportParams params) @@ -2941,7 +2941,9 @@ static void onHelp_commands(Console* console) static void printTable(Console* console, const char* text) { +#ifndef BAREMETALPI printf("%s", text); +#endif for(const char* textPointer = text, *endText = textPointer + strlen(text); textPointer != endText;) { @@ -3087,8 +3089,6 @@ static void processCommand(Console* console, const char* text) { const char* command = console->desc->command; - printf("%s", console->desc->command); - FOR(const Command*, cmd, Commands) if(casecmp(console->desc->command, cmd->name) == 0 || (cmd->alt && casecmp(console->desc->command, cmd->alt) == 0)) @@ -3111,7 +3111,7 @@ static void processCommand(Console* console, const char* text) static void processCommands(Console* console) { - char* command = strdup(console->args.cmd); + char* command = console->args.cmd; static const char Sep[] = " & "; char* next = strstr(command, Sep); @@ -3125,8 +3125,6 @@ static void processCommands(Console* console) printFront(console, command); processCommand(console, command); - - free(command); } static void fillHistory(Console* console) @@ -3182,6 +3180,7 @@ static void processConsoleCommand(Console* console) if(commandSize) { + printf(console->input.text); appendHistory(console, console->input.text); processCommand(console, console->input.text); } @@ -3520,12 +3519,15 @@ static void tick(Console* console) { loadDemo(console, 0); - printBack(console, "\n hello! type "); - printFront(console, "help"); - printBack(console, " for help\n"); + if(!console->args.skip) + { + printBack(console, "\n hello! type "); + printFront(console, "help"); + printBack(console, " for help\n"); - if(getConfig()->checkNewVersion) - tic_net_get(console->net, "/api?fn=version", onHttpVesrsionGet, console); + if(getConfig()->checkNewVersion) + tic_net_get(console->net, "/api?fn=version", onHttpVesrsionGet, console); + } commandDone(console); } @@ -3567,8 +3569,13 @@ static void tick(Console* console) drawCursor(console); - if(console->active && console->args.cmd) - processCommands(console); + if(console->active) + { + if(console->args.cmd) + processCommands(console); + else if(getConfig()->cli) + exitStudio(); + } } console->tickCounter++; diff --git a/src/studio/studio.c b/src/studio/studio.c index b3cf6fe3a..0c0fa6d46 100644 --- a/src/studio/studio.c +++ b/src/studio/studio.c @@ -2060,6 +2060,7 @@ static StartArgs parseArgs(s32 argc, char **argv) OPT_HELP(), OPT_BOOLEAN('\0', "skip", &args.skip, "skip startup animation"), OPT_BOOLEAN('\0', "nosound", &args.nosound, "disable sound output"), + OPT_BOOLEAN('\0', "cli", &args.cli, "console only output"), OPT_BOOLEAN('\0', "fullscreen", &args.fullscreen, "enable fullscreen mode"), OPT_STRING('\0', "fs", &args.fs, "path to the file system folder"), OPT_INTEGER('\0', "scale", &args.scale, "main window scale"), @@ -2145,6 +2146,7 @@ Studio* studioInit(s32 argc, char **argv, s32 samplerate, const char* folder) impl.config->data.goFullscreen = args.fullscreen; impl.config->data.noSound = args.nosound; + impl.config->data.cli = args.cli; impl.studio.tick = studioTick; impl.studio.close = studioClose; @@ -2152,6 +2154,9 @@ Studio* studioInit(s32 argc, char **argv, s32 samplerate, const char* folder) impl.studio.exit = exitStudio; impl.studio.config = getConfig; + if(args.cli) + args.skip = true; + if(args.skip) setStudioMode(TIC_CONSOLE_MODE); diff --git a/src/studio/studio.h b/src/studio/studio.h index 7abde72e0..b908a2ba7 100644 --- a/src/studio/studio.h +++ b/src/studio/studio.h @@ -75,6 +75,7 @@ typedef struct { bool skip; bool nosound; + bool cli; bool fullscreen; s32 scale; char *fs; diff --git a/src/studio/system.h b/src/studio/system.h index 40eff1319..fbee812c0 100644 --- a/src/studio/system.h +++ b/src/studio/system.h @@ -114,6 +114,7 @@ typedef struct bool checkNewVersion; bool noSound; + bool cli; #if defined(CRT_SHADER_SUPPORT) bool crtMonitor; diff --git a/src/system/sdl/main.c b/src/system/sdl/main.c index 8a38a1000..35f0d2ed1 100644 --- a/src/system/sdl/main.c +++ b/src/system/sdl/main.c @@ -1575,87 +1575,93 @@ static void createMouseCursors() static s32 start(s32 argc, char **argv, const char* folder) { - SDL_SetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, "1"); - SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); - - SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK); - - initSound(); + DEFER(platform.studio = studioInit(argc, argv, TIC80_SAMPLERATE, folder), platform.studio->close()) + { + if (platform.studio->config()->cli) + { + while (!platform.studio->quit) + platform.studio->tick(); + } + else + { + SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK); - platform.studio = studioInit(argc, argv, platform.audio.spec.freq, folder); + initSound(); - { - const s32 Width = TIC80_FULLWIDTH * platform.studio->config()->uiScale; - const s32 Height = TIC80_FULLHEIGHT * platform.studio->config()->uiScale; + { + const s32 Width = TIC80_FULLWIDTH * platform.studio->config()->uiScale; + const s32 Height = TIC80_FULLHEIGHT * platform.studio->config()->uiScale; - platform.window = SDL_CreateWindow( TIC_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - Width, Height, SDL_WINDOW_SHOWN - | SDL_WINDOW_RESIZABLE + platform.window = SDL_CreateWindow( TIC_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + Width, Height, SDL_WINDOW_SHOWN + | SDL_WINDOW_RESIZABLE #if defined(CRT_SHADER_SUPPORT) - | SDL_WINDOW_OPENGL + | SDL_WINDOW_OPENGL #endif #if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__) - | SDL_WINDOW_ALLOW_HIGHDPI + | SDL_WINDOW_ALLOW_HIGHDPI #endif - ); + ); - setWindowIcon(); - createMouseCursors(); + setWindowIcon(); + createMouseCursors(); - initGPU(); + initGPU(); - if(platform.studio->config()->goFullscreen) - tic_sys_fullscreen(); - } + if(platform.studio->config()->goFullscreen) + tic_sys_fullscreen(); + } - SDL_PauseAudioDevice(platform.audio.device, 0); + SDL_PauseAudioDevice(platform.audio.device, 0); #if defined(__EMSCRIPTEN__) - emscripten_set_main_loop(emsGpuTick, 0, 1); + emscripten_set_main_loop(emsGpuTick, 0, 1); #else - { - u64 nextTick = SDL_GetPerformanceCounter(); - const u64 Delta = SDL_GetPerformanceFrequency() / TIC80_FRAMERATE; + { + u64 nextTick = SDL_GetPerformanceCounter(); + const u64 Delta = SDL_GetPerformanceFrequency() / TIC80_FRAMERATE; - while (!platform.studio->quit) - { - nextTick += Delta; - - gpuTick(); + while (!platform.studio->quit) + { + nextTick += Delta; + + gpuTick(); - { - s64 delay = nextTick - SDL_GetPerformanceCounter(); + { + s64 delay = nextTick - SDL_GetPerformanceCounter(); - if(delay < 0) - nextTick -= delay; - else - SDL_Delay((u32)(delay * 1000 / SDL_GetPerformanceFrequency())); + if(delay < 0) + nextTick -= delay; + else + SDL_Delay((u32)(delay * 1000 / SDL_GetPerformanceFrequency())); + } + } } - } - } #endif #if defined(TOUCH_INPUT_SUPPORT) - if(SDL_IsTextInputActive()) - SDL_StopTextInput(); + if(SDL_IsTextInputActive()) + SDL_StopTextInput(); #endif - platform.studio->close(); - - { - destroyGPU(); + { + destroyGPU(); #if defined(TOUCH_INPUT_SUPPORT) - if(platform.gamepad.touch.pixels) - SDL_free(platform.gamepad.touch.pixels); + if(platform.gamepad.touch.pixels) + SDL_free(platform.gamepad.touch.pixels); #endif - SDL_DestroyWindow(platform.window); - SDL_CloseAudioDevice(platform.audio.device); + SDL_DestroyWindow(platform.window); + SDL_CloseAudioDevice(platform.audio.device); - for(s32 i = 0; i < COUNT_OF(platform.mouse.cursors); i++) - SDL_FreeCursor(platform.mouse.cursors[i]); + for(s32 i = 0; i < COUNT_OF(platform.mouse.cursors); i++) + SDL_FreeCursor(platform.mouse.cursors[i]); + } + + } } return 0; diff --git a/src/system/sokol/sokol.c b/src/system/sokol/sokol.c index 80993a937..2891dca38 100644 --- a/src/system/sokol/sokol.c +++ b/src/system/sokol/sokol.c @@ -170,11 +170,11 @@ static void app_frame(void) sokol_gfx_draw(platform.studio->tic->screen); s32 count = tic->samples.size / sizeof tic->samples.buffer[0]; - for(s32 i = 0; i < count; i++) - platform.audio.samples[i] = (float)tic->samples.buffer[i] / SHRT_MAX; + for(s32 i = 0; i < count; i++) + platform.audio.samples[i] = (float)tic->samples.buffer[i] / SHRT_MAX; - saudio_push(platform.audio.samples, count / 2); - + saudio_push(platform.audio.samples, count / 2); + input->mouse.scrollx = input->mouse.scrolly = 0; platform.keyboard.text = '\0'; } @@ -404,7 +404,15 @@ sapp_desc sokol_main(s32 argc, char* argv[]) platform.audio.desc.num_channels = TIC_STEREO_CHANNELS; saudio_setup(&platform.audio.desc); - platform.studio = studioInit(argc, (const char**)argv, saudio_sample_rate(), "./"); + platform.studio = studioInit(argc, argv, saudio_sample_rate(), "./"); + + if(platform.studio->config()->cli) + { + while (!platform.studio->quit) + platform.studio->tick(); + + exit(0); + } const s32 Width = TIC80_FULLWIDTH * platform.studio->config()->uiScale; const s32 Height = TIC80_FULLHEIGHT * platform.studio->config()->uiScale;