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