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.
5 // Portions based heavily on:
6 // third_party/WebKit/public/web/gtk/WebInputEventFactory.cpp
9 * Copyright (C) 2006-2011 Google Inc. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are
15 * * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following disclaimer
19 * in the documentation and/or other materials provided with the
21 * * Neither the name of Google Inc. nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "content/browser/renderer_host/web_input_event_aura.h"
40 #include <X11/keysym.h>
42 #include <X11/Xutil.h>
45 #include "base/event_types.h"
46 #include "base/logging.h"
47 #include "content/browser/renderer_host/ui_events_helper.h"
48 #include "ui/events/event.h"
49 #include "ui/events/event_constants.h"
50 #include "ui/events/keycodes/keyboard_code_conversion_x.h"
51 #include "ui/events/keycodes/keyboard_codes.h"
55 // chromium WebKit does not provide a WebInputEventFactory for X11, so we have
56 // to do the work here ourselves.
60 int XKeyEventToWindowsKeyCode(XKeyEvent
* event
) {
61 int windows_key_code
=
62 ui::KeyboardCodeFromXKeyEvent(reinterpret_cast<XEvent
*>(event
));
63 if (windows_key_code
== ui::VKEY_SHIFT
||
64 windows_key_code
== ui::VKEY_CONTROL
||
65 windows_key_code
== ui::VKEY_MENU
) {
66 // To support DOM3 'location' attribute, we need to lookup an X KeySym and
67 // set ui::VKEY_[LR]XXX instead of ui::VKEY_XXX.
68 KeySym keysym
= XK_VoidSymbol
;
69 XLookupString(event
, NULL
, 0, &keysym
, NULL
);
72 return ui::VKEY_LSHIFT
;
74 return ui::VKEY_RSHIFT
;
76 return ui::VKEY_LCONTROL
;
78 return ui::VKEY_RCONTROL
;
81 return ui::VKEY_LMENU
;
84 return ui::VKEY_RMENU
;
87 return windows_key_code
;
92 blink::WebKeyboardEvent
MakeWebKeyboardEventFromAuraEvent(
93 ui::KeyEvent
* event
) {
94 const base::NativeEvent
& native_event
= event
->native_event();
95 blink::WebKeyboardEvent webkit_event
;
96 XKeyEvent
* native_key_event
= &native_event
->xkey
;
98 webkit_event
.timeStampSeconds
= event
->time_stamp().InSecondsF();
99 webkit_event
.modifiers
= EventFlagsToWebEventModifiers(event
->flags());
101 switch (native_event
->type
) {
103 webkit_event
.type
= event
->is_char() ? blink::WebInputEvent::Char
:
104 blink::WebInputEvent::RawKeyDown
;
107 webkit_event
.type
= blink::WebInputEvent::KeyUp
;
113 if (webkit_event
.modifiers
& blink::WebInputEvent::AltKey
)
114 webkit_event
.isSystemKey
= true;
116 webkit_event
.windowsKeyCode
= XKeyEventToWindowsKeyCode(native_key_event
);
117 webkit_event
.nativeKeyCode
= native_key_event
->keycode
;
119 if (webkit_event
.windowsKeyCode
== ui::VKEY_RETURN
)
120 webkit_event
.unmodifiedText
[0] = '\r';
122 webkit_event
.unmodifiedText
[0] = ui::GetCharacterFromXEvent(native_event
);
124 if (webkit_event
.modifiers
& blink::WebInputEvent::ControlKey
) {
125 webkit_event
.text
[0] =
127 webkit_event
.windowsKeyCode
,
128 webkit_event
.modifiers
& blink::WebInputEvent::ShiftKey
);
130 webkit_event
.text
[0] = webkit_event
.unmodifiedText
[0];
133 webkit_event
.setKeyIdentifierFromWindowsKeyCode();
135 // TODO: IsAutoRepeat/IsKeyPad?
140 } // namespace content