keyboard (more more more sorta)
This commit is contained in:
parent
128c8b3d72
commit
f06532e756
1 changed files with 64 additions and 7 deletions
71
src/main.zig
71
src/main.zig
|
@ -56,9 +56,9 @@ pub fn main() !void {
|
|||
for (events) |ev| switch (ev) {
|
||||
.system => {},
|
||||
.keyboard => {
|
||||
if (ev.keyboard.char == 'q') running = false;
|
||||
box3.content = "Pressed " ++ &[_]u8{ev.keyboard.char};
|
||||
box3.makeDirty();
|
||||
if (ev.keyboard.code == 'q') running = false;
|
||||
// box3.content = "Pressed " ++ &[_]u8{ev.keyboard.char};
|
||||
// box3.makeDirty();
|
||||
},
|
||||
};
|
||||
try term.draw();
|
||||
|
@ -68,7 +68,7 @@ pub fn main() !void {
|
|||
|
||||
pub const Terminal = struct {
|
||||
tty: fs.File,
|
||||
buffered_writer: io.BufferedWriter(4096, fs.File.Writer),
|
||||
buffered_writer: io.BufferedWriter(32768, fs.File.Writer),
|
||||
original_termios: os.linux.termios,
|
||||
info: Info,
|
||||
allocator: mem.Allocator,
|
||||
|
@ -89,7 +89,7 @@ pub const Terminal = struct {
|
|||
};
|
||||
errdefer term.tty.close();
|
||||
errdefer term.info.deinit();
|
||||
term.buffered_writer = io.bufferedWriter(term.tty.writer());
|
||||
term.buffered_writer = io.BufferedWriter(32768, fs.File.Writer){ .unbuffered_writer = term.tty.writer() };
|
||||
try term.uncook();
|
||||
try attachSignalHandlers();
|
||||
try term.updateWinSize();
|
||||
|
@ -169,7 +169,28 @@ pub const Terminal = struct {
|
|||
if (os.linux.poll(&pfd, 1, 0) > 0) {
|
||||
var key: [1]u8 = undefined;
|
||||
_ = try self.tty.reader().read(&key);
|
||||
try self.events.append(Event{ .keyboard = .{ .char = key[0] } });
|
||||
if (key[0] != '\x1b') try self.events.append(Event{ .keyboard = .{ .code = key[0], .mods = Modifiers.init(1) } }) else {
|
||||
_ = try self.tty.reader().read(&key);
|
||||
if (key[0] == '[') {
|
||||
var code_list = std.ArrayList(u8).init(self.allocator);
|
||||
defer code_list.deinit();
|
||||
try self.tty.reader().streamUntilDelimiter(code_list.writer(), 'u', 1024);
|
||||
|
||||
const idx1 = mem.indexOfScalar(u8, code_list.items, ';');
|
||||
// TODO: alternate key code
|
||||
if (idx1) |idx| {
|
||||
const idx2 = mem.indexOfScalarPos(u8, code_list.items, idx, ';');
|
||||
// if (idx2) |ix| {
|
||||
const keycode = try fmt.parseInt(u21, code_list.items[0..idx], 10);
|
||||
const modifiers = try fmt.parseInt(u9, if (idx2) |i| code_list.items[idx..i] else code_list.items[idx..], 10);
|
||||
try self.events.append(Event{ .keyboard = .{
|
||||
.code = keycode,
|
||||
.mods = Modifiers.init(modifiers),
|
||||
} });
|
||||
// } else return error.InvalidKittyEscape;
|
||||
} else return error.InvalidKittyEscape; // TODO better error name!!!
|
||||
}
|
||||
}
|
||||
}
|
||||
return try self.events.toOwnedSlice();
|
||||
}
|
||||
|
@ -196,10 +217,46 @@ pub const Terminal = struct {
|
|||
winch,
|
||||
},
|
||||
keyboard: struct {
|
||||
char: u8,
|
||||
code: u21,
|
||||
mods: Modifiers,
|
||||
},
|
||||
};
|
||||
|
||||
pub const Modifiers = struct {
|
||||
shift: bool,
|
||||
alt: bool,
|
||||
ctrl: bool,
|
||||
super: bool,
|
||||
hyper: bool,
|
||||
meta: bool,
|
||||
caps_lock: bool,
|
||||
num_lock: bool,
|
||||
|
||||
pub fn init(bits: u9) Modifiers {
|
||||
const b = bits - 1;
|
||||
return .{
|
||||
.shift = (b & 0b1) > 0,
|
||||
.alt = (b & 0b10) > 0,
|
||||
.ctrl = (b & 0b100) > 0,
|
||||
.super = (b & 0b1000) > 0,
|
||||
.hyper = (b & 0b10000) > 0,
|
||||
.meta = (b & 0b100000) > 0,
|
||||
.caps_lock = (b & 0b1000000) > 0,
|
||||
.num_lock = (b & 0b10000000) > 0,
|
||||
};
|
||||
}
|
||||
// pub const Bits = enum(u8) {
|
||||
// shift = 0b1,
|
||||
// alt = 0b10,
|
||||
// ctrl = 0b100,
|
||||
// super = 0b1000,
|
||||
// hyper = 0b10000,
|
||||
// meta = 0b100000,
|
||||
// caps_lock = 0b1000000,
|
||||
// num_lock = 0b10000000,
|
||||
// };
|
||||
};
|
||||
|
||||
fn updateWinSize(self: *Terminal) !void {
|
||||
var sz: os.linux.winsize = undefined;
|
||||
const ret = os.linux.ioctl(0, os.linux.T.IOCGWINSZ, @intFromPtr(&sz));
|
||||
|
|
Loading…
Add table
Reference in a new issue