[USER32] We have to use the copy of DEVMODEW structure (having size expanded on dmDri...
[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_INVALID_PARAMETER_1;
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_INVALID_PARAMETER_1;
478 }
479
480 iFoundMode = 0;
481 for (i = 0; i < pGraphicsDevice->cDevModes; i++)
482 {
483 pdmentry = &pGraphicsDevice->pDevModeList[i];
484
485 /* FIXME: Consider EDS_RAWMODE */
486 #if 0
487 if ((!(dwFlags & EDS_RAWMODE) && (pdmentry->dwFlags & 1)) ||!
488 (dwFlags & EDS_RAWMODE))
489 #endif
490 {
491 /* Is this the one we want? */
492 if (iFoundMode == iModeNum)
493 {
494 *ppdm = pdmentry->pdm;
495 return STATUS_SUCCESS;
496 }
497
498 /* Increment number of found modes */
499 iFoundMode++;
500 }
501 }
502
503 /* Nothing was found */
504 return STATUS_INVALID_PARAMETER_2;
505 }
506
507 NTSTATUS
508 NTAPI
509 UserOpenDisplaySettingsKey(
510 OUT PHKEY phkey,
511 IN PUNICODE_STRING pustrDevice,
512 IN BOOL bGlobal)
513 {
514 HKEY hkey;
515 DISPLAY_DEVICEW dispdev;
516 NTSTATUS Status;
517
518 /* Get device info */
519 Status = UserEnumDisplayDevices(pustrDevice, 0, &dispdev, 0);
520 if (!NT_SUCCESS(Status))
521 return Status;
522
523 if (bGlobal)
524 {
525 // FIXME: Need to fix the registry key somehow
526 }
527
528 /* Open the registry key */
529 Status = RegOpenKey(dispdev.DeviceKey, &hkey);
530 if (!NT_SUCCESS(Status))
531 return Status;
532
533 *phkey = hkey;
534
535 return Status;
536 }
537
538 NTSTATUS
539 NTAPI
540 UserEnumRegistryDisplaySettings(
541 IN PUNICODE_STRING pustrDevice,
542 OUT LPDEVMODEW pdm)
543 {
544 HKEY hkey;
545 NTSTATUS Status = UserOpenDisplaySettingsKey(&hkey, pustrDevice, 0);
546 if(NT_SUCCESS(Status))
547 {
548 RegReadDisplaySettings(hkey, pdm);
549 ZwClose(hkey);
550 return STATUS_SUCCESS;
551 }
552 return Status ;
553 }
554
555 NTSTATUS
556 APIENTRY
557 NtUserEnumDisplaySettings(
558 IN PUNICODE_STRING pustrDevice,
559 IN DWORD iModeNum,
560 OUT LPDEVMODEW lpDevMode,
561 IN DWORD dwFlags)
562 {
563 UNICODE_STRING ustrDevice;
564 WCHAR awcDevice[CCHDEVICENAME];
565 NTSTATUS Status;
566 ULONG cbSize, cbExtra;
567 DEVMODEW dmReg, *pdm;
568
569 TRACE("Enter NtUserEnumDisplaySettings(%wZ, %lu, %p, 0x%lx)\n",
570 pustrDevice, iModeNum, lpDevMode, dwFlags);
571
572 _SEH2_TRY
573 {
574 ProbeForRead(lpDevMode, sizeof(DEVMODEW), sizeof(UCHAR));
575
576 cbSize = lpDevMode->dmSize;
577 cbExtra = lpDevMode->dmDriverExtra;
578
579 ProbeForWrite(lpDevMode, cbSize + cbExtra, sizeof(UCHAR));
580 }
581 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
582 {
583 _SEH2_YIELD(return _SEH2_GetExceptionCode());
584 }
585 _SEH2_END;
586
587 if (lpDevMode->dmSize != sizeof(DEVMODEW))
588 {
589 return STATUS_BUFFER_TOO_SMALL;
590 }
591
592 if (pustrDevice)
593 {
594 /* Initialize destination string */
595 RtlInitEmptyUnicodeString(&ustrDevice, awcDevice, sizeof(awcDevice));
596
597 _SEH2_TRY
598 {
599 /* Probe the UNICODE_STRING and the buffer */
600 ProbeForReadUnicodeString(pustrDevice);
601
602 if (!pustrDevice->Length || !pustrDevice->Buffer)
603 ExRaiseStatus(STATUS_NO_MEMORY);
604
605 ProbeForRead(pustrDevice->Buffer, pustrDevice->Length, sizeof(UCHAR));
606
607 /* Copy the string */
608 RtlCopyUnicodeString(&ustrDevice, pustrDevice);
609 }
610 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
611 {
612 _SEH2_YIELD(return STATUS_INVALID_PARAMETER_1);
613 }
614 _SEH2_END;
615
616 pustrDevice = &ustrDevice;
617 }
618
619 /* Acquire global USER lock */
620 UserEnterShared();
621
622 if (iModeNum == ENUM_REGISTRY_SETTINGS)
623 {
624 /* Get the registry settings */
625 Status = UserEnumRegistryDisplaySettings(pustrDevice, &dmReg);
626 pdm = &dmReg;
627 pdm->dmSize = sizeof(DEVMODEW);
628 }
629 else if (iModeNum == ENUM_CURRENT_SETTINGS)
630 {
631 /* Get the current settings */
632 Status = UserEnumCurrentDisplaySettings(pustrDevice, &pdm);
633 }
634 else
635 {
636 /* Get specified settings */
637 Status = UserEnumDisplaySettings(pustrDevice, iModeNum, &pdm, dwFlags);
638 }
639
640 /* Release lock */
641 UserLeave();
642
643 /* Did we succeed? */
644 if (NT_SUCCESS(Status))
645 {
646 /* Copy some information back */
647 _SEH2_TRY
648 {
649 /* Output what we got */
650 RtlCopyMemory(lpDevMode, pdm, min(cbSize, pdm->dmSize));
651
652 /* Output private/extra driver data */
653 if (cbExtra > 0 && pdm->dmDriverExtra > 0)
654 {
655 RtlCopyMemory((PCHAR)lpDevMode + cbSize,
656 (PCHAR)pdm + pdm->dmSize,
657 min(cbExtra, pdm->dmDriverExtra));
658 }
659 }
660 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
661 {
662 Status = _SEH2_GetExceptionCode();
663 }
664 _SEH2_END;
665 }
666
667 return Status;
668 }
669 VOID
670 UserUpdateFullscreen(
671 DWORD flags)
672 {
673 if (flags & CDS_FULLSCREEN)
674 gpFullscreen = gptiCurrent->ppi;
675 else
676 gpFullscreen = NULL;
677 }
678
679 LONG
680 APIENTRY
681 UserChangeDisplaySettings(
682 PUNICODE_STRING pustrDevice,
683 LPDEVMODEW pdm,
684 DWORD flags,
685 LPVOID lParam)
686 {
687 DEVMODEW dm;
688 LONG lResult = DISP_CHANGE_SUCCESSFUL;
689 HKEY hkey;
690 NTSTATUS Status;
691 PPDEVOBJ ppdev;
692 //PDESKTOP pdesk;
693
694 /* If no DEVMODE is given, use registry settings */
695 if (!pdm)
696 {
697 /* Get the registry settings */
698 Status = UserEnumRegistryDisplaySettings(pustrDevice, &dm);
699 if (!NT_SUCCESS(Status))
700 {
701 ERR("Could not load registry settings\n");
702 return DISP_CHANGE_BADPARAM;
703 }
704 }
705 else if (pdm->dmSize < FIELD_OFFSET(DEVMODEW, dmFields))
706 return DISP_CHANGE_BADMODE; /* This is what winXP SP3 returns */
707 else
708 dm = *pdm;
709
710 /* Check params */
711 if ((dm.dmFields & (DM_PELSWIDTH | DM_PELSHEIGHT)) != (DM_PELSWIDTH | DM_PELSHEIGHT))
712 {
713 ERR("Devmode doesn't specify the resolution.\n");
714 return DISP_CHANGE_BADMODE;
715 }
716
717 /* Get the PDEV */
718 ppdev = EngpGetPDEV(pustrDevice);
719 if (!ppdev)
720 {
721 ERR("Failed to get PDEV\n");
722 return DISP_CHANGE_BADPARAM;
723 }
724
725 /* Fixup values */
726 if(dm.dmBitsPerPel == 0 || !(dm.dmFields & DM_BITSPERPEL))
727 {
728 dm.dmBitsPerPel = ppdev->pdmwDev->dmBitsPerPel;
729 dm.dmFields |= DM_BITSPERPEL;
730 }
731
732 if((dm.dmFields & DM_DISPLAYFREQUENCY) && (dm.dmDisplayFrequency == 0))
733 dm.dmDisplayFrequency = ppdev->pdmwDev->dmDisplayFrequency;
734
735 /* Look for the requested DEVMODE */
736 pdm = PDEVOBJ_pdmMatchDevMode(ppdev, &dm);
737 if (!pdm)
738 {
739 ERR("Could not find a matching DEVMODE\n");
740 lResult = DISP_CHANGE_BADMODE;
741 goto leave;
742 }
743 else if (flags & CDS_TEST)
744 {
745 /* It's possible, go ahead! */
746 lResult = DISP_CHANGE_SUCCESSFUL;
747 goto leave;
748 }
749
750 /* Shall we update the registry? */
751 if (flags & CDS_UPDATEREGISTRY)
752 {
753 /* Open the local or global settings key */
754 Status = UserOpenDisplaySettingsKey(&hkey, pustrDevice, flags & CDS_GLOBAL);
755 if (NT_SUCCESS(Status))
756 {
757 /* Store the settings */
758 RegWriteDisplaySettings(hkey, pdm);
759
760 /* Close the registry key */
761 ZwClose(hkey);
762 }
763 else
764 {
765 ERR("Could not open registry key\n");
766 lResult = DISP_CHANGE_NOTUPDATED;
767 }
768 }
769
770 /* Check if DEVMODE matches the current mode */
771 if (pdm == ppdev->pdmwDev && !(flags & CDS_RESET))
772 {
773 ERR("DEVMODE matches, nothing to do\n");
774 goto leave;
775 }
776
777 /* Shall we apply the settings? */
778 if (!(flags & CDS_NORESET))
779 {
780 ULONG ulResult;
781 PVOID pvOldCursor;
782
783 /* Remove mouse pointer */
784 pvOldCursor = UserSetCursor(NULL, TRUE);
785
786 /* Do the mode switch */
787 ulResult = PDEVOBJ_bSwitchMode(ppdev, pdm);
788
789 /* Restore mouse pointer, no hooks called */
790 pvOldCursor = UserSetCursor(pvOldCursor, TRUE);
791 ASSERT(pvOldCursor == NULL);
792
793 /* Check for failure */
794 if (!ulResult)
795 {
796 ERR("Failed to set mode\n");
797 lResult = (lResult == DISP_CHANGE_NOTUPDATED) ?
798 DISP_CHANGE_FAILED : DISP_CHANGE_RESTART;
799
800 goto leave;
801 }
802
803 UserUpdateFullscreen(flags);
804
805 /* Update the system metrics */
806 InitMetrics();
807
808 //IntvGetDeviceCaps(&PrimarySurface, &GdiHandleTable->DevCaps);
809
810 /* Set new size of the monitor */
811 UserUpdateMonitorSize((HDEV)ppdev);
812
813 /* Remove all cursor clipping */
814 UserClipCursor(NULL);
815
816 //pdesk = IntGetActiveDesktop();
817 //IntHideDesktop(pdesk);
818
819 /* Send WM_DISPLAYCHANGE to all toplevel windows */
820 co_IntSendMessageTimeout(HWND_BROADCAST,
821 WM_DISPLAYCHANGE,
822 (WPARAM)ppdev->gdiinfo.cBitsPixel,
823 (LPARAM)(ppdev->gdiinfo.ulHorzRes + (ppdev->gdiinfo.ulVertRes << 16)),
824 SMTO_NORMAL,
825 100,
826 &ulResult);
827
828 //co_IntShowDesktop(pdesk, ppdev->gdiinfo.ulHorzRes, ppdev->gdiinfo.ulVertRes);
829
830 UserRedrawDesktop();
831 }
832
833 leave:
834 /* Release the PDEV */
835 PDEVOBJ_vRelease(ppdev);
836
837 return lResult;
838 }
839
840 VOID
841 UserDisplayNotifyShutdown(
842 PPROCESSINFO ppiCurrent)
843 {
844 if (ppiCurrent == gpFullscreen)
845 {
846 UserChangeDisplaySettings(NULL, NULL, 0, NULL);
847 if (gpFullscreen)
848 ERR("Failed to restore display mode!\n");
849 }
850 }
851
852 LONG
853 APIENTRY
854 NtUserChangeDisplaySettings(
855 PUNICODE_STRING pustrDevice,
856 LPDEVMODEW lpDevMode,
857 DWORD dwflags,
858 LPVOID lParam)
859 {
860 WCHAR awcDevice[CCHDEVICENAME];
861 UNICODE_STRING ustrDevice;
862 DEVMODEW dmLocal;
863 LONG lRet;
864
865 /* Check arguments */
866 if ((dwflags != CDS_VIDEOPARAMETERS) && (lParam != NULL))
867 {
868 EngSetLastError(ERROR_INVALID_PARAMETER);
869 return DISP_CHANGE_BADPARAM;
870 }
871
872 /* Check flags */
873 if ((dwflags & (CDS_GLOBAL|CDS_NORESET)) && !(dwflags & CDS_UPDATEREGISTRY))
874 {
875 return DISP_CHANGE_BADFLAGS;
876 }
877
878 /* Copy the device name */
879 if (pustrDevice)
880 {
881 /* Initialize destination string */
882 RtlInitEmptyUnicodeString(&ustrDevice, awcDevice, sizeof(awcDevice));
883
884 _SEH2_TRY
885 {
886 /* Probe the UNICODE_STRING and the buffer */
887 ProbeForRead(pustrDevice, sizeof(UNICODE_STRING), 1);
888 ProbeForRead(pustrDevice->Buffer, pustrDevice->Length, 1);
889
890 /* Copy the string */
891 RtlCopyUnicodeString(&ustrDevice, pustrDevice);
892 }
893 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
894 {
895 /* Set and return error */
896 SetLastNtError(_SEH2_GetExceptionCode());
897 _SEH2_YIELD(return DISP_CHANGE_BADPARAM);
898 }
899 _SEH2_END
900
901 pustrDevice = &ustrDevice;
902 }
903
904 /* Copy devmode */
905 if (lpDevMode)
906 {
907 _SEH2_TRY
908 {
909 /* Probe the size field of the structure */
910 ProbeForRead(lpDevMode, sizeof(dmLocal.dmSize), 1);
911
912 /* Calculate usable size */
913 dmLocal.dmSize = min(sizeof(dmLocal), lpDevMode->dmSize);
914
915 /* Probe and copy the full DEVMODE */
916 ProbeForRead(lpDevMode, dmLocal.dmSize, 1);
917 RtlCopyMemory(&dmLocal, lpDevMode, dmLocal.dmSize);
918 }
919 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
920 {
921 /* Set and return error */
922 SetLastNtError(_SEH2_GetExceptionCode());
923 _SEH2_YIELD(return DISP_CHANGE_BADPARAM);
924 }
925 _SEH2_END
926
927 /* Check for extra parameters */
928 if (dmLocal.dmDriverExtra > 0)
929 {
930 /* FIXME: TODO */
931 ERR("lpDevMode->dmDriverExtra is IGNORED!\n");
932 dmLocal.dmDriverExtra = 0;
933 }
934
935 /* Use the local structure */
936 lpDevMode = &dmLocal;
937 }
938
939 // FIXME: Copy videoparameters
940
941 /* Acquire global USER lock */
942 UserEnterExclusive();
943
944 /* Call internal function */
945 lRet = UserChangeDisplaySettings(pustrDevice, lpDevMode, dwflags, NULL);
946
947 /* Release lock */
948 UserLeave();
949
950 return lRet;
951 }
952