Skip to content

Commit 41aa895

Browse files
committed
Mouse support (relative ones only)
1 parent 1029845 commit 41aa895

File tree

1 file changed

+120
-9
lines changed

1 file changed

+120
-9
lines changed

doomgeneric/doomgeneric_nt.c

Lines changed: 120 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include "doomgeneric.h"
2525
#endif
2626

27+
#include "d_event.h"
28+
#include <ntddmou.h>
29+
2730
#define INBVSHIM_TYPE 40001
2831

2932
#define IOCTL_INBVSHIM_SOLID_COLOR_FILL \
@@ -663,6 +666,8 @@ NT_FILE* nt_fopena(const char* filename, const char* mode)
663666
static HANDLE pDevice;
664667
static HANDLE pKeyboardDevice;
665668
static HANDLE hKeyboardEvent;
669+
static HANDLE pMouseDevice = NULL;
670+
static HANDLE hMouseEvent = NULL;
666671

667672
static BOOLEAN IsWindowsXP()
668673
{
@@ -701,7 +706,59 @@ typedef struct tagRGBQUAD
701706
UCHAR rgbReserved;
702707
} RGBQUAD, *LPRGBQUAD;
703708

704-
RGBQUAD gray = { 127, 127, 127, 255 };
709+
MOUSE_INPUT_DATA mouseData[256];
710+
volatile unsigned int mouseDataWrite = 0, mouseDataRead = 0;
711+
volatile int Quit = 0;
712+
static HANDLE MouseThreadHandle;
713+
714+
NTSTATUS
715+
NTAPI
716+
ZwCancelIoFile(
717+
HANDLE FileHandle,
718+
PIO_STATUS_BLOCK IoStatusBlock
719+
);
720+
721+
void MouseThread(void* parm)
722+
{
723+
NTSTATUS Status;
724+
IO_STATUS_BLOCK Iosb;
725+
UNICODE_STRING ustr;
726+
OBJECT_ATTRIBUTES ObjectAttributes;
727+
LARGE_INTEGER ByteOffset;
728+
729+
ByteOffset.QuadPart = 0;
730+
RtlInitUnicodeString(&ustr, L"\\Device\\PointerClass0");
731+
InitializeObjectAttributes(&ObjectAttributes,
732+
&ustr,
733+
OBJ_CASE_INSENSITIVE,
734+
NULL,
735+
NULL);
736+
737+
Status = ZwCreateFile(&pMouseDevice, SYNCHRONIZE | GENERIC_READ | FILE_READ_ATTRIBUTES, &ObjectAttributes, &Iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_DIRECTORY_FILE, NULL, 0);
738+
if (!NT_SUCCESS(Status))
739+
{
740+
nt_printf("Failed to open \\Device\\PointerClass0: 0x%X.\n", Status);
741+
}
742+
while (!Quit)
743+
{
744+
while ((mouseDataWrite - mouseDataRead) >= 16)
745+
{
746+
DG_SleepMs(1);
747+
}
748+
749+
if (Quit)
750+
break;
751+
Status = ZwReadFile(pMouseDevice, hMouseEvent, NULL, NULL, &Iosb, &mouseData[mouseDataWrite & 0xFF], sizeof(MOUSE_INPUT_DATA), &ByteOffset, 0);
752+
if (Status == STATUS_PENDING)
753+
{
754+
ZwCancelIoFile(pMouseDevice, &Iosb);
755+
}
756+
else if (NT_SUCCESS(Status))
757+
{
758+
mouseDataWrite++;
759+
}
760+
}
761+
}
705762

706763
extern VOID NTAPI SetPaletteEntryRGB(ULONG Id, RGBQUAD Rgb);
707764
void DG_Init()
@@ -711,6 +768,7 @@ void DG_Init()
711768
OBJECT_ATTRIBUTES ObjectAttributes;
712769
UNICODE_STRING ustr;
713770
IO_STATUS_BLOCK Iosb;
771+
MOUSE_ATTRIBUTES mouseAttr;
714772
//ULONG solidcolfillparams[5] = { 0, 0, 639, 479, 0 };
715773
unsigned int i = 0;
716774

@@ -747,13 +805,29 @@ void DG_Init()
747805
}
748806
}
749807

750-
RtlInitUnicodeString(&ustr, L"\\Device\\Video0");
808+
RtlInitUnicodeString(&ustr, L"\\Device\\PointerClass0");
751809
InitializeObjectAttributes(&ObjectAttributes,
752810
&ustr,
753811
OBJ_CASE_INSENSITIVE,
754812
NULL,
755813
NULL);
756814
//nt_printf("%s 5\n", __func__);
815+
Status = ZwCreateFile(&pMouseDevice, SYNCHRONIZE | GENERIC_READ | FILE_READ_ATTRIBUTES, &ObjectAttributes, &Iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_DIRECTORY_FILE, NULL, 0);
816+
if (!NT_SUCCESS(Status))
817+
{
818+
nt_printf("Failed to open \\Device\\PointerClass0: 0x%X.\n", Status);
819+
}
820+
else
821+
{
822+
InitializeObjectAttributes(&ObjectAttributes, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
823+
Status = PsCreateSystemThread(&MouseThreadHandle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, NULL, NULL, MouseThread, NULL);
824+
825+
if (!NT_SUCCESS(Status)) {
826+
nt_printf("Failed to create mouse thread: 0x%X.\n", Status);
827+
}
828+
}
829+
830+
//nt_printf("%s 5\n", __func__);
757831

758832
#if 0
759833
Status = NtCreateFile(&pVideoDevice, SYNCHRONIZE | GENERIC_READ | FILE_READ_ATTRIBUTES, &ObjectAttributes, &Iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_DIRECTORY_FILE, NULL, 0);
@@ -811,6 +885,8 @@ nt_ioctl((NT_FILE*)&pVideoDevice, IOCTL_VIDEO_QUERY_NUM_AVAIL_MODES, "doomgeneri
811885
//nt_printf("%s 6\n", __func__);
812886
InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
813887
ZwCreateEvent(&hKeyboardEvent, EVENT_ALL_ACCESS, &ObjectAttributes, 1, 0);
888+
ZwCreateEvent(&hMouseEvent, EVENT_ALL_ACCESS, &ObjectAttributes, 1, 0);
889+
814890
//nt_printf("%s 7\n", __func__);
815891
}
816892
struct tickCountStruct
@@ -1042,23 +1118,56 @@ static int IncAndReturn()
10421118
return keysRead >= 16 ? 0 : 1;
10431119
}
10441120

1045-
NTSTATUS
1046-
NTAPI
1047-
ZwCancelIoFile(
1048-
HANDLE FileHandle,
1049-
PIO_STATUS_BLOCK IoStatusBlock
1050-
);
1051-
10521121
int DG_GetKey(int* pressed, unsigned char* doomKey)
10531122
{
10541123
IO_STATUS_BLOCK Iosb;
10551124
LARGE_INTEGER ByteOffset;
10561125
NTSTATUS Status;
10571126
KEYBOARD_INPUT_DATA inputData;
1127+
MOUSE_INPUT_DATA mouseInputData;
10581128

10591129
RtlZeroMemory(&Iosb, sizeof(Iosb));
10601130
RtlZeroMemory(&ByteOffset, sizeof(ByteOffset));
10611131
RtlZeroMemory(&inputData, sizeof(KEYBOARD_INPUT_DATA));
1132+
RtlZeroMemory(&mouseInputData, sizeof(MOUSE_INPUT_DATA));
1133+
1134+
#if 0
1135+
Status = NtReadFile(pMouseDevice, hMouseEvent, NULL, NULL, &Iosb, &mouseInputData, sizeof(MOUSE_INPUT_DATA), &ByteOffset, NULL);
1136+
if (Status == STATUS_PENDING)
1137+
{
1138+
// No input to read at the moment, cancel read.
1139+
ZwCancelIoFile(pMouseDevice, &Iosb);
1140+
}
1141+
else if (NT_SUCCESS(Status))
1142+
{
1143+
event_t event;
1144+
event.type = ev_mouse;
1145+
event.data1 = 0;
1146+
event.data2 = mouseInputData.LastX;
1147+
event.data3 = mouseInputData.LastY;
1148+
D_PostEvent(&event);
1149+
}
1150+
#endif
1151+
1152+
while (mouseDataRead != mouseDataWrite)
1153+
{
1154+
static event_t mouseEvent = { ev_mouse, 0, 0, 0, 0 };
1155+
MOUSE_INPUT_DATA data = mouseData[mouseDataRead & 0xFF];
1156+
mouseDataRead++;
1157+
1158+
if (!(data.Flags & MOUSE_MOVE_ABSOLUTE)) {
1159+
mouseEvent.data2 = data.LastX * 2;
1160+
mouseEvent.data3 = -data.LastY * 2;
1161+
}
1162+
1163+
if (data.ButtonFlags & MOUSE_BUTTON_1_DOWN)
1164+
mouseEvent.data1 |= 1;
1165+
if (data.ButtonFlags & MOUSE_BUTTON_1_UP)
1166+
mouseEvent.data1 &= ~1;
1167+
1168+
D_PostEvent(&mouseEvent);
1169+
}
1170+
10621171
Status = NtReadFile(pKeyboardDevice, hKeyboardEvent, NULL, NULL, &Iosb, &inputData, sizeof(KEYBOARD_INPUT_DATA), &ByteOffset, NULL);
10631172
if (Status == STATUS_PENDING)
10641173
{
@@ -1135,6 +1244,8 @@ void nt_exit(int code)
11351244
InbvResetDisplay();
11361245
InbvSolidColorFill(0, 0, 639, 479, 0);
11371246
InhibitGraphicsPrint = 0;
1247+
Quit = 1;
1248+
ZwSetEvent(hMouseEvent, NULL);
11381249
nt_printf("Close not implemented!\n");
11391250

11401251
while (1) {

0 commit comments

Comments
 (0)