2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Window accelerator
5 * FILE: subsystems/win32/win32k/ntuser/accelerator.c
6 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Copyright 1993 Martin Ayotte
8 * Copyright 1994 Alexandre Julliard
9 * Copyright 1997 Morten Welinder
10 * Copyright 2011 Rafal Harabien
14 DBG_DEFAULT_CHANNEL(UserAccel
);
16 #define FVIRT_TBL_END 0x80
17 #define FVIRT_MASK 0x7F
19 /* FUNCTIONS *****************************************************************/
21 PACCELERATOR_TABLE FASTCALL
UserGetAccelObject(HACCEL hAccel
)
23 PACCELERATOR_TABLE Accel
;
27 EngSetLastError(ERROR_INVALID_ACCEL_HANDLE
);
31 Accel
= UserGetObject(gHandleTable
, hAccel
, otAccel
);
34 EngSetLastError(ERROR_INVALID_ACCEL_HANDLE
);
44 co_IntTranslateAccelerator(
52 HMENU hMenu
, hSubMenu
;
53 PMENU_OBJECT MenuObject
, SubMenu
;
56 ASSERT_REFS_CO(Window
);
58 hWnd
= Window
->head
.h
;
60 TRACE("IntTranslateAccelerator(hwnd %x, message %x, wParam %x, lParam %x, fVirt %d, key %x, cmd %x)\n",
61 hWnd
, pMsg
->message
, pMsg
->wParam
, pMsg
->lParam
, pAccel
->fVirt
, pAccel
->key
, pAccel
->cmd
);
63 if (UserGetKeyState(VK_CONTROL
) & 0x8000) Mask
|= FCONTROL
;
64 if (UserGetKeyState(VK_MENU
) & 0x8000) Mask
|= FALT
; // FIXME: VK_LMENU (msg winetest!)
65 if (UserGetKeyState(VK_SHIFT
) & 0x8000) Mask
|= FSHIFT
;
66 TRACE("Mask 0x%x\n", Mask
);
68 if (pAccel
->fVirt
& FVIRTKEY
)
70 /* This is a virtual key. Process WM_(SYS)KEYDOWN messages. */
71 if (pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
73 /* Check virtual key and SHIFT, CTRL, LALT state */
74 if (pMsg
->wParam
== pAccel
->key
&& Mask
== (pAccel
->fVirt
& (FSHIFT
| FCONTROL
| FALT
)))
82 /* This is a char code. Process WM_(SYS)CHAR messages. */
83 if (pMsg
->message
== WM_CHAR
|| pMsg
->message
== WM_SYSCHAR
)
85 /* Check char code and LALT state only */
86 if (pMsg
->wParam
== pAccel
->key
&& (Mask
& FALT
) == (pAccel
->fVirt
& FALT
))
95 /* Don't translate this msg */
96 TRACE("IntTranslateAccelerator returns FALSE\n");
100 /* Check if accelerator is associated with menu command */
101 hMenu
= (Window
->style
& WS_CHILD
) ? 0 : (HMENU
)Window
->IDMenu
;
103 MenuObject
= IntGetMenuObject(hMenu
);
106 nPos
= IntGetMenuItemByFlag(MenuObject
,
112 if (nPos
!= (UINT
) - 1)
113 hSubMenu
= SubMenu
->head
.h
;
119 /* Check system menu now */
120 hMenu
= Window
->SystemMenu
;
121 hSubMenu
= hMenu
; /* system menu is a popup menu */
122 MenuObject
= IntGetMenuObject(hMenu
);
125 nPos
= IntGetMenuItemByFlag(MenuObject
,
131 if (nPos
!= (UINT
) - 1)
132 hSubMenu
= SubMenu
->head
.h
;
138 /* If this is a menu item, there is no capturing enabled and
139 window is not disabled, send WM_INITMENU */
140 if (hMenu
&& !IntGetCaptureWindow())
142 co_IntSendMessage(hWnd
, WM_INITMENU
, (WPARAM
)hMenu
, 0L);
145 nPos
= IntFindSubMenu(&hMenu
, hSubMenu
);
146 TRACE("hSysMenu = %p, hSubMenu = %p, nPos = %d\n", hMenu
, hSubMenu
, nPos
);
147 co_IntSendMessage(hWnd
, WM_INITMENUPOPUP
, (WPARAM
)hSubMenu
, MAKELPARAM(nPos
, TRUE
));
151 /* Don't send any message if:
153 - menu item is disabled
154 - this is window menu and window is minimized */
155 if (!(Window
->style
& WS_DISABLED
) &&
156 !(hMenu
&& IntGetMenuState(hMenu
, pAccel
->cmd
, MF_BYCOMMAND
) & (MF_DISABLED
| MF_GRAYED
)) &&
157 !(hMenu
&& hMenu
== (HMENU
)Window
->IDMenu
&& (Window
->style
& WS_MINIMIZED
)))
159 /* If this is system menu item, send WM_SYSCOMMAND, otherwise send WM_COMMAND */
160 if (hMenu
&& hMenu
== Window
->SystemMenu
)
162 TRACE("Sending WM_SYSCOMMAND, wParam=%0x\n", pAccel
->cmd
);
163 co_IntSendMessage(hWnd
, WM_SYSCOMMAND
, pAccel
->cmd
, 0x00010000L
);
167 TRACE("Sending WM_COMMAND, wParam=%0x\n", 0x10000 | pAccel
->cmd
);
168 co_IntSendMessage(hWnd
, WM_COMMAND
, 0x10000 | pAccel
->cmd
, 0L);
172 TRACE("IntTranslateAccelerator returns TRUE\n");
177 /* SYSCALLS *****************************************************************/
182 NtUserCopyAcceleratorTable(
187 PACCELERATOR_TABLE Accel
;
191 TRACE("Enter NtUserCopyAcceleratorTable\n");
194 Accel
= UserGetAccelObject(hAccel
);
200 /* If Entries is NULL return table size */
203 RETURN(Accel
->Count
);
207 if (Accel
->Count
< EntriesCount
)
208 EntriesCount
= Accel
->Count
;
214 ProbeForWrite(Entries
, EntriesCount
*sizeof(Entries
[0]), 4);
216 for (Ret
= 0; Ret
< EntriesCount
; Ret
++)
218 Entries
[Ret
].fVirt
= Accel
->Table
[Ret
].fVirt
;
219 Entries
[Ret
].key
= Accel
->Table
[Ret
].key
;
220 Entries
[Ret
].cmd
= Accel
->Table
[Ret
].cmd
;
223 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
225 SetLastNtError(_SEH2_GetExceptionCode());
233 TRACE("Leave NtUserCopyAcceleratorTable, ret=%i\n", _ret_
);
240 NtUserCreateAcceleratorTable(
244 PACCELERATOR_TABLE Accel
;
247 NTSTATUS Status
= STATUS_SUCCESS
;
248 DECLARE_RETURN(HACCEL
);
250 TRACE("Enter NtUserCreateAcceleratorTable(Entries %p, EntriesCount %d)\n",
251 Entries
, EntriesCount
);
252 UserEnterExclusive();
254 if (!Entries
|| EntriesCount
<= 0)
256 SetLastNtError(STATUS_INVALID_PARAMETER
);
257 RETURN( (HACCEL
) NULL
);
260 Accel
= UserCreateObject(gHandleTable
, NULL
, (PHANDLE
)&hAccel
, otAccel
, sizeof(ACCELERATOR_TABLE
));
264 SetLastNtError(STATUS_NO_MEMORY
);
265 RETURN( (HACCEL
) NULL
);
268 Accel
->Count
= EntriesCount
;
269 Accel
->Table
= ExAllocatePoolWithTag(PagedPool
, EntriesCount
* sizeof(ACCEL
), USERTAG_ACCEL
);
270 if (Accel
->Table
== NULL
)
272 UserDereferenceObject(Accel
);
273 UserDeleteObject(hAccel
, otAccel
);
274 SetLastNtError(STATUS_NO_MEMORY
);
275 RETURN( (HACCEL
) NULL
);
280 ProbeForRead(Entries
, EntriesCount
* sizeof(ACCEL
), 4);
282 for (Index
= 0; Index
< EntriesCount
; Index
++)
284 Accel
->Table
[Index
].fVirt
= Entries
[Index
].fVirt
& FVIRT_MASK
;
285 if(Accel
->Table
[Index
].fVirt
& FVIRTKEY
)
287 Accel
->Table
[Index
].key
= Entries
[Index
].key
;
291 RtlMultiByteToUnicodeN(&Accel
->Table
[Index
].key
,
294 (PCSTR
)&Entries
[Index
].key
,
298 Accel
->Table
[Index
].cmd
= Entries
[Index
].cmd
;
301 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
303 Status
= _SEH2_GetExceptionCode();
307 if (!NT_SUCCESS(Status
))
309 ExFreePoolWithTag(Accel
->Table
, USERTAG_ACCEL
);
310 UserDereferenceObject(Accel
);
311 UserDeleteObject(hAccel
, otAccel
);
312 SetLastNtError(Status
);
313 RETURN( (HACCEL
) NULL
);
316 /* FIXME: Save HandleTable in a list somewhere so we can clean it up again */
321 TRACE("Leave NtUserCreateAcceleratorTable(Entries %p, EntriesCount %d) = %x\n",
322 Entries
, EntriesCount
, _ret_
);
329 NtUserDestroyAcceleratorTable(
332 PACCELERATOR_TABLE Accel
;
333 DECLARE_RETURN(BOOLEAN
);
335 /* FIXME: If the handle table is from a call to LoadAcceleratorTable, decrement it's
336 usage count (and return TRUE).
337 FIXME: Destroy only tables created using CreateAcceleratorTable.
340 TRACE("NtUserDestroyAcceleratorTable(Table %x)\n", hAccel
);
341 UserEnterExclusive();
343 if (!(Accel
= UserGetAccelObject(hAccel
)))
348 if (Accel
->Table
!= NULL
)
350 ExFreePoolWithTag(Accel
->Table
, USERTAG_ACCEL
);
354 UserDeleteObject(hAccel
, otAccel
);
359 TRACE("Leave NtUserDestroyAcceleratorTable(Table %x) = %i\n", hAccel
, _ret_
);
366 NtUserTranslateAccelerator(
369 LPMSG pUnsafeMessage
)
372 PACCELERATOR_TABLE Accel
= NULL
;
375 USER_REFERENCE_ENTRY AccelRef
, WindowRef
;
378 TRACE("NtUserTranslateAccelerator(hWnd %x, Table %x, Message %p)\n",
379 hWnd
, hAccel
, pUnsafeMessage
);
389 ProbeForRead(pUnsafeMessage
, sizeof(MSG
), 4);
390 RtlCopyMemory(&Message
, pUnsafeMessage
, sizeof(MSG
));
392 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
394 SetLastNtError(_SEH2_GetExceptionCode());
395 _SEH2_YIELD(RETURN( 0));
399 if ((Message
.message
!= WM_KEYDOWN
) &&
400 (Message
.message
!= WM_SYSKEYDOWN
) &&
401 (Message
.message
!= WM_SYSCHAR
) &&
402 (Message
.message
!= WM_CHAR
))
407 Accel
= UserGetAccelObject(hAccel
);
413 UserRefObjectCo(Accel
, &AccelRef
);
415 Window
= UserGetWindowObject(hWnd
);
421 UserRefObjectCo(Window
, &WindowRef
);
423 /* FIXME: Associate AcceleratorTable with the current thread */
425 for (i
= 0; i
< Accel
->Count
; i
++)
427 if (co_IntTranslateAccelerator(Window
, &Message
, &Accel
->Table
[i
]))
432 /* Undocumented feature... */
433 if (Accel
->Table
[i
].fVirt
& FVIRT_TBL_END
)
440 if (Window
) UserDerefObjectCo(Window
);
441 if (Accel
) UserDerefObjectCo(Accel
);
443 TRACE("NtUserTranslateAccelerator returns %d\n", _ret_
);