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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
use std::{
cmp::Ordering, collections::HashMap, ffi::CString, rc::Rc, sync::Arc,
};
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use config::{Config, Value};
use derive_builder::Builder;
use tokio_stream::StreamExt;
use x11rb::{
connection::Connection,
cookie::VoidCookie,
protocol::{
self,
render::{
Color, ConnectionExt as _, CreatePictureAux, PictOp, PictType,
Picture,
},
xproto::{
Atom, AtomEnum, ChangeWindowAttributesAux, ClientMessageEvent,
ConfigureWindowAux, ConnectionExt, CreateWindowAux, EventMask,
PropMode, Rectangle, Window, WindowClass,
},
},
wrapper::ConnectionExt as _,
xcb_ffi::XCBConnection,
COPY_FROM_PARENT,
};
use crate::{
bar::{self, BarInfo, Event, EventResponse, PanelDrawInfo},
common::PanelCommon,
ipc::ChannelEndpoint,
remove_bool_from_config, remove_string_from_config,
remove_uint_from_config,
x::{
find_visual, get_window_name, intern_named_atom, InternedAtoms, XStream,
},
PanelConfig, PanelStream,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum SortMethod {
Arrival(bool),
WindowName(bool),
WindowNameLower(bool),
}
impl SortMethod {
const fn reverse(self) -> Self {
match self {
Self::Arrival(reversed) => Self::Arrival(!reversed),
Self::WindowName(reversed) => Self::WindowName(!reversed),
Self::WindowNameLower(reversed) => Self::WindowNameLower(!reversed),
}
}
}
/// Display icons from some applications. See
/// <https://specifications.freedesktop.org/systemtray-spec/> for details.
#[derive(Debug, Builder, Clone)]
#[builder_struct_attr(allow(missing_docs))]
#[builder_impl_attr(allow(missing_docs))]
pub struct Systray {
name: &'static str,
conn: Arc<XCBConnection>,
screen: usize,
#[builder(default)]
time_start: u32,
#[builder(default)]
time_end: u32,
#[builder(default)]
icons: Vec<Icon>,
#[builder(default)]
pending: Vec<(Window, bool)>,
#[builder(default = "1")]
width: u16,
#[builder(default)]
height: u16,
#[builder(default)]
aggressive: bool,
#[builder(default)]
icon_padding: i16,
#[builder(default = "16")]
icon_size: i16,
#[builder(default = "SortMethod::Arrival(false)")]
icon_sort: SortMethod,
#[builder(default)]
focused: Option<Icon>,
#[builder(default)]
picture: Picture,
common: PanelCommon,
}
impl Systray {
fn draw(
&mut self,
tray: Window,
selection: Window,
root: Window,
bar_info: &bar::BarInfo,
) -> PanelDrawInfo {
let config_conn = self.conn.clone();
let show_conn = self.conn.clone();
let hide_conn = self.conn.clone();
let shutdown_conn = self.conn.clone();
let icons = self.icons.clone();
let picture = self.picture;
let bg = bar_info.bg.to_rgba16();
let bg = Color {
red: bg[0],
green: bg[1],
blue: bg[2],
alpha: bg[3],
};
let width = self.width;
let height = self.height;
let icon_padding = self.icon_padding;
let icon_size = self.icon_size;
let pending = self.pending.pop();
PanelDrawInfo::new(
(self.width as i32, self.height as i32),
self.common.dependence,
Box::new(move |_, x| {
if let Some((window, mapped)) = pending {
config_conn.reparent_window(
window,
tray,
width as i16 + icon_padding / 2,
(height as i16 - icon_size) / 2,
)?;
if mapped {
config_conn.map_window(window)?;
}
}
Self::draw_bg(&*config_conn, bg, picture, width, height)?;
config_conn.configure_window(
tray,
&ConfigureWindowAux::new().x(x as i32).y(0),
)?;
Ok(())
}),
Some(Box::new(move || {
show_conn.map_window(tray)?;
Ok(())
})),
Some(Box::new(move || {
hide_conn.unmap_window(tray)?;
Ok(())
})),
Some(Box::new(move || {
for icon in icons {
let _ = shutdown_conn
.reparent_window(icon.window, root, 0, 0)
.map(VoidCookie::check);
}
let _ = shutdown_conn.destroy_window(selection);
})),
)
}
fn draw_bg(
conn: &impl Connection,
bg: Color,
picture: Picture,
width: u16,
height: u16,
) -> Result<()> {
conn.render_fill_rectangles(
PictOp::SRC,
picture,
bg,
&[Rectangle {
x: 0,
y: 0,
width,
height,
}],
)?;
Ok(())
}
fn resize(&mut self, tray: Window, removed: Option<usize>) -> Result<()> {
let len = self.icons.len();
self.width = ((len
* (self.icon_size as usize + self.icon_padding as usize))
as u16)
.max(1);
self.conn.configure_window(
tray,
&ConfigureWindowAux::new().width(self.width as u32),
)?;
if let Some(destroyed) = removed {
for icon in &mut self.icons {
if icon.idx > destroyed {
icon.idx -= 1;
}
}
}
let icons = self.icons.clone();
for icon in icons {
self.reposition(icon)?;
}
Ok(())
}
fn reposition(&self, icon: Icon) -> Result<()> {
let x = icon.idx as i16 * (self.icon_size + self.icon_padding)
+ self.icon_padding / 2;
self.conn.configure_window(
icon.window,
&ConfigureWindowAux::new().x(x as i32),
)?;
Ok(())
}
}
#[async_trait(?Send)]
impl PanelConfig for Systray {
/// Configuration options:
///
/// - `screen`: The X screen to run on. Only one systray can exist on each
/// screen at any given time. Leaving this unset is probably what you
/// want.
/// - `aggressive`: If this is true, lazybar will take ownership of the
/// system tray for the given screen even if it is already owned. If it is
/// later lost, lazybar will not attempt to reacquire it.
/// - `padding`: The number of pixels between two icons.
/// - `size`: The width and height in pixels of each icon.
/// - `sort`: One of `arrival`, `window_name`, and `window_name_lower`.
/// Defaults to `arrival`. `arrival` will add each new panel on the right.
/// `window_name` will sort the panels by their `_NET_WM_NAME` (failing
/// that, `WM_NAME`) properties. `window_name_lower` will do the same, but
/// convert the strings to lowercase first.
/// - `sort_reverse`: If this is true, the sorting method above will be
/// reversed.
/// See [`PanelCommon::parse_common`]. This is used only for dependence.
fn parse(
name: &'static str,
table: &mut HashMap<String, Value>,
_global: &Config,
) -> Result<Self> {
let mut builder = SystrayBuilder::default();
builder.name(name);
let screen = remove_string_from_config("screen", table);
if let Ok((conn, screen)) = XCBConnection::connect(
screen
.as_ref()
.and_then(|s| CString::new(s.as_bytes()).ok())
.as_deref(),
) {
builder.conn(Arc::new(conn)).screen(screen);
} else {
log::error!("Failed to connect to X server");
}
if let Some(aggressive) = remove_bool_from_config("aggressive", table) {
builder.aggressive(aggressive);
}
if let Some(padding) = remove_uint_from_config("padding", table) {
builder.icon_padding(padding as i16);
}
if let Some(size) = remove_uint_from_config("size", table) {
builder.icon_size(size.max(2) as i16);
}
if let Some(sort) = remove_string_from_config("sort", table) {
builder.icon_sort(match sort.as_str() {
"window_name" => SortMethod::WindowName(false),
"window_name_lower" => SortMethod::WindowNameLower(false),
_ => SortMethod::Arrival(false),
});
}
if remove_bool_from_config("sort_reverse", table) == Some(true) {
builder.icon_sort = builder.icon_sort.map(SortMethod::reverse);
}
let common = PanelCommon::parse_common(table)?;
builder.common(common);
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: crate::attrs::Attrs,
height: i32,
) -> Result<(PanelStream, Option<ChannelEndpoint<Event, EventResponse>>)>
{
let tray_selection_atom = intern_named_atom(
&self.conn,
format!("_NET_SYSTEM_TRAY_S{}", self.screen).as_bytes(),
)?;
let info_atom = InternedAtoms::get(&self.conn, "_XEMBED_INFO")?;
let systray_opcode_atom =
InternedAtoms::get(&self.conn, "_NET_SYSTEM_TRAY_OPCODE")?;
let systray_orientation_atom =
InternedAtoms::get(&self.conn, "_NET_SYSTEM_TRAY_ORIENTATION")?;
let systray_visual_atom =
InternedAtoms::get(&self.conn, "_NET_SYSTEM_TRAY_VISUAL")?;
let xembed_atom = InternedAtoms::get(&self.conn, "_XEMBED")?;
let bar_info = bar::BAR_INFO.get().unwrap();
let bar_visual = bar_info.visual;
let screen = self
.conn
.setup()
.roots
.get(self.screen)
.context("Screen not found")?;
let root = screen.root;
let owner = self
.conn
.get_selection_owner(tray_selection_atom)?
.reply()?
.owner;
if owner != 0 && !self.aggressive {
return Err(anyhow!(
"Systray on screen {} already managed",
self.screen
));
}
let selection_wid: Window = self.conn.generate_id()?;
self.conn.create_window(
24,
selection_wid,
root,
-1,
-1,
1,
1,
0,
WindowClass::INPUT_OUTPUT,
find_visual(screen, 24)
.context("couldn't find visual")?
.visual_id,
&CreateWindowAux::new().event_mask(EventMask::PROPERTY_CHANGE),
)?;
self.conn.change_property32(
PropMode::REPLACE,
selection_wid,
systray_orientation_atom,
AtomEnum::CARDINAL,
&[0],
)?;
self.conn.change_property32(
PropMode::REPLACE,
selection_wid,
systray_visual_atom,
AtomEnum::VISUALID,
&[bar_visual.visual_id],
)?;
let tray_wid: Window = self.conn.generate_id()?;
let depth = if bar_info.transparent { 32 } else { 24 };
self.height = height as u16;
self.conn.create_window(
depth,
tray_wid,
bar_info.window,
0,
0,
1,
self.height,
0,
WindowClass::INPUT_OUTPUT,
COPY_FROM_PARENT,
&CreateWindowAux::new().event_mask(
EventMask::KEY_PRESS
| EventMask::KEY_RELEASE
| EventMask::PROPERTY_CHANGE
| EventMask::STRUCTURE_NOTIFY
| EventMask::SUBSTRUCTURE_NOTIFY,
),
)?;
let pictformat = self
.conn
.render_query_pict_formats()?
.reply()?
.formats
.iter()
.find(|f| {
f.type_ == PictType::DIRECT
&& f.depth == if bar_info.transparent { 32 } else { 24 }
})
// it would be a spec violation for this to fail
// https://cgit.freedesktop.org/xorg/proto/renderproto/tree/renderproto.txt#n257
.unwrap()
.id;
self.picture = self.conn.generate_id()?;
self.conn.render_create_picture(
self.picture,
tray_wid,
pictformat,
&CreatePictureAux::new(),
)?;
self.conn.map_window(tray_wid)?;
self.conn.flush()?;
Ok((
Box::pin(XStream::new(self.conn.clone()).filter_map(
move |event| {
event.map_or(None, |event| {
match self.handle_tray_event(
bar_info,
&event,
selection_wid,
tray_wid,
root,
tray_selection_atom,
info_atom,
systray_opcode_atom,
xembed_atom,
) {
Ok(true) => Some(Ok(self.draw(
tray_wid,
selection_wid,
root,
bar_info,
))),
Ok(false) => None,
Err(e) => Some(Err(e)),
}
})
},
)),
None,
))
}
}
impl Systray {
fn handle_tray_event(
&mut self,
bar_info: &BarInfo,
event: &protocol::Event,
selection_wid: Window,
tray_wid: Window,
root: Window,
// _NET_SYSTEM_TRAY_S{screen}
tray_selection_atom: Atom,
// _XEMBED_INFO
info_atom: Atom,
// _NET_SYSTEM_TRAY_OPCODE
systray_opcode_atom: Atom,
// _XEMBED
xembed_atom: Atom,
) -> Result<bool> {
log::trace!("systray event: {event:?}");
let redraw_needed = match event {
// https://x.org/releases/X11R7.7/doc/xorg-docs/icccm/icccm.html#Acquiring_Selection_Ownership
protocol::Event::PropertyNotify(event) => {
if self.time_start == 0 {
self.time_start = event.time;
self.conn.set_selection_owner(
selection_wid,
tray_selection_atom,
self.time_start,
)?;
let manager_atom =
InternedAtoms::get(&self.conn, "MANAGER")?;
self.conn.send_event(
false,
root,
EventMask::STRUCTURE_NOTIFY,
ClientMessageEvent::new(
32,
root,
manager_atom,
[
self.time_start,
tray_selection_atom,
selection_wid,
0,
0,
],
),
)?;
true
} else if event.atom == info_atom {
let window = event.window;
let xembed_info = self
.conn
.get_property(
false, window, info_atom, info_atom, 0, 64,
)?
.reply()?;
if let Some(0) = xembed_info
.value32()
.context("Invalid reply from X server")?
.nth(1)
.map(|m| m & 0x1)
{
self.conn.unmap_window(window)?;
} else {
self.conn.map_window(window)?;
};
true
} else {
false
}
}
protocol::Event::ClientMessage(event) => {
let ty = event.type_;
if ty == systray_opcode_atom {
let data = event.data.as_data32();
if data[1] != 0 {
return Ok(false);
}
let window = data[2];
self.conn.configure_window(
tray_wid,
&ConfigureWindowAux::new().width(
(self.width as i16
+ self.icon_size
+ self.icon_padding)
as u32,
),
)?;
self.conn.configure_window(
window,
&ConfigureWindowAux::new()
.width(self.icon_size as u32)
.height(self.icon_size as u32),
)?;
self.conn.change_window_attributes(
window,
&ChangeWindowAttributesAux::new()
.event_mask(EventMask::PROPERTY_CHANGE),
)?;
let xembed_info = self
.conn
.get_property(
false,
window,
info_atom,
AtomEnum::CARDINAL,
0,
2,
)?
.reply()?;
let mapped =
!xembed_info.value32().is_some_and(|mut val| {
val.nth(1).is_some_and(|mapped| mapped & 0x1 == 0)
});
let idx = match self.icon_sort {
SortMethod::Arrival(false) => self.icons.len(),
SortMethod::Arrival(true) => {
for icon in &mut self.icons {
icon.idx += 1;
}
0
}
_ => 0,
};
self.icons.push(Icon {
window,
mapped,
idx,
});
if let SortMethod::WindowName(reversed)
| SortMethod::WindowNameLower(reversed) = self.icon_sort
{
self.icons.sort_by(|a, b| {
let mut a_name =
get_window_name(&*self.conn, a.window);
let mut b_name =
get_window_name(&*self.conn, b.window);
if let SortMethod::WindowNameLower(_) =
self.icon_sort
{
a_name = a_name.map(|s| s.to_lowercase());
b_name = b_name.map(|s| s.to_lowercase());
}
match (|| {
Ok::<_, anyhow::Error>((a_name?, b_name?))
})() {
Err(_) => Ordering::Equal,
Ok((a, b)) => {
let ordering = a.cmp(&b);
if reversed {
ordering.reverse()
} else {
ordering
}
}
}
});
self.icons
.iter_mut()
.enumerate()
.for_each(|(idx, icon)| icon.idx = idx);
}
if self.width == 1 {
self.width =
(self.icon_size + self.icon_padding) as u16;
} else {
self.width +=
(self.icon_size + self.icon_padding) as u16;
}
self.resize(tray_wid, None)?;
let bg = bar_info.bg.to_rgba16();
Self::draw_bg(
&*self.conn,
Color {
red: bg[0],
green: bg[1],
blue: bg[2],
alpha: bg[3],
},
self.picture,
self.width,
self.height,
)?;
self.pending.push((window, mapped));
true
} else if ty == xembed_atom {
let data = event.data.as_data32();
let time = data[0];
let major = data[1];
match major {
// request focus
3 => {
if let Some(focused) = self.focused {
self.conn.send_event(
false,
focused.window,
EventMask::NO_EVENT,
ClientMessageEvent::new(
32,
focused.window,
xembed_atom,
[time, 5, 0, 0, 0],
),
)?;
}
let window = event.window;
self.conn.send_event(
false,
window,
EventMask::NO_EVENT,
ClientMessageEvent::new(
32,
window,
xembed_atom,
[time, 4, 0, 0, 0],
),
)?;
}
// focus next/prev
6 | 7 => {
if let Some(focused) = self.focused {
self.conn.send_event(
false,
focused.window,
EventMask::NO_EVENT,
ClientMessageEvent::new(
32,
focused.window,
xembed_atom,
[time, 5, 0, 0, 0],
),
)?;
}
}
_ => {}
}
false
} else {
false
}
}
protocol::Event::ReparentNotify(event) => {
if event.parent == tray_wid {
self.resize(tray_wid, None)?;
} else {
let mut removed = None;
self.icons.retain(|&w| {
if w.window == event.window {
removed = Some(w.idx);
false
} else {
true
}
});
self.resize(tray_wid, removed)?;
}
true
}
protocol::Event::DestroyNotify(event) => {
let mut destroyed = None;
self.icons.retain(|&w| {
if w.window == event.window {
destroyed = Some(w.idx);
false
} else {
true
}
});
self.resize(tray_wid, destroyed)?;
true
}
protocol::Event::ConfigureNotify(event) => {
if event.window != tray_wid
&& (event.height != self.icon_size as u16
|| event.width != self.icon_size as u16)
{
self.conn.configure_window(
event.window,
&ConfigureWindowAux::new()
.width(self.icon_size as u32)
.height(self.icon_size as u32),
)?;
}
true
}
protocol::Event::SelectionClear(event) => {
self.time_end = event.time;
false
}
protocol::Event::KeyPress(mut event)
| protocol::Event::KeyRelease(mut event) => {
if let Some(focused) = self.focused {
event.child = focused.window;
self.conn.send_event(
false,
focused.window,
EventMask::NO_EVENT,
event,
)?;
}
false
}
_ => false,
};
self.conn.flush()?;
Ok(redraw_needed)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Icon {
window: Window,
mapped: bool,
idx: usize,
}