Performance fix + SAO factorization
[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 "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                 m_log_level(LL_ACTION),
57                 m_utf8_bytes_to_wait(0),
58                 m_kill_requested(NULL),
59                 m_esc_mode(false),
60                 m_game_time(0),
61                 m_time_of_day(0)
62         {}
63
64         void setup(
65                 ChatInterface *iface,
66                 bool *kill_requested,
67                 const std::string &nick)
68         {
69                 m_nick = nick;
70                 m_kill_requested = kill_requested;
71                 m_chat_interface = iface;
72         }
73
74         virtual void *run();
75
76         // Highly required!
77         void clearKillStatus() { m_kill_requested = NULL; }
78
79         void stopAndWaitforThread();
80
81 private:
82         // these have stupid names so that nobody missclassifies them
83         // as curses functions. Oh, curses has stupid names too?
84         // Well, at least it was worth a try...
85         void initOfCurses();
86         void deInitOfCurses();
87
88         void draw_text();
89
90         void typeChatMessage(const std::wstring &m);
91
92         void handleInput(int ch, bool &complete_redraw_needed);
93
94         void step(int ch);
95
96         // Used to ensure the deinitialisation is always called.
97         struct CursesInitHelper {
98                 TerminalChatConsole *cons;
99                 CursesInitHelper(TerminalChatConsole * a_console)
100                         : cons(a_console)
101                 { cons->initOfCurses(); }
102                 ~CursesInitHelper() { cons->deInitOfCurses(); }
103         };
104
105         int m_log_level;
106         std::string m_nick;
107
108         u8 m_utf8_bytes_to_wait;
109         std::string m_pending_utf8_bytes;
110
111         std::list<std::string> m_nicks;
112
113         int m_cols;
114         int m_rows;
115         bool m_can_draw_text;
116
117         bool *m_kill_requested;
118         ChatBackend m_chat_backend;
119         ChatInterface *m_chat_interface;
120
121         TermLogOutput m_log_output;
122
123         bool m_esc_mode;
124
125         u64 m_game_time;
126         u32 m_time_of_day;
127 };
128
129 extern TerminalChatConsole g_term_console;
130
131 #endif