Skip to content

Commit

Permalink
Disable accelerometer polling while locked
Browse files Browse the repository at this point in the history
Closes #140.
  • Loading branch information
chrisduerr committed Jan 4, 2024
1 parent fa46ef3 commit 1bf9cca
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
30 changes: 25 additions & 5 deletions src/catacomb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::time::{Duration, Instant};

use _decoration::zv1::server::zxdg_toplevel_decoration_v1::Mode as DecorationMode;
use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as ManagerMode;
use catacomb_ipc::Orientation;
use smithay::backend::allocator::dmabuf::Dmabuf;
use smithay::backend::renderer::ImportDma;
use smithay::input::keyboard::XkbConfig;
Expand Down Expand Up @@ -262,17 +263,22 @@ impl Catacomb {
let touch = seat.add_touch();
let touch_state = TouchState::new(event_loop.clone(), touch);

// Subscribe to device orientation changes.
let accel_token = Accelerometer::new().subscribe(&event_loop, |orientation, catacomb| {
catacomb.handle_orientation(orientation);
});

// Start IPC socket listener.
ipc_server::spawn_ipc_socket(&event_loop, &socket_name).expect("spawn IPC socket");

// Create window manager.
let windows = Windows::new(&display_handle, event_loop.clone());

// Subscribe to device orientation changes.
let accel_token = Accelerometer::new().subscribe(&event_loop, |orientation, catacomb| {
catacomb.handle_orientation(orientation);
});

// Disable accelerometer polling if orientation starts locked.
if windows.orientation_locked() {
trace_error(event_loop.disable(&accel_token));
}

// Run user startup script.
if let Some(mut script_path) = dirs::config_dir() {
script_path.push("catacomb");
Expand Down Expand Up @@ -448,6 +454,20 @@ impl Catacomb {

self.backend.set_sleep(sleep);
}

/// Lock the output's orientation.
pub fn lock_orientation(&mut self, orientation: Option<Orientation>) {
trace_error(self.event_loop.disable(&self.accelerometer_token));
self.windows.lock_orientation(orientation);
self.unstall();
}

/// Unlock the output's orientation.
pub fn unlock_orientation(&mut self) {
trace_error(self.event_loop.enable(&self.accelerometer_token));
self.windows.unlock_orientation();
self.unstall();
}
}

impl CompositorHandler for Catacomb {
Expand Down
10 changes: 2 additions & 8 deletions src/ipc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,8 @@ fn handle_message(buffer: &mut String, mut stream: UnixStream, catacomb: &mut Ca

// Handle IPC events.
match message {
IpcMessage::Orientation { unlock: true, .. } => {
catacomb.windows.unlock_orientation();
catacomb.unstall();
},
IpcMessage::Orientation { lock: orientation, .. } => {
catacomb.windows.lock_orientation(orientation);
catacomb.unstall();
},
IpcMessage::Orientation { unlock: true, .. } => catacomb.unlock_orientation(),
IpcMessage::Orientation { lock: orientation, .. } => catacomb.lock_orientation(orientation),
IpcMessage::Scale { scale, app_id: Some(app_id) } => {
let app_id = match AppIdMatcher::try_from(app_id) {
Ok(app_id) => app_id,
Expand Down
1 change: 1 addition & 0 deletions src/orientation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl AccelerometerSource for SensorAccelerometer {
{
loop_handle
.insert_source(Timer::immediate(), move |_, _, catacomb| {
println!("POLLING ACCELEROMETER");
let fallback = self.last.unwrap_or(Orientation::Portrait);
let orientation = self.orientation().unwrap_or(fallback);
if Some(orientation) != self.last {
Expand Down
5 changes: 5 additions & 0 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,11 @@ impl Windows {
self.update_orientation(self.unlocked_orientation);
}

/// Get orientation lock state.
pub fn orientation_locked(&self) -> bool {
self.orientation_locked
}

/// Check if any window was damaged since the last redraw.
pub fn damaged(&mut self) -> bool {
if self.dirty {
Expand Down

0 comments on commit 1bf9cca

Please sign in to comment.