OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/chromedriver/keycode_text_conversion.h" | 5 #include "chrome/test/chromedriver/keycode_text_conversion.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <X11/keysym.h> | 10 #include <X11/keysym.h> |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 } | 177 } |
178 XFreeModifiermap(mod_map); | 178 XFreeModifiermap(mod_map); |
179 return found; | 179 return found; |
180 } | 180 } |
181 | 181 |
182 } // namespace | 182 } // namespace |
183 | 183 |
184 bool ConvertKeyCodeToText( | 184 bool ConvertKeyCodeToText( |
185 ui::KeyboardCode key_code, int modifiers, std::string* text, | 185 ui::KeyboardCode key_code, int modifiers, std::string* text, |
186 std::string* error_msg) { | 186 std::string* error_msg) { |
| 187 XDisplay* display = gfx::GetXDisplay(); |
| 188 if (!display) { |
| 189 return ConvertKeyCodeToTextOzone(key_code, modifiers, text, error_msg); |
| 190 } |
| 191 |
187 *error_msg = std::string(); | 192 *error_msg = std::string(); |
188 int x_key_code = KeyboardCodeToXKeyCode(key_code); | 193 int x_key_code = KeyboardCodeToXKeyCode(key_code); |
189 if (x_key_code == -1) { | 194 if (x_key_code == -1) { |
190 *text = std::string(); | 195 *text = std::string(); |
191 return true; | 196 return true; |
192 } | 197 } |
193 | 198 |
194 XEvent event; | 199 XEvent event; |
195 memset(&event, 0, sizeof(XEvent)); | 200 memset(&event, 0, sizeof(XEvent)); |
196 XKeyEvent* key_event = &event.xkey; | 201 XKeyEvent* key_event = &event.xkey; |
197 XDisplay* display = gfx::GetXDisplay(); | |
198 if (!display) { | |
199 *error_msg = | |
200 "an X display is required for keycode conversions, consider using Xvfb"; | |
201 *text = std::string(); | |
202 return false; | |
203 } | |
204 key_event->display = display; | 202 key_event->display = display; |
205 key_event->keycode = x_key_code; | 203 key_event->keycode = x_key_code; |
206 if (modifiers & kShiftKeyModifierMask) | 204 if (modifiers & kShiftKeyModifierMask) |
207 key_event->state |= ShiftMask; | 205 key_event->state |= ShiftMask; |
208 if (modifiers & kControlKeyModifierMask) | 206 if (modifiers & kControlKeyModifierMask) |
209 key_event->state |= ControlMask; | 207 key_event->state |= ControlMask; |
210 | 208 |
211 // Make a best attempt for non-standard modifiers. | 209 // Make a best attempt for non-standard modifiers. |
212 int x_modifier; | 210 int x_modifier; |
213 if (modifiers & kAltKeyModifierMask && | 211 if (modifiers & kAltKeyModifierMask && |
(...skipping 16 matching lines...) Expand all Loading... |
230 else | 228 else |
231 *text = base::UTF16ToUTF8(base::string16(1, character)); | 229 *text = base::UTF16ToUTF8(base::string16(1, character)); |
232 return true; | 230 return true; |
233 } | 231 } |
234 | 232 |
235 bool ConvertCharToKeyCode( | 233 bool ConvertCharToKeyCode( |
236 base::char16 key, | 234 base::char16 key, |
237 ui::KeyboardCode* key_code, | 235 ui::KeyboardCode* key_code, |
238 int* necessary_modifiers, | 236 int* necessary_modifiers, |
239 std::string* error_msg) { | 237 std::string* error_msg) { |
| 238 XDisplay* display = gfx::GetXDisplay(); |
| 239 if (!display) { |
| 240 return ConvertCharToKeyCodeOzone(key, key_code, necessary_modifiers, |
| 241 error_msg); |
| 242 } |
| 243 |
240 std::string key_string(base::UTF16ToUTF8(base::string16(1, key))); | 244 std::string key_string(base::UTF16ToUTF8(base::string16(1, key))); |
241 bool found = false; | 245 bool found = false; |
242 ui::KeyboardCode test_code; | 246 ui::KeyboardCode test_code; |
243 int test_modifiers; | 247 int test_modifiers; |
244 *error_msg = std::string(); | 248 *error_msg = std::string(); |
245 std::string conv_string; | 249 std::string conv_string; |
246 for (size_t i = 0; i < arraysize(kKeyCodeToXKeyCode); ++i) { | 250 for (size_t i = 0; i < arraysize(kKeyCodeToXKeyCode); ++i) { |
247 test_code = kKeyCodeToXKeyCode[i].key_code; | 251 test_code = kKeyCodeToXKeyCode[i].key_code; |
248 // Skip the numpad keys. | 252 // Skip the numpad keys. |
249 if (test_code >= ui::VKEY_NUMPAD0 && test_code <= ui::VKEY_DIVIDE) | 253 if (test_code >= ui::VKEY_NUMPAD0 && test_code <= ui::VKEY_DIVIDE) |
(...skipping 14 matching lines...) Expand all Loading... |
264 found = true; | 268 found = true; |
265 break; | 269 break; |
266 } | 270 } |
267 } | 271 } |
268 if (found) { | 272 if (found) { |
269 *key_code = test_code; | 273 *key_code = test_code; |
270 *necessary_modifiers = test_modifiers; | 274 *necessary_modifiers = test_modifiers; |
271 } | 275 } |
272 return found; | 276 return found; |
273 } | 277 } |
OLD | NEW |