terminal_chat_console.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Minetest
  3. Copyright (C) 2015 est31 <MTest31@outlook.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "chat.h"
  18. #include "threading/thread.h"
  19. #include "util/container.h"
  20. #include "log.h"
  21. #include <set>
  22. #include <sstream>
  23. struct ChatInterface;
  24. class TermLogOutput : public ILogOutput {
  25. public:
  26. void logRaw(LogLevel lev, const std::string &line)
  27. {
  28. queue.push_back(std::make_pair(lev, line));
  29. }
  30. virtual void log(LogLevel lev, const std::string &combined,
  31. const std::string &time, const std::string &thread_name,
  32. const std::string &payload_text)
  33. {
  34. std::ostringstream os(std::ios_base::binary);
  35. os << time << ": [" << thread_name << "] " << payload_text;
  36. queue.push_back(std::make_pair(lev, os.str()));
  37. }
  38. MutexedQueue<std::pair<LogLevel, std::string> > queue;
  39. };
  40. class TerminalChatConsole : public Thread {
  41. public:
  42. TerminalChatConsole() :
  43. Thread("TerminalThread")
  44. {}
  45. void setup(
  46. ChatInterface *iface,
  47. bool *kill_requested,
  48. const std::string &nick)
  49. {
  50. m_nick = nick;
  51. m_kill_requested = kill_requested;
  52. m_chat_interface = iface;
  53. }
  54. virtual void *run();
  55. // Highly required!
  56. void clearKillStatus() { m_kill_requested = nullptr; }
  57. void stopAndWaitforThread();
  58. private:
  59. // these have stupid names so that nobody missclassifies them
  60. // as curses functions. Oh, curses has stupid names too?
  61. // Well, at least it was worth a try...
  62. void initOfCurses();
  63. void deInitOfCurses();
  64. void draw_text();
  65. void typeChatMessage(const std::wstring &m);
  66. void handleInput(int ch, bool &complete_redraw_needed);
  67. void step(int ch);
  68. // Used to ensure the deinitialisation is always called.
  69. struct CursesInitHelper {
  70. TerminalChatConsole *cons;
  71. CursesInitHelper(TerminalChatConsole * a_console)
  72. : cons(a_console)
  73. { cons->initOfCurses(); }
  74. ~CursesInitHelper() { cons->deInitOfCurses(); }
  75. };
  76. int m_log_level = LL_ACTION;
  77. std::string m_nick;
  78. u8 m_utf8_bytes_to_wait = 0;
  79. std::string m_pending_utf8_bytes;
  80. std::set<std::string> m_nicks;
  81. int m_cols;
  82. int m_rows;
  83. bool m_can_draw_text;
  84. bool *m_kill_requested = nullptr;
  85. ChatBackend m_chat_backend;
  86. ChatInterface *m_chat_interface;
  87. TermLogOutput m_log_output;
  88. bool m_esc_mode = false;
  89. u64 m_game_time = 0;
  90. u32 m_time_of_day = 0;
  91. };
  92. extern TerminalChatConsole g_term_console;