Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
Increments-patch-version-of: ttyr-api
Increments-patch-version-of: ttyr-terminal
Increments-patch-version-of: ttyr-tty
  • Loading branch information
dajofrey committed Oct 30, 2024
1 parent d0a8038 commit ea35ba6
Show file tree
Hide file tree
Showing 30 changed files with 239 additions and 489 deletions.
13 changes: 7 additions & 6 deletions src/lib/ttyr-api/ttyr-terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ static const char *dependencies_pp[16] = {
};

static bool ttyr_api_add() {
if (NH_LOADER_P == NULL) {
nh_core_Loader *Loader_p = nh_api_getLoader();
if (Loader_p == NULL) {
return false;
}
if (!added) {
NH_LOADER_P->addModule_f(name_p, TTYR_API_PATH_P, dependencies_pp, 1);
Loader_p->addModule_f(name_p, TTYR_API_PATH_P, dependencies_pp, 1);
added = true;
}
return added;
Expand All @@ -47,16 +48,16 @@ ttyr_terminal_Terminal *ttyr_api_openTerminal(
char *config_p, ttyr_tty_TTY *TTY_p)
{
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");
nh_core_Loader *Loader_p = nh_api_getLoader();
ttyr_terminal_openTerminal_f openTerminal_f = !Loader_p || !TTY_p ? NULL : Loader_p->loadExternalSymbol_f(name_p, "ttyr_terminal_openTerminal");
return openTerminal_f ? openTerminal_f(config_p, TTY_p) : NULL;
}

TTYR_TERMINAL_RESULT ttyr_api_setViewport(
ttyr_terminal_Terminal *Terminal_p, nh_api_Viewport *Viewport_p)
{
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");
nh_core_Loader *Loader_p = nh_api_getLoader();
ttyr_terminal_cmd_setViewport_f setViewport_f = !Loader_p || !Terminal_p || !Viewport_p ? NULL : Loader_p->loadExternalSymbol_f(name_p, "ttyr_terminal_cmd_setViewport");
return setViewport_f ? setViewport_f(Terminal_p, Viewport_p) : TTYR_TERMINAL_ERROR_NULL_POINTER;
}
25 changes: 13 additions & 12 deletions src/lib/ttyr-api/ttyr-tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ static const char *dependencies_pp[16] = {
};

static bool ttyr_api_add() {
if (NH_LOADER_P == NULL) {
nh_core_Loader *Loader_p = nh_api_getLoader();
if (Loader_p == NULL) {
return false;
}
if (!added) {
NH_LOADER_P->addModule_f(name_p, TTYR_API_PATH_P, dependencies_pp, 1);
Loader_p->addModule_f(name_p, TTYR_API_PATH_P, dependencies_pp, 1);
added = true;
}
return added;
Expand All @@ -61,43 +62,43 @@ ttyr_tty_TTY *ttyr_api_openTTY(
char *config_p, ttyr_tty_Interface *Interface_p)
{
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");
nh_core_Loader *Loader_p = nh_api_getLoader();
ttyr_tty_openTTY_f openTTY_f = !Loader_p ? NULL : Loader_p->loadExternalSymbol_f(name_p, "ttyr_tty_openTTY");
return openTTY_f ? openTTY_f(config_p, Interface_p) : NULL;
}

TTYR_TTY_RESULT ttyr_api_closeTTY(
ttyr_tty_TTY *TTY_p)
{
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");
nh_core_Loader *Loader_p = nh_api_getLoader();
ttyr_tty_closeTTY_f closeTTY_f = !Loader_p ? NULL : Loader_p->loadExternalSymbol_f(name_p, "ttyr_tty_closeTTY");
return closeTTY_f ? closeTTY_f(TTY_p) : TTYR_TTY_ERROR_BAD_STATE;
}

TTYR_TTY_RESULT ttyr_api_claimStandardIO(
ttyr_tty_TTY *TTY_p)
{
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");
nh_core_Loader *Loader_p = nh_api_getLoader();
ttyr_tty_cmd_claimStandardIO_f claimStandardIO_f = !Loader_p ? NULL : Loader_p->loadExternalSymbol_f(name_p, "ttyr_tty_cmd_claimStandardIO");
return claimStandardIO_f ? claimStandardIO_f(TTY_p) : TTYR_TTY_ERROR_BAD_STATE;
}

TTYR_TTY_RESULT ttyr_api_unclaimStandardIO(
ttyr_tty_TTY *TTY_p)
{
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");
nh_core_Loader *Loader_p = nh_api_getLoader();
ttyr_tty_cmd_unclaimStandardIO_f unclaimStandardIO_f = !Loader_p ? NULL : Loader_p->loadExternalSymbol_f(name_p, "ttyr_tty_cmd_unclaimStandardIO");
return unclaimStandardIO_f ? unclaimStandardIO_f(TTY_p) : TTYR_TTY_ERROR_BAD_STATE;
}

TTYR_TTY_RESULT ttyr_api_sendEvent(
ttyr_tty_TTY *TTY_p, nh_api_WSIEvent Event)
{
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");
nh_core_Loader *Loader_p = nh_api_getLoader();
ttyr_tty_cmd_sendEvent_f sendEvent_f = !Loader_p ? NULL : Loader_p->loadExternalSymbol_f(name_p, "ttyr_tty_cmd_sendEvent");
return sendEvent_f ? sendEvent_f(TTY_p, Event) : TTYR_TTY_ERROR_BAD_STATE;
}
6 changes: 5 additions & 1 deletion src/lib/ttyr-api/ttyr-tty.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@
bool wide;
} ttyr_tty_GlyphAttributes;

typedef struct ttyr_tty_Color {
float r, g, b, a;
} ttyr_tty_Color;

typedef struct ttyr_tty_GlyphColor {
nh_Color Color;
ttyr_tty_Color Color;
bool custom;
} ttyr_tty_GlyphColor;

Expand Down
24 changes: 9 additions & 15 deletions src/lib/ttyr-terminal/Common/Config.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,43 @@ size_t TTYR_TERMINAL_SETTING_NAMES_PP_COUNT =
const char *ttyr_terminal_getSettingName(
TTYR_TERMINAL_SETTING_E setting)
{
TTYR_TERMINAL_BEGIN()
TTYR_TERMINAL_END(TTYR_TERMINAL_SETTING_NAMES_PP[setting])
return TTYR_TERMINAL_SETTING_NAMES_PP[setting];
}

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

static TTYR_TERMINAL_RESULT ttyr_terminal_getSetting(
ttyr_terminal_Config *Config_p, char namespace_p[255], int setting)
{
TTYR_TERMINAL_BEGIN()

nh_core_List *Setting_p = nh_core_getGlobalConfigSetting(namespace_p, -1, TTYR_TERMINAL_SETTING_NAMES_PP[setting]);
TTYR_TERMINAL_CHECK_NULL(Setting_p)

switch (setting) {
case 0 :
if (Setting_p->size != 1) {TTYR_TERMINAL_END(TTYR_TERMINAL_ERROR_BAD_STATE)}
if (Setting_p->size != 1) {return TTYR_TERMINAL_ERROR_BAD_STATE;}
Config_p->fontSize = atoi(Setting_p->pp[0]);
break;
case 1 :
if (Setting_p->size != 1) {TTYR_TERMINAL_END(TTYR_TERMINAL_ERROR_BAD_STATE)}
if (Setting_p->size != 1) {return TTYR_TERMINAL_ERROR_BAD_STATE;}
Config_p->blinkFrequency = atof(Setting_p->pp[0]);
break;
case 2 :
if (Setting_p->size != 4) {TTYR_TERMINAL_END(TTYR_TERMINAL_ERROR_BAD_STATE)}
if (Setting_p->size != 4) {return TTYR_TERMINAL_ERROR_BAD_STATE;}
Config_p->Foreground.r = ((float)atoi(Setting_p->pp[0]))/255.0f;
Config_p->Foreground.g = ((float)atoi(Setting_p->pp[1]))/255.0f;
Config_p->Foreground.b = ((float)atoi(Setting_p->pp[2]))/255.0f;
Config_p->Foreground.a = ((float)atoi(Setting_p->pp[3]))/255.0f;
break;
case 3 :
if (Setting_p->size != 4) {TTYR_TERMINAL_END(TTYR_TERMINAL_ERROR_BAD_STATE)}
if (Setting_p->size != 4) {return TTYR_TERMINAL_ERROR_BAD_STATE;}
Config_p->Background.r = ((float)atoi(Setting_p->pp[0]))/255.0f;
Config_p->Background.g = ((float)atoi(Setting_p->pp[1]))/255.0f;
Config_p->Background.b = ((float)atoi(Setting_p->pp[2]))/255.0f;
Config_p->Background.a = ((float)atoi(Setting_p->pp[3]))/255.0f;
break;
case 4 :
Config_p->accents = 0;
if (Setting_p->size < 4 || (Setting_p->size % 4) != 0) {TTYR_TERMINAL_END(TTYR_TERMINAL_ERROR_BAD_STATE)}
if (Setting_p->size < 4 || (Setting_p->size % 4) != 0) {return TTYR_TERMINAL_ERROR_BAD_STATE;}
for (int i = 0, j = 0; j < Setting_p->size; i++, j += 4) {
Config_p->Accents_p[i].r = ((float)atof(Setting_p->pp[j+0]))/255.0f;
Config_p->Accents_p[i].g = ((float)atof(Setting_p->pp[j+1]))/255.0f;
Expand All @@ -86,18 +83,16 @@ TTYR_TERMINAL_BEGIN()
}
break;
case 5 :
if (Setting_p->size != 1) {TTYR_TERMINAL_END(TTYR_TERMINAL_ERROR_BAD_STATE)}
if (Setting_p->size != 1) {return TTYR_TERMINAL_ERROR_BAD_STATE;}
Config_p->border = atoi(Setting_p->pp[0]);
break;
}

TTYR_TERMINAL_END(TTYR_TERMINAL_SUCCESS)
return TTYR_TERMINAL_SUCCESS;
}

static ttyr_terminal_Config ttyr_terminal_getStaticConfig()
{
TTYR_TERMINAL_BEGIN()

ttyr_terminal_Config Config;
memset(&Config, 0, sizeof(ttyr_terminal_Config));

Expand All @@ -108,12 +103,11 @@ TTYR_TERMINAL_BEGIN()
TTYR_TERMINAL_CHECK_2(Config, ttyr_terminal_getSetting(&Config, Terminal_p->namespace_p, i))
}

TTYR_TERMINAL_END(Config)
return Config;
}

ttyr_terminal_Config ttyr_terminal_getConfig()
{
ttyr_terminal_Config Config = ttyr_terminal_getStaticConfig();
return Config;
}

6 changes: 3 additions & 3 deletions src/lib/ttyr-terminal/Common/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
typedef struct ttyr_terminal_Config {
int fontSize;
float blinkFrequency;
nh_Color Foreground;
nh_Color Background;
nh_Color Accents_p[8];
ttyr_tty_Color Foreground;
ttyr_tty_Color Background;
ttyr_tty_Color Accents_p[8];
int accents;
int border;
} ttyr_terminal_Config;
Expand Down
8 changes: 3 additions & 5 deletions src/lib/ttyr-terminal/Common/Initialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@

TTYR_TERMINAL_RESULT ttyr_terminal_initialize()
{
TTYR_TERMINAL_BEGIN()

nh_core_appendConfig(ttyr_terminal_conf_inc, ttyr_terminal_conf_inc_len, false);

for (int i = 0; i < NH_VULKAN.GPUs.size; ++i)
{
nh_vk_GPU *GPU_p = NH_VULKAN.GPUs.pp[0];
nh_gfx_VulkanGPU *GPU_p = NH_VULKAN.GPUs.pp[0];

GPU_p->Term.Pipelines_p = nh_core_allocate(sizeof(nh_vk_Pipeline) * _TTYR_TERMINAL_VULKAN_PIPELINE_COUNT);
GPU_p->Term.Pipelines_p = nh_core_allocate(sizeof(nh_gfx_VulkanPipeline) * _TTYR_TERMINAL_VULKAN_PIPELINE_COUNT);
TTYR_TERMINAL_CHECK_MEM(GPU_p->Term.Pipelines_p)

TTYR_TERMINAL_CHECK(ttyr_terminal_createVulkanPipelines(&GPU_p->Driver, GPU_p->Term.Pipelines_p))
}

TTYR_TERMINAL_DIAGNOSTIC_END(TTYR_TERMINAL_SUCCESS)
return TTYR_TERMINAL_SUCCESS;
}

40 changes: 6 additions & 34 deletions src/lib/ttyr-terminal/Common/Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,40 @@

#endif // DOXYGEN_SHOULD_SKIP_THIS

// FLOW LOGGING ====================================================================================

#ifdef NH_LOG_FLOW
#define TTYR_TERMINAL_BEGIN() {_ttyr_terminal_logBegin(__FILE__, __func__);}
#else
#define TTYR_TERMINAL_BEGIN()
#endif

#ifdef NH_LOG_FLOW
#define TTYR_TERMINAL_END(result) {_ttyr_terminal_logEnd(__FILE__, __func__); return result;}
#else
#define TTYR_TERMINAL_END(result) {return result;}
#endif

#ifdef NH_LOG_FLOW
#define TTYR_TERMINAL_SILENT_END() {_ttyr_terminal_logEnd(__FILE__, __func__); return;}
#else
#define TTYR_TERMINAL_SILENT_END() {return;}
#endif

#ifdef NH_LOG_FLOW
#define TTYR_TERMINAL_DIAGNOSTIC_END(result) {return _ttyr_terminal_logDiagnosticEnd(__FILE__, __func__, result, __LINE__);}
#else
#define TTYR_TERMINAL_DIAGNOSTIC_END(result) {return result;}
#endif

// ERROR LOGGING ===================================================================================

#define TTYR_TERMINAL_CHECK(checkable) \
{ \
TTYR_TERMINAL_RESULT checkResult = checkable; \
if (checkResult != TTYR_TERMINAL_SUCCESS) {TTYR_TERMINAL_DIAGNOSTIC_END(checkResult)} \
if (checkResult != TTYR_TERMINAL_SUCCESS) {return checkResult;} \
}

#define TTYR_TERMINAL_CHECK_2(checkReturn, checkable) \
{ \
TTYR_TERMINAL_RESULT checkResult = checkable; \
if (checkResult != TTYR_TERMINAL_SUCCESS) {TTYR_TERMINAL_END(checkReturn)} \
if (checkResult != TTYR_TERMINAL_SUCCESS) {return checkReturn;} \
}

#define TTYR_TERMINAL_CHECK_NULL(checkable) \
{ \
void *checkResult_p = checkable; \
if (checkResult_p == NULL) {TTYR_TERMINAL_DIAGNOSTIC_END(TTYR_TERMINAL_ERROR_NULL_POINTER)} \
if (checkResult_p == NULL) {return TTYR_TERMINAL_ERROR_NULL_POINTER;} \
}

#define TTYR_TERMINAL_CHECK_NULL_2(checkReturn, checkable) \
{ \
void *checkResult_p = checkable; \
if (checkResult_p == NULL) {TTYR_TERMINAL_END(checkReturn)} \
if (checkResult_p == NULL) {return checkReturn;} \
}

#define TTYR_TERMINAL_CHECK_MEM(checkable) \
{ \
void *checkResult_p = checkable; \
if (checkResult_p == NULL) {TTYR_TERMINAL_DIAGNOSTIC_END(TTYR_TERMINAL_ERROR_MEMORY_ALLOCATION)} \
if (checkResult_p == NULL) {return TTYR_TERMINAL_ERROR_MEMORY_ALLOCATION;} \
}

#define TTYR_TERMINAL_CHECK_MEM_2(checkReturn, checkable) \
{ \
void *checkResult_p = checkable; \
if (checkResult_p == NULL) {TTYR_TERMINAL_END(checkReturn)} \
if (checkResult_p == NULL) {return checkReturn;} \
}

#endif // TTYR_TERMINAL_COMMON_MACROS_H
4 changes: 1 addition & 3 deletions src/lib/ttyr-terminal/Common/Terminate.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

TTYR_TERMINAL_RESULT ttyr_terminal_terminate()
{
TTYR_TERMINAL_BEGIN()

TTYR_TERMINAL_DIAGNOSTIC_END(TTYR_TERMINAL_SUCCESS)
return TTYR_TERMINAL_SUCCESS;
}

Loading

0 comments on commit ea35ba6

Please sign in to comment.