notcurses/include/ncpp/NotCurses.hh

338 lines
7.7 KiB
C++
Raw Normal View History

2020-01-07 22:51:34 +00:00
#ifndef __NCPP_NOTCURSES_HH
#define __NCPP_NOTCURSES_HH
#include <cstdio>
#include <ctime>
#include <csignal>
#include <mutex>
#include <notcurses/notcurses.h>
2020-01-07 22:51:34 +00:00
#include "CellStyle.hh"
#include "NCKey.hh"
#include "NCLogLevel.hh"
#include "Palette.hh"
2020-01-07 22:51:34 +00:00
#include "Plane.hh"
#include "Root.hh"
#include "_helpers.hh"
namespace ncpp
{
class NCPP_API_EXPORT NotCurses : public Root
{
public:
static notcurses_options default_notcurses_options;
public:
explicit NotCurses (FILE *fp = nullptr)
: NotCurses (default_notcurses_options, fp)
{}
explicit NotCurses (const notcurses_options &nc_opts, FILE *fp = nullptr);
// Must not move or copy a NotCurses instance because we have no way to guarantee validity of any other copy
// when even a single instance is destructed as that operation would close notcurses.
2020-01-07 22:51:34 +00:00
NotCurses (const NotCurses &other) = delete;
NotCurses (NotCurses &&other) = delete;
~NotCurses ();
2020-01-07 22:51:34 +00:00
operator notcurses* () noexcept
{
return nc;
}
operator notcurses const* () const noexcept
2020-01-07 22:51:34 +00:00
{
return nc;
}
static NotCurses& get_instance ()
{
if (_instance == nullptr)
throw invalid_state_error ("NotCurses instance not found.");
2020-01-07 22:51:34 +00:00
if (_instance->nc == nullptr)
throw invalid_state_error (ncpp_invalid_state_message);
2020-01-07 22:51:34 +00:00
return *_instance;
}
static bool is_notcurses_stopped (const NotCurses *ncinst = nullptr)
2020-01-07 22:51:34 +00:00
{
if (ncinst != nullptr)
return ncinst->nc == nullptr;
return *_instance == nullptr || _instance->nc == nullptr;
2020-01-07 22:51:34 +00:00
}
static const char* ncmetric (uintmax_t val, uintmax_t decimal, char *buf, int omitdec, unsigned mult, int uprefix) noexcept
2020-01-07 22:51:34 +00:00
{
ABI3 changes (#2333) Long-planned changes for API/ABI3 #1777 * remove old-style notcurses_ rendering functions * Make notcurses_render() a static inline wrapper around ncpile_render(). Remove the deprecated notcurses_render_to_file() and ncpile_render_to_file(). * ncstrwidth() becomes static inline wrapper #1777 * remove deprecated ncvisual_subtitle() * kill ncvisual_inflate(), long deprecated #1777 * remove deprecated palette256 wrappers #1777 * kill ncplane_new() #1777 * remove deprecated renderfp field from notcurses_options #1777 * reorder ncstats fields to match documentation #1777 * kill deprecated style functions #1777 * ncplane_move{top, bottom} become static inline #1777 ** ncplane_pixelgeom -> ncplane_pixel_geom() #1777 * ncstyle functions ought return uint16_t #1777 #2200 * ncvisualplane_create: provide new pile functionality #1462 * [heuristics] GNU screen 4x never has rgb * [BitmapSmoothMove] only WARN in test until sixel supports this #2258 * contour: enable sextants * interp PoC: clean up ncvisual #2266 * ncselector_options: constify string arguments * Constify strings in selector/mselector_items Use internal types to track items within the selector/mselector widgets, rather than pressing the user-provided item structs into double-duty. With this change, we can constify the strings within those user-provided items. Do so, also removing the internal-side elements. Update documentation. Closes #2267. * constify ncmenu_item/_section strings * constify strings in nctabbed_options * notcurses-demo FPS graph: back to straight seconds * remove unused wchar_from_utf8() * nstrwidth_valid(): properly initialize bytes/width * [ncneofetch] use GetSystemInfo on Windows
2021-11-09 05:53:30 +00:00
return ::ncnmetric (val, INT_MAX, decimal, buf, omitdec, mult, uprefix);
2020-01-07 22:51:34 +00:00
}
2021-11-07 22:52:50 +00:00
static const char* ncqprefix (uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
2020-01-07 22:51:34 +00:00
{
return ::ncqprefix (val, decimal, buf, omitdec);
2020-01-07 22:51:34 +00:00
}
2021-11-07 22:52:50 +00:00
static const char* nciprefix (uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
{
return ::nciprefix (val, decimal, buf, omitdec);
}
2021-11-07 22:52:50 +00:00
static const char* ncbprefix (uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
2020-01-07 22:51:34 +00:00
{
return ::ncbprefix (val, decimal, buf, omitdec);
2020-01-07 22:51:34 +00:00
}
static const char* version () noexcept
{
return notcurses_version ();
}
static void version_components (int* major, int* minor, int* patch, int* tweak) noexcept
{
notcurses_version_components (major, minor, patch, tweak);
}
static const char* str_blitter (ncblitter_e blitter) noexcept
{
return notcurses_str_blitter (blitter);
}
static const char* str_scalemode (ncscale_e scalemode) noexcept
{
return notcurses_str_scalemode (scalemode);
}
static bool lex_margins (const char* op, notcurses_options* opts) NOEXCEPT_MAYBE
{
return error_guard (notcurses_lex_margins (op, opts), -1);
}
static bool lex_blitter (const char* op, ncblitter_e* blitter) NOEXCEPT_MAYBE
{
return error_guard (notcurses_lex_blitter (op, blitter), -1);
}
static bool lex_scalemode (const char* op, ncscale_e* scalemode) NOEXCEPT_MAYBE
{
return error_guard (notcurses_lex_scalemode (op, scalemode), -1);
}
bool stop ();
2020-01-07 22:51:34 +00:00
bool can_sextant () const noexcept
{
return notcurses_cansextant (nc);
}
bool can_utf8 () const noexcept
{
return notcurses_canutf8 (nc);
}
2020-01-07 22:51:34 +00:00
bool can_fade () const noexcept
{
return notcurses_canfade (nc);
}
bool can_open_images () const noexcept
2020-01-07 22:51:34 +00:00
{
return notcurses_canopen_images (nc);
}
bool can_open_videos () const noexcept
{
return notcurses_canopen_videos (nc);
2020-01-07 22:51:34 +00:00
}
bool can_change_color () const noexcept
{
return notcurses_canchangecolor (nc);
}
bool can_truecolor () const noexcept
{
return notcurses_cantruecolor (nc);
}
int cursor_enable (int y, int x) const NOEXCEPT_MAYBE
{
return error_guard (notcurses_cursor_enable (nc, y, x), -1);
}
int cursor_disable () const NOEXCEPT_MAYBE
{
return error_guard (notcurses_cursor_disable (nc), -1);
}
2020-01-07 22:51:34 +00:00
void get_stats (ncstats *stats) const noexcept
{
if (stats == nullptr)
return;
notcurses_stats (nc, stats);
}
void reset_stats (ncstats *stats) const
{
if (stats == nullptr)
throw invalid_argument ("'stats' must be a valid pointer");
2020-01-07 22:51:34 +00:00
2020-10-07 03:33:28 +00:00
notcurses_stats_reset (nc, stats);
2020-01-07 22:51:34 +00:00
}
bool use (const Palette *p) const
2020-01-07 22:51:34 +00:00
{
if (p == nullptr)
throw invalid_argument ("'p' must be a valid pointer");
2020-01-07 22:51:34 +00:00
return use (*p);
}
bool use (const Palette &p) const NOEXCEPT_MAYBE
2020-01-07 22:51:34 +00:00
{
return error_guard (ncpalette_use (nc, reinterpret_cast<const ncpalette*>(&p)), -1);
2020-01-07 22:51:34 +00:00
}
bool render () const NOEXCEPT_MAYBE
2020-01-07 22:51:34 +00:00
{
return error_guard (notcurses_render (nc), -1);
2020-01-07 22:51:34 +00:00
}
2021-11-07 02:32:20 +00:00
void get_term_dim (unsigned *rows, unsigned *cols) const noexcept
2020-01-07 22:51:34 +00:00
{
notcurses_term_dim_yx (nc, rows, cols);
}
2021-11-07 02:32:20 +00:00
void get_term_dim (unsigned &rows, unsigned &cols) const noexcept
2020-01-07 22:51:34 +00:00
{
get_term_dim (&rows, &cols);
}
2021-11-07 02:32:20 +00:00
bool refresh (unsigned* rows, unsigned* cols) const NOEXCEPT_MAYBE
{
return error_guard (notcurses_refresh (nc, rows, cols), -1);
}
2021-11-07 02:32:20 +00:00
bool refresh (unsigned& rows, unsigned& cols) const NOEXCEPT_MAYBE
2020-01-07 22:51:34 +00:00
{
return refresh (&rows, &cols);
2020-01-07 22:51:34 +00:00
}
2020-08-16 04:27:27 +00:00
unsigned get_palette_size () const noexcept
2020-01-07 22:51:34 +00:00
{
return notcurses_palette_size (static_cast<const notcurses*> (nc));
}
bool mouse_enable (unsigned eventmask) const NOEXCEPT_MAYBE
2020-01-07 22:51:34 +00:00
{
return error_guard (notcurses_mice_enable (nc, eventmask), -1);
2020-01-07 22:51:34 +00:00
}
bool mouse_disable () const NOEXCEPT_MAYBE
2020-01-07 22:51:34 +00:00
{
return error_guard (notcurses_mice_disable (nc), -1);
2020-01-07 22:51:34 +00:00
}
CellStyle get_supported_styles () const noexcept
{
return static_cast<CellStyle>(notcurses_supported_styles (nc));
}
2021-07-25 04:38:33 +00:00
uint32_t get (const timespec *ts, ncinput *ni = nullptr) const noexcept
2020-01-07 22:51:34 +00:00
{
2021-07-25 04:38:33 +00:00
return notcurses_get (nc, ts, ni);
2020-01-07 22:51:34 +00:00
}
2021-07-25 04:38:33 +00:00
uint32_t get (bool blocking = false, ncinput *ni = nullptr) const noexcept
2020-01-07 22:51:34 +00:00
{
if (blocking)
return notcurses_get_blocking (nc, ni);
2020-01-07 22:51:34 +00:00
return notcurses_get_nblock (nc, ni);
2020-01-07 22:51:34 +00:00
}
char* get_at (int yoff, int xoff, uint16_t* attr, uint64_t* channels) const noexcept
2020-01-07 22:51:34 +00:00
{
return notcurses_at_yx (nc, yoff, xoff, attr, channels);
2020-01-07 22:51:34 +00:00
}
int get_inputready_fd () const noexcept
{
return notcurses_inputready_fd (nc);
}
void drop_planes () const noexcept
{
notcurses_drop_planes (nc);
}
void debug (FILE *debugfp) const noexcept
{
notcurses_debug (nc, debugfp);
}
bool align (int availcols, ncalign_e align, int cols) const NOEXCEPT_MAYBE
{
return error_guard (notcurses_align (availcols, align, cols), -INT_MAX);
}
bool linesigs_enable () const NOEXCEPT_MAYBE
{
return error_guard (notcurses_linesigs_enable (nc), -1);
}
bool linesigs_disable () const NOEXCEPT_MAYBE
{
return error_guard (notcurses_linesigs_disable (nc), -1);
}
ncstats* stats_alloc () const noexcept
{
return notcurses_stats_alloc (nc);
}
static bool ucs32_to_utf8 (const uint32_t *ucs32, unsigned ucs32count, unsigned char *resultbuf, size_t buflen) NOEXCEPT_MAYBE
{
return error_guard (notcurses_ucs32_to_utf8 (ucs32, ucs32count, resultbuf, buflen), -1);
}
ncblitter_e get_media_defblitter (ncscale_e scale) noexcept
{
return ncvisual_media_defblitter (nc, scale);
}
2020-01-07 22:51:34 +00:00
Plane* get_stdplane () noexcept
{
return new Plane (notcurses_stdplane (nc), true);
}
2021-11-07 02:32:20 +00:00
Plane* get_stdplane (unsigned *y, unsigned *x)
{
if (y == nullptr)
throw invalid_argument ("'y' must be a valid pointer");
if (x == nullptr)
throw invalid_argument ("'x' must be a valid pointer");
return get_stdplane (*y, *x);
}
2021-11-07 02:32:20 +00:00
Plane* get_stdplane (unsigned &y, unsigned &x) noexcept
{
return new Plane (notcurses_stddim_yx (nc, &y, &x));
}
2020-01-07 22:51:34 +00:00
Plane* get_top () noexcept;
Plane* get_bottom () noexcept;
2020-01-07 22:51:34 +00:00
private:
notcurses *nc;
static NotCurses *_instance;
static std::mutex init_mutex;
};
}
#endif