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
10
#include "
cpp-terminal/private/sigwinch.hpp
"
11
12
#include "
cpp-terminal/private/exception.hpp
"
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__)
24
namespace
Term
25
{
26
namespace
Private
27
{
28
volatile
std::sig_atomic_t
m_signalStatus
{0};
29
static
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
38
std::int32_t
Term::Private::Sigwinch::get
() noexcept
39
{
40
#if defined(__APPLE__) || defined(__wasm__) || defined(__wasm) || defined(__EMSCRIPTEN__)
41
return
Term::Private::m_signalStatus
;
42
#else
43
return
m_fd
;
44
#endif
45
}
46
47
std::int32_t
Term::Private::Sigwinch::m_fd
{-1};
48
49
void
Term::Private::Sigwinch::registerSigwinch
()
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
65
void
Term::Private::Sigwinch::blockSigwinch
()
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
75
void
Term::Private::Sigwinch::unblockSigwinch
()
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
85
bool
Term::Private::Sigwinch::isSigwinch
(
const
std::int32_t& file_descriptor)
noexcept
86
{
87
#if defined(__APPLE__) || defined(__wasm__) || defined(__wasm) || defined(__EMSCRIPTEN__)
88
if
(
Term::Private::m_signalStatus
== 1)
89
{
90
static_cast<
void
>
(file_descriptor);
// suppress warning
91
Term::Private::m_signalStatus
= {0};
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
}
Term::Private::Errno
Definition
exception.hpp:55
Term::Private::Errno::throw_exception
void throw_exception(const std::string &str={}) const
Definition
exception.cpp:149
Term::Private::Errno::check_if
Errno & check_if(const bool &ret) noexcept
Definition
exception.cpp:137
Term::Private::Sigwinch::get
static std::int32_t get() noexcept
Definition
sigwinch.cpp:38
Term::Private::Sigwinch::registerSigwinch
static void registerSigwinch()
Definition
sigwinch.cpp:49
Term::Private::Sigwinch::m_fd
static std::int32_t m_fd
Definition
sigwinch.hpp:47
Term::Private::Sigwinch::isSigwinch
static bool isSigwinch(const std::int32_t &file_descriptor=-1) noexcept
Definition
sigwinch.cpp:85
Term::Private::Sigwinch::unblockSigwinch
static void unblockSigwinch()
Definition
sigwinch.cpp:75
Term::Private::Sigwinch::blockSigwinch
static void blockSigwinch()
Definition
sigwinch.cpp:65
Term::Private::m_signalStatus
volatile std::sig_atomic_t m_signalStatus
Definition
sigwinch.cpp:28
Term
Definition
args.cpp:13
exception.hpp
sigwinch.hpp
cpp-terminal
private
sigwinch.cpp
Generated by
1.11.0