Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve config control #550

Merged
merged 4 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
317 changes: 202 additions & 115 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions nix/deps.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ with pkgs;
xlibs.libxcb
libGL
wayland

fontconfig
freetype
libxkbcommon

gtk2
gtk3
Expand All @@ -22,7 +20,6 @@ with pkgs;
];

kimeNativeBuildInputs = [
bash
python3 # xcb 0.9.0
pkg-config
llvmPackages_13.clang
Expand Down
7 changes: 4 additions & 3 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
}:
let
deps = import ./nix/deps.nix { pkgs = pkgs; };
stdenv = pkgs.llvmPackages_13.stdenv;
mkShell = (pkgs.mkShell.override { stdenv = stdenv; });
in
with pkgs;
llvmPackages_13.stdenv.mkDerivation {
mkShell {
name = "kime-shell";
dontUseCmakeConfigure = true;
dontWrapQtApps = true;
Expand All @@ -16,7 +17,7 @@ llvmPackages_13.stdenv.mkDerivation {
]);
CMAKE_EXPORT_COMPILE_COMMANDS = 1;
LIBCLANG_PATH = "${pkgs.llvmPackages_13.libclang.lib}/lib";
LD_LIBRARY_PATH = "./target/debug:${pkgs.wayland}/lib:${pkgs.libGL}/lib";
LD_LIBRARY_PATH = "./target/debug:${pkgs.wayland}/lib:${pkgs.libGL}/lib:${pkgs.libxkbcommon}/lib";
G_MESSAGES_DEBUG = "kime";
GTK_IM_MODULE = "kime";
GTK_IM_MODULE_FILE = builtins.toString ./.vscode/immodules.cache;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ bitflags = "1.2.1"
enum-map = "1.0.0"
enumset = { version = "1.0.4", features = ["serde"] }
serde = { version = "1.0.118", features = ["derive"] }
strum = { version = "0.22.0", features = ["derive"] }
strum = { version = "0.23", features = ["derive"] }
21 changes: 10 additions & 11 deletions src/engine/capi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(clippy::missing_safety_doc)]

pub use kime_engine_core::{
config_load_from_config_dir, Config, DaemonConfig, DaemonModule, IconColor, IndicatorConfig,
InputCategory, InputEngine, InputResult, LogConfig, ModifierState,
load_engine_config_from_config_dir, load_other_configs_from_config_dir, Config, DaemonConfig,
DaemonModule, IconColor, IndicatorConfig, InputCategory, InputEngine, InputResult, LogConfig,
ModifierState,
};

pub const KIME_API_VERSION: usize = 6;
Expand Down Expand Up @@ -156,9 +157,7 @@ pub extern "C" fn kime_engine_press_key(
#[cfg(unix)]
#[no_mangle]
pub extern "C" fn kime_config_load() -> *mut Config {
let config = config_load_from_config_dir()
.map(|c| c.0)
.unwrap_or_default();
let config = load_engine_config_from_config_dir().unwrap_or_default();
Box::into_raw(Box::new(config))
}

Expand Down Expand Up @@ -204,8 +203,8 @@ pub extern "C" fn kime_config_xim_preedit_font(config: &Config) -> FontData {
#[cfg(unix)]
#[no_mangle]
pub extern "C" fn kime_daemon_config_load() -> *mut DaemonConfig {
let config = config_load_from_config_dir()
.map(|c| c.1)
let config = load_other_configs_from_config_dir()
.map(|c| c.0)
.unwrap_or_default();
Box::into_raw(Box::new(config))
}
Expand Down Expand Up @@ -233,8 +232,8 @@ pub unsafe extern "C" fn kime_daemon_config_delete(config: *mut DaemonConfig) {
#[cfg(unix)]
#[no_mangle]
pub extern "C" fn kime_indicator_config_load() -> *mut IndicatorConfig {
let config = config_load_from_config_dir()
.map(|c| c.2)
let config = load_other_configs_from_config_dir()
.map(|c| c.1)
.unwrap_or_default();
Box::into_raw(Box::new(config))
}
Expand Down Expand Up @@ -262,8 +261,8 @@ pub extern "C" fn kime_indicator_config_icon_color(config: &IndicatorConfig) ->
#[cfg(unix)]
#[no_mangle]
pub extern "C" fn kime_log_config_load() -> *mut LogConfig {
let config = config_load_from_config_dir()
.map(|c| c.3)
let config = load_other_configs_from_config_dir()
.map(|c| c.2)
.unwrap_or_default();
Box::into_raw(Box::new(config))
}
Expand Down
6 changes: 5 additions & 1 deletion src/engine/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ kime-engine-backend-latin = { path = "../backends/latin" }
kime-engine-backend-math = { path = "../backends/math" }
kime-engine-backend-emoji = { path = "../backends/emoji" }
serde_yaml = "0.8.17"
font-loader = "0.11.0"
parking_lot = "0.11.2"
fontdb = "0.7.0"
fontconfig-parser = "0.5.0"

[target.'cfg(unix)'.dependencies]
xdg = "2.2.0"
Expand All @@ -27,3 +29,5 @@ pretty_assertions = "1.0.0"
[[bench]]
name = "call_key"
harness = false

[features]
59 changes: 37 additions & 22 deletions src/engine/core/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use font_loader::system_fonts::{self, FontPropertyBuilder};
use fontconfig_parser::FontConfig;
use fontdb::{Family, Query};
pub use kime_engine_config::*;

/// Preprocessed engine config
Expand All @@ -22,6 +23,25 @@ impl Default for Config {

impl Config {
fn new_impl(mut engine: EngineConfig, hangul_data: HangulData) -> Self {
let mut db = fontdb::Database::new();
db.load_system_fonts();

let mut fc = FontConfig::default();
fc.merge_config("/etc/fonts/fonts.conf").ok();

for dir in fc.dirs {
db.load_fonts_dir(dir.path);
}

let load_font = |name| {
db.query(&Query {
families: &[Family::Name(name), Family::Serif],
..Default::default()
})
.and_then(|id| db.with_face_data(id, |data, index| (data.to_vec(), index)))
.unwrap_or_default()
};

Self {
default_category: engine.default_category,
global_category_state: engine.global_category_state,
Expand Down Expand Up @@ -49,23 +69,12 @@ impl Config {
}
},
xim_preedit_font: {
let (font, index) = system_fonts::get(
&FontPropertyBuilder::new()
.family(&engine.xim_preedit_font.0)
.build(),
)
.map(|(d, i)| (d, i as u32))
.unwrap_or_default();
let (font, index) = load_font(&engine.xim_preedit_font.0);
(font, index, engine.xim_preedit_font.1)
},
candidate_font: {
system_fonts::get(
&FontPropertyBuilder::new()
.family(&engine.candidate_font)
.build(),
)
.map(|(d, i)| (d, i as u32))
.unwrap_or_default()
let (font, index) = load_font(&engine.candidate_font);
(font, index)
},
preferred_direct: engine.latin.preferred_direct,
latin_data: LatinData::new(&engine.latin),
Expand All @@ -90,17 +99,23 @@ impl Config {
}

#[cfg(unix)]
pub fn load_from_config_dir() -> Option<(Config, DaemonConfig, IndicatorConfig, LogConfig)> {
pub fn load_engine_config_from_config_dir() -> Option<Config> {
let dir = xdg::BaseDirectories::with_prefix("kime").ok()?;
let config: RawConfig = dir
.find_config_file("config.yaml")
.and_then(|config| serde_yaml::from_reader(std::fs::File::open(config).ok()?).ok())
.unwrap_or_default();

Some(Config::from_engine_config_with_dir(config.engine, &dir))
}

#[cfg(unix)]
pub fn load_other_configs_from_config_dir() -> Option<(DaemonConfig, IndicatorConfig, LogConfig)> {
let dir = xdg::BaseDirectories::with_prefix("kime").ok()?;
let config: RawConfig = dir
.find_config_file("config.yaml")
.and_then(|config| serde_yaml::from_reader(std::fs::File::open(config).ok()?).ok())
.unwrap_or_default();

Some((
Config::from_engine_config_with_dir(config.engine, &dir),
config.daemon,
config.indicator,
config.log,
))
Some((config.daemon, config.indicator, config.log))
}
2 changes: 1 addition & 1 deletion src/engine/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod config;
mod os;

#[cfg(unix)]
pub use config::load_from_config_dir as config_load_from_config_dir;
pub use config::{load_engine_config_from_config_dir, load_other_configs_from_config_dir};
pub use config::{
Config, DaemonConfig, DaemonModule, EngineConfig, EnumSet, Hotkey, IconColor, IndicatorConfig,
InputCategory, InputMode, LevelFilter, LogConfig, RawConfig,
Expand Down
4 changes: 2 additions & 2 deletions src/tools/candidate-window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ license = "GPL-3.0-or-later"

[dependencies]
kime-engine-cffi = { path = "../../engine/cffi" }
eframe = "0.14.0"
egui = "0.14.2"
eframe = "0.15.0"
egui = "0.15.0"
3 changes: 1 addition & 2 deletions src/tools/candidate-window/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ fn main() -> io::Result<()> {
initial_window_size: Some(egui::vec2(400.0, 400.0)),
resizable: false,
transparent: false,
maximized: false,
},
);

Ok(())
}
2 changes: 1 addition & 1 deletion src/tools/check/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ kime-engine-cffi = { path = "../../engine/cffi" }
kime-engine-core = { path = "../../engine/core" }
pad = "0.1.6"
serde_yaml = "0.8.17"
strum = { version = "0.22.0", features = ["derive"] }
strum = { version = "0.23", features = ["derive"] }
xdg = "2.2.0"
2 changes: 1 addition & 1 deletion src/tools/log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ license = "GPL-3.0-or-later"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
simplelog = "0.10.0"
simplelog = "0.11"