Skip to content

Commit

Permalink
rearrange some code
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVerevkin committed Mar 18, 2024
1 parent cb321b0 commit c6626c9
Showing 1 changed file with 62 additions and 62 deletions.
124 changes: 62 additions & 62 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,74 @@ enum Command {
Watch { format: String },
}

fn main() -> anyhow::Result<()> {
let command = Cli::parse().command.unwrap_or(Command::Run);
let dbus_server = dbus_server::DbusServer::new()?;

match command {
Command::Run => {
if let Some(dbus_server) = dbus_server {
let (mut wayland, wayland_state) = wayland::Wayland::new()?;

let mut fds = [pollin(&dbus_server), pollin(&wayland)];
let mut state = State {
wayland_state,
dbus_server,
};

loop {
poll(&mut fds)?;
if fds[0].revents != 0 {
state.dbus_server.poll(&mut state.wayland_state)?;
}
if fds[1].revents != 0 || state.wayland_state.color_changed() {
state = wayland.poll(state)?;
}
}
} else {
eprintln!("wl-gammarelay-rs is already running");
}
}
Command::Watch { format } => {
let mut dbus_client = dbus_client::DbusClient::new(format, dbus_server.is_none())?;
if let Some(dbus_server) = dbus_server {
let (mut wayland, state) = wayland::Wayland::new()?;

let mut fds = [pollin(&dbus_server), pollin(&wayland), pollin(&dbus_client)];
let mut state = State {
wayland_state: state,
dbus_server,
};

loop {
poll(&mut fds)?;
if fds[0].revents != 0 {
state.dbus_server.poll(&mut state.wayland_state)?;
}
if fds[1].revents != 0 || state.wayland_state.color_changed() {
state = wayland.poll(state)?;
}
if fds[2].revents != 0 {
dbus_client.run(false)?;
}
}
} else {
dbus_client.run(true)?;
}
}
}

Ok(())
}

pub struct WaylandState {
outputs: Vec<wayland::Output>,
gamma_manager: ZwlrGammaControlManagerV1,
}

pub struct State {
pub wayland_state: WaylandState,
pub dbus_server: DbusServer,
wayland_state: WaylandState,
dbus_server: DbusServer,
}

impl WaylandState {
Expand Down Expand Up @@ -164,66 +224,6 @@ impl WaylandState {
}
}

fn main() -> anyhow::Result<()> {
let command = Cli::parse().command.unwrap_or(Command::Run);
let dbus_server = dbus_server::DbusServer::new()?;

match command {
Command::Run => {
if let Some(dbus_server) = dbus_server {
let (mut wayland, wayland_state) = wayland::Wayland::new()?;

let mut fds = [pollin(&dbus_server), pollin(&wayland)];
let mut state = State {
wayland_state,
dbus_server,
};

loop {
poll(&mut fds)?;
if fds[0].revents != 0 {
state.dbus_server.poll(&mut state.wayland_state)?;
}
if fds[1].revents != 0 || state.wayland_state.color_changed() {
state = wayland.poll(state)?;
}
}
} else {
eprintln!("wl-gammarelay-rs is already running");
}
}
Command::Watch { format } => {
let mut dbus_client = dbus_client::DbusClient::new(format, dbus_server.is_none())?;
if let Some(dbus_server) = dbus_server {
let (mut wayland, state) = wayland::Wayland::new()?;

let mut fds = [pollin(&dbus_server), pollin(&wayland), pollin(&dbus_client)];
let mut state = State {
wayland_state: state,
dbus_server,
};

loop {
poll(&mut fds)?;
if fds[0].revents != 0 {
state.dbus_server.poll(&mut state.wayland_state)?;
}
if fds[1].revents != 0 || state.wayland_state.color_changed() {
state = wayland.poll(state)?;
}
if fds[2].revents != 0 {
dbus_client.run(false)?;
}
}
} else {
dbus_client.run(true)?;
}
}
}

Ok(())
}

fn pollin(fd: &impl AsRawFd) -> libc::pollfd {
libc::pollfd {
fd: fd.as_raw_fd(),
Expand Down

0 comments on commit c6626c9

Please sign in to comment.