config: add support for mod masks in shortcuts
This commit is contained in:
parent
27f30f8d28
commit
90dbde99ab
15 changed files with 501 additions and 92 deletions
|
|
@ -17,7 +17,7 @@ use {
|
|||
},
|
||||
jay_config::{
|
||||
input::acceleration::AccelProfile,
|
||||
keyboard::{Keymap, ModifiedKeySym},
|
||||
keyboard::{mods::Modifiers, Keymap, ModifiedKeySym},
|
||||
logging::LogLevel,
|
||||
status::MessageFormat,
|
||||
theme::Color,
|
||||
|
|
@ -280,11 +280,18 @@ pub struct RepeatRate {
|
|||
pub delay: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Shortcut {
|
||||
pub mask: Modifiers,
|
||||
pub keysym: ModifiedKeySym,
|
||||
pub action: Action,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Config {
|
||||
pub keymap: Option<ConfigKeymap>,
|
||||
pub repeat_rate: Option<RepeatRate>,
|
||||
pub shortcuts: Vec<(ModifiedKeySym, Action)>,
|
||||
pub shortcuts: Vec<Shortcut>,
|
||||
pub on_graphics_initialized: Option<Action>,
|
||||
pub on_idle: Option<Action>,
|
||||
pub status: Option<Status>,
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ use {
|
|||
log_level::LogLevelParser,
|
||||
output::OutputsParser,
|
||||
repeat_rate::RepeatRateParser,
|
||||
shortcuts::{ShortcutsParser, ShortcutsParserError},
|
||||
shortcuts::{ComplexShortcutsParser, ShortcutsParser, ShortcutsParserError},
|
||||
status::StatusParser,
|
||||
theme::ThemeParser,
|
||||
},
|
||||
|
|
@ -30,6 +30,7 @@ use {
|
|||
},
|
||||
},
|
||||
indexmap::IndexMap,
|
||||
std::collections::HashSet,
|
||||
thiserror::Error,
|
||||
};
|
||||
|
||||
|
|
@ -96,7 +97,7 @@ impl Parser for ConfigParser<'_> {
|
|||
_,
|
||||
idle_val,
|
||||
),
|
||||
(explicit_sync, repeat_rate_val),
|
||||
(explicit_sync, repeat_rate_val, complex_shortcuts_val),
|
||||
) = ext.extract((
|
||||
(
|
||||
opt(val("keymap")),
|
||||
|
|
@ -122,7 +123,11 @@ impl Parser for ConfigParser<'_> {
|
|||
opt(val("$schema")),
|
||||
opt(val("idle")),
|
||||
),
|
||||
(recover(opt(bol("explicit-sync"))), opt(val("repeat-rate"))),
|
||||
(
|
||||
recover(opt(bol("explicit-sync"))),
|
||||
opt(val("repeat-rate")),
|
||||
opt(val("complex-shortcuts")),
|
||||
),
|
||||
))?;
|
||||
let mut keymap = None;
|
||||
if let Some(value) = keymap_val {
|
||||
|
|
@ -136,10 +141,24 @@ impl Parser for ConfigParser<'_> {
|
|||
}
|
||||
}
|
||||
}
|
||||
let mut used_keys = HashSet::new();
|
||||
let mut shortcuts = vec![];
|
||||
if let Some(value) = shortcuts_val {
|
||||
shortcuts = value
|
||||
.parse(&mut ShortcutsParser(self.0))
|
||||
value
|
||||
.parse(&mut ShortcutsParser {
|
||||
cx: self.0,
|
||||
used_keys: &mut used_keys,
|
||||
shortcuts: &mut shortcuts,
|
||||
})
|
||||
.map_spanned_err(ConfigParserError::ParseShortcuts)?;
|
||||
}
|
||||
if let Some(value) = complex_shortcuts_val {
|
||||
value
|
||||
.parse(&mut ComplexShortcutsParser {
|
||||
cx: self.0,
|
||||
used_keys: &mut used_keys,
|
||||
shortcuts: &mut shortcuts,
|
||||
})
|
||||
.map_spanned_err(ConfigParserError::ParseShortcuts)?;
|
||||
}
|
||||
if shortcuts.is_empty() {
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ pub enum ModifiedKeysymParserError {
|
|||
MissingSym,
|
||||
#[error("Unknown keysym {0}")]
|
||||
UnknownKeysym(String),
|
||||
#[error("Unknown modifier {0}")]
|
||||
UnknownModifier(String),
|
||||
}
|
||||
|
||||
pub struct ModifiedKeysymParser;
|
||||
|
|
@ -39,20 +41,8 @@ impl Parser for ModifiedKeysymParser {
|
|||
let mut modifiers = Modifiers(0);
|
||||
let mut sym = None;
|
||||
for part in string.split("-") {
|
||||
let modifier = match part {
|
||||
"shift" => SHIFT,
|
||||
"lock" => LOCK,
|
||||
"ctrl" => CTRL,
|
||||
"mod1" => MOD1,
|
||||
"mod2" => MOD2,
|
||||
"mod3" => MOD3,
|
||||
"mod4" => MOD4,
|
||||
"mod5" => MOD5,
|
||||
"caps" => CAPS,
|
||||
"alt" => ALT,
|
||||
"num" => NUM,
|
||||
"logo" => LOGO,
|
||||
"release" => RELEASE,
|
||||
let modifier = match parse_mod(part) {
|
||||
Some(m) => m,
|
||||
_ => match KEYSYMS.get(part) {
|
||||
Some(new) if sym.is_none() => {
|
||||
sym = Some(*new);
|
||||
|
|
@ -73,3 +63,46 @@ impl Parser for ModifiedKeysymParser {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ModifiersParser;
|
||||
|
||||
impl Parser for ModifiersParser {
|
||||
type Value = Modifiers;
|
||||
type Error = ModifiedKeysymParserError;
|
||||
const EXPECTED: &'static [DataType] = &[DataType::String];
|
||||
|
||||
fn parse_string(&mut self, span: Span, string: &str) -> ParseResult<Self> {
|
||||
let mut modifiers = Modifiers(0);
|
||||
if !string.is_empty() {
|
||||
for part in string.split("-") {
|
||||
let Some(modifier) = parse_mod(part) else {
|
||||
return Err(
|
||||
ModifiedKeysymParserError::UnknownModifier(part.to_string()).spanned(span)
|
||||
);
|
||||
};
|
||||
modifiers |= modifier;
|
||||
}
|
||||
}
|
||||
Ok(modifiers)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_mod(part: &str) -> Option<Modifiers> {
|
||||
let modifier = match part {
|
||||
"shift" => SHIFT,
|
||||
"lock" => LOCK,
|
||||
"ctrl" => CTRL,
|
||||
"mod1" => MOD1,
|
||||
"mod2" => MOD2,
|
||||
"mod3" => MOD3,
|
||||
"mod4" => MOD4,
|
||||
"mod5" => MOD5,
|
||||
"caps" => CAPS,
|
||||
"alt" => ALT,
|
||||
"num" => NUM,
|
||||
"logo" => LOGO,
|
||||
"release" => RELEASE,
|
||||
_ => return None,
|
||||
};
|
||||
Some(modifier)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,16 @@ use {
|
|||
crate::{
|
||||
config::{
|
||||
context::Context,
|
||||
extractor::{opt, str, val, Extractor, ExtractorError},
|
||||
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
|
||||
parsers::{action::ActionParser, modified_keysym::ModifiedKeysymParser},
|
||||
Action,
|
||||
parsers::{
|
||||
action::{ActionParser, ActionParserError},
|
||||
modified_keysym::{
|
||||
ModifiedKeysymParser, ModifiedKeysymParserError, ModifiersParser,
|
||||
},
|
||||
},
|
||||
spanned::SpannedErrorExt,
|
||||
Action, Shortcut, SimpleCommand,
|
||||
},
|
||||
toml::{
|
||||
toml_span::{Span, Spanned, SpannedExt},
|
||||
|
|
@ -12,7 +19,7 @@ use {
|
|||
},
|
||||
},
|
||||
indexmap::IndexMap,
|
||||
jay_config::keyboard::ModifiedKeySym,
|
||||
jay_config::keyboard::{mods::Modifiers, ModifiedKeySym},
|
||||
std::collections::HashSet,
|
||||
thiserror::Error,
|
||||
};
|
||||
|
|
@ -21,12 +28,22 @@ use {
|
|||
pub enum ShortcutsParserError {
|
||||
#[error(transparent)]
|
||||
Expected(#[from] UnexpectedDataType),
|
||||
#[error(transparent)]
|
||||
ExtractorError(#[from] ExtractorError),
|
||||
#[error("Could not parse the mod mask")]
|
||||
ModMask(#[source] ModifiedKeysymParserError),
|
||||
#[error("Could not parse the action")]
|
||||
ActionParserError(#[source] ActionParserError),
|
||||
}
|
||||
|
||||
pub struct ShortcutsParser<'a>(pub &'a Context<'a>);
|
||||
pub struct ShortcutsParser<'a, 'b> {
|
||||
pub cx: &'a Context<'a>,
|
||||
pub used_keys: &'b mut HashSet<Spanned<ModifiedKeySym>>,
|
||||
pub shortcuts: &'b mut Vec<Shortcut>,
|
||||
}
|
||||
|
||||
impl Parser for ShortcutsParser<'_> {
|
||||
type Value = Vec<(ModifiedKeySym, Action)>;
|
||||
impl Parser for ShortcutsParser<'_, '_> {
|
||||
type Value = ();
|
||||
type Error = ShortcutsParserError;
|
||||
const EXPECTED: &'static [DataType] = &[DataType::Table];
|
||||
|
||||
|
|
@ -35,38 +52,137 @@ impl Parser for ShortcutsParser<'_> {
|
|||
_span: Span,
|
||||
table: &IndexMap<Spanned<String>, Spanned<Value>>,
|
||||
) -> ParseResult<Self> {
|
||||
let mut used_keys = HashSet::<Spanned<ModifiedKeySym>>::new();
|
||||
let mut res = vec![];
|
||||
for (key, value) in table.iter() {
|
||||
let keysym = match ModifiedKeysymParser.parse_string(key.span, &key.value) {
|
||||
Ok(k) => k,
|
||||
Err(e) => {
|
||||
log::warn!("Could not parse keysym: {}", self.0.error(e));
|
||||
continue;
|
||||
}
|
||||
let Some(keysym) = parse_modified_keysym(self.cx, key) else {
|
||||
continue;
|
||||
};
|
||||
let action = match value.parse(&mut ActionParser(self.0)) {
|
||||
Ok(a) => a,
|
||||
let Some(action) = parse_action(self.cx, &key.value, value) else {
|
||||
continue;
|
||||
};
|
||||
let spanned = keysym.spanned(key.span);
|
||||
log_used(self.cx, self.used_keys, spanned);
|
||||
self.shortcuts.push(Shortcut {
|
||||
mask: Modifiers(!0),
|
||||
keysym,
|
||||
action,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ComplexShortcutsParser<'a, 'b> {
|
||||
pub cx: &'a Context<'a>,
|
||||
pub used_keys: &'b mut HashSet<Spanned<ModifiedKeySym>>,
|
||||
pub shortcuts: &'b mut Vec<Shortcut>,
|
||||
}
|
||||
|
||||
impl Parser for ComplexShortcutsParser<'_, '_> {
|
||||
type Value = ();
|
||||
type Error = ShortcutsParserError;
|
||||
const EXPECTED: &'static [DataType] = &[DataType::Table];
|
||||
|
||||
fn parse_table(
|
||||
&mut self,
|
||||
_span: Span,
|
||||
table: &IndexMap<Spanned<String>, Spanned<Value>>,
|
||||
) -> ParseResult<Self> {
|
||||
for (key, value) in table.iter() {
|
||||
let Some(keysym) = parse_modified_keysym(self.cx, key) else {
|
||||
continue;
|
||||
};
|
||||
let shortcut = match value.parse(&mut ComplexShortcutParser {
|
||||
keysym,
|
||||
cx: self.cx,
|
||||
}) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
log::warn!(
|
||||
"Could not parse action for keysym {}: {}",
|
||||
"Could not parse shortcut for keysym {}: {}",
|
||||
key.value,
|
||||
self.0.error(e)
|
||||
self.cx.error(e)
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let spanned = keysym.spanned(key.span);
|
||||
if let Some(prev) = used_keys.get(&spanned) {
|
||||
log::warn!(
|
||||
"Duplicate key overrides previous definition: {}",
|
||||
self.0.error3(spanned.span)
|
||||
);
|
||||
log::info!("Previous definition here: {}", self.0.error3(prev.span));
|
||||
}
|
||||
used_keys.insert(spanned);
|
||||
res.push((keysym, action));
|
||||
log_used(self.cx, self.used_keys, spanned);
|
||||
self.shortcuts.push(shortcut);
|
||||
}
|
||||
Ok(res)
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct ComplexShortcutParser<'a> {
|
||||
pub keysym: ModifiedKeySym,
|
||||
pub cx: &'a Context<'a>,
|
||||
}
|
||||
|
||||
impl Parser for ComplexShortcutParser<'_> {
|
||||
type Value = Shortcut;
|
||||
type Error = ShortcutsParserError;
|
||||
const EXPECTED: &'static [DataType] = &[DataType::Table];
|
||||
|
||||
fn parse_table(
|
||||
&mut self,
|
||||
span: Span,
|
||||
table: &IndexMap<Spanned<String>, Spanned<Value>>,
|
||||
) -> ParseResult<Self> {
|
||||
let mut ext = Extractor::new(self.cx, span, table);
|
||||
let (mod_mask_val, action_val) = ext.extract((opt(str("mod-mask")), opt(val("action"))))?;
|
||||
let mod_mask = match mod_mask_val {
|
||||
None => Modifiers(!0),
|
||||
Some(v) => ModifiersParser
|
||||
.parse_string(v.span, v.value)
|
||||
.map_spanned_err(ShortcutsParserError::ModMask)?,
|
||||
};
|
||||
let action = match action_val {
|
||||
None => Action::SimpleCommand {
|
||||
cmd: SimpleCommand::None,
|
||||
},
|
||||
Some(v) => v
|
||||
.parse(&mut ActionParser(self.cx))
|
||||
.map_spanned_err(ShortcutsParserError::ActionParserError)?,
|
||||
};
|
||||
Ok(Shortcut {
|
||||
mask: mod_mask,
|
||||
keysym: self.keysym,
|
||||
action,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_action(cx: &Context<'_>, key: &str, value: &Spanned<Value>) -> Option<Action> {
|
||||
match value.parse(&mut ActionParser(cx)) {
|
||||
Ok(a) => Some(a),
|
||||
Err(e) => {
|
||||
log::warn!("Could not parse action for keysym {key}: {}", cx.error(e));
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_modified_keysym(cx: &Context<'_>, key: &Spanned<String>) -> Option<ModifiedKeySym> {
|
||||
match ModifiedKeysymParser.parse_string(key.span, &key.value) {
|
||||
Ok(k) => Some(k),
|
||||
Err(e) => {
|
||||
log::warn!("Could not parse keysym {}: {}", key.value, cx.error(e));
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn log_used(
|
||||
cx: &Context<'_>,
|
||||
used: &mut HashSet<Spanned<ModifiedKeySym>>,
|
||||
key: Spanned<ModifiedKeySym>,
|
||||
) {
|
||||
if let Some(prev) = used.get(&key) {
|
||||
log::warn!(
|
||||
"Duplicate key overrides previous definition: {}",
|
||||
cx.error3(key.span)
|
||||
);
|
||||
log::info!("Previous definition here: {}", cx.error3(prev.span));
|
||||
}
|
||||
used.insert(key);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ mod toml;
|
|||
use {
|
||||
crate::config::{
|
||||
parse_config, Action, Config, ConfigConnector, ConfigDrmDevice, ConfigKeymap,
|
||||
ConnectorMatch, DrmDeviceMatch, Exec, Input, InputMatch, Output, OutputMatch,
|
||||
ConnectorMatch, DrmDeviceMatch, Exec, Input, InputMatch, Output, OutputMatch, Shortcut,
|
||||
SimpleCommand, Status, Theme,
|
||||
},
|
||||
ahash::{AHashMap, AHashSet},
|
||||
|
|
@ -541,21 +541,22 @@ impl State {
|
|||
}
|
||||
}
|
||||
|
||||
fn apply_shortcuts(
|
||||
self: &Rc<Self>,
|
||||
shortcuts: impl IntoIterator<Item = (ModifiedKeySym, Action)>,
|
||||
) {
|
||||
fn apply_shortcuts(self: &Rc<Self>, shortcuts: impl IntoIterator<Item = Shortcut>) {
|
||||
let mut binds = self.persistent.binds.borrow_mut();
|
||||
for (key, value) in shortcuts {
|
||||
for shortcut in shortcuts {
|
||||
if let Action::SimpleCommand {
|
||||
cmd: SimpleCommand::None,
|
||||
} = value
|
||||
} = shortcut.action
|
||||
{
|
||||
self.persistent.seat.unbind(key);
|
||||
binds.remove(&key);
|
||||
self.persistent.seat.unbind(shortcut.keysym);
|
||||
binds.remove(&shortcut.keysym);
|
||||
} else {
|
||||
self.persistent.seat.bind(key, value.into_fn(self));
|
||||
binds.insert(key);
|
||||
self.persistent.seat.bind_masked(
|
||||
shortcut.mask,
|
||||
shortcut.keysym,
|
||||
shortcut.action.into_fn(self),
|
||||
);
|
||||
binds.insert(shortcut.keysym);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue