2020-01-18 11:38:21 +03: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 12:01:38 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-01-23 00:23:50 +03:00
|
|
|
#include "IRQHandler.h"
|
2019-01-26 08:39:13 +03:00
|
|
|
#include "KeyCode.h"
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <AK/CircularQueue.h>
|
|
|
|
#include <AK/DoublyLinkedList.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <Kernel/Devices/CharacterDevice.h>
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2018-11-02 16:06:48 +03:00
|
|
|
class KeyboardClient;
|
2018-10-30 15:59:29 +03:00
|
|
|
|
2020-01-23 00:23:50 +03:00
|
|
|
class KeyboardDevice final : public IRQHandler
|
2019-05-28 12:53:16 +03:00
|
|
|
, public CharacterDevice {
|
2018-11-01 01:19:15 +03:00
|
|
|
AK_MAKE_ETERNAL
|
2018-10-22 13:58:29 +03:00
|
|
|
public:
|
2019-02-17 02:13:47 +03:00
|
|
|
using Event = KeyEvent;
|
2018-11-02 16:06:48 +03:00
|
|
|
|
2019-07-16 14:44:41 +03:00
|
|
|
static KeyboardDevice& the();
|
2018-10-30 15:59:29 +03:00
|
|
|
|
2019-02-17 10:39:09 +03:00
|
|
|
virtual ~KeyboardDevice() override;
|
|
|
|
KeyboardDevice();
|
2018-10-22 13:58:29 +03:00
|
|
|
|
2018-12-03 02:39:25 +03:00
|
|
|
void set_client(KeyboardClient* client) { m_client = client; }
|
2019-12-31 15:37:38 +03:00
|
|
|
void set_maps(const char* n_map, const char* n_shift_map, const char* n_alt_map, const char* n_altgr_map);
|
2018-10-30 15:59:29 +03:00
|
|
|
|
2018-10-23 11:12:50 +03:00
|
|
|
// ^CharacterDevice
|
2019-07-03 22:17:35 +03:00
|
|
|
virtual ssize_t read(FileDescription&, u8* buffer, ssize_t) override;
|
2019-11-04 16:03:14 +03:00
|
|
|
virtual bool can_read(const FileDescription&) const override;
|
2019-07-03 22:17:35 +03:00
|
|
|
virtual ssize_t write(FileDescription&, const u8* buffer, ssize_t) override;
|
2019-11-04 16:03:14 +03:00
|
|
|
virtual bool can_write(const FileDescription&) const override { return true; }
|
2018-10-23 11:12:50 +03:00
|
|
|
|
2019-01-16 19:20:58 +03:00
|
|
|
private:
|
|
|
|
// ^IRQHandler
|
2020-01-23 00:23:50 +03:00
|
|
|
virtual void handle_irq() override;
|
2019-01-16 19:20:58 +03:00
|
|
|
|
2019-01-21 04:33:01 +03:00
|
|
|
// ^CharacterDevice
|
2019-02-17 10:39:09 +03:00
|
|
|
virtual const char* class_name() const override { return "KeyboardDevice"; }
|
2019-01-21 04:33:01 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void key_state_changed(u8 raw, bool pressed);
|
|
|
|
void update_modifier(u8 modifier, bool state)
|
2019-01-21 09:05:31 +03:00
|
|
|
{
|
|
|
|
if (state)
|
|
|
|
m_modifiers |= modifier;
|
|
|
|
else
|
|
|
|
m_modifiers &= ~modifier;
|
|
|
|
}
|
2018-10-30 17:33:37 +03:00
|
|
|
|
2018-10-30 15:59:29 +03:00
|
|
|
KeyboardClient* m_client { nullptr };
|
2019-01-21 09:05:31 +03:00
|
|
|
CircularQueue<Event, 16> m_queue;
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 m_modifiers { 0 };
|
2019-10-13 22:49:11 +03:00
|
|
|
bool m_caps_lock_on { false };
|
2019-10-14 00:32:26 +03:00
|
|
|
bool m_num_lock_on { false };
|
2019-10-18 00:08:10 +03:00
|
|
|
bool m_has_e0_prefix { false };
|
2018-10-22 13:58:29 +03:00
|
|
|
};
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2018-11-02 16:06:48 +03:00
|
|
|
class KeyboardClient {
|
|
|
|
public:
|
|
|
|
virtual ~KeyboardClient();
|
2019-02-17 10:39:09 +03:00
|
|
|
virtual void on_key_pressed(KeyboardDevice::Event) = 0;
|
2018-11-02 16:06:48 +03:00
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|