Skip to content

Commit

Permalink
Improve API and build procedure.
Browse files Browse the repository at this point in the history
Increments-major-version-of: ttyr-api
Increments-minor-version-of: other
  • Loading branch information
dajofrey committed Jan 30, 2024
1 parent 5f28a3b commit 220592a
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 32 deletions.
22 changes: 13 additions & 9 deletions build/automation/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Define the compiler and compile flags
CC = gcc
CFLAGS = -fPIC -std=gnu99 -Wl,-rpath,$(CURDIR)/external/Netzhaut/lib
CFLAGS = -fPIC -std=gnu99 -Wl,-rpath,$(CURDIR)/lib,-rpath,$(CURDIR)/external/Netzhaut/lib

# Define the linker and linker flags
LD = gcc
LDFLAGS_TTYR_TTY = -lutil -Lexternal/st-0.8.5/ -l:libst.so
LDFLAGS_TTYR_TERMINAL =
LDFLAGS_TTYR = -Lexternal/Netzhaut/lib -lttyr-api -lnhapi
LDFLAGS_TTYR = -Llib -lttyr-api -Lexternal/Netzhaut/lib -lnhapi

# Define the source file directory for each library
SRC_DIR_TTYR_TTY = src/lib/ttyr-tty
Expand Down Expand Up @@ -65,6 +65,7 @@ SRC_FILES_TTYR_TERMINAL = \
Common/Config.c \

SRC_FILES_TTYR_API = \
ttyr-api.c \
ttyr-tty.c \
ttyr-terminal.c \

Expand All @@ -80,14 +81,16 @@ OBJ_FILES_TTYR = $(patsubst %.c, %.o, $(addprefix $(SRC_DIR_TTYR)/, $(SRC_FILES_
OBJ_FILES_ST = $(patsubst %.c, %.o, $(addprefix $(SRC_DIR_ST)/, $(SRC_FILES_ST)))

# Name of the shared libraries and binaries
LIB_TTYR_TTY = external/Netzhaut/lib/libttyr-tty.so
LIB_TTYR_TERMINAL = external/Netzhaut/lib/libttyr-terminal.so
LIB_TTYR_API = external/Netzhaut/lib/libttyr-api.so
LIB_TTYR_TTY = lib/libttyr-tty.so
LIB_TTYR_TERMINAL = lib/libttyr-terminal.so
LIB_TTYR_API = lib/libttyr-api.so
LIB_ST = external/st-0.8.5/libst.so
BIN_TTYR = bin/ttyr

build_netzhaut:
(cd external/Netzhaut && make -f build/automation/Makefile nhapi.so nhcore.so nhencoding.so nhwsi.so nhgfx.so)
(cd external/Netzhaut && git submodule update --init --recursive && make -f build/automation/Makefile nhapi.so nhcore.so nhencoding.so nhwsi.so nhgfx.so)
create_lib_dir:
mkdir -p lib
create_bin_dir:
mkdir -p bin

Expand Down Expand Up @@ -115,11 +118,11 @@ $(OBJ_FILES_TTYR): CFLAGS += -Iexternal -Iexternal/Netzhaut/src/lib -Isrc/lib
$(CC) $(CFLAGS) -c -o $@ $<

# Rule to link object files into the shared libraries
$(LIB_TTYR_TTY): $(OBJ_FILES_TTYR_TTY)
$(LIB_TTYR_TTY): create_lib_dir $(OBJ_FILES_TTYR_TTY)
$(LD) $(CFLAGS) -Wl,-rpath,':$(CURDIR)/external/st-0.8.5' -shared -o $@ $(OBJ_FILES_TTYR_TTY) $(LDFLAGS_TTYR_TTY)
$(LIB_TTYR_TERMINAL): $(OBJ_FILES_TTYR_TERMINAL)
$(LIB_TTYR_TERMINAL): create_lib_dir $(OBJ_FILES_TTYR_TERMINAL)
$(LD) $(CFLAGS) -shared -o $@ $(OBJ_FILES_TTYR_TERMINAL) $(LDFLAGS_TTYR_TERMINAL)
$(LIB_TTYR_API): $(OBJ_FILES_TTYR_API)
$(LIB_TTYR_API): create_lib_dir $(OBJ_FILES_TTYR_API)
$(LD) $(CFLAGS) -shared -o $@ $(OBJ_FILES_TTYR_API) $(LDFLAGS_TTYR_API)
$(LIB_ST): $(OBJ_FILES_ST)
$(LD) $(CFLAGS) -shared -o $@ $(OBJ_FILES_ST) $(LDFLAGS_ST)
Expand All @@ -140,5 +143,6 @@ clean:
rm -f $(OBJ_FILES_ST) $(LIB_ST)
rm -f $(OBJ_FILES_TTYR) $(BIN_TTYR)
rm -rf bin
(cd external/Netzhaut && make -f build/automation/Makefile clean)

.PHONY: all lib bin clean
8 changes: 5 additions & 3 deletions src/bin/ttyr/Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

// INCLUDES ========================================================================================

#include "ttyr-api/ttyr-terminal.h"
#include "Netzhaut/src/lib/nhapi/nhapi.h"
#include "ttyr-api/ttyr-api.h"
#include "nhapi/nhapi.h"

#include <stdlib.h>
#include <stdio.h>
Expand Down Expand Up @@ -85,10 +85,12 @@ static void handleInput(
int main(int argc, char **argv_pp)
{
if (handleArgs(argc, argv_pp)) {return 1;}
if (nh_api_initialize(NH_LOADER_SCOPE_SYSTEM, NULL, NULL, 0) != NH_CORE_SUCCESS) {return 1;}
if (nh_api_initialize(NULL, NULL, 0) != NH_CORE_SUCCESS) {return 1;}

nh_api_registerConfig("/etc/ttyr.conf", 14);

ttyr_api_initialize();

TTY_p = ttyr_api_openTTY(NULL, NULL);
if (!TTY_p) {return 1;}

Expand Down
55 changes: 55 additions & 0 deletions src/lib/ttyr-api/ttyr-api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// LICENSE NOTICE ==================================================================================

/**
* TTýr - Terminal Emulator
* Copyright (C) 2022 Dajo Frey
* Published under GNU LGPL. See TTyr/LICENSE.LGPL file.
*/

// INCLUDES ========================================================================================

#include "ttyr-api.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <elf.h>
#include <link.h>
#include <string.h>

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

char TTYR_API_PATH_P[255] = {NULL};

void ttyr_api_initialize()
{
const ElfW(Dyn) *dyn = _DYNAMIC;
const ElfW(Dyn) *rpath = NULL;
const ElfW(Dyn) *runpath = NULL;
const char *strtab = NULL;

for (; dyn->d_tag != DT_NULL; ++dyn) {
if (dyn->d_tag == DT_RPATH) {
rpath = dyn;
} else if (dyn->d_tag == DT_RUNPATH) {
runpath = dyn;
} else if (dyn->d_tag == DT_STRTAB) {
strtab = (const char *)dyn->d_un.d_val;
}
}

assert(strtab != NULL);

if (rpath != NULL) {
sprintf(TTYR_API_PATH_P, strtab + rpath->d_un.d_val);
} 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;
}
}
}
38 changes: 38 additions & 0 deletions src/lib/ttyr-api/ttyr-api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef TTYR_API_H
#define TTYR_API_H

#ifndef DOXYGEN_SHOULD_SKIP_THIS

/**
* TTýr - Terminal Emulator
* Copyright (C) 2022 Dajo Frey
* Published under GNU LGPL. See TTyr/LICENSE.LGPL file.
*/

#include "ttyr-tty.h"
#include "ttyr-terminal.h"

#endif

/** @defgroup api_nhtty nhtty
* @brief Environment for running text based programs.
*
* In this API, TTY stands for TeleType and provides an interface for
* writing text based programs. Using these programs, it processes input streams from either
* standard input or \ref ttyr_tty_sendInput and sends the result to either standard
* output or a \ref ttyr_terminal_Terminal. Text based programs can be either added
* by using \ref ttyr_tty_addDefaultProgram or \ref ttyr_tty_addCustomProgram.
*/

/** @addtogroup api_nhtty
* @{
*/

extern char TTYR_API_PATH_P[255];

void ttyr_api_initialize(
);

/** @} */

#endif // TTYR_API_H
23 changes: 16 additions & 7 deletions src/lib/ttyr-api/ttyr-terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// INCLUDES ========================================================================================

#include "ttyr-terminal.h"
#include "ttyr-api.h"

#include "nhcore/Loader/Loader.h"

// TYPEDEFS ========================================================================================
Expand All @@ -23,32 +25,39 @@ typedef TTYR_TERMINAL_RESULT (*ttyr_terminal_cmd_setViewport_f)(

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

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

static void ttyr_api_initialize() {
if (!initialized && NH_LOADER_P != NULL) {
NH_LOADER_P->addModule_f(name_p, dependencies_pp, 2);
initialized = true;
static bool ttyr_api_load() {
if (loaded) {
return true;
}
if (NH_LOADER_P == NULL || TTYR_API_PATH_P == NULL) {
return false;
}
NH_LOADER_P->addModule_f(name_p, TTYR_API_PATH_P, dependencies_pp, 1);
loaded = true;
return true;
}

ttyr_terminal_Terminal *ttyr_api_openTerminal(
NH_BYTE *config_p, ttyr_tty_TTY *TTY_p)
{
ttyr_api_initialize();
if (!ttyr_api_load()) {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;
}

TTYR_TERMINAL_RESULT ttyr_api_setViewport(
ttyr_terminal_Terminal *Terminal_p, nh_gfx_Viewport *Viewport_p)
{
ttyr_api_initialize();
if (!ttyr_api_load()) {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;
}
36 changes: 25 additions & 11 deletions src/lib/ttyr-api/ttyr-tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
// INCLUDES ========================================================================================

#include "ttyr-tty.h"
#include "ttyr-api.h"

#include "nhcore/Loader/Loader.h"

#include <stdio.h>
#include <stdlib.h>

// TYPEDEFS ========================================================================================

typedef ttyr_tty_TTY *(*ttyr_tty_openTTY_f)(
Expand All @@ -35,56 +40,65 @@ typedef TTYR_TTY_RESULT (*ttyr_tty_cmd_sendEvent_f)(

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

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

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

ttyr_tty_TTY *ttyr_api_openTTY(
NH_BYTE *config_p, ttyr_tty_Interface *Interface_p)
{
ttyr_api_initialize();
if (!ttyr_api_load()) {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;
}

TTYR_TTY_RESULT ttyr_api_closeTTY(
ttyr_tty_TTY *TTY_p)
{
ttyr_api_initialize();
if (!ttyr_api_load()) {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;
}

TTYR_TTY_RESULT ttyr_api_claimStandardIO(
ttyr_tty_TTY *TTY_p)
{
ttyr_api_initialize();
if (!ttyr_api_load()) {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;
}

TTYR_TTY_RESULT ttyr_api_unclaimStandardIO(
ttyr_tty_TTY *TTY_p)
{
ttyr_api_initialize();
if (!ttyr_api_load()) {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;
}

TTYR_TTY_RESULT ttyr_api_sendEvent(
ttyr_tty_TTY *TTY_p, nh_wsi_Event Event)
{
ttyr_api_initialize();
if (!ttyr_api_load()) {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;
}

2 changes: 1 addition & 1 deletion src/lib/ttyr-api/ttyr-tty.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* Published under GNU LGPL. See TTyr/LICENSE.LGPL file.
*/

#include <stdbool.h>
#include "nhapi/nhapi.h"
#include <stdbool.h>

#endif

Expand Down

0 comments on commit 220592a

Please sign in to comment.