Skip to content

Commit

Permalink
own systray
Browse files Browse the repository at this point in the history
  • Loading branch information
ntBre committed Dec 7, 2024
1 parent 6f56804 commit 4b69602
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
19 changes: 10 additions & 9 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub(crate) fn clientmessage(state: &mut State, e: *mut XEvent) {
let mut c = wintoclient(state, cme.window);

if CONFIG.showsystray
&& cme.window == (*state.systray).win
&& cme.window == state.systray().win
&& cme.message_type == state.netatom[Net::SystemTrayOP as usize]
{
// add systray icons
Expand All @@ -134,8 +134,9 @@ pub(crate) fn clientmessage(state: &mut State, e: *mut XEvent) {
return;
}
(*c).mon = state.selmon;
(*c).next = (*state.systray).icons;
(*state.systray).icons = c;
(*c).next = state.systray().icons;
let place = &mut state.systray_mut().icons;
*place = c;
let mut wa = MaybeUninit::uninit();
if XGetWindowAttributes(state.dpy, (*c).win, wa.as_mut_ptr()) == 0 {
// use sane defaults
Expand Down Expand Up @@ -173,7 +174,7 @@ pub(crate) fn clientmessage(state: &mut State, e: *mut XEvent) {
c.win,
StructureNotifyMask | PropertyChangeMask | ResizeRedirectMask,
);
XReparentWindow(state.dpy, c.win, (*state.systray).win, 0, 0);
XReparentWindow(state.dpy, c.win, state.systray().win, 0, 0);
// use parent's background color
let mut swa = XSetWindowAttributes {
background_pixmap: 0,
Expand Down Expand Up @@ -206,7 +207,7 @@ pub(crate) fn clientmessage(state: &mut State, e: *mut XEvent) {
CurrentTime as i64,
XEMBED_EMBEDDED_NOTIFY as i64,
0,
(*state.systray).win as i64,
state.systray().win as i64,
XEMBED_EMBEDDED_VERSION as i64,
);

Expand All @@ -220,7 +221,7 @@ pub(crate) fn clientmessage(state: &mut State, e: *mut XEvent) {
CurrentTime as i64,
XEMBED_FOCUS_IN as i64,
0,
(*state.systray).win as i64,
state.systray().win as i64,
XEMBED_EMBEDDED_VERSION as i64,
);
sendevent(
Expand All @@ -231,7 +232,7 @@ pub(crate) fn clientmessage(state: &mut State, e: *mut XEvent) {
CurrentTime as i64,
XEMBED_WINDOW_ACTIVATE as i64,
0,
(*state.systray).win as i64,
state.systray().win as i64,
XEMBED_EMBEDDED_VERSION as i64,
);
sendevent(
Expand All @@ -242,7 +243,7 @@ pub(crate) fn clientmessage(state: &mut State, e: *mut XEvent) {
CurrentTime as i64,
XEMBED_MODALITY_ON as i64,
0,
(*state.systray).win as i64,
state.systray().win as i64,
XEMBED_EMBEDDED_VERSION as i64,
);
XSync(state.dpy, False);
Expand Down Expand Up @@ -540,7 +541,7 @@ pub(crate) fn maprequest(state: &mut State, e: *mut XEvent) {
CurrentTime as i64,
XEMBED_WINDOW_ACTIVATE as i64,
0,
(*state.systray).win as i64,
state.systray().win as i64,
XEMBED_EMBEDDED_VERSION as i64,
);
resizebarwin(state, state.selmon);
Expand Down
2 changes: 1 addition & 1 deletion src/key_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub(crate) fn togglebar(state: &mut State, _arg: *const Arg) {
}
XConfigureWindow(
state.dpy,
(*state.systray).win,
state.systray().win,
CWY as u32,
&mut wc,
);
Expand Down
57 changes: 25 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ fn setup(dpy: *mut Display) -> State {
),
numlockmask: 0,
running: true,
systray: null_mut(),
systray: None,
xcon: null_mut(),
};

Expand Down Expand Up @@ -1198,7 +1198,7 @@ fn updatesystrayiconstate(
CurrentTime as i64,
code as i64,
0,
(*state.systray).win as i64,
state.systray().win as i64,
XEMBED_EMBEDDED_VERSION as i64,
);
}
Expand Down Expand Up @@ -1241,10 +1241,9 @@ fn updatesystray(state: &mut State) {
if CONFIG.systrayonleft {
x -= sw + state.lrpad / 2;
}
if state.systray.is_null() {
if state.systray.is_none() {
// init systray
state.systray = ecalloc(1, size_of::<Systray>()).cast();
(*state.systray).win = XCreateSimpleWindow(
let win = XCreateSimpleWindow(
state.dpy,
state.root,
x,
Expand All @@ -1258,14 +1257,10 @@ fn updatesystray(state: &mut State) {
wa.event_mask = ButtonPressMask | ExposureMask;
wa.override_redirect = True;
wa.background_pixel = state.scheme[(Scheme::Norm, Col::Bg)].pixel;
XSelectInput(
state.dpy,
(*state.systray).win,
SubstructureNotifyMask,
);
XSelectInput(state.dpy, win, SubstructureNotifyMask);
XChangeProperty(
state.dpy,
(*state.systray).win,
win,
state.netatom[Net::SystemTrayOrientation as usize],
XA_CARDINAL,
32,
Expand All @@ -1276,21 +1271,21 @@ fn updatesystray(state: &mut State) {
);
XChangeWindowAttributes(
state.dpy,
(*state.systray).win,
win,
CWEventMask | CWOverrideRedirect | CWBackPixel,
&mut wa,
);
XMapRaised(state.dpy, (*state.systray).win);
XMapRaised(state.dpy, win);
XSetSelectionOwner(
state.dpy,
state.netatom[Net::SystemTray as usize],
(*state.systray).win,
win,
CurrentTime,
);
if XGetSelectionOwner(
state.dpy,
state.netatom[Net::SystemTray as usize],
) == (*state.systray).win
) == win
{
sendevent(
state,
Expand All @@ -1299,19 +1294,18 @@ fn updatesystray(state: &mut State) {
StructureNotifyMask as i32,
CurrentTime as i64,
state.netatom[Net::SystemTray as usize] as i64,
(*state.systray).win as i64,
win as i64,
0_i64,
0_i64,
);
XSync(state.dpy, False);
state.systray = Some(Systray { win, icons: null_mut() });
} else {
log::error!("unable to obtain system tray");
libc::free(state.systray.cast());
state.systray = null_mut();
return;
}
} // end if !SYSTRAY
cfor!(((w, i) = (0, (*state.systray).icons);
cfor!(((w, i) = (0, state.systray().icons);
!i.is_null();
i = (*i).next) {
// make sure the background color stays the same
Expand All @@ -1330,7 +1324,7 @@ fn updatesystray(state: &mut State) {
x -= w as i32;
XMoveResizeWindow(
state.dpy,
(*state.systray).win,
state.systray().win,
x,
(*m).by,
w,
Expand All @@ -1347,12 +1341,12 @@ fn updatesystray(state: &mut State) {
};
XConfigureWindow(
state.dpy,
(*state.systray).win,
state.systray().win,
(CWX | CWY | CWWidth | CWHeight | CWSibling | CWStackMode) as u32,
&mut wc,
);
XMapWindow(state.dpy, (*state.systray).win);
XMapSubwindows(state.dpy, (*state.systray).win);
XMapWindow(state.dpy, state.systray().win);
XMapSubwindows(state.dpy, state.systray().win);
// redraw background
XSetForeground(
state.dpy,
Expand All @@ -1361,7 +1355,7 @@ fn updatesystray(state: &mut State) {
);
XFillRectangle(
state.dpy,
(*state.systray).win,
state.systray().win,
state.drw.gc,
0,
0,
Expand All @@ -1378,7 +1372,7 @@ fn wintosystrayicon(state: &State, w: Window) -> *mut Client {
if !CONFIG.showsystray || w == 0 {
return i;
}
cfor!((i = (*state.systray).icons; !i.is_null() && (*i).win != w;
cfor!((i = state.systray().icons; !i.is_null() && (*i).win != w;
i = (*i).next) {});

i
Expand Down Expand Up @@ -1703,7 +1697,7 @@ fn updatebars(state: &mut State) {
state.cursors.normal.cursor,
);
if CONFIG.showsystray && m == systraytomon(state, m) {
xlib::XMapRaised(state.dpy, (*state.systray).win);
xlib::XMapRaised(state.dpy, state.systray().win);
}
xlib::XMapRaised(state.dpy, (*m).barwin);
xlib::XSetClassHint(state.dpy, (*m).barwin, &mut ch);
Expand Down Expand Up @@ -1919,14 +1913,14 @@ fn recttomon(
}
}

fn removesystrayicon(state: &State, i: *mut Client) {
fn removesystrayicon(state: &mut State, i: *mut Client) {
unsafe {
if !CONFIG.showsystray || i.is_null() {
return;
}
let mut ii: *mut *mut Client;
cfor!((
ii = &mut (*state.systray).icons as *mut _;
ii = &mut state.systray_mut().icons as *mut _;
!ii.is_null() && *ii != i;
ii = &mut (*(*ii)).next) {});
if !ii.is_null() {
Expand Down Expand Up @@ -2106,9 +2100,8 @@ fn cleanup(mut state: State) {
}

if CONFIG.showsystray {
XUnmapWindow(state.dpy, (*state.systray).win);
XDestroyWindow(state.dpy, (*state.systray).win);
libc::free(state.systray.cast());
XUnmapWindow(state.dpy, state.systray().win);
XDestroyWindow(state.dpy, state.systray().win);
}

xlib::XDestroyWindow(state.dpy, state.wmcheckwin);
Expand Down Expand Up @@ -2772,7 +2765,7 @@ fn getsystraywidth(state: &State) -> c_uint {
let mut i;
if CONFIG.showsystray {
cfor!((
i = (*state.systray).icons;
i = state.systray().icons;
!i.is_null();
(w, i) = (w + (*i).w + config::CONFIG.systrayspacing as i32, (*i).next))
{});
Expand Down
12 changes: 11 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct State {
pub root: Window,
/// sum of left and right padding for text
pub lrpad: c_int,
pub systray: *mut Systray,
pub systray: Option<Systray>,
/// Supporting window for NetWMCheck
pub wmcheckwin: Window,
pub running: bool,
Expand All @@ -71,6 +71,16 @@ pub struct State {
pub xcon: *mut Connection,
}

impl State {
pub fn systray(&self) -> &Systray {
self.systray.as_ref().unwrap()
}

pub fn systray_mut(&mut self) -> &mut Systray {
self.systray.as_mut().unwrap()
}
}

impl Drop for State {
fn drop(&mut self) {
unsafe {
Expand Down

0 comments on commit 4b69602

Please sign in to comment.