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
, TYPE_CURSOR
);
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
;
172 PCURICON_OBJECT FASTCALL
173 IntFindExistingCurIconObject(HMODULE hModule
,
174 HRSRC hRsrc
, LONG cx
, LONG cy
)
176 PCURICON_OBJECT CurIcon
;
178 LIST_FOR_EACH(CurIcon
, &gCurIconList
, CURICON_OBJECT
, ListEntry
)
181 // if (NT_SUCCESS(UserReferenceObjectByPointer(Object, TYPE_CURSOR))) // <- huh????
182 // UserReferenceObject( CurIcon);
184 if ((CurIcon
->hModule
== hModule
) && (CurIcon
->hRsrc
== hRsrc
))
186 if (cx
&& ((cx
!= CurIcon
->Size
.cx
) || (cy
!= CurIcon
->Size
.cy
)))
188 // UserDereferenceObject(CurIcon);
191 if (! ReferenceCurIconByProcess(CurIcon
))
199 // UserDereferenceObject(CurIcon);
207 IntCreateCurIconHandle(DWORD dwNumber
)
209 PCURICON_OBJECT CurIcon
;
212 CurIcon
= UserCreateObject(gHandleTable
, NULL
, NULL
, &hCurIcon
, TYPE_CURSOR
, sizeof(CURICON_OBJECT
));
216 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
220 CurIcon
->Self
= hCurIcon
;
221 InitializeListHead(&CurIcon
->ProcessList
);
223 if (! ReferenceCurIconByProcess(CurIcon
))
225 ERR("Failed to add process\n");
226 UserDeleteObject(hCurIcon
, TYPE_CURSOR
);
227 UserDereferenceObject(CurIcon
);
231 InsertHeadList(&gCurIconList
, &CurIcon
->ListEntry
);
237 IntDestroyCurIconObject(PCURICON_OBJECT CurIcon
, PPROCESSINFO ppi
)
239 PSYSTEM_CURSORINFO CurInfo
;
240 HBITMAP bmpMask
, bmpColor
;
241 BOOLEAN Ret
, bListEmpty
, bFound
= FALSE
;
242 PCURICON_PROCESS Current
= NULL
;
244 /* For handles created without any data (error handling) */
245 if(IsListEmpty(&CurIcon
->ProcessList
))
248 /* Now find this process in the list of processes referencing this object and
249 remove it from that list */
250 LIST_FOR_EACH(Current
, &CurIcon
->ProcessList
, CURICON_PROCESS
, ListEntry
)
252 if (Current
->Process
== ppi
)
255 bListEmpty
= RemoveEntryList(&Current
->ListEntry
);
262 /* This object doesn't belong to this process */
263 EngSetLastError(ERROR_INVALID_HANDLE
);
267 ExFreeToPagedLookasideList(pgProcessLookasideList
, Current
);
269 /* If there are still processes referencing this object we can't destroy it yet */
272 if(CurIcon
->head
.ppi
== ppi
)
274 /* Set the first process of the list as owner */
275 Current
= CONTAINING_RECORD(CurIcon
->ProcessList
.Flink
, CURICON_PROCESS
, ListEntry
);
276 UserSetObjectOwner(CurIcon
, TYPE_CURSOR
, Current
->Process
);
278 UserDereferenceObject(CurIcon
);
283 /* Remove it from the list */
284 RemoveEntryList(&CurIcon
->ListEntry
);
286 CurInfo
= IntGetSysCursorInfo();
288 if (CurInfo
->CurrentCursorObject
== CurIcon
)
290 /* Hide the cursor if we're destroying the current cursor */
291 UserSetCursor(NULL
, TRUE
);
294 bmpMask
= CurIcon
->IconInfo
.hbmMask
;
295 bmpColor
= CurIcon
->IconInfo
.hbmColor
;
300 GreSetObjectOwner(bmpMask
, GDI_OBJ_HMGR_POWNED
);
301 GreDeleteObject(bmpMask
);
302 CurIcon
->IconInfo
.hbmMask
= NULL
;
306 GreSetObjectOwner(bmpColor
, GDI_OBJ_HMGR_POWNED
);
307 GreDeleteObject(bmpColor
);
308 CurIcon
->IconInfo
.hbmColor
= NULL
;
311 /* We were given a pointer, no need to keep the reference anylonger! */
312 UserDereferenceObject(CurIcon
);
313 Ret
= UserDeleteObject(CurIcon
->Self
, TYPE_CURSOR
);
319 IntCleanupCurIcons(struct _EPROCESS
*Process
, PPROCESSINFO Win32Process
)
321 PCURICON_OBJECT CurIcon
, tmp
;
323 /* Run through the list of icon objects */
324 LIST_FOR_EACH_SAFE(CurIcon
, tmp
, &gCurIconList
, CURICON_OBJECT
, ListEntry
)
326 UserReferenceObject(CurIcon
);
327 IntDestroyCurIconObject(CurIcon
, Win32Process
);
340 PUNICODE_STRING lpInstName
, // Optional
341 PUNICODE_STRING lpResName
, // Optional
342 LPDWORD pbpp
, // Optional
346 PCURICON_OBJECT CurIcon
;
347 NTSTATUS Status
= STATUS_SUCCESS
;
351 TRACE("Enter NtUserGetIconInfo\n");
352 UserEnterExclusive();
356 EngSetLastError(ERROR_INVALID_PARAMETER
);
360 if (!(CurIcon
= UserGetCurIconObject(hCurIcon
)))
365 RtlCopyMemory(&ii
, &CurIcon
->IconInfo
, sizeof(ICONINFO
));
368 ii
.hbmMask
= BITMAP_CopyBitmap(CurIcon
->IconInfo
.hbmMask
);
369 ii
.hbmColor
= BITMAP_CopyBitmap(CurIcon
->IconInfo
.hbmColor
);
375 psurfBmp
= SURFACE_ShareLockSurface(CurIcon
->IconInfo
.hbmColor
);
378 colorBpp
= BitsPerFormat(psurfBmp
->SurfObj
.iBitmapFormat
);
379 SURFACE_ShareUnlockSurface(psurfBmp
);
386 ProbeForWrite(IconInfo
, sizeof(ICONINFO
), 1);
387 RtlCopyMemory(IconInfo
, &ii
, sizeof(ICONINFO
));
389 /// @todo Implement support for lpInstName
392 RtlInitEmptyUnicodeString(lpInstName
, NULL
, 0);
395 /// @todo Implement support for lpResName
398 RtlInitEmptyUnicodeString(lpResName
, NULL
, 0);
403 ProbeForWrite(pbpp
, sizeof(DWORD
), 1);
407 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
409 Status
= _SEH2_GetExceptionCode();
413 if (NT_SUCCESS(Status
))
416 SetLastNtError(Status
);
418 UserDereferenceObject(CurIcon
);
421 TRACE("Leave NtUserGetIconInfo, ret=%i\n", Ret
);
436 PLONG plcx
, // &size.cx
437 PLONG plcy
) // &size.cy
439 PCURICON_OBJECT CurIcon
;
440 NTSTATUS Status
= STATUS_SUCCESS
;
443 TRACE("Enter NtUserGetIconSize\n");
444 UserEnterExclusive();
446 if (!(CurIcon
= UserGetCurIconObject(hCurIcon
)))
453 ProbeForWrite(plcx
, sizeof(LONG
), 1);
454 RtlCopyMemory(plcx
, &CurIcon
->Size
.cx
, sizeof(LONG
));
455 ProbeForWrite(plcy
, sizeof(LONG
), 1);
456 RtlCopyMemory(plcy
, &CurIcon
->Size
.cy
, sizeof(LONG
));
458 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
460 Status
= _SEH2_GetExceptionCode();
464 if (NT_SUCCESS(Status
))
467 SetLastNtError(Status
); // Maybe not, test this
469 UserDereferenceObject(CurIcon
);
472 TRACE("Leave NtUserGetIconSize, ret=%i\n", bRet
);
487 PSYSTEM_CURSORINFO CurInfo
;
488 NTSTATUS Status
= STATUS_SUCCESS
;
489 PCURICON_OBJECT CurIcon
;
491 DECLARE_RETURN(BOOL
);
493 TRACE("Enter NtUserGetCursorInfo\n");
494 UserEnterExclusive();
496 CurInfo
= IntGetSysCursorInfo();
497 CurIcon
= (PCURICON_OBJECT
)CurInfo
->CurrentCursorObject
;
499 SafeCi
.cbSize
= sizeof(CURSORINFO
);
500 SafeCi
.flags
= ((CurIcon
&& CurInfo
->ShowingCursor
>= 0) ? CURSOR_SHOWING
: 0);
501 SafeCi
.hCursor
= (CurIcon
? (HCURSOR
)CurIcon
->Self
: (HCURSOR
)0);
503 SafeCi
.ptScreenPos
= gpsi
->ptCursor
;
507 if (pci
->cbSize
== sizeof(CURSORINFO
))
509 ProbeForWrite(pci
, sizeof(CURSORINFO
), 1);
510 RtlCopyMemory(pci
, &SafeCi
, sizeof(CURSORINFO
));
515 EngSetLastError(ERROR_INVALID_PARAMETER
);
518 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
520 Status
= _SEH2_GetExceptionCode();
523 if (!NT_SUCCESS(Status
))
525 SetLastNtError(Status
);
531 TRACE("Leave NtUserGetCursorInfo, ret=%i\n",_ret_
);
541 /* FIXME: Check if process has WINSTA_WRITEATTRIBUTES */
542 PSYSTEM_CURSORINFO CurInfo
;
543 PWND DesktopWindow
= NULL
;
545 CurInfo
= IntGetSysCursorInfo();
547 DesktopWindow
= UserGetDesktopWindow();
549 if (prcl
!= NULL
&& DesktopWindow
!= NULL
)
551 if (prcl
->right
< prcl
->left
|| prcl
->bottom
< prcl
->top
)
553 EngSetLastError(ERROR_INVALID_PARAMETER
);
557 CurInfo
->bClipped
= TRUE
;
559 /* Set nw cliping region. Note: we can't use RECTL_bIntersectRect because
560 it sets rect to 0 0 0 0 when it's empty. For more info see monitor winetest */
561 CurInfo
->rcClip
.left
= max(prcl
->left
, DesktopWindow
->rcWindow
.left
);
562 CurInfo
->rcClip
.right
= min(prcl
->right
, DesktopWindow
->rcWindow
.right
);
563 if (CurInfo
->rcClip
.right
< CurInfo
->rcClip
.left
)
564 CurInfo
->rcClip
.right
= CurInfo
->rcClip
.left
;
566 CurInfo
->rcClip
.top
= max(prcl
->top
, DesktopWindow
->rcWindow
.top
);
567 CurInfo
->rcClip
.bottom
= min(prcl
->bottom
, DesktopWindow
->rcWindow
.bottom
);
568 if (CurInfo
->rcClip
.bottom
< CurInfo
->rcClip
.top
)
569 CurInfo
->rcClip
.bottom
= CurInfo
->rcClip
.top
;
571 /* Make sure cursor is in clipping region */
572 UserSetCursorPos(gpsi
->ptCursor
.x
, gpsi
->ptCursor
.y
, 0, 0, FALSE
);
576 CurInfo
->bClipped
= FALSE
;
597 /* Probe and copy rect */
598 ProbeForRead(prcl
, sizeof(RECTL
), 1);
601 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
603 EngSetLastError(ERROR_INVALID_PARAMETER
);
604 _SEH2_YIELD(return FALSE
;)
611 UserEnterExclusive();
613 /* Call the internal function */
614 bResult
= UserClipCursor(prcl
);
628 _In_ HANDLE hCurIcon
,
631 PCURICON_OBJECT CurIcon
;
633 DECLARE_RETURN(BOOL
);
635 TRACE("Enter NtUserDestroyCursorIcon\n");
636 UserEnterExclusive();
638 if (!(CurIcon
= UserGetCurIconObject(hCurIcon
)))
643 ret
= IntDestroyCurIconObject(CurIcon
, PsGetCurrentProcessWin32Process());
644 /* Note: IntDestroyCurIconObject will remove our reference for us! */
649 TRACE("Leave NtUserDestroyCursorIcon, ret=%i\n",_ret_
);
660 NtUserFindExistingCursorIcon(
666 PCURICON_OBJECT CurIcon
;
667 HANDLE Ret
= (HANDLE
)0;
668 DECLARE_RETURN(HICON
);
670 TRACE("Enter NtUserFindExistingCursorIcon\n");
671 UserEnterExclusive();
673 CurIcon
= IntFindExistingCurIconObject(hModule
, hRsrc
, cx
, cy
);
678 // IntReleaseCurIconObject(CurIcon); // FIXME: Is this correct? Does IntFindExistingCurIconObject add a ref?
682 EngSetLastError(ERROR_INVALID_CURSOR_HANDLE
);
686 TRACE("Leave NtUserFindExistingCursorIcon, ret=%p\n",_ret_
);
700 /* FIXME: Check if process has WINSTA_READATTRIBUTES */
701 PSYSTEM_CURSORINFO CurInfo
;
704 DECLARE_RETURN(BOOL
);
706 TRACE("Enter NtUserGetClipCursor\n");
707 UserEnterExclusive();
712 CurInfo
= IntGetSysCursorInfo();
713 if (CurInfo
->bClipped
)
715 Rect
= CurInfo
->rcClip
;
721 Rect
.right
= UserGetSystemMetrics(SM_CXSCREEN
);
722 Rect
.bottom
= UserGetSystemMetrics(SM_CYSCREEN
);
725 Status
= MmCopyToCaller(lpRect
, &Rect
, sizeof(RECT
));
726 if (!NT_SUCCESS(Status
))
728 SetLastNtError(Status
);
735 TRACE("Leave NtUserGetClipCursor, ret=%i\n",_ret_
);
749 PCURICON_OBJECT pcurOld
, pcurNew
;
750 HCURSOR hOldCursor
= NULL
;
752 TRACE("Enter NtUserSetCursor\n");
753 UserEnterExclusive();
757 pcurNew
= UserGetCurIconObject(hCursor
);
760 EngSetLastError(ERROR_INVALID_CURSOR_HANDLE
);
769 pcurOld
= UserSetCursor(pcurNew
, FALSE
);
772 hOldCursor
= (HCURSOR
)pcurOld
->Self
;
773 UserDereferenceObject(pcurOld
);
787 NtUserSetCursorContents(
789 PICONINFO UnsafeIconInfo
)
791 PCURICON_OBJECT CurIcon
;
796 DECLARE_RETURN(BOOL
);
798 TRACE("Enter NtUserSetCursorContents\n");
799 UserEnterExclusive();
801 if (!(CurIcon
= UserGetCurIconObject(hCurIcon
)))
807 Status
= MmCopyFromCaller(&IconInfo
, UnsafeIconInfo
, sizeof(ICONINFO
));
808 if (!NT_SUCCESS(Status
))
810 SetLastNtError(Status
);
814 /* Delete old bitmaps */
815 if ((CurIcon
->IconInfo
.hbmColor
)
816 && (CurIcon
->IconInfo
.hbmColor
!= IconInfo
.hbmColor
))
818 GreDeleteObject(CurIcon
->IconInfo
.hbmColor
);
820 if ((CurIcon
->IconInfo
.hbmMask
)
821 && CurIcon
->IconInfo
.hbmMask
!= IconInfo
.hbmMask
)
823 GreDeleteObject(CurIcon
->IconInfo
.hbmMask
);
826 /* Copy new IconInfo field */
827 CurIcon
->IconInfo
= IconInfo
;
829 if (CurIcon
->IconInfo
.hbmColor
)
831 psurfBmp
= SURFACE_ShareLockSurface(CurIcon
->IconInfo
.hbmColor
);
835 CurIcon
->Size
.cx
= psurfBmp
->SurfObj
.sizlBitmap
.cx
;
836 CurIcon
->Size
.cy
= psurfBmp
->SurfObj
.sizlBitmap
.cy
;
837 SURFACE_ShareUnlockSurface(psurfBmp
);
838 GreSetObjectOwner(CurIcon
->IconInfo
.hbmColor
, GDI_OBJ_HMGR_PUBLIC
);
842 psurfBmp
= SURFACE_ShareLockSurface(CurIcon
->IconInfo
.hbmMask
);
846 CurIcon
->Size
.cx
= psurfBmp
->SurfObj
.sizlBitmap
.cx
;
847 CurIcon
->Size
.cy
= psurfBmp
->SurfObj
.sizlBitmap
.cy
/ 2;
849 SURFACE_ShareUnlockSurface(psurfBmp
);
851 GreSetObjectOwner(CurIcon
->IconInfo
.hbmMask
, GDI_OBJ_HMGR_PUBLIC
);
859 UserDereferenceObject(CurIcon
);
864 TRACE("Leave NtUserSetCursorContents, ret=%i\n",_ret_
);
876 NtUserSetCursorIconData(
879 PUNICODE_STRING pstrResName
,
882 PCURICON_OBJECT CurIcon
;
884 NTSTATUS Status
= STATUS_SUCCESS
;
886 DECLARE_RETURN(BOOL
);
888 TRACE("Enter NtUserSetCursorIconData\n");
889 UserEnterExclusive();
891 if (!(CurIcon
= UserGetCurIconObject(Handle
)))
896 CurIcon
->hModule
= hModule
;
897 CurIcon
->hRsrc
= NULL
; //hRsrc;
898 CurIcon
->hGroupRsrc
= NULL
; //hGroupRsrc;
902 ProbeForRead(pIconInfo
, sizeof(ICONINFO
), 1);
903 RtlCopyMemory(&CurIcon
->IconInfo
, pIconInfo
, sizeof(ICONINFO
));
905 CurIcon
->IconInfo
.hbmMask
= BITMAP_CopyBitmap(pIconInfo
->hbmMask
);
906 CurIcon
->IconInfo
.hbmColor
= BITMAP_CopyBitmap(pIconInfo
->hbmColor
);
908 if (CurIcon
->IconInfo
.hbmColor
)
910 if ((psurfBmp
= SURFACE_LockSurface(CurIcon
->IconInfo
.hbmColor
)))
912 CurIcon
->Size
.cx
= psurfBmp
->SurfObj
.sizlBitmap
.cx
;
913 CurIcon
->Size
.cy
= psurfBmp
->SurfObj
.sizlBitmap
.cy
;
914 SURFACE_UnlockSurface(psurfBmp
);
915 GreSetObjectOwner(CurIcon
->IconInfo
.hbmMask
, GDI_OBJ_HMGR_PUBLIC
);
918 if (CurIcon
->IconInfo
.hbmMask
)
920 if (CurIcon
->IconInfo
.hbmColor
== NULL
)
922 if ((psurfBmp
= SURFACE_LockSurface(CurIcon
->IconInfo
.hbmMask
)))
924 CurIcon
->Size
.cx
= psurfBmp
->SurfObj
.sizlBitmap
.cx
;
925 CurIcon
->Size
.cy
= psurfBmp
->SurfObj
.sizlBitmap
.cy
;
926 SURFACE_UnlockSurface(psurfBmp
);
929 GreSetObjectOwner(CurIcon
->IconInfo
.hbmMask
, GDI_OBJ_HMGR_PUBLIC
);
932 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
934 Status
= _SEH2_GetExceptionCode();
938 if (!NT_SUCCESS(Status
))
939 SetLastNtError(Status
);
943 UserDereferenceObject(CurIcon
);
947 TRACE("Leave NtUserSetCursorIconData, ret=%i\n",_ret_
);
954 NtUserSetCursorIconData(
962 PCURICON_OBJECT CurIcon
;
966 DECLARE_RETURN(BOOL
);
968 TRACE("Enter NtUserSetCursorIconData\n");
969 UserEnterExclusive();
971 if (!(CurIcon
= UserGetCurIconObject(hCurIcon
)))
976 CurIcon
->hModule
= hModule
;
977 CurIcon
->hRsrc
= hRsrc
;
978 CurIcon
->hGroupRsrc
= hGroupRsrc
;
983 Status
= MmCopyFromCaller(&CurIcon
->IconInfo
.fIcon
, fIcon
, sizeof(BOOL
));
984 if (!NT_SUCCESS(Status
))
986 SetLastNtError(Status
);
998 Status
= MmCopyFromCaller(&SafeHotspot
, Hotspot
, sizeof(POINT
));
999 if (NT_SUCCESS(Status
))
1001 CurIcon
->IconInfo
.xHotspot
= SafeHotspot
.x
;
1002 CurIcon
->IconInfo
.yHotspot
= SafeHotspot
.y
;
1007 SetLastNtError(Status
);
1010 if (!fIcon
&& !Hotspot
)
1018 /* This icon is shared now */
1019 GreSetObjectOwner(CurIcon
->IconInfo
.hbmMask
, GDI_OBJ_HMGR_PUBLIC
);
1020 if(CurIcon
->IconInfo
.hbmColor
)
1022 GreSetObjectOwner(CurIcon
->IconInfo
.hbmColor
, GDI_OBJ_HMGR_PUBLIC
);
1025 UserDereferenceObject(CurIcon
);
1030 TRACE("Leave NtUserSetCursorIconData, ret=%i\n",_ret_
);
1036 /* Mostly inspired from wine code.
1037 * We use low level functions because:
1038 * - at this point, the icon bitmap could have a different bit depth than the DC,
1039 * making it thus impossible to use NtCreateCompatibleDC and selecting the bitmap.
1040 * This happens after a mode setting change.
1041 * - it avoids massive GDI objects locking when only the destination surface needs it.
1042 * - It makes (small) performance gains.
1049 PCURICON_OBJECT pIcon
,
1053 HBRUSH hbrFlickerFreeDraw
,
1056 PSURFACE psurfDest
, psurfMask
, psurfColor
, psurfOffScreen
;
1059 HBITMAP hbmMask
, hbmColor
;
1060 BOOL bOffScreen
, bAlpha
= FALSE
;
1061 RECTL rcDest
, rcSrc
;
1062 CLIPOBJ
* pdcClipObj
= NULL
;
1066 if((diFlags
& DI_NORMAL
) == 0)
1068 ERR("DrawIconEx called without mask or color bitmap to draw.\n");
1072 hbmMask
= pIcon
->IconInfo
.hbmMask
;
1073 hbmColor
= pIcon
->IconInfo
.hbmColor
;
1076 ERR("NtUserDrawIconEx: istepIfAniCur is not supported!\n");
1080 * Shared locks are enough, we are only reading those bitmaps
1082 psurfMask
= SURFACE_ShareLockSurface(hbmMask
);
1083 if(psurfMask
== NULL
)
1085 ERR("Unable to lock the mask surface.\n");
1089 /* Color bitmap is not mandatory */
1090 if(hbmColor
== NULL
)
1092 /* But then the mask bitmap must have the information in it's bottom half */
1093 ASSERT(psurfMask
->SurfObj
.sizlBitmap
.cy
== 2*pIcon
->Size
.cy
);
1096 else if ((psurfColor
= SURFACE_ShareLockSurface(hbmColor
)) == NULL
)
1098 ERR("Unable to lock the color bitmap.\n");
1099 SURFACE_ShareUnlockSurface(psurfMask
);
1103 /* Set source rect */
1104 RECTL_vSetRect(&rcSrc
, 0, 0, pIcon
->Size
.cx
, pIcon
->Size
.cy
);
1106 /* Check for alpha */
1108 (psurfColor
->SurfObj
.iBitmapFormat
== BMF_32BPP
) &&
1109 (diFlags
& DI_IMAGE
))
1111 PFN_DIB_GetPixel fnSource_GetPixel
= NULL
;
1114 /* In order to correctly display 32 bit icons Windows first scans the image,
1115 because information about transparency is not stored in any image's headers */
1116 fnSource_GetPixel
= DibFunctionsForBitmapFormat
[BMF_32BPP
].DIB_GetPixel
;
1117 for (i
= 0; i
< psurfColor
->SurfObj
.sizlBitmap
.cx
; i
++)
1119 for (j
= 0; j
< psurfColor
->SurfObj
.sizlBitmap
.cy
; j
++)
1121 bAlpha
= ((BYTE
)(fnSource_GetPixel(&psurfColor
->SurfObj
, i
, j
) >> 24) & 0xff);
1130 /* Fix width parameter, if needed */
1133 if(diFlags
& DI_DEFAULTSIZE
)
1134 cxWidth
= pIcon
->IconInfo
.fIcon
?
1135 UserGetSystemMetrics(SM_CXICON
) : UserGetSystemMetrics(SM_CXCURSOR
);
1137 cxWidth
= pIcon
->Size
.cx
;
1140 /* Fix height parameter, if needed */
1143 if(diFlags
& DI_DEFAULTSIZE
)
1144 cyHeight
= pIcon
->IconInfo
.fIcon
?
1145 UserGetSystemMetrics(SM_CYICON
) : UserGetSystemMetrics(SM_CYCURSOR
);
1147 cyHeight
= pIcon
->Size
.cy
;
1150 /* Should we render off-screen? */
1151 bOffScreen
= hbrFlickerFreeDraw
&& (GDI_HANDLE_GET_TYPE(hbrFlickerFreeDraw
) == GDI_OBJECT_TYPE_BRUSH
);
1155 /* Yes: Allocate and paint the offscreen surface */
1157 PBRUSH pbrush
= BRUSH_ShareLockBrush(hbrFlickerFreeDraw
);
1159 TRACE("Performing off-screen rendering.\n");
1163 ERR("Failed to get brush object.\n");
1164 SURFACE_ShareUnlockSurface(psurfMask
);
1165 if(psurfColor
) SURFACE_ShareUnlockSurface(psurfColor
);
1171 ERR("Attention HAX FIX API DrawIconEx TEST Line 37: psurfColor is NULL going with Mask!\n");
1172 psurfColor
= SURFACE_ShareLockSurface(hbmMask
);
1175 psurfOffScreen
= SURFACE_AllocSurface(STYPE_BITMAP
,
1176 cxWidth
, cyHeight
, psurfColor
->SurfObj
.iBitmapFormat
,
1180 ERR("Failed to allocate the off-screen surface.\n");
1181 SURFACE_ShareUnlockSurface(psurfMask
);
1182 if(psurfColor
) SURFACE_ShareUnlockSurface(psurfColor
);
1183 BRUSH_ShareUnlockBrush(pbrush
);
1187 /* Paint the brush */
1188 EBRUSHOBJ_vInit(&eboFill
, pbrush
, psurfOffScreen
, 0x00FFFFFF, 0, NULL
);
1189 RECTL_vSetRect(&rcDest
, 0, 0, cxWidth
, cyHeight
);
1191 Ret
= IntEngBitBlt(&psurfOffScreen
->SurfObj
,
1199 &eboFill
.BrushObject
,
1203 /* Clean up everything */
1204 EBRUSHOBJ_vCleanup(&eboFill
);
1205 BRUSH_ShareUnlockBrush(pbrush
);
1209 ERR("Failed to paint the off-screen surface.\n");
1210 SURFACE_ShareUnlockSurface(psurfMask
);
1211 if(psurfColor
) SURFACE_ShareUnlockSurface(psurfColor
);
1212 GDIOBJ_vDeleteObject(&psurfOffScreen
->BaseObject
);
1216 /* We now have our destination surface */
1217 psurfDest
= psurfOffScreen
;
1221 /* We directly draw to the DC */
1222 TRACE("Performing on screen rendering.\n");
1224 psurfOffScreen
= NULL
;
1225 pdc
= DC_LockDc(hDc
);
1228 ERR("Could not lock the destination DC.\n");
1229 SURFACE_ShareUnlockSurface(psurfMask
);
1230 if(psurfColor
) SURFACE_ShareUnlockSurface(psurfColor
);
1233 /* Calculate destination rectangle */
1234 RECTL_vSetRect(&rcDest
, xLeft
, yTop
, xLeft
+ cxWidth
, yTop
+ cyHeight
);
1235 IntLPtoDP(pdc
, (LPPOINT
)&rcDest
, 2);
1236 RECTL_vOffsetRect(&rcDest
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1238 /* Prepare the underlying surface */
1239 DC_vPrepareDCsForBlit(pdc
, rcDest
, NULL
, rcDest
);
1241 /* Get the clip object */
1242 pdcClipObj
= pdc
->rosdc
.CombinedClip
;
1244 /* We now have our destination surface and rectangle */
1245 psurfDest
= pdc
->dclevel
.pSurface
;
1247 if(psurfDest
== NULL
)
1250 DC_vFinishBlit(pdc
, NULL
);
1252 SURFACE_ShareUnlockSurface(psurfMask
);
1253 if(psurfColor
) SURFACE_ShareUnlockSurface(psurfColor
);
1258 /* Now do the rendering */
1259 if(bAlpha
&& (diFlags
& DI_IMAGE
))
1261 BLENDOBJ blendobj
= { {AC_SRC_OVER
, 0, 255, AC_SRC_ALPHA
} };
1264 PSURFACE psurf
= NULL
;
1266 HBITMAP hsurfCopy
= NULL
;
1268 hsurfCopy
= BITMAP_CopyBitmap(hbmColor
);
1271 ERR("BITMAP_CopyBitmap failed!");
1275 psurf
= SURFACE_ShareLockSurface(hsurfCopy
);
1278 ERR("SURFACE_LockSurface failed!\n");
1282 /* Premultiply with the alpha channel value */
1283 for (i
= 0; i
< psurf
->SurfObj
.sizlBitmap
.cy
; i
++)
1285 ptr
= (PBYTE
)psurf
->SurfObj
.pvScan0
+ i
*psurf
->SurfObj
.lDelta
;
1286 for (j
= 0; j
< psurf
->SurfObj
.sizlBitmap
.cx
; j
++)
1289 ptr
[0] = (ptr
[0] * Alpha
) / 0xff;
1290 ptr
[1] = (ptr
[1] * Alpha
) / 0xff;
1291 ptr
[2] = (ptr
[2] * Alpha
) / 0xff;
1297 /* Initialize color translation object */
1298 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xFFFFFFFF, 0xFFFFFFFF, 0);
1301 Ret
= IntEngAlphaBlend(&psurfDest
->SurfObj
,
1309 EXLATEOBJ_vCleanup(&exlo
);
1312 if(psurf
) SURFACE_ShareUnlockSurface(psurf
);
1313 if(hsurfCopy
) NtGdiDeleteObjectApp(hsurfCopy
);
1315 ERR("NtGdiAlphaBlend failed!\n");
1318 if (diFlags
& DI_MASK
)
1320 DWORD rop4
= (diFlags
& DI_IMAGE
) ? ROP4_SRCAND
: ROP4_SRCCOPY
;
1322 EXLATEOBJ_vInitSrcMonoXlate(&exlo
, psurfDest
->ppal
, 0x00FFFFFF, 0);
1324 Ret
= IntEngStretchBlt(&psurfDest
->SurfObj
,
1325 &psurfMask
->SurfObj
,
1337 EXLATEOBJ_vCleanup(&exlo
);
1341 ERR("Failed to mask the bitmap data.\n");
1346 if(diFlags
& DI_IMAGE
)
1350 DWORD rop4
= (diFlags
& DI_MASK
) ? ROP4_SRCINVERT
: ROP4_SRCCOPY
;
1352 EXLATEOBJ_vInitialize(&exlo
, psurfColor
->ppal
, psurfDest
->ppal
, 0x00FFFFFF, 0x00FFFFFF, 0);
1354 Ret
= IntEngStretchBlt(&psurfDest
->SurfObj
,
1355 &psurfColor
->SurfObj
,
1367 EXLATEOBJ_vCleanup(&exlo
);
1371 ERR("Failed to render the icon bitmap.\n");
1377 /* Mask bitmap holds the information in its bottom half */
1378 DWORD rop4
= (diFlags
& DI_MASK
) ? ROP4_SRCINVERT
: ROP4_SRCCOPY
;
1379 RECTL_vOffsetRect(&rcSrc
, 0, pIcon
->Size
.cy
);
1381 EXLATEOBJ_vInitSrcMonoXlate(&exlo
, psurfDest
->ppal
, 0x00FFFFFF, 0);
1383 Ret
= IntEngStretchBlt(&psurfDest
->SurfObj
,
1384 &psurfMask
->SurfObj
,
1396 EXLATEOBJ_vCleanup(&exlo
);
1400 ERR("Failed to render the icon bitmap.\n");
1407 /* We're done. Was it a double buffered draw ? */
1410 /* Yes. Draw it back to our DC */
1411 POINTL ptSrc
= {0, 0};
1412 pdc
= DC_LockDc(hDc
);
1415 ERR("Could not lock the destination DC.\n");
1418 /* Calculate destination rectangle */
1419 RECTL_vSetRect(&rcDest
, xLeft
, yTop
, xLeft
+ cxWidth
, yTop
+ cyHeight
);
1420 IntLPtoDP(pdc
, (LPPOINT
)&rcDest
, 2);
1421 RECTL_vOffsetRect(&rcDest
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1423 /* Prepare the underlying surface */
1424 DC_vPrepareDCsForBlit(pdc
, rcDest
, NULL
, rcDest
);
1426 /* Get the clip object */
1427 pdcClipObj
= pdc
->rosdc
.CombinedClip
;
1429 /* We now have our destination surface and rectangle */
1430 psurfDest
= pdc
->dclevel
.pSurface
;
1433 /* So, you did all of this for an empty DC. */
1438 /* Color translation */
1439 EXLATEOBJ_vInitialize(&exlo
, psurfOffScreen
->ppal
, psurfDest
->ppal
, 0x00FFFFFF, 0x00FFFFFF, 0);
1442 Ret
= IntEngBitBlt(&psurfDest
->SurfObj
,
1443 &psurfOffScreen
->SurfObj
,
1454 EXLATEOBJ_vCleanup(&exlo
);
1459 DC_vFinishBlit(pdc
, NULL
);
1464 /* Delete off screen rendering surface */
1466 GDIOBJ_vDeleteObject(&psurfOffScreen
->BaseObject
);
1468 /* Unlock other surfaces */
1469 SURFACE_ShareUnlockSurface(psurfMask
);
1470 if(psurfColor
) SURFACE_ShareUnlockSurface(psurfColor
);
1488 HBRUSH hbrFlickerFreeDraw
,
1490 BOOL bMetaHDC
, // When TRUE, GDI functions need to be handled in User32!
1493 PCURICON_OBJECT pIcon
;
1496 TRACE("Enter NtUserDrawIconEx\n");
1497 UserEnterExclusive();
1499 if (!(pIcon
= UserGetCurIconObject(hIcon
)))
1501 ERR("UserGetCurIconObject() failed!\n");
1506 Ret
= UserDrawIconEx(hdc
,
1516 UserDereferenceObject(pIcon
);