implement initial event manager
This commit is contained in:
11
c_os/user/KeyEventListener.cc
Normal file
11
c_os/user/KeyEventListener.cc
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "user/KeyEventListener.h"
|
||||||
|
#include "kernel/Globals.h"
|
||||||
|
|
||||||
|
void KeyEventListener::trigger(char c) {
|
||||||
|
this->lastChar = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
char KeyEventListener::waitForKeyEvent() const {
|
||||||
|
scheduler.block();
|
||||||
|
return this->lastChar;
|
||||||
|
}
|
||||||
21
c_os/user/KeyEventListener.h
Normal file
21
c_os/user/KeyEventListener.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#ifndef __KeyEventListener_Include_H_
|
||||||
|
#define __KeyEventListener_Include_H_
|
||||||
|
|
||||||
|
#include "kernel/threads/Thread.h"
|
||||||
|
|
||||||
|
class KeyEventListener {
|
||||||
|
private:
|
||||||
|
KeyEventListener(const KeyEventListener& copy) = delete;
|
||||||
|
|
||||||
|
char lastChar;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Thread& thread; // Thread which contains this listener, so the listener can block the thread
|
||||||
|
|
||||||
|
KeyEventListener(Thread& thread) : thread(thread) {}
|
||||||
|
|
||||||
|
char waitForKeyEvent() const; // Blocks the thread until woken up by manager
|
||||||
|
void trigger(char c); // Gets called from KeyEventManager
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
14
c_os/user/KeyEventManager.cc
Normal file
14
c_os/user/KeyEventManager.cc
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#include "user/KeyEventManager.h"
|
||||||
|
#include "kernel/Globals.h"
|
||||||
|
|
||||||
|
void KeyEventManager::subscribe(KeyEventListener& listener) {
|
||||||
|
this->listeners[this->num_subscribed] = &listener;
|
||||||
|
this->num_subscribed = this->num_subscribed + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void KeyEventManager::broadcast(char c) {
|
||||||
|
for (unsigned int i = 0; i < this->num_subscribed; ++i) {
|
||||||
|
this->listeners[i]->trigger(c); // Sets lastChar
|
||||||
|
scheduler.deblock(&this->listeners[i]->thread);
|
||||||
|
}
|
||||||
|
}
|
||||||
23
c_os/user/KeyEventManager.h
Normal file
23
c_os/user/KeyEventManager.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef __KeyEventManager_Include_H_
|
||||||
|
#define __KeyEventManager_Include_H_
|
||||||
|
|
||||||
|
#include "user/KeyEventListener.h"
|
||||||
|
|
||||||
|
// NOTE: Could do this more generally but we only have key events
|
||||||
|
|
||||||
|
class KeyEventManager {
|
||||||
|
private:
|
||||||
|
KeyEventManager(const KeyEventManager& copy) = delete;
|
||||||
|
|
||||||
|
// TODO: Implement some sort of set structure
|
||||||
|
KeyEventListener* listeners[16]; // This is pretty limited but should be enough
|
||||||
|
unsigned int num_subscribed = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
KeyEventManager() {}
|
||||||
|
|
||||||
|
void subscribe(KeyEventListener& listener);
|
||||||
|
void broadcast(char c); // Unblocks all input waiting threads, I don't have a method to direct input
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user