Skip to content

Commit ec293bd

Browse files
authored
Merge pull request #57 from mahkoh/jorth/dump-x11
cli: add dump-x11 command
2 parents ddf2298 + d90f80c commit ec293bd

File tree

7 files changed

+106
-85
lines changed

7 files changed

+106
-85
lines changed

Cargo.lock

Lines changed: 30 additions & 80 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kbvm-cli/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ utf8-console = "0.1.0"
3232

3333
[target.'cfg(unix)'.dependencies]
3434
wl-client = "0.2.0"
35+
x11rb = { version = "0.13.2", default-features = false }
36+
kbvm = { version = "0.1.5", path = "../kbvm", features = ["x11"] }
3537

3638
[target.'cfg(target_os = "linux")'.dependencies]
3739
uapi = "0.2.13"

kbvm-cli/src/cli.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#[cfg(unix)]
2+
use crate::dump_x11::{self, DumpX11Args};
3+
#[cfg(unix)]
24
use crate::test_wayland::{self, TestWaylandArgs};
35
use {
46
crate::{
@@ -31,6 +33,9 @@ enum Cmd {
3133
CompileXkb(CompileXkbArgs),
3234
/// Generate shell completion scripts for kbvm.
3335
GenerateCompletion(GenerateArgs),
36+
/// Loads the keymap from the X server and writes it to stdout.
37+
#[cfg(unix)]
38+
DumpX11(DumpX11Args),
3439
}
3540

3641
#[derive(Args, Debug, Default)]
@@ -44,6 +49,10 @@ pub struct CompileArgs {
4449
/// Append an include path.
4550
#[clap(long, value_hint = ValueHint::DirPath)]
4651
pub append_include: Vec<String>,
52+
}
53+
54+
#[derive(Args, Debug, Default)]
55+
pub struct FormatArgs {
4756
/// Don't include key actions or behaviors.
4857
#[clap(long)]
4958
pub lookup_only: bool,
@@ -64,8 +73,10 @@ impl CompileArgs {
6473
builder.append_path(dir);
6574
}
6675
}
76+
}
6777

68-
pub fn apply2<'a>(&self, mut formatter: Formatter<'a>) -> Formatter<'a> {
78+
impl FormatArgs {
79+
pub fn apply<'a>(&self, mut formatter: Formatter<'a>) -> Formatter<'a> {
6980
if self.lookup_only {
7081
formatter = formatter.lookup_only(true);
7182
}
@@ -85,5 +96,7 @@ pub fn main() {
8596
Cmd::CompileRmlvo(args) => compile_rmlvo::main(args),
8697
Cmd::CompileXkb(args) => compile_xkb::main(args),
8798
Cmd::GenerateCompletion(args) => generate::main(args),
99+
#[cfg(unix)]
100+
Cmd::DumpX11(args) => dump_x11::main(args),
88101
}
89102
}

kbvm-cli/src/compile_rmlvo.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use {
2-
crate::{cli::CompileArgs, compile_xkb::format_keymap, expand_rmlvo::RmlvoArgs},
2+
crate::{
3+
cli::{CompileArgs, FormatArgs},
4+
compile_xkb::format_keymap,
5+
expand_rmlvo::RmlvoArgs,
6+
},
37
clap::Args,
48
kbvm::xkb::{Context, diagnostic::WriteToLog},
59
};
@@ -9,6 +13,8 @@ pub struct CompileRmlvoArgs {
913
#[clap(flatten)]
1014
compile_args: CompileArgs,
1115
#[clap(flatten)]
16+
format_args: FormatArgs,
17+
#[clap(flatten)]
1218
rmlvo: RmlvoArgs,
1319
}
1420

@@ -24,5 +30,5 @@ pub fn main(args: CompileRmlvoArgs) {
2430
groups.as_deref(),
2531
options.as_deref(),
2632
);
27-
format_keymap(args.compile_args.apply2(expanded.format()));
33+
format_keymap(args.format_args.apply(expanded.format()));
2834
}

kbvm-cli/src/compile_xkb.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use {
2-
crate::{cli::CompileArgs, utils::read_path},
2+
crate::{
3+
cli::{CompileArgs, FormatArgs},
4+
utils::read_path,
5+
},
36
clap::{Args, ValueHint},
47
error_reporter::Report,
58
kbvm::xkb::{Context, diagnostic::WriteToLog},
@@ -14,6 +17,8 @@ use {
1417
pub struct CompileXkbArgs {
1518
#[clap(flatten)]
1619
compile_args: CompileArgs,
20+
#[clap(flatten)]
21+
format_args: FormatArgs,
1722
/// The path to the keymap.
1823
///
1924
/// If the path is not specified or if the path is `-`, the keymap is read from stdin.
@@ -30,7 +35,7 @@ pub fn main(args: CompileXkbArgs) {
3035
let expanded = context.keymap_from_bytes(WriteToLog, Some(path.as_ref()), &source);
3136
match expanded {
3237
Ok(map) => {
33-
format_keymap(args.compile_args.apply2(map.format()));
38+
format_keymap(args.format_args.apply(map.format()));
3439
}
3540
Err(_) => {
3641
log::error!("could not compile keymap");

kbvm-cli/src/dump_x11.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use {
2+
crate::{cli::FormatArgs, compile_xkb::format_keymap},
3+
clap::Args,
4+
error_reporter::Report,
5+
kbvm::xkb::x11::KbvmX11Ext,
6+
};
7+
8+
#[derive(Args, Debug, Default)]
9+
pub struct DumpX11Args {
10+
#[clap(flatten)]
11+
format_args: FormatArgs,
12+
}
13+
14+
pub fn main(args: DumpX11Args) {
15+
let (con, _) = match x11rb::connect(None) {
16+
Ok(c) => c,
17+
Err(e) => {
18+
log::error!("could not connect to X11: {}", Report::new(e));
19+
std::process::exit(1);
20+
}
21+
};
22+
if let Err(e) = con.setup_xkb_extension() {
23+
log::error!("could not enable the XKB extension: {}", Report::new(e));
24+
std::process::exit(1);
25+
}
26+
let dev_id = match con.get_xkb_core_device_id() {
27+
Ok(i) => i,
28+
Err(e) => {
29+
log::error!("could not retrieve the core device ID: {}", Report::new(e));
30+
std::process::exit(1);
31+
}
32+
};
33+
let expanded = con.get_xkb_keymap(dev_id);
34+
match expanded {
35+
Ok(map) => {
36+
format_keymap(args.format_args.apply(map.format()));
37+
}
38+
Err(e) => {
39+
log::error!("could not retrieve the keymap from X11: {}", Report::new(e));
40+
std::process::exit(1);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)