Skip to content

Commit

Permalink
New Event System
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtyson123 committed Oct 10, 2023
1 parent ebe6946 commit f91da2a
Show file tree
Hide file tree
Showing 19 changed files with 461 additions and 213 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ Kernel Cleanup
- [x] Console rewrite
- [x] VESA Video Mode
- [x] Kernel Boot Rewrite
- [ ] Chuck a few more event handlers in there
- [ ] Rewrite Event Handlers
- [ ] USB
- [ ] Usable Desktop - Windows Resizing is working but mouse is weird
- [ ] Better Doxygen Documentation
- [ ] Use better c++ coding conventions
- [ ] Codebase cleanup / rewrite
- [ ] Example Telnet Server (GUI) (EMBEDDED)
- [ ] USB
- [ ] HTTP Protocol, DCHP protocol
- [ ] CMAKE
- [ ] Fix Fat32 Filesystem
Expand Down
10 changes: 5 additions & 5 deletions docs/Bugs.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

# Known Bugs
| Status | #Issue Code | Info |
|:------:|:-----------:|:-------------------------------------------------------------------------------------------------------:|
| Fixing | X | Example |
| Known | | Mouse cant have multiple handlers (when there is the console hander the desktop cant handle mouse stuff |
| Status | #Issue Code | Info |
|:------:|:-----------:|:-------------------------------------------------:|
| Fixing | X | Example |
| Known | | Event handlers only trigger for one event handler |
| Known | | Windows have to be weirdly clicked to resize |


# Annoying
Expand All @@ -15,4 +16,3 @@ Bugs that were simple to fix but took forever to find
| 3:44 | amd network driver not init | - PCI read/write check fail AND driver issue | [Github](https://github.com/maxtyson123/max-os/commit/4a0a080a271a9bf27d9cc3701c7d32b40aa2bab3) |


Still got mouse issues but widgets can move yay
7 changes: 3 additions & 4 deletions docs/commit.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# New VESA Boot Console- 3/10/23
- New Output console that is simmilar to textmode console for VESA
- Changed Font foreground and background color prinitng
- Fixed writing ints to the screen more than 100
# OOP Events - 3/10/23
- changed events to be in a more OOP style
- for now old functions are kept for backwards compatibility but may be removed in the future
2 changes: 1 addition & 1 deletion kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ set(
${fs_h}/fat32.h ${fs_c}/fat32.cpp
${fs_h}/filesystem.h ${fs_c}/filesystem.cpp

include/common/rectangle.h include/common/coordinates.h include/common/pair.h include/common/constants.h include/common/time.h include/drivers/clock/clock.h src/drivers/clock/clock.cpp src/common/inputStream.cpp include/common/inputStream.h src/common/outputStream.cpp include/common/outputStream.h include/gui/font.h src/gui/font.cpp include/system/multiboot.h include/drivers/video/vesa.h src/drivers/video/vesa.cpp include/drivers/console/console.h src/drivers/console/console.cpp include/drivers/console/textmode.h src/drivers/console/textmode.cpp include/drivers/console/vesaboot.h src/drivers/console/vesaboot.cpp include/common/logo.h)
include/common/rectangle.h include/common/coordinates.h include/common/pair.h include/common/constants.h include/common/time.h include/drivers/clock/clock.h src/drivers/clock/clock.cpp src/common/inputStream.cpp include/common/inputStream.h src/common/outputStream.cpp include/common/outputStream.h include/gui/font.h src/gui/font.cpp include/system/multiboot.h include/drivers/video/vesa.h src/drivers/video/vesa.cpp include/drivers/console/console.h src/drivers/console/console.cpp include/drivers/console/textmode.h src/drivers/console/textmode.cpp include/drivers/console/vesaboot.h src/drivers/console/vesaboot.cpp include/common/logo.h include/common/eventHandler.h)
#Ignore standard library
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -Wl,nostdlib -Wl,nodefaultlibs")

Expand Down
126 changes: 126 additions & 0 deletions kernel/include/common/eventHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//
// Created by 98max on 9/10/2023.
//

#ifndef MAXOS_COMMON_EVENTHANDLER_H
#define MAXOS_COMMON_EVENTHANDLER_H

#include <common/vector.h>

namespace maxOS{

namespace common{


template <typename EventType> class Event
{
public:
Event(EventType type);
~Event();

EventType type;
};

template <typename EventType> class EventHandler
{
public:
EventHandler();
~EventHandler();
virtual void onEvent(Event<EventType>* event);
};

template <typename EventType> class EventManager
{
protected:
Vector<EventHandler<EventType>*> handlers;

public:
EventManager();
~EventManager();
void connectEventHandler(EventHandler<EventType>* handler);
void disconnectEventHandler(EventHandler<EventType>* handler);
void raiseEvent(Event<EventType>* event);
};


/// ___________________________ Template Implementation ___________________________
template<typename EventType> Event<EventType>::Event(EventType type) {
this->type = type;
}


template<typename EventType> Event<EventType>::~Event() {

}

template<typename EventType> EventHandler<EventType>::EventHandler() {

}


template<typename EventType> EventHandler<EventType>::~EventHandler() {

}

/**
* This function is called when an event is raised
* @tparam EventType The type of event
* @param event The event that was raised
*/
template<typename EventType> void EventHandler<EventType>::onEvent(Event<EventType> *event) {

}

template<typename EventType> EventManager<EventType>::EventManager() {

}

template<typename EventType> EventManager<EventType>::~EventManager() {

}


/**
* Connect an event handler to the event manager
* @tparam EventType The type of event
* @param handler The event handler to connect
*/
template<typename EventType> void EventManager<EventType>::connectEventHandler(EventHandler<EventType> *handler) {
// If the handler is already connected, return
if(handlers.find(handler) != handlers.end()) {
return;
}

handlers.pushBack(handler);

}

/**
* Disconnect an event handler from the event manager
* @tparam EventType The type of event
* @param handler The event handler to disconnect
*/
template<typename EventType> void EventManager<EventType>::disconnectEventHandler(EventHandler<EventType> *handler) {
// If the handler is not connected, return
if(handlers.find(handler) == handlers.end()) {
return;
}

handlers.erase(handler);
}

/**
* Raise an event
* @tparam EventType The type of event
* @param event The event to raise
*/
template<typename EventType> void EventManager<EventType>::raiseEvent(Event<EventType> *event) {

for(typename Vector<EventHandler<EventType>*>::iterator handler = handlers.begin(); handler != handlers.end(); ++handler) {
(*handler)->onEvent(event);
}
}
}
}

#endif //MAXOS_COMMON_EVENTHANDLER_H
21 changes: 16 additions & 5 deletions kernel/include/drivers/clock/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,39 @@
#include <drivers/driver.h>
#include <hardwarecommunication/interrupts.h>
#include <common/vector.h>
#include <common/eventHandler.h>

namespace maxOS {

namespace drivers {

namespace clock {

class ClockEventHandler {
enum ClockEvents{
TIME
};

class TimeEvent : public common::Event<ClockEvents>{
public:
common::Time* time;
TimeEvent(common::Time* time);
~TimeEvent();
};
class ClockEventHandler : public common::EventHandler<ClockEvents>{

public:
ClockEventHandler();
~ClockEventHandler();

void onEvent(common::Event<ClockEvents>* event);

virtual void onTime(const common::Time& time);

};

class Clock: public Driver, public hardwarecommunication::InterruptHandler{
class Clock: public Driver, public hardwarecommunication::InterruptHandler, public common::EventManager<ClockEvents>{
private:
volatile common::uint64_t ticks; // Enusre that the compiler does not optimize this variable out of the code (volatile)
volatile common::uint64_t ticks; // Ensure that the compiler does not optimize this variable out of the code (volatile)

protected:
// Store all the event clockEventHandlers
Expand All @@ -54,8 +67,6 @@ namespace maxOS {
~Clock();

void activate();
void connectClockEventHandler(ClockEventHandler* clockEventHandler);
void disconnectClockEventHandler(ClockEventHandler* clockEventHandler);
void delay(common::uint32_t milliseconds);

common::string getVendorName();
Expand Down
15 changes: 2 additions & 13 deletions kernel/include/drivers/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,15 @@
#include <common/outputStream.h>
#include <memory/memorymanagement.h>
#include <hardwarecommunication/interrupts.h>

#include <common/eventHandler.h>
namespace maxOS
{
namespace drivers {

class DriverEventHandler
{
public:
DriverEventHandler();
~DriverEventHandler();

//Todo: yea... this is a bit of a mess. I should probably make a class for the event itself.
};

class Driver {
protected:
common::OutputStream* driverMessageStream;
common::Vector<DriverEventHandler*> driverEventHandlers;

public:
Driver(common::OutputStream* driverMessageStream = 0);
Expand All @@ -36,9 +27,6 @@ namespace maxOS
void errorMessage(int intToWrite);
void errorMessage(common::uint32_t hexToWrite);

void connectDriverEventHandler(DriverEventHandler* driverEventHandler);
void disconnectDriverEventHandler(DriverEventHandler* driverEventHandler);

virtual void activate();
virtual void deactivate();
virtual void initialise();
Expand All @@ -48,6 +36,7 @@ namespace maxOS
virtual common::string getDeviceName();
};

//NOTE: Driver doesn't use the EventHandler class because it doesn't need to be connected to anything (May want to change this later)
class DriverSelectorEventHandler
{
public:
Expand Down
41 changes: 36 additions & 5 deletions kernel/include/drivers/ethernet/ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <common/types.h>
#include <drivers/driver.h>
#include <common/vector.h>
#include <common/eventHandler.h>

namespace maxOS{

Expand All @@ -17,21 +18,53 @@ namespace maxOS{

typedef common::uint64_t MediaAccessControlAddress;

class EthernetDriverEventHandler
enum EthernetDriverEvents{
BEFORE_SEND,
DATA_SENT,
DATA_RECEIVED
};


class BeforeSendEvent : public common::Event<EthernetDriverEvents>{
public:
common::uint8_t* buffer;
common::uint32_t size;
BeforeSendEvent(common::uint8_t* buffer, common::uint32_t size);
~BeforeSendEvent();
};

class DataSentEvent : public common::Event<EthernetDriverEvents>{
public:
common::uint8_t* buffer;
common::uint32_t size;
DataSentEvent(common::uint8_t* buffer, common::uint32_t size);
~DataSentEvent();
};

class DataReceivedEvent : public common::Event<EthernetDriverEvents>{
public:
common::uint8_t* buffer;
common::uint32_t size;
DataReceivedEvent(common::uint8_t* buffer, common::uint32_t size);
~DataReceivedEvent();
};

class EthernetDriverEventHandler : public common::EventHandler<EthernetDriverEvents>
{
public:
EthernetDriverEventHandler();
~EthernetDriverEventHandler();

virtual void onEvent(common::Event<EthernetDriverEvents>* event);

virtual void BeforeSend(common::uint8_t* buffer, common::uint32_t size);
virtual void DataSent(common::uint8_t* buffer, common::uint32_t size);
virtual bool DataReceived(common::uint8_t* buffer, common::uint32_t size);
};

class EthernetDriver : public Driver
class EthernetDriver : public Driver, public common::EventManager<EthernetDriverEvents>
{
protected:
common::Vector<EthernetDriverEventHandler*> ethernetEventHandlers;
virtual void DoSend(common::uint8_t* buffer, common::uint32_t size);
void FireDataReceived(common::uint8_t* buffer, common::uint32_t size);
void FireDataSent(common::uint8_t* buffer, common::uint32_t size);
Expand All @@ -40,12 +73,10 @@ namespace maxOS{
EthernetDriver(common::OutputStream* ethernetMessageStream);
~EthernetDriver();

common::string GetDeviceName();
static MediaAccessControlAddress CreateMediaAccessControlAddress(common::uint8_t digit1, common::uint8_t digit2, common::uint8_t digit3, common::uint8_t digit4, common::uint8_t digit5, common::uint8_t digit6);
virtual MediaAccessControlAddress GetMediaAccessControlAddress();

void Send(common::uint8_t* buffer, common::uint32_t size);
void ConnectEventHandler(EthernetDriverEventHandler* ethernetDriverEventHandler);
};

}
Expand Down
Loading

0 comments on commit f91da2a

Please sign in to comment.