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