From 25ce0fdf9b05d0ec15f813dedd3e1e7ca7fa4fd2 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Wed, 12 Oct 2016 14:21:17 +0200 Subject: [PATCH] console in separate class (nw) --- scripts/src/mame/frontend.lua | 2 + src/frontend/mame/clifront.cpp | 96 +----------------------- src/frontend/mame/clifront.h | 1 - src/frontend/mame/console.cpp | 133 +++++++++++++++++++++++++++++++++ src/frontend/mame/console.h | 32 ++++++++ 5 files changed, 170 insertions(+), 94 deletions(-) create mode 100644 src/frontend/mame/console.cpp create mode 100644 src/frontend/mame/console.h diff --git a/scripts/src/mame/frontend.lua b/scripts/src/mame/frontend.lua index dbde7bfd3c2..93129cf85d9 100644 --- a/scripts/src/mame/frontend.lua +++ b/scripts/src/mame/frontend.lua @@ -61,6 +61,8 @@ files { MAME_DIR .. "src/frontend/mame/cheat.h", MAME_DIR .. "src/frontend/mame/clifront.cpp", MAME_DIR .. "src/frontend/mame/clifront.h", + MAME_DIR .. "src/frontend/mame/console.cpp", + MAME_DIR .. "src/frontend/mame/console.h", MAME_DIR .. "src/frontend/mame/info.cpp", MAME_DIR .. "src/frontend/mame/info.h", MAME_DIR .. "src/frontend/mame/language.cpp", diff --git a/src/frontend/mame/clifront.cpp b/src/frontend/mame/clifront.cpp index f0d1dde36f7..d7f9a7e76e6 100644 --- a/src/frontend/mame/clifront.cpp +++ b/src/frontend/mame/clifront.cpp @@ -29,10 +29,7 @@ #include "ui/moptions.h" #include "language.h" #include "pluginopts.h" - -#include "linenoise-ng/include/linenoise.h" -#include -#include +#include "console.h" #include #include @@ -291,95 +288,7 @@ void cli_frontend::start_execution(mame_machine_manager *manager,int argc, char m_result = manager->execute(); } } -/* -static const char* examples[] = { - "db", "hello", "hallo", "hans", "hansekogge", "seamann", "quetzalcoatl", "quit", "power", NULL -}; -void completionHook(char const* prefix, linenoiseCompletions* lc) { - size_t i; - - for (i = 0; examples[i] != NULL; ++i) { - if (strncmp(prefix, examples[i], strlen(prefix)) == 0) { - linenoiseAddCompletion(lc, examples[i]); - } - } -} -*/ - -void read_console(std::atomic& run, std::atomic& wait, std::string &cmdLine) -{ - char const* prompt = "\x1b[1;36m[MAME]\x1b[0m> "; - - while (run.load()) - { - while (wait.load()) - { - using namespace std::chrono_literals; - std::this_thread::sleep_for(100ms); - } - char* result = linenoise(prompt); - if (result == NULL) - { - continue; - } -/* else if (!strncmp(result, "/history", 8)) { - // Display the current history. - for (int index = 0; ; ++index) { - char* hist = linenoiseHistoryLine(index); - if (hist == NULL) break; - printf("%4d: %s\n", index, hist); - free(hist); - } - }*/ - else if (*result == '\0') { - free(result); - continue; - } - cmdLine = std::string(result); - linenoiseHistoryAdd(result); - //prompt = "\x1b[1;36m[MAME]\x1b[0m \x1b[1;32m[test]\x1b[0m> "; - - free(result); - wait.store(true); - } -} - -void cli_frontend::start_console() -{ - linenoiseInstallWindowChangeHandler(); - std::string cmdLine = ""; - const char* file = "./history"; - - linenoiseHistoryLoad(file); - //linenoiseSetCompletionCallback(completionHook); - printf(" _/ _/ _/_/ _/ _/ _/_/_/_/\n"); - printf(" _/_/ _/_/ _/ _/ _/_/ _/_/ _/ \n"); - printf(" _/ _/ _/ _/_/_/_/ _/ _/ _/ _/_/_/ \n"); - printf(" _/ _/ _/ _/ _/ _/ _/ \n"); - printf("_/ _/ _/ _/ _/ _/ _/_/_/_/ \n"); - printf("\n"); - printf("%s v%s\n%s\n\n", emulator_info::get_appname(), build_version, emulator_info::get_copyright_info()); - - std::atomic run(true), wait(false); - std::thread cinThread(read_console, std::ref(run), std::ref(wait), std::ref(cmdLine)); - - while (run.load()) - { - if (wait.load()) - { - //printf("command %s\n", cmdLine.c_str()); - cmdLine.clear(); - wait.store(false); - } - } - - run.store(false); - cinThread.join(); - - linenoiseHistorySave(file); - linenoiseHistoryFree(); -} //------------------------------------------------- // execute - execute a game via the standard // command line interface @@ -405,7 +314,8 @@ int cli_frontend::execute(int argc, char **argv) if (m_options.console()) { //manager->lua()->start_console(); - start_console(); + console_frontend console(m_options, m_osd); + console.start_console(); } else { start_execution(manager, argc, argv, option_errors); } diff --git a/src/frontend/mame/clifront.h b/src/frontend/mame/clifront.h index aeceeace397..4e3958cd5c3 100644 --- a/src/frontend/mame/clifront.h +++ b/src/frontend/mame/clifront.h @@ -62,7 +62,6 @@ private: void display_suggestions(const char *gamename); void output_single_softlist(FILE *out, software_list_device &swlist); void start_execution(mame_machine_manager *manager, int argc, char **argv, std::string &option_errors); - void start_console(); // internal state emu_options & m_options; diff --git a/src/frontend/mame/console.cpp b/src/frontend/mame/console.cpp new file mode 100644 index 00000000000..e1b7e5e7853 --- /dev/null +++ b/src/frontend/mame/console.cpp @@ -0,0 +1,133 @@ +// license:BSD-3-Clause +// copyright-holders:Miodrag Milanovic +/*************************************************************************** + + console.c + + Console interface frontend for MAME. + +***************************************************************************/ + +#include "emu.h" +#include "console.h" +#include "linenoise-ng/include/linenoise.h" +#include "mame.h" + +#include +#include + + +//------------------------------------------------- +// console_frontend - constructor +//------------------------------------------------- + +console_frontend::console_frontend(emu_options &options, osd_interface &osd) + : m_options(options), + m_osd(osd) +{ +} + + +//------------------------------------------------- +// ~console_frontend - destructor +//------------------------------------------------- + +console_frontend::~console_frontend() +{ +} + + +/* +static const char* examples[] = { + "db", "hello", "hallo", "hans", "hansekogge", "seamann", "quetzalcoatl", "quit", "power", NULL +}; + +void completionHook(char const* prefix, linenoiseCompletions* lc) { + size_t i; + + for (i = 0; examples[i] != NULL; ++i) { + if (strncmp(prefix, examples[i], strlen(prefix)) == 0) { + linenoiseAddCompletion(lc, examples[i]); + } + } +} +*/ + +static void read_console(std::atomic& run, std::atomic& wait, std::string &cmdLine) +{ + char const* prompt = "\x1b[1;36m[MAME]\x1b[0m> "; + + while (run.load()) + { + while (wait.load()) + { + using namespace std::chrono_literals; + std::this_thread::sleep_for(100ms); + } + char* result = linenoise(prompt); + if (result == NULL) + { + continue; + } +/* else if (!strncmp(result, "/history", 8)) { + // Display the current history. + for (int index = 0; ; ++index) { + char* hist = linenoiseHistoryLine(index); + if (hist == NULL) break; + printf("%4d: %s\n", index, hist); + free(hist); + } + }*/ + else if (*result == '\0') { + free(result); + continue; + } + cmdLine = std::string(result); + linenoiseHistoryAdd(result); + //prompt = "\x1b[1;36m[MAME]\x1b[0m \x1b[1;32m[test]\x1b[0m> "; + + free(result); + wait.store(true); + } +} + +void console_frontend::start_console() +{ + linenoiseInstallWindowChangeHandler(); + std::string cmdLine = ""; + const char* file = "./history"; + + linenoiseHistoryLoad(file); + //linenoiseSetCompletionCallback(completionHook); + + // Display app info + printf(" _/ _/ _/_/ _/ _/ _/_/_/_/\n"); + printf(" _/_/ _/_/ _/ _/ _/_/ _/_/ _/ \n"); + printf(" _/ _/ _/ _/_/_/_/ _/ _/ _/ _/_/_/ \n"); + printf(" _/ _/ _/ _/ _/ _/ _/ \n"); + printf("_/ _/ _/ _/ _/ _/ _/_/_/_/ \n"); + printf("\n"); + printf("%s v%s\n%s\n\n", emulator_info::get_appname(), build_version, emulator_info::get_copyright_info()); + + std::atomic run(true), wait(false); + std::thread cinThread(read_console, std::ref(run), std::ref(wait), std::ref(cmdLine)); + + while (run.load()) + { + if (wait.load()) + { + //printf("command %s\n", cmdLine.c_str()); + cmdLine.clear(); + wait.store(false); + } else { + using namespace std::chrono_literals; + std::this_thread::sleep_for(100ms); + } + } + + run.store(false); + cinThread.join(); + + linenoiseHistorySave(file); + linenoiseHistoryFree(); +} diff --git a/src/frontend/mame/console.h b/src/frontend/mame/console.h new file mode 100644 index 00000000000..4b3d2c40057 --- /dev/null +++ b/src/frontend/mame/console.h @@ -0,0 +1,32 @@ +// license:BSD-3-Clause +// copyright-holders:Miodrag Milanovic +/*************************************************************************** + + console.c + + Console interface frontend for MAME. + +***************************************************************************/ +#ifndef MAME_FRONTEND_CONSOLE_H +#define MAME_FRONTEND_CONSOLE_H + +#pragma once + +#include "emu.h" + +class console_frontend +{ +public: + // construction/destruction + console_frontend(emu_options &options, osd_interface &osd); + ~console_frontend(); + + void start_console(); + +private: + // internal state + emu_options & m_options; + osd_interface & m_osd; +}; + +#endif /* MAME_FRONTEND_CONSOLE_H */