90 lines
2.7 KiB
C
90 lines
2.7 KiB
C
#include "fcitx4.h"
|
||
#include "../util.h"
|
||
|
||
#include <gio/gio.h>
|
||
#include <stdio.h>
|
||
#include <string.h>
|
||
|
||
struct input_method {
|
||
const char *name;
|
||
const char *symbol;
|
||
};
|
||
const struct input_method input_methods[] = {
|
||
{"fcitx-keyboard-us", "E"},
|
||
{"kkc", "和"},
|
||
{"mozc", "和"}
|
||
};
|
||
static const size_t im_count = sizeof(input_methods) / sizeof(struct input_method);
|
||
static const struct input_method *current_im = NULL;
|
||
|
||
static const struct input_method *find_im(const char *name) {
|
||
size_t i;
|
||
for (i = 0; i < im_count; ++i) {
|
||
if (strcmp(name, input_methods[i].name) == 0) {
|
||
return &input_methods[i];
|
||
}
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
static void set_current_im(GDBusConnection *dbus) {
|
||
current_im = NULL;
|
||
GVariant *tuple = g_dbus_connection_call_sync(
|
||
dbus, "org.fcitx.Fcitx", "/inputmethod", "org.fcitx.Fcitx.InputMethod",
|
||
"GetCurrentIM", NULL, G_VARIANT_TYPE_TUPLE, G_DBUS_CALL_FLAGS_NONE, -1,
|
||
NULL, NULL);
|
||
if (tuple) {
|
||
GVariant *response = g_variant_get_child_value(tuple, 0);
|
||
size_t response_len;
|
||
const char *response_im = g_variant_get_string(response, &response_len);
|
||
current_im = find_im(response_im);
|
||
g_variant_unref(response);
|
||
g_variant_unref(tuple);
|
||
}
|
||
}
|
||
|
||
static void im_changed_callback(GDBusConnection *dbus, const char *sender_name,
|
||
const char *object_path, const char *interface_name,
|
||
const char *signal_name, GVariant *params,
|
||
gpointer user_data) {
|
||
set_current_im(dbus);
|
||
qtb_signal_modules(3);
|
||
}
|
||
|
||
static void *thread_action(gpointer user_data) {
|
||
GMainContext *context = g_main_context_new();
|
||
g_main_context_push_thread_default(context);
|
||
GError *err = NULL;
|
||
GDBusConnection *dbus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &err);
|
||
if (err) {
|
||
qtb_log("could not connect to fcitx on dbus: %s", err->message);
|
||
qtb_die();
|
||
}
|
||
set_current_im(dbus);
|
||
qtb_signal_modules(3);
|
||
g_dbus_connection_signal_subscribe(
|
||
dbus, NULL, "org.freedesktop.DBus.Properties", "PropertiesChanged",
|
||
"/inputmethod", "org.fcitx.Fcitx.InputMethod", G_DBUS_SIGNAL_FLAGS_NONE,
|
||
&im_changed_callback, NULL, NULL);
|
||
GMainLoop *main_loop = g_main_loop_new(context, FALSE);
|
||
g_main_loop_run(main_loop);
|
||
return NULL;
|
||
}
|
||
|
||
void fcitx4_module_init() {
|
||
g_thread_new("fcitx-module", &thread_action, NULL);
|
||
}
|
||
|
||
char *fcitx4_module_poll() {
|
||
const char *symbol;
|
||
if (current_im) {
|
||
symbol = current_im->symbol;
|
||
} else {
|
||
symbol = "?";
|
||
}
|
||
size_t sym_len = strlen(symbol);
|
||
char *output = qtb_malloc(sizeof(" ") + sym_len);
|
||
sprintf(output, " %s", symbol);
|
||
return output;
|
||
}
|