- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / chromedriver / keycode_text_conversion_mac.mm
1 // Copyright (c) 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.
4
5 #include "chrome/test/chromedriver/keycode_text_conversion.h"
6
7 #import <Carbon/Carbon.h>
8
9 #include <cctype>
10
11 #include "base/mac/scoped_cftyperef.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/test/chromedriver/chrome/ui_events.h"
14 #include "ui/events/keycodes/keyboard_code_conversion_mac.h"
15
16 bool ConvertKeyCodeToText(
17     ui::KeyboardCode key_code, int modifiers, std::string* text,
18     std::string* error_msg) {
19   int mac_key_code = 0;
20   {
21     unichar character, unmodified_character;
22     mac_key_code = ui::MacKeyCodeForWindowsKeyCode(
23         key_code,
24         0,
25         &character,
26         &unmodified_character);
27   }
28   *error_msg = std::string();
29   if (mac_key_code < 0) {
30     *text = std::string();
31     return true;
32   }
33
34   int mac_modifiers = 0;
35   if (modifiers & kShiftKeyModifierMask)
36     mac_modifiers |= shiftKey;
37   if (modifiers & kControlKeyModifierMask)
38     mac_modifiers |= controlKey;
39   if (modifiers & kAltKeyModifierMask)
40     mac_modifiers |= optionKey;
41   if (modifiers & kMetaKeyModifierMask)
42     mac_modifiers |= cmdKey;
43   // Convert EventRecord modifiers to format UCKeyTranslate accepts. See docs
44   // on UCKeyTranslate for more info.
45   UInt32 modifier_key_state = (mac_modifiers >> 8) & 0xFF;
46
47   base::ScopedCFTypeRef<TISInputSourceRef> input_source_copy(
48       TISCopyCurrentKeyboardLayoutInputSource());
49   CFDataRef layout_data = static_cast<CFDataRef>(TISGetInputSourceProperty(
50       input_source_copy, kTISPropertyUnicodeKeyLayoutData));
51
52   UInt32 dead_key_state = 0;
53   UniCharCount char_count = 0;
54   UniChar character = 0;
55   OSStatus status = UCKeyTranslate(
56       reinterpret_cast<const UCKeyboardLayout*>(CFDataGetBytePtr(layout_data)),
57       static_cast<UInt16>(mac_key_code),
58       kUCKeyActionDown,
59       modifier_key_state,
60       LMGetKbdLast(),
61       kUCKeyTranslateNoDeadKeysBit,
62       &dead_key_state,
63       1,
64       &char_count,
65       &character);
66   if (status == noErr && char_count == 1 && !std::iscntrl(character)) {
67     string16 text16;
68     text16.push_back(character);
69     *text = UTF16ToUTF8(text16);
70     return true;
71   }
72   *text = std::string();
73   return true;
74 }
75
76 bool ConvertCharToKeyCode(
77     char16 key, ui::KeyboardCode* key_code, int *necessary_modifiers,
78     std::string* error_msg) {
79   string16 key_string;
80   key_string.push_back(key);
81   std::string key_string_utf8 = UTF16ToUTF8(key_string);
82   bool found_code = false;
83   *error_msg = std::string();
84   // There doesn't seem to be a way to get a mac key code for a given unicode
85   // character. So here we check every key code to see if it produces the
86   // right character. We could cache the results and regenerate everytime the
87   // language changes, but this brute force technique has negligble performance
88   // effects (on my laptop it is a submillisecond difference).
89   for (int i = 0; i < 256; ++i) {
90     ui::KeyboardCode code = static_cast<ui::KeyboardCode>(i);
91     // Skip the numpad keys.
92     if (code >= ui::VKEY_NUMPAD0 && code <= ui::VKEY_DIVIDE)
93       continue;
94     std::string key_string;
95     if (!ConvertKeyCodeToText(code, 0, &key_string, error_msg))
96       return false;
97     found_code = key_string_utf8 == key_string;
98     std::string key_string_utf8_tmp;
99     if (!ConvertKeyCodeToText(
100         code, kShiftKeyModifierMask, &key_string_utf8_tmp, error_msg))
101       return false;
102     if (!found_code && key_string_utf8 == key_string_utf8_tmp) {
103       *necessary_modifiers = kShiftKeyModifierMask;
104       found_code = true;
105     }
106     if (found_code) {
107       *key_code = code;
108       break;
109     }
110   }
111   return found_code;
112 }