2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-16 09:01:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/CircularQueue.h>
|
|
|
|
#include <AK/DoublyLinkedList.h>
|
|
|
|
#include <AK/Types.h>
|
2020-09-18 07:49:51 +00:00
|
|
|
#include <Kernel/API/KeyCode.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <Kernel/Devices/CharacterDevice.h>
|
2020-02-22 17:53:03 +00:00
|
|
|
#include <Kernel/Interrupts/IRQHandler.h>
|
2020-06-24 20:07:28 +00:00
|
|
|
#include <Kernel/Random.h>
|
2020-06-10 08:22:31 +00:00
|
|
|
#include <LibKeyboard/CharacterMap.h>
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2018-11-02 13:06:48 +00:00
|
|
|
class KeyboardClient;
|
2018-10-30 12:59:29 +00:00
|
|
|
|
2020-01-22 21:23:50 +00:00
|
|
|
class KeyboardDevice final : public IRQHandler
|
2019-05-28 09:53:16 +00:00
|
|
|
, public CharacterDevice {
|
2018-10-31 22:19:15 +00:00
|
|
|
AK_MAKE_ETERNAL
|
2018-10-22 10:58:29 +00:00
|
|
|
public:
|
2019-02-16 23:13:47 +00:00
|
|
|
using Event = KeyEvent;
|
2018-11-02 13:06:48 +00:00
|
|
|
|
2020-08-25 01:35:19 +00:00
|
|
|
static void initialize();
|
2019-07-16 11:44:41 +00:00
|
|
|
static KeyboardDevice& the();
|
2018-10-30 12:59:29 +00:00
|
|
|
|
2019-02-17 07:39:09 +00:00
|
|
|
virtual ~KeyboardDevice() override;
|
|
|
|
KeyboardDevice();
|
2018-10-22 10:58:29 +00:00
|
|
|
|
2018-12-02 23:39:25 +00:00
|
|
|
void set_client(KeyboardClient* client) { m_client = client; }
|
2020-08-05 23:03:32 +00:00
|
|
|
void set_maps(const Keyboard::CharacterMapData& character_map, const String& character_map_name);
|
|
|
|
|
|
|
|
const String keymap_name() { return m_character_map.character_map_name(); }
|
2018-10-30 12:59:29 +00:00
|
|
|
|
2018-10-23 08:12:50 +00:00
|
|
|
// ^CharacterDevice
|
2020-09-12 03:11:07 +00:00
|
|
|
virtual KResultOr<size_t> read(FileDescription&, size_t, UserOrKernelBuffer&, size_t) override;
|
2020-04-10 09:44:42 +00:00
|
|
|
virtual bool can_read(const FileDescription&, size_t) const override;
|
2020-09-12 03:11:07 +00:00
|
|
|
virtual KResultOr<size_t> write(FileDescription&, size_t, const UserOrKernelBuffer&, size_t) override;
|
2020-04-10 09:44:42 +00:00
|
|
|
virtual bool can_write(const FileDescription&, size_t) const override { return true; }
|
2018-10-23 08:12:50 +00:00
|
|
|
|
2020-03-05 17:13:55 +00:00
|
|
|
virtual const char* purpose() const override { return class_name(); }
|
|
|
|
|
2019-01-16 16:20:58 +00:00
|
|
|
private:
|
|
|
|
// ^IRQHandler
|
2020-03-09 14:24:29 +00:00
|
|
|
virtual void handle_irq(const RegisterState&) override;
|
2019-01-16 16:20:58 +00:00
|
|
|
|
2019-01-21 01:33:01 +00:00
|
|
|
// ^CharacterDevice
|
2019-02-17 07:39:09 +00:00
|
|
|
virtual const char* class_name() const override { return "KeyboardDevice"; }
|
2019-01-21 01:33:01 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
void key_state_changed(u8 raw, bool pressed);
|
|
|
|
void update_modifier(u8 modifier, bool state)
|
2019-01-21 06:05:31 +00:00
|
|
|
{
|
|
|
|
if (state)
|
|
|
|
m_modifiers |= modifier;
|
|
|
|
else
|
|
|
|
m_modifiers &= ~modifier;
|
|
|
|
}
|
2018-10-30 14:33:37 +00:00
|
|
|
|
2018-10-30 12:59:29 +00:00
|
|
|
KeyboardClient* m_client { nullptr };
|
2019-01-21 06:05:31 +00:00
|
|
|
CircularQueue<Event, 16> m_queue;
|
2019-07-03 19:17:35 +00:00
|
|
|
u8 m_modifiers { 0 };
|
2019-10-13 19:49:11 +00:00
|
|
|
bool m_caps_lock_on { false };
|
2019-10-13 21:32:26 +00:00
|
|
|
bool m_num_lock_on { false };
|
2019-10-17 21:08:10 +00:00
|
|
|
bool m_has_e0_prefix { false };
|
2020-06-24 20:07:28 +00:00
|
|
|
EntropySource m_entropy_source;
|
2020-06-10 08:22:31 +00:00
|
|
|
|
|
|
|
Keyboard::CharacterMap m_character_map = Keyboard::CharacterMap("en");
|
2018-10-22 10:58:29 +00:00
|
|
|
};
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2018-11-02 13:06:48 +00:00
|
|
|
class KeyboardClient {
|
|
|
|
public:
|
|
|
|
virtual ~KeyboardClient();
|
2019-02-17 07:39:09 +00:00
|
|
|
virtual void on_key_pressed(KeyboardDevice::Event) = 0;
|
2018-11-02 13:06:48 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|