libinput Generic way of gathering user commandline input
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m49s

This commit is contained in:
2026-03-20 18:04:26 +01:00
parent 3e44106752
commit bf1e8e8573
22 changed files with 310 additions and 202 deletions

4
libinput/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
*.o
*.json
docs/
.cache/

14
libinput/Makefile Normal file
View File

@@ -0,0 +1,14 @@
include ../make/ufuncs.mk
$(eval $(call add_include,libsystem))
$(eval $(call add_include,libstring))
$(eval $(call add_include,libmalloc))
$(eval $(call add_include,libaux))
$(eval $(call add_include,libterminal))
$(eval $(call add_include,libarena))
$(eval $(call add_include,libkb))
$(eval $(call add_include,libprocess))
libname := libinput
include ../make/lib.mk

1
libinput/build/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.a

135
libinput/in_gb.c Normal file
View File

@@ -0,0 +1,135 @@
#include <in_gb.h>
#include <malloc.h>
#include <stddef.h>
#include <string.h>
void in_gb_init (in_gb_malloc_func_t mallocfn, void* ctx, struct in_gb* gb, size_t capacity) {
gb->buffer = mallocfn (ctx, capacity);
if (gb->buffer == NULL)
return;
memset (gb->buffer, 0, capacity);
gb->size = capacity;
gb->gap_start = 0;
gb->gap_end = capacity;
}
void in_gb_fini (in_gb_free_func_t freefn, void* ctx, struct in_gb* gb) {
freefn (ctx, gb->buffer);
memset (gb, 0, sizeof (*gb));
}
void in_gb_grow (in_gb_realloc_func_t reallocfn, void* ctx, struct in_gb* gb) {
size_t old_size = gb->size;
size_t new_size = gb->size * 2;
char* new = reallocfn (ctx, gb->buffer, old_size, new_size);
if (new == NULL)
return;
gb->buffer = new;
size_t post_gap_size = old_size - gb->gap_end;
size_t new_gap_end = new_size - post_gap_size;
memmove (gb->buffer + new_gap_end, gb->buffer + gb->gap_end, post_gap_size);
gb->gap_end = new_gap_end;
gb->size = new_size;
}
void in_gb_move (struct in_gb* gb, size_t pos) {
if (pos > (gb->size - (gb->gap_end - gb->gap_start)))
pos = (gb->size - (gb->gap_end - gb->gap_start));
while (gb->gap_start > pos) {
gb->gap_start--;
gb->gap_end--;
gb->buffer[gb->gap_end] = gb->buffer[gb->gap_start];
}
while (gb->gap_start < pos) {
gb->buffer[gb->gap_start] = gb->buffer[gb->gap_end];
gb->gap_start++;
gb->gap_end++;
}
}
void in_gb_insert (in_gb_realloc_func_t reallocfn, void* ctx, struct in_gb* gb, char c) {
if (gb->gap_start == gb->gap_end)
in_gb_grow (reallocfn, ctx, gb);
gb->buffer[gb->gap_start] = c;
gb->gap_start++;
}
void in_gb_backspace (struct in_gb* gb) {
if (gb->gap_start > 0)
gb->gap_start--;
}
size_t in_gb_length (struct in_gb* gb) { return gb->size - (gb->gap_end - gb->gap_start); }
char* in_gb_string_at (in_gb_malloc_func_t mallocfn, void* ctx, struct in_gb* gb, size_t pos) {
size_t total_size = in_gb_length (gb);
if (pos >= total_size)
return "";
size_t tail_size = total_size - pos;
char* res = mallocfn (ctx, tail_size + 1);
if (res == NULL)
return NULL;
size_t written = 0;
if (pos < gb->gap_start) {
size_t copy = gb->gap_start - pos;
if (copy > tail_size)
copy = tail_size;
memcpy (res, &gb->buffer[pos], copy);
written += copy;
pos += copy;
}
if (written < tail_size) {
size_t gap_size = gb->gap_end - gb->gap_start;
size_t phys_pos = pos + gap_size;
size_t copy = tail_size - written;
memcpy (&res[written], &gb->buffer[phys_pos], copy);
written += copy;
}
res[written] = '\0';
return res;
}
void in_gb_concat (in_gb_realloc_func_t reallocfn, void* ctx, struct in_gb* dest,
struct in_gb* src) {
size_t src_len = in_gb_length (src);
if (src_len == 0)
return;
while ((dest->gap_end - dest->gap_start) < src_len)
in_gb_grow (reallocfn, ctx, dest);
in_gb_move (dest, in_gb_length (dest));
size_t part1_len = src->gap_start;
memcpy (dest->buffer + dest->gap_start, src->buffer, part1_len);
dest->gap_start += part1_len;
size_t part2_len = src->size - src->gap_end;
if (part2_len > 0) {
memcpy (dest->buffer + dest->gap_start, src->buffer + src->gap_end, part2_len);
dest->gap_start += part2_len;
}
}

38
libinput/in_gb.h Normal file
View File

@@ -0,0 +1,38 @@
#ifndef _LIBINPUT_INPUT_GAPBUFFER_H
#define _LIBINPUT_INPUT_GAPBUFFER_H
#include <stddef.h>
struct in_gb {
char* buffer;
size_t size;
size_t gap_start;
size_t gap_end;
};
typedef void* (*in_gb_malloc_func_t) (void*, size_t);
typedef void* (*in_gb_realloc_func_t) (void*, void*, size_t, size_t);
typedef void (*in_gb_free_func_t) (void*, void*);
void in_gb_init (in_gb_malloc_func_t mallocfn, void* ctx, struct in_gb* gb, size_t capacity);
void in_gb_fini (in_gb_free_func_t freefn, void* ctx, struct in_gb* gb);
void in_gb_move (struct in_gb* gb, size_t pos);
void in_gb_insert (in_gb_realloc_func_t reallocfn, void* ctx, struct in_gb* gb, char c);
void in_gb_backspace (struct in_gb* gb);
void in_gb_grow (in_gb_realloc_func_t reallocfn, void* ctx, struct in_gb* gb);
size_t in_gb_length (struct in_gb* gb);
char* in_gb_string_at (in_gb_malloc_func_t mallocfn, void* ctx, struct in_gb* gb, size_t pos);
void in_gb_concat (in_gb_realloc_func_t reallocfn, void* ctx, struct in_gb* dest,
struct in_gb* src);
#endif // _LIBINPUT_INPUT_GAPBUFFER_H

125
libinput/in_input.c Normal file
View File

@@ -0,0 +1,125 @@
#include <in_gb.h>
#include <kb.h>
#include <malloc.h>
#include <minmax.h>
#include <mprintf.h>
#include <process_self.h>
#include <stddef.h>
#include <stdint.h>
#include <streams.h>
#include <string.h>
#include <system.h>
#include <tcursor.h>
#include <tscreen.h>
static void* in_wmalloc (void* ctx, size_t size) {
(void)ctx;
return malloc (size);
}
static void in_wfree (void* ctx, void* mem) {
(void)ctx;
free (mem);
}
static void* in_wrealloc (void* ctx, void* mem, size_t old, size_t new) {
(void)ctx, (void)old;
return realloc (mem, new);
}
struct edit_line {
struct in_gb gb;
size_t cursor;
};
void in_stream_read_line (const char* prompt, char* buffer, size_t max) {
struct edit_line edit_line;
memset (&edit_line, 0, sizeof (edit_line));
const char* render = NULL;
in_gb_init (&in_wmalloc, NULL, &edit_line.gb, max);
edit_line.cursor = 0;
mprintf ("%s", prompt);
for (;;) {
uint8_t ch;
if (stream_read (process_get_pgid (), STREAM_IN, &ch, 1) <= 0) {
sched ();
continue;
}
if (ch == '\n')
break;
in_gb_move (&edit_line.gb, edit_line.cursor);
switch (ch) {
case '\b':
if (edit_line.cursor > 0) {
edit_line.cursor--;
in_gb_backspace (&edit_line.gb);
mprintf (ANSIQ_CUR_LEFT (1));
char* tail = in_gb_string_at (&in_wmalloc, NULL, &edit_line.gb, edit_line.cursor);
mprintf ("%s" ANSIQ_SCR_CLR2LEND, tail);
int move_back = strlen (tail);
if (move_back > 0)
mprintf (ANSIQ_DYN_CUR_LEFT, move_back);
}
break;
case KB_DELETE:
if (edit_line.cursor < in_gb_length (&edit_line.gb)) {
edit_line.gb.gap_end++;
char* tail = in_gb_string_at (&in_wmalloc, NULL, &edit_line.gb, edit_line.cursor);
mprintf ("%s" ANSIQ_SCR_CLR2LEND, tail);
int move_back = strlen (tail);
if (move_back > 0)
mprintf (ANSIQ_DYN_CUR_LEFT, move_back);
}
break;
case KB_LEFT:
if (edit_line.cursor > 0) {
edit_line.cursor--;
mprintf (ANSIQ_CUR_LEFT (1));
}
break;
case KB_RIGHT:
if (edit_line.cursor < in_gb_length (&edit_line.gb)) {
edit_line.cursor++;
mprintf (ANSIQ_CUR_RIGHT (1));
}
break;
default:
if (isprint (ch)) {
in_gb_insert (&in_wrealloc, NULL, &edit_line.gb, ch);
edit_line.cursor++;
if (edit_line.cursor == in_gb_length (&edit_line.gb)) {
mprintf ("%c", ch);
} else {
char* tail = in_gb_string_at (&in_wmalloc, NULL, &edit_line.gb, edit_line.cursor - 1);
size_t move_back = strlen (tail) - 1;
mprintf ("%s" ANSIQ_DYN_CUR_LEFT, tail, move_back);
}
}
break;
}
}
mprintf ("\n");
render = in_gb_string_at (&in_wmalloc, NULL, &edit_line.gb, 0);
if (render != NULL) {
size_t len_render = strlen (render);
memcpy (buffer, render, min (len_render, max));
}
in_gb_fini (&in_wfree, NULL, &edit_line.gb);
}

8
libinput/in_input.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef _LIBINPUT_IN_INPUT_H
#define _LIBINPUT_IN_INPUT_H
#include <stddef.h>
void in_stream_read_line (const char* prompt, char* buffer, size_t max);
#endif // _LIBINPUT_IN_INPUT_H

5
libinput/src.mk Normal file
View File

@@ -0,0 +1,5 @@
c += in_gb.c \
in_input.c
o += in_gb.o \
in_input.o