1
0
src/dmenubar/dmenubar.c

262 lines
6.9 KiB
C
Raw Normal View History

2011-05-14 10:47:12 -06:00
/* See LICENSE file for copyright and license details. */
/* I'm not a very good C programmer and this isn't the best I can do.
* There are a lot of fragments here from Suckless' dmenu. I would
* recommend hacking on that instead. This is itself a total hack and
* not a pretty one, but it works and I'm at least proud of that.
* ~ trinity */
2006-08-04 01:35:27 -06:00
#include <ctype.h>
#include <locale.h>
2006-08-04 01:35:27 -06:00
#include <stdio.h>
2008-06-13 04:46:50 -06:00
#include <stdlib.h>
2006-08-04 01:35:27 -06:00
#include <string.h>
2011-05-15 14:54:26 -06:00
#include <strings.h>
#include <time.h>
#include <unistd.h>
2015-09-27 16:38:17 -06:00
2007-09-16 12:14:09 -06:00
#include <X11/Xlib.h>
2010-08-10 06:38:49 -06:00
#include <X11/Xatom.h>
2006-08-04 01:35:27 -06:00
#include <X11/Xutil.h>
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif
#include <X11/Xft/Xft.h>
#include "drw.h"
#include "util.h"
/* macros */
#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)))
#define LENGTH(X) (sizeof X / sizeof X[0])
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
/* enums */
2021-05-15 22:24:38 -06:00
enum { Scheme, SchemeLast }; /* color schemes */
/* enumerated because Suckless has SchemeNorm, SchemeSel, etc.
* should be made into like one char *Scheme[2] */
/* BTW SchemeLast is the size of an array of the schemes and it's used
* in this program for iteration. Assumes enums will be assigned
* 0,1,2,3... */
2007-01-11 07:52:37 -07:00
2016-10-08 06:08:28 -06:00
static char *embed;
2010-08-18 10:33:34 -06:00
static int bh, mw, mh;
2021-05-15 22:24:38 -06:00
static int inputw = 0;
static int lrpad; /* sum of left and right padding */
static int mon = -1, screen;
static Atom clip, utf8;
static Display *dpy;
2016-10-08 06:08:28 -06:00
static Window root, parentwin, win;
2011-10-16 10:21:33 -06:00
static XIC xic;
static Drw *drw;
static Clr *scheme[SchemeLast];
#include "config.h"
static void
cleanup(void)
{
size_t i;
for (i = 0; i < SchemeLast; i++)
free(scheme[i]);
drw_free(drw);
XSync(dpy, False);
XCloseDisplay(dpy);
}
2021-05-16 08:06:08 -06:00
int
textw(char *text) /* figure out how to remove me */
{
return (text && *text) ? TEXTW(text) - lrpad / 4 : 0;
}
static void
2021-05-15 22:24:38 -06:00
drawmenu(char *text)
{
2021-05-16 08:06:08 -06:00
int x = 0;
2010-08-03 10:10:29 -06:00
2021-05-15 22:24:38 -06:00
drw_setscheme(drw, scheme[Scheme]);
drw_rect(drw, 0, 0, mw, mh, 1, 1);
2010-08-09 04:54:46 -06:00
2021-05-15 22:24:38 -06:00
if (text && *text)
x = drw_text(drw, x, 0, textw(text), bh, lrpad / 2, text, 0);
2021-05-11 12:48:58 -06:00
drw_map(drw, win, 0, 0, mw, mh);
}
/* I don't know how anyting here works */
static void
setup(void)
{
int x, y, i, j;
unsigned int du;
XSetWindowAttributes swa;
2011-10-16 10:21:33 -06:00
XIM xim;
Window w, dw, *dws;
XWindowAttributes wa;
XClassHint ch = {"dmenubar", "dmenubar"};
#ifdef XINERAMA
XineramaScreenInfo *info;
Window pw;
int a, di, n, area = 0;
#endif
/* init appearance */
for (j = 0; j < SchemeLast; j++)
scheme[j] = drw_scm_create(drw, colors[j], 2);
clip = XInternAtom(dpy, "CLIPBOARD", False);
utf8 = XInternAtom(dpy, "UTF8_STRING", False);
2011-05-15 07:13:31 -06:00
2011-10-26 06:20:14 -06:00
/* calculate menu geometry */
bh = drw->fonts->h + 2;
2021-05-11 12:02:27 -06:00
mh = bh;
#ifdef XINERAMA
i = 0;
2016-10-08 06:08:28 -06:00
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) {
2011-10-26 06:20:14 -06:00
/* 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);
2011-10-26 06:20:14 -06:00
/* 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;
}
}
2011-10-26 06:20:14 -06:00
/* 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;
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;
XFree(info);
} else
#endif
{
2016-10-08 06:08:28 -06:00
if (!XGetWindowAttributes(dpy, parentwin, &wa))
die("could not get embedding window attributes: 0x%lx",
parentwin);
x = 0;
2016-10-08 06:08:28 -06:00
y = topbar ? 0 : wa.height - mh;
mw = wa.width;
}
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 */
swa.override_redirect = True;
2021-05-15 22:24:38 -06:00
swa.background_pixel = scheme[Scheme][ColBg].pixel;
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);
XSetClassHint(dpy, win, &ch);
/* input methods */
if ((xim = XOpenIM(dpy, NULL, NULL, NULL)) == NULL)
die("XOpenIM failed: could not open input device");
2011-10-16 10:21:33 -06:00
xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
2011-10-16 18:18:57 -06:00
XNClientWindow, win, XNFocusWindow, win, NULL);
2011-10-16 10:21:33 -06:00
XMapRaised(dpy, win);
2016-10-08 06:08:28 -06:00
if (embed) {
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);
}
}
drw_resize(drw, mw, mh);
}
2011-05-18 09:20:03 -06:00
static void
usage(void)
{
fputs("usage: dmenubar [-bv] [-fn font] [-m monitor]\n"
" [-cb color] [-cf color] [-w windowid]\n", stderr);
exit(1);
2011-05-18 09:20:03 -06:00
}
int
main(int argc, char *argv[])
{
2016-10-08 06:08:28 -06:00
XWindowAttributes wa;
2021-05-11 12:19:56 -06:00
int i;
2021-05-15 22:24:38 -06:00
char text[text_s];
for(i = 0; i < text_s; ++i) /* initialize text[] */
2021-05-15 22:24:38 -06:00
text[i] = '\0';
for (i = 1; i < argc; i++)
/* these options take no arguments */
if (!strcmp(argv[i], "-v")) { /* prints version information */
puts("dmenuBAR-"VERSION);
exit(0);
2015-09-27 16:38:17 -06:00
} else if (!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */
2015-11-08 15:03:34 -07:00
topbar = 0;
2021-05-11 12:14:33 -06:00
else if (i + 1 == argc)
usage();
/* these options take one argument */
else if (!strcmp(argv[i], "-m"))
mon = atoi(argv[++i]);
else if (!strcmp(argv[i], "-fn")) /* font or font set */
fonts[0] = argv[++i];
else if (!strcmp(argv[i], "-cb")) /* bg color */
2021-05-15 22:24:38 -06:00
colors[Scheme][ColBg] = argv[++i];
else if (!strcmp(argv[i], "-cf")) /* fg color */
2021-05-15 22:24:38 -06:00
colors[Scheme][ColFg] = argv[++i];
2016-10-08 06:08:28 -06:00
else if (!strcmp(argv[i], "-w")) /* embedding window id */
embed = argv[++i];
else
usage();
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
fputs("warning: no locale support\n", stderr);
if (!(dpy = XOpenDisplay(NULL)))
2016-08-12 06:39:30 -06:00
die("cannot open display");
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
2016-10-08 06:08:28 -06:00
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, LENGTH(fonts)))
2016-08-12 06:39:30 -06:00
die("no fonts could be loaded.");
lrpad = drw->fonts->h;
/* ??? */
#ifdef __OpenBSD__
if (pledge("stdio rpath", NULL) == -1)
2018-05-25 04:04:22 -06:00
die("pledge");
#endif
2018-05-25 04:04:22 -06:00
setup();
do {
2021-05-15 22:24:38 -06:00
/* fgets adds a newline which will be printable in some fonts */
if (strlen(text) > 0)
2021-05-15 22:24:38 -06:00
text[strlen(text)-1] = '\0';
drawmenu(text);
} while (fgets(text, text_s, stdin) != NULL || !BREAK_ON_EOF);
cleanup();
return 0;
}