ozone: evdev: Use kernel timestamp from input events for KeyEvents
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / input_method_engine.cc
blobe69a73fa08887081d2686befa0be663a20d76e6b
1 // Copyright 2013 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 #include "chrome/browser/chromeos/input_method/input_method_engine.h"
7 #undef FocusIn
8 #undef FocusOut
9 #undef RootWindow
10 #include <map>
12 #include "ash/shell.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/metrics/histogram.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "ui/aura/window.h"
21 #include "ui/aura/window_tree_host.h"
22 #include "ui/base/ime/candidate_window.h"
23 #include "ui/base/ime/chromeos/component_extension_ime_manager.h"
24 #include "ui/base/ime/chromeos/composition_text.h"
25 #include "ui/base/ime/chromeos/extension_ime_util.h"
26 #include "ui/base/ime/chromeos/ime_keymap.h"
27 #include "ui/base/ime/chromeos/input_method_manager.h"
28 #include "ui/base/ime/text_input_flags.h"
29 #include "ui/chromeos/ime/input_method_menu_item.h"
30 #include "ui/chromeos/ime/input_method_menu_manager.h"
31 #include "ui/events/event.h"
32 #include "ui/events/event_processor.h"
33 #include "ui/events/event_utils.h"
34 #include "ui/events/keycodes/dom3/dom_code.h"
35 #include "ui/events/keycodes/dom4/keycode_converter.h"
36 #include "ui/keyboard/keyboard_controller.h"
37 #include "ui/keyboard/keyboard_util.h"
39 namespace chromeos {
41 namespace {
43 const char kErrorNotActive[] = "IME is not active";
44 const char kErrorWrongContext[] = "Context is not active";
45 const char kCandidateNotFound[] = "Candidate not found";
47 // Notifies InputContextHandler that the composition is changed.
48 void UpdateComposition(const CompositionText& composition_text,
49 uint32 cursor_pos,
50 bool is_visible) {
51 IMEInputContextHandlerInterface* input_context =
52 IMEBridge::Get()->GetInputContextHandler();
53 if (input_context)
54 input_context->UpdateCompositionText(
55 composition_text, cursor_pos, is_visible);
58 // Returns the length of characters of a UTF-8 string with unknown string
59 // length. Cannot apply faster algorithm to count characters in an utf-8
60 // string without knowing the string length, so just does a full scan.
61 size_t GetUtf8StringLength(const char* s) {
62 size_t ret = 0;
63 while (*s) {
64 if ((*s & 0xC0) != 0x80)
65 ret++;
66 ++s;
68 return ret;
71 std::string GetKeyFromEvent(const ui::KeyEvent& event) {
72 const std::string code = event.GetCodeString();
73 if (StartsWithASCII(code, "Control", true))
74 return "Ctrl";
75 if (StartsWithASCII(code, "Shift", true))
76 return "Shift";
77 if (StartsWithASCII(code, "Alt", true))
78 return "Alt";
79 if (StartsWithASCII(code, "Arrow", true))
80 return code.substr(5);
81 if (code == "Escape")
82 return "Esc";
83 if (code == "Backspace" || code == "Tab" ||
84 code == "Enter" || code == "CapsLock" ||
85 code == "Power")
86 return code;
87 // Cases for media keys.
88 switch (event.key_code()) {
89 case ui::VKEY_BROWSER_BACK:
90 case ui::VKEY_F1:
91 return "HistoryBack";
92 case ui::VKEY_BROWSER_FORWARD:
93 case ui::VKEY_F2:
94 return "HistoryForward";
95 case ui::VKEY_BROWSER_REFRESH:
96 case ui::VKEY_F3:
97 return "BrowserRefresh";
98 case ui::VKEY_MEDIA_LAUNCH_APP2:
99 case ui::VKEY_F4:
100 return "ChromeOSFullscreen";
101 case ui::VKEY_MEDIA_LAUNCH_APP1:
102 case ui::VKEY_F5:
103 return "ChromeOSSwitchWindow";
104 case ui::VKEY_BRIGHTNESS_DOWN:
105 case ui::VKEY_F6:
106 return "BrightnessDown";
107 case ui::VKEY_BRIGHTNESS_UP:
108 case ui::VKEY_F7:
109 return "BrightnessUp";
110 case ui::VKEY_VOLUME_MUTE:
111 case ui::VKEY_F8:
112 return "AudioVolumeMute";
113 case ui::VKEY_VOLUME_DOWN:
114 case ui::VKEY_F9:
115 return "AudioVolumeDown";
116 case ui::VKEY_VOLUME_UP:
117 case ui::VKEY_F10:
118 return "AudioVolumeUp";
119 default:
120 break;
122 uint16 ch = 0;
123 // Ctrl+? cases, gets key value for Ctrl is not down.
124 if (event.flags() & ui::EF_CONTROL_DOWN) {
125 ui::KeyEvent event_no_ctrl(event.type(),
126 event.key_code(),
127 event.flags() ^ ui::EF_CONTROL_DOWN);
128 ch = event_no_ctrl.GetCharacter();
129 } else {
130 ch = event.GetCharacter();
132 return base::UTF16ToUTF8(base::string16(1, ch));
135 void GetExtensionKeyboardEventFromKeyEvent(
136 const ui::KeyEvent& event,
137 InputMethodEngine::KeyboardEvent* ext_event) {
138 DCHECK(event.type() == ui::ET_KEY_RELEASED ||
139 event.type() == ui::ET_KEY_PRESSED);
140 DCHECK(ext_event);
141 ext_event->type = (event.type() == ui::ET_KEY_RELEASED) ? "keyup" : "keydown";
143 if (event.code() == ui::DomCode::NONE)
144 ext_event->code = ui::KeyboardCodeToDomKeycode(event.key_code());
145 else
146 ext_event->code = event.GetCodeString();
147 ext_event->key_code = static_cast<int>(event.key_code());
148 ext_event->alt_key = event.IsAltDown();
149 ext_event->ctrl_key = event.IsControlDown();
150 ext_event->shift_key = event.IsShiftDown();
151 ext_event->caps_lock = event.IsCapsLockDown();
152 ext_event->key = GetKeyFromEvent(event);
155 } // namespace
157 InputMethodEngine::InputMethodEngine()
158 : current_input_type_(ui::TEXT_INPUT_TYPE_NONE),
159 context_id_(0),
160 next_context_id_(1),
161 composition_text_(new CompositionText()),
162 composition_cursor_(0),
163 candidate_window_(new ui::CandidateWindow()),
164 window_visible_(false),
165 sent_key_event_(NULL) {
168 InputMethodEngine::~InputMethodEngine() {
171 void InputMethodEngine::Initialize(
172 scoped_ptr<InputMethodEngineInterface::Observer> observer,
173 const char* extension_id) {
174 DCHECK(observer) << "Observer must not be null.";
176 // TODO(komatsu): It is probably better to set observer out of Initialize.
177 observer_ = observer.Pass();
178 extension_id_ = extension_id;
181 const std::string& InputMethodEngine::GetActiveComponentId() const {
182 return active_component_id_;
185 bool InputMethodEngine::SetComposition(
186 int context_id,
187 const char* text,
188 int selection_start,
189 int selection_end,
190 int cursor,
191 const std::vector<SegmentInfo>& segments,
192 std::string* error) {
193 if (!IsActive()) {
194 *error = kErrorNotActive;
195 return false;
197 if (context_id != context_id_ || context_id_ == -1) {
198 *error = kErrorWrongContext;
199 return false;
202 composition_cursor_ = cursor;
203 composition_text_.reset(new CompositionText());
204 composition_text_->set_text(base::UTF8ToUTF16(text));
206 composition_text_->set_selection_start(selection_start);
207 composition_text_->set_selection_end(selection_end);
209 // TODO: Add support for displaying selected text in the composition string.
210 for (std::vector<SegmentInfo>::const_iterator segment = segments.begin();
211 segment != segments.end(); ++segment) {
212 CompositionText::UnderlineAttribute underline;
214 switch (segment->style) {
215 case SEGMENT_STYLE_UNDERLINE:
216 underline.type = CompositionText::COMPOSITION_TEXT_UNDERLINE_SINGLE;
217 break;
218 case SEGMENT_STYLE_DOUBLE_UNDERLINE:
219 underline.type = CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE;
220 break;
221 case SEGMENT_STYLE_NO_UNDERLINE:
222 underline.type = CompositionText::COMPOSITION_TEXT_UNDERLINE_NONE;
223 break;
224 default:
225 continue;
228 underline.start_index = segment->start;
229 underline.end_index = segment->end;
230 composition_text_->mutable_underline_attributes()->push_back(underline);
233 // TODO(nona): Makes focus out mode configuable, if necessary.
234 UpdateComposition(*composition_text_, composition_cursor_, true);
235 return true;
238 bool InputMethodEngine::ClearComposition(int context_id,
239 std::string* error) {
240 if (!IsActive()) {
241 *error = kErrorNotActive;
242 return false;
244 if (context_id != context_id_ || context_id_ == -1) {
245 *error = kErrorWrongContext;
246 return false;
249 composition_cursor_ = 0;
250 composition_text_.reset(new CompositionText());
251 UpdateComposition(*composition_text_, composition_cursor_, false);
252 return true;
255 bool InputMethodEngine::CommitText(int context_id, const char* text,
256 std::string* error) {
257 if (!IsActive()) {
258 // TODO: Commit the text anyways.
259 *error = kErrorNotActive;
260 return false;
262 if (context_id != context_id_ || context_id_ == -1) {
263 *error = kErrorWrongContext;
264 return false;
267 IMEBridge::Get()->GetInputContextHandler()->CommitText(text);
269 // Records histograms for committed characters.
270 if (!composition_text_->text().empty()) {
271 size_t len = GetUtf8StringLength(text);
272 UMA_HISTOGRAM_CUSTOM_COUNTS("InputMethod.CommitLength",
273 len, 1, 25, 25);
275 return true;
278 bool InputMethodEngine::SendKeyEvents(
279 int context_id,
280 const std::vector<KeyboardEvent>& events) {
281 if (!IsActive()) {
282 return false;
284 // context_id == 0, means sending key events to non-input field.
285 // context_id_ == -1, means the focus is not in an input field.
286 if (context_id != 0 && (context_id != context_id_ || context_id_ == -1)) {
287 return false;
290 ui::EventProcessor* dispatcher =
291 ash::Shell::GetPrimaryRootWindow()->GetHost()->event_processor();
293 for (size_t i = 0; i < events.size(); ++i) {
294 const KeyboardEvent& event = events[i];
295 const ui::EventType type =
296 (event.type == "keyup") ? ui::ET_KEY_RELEASED : ui::ET_KEY_PRESSED;
297 ui::KeyboardCode key_code = static_cast<ui::KeyboardCode>(event.key_code);
298 if (key_code == ui::VKEY_UNKNOWN)
299 key_code = ui::DomKeycodeToKeyboardCode(event.code);
301 int flags = ui::EF_NONE;
302 flags |= event.alt_key ? ui::EF_ALT_DOWN : ui::EF_NONE;
303 flags |= event.ctrl_key ? ui::EF_CONTROL_DOWN : ui::EF_NONE;
304 flags |= event.shift_key ? ui::EF_SHIFT_DOWN : ui::EF_NONE;
305 flags |= event.caps_lock ? ui::EF_CAPS_LOCK_DOWN : ui::EF_NONE;
307 base::char16 ch = 0;
308 // 4-bytes UTF-8 string is at least 2-characters UTF-16 string.
309 // And Key char can only be single UTF-16 character.
310 if (!event.key.empty() && event.key.size() < 4) {
311 base::string16 key_char = base::UTF8ToUTF16(event.key);
312 if (key_char.size() == 1)
313 ch = key_char[0];
315 ui::KeyEvent ui_event(
316 type, key_code,
317 ui::KeycodeConverter::CodeStringToDomCode(event.code.c_str()), flags,
318 ui::KeycodeConverter::KeyStringToDomKey(event.key.c_str()), ch,
319 ui::EventTimeForNow());
320 base::AutoReset<const ui::KeyEvent*> reset_sent_key(&sent_key_event_,
321 &ui_event);
322 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&ui_event);
323 if (details.dispatcher_destroyed)
324 break;
327 return true;
330 const InputMethodEngine::CandidateWindowProperty&
331 InputMethodEngine::GetCandidateWindowProperty() const {
332 return candidate_window_property_;
335 void InputMethodEngine::SetCandidateWindowProperty(
336 const CandidateWindowProperty& property) {
337 // Type conversion from InputMethodEngineInterface::CandidateWindowProperty to
338 // CandidateWindow::CandidateWindowProperty defined in chromeos/ime/.
339 ui::CandidateWindow::CandidateWindowProperty dest_property;
340 dest_property.page_size = property.page_size;
341 dest_property.is_cursor_visible = property.is_cursor_visible;
342 dest_property.is_vertical = property.is_vertical;
343 dest_property.show_window_at_composition =
344 property.show_window_at_composition;
345 dest_property.cursor_position =
346 candidate_window_->GetProperty().cursor_position;
347 dest_property.auxiliary_text = property.auxiliary_text;
348 dest_property.is_auxiliary_text_visible = property.is_auxiliary_text_visible;
350 candidate_window_->SetProperty(dest_property);
351 candidate_window_property_ = property;
353 if (IsActive()) {
354 IMECandidateWindowHandlerInterface* cw_handler =
355 IMEBridge::Get()->GetCandidateWindowHandler();
356 if (cw_handler)
357 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
361 bool InputMethodEngine::SetCandidateWindowVisible(bool visible,
362 std::string* error) {
363 if (!IsActive()) {
364 *error = kErrorNotActive;
365 return false;
368 window_visible_ = visible;
369 IMECandidateWindowHandlerInterface* cw_handler =
370 IMEBridge::Get()->GetCandidateWindowHandler();
371 if (cw_handler)
372 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
373 return true;
376 bool InputMethodEngine::SetCandidates(
377 int context_id,
378 const std::vector<Candidate>& candidates,
379 std::string* error) {
380 if (!IsActive()) {
381 *error = kErrorNotActive;
382 return false;
384 if (context_id != context_id_ || context_id_ == -1) {
385 *error = kErrorWrongContext;
386 return false;
389 // TODO: Nested candidates
390 candidate_ids_.clear();
391 candidate_indexes_.clear();
392 candidate_window_->mutable_candidates()->clear();
393 for (std::vector<Candidate>::const_iterator ix = candidates.begin();
394 ix != candidates.end(); ++ix) {
395 ui::CandidateWindow::Entry entry;
396 entry.value = base::UTF8ToUTF16(ix->value);
397 entry.label = base::UTF8ToUTF16(ix->label);
398 entry.annotation = base::UTF8ToUTF16(ix->annotation);
399 entry.description_title = base::UTF8ToUTF16(ix->usage.title);
400 entry.description_body = base::UTF8ToUTF16(ix->usage.body);
402 // Store a mapping from the user defined ID to the candidate index.
403 candidate_indexes_[ix->id] = candidate_ids_.size();
404 candidate_ids_.push_back(ix->id);
406 candidate_window_->mutable_candidates()->push_back(entry);
408 if (IsActive()) {
409 IMECandidateWindowHandlerInterface* cw_handler =
410 IMEBridge::Get()->GetCandidateWindowHandler();
411 if (cw_handler)
412 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
414 return true;
417 bool InputMethodEngine::SetCursorPosition(int context_id, int candidate_id,
418 std::string* error) {
419 if (!IsActive()) {
420 *error = kErrorNotActive;
421 return false;
423 if (context_id != context_id_ || context_id_ == -1) {
424 *error = kErrorWrongContext;
425 return false;
428 std::map<int, int>::const_iterator position =
429 candidate_indexes_.find(candidate_id);
430 if (position == candidate_indexes_.end()) {
431 *error = kCandidateNotFound;
432 return false;
435 candidate_window_->set_cursor_position(position->second);
436 IMECandidateWindowHandlerInterface* cw_handler =
437 IMEBridge::Get()->GetCandidateWindowHandler();
438 if (cw_handler)
439 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
440 return true;
443 bool InputMethodEngine::SetMenuItems(const std::vector<MenuItem>& items) {
444 return UpdateMenuItems(items);
447 bool InputMethodEngine::UpdateMenuItems(
448 const std::vector<MenuItem>& items) {
449 if (!IsActive())
450 return false;
452 ui::ime::InputMethodMenuItemList menu_item_list;
453 for (std::vector<MenuItem>::const_iterator item = items.begin();
454 item != items.end(); ++item) {
455 ui::ime::InputMethodMenuItem property;
456 MenuItemToProperty(*item, &property);
457 menu_item_list.push_back(property);
460 ui::ime::InputMethodMenuManager::GetInstance()->
461 SetCurrentInputMethodMenuItemList(
462 menu_item_list);
463 return true;
466 bool InputMethodEngine::IsActive() const {
467 return !active_component_id_.empty();
470 bool InputMethodEngine::DeleteSurroundingText(int context_id,
471 int offset,
472 size_t number_of_chars,
473 std::string* error) {
474 if (!IsActive()) {
475 *error = kErrorNotActive;
476 return false;
478 if (context_id != context_id_ || context_id_ == -1) {
479 *error = kErrorWrongContext;
480 return false;
483 // TODO(nona): Return false if there is ongoing composition.
485 IMEInputContextHandlerInterface* input_context =
486 IMEBridge::Get()->GetInputContextHandler();
487 if (input_context)
488 input_context->DeleteSurroundingText(offset, number_of_chars);
490 return true;
493 void InputMethodEngine::HideInputView() {
494 keyboard::KeyboardController* keyboard_controller =
495 keyboard::KeyboardController::GetInstance();
496 if (keyboard_controller) {
497 keyboard_controller->HideKeyboard(
498 keyboard::KeyboardController::HIDE_REASON_MANUAL);
502 void InputMethodEngine::SetCompositionBounds(
503 const std::vector<gfx::Rect>& bounds) {
504 observer_->OnCompositionBoundsChanged(bounds);
507 void InputMethodEngine::EnableInputView() {
508 keyboard::SetOverrideContentUrl(input_method::InputMethodManager::Get()
509 ->GetActiveIMEState()
510 ->GetCurrentInputMethod()
511 .input_view_url());
512 keyboard::KeyboardController* keyboard_controller =
513 keyboard::KeyboardController::GetInstance();
514 if (keyboard_controller)
515 keyboard_controller->Reload();
518 void InputMethodEngine::FocusIn(
519 const IMEEngineHandlerInterface::InputContext& input_context) {
520 current_input_type_ = input_context.type;
522 if (!IsActive() || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
523 return;
525 context_id_ = next_context_id_;
526 ++next_context_id_;
528 InputMethodEngineInterface::InputContext context;
529 context.id = context_id_;
530 switch (current_input_type_) {
531 case ui::TEXT_INPUT_TYPE_SEARCH:
532 context.type = "search";
533 break;
534 case ui::TEXT_INPUT_TYPE_TELEPHONE:
535 context.type = "tel";
536 break;
537 case ui::TEXT_INPUT_TYPE_URL:
538 context.type = "url";
539 break;
540 case ui::TEXT_INPUT_TYPE_EMAIL:
541 context.type = "email";
542 break;
543 case ui::TEXT_INPUT_TYPE_NUMBER:
544 context.type = "number";
545 break;
546 case ui::TEXT_INPUT_TYPE_PASSWORD:
547 context.type = "password";
548 break;
549 default:
550 context.type = "text";
551 break;
554 context.auto_correct =
555 !(input_context.flags & ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF);
556 context.auto_complete =
557 !(input_context.flags & ui::TEXT_INPUT_FLAG_AUTOCOMPLETE_OFF);
558 context.spell_check =
559 !(input_context.flags & ui::TEXT_INPUT_FLAG_SPELLCHECK_OFF);
561 observer_->OnFocus(context);
564 void InputMethodEngine::FocusOut() {
565 if (!IsActive() || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
566 return;
568 current_input_type_ = ui::TEXT_INPUT_TYPE_NONE;
570 int context_id = context_id_;
571 context_id_ = -1;
572 observer_->OnBlur(context_id);
575 void InputMethodEngine::Enable(const std::string& component_id) {
576 DCHECK(!component_id.empty());
577 active_component_id_ = component_id;
578 observer_->OnActivate(component_id);
579 current_input_type_ = IMEBridge::Get()->GetCurrentTextInputType();
580 FocusIn(IMEEngineHandlerInterface::InputContext(current_input_type_,
581 ui::TEXT_INPUT_MODE_DEFAULT,
582 ui::TEXT_INPUT_FLAG_NONE));
583 EnableInputView();
586 void InputMethodEngine::Disable() {
587 active_component_id_.clear();
588 observer_->OnDeactivated(active_component_id_);
591 void InputMethodEngine::PropertyActivate(const std::string& property_name) {
592 observer_->OnMenuItemActivated(active_component_id_, property_name);
595 void InputMethodEngine::Reset() {
596 observer_->OnReset(active_component_id_);
599 void InputMethodEngine::ProcessKeyEvent(
600 const ui::KeyEvent& key_event,
601 const KeyEventDoneCallback& callback) {
603 KeyEventDoneCallback* handler = new KeyEventDoneCallback();
604 *handler = callback;
606 KeyboardEvent ext_event;
607 GetExtensionKeyboardEventFromKeyEvent(key_event, &ext_event);
609 // If the given key event is equal to the key event sent by
610 // SendKeyEvents, this engine ID is propagated to the extension IME.
611 // Note, this check relies on that ui::KeyEvent is propagated as
612 // reference without copying.
613 if (&key_event == sent_key_event_)
614 ext_event.extension_id = extension_id_;
616 observer_->OnKeyEvent(
617 active_component_id_,
618 ext_event,
619 reinterpret_cast<input_method::KeyEventHandle*>(handler));
622 void InputMethodEngine::CandidateClicked(uint32 index) {
623 if (index > candidate_ids_.size()) {
624 return;
627 // Only left button click is supported at this moment.
628 observer_->OnCandidateClicked(
629 active_component_id_, candidate_ids_.at(index), MOUSE_BUTTON_LEFT);
632 void InputMethodEngine::SetSurroundingText(const std::string& text,
633 uint32 cursor_pos,
634 uint32 anchor_pos) {
635 observer_->OnSurroundingTextChanged(active_component_id_,
636 text,
637 static_cast<int>(cursor_pos),
638 static_cast<int>(anchor_pos));
641 // TODO(uekawa): rename this method to a more reasonable name.
642 void InputMethodEngine::MenuItemToProperty(
643 const MenuItem& item,
644 ui::ime::InputMethodMenuItem* property) {
645 property->key = item.id;
647 if (item.modified & MENU_ITEM_MODIFIED_LABEL) {
648 property->label = item.label;
650 if (item.modified & MENU_ITEM_MODIFIED_VISIBLE) {
651 // TODO(nona): Implement it.
653 if (item.modified & MENU_ITEM_MODIFIED_CHECKED) {
654 property->is_selection_item_checked = item.checked;
656 if (item.modified & MENU_ITEM_MODIFIED_ENABLED) {
657 // TODO(nona): implement sensitive entry(crbug.com/140192).
659 if (item.modified & MENU_ITEM_MODIFIED_STYLE) {
660 if (!item.children.empty()) {
661 // TODO(nona): Implement it.
662 } else {
663 switch (item.style) {
664 case MENU_ITEM_STYLE_NONE:
665 NOTREACHED();
666 break;
667 case MENU_ITEM_STYLE_CHECK:
668 // TODO(nona): Implement it.
669 break;
670 case MENU_ITEM_STYLE_RADIO:
671 property->is_selection_item = true;
672 break;
673 case MENU_ITEM_STYLE_SEPARATOR:
674 // TODO(nona): Implement it.
675 break;
680 // TODO(nona): Support item.children.
683 } // namespace chromeos