Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / web_input_event_aurax11.cc
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.
4
5 // Portions based heavily on:
6 // third_party/WebKit/public/web/gtk/WebInputEventFactory.cpp
7 //
8 /*
9  * Copyright (C) 2006-2011 Google Inc. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
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
20  * distribution.
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.
24  *
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.
36  */
37
38 #include "content/browser/renderer_host/web_input_event_aura.h"
39
40 #include <X11/keysym.h>
41 #include <X11/Xlib.h>
42 #include <X11/Xutil.h>
43 #include <cstdlib>
44
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"
52
53 namespace content {
54
55 // chromium WebKit does not provide a WebInputEventFactory for X11, so we have
56 // to do the work here ourselves.
57
58 namespace {
59
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);
70     switch (keysym) {
71       case XK_Shift_L:
72         return ui::VKEY_LSHIFT;
73       case XK_Shift_R:
74         return ui::VKEY_RSHIFT;
75       case XK_Control_L:
76         return ui::VKEY_LCONTROL;
77       case XK_Control_R:
78         return ui::VKEY_RCONTROL;
79       case XK_Meta_L:
80       case XK_Alt_L:
81         return ui::VKEY_LMENU;
82       case XK_Meta_R:
83       case XK_Alt_R:
84         return ui::VKEY_RMENU;
85     }
86   }
87   return windows_key_code;
88 }
89
90 }  // namespace
91
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;
97
98   webkit_event.timeStampSeconds = event->time_stamp().InSecondsF();
99   webkit_event.modifiers = EventFlagsToWebEventModifiers(event->flags());
100
101   switch (native_event->type) {
102     case KeyPress:
103       webkit_event.type = event->is_char() ? blink::WebInputEvent::Char :
104           blink::WebInputEvent::RawKeyDown;
105       break;
106     case KeyRelease:
107       webkit_event.type = blink::WebInputEvent::KeyUp;
108       break;
109     default:
110       NOTREACHED();
111   }
112
113   if (webkit_event.modifiers & blink::WebInputEvent::AltKey)
114     webkit_event.isSystemKey = true;
115
116   webkit_event.windowsKeyCode = XKeyEventToWindowsKeyCode(native_key_event);
117   webkit_event.nativeKeyCode = native_key_event->keycode;
118
119   if (webkit_event.windowsKeyCode == ui::VKEY_RETURN)
120     webkit_event.unmodifiedText[0] = '\r';
121   else
122     webkit_event.unmodifiedText[0] = ui::GetCharacterFromXEvent(native_event);
123
124   if (webkit_event.modifiers & blink::WebInputEvent::ControlKey) {
125     webkit_event.text[0] =
126         GetControlCharacter(
127             webkit_event.windowsKeyCode,
128             webkit_event.modifiers & blink::WebInputEvent::ShiftKey);
129   } else {
130     webkit_event.text[0] = webkit_event.unmodifiedText[0];
131   }
132
133   webkit_event.setKeyIdentifierFromWindowsKeyCode();
134
135   // TODO: IsKeyPad?
136
137   return webkit_event;
138 }
139
140 }  // namespace content