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