mirror of
https://github.com/dankamongmen/notcurses.git
synced 2024-11-18 03:25:55 +00:00
b5d8549bb3
This is to make it possible, in the future, to create multiple instances of `NotCurses` for multiple terminals. The first instance of `NotCurses` becomes the default one, so that any instances of other classes that aren't explicitly created with a pointer to another `NotCurses` instance still work as expected. Note that currently trying to call `notcurses_init` twice results in the following error for me: 0x55555559bfc0 is already registered for signals Couldn't drop signals: 0x55555559bfc0 != 0x5555555b6720 terminate called after throwing an instance of 'ncpp::init_error*' Program received signal SIGABRT, Aborted. The error is signalled by `setup_signals` and the pointer shown in the message points to the first `struct notcurses` instance created.
120 lines
4.2 KiB
C++
120 lines
4.2 KiB
C++
#ifndef __NCPP_SUBPROC_HH
|
|
#define __NCPP_SUBPROC_HH
|
|
|
|
#include <notcurses/notcurses.h>
|
|
|
|
#include "Root.hh"
|
|
#include "Plane.hh"
|
|
|
|
namespace ncpp
|
|
{
|
|
class NCPP_API_EXPORT Subproc : public Root
|
|
{
|
|
public:
|
|
static ncsubproc_options default_options;
|
|
|
|
public:
|
|
explicit Subproc (Plane* n, const char* bin, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Subproc (n, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
|
|
{}
|
|
|
|
explicit Subproc (const Plane* n, const char* bin, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Subproc (n, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
|
|
{}
|
|
|
|
explicit Subproc (Plane* n, const char* bin, const ncsubproc_options* opts, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Subproc (static_cast<const Plane*>(n), bin, opts, use_path, arg, env, cbfxn, donecbfxn)
|
|
{}
|
|
|
|
explicit Subproc (const Plane* n, const char* bin, const ncsubproc_options* opts, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Root (Utilities::get_notcurses_cpp (n))
|
|
{
|
|
if (n == nullptr)
|
|
throw invalid_argument ("'n' must be a valid pointer");
|
|
create_subproc (const_cast<Plane&>(*n), bin, opts, use_path, arg, env, cbfxn, donecbfxn);
|
|
}
|
|
|
|
explicit Subproc (Plane const& n, const char* bin, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Subproc (n, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
|
|
{}
|
|
|
|
explicit Subproc (Plane& n, const char* bin, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Subproc (n, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
|
|
{}
|
|
|
|
explicit Subproc (Plane& n, const char* bin, const ncsubproc_options* opts, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Subproc (static_cast<Plane const&>(n), bin, opts, use_path, arg, env, cbfxn, donecbfxn)
|
|
{}
|
|
|
|
explicit Subproc (const Plane& n, const char* bin, const ncsubproc_options* opts, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Root (Utilities::get_notcurses_cpp (n))
|
|
{
|
|
create_subproc (const_cast<Plane&>(n), bin, opts, use_path, arg, env, cbfxn, donecbfxn);
|
|
}
|
|
|
|
~Subproc ()
|
|
{
|
|
if (is_notcurses_stopped ())
|
|
return;
|
|
|
|
ncsubproc_destroy (subproc);
|
|
}
|
|
|
|
Plane* get_plane () const noexcept
|
|
{
|
|
return Plane::map_plane (ncsubproc_plane (subproc));
|
|
}
|
|
|
|
private:
|
|
void create_subproc (Plane& n, const char* bin, const ncsubproc_options* opts, bool use_path,
|
|
char* const arg[], char* const env[],
|
|
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
|
|
{
|
|
if (bin == nullptr)
|
|
throw invalid_argument ("'bin' must be a valid pointer");
|
|
|
|
if (opts == nullptr)
|
|
opts = &default_options;
|
|
|
|
if (use_path) {
|
|
if (env != nullptr) {
|
|
subproc = ncsubproc_createvpe (
|
|
n, opts, bin, arg, env, cbfxn, donecbfxn
|
|
);
|
|
} else {
|
|
subproc = ncsubproc_createvp (
|
|
n, opts, bin, arg, cbfxn, donecbfxn
|
|
);
|
|
}
|
|
} else {
|
|
subproc = ncsubproc_createv (
|
|
n, opts, bin, arg, cbfxn, donecbfxn
|
|
);
|
|
}
|
|
|
|
if (subproc == nullptr)
|
|
throw new init_error ("Notcurses failed to create ncsubproc instance");
|
|
}
|
|
|
|
private:
|
|
ncsubproc *subproc;
|
|
};
|
|
}
|
|
#endif // __NCPP_SUBPROC_HH
|