2011-05-14 10:47:12 -06:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2021-05-15 22:41:18 -06:00
|
|
|
|
2023-09-14 08:07:38 -06:00
|
|
|
/* This is a hack I did years ago. Fork dmenu and start from there if you can.
|
2021-05-15 22:41:18 -06:00
|
|
|
* ~ trinity */
|
|
|
|
|
2023-09-14 09:09:21 -06:00
|
|
|
#include <locale.h> /* setlocale(3), LC_CTYPE */
|
2006-08-04 01:35:27 -06:00
|
|
|
#include <stdio.h>
|
2008-06-13 04:46:50 -06:00
|
|
|
#include <stdlib.h>
|
2023-09-14 09:09:21 -06:00
|
|
|
#include <string.h> /* strlen(3) */
|
2023-09-14 09:09:05 -06:00
|
|
|
#include <sysexits.h> /* EX_USAGE */
|
2023-09-14 09:09:21 -06:00
|
|
|
#include <unistd.h> /* getopt(3) */
|
2007-09-16 12:14:09 -06:00
|
|
|
#include <X11/Xlib.h>
|
2006-08-04 01:35:27 -06:00
|
|
|
#include <X11/Xutil.h>
|
2010-07-30 06:40:56 -06:00
|
|
|
#ifdef XINERAMA
|
2023-09-14 09:35:36 -06:00
|
|
|
# include <X11/extensions/Xinerama.h>
|
2010-07-30 06:40:56 -06:00
|
|
|
#endif
|
2015-05-04 13:54:46 -06:00
|
|
|
#include <X11/Xft/Xft.h>
|
|
|
|
#include "drw.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2011-10-26 05:14:50 -06:00
|
|
|
#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)))
|
2015-05-04 13:54:46 -06:00
|
|
|
|
2023-09-14 09:35:36 -06:00
|
|
|
/* chars to be allocated for the text displayed in the bar */
|
|
|
|
#define BUFSIZE 1024
|
|
|
|
|
2021-05-15 22:24:38 -06:00
|
|
|
enum { Scheme, SchemeLast }; /* color schemes */
|
2021-05-15 22:41:18 -06:00
|
|
|
/* enumerated because Suckless has SchemeNorm, SchemeSel, etc.
|
|
|
|
* should be made into like one char *Scheme[2] */
|
2007-01-11 07:52:37 -07:00
|
|
|
|
2023-09-14 09:35:36 -06:00
|
|
|
static int bh;
|
|
|
|
static XClassHint ch = {"dmenubar", "dmenubar"};
|
|
|
|
static Display *dpy;
|
|
|
|
static Drw *drw;
|
2016-10-08 06:08:28 -06:00
|
|
|
static char *embed;
|
2021-05-15 22:24:38 -06:00
|
|
|
static int inputw = 0;
|
2016-05-21 13:51:14 -06:00
|
|
|
static int lrpad; /* sum of left and right padding */
|
2023-09-14 09:35:36 -06:00
|
|
|
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;
|
2015-09-27 15:57:39 -06:00
|
|
|
|
2023-09-14 09:35:36 -06:00
|
|
|
#define BREAK_ON_EOF 0
|
2010-07-30 06:40:56 -06:00
|
|
|
|
2023-09-14 09:35:36 -06:00
|
|
|
/* -f option overrides fonts[0]; default X11 font or font set */
|
|
|
|
static const char *fonts[] = {
|
|
|
|
"monospace:size=10"
|
|
|
|
};
|
2015-05-04 13:54:46 -06:00
|
|
|
|
2023-09-14 09:35:36 -06:00
|
|
|
static const char *colors[SchemeLast][2] = {
|
|
|
|
/* fg, bg */
|
|
|
|
[Scheme] = { "#eeeeee", "#005577" }
|
|
|
|
};
|
2013-04-17 13:04:05 -06:00
|
|
|
|
2023-09-14 09:09:05 -06:00
|
|
|
int
|
2023-09-14 09:09:21 -06:00
|
|
|
main(int argc, char *argv[]){
|
|
|
|
int c;
|
2016-11-25 05:38:09 -07:00
|
|
|
unsigned int du;
|
2023-09-14 09:35:36 -06:00
|
|
|
Window dw;
|
|
|
|
Window *dws;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
extern char *optarg;
|
2011-09-19 11:15:03 -06:00
|
|
|
XSetWindowAttributes swa;
|
2023-09-14 09:35:36 -06:00
|
|
|
char text[BUFSIZE];
|
|
|
|
int topbar; /* 0=bottom 1=top */
|
|
|
|
Window w;
|
2016-11-25 05:38:09 -07:00
|
|
|
XWindowAttributes wa;
|
2023-09-14 09:35:36 -06:00
|
|
|
int x;
|
|
|
|
int y;
|
2010-07-31 07:56:27 -06:00
|
|
|
#ifdef XINERAMA
|
|
|
|
XineramaScreenInfo *info;
|
2016-11-25 05:38:09 -07:00
|
|
|
Window pw;
|
2018-01-04 05:27:37 -07:00
|
|
|
int a, di, n, area = 0;
|
2010-07-30 06:40:56 -06:00
|
|
|
#endif
|
2023-09-14 09:09:05 -06:00
|
|
|
|
2023-09-14 09:35:36 -06:00
|
|
|
topbar = 1;
|
2023-09-14 09:09:21 -06:00
|
|
|
while((c = getopt(argc, argv, ":bf:m:B:F:w:")) != -1)
|
|
|
|
switch(c){
|
|
|
|
case 'b':
|
2023-09-14 09:09:05 -06:00
|
|
|
topbar = 0;
|
2023-09-14 09:09:21 -06:00
|
|
|
break;
|
|
|
|
case 'B': if(optarg){
|
|
|
|
colors[Scheme][ColBg] = optarg;
|
|
|
|
break;
|
|
|
|
}case 'f': if(optarg){
|
|
|
|
fonts[0] = optarg;
|
|
|
|
break;
|
|
|
|
}case 'F': if(optarg){
|
2023-09-14 09:35:36 -06:00
|
|
|
colors[Scheme][ColFg] = optarg;
|
2023-09-14 09:09:21 -06:00
|
|
|
break;
|
|
|
|
}case 'm': if(optarg){
|
2023-09-14 09:35:36 -06:00
|
|
|
mon = atoi(optarg);
|
2023-09-14 09:09:21 -06:00
|
|
|
break;
|
|
|
|
}case 'w': if(optarg){
|
2023-09-14 09:35:36 -06:00
|
|
|
embed = optarg;
|
2023-09-14 09:09:21 -06:00
|
|
|
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;
|
|
|
|
}
|
2023-09-14 09:09:05 -06:00
|
|
|
|
|
|
|
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
|
2023-09-14 09:09:21 -06:00
|
|
|
fprintf(stderr, "%s: No locale support.\n", argv[0]);
|
2023-09-14 09:09:05 -06:00
|
|
|
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);
|
2023-09-14 09:09:21 -06:00
|
|
|
if (!drw_fontset_create(drw, fonts, (sizeof fonts) / (sizeof *fonts)))
|
2023-09-14 09:09:05 -06:00
|
|
|
die("no fonts could be loaded.");
|
|
|
|
lrpad = drw->fonts->h;
|
|
|
|
|
|
|
|
/* ??? */
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
if (pledge("stdio rpath", NULL) == -1)
|
|
|
|
die("pledge");
|
|
|
|
#endif
|
|
|
|
|
2015-05-04 13:54:46 -06:00
|
|
|
/* init appearance */
|
2017-11-03 14:05:29 -06:00
|
|
|
for (j = 0; j < SchemeLast; j++)
|
|
|
|
scheme[j] = drw_scm_create(drw, colors[j], 2);
|
2010-07-30 06:40:56 -06:00
|
|
|
|
2011-10-26 06:20:14 -06:00
|
|
|
/* calculate menu geometry */
|
2016-05-21 13:51:14 -06:00
|
|
|
bh = drw->fonts->h + 2;
|
2021-05-11 12:02:27 -06:00
|
|
|
mh = bh;
|
2010-07-31 07:56:27 -06:00
|
|
|
#ifdef XINERAMA
|
2018-01-04 15:45:49 -07:00
|
|
|
i = 0;
|
2016-10-08 06:08:28 -06:00
|
|
|
if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) {
|
2015-05-04 13:54:46 -06:00
|
|
|
XGetInputFocus(dpy, &w, &di);
|
2016-07-26 15:13:06 -06:00
|
|
|
if (mon >= 0 && mon < n)
|
2013-08-02 14:30:20 -06:00
|
|
|
i = mon;
|
2015-11-07 04:43:00 -07:00
|
|
|
else if (w != root && w != PointerRoot && w != None) {
|
2011-10-26 06:20:14 -06:00
|
|
|
/* find top-level window containing current input focus */
|
2011-10-26 05:14:50 -06:00
|
|
|
do {
|
2015-09-27 15:57:39 -06:00
|
|
|
if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws)
|
2011-10-26 05:14:50 -06:00
|
|
|
XFree(dws);
|
2015-09-27 15:57:39 -06:00
|
|
|
} while (w != root && w != pw);
|
2011-10-26 06:20:14 -06:00
|
|
|
/* find xinerama screen with which the window intersects most */
|
2015-09-27 15:57:39 -06:00
|
|
|
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) {
|
2011-10-26 05:14:50 -06:00
|
|
|
area = a;
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
}
|
2011-10-26 06:20:14 -06:00
|
|
|
/* no focused window is on screen, so use pointer location instead */
|
2016-07-26 15:13:06 -06:00
|
|
|
if (mon < 0 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
|
2015-09-27 15:57:39 -06:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
if (INTERSECT(x, y, 1, 1, info[i]))
|
2011-10-26 05:14:50 -06:00
|
|
|
break;
|
2011-10-26 06:20:14 -06:00
|
|
|
|
2011-05-12 06:17:41 -06:00
|
|
|
x = info[i].x_org;
|
|
|
|
y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
|
|
|
|
mw = info[i].width;
|
2010-07-30 06:40:56 -06:00
|
|
|
XFree(info);
|
2015-09-27 15:57:39 -06:00
|
|
|
} else
|
2010-07-30 06:40:56 -06:00
|
|
|
#endif
|
|
|
|
{
|
2016-10-08 06:08:28 -06:00
|
|
|
if (!XGetWindowAttributes(dpy, parentwin, &wa))
|
|
|
|
die("could not get embedding window attributes: 0x%lx",
|
|
|
|
parentwin);
|
2010-07-30 06:40:56 -06:00
|
|
|
x = 0;
|
2016-10-08 06:08:28 -06:00
|
|
|
y = topbar ? 0 : wa.height - mh;
|
|
|
|
mw = wa.width;
|
2010-07-30 06:40:56 -06:00
|
|
|
}
|
2011-05-18 09:20:03 -06:00
|
|
|
inputw = MIN(inputw, mw/3);
|
2011-05-15 07:21:00 -06:00
|
|
|
|
2011-10-26 06:20:14 -06:00
|
|
|
/* create menu window */
|
2011-09-19 11:15:03 -06:00
|
|
|
swa.override_redirect = True;
|
2021-05-15 22:24:38 -06:00
|
|
|
swa.background_pixel = scheme[Scheme][ColBg].pixel;
|
2011-09-19 11:15:03 -06:00
|
|
|
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
|
2016-10-08 06:08:28 -06:00
|
|
|
win = XCreateWindow(dpy, parentwin, x, y, mw, mh, 0,
|
|
|
|
CopyFromParent, CopyFromParent, CopyFromParent,
|
2011-11-23 06:40:21 -07:00
|
|
|
CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
|
2017-11-03 10:49:10 -06:00
|
|
|
XSetClassHint(dpy, win, &ch);
|
2010-07-30 06:40:56 -06:00
|
|
|
|
2019-02-12 11:10:43 -07:00
|
|
|
|
2015-05-04 13:54:46 -06:00
|
|
|
XMapRaised(dpy, win);
|
2016-10-08 06:08:28 -06:00
|
|
|
if (embed) {
|
2019-02-03 16:29:26 -07:00
|
|
|
XSelectInput(dpy, parentwin, FocusChangeMask | SubstructureNotifyMask);
|
2016-10-08 06:08:28 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2015-05-04 13:54:46 -06:00
|
|
|
drw_resize(drw, mw, mh);
|
2021-05-15 22:41:18 -06:00
|
|
|
|
2023-09-14 09:35:36 -06:00
|
|
|
text[0] = '\0';
|
|
|
|
do{
|
|
|
|
/* trim newline */
|
|
|
|
for(i = 0; text[i] != '\0' && text[i] != '\n';);
|
|
|
|
if(text[i] == '\n')
|
|
|
|
text[i] = '\0';
|
|
|
|
|
2023-09-14 09:09:21 -06:00
|
|
|
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);
|
2023-09-14 09:35:36 -06:00
|
|
|
}while(
|
|
|
|
fgets(text, (sizeof text) / (sizeof *text), stdin) != NULL
|
|
|
|
|| !BREAK_ON_EOF
|
|
|
|
);
|
2015-09-27 15:57:39 -06:00
|
|
|
|
2023-09-14 09:09:21 -06:00
|
|
|
for (i = 0; i < SchemeLast; i++)
|
|
|
|
free(scheme[i]);
|
|
|
|
drw_free(drw);
|
|
|
|
XSync(dpy, False);
|
|
|
|
XCloseDisplay(dpy);
|
|
|
|
|
2021-05-16 07:59:02 -06:00
|
|
|
return 0;
|
2015-09-27 15:57:39 -06:00
|
|
|
}
|