mirror of
https://github.com/javalsai/lidm.git
synced 2025-07-04 23:08:42 +02:00
chore: dry and modularize a bit
This commit is contained in:
parent
f5445248d3
commit
9f905622a2
@ -1,6 +1,7 @@
|
|||||||
#ifndef UTILH_
|
#ifndef UTILH_
|
||||||
#define UTILH_
|
#define UTILH_
|
||||||
|
|
||||||
|
#include <bits/types/struct_timeval.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@ -12,6 +13,8 @@
|
|||||||
int find_keyname(enum keys* at, const char* name);
|
int find_keyname(enum keys* at, const char* name);
|
||||||
enum keys find_ansi(const char* seq);
|
enum keys find_ansi(const char* seq);
|
||||||
void read_press(u_char* length, char* out);
|
void read_press(u_char* length, char* out);
|
||||||
|
// non blocking, waits up to tv, returns true if actually read
|
||||||
|
bool read_press_nb(u_char* length, char* out, struct timeval* tv);
|
||||||
|
|
||||||
bool utf8_iscont(char byte);
|
bool utf8_iscont(char byte);
|
||||||
size_t utf8len(const char* str);
|
size_t utf8len(const char* str);
|
||||||
|
69
src/ui.c
69
src/ui.c
@ -46,7 +46,7 @@ static void print_session(struct uint_point origin, struct session session,
|
|||||||
static void print_user(struct uint_point origin, struct user user,
|
static void print_user(struct uint_point origin, struct user user,
|
||||||
bool multiple);
|
bool multiple);
|
||||||
static void print_passwd(struct uint_point origin, uint length, bool err);
|
static void print_passwd(struct uint_point origin, uint length, bool err);
|
||||||
static void print_ui();
|
static void scratch_print_ui();
|
||||||
|
|
||||||
// ansi resource: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
|
// ansi resource: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
|
||||||
static struct termios orig_term;
|
static struct termios orig_term;
|
||||||
@ -58,38 +58,12 @@ struct config* g_config = NULL;
|
|||||||
|
|
||||||
static volatile sig_atomic_t need_resize = 0;
|
static volatile sig_atomic_t need_resize = 0;
|
||||||
|
|
||||||
static void process_sigwinch(int) {
|
static void process_sigwinch(int signal) {
|
||||||
need_resize = 1;
|
need_resize = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void resize_ui() {
|
|
||||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &window);
|
|
||||||
|
|
||||||
if (window.ws_row < BOX_HEIGHT + INNER_BOX_OUT_MARGIN * 2 ||
|
|
||||||
window.ws_col < BOX_WIDTH + INNER_BOX_OUT_MARGIN * 2) {
|
|
||||||
printf("\033[2J\033[H"); // Clear screen
|
|
||||||
printf("\x1b[1;31mScreen too small\x1b[0m\n");
|
|
||||||
printf("\x1b[%s;%sm\x1b[2J", g_config->colors.bg, g_config->colors.fg);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\033[2J\033[H"); // Clear screen
|
|
||||||
|
|
||||||
print_ui();
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup(struct config* config) {
|
void setup(struct config* config) {
|
||||||
g_config = config;
|
g_config = config;
|
||||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &window);
|
|
||||||
|
|
||||||
// at least
|
|
||||||
// 2 padding top and bottom for footer and vertical compensation
|
|
||||||
// 2 padding left & right to not overflow footer width
|
|
||||||
if (window.ws_row < BOX_HEIGHT + INNER_BOX_OUT_MARGIN * 2 ||
|
|
||||||
window.ws_col < BOX_WIDTH + INNER_BOX_OUT_MARGIN * 2) {
|
|
||||||
(void)fprintf(stderr, "\x1b[1;31mScreen too small\x1b[0m\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
tcgetattr(STDOUT_FILENO, &orig_term);
|
tcgetattr(STDOUT_FILENO, &orig_term);
|
||||||
term = orig_term; // save term
|
term = orig_term; // save term
|
||||||
@ -197,7 +171,19 @@ void ui_update_ofield(struct opts_field* NNULLABLE self) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static char* unknown_str = "unknown";
|
static char* unknown_str = "unknown";
|
||||||
void print_ui() {
|
void scratch_print_ui() {
|
||||||
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &window);
|
||||||
|
|
||||||
|
if (window.ws_row < BOX_HEIGHT + INNER_BOX_OUT_MARGIN * 2 ||
|
||||||
|
window.ws_col < BOX_WIDTH + INNER_BOX_OUT_MARGIN * 2) {
|
||||||
|
printf("\033[2J\033[H"); // Clear screen
|
||||||
|
printf("\x1b[1;31mScreen too small\x1b[0m\n");
|
||||||
|
printf("\x1b[%s;%sm\x1b[2J", g_config->colors.bg, g_config->colors.fg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\033[2J\033[H"); // Clear screen
|
||||||
|
|
||||||
// hostnames larger won't render properly
|
// hostnames larger won't render properly
|
||||||
const u_char HOSTNAME_SIZE = VALUES_COL - VALUES_SEPR - BOX_HMARGIN;
|
const u_char HOSTNAME_SIZE = VALUES_COL - VALUES_SEPR - BOX_HMARGIN;
|
||||||
char hostname_buf[HOSTNAME_SIZE];
|
char hostname_buf[HOSTNAME_SIZE];
|
||||||
@ -239,8 +225,13 @@ void print_ui() {
|
|||||||
ui_update_cursor_focus();
|
ui_update_cursor_focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define READ_NONBLOCK_DELAY 100000
|
||||||
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
|
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
|
||||||
int load(struct Vector* users, struct Vector* sessions) {
|
int load(struct Vector* users, struct Vector* sessions) {
|
||||||
|
struct timeval tv;
|
||||||
|
tv.tv_sec = 0;
|
||||||
|
tv.tv_usec = READ_NONBLOCK_DELAY; // timeout = 100ms
|
||||||
|
|
||||||
/// SETUP
|
/// SETUP
|
||||||
gusers = users;
|
gusers = users;
|
||||||
gsessions = sessions;
|
gsessions = sessions;
|
||||||
@ -278,7 +269,7 @@ int load(struct Vector* users, struct Vector* sessions) {
|
|||||||
free(initial_state.session_opt);
|
free(initial_state.session_opt);
|
||||||
}
|
}
|
||||||
|
|
||||||
print_ui();
|
scratch_print_ui();
|
||||||
|
|
||||||
/// INTERACTIVE
|
/// INTERACTIVE
|
||||||
u_char len;
|
u_char len;
|
||||||
@ -287,24 +278,10 @@ int load(struct Vector* users, struct Vector* sessions) {
|
|||||||
while (true) {
|
while (true) {
|
||||||
if (need_resize) {
|
if (need_resize) {
|
||||||
need_resize = 0;
|
need_resize = 0;
|
||||||
resize_ui();
|
scratch_print_ui();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Async press handling (magic)
|
if (!read_press_nb(&len, seq, &tv)) continue;
|
||||||
fd_set fds;
|
|
||||||
FD_ZERO(&fds);
|
|
||||||
FD_SET(STDIN_FILENO, &fds);
|
|
||||||
struct timeval tv;
|
|
||||||
tv.tv_sec = 0;
|
|
||||||
tv.tv_usec = 100000; // timeout = 100ms
|
|
||||||
// Wait for input with timeout
|
|
||||||
int ret = select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
|
|
||||||
if (ret < 0) {
|
|
||||||
if (errno == EINTR) continue; // Interrupted by signal
|
|
||||||
}
|
|
||||||
if (ret == 0) continue; // Timeout
|
|
||||||
|
|
||||||
read_press(&len, seq);
|
|
||||||
if (*seq == '\x1b') {
|
if (*seq == '\x1b') {
|
||||||
enum keys ansi_code = find_ansi(seq);
|
enum keys ansi_code = find_ansi(seq);
|
||||||
if (ansi_code != -1) {
|
if (ansi_code != -1) {
|
||||||
|
13
src/util.c
13
src/util.c
@ -1,3 +1,5 @@
|
|||||||
|
#include <asm-generic/errno-base.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -53,6 +55,17 @@ void read_press(u_char* length, char* out) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool read_press_nb(u_char* length, char* out, struct timeval* tv) {
|
||||||
|
fd_set fds;
|
||||||
|
FD_ZERO(&fds);
|
||||||
|
FD_SET(STDIN_FILENO, &fds);
|
||||||
|
int ret = select(STDIN_FILENO + 1, &fds, NULL, NULL, tv);
|
||||||
|
if ((ret < 0 && errno == EINTR) || ret == 0) return false;
|
||||||
|
|
||||||
|
read_press(length, out);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// https://stackoverflow.com/a/48040042
|
// https://stackoverflow.com/a/48040042
|
||||||
static int selret_magic() {
|
static int selret_magic() {
|
||||||
fd_set set;
|
fd_set set;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user