summaryrefslogtreecommitdiff
path: root/src/output.rs
blob: 222245c7be37b10f9bc22bf4e70e8c1007a08caf (plain) (blame)
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
use wayrs_client::global::{Global, GlobalExt};
use wayrs_client::protocol::*;
use wayrs_client::{Connection, EventCtx};

use crate::state::State;

#[derive(Debug)]
pub struct Output {
    pub wl: WlOutput,
    pub reg_name: u32,
    pub scale: u32,
    pub name: String,
}

pub struct PendingOutput {
    pub wl: WlOutput,
    pub reg_name: u32,
    pub scale: u32,
}

impl PendingOutput {
    pub fn bind(conn: &mut Connection<State>, global: &Global) -> Self {
        Self {
            wl: global
                .bind_with_cb(conn, 4, wl_output_cb)
                .expect("could not bind wl_output"),
            reg_name: global.name,
            scale: 1,
        }
    }
}

impl Output {
    pub fn destroy(self, conn: &mut Connection<State>) {
        self.wl.release(conn);
    }
}

fn wl_output_cb(ctx: EventCtx<State, WlOutput>) {
    match ctx.event {
        wl_output::Event::Name(name) => {
            let i = ctx
                .state
                .pending_outputs
                .iter()
                .position(|o| o.wl == ctx.proxy)
                .unwrap();
            let output = ctx.state.pending_outputs.swap_remove(i);
            let name = String::from_utf8(name.into_bytes()).expect("invalid output name");
            let output = Output {
                wl: output.wl,
                reg_name: output.reg_name,
                scale: output.scale,
                name,
            };
            ctx.state.register_output(ctx.conn, output);
        }
        wl_output::Event::Scale(scale) => {
            if let Some(bar) = ctx
                .state
                .bars
                .iter_mut()
                .find(|bar| bar.output.wl == ctx.proxy)
            {
                bar.output.scale = scale as u32;
            } else if let Some(output) = ctx
                .state
                .pending_outputs
                .iter_mut()
                .find(|o| o.wl == ctx.proxy)
            {
                output.scale = scale as u32;
            }
        }
        _ => (),
    }
}