1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "ui/events/event_constants.h"
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "base/win/win_util.h"
12 #include "ui/events/event_utils.h"
13 #include "ui/events/keycodes/keyboard_code_conversion_win.h"
14 #include "ui/gfx/point.h"
15 #include "ui/gfx/win/dpi.h"
21 // From MSDN: "Mouse" events are flagged with 0xFF515700 if they come
22 // from a touch or stylus device. In Vista or later, they are also flagged
23 // with 0x80 if they come from touch.
24 #define MOUSEEVENTF_FROMTOUCH (0xFF515700 | 0x80)
26 // Get the native mouse key state from the native event message type.
27 int GetNativeMouseKey(const base::NativeEvent& native_event) {
28 switch (native_event.message) {
29 case WM_LBUTTONDBLCLK:
32 case WM_NCLBUTTONDBLCLK:
33 case WM_NCLBUTTONDOWN:
36 case WM_MBUTTONDBLCLK:
39 case WM_NCMBUTTONDBLCLK:
40 case WM_NCMBUTTONDOWN:
43 case WM_RBUTTONDBLCLK:
46 case WM_NCRBUTTONDBLCLK:
47 case WM_NCRBUTTONDOWN:
50 case WM_NCXBUTTONDBLCLK:
51 case WM_NCXBUTTONDOWN:
53 case WM_XBUTTONDBLCLK:
61 bool IsButtonDown(const base::NativeEvent& native_event) {
62 return ((MK_LBUTTON | MK_MBUTTON | MK_RBUTTON | MK_XBUTTON1 | MK_XBUTTON2) &
63 native_event.wParam) != 0;
66 bool IsClientMouseEvent(const base::NativeEvent& native_event) {
67 return native_event.message == WM_MOUSELEAVE ||
68 native_event.message == WM_MOUSEHOVER ||
69 (native_event.message >= WM_MOUSEFIRST &&
70 native_event.message <= WM_MOUSELAST);
73 bool IsNonClientMouseEvent(const base::NativeEvent& native_event) {
74 return native_event.message == WM_NCMOUSELEAVE ||
75 native_event.message == WM_NCMOUSEHOVER ||
76 (native_event.message >= WM_NCMOUSEMOVE &&
77 native_event.message <= WM_NCXBUTTONDBLCLK);
80 bool IsMouseEvent(const base::NativeEvent& native_event) {
81 return IsClientMouseEvent(native_event) ||
82 IsNonClientMouseEvent(native_event);
85 bool IsMouseWheelEvent(const base::NativeEvent& native_event) {
86 return native_event.message == WM_MOUSEWHEEL ||
87 native_event.message == WM_MOUSEHWHEEL;
90 bool IsKeyEvent(const base::NativeEvent& native_event) {
91 return native_event.message == WM_KEYDOWN ||
92 native_event.message == WM_SYSKEYDOWN ||
93 native_event.message == WM_CHAR ||
94 native_event.message == WM_KEYUP ||
95 native_event.message == WM_SYSKEYUP;
98 bool IsScrollEvent(const base::NativeEvent& native_event) {
99 return native_event.message == WM_VSCROLL ||
100 native_event.message == WM_HSCROLL;
103 // Returns a mask corresponding to the set of pressed modifier keys.
104 // Checks the current global state and the state sent by client mouse messages.
105 int KeyStateFlagsFromNative(const base::NativeEvent& native_event) {
107 flags |= base::win::IsAltPressed() ? EF_ALT_DOWN : EF_NONE;
108 flags |= base::win::IsShiftPressed() ? EF_SHIFT_DOWN : EF_NONE;
109 flags |= base::win::IsCtrlPressed() ? EF_CONTROL_DOWN : EF_NONE;
111 // Check key messages for the extended key flag.
112 if (IsKeyEvent(native_event))
113 flags |= (HIWORD(native_event.lParam) & KF_EXTENDED) ? EF_EXTENDED : 0;
115 // Most client mouse messages include key state information.
116 if (IsClientMouseEvent(native_event)) {
117 int win_flags = GET_KEYSTATE_WPARAM(native_event.wParam);
118 flags |= (win_flags & MK_SHIFT) ? EF_SHIFT_DOWN : 0;
119 flags |= (win_flags & MK_CONTROL) ? EF_CONTROL_DOWN : 0;
125 // Returns a mask corresponding to the set of pressed mouse buttons.
126 // This includes the button of the given message, even if it is being released.
127 int MouseStateFlagsFromNative(const base::NativeEvent& native_event) {
128 int win_flags = GetNativeMouseKey(native_event);
130 // Client mouse messages provide key states in their WPARAMs.
131 if (IsClientMouseEvent(native_event))
132 win_flags |= GET_KEYSTATE_WPARAM(native_event.wParam);
135 flags |= (win_flags & MK_LBUTTON) ? EF_LEFT_MOUSE_BUTTON : 0;
136 flags |= (win_flags & MK_MBUTTON) ? EF_MIDDLE_MOUSE_BUTTON : 0;
137 flags |= (win_flags & MK_RBUTTON) ? EF_RIGHT_MOUSE_BUTTON : 0;
138 flags |= IsNonClientMouseEvent(native_event) ? EF_IS_NON_CLIENT : 0;
144 void UpdateDeviceList() {
148 EventType EventTypeFromNative(const base::NativeEvent& native_event) {
149 switch (native_event.message) {
153 return ET_KEY_PRESSED;
156 return ET_KEY_RELEASED;
157 case WM_LBUTTONDBLCLK:
159 case WM_MBUTTONDBLCLK:
161 case WM_NCLBUTTONDBLCLK:
162 case WM_NCLBUTTONDOWN:
163 case WM_NCMBUTTONDBLCLK:
164 case WM_NCMBUTTONDOWN:
165 case WM_NCRBUTTONDBLCLK:
166 case WM_NCRBUTTONDOWN:
167 case WM_NCXBUTTONDBLCLK:
168 case WM_NCXBUTTONDOWN:
169 case WM_RBUTTONDBLCLK:
171 case WM_XBUTTONDBLCLK:
173 return ET_MOUSE_PRESSED;
182 return ET_MOUSE_RELEASED;
184 return IsButtonDown(native_event) ? ET_MOUSE_DRAGGED : ET_MOUSE_MOVED;
186 return ET_MOUSE_MOVED;
189 return ET_MOUSEWHEEL;
191 case WM_NCMOUSELEAVE:
192 return ET_MOUSE_EXITED;
197 // We can't NOTREACHED() here, since this function can be called for any
204 int EventFlagsFromNative(const base::NativeEvent& native_event) {
205 int flags = KeyStateFlagsFromNative(native_event);
206 if (IsMouseEvent(native_event))
207 flags |= MouseStateFlagsFromNative(native_event);
212 base::TimeDelta EventTimeFromNative(const base::NativeEvent& native_event) {
213 return base::TimeDelta::FromMilliseconds(native_event.time);
216 gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) {
218 if ((native_event.message == WM_MOUSELEAVE ||
219 native_event.message == WM_NCMOUSELEAVE) ||
220 IsScrollEvent(native_event)) {
221 // These events have no coordinates. For sanity with rest of events grab
222 // coordinates from the OS.
223 ::GetCursorPos(&native_point);
224 } else if (IsClientMouseEvent(native_event) &&
225 !IsMouseWheelEvent(native_event)) {
226 // Note: Wheel events are considered client, but their position is in screen
228 // Client message. The position is contained in the LPARAM.
229 return gfx::Point(native_event.lParam);
231 DCHECK(IsNonClientMouseEvent(native_event) ||
232 IsMouseWheelEvent(native_event) || IsScrollEvent(native_event));
233 // Non-client message. The position is contained in a POINTS structure in
234 // LPARAM, and is in screen coordinates so we have to convert to client.
235 native_point.x = GET_X_LPARAM(native_event.lParam);
236 native_point.y = GET_Y_LPARAM(native_event.lParam);
238 ScreenToClient(native_event.hwnd, &native_point);
239 return gfx::win::ScreenToDIPPoint(gfx::Point(native_point));
242 gfx::Point EventSystemLocationFromNative(
243 const base::NativeEvent& native_event) {
244 // TODO(ben): Needs to always return screen position here. Returning normal
245 // origin for now since that's obviously wrong.
246 return gfx::Point(0, 0);
249 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) {
250 return KeyboardCodeForWindowsKeyCode(native_event.wParam);
253 const char* CodeFromNative(const base::NativeEvent& native_event) {
254 const uint16 scan_code = GetScanCodeFromLParam(native_event.lParam);
255 return CodeForWindowsScanCode(scan_code);
258 int GetChangedMouseButtonFlagsFromNative(
259 const base::NativeEvent& native_event) {
260 switch (GetNativeMouseKey(native_event)) {
262 return EF_LEFT_MOUSE_BUTTON;
264 return EF_MIDDLE_MOUSE_BUTTON;
266 return EF_RIGHT_MOUSE_BUTTON;
267 // TODO: add support for MK_XBUTTON1.
274 gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& native_event) {
275 DCHECK(native_event.message == WM_MOUSEWHEEL ||
276 native_event.message == WM_MOUSEHWHEEL);
277 if (native_event.message == WM_MOUSEWHEEL)
278 return gfx::Vector2d(0, GET_WHEEL_DELTA_WPARAM(native_event.wParam));
279 return gfx::Vector2d(GET_WHEEL_DELTA_WPARAM(native_event.wParam), 0);
282 void ClearTouchIdIfReleased(const base::NativeEvent& xev) {
286 int GetTouchId(const base::NativeEvent& xev) {
291 float GetTouchRadiusX(const base::NativeEvent& native_event) {
296 float GetTouchRadiusY(const base::NativeEvent& native_event) {
301 float GetTouchAngle(const base::NativeEvent& native_event) {
306 float GetTouchForce(const base::NativeEvent& native_event) {
311 bool GetScrollOffsets(const base::NativeEvent& native_event,
314 float* x_offset_ordinal,
315 float* y_offset_ordinal,
318 // Support retrieving the scroll offsets from the scroll event.
319 if (native_event.message == WM_VSCROLL || native_event.message == WM_HSCROLL)
324 bool GetFlingData(const base::NativeEvent& native_event,
330 // Not supported in Windows.
335 bool GetGestureTimes(const base::NativeEvent& native_event,
338 // Not supported in Windows.
344 void SetNaturalScroll(bool enabled) {
348 bool IsNaturalScrollEnabled() {
353 bool IsTouchpadEvent(const base::NativeEvent& event) {
358 int GetModifiersFromACCEL(const ACCEL& accel) {
359 int modifiers = EF_NONE;
360 if (accel.fVirt & FSHIFT)
361 modifiers |= EF_SHIFT_DOWN;
362 if (accel.fVirt & FCONTROL)
363 modifiers |= EF_CONTROL_DOWN;
364 if (accel.fVirt & FALT)
365 modifiers |= EF_ALT_DOWN;
369 int GetModifiersFromKeyState() {
370 int modifiers = EF_NONE;
371 if (base::win::IsShiftPressed())
372 modifiers |= EF_SHIFT_DOWN;
373 if (base::win::IsCtrlPressed())
374 modifiers |= EF_CONTROL_DOWN;
375 if (base::win::IsAltPressed())
376 modifiers |= EF_ALT_DOWN;
377 if (base::win::IsAltGrPressed())
378 modifiers |= EF_ALTGR_DOWN;
382 // Windows emulates mouse messages for touch events.
383 bool IsMouseEventFromTouch(UINT message) {
384 return (message >= WM_MOUSEFIRST) && (message <= WM_MOUSELAST) &&
385 (GetMessageExtraInfo() & MOUSEEVENTF_FROMTOUCH) ==
386 MOUSEEVENTF_FROMTOUCH;
389 // Conversion scan_code and LParam each other.
391 // ui/events/keycodes/dom4/keycode_converter_data.h
392 // 0 - 15bits: represetns the scan code.
393 // 28 - 30 bits (0xE000): represents whether this is an extended key or not.
396 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms644984.aspx
397 // 16 - 23bits: represetns the scan code.
398 // 24bit (0x0100): represents whether this is an extended key or not.
399 uint16 GetScanCodeFromLParam(LPARAM l_param) {
400 uint16 scan_code = ((l_param >> 16) & 0x00FF);
401 if (l_param & (1 << 24))
406 LPARAM GetLParamFromScanCode(uint16 scan_code) {
407 LPARAM l_param = static_cast<LPARAM>(scan_code & 0x00FF) << 16;
408 if ((scan_code & 0xE000) == 0xE000)
409 l_param |= (1 << 24);