]> git.lizzy.rs Git - minetest.git/blob - src/terminal_chat_console.h
[CSM] Add flavour limits controlled by server (#5930)
[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 "chat_interface.h"
26 #include "log.h"
27
28 #include <sstream>
29
30 class TermLogOutput : public ILogOutput {
31 public:
32
33         void logRaw(LogLevel lev, const std::string &line)
34         {
35                 queue.push_back(std::make_pair(lev, line));
36         }
37
38         virtual void log(LogLevel lev, const std::string &combined,
39                 const std::string &time, const std::string &thread_name,
40                 const std::string &payload_text)
41         {
42                 std::ostringstream os(std::ios_base::binary);
43                 os << time << ": [" << thread_name << "] " << payload_text;
44
45                 queue.push_back(std::make_pair(lev, os.str()));
46         }
47
48         MutexedQueue<std::pair<LogLevel, std::string> > queue;
49 };
50
51 class TerminalChatConsole : public Thread {
52 public:
53
54         TerminalChatConsole() :
55                 Thread("TerminalThread")
56         {}
57
58         void setup(
59                 ChatInterface *iface,
60                 bool *kill_requested,
61                 const std::string &nick)
62         {
63                 m_nick = nick;
64                 m_kill_requested = kill_requested;
65                 m_chat_interface = iface;
66         }
67
68         virtual void *run();
69
70         // Highly required!
71         void clearKillStatus() { m_kill_requested = nullptr; }
72
73         void stopAndWaitforThread();
74
75 private:
76         // these have stupid names so that nobody missclassifies them
77         // as curses functions. Oh, curses has stupid names too?
78         // Well, at least it was worth a try...
79         void initOfCurses();
80         void deInitOfCurses();
81
82         void draw_text();
83
84         void typeChatMessage(const std::wstring &m);
85
86         void handleInput(int ch, bool &complete_redraw_needed);
87
88         void step(int ch);
89
90         // Used to ensure the deinitialisation is always called.
91         struct CursesInitHelper {
92                 TerminalChatConsole *cons;
93                 CursesInitHelper(TerminalChatConsole * a_console)
94                         : cons(a_console)
95                 { cons->initOfCurses(); }
96                 ~CursesInitHelper() { cons->deInitOfCurses(); }
97         };
98
99         int m_log_level = LL_ACTION;
100         std::string m_nick;
101
102         u8 m_utf8_bytes_to_wait = 0;
103         std::string m_pending_utf8_bytes;
104
105         std::list<std::string> m_nicks;
106
107         int m_cols;
108         int m_rows;
109         bool m_can_draw_text;
110
111         bool *m_kill_requested = nullptr;
112         ChatBackend m_chat_backend;
113         ChatInterface *m_chat_interface;
114
115         TermLogOutput m_log_output;
116
117         bool m_esc_mode = false;
118
119         u64 m_game_time = 0;
120         u32 m_time_of_day = 0;
121 };
122
123 extern TerminalChatConsole g_term_console;
124
125 #endif