Files

111 lines
3.1 KiB
Go

// Fenrir console screen reader.
// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
//go:build linux && cgo
package console
/*
#include <linux/kd.h>
#include <linux/keyboard.h>
#include <linux/input.h>
#include <sys/ioctl.h>
#include <errno.h>
static int ui_key_text(int fd, unsigned int code, int shift, int altgr) {
if (code >= NR_KEYS) return 0;
unsigned char leds = 0;
if (ioctl(fd, KDGKBLED, &leds) < 0) return -errno;
struct kbentry entry = {.kb_table = (shift ? 1 << KG_SHIFT : 0) |
(altgr ? 1 << KG_ALTGR : 0), .kb_index = code};
if (ioctl(fd, KDGKBENT, &entry) < 0) return -errno;
if (KTYP(entry.kb_value) == KT_LETTER && (leds & LED_CAP)) {
entry.kb_table ^= 1 << KG_SHIFT;
if (ioctl(fd, KDGKBENT, &entry) < 0) return -errno;
}
unsigned int value = entry.kb_value;
if (KTYP(value) == KT_LATIN || KTYP(value) == KT_LETTER) return KVAL(value);
// The console encodes Unicode keysyms by inverting their high nibble.
if (KTYP(value) >= 0x10) return value ^ 0xf000;
return 0; // Compose/dead-key sequences are not text by themselves.
}
*/
import "C"
import (
"os"
"syscall"
"unicode"
"git.stormux.org/storm/fenrir/internal/input"
)
func (k *Keyboards) Capture(enabled bool) (result error) {
if enabled {
file, err := os.OpenFile("/dev/tty0", os.O_RDONLY|syscall.O_NOCTTY, 0)
if err != nil {
return err
}
defer func() {
if result != nil {
file.Close()
}
}()
// Neutralize terminal-visible modifiers before taking modal ownership.
for _, device := range k.devices {
for _, code := range k.router.Forwarded(device.fd) {
if err := device.forward(1, code, 0); err != nil {
return err
}
}
if err := device.forward(0, 0, 0); err != nil {
return err
}
}
if k.uiMap != nil {
k.uiMap.Close()
}
k.uiMap = file
} else if k.uiMap != nil {
k.uiMap.Close()
k.uiMap = nil
}
k.router.Capture(enabled)
return nil
}
func (k *Keyboards) ui_text(code uint16, decision input.Decision) error {
if decision.UI == nil || decision.UI.Name != "" || k.uiMap == nil {
return nil
}
shift, altGr := C.int(0), C.int(0)
if decision.UI.Shift {
shift = 1
}
if decision.UI.AltGr {
altGr = 1
}
value := int(C.ui_key_text(C.int(k.uiMap.Fd()), C.uint(code), shift, altGr))
if value < 0 {
return syscall.Errno(-value)
}
if value > 0 && unicode.IsPrint(rune(value)) {
decision.UI.Text = string(rune(value))
}
return nil
}