Extract inhibit_power_key()
Browse files at this commit66533ae0b1b84c0d5acbc5e6618fb1700eeb61dbParent: d66bd872 changed files +24 -28
- src/main.rs +5 -0
- src/niri.rs +19 -28
src/main.rsmodified+5 -0
| @@ -100,6 +100,11 @@ fn main() { | |||
| 100 | if is_systemd_service { | 100 | if is_systemd_service { |
| 101 | // We're starting as a systemd service. Export our variables. | 101 | // We're starting as a systemd service. Export our variables. |
| 102 | import_env_to_systemd(); | 102 | import_env_to_systemd(); |
| 103 | + | ||
| 104 | + // Inhibit power key handling so we can suspend on it. | ||
| 105 | + if let Err(err) = state.niri.inhibit_power_key() { | ||
| 106 | + warn!("error inhibiting power key: {err:?}"); | ||
| 107 | + } | ||
| 103 | } | 108 | } |
| 104 | 109 | ||
| 105 | state.niri.start_dbus(&state.backend, is_systemd_service); | 110 | state.niri.start_dbus(&state.backend, is_systemd_service); |
src/niri.rsmodified+19 -28
| @@ -571,7 +571,6 @@ impl Niri { | |||
| 571 | let screenshot = gnome_shell_screenshot::Screenshot::new(to_niri, from_niri); | 571 | let screenshot = gnome_shell_screenshot::Screenshot::new(to_niri, from_niri); |
| 572 | 572 | ||
| 573 | let mut zbus_conn = None; | 573 | let mut zbus_conn = None; |
| 574 | - let mut inhibit_power_key_fd = None; | ||
| 575 | if is_session_instance { | 574 | if is_session_instance { |
| 576 | let conn = zbus::blocking::ConnectionBuilder::session() | 575 | let conn = zbus::blocking::ConnectionBuilder::session() |
| 577 | .unwrap() | 576 | .unwrap() |
| @@ -617,32 +616,6 @@ impl Niri { | |||
| 617 | } | 616 | } |
| 618 | 617 | ||
| 619 | zbus_conn = Some(conn); | 618 | zbus_conn = Some(conn); |
| 620 | - | ||
| 621 | - // Inhibit power key handling so we can suspend on it. | ||
| 622 | - let zbus_system_conn = zbus::blocking::ConnectionBuilder::system() | ||
| 623 | - .unwrap() | ||
| 624 | - .build() | ||
| 625 | - .unwrap(); | ||
| 626 | - | ||
| 627 | - // logind-zbus has a wrong signature for this method, so do it manually. | ||
| 628 | - // https://gitlab.com/flukejones/logind-zbus/-/merge_requests/5 | ||
| 629 | - let message = zbus_system_conn | ||
| 630 | - .call_method( | ||
| 631 | - Some("org.freedesktop.login1"), | ||
| 632 | - "/org/freedesktop/login1", | ||
| 633 | - Some("org.freedesktop.login1.Manager"), | ||
| 634 | - "Inhibit", | ||
| 635 | - &("handle-power-key", "niri", "Power key handling", "block"), | ||
| 636 | - ) | ||
| 637 | - .unwrap(); | ||
| 638 | - match message.body() { | ||
| 639 | - Ok(fd) => { | ||
| 640 | - inhibit_power_key_fd = Some(fd); | ||
| 641 | - } | ||
| 642 | - Err(err) => { | ||
| 643 | - warn!("error inhibiting power key: {err:?}"); | ||
| 644 | - } | ||
| 645 | - } | ||
| 646 | } else if config.debug.dbus_interfaces_in_non_session_instances { | 619 | } else if config.debug.dbus_interfaces_in_non_session_instances { |
| 647 | let conn = zbus::blocking::Connection::session().unwrap(); | 620 | let conn = zbus::blocking::Connection::session().unwrap(); |
| 648 | let flags = RequestNameFlags::AllowReplacement | 621 | let flags = RequestNameFlags::AllowReplacement |
| @@ -681,7 +654,25 @@ impl Niri { | |||
| 681 | } | 654 | } |
| 682 | 655 | ||
| 683 | self.zbus_conn = zbus_conn; | 656 | self.zbus_conn = zbus_conn; |
| 684 | - self.inhibit_power_key_fd = inhibit_power_key_fd; | 657 | + } |
| 658 | + | ||
| 659 | + pub fn inhibit_power_key(&mut self) -> anyhow::Result<()> { | ||
| 660 | + let conn = zbus::blocking::ConnectionBuilder::system()?.build()?; | ||
| 661 | + | ||
| 662 | + // logind-zbus has a wrong signature for this method, so do it manually. | ||
| 663 | + // https://gitlab.com/flukejones/logind-zbus/-/merge_requests/5 | ||
| 664 | + let message = conn.call_method( | ||
| 665 | + Some("org.freedesktop.login1"), | ||
| 666 | + "/org/freedesktop/login1", | ||
| 667 | + Some("org.freedesktop.login1.Manager"), | ||
| 668 | + "Inhibit", | ||
| 669 | + &("handle-power-key", "niri", "Power key handling", "block"), | ||
| 670 | + )?; | ||
| 671 | + | ||
| 672 | + let fd = message.body()?; | ||
| 673 | + self.inhibit_power_key_fd = Some(fd); | ||
| 674 | + | ||
| 675 | + Ok(()) | ||
| 685 | } | 676 | } |
| 686 | 677 | ||
| 687 | pub fn add_output(&mut self, output: Output, refresh_interval: Option<Duration>) { | 678 | pub fn add_output(&mut self, output: Output, refresh_interval: Option<Duration>) { |