[WIN32K:NTUSER]
[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 if (pustrDevice)
576 {
577 /* Initialize destination string */
578 RtlInitEmptyUnicodeString(&ustrDevice, awcDevice, sizeof(awcDevice));
579
580 _SEH2_TRY
581 {
582 /* Probe the UNICODE_STRING and the buffer */
583 ProbeForRead(pustrDevice, sizeof(UNICODE_STRING), 1);
584 ProbeForRead(pustrDevice->Buffer, pustrDevice->Length, 1);
585
586 /* Copy the string */
587 RtlCopyUnicodeString(&ustrDevice, pustrDevice);
588 }
589 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
590 {
591 _SEH2_YIELD(return _SEH2_GetExceptionCode());
592 }
593 _SEH2_END
594
595 pustrDevice = &ustrDevice;
596 }
597
598 /* Acquire global USER lock */
599 UserEnterShared();
600
601 if (iModeNum == ENUM_REGISTRY_SETTINGS)
602 {
603 /* Get the registry settings */
604 Status = UserEnumRegistryDisplaySettings(pustrDevice, &dmReg);
605 pdm = &dmReg;
606 pdm->dmSize = sizeof(DEVMODEW);
607 }
608 else if (iModeNum == ENUM_CURRENT_SETTINGS)
609 {
610 /* Get the current settings */
611 Status = UserEnumCurrentDisplaySettings(pustrDevice, &pdm);
612 }
613 else
614 {
615 /* Get specified settings */
616 Status = UserEnumDisplaySettings(pustrDevice, iModeNum, &pdm, dwFlags);
617 }
618
619 /* Release lock */
620 UserLeave();
621
622 /* Did we succeed? */
623 if (NT_SUCCESS(Status))
624 {
625 /* Copy some information back */
626 _SEH2_TRY
627 {
628 ProbeForRead(lpDevMode, sizeof(DEVMODEW), 1);
629 cbSize = lpDevMode->dmSize;
630 cbExtra = lpDevMode->dmDriverExtra;
631
632 ProbeForWrite(lpDevMode, cbSize + cbExtra, 1);
633 /* Output what we got */
634 RtlCopyMemory(lpDevMode, pdm, min(cbSize, pdm->dmSize));
635
636 /* Output private/extra driver data */
637 if (cbExtra > 0 && pdm->dmDriverExtra > 0)
638 {
639 RtlCopyMemory((PCHAR)lpDevMode + cbSize,
640 (PCHAR)pdm + pdm->dmSize,
641 min(cbExtra, pdm->dmDriverExtra));
642 }
643 }
644 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
645 {
646 Status = _SEH2_GetExceptionCode();
647 }
648 _SEH2_END;
649 }
650
651 return Status;
652 }
653
654 VOID
655 UserUpdateFullscreen(
656 DWORD flags)
657 {
658 if (flags & CDS_FULLSCREEN)
659 gpFullscreen = gptiCurrent->ppi;
660 else
661 gpFullscreen = NULL;
662 }
663
664 LONG
665 APIENTRY
666 UserChangeDisplaySettings(
667 PUNICODE_STRING pustrDevice,
668 LPDEVMODEW pdm,
669 DWORD flags,
670 LPVOID lParam)
671 {
672 DEVMODEW dm;
673 LONG lResult = DISP_CHANGE_SUCCESSFUL;
674 HKEY hkey;
675 NTSTATUS Status;
676 PPDEVOBJ ppdev;
677 //PDESKTOP pdesk;
678
679 /* If no DEVMODE is given, use registry settings */
680 if (!pdm)
681 {
682 /* Get the registry settings */
683 Status = UserEnumRegistryDisplaySettings(pustrDevice, &dm);
684 if (!NT_SUCCESS(Status))
685 {
686 ERR("Could not load registry settings\n");
687 return DISP_CHANGE_BADPARAM;
688 }
689 }
690 else if (pdm->dmSize < FIELD_OFFSET(DEVMODEW, dmFields))
691 return DISP_CHANGE_BADMODE; /* This is what winXP SP3 returns */
692 else
693 dm = *pdm;
694
695 /* Check params */
696 if ((dm.dmFields & (DM_PELSWIDTH | DM_PELSHEIGHT)) != (DM_PELSWIDTH | DM_PELSHEIGHT))
697 {
698 ERR("Devmode doesn't specify the resolution.\n");
699 return DISP_CHANGE_BADMODE;
700 }
701
702 /* Get the PDEV */
703 ppdev = EngpGetPDEV(pustrDevice);
704 if (!ppdev)
705 {
706 ERR("Failed to get PDEV\n");
707 return DISP_CHANGE_BADPARAM;
708 }
709
710 /* Fixup values */
711 if(dm.dmBitsPerPel == 0 || !(dm.dmFields & DM_BITSPERPEL))
712 {
713 dm.dmBitsPerPel = ppdev->pdmwDev->dmBitsPerPel;
714 dm.dmFields |= DM_BITSPERPEL;
715 }
716
717 if((dm.dmFields & DM_DISPLAYFREQUENCY) && (dm.dmDisplayFrequency == 0))
718 dm.dmDisplayFrequency = ppdev->pdmwDev->dmDisplayFrequency;
719
720 /* Look for the requested DEVMODE */
721 pdm = PDEVOBJ_pdmMatchDevMode(ppdev, &dm);
722 if (!pdm)
723 {
724 ERR("Could not find a matching DEVMODE\n");
725 lResult = DISP_CHANGE_BADMODE;
726 goto leave;
727 }
728 else if (flags & CDS_TEST)
729 {
730 /* It's possible, go ahead! */
731 lResult = DISP_CHANGE_SUCCESSFUL;
732 goto leave;
733 }
734
735 /* Shall we update the registry? */
736 if (flags & CDS_UPDATEREGISTRY)
737 {
738 /* Open the local or global settings key */
739 Status = UserOpenDisplaySettingsKey(&hkey, pustrDevice, flags & CDS_GLOBAL);
740 if (NT_SUCCESS(Status))
741 {
742 /* Store the settings */
743 RegWriteDisplaySettings(hkey, pdm);
744
745 /* Close the registry key */
746 ZwClose(hkey);
747 }
748 else
749 {
750 ERR("Could not open registry key\n");
751 lResult = DISP_CHANGE_NOTUPDATED;
752 }
753 }
754
755 /* Check if DEVMODE matches the current mode */
756 if (pdm == ppdev->pdmwDev && !(flags & CDS_RESET))
757 {
758 ERR("DEVMODE matches, nothing to do\n");
759 goto leave;
760 }
761
762 /* Shall we apply the settings? */
763 if (!(flags & CDS_NORESET))
764 {
765 ULONG ulResult;
766 PVOID pvOldCursor;
767
768 /* Remove mouse pointer */
769 pvOldCursor = UserSetCursor(NULL, TRUE);
770
771 /* Do the mode switch */
772 ulResult = PDEVOBJ_bSwitchMode(ppdev, pdm);
773
774 /* Restore mouse pointer, no hooks called */
775 pvOldCursor = UserSetCursor(pvOldCursor, TRUE);
776 ASSERT(pvOldCursor == NULL);
777
778 /* Check for failure */
779 if (!ulResult)
780 {
781 ERR("Failed to set mode\n");
782 lResult = (lResult == DISP_CHANGE_NOTUPDATED) ?
783 DISP_CHANGE_FAILED : DISP_CHANGE_RESTART;
784
785 goto leave;
786 }
787
788 UserUpdateFullscreen(flags);
789
790 /* Update the system metrics */
791 InitMetrics();
792
793 //IntvGetDeviceCaps(&PrimarySurface, &GdiHandleTable->DevCaps);
794
795 /* Set new size of the monitor */
796 UserUpdateMonitorSize((HDEV)ppdev);
797
798 /* Remove all cursor clipping */
799 UserClipCursor(NULL);
800
801 //pdesk = IntGetActiveDesktop();
802 //IntHideDesktop(pdesk);
803
804 /* Send WM_DISPLAYCHANGE to all toplevel windows */
805 co_IntSendMessageTimeout(HWND_BROADCAST,
806 WM_DISPLAYCHANGE,
807 (WPARAM)ppdev->gdiinfo.cBitsPixel,
808 (LPARAM)(ppdev->gdiinfo.ulHorzRes + (ppdev->gdiinfo.ulVertRes << 16)),
809 SMTO_NORMAL,
810 100,
811 &ulResult);
812
813 //co_IntShowDesktop(pdesk, ppdev->gdiinfo.ulHorzRes, ppdev->gdiinfo.ulVertRes);
814
815 UserRedrawDesktop();
816 }
817
818 leave:
819 /* Release the PDEV */
820 PDEVOBJ_vRelease(ppdev);
821
822 return lResult;
823 }
824
825 VOID
826 UserDisplayNotifyShutdown(
827 PPROCESSINFO ppiCurrent)
828 {
829 if (ppiCurrent == gpFullscreen)
830 {
831 UserChangeDisplaySettings(NULL, NULL, 0, NULL);
832 if (gpFullscreen)
833 ERR("Failed to restore display mode!\n");
834 }
835 }
836
837 LONG
838 APIENTRY
839 NtUserChangeDisplaySettings(
840 PUNICODE_STRING pustrDevice,
841 LPDEVMODEW lpDevMode,
842 DWORD dwflags,
843 LPVOID lParam)
844 {
845 WCHAR awcDevice[CCHDEVICENAME];
846 UNICODE_STRING ustrDevice;
847 DEVMODEW dmLocal;
848 LONG lRet;
849
850 /* Check arguments */
851 if ((dwflags != CDS_VIDEOPARAMETERS) && (lParam != NULL))
852 {
853 EngSetLastError(ERROR_INVALID_PARAMETER);
854 return DISP_CHANGE_BADPARAM;
855 }
856
857 /* Check flags */
858 if ((dwflags & (CDS_GLOBAL|CDS_NORESET)) && !(dwflags & CDS_UPDATEREGISTRY))
859 {
860 return DISP_CHANGE_BADFLAGS;
861 }
862
863 /* Copy the device name */
864 if (pustrDevice)
865 {
866 /* Initialize destination string */
867 RtlInitEmptyUnicodeString(&ustrDevice, awcDevice, sizeof(awcDevice));
868
869 _SEH2_TRY
870 {
871 /* Probe the UNICODE_STRING and the buffer */
872 ProbeForRead(pustrDevice, sizeof(UNICODE_STRING), 1);
873 ProbeForRead(pustrDevice->Buffer, pustrDevice->Length, 1);
874
875 /* Copy the string */
876 RtlCopyUnicodeString(&ustrDevice, pustrDevice);
877 }
878 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
879 {
880 /* Set and return error */
881 SetLastNtError(_SEH2_GetExceptionCode());
882 _SEH2_YIELD(return DISP_CHANGE_BADPARAM);
883 }
884 _SEH2_END
885
886 pustrDevice = &ustrDevice;
887 }
888
889 /* Copy devmode */
890 if (lpDevMode)
891 {
892 _SEH2_TRY
893 {
894 /* Probe the size field of the structure */
895 ProbeForRead(lpDevMode, sizeof(dmLocal.dmSize), 1);
896
897 /* Calculate usable size */
898 dmLocal.dmSize = min(sizeof(dmLocal), lpDevMode->dmSize);
899
900 /* Probe and copy the full DEVMODE */
901 ProbeForRead(lpDevMode, dmLocal.dmSize, 1);
902 RtlCopyMemory(&dmLocal, lpDevMode, dmLocal.dmSize);
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 /* Check for extra parameters */
913 if (dmLocal.dmDriverExtra > 0)
914 {
915 /* FIXME: TODO */
916 ERR("lpDevMode->dmDriverExtra is IGNORED!\n");
917 dmLocal.dmDriverExtra = 0;
918 }
919
920 /* Use the local structure */
921 lpDevMode = &dmLocal;
922 }
923
924 // FIXME: Copy videoparameters
925
926 /* Acquire global USER lock */
927 UserEnterExclusive();
928
929 /* Call internal function */
930 lRet = UserChangeDisplaySettings(pustrDevice, lpDevMode, dwflags, NULL);
931
932 /* Release lock */
933 UserLeave();
934
935 return lRet;
936 }
937