Nux

Merge lp:~3v1n0/nux/cancellable-ibus into lp:nux

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Brandon Schaefer
Approved revision: 826
Merged at revision: 819
Proposed branch: lp:~3v1n0/nux/cancellable-ibus
Merge into: lp:nux
Diff against target: 307 lines (+69/-66)
2 files modified
Nux/InputMethodIBus.cpp (+68/-66)
Nux/InputMethodIBus.h (+1/-0)
To merge this branch: bzr merge lp:~3v1n0/nux/cancellable-ibus
Reviewer Review Type Date Requested Status
Brandon Schaefer (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+187813@code.launchpad.net

Commit message

InputMethodIBus: use GCancellable to ignore callback events after destruction

Also use nux logger and add some code cleanup

Description of the change

This will finally fixes the test-gtest crashes in unity when running ibus

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
lp:~3v1n0/nux/cancellable-ibus updated
826. By Marco Trevisan (Treviño)

InputMethodIBus: process the key event natevely only if ibus didn't handle that

Revision history for this message
Brandon Schaefer (brandontschaefer) wrote :

Cool LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Nux/InputMethodIBus.cpp'
2--- Nux/InputMethodIBus.cpp 2013-09-17 17:54:56 +0000
3+++ Nux/InputMethodIBus.cpp 2013-09-26 16:41:02 +0000
4@@ -22,20 +22,26 @@
5
6
7 #include "Nux.h"
8+#include "NuxCore/Logger.h"
9
10 #include "InputMethodIBus.h"
11
12 namespace nux
13 {
14+namespace
15+{
16+ DECLARE_LOGGER(logger, "nux.inputmethod.ibus");
17+}
18
19 std::vector<Event> IBusIMEContext::hotkeys_;
20
21 IBusBus* IBusIMEContext::bus_ = NULL;
22
23 IBusIMEContext::IBusIMEContext(TextEntry* text_entry)
24- : text_entry_(text_entry),
25- context_(NULL),
26- is_focused_(false)
27+ : text_entry_(text_entry)
28+ , context_(NULL)
29+ , cancellable_(g_cancellable_new())
30+ , is_focused_(false)
31 {
32 // init ibus
33 if (!bus_)
34@@ -54,17 +60,15 @@
35 }
36 else
37 {
38- nuxDebugMsg("[IBusIMEContext::IBusIMEContext] Can not connect to ibus");
39+ LOG_WARN(logger) << "Impossible to connect to connect to ibus";
40 }
41 }
42
43 IBusIMEContext::~IBusIMEContext()
44 {
45- // disconnect bus signals
46- g_signal_handlers_disconnect_by_func(bus_, reinterpret_cast<gpointer>(OnConnected_), this);
47- g_signal_handlers_disconnect_by_func(bus_, reinterpret_cast<gpointer>(OnDisconnected_), this);
48-
49 DestroyContext();
50+ g_signal_handlers_disconnect_by_data(bus_, this);
51+ g_object_unref(cancellable_);
52 }
53
54 void IBusIMEContext::Focus()
55@@ -98,7 +102,8 @@
56 {
57 guint keyval = event.key_sym(); // todo(jaytaoko): ui::GdkKeyCodeForWindowsKeyCode(event.key_code(), event.IsShiftDown() ^ event.IsCapsLockDown());
58
59- if (context_) {
60+ if (context_)
61+ {
62 guint modifiers = 0;
63
64 if (event.flags() & IBUS_IGNORED_MASK)
65@@ -117,9 +122,7 @@
66 modifiers |= IBUS_LOCK_MASK;
67
68 ibus_input_context_process_key_event_async(context_,
69- keyval, event.key_code() - 8, modifiers,
70- -1,
71- NULL,
72+ keyval, event.key_code() - 8, modifiers, -1, cancellable_,
73 reinterpret_cast<GAsyncReadyCallback>(ProcessKeyEventDone),
74 new ProcessKeyEventData(this, event));
75
76@@ -133,16 +136,18 @@
77 // TODO(penghuang) support surrounding
78 }
79
80- void IBusIMEContext::CreateContext() {
81+ void IBusIMEContext::CreateContext()
82+ {
83 nuxAssert(bus_ != NULL);
84 nuxAssert(ibus_bus_is_connected(bus_));
85
86 if (!(context_ = ibus_bus_create_input_context(bus_, "nux")))
87 {
88- nuxDebugMsg("[IBusIMEContext::IBusIMEContext] Cannot create InputContext");
89+ LOG_WARN(logger) << "Cannot create InputContext";
90 return;
91 }
92
93+ g_cancellable_reset(cancellable_);
94 text_entry_->ime_active_ = false;
95
96 // connect input context signals
97@@ -164,10 +169,7 @@
98
99 UpdateCursorLocation();
100
101- IBusConfig* bus_conf = ibus_bus_get_config(bus_);
102-
103- // may be null if not connected to bus or can't get ibus name
104- if (bus_conf)
105+ if (IBusConfig* bus_conf = ibus_bus_get_config(bus_))
106 {
107 g_signal_handlers_disconnect_by_func(bus_conf, reinterpret_cast<gpointer>(OnConfigChanged_), this);
108 g_signal_connect(bus_conf, "value-changed", G_CALLBACK(OnConfigChanged_), this);
109@@ -178,17 +180,12 @@
110
111 void IBusIMEContext::DestroyContext()
112 {
113- //nuxDebugMsg("***IBusIMEContext::DestroyContext***");
114+ LOG_DEBUG(logger) << "Destroy context";
115
116 if (ibus_bus_is_connected(bus_))
117 {
118- IBusConfig* bus_conf = ibus_bus_get_config(bus_);
119-
120- // may be null if not connected to bus or can't get ibus name
121- if (bus_conf)
122- {
123- g_signal_handlers_disconnect_by_func(bus_conf, reinterpret_cast<gpointer>(OnConfigChanged_), this);
124- }
125+ if (IBusConfig* bus_conf = ibus_bus_get_config(bus_))
126+ g_signal_handlers_disconnect_by_data(bus_conf, this);
127 }
128
129 if (!context_)
130@@ -196,7 +193,6 @@
131
132 text_entry_->ResetPreedit();
133 ibus_proxy_destroy(reinterpret_cast<IBusProxy *>(context_));
134-
135 nuxAssert(!context_);
136 }
137
138@@ -225,7 +221,8 @@
139
140 void IBusIMEContext::OnConnected(IBusBus * /* bus */)
141 {
142- //nuxDebugMsg("***IBusIMEContext::OnConnected***");
143+ LOG_DEBUG(logger) << "Connected";
144+
145 if (ibus_bus_is_connected(bus_))
146 {
147 DestroyContext();
148@@ -235,9 +232,9 @@
149
150 void IBusIMEContext::OnDisconnected(IBusBus * /* bus */)
151 {
152- //nuxDebugMsg("***IBusIMEContext::OnDisonnected***");
153+ LOG_DEBUG(logger) << "Disconnected";
154+
155 hotkeys_.clear();
156-
157 DestroyContext();
158 }
159
160@@ -252,7 +249,7 @@
161
162 void IBusIMEContext::OnCommitText(IBusInputContext * /* context */, IBusText* text)
163 {
164- //nuxDebugMsg("***IBusIMEContext::OnCommitText::%s***", text->text);
165+ LOG_DEBUG(logger) << "Text committed " << text->text;
166
167 text_entry_->DeleteSelection();
168
169@@ -271,7 +268,7 @@
170
171 void IBusIMEContext::OnUpdatePreeditText(IBusInputContext* /* context */, IBusText* text, guint /* cursor_pos */, gboolean visible)
172 {
173- //nuxDebugMsg("***IBusIMEContext::OnUpdatePreeditText***");
174+ LOG_DEBUG(logger) << "Preedit Update";
175 nuxAssert(IBUS_IS_TEXT(text));
176
177 if (text_entry_->preedit_.empty())
178@@ -337,12 +334,12 @@
179
180 void IBusIMEContext::OnShowPreeditText(IBusInputContext * /* context */)
181 {
182- //nuxDebugMsg("***IBusIMEContext::OnShowPreeditText***");
183+ LOG_DEBUG(logger) << "Show preedit";
184 }
185
186 void IBusIMEContext::OnHidePreeditText(IBusInputContext * /* context */)
187 {
188- //nuxDebugMsg("***IBusIMEContext::OnHidePreeditText***");
189+ LOG_DEBUG(logger) << "Hide preedit";
190
191 text_entry_->ResetPreedit();
192 text_entry_->QueueRefresh (true, true);
193@@ -350,7 +347,7 @@
194
195 void IBusIMEContext::OnEnable(IBusInputContext * /* context */)
196 {
197- //nuxDebugMsg("***IBusIMEContext::OnEnable***");
198+ LOG_DEBUG(logger) << "On enable";
199
200 text_entry_->ime_active_ = true;
201 text_entry_->text_changed.emit(text_entry_);
202@@ -359,7 +356,7 @@
203
204 void IBusIMEContext::OnDisable(IBusInputContext * /* context */)
205 {
206- //nuxDebugMsg("***IBusIMEContext::OnDisable***");
207+ LOG_DEBUG(logger) << "On disable";
208 text_entry_->ime_active_ = false;
209 text_entry_->ResetPreedit();
210 text_entry_->QueueRefresh (true, true);
211@@ -367,38 +364,46 @@
212
213 void IBusIMEContext::OnDestroy(IBusInputContext * /* context */)
214 {
215- //nuxDebugMsg("***IBusIMEContext::OnDestroy***");
216-
217+ LOG_DEBUG(logger) << "On Destroy";
218+
219+ if (!context_)
220+ return;
221+
222+ g_cancellable_cancel(cancellable_);
223+ g_signal_handlers_disconnect_by_data(context_, this);
224 g_object_unref(context_);
225 context_ = NULL;
226 }
227
228 void IBusIMEContext::ProcessKeyEventDone(IBusInputContext *context, GAsyncResult* res, ProcessKeyEventData *data)
229 {
230- //nuxDebugMsg("***IBusIMEContext::ProcessKeyEventDone***");
231- std::unique_ptr<ProcessKeyEventData> key_ev(data);
232- nuxAssert(key_ev->context->context_ == context);
233-
234- GError *error = NULL;
235- gboolean processed = ibus_input_context_process_key_event_async_finish (
236- context,
237- res,
238- &error);
239-
240- if (error)
241- {
242- g_warning ("Process Key Event failed: %s.", error->message);
243- g_error_free (error);
244- }
245-
246- if (!processed)
247- {
248- key_ev->context->text_entry_->ProcessKeyEvent(key_ev->event.type(),
249- key_ev->event.key_sym(),
250- key_ev->event.flags() | IBUS_IGNORED_MASK,
251- key_ev->event.character().c_str(),
252- 0);
253- }
254+ LOG_DEBUG(logger) << "Key event processed";
255+ std::unique_ptr<ProcessKeyEventData> key_ev(data);
256+
257+ GError *error = NULL;
258+ gboolean processed = ibus_input_context_process_key_event_async_finish(context, res, &error);
259+
260+ if (error)
261+ {
262+ if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
263+ {
264+ LOG_ERROR(logger) << "Process Key Event failed: " << error->message << ".";
265+ }
266+
267+ g_error_free(error);
268+ return;
269+ }
270+
271+ nuxAssert(key_ev->context->context_ == context);
272+
273+ if (!processed)
274+ {
275+ key_ev->context->text_entry_->ProcessKeyEvent(key_ev->event.type(),
276+ key_ev->event.key_sym(),
277+ key_ev->event.flags() | IBUS_IGNORED_MASK,
278+ key_ev->event.character().c_str(),
279+ 0);
280+ }
281 }
282
283 std::vector<Event> IBusIMEContext::ParseIBusHotkeys(const gchar** keybindings)
284@@ -491,10 +496,7 @@
285
286 void IBusIMEContext::UpdateHotkeys()
287 {
288- IBusConfig* conf = ibus_bus_get_config(bus_);
289-
290- // may be null if not connected to bus or can't get ibus name
291- if (conf)
292+ if (IBusConfig* conf = ibus_bus_get_config(bus_))
293 {
294 GVariant* val = ibus_config_get_value(conf, "general/hotkey", "triggers");
295 const gchar** keybindings = g_variant_get_strv(val, NULL);
296
297=== modified file 'Nux/InputMethodIBus.h'
298--- Nux/InputMethodIBus.h 2012-06-26 22:35:16 +0000
299+++ Nux/InputMethodIBus.h 2013-09-26 16:41:02 +0000
300@@ -161,6 +161,7 @@
301
302 TextEntry* text_entry_;
303 IBusInputContext* context_;
304+ GCancellable* cancellable_;
305 bool is_focused_;
306
307 static IBusBus* bus_;

Subscribers

People subscribed via source and target branches