display

Layout 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 (1820B)


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 using namespace literals; // NOLINT(*namespace*)
23 bool Display::is_resize_track = false;
25 Display& Display::display()
26 {
27 static Display instance;
28 return instance;
29 }
31 Display::Display()
32 : m_layout(plc_t(pos_t(0_x, 0_y), alec::get_screen_size()))
33 {
34 struct sigaction old_sig_action = {};
35 sigaction(SIGWINCH, nullptr, &old_sig_action);
36 if (old_sig_action.sa_handler != SIG_IGN) {
37 struct sigaction new_sig_action = {};
39 new_sig_action.sa_handler = handle_sigwinch;
40 sigemptyset(&new_sig_action.sa_mask);
41 new_sig_action.sa_flags = SA_RESTART;
43 sigaction(SIGWINCH, &new_sig_action, nullptr);
44 is_resize_track = true;
45 }
47 alec::init_buffer(STDIN_FILENO);
48 write<alec::abuf_enable_v>();
49 write<alec::cursor_hide_v>();
51 resize();
52 }
54 Display::~Display()
55 {
56 alec::dest_buffer();
57 write<alec::cursor_show_v>();
58 write<alec::abuf_disable_v>();
59 }
61 void Display::handle_sigwinch(int /* unused */)
62 {
63 Display::display().set_resized();
64 }
66 event Display::get_event() // NOLINT
67 {
68 if (is_resize_track && m_is_resized) {
69 Display::reset_resized();
70 return {event::type::resize, 0, 0};
71 }
73 return alec::get_event();
74 }
76 void Display::set_resized()
77 {
78 m_is_resized = true;
79 resize();
80 }
82 void Display::reset_resized()
83 {
84 m_is_resized = false;
85 }
87 bool Display::get_resized() const
88 {
89 return m_is_resized;
90 }
92 void Display::resize()
93 {
94 m_layout.resize(plc_t(pos_t(0_x, 0_y), alec::get_screen_size()));
95 }
97 void Display::render() const
98 {
99 m_layout.render();
102 void Display::input(event& evnt)
104 m_layout.input(evnt);
107 } // namespace display