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..f1b508a --- /dev/null +++ b/include/launch_state.h @@ -0,0 +1,17 @@ +#ifndef LAUNCHSTATEH_ +#define LAUNCHSTATEH_ + +#include +#include + +#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 diff --git a/src/launch_state.c b/src/launch_state.c new file mode 100644 index 0000000..ae16661 --- /dev/null +++ b/src/launch_state.c @@ -0,0 +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" + +#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; +} diff --git a/src/ui.c b/src/ui.c index eb2c463..85ab74c 100644 --- a/src/ui.c +++ b/src/ui.c @@ -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)) {