1 // Copyright 2014 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/extensions/api/braille_display_private/brlapi_keycode_map.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversion_utils.h"
10 namespace extensions
{
12 namespace braille_display_private
{
15 // Bitmask for all braille dots in a key command argument, which coincides
16 // with the representation in the braille_dots member of the KeyEvent
18 const int kAllDots
= BRLAPI_DOT1
| BRLAPI_DOT2
| BRLAPI_DOT3
| BRLAPI_DOT4
|
19 BRLAPI_DOT5
| BRLAPI_DOT6
| BRLAPI_DOT7
| BRLAPI_DOT8
;
21 // Maximum Latin 1 character keyboard symbol.
22 const brlapi_keyCode_t kMaxLatin1KeySym
= 0xff;
24 // Range of function keys that we support.
25 // See ui/events/keycodes/dom/keycode_converter_data.inc for the list of all
27 const brlapi_keyCode_t kMinFunctionKey
= BRLAPI_KEY_SYM_FUNCTION
;
28 const brlapi_keyCode_t kMaxFunctionKey
= BRLAPI_KEY_SYM_FUNCTION
+ 23;
30 // Maps the keyboard modifier flags to their corresponding flags in a
32 void MapModifierFlags(brlapi_keyCode_t code
, KeyEvent
* event
) {
33 if (code
& BRLAPI_KEY_FLG_CONTROL
)
34 event
->ctrl_key
.reset(new bool(true));
35 if (code
& BRLAPI_KEY_FLG_META
)
36 event
->alt_key
.reset(new bool(true));
37 if (code
& BRLAPI_KEY_FLG_SHIFT
)
38 event
->shift_key
.reset(new bool(true));
41 // Maps a brlapi keysym, which is similar to an X keysym into the
43 // See ui/events/keycodes/dom/keycode_converter_data.cc for the full
45 void MapKeySym(brlapi_keyCode_t code
, KeyEvent
* event
) {
46 brlapi_keyCode_t key_sym
= code
& BRLAPI_KEY_CODE_MASK
;
47 if (key_sym
< kMaxLatin1KeySym
||
48 (key_sym
& BRLAPI_KEY_SYM_UNICODE
) != 0) {
49 uint32 code_point
= key_sym
& ~BRLAPI_KEY_SYM_UNICODE
;
50 if (!base::IsValidCharacter(code_point
))
52 event
->standard_key_char
.reset(new std::string
);
53 base::WriteUnicodeCharacter(code_point
, event
->standard_key_char
.get());
54 } else if (key_sym
>= kMinFunctionKey
&& key_sym
<= kMaxFunctionKey
) {
55 // Function keys are 0-based here, so we need to add one to get e.g.
56 // 'F1' for the first key.
57 int function_key_number
= key_sym
- kMinFunctionKey
+ 1;
58 event
->standard_key_code
.reset(
59 new std::string(base::StringPrintf("F%d", function_key_number
)));
61 // Explicitly map the keys that brlapi provides.
62 const char* code_string
;
64 case BRLAPI_KEY_SYM_BACKSPACE
:
65 code_string
= "Backspace";
67 case BRLAPI_KEY_SYM_TAB
:
70 case BRLAPI_KEY_SYM_LINEFEED
:
71 code_string
= "Enter";
73 case BRLAPI_KEY_SYM_ESCAPE
:
74 code_string
= "Escape";
76 case BRLAPI_KEY_SYM_HOME
:
79 case BRLAPI_KEY_SYM_LEFT
:
80 code_string
= "ArrowLeft";
82 case BRLAPI_KEY_SYM_UP
:
83 code_string
= "ArrowUp";
85 case BRLAPI_KEY_SYM_RIGHT
:
86 code_string
= "ArrowRight";
88 case BRLAPI_KEY_SYM_DOWN
:
89 code_string
= "ArrowDown";
91 case BRLAPI_KEY_SYM_PAGE_UP
:
92 code_string
= "PageUp";
94 case BRLAPI_KEY_SYM_PAGE_DOWN
:
95 code_string
= "PageDown";
97 case BRLAPI_KEY_SYM_END
:
100 case BRLAPI_KEY_SYM_INSERT
:
101 code_string
= "Insert";
103 case BRLAPI_KEY_SYM_DELETE
:
104 code_string
= "Delete";
109 event
->standard_key_code
.reset(new std::string(code_string
));
111 MapModifierFlags(code
, event
);
112 event
->command
= KEY_COMMAND_STANDARD_KEY
;
115 void MapCommand(brlapi_keyCode_t code
, KeyEvent
* event
) {
116 brlapi_keyCode_t argument
= code
& BRLAPI_KEY_CMD_ARG_MASK
;
117 switch (code
& BRLAPI_KEY_CODE_MASK
) {
118 case BRLAPI_KEY_CMD_LNUP
:
119 event
->command
= KEY_COMMAND_LINE_UP
;
121 case BRLAPI_KEY_CMD_LNDN
:
122 event
->command
= KEY_COMMAND_LINE_DOWN
;
124 case BRLAPI_KEY_CMD_FWINLT
:
125 event
->command
= KEY_COMMAND_PAN_LEFT
;
127 case BRLAPI_KEY_CMD_FWINRT
:
128 event
->command
= KEY_COMMAND_PAN_RIGHT
;
130 case BRLAPI_KEY_CMD_TOP
:
131 event
->command
= KEY_COMMAND_TOP
;
133 case BRLAPI_KEY_CMD_BOT
:
134 event
->command
= KEY_COMMAND_BOTTOM
;
137 switch (code
& BRLAPI_KEY_CMD_BLK_MASK
) {
138 case BRLAPI_KEY_CMD_ROUTE
:
139 event
->command
= KEY_COMMAND_ROUTING
;
140 event
->display_position
.reset(new int(argument
));
142 case BRLAPI_KEY_CMD_PASSDOTS
:
143 event
->command
= KEY_COMMAND_DOTS
;
144 event
->braille_dots
.reset(new int(argument
& kAllDots
));
145 MapModifierFlags(code
, event
);
153 scoped_ptr
<KeyEvent
> BrlapiKeyCodeToEvent(brlapi_keyCode_t code
) {
154 scoped_ptr
<KeyEvent
> result(new KeyEvent
);
155 result
->command
= KEY_COMMAND_NONE
;
156 switch (code
& BRLAPI_KEY_TYPE_MASK
) {
157 case BRLAPI_KEY_TYPE_SYM
:
158 MapKeySym(code
, result
.get());
160 case BRLAPI_KEY_TYPE_CMD
:
161 MapCommand(code
, result
.get());
164 if (result
->command
== KEY_COMMAND_NONE
)
166 return result
.Pass();
169 } // namespace braille_display_private
171 } // namespace extensions