clientobject: add null checks
[waspsaliva.git] / src / terminal_chat_console.h
blobeae7c6b22db5fecfa142561f1ce0efad51ddd0fa
1 /*
2 Minetest
3 Copyright (C) 2015 est31 <MTest31@outlook.com>
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.
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.
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.
20 #pragma once
22 #include "chat.h"
23 #include "threading/thread.h"
24 #include "util/container.h"
25 #include "log.h"
26 #include <sstream>
29 struct ChatInterface;
31 class TermLogOutput : public ILogOutput {
32 public:
34 void logRaw(LogLevel lev, const std::string &line)
36 queue.push_back(std::make_pair(lev, line));
39 virtual void log(LogLevel lev, const std::string &combined,
40 const std::string &time, const std::string &thread_name,
41 const std::string &payload_text)
43 std::ostringstream os(std::ios_base::binary);
44 os << time << ": [" << thread_name << "] " << payload_text;
46 queue.push_back(std::make_pair(lev, os.str()));
49 MutexedQueue<std::pair<LogLevel, std::string> > queue;
52 class TerminalChatConsole : public Thread {
53 public:
55 TerminalChatConsole() :
56 Thread("TerminalThread")
59 void setup(
60 ChatInterface *iface,
61 bool *kill_requested,
62 const std::string &nick)
64 m_nick = nick;
65 m_kill_requested = kill_requested;
66 m_chat_interface = iface;
69 virtual void *run();
71 // Highly required!
72 void clearKillStatus() { m_kill_requested = nullptr; }
74 void stopAndWaitforThread();
76 private:
77 // these have stupid names so that nobody missclassifies them
78 // as curses functions. Oh, curses has stupid names too?
79 // Well, at least it was worth a try...
80 void initOfCurses();
81 void deInitOfCurses();
83 void draw_text();
85 void typeChatMessage(const std::wstring &m);
87 void handleInput(int ch, bool &complete_redraw_needed);
89 void step(int ch);
91 // Used to ensure the deinitialisation is always called.
92 struct CursesInitHelper {
93 TerminalChatConsole *cons;
94 CursesInitHelper(TerminalChatConsole * a_console)
95 : cons(a_console)
96 { cons->initOfCurses(); }
97 ~CursesInitHelper() { cons->deInitOfCurses(); }
100 int m_log_level = LL_ACTION;
101 std::string m_nick;
103 u8 m_utf8_bytes_to_wait = 0;
104 std::string m_pending_utf8_bytes;
106 std::list<std::string> m_nicks;
108 int m_cols;
109 int m_rows;
110 bool m_can_draw_text;
112 bool *m_kill_requested = nullptr;
113 ChatBackend m_chat_backend;
114 ChatInterface *m_chat_interface;
116 TermLogOutput m_log_output;
118 bool m_esc_mode = false;
120 u64 m_game_time = 0;
121 u32 m_time_of_day = 0;
124 extern TerminalChatConsole g_term_console;