From 3b2298180adae2f6efc4f62ec17bcbd7395fcc90 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Mon, 17 Nov 2014 15:55:14 +0200 Subject: [PATCH] Allow binding commands to class instance --- .gitignore | 2 + include/cmdhelp/inverselist.hpp | 2 + include/core/controller.hpp | 20 ++- include/core/controllerframe.hpp | 9 +- include/core/debug.hpp | 10 +- include/core/framebuffer.hpp | 6 +- include/core/framerate.hpp | 8 +- include/core/inthread.hpp | 7 +- include/core/jukebox.hpp | 7 +- include/core/multitrack.hpp | 10 +- include/core/project.hpp | 23 ++-- include/core/subtitles.hpp | 11 +- include/library/command.hpp | 66 +++++++-- src/Makefile | 9 +- src/cmdhelp/Makefile | 39 ++++++ src/cmdhelp/button.json | 56 ++++++++ src/cmdhelp/commentary.json | 15 ++ src/cmdhelp/debug.json | 21 +++ src/cmdhelp/framebuffer.json | 7 + src/cmdhelp/jukebox.json | 56 ++++++++ src/cmdhelp/loadsave.json | 82 +++++++++++ src/cmdhelp/macro.json | 20 +++ src/cmdhelp/mhold.json | 22 +++ src/cmdhelp/mkstubs.cpp | 144 ++++++++++++++++++++ src/cmdhelp/mkstubsi.cpp | 77 +++++++++++ src/cmdhelp/multitrack.json | 19 +++ src/cmdhelp/project.json | 38 ++++++ src/cmdhelp/subtitles.json | 18 +++ src/cmdhelp/turbo.json | 22 +++ src/core/controller.cpp | 107 ++++----------- src/core/controllerframe.cpp | 9 +- src/core/debug.cpp | 127 +++++++++-------- src/core/framebuffer.cpp | 20 +-- src/core/framerate.cpp | 32 +---- src/core/instance.cpp | 18 +-- src/core/inthread.cpp | 20 +-- src/core/jukebox.cpp | 16 ++- src/core/mainloop.cpp | 161 +++------------------- src/core/multitrack.cpp | 85 ++++++------ src/core/project.cpp | 286 +++++++++++++++++++-------------------- src/core/reginverse.cpp | 29 ++++ src/core/subtitles.cpp | 129 +++++++++--------- src/library/command.cpp | 6 +- 43 files changed, 1221 insertions(+), 650 deletions(-) create mode 100644 include/cmdhelp/inverselist.hpp create mode 100644 src/cmdhelp/Makefile create mode 100644 src/cmdhelp/button.json create mode 100644 src/cmdhelp/commentary.json create mode 100644 src/cmdhelp/debug.json create mode 100644 src/cmdhelp/framebuffer.json create mode 100644 src/cmdhelp/jukebox.json create mode 100644 src/cmdhelp/loadsave.json create mode 100644 src/cmdhelp/macro.json create mode 100644 src/cmdhelp/mhold.json create mode 100644 src/cmdhelp/mkstubs.cpp create mode 100644 src/cmdhelp/mkstubsi.cpp create mode 100644 src/cmdhelp/multitrack.json create mode 100644 src/cmdhelp/project.json create mode 100644 src/cmdhelp/subtitles.json create mode 100644 src/cmdhelp/turbo.json create mode 100644 src/core/reginverse.cpp diff --git a/.gitignore b/.gitignore index cd2891a4..ddcb3313 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ lsnes /gambatte src/fonts/font.cpp src/core/version.cpp +src/cmdhelp/*.cpp +include/cmdhelp/*.hpp diff --git a/include/cmdhelp/inverselist.hpp b/include/cmdhelp/inverselist.hpp new file mode 100644 index 00000000..264467ba --- /dev/null +++ b/include/cmdhelp/inverselist.hpp @@ -0,0 +1,2 @@ +#pragma once +namespace STUBS { extern const char* inverse_cmd_list[]; } diff --git a/include/core/controller.hpp b/include/core/controller.hpp index 3a07a53a..ab329a24 100644 --- a/include/core/controller.hpp +++ b/include/core/controller.hpp @@ -4,6 +4,7 @@ #include #include #include "library/dispatch.hpp" +#include "library/command.hpp" struct project_info; struct controller_state; @@ -65,7 +66,7 @@ public: * Ctor. */ button_mapping(controller_state& _controls, keyboard::mapper& mapper, keyboard::keyboard& keyboard, - emu_framebuffer& fbuf, emulator_dispatch& _dispatch, lua_state& _lua2); + emu_framebuffer& fbuf, emulator_dispatch& _dispatch, lua_state& _lua2, command::group& _cmd); /** * Dtor. */ @@ -98,13 +99,10 @@ public: * Map of button keys. */ std::map button_keys; -/** - * Do button/axis action. - */ - void do_action(const std::string& name, short state, int mode); +private: void do_analog_action(const std::string& a); void do_autofire_action(const std::string& a, int mode); -private: + void do_action(const std::string& name, short state, int mode); void promote_key(keyboard::ctrlrkey& k); void add_button(const std::string& name, const controller_bind& binding); void process_controller(portctrl::controller& controller, unsigned number); @@ -129,7 +127,17 @@ private: emu_framebuffer& fbuf; emulator_dispatch& edispatch; lua_state& lua2; + command::group& cmd; struct dispatch::target<> ncore; + command::_fnptr button_p; + command::_fnptr button_r; + command::_fnptr button_h; + command::_fnptr button_t; + command::_fnptr button_d; + command::_fnptr button_ap; + command::_fnptr button_ar; + command::_fnptr button_at; + command::_fnptr button_a; }; diff --git a/include/core/controllerframe.hpp b/include/core/controllerframe.hpp index 23274831..9ba11f9d 100644 --- a/include/core/controllerframe.hpp +++ b/include/core/controllerframe.hpp @@ -16,6 +16,7 @@ #include #include #include "library/portctrl-data.hpp" +#include "library/command.hpp" #include "library/threads.hpp" class project_state; @@ -34,7 +35,7 @@ public: * Constructor. */ controller_state(project_state& _project, movie_logic& _mlogic, button_mapping& _buttons, - emulator_dispatch& _dispatch, status_updater& _supdater) throw(); + emulator_dispatch& _dispatch, status_updater& _supdater, command::group& _cmd) throw(); /** * Convert lcid (Logical Controller ID) into pcid (Physical Controler ID). * @@ -205,12 +206,12 @@ public: void set_macro(const std::string& macro, const portctrl::macro& m); void apply_macro(portctrl::frame& f); void rename_macro(const std::string& old, const std::string& newn); - void do_macro(const std::string& a, int mode); std::set active_macro_set(); void advance_macros(); std::map get_macro_frames(); void set_macro_frames(const std::map& f); private: + void do_macro(const std::string& a, int mode); struct autofire_info { uint64_t first_frame; @@ -240,6 +241,10 @@ private: button_mapping& buttons; emulator_dispatch& edispatch; status_updater& supdater; + command::group& cmd; + command::_fnptr macro_p; + command::_fnptr macro_r; + command::_fnptr macro_t; }; #endif diff --git a/include/core/debug.hpp b/include/core/debug.hpp index 513f8750..4b5e0f05 100644 --- a/include/core/debug.hpp +++ b/include/core/debug.hpp @@ -4,6 +4,7 @@ #include #include #include +#include "library/command.hpp" #include "library/dispatch.hpp" class emulator_dispatch; @@ -15,7 +16,7 @@ class loaded_rom; class debug_context { public: - debug_context(emulator_dispatch& _dispatch, loaded_rom& _rom); + debug_context(emulator_dispatch& _dispatch, loaded_rom& _rom, command::group& _cmd); /** * Type of event. */ @@ -154,14 +155,21 @@ public: std::map trace_cb; std::map frame_cb; private: + void do_showhooks(); + void do_genevent(const std::string& a); + void do_tracecmd(const std::string& a); cb_list dummy_cb; //Always empty. uint64_t xmask = 1; std::function tracelog_change_cb; emulator_dispatch& edispatch; loaded_rom& rom; + command::group& cmd; struct dispatch::target<> corechange; bool corechange_r = false; bool requesting_break = false; + command::_fnptr<> showhooks; + command::_fnptr genevent; + command::_fnptr tracecmd; struct tracelog_file : public callback_base { diff --git a/include/core/framebuffer.hpp b/include/core/framebuffer.hpp index 9a5e2282..9cd79fde 100644 --- a/include/core/framebuffer.hpp +++ b/include/core/framebuffer.hpp @@ -2,6 +2,7 @@ #define _framebuffer__hpp__included__ #include "core/window.hpp" +#include "library/command.hpp" #include "library/framebuffer.hpp" #include "library/triplebuffer.hpp" @@ -30,7 +31,7 @@ class emu_framebuffer public: emu_framebuffer(subtitle_commentary& _subtitles, settingvar::group& _settings, memwatch_set& _mwatch, keyboard::keyboard& _keyboard, emulator_dispatch& _dispatch, lua_state& _lua2, loaded_rom& _rom, - status_updater& _supdater); + status_updater& _supdater, command::group& _cmd); /** * The main framebuffer. */ @@ -83,6 +84,7 @@ public: framebuffer::raw& render_get_latest_screen(); void render_get_latest_screen_end(); private: + void do_screenshot(command::arg_filename a); struct render_info { framebuffer::raw fbuf; @@ -107,6 +109,8 @@ private: lua_state& lua2; loaded_rom& rom; status_updater& supdater; + command::group& cmd; + command::_fnptr screenshot; }; #endif diff --git a/include/core/framerate.hpp b/include/core/framerate.hpp index 82be8f5e..1b323b10 100644 --- a/include/core/framerate.hpp +++ b/include/core/framerate.hpp @@ -3,6 +3,7 @@ #include #include "library/threads.hpp" +#include "library/command.hpp" #define FRAMERATE_HISTORY_FRAMES 10 @@ -12,7 +13,7 @@ class framerate_regulator { public: - framerate_regulator(); + framerate_regulator(command::group& _cmd); /** * Set the target speed multiplier. * @@ -96,7 +97,10 @@ private: double nominal_framerate; double multiplier_framerate; threads::lock framerate_lock; - + command::group& cmd; + command::_fnptr<> turbo_p; + command::_fnptr<> turbo_r; + command::_fnptr<> turbo_t; }; #endif diff --git a/include/core/inthread.hpp b/include/core/inthread.hpp index af34bd8f..8befd89b 100644 --- a/include/core/inthread.hpp +++ b/include/core/inthread.hpp @@ -4,6 +4,7 @@ #include #include #include +#include "library/command.hpp" class emulator_dispatch; @@ -28,7 +29,8 @@ public: uint64_t length; }; - voice_commentary(settingvar::group& _settings, emulator_dispatch& _dispatch, audioapi_instance& _audio); + voice_commentary(settingvar::group& _settings, emulator_dispatch& _dispatch, audioapi_instance& _audio, + command::group& _cmd); ~voice_commentary(); void init(); void kill(); @@ -53,6 +55,9 @@ private: settingvar::group& settings; emulator_dispatch& edispatch; audioapi_instance& audio; + command::group& cmd; + command::_fnptr<> tangentp; + command::_fnptr<> tangentr; }; #endif diff --git a/include/core/jukebox.hpp b/include/core/jukebox.hpp index 191e09f1..2ee68470 100644 --- a/include/core/jukebox.hpp +++ b/include/core/jukebox.hpp @@ -18,7 +18,7 @@ public: /** * Ctor. */ - save_jukebox(settingvar::group& _settings); + save_jukebox(settingvar::group& _settings, command::group& _cmd); /** * Dtor. */ @@ -73,11 +73,16 @@ public: */ void unset_update(); private: + void do_slotsel(const std::string& arg); settingvar::group& settings; size_t current_slot; size_t current_size; std::function update; save_jukebox_listener* listener; + command::group& cmd; + command::_fnptr slotsel; + command::_fnptr<> cycleprev; + command::_fnptr<> cyclenext; }; #endif diff --git a/include/core/multitrack.hpp b/include/core/multitrack.hpp index 6321b964..c3658708 100644 --- a/include/core/multitrack.hpp +++ b/include/core/multitrack.hpp @@ -19,7 +19,7 @@ public: MT_XOR }; multitrack_edit(movie_logic& _mlogic, controller_state& _controls, emulator_dispatch& _dispatch, - status_updater& _supdater); + status_updater& _supdater, button_mapping& _buttons, command::group& _cmd); void enable(bool state); void set(unsigned port, unsigned controller, state s); void set_and_notify(unsigned port, unsigned controller, state s); @@ -30,6 +30,9 @@ public: void process_frame(portctrl::frame& input); bool any_records(); private: + void do_mt_fwd(); + void do_mt_bw(); + void do_mt_set(const std::string& args); threads::lock mlock; bool enabled; std::map, state> controllerstate; @@ -37,6 +40,11 @@ private: controller_state& controls; emulator_dispatch& edispatch; status_updater& supdater; + button_mapping& buttons; + command::group& cmd; + command::_fnptr<> mt_f; + command::_fnptr<> mt_b; + command::_fnptr mt_s; }; #endif diff --git a/include/core/project.hpp b/include/core/project.hpp index 2084cea5..916ab8fc 100644 --- a/include/core/project.hpp +++ b/include/core/project.hpp @@ -6,6 +6,7 @@ #include #include #include "core/rom-small.hpp" +#include "library/command.hpp" #include "library/json.hpp" class voice_commentary; @@ -15,14 +16,7 @@ class button_mapping; class emulator_dispatch; class input_queue; class status_updater; -namespace command -{ - class group; -} -namespace settingvar -{ - class cache; -} +namespace settingvar { class cache; } //A branch. struct project_branch_info @@ -226,6 +220,13 @@ public: */ void copy_macros(project_info& p, controller_state& s); private: + void recursive_list_branch(uint64_t bid, std::set& dset, unsigned depth, bool last_of); + void do_branch_ls(); + void do_branch_mk(const std::string& a); + void do_branch_rm(const std::string& a); + void do_branch_set(const std::string& a); + void do_branch_rp(const std::string& a); + void do_branch_mv(const std::string& a); project_info* active_project; voice_commentary& commentary; memwatch_set& mwatch; @@ -237,6 +238,12 @@ private: input_queue& iqueue; loaded_rom& rom; status_updater& supdater; + command::_fnptr<> branch_ls; + command::_fnptr branch_mk; + command::_fnptr branch_rm; + command::_fnptr branch_set; + command::_fnptr branch_rp; + command::_fnptr branch_mv; }; #endif diff --git a/include/core/subtitles.hpp b/include/core/subtitles.hpp index aca85444..cc9f4a6b 100644 --- a/include/core/subtitles.hpp +++ b/include/core/subtitles.hpp @@ -4,6 +4,7 @@ #include #include #include +#include "library/command.hpp" namespace lua { class render_context; } @@ -30,7 +31,8 @@ class emulator_dispatch; struct subtitle_commentary { public: - subtitle_commentary(movie_logic& _mlogic, emu_framebuffer& _fbuf, emulator_dispatch& _dispatch); + subtitle_commentary(movie_logic& _mlogic, emu_framebuffer& _fbuf, emulator_dispatch& _dispatch, + command::group& _cmd); std::set> get_all(); std::string get(uint64_t f, uint64_t l); void set(uint64_t f, uint64_t l, const std::string& x); @@ -38,9 +40,16 @@ public: static std::string s_escape(std::string x); void render(lua::render_context& ctx); private: + void do_editsub(const std::string& a); + void do_listsub(); + void do_savesub(const std::string& a); movie_logic& mlogic; emu_framebuffer& fbuf; emulator_dispatch& edispatch; + command::group& cmd; + command::_fnptr editsub; + command::_fnptr<> listsub; + command::_fnptr savesub; }; #endif diff --git a/include/library/command.hpp b/include/library/command.hpp index 42685116..b6612a19 100644 --- a/include/library/command.hpp +++ b/include/library/command.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -246,6 +247,25 @@ private: }; /** + * Name, Description and help for command. + */ +struct stub +{ +/** + * The command name. + */ + const char* name; +/** + * The short description. + */ + const char* desc; +/** + * The long help. + */ + const char* help; +}; + +/** * Mandatory filename */ struct arg_filename @@ -269,7 +289,7 @@ struct arg_filename * parameter a: The arguments to pass. */ template -void invoke_fn(void (*fn)(args... arguments), const std::string& a); +void invoke_fn(std::function fn, const std::string& a); /** * Warp function pointer as command. @@ -292,11 +312,26 @@ public: const std::string& _help, void (*_fn)(args... arguments), bool dynamic = false) throw(std::bad_alloc) : base(group, name, dynamic) { - description = _description; + shorthelp = _description; help = _help; fn = _fn; } /** + * Create a new command. + * + * parameter _set: The set command will be part of. + * parameter name: Name of the command + * parameter fn: Function to call on command. + * parameter description Description&Help for the command + */ + _fnptr(group& _group, stub _name, std::function _fn) throw(std::bad_alloc) + : base(_group, _name.name, false) + { + shorthelp = _name.desc; + help = _name.help; + fn = _fn; + } +/** * Destroy a commnad. */ ~_fnptr() throw() @@ -319,7 +354,7 @@ public: */ std::string get_short_help() throw(std::bad_alloc) { - return description; + return shorthelp; } /** * Get long help. @@ -332,8 +367,8 @@ public: return help; } private: - void (*fn)(args... arguments); - std::string description; + std::function fn; + std::string shorthelp; std::string help; }; @@ -348,6 +383,21 @@ public: * Create a new command. * * parameter _set: The set command will be part of. + * parameter desc: Command descriptor. + * parameter fn: Function to call on command. + */ + fnptr(set& _set, stub _name, void (*_fn)(args... arguments)) throw(std::bad_alloc) + { + shorthelp = _name.desc; + name = _name.name; + help = _name.help; + fn = _fn; + _factory_base(_set, name); + } +/** + * Create a new command. + * + * parameter _set: The set command will be part of. * parameter name: Name of the command * parameter description Description for the command * parameter help: Help for the command. @@ -356,7 +406,7 @@ public: fnptr(set& _set, const std::string& _name, const std::string& _description, const std::string& _help, void (*_fn)(args... arguments)) throw(std::bad_alloc) { - description = _description; + shorthelp = _description; name = _name; help = _help; fn = _fn; @@ -373,11 +423,11 @@ public: */ base* make(group& grp) throw(std::bad_alloc) { - return new _fnptr(grp, name, description, help, fn, true); + return new _fnptr(grp, name, shorthelp, help, fn, true); } private: void (*fn)(args... arguments); - std::string description; + std::string shorthelp; std::string help; std::string name; }; diff --git a/src/Makefile b/src/Makefile index d8634144..3a0db3d2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -23,7 +23,7 @@ CFLAGS += -DOPUS_SUPPORTS_SURROUND endif endif -COMMON_LIBRARY=core lua fonts library interface video emulation +COMMON_LIBRARY=core lua fonts library interface video emulation cmdhelp ALLFILES=__all__.files ALLFLAGS=__all__.ldflags COMMON_LIBRARY_FILES=$(patsubst %,%/$(ALLFILES),$(COMMON_LIBRARY)) @@ -37,7 +37,7 @@ __all_common__.files: $(COMMON_LIBRARY_FILES) __all_platform__.files: $(PLATFORM_LIBRARY_FILES) lua genfilelist.lua $^ >$@ -core/$(ALLFILES): forcelook +core/$(ALLFILES): forcelook cmdhelp/$(ALLFILES) $(MAKE) -C core emulation/$(ALLFILES): forcelook @@ -58,6 +58,9 @@ lua/$(ALLFILES): forcelook platform/$(ALLFILES): forcelook $(MAKE) -C platform +cmdhelp/$(ALLFILES): forcelook + $(MAKE) -C cmdhelp + util/__all_files__: forcelook $(MAKE) -C util @@ -85,6 +88,7 @@ precheck: $(MAKE) -C platform precheck $(MAKE) -C util precheck $(MAKE) -C video precheck + $(MAKE) -C cmdhelp precheck platclean: $(MAKE) -C emulation clean @@ -101,6 +105,7 @@ clean: $(MAKE) -C platform clean $(MAKE) -C util clean $(MAKE) -C video clean + $(MAKE) -C cmdhelp clean forcelook: @true diff --git a/src/cmdhelp/Makefile b/src/cmdhelp/Makefile new file mode 100644 index 00000000..afe2cf0b --- /dev/null +++ b/src/cmdhelp/Makefile @@ -0,0 +1,39 @@ +ALLFILES=__all__.files +ALLFLAGS=__all__.ldflags + +JSON_FILES=$(wildcard *.json) +JSON_SRC=$(patsubst %.json,%.cpp,$(JSON_FILES)) +JSON_OBJECTS=$(patsubst %.json,%.$(OBJECT_SUFFIX),$(JSON_FILES)) + +__all__.files: $(JSON_OBJECTS) inverselist.$(OBJECT_SUFFIX) + lua ../genfilelist.lua $^ >$@ + touch $(ALLFLAGS) + +mkstubs.exe: mkstubs.cpp ../library/json.cpp ../library/utf8.cpp ../library/string.cpp ../library/hex.cpp ../library/eatarg.cpp ../library/int24.cpp + $(HOSTCC) -g -std=gnu++0x -I../../include/library -o $@ $^ -lboost_regex$(HOST_BOOST_POSTFIX) -lboost_system$(HOST_BOOST_POSTFIX) -Wall + +mkstubsi.exe: mkstubsi.cpp ../library/json.cpp ../library/utf8.cpp ../library/string.cpp ../library/hex.cpp ../library/eatarg.cpp ../library/int24.cpp + $(HOSTCC) -g -std=gnu++0x -I../../include/library -o $@ $^ -lboost_regex$(HOST_BOOST_POSTFIX) -lboost_system$(HOST_BOOST_POSTFIX) -Wall + +inverselist.cpp: $(JSON_FILES) mkstubsi.exe + ./mkstubsi.exe $^ + +%.cpp: %.json mkstubs.exe + ./mkstubs.exe $< + mv *.hpp ../../include/cmdhelp + +%.$(OBJECT_SUFFIX): %.cpp + $(REALCC) $(CFLAGS) -c -o $@ $< -I../../include -Wall + + +.PRECIOUS: %.$(OBJECT_SUFFIX) %.files + +precheck: + @true + +clean: + rm -f *.$(OBJECT_SUFFIX) $(JSON_SRC) inverselist.cpp __all__.ldflags __all__.files + rm -f mkstubs.exe + +forcelook: + @true diff --git a/src/cmdhelp/button.json b/src/cmdhelp/button.json new file mode 100644 index 00000000..ebbb079e --- /dev/null +++ b/src/cmdhelp/button.json @@ -0,0 +1,56 @@ +{ + "btnp":{ + "__class":"button", + "__name":"+controller", + "__description":"Press a button", + "