Bug 1822519 - When qcms_profile_from_memory fails, default to sRGB for display space...
[gecko.git] / widget / WidgetEventImpl.cpp
blobfb40c763acc2c0b7066dcd34b06c13a7e14c64e5
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "BasicEvents.h"
7 #include "ContentEvents.h"
8 #include "MiscEvents.h"
9 #include "MouseEvents.h"
10 #include "NativeKeyBindingsType.h"
11 #include "TextEventDispatcher.h"
12 #include "TextEvents.h"
13 #include "TouchEvents.h"
15 #include "mozilla/EventStateManager.h"
16 #include "mozilla/InternalMutationEvent.h"
17 #include "mozilla/Maybe.h"
18 #include "mozilla/Preferences.h"
19 #include "mozilla/StaticPrefs_mousewheel.h"
20 #include "mozilla/StaticPrefs_ui.h"
21 #include "mozilla/WritingModes.h"
22 #include "mozilla/dom/KeyboardEventBinding.h"
23 #include "mozilla/dom/WheelEventBinding.h"
24 #include "nsCommandParams.h"
25 #include "nsContentUtils.h"
26 #include "nsIContent.h"
27 #include "nsIDragSession.h"
28 #include "nsPrintfCString.h"
30 #if defined(XP_WIN)
31 # include "npapi.h"
32 # include "WinUtils.h"
33 #endif // #if defined (XP_WIN)
35 #if defined(MOZ_WIDGET_GTK) || defined(XP_MACOSX)
36 # include "NativeKeyBindings.h"
37 #endif // #if defined(MOZ_WIDGET_GTK) || defined(XP_MACOSX)
39 namespace mozilla {
41 /******************************************************************************
42 * Global helper methods
43 ******************************************************************************/
45 const char* ToChar(EventMessage aEventMessage) {
46 switch (aEventMessage) {
47 #define NS_EVENT_MESSAGE(aMessage) \
48 case aMessage: \
49 return #aMessage;
51 #include "mozilla/EventMessageList.h"
53 #undef NS_EVENT_MESSAGE
54 default:
55 return "illegal event message";
59 const char* ToChar(EventClassID aEventClassID) {
60 switch (aEventClassID) {
61 #define NS_ROOT_EVENT_CLASS(aPrefix, aName) \
62 case eBasic##aName##Class: \
63 return "eBasic" #aName "Class";
65 #define NS_EVENT_CLASS(aPrefix, aName) \
66 case e##aName##Class: \
67 return "e" #aName "Class";
69 #include "mozilla/EventClassList.h"
71 #undef NS_EVENT_CLASS
72 #undef NS_ROOT_EVENT_CLASS
73 default:
74 return "illegal event class ID";
78 const nsCString ToString(KeyNameIndex aKeyNameIndex) {
79 if (aKeyNameIndex == KEY_NAME_INDEX_USE_STRING) {
80 return "USE_STRING"_ns;
82 nsAutoString keyName;
83 WidgetKeyboardEvent::GetDOMKeyName(aKeyNameIndex, keyName);
84 return NS_ConvertUTF16toUTF8(keyName);
87 const nsCString ToString(CodeNameIndex aCodeNameIndex) {
88 if (aCodeNameIndex == CODE_NAME_INDEX_USE_STRING) {
89 return "USE_STRING"_ns;
91 nsAutoString codeName;
92 WidgetKeyboardEvent::GetDOMCodeName(aCodeNameIndex, codeName);
93 return NS_ConvertUTF16toUTF8(codeName);
96 const char* ToChar(Command aCommand) {
97 if (aCommand == Command::DoNothing) {
98 return "CommandDoNothing";
101 switch (aCommand) {
102 #define NS_DEFINE_COMMAND(aName, aCommandStr) \
103 case Command::aName: \
104 return "Command::" #aName;
105 #define NS_DEFINE_COMMAND_WITH_PARAM(aName, aCommandStr, aParam) \
106 case Command::aName: \
107 return "Command::" #aName;
108 #define NS_DEFINE_COMMAND_NO_EXEC_COMMAND(aName) \
109 case Command::aName: \
110 return "Command::" #aName;
112 #include "mozilla/CommandList.h"
114 #undef NS_DEFINE_COMMAND
115 #undef NS_DEFINE_COMMAND_WITH_PARAM
116 #undef NS_DEFINE_COMMAND_NO_EXEC_COMMAND
118 default:
119 return "illegal command value";
123 const nsCString GetDOMKeyCodeName(uint32_t aKeyCode) {
124 switch (aKeyCode) {
125 #define NS_DISALLOW_SAME_KEYCODE
126 #define NS_DEFINE_VK(aDOMKeyName, aDOMKeyCode) \
127 case aDOMKeyCode: \
128 return nsLiteralCString(#aDOMKeyName);
130 #include "mozilla/VirtualKeyCodeList.h"
132 #undef NS_DEFINE_VK
133 #undef NS_DISALLOW_SAME_KEYCODE
135 default:
136 return nsPrintfCString("Invalid DOM keyCode (0x%08X)", aKeyCode);
140 bool IsValidRawTextRangeValue(RawTextRangeType aRawTextRangeType) {
141 switch (static_cast<TextRangeType>(aRawTextRangeType)) {
142 case TextRangeType::eUninitialized:
143 case TextRangeType::eCaret:
144 case TextRangeType::eRawClause:
145 case TextRangeType::eSelectedRawClause:
146 case TextRangeType::eConvertedClause:
147 case TextRangeType::eSelectedClause:
148 return true;
149 default:
150 return false;
154 RawTextRangeType ToRawTextRangeType(TextRangeType aTextRangeType) {
155 return static_cast<RawTextRangeType>(aTextRangeType);
158 TextRangeType ToTextRangeType(RawTextRangeType aRawTextRangeType) {
159 MOZ_ASSERT(IsValidRawTextRangeValue(aRawTextRangeType));
160 return static_cast<TextRangeType>(aRawTextRangeType);
163 const char* ToChar(TextRangeType aTextRangeType) {
164 switch (aTextRangeType) {
165 case TextRangeType::eUninitialized:
166 return "TextRangeType::eUninitialized";
167 case TextRangeType::eCaret:
168 return "TextRangeType::eCaret";
169 case TextRangeType::eRawClause:
170 return "TextRangeType::eRawClause";
171 case TextRangeType::eSelectedRawClause:
172 return "TextRangeType::eSelectedRawClause";
173 case TextRangeType::eConvertedClause:
174 return "TextRangeType::eConvertedClause";
175 case TextRangeType::eSelectedClause:
176 return "TextRangeType::eSelectedClause";
177 default:
178 return "Invalid TextRangeType";
182 SelectionType ToSelectionType(TextRangeType aTextRangeType) {
183 switch (aTextRangeType) {
184 case TextRangeType::eRawClause:
185 return SelectionType::eIMERawClause;
186 case TextRangeType::eSelectedRawClause:
187 return SelectionType::eIMESelectedRawClause;
188 case TextRangeType::eConvertedClause:
189 return SelectionType::eIMEConvertedClause;
190 case TextRangeType::eSelectedClause:
191 return SelectionType::eIMESelectedClause;
192 default:
193 MOZ_CRASH("TextRangeType is invalid");
194 return SelectionType::eNormal;
198 /******************************************************************************
199 * non class method implementation
200 ******************************************************************************/
202 static nsTHashMap<nsDepCharHashKey, Command>* sCommandHashtable = nullptr;
204 Command GetInternalCommand(const char* aCommandName,
205 const nsCommandParams* aCommandParams) {
206 if (!aCommandName) {
207 return Command::DoNothing;
210 // Special cases for "cmd_align". It's mapped to multiple internal commands
211 // with additional param. Therefore, we cannot handle it with the hashtable.
212 if (!strcmp(aCommandName, "cmd_align")) {
213 if (!aCommandParams) {
214 // Note that if this is called by EditorCommand::IsCommandEnabled(),
215 // it cannot set aCommandParams. So, don't warn in this case even though
216 // this is illegal case for DoCommandParams().
217 return Command::FormatJustify;
219 nsAutoCString cValue;
220 nsresult rv = aCommandParams->GetCString("state_attribute", cValue);
221 if (NS_FAILED(rv)) {
222 nsString value; // Avoid copying the string buffer with using nsString.
223 rv = aCommandParams->GetString("state_attribute", value);
224 if (NS_FAILED(rv)) {
225 return Command::FormatJustifyNone;
227 CopyUTF16toUTF8(value, cValue);
229 if (cValue.LowerCaseEqualsASCII("left")) {
230 return Command::FormatJustifyLeft;
232 if (cValue.LowerCaseEqualsASCII("right")) {
233 return Command::FormatJustifyRight;
235 if (cValue.LowerCaseEqualsASCII("center")) {
236 return Command::FormatJustifyCenter;
238 if (cValue.LowerCaseEqualsASCII("justify")) {
239 return Command::FormatJustifyFull;
241 if (cValue.IsEmpty()) {
242 return Command::FormatJustifyNone;
244 return Command::DoNothing;
247 if (!sCommandHashtable) {
248 sCommandHashtable = new nsTHashMap<nsDepCharHashKey, Command>();
249 #define NS_DEFINE_COMMAND(aName, aCommandStr) \
250 sCommandHashtable->InsertOrUpdate(#aCommandStr, Command::aName);
252 #define NS_DEFINE_COMMAND_WITH_PARAM(aName, aCommandStr, aParam)
254 #define NS_DEFINE_COMMAND_NO_EXEC_COMMAND(aName)
256 #include "mozilla/CommandList.h"
258 #undef NS_DEFINE_COMMAND
259 #undef NS_DEFINE_COMMAND_WITH_PARAM
260 #undef NS_DEFINE_COMMAND_NO_EXEC_COMMAND
262 Command command = Command::DoNothing;
263 if (!sCommandHashtable->Get(aCommandName, &command)) {
264 return Command::DoNothing;
266 return command;
269 /******************************************************************************
270 * As*Event() implementation
271 ******************************************************************************/
273 #define NS_ROOT_EVENT_CLASS(aPrefix, aName)
274 #define NS_EVENT_CLASS(aPrefix, aName) \
275 aPrefix##aName* WidgetEvent::As##aName() { return nullptr; } \
277 const aPrefix##aName* WidgetEvent::As##aName() const { \
278 return const_cast<WidgetEvent*>(this)->As##aName(); \
281 #include "mozilla/EventClassList.h"
283 #undef NS_EVENT_CLASS
284 #undef NS_ROOT_EVENT_CLASS
286 /******************************************************************************
287 * mozilla::WidgetEvent
289 * Event struct type checking methods.
290 ******************************************************************************/
292 bool WidgetEvent::IsQueryContentEvent() const {
293 return mClass == eQueryContentEventClass;
296 bool WidgetEvent::IsSelectionEvent() const {
297 return mClass == eSelectionEventClass;
300 bool WidgetEvent::IsContentCommandEvent() const {
301 return mClass == eContentCommandEventClass;
304 /******************************************************************************
305 * mozilla::WidgetEvent
307 * Event message checking methods.
308 ******************************************************************************/
310 bool WidgetEvent::HasMouseEventMessage() const {
311 switch (mMessage) {
312 case eMouseDown:
313 case eMouseUp:
314 case eMouseClick:
315 case eMouseDoubleClick:
316 case eMouseAuxClick:
317 case eMouseEnterIntoWidget:
318 case eMouseExitFromWidget:
319 case eMouseActivate:
320 case eMouseOver:
321 case eMouseOut:
322 case eMouseHitTest:
323 case eMouseMove:
324 return true;
325 default:
326 return false;
330 bool WidgetEvent::HasDragEventMessage() const {
331 switch (mMessage) {
332 case eDragEnter:
333 case eDragOver:
334 case eDragExit:
335 case eDrag:
336 case eDragEnd:
337 case eDragStart:
338 case eDrop:
339 case eDragLeave:
340 return true;
341 default:
342 return false;
346 /* static */
347 bool WidgetEvent::IsKeyEventMessage(EventMessage aMessage) {
348 switch (aMessage) {
349 case eKeyDown:
350 case eKeyPress:
351 case eKeyUp:
352 case eAccessKeyNotFound:
353 return true;
354 default:
355 return false;
359 bool WidgetEvent::HasIMEEventMessage() const {
360 switch (mMessage) {
361 case eCompositionStart:
362 case eCompositionEnd:
363 case eCompositionUpdate:
364 case eCompositionChange:
365 case eCompositionCommitAsIs:
366 case eCompositionCommit:
367 return true;
368 default:
369 return false;
373 /******************************************************************************
374 * mozilla::WidgetEvent
376 * Specific event checking methods.
377 ******************************************************************************/
379 bool WidgetEvent::CanBeSentToRemoteProcess() const {
380 // If this event is explicitly marked as shouldn't be sent to remote process,
381 // just return false.
382 if (IsCrossProcessForwardingStopped()) {
383 return false;
386 if (mClass == eKeyboardEventClass || mClass == eWheelEventClass) {
387 return true;
390 switch (mMessage) {
391 case eMouseDown:
392 case eMouseUp:
393 case eMouseMove:
394 case eMouseExploreByTouch:
395 case eContextMenu:
396 case eMouseEnterIntoWidget:
397 case eMouseExitFromWidget:
398 case eMouseTouchDrag:
399 case eTouchStart:
400 case eTouchMove:
401 case eTouchEnd:
402 case eTouchCancel:
403 case eDragOver:
404 case eDragExit:
405 case eDrop:
406 return true;
407 default:
408 return false;
412 bool WidgetEvent::WillBeSentToRemoteProcess() const {
413 // This event won't be posted to remote process if it's already explicitly
414 // stopped.
415 if (IsCrossProcessForwardingStopped()) {
416 return false;
419 // When mOriginalTarget is nullptr, this method shouldn't be used.
420 if (NS_WARN_IF(!mOriginalTarget)) {
421 return false;
424 return EventStateManager::IsTopLevelRemoteTarget(
425 nsIContent::FromEventTarget(mOriginalTarget));
428 bool WidgetEvent::IsIMERelatedEvent() const {
429 return HasIMEEventMessage() || IsQueryContentEvent() || IsSelectionEvent();
432 bool WidgetEvent::IsUsingCoordinates() const {
433 const WidgetMouseEvent* mouseEvent = AsMouseEvent();
434 if (mouseEvent) {
435 return !mouseEvent->IsContextMenuKeyEvent();
437 return !HasKeyEventMessage() && !IsIMERelatedEvent() &&
438 !IsContentCommandEvent();
441 bool WidgetEvent::IsTargetedAtFocusedWindow() const {
442 const WidgetMouseEvent* mouseEvent = AsMouseEvent();
443 if (mouseEvent) {
444 return mouseEvent->IsContextMenuKeyEvent();
446 return HasKeyEventMessage() || IsIMERelatedEvent() || IsContentCommandEvent();
449 bool WidgetEvent::IsTargetedAtFocusedContent() const {
450 const WidgetMouseEvent* mouseEvent = AsMouseEvent();
451 if (mouseEvent) {
452 return mouseEvent->IsContextMenuKeyEvent();
454 return HasKeyEventMessage() || IsIMERelatedEvent();
457 bool WidgetEvent::IsAllowedToDispatchDOMEvent() const {
458 switch (mClass) {
459 case eMouseEventClass:
460 if (mMessage == eMouseTouchDrag) {
461 return false;
463 [[fallthrough]];
464 case ePointerEventClass:
465 // We want synthesized mouse moves to cause mouseover and mouseout
466 // DOM events (EventStateManager::PreHandleEvent), but not mousemove
467 // DOM events.
468 // Synthesized button up events also do not cause DOM events because they
469 // do not have a reliable mRefPoint.
470 return AsMouseEvent()->mReason == WidgetMouseEvent::eReal;
472 case eWheelEventClass: {
473 // wheel event whose all delta values are zero by user pref applied, it
474 // shouldn't cause a DOM event.
475 const WidgetWheelEvent* wheelEvent = AsWheelEvent();
476 return wheelEvent->mDeltaX != 0.0 || wheelEvent->mDeltaY != 0.0 ||
477 wheelEvent->mDeltaZ != 0.0;
479 case eTouchEventClass:
480 return mMessage != eTouchPointerCancel;
481 // Following events are handled in EventStateManager, so, we don't need to
482 // dispatch DOM event for them into the DOM tree.
483 case eQueryContentEventClass:
484 case eSelectionEventClass:
485 case eContentCommandEventClass:
486 return false;
488 default:
489 return true;
493 bool WidgetEvent::IsAllowedToDispatchInSystemGroup() const {
494 // We don't expect to implement default behaviors with pointer events because
495 // if we do, prevent default on mouse events can't prevent default behaviors
496 // anymore.
497 return mClass != ePointerEventClass;
500 bool WidgetEvent::IsBlockedForFingerprintingResistance() const {
501 switch (mClass) {
502 case eKeyboardEventClass: {
503 const WidgetKeyboardEvent* keyboardEvent = AsKeyboardEvent();
505 return (keyboardEvent->mKeyNameIndex == KEY_NAME_INDEX_Alt ||
506 keyboardEvent->mKeyNameIndex == KEY_NAME_INDEX_Shift ||
507 keyboardEvent->mKeyNameIndex == KEY_NAME_INDEX_Control ||
508 keyboardEvent->mKeyNameIndex == KEY_NAME_INDEX_AltGraph);
510 case ePointerEventClass: {
511 const WidgetPointerEvent* pointerEvent = AsPointerEvent();
513 // We suppress the pointer events if it is not primary for fingerprinting
514 // resistance. It is because of that we want to spoof any pointer event
515 // into a mouse pointer event and the mouse pointer event only has
516 // isPrimary as true.
517 return !pointerEvent->mIsPrimary;
519 default:
520 return false;
524 /******************************************************************************
525 * mozilla::WidgetEvent
527 * Misc methods.
528 ******************************************************************************/
530 static dom::EventTarget* GetTargetForDOMEvent(dom::EventTarget* aTarget) {
531 return aTarget ? aTarget->GetTargetForDOMEvent() : nullptr;
534 dom::EventTarget* WidgetEvent::GetDOMEventTarget() const {
535 return GetTargetForDOMEvent(mTarget);
538 dom::EventTarget* WidgetEvent::GetCurrentDOMEventTarget() const {
539 return GetTargetForDOMEvent(mCurrentTarget);
542 dom::EventTarget* WidgetEvent::GetOriginalDOMEventTarget() const {
543 if (mOriginalTarget) {
544 return GetTargetForDOMEvent(mOriginalTarget);
546 return GetDOMEventTarget();
549 void WidgetEvent::PreventDefault(bool aCalledByDefaultHandler,
550 nsIPrincipal* aPrincipal) {
551 if (mMessage == ePointerDown) {
552 if (aCalledByDefaultHandler) {
553 // Shouldn't prevent default on pointerdown by default handlers to stop
554 // firing legacy mouse events. Use MOZ_ASSERT to catch incorrect usages
555 // in debug builds.
556 MOZ_ASSERT(false);
557 return;
559 if (aPrincipal) {
560 nsAutoString addonId;
561 Unused << NS_WARN_IF(NS_FAILED(aPrincipal->GetAddonId(addonId)));
562 if (!addonId.IsEmpty()) {
563 // Ignore the case that it's called by a web extension.
564 return;
568 mFlags.PreventDefault(aCalledByDefaultHandler);
571 bool WidgetEvent::IsUserAction() const {
572 if (!IsTrusted()) {
573 return false;
575 // FYI: eMouseScrollEventClass and ePointerEventClass represent
576 // user action but they are synthesized events.
577 switch (mClass) {
578 case eKeyboardEventClass:
579 case eCompositionEventClass:
580 case eMouseScrollEventClass:
581 case eWheelEventClass:
582 case eGestureNotifyEventClass:
583 case eSimpleGestureEventClass:
584 case eTouchEventClass:
585 case eCommandEventClass:
586 case eContentCommandEventClass:
587 return true;
588 case eMouseEventClass:
589 case eDragEventClass:
590 case ePointerEventClass:
591 return AsMouseEvent()->IsReal();
592 default:
593 return false;
597 /******************************************************************************
598 * mozilla::WidgetInputEvent
599 ******************************************************************************/
601 /* static */
602 Modifier WidgetInputEvent::GetModifier(const nsAString& aDOMKeyName) {
603 if (aDOMKeyName.EqualsLiteral("Accel")) {
604 return AccelModifier();
606 KeyNameIndex keyNameIndex = WidgetKeyboardEvent::GetKeyNameIndex(aDOMKeyName);
607 return WidgetKeyboardEvent::GetModifierForKeyName(keyNameIndex);
610 /* static */
611 Modifier WidgetInputEvent::AccelModifier() {
612 static Modifier sAccelModifier = MODIFIER_NONE;
613 if (sAccelModifier == MODIFIER_NONE) {
614 switch (StaticPrefs::ui_key_accelKey()) {
615 case dom::KeyboardEvent_Binding::DOM_VK_META:
616 sAccelModifier = MODIFIER_META;
617 break;
618 case dom::KeyboardEvent_Binding::DOM_VK_WIN:
619 sAccelModifier = MODIFIER_OS;
620 break;
621 case dom::KeyboardEvent_Binding::DOM_VK_ALT:
622 sAccelModifier = MODIFIER_ALT;
623 break;
624 case dom::KeyboardEvent_Binding::DOM_VK_CONTROL:
625 sAccelModifier = MODIFIER_CONTROL;
626 break;
627 default:
628 #ifdef XP_MACOSX
629 sAccelModifier = MODIFIER_META;
630 #else
631 sAccelModifier = MODIFIER_CONTROL;
632 #endif
633 break;
636 return sAccelModifier;
639 /******************************************************************************
640 * mozilla::WidgetMouseEvent (MouseEvents.h)
641 ******************************************************************************/
643 /* static */
644 bool WidgetMouseEvent::IsMiddleClickPasteEnabled() {
645 return Preferences::GetBool("middlemouse.paste", false);
648 #ifdef DEBUG
649 void WidgetMouseEvent::AssertContextMenuEventButtonConsistency() const {
650 if (mMessage != eContextMenu) {
651 return;
654 if (mContextMenuTrigger == eNormal) {
655 NS_WARNING_ASSERTION(mButton == MouseButton::eSecondary,
656 "eContextMenu events with eNormal trigger should use "
657 "secondary mouse button");
658 } else {
659 NS_WARNING_ASSERTION(mButton == MouseButton::ePrimary,
660 "eContextMenu events with non-eNormal trigger should "
661 "use primary mouse button");
664 if (mContextMenuTrigger == eControlClick) {
665 NS_WARNING_ASSERTION(IsControl(),
666 "eContextMenu events with eControlClick trigger "
667 "should return true from IsControl()");
670 #endif
672 /******************************************************************************
673 * mozilla::WidgetDragEvent (MouseEvents.h)
674 ******************************************************************************/
676 void WidgetDragEvent::InitDropEffectForTests() {
677 MOZ_ASSERT(mFlags.mIsSynthesizedForTests);
679 nsCOMPtr<nsIDragSession> session = nsContentUtils::GetDragSession();
680 if (NS_WARN_IF(!session)) {
681 return;
684 uint32_t effectAllowed = session->GetEffectAllowedForTests();
685 uint32_t desiredDropEffect = nsIDragService::DRAGDROP_ACTION_NONE;
686 #ifdef XP_MACOSX
687 if (IsAlt()) {
688 desiredDropEffect = IsMeta() ? nsIDragService::DRAGDROP_ACTION_LINK
689 : nsIDragService::DRAGDROP_ACTION_COPY;
691 #else
692 // On Linux, we know user's intention from API, but we should use
693 // same modifiers as Windows for tests because GNOME on Ubuntu use
694 // them and that makes each test simpler.
695 if (IsControl()) {
696 desiredDropEffect = IsShift() ? nsIDragService::DRAGDROP_ACTION_LINK
697 : nsIDragService::DRAGDROP_ACTION_COPY;
698 } else if (IsShift()) {
699 desiredDropEffect = nsIDragService::DRAGDROP_ACTION_MOVE;
701 #endif // #ifdef XP_MACOSX #else
702 // First, use modifier state for preferring action which is explicitly
703 // specified by the synthesizer.
704 if (!(desiredDropEffect &= effectAllowed)) {
705 // Otherwise, use an action which is allowed at starting the session.
706 desiredDropEffect = effectAllowed;
708 if (desiredDropEffect & nsIDragService::DRAGDROP_ACTION_MOVE) {
709 session->SetDragAction(nsIDragService::DRAGDROP_ACTION_MOVE);
710 } else if (desiredDropEffect & nsIDragService::DRAGDROP_ACTION_COPY) {
711 session->SetDragAction(nsIDragService::DRAGDROP_ACTION_COPY);
712 } else if (desiredDropEffect & nsIDragService::DRAGDROP_ACTION_LINK) {
713 session->SetDragAction(nsIDragService::DRAGDROP_ACTION_LINK);
714 } else {
715 session->SetDragAction(nsIDragService::DRAGDROP_ACTION_NONE);
719 /******************************************************************************
720 * mozilla::WidgetWheelEvent (MouseEvents.h)
721 ******************************************************************************/
723 /* static */
724 double WidgetWheelEvent::ComputeOverriddenDelta(double aDelta,
725 bool aIsForVertical) {
726 if (!StaticPrefs::mousewheel_system_scroll_override_enabled()) {
727 return aDelta;
729 int32_t intFactor =
730 aIsForVertical
731 ? StaticPrefs::mousewheel_system_scroll_override_vertical_factor()
732 : StaticPrefs::mousewheel_system_scroll_override_horizontal_factor();
733 // Making the scroll speed slower doesn't make sense. So, ignore odd factor
734 // which is less than 1.0.
735 if (intFactor <= 100) {
736 return aDelta;
738 double factor = static_cast<double>(intFactor) / 100;
739 return aDelta * factor;
742 double WidgetWheelEvent::OverriddenDeltaX() const {
743 if (!mAllowToOverrideSystemScrollSpeed ||
744 mDeltaMode != dom::WheelEvent_Binding::DOM_DELTA_LINE ||
745 mCustomizedByUserPrefs) {
746 return mDeltaX;
748 return ComputeOverriddenDelta(mDeltaX, false);
751 double WidgetWheelEvent::OverriddenDeltaY() const {
752 if (!mAllowToOverrideSystemScrollSpeed ||
753 mDeltaMode != dom::WheelEvent_Binding::DOM_DELTA_LINE ||
754 mCustomizedByUserPrefs) {
755 return mDeltaY;
757 return ComputeOverriddenDelta(mDeltaY, true);
760 /******************************************************************************
761 * mozilla::WidgetKeyboardEvent (TextEvents.h)
762 ******************************************************************************/
764 #define NS_DEFINE_KEYNAME(aCPPName, aDOMKeyName) (u"" aDOMKeyName),
765 const char16_t* const WidgetKeyboardEvent::kKeyNames[] = {
766 #include "mozilla/KeyNameList.h"
768 #undef NS_DEFINE_KEYNAME
770 #define NS_DEFINE_PHYSICAL_KEY_CODE_NAME(aCPPName, aDOMCodeName) \
771 (u"" aDOMCodeName),
772 const char16_t* const WidgetKeyboardEvent::kCodeNames[] = {
773 #include "mozilla/PhysicalKeyCodeNameList.h"
775 #undef NS_DEFINE_PHYSICAL_KEY_CODE_NAME
777 WidgetKeyboardEvent::KeyNameIndexHashtable*
778 WidgetKeyboardEvent::sKeyNameIndexHashtable = nullptr;
779 WidgetKeyboardEvent::CodeNameIndexHashtable*
780 WidgetKeyboardEvent::sCodeNameIndexHashtable = nullptr;
782 void WidgetKeyboardEvent::InitAllEditCommands(
783 const Maybe<WritingMode>& aWritingMode) {
784 // If this event is synthesized for tests, we don't need to retrieve the
785 // command via the main process. So, we don't need widget and can trust
786 // the event.
787 if (!mFlags.mIsSynthesizedForTests) {
788 // If the event was created without widget, e.g., created event in chrome
789 // script, this shouldn't execute native key bindings.
790 if (NS_WARN_IF(!mWidget)) {
791 return;
794 // This event should be trusted event here and we shouldn't expose native
795 // key binding information to web contents with untrusted events.
796 if (NS_WARN_IF(!IsTrusted())) {
797 return;
800 MOZ_ASSERT(
801 XRE_IsParentProcess(),
802 "It's too expensive to retrieve all edit commands from remote process");
803 MOZ_ASSERT(!AreAllEditCommandsInitialized(),
804 "Shouldn't be called two or more times");
807 DebugOnly<bool> okIgnored = InitEditCommandsFor(
808 NativeKeyBindingsType::SingleLineEditor, aWritingMode);
809 NS_WARNING_ASSERTION(okIgnored,
810 "InitEditCommandsFor(NativeKeyBindingsType::"
811 "SingleLineEditor) failed, but ignored");
812 okIgnored =
813 InitEditCommandsFor(NativeKeyBindingsType::MultiLineEditor, aWritingMode);
814 NS_WARNING_ASSERTION(okIgnored,
815 "InitEditCommandsFor(NativeKeyBindingsType::"
816 "MultiLineEditor) failed, but ignored");
817 okIgnored =
818 InitEditCommandsFor(NativeKeyBindingsType::RichTextEditor, aWritingMode);
819 NS_WARNING_ASSERTION(okIgnored,
820 "InitEditCommandsFor(NativeKeyBindingsType::"
821 "RichTextEditor) failed, but ignored");
824 bool WidgetKeyboardEvent::InitEditCommandsFor(
825 NativeKeyBindingsType aType, const Maybe<WritingMode>& aWritingMode) {
826 bool& initialized = IsEditCommandsInitializedRef(aType);
827 if (initialized) {
828 return true;
830 nsTArray<CommandInt>& commands = EditCommandsRef(aType);
832 // If this event is synthesized for tests, we shouldn't access customized
833 // shortcut settings of the environment. Therefore, we don't need to check
834 // whether `widget` is set or not. And we can treat synthesized events are
835 // always trusted.
836 if (mFlags.mIsSynthesizedForTests) {
837 MOZ_DIAGNOSTIC_ASSERT(IsTrusted());
838 #if defined(MOZ_WIDGET_GTK) || defined(XP_MACOSX)
839 // TODO: We should implement `NativeKeyBindings` for Windows and Android
840 // too in bug 1301497 for getting rid of the #if.
841 widget::NativeKeyBindings::GetEditCommandsForTests(aType, *this,
842 aWritingMode, commands);
843 #endif
844 initialized = true;
845 return true;
848 if (NS_WARN_IF(!mWidget) || NS_WARN_IF(!IsTrusted())) {
849 return false;
851 // `nsIWidget::GetEditCommands()` will retrieve `WritingMode` at selection
852 // again, but it should be almost zero-cost since `TextEventDispatcher`
853 // caches the value.
854 nsCOMPtr<nsIWidget> widget = mWidget;
855 initialized = widget->GetEditCommands(aType, *this, commands);
856 return initialized;
859 bool WidgetKeyboardEvent::ExecuteEditCommands(NativeKeyBindingsType aType,
860 DoCommandCallback aCallback,
861 void* aCallbackData) {
862 // If the event was created without widget, e.g., created event in chrome
863 // script, this shouldn't execute native key bindings.
864 if (NS_WARN_IF(!mWidget)) {
865 return false;
868 // This event should be trusted event here and we shouldn't expose native
869 // key binding information to web contents with untrusted events.
870 if (NS_WARN_IF(!IsTrusted())) {
871 return false;
874 if (!IsEditCommandsInitializedRef(aType)) {
875 Maybe<WritingMode> writingMode;
876 if (RefPtr<widget::TextEventDispatcher> textEventDispatcher =
877 mWidget->GetTextEventDispatcher()) {
878 writingMode = textEventDispatcher->MaybeQueryWritingModeAtSelection();
880 if (NS_WARN_IF(!InitEditCommandsFor(aType, writingMode))) {
881 return false;
885 const nsTArray<CommandInt>& commands = EditCommandsRef(aType);
886 if (commands.IsEmpty()) {
887 return false;
890 for (CommandInt command : commands) {
891 aCallback(static_cast<Command>(command), aCallbackData);
893 return true;
896 bool WidgetKeyboardEvent::ShouldCauseKeypressEvents() const {
897 // Currently, we don't dispatch keypress events of modifier keys and
898 // dead keys.
899 switch (mKeyNameIndex) {
900 case KEY_NAME_INDEX_Alt:
901 case KEY_NAME_INDEX_AltGraph:
902 case KEY_NAME_INDEX_CapsLock:
903 case KEY_NAME_INDEX_Control:
904 case KEY_NAME_INDEX_Fn:
905 case KEY_NAME_INDEX_FnLock:
906 // case KEY_NAME_INDEX_Hyper:
907 case KEY_NAME_INDEX_Meta:
908 case KEY_NAME_INDEX_NumLock:
909 case KEY_NAME_INDEX_OS:
910 case KEY_NAME_INDEX_ScrollLock:
911 case KEY_NAME_INDEX_Shift:
912 // case KEY_NAME_INDEX_Super:
913 case KEY_NAME_INDEX_Symbol:
914 case KEY_NAME_INDEX_SymbolLock:
915 case KEY_NAME_INDEX_Dead:
916 return false;
917 default:
918 return true;
922 static bool HasASCIIDigit(const ShortcutKeyCandidateArray& aCandidates) {
923 for (uint32_t i = 0; i < aCandidates.Length(); ++i) {
924 uint32_t ch = aCandidates[i].mCharCode;
925 if (ch >= '0' && ch <= '9') return true;
927 return false;
930 static bool CharsCaseInsensitiveEqual(uint32_t aChar1, uint32_t aChar2) {
931 return aChar1 == aChar2 || (IS_IN_BMP(aChar1) && IS_IN_BMP(aChar2) &&
932 ToLowerCase(static_cast<char16_t>(aChar1)) ==
933 ToLowerCase(static_cast<char16_t>(aChar2)));
936 static bool IsCaseChangeableChar(uint32_t aChar) {
937 return IS_IN_BMP(aChar) && ToLowerCase(static_cast<char16_t>(aChar)) !=
938 ToUpperCase(static_cast<char16_t>(aChar));
941 void WidgetKeyboardEvent::GetShortcutKeyCandidates(
942 ShortcutKeyCandidateArray& aCandidates) const {
943 MOZ_ASSERT(aCandidates.IsEmpty(), "aCandidates must be empty");
945 // ShortcutKeyCandidate::mCharCode is a candidate charCode.
946 // ShortcutKeyCandidate::mIgnoreShift means the mCharCode should be tried to
947 // execute a command with/without shift key state. If this is TRUE, the
948 // shifted key state should be ignored. Otherwise, don't ignore the state.
949 // the priority of the charCodes are (shift key is not pressed):
950 // 0: PseudoCharCode()/false,
951 // 1: unshiftedCharCodes[0]/false, 2: unshiftedCharCodes[1]/false...
952 // the priority of the charCodes are (shift key is pressed):
953 // 0: PseudoCharCode()/false,
954 // 1: shiftedCharCodes[0]/false, 2: shiftedCharCodes[0]/true,
955 // 3: shiftedCharCodes[1]/false, 4: shiftedCharCodes[1]/true...
956 uint32_t pseudoCharCode = PseudoCharCode();
957 if (pseudoCharCode) {
958 ShortcutKeyCandidate key(pseudoCharCode, false);
959 aCandidates.AppendElement(key);
962 uint32_t len = mAlternativeCharCodes.Length();
963 if (!IsShift()) {
964 for (uint32_t i = 0; i < len; ++i) {
965 uint32_t ch = mAlternativeCharCodes[i].mUnshiftedCharCode;
966 if (!ch || ch == pseudoCharCode) {
967 continue;
969 ShortcutKeyCandidate key(ch, false);
970 aCandidates.AppendElement(key);
972 // If unshiftedCharCodes doesn't have numeric but shiftedCharCode has it,
973 // this keyboard layout is AZERTY or similar layout, probably.
974 // In this case, Accel+[0-9] should be accessible without shift key.
975 // However, the priority should be lowest.
976 if (!HasASCIIDigit(aCandidates)) {
977 for (uint32_t i = 0; i < len; ++i) {
978 uint32_t ch = mAlternativeCharCodes[i].mShiftedCharCode;
979 if (ch >= '0' && ch <= '9') {
980 ShortcutKeyCandidate key(ch, false);
981 aCandidates.AppendElement(key);
982 break;
986 } else {
987 for (uint32_t i = 0; i < len; ++i) {
988 uint32_t ch = mAlternativeCharCodes[i].mShiftedCharCode;
989 if (!ch) {
990 continue;
993 if (ch != pseudoCharCode) {
994 ShortcutKeyCandidate key(ch, false);
995 aCandidates.AppendElement(key);
998 // If the char is an alphabet, the shift key state should not be
999 // ignored. E.g., Ctrl+Shift+C should not execute Ctrl+C.
1001 // And checking the charCode is same as unshiftedCharCode too.
1002 // E.g., for Ctrl+Shift+(Plus of Numpad) should not run Ctrl+Plus.
1003 uint32_t unshiftCh = mAlternativeCharCodes[i].mUnshiftedCharCode;
1004 if (CharsCaseInsensitiveEqual(ch, unshiftCh)) {
1005 continue;
1008 // On the Hebrew keyboard layout on Windows, the unshifted char is a
1009 // localized character but the shifted char is a Latin alphabet,
1010 // then, we should not execute without the shift state. See bug 433192.
1011 if (IsCaseChangeableChar(ch)) {
1012 continue;
1015 // Setting the alternative charCode candidates for retry without shift
1016 // key state only when the shift key is pressed.
1017 ShortcutKeyCandidate key(ch, true);
1018 aCandidates.AppendElement(key);
1022 // Special case for "Space" key. With some keyboard layouts, "Space" with
1023 // or without Shift key causes non-ASCII space. For such keyboard layouts,
1024 // we should guarantee that the key press works as an ASCII white space key
1025 // press. However, if the space key is assigned to a function key, it
1026 // shouldn't work as a space key.
1027 if (mKeyNameIndex == KEY_NAME_INDEX_USE_STRING &&
1028 mCodeNameIndex == CODE_NAME_INDEX_Space && pseudoCharCode != ' ') {
1029 ShortcutKeyCandidate spaceKey(' ', false);
1030 aCandidates.AppendElement(spaceKey);
1034 void WidgetKeyboardEvent::GetAccessKeyCandidates(
1035 nsTArray<uint32_t>& aCandidates) const {
1036 MOZ_ASSERT(aCandidates.IsEmpty(), "aCandidates must be empty");
1038 // return the lower cased charCode candidates for access keys.
1039 // the priority of the charCodes are:
1040 // 0: charCode, 1: unshiftedCharCodes[0], 2: shiftedCharCodes[0]
1041 // 3: unshiftedCharCodes[1], 4: shiftedCharCodes[1],...
1042 uint32_t pseudoCharCode = PseudoCharCode();
1043 if (pseudoCharCode) {
1044 uint32_t ch = pseudoCharCode;
1045 if (IS_IN_BMP(ch)) {
1046 ch = ToLowerCase(static_cast<char16_t>(ch));
1048 aCandidates.AppendElement(ch);
1050 for (uint32_t i = 0; i < mAlternativeCharCodes.Length(); ++i) {
1051 uint32_t ch[2] = {mAlternativeCharCodes[i].mUnshiftedCharCode,
1052 mAlternativeCharCodes[i].mShiftedCharCode};
1053 for (uint32_t j = 0; j < 2; ++j) {
1054 if (!ch[j]) {
1055 continue;
1057 if (IS_IN_BMP(ch[j])) {
1058 ch[j] = ToLowerCase(static_cast<char16_t>(ch[j]));
1060 // Don't append the charcode that was already appended.
1061 if (aCandidates.IndexOf(ch[j]) == aCandidates.NoIndex) {
1062 aCandidates.AppendElement(ch[j]);
1066 // Special case for "Space" key. With some keyboard layouts, "Space" with
1067 // or without Shift key causes non-ASCII space. For such keyboard layouts,
1068 // we should guarantee that the key press works as an ASCII white space key
1069 // press. However, if the space key is assigned to a function key, it
1070 // shouldn't work as a space key.
1071 if (mKeyNameIndex == KEY_NAME_INDEX_USE_STRING &&
1072 mCodeNameIndex == CODE_NAME_INDEX_Space && pseudoCharCode != ' ') {
1073 aCandidates.AppendElement(' ');
1077 // mask values for ui.key.chromeAccess and ui.key.contentAccess
1078 #define NS_MODIFIER_SHIFT 1
1079 #define NS_MODIFIER_CONTROL 2
1080 #define NS_MODIFIER_ALT 4
1081 #define NS_MODIFIER_META 8
1082 #define NS_MODIFIER_OS 16
1084 static Modifiers PrefFlagsToModifiers(int32_t aPrefFlags) {
1085 Modifiers result = 0;
1086 if (aPrefFlags & NS_MODIFIER_SHIFT) {
1087 result |= MODIFIER_SHIFT;
1089 if (aPrefFlags & NS_MODIFIER_CONTROL) {
1090 result |= MODIFIER_CONTROL;
1092 if (aPrefFlags & NS_MODIFIER_ALT) {
1093 result |= MODIFIER_ALT;
1095 if (aPrefFlags & NS_MODIFIER_META) {
1096 result |= MODIFIER_META;
1098 if (aPrefFlags & NS_MODIFIER_OS) {
1099 result |= MODIFIER_OS;
1101 return result;
1104 bool WidgetKeyboardEvent::ModifiersMatchWithAccessKey(
1105 AccessKeyType aType) const {
1106 if (!ModifiersForAccessKeyMatching()) {
1107 return false;
1109 return ModifiersForAccessKeyMatching() == AccessKeyModifiers(aType);
1112 Modifiers WidgetKeyboardEvent::ModifiersForAccessKeyMatching() const {
1113 static const Modifiers kModifierMask = MODIFIER_SHIFT | MODIFIER_CONTROL |
1114 MODIFIER_ALT | MODIFIER_META |
1115 MODIFIER_OS;
1116 return mModifiers & kModifierMask;
1119 /* static */
1120 Modifiers WidgetKeyboardEvent::AccessKeyModifiers(AccessKeyType aType) {
1121 switch (StaticPrefs::ui_key_generalAccessKey()) {
1122 case -1:
1123 break; // use the individual prefs
1124 case NS_VK_SHIFT:
1125 return MODIFIER_SHIFT;
1126 case NS_VK_CONTROL:
1127 return MODIFIER_CONTROL;
1128 case NS_VK_ALT:
1129 return MODIFIER_ALT;
1130 case NS_VK_META:
1131 return MODIFIER_META;
1132 case NS_VK_WIN:
1133 return MODIFIER_OS;
1134 default:
1135 return MODIFIER_NONE;
1138 switch (aType) {
1139 case AccessKeyType::eChrome:
1140 return PrefFlagsToModifiers(StaticPrefs::ui_key_chromeAccess());
1141 case AccessKeyType::eContent:
1142 return PrefFlagsToModifiers(StaticPrefs::ui_key_contentAccess());
1143 default:
1144 return MODIFIER_NONE;
1148 /* static */
1149 void WidgetKeyboardEvent::Shutdown() {
1150 delete sKeyNameIndexHashtable;
1151 sKeyNameIndexHashtable = nullptr;
1152 delete sCodeNameIndexHashtable;
1153 sCodeNameIndexHashtable = nullptr;
1154 // Although sCommandHashtable is not a member of WidgetKeyboardEvent, but
1155 // let's delete it here since we need to do it at same time.
1156 delete sCommandHashtable;
1157 sCommandHashtable = nullptr;
1160 /* static */
1161 void WidgetKeyboardEvent::GetDOMKeyName(KeyNameIndex aKeyNameIndex,
1162 nsAString& aKeyName) {
1163 if (aKeyNameIndex >= KEY_NAME_INDEX_USE_STRING) {
1164 aKeyName.Truncate();
1165 return;
1168 MOZ_RELEASE_ASSERT(
1169 static_cast<size_t>(aKeyNameIndex) < ArrayLength(kKeyNames),
1170 "Illegal key enumeration value");
1171 aKeyName = kKeyNames[aKeyNameIndex];
1174 /* static */
1175 void WidgetKeyboardEvent::GetDOMCodeName(CodeNameIndex aCodeNameIndex,
1176 nsAString& aCodeName) {
1177 if (aCodeNameIndex >= CODE_NAME_INDEX_USE_STRING) {
1178 aCodeName.Truncate();
1179 return;
1182 MOZ_RELEASE_ASSERT(
1183 static_cast<size_t>(aCodeNameIndex) < ArrayLength(kCodeNames),
1184 "Illegal physical code enumeration value");
1186 // Generate some continuous runs of codes, rather than looking them up.
1187 if (aCodeNameIndex >= CODE_NAME_INDEX_KeyA &&
1188 aCodeNameIndex <= CODE_NAME_INDEX_KeyZ) {
1189 uint32_t index = aCodeNameIndex - CODE_NAME_INDEX_KeyA;
1190 aCodeName.AssignLiteral(u"Key");
1191 aCodeName.Append(u'A' + index);
1192 return;
1194 if (aCodeNameIndex >= CODE_NAME_INDEX_Digit0 &&
1195 aCodeNameIndex <= CODE_NAME_INDEX_Digit9) {
1196 uint32_t index = aCodeNameIndex - CODE_NAME_INDEX_Digit0;
1197 aCodeName.AssignLiteral(u"Digit");
1198 aCodeName.AppendInt(index);
1199 return;
1201 if (aCodeNameIndex >= CODE_NAME_INDEX_Numpad0 &&
1202 aCodeNameIndex <= CODE_NAME_INDEX_Numpad9) {
1203 uint32_t index = aCodeNameIndex - CODE_NAME_INDEX_Numpad0;
1204 aCodeName.AssignLiteral(u"Numpad");
1205 aCodeName.AppendInt(index);
1206 return;
1208 if (aCodeNameIndex >= CODE_NAME_INDEX_F1 &&
1209 aCodeNameIndex <= CODE_NAME_INDEX_F24) {
1210 uint32_t index = aCodeNameIndex - CODE_NAME_INDEX_F1;
1211 aCodeName.Assign(u'F');
1212 aCodeName.AppendInt(index + 1);
1213 return;
1216 aCodeName = kCodeNames[aCodeNameIndex];
1219 /* static */
1220 KeyNameIndex WidgetKeyboardEvent::GetKeyNameIndex(const nsAString& aKeyValue) {
1221 if (!sKeyNameIndexHashtable) {
1222 sKeyNameIndexHashtable = new KeyNameIndexHashtable(ArrayLength(kKeyNames));
1223 for (size_t i = 0; i < ArrayLength(kKeyNames); i++) {
1224 sKeyNameIndexHashtable->InsertOrUpdate(nsDependentString(kKeyNames[i]),
1225 static_cast<KeyNameIndex>(i));
1228 return sKeyNameIndexHashtable->MaybeGet(aKeyValue).valueOr(
1229 KEY_NAME_INDEX_USE_STRING);
1232 /* static */
1233 CodeNameIndex WidgetKeyboardEvent::GetCodeNameIndex(
1234 const nsAString& aCodeValue) {
1235 if (!sCodeNameIndexHashtable) {
1236 sCodeNameIndexHashtable =
1237 new CodeNameIndexHashtable(ArrayLength(kCodeNames));
1238 for (size_t i = 0; i < ArrayLength(kCodeNames); i++) {
1239 sCodeNameIndexHashtable->InsertOrUpdate(nsDependentString(kCodeNames[i]),
1240 static_cast<CodeNameIndex>(i));
1243 return sCodeNameIndexHashtable->MaybeGet(aCodeValue)
1244 .valueOr(CODE_NAME_INDEX_USE_STRING);
1247 /* static */
1248 uint32_t WidgetKeyboardEvent::GetFallbackKeyCodeOfPunctuationKey(
1249 CodeNameIndex aCodeNameIndex) {
1250 switch (aCodeNameIndex) {
1251 case CODE_NAME_INDEX_Semicolon: // VK_OEM_1 on Windows
1252 return dom::KeyboardEvent_Binding::DOM_VK_SEMICOLON;
1253 case CODE_NAME_INDEX_Equal: // VK_OEM_PLUS on Windows
1254 return dom::KeyboardEvent_Binding::DOM_VK_EQUALS;
1255 case CODE_NAME_INDEX_Comma: // VK_OEM_COMMA on Windows
1256 return dom::KeyboardEvent_Binding::DOM_VK_COMMA;
1257 case CODE_NAME_INDEX_Minus: // VK_OEM_MINUS on Windows
1258 return dom::KeyboardEvent_Binding::DOM_VK_HYPHEN_MINUS;
1259 case CODE_NAME_INDEX_Period: // VK_OEM_PERIOD on Windows
1260 return dom::KeyboardEvent_Binding::DOM_VK_PERIOD;
1261 case CODE_NAME_INDEX_Slash: // VK_OEM_2 on Windows
1262 return dom::KeyboardEvent_Binding::DOM_VK_SLASH;
1263 case CODE_NAME_INDEX_Backquote: // VK_OEM_3 on Windows
1264 return dom::KeyboardEvent_Binding::DOM_VK_BACK_QUOTE;
1265 case CODE_NAME_INDEX_BracketLeft: // VK_OEM_4 on Windows
1266 return dom::KeyboardEvent_Binding::DOM_VK_OPEN_BRACKET;
1267 case CODE_NAME_INDEX_Backslash: // VK_OEM_5 on Windows
1268 return dom::KeyboardEvent_Binding::DOM_VK_BACK_SLASH;
1269 case CODE_NAME_INDEX_BracketRight: // VK_OEM_6 on Windows
1270 return dom::KeyboardEvent_Binding::DOM_VK_CLOSE_BRACKET;
1271 case CODE_NAME_INDEX_Quote: // VK_OEM_7 on Windows
1272 return dom::KeyboardEvent_Binding::DOM_VK_QUOTE;
1273 case CODE_NAME_INDEX_IntlBackslash: // VK_OEM_5 on Windows (ABNT, etc)
1274 case CODE_NAME_INDEX_IntlYen: // VK_OEM_5 on Windows (JIS)
1275 case CODE_NAME_INDEX_IntlRo: // VK_OEM_102 on Windows
1276 return dom::KeyboardEvent_Binding::DOM_VK_BACK_SLASH;
1277 default:
1278 return 0;
1282 /* static */ const char* WidgetKeyboardEvent::GetCommandStr(Command aCommand) {
1283 #define NS_DEFINE_COMMAND(aName, aCommandStr) , #aCommandStr
1284 #define NS_DEFINE_COMMAND_WITH_PARAM(aName, aCommandStr, aParam) , #aCommandStr
1285 #define NS_DEFINE_COMMAND_NO_EXEC_COMMAND(aName) , ""
1286 static const char* const kCommands[] = {
1287 "" // DoNothing
1288 #include "mozilla/CommandList.h"
1290 #undef NS_DEFINE_COMMAND
1291 #undef NS_DEFINE_COMMAND_WITH_PARAM
1292 #undef NS_DEFINE_COMMAND_NO_EXEC_COMMAND
1294 MOZ_RELEASE_ASSERT(static_cast<size_t>(aCommand) < ArrayLength(kCommands),
1295 "Illegal command enumeration value");
1296 return kCommands[static_cast<CommandInt>(aCommand)];
1299 /* static */
1300 uint32_t WidgetKeyboardEvent::ComputeLocationFromCodeValue(
1301 CodeNameIndex aCodeNameIndex) {
1302 // Following commented out cases are not defined in PhysicalKeyCodeNameList.h
1303 // but are defined by D3E spec. So, they should be uncommented when the
1304 // code values are defined in the header.
1305 switch (aCodeNameIndex) {
1306 case CODE_NAME_INDEX_AltLeft:
1307 case CODE_NAME_INDEX_ControlLeft:
1308 case CODE_NAME_INDEX_OSLeft:
1309 case CODE_NAME_INDEX_ShiftLeft:
1310 return eKeyLocationLeft;
1311 case CODE_NAME_INDEX_AltRight:
1312 case CODE_NAME_INDEX_ControlRight:
1313 case CODE_NAME_INDEX_OSRight:
1314 case CODE_NAME_INDEX_ShiftRight:
1315 return eKeyLocationRight;
1316 case CODE_NAME_INDEX_Numpad0:
1317 case CODE_NAME_INDEX_Numpad1:
1318 case CODE_NAME_INDEX_Numpad2:
1319 case CODE_NAME_INDEX_Numpad3:
1320 case CODE_NAME_INDEX_Numpad4:
1321 case CODE_NAME_INDEX_Numpad5:
1322 case CODE_NAME_INDEX_Numpad6:
1323 case CODE_NAME_INDEX_Numpad7:
1324 case CODE_NAME_INDEX_Numpad8:
1325 case CODE_NAME_INDEX_Numpad9:
1326 case CODE_NAME_INDEX_NumpadAdd:
1327 case CODE_NAME_INDEX_NumpadBackspace:
1328 case CODE_NAME_INDEX_NumpadClear:
1329 case CODE_NAME_INDEX_NumpadClearEntry:
1330 case CODE_NAME_INDEX_NumpadComma:
1331 case CODE_NAME_INDEX_NumpadDecimal:
1332 case CODE_NAME_INDEX_NumpadDivide:
1333 case CODE_NAME_INDEX_NumpadEnter:
1334 case CODE_NAME_INDEX_NumpadEqual:
1335 case CODE_NAME_INDEX_NumpadMemoryAdd:
1336 case CODE_NAME_INDEX_NumpadMemoryClear:
1337 case CODE_NAME_INDEX_NumpadMemoryRecall:
1338 case CODE_NAME_INDEX_NumpadMemoryStore:
1339 case CODE_NAME_INDEX_NumpadMemorySubtract:
1340 case CODE_NAME_INDEX_NumpadMultiply:
1341 case CODE_NAME_INDEX_NumpadParenLeft:
1342 case CODE_NAME_INDEX_NumpadParenRight:
1343 case CODE_NAME_INDEX_NumpadSubtract:
1344 return eKeyLocationNumpad;
1345 default:
1346 return eKeyLocationStandard;
1350 /* static */
1351 uint32_t WidgetKeyboardEvent::ComputeKeyCodeFromKeyNameIndex(
1352 KeyNameIndex aKeyNameIndex) {
1353 switch (aKeyNameIndex) {
1354 case KEY_NAME_INDEX_Cancel:
1355 return dom::KeyboardEvent_Binding::DOM_VK_CANCEL;
1356 case KEY_NAME_INDEX_Help:
1357 return dom::KeyboardEvent_Binding::DOM_VK_HELP;
1358 case KEY_NAME_INDEX_Backspace:
1359 return dom::KeyboardEvent_Binding::DOM_VK_BACK_SPACE;
1360 case KEY_NAME_INDEX_Tab:
1361 return dom::KeyboardEvent_Binding::DOM_VK_TAB;
1362 case KEY_NAME_INDEX_Clear:
1363 return dom::KeyboardEvent_Binding::DOM_VK_CLEAR;
1364 case KEY_NAME_INDEX_Enter:
1365 return dom::KeyboardEvent_Binding::DOM_VK_RETURN;
1366 case KEY_NAME_INDEX_Shift:
1367 return dom::KeyboardEvent_Binding::DOM_VK_SHIFT;
1368 case KEY_NAME_INDEX_Control:
1369 return dom::KeyboardEvent_Binding::DOM_VK_CONTROL;
1370 case KEY_NAME_INDEX_Alt:
1371 return dom::KeyboardEvent_Binding::DOM_VK_ALT;
1372 case KEY_NAME_INDEX_Pause:
1373 return dom::KeyboardEvent_Binding::DOM_VK_PAUSE;
1374 case KEY_NAME_INDEX_CapsLock:
1375 return dom::KeyboardEvent_Binding::DOM_VK_CAPS_LOCK;
1376 case KEY_NAME_INDEX_Hiragana:
1377 case KEY_NAME_INDEX_Katakana:
1378 case KEY_NAME_INDEX_HiraganaKatakana:
1379 case KEY_NAME_INDEX_KanaMode:
1380 return dom::KeyboardEvent_Binding::DOM_VK_KANA;
1381 case KEY_NAME_INDEX_HangulMode:
1382 return dom::KeyboardEvent_Binding::DOM_VK_HANGUL;
1383 case KEY_NAME_INDEX_Eisu:
1384 return dom::KeyboardEvent_Binding::DOM_VK_EISU;
1385 case KEY_NAME_INDEX_JunjaMode:
1386 return dom::KeyboardEvent_Binding::DOM_VK_JUNJA;
1387 case KEY_NAME_INDEX_FinalMode:
1388 return dom::KeyboardEvent_Binding::DOM_VK_FINAL;
1389 case KEY_NAME_INDEX_HanjaMode:
1390 return dom::KeyboardEvent_Binding::DOM_VK_HANJA;
1391 case KEY_NAME_INDEX_KanjiMode:
1392 return dom::KeyboardEvent_Binding::DOM_VK_KANJI;
1393 case KEY_NAME_INDEX_Escape:
1394 return dom::KeyboardEvent_Binding::DOM_VK_ESCAPE;
1395 case KEY_NAME_INDEX_Convert:
1396 return dom::KeyboardEvent_Binding::DOM_VK_CONVERT;
1397 case KEY_NAME_INDEX_NonConvert:
1398 return dom::KeyboardEvent_Binding::DOM_VK_NONCONVERT;
1399 case KEY_NAME_INDEX_Accept:
1400 return dom::KeyboardEvent_Binding::DOM_VK_ACCEPT;
1401 case KEY_NAME_INDEX_ModeChange:
1402 return dom::KeyboardEvent_Binding::DOM_VK_MODECHANGE;
1403 case KEY_NAME_INDEX_PageUp:
1404 return dom::KeyboardEvent_Binding::DOM_VK_PAGE_UP;
1405 case KEY_NAME_INDEX_PageDown:
1406 return dom::KeyboardEvent_Binding::DOM_VK_PAGE_DOWN;
1407 case KEY_NAME_INDEX_End:
1408 return dom::KeyboardEvent_Binding::DOM_VK_END;
1409 case KEY_NAME_INDEX_Home:
1410 return dom::KeyboardEvent_Binding::DOM_VK_HOME;
1411 case KEY_NAME_INDEX_ArrowLeft:
1412 return dom::KeyboardEvent_Binding::DOM_VK_LEFT;
1413 case KEY_NAME_INDEX_ArrowUp:
1414 return dom::KeyboardEvent_Binding::DOM_VK_UP;
1415 case KEY_NAME_INDEX_ArrowRight:
1416 return dom::KeyboardEvent_Binding::DOM_VK_RIGHT;
1417 case KEY_NAME_INDEX_ArrowDown:
1418 return dom::KeyboardEvent_Binding::DOM_VK_DOWN;
1419 case KEY_NAME_INDEX_Select:
1420 return dom::KeyboardEvent_Binding::DOM_VK_SELECT;
1421 case KEY_NAME_INDEX_Print:
1422 return dom::KeyboardEvent_Binding::DOM_VK_PRINT;
1423 case KEY_NAME_INDEX_Execute:
1424 return dom::KeyboardEvent_Binding::DOM_VK_EXECUTE;
1425 case KEY_NAME_INDEX_PrintScreen:
1426 return dom::KeyboardEvent_Binding::DOM_VK_PRINTSCREEN;
1427 case KEY_NAME_INDEX_Insert:
1428 return dom::KeyboardEvent_Binding::DOM_VK_INSERT;
1429 case KEY_NAME_INDEX_Delete:
1430 return dom::KeyboardEvent_Binding::DOM_VK_DELETE;
1431 case KEY_NAME_INDEX_OS:
1432 // case KEY_NAME_INDEX_Super:
1433 // case KEY_NAME_INDEX_Hyper:
1434 return dom::KeyboardEvent_Binding::DOM_VK_WIN;
1435 case KEY_NAME_INDEX_ContextMenu:
1436 return dom::KeyboardEvent_Binding::DOM_VK_CONTEXT_MENU;
1437 case KEY_NAME_INDEX_Standby:
1438 return dom::KeyboardEvent_Binding::DOM_VK_SLEEP;
1439 case KEY_NAME_INDEX_F1:
1440 return dom::KeyboardEvent_Binding::DOM_VK_F1;
1441 case KEY_NAME_INDEX_F2:
1442 return dom::KeyboardEvent_Binding::DOM_VK_F2;
1443 case KEY_NAME_INDEX_F3:
1444 return dom::KeyboardEvent_Binding::DOM_VK_F3;
1445 case KEY_NAME_INDEX_F4:
1446 return dom::KeyboardEvent_Binding::DOM_VK_F4;
1447 case KEY_NAME_INDEX_F5:
1448 return dom::KeyboardEvent_Binding::DOM_VK_F5;
1449 case KEY_NAME_INDEX_F6:
1450 return dom::KeyboardEvent_Binding::DOM_VK_F6;
1451 case KEY_NAME_INDEX_F7:
1452 return dom::KeyboardEvent_Binding::DOM_VK_F7;
1453 case KEY_NAME_INDEX_F8:
1454 return dom::KeyboardEvent_Binding::DOM_VK_F8;
1455 case KEY_NAME_INDEX_F9:
1456 return dom::KeyboardEvent_Binding::DOM_VK_F9;
1457 case KEY_NAME_INDEX_F10:
1458 return dom::KeyboardEvent_Binding::DOM_VK_F10;
1459 case KEY_NAME_INDEX_F11:
1460 return dom::KeyboardEvent_Binding::DOM_VK_F11;
1461 case KEY_NAME_INDEX_F12:
1462 return dom::KeyboardEvent_Binding::DOM_VK_F12;
1463 case KEY_NAME_INDEX_F13:
1464 return dom::KeyboardEvent_Binding::DOM_VK_F13;
1465 case KEY_NAME_INDEX_F14:
1466 return dom::KeyboardEvent_Binding::DOM_VK_F14;
1467 case KEY_NAME_INDEX_F15:
1468 return dom::KeyboardEvent_Binding::DOM_VK_F15;
1469 case KEY_NAME_INDEX_F16:
1470 return dom::KeyboardEvent_Binding::DOM_VK_F16;
1471 case KEY_NAME_INDEX_F17:
1472 return dom::KeyboardEvent_Binding::DOM_VK_F17;
1473 case KEY_NAME_INDEX_F18:
1474 return dom::KeyboardEvent_Binding::DOM_VK_F18;
1475 case KEY_NAME_INDEX_F19:
1476 return dom::KeyboardEvent_Binding::DOM_VK_F19;
1477 case KEY_NAME_INDEX_F20:
1478 return dom::KeyboardEvent_Binding::DOM_VK_F20;
1479 case KEY_NAME_INDEX_F21:
1480 return dom::KeyboardEvent_Binding::DOM_VK_F21;
1481 case KEY_NAME_INDEX_F22:
1482 return dom::KeyboardEvent_Binding::DOM_VK_F22;
1483 case KEY_NAME_INDEX_F23:
1484 return dom::KeyboardEvent_Binding::DOM_VK_F23;
1485 case KEY_NAME_INDEX_F24:
1486 return dom::KeyboardEvent_Binding::DOM_VK_F24;
1487 case KEY_NAME_INDEX_NumLock:
1488 return dom::KeyboardEvent_Binding::DOM_VK_NUM_LOCK;
1489 case KEY_NAME_INDEX_ScrollLock:
1490 return dom::KeyboardEvent_Binding::DOM_VK_SCROLL_LOCK;
1491 case KEY_NAME_INDEX_AudioVolumeMute:
1492 return dom::KeyboardEvent_Binding::DOM_VK_VOLUME_MUTE;
1493 case KEY_NAME_INDEX_AudioVolumeDown:
1494 return dom::KeyboardEvent_Binding::DOM_VK_VOLUME_DOWN;
1495 case KEY_NAME_INDEX_AudioVolumeUp:
1496 return dom::KeyboardEvent_Binding::DOM_VK_VOLUME_UP;
1497 case KEY_NAME_INDEX_Meta:
1498 return dom::KeyboardEvent_Binding::DOM_VK_META;
1499 case KEY_NAME_INDEX_AltGraph:
1500 return dom::KeyboardEvent_Binding::DOM_VK_ALTGR;
1501 case KEY_NAME_INDEX_Process:
1502 return dom::KeyboardEvent_Binding::DOM_VK_PROCESSKEY;
1503 case KEY_NAME_INDEX_Attn:
1504 return dom::KeyboardEvent_Binding::DOM_VK_ATTN;
1505 case KEY_NAME_INDEX_CrSel:
1506 return dom::KeyboardEvent_Binding::DOM_VK_CRSEL;
1507 case KEY_NAME_INDEX_ExSel:
1508 return dom::KeyboardEvent_Binding::DOM_VK_EXSEL;
1509 case KEY_NAME_INDEX_EraseEof:
1510 return dom::KeyboardEvent_Binding::DOM_VK_EREOF;
1511 case KEY_NAME_INDEX_Play:
1512 return dom::KeyboardEvent_Binding::DOM_VK_PLAY;
1513 case KEY_NAME_INDEX_ZoomToggle:
1514 case KEY_NAME_INDEX_ZoomIn:
1515 case KEY_NAME_INDEX_ZoomOut:
1516 return dom::KeyboardEvent_Binding::DOM_VK_ZOOM;
1517 default:
1518 return 0;
1522 /* static */
1523 CodeNameIndex WidgetKeyboardEvent::ComputeCodeNameIndexFromKeyNameIndex(
1524 KeyNameIndex aKeyNameIndex, const Maybe<uint32_t>& aLocation) {
1525 if (aLocation.isSome() &&
1526 aLocation.value() ==
1527 dom::KeyboardEvent_Binding::DOM_KEY_LOCATION_NUMPAD) {
1528 // On macOS, NumLock is not supported. Therefore, this handles
1529 // control key values except "Enter" only on non-macOS platforms.
1530 switch (aKeyNameIndex) {
1531 #ifndef XP_MACOSX
1532 case KEY_NAME_INDEX_Insert:
1533 return CODE_NAME_INDEX_Numpad0;
1534 case KEY_NAME_INDEX_End:
1535 return CODE_NAME_INDEX_Numpad1;
1536 case KEY_NAME_INDEX_ArrowDown:
1537 return CODE_NAME_INDEX_Numpad2;
1538 case KEY_NAME_INDEX_PageDown:
1539 return CODE_NAME_INDEX_Numpad3;
1540 case KEY_NAME_INDEX_ArrowLeft:
1541 return CODE_NAME_INDEX_Numpad4;
1542 case KEY_NAME_INDEX_Clear:
1543 // FYI: "Clear" on macOS should be DOM_KEY_LOCATION_STANDARD.
1544 return CODE_NAME_INDEX_Numpad5;
1545 case KEY_NAME_INDEX_ArrowRight:
1546 return CODE_NAME_INDEX_Numpad6;
1547 case KEY_NAME_INDEX_Home:
1548 return CODE_NAME_INDEX_Numpad7;
1549 case KEY_NAME_INDEX_ArrowUp:
1550 return CODE_NAME_INDEX_Numpad8;
1551 case KEY_NAME_INDEX_PageUp:
1552 return CODE_NAME_INDEX_Numpad9;
1553 case KEY_NAME_INDEX_Delete:
1554 return CODE_NAME_INDEX_NumpadDecimal;
1555 #endif // #ifndef XP_MACOSX
1556 case KEY_NAME_INDEX_Enter:
1557 return CODE_NAME_INDEX_NumpadEnter;
1558 default:
1559 return CODE_NAME_INDEX_UNKNOWN;
1563 if (WidgetKeyboardEvent::IsLeftOrRightModiferKeyNameIndex(aKeyNameIndex)) {
1564 if (aLocation.isSome() &&
1565 (aLocation.value() !=
1566 dom::KeyboardEvent_Binding::DOM_KEY_LOCATION_LEFT &&
1567 aLocation.value() !=
1568 dom::KeyboardEvent_Binding::DOM_KEY_LOCATION_RIGHT)) {
1569 return CODE_NAME_INDEX_UNKNOWN;
1571 bool isRight =
1572 aLocation.isSome() &&
1573 aLocation.value() == dom::KeyboardEvent_Binding::DOM_KEY_LOCATION_RIGHT;
1574 switch (aKeyNameIndex) {
1575 case KEY_NAME_INDEX_Alt:
1576 return isRight ? CODE_NAME_INDEX_AltRight : CODE_NAME_INDEX_AltLeft;
1577 case KEY_NAME_INDEX_Control:
1578 return isRight ? CODE_NAME_INDEX_ControlRight
1579 : CODE_NAME_INDEX_ControlLeft;
1580 case KEY_NAME_INDEX_Shift:
1581 return isRight ? CODE_NAME_INDEX_ShiftRight : CODE_NAME_INDEX_ShiftLeft;
1582 #if defined(XP_WIN)
1583 case KEY_NAME_INDEX_Meta:
1584 return CODE_NAME_INDEX_UNKNOWN;
1585 case KEY_NAME_INDEX_OS: // win key.
1586 return isRight ? CODE_NAME_INDEX_OSRight : CODE_NAME_INDEX_OSLeft;
1587 #elif defined(XP_MACOSX) || defined(ANDROID)
1588 case KEY_NAME_INDEX_Meta: // command key.
1589 return isRight ? CODE_NAME_INDEX_OSRight : CODE_NAME_INDEX_OSLeft;
1590 case KEY_NAME_INDEX_OS:
1591 return CODE_NAME_INDEX_UNKNOWN;
1592 #else
1593 case KEY_NAME_INDEX_Meta: // Alt + Shift.
1594 return isRight ? CODE_NAME_INDEX_AltRight : CODE_NAME_INDEX_AltLeft;
1595 case KEY_NAME_INDEX_OS: // Super/Hyper key.
1596 return isRight ? CODE_NAME_INDEX_OSRight : CODE_NAME_INDEX_OSLeft;
1597 #endif
1598 default:
1599 return CODE_NAME_INDEX_UNKNOWN;
1603 if (aLocation.isSome() &&
1604 aLocation.value() !=
1605 dom::KeyboardEvent_Binding::DOM_KEY_LOCATION_STANDARD) {
1606 return CODE_NAME_INDEX_UNKNOWN;
1609 switch (aKeyNameIndex) {
1610 // Standard section:
1611 case KEY_NAME_INDEX_Escape:
1612 return CODE_NAME_INDEX_Escape;
1613 case KEY_NAME_INDEX_Tab:
1614 return CODE_NAME_INDEX_Tab;
1615 case KEY_NAME_INDEX_CapsLock:
1616 return CODE_NAME_INDEX_CapsLock;
1617 case KEY_NAME_INDEX_ContextMenu:
1618 return CODE_NAME_INDEX_ContextMenu;
1619 case KEY_NAME_INDEX_Backspace:
1620 return CODE_NAME_INDEX_Backspace;
1621 case KEY_NAME_INDEX_Enter:
1622 return CODE_NAME_INDEX_Enter;
1623 #ifdef XP_MACOSX
1624 // Although, macOS does not fire native key event of "Fn" key, we support
1625 // Fn key event if it's sent by other apps directly.
1626 case KEY_NAME_INDEX_Fn:
1627 return CODE_NAME_INDEX_Fn;
1628 #endif // #ifdef
1630 // Arrow Pad section:
1631 case KEY_NAME_INDEX_ArrowLeft:
1632 return CODE_NAME_INDEX_ArrowLeft;
1633 case KEY_NAME_INDEX_ArrowUp:
1634 return CODE_NAME_INDEX_ArrowUp;
1635 case KEY_NAME_INDEX_ArrowDown:
1636 return CODE_NAME_INDEX_ArrowDown;
1637 case KEY_NAME_INDEX_ArrowRight:
1638 return CODE_NAME_INDEX_ArrowRight;
1640 // Control Pad section:
1641 #ifndef XP_MACOSX
1642 case KEY_NAME_INDEX_Insert:
1643 return CODE_NAME_INDEX_Insert;
1644 #else
1645 case KEY_NAME_INDEX_Help:
1646 return CODE_NAME_INDEX_Help;
1647 #endif // #ifndef XP_MACOSX #else
1648 case KEY_NAME_INDEX_Delete:
1649 return CODE_NAME_INDEX_Delete;
1650 case KEY_NAME_INDEX_Home:
1651 return CODE_NAME_INDEX_Home;
1652 case KEY_NAME_INDEX_End:
1653 return CODE_NAME_INDEX_End;
1654 case KEY_NAME_INDEX_PageUp:
1655 return CODE_NAME_INDEX_PageUp;
1656 case KEY_NAME_INDEX_PageDown:
1657 return CODE_NAME_INDEX_PageDown;
1659 // Function keys:
1660 case KEY_NAME_INDEX_F1:
1661 return CODE_NAME_INDEX_F1;
1662 case KEY_NAME_INDEX_F2:
1663 return CODE_NAME_INDEX_F2;
1664 case KEY_NAME_INDEX_F3:
1665 return CODE_NAME_INDEX_F3;
1666 case KEY_NAME_INDEX_F4:
1667 return CODE_NAME_INDEX_F4;
1668 case KEY_NAME_INDEX_F5:
1669 return CODE_NAME_INDEX_F5;
1670 case KEY_NAME_INDEX_F6:
1671 return CODE_NAME_INDEX_F6;
1672 case KEY_NAME_INDEX_F7:
1673 return CODE_NAME_INDEX_F7;
1674 case KEY_NAME_INDEX_F8:
1675 return CODE_NAME_INDEX_F8;
1676 case KEY_NAME_INDEX_F9:
1677 return CODE_NAME_INDEX_F9;
1678 case KEY_NAME_INDEX_F10:
1679 return CODE_NAME_INDEX_F10;
1680 case KEY_NAME_INDEX_F11:
1681 return CODE_NAME_INDEX_F11;
1682 case KEY_NAME_INDEX_F12:
1683 return CODE_NAME_INDEX_F12;
1684 case KEY_NAME_INDEX_F13:
1685 return CODE_NAME_INDEX_F13;
1686 case KEY_NAME_INDEX_F14:
1687 return CODE_NAME_INDEX_F14;
1688 case KEY_NAME_INDEX_F15:
1689 return CODE_NAME_INDEX_F15;
1690 case KEY_NAME_INDEX_F16:
1691 return CODE_NAME_INDEX_F16;
1692 case KEY_NAME_INDEX_F17:
1693 return CODE_NAME_INDEX_F17;
1694 case KEY_NAME_INDEX_F18:
1695 return CODE_NAME_INDEX_F18;
1696 case KEY_NAME_INDEX_F19:
1697 return CODE_NAME_INDEX_F19;
1698 case KEY_NAME_INDEX_F20:
1699 return CODE_NAME_INDEX_F20;
1700 #ifndef XP_MACOSX
1701 case KEY_NAME_INDEX_F21:
1702 return CODE_NAME_INDEX_F21;
1703 case KEY_NAME_INDEX_F22:
1704 return CODE_NAME_INDEX_F22;
1705 case KEY_NAME_INDEX_F23:
1706 return CODE_NAME_INDEX_F23;
1707 case KEY_NAME_INDEX_F24:
1708 return CODE_NAME_INDEX_F24;
1709 case KEY_NAME_INDEX_Pause:
1710 return CODE_NAME_INDEX_Pause;
1711 case KEY_NAME_INDEX_PrintScreen:
1712 return CODE_NAME_INDEX_PrintScreen;
1713 case KEY_NAME_INDEX_ScrollLock:
1714 return CODE_NAME_INDEX_ScrollLock;
1715 #endif // #ifndef XP_MACOSX
1717 // NumLock key:
1718 #ifndef XP_MACOSX
1719 case KEY_NAME_INDEX_NumLock:
1720 return CODE_NAME_INDEX_NumLock;
1721 #else
1722 case KEY_NAME_INDEX_Clear:
1723 return CODE_NAME_INDEX_NumLock;
1724 #endif // #ifndef XP_MACOSX #else
1726 // Media keys:
1727 case KEY_NAME_INDEX_AudioVolumeDown:
1728 return CODE_NAME_INDEX_VolumeDown;
1729 case KEY_NAME_INDEX_AudioVolumeMute:
1730 return CODE_NAME_INDEX_VolumeMute;
1731 case KEY_NAME_INDEX_AudioVolumeUp:
1732 return CODE_NAME_INDEX_VolumeUp;
1733 #ifndef XP_MACOSX
1734 case KEY_NAME_INDEX_BrowserBack:
1735 return CODE_NAME_INDEX_BrowserBack;
1736 case KEY_NAME_INDEX_BrowserFavorites:
1737 return CODE_NAME_INDEX_BrowserFavorites;
1738 case KEY_NAME_INDEX_BrowserForward:
1739 return CODE_NAME_INDEX_BrowserForward;
1740 case KEY_NAME_INDEX_BrowserRefresh:
1741 return CODE_NAME_INDEX_BrowserRefresh;
1742 case KEY_NAME_INDEX_BrowserSearch:
1743 return CODE_NAME_INDEX_BrowserSearch;
1744 case KEY_NAME_INDEX_BrowserStop:
1745 return CODE_NAME_INDEX_BrowserStop;
1746 case KEY_NAME_INDEX_MediaPlayPause:
1747 return CODE_NAME_INDEX_MediaPlayPause;
1748 case KEY_NAME_INDEX_MediaStop:
1749 return CODE_NAME_INDEX_MediaStop;
1750 case KEY_NAME_INDEX_MediaTrackNext:
1751 return CODE_NAME_INDEX_MediaTrackNext;
1752 case KEY_NAME_INDEX_MediaTrackPrevious:
1753 return CODE_NAME_INDEX_MediaTrackPrevious;
1754 case KEY_NAME_INDEX_LaunchApplication1:
1755 return CODE_NAME_INDEX_LaunchApp1;
1756 #endif // #ifndef XP_MACOSX
1758 // Only Windows and GTK supports the following multimedia keys.
1759 #if defined(XP_WIN) || defined(MOZ_WIDGET_GTK)
1760 case KEY_NAME_INDEX_BrowserHome:
1761 return CODE_NAME_INDEX_BrowserHome;
1762 case KEY_NAME_INDEX_LaunchApplication2:
1763 return CODE_NAME_INDEX_LaunchApp2;
1764 #endif // #if defined(XP_WIN) || defined(MOZ_WIDGET_GTK)
1766 // Only GTK and Android supports the following multimedia keys.
1767 #if defined(MOZ_WIDGET_GTK) || defined(ANDROID)
1768 case KEY_NAME_INDEX_Eject:
1769 return CODE_NAME_INDEX_Eject;
1770 case KEY_NAME_INDEX_WakeUp:
1771 return CODE_NAME_INDEX_WakeUp;
1772 #endif // #if defined(MOZ_WIDGET_GTK) || defined(ANDROID)
1774 // Only Windows does not support Help key (and macOS handled above).
1775 #if !defined(XP_WIN) && !defined(XP_MACOSX)
1776 case KEY_NAME_INDEX_Help:
1777 return CODE_NAME_INDEX_Help;
1778 #endif // #if !defined(XP_WIN) && !defined(XP_MACOSX)
1780 // IME specific keys:
1781 #ifdef XP_WIN
1782 case KEY_NAME_INDEX_Convert:
1783 return CODE_NAME_INDEX_Convert;
1784 case KEY_NAME_INDEX_NonConvert:
1785 return CODE_NAME_INDEX_NonConvert;
1786 case KEY_NAME_INDEX_Alphanumeric:
1787 return CODE_NAME_INDEX_CapsLock;
1788 case KEY_NAME_INDEX_KanaMode:
1789 case KEY_NAME_INDEX_Romaji:
1790 case KEY_NAME_INDEX_Katakana:
1791 case KEY_NAME_INDEX_Hiragana:
1792 return CODE_NAME_INDEX_KanaMode;
1793 case KEY_NAME_INDEX_Hankaku:
1794 case KEY_NAME_INDEX_Zenkaku:
1795 case KEY_NAME_INDEX_KanjiMode:
1796 return CODE_NAME_INDEX_Backquote;
1797 case KEY_NAME_INDEX_HanjaMode:
1798 return CODE_NAME_INDEX_Lang2;
1799 case KEY_NAME_INDEX_HangulMode:
1800 return CODE_NAME_INDEX_Lang1;
1801 #endif // #ifdef XP_WIN
1803 #ifdef MOZ_WIDGET_GTK
1804 case KEY_NAME_INDEX_Convert:
1805 return CODE_NAME_INDEX_Convert;
1806 case KEY_NAME_INDEX_NonConvert:
1807 return CODE_NAME_INDEX_NonConvert;
1808 case KEY_NAME_INDEX_Alphanumeric:
1809 return CODE_NAME_INDEX_CapsLock;
1810 case KEY_NAME_INDEX_HiraganaKatakana:
1811 return CODE_NAME_INDEX_KanaMode;
1812 case KEY_NAME_INDEX_ZenkakuHankaku:
1813 return CODE_NAME_INDEX_Backquote;
1814 #endif // #ifdef MOZ_WIDGET_GTK
1816 #ifdef ANDROID
1817 case KEY_NAME_INDEX_Convert:
1818 return CODE_NAME_INDEX_Convert;
1819 case KEY_NAME_INDEX_NonConvert:
1820 return CODE_NAME_INDEX_NonConvert;
1821 case KEY_NAME_INDEX_HiraganaKatakana:
1822 return CODE_NAME_INDEX_KanaMode;
1823 case KEY_NAME_INDEX_ZenkakuHankaku:
1824 return CODE_NAME_INDEX_Backquote;
1825 case KEY_NAME_INDEX_Eisu:
1826 return CODE_NAME_INDEX_Lang2;
1827 case KEY_NAME_INDEX_KanjiMode:
1828 return CODE_NAME_INDEX_Lang1;
1829 #endif // #ifdef ANDROID
1831 #ifdef XP_MACOSX
1832 case KEY_NAME_INDEX_Eisu:
1833 return CODE_NAME_INDEX_Lang2;
1834 case KEY_NAME_INDEX_KanjiMode:
1835 return CODE_NAME_INDEX_Lang1;
1836 #endif // #ifdef XP_MACOSX
1838 default:
1839 return CODE_NAME_INDEX_UNKNOWN;
1843 /* static */
1844 Modifier WidgetKeyboardEvent::GetModifierForKeyName(
1845 KeyNameIndex aKeyNameIndex) {
1846 switch (aKeyNameIndex) {
1847 case KEY_NAME_INDEX_Alt:
1848 return MODIFIER_ALT;
1849 case KEY_NAME_INDEX_AltGraph:
1850 return MODIFIER_ALTGRAPH;
1851 case KEY_NAME_INDEX_CapsLock:
1852 return MODIFIER_CAPSLOCK;
1853 case KEY_NAME_INDEX_Control:
1854 return MODIFIER_CONTROL;
1855 case KEY_NAME_INDEX_Fn:
1856 return MODIFIER_FN;
1857 case KEY_NAME_INDEX_FnLock:
1858 return MODIFIER_FNLOCK;
1859 // case KEY_NAME_INDEX_Hyper:
1860 case KEY_NAME_INDEX_Meta:
1861 return MODIFIER_META;
1862 case KEY_NAME_INDEX_NumLock:
1863 return MODIFIER_NUMLOCK;
1864 case KEY_NAME_INDEX_OS:
1865 return MODIFIER_OS;
1866 case KEY_NAME_INDEX_ScrollLock:
1867 return MODIFIER_SCROLLLOCK;
1868 case KEY_NAME_INDEX_Shift:
1869 return MODIFIER_SHIFT;
1870 // case KEY_NAME_INDEX_Super:
1871 case KEY_NAME_INDEX_Symbol:
1872 return MODIFIER_SYMBOL;
1873 case KEY_NAME_INDEX_SymbolLock:
1874 return MODIFIER_SYMBOLLOCK;
1875 default:
1876 return MODIFIER_NONE;
1880 /* static */
1881 bool WidgetKeyboardEvent::IsLockableModifier(KeyNameIndex aKeyNameIndex) {
1882 switch (aKeyNameIndex) {
1883 case KEY_NAME_INDEX_CapsLock:
1884 case KEY_NAME_INDEX_FnLock:
1885 case KEY_NAME_INDEX_NumLock:
1886 case KEY_NAME_INDEX_ScrollLock:
1887 case KEY_NAME_INDEX_SymbolLock:
1888 return true;
1889 default:
1890 return false;
1894 /******************************************************************************
1895 * mozilla::InternalEditorInputEvent (TextEvents.h)
1896 ******************************************************************************/
1898 #define NS_DEFINE_INPUTTYPE(aCPPName, aDOMName) (u"" aDOMName),
1899 const char16_t* const InternalEditorInputEvent::kInputTypeNames[] = {
1900 #include "mozilla/InputTypeList.h"
1902 #undef NS_DEFINE_INPUTTYPE
1904 InternalEditorInputEvent::InputTypeHashtable*
1905 InternalEditorInputEvent::sInputTypeHashtable = nullptr;
1907 /* static */
1908 void InternalEditorInputEvent::Shutdown() {
1909 delete sInputTypeHashtable;
1910 sInputTypeHashtable = nullptr;
1913 /* static */
1914 void InternalEditorInputEvent::GetDOMInputTypeName(EditorInputType aInputType,
1915 nsAString& aInputTypeName) {
1916 if (static_cast<size_t>(aInputType) >=
1917 static_cast<size_t>(EditorInputType::eUnknown)) {
1918 aInputTypeName.Truncate();
1919 return;
1922 MOZ_RELEASE_ASSERT(
1923 static_cast<size_t>(aInputType) < ArrayLength(kInputTypeNames),
1924 "Illegal input type enumeration value");
1925 aInputTypeName.Assign(kInputTypeNames[static_cast<size_t>(aInputType)]);
1928 /* static */
1929 EditorInputType InternalEditorInputEvent::GetEditorInputType(
1930 const nsAString& aInputType) {
1931 if (aInputType.IsEmpty()) {
1932 return EditorInputType::eUnknown;
1935 if (!sInputTypeHashtable) {
1936 sInputTypeHashtable = new InputTypeHashtable(ArrayLength(kInputTypeNames));
1937 for (size_t i = 0; i < ArrayLength(kInputTypeNames); i++) {
1938 sInputTypeHashtable->InsertOrUpdate(nsDependentString(kInputTypeNames[i]),
1939 static_cast<EditorInputType>(i));
1942 return sInputTypeHashtable->MaybeGet(aInputType)
1943 .valueOr(EditorInputType::eUnknown);
1946 } // namespace mozilla