Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "KeyCode.h"
30#include <AK/CircularQueue.h>
31#include <AK/DoublyLinkedList.h>
32#include <AK/Types.h>
33#include <Kernel/Devices/CharacterDevice.h>
34#include <Kernel/Interrupts/IRQHandler.h>
35
36namespace Kernel {
37
38class KeyboardClient;
39
40class KeyboardDevice final : public IRQHandler
41 , public CharacterDevice {
42 AK_MAKE_ETERNAL
43public:
44 using Event = KeyEvent;
45
46 static KeyboardDevice& the();
47
48 virtual ~KeyboardDevice() override;
49 KeyboardDevice();
50
51 void set_client(KeyboardClient* client) { m_client = client; }
52 void set_maps(const char* n_map, const char* n_shift_map, const char* n_alt_map, const char* n_altgr_map);
53
54 // ^CharacterDevice
55 virtual ssize_t read(FileDescription&, u8* buffer, ssize_t) override;
56 virtual bool can_read(const FileDescription&) const override;
57 virtual ssize_t write(FileDescription&, const u8* buffer, ssize_t) override;
58 virtual bool can_write(const FileDescription&) const override { return true; }
59
60 virtual const char* purpose() const override { return class_name(); }
61
62private:
63 // ^IRQHandler
64 virtual void handle_irq(const RegisterState&) override;
65
66 // ^CharacterDevice
67 virtual const char* class_name() const override { return "KeyboardDevice"; }
68
69 void key_state_changed(u8 raw, bool pressed);
70 void update_modifier(u8 modifier, bool state)
71 {
72 if (state)
73 m_modifiers |= modifier;
74 else
75 m_modifiers &= ~modifier;
76 }
77
78 KeyboardClient* m_client { nullptr };
79 CircularQueue<Event, 16> m_queue;
80 u8 m_modifiers { 0 };
81 bool m_caps_lock_on { false };
82 bool m_num_lock_on { false };
83 bool m_has_e0_prefix { false };
84};
85
86class KeyboardClient {
87public:
88 virtual ~KeyboardClient();
89 virtual void on_key_pressed(KeyboardDevice::Event) = 0;
90};
91
92}