diff --git a/src/internal.cpp b/src/internal.cpp index e44032c..d4ef7e9 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -35,3 +35,15 @@ void sendSignal(pid_t pid, int signalNumber) { void onSignal(int signalNumber, void(*callback)(int)) { signal(signalNumber, callback); } + +InterruptEntry *getInterrupTable(){ + return (InterruptEntry*)((char*)getSharedMemory() + sizeof(Registers) + 1024/*reserved*/); +} + +bool callEvent(EventType type) { + auto ivt = getInterrupTable(); + if(ivt[type].pid != 0){ + sendSignal(ivt[type].pid, ivt[type].signum); + } + return true; +} diff --git a/src/internal.h b/src/internal.h index 2365c6e..a34a42b 100644 --- a/src/internal.h +++ b/src/internal.h @@ -7,6 +7,8 @@ #include "GraphicMode.h" #include "Color.h" #include +#include +#include /** * the Control Registers @@ -47,7 +49,10 @@ struct Impl { int sharedMemoryPid; key_t sharedMemoryKey; int sharedMemorySize; + int interruptEntyCount = 64; + std::vector> eventTable; }; + extern Impl impl; void *getSharedMemory(); @@ -67,6 +72,9 @@ void sendSignal(pid_t pid, int signalNumber); */ void onSignal(int signalNumber, void(*callback)(int)); + +InterruptEntry *getInterrupTable(); + /** * set layout version. * @param newValue new layout version number. diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 1b63d02..27350f4 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -1,8 +1,28 @@ #include "vkvm.h" #include "internal.h" +#include +#include void initialize(int pid) { impl.sharedMemoryPid = pid; impl.sharedMemoryKey = 892348; impl.sharedMemorySize = 0; } + +bool registerEvent(EventType type, std::function handler) { + int signum = SIGUSR1 + impl.eventTable.size(); + + auto ivt = getInterrupTable(); + ivt[type].pid = getpid(); + ivt[type].signum = signum; + + onSignal(signum, [](int sig){ + if(sig >= SIGUSR1){ + if((sig - SIGUSR1) < impl.eventTable.size()){ + impl.eventTable[sig - SIGUSR1](); + } + } + }); + + return true; +}