BOOLEAN EngpPopulateDeviceModeList( _Inout_ PGRAPHICS_DEVICE pGraphicsDevice, _In_ PDEVMODEW pdmDefault) { PWSTR pwsz; PLDEVOBJ pldev; PDEVMODEINFO pdminfo; PDEVMODEW pdm, pdmEnd; ULONG i, cModes = 0; BOOLEAN bModeMatch = FALSE; ASSERT(pGraphicsDevice->pdevmodeInfo == NULL); ASSERT(pGraphicsDevice->pDevModeList == NULL); pwsz = pGraphicsDevice->pDiplayDrivers; /* Loop through the driver names * This is a REG_MULTI_SZ string */ for (; *pwsz; pwsz += wcslen(pwsz) + 1) { TRACE("trying driver: %ls\n", pwsz); /* Try to load the display driver */ pldev = EngLoadImageEx(pwsz, LDEV_DEVICE_DISPLAY); if (!pldev) { ERR("Could not load driver: '%ls'\n", pwsz); continue; } /* Get the mode list from the driver */ pdminfo = LDEVOBJ_pdmiGetModes(pldev, pGraphicsDevice->DeviceObject); if (!pdminfo) { ERR("Could not get mode list for '%ls'\n", pwsz); continue; } /* Attach the mode info to the device */ pdminfo->pdmiNext = pGraphicsDevice->pdevmodeInfo; pGraphicsDevice->pdevmodeInfo = pdminfo; /* Loop all DEVMODEs */ pdmEnd = (DEVMODEW*)((PCHAR)pdminfo->adevmode + pdminfo->cbdevmode); for (pdm = pdminfo->adevmode; (pdm + 1 <= pdmEnd) && (pdm->dmSize != 0); pdm = (DEVMODEW*)((PCHAR)pdm + pdm->dmSize + pdm->dmDriverExtra)) { /* Count this DEVMODE */ cModes++; /* Some drivers like the VBox driver don't fill the dmDeviceName with the name of the display driver. So fix that here. */ wcsncpy(pdm->dmDeviceName, pwsz, CCHDEVICENAME); pdm->dmDeviceName[CCHDEVICENAME - 1] = 0; } // FIXME: release the driver again until it's used? } if (!pGraphicsDevice->pdevmodeInfo || cModes == 0) { ERR("No devmodes\n"); return FALSE; } /* Allocate an index buffer */ pGraphicsDevice->cDevModes = cModes; pGraphicsDevice->pDevModeList = ExAllocatePoolWithTag(PagedPool, cModes * sizeof(DEVMODEENTRY), GDITAG_GDEVICE); if (!pGraphicsDevice->pDevModeList) { ERR("No devmode list\n"); return FALSE; } TRACE("Looking for mode %lux%lux%lu(%lu Hz)\n", pdmDefault->dmPelsWidth, pdmDefault->dmPelsHeight, pdmDefault->dmBitsPerPel, pdmDefault->dmDisplayFrequency); /* Loop through all DEVMODEINFOs */ for (pdminfo = pGraphicsDevice->pdevmodeInfo, i = 0; pdminfo; pdminfo = pdminfo->pdmiNext) { /* Calculate End of the DEVMODEs */ pdmEnd = (DEVMODEW*)((PCHAR)pdminfo->adevmode + pdminfo->cbdevmode); /* Loop through the DEVMODEs */ for (pdm = pdminfo->adevmode; (pdm + 1 <= pdmEnd) && (pdm->dmSize != 0); pdm = (PDEVMODEW)((PCHAR)pdm + pdm->dmSize + pdm->dmDriverExtra)) { TRACE(" %S has mode %lux%lux%lu(%lu Hz)\n", pdm->dmDeviceName, pdm->dmPelsWidth, pdm->dmPelsHeight, pdm->dmBitsPerPel, pdm->dmDisplayFrequency); /* Compare with the default entry */ if (!bModeMatch && pdm->dmBitsPerPel == pdmDefault->dmBitsPerPel && pdm->dmPelsWidth == pdmDefault->dmPelsWidth && pdm->dmPelsHeight == pdmDefault->dmPelsHeight) { pGraphicsDevice->iDefaultMode = i; pGraphicsDevice->iCurrentMode = i; TRACE("Found default entry: %lu '%ls'\n", i, pdm->dmDeviceName); if (pdm->dmDisplayFrequency == pdmDefault->dmDisplayFrequency) { /* Uh oh, even the display frequency matches. */ bModeMatch = TRUE; } } /* Initialize the entry */ pGraphicsDevice->pDevModeList[i].dwFlags = 0; pGraphicsDevice->pDevModeList[i].pdm = pdm; i++; } } return TRUE; }
PGRAPHICS_DEVICE NTAPI EngpRegisterGraphicsDevice( PUNICODE_STRING pustrDeviceName, PUNICODE_STRING pustrDiplayDrivers, PUNICODE_STRING pustrDescription, PDEVMODEW pdmDefault) { PGRAPHICS_DEVICE pGraphicsDevice; PDEVICE_OBJECT pDeviceObject; PFILE_OBJECT pFileObject; NTSTATUS Status; PWSTR pwsz; ULONG i, cj, cModes = 0; SIZE_T cjWritten; BOOL bEnable = TRUE; PDEVMODEINFO pdminfo; PDEVMODEW pdm, pdmEnd; PLDEVOBJ pldev; BOOLEAN bModeMatch = FALSE; TRACE("EngpRegisterGraphicsDevice(%wZ)\n", pustrDeviceName); /* Allocate a GRAPHICS_DEVICE structure */ pGraphicsDevice = ExAllocatePoolWithTag(PagedPool, sizeof(GRAPHICS_DEVICE), GDITAG_GDEVICE); if (!pGraphicsDevice) { ERR("ExAllocatePoolWithTag failed\n"); return NULL; } /* Try to open the driver */ Status = IoGetDeviceObjectPointer(pustrDeviceName, FILE_READ_DATA | FILE_WRITE_DATA, &pFileObject, &pDeviceObject); if (!NT_SUCCESS(Status)) { ERR("Could not open driver %wZ, 0x%lx\n", pustrDeviceName, Status); ExFreePoolWithTag(pGraphicsDevice, GDITAG_GDEVICE); return NULL; } /* Enable the device */ EngFileWrite(pFileObject, &bEnable, sizeof(BOOL), &cjWritten); /* Copy the device and file object pointers */ pGraphicsDevice->DeviceObject = pDeviceObject; pGraphicsDevice->FileObject = pFileObject; /* Copy device name */ RtlStringCbCopyNW(pGraphicsDevice->szNtDeviceName, sizeof(pGraphicsDevice->szNtDeviceName), pustrDeviceName->Buffer, pustrDeviceName->Length); /* Create a win device name (FIXME: virtual devices!) */ swprintf(pGraphicsDevice->szWinDeviceName, L"\\\\.\\DISPLAY%d", (int)giDevNum); /* Allocate a buffer for the strings */ cj = pustrDiplayDrivers->Length + pustrDescription->Length + sizeof(WCHAR); pwsz = ExAllocatePoolWithTag(PagedPool, cj, GDITAG_DRVSUP); if (!pwsz) { ERR("Could not allocate string buffer\n"); ASSERT(FALSE); // FIXME ExFreePoolWithTag(pGraphicsDevice, GDITAG_GDEVICE); return NULL; } /* Copy display driver names */ pGraphicsDevice->pDiplayDrivers = pwsz; RtlCopyMemory(pGraphicsDevice->pDiplayDrivers, pustrDiplayDrivers->Buffer, pustrDiplayDrivers->Length); /* Copy description */ pGraphicsDevice->pwszDescription = pwsz + pustrDiplayDrivers->Length / sizeof(WCHAR); RtlCopyMemory(pGraphicsDevice->pwszDescription, pustrDescription->Buffer, pustrDescription->Length); pGraphicsDevice->pwszDescription[pustrDescription->Length/sizeof(WCHAR)] = 0; /* Initialize the pdevmodeInfo list and default index */ pGraphicsDevice->pdevmodeInfo = NULL; pGraphicsDevice->iDefaultMode = 0; pGraphicsDevice->iCurrentMode = 0; // FIXME: initialize state flags pGraphicsDevice->StateFlags = 0; /* Loop through the driver names * This is a REG_MULTI_SZ string */ for (; *pwsz; pwsz += wcslen(pwsz) + 1) { TRACE("trying driver: %ls\n", pwsz); /* Try to load the display driver */ pldev = EngLoadImageEx(pwsz, LDEV_DEVICE_DISPLAY); if (!pldev) { ERR("Could not load driver: '%ls'\n", pwsz); continue; } /* Get the mode list from the driver */ pdminfo = LDEVOBJ_pdmiGetModes(pldev, pDeviceObject); if (!pdminfo) { ERR("Could not get mode list for '%ls'\n", pwsz); continue; } /* Attach the mode info to the device */ pdminfo->pdmiNext = pGraphicsDevice->pdevmodeInfo; pGraphicsDevice->pdevmodeInfo = pdminfo; /* Loop all DEVMODEs */ pdmEnd = (DEVMODEW*)((PCHAR)pdminfo->adevmode + pdminfo->cbdevmode); for (pdm = pdminfo->adevmode; (pdm + 1 <= pdmEnd) && (pdm->dmSize != 0); pdm = (DEVMODEW*)((PCHAR)pdm + pdm->dmSize + pdm->dmDriverExtra)) { /* Count this DEVMODE */ cModes++; /* Some drivers like the VBox driver don't fill the dmDeviceName with the name of the display driver. So fix that here. */ wcsncpy(pdm->dmDeviceName, pwsz, CCHDEVICENAME); pdm->dmDeviceName[CCHDEVICENAME - 1] = 0; } // FIXME: release the driver again until it's used? } if (!pGraphicsDevice->pdevmodeInfo || cModes == 0) { ERR("No devmodes\n"); ExFreePoolWithTag(pGraphicsDevice, GDITAG_GDEVICE); return NULL; } /* Allocate an index buffer */ pGraphicsDevice->cDevModes = cModes; pGraphicsDevice->pDevModeList = ExAllocatePoolWithTag(PagedPool, cModes * sizeof(DEVMODEENTRY), GDITAG_GDEVICE); if (!pGraphicsDevice->pDevModeList) { ERR("No devmode list\n"); ExFreePoolWithTag(pGraphicsDevice, GDITAG_GDEVICE); return NULL; } TRACE("Looking for mode %lux%lux%lu(%lu Hz)\n", pdmDefault->dmPelsWidth, pdmDefault->dmPelsHeight, pdmDefault->dmBitsPerPel, pdmDefault->dmDisplayFrequency); /* Loop through all DEVMODEINFOs */ for (pdminfo = pGraphicsDevice->pdevmodeInfo, i = 0; pdminfo; pdminfo = pdminfo->pdmiNext) { /* Calculate End of the DEVMODEs */ pdmEnd = (DEVMODEW*)((PCHAR)pdminfo->adevmode + pdminfo->cbdevmode); /* Loop through the DEVMODEs */ for (pdm = pdminfo->adevmode; (pdm + 1 <= pdmEnd) && (pdm->dmSize != 0); pdm = (PDEVMODEW)((PCHAR)pdm + pdm->dmSize + pdm->dmDriverExtra)) { TRACE(" %S has mode %lux%lux%lu(%lu Hz)\n", pdm->dmDeviceName, pdm->dmPelsWidth, pdm->dmPelsHeight, pdm->dmBitsPerPel, pdm->dmDisplayFrequency); /* Compare with the default entry */ if (!bModeMatch && pdm->dmBitsPerPel == pdmDefault->dmBitsPerPel && pdm->dmPelsWidth == pdmDefault->dmPelsWidth && pdm->dmPelsHeight == pdmDefault->dmPelsHeight) { pGraphicsDevice->iDefaultMode = i; pGraphicsDevice->iCurrentMode = i; TRACE("Found default entry: %lu '%ls'\n", i, pdm->dmDeviceName); if (pdm->dmDisplayFrequency == pdmDefault->dmDisplayFrequency) { /* Uh oh, even the display frequency matches. */ bModeMatch = TRUE; } } /* Initialize the entry */ pGraphicsDevice->pDevModeList[i].dwFlags = 0; pGraphicsDevice->pDevModeList[i].pdm = pdm; i++; } } /* Lock loader */ EngAcquireSemaphore(ghsemGraphicsDeviceList); /* Insert the device into the global list */ pGraphicsDevice->pNextGraphicsDevice = NULL; if (gpGraphicsDeviceLast) gpGraphicsDeviceLast->pNextGraphicsDevice = pGraphicsDevice; gpGraphicsDeviceLast = pGraphicsDevice; if (!gpGraphicsDeviceFirst) gpGraphicsDeviceFirst = pGraphicsDevice; /* Increment device number */ giDevNum++; /* Unlock loader */ EngReleaseSemaphore(ghsemGraphicsDeviceList); TRACE("Prepared %lu modes for %ls\n", cModes, pGraphicsDevice->pwszDescription); return pGraphicsDevice; }