]> git.lizzy.rs Git - dragonfireclient.git/blob - src/terminal_chat_console.h
Make Lint Happy
[dragonfireclient.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 #pragma once
21
22 #include "chat.h"
23 #include "threading/thread.h"
24 #include "util/container.h"
25 #include "log.h"
26 #include <sstream>
27
28 struct ChatInterface;
29
30 class TermLogOutput : public ILogOutput
31 {
32 public:
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 {
53 public:
54         TerminalChatConsole() : Thread("TerminalThread") {}
55
56         void setup(ChatInterface *iface, bool *kill_requested, const std::string &nick)
57         {
58                 m_nick = nick;
59                 m_kill_requested = kill_requested;
60                 m_chat_interface = iface;
61         }
62
63         virtual void *run();
64
65         // Highly required!
66         void clearKillStatus() { m_kill_requested = nullptr; }
67
68         void stopAndWaitforThread();
69
70 private:
71         // these have stupid names so that nobody missclassifies them
72         // as curses functions. Oh, curses has stupid names too?
73         // Well, at least it was worth a try...
74         void initOfCurses();
75         void deInitOfCurses();
76
77         void draw_text();
78
79         void typeChatMessage(const std::wstring &m);
80
81         void handleInput(int ch, bool &complete_redraw_needed);
82
83         void step(int ch);
84
85         // Used to ensure the deinitialisation is always called.
86         struct CursesInitHelper
87         {
88                 TerminalChatConsole *cons;
89                 CursesInitHelper(TerminalChatConsole *a_console) : cons(a_console)
90                 {
91                         cons->initOfCurses();
92                 }
93                 ~CursesInitHelper() { cons->deInitOfCurses(); }
94         };
95
96         int m_log_level = LL_ACTION;
97         std::string m_nick;
98
99         u8 m_utf8_bytes_to_wait = 0;
100         std::string m_pending_utf8_bytes;
101
102         std::list<std::string> m_nicks;
103
104         int m_cols;
105         int m_rows;
106         bool m_can_draw_text;
107
108         bool *m_kill_requested = nullptr;
109         ChatBackend m_chat_backend;
110         ChatInterface *m_chat_interface;
111
112         TermLogOutput m_log_output;
113
114         bool m_esc_mode = false;
115
116         u64 m_game_time = 0;
117         u32 m_time_of_day = 0;
118 };
119
120 extern TerminalChatConsole g_term_console;