[WIN32K] Add some missing parameter checks. Fixes all but 4 tests in win32knt:NtUserE...
[reactos.git] / reactos / win32ss / user / ntuser / display.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Video initialization and display settings
5 * FILE: win32ss/user/ntuser/display.c
6 * PROGRAMER: Timo Kreuzer (timo.kreuzer@reactos.org)
7 */
8
9 #include <win32k.h>
10 DBG_DEFAULT_CHANNEL(UserDisplay);
11
12 BOOL gbBaseVideo = 0;
13 static PPROCESSINFO gpFullscreen = NULL;
14
15 static const PWCHAR KEY_VIDEO = L"\\Registry\\Machine\\HARDWARE\\DEVICEMAP\\VIDEO";
16
17 VOID
18 RegWriteDisplaySettings(HKEY hkey, PDEVMODEW pdm)
19 {
20 RegWriteDWORD(hkey, L"DefaultSettings.BitsPerPel", pdm->dmBitsPerPel);
21 RegWriteDWORD(hkey, L"DefaultSettings.XResolution", pdm->dmPelsWidth);
22 RegWriteDWORD(hkey, L"DefaultSettings.YResolution", pdm->dmPelsHeight);
23 RegWriteDWORD(hkey, L"DefaultSettings.Flags", pdm->dmDisplayFlags);
24 RegWriteDWORD(hkey, L"DefaultSettings.VRefresh", pdm->dmDisplayFrequency);
25 RegWriteDWORD(hkey, L"DefaultSettings.XPanning", pdm->dmPanningWidth);
26 RegWriteDWORD(hkey, L"DefaultSettings.YPanning", pdm->dmPanningHeight);
27 RegWriteDWORD(hkey, L"DefaultSettings.Orientation", pdm->dmDisplayOrientation);
28 RegWriteDWORD(hkey, L"DefaultSettings.FixedOutput", pdm->dmDisplayFixedOutput);
29 RegWriteDWORD(hkey, L"Attach.RelativeX", pdm->dmPosition.x);
30 RegWriteDWORD(hkey, L"Attach.RelativeY", pdm->dmPosition.y);
31 // RegWriteDWORD(hkey, L"Attach.ToDesktop, pdm->dmBitsPerPel", pdm->);
32 }
33
34 VOID
35 RegReadDisplaySettings(HKEY hkey, PDEVMODEW pdm)
36 {
37 DWORD dwValue;
38
39 /* Zero out the structure */
40 RtlZeroMemory(pdm, sizeof(DEVMODEW));
41
42 /* Helper macro */
43 #define READ(field, str, flag) \
44 if (RegReadDWORD(hkey, L##str, &dwValue)) \
45 { \
46 pdm->field = dwValue; \
47 pdm->dmFields |= flag; \
48 }
49
50 /* Read all present settings */
51 READ(dmBitsPerPel, "DefaultSettings.BitsPerPel", DM_BITSPERPEL);
52 READ(dmPelsWidth, "DefaultSettings.XResolution", DM_PELSWIDTH);
53 READ(dmPelsHeight, "DefaultSettings.YResolution", DM_PELSHEIGHT);
54 READ(dmDisplayFlags, "DefaultSettings.Flags", DM_DISPLAYFLAGS);
55 READ(dmDisplayFrequency, "DefaultSettings.VRefresh", DM_DISPLAYFREQUENCY);
56 READ(dmPanningWidth, "DefaultSettings.XPanning", DM_PANNINGWIDTH);
57 READ(dmPanningHeight, "DefaultSettings.YPanning", DM_PANNINGHEIGHT);
58 READ(dmDisplayOrientation, "DefaultSettings.Orientation", DM_DISPLAYORIENTATION);
59 READ(dmDisplayFixedOutput, "DefaultSettings.FixedOutput", DM_DISPLAYFIXEDOUTPUT);
60 READ(dmPosition.x, "Attach.RelativeX", DM_POSITION);
61 READ(dmPosition.y, "Attach.RelativeY", DM_POSITION);
62 }
63
64 PGRAPHICS_DEVICE
65 NTAPI
66 InitDisplayDriver(
67 IN PWSTR pwszDeviceName,
68 IN PWSTR pwszRegKey)
69 {
70 PGRAPHICS_DEVICE pGraphicsDevice;
71 UNICODE_STRING ustrDeviceName, ustrDisplayDrivers, ustrDescription;
72 NTSTATUS Status;
73 WCHAR awcBuffer[128];
74 ULONG cbSize;
75 HKEY hkey;
76 DEVMODEW dmDefault;
77 DWORD dwVga;
78
79 TRACE("InitDisplayDriver(%S, %S);\n",
80 pwszDeviceName, pwszRegKey);
81
82 /* Open the driver's registry key */
83 Status = RegOpenKey(pwszRegKey, &hkey);
84 if (!NT_SUCCESS(Status))
85 {
86 ERR("Failed to open registry key: %ls\n", pwszRegKey);
87 return NULL;
88 }
89
90 /* Query the diplay drivers */
91 cbSize = sizeof(awcBuffer) - 10;
92 Status = RegQueryValue(hkey,
93 L"InstalledDisplayDrivers",
94 REG_MULTI_SZ,
95 awcBuffer,
96 &cbSize);
97 if (!NT_SUCCESS(Status))
98 {
99 ERR("Didn't find 'InstalledDisplayDrivers', status = 0x%lx\n", Status);
100 ZwClose(hkey);
101 return NULL;
102 }
103
104 /* Initialize the UNICODE_STRING */
105 ustrDisplayDrivers.Buffer = awcBuffer;
106 ustrDisplayDrivers.MaximumLength = (USHORT)cbSize;
107 ustrDisplayDrivers.Length = (USHORT)cbSize;
108
109 /* Set Buffer for description and size of remaining buffer */
110 ustrDescription.Buffer = awcBuffer + (cbSize / sizeof(WCHAR));
111 cbSize = sizeof(awcBuffer) - cbSize;
112
113 /* Query the device string */
114 Status = RegQueryValue(hkey,
115 L"Device Description",
116 REG_SZ,
117 ustrDescription.Buffer,
118 &cbSize);
119 if (NT_SUCCESS(Status))
120 {
121 ustrDescription.MaximumLength = (USHORT)cbSize;
122 ustrDescription.Length = (USHORT)cbSize;
123 }
124 else
125 {
126 RtlInitUnicodeString(&ustrDescription, L"<unknown>");
127 }
128
129 /* Query the default settings */
130 RegReadDisplaySettings(hkey, &dmDefault);
131
132 /* Query if this is a VGA compatible driver */
133 cbSize = sizeof(DWORD);
134 Status = RegQueryValue(hkey, L"VgaCompatible", REG_DWORD, &dwVga, &cbSize);
135 if (!NT_SUCCESS(Status)) dwVga = 0;
136
137 /* Close the registry key */
138 ZwClose(hkey);
139
140 /* Register the device with GDI */
141 RtlInitUnicodeString(&ustrDeviceName, pwszDeviceName);
142 pGraphicsDevice = EngpRegisterGraphicsDevice(&ustrDeviceName,
143 &ustrDisplayDrivers,
144 &ustrDescription,
145 &dmDefault);
146 if (pGraphicsDevice && dwVga)
147 {
148 pGraphicsDevice->StateFlags |= DISPLAY_DEVICE_VGA_COMPATIBLE;
149 }
150
151 return pGraphicsDevice;
152 }
153
154 NTSTATUS
155 NTAPI
156 InitVideo(VOID)
157 {
158 ULONG iDevNum, iVGACompatible = -1, ulMaxObjectNumber = 0;
159 WCHAR awcDeviceName[20];
160 WCHAR awcBuffer[256];
161 NTSTATUS Status;
162 PGRAPHICS_DEVICE pGraphicsDevice;
163 ULONG cbValue;
164 HKEY hkey;
165
166 TRACE("----------------------------- InitVideo() -------------------------------\n");
167
168 /* Open the key for the boot command line */
169 Status = RegOpenKey(L"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\Control", &hkey);
170 if (NT_SUCCESS(Status))
171 {
172 cbValue = 256;
173 Status = RegQueryValue(hkey, L"SystemStartOptions", REG_SZ, awcBuffer, &cbValue);
174 if (NT_SUCCESS(Status))
175 {
176 /* Check if VGA mode is requested. */
177 if (wcsstr(awcBuffer, L"BASEVIDEO") != 0)
178 {
179 ERR("VGA mode requested.\n");
180 gbBaseVideo = TRUE;
181 }
182 }
183
184 ZwClose(hkey);
185 }
186
187 /* Open the key for the adapters */
188 Status = RegOpenKey(KEY_VIDEO, &hkey);
189 if (!NT_SUCCESS(Status))
190 {
191 ERR("Could not open HARDWARE\\DEVICEMAP\\VIDEO registry key:0x%lx\n", Status);
192 return Status;
193 }
194
195 /* Read the name of the VGA adapter */
196 cbValue = 20;
197 Status = RegQueryValue(hkey, L"VgaCompatible", REG_SZ, awcDeviceName, &cbValue);
198 if (NT_SUCCESS(Status))
199 {
200 iVGACompatible = _wtoi(&awcDeviceName[13]);
201 ERR("VGA adapter = %lu\n", iVGACompatible);
202 }
203
204 /* Get the maximum mumber of adapters */
205 if (!RegReadDWORD(hkey, L"MaxObjectNumber", &ulMaxObjectNumber))
206 {
207 ERR("Could not read MaxObjectNumber, defaulting to 0.\n");
208 }
209
210 TRACE("Found %lu devices\n", ulMaxObjectNumber + 1);
211
212 /* Loop through all adapters */
213 for (iDevNum = 0; iDevNum <= ulMaxObjectNumber; iDevNum++)
214 {
215 /* Create the adapter's key name */
216 swprintf(awcDeviceName, L"\\Device\\Video%lu", iDevNum);
217
218 /* Read the reg key name */
219 cbValue = sizeof(awcBuffer);
220 Status = RegQueryValue(hkey, awcDeviceName, REG_SZ, awcBuffer, &cbValue);
221 if (!NT_SUCCESS(Status))
222 {
223 ERR("failed to query the registry path:0x%lx\n", Status);
224 continue;
225 }
226
227 /* Initialize the driver for this device */
228 pGraphicsDevice = InitDisplayDriver(awcDeviceName, awcBuffer);
229 if (!pGraphicsDevice) continue;
230
231 /* Check if this is a VGA compatible adapter */
232 if (pGraphicsDevice->StateFlags & DISPLAY_DEVICE_VGA_COMPATIBLE)
233 {
234 /* Save this as the VGA adapter */
235 if (!gpVgaGraphicsDevice)
236 gpVgaGraphicsDevice = pGraphicsDevice;
237 TRACE("gpVgaGraphicsDevice = %p\n", gpVgaGraphicsDevice);
238 }
239 else
240 {
241 /* Set the first one as primary device */
242 if (!gpPrimaryGraphicsDevice)
243 gpPrimaryGraphicsDevice = pGraphicsDevice;
244 TRACE("gpPrimaryGraphicsDevice = %p\n", gpPrimaryGraphicsDevice);
245 }
246 }
247
248 /* Close the device map registry key */
249 ZwClose(hkey);
250
251 /* Was VGA mode requested? */
252 if (gbBaseVideo)
253 {
254 /* Check if we found a VGA compatible device */
255 if (gpVgaGraphicsDevice)
256 {
257 /* Set the VgaAdapter as primary */
258 gpPrimaryGraphicsDevice = gpVgaGraphicsDevice;
259 // FIXME: DEVMODE
260 }
261 else
262 {
263 ERR("Could not find VGA compatible driver. Trying normal.\n");
264 }
265 }
266
267 /* Check if we had any success */
268 if (!gpPrimaryGraphicsDevice)
269 {
270 /* Check if there is a VGA device we skipped */
271 if (gpVgaGraphicsDevice)
272 {
273 /* There is, use the VGA device */
274 gpPrimaryGraphicsDevice = gpVgaGraphicsDevice;
275 }
276 else
277 {
278 ERR("No usable display driver was found.\n");
279 return STATUS_UNSUCCESSFUL;
280 }
281 }
282
283 InitSysParams();
284
285 return 1;
286 }
287
288 NTSTATUS
289 NTAPI
290 UserEnumDisplayDevices(
291 PUNICODE_STRING pustrDevice,
292 DWORD iDevNum,
293 PDISPLAY_DEVICEW pdispdev,
294 DWORD dwFlags)
295 {
296 PGRAPHICS_DEVICE pGraphicsDevice;
297 ULONG cbSize;
298 HKEY hkey;
299 NTSTATUS Status;
300
301 /* Ask gdi for the GRAPHICS_DEVICE */
302 pGraphicsDevice = EngpFindGraphicsDevice(pustrDevice, iDevNum, 0);
303 if (!pGraphicsDevice)
304 {
305 /* No device found */
306 ERR("No GRAPHICS_DEVICE found\n");
307 return STATUS_UNSUCCESSFUL;
308 }
309
310 /* Open the device map registry key */
311 Status = RegOpenKey(KEY_VIDEO, &hkey);
312 if (!NT_SUCCESS(Status))
313 {
314 /* No device found */
315 ERR("Could not open reg key\n");
316 return STATUS_UNSUCCESSFUL;
317 }
318
319 /* Query the registry path */
320 cbSize = sizeof(pdispdev->DeviceKey);
321 RegQueryValue(hkey,
322 pGraphicsDevice->szNtDeviceName,
323 REG_SZ,
324 pdispdev->DeviceKey,
325 &cbSize);
326
327 /* Close registry key */
328 ZwClose(hkey);
329
330 /* Copy device name, device string and StateFlags */
331 RtlStringCbCopyW(pdispdev->DeviceName, sizeof(pdispdev->DeviceName), pGraphicsDevice->szWinDeviceName);
332 RtlStringCbCopyW(pdispdev->DeviceString, sizeof(pdispdev->DeviceString), pGraphicsDevice->pwszDescription);
333 pdispdev->StateFlags = pGraphicsDevice->StateFlags;
334 // FIXME: fill in DEVICE ID
335 pdispdev->DeviceID[0] = UNICODE_NULL;
336
337 return STATUS_SUCCESS;
338 }
339
340 //NTSTATUS
341 BOOL
342 NTAPI
343 NtUserEnumDisplayDevices(
344 PUNICODE_STRING pustrDevice,
345 DWORD iDevNum,
346 PDISPLAY_DEVICEW pDisplayDevice,
347 DWORD dwFlags)
348 {
349 UNICODE_STRING ustrDevice;
350 WCHAR awcDevice[CCHDEVICENAME];
351 DISPLAY_DEVICEW dispdev;
352 NTSTATUS Status;
353
354 TRACE("Enter NtUserEnumDisplayDevices(%wZ, %lu)\n",
355 pustrDevice, iDevNum);
356
357 dispdev.cb = sizeof(dispdev);
358
359 if (pustrDevice)
360 {
361 /* Initialize destination string */
362 RtlInitEmptyUnicodeString(&ustrDevice, awcDevice, sizeof(awcDevice));
363
364 _SEH2_TRY
365 {
366 /* Probe the UNICODE_STRING and the buffer */
367 ProbeForRead(pustrDevice, sizeof(UNICODE_STRING), 1);
368 ProbeForRead(pustrDevice->Buffer, pustrDevice->Length, 1);
369
370 /* Copy the string */
371 RtlCopyUnicodeString(&ustrDevice, pustrDevice);
372 }
373 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
374 {
375 // _SEH2_YIELD(return _SEH2_GetExceptionCode());
376 _SEH2_YIELD(return NT_SUCCESS(_SEH2_GetExceptionCode()));
377 }
378 _SEH2_END
379
380 if (ustrDevice.Length > 0)
381 pustrDevice = &ustrDevice;
382 else
383 pustrDevice = NULL;
384 }
385
386 /* If name is given only iDevNum==0 gives results */
387 if (pustrDevice && iDevNum != 0)
388 return FALSE;
389
390 /* Acquire global USER lock */
391 UserEnterShared();
392
393 /* Call the internal function */
394 Status = UserEnumDisplayDevices(pustrDevice, iDevNum, &dispdev, dwFlags);
395
396 /* Release lock */
397 UserLeave();
398
399 /* On success copy data to caller */
400 if (NT_SUCCESS(Status))
401 {
402 /* Enter SEH */
403 _SEH2_TRY
404 {
405 /* First probe the cb field */
406 ProbeForWrite(&pDisplayDevice->cb, sizeof(DWORD), 1);
407
408 /* Check the buffer size */
409 if (pDisplayDevice->cb)
410 {
411 /* Probe the output buffer */
412 pDisplayDevice->cb = min(pDisplayDevice->cb, sizeof(dispdev));
413 ProbeForWrite(pDisplayDevice, pDisplayDevice->cb, 1);
414
415 /* Copy as much as the given buffer allows */
416 RtlCopyMemory(pDisplayDevice, &dispdev, pDisplayDevice->cb);
417 }
418 }
419 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
420 {
421 Status = _SEH2_GetExceptionCode();
422 }
423 _SEH2_END
424 }
425
426 TRACE("Leave NtUserEnumDisplayDevices, Status = 0x%lx\n", Status);
427 /* Return the result */
428 // return Status;
429 return NT_SUCCESS(Status); // FIXME
430 }
431
432 NTSTATUS
433 NTAPI
434 UserEnumCurrentDisplaySettings(
435 PUNICODE_STRING pustrDevice,
436 PDEVMODEW *ppdm)
437 {
438 PPDEVOBJ ppdev;
439
440 /* Get the PDEV for the device */
441 ppdev = EngpGetPDEV(pustrDevice);
442 if (!ppdev)
443 {
444 /* No device found */
445 ERR("No PDEV found!\n");
446 return STATUS_UNSUCCESSFUL;
447 }
448
449 *ppdm = ppdev->pdmwDev;
450 PDEVOBJ_vRelease(ppdev);
451
452 return STATUS_SUCCESS;
453 }
454
455 NTSTATUS
456 NTAPI
457 UserEnumDisplaySettings(
458 PUNICODE_STRING pustrDevice,
459 DWORD iModeNum,
460 LPDEVMODEW *ppdm,
461 DWORD dwFlags)
462 {
463 PGRAPHICS_DEVICE pGraphicsDevice;
464 PDEVMODEENTRY pdmentry;
465 ULONG i, iFoundMode;
466
467 TRACE("Enter UserEnumDisplaySettings('%wZ', %lu)\n",
468 pustrDevice, iModeNum);
469
470 /* Ask GDI for the GRAPHICS_DEVICE */
471 pGraphicsDevice = EngpFindGraphicsDevice(pustrDevice, 0, 0);
472
473 if (!pGraphicsDevice)
474 {
475 /* No device found */
476 ERR("No device found!\n");
477 return STATUS_UNSUCCESSFUL;
478 }
479
480 if (iModeNum >= pGraphicsDevice->cDevModes)
481 return STATUS_NO_MORE_ENTRIES;
482
483 iFoundMode = 0;
484 for (i = 0; i < pGraphicsDevice->cDevModes; i++)
485 {
486 pdmentry = &pGraphicsDevice->pDevModeList[i];
487
488 /* FIXME: Consider EDS_RAWMODE */
489 #if 0
490 if ((!(dwFlags & EDS_RAWMODE) && (pdmentry->dwFlags & 1)) ||!
491 (dwFlags & EDS_RAWMODE))
492 #endif
493 {
494 /* Is this the one we want? */
495 if (iFoundMode == iModeNum)
496 {
497 *ppdm = pdmentry->pdm;
498 return STATUS_SUCCESS;
499 }
500
501 /* Increment number of found modes */
502 iFoundMode++;
503 }
504 }
505
506 /* Nothing was found */
507 return STATUS_INVALID_PARAMETER;
508 }
509
510 NTSTATUS
511 NTAPI
512 UserOpenDisplaySettingsKey(
513 OUT PHKEY phkey,
514 IN PUNICODE_STRING pustrDevice,
515 IN BOOL bGlobal)
516 {
517 HKEY hkey;
518 DISPLAY_DEVICEW dispdev;
519 NTSTATUS Status;
520
521 /* Get device info */
522 Status = UserEnumDisplayDevices(pustrDevice, 0, &dispdev, 0);
523 if (!NT_SUCCESS(Status))
524 return Status;
525
526 if (bGlobal)
527 {
528 // FIXME: Need to fix the registry key somehow
529 }
530
531 /* Open the registry key */
532 Status = RegOpenKey(dispdev.DeviceKey, &hkey);
533 if (!NT_SUCCESS(Status))
534 return Status;
535
536 *phkey = hkey;
537
538 return Status;
539 }
540
541 NTSTATUS
542 NTAPI
543 UserEnumRegistryDisplaySettings(
544 IN PUNICODE_STRING pustrDevice,
545 OUT LPDEVMODEW pdm)
546 {
547 HKEY hkey;
548 NTSTATUS Status = UserOpenDisplaySettingsKey(&hkey, pustrDevice, 0);
549 if(NT_SUCCESS(Status))
550 {
551 RegReadDisplaySettings(hkey, pdm);
552 ZwClose(hkey);
553 return STATUS_SUCCESS;
554 }
555 return Status ;
556 }
557
558 NTSTATUS
559 APIENTRY
560 NtUserEnumDisplaySettings(
561 IN PUNICODE_STRING pustrDevice,
562 IN DWORD iModeNum,
563 OUT LPDEVMODEW lpDevMode,
564 IN DWORD dwFlags)
565 {
566 UNICODE_STRING ustrDevice;
567 WCHAR awcDevice[CCHDEVICENAME];
568 NTSTATUS Status;
569 ULONG cbSize, cbExtra;
570 DEVMODEW dmReg, *pdm;
571
572 TRACE("Enter NtUserEnumDisplaySettings(%wZ, %lu, %p, 0x%lx)\n",
573 pustrDevice, iModeNum, lpDevMode, dwFlags);
574
575 _SEH2_TRY
576 {
577 ProbeForWrite(lpDevMode, sizeof(DEVMODEW), 1);
578 }
579 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
580 {
581 _SEH2_YIELD(return _SEH2_GetExceptionCode());
582 }
583 _SEH2_END
584
585 if (lpDevMode->dmSize != sizeof(DEVMODEW))
586 {
587 return STATUS_BUFFER_TOO_SMALL;
588 }
589
590 if (pustrDevice)
591 {
592 if (pustrDevice->Buffer == NULL || pustrDevice->Length == 0)
593 {
594 Status = STATUS_INVALID_PARAMETER_1;
595 }
596
597 /* Initialize destination string */
598 RtlInitEmptyUnicodeString(&ustrDevice, awcDevice, sizeof(awcDevice));
599
600 _SEH2_TRY
601 {
602 /* Probe the UNICODE_STRING and the buffer */
603 ProbeForRead(pustrDevice, sizeof(UNICODE_STRING), 1);
604 ProbeForRead(pustrDevice->Buffer, pustrDevice->Length, 1);
605
606 /* Copy the string */
607 RtlCopyUnicodeString(&ustrDevice, pustrDevice);
608 }
609 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
610 {
611 _SEH2_YIELD(return _SEH2_GetExceptionCode());
612 }
613 _SEH2_END
614
615 pustrDevice = &ustrDevice;
616 }
617
618 /* Acquire global USER lock */
619 UserEnterShared();
620
621 if (iModeNum == ENUM_REGISTRY_SETTINGS)
622 {
623 /* Get the registry settings */
624 Status = UserEnumRegistryDisplaySettings(pustrDevice, &dmReg);
625 pdm = &dmReg;
626 pdm->dmSize = sizeof(DEVMODEW);
627 }
628 else if (iModeNum == ENUM_CURRENT_SETTINGS)
629 {
630 /* Get the current settings */
631 Status = UserEnumCurrentDisplaySettings(pustrDevice, &pdm);
632 }
633 else
634 {
635 /* Get specified settings */
636 Status = UserEnumDisplaySettings(pustrDevice, iModeNum, &pdm, dwFlags);
637 }
638
639 /* Release lock */
640 UserLeave();
641
642 /* Did we succeed? */
643 if (NT_SUCCESS(Status))
644 {
645 /* Copy some information back */
646 _SEH2_TRY
647 {
648 ProbeForRead(lpDevMode, sizeof(DEVMODEW), 1);
649 cbSize = lpDevMode->dmSize;
650 cbExtra = lpDevMode->dmDriverExtra;
651
652 ProbeForWrite(lpDevMode, cbSize + cbExtra, 1);
653 /* Output what we got */
654 RtlCopyMemory(lpDevMode, pdm, min(cbSize, pdm->dmSize));
655
656 /* Output private/extra driver data */
657 if (cbExtra > 0 && pdm->dmDriverExtra > 0)
658 {
659 RtlCopyMemory((PCHAR)lpDevMode + cbSize,
660 (PCHAR)pdm + pdm->dmSize,
661 min(cbExtra, pdm->dmDriverExtra));
662 }
663 }
664 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
665 {
666 Status = _SEH2_GetExceptionCode();
667 }
668 _SEH2_END;
669 }
670 else
671 {
672 if (Status == STATUS_UNSUCCESSFUL)
673 {
674 Status = STATUS_INVALID_PARAMETER_1;
675 }
676 }
677
678 return Status;
679 }
680 VOID
681 UserUpdateFullscreen(
682 DWORD flags)
683 {
684 if (flags & CDS_FULLSCREEN)
685 gpFullscreen = gptiCurrent->ppi;
686 else
687 gpFullscreen = NULL;
688 }
689
690 LONG
691 APIENTRY
692 UserChangeDisplaySettings(
693 PUNICODE_STRING pustrDevice,
694 LPDEVMODEW pdm,
695 DWORD flags,
696 LPVOID lParam)
697 {
698 DEVMODEW dm;
699 LONG lResult = DISP_CHANGE_SUCCESSFUL;
700 HKEY hkey;
701 NTSTATUS Status;
702 PPDEVOBJ ppdev;
703 //PDESKTOP pdesk;
704
705 /* If no DEVMODE is given, use registry settings */
706 if (!pdm)
707 {
708 /* Get the registry settings */
709 Status = UserEnumRegistryDisplaySettings(pustrDevice, &dm);
710 if (!NT_SUCCESS(Status))
711 {
712 ERR("Could not load registry settings\n");
713 return DISP_CHANGE_BADPARAM;
714 }
715 }
716 else if (pdm->dmSize < FIELD_OFFSET(DEVMODEW, dmFields))
717 return DISP_CHANGE_BADMODE; /* This is what winXP SP3 returns */
718 else
719 dm = *pdm;
720
721 /* Check params */
722 if ((dm.dmFields & (DM_PELSWIDTH | DM_PELSHEIGHT)) != (DM_PELSWIDTH | DM_PELSHEIGHT))
723 {
724 ERR("Devmode doesn't specify the resolution.\n");
725 return DISP_CHANGE_BADMODE;
726 }
727
728 /* Get the PDEV */
729 ppdev = EngpGetPDEV(pustrDevice);
730 if (!ppdev)
731 {
732 ERR("Failed to get PDEV\n");
733 return DISP_CHANGE_BADPARAM;
734 }
735
736 /* Fixup values */
737 if(dm.dmBitsPerPel == 0 || !(dm.dmFields & DM_BITSPERPEL))
738 {
739 dm.dmBitsPerPel = ppdev->pdmwDev->dmBitsPerPel;
740 dm.dmFields |= DM_BITSPERPEL;
741 }
742
743 if((dm.dmFields & DM_DISPLAYFREQUENCY) && (dm.dmDisplayFrequency == 0))
744 dm.dmDisplayFrequency = ppdev->pdmwDev->dmDisplayFrequency;
745
746 /* Look for the requested DEVMODE */
747 pdm = PDEVOBJ_pdmMatchDevMode(ppdev, &dm);
748 if (!pdm)
749 {
750 ERR("Could not find a matching DEVMODE\n");
751 lResult = DISP_CHANGE_BADMODE;
752 goto leave;
753 }
754 else if (flags & CDS_TEST)
755 {
756 /* It's possible, go ahead! */
757 lResult = DISP_CHANGE_SUCCESSFUL;
758 goto leave;
759 }
760
761 /* Shall we update the registry? */
762 if (flags & CDS_UPDATEREGISTRY)
763 {
764 /* Open the local or global settings key */
765 Status = UserOpenDisplaySettingsKey(&hkey, pustrDevice, flags & CDS_GLOBAL);
766 if (NT_SUCCESS(Status))
767 {
768 /* Store the settings */
769 RegWriteDisplaySettings(hkey, pdm);
770
771 /* Close the registry key */
772 ZwClose(hkey);
773 }
774 else
775 {
776 ERR("Could not open registry key\n");
777 lResult = DISP_CHANGE_NOTUPDATED;
778 }
779 }
780
781 /* Check if DEVMODE matches the current mode */
782 if (pdm == ppdev->pdmwDev && !(flags & CDS_RESET))
783 {
784 ERR("DEVMODE matches, nothing to do\n");
785 goto leave;
786 }
787
788 /* Shall we apply the settings? */
789 if (!(flags & CDS_NORESET))
790 {
791 ULONG ulResult;
792 PVOID pvOldCursor;
793
794 /* Remove mouse pointer */
795 pvOldCursor = UserSetCursor(NULL, TRUE);
796
797 /* Do the mode switch */
798 ulResult = PDEVOBJ_bSwitchMode(ppdev, pdm);
799
800 /* Restore mouse pointer, no hooks called */
801 pvOldCursor = UserSetCursor(pvOldCursor, TRUE);
802 ASSERT(pvOldCursor == NULL);
803
804 /* Check for failure */
805 if (!ulResult)
806 {
807 ERR("Failed to set mode\n");
808 lResult = (lResult == DISP_CHANGE_NOTUPDATED) ?
809 DISP_CHANGE_FAILED : DISP_CHANGE_RESTART;
810
811 goto leave;
812 }
813
814 UserUpdateFullscreen(flags);
815
816 /* Update the system metrics */
817 InitMetrics();
818
819 //IntvGetDeviceCaps(&PrimarySurface, &GdiHandleTable->DevCaps);
820
821 /* Set new size of the monitor */
822 UserUpdateMonitorSize((HDEV)ppdev);
823
824 /* Remove all cursor clipping */
825 UserClipCursor(NULL);
826
827 //pdesk = IntGetActiveDesktop();
828 //IntHideDesktop(pdesk);
829
830 /* Send WM_DISPLAYCHANGE to all toplevel windows */
831 co_IntSendMessageTimeout(HWND_BROADCAST,
832 WM_DISPLAYCHANGE,
833 (WPARAM)ppdev->gdiinfo.cBitsPixel,
834 (LPARAM)(ppdev->gdiinfo.ulHorzRes + (ppdev->gdiinfo.ulVertRes << 16)),
835 SMTO_NORMAL,
836 100,
837 &ulResult);
838
839 //co_IntShowDesktop(pdesk, ppdev->gdiinfo.ulHorzRes, ppdev->gdiinfo.ulVertRes);
840
841 UserRedrawDesktop();
842 }
843
844 leave:
845 /* Release the PDEV */
846 PDEVOBJ_vRelease(ppdev);
847
848 return lResult;
849 }
850
851 VOID
852 UserDisplayNotifyShutdown(
853 PPROCESSINFO ppiCurrent)
854 {
855 if (ppiCurrent == gpFullscreen)
856 {
857 UserChangeDisplaySettings(NULL, NULL, 0, NULL);
858 if (gpFullscreen)
859 ERR("Failed to restore display mode!\n");
860 }
861 }
862
863 LONG
864 APIENTRY
865 NtUserChangeDisplaySettings(
866 PUNICODE_STRING pustrDevice,
867 LPDEVMODEW lpDevMode,
868 DWORD dwflags,
869 LPVOID lParam)
870 {
871 WCHAR awcDevice[CCHDEVICENAME];
872 UNICODE_STRING ustrDevice;
873 DEVMODEW dmLocal;
874 LONG lRet;
875
876 /* Check arguments */
877 if ((dwflags != CDS_VIDEOPARAMETERS) && (lParam != NULL))
878 {
879 EngSetLastError(ERROR_INVALID_PARAMETER);
880 return DISP_CHANGE_BADPARAM;
881 }
882
883 /* Check flags */
884 if ((dwflags & (CDS_GLOBAL|CDS_NORESET)) && !(dwflags & CDS_UPDATEREGISTRY))
885 {
886 return DISP_CHANGE_BADFLAGS;
887 }
888
889 /* Copy the device name */
890 if (pustrDevice)
891 {
892 /* Initialize destination string */
893 RtlInitEmptyUnicodeString(&ustrDevice, awcDevice, sizeof(awcDevice));
894
895 _SEH2_TRY
896 {
897 /* Probe the UNICODE_STRING and the buffer */
898 ProbeForRead(pustrDevice, sizeof(UNICODE_STRING), 1);
899 ProbeForRead(pustrDevice->Buffer, pustrDevice->Length, 1);
900
901 /* Copy the string */
902 RtlCopyUnicodeString(&ustrDevice, pustrDevice);
903 }
904 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
905 {
906 /* Set and return error */
907 SetLastNtError(_SEH2_GetExceptionCode());
908 _SEH2_YIELD(return DISP_CHANGE_BADPARAM);
909 }
910 _SEH2_END
911
912 pustrDevice = &ustrDevice;
913 }
914
915 /* Copy devmode */
916 if (lpDevMode)
917 {
918 _SEH2_TRY
919 {
920 /* Probe the size field of the structure */
921 ProbeForRead(lpDevMode, sizeof(dmLocal.dmSize), 1);
922
923 /* Calculate usable size */
924 dmLocal.dmSize = min(sizeof(dmLocal), lpDevMode->dmSize);
925
926 /* Probe and copy the full DEVMODE */
927 ProbeForRead(lpDevMode, dmLocal.dmSize, 1);
928 RtlCopyMemory(&dmLocal, lpDevMode, dmLocal.dmSize);
929 }
930 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
931 {
932 /* Set and return error */
933 SetLastNtError(_SEH2_GetExceptionCode());
934 _SEH2_YIELD(return DISP_CHANGE_BADPARAM);
935 }
936 _SEH2_END
937
938 /* Check for extra parameters */
939 if (dmLocal.dmDriverExtra > 0)
940 {
941 /* FIXME: TODO */
942 ERR("lpDevMode->dmDriverExtra is IGNORED!\n");
943 dmLocal.dmDriverExtra = 0;
944 }
945
946 /* Use the local structure */
947 lpDevMode = &dmLocal;
948 }
949
950 // FIXME: Copy videoparameters
951
952 /* Acquire global USER lock */
953 UserEnterExclusive();
954
955 /* Call internal function */
956 lRet = UserChangeDisplaySettings(pustrDevice, lpDevMode, dwflags, NULL);
957
958 /* Release lock */
959 UserLeave();
960
961 return lRet;
962 }
963