-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathui.cpp
150 lines (120 loc) · 2.83 KB
/
ui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// ui.cpp
//
// Copyright (c) 2019-2025 Kristofer Berggren
// All rights reserved.
//
// nchat is distributed under the MIT license, see LICENSE for details.
#include "ui.h"
#include <locale.h>
#include <unistd.h>
#include <ncurses.h>
#include "emojilist.h"
#include "log.h"
#include "messagecache.h"
#include "timeutil.h"
#include "uicolorconfig.h"
#include "uiconfig.h"
#include "uicontroller.h"
#include "uikeyconfig.h"
#include "uikeydump.h"
#include "uimodel.h"
Ui::Ui()
{
UiConfig::Init();
m_Controller = std::make_shared<UiController>();
m_Model = std::make_shared<UiModel>();
}
Ui::~Ui()
{
Cleanup();
UiConfig::Cleanup();
}
void Ui::Init()
{
m_TerminalTitle = UiConfig::GetStr("terminal_title");
if (!m_TerminalTitle.empty())
{
printf("\033]0;%s\007", m_TerminalTitle.c_str());
}
printf("\033[?1004h"); // enable terminal focus in/out event
setlocale(LC_ALL, "");
m_TerminalInFile = fopen("/dev/tty", "r");
m_TerminalOutFile = fopen("/dev/tty", "w");
m_Screen = newterm(nullptr, m_TerminalOutFile, m_TerminalInFile);
m_OldScreen = set_term(m_Screen);
noecho();
cbreak();
UiConfig::GetBool("linefeed_on_enter") ? nl() : nonl();
keypad(stdscr, TRUE);
curs_set(0);
timeout(0);
EmojiList::Init();
UiKeyConfig::Init(true);
UiColorConfig::Init();
m_Model->Init();
m_Controller->Init();
}
void Ui::Cleanup()
{
m_Controller->Cleanup();
m_Model->Cleanup();
UiColorConfig::Cleanup();
UiKeyConfig::Cleanup();
EmojiList::Cleanup();
m_Model.reset();
m_Controller.reset();
wclear(stdscr);
endwin();
set_term(m_OldScreen);
delscreen(m_Screen);
fclose(m_TerminalInFile);
fclose(m_TerminalOutFile);
printf("\033[?1004l"); // disable terminal focus in/out event
if (!m_TerminalTitle.empty())
{
printf("\033]0;%s\007", "");
}
}
void Ui::Run()
{
std::unordered_map<std::string, std::shared_ptr<Protocol>>& protocols = m_Model->GetProtocols();
// retrieve cached contacts for use until receiving latest from chat service
for (auto& protocol : protocols)
{
MessageCache::FetchContacts(protocol.first);
}
LOG_INFO("ui loop start");
raw();
curs_set(1);
while (m_Model->Process())
{
wint_t key = UiController::GetKey(50);
if (key != 0)
{
m_Model->KeyHandler(key);
}
}
LOG_INFO("ui loop end");
// set as offline before logging off
for (auto& protocol : protocols)
{
m_Model->SetStatusOnline(protocol.first, false);
}
TimeUtil::Sleep(0.100);
}
void Ui::AddProtocol(std::shared_ptr<Protocol> p_Protocol)
{
m_Model->AddProtocol(p_Protocol);
}
std::unordered_map<std::string, std::shared_ptr<Protocol>>& Ui::GetProtocols()
{
return m_Model->GetProtocols();
}
void Ui::MessageHandler(std::shared_ptr<ServiceMessage> p_ServiceMessage)
{
m_Model->MessageHandler(p_ServiceMessage);
}
void Ui::RunKeyDump()
{
UiKeyDump::Run();
}