1
0
src/dmenubar/dmenubar.c
2023-09-14 11:35:36 -04:00

235 lines
5.9 KiB
C

/* See LICENSE file for copyright and license details. */
/* This is a hack I did years ago. Fork dmenu and start from there if you can.
* ~ trinity */
#include <locale.h> /* setlocale(3), LC_CTYPE */
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* strlen(3) */
#include <sysexits.h> /* EX_USAGE */
#include <unistd.h> /* getopt(3) */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef XINERAMA
# include <X11/extensions/Xinerama.h>
#endif
#include <X11/Xft/Xft.h>
#include "drw.h"
#include "util.h"
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
* MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
/* chars to be allocated for the text displayed in the bar */
#define BUFSIZE 1024
enum { Scheme, SchemeLast }; /* color schemes */
/* enumerated because Suckless has SchemeNorm, SchemeSel, etc.
* should be made into like one char *Scheme[2] */
static int bh;
static XClassHint ch = {"dmenubar", "dmenubar"};
static Display *dpy;
static Drw *drw;
static char *embed;
static int inputw = 0;
static int lrpad; /* sum of left and right padding */
static int mh;
static int mon = -1;
static int mw;
static Window parentwin;
static Window root;
static Clr *scheme[SchemeLast];
static int screen;
static Window win;
#define BREAK_ON_EOF 0
/* -f option overrides fonts[0]; default X11 font or font set */
static const char *fonts[] = {
"monospace:size=10"
};
static const char *colors[SchemeLast][2] = {
/* fg, bg */
[Scheme] = { "#eeeeee", "#005577" }
};
int
main(int argc, char *argv[]){
int c;
unsigned int du;
Window dw;
Window *dws;
int i;
int j;
extern char *optarg;
XSetWindowAttributes swa;
char text[BUFSIZE];
int topbar; /* 0=bottom 1=top */
Window w;
XWindowAttributes wa;
int x;
int y;
#ifdef XINERAMA
XineramaScreenInfo *info;
Window pw;
int a, di, n, area = 0;
#endif
topbar = 1;
while((c = getopt(argc, argv, ":bf:m:B:F:w:")) != -1)
switch(c){
case 'b':
topbar = 0;
break;
case 'B': if(optarg){
colors[Scheme][ColBg] = optarg;
break;
}case 'f': if(optarg){
fonts[0] = optarg;
break;
}case 'F': if(optarg){
colors[Scheme][ColFg] = optarg;
break;
}case 'm': if(optarg){
mon = atoi(optarg);
break;
}case 'w': if(optarg){
embed = optarg;
break;
}default: /* optarg==0 falls through */
fprintf(stderr,
"Usage: %s (-b) (-B [background color])"
"(-f [font]) (-F foreground color)\n"
"\t(-m [monitor]) (-w [windowid)\n",
argv[0]);
return EX_USAGE;
}
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
fprintf(stderr, "%s: No locale support.\n", argv[0]);
if (!(dpy = XOpenDisplay(NULL)))
die("cannot open display");
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
if (!embed || !(parentwin = strtol(embed, NULL, 0)))
parentwin = root;
if (!XGetWindowAttributes(dpy, parentwin, &wa))
die("could not get embedding window attributes: 0x%lx",
parentwin);
drw = drw_create(dpy, screen, root, wa.width, wa.height);
if (!drw_fontset_create(drw, fonts, (sizeof fonts) / (sizeof *fonts)))
die("no fonts could be loaded.");
lrpad = drw->fonts->h;
/* ??? */
#ifdef __OpenBSD__
if (pledge("stdio rpath", NULL) == -1)
die("pledge");
#endif
/* init appearance */
for (j = 0; j < SchemeLast; j++)
scheme[j] = drw_scm_create(drw, colors[j], 2);
/* calculate menu geometry */
bh = drw->fonts->h + 2;
mh = bh;
#ifdef XINERAMA
i = 0;
if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) {
XGetInputFocus(dpy, &w, &di);
if (mon >= 0 && mon < n)
i = mon;
else if (w != root && w != PointerRoot && w != None) {
/* find top-level window containing current input focus */
do {
if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws)
XFree(dws);
} while (w != root && w != pw);
/* find xinerama screen with which the window intersects most */
if (XGetWindowAttributes(dpy, pw, &wa))
for (j = 0; j < n; j++)
if ((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) {
area = a;
i = j;
}
}
/* no focused window is on screen, so use pointer location instead */
if (mon < 0 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
for (i = 0; i < n; i++)
if (INTERSECT(x, y, 1, 1, info[i]))
break;
x = info[i].x_org;
y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
mw = info[i].width;
XFree(info);
} else
#endif
{
if (!XGetWindowAttributes(dpy, parentwin, &wa))
die("could not get embedding window attributes: 0x%lx",
parentwin);
x = 0;
y = topbar ? 0 : wa.height - mh;
mw = wa.width;
}
inputw = MIN(inputw, mw/3);
/* create menu window */
swa.override_redirect = True;
swa.background_pixel = scheme[Scheme][ColBg].pixel;
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
win = XCreateWindow(dpy, parentwin, x, y, mw, mh, 0,
CopyFromParent, CopyFromParent, CopyFromParent,
CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
XSetClassHint(dpy, win, &ch);
XMapRaised(dpy, win);
if (embed) {
XSelectInput(dpy, parentwin, FocusChangeMask | SubstructureNotifyMask);
if (XQueryTree(dpy, parentwin, &dw, &w, &dws, &du) && dws) {
for (i = 0; i < du && dws[i] != win; ++i)
XSelectInput(dpy, dws[i], FocusChangeMask);
XFree(dws);
}
}
drw_resize(drw, mw, mh);
text[0] = '\0';
do{
/* trim newline */
for(i = 0; text[i] != '\0' && text[i] != '\n';);
if(text[i] == '\n')
text[i] = '\0';
drw_setscheme(drw, scheme[Scheme]);
drw_rect(drw, 0, 0, mw, mh, 1, 1);
if(*text != '\0')
drw_text(drw, x, 0,
*text != '\0'
? (drw_fontset_getwidth(drw, text)
+ lrpad) - lrpad / 4
: 0,
bh, lrpad / 2, text, 0);
drw_map(drw, win, 0, 0, mw, mh);
}while(
fgets(text, (sizeof text) / (sizeof *text), stdin) != NULL
|| !BREAK_ON_EOF
);
for (i = 0; i < SchemeLast; i++)
free(scheme[i]);
drw_free(drw);
XSync(dpy, False);
XCloseDisplay(dpy);
return 0;
}