dwm-flexipatch/patch/bar_systray.c

197 lines
5.3 KiB
C
Raw Normal View History

2019-09-05 20:10:00 +00:00
static Systray *systray = NULL;
static unsigned long systrayorientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
int
width_systray(Bar *bar, BarArg *a)
2019-09-05 20:10:00 +00:00
{
unsigned int w = 0;
Client *i;
if (!systray)
return 1;
2019-09-05 20:10:00 +00:00
if (showsystray)
for (i = systray->icons; i; w += i->w + systrayspacing, i = i->next);
2020-07-18 11:03:30 +00:00
return w ? w + lrpad - systrayspacing : 0;
2019-09-05 20:10:00 +00:00
}
int
draw_systray(Bar *bar, BarArg *a)
2019-09-05 20:10:00 +00:00
{
if (!showsystray)
return 0;
2020-07-18 11:03:30 +00:00
2019-09-05 20:10:00 +00:00
XSetWindowAttributes wa;
XWindowChanges wc;
2019-09-05 20:10:00 +00:00
Client *i;
2020-07-18 11:03:30 +00:00
unsigned int w;
2019-09-05 20:10:00 +00:00
if (!systray) {
/* init systray */
if (!(systray = (Systray *)calloc(1, sizeof(Systray))))
die("fatal: could not malloc() %u bytes\n", sizeof(Systray));
2020-07-18 11:03:30 +00:00
wa.override_redirect = True;
wa.event_mask = ButtonPressMask|ExposureMask;
wa.border_pixel = 0;
systray->h = MIN(a->h, drw->fonts->h);
2020-07-18 11:03:30 +00:00
#if BAR_ALPHA_PATCH
wa.background_pixel = 0;
wa.colormap = cmap;
systray->win = XCreateWindow(dpy, root, bar->bx + a->x + lrpad / 2, bar->by + a->y + (a->h - systray->h) / 2, MAX(a->w + 40, 1), systray->h, 0, depth,
2020-07-18 11:03:30 +00:00
InputOutput, visual,
CWOverrideRedirect|CWBorderPixel|CWBackPixel|CWColormap|CWEventMask, &wa); // CWBackPixmap
#else
wa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
systray->win = XCreateSimpleWindow(dpy, root, bar->bx + a->x + lrpad / 2, bar->by + a->y + (a->h - systray->h) / 2, MIN(a->w, 1), systray->h, 0, 0, scheme[SchemeNorm][ColBg].pixel);
2020-07-18 11:03:30 +00:00
XChangeWindowAttributes(dpy, systray->win, CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWEventMask, &wa);
#endif // BAR_ALPHA_PATCH
2020-07-18 11:03:30 +00:00
XSelectInput(dpy, systray->win, SubstructureNotifyMask);
2019-09-05 20:10:00 +00:00
XChangeProperty(dpy, systray->win, netatom[NetSystemTrayOrientation], XA_CARDINAL, 32,
PropModeReplace, (unsigned char *)&systrayorientation, 1);
#if BAR_ALPHA_PATCH
2019-09-05 20:10:00 +00:00
XChangeProperty(dpy, systray->win, netatom[NetSystemTrayVisual], XA_VISUALID, 32,
PropModeReplace, (unsigned char *)&visual->visualid, 1);
#endif // BAR_ALPHA_PATCH
2019-09-05 20:10:00 +00:00
XChangeProperty(dpy, systray->win, netatom[NetWMWindowType], XA_ATOM, 32,
PropModeReplace, (unsigned char *)&netatom[NetWMWindowTypeDock], 1);
XMapRaised(dpy, systray->win);
XSetSelectionOwner(dpy, netatom[NetSystemTray], systray->win, CurrentTime);
if (XGetSelectionOwner(dpy, netatom[NetSystemTray]) == systray->win) {
sendevent(root, xatom[Manager], StructureNotifyMask, CurrentTime, netatom[NetSystemTray], systray->win, 0, 0);
XSync(dpy, False);
} else {
2019-09-05 20:10:00 +00:00
fprintf(stderr, "dwm: unable to obtain system tray.\n");
free(systray);
systray = NULL;
return 0;
2019-09-05 20:10:00 +00:00
}
}
2020-07-18 11:03:30 +00:00
systray->bar = bar;
wc.stack_mode = Above;
wc.sibling = bar->win;
XConfigureWindow(dpy, systray->win, CWSibling|CWStackMode, &wc);
2020-07-18 11:03:30 +00:00
drw_setscheme(drw, scheme[SchemeNorm]);
2019-09-05 20:10:00 +00:00
for (w = 0, i = systray->icons; i; i = i->next) {
2020-07-18 11:03:30 +00:00
#if BAR_ALPHA_PATCH
wa.background_pixel = 0;
#else
2019-09-05 20:10:00 +00:00
wa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
2020-07-18 11:03:30 +00:00
#endif // BAR_ALPHA_PATCH
2019-09-05 20:10:00 +00:00
XChangeWindowAttributes(dpy, i->win, CWBackPixel, &wa);
XMapRaised(dpy, i->win);
2020-07-18 11:03:30 +00:00
i->x = w;
2019-09-05 20:10:00 +00:00
XMoveResizeWindow(dpy, i->win, i->x, 0, i->w, i->h);
w += i->w;
2020-07-18 11:03:30 +00:00
if (i->next)
w += systrayspacing;
if (i->mon != bar->mon)
i->mon = bar->mon;
}
XMoveResizeWindow(dpy, systray->win, bar->bx + a->x + lrpad / 2, (w ? bar->by + a->y + (a->h - systray->h) / 2: -bar->by - a->y), MAX(w, 1), systray->h);
return w;
2020-07-18 11:03:30 +00:00
}
int
click_systray(Bar *bar, Arg *arg, BarArg *a)
2020-07-18 11:03:30 +00:00
{
return -1;
}
void
removesystrayicon(Client *i)
{
Client **ii;
if (!showsystray || !i)
return;
for (ii = &systray->icons; *ii && *ii != i; ii = &(*ii)->next);
if (ii)
*ii = i->next;
free(i);
drawbarwin(systray->bar);
}
void
resizerequest(XEvent *e)
{
XResizeRequestEvent *ev = &e->xresizerequest;
Client *i;
if ((i = wintosystrayicon(ev->window))) {
updatesystrayicongeom(i, ev->width, ev->height);
drawbarwin(systray->bar);
2019-09-05 20:10:00 +00:00
}
}
void
updatesystrayicongeom(Client *i, int w, int h)
{
if (!systray)
return;
int icon_height = systray->h;
2019-09-05 20:10:00 +00:00
if (i) {
i->h = icon_height;
2019-09-05 20:10:00 +00:00
if (w == h)
i->w = icon_height;
else if (h == icon_height)
2019-09-05 20:10:00 +00:00
i->w = w;
else
i->w = (int) ((float)icon_height * ((float)w / (float)h));
2019-09-05 20:10:00 +00:00
applysizehints(i, &(i->x), &(i->y), &(i->w), &(i->h), False);
/* force icons into the systray dimensions if they don't want to */
if (i->h > icon_height) {
2019-09-05 20:10:00 +00:00
if (i->w == i->h)
i->w = icon_height;
2019-09-05 20:10:00 +00:00
else
i->w = (int) ((float)icon_height * ((float)i->w / (float)i->h));
i->h = icon_height;
2019-09-05 20:10:00 +00:00
}
if (i->w > 2 * icon_height)
i->w = icon_height;
2019-09-05 20:10:00 +00:00
}
}
void
updatesystrayiconstate(Client *i, XPropertyEvent *ev)
{
long flags;
int code = 0;
2020-09-07 10:43:37 +00:00
if (!showsystray || !systray || !i || ev->atom != xatom[XembedInfo] ||
2019-09-05 20:10:00 +00:00
!(flags = getatomprop(i, xatom[XembedInfo])))
return;
if (flags & XEMBED_MAPPED && !i->tags) {
i->tags = 1;
code = XEMBED_WINDOW_ACTIVATE;
XMapRaised(dpy, i->win);
setclientstate(i, NormalState);
}
else if (!(flags & XEMBED_MAPPED) && i->tags) {
i->tags = 0;
code = XEMBED_WINDOW_DEACTIVATE;
XUnmapWindow(dpy, i->win);
setclientstate(i, WithdrawnState);
}
else
return;
sendevent(i->win, xatom[Xembed], StructureNotifyMask, CurrentTime, code, 0,
systray->win, XEMBED_EMBEDDED_VERSION);
}
Client *
2019-09-10 22:51:37 +00:00
wintosystrayicon(Window w)
{
2020-07-18 11:03:30 +00:00
if (!systray)
return NULL;
2019-09-05 20:10:00 +00:00
Client *i = NULL;
if (!showsystray || !w)
return i;
for (i = systray->icons; i && i->win != w; i = i->next);
return i;
}