1const std = @import("std");
2const uefi = std.os.uefi;
3const Event = uefi.Event;
4const Guid = uefi.Guid;
5const Status = uefi.Status;
6const cc = uefi.cc;
7
8/// Character input devices, e.g. Keyboard
9pub const SimpleTextInputEx = extern struct {
10 _reset: *const fn (*SimpleTextInputEx, bool) callconv(cc) Status,
11 _read_key_stroke_ex: *const fn (*SimpleTextInputEx, *Key) callconv(cc) Status,
12 wait_for_key_ex: Event,
13 _set_state: *const fn (*SimpleTextInputEx, *const u8) callconv(cc) Status,
14 _register_key_notify: *const fn (*SimpleTextInputEx, *const Key, *const fn (*const Key) callconv(cc) Status, **anyopaque) callconv(cc) Status,
15 _unregister_key_notify: *const fn (*SimpleTextInputEx, *const anyopaque) callconv(cc) Status,
16
17 pub const ResetError = uefi.UnexpectedError || error{DeviceError};
18 pub const ReadKeyStrokeError = uefi.UnexpectedError || error{
19 NotReady,
20 DeviceError,
21 Unsupported,
22 };
23 pub const SetStateError = uefi.UnexpectedError || error{
24 DeviceError,
25 Unsupported,
26 };
27 pub const RegisterKeyNotifyError = uefi.UnexpectedError || error{OutOfResources};
28 pub const UnregisterKeyNotifyError = uefi.UnexpectedError || error{InvalidParameter};
29
30 /// Resets the input device hardware.
31 pub fn reset(self: *SimpleTextInputEx, verify: bool) ResetError!void {
32 switch (self._reset(self, verify)) {
33 .success => {},
34 .device_error => return error.DeviceError,
35 else => |status| return uefi.unexpectedStatus(status),
36 }
37 }
38
39 /// Reads the next keystroke from the input device.
40 pub fn readKeyStroke(self: *SimpleTextInputEx) ReadKeyStrokeError!Key {
41 var key: Key = undefined;
42 switch (self._read_key_stroke_ex(self, &key)) {
43 .success => return key,
44 .not_ready => return error.NotReady,
45 .device_error => return error.DeviceError,
46 .unsupported => return error.Unsupported,
47 else => |status| return uefi.unexpectedStatus(status),
48 }
49 }
50
51 /// Set certain state for the input device.
52 pub fn setState(self: *SimpleTextInputEx, state: *const Key.State.Toggle) SetStateError!void {
53 switch (self._set_state(self, @ptrCast(state))) {
54 .success => {},
55 .device_error => return error.DeviceError,
56 .unsupported => return error.Unsupported,
57 else => |status| return uefi.unexpectedStatus(status),
58 }
59 }
60
61 /// Register a notification function for a particular keystroke for the input device.
62 pub fn registerKeyNotify(
63 self: *SimpleTextInputEx,
64 key_data: *const Key,
65 notify: *const fn (*const Key) callconv(cc) Status,
66 ) RegisterKeyNotifyError!uefi.Handle {
67 var handle: uefi.Handle = undefined;
68 switch (self._register_key_notify(self, key_data, notify, &handle)) {
69 .success => return handle,
70 .out_of_resources => return error.OutOfResources,
71 else => |status| return uefi.unexpectedStatus(status),
72 }
73 }
74
75 /// Remove the notification that was previously registered.
76 pub fn unregisterKeyNotify(
77 self: *SimpleTextInputEx,
78 handle: uefi.Handle,
79 ) UnregisterKeyNotifyError!void {
80 switch (self._unregister_key_notify(self, handle)) {
81 .success => {},
82 .invalid_parameter => return error.InvalidParameter,
83 else => |status| return uefi.unexpectedStatus(status),
84 }
85 }
86
87 pub const guid align(8) = Guid{
88 .time_low = 0xdd9e7534,
89 .time_mid = 0x7762,
90 .time_high_and_version = 0x4698,
91 .clock_seq_high_and_reserved = 0x8c,
92 .clock_seq_low = 0x14,
93 .node = [_]u8{ 0xf5, 0x85, 0x17, 0xa6, 0x25, 0xaa },
94 };
95
96 pub const Key = extern struct {
97 input: Input,
98 state: State,
99
100 pub const State = extern struct {
101 shift: Shift,
102 toggle: Toggle,
103
104 pub const Shift = packed struct(u32) {
105 right_shift_pressed: bool,
106 left_shift_pressed: bool,
107 right_control_pressed: bool,
108 left_control_pressed: bool,
109 right_alt_pressed: bool,
110 left_alt_pressed: bool,
111 right_logo_pressed: bool,
112 left_logo_pressed: bool,
113 menu_key_pressed: bool,
114 sys_req_pressed: bool,
115 _pad: u21 = 0,
116 shift_state_valid: bool,
117 };
118
119 pub const Toggle = packed struct(u8) {
120 scroll_lock_active: bool,
121 num_lock_active: bool,
122 caps_lock_active: bool,
123 _pad: u3 = 0,
124 key_state_exposed: bool,
125 toggle_state_valid: bool,
126 };
127 };
128
129 pub const Input = extern struct {
130 scan_code: u16,
131 unicode_char: u16,
132 };
133 };
134};