Merge pull request #42 from rmntgx/save-last-selection

Save last selection
This commit is contained in:
javalsai 2025-06-29 18:55:44 +02:00 committed by GitHub
commit 7d7cf10600
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 107 additions and 2 deletions

View File

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

17
include/launch_state.h Normal file
View File

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

52
src/launch_state.c Normal file
View File

@ -0,0 +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"
#define RWXR_X___ 0750
int read_launch_state(struct LaunchState* NNULLABLE state) {
FILE* state_fd = fopen(STATE_FILE, "r");
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 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, RWXR_X___) == -1) return false;
state_fd = fopen(STATE_FILE, "w");
if (state_fd == NULL) return false;
}
(void)fprintf(state_fd, "%s\n%s\n", state.username, state.session_opt);
(void)fclose(state_fd);
return true;
}

View File

@ -20,6 +20,8 @@
#include "auth.h"
#include "efield.h"
#include "keys.h"
#include "launch_state.h"
#include "log.h"
#include "ofield.h"
#include "sessions.h"
#include "ui.h"
@ -198,6 +200,34 @@ int load(struct Vector* users, struct Vector* sessions) {
of_user = ofield_new(users->length);
of_passwd = ofield_new(0);
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);
}
/// PRINTING
const struct uint_point BOXSTART = box_start();
@ -254,6 +284,12 @@ int load(struct Vector* users, struct Vector* sessions) {
}
} else {
if (len == 1 && *seq == '\n') {
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),
&restore_all, g_config)) {