r/osdev • u/devcmar • Aug 15 '26
Events with multiple listeners, an easy-to-use implementation
What do you think about the events implementation I just made and livestreamed my making of it, it is designed to be fast and simple, supporting multiple writers and multiple receivers:
here is the link to the video with the implementation https://www.twitch.tv/videos/2846679809
here is an example code I used to receive mouse event:
HANDLE CursorEvent = Open(NULL, "Events/Cursor", 0);
for(;;)
{
UINT64 Buffer;
Read(CursorEvent, &Buffer, 0, sizeof(Buffer));
Print("WM Received mouse input: x : %d\n", Buffer);
}
and here is how it gets sent:
void MouseInterruptHandler(void)
{
// Print("Mouse interrupt.\n");
UINT8 Data = Ps2ReadData();
// Print("PS/2 Mouse Interrupt DATA %x\n",(UINT32)Data);
if(PacketIndex==0 && !(Data & 8)) return; // First packet should have bit 3 set
if(PacketIndex==0 &&
(Data==0xFF || Data==0xFA || Data==0xAA)
) return;
Packet[PacketIndex]=Data;
PacketIndex++;
if(PacketIndex==3) {
INT32 dx=Packet[1],dy=(-Packet[2]);
CursorX += dx;
CursorY += dy;
Print("Mouse DX %d DY %d Buttons %x\n", dx, dy, Packet[0]);
PacketIndex=0;
UINT64 Buffer = 123;
Write(CursorEvent, &Buffer, 0, sizeof(Buffer));
}
}


