displayLayout and Rendering TUI library |
git clone git://git.dimitrijedobrota.com/display.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
display.cpp (1788B)
0 #include <csignal>
2 #include "display/display.hpp"
4 #include <termios.h>
5 #include <unistd.h>
7 namespace
8 {
10 template<const char* Val>
11 inline void write()
12 {
13 ::write(STDIN_FILENO, Val, sizeof(Val));
14 }
16 } // namespace
18 namespace display
19 {
21 bool Display::is_resize_track = false;
23 Display& Display::display()
24 {
25 static Display instance;
26 return instance;
27 }
29 Display::Display()
30 : m_layout(plc_t(pos_t(0, 0), alec::get_screen_size()))
31 {
32 struct sigaction old_sig_action = {};
33 sigaction(SIGWINCH, nullptr, &old_sig_action);
34 if (old_sig_action.sa_handler != SIG_IGN) {
35 struct sigaction new_sig_action = {};
37 new_sig_action.sa_handler = handle_sigwinch;
38 sigemptyset(&new_sig_action.sa_mask);
39 new_sig_action.sa_flags = SA_RESTART;
41 sigaction(SIGWINCH, &new_sig_action, nullptr);
42 is_resize_track = true;
43 }
45 alec::init_buffer(STDIN_FILENO);
46 write<alec::abuf_enable_v>();
47 write<alec::cursor_hide_v>();
49 resize();
50 }
52 Display::~Display()
53 {
54 alec::dest_buffer();
55 write<alec::cursor_show_v>();
56 write<alec::abuf_disable_v>();
57 }
59 void Display::handle_sigwinch(int /* unused */)
60 {
61 Display::display().set_resized();
62 }
64 event Display::get_event() // NOLINT
65 {
66 if (is_resize_track && m_is_resized) {
67 Display::reset_resized();
68 return {event::Type::RESIZE, 0, 0};
69 }
71 return alec::get_event();
72 }
74 void Display::set_resized()
75 {
76 m_is_resized = true;
77 resize();
78 }
80 void Display::reset_resized()
81 {
82 m_is_resized = false;
83 }
85 bool Display::get_resized() const
86 {
87 return m_is_resized;
88 }
90 void Display::resize()
91 {
92 m_layout.resize(plc_t(pos_t(0, 0), alec::get_screen_size()));
93 }
95 void Display::render() const
96 {
97 m_layout.render();
98 std::cout << std::flush;
99 }
101 void Display::input(event& evnt)
102 {
103 m_layout.input(evnt);
104 }
106 } // namespace display