2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: Cursor and icon functions
5 * FILE: subsystems/win32/win32k/ntuser/cursoricon.c
6 * PROGRAMER: ReactOS Team
9 * We handle two types of cursors/icons:
11 * Loaded without LR_SHARED flag
12 * Private to a process
13 * Can be deleted by calling NtDestroyCursorIcon()
14 * CurIcon->hModule, CurIcon->hRsrc and CurIcon->hGroupRsrc set to NULL
16 * Loaded with LR_SHARED flag
17 * Possibly shared by multiple processes
18 * Immune to NtDestroyCursorIcon()
19 * CurIcon->hModule, CurIcon->hRsrc and CurIcon->hGroupRsrc are valid
20 * There's a M:N relationship between processes and (shared) cursor/icons.
21 * A process can have multiple cursor/icons and a cursor/icon can be used
22 * by multiple processes. To keep track of this we keep a list of all
23 * cursor/icons (CurIconList) and per cursor/icon we keep a list of
24 * CURICON_PROCESS structs starting at CurIcon->ProcessList.
28 DBG_DEFAULT_CHANNEL(UserIcon
);
30 static PPAGED_LOOKASIDE_LIST pgProcessLookasideList
;
31 static LIST_ENTRY gCurIconList
;
33 SYSTEM_CURSORINFO gSysCursorInfo
;
38 pgProcessLookasideList
= ExAllocatePool(NonPagedPool
, sizeof(PAGED_LOOKASIDE_LIST
));
39 if(!pgProcessLookasideList
)
42 ExInitializePagedLookasideList(pgProcessLookasideList
,
46 sizeof(CURICON_PROCESS
),
49 InitializeListHead(&gCurIconList
);
51 gSysCursorInfo
.Enabled
= FALSE
;
52 gSysCursorInfo
.ButtonsDown
= 0;
53 gSysCursorInfo
.bClipped
= FALSE
;
54 gSysCursorInfo
.LastBtnDown
= 0;
55 gSysCursorInfo
.CurrentCursorObject
= NULL
;
56 gSysCursorInfo
.ShowingCursor
= -1;
57 gSysCursorInfo
.ClickLockActive
= FALSE
;
58 gSysCursorInfo
.ClickLockTime
= 0;
66 return &gSysCursorInfo
;
69 /* This function creates a reference for the object! */
70 PCURICON_OBJECT FASTCALL
UserGetCurIconObject(HCURSOR hCurIcon
)
72 PCURICON_OBJECT CurIcon
;
76 EngSetLastError(ERROR_INVALID_CURSOR_HANDLE
);
80 CurIcon
= (PCURICON_OBJECT
)UserReferenceObjectByHandle(hCurIcon
, otCursorIcon
);
83 /* We never set ERROR_INVALID_ICON_HANDLE. lets hope noone ever checks for it */
84 EngSetLastError(ERROR_INVALID_CURSOR_HANDLE
);
88 ASSERT(CurIcon
->head
.cLockObj
>= 1);
92 BOOL
UserSetCursorPos( INT x
, INT y
, DWORD flags
, ULONG_PTR dwExtraInfo
, BOOL Hook
)
95 PSYSTEM_CURSORINFO CurInfo
;
100 if(!(DesktopWindow
= UserGetDesktopWindow()))
105 CurInfo
= IntGetSysCursorInfo();
107 /* Clip cursor position */
108 if (!CurInfo
->bClipped
)
109 rcClip
= DesktopWindow
->rcClient
;
111 rcClip
= CurInfo
->rcClip
;
113 if(x
>= rcClip
.right
) x
= rcClip
.right
- 1;
114 if(x
< rcClip
.left
) x
= rcClip
.left
;
115 if(y
>= rcClip
.bottom
) y
= rcClip
.bottom
- 1;
116 if(y
< rcClip
.top
) y
= rcClip
.top
;
121 /* 1. Generate a mouse move message, this sets the htEx and Track Window too. */
122 Msg
.message
= WM_MOUSEMOVE
;
123 Msg
.wParam
= UserGetMouseButtonsState();
124 Msg
.lParam
= MAKELPARAM(x
, y
);
126 co_MsqInsertMouseMessage(&Msg
, flags
, dwExtraInfo
, Hook
);
128 /* 2. Store the new cursor position */
135 * We have to register that this object is in use by the current
136 * process. The only way to do that seems to be to walk the list
137 * of cursor/icon objects starting at W32Process->CursorIconListHead.
138 * If the object is already present in the list, we don't have to do
139 * anything, if it's not present we add it and inc the ProcessCount
140 * in the object. Having to walk the list kind of sucks, but that's
143 static BOOLEAN FASTCALL
144 ReferenceCurIconByProcess(PCURICON_OBJECT CurIcon
)
146 PPROCESSINFO Win32Process
;
147 PCURICON_PROCESS Current
;
149 Win32Process
= PsGetCurrentProcessWin32Process();
151 LIST_FOR_EACH(Current
, &CurIcon
->ProcessList
, CURICON_PROCESS
, ListEntry
)
153 if (Current
->Process
== Win32Process
)
155 /* Already registered for this process */
160 /* Not registered yet */
161 Current
= ExAllocateFromPagedLookasideList(pgProcessLookasideList
);
166 InsertHeadList(&CurIcon
->ProcessList
, &Current
->ListEntry
);
167 Current
->Process
= Win32Process
;
175 IntFindExistingCurIconObject(
176 PUNICODE_STRING pustrModule
,
177 PUNICODE_STRING pustrRsrc
,
181 PCURICON_OBJECT CurIcon
;
183 LIST_FOR_EACH(CurIcon
, &gCurIconList
, CURICON_OBJECT
, ListEntry
)
185 /* See if module names match */
186 if(RtlCompareUnicodeString(pustrModule
, &CurIcon
->ustrModule
, TRUE
) == 0)
188 /* They do. Now see if this is the same resource */
189 if(IS_INTRESOURCE(CurIcon
->ustrRsrc
.Buffer
) && IS_INTRESOURCE(pustrRsrc
->Buffer
))
191 if(CurIcon
->ustrRsrc
.Buffer
!= pustrRsrc
->Buffer
)
194 else if(IS_INTRESOURCE(CurIcon
->ustrRsrc
.Buffer
) || IS_INTRESOURCE(pustrRsrc
->Buffer
))
196 else if(RtlCompareUnicodeString(pustrRsrc
, &CurIcon
->ustrRsrc
, TRUE
) != 0)
199 if ((cxDesired
== CurIcon
->Size
.cx
) &&(cyDesired
== CurIcon
->Size
.cy
))
201 if (! ReferenceCurIconByProcess(CurIcon
))
215 IntCreateCurIconHandle(DWORD dwNumber
)
217 PCURICON_OBJECT CurIcon
;
218 BOOLEAN bIcon
= dwNumber
== 0;
224 CurIcon
= UserCreateObject(gHandleTable
, NULL
, NULL
, &hCurIcon
, otCursorIcon
, FIELD_OFFSET(CURICON_OBJECT
, aFrame
[dwNumber
]));
228 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
232 CurIcon
->Self
= hCurIcon
;
233 CurIcon
->bIcon
= bIcon
;
234 InitializeListHead(&CurIcon
->ProcessList
);
236 if (! ReferenceCurIconByProcess(CurIcon
))
238 ERR("Failed to add process\n");
239 UserDeleteObject(hCurIcon
, otCursorIcon
);
240 UserDereferenceObject(CurIcon
);
244 InsertHeadList(&gCurIconList
, &CurIcon
->ListEntry
);
250 IntDestroyCurIconObject(PCURICON_OBJECT CurIcon
, PPROCESSINFO ppi
, BOOLEAN bForce
)
252 PSYSTEM_CURSORINFO CurInfo
;
253 HBITMAP bmpMask
, bmpColor
, bmpAlpha
;
254 BOOLEAN Ret
, bListEmpty
, bFound
= FALSE
;
255 PCURICON_PROCESS Current
= NULL
;
257 /* For handles created without any data (error handling) */
258 if(IsListEmpty(&CurIcon
->ProcessList
))
261 /* Now find this process in the list of processes referencing this object and
262 remove it from that list */
263 LIST_FOR_EACH(Current
, &CurIcon
->ProcessList
, CURICON_PROCESS
, ListEntry
)
265 if (Current
->Process
== ppi
)
268 bListEmpty
= RemoveEntryList(&Current
->ListEntry
);
275 /* This object doesn't belong to this process */
276 EngSetLastError(ERROR_INVALID_HANDLE
);
277 /* Caller expects us to dereference! */
278 UserDereferenceObject(CurIcon
);
282 /* We found our process, but we're told to not destroy it in case it is shared */
283 if((CurIcon
->ustrModule
.Buffer
!= NULL
) && !bForce
)
285 /* Tests show this is a valid call */
286 UserDereferenceObject(CurIcon
);
290 ExFreeToPagedLookasideList(pgProcessLookasideList
, Current
);
292 /* If there are still processes referencing this object we can't destroy it yet */
295 if(CurIcon
->head
.ppi
== ppi
)
297 /* Set the first process of the list as owner */
298 Current
= CONTAINING_RECORD(CurIcon
->ProcessList
.Flink
, CURICON_PROCESS
, ListEntry
);
299 UserSetObjectOwner(CurIcon
, otCursorIcon
, Current
->Process
);
301 UserDereferenceObject(CurIcon
);
306 /* Remove it from the list */
307 RemoveEntryList(&CurIcon
->ListEntry
);
309 CurInfo
= IntGetSysCursorInfo();
311 if (CurInfo
->CurrentCursorObject
== CurIcon
)
313 /* Hide the cursor if we're destroying the current cursor */
314 UserSetCursor(NULL
, TRUE
);
317 bmpMask
= CurIcon
->aFrame
[0].hbmMask
;
318 bmpColor
= CurIcon
->aFrame
[0].hbmColor
;
319 bmpAlpha
= CurIcon
->aFrame
[0].hbmAlpha
;
324 GreSetObjectOwner(bmpMask
, GDI_OBJ_HMGR_POWNED
);
325 GreDeleteObject(bmpMask
);
326 CurIcon
->aFrame
[0].hbmMask
= NULL
;
330 GreSetObjectOwner(bmpColor
, GDI_OBJ_HMGR_POWNED
);
331 GreDeleteObject(bmpColor
);
332 CurIcon
->aFrame
[0].hbmColor
= NULL
;
336 GreSetObjectOwner(bmpAlpha
, GDI_OBJ_HMGR_POWNED
);
337 GreDeleteObject(bmpAlpha
);
338 CurIcon
->aFrame
[0].hbmAlpha
= NULL
;
341 if(!IS_INTRESOURCE(CurIcon
->ustrRsrc
.Buffer
))
342 ExFreePoolWithTag(CurIcon
->ustrRsrc
.Buffer
, TAG_STRING
);
343 if(CurIcon
->ustrModule
.Buffer
)
344 ReleaseCapturedUnicodeString(&CurIcon
->ustrModule
, UserMode
);
346 /* We were given a pointer, no need to keep the reference anylonger! */
347 UserDereferenceObject(CurIcon
);
348 Ret
= UserDeleteObject(CurIcon
->Self
, otCursorIcon
);
354 IntCleanupCurIcons(struct _EPROCESS
*Process
, PPROCESSINFO Win32Process
)
356 PCURICON_OBJECT CurIcon
, tmp
;
358 /* Run through the list of icon objects */
359 LIST_FOR_EACH_SAFE(CurIcon
, tmp
, &gCurIconList
, CURICON_OBJECT
, ListEntry
)
361 UserReferenceObject(CurIcon
);
362 IntDestroyCurIconObject(CurIcon
, Win32Process
, TRUE
);
373 _In_ HANDLE hCurIcon
,
374 _Out_opt_ PICONINFO IconInfo
,
375 _Out_opt_ PUNICODE_STRING lpModule
, // Optional
376 _Out_opt_ PUNICODE_STRING lpResName
, // Optional
377 _Out_opt_ LPDWORD pbpp
, // Optional
381 PCURICON_OBJECT CurIcon
;
382 NTSTATUS Status
= STATUS_SUCCESS
;
386 TRACE("Enter NtUserGetIconInfo\n");
388 /* Check if something was actually asked */
389 if (!IconInfo
&& !lpModule
&& !lpResName
)
391 WARN("Nothing to fill.\n");
392 EngSetLastError(ERROR_INVALID_PARAMETER
);
396 UserEnterExclusive();
398 if (!(CurIcon
= UserGetCurIconObject(hCurIcon
)))
400 WARN("UserGetIconObject(0x%08x) Failed.\n", hCurIcon
);
405 /* Give back the icon information */
409 ii
.fIcon
= CurIcon
->bIcon
;
410 ii
.xHotspot
= CurIcon
->ptlHotspot
.x
;
411 ii
.yHotspot
= CurIcon
->ptlHotspot
.y
;
414 ii
.hbmMask
= BITMAP_CopyBitmap(CurIcon
->aFrame
[0].hbmMask
);
415 GreSetObjectOwner(ii
.hbmMask
, GDI_OBJ_HMGR_POWNED
);
416 ii
.hbmColor
= BITMAP_CopyBitmap(CurIcon
->aFrame
[0].hbmColor
);
417 GreSetObjectOwner(ii
.hbmColor
, GDI_OBJ_HMGR_POWNED
);
423 psurfBmp
= SURFACE_ShareLockSurface(CurIcon
->aFrame
[0].hbmColor
);
426 colorBpp
= BitsPerFormat(psurfBmp
->SurfObj
.iBitmapFormat
);
427 SURFACE_ShareUnlockSurface(psurfBmp
);
434 ProbeForWrite(IconInfo
, sizeof(ICONINFO
), 1);
435 RtlCopyMemory(IconInfo
, &ii
, sizeof(ICONINFO
));
439 ProbeForWrite(pbpp
, sizeof(DWORD
), 1);
443 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
445 Status
= _SEH2_GetExceptionCode();
450 if (!NT_SUCCESS(Status
))
452 WARN("Status: 0x%08x.\n", Status
);
453 SetLastNtError(Status
);
457 /* Give back the module name */
460 if(!CurIcon
->ustrModule
.Buffer
)
462 EngSetLastError(ERROR_INVALID_HANDLE
);
465 /* Copy what we can */
468 ProbeForWrite(lpModule
, sizeof(UNICODE_STRING
), 1);
469 ProbeForWrite(lpModule
->Buffer
, lpModule
->MaximumLength
, 1);
470 lpModule
->Length
= min(lpModule
->MaximumLength
, CurIcon
->ustrModule
.Length
);
471 RtlCopyMemory(lpModule
->Buffer
, CurIcon
->ustrModule
.Buffer
, lpModule
->Length
);
473 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
475 Status
= _SEH2_GetExceptionCode();
480 if (!NT_SUCCESS(Status
))
482 SetLastNtError(Status
);
488 if(!CurIcon
->ustrRsrc
.Buffer
)
490 EngSetLastError(ERROR_INVALID_HANDLE
);
496 ProbeForWrite(lpResName
, sizeof(UNICODE_STRING
), 1);
497 if(IS_INTRESOURCE(CurIcon
->ustrRsrc
.Buffer
))
499 lpResName
->Buffer
= CurIcon
->ustrRsrc
.Buffer
;
500 lpResName
->Length
= 0;
504 lpResName
->Length
= min(lpResName
->MaximumLength
, CurIcon
->ustrRsrc
.Length
);
505 RtlCopyMemory(lpResName
->Buffer
, CurIcon
->ustrRsrc
.Buffer
, lpResName
->Length
);
508 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
510 Status
= _SEH2_GetExceptionCode();
515 if (!NT_SUCCESS(Status
))
517 SetLastNtError(Status
);
524 UserDereferenceObject(CurIcon
);
526 TRACE("Leave NtUserGetIconInfo, ret=%i\n", Ret
);
541 PLONG plcx
, // &size.cx
542 PLONG plcy
) // &size.cy
544 PCURICON_OBJECT CurIcon
;
545 NTSTATUS Status
= STATUS_SUCCESS
;
548 TRACE("Enter NtUserGetIconSize\n");
549 UserEnterExclusive();
551 if (!(CurIcon
= UserGetCurIconObject(hCurIcon
)))
558 ProbeForWrite(plcx
, sizeof(LONG
), 1);
559 RtlCopyMemory(plcx
, &CurIcon
->Size
.cx
, sizeof(LONG
));
560 ProbeForWrite(plcy
, sizeof(LONG
), 1);
561 RtlCopyMemory(plcy
, &CurIcon
->Size
.cy
, sizeof(LONG
));
563 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
565 Status
= _SEH2_GetExceptionCode();
569 if (NT_SUCCESS(Status
))
572 SetLastNtError(Status
); // Maybe not, test this
574 UserDereferenceObject(CurIcon
);
577 TRACE("Leave NtUserGetIconSize, ret=%i\n", bRet
);
592 PSYSTEM_CURSORINFO CurInfo
;
593 NTSTATUS Status
= STATUS_SUCCESS
;
594 PCURICON_OBJECT CurIcon
;
596 DECLARE_RETURN(BOOL
);
598 TRACE("Enter NtUserGetCursorInfo\n");
599 UserEnterExclusive();
601 CurInfo
= IntGetSysCursorInfo();
602 CurIcon
= (PCURICON_OBJECT
)CurInfo
->CurrentCursorObject
;
604 SafeCi
.cbSize
= sizeof(CURSORINFO
);
605 SafeCi
.flags
= ((CurIcon
&& CurInfo
->ShowingCursor
>= 0) ? CURSOR_SHOWING
: 0);
606 SafeCi
.hCursor
= (CurIcon
? (HCURSOR
)CurIcon
->Self
: (HCURSOR
)0);
608 SafeCi
.ptScreenPos
= gpsi
->ptCursor
;
612 if (pci
->cbSize
== sizeof(CURSORINFO
))
614 ProbeForWrite(pci
, sizeof(CURSORINFO
), 1);
615 RtlCopyMemory(pci
, &SafeCi
, sizeof(CURSORINFO
));
620 EngSetLastError(ERROR_INVALID_PARAMETER
);
623 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
625 Status
= _SEH2_GetExceptionCode();
628 if (!NT_SUCCESS(Status
))
630 SetLastNtError(Status
);
636 TRACE("Leave NtUserGetCursorInfo, ret=%i\n",_ret_
);
646 /* FIXME: Check if process has WINSTA_WRITEATTRIBUTES */
647 PSYSTEM_CURSORINFO CurInfo
;
648 PWND DesktopWindow
= NULL
;
650 CurInfo
= IntGetSysCursorInfo();
652 DesktopWindow
= UserGetDesktopWindow();
654 if (prcl
!= NULL
&& DesktopWindow
!= NULL
)
656 if (prcl
->right
< prcl
->left
|| prcl
->bottom
< prcl
->top
)
658 EngSetLastError(ERROR_INVALID_PARAMETER
);
662 CurInfo
->bClipped
= TRUE
;
664 /* Set nw cliping region. Note: we can't use RECTL_bIntersectRect because
665 it sets rect to 0 0 0 0 when it's empty. For more info see monitor winetest */
666 CurInfo
->rcClip
.left
= max(prcl
->left
, DesktopWindow
->rcWindow
.left
);
667 CurInfo
->rcClip
.right
= min(prcl
->right
, DesktopWindow
->rcWindow
.right
);
668 if (CurInfo
->rcClip
.right
< CurInfo
->rcClip
.left
)
669 CurInfo
->rcClip
.right
= CurInfo
->rcClip
.left
;
671 CurInfo
->rcClip
.top
= max(prcl
->top
, DesktopWindow
->rcWindow
.top
);
672 CurInfo
->rcClip
.bottom
= min(prcl
->bottom
, DesktopWindow
->rcWindow
.bottom
);
673 if (CurInfo
->rcClip
.bottom
< CurInfo
->rcClip
.top
)
674 CurInfo
->rcClip
.bottom
= CurInfo
->rcClip
.top
;
676 /* Make sure cursor is in clipping region */
677 UserSetCursorPos(gpsi
->ptCursor
.x
, gpsi
->ptCursor
.y
, 0, 0, FALSE
);
681 CurInfo
->bClipped
= FALSE
;
702 /* Probe and copy rect */
703 ProbeForRead(prcl
, sizeof(RECTL
), 1);
706 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
708 EngSetLastError(ERROR_INVALID_PARAMETER
);
709 _SEH2_YIELD(return FALSE
;)
716 UserEnterExclusive();
718 /* Call the internal function */
719 bResult
= UserClipCursor(prcl
);
733 _In_ HANDLE hCurIcon
,
736 PCURICON_OBJECT CurIcon
;
738 DECLARE_RETURN(BOOL
);
740 TRACE("Enter NtUserDestroyCursorIcon\n");
741 UserEnterExclusive();
743 if (!(CurIcon
= UserGetCurIconObject(hCurIcon
)))
748 ret
= IntDestroyCurIconObject(CurIcon
, PsGetCurrentProcessWin32Process(), bForce
);
749 /* Note: IntDestroyCurIconObject will remove our reference for us! */
754 TRACE("Leave NtUserDestroyCursorIcon, ret=%i\n",_ret_
);
765 NtUserFindExistingCursorIcon(
766 _In_ PUNICODE_STRING pustrModule
,
767 _In_ PUNICODE_STRING pustrRsrc
,
771 PCURICON_OBJECT CurIcon
;
773 UNICODE_STRING ustrModuleSafe
, ustrRsrcSafe
;
776 TRACE("Enter NtUserFindExistingCursorIcon\n");
778 /* Capture resource name (it can be an INTRESOURCE == ATOM) */
779 Status
= ProbeAndCaptureUnicodeStringOrAtom(&ustrRsrcSafe
, pustrRsrc
);
780 if(!NT_SUCCESS(Status
))
782 Status
= ProbeAndCaptureUnicodeString(&ustrModuleSafe
, UserMode
, pustrModule
);
783 if(!NT_SUCCESS(Status
))
786 UserEnterExclusive();
787 CurIcon
= IntFindExistingCurIconObject(&ustrModuleSafe
, &ustrRsrcSafe
, cxDesired
, cyDesired
);
793 if(!IS_INTRESOURCE(ustrRsrcSafe
.Buffer
))
794 ExFreePoolWithTag(ustrRsrcSafe
.Buffer
, TAG_STRING
);
795 ReleaseCapturedUnicodeString(&ustrModuleSafe
, UserMode
);
809 /* FIXME: Check if process has WINSTA_READATTRIBUTES */
810 PSYSTEM_CURSORINFO CurInfo
;
813 DECLARE_RETURN(BOOL
);
815 TRACE("Enter NtUserGetClipCursor\n");
816 UserEnterExclusive();
821 CurInfo
= IntGetSysCursorInfo();
822 if (CurInfo
->bClipped
)
824 Rect
= CurInfo
->rcClip
;
830 Rect
.right
= UserGetSystemMetrics(SM_CXSCREEN
);
831 Rect
.bottom
= UserGetSystemMetrics(SM_CYSCREEN
);
834 Status
= MmCopyToCaller(lpRect
, &Rect
, sizeof(RECT
));
835 if (!NT_SUCCESS(Status
))
837 SetLastNtError(Status
);
844 TRACE("Leave NtUserGetClipCursor, ret=%i\n",_ret_
);
858 PCURICON_OBJECT pcurOld
, pcurNew
;
859 HCURSOR hOldCursor
= NULL
;
861 TRACE("Enter NtUserSetCursor\n");
862 UserEnterExclusive();
866 pcurNew
= UserGetCurIconObject(hCursor
);
869 EngSetLastError(ERROR_INVALID_CURSOR_HANDLE
);
878 pcurOld
= UserSetCursor(pcurNew
, FALSE
);
881 hOldCursor
= (HCURSOR
)pcurOld
->Self
;
882 UserDereferenceObject(pcurOld
);
896 NtUserSetCursorContents(
898 PICONINFO UnsafeIconInfo
)
900 PCURICON_OBJECT CurIcon
;
905 DECLARE_RETURN(BOOL
);
907 TRACE("Enter NtUserSetCursorContents\n");
908 UserEnterExclusive();
910 if (!(CurIcon
= UserGetCurIconObject(hCurIcon
)))
916 Status
= MmCopyFromCaller(&IconInfo
, UnsafeIconInfo
, sizeof(ICONINFO
));
917 if (!NT_SUCCESS(Status
))
919 SetLastNtError(Status
);
924 /* Check if we get valid information */
925 if(IconInfo
.fIcon
!= CurInfo
->bIcon
)
927 EngSetLastError(ERROR_INVALID_PARAMETER
);
932 /* Delete old bitmaps */
933 if (CurIcon
->aFrame
[0].hbmColor
)
934 GreDeleteObject(CurIcon
->aFrame
[0].hbmColor
);
935 if (CurIcon
->aFrame
[0].hbmMask
)
936 GreDeleteObject(CurIcon
->aFrame
[0].hbmMask
);
937 if(CurIcon
->aFrame
[0].hbmAlpha
)
938 GreDeleteObject(CurIcon
->aFrame
[0].hbmAlpha
);
941 CurIcon
->bIcon
= IconInfo
.fIcon
;
942 CurIcon
->ptlHotspot
.x
= IconInfo
.xHotspot
;
943 CurIcon
->ptlHotspot
.y
= IconInfo
.yHotspot
;
944 CurIcon
->aFrame
[0].hbmMask
= IconInfo
.hbmMask
;
945 CurIcon
->aFrame
[0].hbmColor
= IconInfo
.hbmColor
;
946 CurIcon
->aFrame
[0].hbmAlpha
= NULL
;
948 if (IconInfo
.hbmColor
)
950 BOOLEAN bAlpha
= FALSE
;
951 psurfBmp
= SURFACE_ShareLockSurface(IconInfo
.hbmColor
);
954 CurIcon
->Size
.cx
= psurfBmp
->SurfObj
.sizlBitmap
.cx
;
955 CurIcon
->Size
.cy
= psurfBmp
->SurfObj
.sizlBitmap
.cy
;
957 /* 32bpp bitmap is likely to have an alpha channel */
958 if(psurfBmp
->SurfObj
.iBitmapFormat
== BMF_32BPP
)
960 PFN_DIB_GetPixel fn_GetPixel
= DibFunctionsForBitmapFormat
[BMF_32BPP
].DIB_GetPixel
;
963 fn_GetPixel
= DibFunctionsForBitmapFormat
[BMF_32BPP
].DIB_GetPixel
;
964 for (i
= 0; i
< psurfBmp
->SurfObj
.sizlBitmap
.cx
; i
++)
966 for (j
= 0; j
< psurfBmp
->SurfObj
.sizlBitmap
.cy
; j
++)
968 bAlpha
= ((BYTE
)(fn_GetPixel(&psurfBmp
->SurfObj
, i
, j
) >> 24)) != 0;
976 /* We're done with this one */
977 SURFACE_ShareUnlockSurface(psurfBmp
);
978 GreSetObjectOwner(IconInfo
.hbmColor
, GDI_OBJ_HMGR_PUBLIC
);
985 /* Copy the bitmap */
986 CurIcon
->aFrame
[0].hbmAlpha
= BITMAP_CopyBitmap(IconInfo
.hbmColor
);
987 if(!CurIcon
->aFrame
[0].hbmAlpha
)
989 ERR("BITMAP_CopyBitmap failed!");
993 psurfBmp
= SURFACE_ShareLockSurface(CurIcon
->aFrame
[0].hbmAlpha
);
996 ERR("SURFACE_LockSurface failed!\n");
1000 /* Premultiply with the alpha channel value */
1001 for (i
= 0; i
< psurfBmp
->SurfObj
.sizlBitmap
.cy
; i
++)
1003 ptr
= (PBYTE
)psurfBmp
->SurfObj
.pvScan0
+ i
*psurfBmp
->SurfObj
.lDelta
;
1004 for (j
= 0; j
< psurfBmp
->SurfObj
.sizlBitmap
.cx
; j
++)
1007 ptr
[0] = (ptr
[0] * Alpha
) / 0xff;
1008 ptr
[1] = (ptr
[1] * Alpha
) / 0xff;
1009 ptr
[2] = (ptr
[2] * Alpha
) / 0xff;
1013 SURFACE_ShareUnlockSurface(psurfBmp
);
1014 GreSetObjectOwner(CurIcon
->aFrame
[0].hbmAlpha
, GDI_OBJ_HMGR_PUBLIC
);
1019 psurfBmp
= SURFACE_ShareLockSurface(IconInfo
.hbmMask
);
1023 CurIcon
->Size
.cx
= psurfBmp
->SurfObj
.sizlBitmap
.cx
;
1024 CurIcon
->Size
.cy
= psurfBmp
->SurfObj
.sizlBitmap
.cy
/ 2;
1026 SURFACE_ShareUnlockSurface(psurfBmp
);
1028 GreSetObjectOwner(IconInfo
.hbmMask
, GDI_OBJ_HMGR_PUBLIC
);
1036 UserDereferenceObject(CurIcon
);
1041 TRACE("Leave NtUserSetCursorContents, ret=%i\n",_ret_
);
1052 NtUserSetCursorIconData(
1053 _In_ HCURSOR Handle
,
1054 _In_opt_ PUNICODE_STRING pustrModule
,
1055 _In_opt_ PUNICODE_STRING pustrRsrc
,
1056 _In_ PICONINFO pIconInfo
)
1058 PCURICON_OBJECT CurIcon
;
1060 NTSTATUS Status
= STATUS_SUCCESS
;
1064 TRACE("Enter NtUserSetCursorIconData\n");
1066 /* If a module name is provided, we need a resource name, and vice versa */
1067 if((pustrModule
&& !pustrRsrc
) || (!pustrModule
&& pustrRsrc
))
1070 UserEnterExclusive();
1072 if (!(CurIcon
= UserGetCurIconObject(Handle
)))
1075 EngSetLastError(ERROR_INVALID_HANDLE
);
1081 ProbeForRead(pIconInfo
, sizeof(ICONINFO
), 1);
1084 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1086 Status
= _SEH2_GetExceptionCode();
1090 if (!NT_SUCCESS(Status
))
1092 SetLastNtError(Status
);
1096 /* This is probably not what windows does, but consistency checks can't hurt */
1097 if(CurIcon
->bIcon
!= ii
.fIcon
)
1099 EngSetLastError(ERROR_INVALID_PARAMETER
);
1102 CurIcon
->ptlHotspot
.x
= ii
.xHotspot
;
1103 CurIcon
->ptlHotspot
.y
= ii
.yHotspot
;
1107 EngSetLastError(ERROR_INVALID_PARAMETER
);
1111 CurIcon
->aFrame
[0].hbmMask
= BITMAP_CopyBitmap(ii
.hbmMask
);
1112 if(!CurIcon
->aFrame
[0].hbmMask
)
1117 CurIcon
->aFrame
[0].hbmColor
= BITMAP_CopyBitmap(ii
.hbmColor
);
1118 if(!CurIcon
->aFrame
[0].hbmColor
)
1122 if (CurIcon
->aFrame
[0].hbmColor
)
1124 psurfBmp
= SURFACE_ShareLockSurface(CurIcon
->aFrame
[0].hbmColor
);
1128 CurIcon
->Size
.cx
= psurfBmp
->SurfObj
.sizlBitmap
.cx
;
1129 CurIcon
->Size
.cy
= psurfBmp
->SurfObj
.sizlBitmap
.cy
;
1130 SURFACE_ShareUnlockSurface(psurfBmp
);
1131 GreSetObjectOwner(CurIcon
->aFrame
[0].hbmColor
, GDI_OBJ_HMGR_PUBLIC
);
1135 psurfBmp
= SURFACE_ShareLockSurface(CurIcon
->aFrame
[0].hbmMask
);
1139 CurIcon
->Size
.cx
= psurfBmp
->SurfObj
.sizlBitmap
.cx
;
1140 CurIcon
->Size
.cy
= psurfBmp
->SurfObj
.sizlBitmap
.cy
/2;
1141 SURFACE_ShareUnlockSurface(psurfBmp
);
1143 GreSetObjectOwner(CurIcon
->aFrame
[0].hbmMask
, GDI_OBJ_HMGR_PUBLIC
);
1147 /* We use this convenient function, because INTRESOURCEs and ATOMs are the same */
1148 Status
= ProbeAndCaptureUnicodeStringOrAtom(&CurIcon
->ustrRsrc
, pustrRsrc
);
1149 if(!NT_SUCCESS(Status
))
1151 Status
= ProbeAndCaptureUnicodeString(&CurIcon
->ustrModule
, UserMode
, pustrModule
);
1152 if(!NT_SUCCESS(Status
))
1159 UserDereferenceObject(CurIcon
);
1162 if (CurIcon
->aFrame
[0].hbmMask
)
1164 GreSetObjectOwner(CurIcon
->aFrame
[0].hbmMask
, GDI_OBJ_HMGR_POWNED
);
1165 GreDeleteObject(CurIcon
->aFrame
[0].hbmMask
);
1166 CurIcon
->aFrame
[0].hbmMask
= NULL
;
1168 if (CurIcon
->aFrame
[0].hbmColor
)
1170 GreSetObjectOwner(CurIcon
->aFrame
[0].hbmColor
, GDI_OBJ_HMGR_POWNED
);
1171 GreDeleteObject(CurIcon
->aFrame
[0].hbmColor
);
1172 CurIcon
->aFrame
[0].hbmColor
= NULL
;
1174 if(!IS_INTRESOURCE(CurIcon
->ustrRsrc
.Buffer
))
1175 ExFreePoolWithTag(CurIcon
->ustrRsrc
.Buffer
, TAG_STRING
);
1176 if(CurIcon
->ustrModule
.Buffer
)
1177 ReleaseCapturedUnicodeString(&CurIcon
->ustrModule
, UserMode
);
1180 TRACE("Leave NtUserSetCursorIconData, ret=%i\n",Ret
);
1186 /* Mostly inspired from wine code.
1187 * We use low level functions because:
1188 * - at this point, the icon bitmap could have a different bit depth than the DC,
1189 * making it thus impossible to use NtCreateCompatibleDC and selecting the bitmap.
1190 * This happens after a mode setting change.
1191 * - it avoids massive GDI objects locking when only the destination surface needs it.
1192 * - It makes (small) performance gains.
1199 PCURICON_OBJECT pIcon
,
1203 HBRUSH hbrFlickerFreeDraw
,
1206 PSURFACE psurfDest
, psurfMask
, psurfColor
; //, psurfOffScreen = NULL;
1209 HBITMAP hbmMask
, hbmColor
, hbmAlpha
;
1211 RECTL rcDest
, rcSrc
;
1212 CLIPOBJ
* pdcClipObj
= NULL
;
1216 if((diFlags
& DI_NORMAL
) == 0)
1218 ERR("DrawIconEx called without mask or color bitmap to draw.\n");
1222 hbmMask
= pIcon
->aFrame
[0].hbmMask
;
1223 hbmColor
= pIcon
->aFrame
[0].hbmColor
;
1224 hbmAlpha
= pIcon
->aFrame
[0].hbmAlpha
;
1227 ERR("NtUserDrawIconEx: istepIfAniCur is not supported!\n");
1231 * Shared locks are enough, we are only reading those bitmaps
1233 psurfMask
= SURFACE_ShareLockSurface(hbmMask
);
1234 if(psurfMask
== NULL
)
1236 ERR("Unable to lock the mask surface.\n");
1240 /* Color bitmap is not mandatory */
1241 if(hbmColor
== NULL
)
1243 /* But then the mask bitmap must have the information in it's bottom half */
1244 ASSERT(psurfMask
->SurfObj
.sizlBitmap
.cy
== 2*pIcon
->Size
.cy
);
1247 else if ((psurfColor
= SURFACE_ShareLockSurface(hbmColor
)) == NULL
)
1249 ERR("Unable to lock the color bitmap.\n");
1250 SURFACE_ShareUnlockSurface(psurfMask
);
1254 pdc
= DC_LockDc(hDc
);
1257 ERR("Could not lock the destination DC.\n");
1258 SURFACE_ShareUnlockSurface(psurfMask
);
1259 if(psurfColor
) SURFACE_ShareUnlockSurface(psurfColor
);
1262 /* Calculate destination rectangle */
1263 RECTL_vSetRect(&rcDest
, xLeft
, yTop
, xLeft
+ cxWidth
, yTop
+ cyHeight
);
1264 IntLPtoDP(pdc
, (LPPOINT
)&rcDest
, 2);
1265 RECTL_vOffsetRect(&rcDest
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1267 /* Prepare the underlying surface */
1268 DC_vPrepareDCsForBlit(pdc
, rcDest
, NULL
, rcDest
);
1270 /* We now have our destination surface and rectangle */
1271 psurfDest
= pdc
->dclevel
.pSurface
;
1273 if(psurfDest
== NULL
)
1276 DC_vFinishBlit(pdc
, NULL
);
1278 SURFACE_ShareUnlockSurface(psurfMask
);
1279 if(psurfColor
) SURFACE_ShareUnlockSurface(psurfColor
);
1283 /* Set source rect */
1284 RECTL_vSetRect(&rcSrc
, 0, 0, pIcon
->Size
.cx
, pIcon
->Size
.cy
);
1286 /* Fix width parameter, if needed */
1289 if(diFlags
& DI_DEFAULTSIZE
)
1290 cxWidth
= pIcon
->bIcon
?
1291 UserGetSystemMetrics(SM_CXICON
) : UserGetSystemMetrics(SM_CXCURSOR
);
1293 cxWidth
= pIcon
->Size
.cx
;
1296 /* Fix height parameter, if needed */
1299 if(diFlags
& DI_DEFAULTSIZE
)
1300 cyHeight
= pIcon
->bIcon
?
1301 UserGetSystemMetrics(SM_CYICON
) : UserGetSystemMetrics(SM_CYCURSOR
);
1303 cyHeight
= pIcon
->Size
.cy
;
1306 /* Should we render off-screen? */
1307 bOffScreen
= hbrFlickerFreeDraw
&&
1308 (GDI_HANDLE_GET_TYPE(hbrFlickerFreeDraw
) == GDI_OBJECT_TYPE_BRUSH
);
1312 /* Yes: Allocate and paint the offscreen surface */
1314 PBRUSH pbrush
= BRUSH_ShareLockBrush(hbrFlickerFreeDraw
);
1316 TRACE("Performing off-screen rendering.\n");
1320 ERR("Failed to get brush object.\n");
1324 #if 0 //We lock the hdc surface during the whole function it makes no sense to use an offscreen surface for "flicker free" drawing
1325 psurfOffScreen
= SURFACE_AllocSurface(STYPE_BITMAP
,
1326 cxWidth
, cyHeight
, psurfDest
->SurfObj
.iBitmapFormat
,
1330 ERR("Failed to allocate the off-screen surface.\n");
1331 BRUSH_ShareUnlockBrush(pbrush
);
1335 /* Paint the brush */
1336 EBRUSHOBJ_vInit(&eboFill
, pbrush
, psurfOffScreen
, 0x00FFFFFF, 0, NULL
);
1337 RECTL_vSetRect(&rcDest
, 0, 0, cxWidth
, cyHeight
);
1339 Ret
= IntEngBitBlt(&psurfOffScreen
->SurfObj
,
1347 &eboFill
.BrushObject
,
1351 /* Clean up everything */
1352 EBRUSHOBJ_vCleanup(&eboFill
);
1353 BRUSH_ShareUnlockBrush(pbrush
);
1357 ERR("Failed to paint the off-screen surface.\n");
1361 /* We now have our destination surface */
1362 psurfDest
= psurfOffScreen
;
1364 pdcClipObj
= pdc
->rosdc
.CombinedClip
;
1365 /* Paint the brush */
1366 EBRUSHOBJ_vInit(&eboFill
, pbrush
, psurfDest
, 0x00FFFFFF, 0, NULL
);
1368 Ret
= IntEngBitBlt(&psurfDest
->SurfObj
,
1376 &eboFill
.BrushObject
,
1380 /* Clean up everything */
1381 EBRUSHOBJ_vCleanup(&eboFill
);
1382 BRUSH_ShareUnlockBrush(pbrush
);
1386 ERR("Failed to paint the off-screen surface.\n");
1393 /* We directly draw to the DC */
1394 TRACE("Performing on screen rendering.\n");
1395 pdcClipObj
= pdc
->rosdc
.CombinedClip
;
1396 // psurfOffScreen = NULL;
1399 /* Now do the rendering */
1400 if(hbmAlpha
&& (diFlags
& DI_IMAGE
))
1402 BLENDOBJ blendobj
= { {AC_SRC_OVER
, 0, 255, AC_SRC_ALPHA
} };
1403 PSURFACE psurf
= NULL
;
1405 psurf
= SURFACE_ShareLockSurface(hbmAlpha
);
1408 ERR("SURFACE_LockSurface failed!\n");
1412 /* Initialize color translation object */
1413 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xFFFFFFFF, 0xFFFFFFFF, 0);
1416 Ret
= IntEngAlphaBlend(&psurfDest
->SurfObj
,
1424 EXLATEOBJ_vCleanup(&exlo
);
1425 SURFACE_ShareUnlockSurface(psurf
);
1427 ERR("NtGdiAlphaBlend failed!\n");
1430 if (diFlags
& DI_MASK
)
1432 DWORD rop4
= (diFlags
& DI_IMAGE
) ? ROP4_SRCAND
: ROP4_SRCCOPY
;
1434 EXLATEOBJ_vInitSrcMonoXlate(&exlo
, psurfDest
->ppal
, 0x00FFFFFF, 0);
1436 Ret
= IntEngStretchBlt(&psurfDest
->SurfObj
,
1437 &psurfMask
->SurfObj
,
1449 EXLATEOBJ_vCleanup(&exlo
);
1453 ERR("Failed to mask the bitmap data.\n");
1458 if(diFlags
& DI_IMAGE
)
1462 DWORD rop4
= (diFlags
& DI_MASK
) ? ROP4_SRCINVERT
: ROP4_SRCCOPY
;
1464 EXLATEOBJ_vInitialize(&exlo
, psurfColor
->ppal
, psurfDest
->ppal
, 0x00FFFFFF, 0x00FFFFFF, 0);
1466 Ret
= IntEngStretchBlt(&psurfDest
->SurfObj
,
1467 &psurfColor
->SurfObj
,
1479 EXLATEOBJ_vCleanup(&exlo
);
1483 ERR("Failed to render the icon bitmap.\n");
1489 /* Mask bitmap holds the information in its bottom half */
1490 DWORD rop4
= (diFlags
& DI_MASK
) ? ROP4_SRCINVERT
: ROP4_SRCCOPY
;
1491 RECTL_vOffsetRect(&rcSrc
, 0, pIcon
->Size
.cy
);
1493 EXLATEOBJ_vInitSrcMonoXlate(&exlo
, psurfDest
->ppal
, 0x00FFFFFF, 0);
1495 Ret
= IntEngStretchBlt(&psurfDest
->SurfObj
,
1496 &psurfMask
->SurfObj
,
1508 EXLATEOBJ_vCleanup(&exlo
);
1512 ERR("Failed to render the icon bitmap.\n");
1520 /* We're done. Was it a double buffered draw ? */
1523 /* Yes. Draw it back to our DC */
1524 POINTL ptSrc
= {0, 0};
1526 /* Calculate destination rectangle */
1527 RECTL_vSetRect(&rcDest
, xLeft
, yTop
, xLeft
+ cxWidth
, yTop
+ cyHeight
);
1528 IntLPtoDP(pdc
, (LPPOINT
)&rcDest
, 2);
1529 RECTL_vOffsetRect(&rcDest
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1531 /* Get the clip object */
1532 pdcClipObj
= pdc
->rosdc
.CombinedClip
;
1534 /* We now have our destination surface and rectangle */
1535 psurfDest
= pdc
->dclevel
.pSurface
;
1537 /* Color translation */
1538 EXLATEOBJ_vInitialize(&exlo
, psurfOffScreen
->ppal
, psurfDest
->ppal
, 0x00FFFFFF, 0x00FFFFFF, 0);
1541 Ret
= IntEngBitBlt(&psurfDest
->SurfObj
,
1542 &psurfOffScreen
->SurfObj
,
1553 EXLATEOBJ_vCleanup(&exlo
);
1559 DC_vFinishBlit(pdc
, NULL
);
1564 /* Delete off screen rendering surface */
1566 GDIOBJ_vDeleteObject(&psurfOffScreen
->BaseObject
);
1569 /* Unlock other surfaces */
1570 SURFACE_ShareUnlockSurface(psurfMask
);
1571 if(psurfColor
) SURFACE_ShareUnlockSurface(psurfColor
);
1589 HBRUSH hbrFlickerFreeDraw
,
1591 BOOL bMetaHDC
, // When TRUE, GDI functions need to be handled in User32!
1594 PCURICON_OBJECT pIcon
;
1597 TRACE("Enter NtUserDrawIconEx\n");
1598 UserEnterExclusive();
1600 if (!(pIcon
= UserGetCurIconObject(hIcon
)))
1602 ERR("UserGetCurIconObject(0x%08x) failed!\n", hIcon
);
1607 Ret
= UserDrawIconEx(hdc
,
1617 UserDereferenceObject(pIcon
);