summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/input_method/input_method_engine_ibus.h
blob: 1711460a0de5c203d777b2077035244bdf1af1a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_IBUS_H_
#define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_IBUS_H_

#include <map>
#include <string>
#include <vector>
#include "chrome/browser/chromeos/input_method/input_method_engine.h"
#include "chromeos/dbus/ibus/ibus_engine_factory_service.h"
#include "chromeos/dbus/ibus/ibus_engine_service.h"
#include "dbus/object_path.h"

namespace chromeos {

class IBusComponent;
class IBusLookupTable;
class IBusText;

class IBusEngineService;
namespace input_method {
struct KeyEventHandle;
}  // namespace input_method

class InputMethodEngineIBus : public InputMethodEngine,
                              public IBusEngineHandlerInterface {
 public:
  InputMethodEngineIBus();

  virtual ~InputMethodEngineIBus();

  void Initialize(
      InputMethodEngine::Observer* observer,
      const char* engine_name,
      const char* extension_id,
      const char* engine_id,
      const char* description,
      const char* language,
      const std::vector<std::string>& layouts,
      std::string* error);

  // InputMethodEngine overrides.
  virtual bool SetComposition(int context_id,
                              const char* text,
                              int selection_start,
                              int selection_end,
                              int cursor,
                              const std::vector<SegmentInfo>& segments,
                              std::string* error) OVERRIDE;
  virtual bool ClearComposition(int context_id, std::string* error) OVERRIDE;
  virtual bool CommitText(int context_id, const char* text,
                          std::string* error) OVERRIDE;
  virtual bool SetCandidateWindowVisible(bool visible,
                                         std::string* error) OVERRIDE;
  virtual void SetCandidateWindowCursorVisible(bool visible) OVERRIDE;
  virtual void SetCandidateWindowVertical(bool vertical) OVERRIDE;
  virtual void SetCandidateWindowPageSize(int size) OVERRIDE;
  virtual void SetCandidateWindowAuxText(const char* text) OVERRIDE;
  virtual void SetCandidateWindowAuxTextVisible(bool visible) OVERRIDE;
  virtual void SetCandidateWindowPosition(
      CandidateWindowPosition position) OVERRIDE;
  virtual bool SetCandidates(int context_id,
                             const std::vector<Candidate>& candidates,
                             std::string* error) OVERRIDE;
  virtual bool SetCursorPosition(int context_id, int candidate_id,
                                 std::string* error) OVERRIDE;
  virtual bool SetMenuItems(const std::vector<MenuItem>& items) OVERRIDE;
  virtual bool UpdateMenuItems(const std::vector<MenuItem>& items) OVERRIDE;
  virtual bool IsActive() const OVERRIDE;
  virtual void KeyEventDone(input_method::KeyEventHandle* key_data,
                            bool handled) OVERRIDE;
  virtual bool DeleteSurroundingText(int context_id,
                                     int offset,
                                     size_t number_of_chars,
                                     std::string* error) OVERRIDE;

  // IBusEngineHandlerInterface overrides.
  virtual void FocusIn() OVERRIDE;
  virtual void FocusOut() OVERRIDE;
  virtual void Enable() OVERRIDE;
  virtual void Disable() OVERRIDE;
  virtual void PropertyActivate(
      const std::string& property_name,
      ibus::IBusPropertyState property_state) OVERRIDE;
  virtual void PropertyShow(const std::string& property_name) OVERRIDE;
  virtual void PropertyHide(const std::string& property_name) OVERRIDE;
  virtual void SetCapability(IBusCapability capability) OVERRIDE;
  virtual void Reset() OVERRIDE;
  virtual void ProcessKeyEvent(uint32 keysym, uint32 keycode, uint32 state,
                               const KeyEventDoneCallback& callback) OVERRIDE;
  virtual void CandidateClicked(uint32 index, ibus::IBusMouseButton button,
                                uint32 state) OVERRIDE;
  virtual void SetSurroundingText(const std::string& text, uint32 cursor_pos,
                                  uint32 anchor_pos) OVERRIDE;

  // Called when the connection with ibus-daemon is connected.
  void OnConnected();

  // Called whtn the connection with ibus-daemon is disconnected.
  void OnDisconnected();

 private:
  // Returns true if the connection to ibus-daemon is avaiable.
  bool IsConnected();

  // Converts MenuItem to IBusProperty.
  bool MenuItemToProperty(const MenuItem& item, IBusProperty* property);

  // Registers the engine component.
  void RegisterComponent();

  // Called when the RegisterComponent is failed.
  void OnComponentRegistrationFailed();

  // Called when the RegisterComponent is succeeded.
  void OnComponentRegistered();

  // Called when the ibus-daemon sends CreateEngine message with corresponding
  // engine id.
  void CreateEngineHandler(
      const IBusEngineFactoryService::CreateEngineResponseSender& sender);

  // Returns current IBusEngineService, if there is no available service, this
  // function returns NULL.
  IBusEngineService* GetCurrentService();

  // True if the current context has focus.
  bool focused_;

  // True if this engine is active.
  bool active_;

  // ID that is used for the current input context.  False if there is no focus.
  int context_id_;

  // Next id that will be assigned to a context.
  int next_context_id_;

  // This IME ID in Chrome Extension.
  std::string engine_id_;

  // This IME ID in ibus.
  std::string ibus_id_;

  // The current object path.
  dbus::ObjectPath object_path_;

  // The current auxialy text and it's visiblity.
  scoped_ptr<IBusText> aux_text_;
  bool aux_text_visible_;

  // Pointer to the object recieving events for this IME.
  InputMethodEngine::Observer* observer_;

  // The current preedit text, and it's cursor position.
  scoped_ptr<IBusText> preedit_text_;
  int preedit_cursor_;

  // The current engine component.
  scoped_ptr<IBusComponent> component_;

  // The current lookup table.
  scoped_ptr<IBusLookupTable> table_;

  // Indicates whether the candidate window is visible.
  bool window_visible_;

  // Mapping of candidate index to candidate id.
  std::vector<int> candidate_ids_;

  // Mapping of candidate id to index.
  std::map<int, int> candidate_indexes_;

  // Used for making callbacks.
  base::WeakPtrFactory<InputMethodEngineIBus> weak_ptr_factory_;
};

}  // namespace chromeos

#endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_IBUS_H_