From 4a83e5d8251deaf2823db493d6713427b2ffcb89 Mon Sep 17 00:00:00 2001 From: rmntgx Date: Wed, 21 May 2025 00:22:04 +0500 Subject: [PATCH 1/6] feature: saving previous selection --- Makefile | 4 ++-- include/launch_state.h | 12 ++++++++++++ src/launch_state.c | 35 +++++++++++++++++++++++++++++++++++ src/ui.c | 14 ++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 include/launch_state.h create mode 100644 src/launch_state.c diff --git a/Makefile b/Makefile index a542b1e..422604c 100644 --- a/Makefile +++ b/Makefile @@ -12,10 +12,10 @@ ALLFLAGS=$(CFLAGS) -I$(IDIR) LIBS=-lpam -_DEPS = log.h util.h ui.h ui_state.h config.h desktop.h auth.h ofield.h efield.h keys.h users.h sessions.h chvt.h macros.h +_DEPS = log.h util.h ui.h ui_state.h config.h desktop.h auth.h ofield.h efield.h keys.h users.h sessions.h chvt.h macros.h launch_state.h DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS)) -_OBJ = main.o log.o util.o ui.o ui_state.o config.o desktop.o auth.o ofield.o efield.o users.o sessions.o chvt.o +_OBJ = main.o log.o util.o ui.o ui_state.o config.o desktop.o auth.o ofield.o efield.o users.o sessions.o chvt.o launch_state.o OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) $(ODIR)/%.o: $(CDIR)/%.c $(DEPS) diff --git a/include/launch_state.h b/include/launch_state.h new file mode 100644 index 0000000..bc05bba --- /dev/null +++ b/include/launch_state.h @@ -0,0 +1,12 @@ +#ifndef _LAUNCHSTATEH_ +#define _LAUNCHSTATEH_ + +struct LaunchState { + int user_opt; + int session_opt; +}; + +struct LaunchState read_launch_state(); +bool write_launch_state(struct LaunchState state); + +#endif diff --git a/src/launch_state.c b/src/launch_state.c new file mode 100644 index 0000000..ec19c02 --- /dev/null +++ b/src/launch_state.c @@ -0,0 +1,35 @@ +// Small file to save last selection + +#define STATE_PATH "/var/lib/lidm/state" + +#include +#include + +#include "launch_state.h" + +static void serialize_lstate(FILE* f, struct LaunchState st) { + fprintf(f, "%i;%i", st.user_opt, st.session_opt); +} + +static void deserialize_lstate(FILE* f, struct LaunchState* st) { + fscanf(f, "%i;%i", &st->user_opt, &st->session_opt); +} + +struct LaunchState read_launch_state() { + struct LaunchState state; + state.user_opt = 1; + state.session_opt = 1; + FILE* f = fopen(STATE_PATH, "r"); + if(!f) return state; + deserialize_lstate(f, &state); + fclose(f); + return state; +} + +bool write_launch_state(struct LaunchState state) { + FILE* f = fopen(STATE_PATH, "w"); + if(!f) return false; + serialize_lstate(f, state); + fclose(f); + return true; +} diff --git a/src/ui.c b/src/ui.c index eb2c463..c0fd3b0 100644 --- a/src/ui.c +++ b/src/ui.c @@ -26,6 +26,7 @@ #include "ui_state.h" #include "users.h" #include "util.h" +#include "launch_state.h" const u_char INPUTS_N = 3; @@ -198,6 +199,14 @@ int load(struct Vector* users, struct Vector* sessions) { of_user = ofield_new(users->length); of_passwd = ofield_new(0); + struct LaunchState initial_state = read_launch_state(); + if(initial_state.user_opt > users->length || initial_state.session_opt > sessions->length + behavior.include_defshell) { + initial_state.user_opt = 1; + initial_state.session_opt = 1; + } + of_user.current_opt = initial_state.user_opt; + of_session.current_opt = initial_state.session_opt; + /// PRINTING const struct uint_point BOXSTART = box_start(); @@ -254,6 +263,11 @@ int load(struct Vector* users, struct Vector* sessions) { } } else { if (len == 1 && *seq == '\n') { + struct LaunchState ls; + ls.user_opt = of_user.current_opt; + ls.session_opt = of_session.current_opt; + write_launch_state(ls); + if (!launch(st_user().username, of_passwd.efield.content, st_session(g_config->behavior.include_defshell), &restore_all, g_config)) { From a08ffa5a759cb22ddb69d456d128a61220f5c352 Mon Sep 17 00:00:00 2001 From: rmntgx Date: Wed, 21 May 2025 13:33:15 +0500 Subject: [PATCH 2/6] fix: simplified code --- src/launch_state.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/launch_state.c b/src/launch_state.c index ec19c02..9ac2ac6 100644 --- a/src/launch_state.c +++ b/src/launch_state.c @@ -7,21 +7,13 @@ #include "launch_state.h" -static void serialize_lstate(FILE* f, struct LaunchState st) { - fprintf(f, "%i;%i", st.user_opt, st.session_opt); -} - -static void deserialize_lstate(FILE* f, struct LaunchState* st) { - fscanf(f, "%i;%i", &st->user_opt, &st->session_opt); -} - struct LaunchState read_launch_state() { struct LaunchState state; state.user_opt = 1; state.session_opt = 1; FILE* f = fopen(STATE_PATH, "r"); if(!f) return state; - deserialize_lstate(f, &state); + fscanf(f, "%i;%i", &state.user_opt, &state.session_opt); fclose(f); return state; } @@ -29,7 +21,7 @@ struct LaunchState read_launch_state() { bool write_launch_state(struct LaunchState state) { FILE* f = fopen(STATE_PATH, "w"); if(!f) return false; - serialize_lstate(f, state); + fprintf(f, "%i;%i", state.user_opt, state.session_opt); fclose(f); return true; } From 1714b477adb6312406b4e36f42b6c7eaadd5a57a Mon Sep 17 00:00:00 2001 From: rmntgx Date: Sun, 29 Jun 2025 10:24:58 +0500 Subject: [PATCH 3/6] fix: compilation error after rebase --- src/ui.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui.c b/src/ui.c index c0fd3b0..26c380b 100644 --- a/src/ui.c +++ b/src/ui.c @@ -200,7 +200,7 @@ int load(struct Vector* users, struct Vector* sessions) { of_passwd = ofield_new(0); struct LaunchState initial_state = read_launch_state(); - if(initial_state.user_opt > users->length || initial_state.session_opt > sessions->length + behavior.include_defshell) { + if(initial_state.user_opt > users->length || initial_state.session_opt > sessions->length + g_config->behavior.include_defshell) { initial_state.user_opt = 1; initial_state.session_opt = 1; } From 16d1325f96f7823289c1d1978e40bf679aaf376a Mon Sep 17 00:00:00 2001 From: rmntgx Date: Sun, 29 Jun 2025 10:56:31 +0500 Subject: [PATCH 4/6] style: formatting and linter satisfaction --- include/launch_state.h | 11 +++++++---- src/launch_state.c | 34 +++++++++++++++++----------------- src/ui.c | 16 +++++++++------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/include/launch_state.h b/include/launch_state.h index bc05bba..eb15b23 100644 --- a/include/launch_state.h +++ b/include/launch_state.h @@ -1,9 +1,12 @@ -#ifndef _LAUNCHSTATEH_ -#define _LAUNCHSTATEH_ +#ifndef LAUNCHSTATEH_ +#define LAUNCHSTATEH_ + +#include +#include struct LaunchState { - int user_opt; - int session_opt; + int user_opt; + int session_opt; }; struct LaunchState read_launch_state(); diff --git a/src/launch_state.c b/src/launch_state.c index 9ac2ac6..381209d 100644 --- a/src/launch_state.c +++ b/src/launch_state.c @@ -1,27 +1,27 @@ -// Small file to save last selection +// Small file for saving last selection #define STATE_PATH "/var/lib/lidm/state" -#include -#include - #include "launch_state.h" struct LaunchState read_launch_state() { - struct LaunchState state; - state.user_opt = 1; - state.session_opt = 1; - FILE* f = fopen(STATE_PATH, "r"); - if(!f) return state; - fscanf(f, "%i;%i", &state.user_opt, &state.session_opt); - fclose(f); - return state; + struct LaunchState state; + state.user_opt = 1; + state.session_opt = 1; + FILE* state_fd = fopen(STATE_PATH, "r"); + if (!state_fd) return state; + if (fscanf(state_fd, "%i;%i", &state.user_opt, &state.session_opt) != 2) { + state.user_opt = 1; + state.session_opt = 1; + } + (void)fclose(state_fd); + return state; } bool write_launch_state(struct LaunchState state) { - FILE* f = fopen(STATE_PATH, "w"); - if(!f) return false; - fprintf(f, "%i;%i", state.user_opt, state.session_opt); - fclose(f); - return true; + FILE* state_fd = fopen(STATE_PATH, "w"); + if (!state_fd) return false; + (void)fprintf(state_fd, "%i;%i", state.user_opt, state.session_opt); + (void)fclose(state_fd); + return true; } diff --git a/src/ui.c b/src/ui.c index 26c380b..f411c75 100644 --- a/src/ui.c +++ b/src/ui.c @@ -20,13 +20,13 @@ #include "auth.h" #include "efield.h" #include "keys.h" +#include "launch_state.h" #include "ofield.h" #include "sessions.h" #include "ui.h" #include "ui_state.h" #include "users.h" #include "util.h" -#include "launch_state.h" const u_char INPUTS_N = 3; @@ -200,9 +200,11 @@ int load(struct Vector* users, struct Vector* sessions) { of_passwd = ofield_new(0); struct LaunchState initial_state = read_launch_state(); - if(initial_state.user_opt > users->length || initial_state.session_opt > sessions->length + g_config->behavior.include_defshell) { - initial_state.user_opt = 1; - initial_state.session_opt = 1; + if (initial_state.user_opt > users->length || + initial_state.session_opt > + sessions->length + g_config->behavior.include_defshell) { + initial_state.user_opt = 1; + initial_state.session_opt = 1; } of_user.current_opt = initial_state.user_opt; of_session.current_opt = initial_state.session_opt; @@ -264,9 +266,9 @@ int load(struct Vector* users, struct Vector* sessions) { } else { if (len == 1 && *seq == '\n') { struct LaunchState ls; - ls.user_opt = of_user.current_opt; - ls.session_opt = of_session.current_opt; - write_launch_state(ls); + ls.user_opt = of_user.current_opt; + ls.session_opt = of_session.current_opt; + write_launch_state(ls); if (!launch(st_user().username, of_passwd.efield.content, st_session(g_config->behavior.include_defshell), From f0e962ad506d234c40bf8c895fcb43071d3babf3 Mon Sep 17 00:00:00 2001 From: rmntgx Date: Sun, 29 Jun 2025 11:51:42 +0500 Subject: [PATCH 5/6] fix: create directory --- include/launch_state.h | 1 + src/launch_state.c | 15 ++++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/launch_state.h b/include/launch_state.h index eb15b23..e63ebe3 100644 --- a/include/launch_state.h +++ b/include/launch_state.h @@ -3,6 +3,7 @@ #include #include +#include struct LaunchState { int user_opt; diff --git a/src/launch_state.c b/src/launch_state.c index 381209d..5a26fdb 100644 --- a/src/launch_state.c +++ b/src/launch_state.c @@ -1,6 +1,7 @@ // Small file for saving last selection -#define STATE_PATH "/var/lib/lidm/state" +#define STATE_DIR "/var/lib/lidm" +#define STATE_FILE "/var/lib/lidm/state" #include "launch_state.h" @@ -8,8 +9,8 @@ struct LaunchState read_launch_state() { struct LaunchState state; state.user_opt = 1; state.session_opt = 1; - FILE* state_fd = fopen(STATE_PATH, "r"); - if (!state_fd) return state; + FILE* state_fd = fopen(STATE_FILE, "r"); + if (state_fd == NULL) return state; if (fscanf(state_fd, "%i;%i", &state.user_opt, &state.session_opt) != 2) { state.user_opt = 1; state.session_opt = 1; @@ -19,8 +20,12 @@ struct LaunchState read_launch_state() { } bool write_launch_state(struct LaunchState state) { - FILE* state_fd = fopen(STATE_PATH, "w"); - if (!state_fd) return false; + FILE* state_fd = fopen(STATE_FILE, "w"); + if (state_fd == NULL) { + if (mkdir(STATE_DIR, 0755) == -1) return false; + state_fd = fopen(STATE_FILE, "w"); + if (state_fd == NULL) return false; + } (void)fprintf(state_fd, "%i;%i", state.user_opt, state.session_opt); (void)fclose(state_fd); return true; From bceb13b844972f6094661a9d5a1bbe53163caec8 Mon Sep 17 00:00:00 2001 From: javalsai Date: Sun, 29 Jun 2025 18:50:35 +0200 Subject: [PATCH 6/6] feat: save user and session as strings --- include/launch_state.h | 9 +++++---- src/launch_state.c | 44 ++++++++++++++++++++++++++++++------------ src/ui.c | 44 ++++++++++++++++++++++++++++++------------ 3 files changed, 69 insertions(+), 28 deletions(-) diff --git a/include/launch_state.h b/include/launch_state.h index e63ebe3..f1b508a 100644 --- a/include/launch_state.h +++ b/include/launch_state.h @@ -2,15 +2,16 @@ #define LAUNCHSTATEH_ #include -#include #include +#include "macros.h" + struct LaunchState { - int user_opt; - int session_opt; + char* NNULLABLE username; + char* NNULLABLE session_opt; }; -struct LaunchState read_launch_state(); +int read_launch_state(struct LaunchState* NNULLABLE state); bool write_launch_state(struct LaunchState state); #endif diff --git a/src/launch_state.c b/src/launch_state.c index 5a26fdb..ae16661 100644 --- a/src/launch_state.c +++ b/src/launch_state.c @@ -1,32 +1,52 @@ // Small file for saving last selection +#include +#include +#include + +#include "launch_state.h" + #define STATE_DIR "/var/lib/lidm" #define STATE_FILE "/var/lib/lidm/state" -#include "launch_state.h" +#define RWXR_X___ 0750 -struct LaunchState read_launch_state() { - struct LaunchState state; - state.user_opt = 1; - state.session_opt = 1; +int read_launch_state(struct LaunchState* NNULLABLE state) { FILE* state_fd = fopen(STATE_FILE, "r"); - if (state_fd == NULL) return state; - if (fscanf(state_fd, "%i;%i", &state.user_opt, &state.session_opt) != 2) { - state.user_opt = 1; - state.session_opt = 1; + if (state_fd == NULL) return -1; + + *state = (struct LaunchState){ + .username = NULL, + .session_opt = NULL, + }; + + size_t num = 0; + if (getline(&state->username, &num, state_fd) < 0) goto fail; + state->username[strcspn(state->username, "\n")] = 0; + + num = 0; + if (getline(&state->session_opt, &num, state_fd) < 0) { + free(state->session_opt); + goto fail; } + state->session_opt[strcspn(state->session_opt, "\n")] = 0; + (void)fclose(state_fd); - return state; + return 0; + +fail: + (void)fclose(state_fd); + return -1; } bool write_launch_state(struct LaunchState state) { FILE* state_fd = fopen(STATE_FILE, "w"); if (state_fd == NULL) { - if (mkdir(STATE_DIR, 0755) == -1) return false; + if (mkdir(STATE_DIR, RWXR_X___) == -1) return false; state_fd = fopen(STATE_FILE, "w"); if (state_fd == NULL) return false; } - (void)fprintf(state_fd, "%i;%i", state.user_opt, state.session_opt); + (void)fprintf(state_fd, "%s\n%s\n", state.username, state.session_opt); (void)fclose(state_fd); return true; } diff --git a/src/ui.c b/src/ui.c index f411c75..85ab74c 100644 --- a/src/ui.c +++ b/src/ui.c @@ -21,6 +21,7 @@ #include "efield.h" #include "keys.h" #include "launch_state.h" +#include "log.h" #include "ofield.h" #include "sessions.h" #include "ui.h" @@ -199,15 +200,33 @@ int load(struct Vector* users, struct Vector* sessions) { of_user = ofield_new(users->length); of_passwd = ofield_new(0); - struct LaunchState initial_state = read_launch_state(); - if (initial_state.user_opt > users->length || - initial_state.session_opt > - sessions->length + g_config->behavior.include_defshell) { - initial_state.user_opt = 1; - initial_state.session_opt = 1; + of_user.current_opt = users->length > 0; + of_session.current_opt = sessions->length > 0; + struct LaunchState initial_state; + if (read_launch_state(&initial_state) == 0) { + for (size_t i = 0; i < users->length; i++) { + struct user* user_i = (struct user*)vec_get(users, i); + if (strcmp(user_i->username, initial_state.username) == 0) { + of_user.current_opt = i + 1; + break; + } + } + + for (size_t i = 0; i < sessions->length; i++) { + struct session* session_i = (struct session*)vec_get(sessions, i); + if (strcmp(session_i->name, initial_state.session_opt) == 0) { + of_session.current_opt = i + 1; + break; + } + } + if (g_config->behavior.include_defshell) { + if (strcmp(st_user().shell, initial_state.session_opt) == 0) + of_session.current_opt = sessions->length + 1; + } + + free(initial_state.username); + free(initial_state.session_opt); } - of_user.current_opt = initial_state.user_opt; - of_session.current_opt = initial_state.session_opt; /// PRINTING const struct uint_point BOXSTART = box_start(); @@ -265,10 +284,11 @@ int load(struct Vector* users, struct Vector* sessions) { } } else { if (len == 1 && *seq == '\n') { - struct LaunchState ls; - ls.user_opt = of_user.current_opt; - ls.session_opt = of_session.current_opt; - write_launch_state(ls); + bool successful_write = write_launch_state((struct LaunchState){ + .username = st_user().username, + .session_opt = st_session(g_config->behavior.include_defshell).name, + }); + if (!successful_write) log_puts("[E] failed to write launch state"); if (!launch(st_user().username, of_passwd.efield.content, st_session(g_config->behavior.include_defshell),