Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
fix: Keyboard layout
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainTHD committed Oct 10, 2020
1 parent 1970291 commit 6d7348e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
11 changes: 10 additions & 1 deletion include/std/io/keyboardLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
#ifndef ROMAINOS_KEYBOARDLAYOUT_HPP
#define ROMAINOS_KEYBOARDLAYOUT_HPP

namespace std::io {
namespace std::io::keyboard {
extern char VK_ESC;
extern char VK_DEL;
extern char VK_SHIFT;
extern char VK_ALT;
extern char VK_CTRL;
extern char VK_CAPS_LOCK;

struct KeyboardLayout {
char layout[256];
char VK_ESC;
Expand All @@ -20,6 +27,8 @@ namespace std::io {
QWERTY = 1
} KeyboardLayoutType;

void setLayoutSpecialChars(KeyboardLayoutType type);

extern KeyboardLayout layouts[2];
}

Expand Down
6 changes: 6 additions & 0 deletions src/kernel/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ extern "C" void _start() {
std::io::idt::initIDT();
std::memory::initHeap(0x100000, 0x100000);

std::io::keyboard::addEventHandler([](std::io::keyboard::KeyEvent e) {
if (e.keyCode == std::io::keyboard::VK_ESC) {
std::system::shutdownAndPrint();
}
});

main();
}
52 changes: 29 additions & 23 deletions src/std/io/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ namespace std::io::keyboard {
*/
bool _isCapsLock = false;

Stack<void (*)(KeyEvent)>* _eventHandlers;
/**
* Event handlers
*/
Stack<void (*)(KeyEvent)> _eventHandlers;

/**
* Keyboard to char
Expand All @@ -65,18 +68,18 @@ namespace std::io::keyboard {
break;
}

if (event.keyCode == _keyboardLayout.VK_SHIFT) {
if (event.keyCode == VK_SHIFT) {
_isShift = !_isShift;
}
else if (event.keyCode == _keyboardLayout.VK_ALT) {
else if (event.keyCode == VK_ALT) {
_isAlt = !_isAlt;
}
else if (event.keyCode == _keyboardLayout.VK_CAPS_LOCK) {
else if (event.keyCode == VK_CAPS_LOCK) {
if (event.pressed) {
_isCapsLock = !_isCapsLock;
}
}
else if (event.keyCode == _keyboardLayout.VK_CTRL) {
else if (event.keyCode == VK_CTRL) {
_isCtrl = !_isCtrl;
}

Expand All @@ -91,39 +94,40 @@ namespace std::io::keyboard {
}

void _notifyEvent(byte b) {
if (_eventHandlers == nullptr) {
return;
}

KeyEvent event = _getEvent(b);
Stack<void (*)(KeyEvent)> tmp;
void (*f)(KeyEvent);

while (!_eventHandlers->empty()) {
f = _eventHandlers->pop();
while (!_eventHandlers.empty()) {
f = _eventHandlers.pop();
tmp.push(f);
f(event);
}

while (!tmp.empty()) {
_eventHandlers->push(tmp.pop());
}

if (event.keyCode == _keyboardLayout.VK_ESC) {
std::system::shutdownAndPrint();
_eventHandlers.push(tmp.pop());
}
}

void addEventHandler(void (*f)(KeyEvent)) {
if (_eventHandlers == nullptr) {
_eventHandlers = new Stack<void (*)(KeyEvent)>();
}

_eventHandlers->push(f);
_eventHandlers.push(f);
}

void deleteEventHandler(void (*f)(KeyEvent)) {
// TODO: Passer sur linked list
Stack<void (*)(KeyEvent)> tmp;
void (*f_tmp)(KeyEvent);

while (!_eventHandlers.empty()) {
f_tmp = _eventHandlers.pop();

if (f != f_tmp) {
tmp.push(f_tmp);
}
}

while (!tmp.empty()) {
_eventHandlers.push(tmp.pop());
}
}

/**
Expand All @@ -132,11 +136,13 @@ namespace std::io::keyboard {
* @param layout Layout
*/
void setKeyboardLayout(KeyboardLayoutType layout) {
_keyboardLayout = layouts[layout];
_keyboardLayoutType = layout;
_isCtrl = false;
_isAlt = false;
_isShift = false;
_isCapsLock = false;

setLayoutSpecialChars(layout);
_keyboardLayout = layouts[layout];
}
}
20 changes: 19 additions & 1 deletion src/std/io/keyboardLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#define _layoutQWERTY {\
}

namespace std::io {
namespace std::io::keyboard {
KeyboardLayout layouts[2] = {
{
_layoutAZERTY,
Expand All @@ -52,6 +52,24 @@ namespace std::io {
0x00
},
};

char VK_ESC;
char VK_DEL;
char VK_SHIFT;
char VK_ALT;
char VK_CTRL;
char VK_CAPS_LOCK;

void setLayoutSpecialChars(KeyboardLayoutType type) {
KeyboardLayout layout = layouts[type];

std::io::keyboard::VK_ESC = layout.VK_ESC;
std::io::keyboard::VK_DEL = layout.VK_DEL;
std::io::keyboard::VK_SHIFT = layout.VK_SHIFT;
std::io::keyboard::VK_ALT = layout.VK_ALT;
std::io::keyboard::VK_CTRL = layout.VK_CTRL;
std::io::keyboard::VK_CAPS_LOCK = layout.VK_CAPS_LOCK;
}
}

#undef _layoutAZERTY
Expand Down

0 comments on commit 6d7348e

Please sign in to comment.