blob: 98c8827e4ed0dd3a51bb23272a72d049a6c1fd59 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/webdriver/webdriver_test_util.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
namespace webdriver {
RestoreKeyboardLayoutOnDestruct::RestoreKeyboardLayoutOnDestruct() {
#if defined(OS_WIN)
layout_ = GetKeyboardLayout(NULL);
#elif defined(OS_MACOSX)
layout_.reset(TISCopyCurrentKeyboardInputSource());
#elif defined(OS_LINUX)
NOTIMPLEMENTED();
#endif
}
RestoreKeyboardLayoutOnDestruct::~RestoreKeyboardLayoutOnDestruct() {
#if defined(OS_WIN)
ActivateKeyboardLayout(layout_, 0);
#elif defined(OS_MACOSX)
TISSelectInputSource(layout_);
#elif defined(OS_LINUX)
NOTIMPLEMENTED();
#endif
}
#if defined(OS_WIN)
bool SwitchKeyboardLayout(const std::string& input_locale_identifier) {
HKL layout = LoadKeyboardLayout(
UTF8ToWide(input_locale_identifier).c_str(), 0);
if (!layout)
return false;
return !!ActivateKeyboardLayout(layout, 0);
}
#endif // defined(OS_WIN)
#if defined(OS_MACOSX)
bool SwitchKeyboardLayout(const std::string& input_source_id) {
base::mac::ScopedCFTypeRef<CFMutableDictionaryRef> filter_dict(
CFDictionaryCreateMutable(kCFAllocatorDefault,
1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks));
base::mac::ScopedCFTypeRef<CFStringRef> id_ref(CFStringCreateWithCString(
kCFAllocatorDefault, input_source_id.c_str(), kCFStringEncodingUTF8));
CFDictionaryAddValue(filter_dict, kTISPropertyInputSourceID, id_ref);
base::mac::ScopedCFTypeRef<CFArrayRef> sources(
TISCreateInputSourceList(filter_dict, true));
if (CFArrayGetCount(sources) != 1)
return false;
TISInputSourceRef source = (TISInputSourceRef)CFArrayGetValueAtIndex(
sources, 0);
return TISSelectInputSource(source) == noErr;
}
#endif // defined(OS_WIN)
} // namespace webdriver
|