From 64a63fa4be2035bde3f6780285af33554e9705cd Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 10 Jan 2024 14:57:50 +0200 Subject: [PATCH] Userspace: Add loadkeys program to change keymap --- userspace/CMakeLists.txt | 1 + userspace/loadkeys/CMakeLists.txt | 16 ++++++++++++ userspace/loadkeys/main.cpp | 42 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 userspace/loadkeys/CMakeLists.txt create mode 100644 userspace/loadkeys/main.cpp diff --git a/userspace/CMakeLists.txt b/userspace/CMakeLists.txt index bc952251..52651552 100644 --- a/userspace/CMakeLists.txt +++ b/userspace/CMakeLists.txt @@ -12,6 +12,7 @@ set(USERSPACE_PROJECTS id init image + loadkeys ls meminfo mkdir diff --git a/userspace/loadkeys/CMakeLists.txt b/userspace/loadkeys/CMakeLists.txt new file mode 100644 index 00000000..dc341366 --- /dev/null +++ b/userspace/loadkeys/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.26) + +project(loadkeys CXX) + +set(SOURCES + main.cpp +) + +add_executable(loadkeys ${SOURCES}) +target_compile_options(loadkeys PUBLIC -O2 -g) +target_link_libraries(loadkeys PUBLIC libc) + +add_custom_target(loadkeys-install + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/loadkeys ${BANAN_BIN}/ + DEPENDS loadkeys +) diff --git a/userspace/loadkeys/main.cpp b/userspace/loadkeys/main.cpp new file mode 100644 index 00000000..6aa3dbdd --- /dev/null +++ b/userspace/loadkeys/main.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +int try_load_keymap(const char* path) +{ + if (load_keymap(path) == -1) + { + perror("load_keymap"); + return 1; + } + return 0; +} + +int main(int argc, char** argv) +{ + if (argc != 2) + { + fprintf(stderr, "usage: %s KEYMAP\n", argv[0]); + return 1; + } + + struct stat st; + if (stat(argv[1], &st) == 0) + return try_load_keymap(argv[1]); + + char buffer[128]; + strcpy(buffer, "/usr/share/keymaps/"); + strcat(buffer, argv[1]); + + if (stat(buffer, &st) == 0) + return try_load_keymap(buffer); + + strcat(buffer, ".keymap"); + + if (stat(buffer, &st) == 0) + return try_load_keymap(buffer); + + fprintf(stderr, "Keymap '%s' not found\n", argv[1]); + return 1; +}