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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use std::{
    collections::{HashMap, HashSet},
    pin::Pin,
    rc::Rc,
    sync::Arc,
    task::Poll,
};

use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use config::{Config, Value};
use derive_builder::Builder;
use tokio::task::{self, JoinHandle};
use tokio_stream::{Stream, StreamExt};
use x11rb::{
    connection::Connection,
    protocol::{
        self,
        xproto::{
            Atom, AtomEnum, ChangeWindowAttributesAux, ConnectionExt,
            EventMask, Window,
        },
    },
    rust_connection::RustConnection,
};

use crate::{
    bar::{Event, EventResponse, PanelDrawInfo},
    common::{draw_common, PanelCommon, ShowHide},
    ipc::ChannelEndpoint,
    remove_string_from_config, remove_uint_from_config,
    x::InternedAtoms,
    Attrs, Highlight, PanelConfig, PanelStream,
};

/// Displays the title (_NET_WM_NAME) of the focused window (_NET_ACTIVE_WINDOW)
///
/// Requires an EWMH-compliant window manager
#[derive(Debug, Builder, Clone)]
#[builder_struct_attr(allow(missing_docs))]
#[builder_impl_attr(allow(missing_docs))]
pub struct XWindow {
    name: &'static str,
    conn: Arc<RustConnection>,
    screen: usize,
    windows: HashSet<Window>,
    #[builder(setter(strip_option), default = "None")]
    max_width: Option<u32>,
    format: &'static str,
    attrs: Attrs,
    #[builder(default, setter(strip_option))]
    highlight: Option<Highlight>,
    common: PanelCommon,
}

impl XWindow {
    fn draw(
        &mut self,
        cr: &Rc<cairo::Context>,
        name_atom: Atom,
        window_atom: Atom,
        root: Window,
        utf8_atom: Atom,
        height: i32,
    ) -> Result<PanelDrawInfo> {
        let active: u32 = self
            .conn
            .get_property(false, root, window_atom, AtomEnum::WINDOW, 0, 1)?
            .reply()?
            .value32()
            .context("Invalid reply from X server")?
            .next()
            .context("Empty reply from X server")?;
        let name = if active == 0 {
            String::new()
        } else {
            if self.windows.insert(active) {
                self.conn.change_window_attributes(
                    active,
                    &ChangeWindowAttributesAux::new()
                        .event_mask(EventMask::PROPERTY_CHANGE),
                )?;
            }

            if let Some(max_width) = self.max_width {
                let bytes = self
                    .conn
                    .get_property(
                        false, active, name_atom, utf8_atom, 0, max_width,
                    )?
                    .reply()?
                    .value;

                unsafe { std::str::from_utf8_unchecked(bytes.as_slice()) }
                    .chars()
                    .take(max_width as usize)
                    .collect()
            } else {
                let mut offset = 0;
                let mut title = String::new();
                loop {
                    let reply = self
                        .conn
                        .get_property(
                            false, active, name_atom, utf8_atom, offset, 64,
                        )?
                        .reply()?;

                    title.push_str(unsafe {
                        String::from_utf8_unchecked(reply.value).as_str()
                    });

                    if reply.bytes_after == 0 {
                        break;
                    }

                    offset += 64;
                }

                title
            }
        };

        let text = self.format.replace(
            "%name%",
            glib::markup_escape_text(name.as_str()).as_str(),
        );

        let conn = self.conn.clone();
        let conn_ = self.conn.clone();

        draw_common(
            cr,
            text.as_str(),
            &self.attrs,
            self.common.dependence,
            self.highlight.clone(),
            self.common.images.clone(),
            height,
            ShowHide::Custom(
                Some(Box::new(move || {
                    conn.change_window_attributes(
                        root,
                        &ChangeWindowAttributesAux::new()
                            .event_mask(EventMask::PROPERTY_CHANGE),
                    )?;
                    Ok(())
                })),
                Some(Box::new(move || {
                    conn_.change_window_attributes(
                        root,
                        &ChangeWindowAttributesAux::new()
                            .event_mask(EventMask::NO_EVENT),
                    )?;
                    Ok(())
                })),
            ),
        )
    }
}

#[async_trait(?Send)]
impl PanelConfig for XWindow {
    /// Configuration options:
    ///
    /// - `screen`: the name of the X screen to monitor
    ///   - type: String
    ///   - default: None (This will tell X to choose the default screen, which
    ///     is probably what you want.)
    /// - `format`: the format string
    ///   - type: String
    ///   - default: `%name%`
    ///   - formatting options: `%name%`
    /// - `attrs`: A string specifying the attrs for the panel. See
    ///   [`Attrs::parse`] for details.
    /// - `highlight`: A string specifying the highlight for the panel. See
    ///   [`Highlight::parse`] for details.
    /// - See [`PanelCommon::parse_common`].
    fn parse(
        name: &'static str,
        table: &mut HashMap<String, Value>,
        _global: &Config,
    ) -> Result<Self> {
        let mut builder = XWindowBuilder::default();

        builder.name(name);
        let screen = remove_string_from_config("screen", table);
        if let Ok((conn, screen)) = RustConnection::connect(screen.as_deref()) {
            builder.conn(Arc::new(conn)).screen(screen);
        } else {
            log::error!("Failed to connect to X server");
        }

        builder.windows(HashSet::new());
        if let Some(max_width) = remove_uint_from_config("max_width", table) {
            builder.max_width(max_width as u32);
        }

        let common = PanelCommon::parse_common(table)?;
        let format = PanelCommon::parse_format(table, "", "%name%");
        let attrs = PanelCommon::parse_attr(table, "");
        let highlight = PanelCommon::parse_highlight(table, "");

        builder.common(common);
        builder.format(format.leak());
        builder.attrs(attrs);
        builder.highlight(highlight);

        Ok(builder.build()?)
    }

    fn props(&self) -> (&'static str, bool) {
        (self.name, self.common.visible)
    }

    async fn run(
        mut self: Box<Self>,
        cr: Rc<cairo::Context>,
        global_attrs: Attrs,
        height: i32,
    ) -> Result<(PanelStream, Option<ChannelEndpoint<Event, EventResponse>>)>
    {
        let name_atom = InternedAtoms::get(&self.conn, "_NET_WM_NAME")?;
        let window_atom = InternedAtoms::get(&self.conn, "_NET_ACTIVE_WINDOW")?;
        let utf8_atom = InternedAtoms::get(&self.conn, "UTF8_STRING")?;
        let root = self
            .conn
            .setup()
            .roots
            .get(self.screen)
            .ok_or_else(|| anyhow!("Screen not found"))?
            .root;
        self.conn.change_window_attributes(
            root,
            &ChangeWindowAttributesAux::new()
                .event_mask(EventMask::PROPERTY_CHANGE),
        )?;

        self.attrs.apply_to(&global_attrs);

        let stream = tokio_stream::once(())
            .chain(XStream::new(self.conn.clone(), name_atom, window_atom))
            .map(move |()| {
                self.draw(&cr, name_atom, window_atom, root, utf8_atom, height)
            });
        Ok((Box::pin(stream), None))
    }
}

struct XStream {
    conn: Arc<RustConnection>,
    name_atom: Atom,
    window_atom: Atom,
    handle: Option<JoinHandle<()>>,
}

impl XStream {
    const fn new(
        conn: Arc<RustConnection>,
        name_atom: Atom,
        window_atom: Atom,
    ) -> Self {
        Self {
            conn,
            name_atom,
            window_atom,
            handle: None,
        }
    }
}

impl Stream for XStream {
    type Item = ();

    fn poll_next(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Option<Self::Item>> {
        if let Some(handle) = &self.handle {
            if handle.is_finished() {
                self.handle = None;
                Poll::Ready(Some(()))
            } else {
                Poll::Pending
            }
        } else {
            let conn = self.conn.clone();
            let waker = cx.waker().clone();
            let name_atom = self.name_atom;
            let window_atom = self.window_atom;
            self.handle = Some(task::spawn_blocking(move || loop {
                let event = conn.wait_for_event();
                if let Ok(protocol::Event::PropertyNotify(event)) = event {
                    if event.atom == name_atom || event.atom == window_atom {
                        waker.wake();
                        break;
                    }
                }
            }));
            Poll::Pending
        }
    }
}