[Ask] Dbus async api issue
I pass the key sequence to fcitx through DBus and asynchronously accept the dbus response message. The following is my specific implementation code:
static Object FXNative_eventLoop() {
DBusError error;
dbus_error_init(&error);
DBusConnection *connection = context->connection;
dbus_connection_add_filter(connection, FXNative_message, NULL, NULL);
dbus_bus_add_match(connection, "type='signal',interface='org.fcitx.Fcitx.InputContext1'", &error);
if (dbus_error_is_set(&error)) {
g_warning("Add dbus fcitx signal fail:%s", error.message);
dbus_error_free(&error);
return NULL;
}
while (dbus_connection_read_write_dispatch(connection, 20));
return NULL;
}
int main(int argc,String *argv){
// Use a thread loop event
g_thread_new("FXNative_eventLoop", FXNative_eventLoop, NULL);
}
The following code is for me to transfer the keyboard sequence to fcitx through the dbus interface through gtk:
bool FXNative_processKeyEvent(InputContext *context, KeywordEvent event) {
DBusMessage *message = dbus_message_new_method_call(
"org.freedesktop.portal.Fcitx",
context->objectPath, "org.fcitx.Fcitx.InputContext1",
"ProcessKeyEvent"
);
bool type = event.type;
uint32 time = event.time;
uint32 state = event.state;
uint32 keyVal = event.keyVal;
uint32 keyCode = event.keyCode;
//Append method args
dbus_message_append_args(
message,
DBUS_TYPE_UINT32, &keyVal,
DBUS_TYPE_UINT32, &keyCode,
DBUS_TYPE_UINT32, &state,
DBUS_TYPE_BOOLEAN, &type,
DBUS_TYPE_UINT32, &time,
DBUS_TYPE_INVALID
);
dbus_connection_send_with_reply_and_block(context->connection, message, 20, NULL);
// bool success = dbus_connection_send(context->connection, message, &serial);
// if (!success) {
// fprintf(stderr, "Send keyword event fail:OutOfMemory.\n");
// }
// serial++;
// return success;
}
dbus_connection_send_with_reply_and_block method will always block the gtk main loop,if I change the method to dbus_connection_send, it will not block,But there's a problem with this. I can't immediately know the results returned by fcitx.