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_
#include <stdbool.h>
#include <stdio.h>
#include <sys/stat.h>
#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

View File

@ -1,32 +1,52 @@
// 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_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;
}

View File

@ -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),