Skip to content

Commit

Permalink
rime_settings: load settings from ibus_rime.yaml
Browse files Browse the repository at this point in the history
Fixes #39: migrate away from IBusConfig
  • Loading branch information
lotem committed Feb 12, 2018
1 parent cdb5d00 commit c8492d7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 91 deletions.
16 changes: 2 additions & 14 deletions rime_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static void notification_handler(void* context_object,
}
else if (!strcmp(message_value, "success")) {
show_message(_("Rime is ready."), NULL);
ibus_rime_load_settings();
}
else if (!strcmp(message_value, "failure")) {
show_message(_("Rime has encountered an error."),
Expand Down Expand Up @@ -114,17 +115,6 @@ static void rime_with_ibus() {

g_signal_connect(bus, "disconnected", G_CALLBACK(ibus_disconnect_cb), NULL);

IBusConfig *config = ibus_bus_get_config(bus);
if (!config) {
g_warning("ibus config not accessible");
}
else {
g_object_ref_sink(config);
ibus_rime_load_settings(config);
g_signal_connect(config, "value-changed",
G_CALLBACK(ibus_rime_config_value_changed_cb), NULL);
}

IBusFactory *factory = ibus_factory_new(ibus_bus_get_connection(bus));
g_object_ref_sink(factory);

Expand All @@ -144,16 +134,14 @@ static void rime_with_ibus() {

gboolean full_check = FALSE;
ibus_rime_start(full_check);
ibus_rime_load_settings();

ibus_main();

RimeFinalize();
unload_plugin_modules();
notify_uninit();

if (config) {
g_object_unref(config);
}
g_object_unref(factory);
g_object_unref(bus);
}
Expand Down
95 changes: 28 additions & 67 deletions rime_settings.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "rime_config.h"
#include <string.h>
#include <ibus.h>
#include <rime_api.h>
#include "rime_settings.h"

static struct ColorSchemeDefinition preset_color_schemes[] = {
Expand All @@ -11,97 +12,57 @@ static struct ColorSchemeDefinition preset_color_schemes[] = {
{ NULL, 0, 0 }
};

struct IBusRimeSettings g_ibus_rime_settings = {
static struct IBusRimeSettings ibus_rime_settings_default = {
FALSE,
IBUS_ORIENTATION_SYSTEM,
&preset_color_schemes[0],
};

struct IBusRimeSettings g_ibus_rime_settings;

static void
ibus_rime_select_color_scheme(const char* color_scheme_id)
select_color_scheme(struct IBusRimeSettings* settings,
const char* color_scheme_id)
{
struct ColorSchemeDefinition* c;
for (c = preset_color_schemes; c->color_scheme_id; ++c) {
if (!strcmp(c->color_scheme_id, color_scheme_id)) {
g_ibus_rime_settings.color_scheme = c;
settings->color_scheme = c;
g_debug("selected color scheme: %s", color_scheme_id);
return;
}
}
g_ibus_rime_settings.color_scheme = &preset_color_schemes[0];
// fallback to default
settings->color_scheme = &preset_color_schemes[0];
}

void
ibus_rime_load_settings(IBusConfig* config)
ibus_rime_load_settings()
{
//g_debug("ibus_rime_load_settings");
GVariant* value;
g_ibus_rime_settings = ibus_rime_settings_default;

value = ibus_config_get_value(config, "engine/Rime", "embed_preedit_text");
if (!value) {
value = ibus_config_get_value(config, "general", "embed_preedit_text");
}
if (value && g_variant_classify(value) == G_VARIANT_CLASS_BOOLEAN) {
g_ibus_rime_settings.embed_preedit_text = g_variant_get_boolean(value);
RimeConfig config = {0};
if (!RimeConfigOpen("ibus_rime", &config)) {
g_error("error loading settings for ibus_rime");
return;
}

value = ibus_config_get_value(config, "engine/Rime", "lookup_table_orientation");
if (!value) {
value = ibus_config_get_value(config, "panel", "lookup_table_orientation");
}
if (value && g_variant_classify(value) == G_VARIANT_CLASS_INT32) {
g_ibus_rime_settings.lookup_table_orientation = g_variant_get_int32(value);
Bool inline_preedit = False;
if (RimeConfigGetBool(&config, "style/inline_preedit", &inline_preedit)) {
g_ibus_rime_settings.embed_preedit_text = !!inline_preedit;
}

value = ibus_config_get_value(config, "engine/Rime", "color_scheme");
if (value && g_variant_classify(value) == G_VARIANT_CLASS_STRING) {
ibus_rime_select_color_scheme(g_variant_get_string(value, NULL));
Bool horizontal = False;
if (RimeConfigGetBool(&config, "style/horizontal", &horizontal)) {
g_ibus_rime_settings.lookup_table_orientation =
horizontal ? IBUS_ORIENTATION_HORIZONTAL : IBUS_ORIENTATION_VERTICAL;
}
}

void
ibus_rime_config_value_changed_cb(IBusConfig* config,
const gchar* section,
const gchar* name,
GVariant* value,
gpointer unused)
{
//g_debug("ibus_rime_config_value_changed_cb [%s/%s]", section, name);
if (!strcmp("general", section)) {
if (!strcmp("embed_preedit_text", name) &&
value && g_variant_classify(value) == G_VARIANT_CLASS_BOOLEAN) {
g_ibus_rime_settings.embed_preedit_text = g_variant_get_boolean(value);
return;
}
}
else if (!strcmp("panel", section)) {
if (!strcmp("lookup_table_orientation", name) &&
value && g_variant_classify(value) == G_VARIANT_CLASS_INT32) {
g_ibus_rime_settings.lookup_table_orientation = g_variant_get_int32(value);
return;
}
}
else if (!strcmp("engine/Rime", section)) {
if (!strcmp("embed_preedit_text", name) &&
value && g_variant_classify(value) == G_VARIANT_CLASS_BOOLEAN) {
GVariant* overridden = ibus_config_get_value(config, "engine/Rime", "embed_preedit_text");
if (!overridden) {
g_ibus_rime_settings.embed_preedit_text = g_variant_get_boolean(value);
}
return;
}
if (!strcmp("lookup_table_orientation", name) &&
value && g_variant_classify(value) == G_VARIANT_CLASS_INT32) {
GVariant* overridden = ibus_config_get_value(config, "engine/Rime", "lookup_table_orientation");
if (!overridden) {
g_ibus_rime_settings.lookup_table_orientation = g_variant_get_int32(value);
}
return;
}
if (!strcmp("color_scheme", name) &&
value && g_variant_classify(value) == G_VARIANT_CLASS_STRING) {
ibus_rime_select_color_scheme(g_variant_get_string(value, NULL));
return;
}
const char* color_scheme =
RimeConfigGetCString(&config, "style/color_scheme");
if (color_scheme) {
select_color_scheme(&g_ibus_rime_settings, color_scheme);
}

RimeConfigClose(&config);
}
11 changes: 1 addition & 10 deletions rime_settings.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef __IBUS_RIME_SETTINGS_H__
#define __IBUS_RIME_SETTINGS_H__

#include <glib.h>

// colors

#define RIME_COLOR_LIGHT 0xd4d4d4
Expand All @@ -26,13 +24,6 @@ struct IBusRimeSettings {
struct IBusRimeSettings g_ibus_rime_settings;

void
ibus_rime_load_settings(IBusConfig* config);

void
ibus_rime_config_value_changed_cb(IBusConfig* config,
const gchar* section,
const gchar* name,
GVariant* value,
gpointer unused);
ibus_rime_load_settings();

#endif

1 comment on commit c8492d7

@chenzhiwei
Copy link

@chenzhiwei chenzhiwei commented on c8492d7 Oct 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lotem , thank you for changing this to make it consist with macOS and Windows. I really appreciate this change.

I am currently using Ubuntu 18.10 beta and I manually created a config file ~/.config/ibus/rime/build/ibus_rime.yaml with following content:

style/horizontal: true

The orientation is still vertical, I don't know how to debug this issue, just re-compiled the ibus-engine-rime binary with hardcoded orientation to IBUS_ORIENTATION_HORIZONTAL.

By the way, the ibus_rime.custom.yaml does not work, it will not generate the final ibus_rime.yaml file.

Since Ubuntu 18.10 will be released two weeks later(18th October 2018), so I think it is better to solve this issue before the date.

My Rime version:

ibus-rime: 1.3.0-1
librime1: 1.3.1+dfsg1-1

Please sign in to comment.