From fa970fc8fa461678adfc9d9987de5ebe826915bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 1 Aug 2011 04:40:23 +0200 Subject: [PATCH] Hotkey commands work with TEXT mode (limited). --- library/Core.cpp | 37 ++++++++++++++++++++++++++++++++ library/FakeSDL-linux.cpp | 24 +++++++++++++++++++++ library/include/dfhack/Core.h | 4 ++++ library/include/dfhack/FakeSDL.h | 2 ++ 4 files changed, 67 insertions(+) diff --git a/library/Core.cpp b/library/Core.cpp index f26a37a9b..bbc473a58 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -532,6 +532,43 @@ int Core::Shutdown ( void ) return -1; } +// from ncurses +#define KEY_F0 0410 /* Function keys. Space for 64 */ +#define KEY_F(n) (KEY_F0+(n)) /* Value of function key n */ + +bool Core::ncurses_wgetch(int in, int & out) +{ + if(!started) + { + out = in; + return true; + } + if(in >= KEY_F(1) && in <= KEY_F(8)) + { + int idx = in - KEY_F(1); + // FIXME: copypasta, push into a method! + Gui * g = getGui(); + if(g->hotkeys && g->df_interface && g->df_menu_state) + { + t_viewscreen * ws = g->GetCurrentScreen(); + // FIXME: put hardcoded values into memory.xml + if(ws->getClassName() == "viewscreen_dwarfmodest" && *g->df_menu_state == 0x23) + { + out = in; + return true; + } + else + { + t_hotkey & hotkey = (*g->hotkeys)[idx]; + setHotkeyCmd(hotkey.name); + return false; + } + } + } + out = in; + return true; +} + int Core::SDL_Event(SDL::Event* ev, int orig_return) { // do NOT process events before we are ready. diff --git a/library/FakeSDL-linux.cpp b/library/FakeSDL-linux.cpp index 8409a2b1e..4f1fde0ad 100644 --- a/library/FakeSDL-linux.cpp +++ b/library/FakeSDL-linux.cpp @@ -104,6 +104,30 @@ DFhackCExport int SDL_PollEvent(SDL::Event* event) return orig_return; } +struct WINDOW; +DFhackCExport int wgetch(WINDOW *win) +{ + static int (*_wgetch)(WINDOW * win) = (int (*)( WINDOW * )) dlsym(RTLD_NEXT, "wgetch"); + if(!_wgetch) + { + exit(EXIT_FAILURE); + } + DFHack::Core & c = DFHack::Core::getInstance(); + wgetch_again: + int in = _wgetch(win); + int out; + if(c.ncurses_wgetch(in, out)) + { + // not consumed, give to DF + return out; + } + else + { + // consumed, repeat + goto wgetch_again; + } +} + // hook - called at program start, initialize some stuffs we'll use later static int (*_SDL_Init)(uint32_t flags) = 0; DFhackCExport int SDL_Init(uint32_t flags) diff --git a/library/include/dfhack/Core.h b/library/include/dfhack/Core.h index 8b4cf4991..9d5b1fe5a 100644 --- a/library/include/dfhack/Core.h +++ b/library/include/dfhack/Core.h @@ -33,6 +33,8 @@ distribution. #include #include "dfhack/Console.h" +struct WINDOW; + namespace tthread { class mutex; @@ -77,6 +79,7 @@ namespace DFHack friend void ::SDL_Quit(void); friend int ::SDL_PollEvent(SDL::Event *); friend int ::SDL_Init(uint32_t flags); + friend int ::wgetch(WINDOW * w); public: /// Get the single Core instance or make one. static Core& getInstance() @@ -132,6 +135,7 @@ namespace DFHack int Update (void); int Shutdown (void); int SDL_Event(SDL::Event* event, int orig_return); + bool ncurses_wgetch(int in, int & out); Core(Core const&); // Don't Implement void operator=(Core const&); // Don't implement bool errorstate; diff --git a/library/include/dfhack/FakeSDL.h b/library/include/dfhack/FakeSDL.h index d14ae2734..0f3ced545 100644 --- a/library/include/dfhack/FakeSDL.h +++ b/library/include/dfhack/FakeSDL.h @@ -37,6 +37,7 @@ distribution. typedef void * fPtr; typedef void * vPtr; +struct WINDOW; namespace SDL { union Event; @@ -66,5 +67,6 @@ DFhackCExport int SDL_NumJoysticks(void); DFhackCExport void SDL_Quit(void); DFhackCExport int SDL_PollEvent(SDL::Event* event); DFhackCExport int SDL_Init(uint32_t flags); +DFhackCExport int wgetch(WINDOW * win); // Other crud is in the OS-specific core files.