Notcurses 3.0.13
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
NotCurses.hh
Go to the documentation of this file.
1#ifndef __NCPP_NOTCURSES_HH
2#define __NCPP_NOTCURSES_HH
3
4#include <cstdio>
5#include <ctime>
6#include <csignal>
7#include <mutex>
8
10
11#include "CellStyle.hh"
12#include "NCKey.hh"
13#include "NCLogLevel.hh"
14#include "Palette.hh"
15#include "Plane.hh"
16#include "Root.hh"
17#include "_helpers.hh"
18
19namespace ncpp
20{
22 {
23 public:
25
26 public:
27 explicit NotCurses (FILE *fp = nullptr)
28 : NotCurses (default_notcurses_options, fp)
29 {}
30
31 explicit NotCurses (const notcurses_options &nc_opts, FILE *fp = nullptr);
32
33 // Must not move or copy a NotCurses instance because we have no way to guarantee validity of any other copy
34 // when even a single instance is destructed as that operation would close notcurses.
35 NotCurses (const NotCurses &other) = delete;
36 NotCurses (NotCurses &&other) = delete;
38
39 operator notcurses* () noexcept
40 {
41 return nc;
42 }
43
44 operator notcurses const* () const noexcept
45 {
46 return nc;
47 }
48
50 {
51 if (_instance == nullptr)
52 throw invalid_state_error ("NotCurses instance not found.");
53 if (_instance->nc == nullptr)
54 throw invalid_state_error (ncpp_invalid_state_message);
55
56 return *_instance;
57 }
58
59 static bool is_notcurses_stopped (const NotCurses *ncinst = nullptr)
60 {
61 if (ncinst != nullptr)
62 return ncinst->nc == nullptr;
63
64 return *_instance == nullptr || _instance->nc == nullptr;
65 }
66
67 static const char* ncmetric (uintmax_t val, uintmax_t decimal, char *buf, int omitdec, unsigned mult, int uprefix) noexcept
68 {
69 return ::ncnmetric (val, INT_MAX, decimal, buf, omitdec, mult, uprefix);
70 }
71
72 static const char* ncqprefix (uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
73 {
74 return ::ncqprefix (val, decimal, buf, omitdec);
75 }
76
77 static const char* nciprefix (uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
78 {
79 return ::nciprefix (val, decimal, buf, omitdec);
80 }
81
82 static const char* ncbprefix (uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
83 {
84 return ::ncbprefix (val, decimal, buf, omitdec);
85 }
86
87 static const char* version () noexcept
88 {
89 return notcurses_version ();
90 }
91
92 static void version_components (int* major, int* minor, int* patch, int* tweak) noexcept
93 {
94 notcurses_version_components (major, minor, patch, tweak);
95 }
96
97 static const char* str_blitter (ncblitter_e blitter) noexcept
98 {
99 return notcurses_str_blitter (blitter);
100 }
101
102 static const char* str_scalemode (ncscale_e scalemode) noexcept
103 {
104 return notcurses_str_scalemode (scalemode);
105 }
106
107 static bool lex_margins (const char* op, notcurses_options* opts) NOEXCEPT_MAYBE
108 {
109 return error_guard (notcurses_lex_margins (op, opts), -1);
110 }
111
112 static bool lex_blitter (const char* op, ncblitter_e* blitter) NOEXCEPT_MAYBE
113 {
114 return error_guard (notcurses_lex_blitter (op, blitter), -1);
115 }
116
117 static bool lex_scalemode (const char* op, ncscale_e* scalemode) NOEXCEPT_MAYBE
118 {
119 return error_guard (notcurses_lex_scalemode (op, scalemode), -1);
120 }
121
122 bool stop ();
123
124 bool can_sextant () const noexcept
125 {
126 return notcurses_cansextant (nc);
127 }
128
129 bool can_octant () const noexcept
130 {
131 return notcurses_canoctant (nc);
132 }
133
134 bool can_utf8 () const noexcept
135 {
136 return notcurses_canutf8 (nc);
137 }
138
139 bool can_fade () const noexcept
140 {
141 return notcurses_canfade (nc);
142 }
143
144 bool can_open_images () const noexcept
145 {
146 return notcurses_canopen_images (nc);
147 }
148
149 bool can_open_videos () const noexcept
150 {
151 return notcurses_canopen_videos (nc);
152 }
153
154 bool can_change_color () const noexcept
155 {
156 return notcurses_canchangecolor (nc);
157 }
158
159 bool can_truecolor () const noexcept
160 {
161 return notcurses_cantruecolor (nc);
162 }
163
164 int cursor_enable (int y, int x) const NOEXCEPT_MAYBE
165 {
166 return error_guard (notcurses_cursor_enable (nc, y, x), -1);
167 }
168
170 {
171 return error_guard (notcurses_cursor_disable (nc), -1);
172 }
173
174 void get_stats (ncstats *stats) const noexcept
175 {
176 if (stats == nullptr)
177 return;
178
179 notcurses_stats (nc, stats);
180 }
181
182 void reset_stats (ncstats *stats) const
183 {
184 if (stats == nullptr)
185 throw invalid_argument ("'stats' must be a valid pointer");
186
187 notcurses_stats_reset (nc, stats);
188 }
189
190 bool use (const Palette *p) const
191 {
192 if (p == nullptr)
193 throw invalid_argument ("'p' must be a valid pointer");
194
195 return use (*p);
196 }
197
198 bool use (const Palette &p) const NOEXCEPT_MAYBE
199 {
200 return error_guard (ncpalette_use (nc, reinterpret_cast<const ncpalette*>(&p)), -1);
201 }
202
203 bool render () const NOEXCEPT_MAYBE
204 {
205 return error_guard (notcurses_render (nc), -1);
206 }
207
208 void get_term_dim (unsigned *rows, unsigned *cols) const noexcept
209 {
210 notcurses_term_dim_yx (nc, rows, cols);
211 }
212
213 void get_term_dim (unsigned &rows, unsigned &cols) const noexcept
214 {
215 get_term_dim (&rows, &cols);
216 }
217
218 bool refresh (unsigned* rows, unsigned* cols) const NOEXCEPT_MAYBE
219 {
220 return error_guard (notcurses_refresh (nc, rows, cols), -1);
221 }
222
223 bool refresh (unsigned& rows, unsigned& cols) const NOEXCEPT_MAYBE
224 {
225 return refresh (&rows, &cols);
226 }
227
228 unsigned get_palette_size () const noexcept
229 {
230 return notcurses_palette_size (static_cast<const notcurses*> (nc));
231 }
232
233 bool mouse_enable (unsigned eventmask) const NOEXCEPT_MAYBE
234 {
235 return error_guard (notcurses_mice_enable (nc, eventmask), -1);
236 }
237
239 {
240 return error_guard (notcurses_mice_disable (nc), -1);
241 }
242
244 {
245 return static_cast<CellStyle>(notcurses_supported_styles (nc));
246 }
247
248 uint32_t get (const timespec *ts, ncinput *ni = nullptr) const noexcept
249 {
250 return notcurses_get (nc, ts, ni);
251 }
252
253 uint32_t get (bool blocking = false, ncinput *ni = nullptr) const noexcept
254 {
255 if (blocking)
256 return notcurses_get_blocking (nc, ni);
257
258 return notcurses_get_nblock (nc, ni);
259 }
260
261 char* get_at (int yoff, int xoff, uint16_t* attr, uint64_t* channels) const noexcept
262 {
263 return notcurses_at_yx (nc, yoff, xoff, attr, channels);
264 }
265
266 int get_inputready_fd () const noexcept
267 {
268 return notcurses_inputready_fd (nc);
269 }
270
271 void drop_planes () const noexcept
272 {
274 }
275
276 void debug (FILE *debugfp) const noexcept
277 {
278 notcurses_debug (nc, debugfp);
279 }
280
281 bool align (int availcols, ncalign_e align, int cols) const NOEXCEPT_MAYBE
282 {
283 return error_guard (notcurses_align (availcols, align, cols), -INT_MAX);
284 }
285
287 {
288 return error_guard (notcurses_linesigs_enable (nc), -1);
289 }
290
292 {
293 return error_guard (notcurses_linesigs_disable (nc), -1);
294 }
295
296 ncstats* stats_alloc () const noexcept
297 {
298 return notcurses_stats_alloc (nc);
299 }
300
301 static bool ucs32_to_utf8 (const uint32_t *ucs32, unsigned ucs32count, unsigned char *resultbuf, size_t buflen) NOEXCEPT_MAYBE
302 {
303 return error_guard (notcurses_ucs32_to_utf8 (ucs32, ucs32count, resultbuf, buflen), -1);
304 }
305
307 {
308 return ncvisual_media_defblitter (nc, scale);
309 }
310
311 Plane* get_stdplane () noexcept
312 {
313 return new Plane (notcurses_stdplane (nc), true);
314 }
315
316 Plane* get_stdplane (unsigned *y, unsigned *x)
317 {
318 if (y == nullptr)
319 throw invalid_argument ("'y' must be a valid pointer");
320 if (x == nullptr)
321 throw invalid_argument ("'x' must be a valid pointer");
322
323 return get_stdplane (*y, *x);
324 }
325
326 Plane* get_stdplane (unsigned &y, unsigned &x) noexcept
327 {
328 return new Plane (notcurses_stddim_yx (nc, &y, &x));
329 }
330
331 Plane* get_top () noexcept;
332 Plane* get_bottom () noexcept;
333
334 private:
335 notcurses *nc;
336
337 static NotCurses *_instance;
338 static std::mutex init_mutex;
339 };
340}
341
342#endif
#define NCPP_API_EXPORT
Definition _helpers.hh:9
#define NOEXCEPT_MAYBE
Definition Root.hh:15
ncblitter_e ncvisual_media_defblitter(const notcurses *nc, ncscale_e scale)
Definition blit.c:1475
const char * notcurses_str_blitter(ncblitter_e blitfxn)
Definition blit.c:1394
int notcurses_lex_blitter(const char *op, ncblitter_e *blitfxn)
Definition blit.c:1378
bool can_fade() const noexcept
Definition NotCurses.hh:139
int cursor_enable(int y, int x) const NOEXCEPT_MAYBE
Definition NotCurses.hh:164
bool can_utf8() const noexcept
Definition NotCurses.hh:134
bool can_octant() const noexcept
Definition NotCurses.hh:129
bool render() const NOEXCEPT_MAYBE
Definition NotCurses.hh:203
static NotCurses & get_instance()
Definition NotCurses.hh:49
int cursor_disable() const NOEXCEPT_MAYBE
Definition NotCurses.hh:169
static bool lex_blitter(const char *op, ncblitter_e *blitter) NOEXCEPT_MAYBE
Definition NotCurses.hh:112
bool can_truecolor() const noexcept
Definition NotCurses.hh:159
bool align(int availcols, ncalign_e align, int cols) const NOEXCEPT_MAYBE
Definition NotCurses.hh:281
unsigned get_palette_size() const noexcept
Definition NotCurses.hh:228
void get_term_dim(unsigned *rows, unsigned *cols) const noexcept
Definition NotCurses.hh:208
ncblitter_e get_media_defblitter(ncscale_e scale) noexcept
Definition NotCurses.hh:306
bool linesigs_enable() const NOEXCEPT_MAYBE
Definition NotCurses.hh:286
CellStyle get_supported_styles() const noexcept
Definition NotCurses.hh:243
bool use(const Palette &p) const NOEXCEPT_MAYBE
Definition NotCurses.hh:198
int get_inputready_fd() const noexcept
Definition NotCurses.hh:266
void get_stats(ncstats *stats) const noexcept
Definition NotCurses.hh:174
bool refresh(unsigned &rows, unsigned &cols) const NOEXCEPT_MAYBE
Definition NotCurses.hh:223
void debug(FILE *debugfp) const noexcept
Definition NotCurses.hh:276
static const char * ncbprefix(uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
Definition NotCurses.hh:82
static const char * ncqprefix(uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
Definition NotCurses.hh:72
static const char * str_blitter(ncblitter_e blitter) noexcept
Definition NotCurses.hh:97
static const char * ncmetric(uintmax_t val, uintmax_t decimal, char *buf, int omitdec, unsigned mult, int uprefix) noexcept
Definition NotCurses.hh:67
static bool ucs32_to_utf8(const uint32_t *ucs32, unsigned ucs32count, unsigned char *resultbuf, size_t buflen) NOEXCEPT_MAYBE
Definition NotCurses.hh:301
bool can_open_videos() const noexcept
Definition NotCurses.hh:149
bool mouse_enable(unsigned eventmask) const NOEXCEPT_MAYBE
Definition NotCurses.hh:233
uint32_t get(const timespec *ts, ncinput *ni=nullptr) const noexcept
Definition NotCurses.hh:248
NotCurses(NotCurses &&other)=delete
bool refresh(unsigned *rows, unsigned *cols) const NOEXCEPT_MAYBE
Definition NotCurses.hh:218
static notcurses_options default_notcurses_options
Definition NotCurses.hh:24
bool can_change_color() const noexcept
Definition NotCurses.hh:154
static bool lex_scalemode(const char *op, ncscale_e *scalemode) NOEXCEPT_MAYBE
Definition NotCurses.hh:117
static bool lex_margins(const char *op, notcurses_options *opts) NOEXCEPT_MAYBE
Definition NotCurses.hh:107
Plane * get_stdplane(unsigned *y, unsigned *x)
Definition NotCurses.hh:316
bool mouse_disable() const NOEXCEPT_MAYBE
Definition NotCurses.hh:238
static bool is_notcurses_stopped(const NotCurses *ncinst=nullptr)
Definition NotCurses.hh:59
ncstats * stats_alloc() const noexcept
Definition NotCurses.hh:296
NotCurses(FILE *fp=nullptr)
Definition NotCurses.hh:27
Plane * get_top() noexcept
bool can_sextant() const noexcept
Definition NotCurses.hh:124
uint32_t get(bool blocking=false, ncinput *ni=nullptr) const noexcept
Definition NotCurses.hh:253
NotCurses(const NotCurses &other)=delete
static void version_components(int *major, int *minor, int *patch, int *tweak) noexcept
Definition NotCurses.hh:92
void reset_stats(ncstats *stats) const
Definition NotCurses.hh:182
NotCurses(const notcurses_options &nc_opts, FILE *fp=nullptr)
void get_term_dim(unsigned &rows, unsigned &cols) const noexcept
Definition NotCurses.hh:213
Plane * get_stdplane(unsigned &y, unsigned &x) noexcept
Definition NotCurses.hh:326
static const char * nciprefix(uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
Definition NotCurses.hh:77
void drop_planes() const noexcept
Definition NotCurses.hh:271
static const char * version() noexcept
Definition NotCurses.hh:87
bool use(const Palette *p) const
Definition NotCurses.hh:190
char * get_at(int yoff, int xoff, uint16_t *attr, uint64_t *channels) const noexcept
Definition NotCurses.hh:261
bool can_open_images() const noexcept
Definition NotCurses.hh:144
static const char * str_scalemode(ncscale_e scalemode) noexcept
Definition NotCurses.hh:102
bool linesigs_disable() const NOEXCEPT_MAYBE
Definition NotCurses.hh:291
Plane * get_stdplane() noexcept
Definition NotCurses.hh:311
void notcurses_debug(const notcurses *nc, FILE *debugfp)
Definition debug.c:5
uint32_t notcurses_get(notcurses *nc, const struct timespec *absdl, ncinput *ni)
Definition in.c:2751
int notcurses_linesigs_enable(notcurses *n)
Definition in.c:2892
int notcurses_linesigs_disable(notcurses *nc)
Definition in.c:2850
CellStyle
Definition CellStyle.hh:13
const char * notcurses_version(void)
Definition notcurses.c:182
ncplane * notcurses_stdplane(notcurses *nc)
Definition notcurses.c:699
int notcurses_ucs32_to_utf8(const uint32_t *ucs32, unsigned ucs32count, unsigned char *resultbuf, size_t buflen)
Definition notcurses.c:3301
const char * notcurses_str_scalemode(ncscale_e scalemode)
Definition notcurses.c:3052
void notcurses_version_components(int *major, int *minor, int *patch, int *tweak)
Definition notcurses.c:24
int ncpalette_use(notcurses *nc, const ncpalette *p)
Definition notcurses.c:2571
int notcurses_inputready_fd(notcurses *n)
Definition notcurses.c:3091
int notcurses_mice_enable(notcurses *n, unsigned eventmask)
Definition notcurses.c:2556
int notcurses_lex_scalemode(const char *op, ncscale_e *scalemode)
Definition notcurses.c:3035
unsigned notcurses_palette_size(const notcurses *nc)
Definition notcurses.c:2062
uint16_t notcurses_supported_styles(const notcurses *nc)
Definition notcurses.c:2058
void notcurses_drop_planes(notcurses *nc)
Definition notcurses.c:1434
int notcurses_lex_margins(const char *op, notcurses_options *opts)
Definition notcurses.c:3067
ncscale_e
Definition notcurses.h:96
int y
Definition notcurses.h:1905
const struct ncplane_options * opts
Definition notcurses.h:3483
ncalign_e
Definition notcurses.h:80
ncblitter_e
Definition notcurses.h:65
int int x
Definition notcurses.h:1905
int notcurses_cursor_enable(notcurses *nc, int y, int x)
Definition render.c:1738
char * notcurses_at_yx(notcurses *nc, unsigned yoff, unsigned xoff, uint16_t *stylemask, uint64_t *channels)
Definition render.c:1625
int notcurses_cursor_disable(notcurses *nc)
Definition render.c:1772
int notcurses_refresh(notcurses *nc, unsigned *restrict dimy, unsigned *restrict dimx)
Definition render.c:1417
ncstats * notcurses_stats_alloc(const notcurses *nc __attribute__((unused)))
Definition stats.c:92
void notcurses_stats_reset(notcurses *nc, ncstats *stats)
Definition stats.c:100
void notcurses_stats(notcurses *nc, ncstats *stats)
Definition stats.c:86
bool notcurses_canopen_images(const notcurses *nc __attribute__((unused)))
Definition visual.c:1357
bool notcurses_canopen_videos(const notcurses *nc __attribute__((unused)))
Definition visual.c:1364