1/*
2 * Copyright (C) 2004, 2005, 2006 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Collabora, Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "KeypressCommand.h"
30#include "PlatformEvent.h"
31#include <wtf/WindowsExtras.h>
32#include <wtf/text/WTFString.h>
33
34#if PLATFORM(COCOA)
35#include <wtf/RetainPtr.h>
36OBJC_CLASS NSEvent;
37#endif
38
39#if PLATFORM(IOS_FAMILY)
40OBJC_CLASS WebEvent;
41#endif
42
43#if PLATFORM(GTK)
44typedef struct _GdkEventKey GdkEventKey;
45#include "CompositionResults.h"
46#endif
47
48namespace WebCore {
49
50 class PlatformKeyboardEvent : public PlatformEvent {
51 WTF_MAKE_FAST_ALLOCATED;
52 public:
53 PlatformKeyboardEvent()
54 : PlatformEvent(PlatformEvent::KeyDown)
55 , m_windowsVirtualKeyCode(0)
56 , m_autoRepeat(false)
57 , m_isKeypad(false)
58 , m_isSystemKey(false)
59#if PLATFORM(GTK)
60 , m_gdkEventKey(0)
61#endif
62 {
63 }
64
65 PlatformKeyboardEvent(Type type, const String& text, const String& unmodifiedText,
66#if ENABLE(KEYBOARD_KEY_ATTRIBUTE)
67 const String& key,
68#endif
69#if ENABLE(KEYBOARD_CODE_ATTRIBUTE)
70 const String& code,
71#endif
72 const String& keyIdentifier, int windowsVirtualKeyCode, bool isAutoRepeat, bool isKeypad, bool isSystemKey, OptionSet<Modifier> modifiers, WallTime timestamp)
73 : PlatformEvent(type, modifiers, timestamp)
74 , m_text(text)
75 , m_unmodifiedText(unmodifiedText)
76#if ENABLE(KEYBOARD_KEY_ATTRIBUTE)
77 , m_key(key)
78#endif
79#if ENABLE(KEYBOARD_CODE_ATTRIBUTE)
80 , m_code(code)
81#endif
82 , m_keyIdentifier(keyIdentifier)
83 , m_windowsVirtualKeyCode(windowsVirtualKeyCode)
84 , m_autoRepeat(isAutoRepeat)
85 , m_isKeypad(isKeypad)
86 , m_isSystemKey(isSystemKey)
87 {
88 }
89
90 WEBCORE_EXPORT void disambiguateKeyDownEvent(Type, bool backwardCompatibilityMode = false); // Only used on platforms that need it, i.e. those that generate KeyDown events.
91
92 // Text as as generated by processing a virtual key code with a keyboard layout
93 // (in most cases, just a character code, but the layout can emit several
94 // characters in a single keypress event on some platforms).
95 // This may bear no resemblance to the ultimately inserted text if an input method
96 // processes the input.
97 // Will be null for KeyUp and RawKeyDown events.
98 String text() const { return m_text; }
99
100 // Text that would have been generated by the keyboard if no modifiers were pressed
101 // (except for Shift); useful for shortcut (accelerator) key handling.
102 // Otherwise, same as text().
103 String unmodifiedText() const { return m_unmodifiedText; }
104
105 String keyIdentifier() const { return m_keyIdentifier; }
106
107#if ENABLE(KEYBOARD_KEY_ATTRIBUTE)
108 const String& key() const { return m_key; }
109#endif
110#if ENABLE(KEYBOARD_CODE_ATTRIBUTE)
111 const String& code() const { return m_code; }
112#endif
113
114 // Most compatible Windows virtual key code associated with the event.
115 // Zero for Char events.
116 int windowsVirtualKeyCode() const { return m_windowsVirtualKeyCode; }
117 void setWindowsVirtualKeyCode(int code) { m_windowsVirtualKeyCode = code; }
118
119#if USE(APPKIT) || USE(UIKIT_KEYBOARD_ADDITIONS) || PLATFORM(GTK)
120 bool handledByInputMethod() const { return m_handledByInputMethod; }
121#endif
122#if USE(APPKIT)
123 const Vector<KeypressCommand>& commands() const { return m_commands; }
124#elif PLATFORM(GTK)
125 const Vector<String>& commands() const { return m_commands; }
126#endif
127
128 bool isAutoRepeat() const { return m_autoRepeat; }
129 bool isKeypad() const { return m_isKeypad; }
130 bool isSystemKey() const { return m_isSystemKey; }
131
132 bool isSyntheticEvent() const { return m_isSyntheticEvent; }
133 void setIsSyntheticEvent() { m_isSyntheticEvent = true; }
134
135 WEBCORE_EXPORT static bool currentCapsLockState();
136 WEBCORE_EXPORT static void getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey);
137 WEBCORE_EXPORT static void setCurrentModifierState(OptionSet<Modifier>);
138 WEBCORE_EXPORT static OptionSet<Modifier> currentStateOfModifierKeys();
139
140#if PLATFORM(COCOA)
141#if !PLATFORM(IOS_FAMILY)
142 NSEvent* macEvent() const { return m_macEvent.get(); }
143#else
144 ::WebEvent *event() const { return m_Event.get(); }
145#endif
146#endif
147
148#if PLATFORM(WIN)
149 PlatformKeyboardEvent(HWND, WPARAM, LPARAM, Type, bool);
150#endif
151
152#if PLATFORM(GTK)
153 PlatformKeyboardEvent(GdkEventKey*, const CompositionResults&);
154 GdkEventKey* gdkEventKey() const { return m_gdkEventKey; }
155 const CompositionResults& compositionResults() const { return m_compositionResults; }
156
157 // Used by WebKit2
158 static String keyValueForGdkKeyCode(unsigned);
159 static String keyCodeForHardwareKeyCode(unsigned);
160 static String keyIdentifierForGdkKeyCode(unsigned);
161 static int windowsKeyCodeForGdkKeyCode(unsigned);
162 static String singleCharacterString(unsigned);
163 static bool modifiersContainCapsLock(unsigned);
164#endif
165
166#if USE(LIBWPE)
167 static String keyValueForWPEKeyCode(unsigned);
168 static String keyCodeForHardwareKeyCode(unsigned);
169 static String keyIdentifierForWPEKeyCode(unsigned);
170 static int windowsKeyCodeForWPEKeyCode(unsigned);
171 static String singleCharacterString(unsigned);
172#endif
173
174 protected:
175 String m_text;
176 String m_unmodifiedText;
177#if ENABLE(KEYBOARD_KEY_ATTRIBUTE)
178 String m_key;
179#endif
180#if ENABLE(KEYBOARD_CODE_ATTRIBUTE)
181 String m_code;
182#endif
183 String m_keyIdentifier;
184 int m_windowsVirtualKeyCode;
185#if USE(APPKIT) || USE(UIKIT_KEYBOARD_ADDITIONS) || PLATFORM(GTK)
186 bool m_handledByInputMethod { false };
187#endif
188#if USE(APPKIT)
189 Vector<KeypressCommand> m_commands;
190#elif PLATFORM(GTK)
191 Vector<String> m_commands;
192#endif
193 bool m_autoRepeat;
194 bool m_isKeypad;
195 bool m_isSystemKey;
196
197 bool m_isSyntheticEvent { false };
198
199#if PLATFORM(COCOA)
200#if !PLATFORM(IOS_FAMILY)
201 RetainPtr<NSEvent> m_macEvent;
202#else
203 RetainPtr<::WebEvent> m_Event;
204#endif
205#endif
206#if PLATFORM(GTK)
207 GdkEventKey* m_gdkEventKey;
208 CompositionResults m_compositionResults;
209#endif
210
211 // The modifier state is optional, since it is not needed in the UI process or in legacy WebKit.
212 static Optional<OptionSet<Modifier>> s_currentModifiers;
213 };
214
215} // namespace WebCore
216