+ register and call event

This commit is contained in:
Julian Hinxlage 2019-11-06 13:41:24 +01:00
parent daa65080e6
commit 2dec86e752
3 changed files with 40 additions and 0 deletions

View File

@ -35,3 +35,15 @@ void sendSignal(pid_t pid, int signalNumber) {
void onSignal(int signalNumber, void(*callback)(int)) { void onSignal(int signalNumber, void(*callback)(int)) {
signal(signalNumber, callback); 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;
}

View File

@ -7,6 +7,8 @@
#include "GraphicMode.h" #include "GraphicMode.h"
#include "Color.h" #include "Color.h"
#include <sys/types.h> #include <sys/types.h>
#include <vector>
#include <functional>
/** /**
* the Control Registers * the Control Registers
@ -47,7 +49,10 @@ struct Impl {
int sharedMemoryPid; int sharedMemoryPid;
key_t sharedMemoryKey; key_t sharedMemoryKey;
int sharedMemorySize; int sharedMemorySize;
int interruptEntyCount = 64;
std::vector<std::function<void()>> eventTable;
}; };
extern Impl impl; extern Impl impl;
void *getSharedMemory(); void *getSharedMemory();
@ -67,6 +72,9 @@ void sendSignal(pid_t pid, int signalNumber);
*/ */
void onSignal(int signalNumber, void(*callback)(int)); void onSignal(int signalNumber, void(*callback)(int));
InterruptEntry *getInterrupTable();
/** /**
* set layout version. * set layout version.
* @param newValue new layout version number. * @param newValue new layout version number.

View File

@ -1,8 +1,28 @@
#include "vkvm.h" #include "vkvm.h"
#include "internal.h" #include "internal.h"
#include <unistd.h>
#include <csignal>
void initialize(int pid) { void initialize(int pid) {
impl.sharedMemoryPid = pid; impl.sharedMemoryPid = pid;
impl.sharedMemoryKey = 892348; impl.sharedMemoryKey = 892348;
impl.sharedMemorySize = 0; impl.sharedMemorySize = 0;
} }
bool registerEvent(EventType type, std::function<void()> 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;
}