Skip to content

Commit

Permalink
Improve lib loading.
Browse files Browse the repository at this point in the history
Increments-patch-version-of: ttyr-api
Increments-patch-version-of: ttyr-tty
  • Loading branch information
dajofrey committed Feb 3, 2024
1 parent 06f73c2 commit 9fd794f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 40 deletions.
8 changes: 1 addition & 7 deletions src/lib/ttyr-api/ttyr-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void ttyr_api_initialize()
const ElfW(Dyn) *rpath = NULL;
const ElfW(Dyn) *runpath = NULL;
const char *strtab = NULL;
char path_p[1024] = {};

for (; dyn->d_tag != DT_NULL; ++dyn) {
if (dyn->d_tag == DT_RPATH) {
Expand All @@ -45,11 +46,4 @@ void ttyr_api_initialize()
} else if (runpath != NULL) {
sprintf(TTYR_API_PATH_P, strtab + runpath->d_un.d_val);
}

for (int i = 0; i < strlen(TTYR_API_PATH_P); ++i) {
if (TTYR_API_PATH_P[i] == ':') {
TTYR_API_PATH_P[i] = 0;
break;
}
}
}
21 changes: 10 additions & 11 deletions src/lib/ttyr-api/ttyr-terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,28 @@ typedef TTYR_TERMINAL_RESULT (*ttyr_terminal_cmd_setViewport_f)(

// FUNCTIONS =======================================================================================

static bool loaded = false;
static bool added = false;
static const char name_p[] = "ttyr-terminal";
static const char *dependencies_pp[16] = {
"nhgfx",
"ttyr-tty",
};

static bool ttyr_api_load() {
if (loaded) {
return true;
}
if (NH_LOADER_P == NULL || TTYR_API_PATH_P == NULL) {
static bool ttyr_api_add() {
if (NH_LOADER_P == NULL) {
return false;
}
NH_LOADER_P->addModule_f(name_p, TTYR_API_PATH_P, dependencies_pp, 1);
loaded = true;
return true;
if (!added) {
NH_LOADER_P->addModule_f(name_p, TTYR_API_PATH_P, dependencies_pp, 1);
added = true;
}
return added;
}

ttyr_terminal_Terminal *ttyr_api_openTerminal(
NH_BYTE *config_p, ttyr_tty_TTY *TTY_p)
{
if (!ttyr_api_load()) {return NULL;}
if (!ttyr_api_add()) {return NULL;}

ttyr_terminal_openTerminal_f openTerminal_f = !NH_LOADER_P || !TTY_p ? NULL : NH_LOADER_P->loadExternalSymbol_f(name_p, "ttyr_terminal_openTerminal");
return openTerminal_f ? openTerminal_f(config_p, TTY_p) : NULL;
Expand All @@ -56,7 +55,7 @@ ttyr_terminal_Terminal *ttyr_api_openTerminal(
TTYR_TERMINAL_RESULT ttyr_api_setViewport(
ttyr_terminal_Terminal *Terminal_p, nh_gfx_Viewport *Viewport_p)
{
if (!ttyr_api_load()) {return TTYR_TERMINAL_ERROR_BAD_STATE;}
if (!ttyr_api_add()) {return TTYR_TERMINAL_ERROR_BAD_STATE;}

ttyr_terminal_cmd_setViewport_f setViewport_f = !NH_LOADER_P || !Terminal_p || !Viewport_p ? NULL : NH_LOADER_P->loadExternalSymbol_f(name_p, "ttyr_terminal_cmd_setViewport");
return setViewport_f ? setViewport_f(Terminal_p, Viewport_p) : TTYR_TERMINAL_ERROR_NULL_POINTER;
Expand Down
27 changes: 13 additions & 14 deletions src/lib/ttyr-api/ttyr-tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,27 @@ typedef TTYR_TTY_RESULT (*ttyr_tty_cmd_sendEvent_f)(

// ADD =============================================================================================

static bool loaded = false;
static bool added = false;
static const char name_p[] = "ttyr-tty";
static const char *dependencies_pp[16] = {
"nhencoding",
};

static bool ttyr_api_load() {
if (loaded) {
return true;
}
if (NH_LOADER_P == NULL || TTYR_API_PATH_P == NULL) {
static bool ttyr_api_add() {
if (NH_LOADER_P == NULL) {
return false;
}
NH_LOADER_P->addModule_f(name_p, TTYR_API_PATH_P, dependencies_pp, 1);
loaded = true;
return true;
if (!added) {
NH_LOADER_P->addModule_f(name_p, TTYR_API_PATH_P, dependencies_pp, 1);
added = true;
}
return added;
}

ttyr_tty_TTY *ttyr_api_openTTY(
NH_BYTE *config_p, ttyr_tty_Interface *Interface_p)
{
if (!ttyr_api_load()) {return NULL;}
if (!ttyr_api_add()) {return NULL;}

ttyr_tty_openTTY_f openTTY_f = !NH_LOADER_P ? NULL : NH_LOADER_P->loadExternalSymbol_f(name_p, "ttyr_tty_openTTY");
return openTTY_f ? openTTY_f(config_p, Interface_p) : NULL;
Expand All @@ -70,7 +69,7 @@ ttyr_tty_TTY *ttyr_api_openTTY(
TTYR_TTY_RESULT ttyr_api_closeTTY(
ttyr_tty_TTY *TTY_p)
{
if (!ttyr_api_load()) {return TTYR_TTY_ERROR_BAD_STATE;}
if (!ttyr_api_add()) {return TTYR_TTY_ERROR_BAD_STATE;}

ttyr_tty_closeTTY_f closeTTY_f = !NH_LOADER_P ? NULL : NH_LOADER_P->loadExternalSymbol_f(name_p, "ttyr_tty_closeTTY");
return closeTTY_f ? closeTTY_f(TTY_p) : TTYR_TTY_ERROR_BAD_STATE;
Expand All @@ -79,7 +78,7 @@ TTYR_TTY_RESULT ttyr_api_closeTTY(
TTYR_TTY_RESULT ttyr_api_claimStandardIO(
ttyr_tty_TTY *TTY_p)
{
if (!ttyr_api_load()) {return TTYR_TTY_ERROR_BAD_STATE;}
if (!ttyr_api_add()) {return TTYR_TTY_ERROR_BAD_STATE;}

ttyr_tty_cmd_claimStandardIO_f claimStandardIO_f = !NH_LOADER_P ? NULL : NH_LOADER_P->loadExternalSymbol_f(name_p, "ttyr_tty_cmd_claimStandardIO");
return claimStandardIO_f ? claimStandardIO_f(TTY_p) : TTYR_TTY_ERROR_BAD_STATE;
Expand All @@ -88,7 +87,7 @@ TTYR_TTY_RESULT ttyr_api_claimStandardIO(
TTYR_TTY_RESULT ttyr_api_unclaimStandardIO(
ttyr_tty_TTY *TTY_p)
{
if (!ttyr_api_load()) {return TTYR_TTY_ERROR_BAD_STATE;}
if (!ttyr_api_add()) {return TTYR_TTY_ERROR_BAD_STATE;}

ttyr_tty_cmd_unclaimStandardIO_f unclaimStandardIO_f = !NH_LOADER_P ? NULL : NH_LOADER_P->loadExternalSymbol_f(name_p, "ttyr_tty_cmd_unclaimStandardIO");
return unclaimStandardIO_f ? unclaimStandardIO_f(TTY_p) : TTYR_TTY_ERROR_BAD_STATE;
Expand All @@ -97,7 +96,7 @@ TTYR_TTY_RESULT ttyr_api_unclaimStandardIO(
TTYR_TTY_RESULT ttyr_api_sendEvent(
ttyr_tty_TTY *TTY_p, nh_wsi_Event Event)
{
if (!ttyr_api_load()) {return TTYR_TTY_ERROR_BAD_STATE;}
if (!ttyr_api_add()) {return TTYR_TTY_ERROR_BAD_STATE;}

ttyr_tty_cmd_sendEvent_f sendEvent_f = !NH_LOADER_P ? NULL : NH_LOADER_P->loadExternalSymbol_f(name_p, "ttyr_tty_cmd_sendEvent");
return sendEvent_f ? sendEvent_f(TTY_p, Event) : TTYR_TTY_ERROR_BAD_STATE;
Expand Down
6 changes: 4 additions & 2 deletions src/lib/ttyr-tty/TTY/Draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static TTYR_TTY_RESULT ttyr_tty_draw(
cols--;
}

if (topbar) {
if (topbar && !View_p->standardIO) {
if (Tile_p->type == TTYR_TTY_TILE_TYPE_MACRO) {
return ttyr_tty_drawTopbarRow(
Tile_p, View_p->Row.Glyphs_p, cols, row, View_p->standardIO
Expand All @@ -147,7 +147,9 @@ static TTYR_TTY_RESULT ttyr_tty_draw(
))
} else if (TTYR_TTY_MICRO_TILE(Tile_p)->Program_p) {
TTYR_CHECK(TTYR_TTY_MICRO_TILE(Tile_p)->Program_p->Prototype_p->Callbacks.draw_f(
TTYR_TTY_MICRO_TILE(Tile_p)->Program_p, View_p->Row.Glyphs_p, cols, Tile_p->rowSize-1, row-1
TTYR_TTY_MICRO_TILE(Tile_p)->Program_p, View_p->Row.Glyphs_p, cols,
View_p->standardIO ? Tile_p->rowSize : Tile_p->rowSize-1,
View_p->standardIO ? row : row-1
))
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/ttyr-tty/TTY/Program.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
// PROGRAM INSTANCE ================================================================================

ttyr_tty_Program *ttyr_tty_createProgramInstance(
ttyr_tty_Interface *Prototype_p, bool once)
ttyr_tty_Interface *Interface_p, bool once)
{
ttyr_tty_Program *Program_p = nh_core_allocate(sizeof(ttyr_tty_Program));
TTYR_CHECK_MEM_2(NULL, Program_p)

Program_p->once = once;
Program_p->refresh = false;
Program_p->close = false;
Program_p->Prototype_p = Prototype_p;
Program_p->handle_p = Prototype_p->Callbacks.init_f ?
Prototype_p->Callbacks.init_f(Prototype_p->initArg_p) : NULL;
Program_p->Prototype_p = Interface_p;
Program_p->handle_p = Interface_p->Callbacks.init_f ?
Interface_p->Callbacks.init_f(Interface_p->initArg_p) : NULL;

return Program_p;
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/ttyr-tty/TTY/TTY.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ static void ttyr_tty_freeTTY(
}

for (int i = 0; i < TTY_p->Prototypes.size; ++i) {
((ttyr_tty_Interface*)TTY_p->Prototypes.pp[i])
->Callbacks.destroyPrototype_f(TTY_p->Prototypes.pp[i]);
if (((ttyr_tty_Interface*)TTY_p->Prototypes.pp[i])->Callbacks.destroyPrototype_f) {
((ttyr_tty_Interface*)TTY_p->Prototypes.pp[i])->Callbacks.destroyPrototype_f(TTY_p->Prototypes.pp[i]);
}
}
nh_core_freeList(&TTY_p->Prototypes, NH_FALSE);

Expand Down

0 comments on commit 9fd794f

Please sign in to comment.