1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #ifndef MOZC_UNIX_IBUS_KEY_TRANSLATOR_H_
31 #define MOZC_UNIX_IBUS_KEY_TRANSLATOR_H_
32 
33 #include <map>
34 
35 #include "base/port.h"
36 #include "protocol/commands.pb.h"
37 #include "unix/ibus/ibus_header.h"
38 
39 namespace mozc {
40 namespace ibus {
41 
42 // This class is responsible for converting key code sent from ibus-daemon
43 // (defined in /usr/include/ibus-1.0/ibuskeysyms.h) to a KeyEvent object for
44 // the input of session_interface.
45 class KeyTranslator {
46  public:
47   KeyTranslator();
48   virtual ~KeyTranslator();
49 
50   // Converts ibus keycode to Mozc key code and stores them on |out_event|.
51   // Returns true if ibus keycode is successfully converted to Mozc key code.
52   bool Translate(guint keyval,
53                  guint keycode,
54                  guint modifiers,
55                  config::Config::PreeditMethod method,
56                  bool layout_is_jp,
57                  commands::KeyEvent *out_event) const;
58 
59  private:
60   typedef std::map<guint, commands::KeyEvent::SpecialKey> SpecialKeyMap;
61   typedef std::map<guint, commands::KeyEvent::ModifierKey> ModifierKeyMap;
62   typedef std::map<guint, std::pair<string, string> > KanaMap;
63 
64   // Returns true iff key is modifier key such as SHIFT, ALT, or CAPSLOCK.
65   bool IsModifierKey(guint keyval,
66                      guint keycode,
67                      guint modifiers) const;
68 
69   // Returns true iff key is special key such as ENTER, ESC, or PAGE_UP.
70   bool IsSpecialKey(guint keyval,
71                     guint keycode,
72                     guint modifiers) const;
73 
74   // Returns true iff |keyval| is a key with a kana assigned.
75   bool IsKanaAvailable(guint keyval,
76                        guint keycode,
77                        guint modifiers,
78                        bool layout_is_jp,
79                        string *out) const;
80 
81   // Returns true iff key is ASCII such as '0', 'A', or '!'.
82   static bool IsAscii(guint keyval,
83                       guint keycode,
84                       guint modifiers);
85 
86   // Returns true iff key is printable.
87   static bool IsPrintable(guint keyval, guint keycode, guint modifiers);
88 
89   // Returns true iff key is HiraganaKatakana with shift modifier.
90   static bool IsHiraganaKatakanaKeyWithShift(guint keyval,
91                                              guint keycode,
92                                              guint modifiers);
93 
94   // Initializes private fields.
95   void Init();
96 
97   // Stores a mapping from ibus keys to Mozc's special keys.
98   SpecialKeyMap special_key_map_;
99   // Stores a mapping from ibus modifier keys to Mozc's modifier keys.
100   ModifierKeyMap modifier_key_map_;
101   // Stores a mapping from ibus modifier masks to Mozc's modifier keys.
102   ModifierKeyMap modifier_mask_map_;
103   // Stores a mapping from ASCII to Kana character. For example, ASCII character
104   // '4' is mapped to Japanese 'Hiragana Letter U' (without Shift modifier) and
105   // 'Hiragana Letter Small U' (with Shift modifier).
106   KanaMap kana_map_jp_;  // mapping for JP keyboard.
107   KanaMap kana_map_us_;  // mapping for US keyboard.
108 
109   DISALLOW_COPY_AND_ASSIGN(KeyTranslator);
110 };
111 
112 }  // namespace ibus
113 }  // namespace mozc
114 
115 #endif  // MOZC_UNIX_IBUS_KEY_TRANSLATOR_H_
116