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

[refactor] replace manual insertions with collects #241

Merged
merged 1 commit into from
Feb 26, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ target
*.out
com.github.swhkd.pkexec.policy
*rc
!*rc/
.direnv
2 changes: 1 addition & 1 deletion swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ fn socket_write(command: &str, socket_path: PathBuf) -> Result<(), Box<dyn Error
pub fn check_input_group() -> Result<(), Box<dyn Error>> {
if !Uid::current().is_root() {
let groups = nix::unistd::getgroups();
for (_, groups) in groups.iter().enumerate() {
for groups in groups.iter() {
for group in groups {
let group = Group::from_gid(*group);
if group.unwrap().unwrap().name == "input" {
Expand Down
34 changes: 12 additions & 22 deletions swhkd/src/uinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,21 @@ use std::os::unix::io::AsRawFd;
ioctl_none!(rfkill_noinput, b'R', 1);

pub fn create_uinput_device() -> Result<VirtualDevice, Box<dyn std::error::Error>> {
let mut keys = AttributeSet::<Key>::new();
for key in get_all_keys() {
keys.insert(key);
}
let keys: AttributeSet<Key> = get_all_keys().iter().copied().collect();

let mut relative_axes = AttributeSet::<RelativeAxisType>::new();
for axis in get_all_relative_axes() {
relative_axes.insert(axis);
}
let relative_axes: AttributeSet<RelativeAxisType> =
get_all_relative_axes().iter().copied().collect();

let device = VirtualDeviceBuilder::new()?
.name("swhkd virtual output")
.with_keys(&keys)?
.with_relative_axes(&relative_axes)?
.build()
.unwrap();
.build()?;
Ok(device)
}

pub fn create_uinput_switches_device() -> Result<VirtualDevice, Box<dyn std::error::Error>> {
let mut switches = AttributeSet::<SwitchType>::new();
for switch in get_all_switches() {
switches.insert(switch);
}
let switches: AttributeSet<SwitchType> = get_all_switches().iter().copied().collect();

// We have to disable rfkill-input to avoid blocking all radio devices. When
// a new device (virtual or physical) with the SW_RFKILL_ALL capability bit
Expand All @@ -52,12 +43,11 @@ pub fn create_uinput_switches_device() -> Result<VirtualDevice, Box<dyn std::err
let device = VirtualDeviceBuilder::new()?
.name("swhkd switches virtual output")
.with_switches(&switches)?
.build()
.unwrap();
.build()?;
Ok(device)
}
pub fn get_all_keys() -> Vec<Key> {
vec![
pub fn get_all_keys() -> &'static [Key] {
&[
evdev::Key::KEY_RESERVED,
evdev::Key::KEY_ESC,
evdev::Key::KEY_1,
Expand Down Expand Up @@ -609,8 +599,8 @@ pub fn get_all_keys() -> Vec<Key> {
]
}

pub fn get_all_relative_axes() -> Vec<RelativeAxisType> {
vec![
pub fn get_all_relative_axes() -> &'static [RelativeAxisType] {
&[
RelativeAxisType::REL_X,
RelativeAxisType::REL_Y,
RelativeAxisType::REL_Z,
Expand All @@ -627,8 +617,8 @@ pub fn get_all_relative_axes() -> Vec<RelativeAxisType> {
]
}

pub fn get_all_switches() -> Vec<SwitchType> {
vec![
pub fn get_all_switches() -> &'static [SwitchType] {
&[
SwitchType::SW_LID,
SwitchType::SW_TABLET_MODE,
SwitchType::SW_HEADPHONE_INSERT,
Expand Down
Loading