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