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_EVENT_HANDLER_H_
31 #define MOZC_UNIX_IBUS_KEY_EVENT_HANDLER_H_
32 
33 #include <memory>
34 #include <set>
35 
36 #include "base/port.h"
37 #include "protocol/commands.pb.h"
38 #include "protocol/config.pb.h"
39 #include "unix/ibus/key_translator.h"
40 
41 namespace mozc {
42 namespace ibus {
43 
44 class KeyEventHandler {
45  public:
46   KeyEventHandler();
47 
48   // Converts a key event came from ibus to commands::KeyEvent. This is a
49   // stateful method. It stores modifier keys states since ibus doesn't send
50   // an enough information about the modifier keys.
51   bool GetKeyEvent(guint keyval, guint keycode, guint modifiers,
52                    config::Config::PreeditMethod preedit_method,
53                    bool layout_is_jp, commands::KeyEvent *key);
54 
55   // Clears states.
56   void Clear();
57 
58  private:
59   friend class KeyEventHandlerTest;
60 
61   // Manages modifier keys. Returns false if it should not be sent to server.
62   bool ProcessModifiers(bool is_key_up, guint 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<guint> 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 ibus
77 }  // namespace mozc
78 
79 #endif  // MOZC_UNIX_IBUS_KEY_EVENT_HANDLER_H_
80