feat: save user and session as strings

This commit is contained in:
javalsai 2025-06-29 18:50:35 +02:00
parent f0e962ad50
commit bceb13b844
Signed by: javalsai
SSH Key Fingerprint: SHA256:3G83yKhBUWVABVX/vPWH88xnK4+ptMtHkZGCRXD4Mk8
3 changed files with 69 additions and 28 deletions

View File

@ -2,15 +2,16 @@
#define LAUNCHSTATEH_ #define LAUNCHSTATEH_
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "macros.h"
struct LaunchState { struct LaunchState {
int user_opt; char* NNULLABLE username;
int session_opt; char* NNULLABLE session_opt;
}; };
struct LaunchState read_launch_state(); int read_launch_state(struct LaunchState* NNULLABLE state);
bool write_launch_state(struct LaunchState state); bool write_launch_state(struct LaunchState state);
#endif #endif

View File

@ -1,32 +1,52 @@
// Small file for saving last selection // Small file for saving last selection
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "launch_state.h"
#define STATE_DIR "/var/lib/lidm" #define STATE_DIR "/var/lib/lidm"
#define STATE_FILE "/var/lib/lidm/state" #define STATE_FILE "/var/lib/lidm/state"
#include "launch_state.h" #define RWXR_X___ 0750
struct LaunchState read_launch_state() { int read_launch_state(struct LaunchState* NNULLABLE state) {
struct LaunchState state;
state.user_opt = 1;
state.session_opt = 1;
FILE* state_fd = fopen(STATE_FILE, "r"); FILE* state_fd = fopen(STATE_FILE, "r");
if (state_fd == NULL) return state; if (state_fd == NULL) return -1;
if (fscanf(state_fd, "%i;%i", &state.user_opt, &state.session_opt) != 2) {
state.user_opt = 1; *state = (struct LaunchState){
state.session_opt = 1; .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); (void)fclose(state_fd);
return state; return 0;
fail:
(void)fclose(state_fd);
return -1;
} }
bool write_launch_state(struct LaunchState state) { bool write_launch_state(struct LaunchState state) {
FILE* state_fd = fopen(STATE_FILE, "w"); FILE* state_fd = fopen(STATE_FILE, "w");
if (state_fd == NULL) { 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"); state_fd = fopen(STATE_FILE, "w");
if (state_fd == NULL) return false; 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); (void)fclose(state_fd);
return true; return true;
} }

View File

@ -21,6 +21,7 @@
#include "efield.h" #include "efield.h"
#include "keys.h" #include "keys.h"
#include "launch_state.h" #include "launch_state.h"
#include "log.h"
#include "ofield.h" #include "ofield.h"
#include "sessions.h" #include "sessions.h"
#include "ui.h" #include "ui.h"
@ -199,15 +200,33 @@ int load(struct Vector* users, struct Vector* sessions) {
of_user = ofield_new(users->length); of_user = ofield_new(users->length);
of_passwd = ofield_new(0); of_passwd = ofield_new(0);
struct LaunchState initial_state = read_launch_state(); of_user.current_opt = users->length > 0;
if (initial_state.user_opt > users->length || of_session.current_opt = sessions->length > 0;
initial_state.session_opt > struct LaunchState initial_state;
sessions->length + g_config->behavior.include_defshell) { if (read_launch_state(&initial_state) == 0) {
initial_state.user_opt = 1; for (size_t i = 0; i < users->length; i++) {
initial_state.session_opt = 1; 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 /// PRINTING
const struct uint_point BOXSTART = box_start(); const struct uint_point BOXSTART = box_start();
@ -265,10 +284,11 @@ int load(struct Vector* users, struct Vector* sessions) {
} }
} else { } else {
if (len == 1 && *seq == '\n') { if (len == 1 && *seq == '\n') {
struct LaunchState ls; bool successful_write = write_launch_state((struct LaunchState){
ls.user_opt = of_user.current_opt; .username = st_user().username,
ls.session_opt = of_session.current_opt; .session_opt = st_session(g_config->behavior.include_defshell).name,
write_launch_state(ls); });
if (!successful_write) log_puts("[E] failed to write launch state");
if (!launch(st_user().username, of_passwd.efield.content, if (!launch(st_user().username, of_passwd.efield.content,
st_session(g_config->behavior.include_defshell), st_session(g_config->behavior.include_defshell),