1 // Copyright 2010-2018, 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_KEY_EVENT_HANDLER_H_
32 #define MOZC_UNIX_FCITX_KEY_EVENT_HANDLER_H_
33 
34 #include <set>
35 #include <memory>
36 
37 #include "base/port.h"
38 #include "protocol/config.pb.h"
39 #include "protocol/commands.pb.h"
40 #include "unix/fcitx/fcitx_key_translator.h"
41 
42 namespace mozc {
43 namespace fcitx {
44 
45 class KeyEventHandler {
46  public:
47   KeyEventHandler();
48 
49   // Converts a key event came from fcitx to commands::KeyEvent. This is a
50   // stateful method. It stores modifier keys states since ibus doesn't send
51   // an enough information about the modifier keys.
52   bool GetKeyEvent(FcitxKeySym keyval, uint32 keycode, uint32 modifiers,
53                    config::Config::PreeditMethod preedit_method,
54                    bool layout_is_jp, bool is_key_up, commands::KeyEvent *key);
55 
56   // Clears states.
57   void Clear();
58 
59  private:
60 
61   // Manages modifier keys. Returns false if it should not be sent to server.
62   bool ProcessModifiers(bool is_key_up, uint32 keyval,
63                         commands::KeyEvent *key_event);
64 
65   std::unique_ptr<KeyTranslator> key_translator_;
66   // Non modifier key is pressed or not after all keys are released.
67   bool is_non_modifier_key_pressed_;
68   // Currently pressed modifier keys.  It is set of keyval.
69   std::set<uint32> currently_pressed_modifiers_;
70   // Pending modifier keys.
71   std::set<commands::KeyEvent::ModifierKey> modifiers_to_be_sent_;
72 
73   DISALLOW_COPY_AND_ASSIGN(KeyEventHandler);
74 };
75 
76 }  // namespace fcitx
77 }  // namespace mozc
78 
79 #endif  // MOZC_UNIX_FCITX_KEY_EVENT_HANDLER_H_
80