cpp-terminal 1.0.0
Small C++ library for writing multiplatform terminal applications
Loading...
Searching...
No Matches
sigwinch.cpp
Go to the documentation of this file.
1/*
2* cpp-terminal
3* C++ library for writing multi-platform terminal applications.
4*
5* SPDX-FileCopyrightText: 2019-2024 cpp-terminal
6*
7* SPDX-License-Identifier: MIT
8*/
9
11
13
14#if !defined(_WIN32)
15 #include <csignal>
16 #include <unistd.h>
17#endif
18
19#if defined(__linux__)
20 #include <sys/signalfd.h>
21#endif
22
23#if defined(__APPLE__) || defined(__wasm__) || defined(__wasm) || defined(__EMSCRIPTEN__)
24namespace Term
25{
26namespace Private
27{
28volatile std::sig_atomic_t m_signalStatus{0};
29static void sigwinchHandler(int sig)
30{
31 if(sig == SIGWINCH) { m_signalStatus = 1; }
32 else { m_signalStatus = 0; }
33}
34} // namespace Private
35} // namespace Term
36#endif
37
38std::int32_t Term::Private::Sigwinch::get() noexcept
39{
40#if defined(__APPLE__) || defined(__wasm__) || defined(__wasm) || defined(__EMSCRIPTEN__)
42#else
43 return m_fd;
44#endif
45}
46
47std::int32_t Term::Private::Sigwinch::m_fd{-1};
48
50{
51#if defined(__APPLE__) || defined(__wasm__) || defined(__wasm) || defined(__EMSCRIPTEN__)
52 struct sigaction sa;
53 Term::Private::Errno().check_if(sigemptyset(&sa.sa_mask) != 0).throw_exception("sigemptyset(&sa.sa_mask)");
54 sa.sa_flags = {0};
55 sa.sa_handler = {Term::Private::sigwinchHandler};
56 Term::Private::Errno().check_if(sigaction(SIGWINCH, &sa, nullptr) != 0).throw_exception("sigaction(SIGWINCH, &sa, nullptr)");
57#elif defined(__linux__)
58 ::sigset_t windows_event = {};
59 Term::Private::Errno().check_if(sigemptyset(&windows_event) != 0).throw_exception("sigemptyset(&windows_event)");
60 Term::Private::Errno().check_if(sigaddset(&windows_event, SIGWINCH) != 0).throw_exception("sigaddset(&windows_event, SIGWINCH)");
61 Term::Private::Errno().check_if((m_fd = ::signalfd(-1, &windows_event, SFD_NONBLOCK | SFD_CLOEXEC)) == -1).throw_exception("m_fd = ::signalfd(-1, &windows_event, SFD_NONBLOCK | SFD_CLOEXEC)");
62#endif
63}
64
66{
67#if !defined(_WIN32)
68 ::sigset_t windows_event = {};
69 Term::Private::Errno().check_if(sigemptyset(&windows_event) != 0).throw_exception("sigemptyset(&windows_event)");
70 Term::Private::Errno().check_if(sigaddset(&windows_event, SIGWINCH) != 0).throw_exception("sigaddset(&windows_event, SIGWINCH)");
71 Term::Private::Errno().check_if(::pthread_sigmask(SIG_BLOCK, &windows_event, nullptr) != 0).throw_exception("::pthread_sigmask(SIG_BLOCK, &windows_event, nullptr)");
72#endif
73}
74
76{
77#if !defined(_WIN32)
78 ::sigset_t windows_event = {};
79 Term::Private::Errno().check_if(sigemptyset(&windows_event) != 0).throw_exception("sigemptyset(&windows_event)");
80 Term::Private::Errno().check_if(sigaddset(&windows_event, SIGWINCH) != 0).throw_exception("sigaddset(&windows_event, SIGWINCH)");
81 Term::Private::Errno().check_if(::pthread_sigmask(SIG_UNBLOCK, &windows_event, nullptr) != 0).throw_exception("::pthread_sigmask(SIG_UNBLOCK, &windows_event, nullptr)");
82#endif
83}
84
85bool Term::Private::Sigwinch::isSigwinch(const std::int32_t& file_descriptor) noexcept
86{
87#if defined(__APPLE__) || defined(__wasm__) || defined(__wasm) || defined(__EMSCRIPTEN__)
89 {
90 static_cast<void>(file_descriptor); // suppress warning
92 return true;
93 }
94 return false;
95#elif defined(__linux__)
96 if(m_fd == file_descriptor)
97 {
98 // read it to clean
99 ::signalfd_siginfo fdsi = {};
100 ::read(m_fd, &fdsi, sizeof(fdsi));
101 return true;
102 }
103 return false;
104#else
105 static_cast<void>(file_descriptor); // suppress warning
106 return false;
107#endif
108}
void throw_exception(const std::string &str={}) const
Errno & check_if(const bool &ret) noexcept
static std::int32_t get() noexcept
Definition sigwinch.cpp:38
static void registerSigwinch()
Definition sigwinch.cpp:49
static std::int32_t m_fd
Definition sigwinch.hpp:47
static bool isSigwinch(const std::int32_t &file_descriptor=-1) noexcept
Definition sigwinch.cpp:85
static void unblockSigwinch()
Definition sigwinch.cpp:75
static void blockSigwinch()
Definition sigwinch.cpp:65
volatile std::sig_atomic_t m_signalStatus
Definition sigwinch.cpp:28
Definition args.cpp:13