aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 6c5e9ab0c46b084b36c26ae59ccfc433bd7a408c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#[macro_use]
extern crate tracing;

mod animation;
mod backend;
mod config;
mod dbus;
mod frame_clock;
mod handlers;
mod input;
mod layout;
mod niri;
mod pw_utils;
mod utils;

use std::env;
use std::ffi::OsString;
use std::path::PathBuf;

use clap::Parser;
use config::Config;
use miette::Context;
use niri::{Niri, State};
use portable_atomic::Ordering;
use smithay::reexports::calloop::EventLoop;
use smithay::reexports::wayland_server::Display;
use tracing_subscriber::EnvFilter;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
    /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).
    #[arg(short, long)]
    config: Option<PathBuf>,
    /// Command to run upon compositor startup.
    #[arg(last = true)]
    command: Vec<OsString>,
}

pub struct LoopData {
    display: Display<State>,
    state: State,
}

fn main() {
    env::set_var("RUST_BACKTRACE", "1");

    let directives = env::var("RUST_LOG").unwrap_or_else(|_| "niri=debug,info".to_owned());
    let env_filter = EnvFilter::builder().parse_lossy(directives);
    tracing_subscriber::fmt()
        .compact()
        .with_env_filter(env_filter)
        .init();

    let cli = Cli::parse();

    let _client = tracy_client::Client::start();

    let config = match Config::load(cli.config).context("error loading config") {
        Ok(config) => config,
        Err(err) => {
            warn!("{err:?}");
            Config::default()
        }
    };
    animation::ANIMATION_SLOWDOWN.store(config.debug.animation_slowdown, Ordering::Relaxed);

    let mut event_loop = EventLoop::try_new().unwrap();
    let mut display = Display::new().unwrap();
    let state = State::new(
        config,
        event_loop.handle(),
        event_loop.get_signal(),
        &mut display,
    );
    let mut data = LoopData { display, state };

    if let Some((command, args)) = cli.command.split_first() {
        if let Err(err) = std::process::Command::new(command).args(args).spawn() {
            warn!("error spawning command: {err:?}");
        }
    }

    event_loop
        .run(None, &mut data, move |data| {
            let _span = tracy_client::span!("loop callback");

            // These should be called periodically, before flushing the clients.
            data.state.niri.monitor_set.refresh();
            data.state.niri.popups.cleanup();
            data.state.update_focus();

            {
                let _span = tracy_client::span!("flush_clients");
                data.display.flush_clients().unwrap();
            }
        })
        .unwrap();
}