From 67168cd1db1af115418b520536e1a09e0aebe309 Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Sun, 1 Oct 2023 21:03:26 +0200 Subject: [PATCH] $XDG_CONFIG_HOME --- README.md | 14 ++++--- dist/config.lua | 26 ++++--------- src/runtime_options.c | 85 +++++++++++++++++++++++++------------------ 3 files changed, 65 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index c7952cc..1b4b1bf 100644 --- a/README.md +++ b/README.md @@ -20,15 +20,15 @@ This is my fork of x48-0.6.4 where I deviate from the original code and do my ow ## Usage -`./dist/x48ng --help` +`x48ng --help` -You can use the script `./dist/setup-x48ng-home.sh` or simply run `./dist/x48ng --rom=` +You can use the script `setup-x48ng-home.sh` or simply run `x48ng --rom=` ### manual setup -1. Create `~/.config/x48ng/` -2. Copy `/usr/share/x48ng/ROMs/gxrom-r` (or any other rom) to `~/.config/x48ng/rom` -3. Run `x48ng --print-config > ~/.config/x48ng/config.lua` +1. Create `$XDG_CONFIG_HOME/x48ng` (usually `~/.config/x48ng/`) +2. Copy `/usr/share/x48ng/ROMs/gxrom-r` (or any other rom) to `$XDG_CONFIG_HOME/x48ng/rom` +3. Run `x48ng --print-config > $XDG_CONFIG_HOME/x48ng/config.lua` 4. Run `x48ng` ### Ncurses UI (`--tui`) @@ -50,10 +50,12 @@ _To quit `x48ng --tui` use `F10` or `Shift+End` or `|`_ 3. removed the autotools-based build system and wrote a simple Makefile instead 4. added a x48ng.desktop file and an icon 5. refactoring as a way to explore/understand the codebase +6. drop Xresources +7. link to lua to use it as a config file reader ## Bugs to fix -See https://github.com/gwenhael-le-moine/x48ng/issues +See and report at https://github.com/gwenhael-le-moine/x48ng/issues ## What more I would like to do: diff --git a/dist/config.lua b/dist/config.lua index e729e4c..1b4770d 100644 --- a/dist/config.lua +++ b/dist/config.lua @@ -1,7 +1,8 @@ +-------------------------------------------------------------------------------- -- Configuration file for x48ng - --- `config_dir` is relative to $HOME or absolute -config_dir = ".config/x48ng" +-- This is a comment +-- `config_dir` is relative to $XDG_CONFIG_HOME/, or $HOME/.config/ or absolute +config_dir = "x48ng" -- Pathes are either relative to `config_dir` or absolute rom = "rom" @@ -11,39 +12,26 @@ port1 = "port1" port2 = "port2" pseudo_terminal = false - serial = false - serial_line = "/dev/ttyS0" verbose = false - debugger = false - throttle = false - -------------------- -- User Interface -- -------------------- - -frontend = "tui" -- possible values: "x11", "sdl", "tui" +frontend = "x11" -- possible values: "x11", "sdl", "tui" hide_chrome = false - fullscreen = false - - -netbook = false - - mono = false - gray = false - x11_visual = "default" - +netbook = false font_small = "-*-fixed-bold-r-normal-*-14-*-*-*-*-*-iso8859-1" font_medium = "-*-fixed-bold-r-normal-*-15-*-*-*-*-*-iso8859-1" font_large = "-*-fixed-medium-r-normal-*-20-*-*-*-*-*-iso8859-1" font_devices = "-*-fixed-medium-r-normal-*-12-*-*-*-*-*-iso8859-1" +-------------------------------------------------------------------------------- diff --git a/src/runtime_options.c b/src/runtime_options.c index 995e975..742ec0d 100644 --- a/src/runtime_options.c +++ b/src/runtime_options.c @@ -27,7 +27,7 @@ bool resetOnStartup = false; char* serialLine; -char* configDir = ".config/x48ng"; +char* configDir = "x48ng"; char* config_file = "config.lua"; char* romFileName = NULL; char* ramFileName = NULL; @@ -127,28 +127,46 @@ static inline bool config_read( const char* filename ) static inline void get_absolute_config_dir( char* source, char* dest ) { - char* home; - struct passwd* pwd; - if ( source[ 0 ] != '/' ) { - home = getenv( "HOME" ); - if ( home ) { - strcpy( dest, home ); + char* xdg_config_home = getenv( "XDG_CONFIG_HOME" ); + + if ( xdg_config_home ) { + if ( verbose ) + fprintf( stderr, "XDG_CONFIG_HOME is %s\n", xdg_config_home ); + + strcpy( dest, xdg_config_home ); strcat( dest, "/" ); } else { - pwd = getpwuid( getuid() ); - if ( pwd ) { - strcpy( dest, pwd->pw_dir ); - strcat( dest, "/" ); - } else { + char* home = getenv( "HOME" ); + + if ( home ) { if ( verbose ) - fprintf( stderr, "can\'t figure out your home directory, " - "trying /tmp\n" ); - strcpy( dest, "/tmp" ); + fprintf( stderr, "HOME is %s\n", home ); + + strcpy( dest, home ); + strcat( dest, "/.config/" ); + } else { + struct passwd* pwd = getpwuid( getuid() ); + + if ( pwd ) { + if ( verbose ) + fprintf( stderr, "pwd->pw_dir is %s\n", pwd->pw_dir ); + + strcpy( dest, pwd->pw_dir ); + strcat( dest, "/" ); + } else { + if ( verbose ) + fprintf( stderr, "can\'t figure out your home directory, " + "trying /tmp\n" ); + + strcpy( dest, "/tmp" ); + } } } } + strcat( dest, source ); + if ( dest[ strlen( dest ) ] != '/' ) strcat( dest, "/" ); } @@ -549,9 +567,10 @@ int parse_args( int argc, char* argv[] ) print_config |= verbose; if ( print_config ) { + fprintf( stdout, "--------------------------------------------------------------------------------\n" ); fprintf( stdout, "-- Configuration file for x48ng\n" ); - fprintf( stdout, "\n" ); - fprintf( stdout, "-- `config_dir` is relative to $HOME or absolute\n" ); + fprintf( stdout, "-- This is a comment\n" ); + fprintf( stdout, "-- `config_dir` is relative to $XDG_CONFIG_HOME/, or $HOME/.config/ or absolute\n" ); fprintf( stdout, "config_dir = \"%s\"\n", configDir ); fprintf( stdout, "\n" ); fprintf( stdout, "-- Pathes are either relative to `config_dir` or absolute\n" ); @@ -561,18 +580,17 @@ int parse_args( int argc, char* argv[] ) fprintf( stdout, "port1 = \"%s\"\n", port1FileName ); fprintf( stdout, "port2 = \"%s\"\n", port2FileName ); fprintf( stdout, "\n" ); - fprintf( stdout, "pseudo_terminal = %s\n", useTerminal ? "true" : "false\n" ); - fprintf( stdout, "serial = %s\n", useSerial ? "true" : "false\n" ); + fprintf( stdout, "pseudo_terminal = %s\n", useTerminal ? "true" : "false" ); + fprintf( stdout, "serial = %s\n", useSerial ? "true" : "false" ); fprintf( stdout, "serial_line = \"%s\"\n", serialLine ); fprintf( stdout, "\n" ); - fprintf( stdout, "verbose = %s\n", verbose ? "true" : "false\n" ); - fprintf( stdout, "debugger = %s\n", useDebugger ? "true" : "false\n" ); - fprintf( stdout, "throttle = %s\n", throttle ? "true" : "false\n" ); + fprintf( stdout, "verbose = %s\n", verbose ? "true" : "false" ); + fprintf( stdout, "debugger = %s\n", useDebugger ? "true" : "false" ); + fprintf( stdout, "throttle = %s\n", throttle ? "true" : "false" ); fprintf( stdout, "\n" ); fprintf( stdout, "--------------------\n" ); fprintf( stdout, "-- User Interface --\n" ); fprintf( stdout, "--------------------\n" ); - fprintf( stdout, "\n" ); fprintf( stdout, "frontend = \"" ); switch ( frontend_type ) { case FRONTEND_X11: @@ -585,25 +603,22 @@ int parse_args( int argc, char* argv[] ) fprintf( stdout, "tui" ); break; } - fprintf( stdout, "\" -- possible values: \"x11\", \"sdl\", \"tui\"" ); - - fprintf( stdout, "\n" ); - fprintf( stdout, "hide_chrome = %s\n", hide_chrome ? "true" : "false\n" ); - fprintf( stdout, "fullscreen = %s\n", show_ui_fullscreen ? "true" : "false\n" ); - fprintf( stdout, "\n" ); - fprintf( stdout, "netbook = %s\n", netbook ? "true" : "false\n" ); - fprintf( stdout, "\n" ); - fprintf( stdout, "mono = %s\n", mono ? "true" : "false\n" ); - fprintf( stdout, "gray = %s\n", gray ? "true" : "false\n" ); + fprintf( stdout, "\" -- possible values: \"x11\", \"sdl\", \"tui\"\n" ); + fprintf( stdout, "hide_chrome = %s\n", hide_chrome ? "true" : "false" ); + fprintf( stdout, "fullscreen = %s\n", show_ui_fullscreen ? "true" : "false" ); + fprintf( stdout, "mono = %s\n", mono ? "true" : "false" ); + fprintf( stdout, "gray = %s\n", gray ? "true" : "false" ); fprintf( stdout, "\n" ); fprintf( stdout, "x11_visual = \"%s\"\n", x11_visual ); - fprintf( stdout, "\n" ); + fprintf( stdout, "netbook = %s\n", netbook ? "true" : "false" ); fprintf( stdout, "font_small = \"%s\"\n", smallFont ); fprintf( stdout, "font_medium = \"%s\"\n", mediumFont ); fprintf( stdout, "font_large = \"%s\"\n", largeFont ); fprintf( stdout, "font_devices = \"%s\"\n", connFont ); + fprintf( stdout, "--------------------------------------------------------------------------------\n" ); - exit( 0 ); + if ( !verbose ) + exit( 0 ); } if ( verbose ) { fprintf( stderr, "normalized_config_path = %s\n", normalized_config_path );