Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ dist/
/compile_commands.json

# csv exports
*.csv
*.csv

# ignore CLANGD cache
.cache
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ else
LIB_WIN_PATH = -Ilib/raylib_win/include -Llib/raylib_win/lib -I$(MINGW_PREFIX)/include -L$(MINGW_PREFIX)/lib
endif

ifdef BUILD_FOR_DIST
CFLAGS += -DBUILD_FOR_DIST
endif

LIB_LIN_PATH = -Ilib/raylib_lin/include -Llib/raylib_lin/lib

SRC = src/main.c src/astro.c src/config.c src/ui.c src/rotator.c
Expand Down Expand Up @@ -47,6 +51,9 @@ DIST_MACOS = dist/TLEscope-macOS-Portable
all: linux

linux: bin/TLEscope
ifeq ($(BUILD_FOR_DIST),yes)
@echo "Skipping bundling for distribution build (BUILD_FOR_DIST=yes) the binary can be found in bin."
else
@mkdir -p $(DIST_LINUX)
cp bin/TLEscope $(DIST_LINUX)/
cp -r themes/ $(DIST_LINUX)/
Expand All @@ -55,6 +62,7 @@ linux: bin/TLEscope
cp logo*.png $(DIST_LINUX)/ 2>/dev/null || true
@echo "Linux build bundled in $(DIST_LINUX)/, do not run bin/*"
@echo "Here's your subshell command to run it! (cd $(DIST_LINUX)/ && ./TLEscope)"
endif

macos: bin/TLEscope-macos
@mkdir -p $(DIST_MACOS)
Expand Down
19 changes: 17 additions & 2 deletions src/astro.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,24 @@ bool add_satellite_from_tle(const char* line0, const char* line1, const char* li
}

/* bulk loading of celestial junk from flat files */
void load_tle_data(const char *filename)
void load_tle_data(const char *tlefile)
{
FILE *file = fopen(filename, "r");
char filename[1024];

#if defined (BUILD_FOR_DIST)
//The full path, with /<the tle file> at the end
char fullPath[1024];
const char *homeDir = getenv("XDG_CONFIG_HOME");
if (!homeDir) homeDir = getenv("HOME");
snprintf(fullPath,sizeof(fullPath),"%s/.config/TLEscope/%s", homeDir,tlefile);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XDG_CONFIG_HOME is already the full base directory (e.g. /home/user/.config) per the XDG basedir spec. Appending /.config/ unconditionally means that when the var is set, the path becomes /home/user/.config/.config/TLEscope/....

Correct handling:

const char *configHome = getenv("XDG_CONFIG_HOME");
if (configHome && configHome[0] == '/')
    snprintf(path, sizeof(path), "%s/TLEscope/%s", configHome, file);
else
    snprintf(path, sizeof(path), "%s/.config/TLEscope/%s", getenv("HOME"), file);

The [0] == '/' check: spec says relative paths must be treated as invalid.

Same bug in every copy of this block across config.c and ui.c.


printf("DEBUG: %s\n", fullPath);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug printfs should be removed or gated behind #ifdef DEBUG. Same across all copies.

snprintf(filename,sizeof(filename),fullPath);
#else
snprintf(filename, sizeof(filename),tlefile);
#endif

FILE *file = fopen(filename, "r");
if (!file)
{
printf("Failed to open %s\n", filename);
Expand Down
51 changes: 45 additions & 6 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <stdlib.h>
#include <string.h>



Marker home_location;

// turn hex strings into real colors
Expand All @@ -29,7 +31,7 @@ Color ParseHexColor(const char *hexStr, Color fallback)
}

// read the json file and grab our settings
void LoadAppConfig(const char *filename, AppConfig *config)
void LoadAppConfig(const char *resource, AppConfig *config)
{
// default theme configuration
strcpy(config->theme, "default");
Expand All @@ -42,6 +44,22 @@ void LoadAppConfig(const char *filename, AppConfig *config)
config->show_first_run_dialog = false; //default
config->hint_vsync = true; // default
config->custom_tle_source_count = 0;


char filename[1024];

#if defined (BUILD_FOR_DIST)
//The full path, with /settings.json at the end
char fullPath[1024];
const char *homeDir = getenv("XDG_CONFIG_HOME");
if (!homeDir) homeDir = getenv("HOME");
snprintf(fullPath,sizeof(fullPath),"%s/.config/TLEscope/%s", homeDir,resource);

printf("DEBUG: %s\n", fullPath);
snprintf(filename,sizeof(filename),fullPath);
#else
snprintf(filename, sizeof(filename),resource);
#endif

if (FileExists(filename))
{
Expand Down Expand Up @@ -435,8 +453,11 @@ void LoadAppConfig(const char *filename, AppConfig *config)

// load colors from the selected theme file
char theme_path[256];
#if defined (BUILD_FOR_DIST)
snprintf(theme_path, sizeof(theme_path), "/usr/share/TLEscope/themes/%s/theme.json", config->theme);
#else
snprintf(theme_path, sizeof(theme_path), "themes/%s/theme.json", config->theme);

#endif
if (FileExists(theme_path))
{
char *theme_text = LoadFileText(theme_path);
Expand Down Expand Up @@ -487,15 +508,33 @@ void LoadAppConfig(const char *filename, AppConfig *config)

UnloadFileText(theme_text);
}
}else {
printf("ERROR: Theme files could not be found!\n");
}
}

void SaveAppConfig(const char *filename, AppConfig *config)
void SaveAppConfig(const char *resource, AppConfig *config)
{
char filename[1024];

#if defined (BUILD_FOR_DIST)
//The full path, with /settings.json at the end
char fullPath[1024];
const char *homeDir = getenv("HOME");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LoadAppConfig checks XDG_CONFIG_HOME before HOME, but SaveAppConfig only checks HOME. If a user has XDG_CONFIG_HOME set (Nix, some Arch setups, etc.), the app reads config from the XDG path but writes it back to $HOME/.config/ — changes never persist because next launch it reads from the XDG location again where the old file still lives.

snprintf(fullPath,sizeof(fullPath),"%s/.config/TLEscope/%s", homeDir,resource);

printf("DEBUG: %s\n", fullPath);
snprintf(filename,sizeof(filename),fullPath);
#else
snprintf(filename, sizeof(filename),resource);
#endif

printf("Saving config to %s \n",filename);
FILE *file = fopen(filename, "w");
if (!file)
if (!file){
printf("ERROR couldn't open path: %s \n",filename);
return;

}
fprintf(file, "{\n");
fprintf(file, " \"theme\": \"%s\",\n", config->theme);
fprintf(file, " \"window_width\": %d,\n", config->window_width);
Expand All @@ -513,7 +552,7 @@ void SaveAppConfig(const char *filename, AppConfig *config)
fprintf(file, " \"show_scattering\": %s,\n", config->show_scattering ? "true" : "false");
fprintf(file, " \"show_skybox\": %s,\n", config->show_skybox ? "true" : "false");
fprintf(file, " \"hint_vsync\": %s,\n", config->hint_vsync ? "true" : "false");
fprintf(file, " \"show_first_run_dialog\": %s,\n", config->show_first_run_dialog ? "true" : "false");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

show_first_run_dialog was removed from the saved JSON output — the setting will be lost on every save. Intentional?



if (config->custom_tle_source_count > 0)
{
Expand Down
50 changes: 46 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#include "astro.h"
#include "config.h"

const char* get_resource_path(const char* resource) {
static char fullPath[512];
#if defined (BUILD_FOR_DIST)
snprintf(fullPath, sizeof(fullPath), "/usr/share/TLEscope/%s", resource);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider a compile-time define (-DDATA_DIR="/usr/share/TLEscope") so packagers targeting other prefixes can override without patching.

if (FileExists(fullPath)) {
return fullPath;
} else {
printf("ERROR: resource file was not found at %s, trying fallabck path\n",fullPath);
return resource;
}
#else
return resource;
#endif
}

static const char* GetAssetPath(const char* theme, const char* filename) {
static char path[256];
snprintf(path, sizeof(path), "themes/%s/%s", theme, filename);
if (FileExists(path)) return path;
snprintf(path, sizeof(path), "themes/default/%s", filename);
return path;
return get_resource_path(path);
}
#include "types.h"
#include "ui.h"
Expand Down Expand Up @@ -493,10 +509,36 @@ static bool GetMouseEarthIntersection(Vector2 mouse, bool is_2d, Camera2D cam2d,
}
}



int main(void)
{
LoadAppConfig("settings.json", &cfg);

#if defined(BUILD_FOR_DIST)
//The path to the user's .config/TLEscope. Can we expect a path longer than this?? I sure hope not :omba:
char configPath[1024];

const char *homeDir = getenv("HOME");
if (!homeDir) {
printf("The $HOME env variable is not set. Exiting\n");
return 1;
}
snprintf(configPath,sizeof(configPath),"%s/.config/TLEscope", homeDir);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same XDG issue — hardcodes $HOME/.config/TLEscope without checking XDG_CONFIG_HOME.

mkdir also won't create parent dirs, so this fails if ~/.config/ doesn't exist.


if (!DirectoryExists(configPath)) {
//if the config directory is missing in the user's home directory, create it.
printf("creating config directory, as it didn't exist before at %s \n",configPath);
int status = mkdir(configPath,S_IRWXU);
if ( status !=0 ) {
// Maybe print the meaning of the error code, not just th code itself?
printf("The config directory couldn't be created, with error code %i. Exiting\n",status);
return 1;
}
}

#endif
LoadAppConfig("settings.json", &cfg);

/* window setup and msaa */
SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_WINDOW_RESIZABLE);

Expand Down Expand Up @@ -535,15 +577,15 @@ int main(void)
SetExitKey(0);

/* logo and icon loading */
Image logoImg = LoadImage("logo.png");
Image logoImg = LoadImage(get_resource_path("logo.png")); //DONE
Texture2D logoTex = {0};
if (logoImg.data != NULL)
{
SetWindowIcon(logoImg);
logoTex = LoadTextureFromImage(logoImg);
UnloadImage(logoImg);
}
Image logoLImg = LoadImage("logo.png");
Image logoLImg = LoadImage(get_resource_path("logo_l.png")); //DONE
Texture2D logoLTex = {0};
if (logoLImg.data != NULL)
{
Expand Down
3 changes: 2 additions & 1 deletion src/rotator.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#define _GNU_SOURCE
#include "rotator.h"
#include "astro.h"
#include <math.h>
#include <stdio.h>
Expand All @@ -18,6 +17,8 @@
#include <unistd.h>
#endif

#include "rotator.h"

typedef struct
{
char host[64];
Expand Down
3 changes: 3 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <stdlib.h>
#include <sys/types.h>

//This is toggled by make options. Only here for convinience.
//#define BUILD_FOR_DIST
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented-out #define probably shouldn't be commited. Doing make BUILD_FOR_DIST=1 would be enough for quick dev.


// basic limits and math constants
#define MAX_SATELLITES 15000
#define MAX_MARKERS 100
Expand Down
Loading
Loading