1
0
src/dmenubar/dmenubar.c

215 lines
5.7 KiB
C
Raw Normal View History

2011-05-14 10:47:12 -06:00
/* See LICENSE file for copyright and license details. */
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.
* ~ 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) */
2015-09-27 16:38:17 -06:00
2007-09-16 12:14:09 -06:00
#include <X11/Xlib.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)))
/* 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
2023-09-14 09:09:21 -06:00
* in this program for iteration. */
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 Display *dpy;
2016-10-08 06:08:28 -06:00
static Window root, parentwin, win;
static Drw *drw;
static Clr *scheme[SchemeLast];
#include "config.h"
2023-09-14 09:09:05 -06:00
int
2023-09-14 09:09:21 -06:00
main(int argc, char *argv[]){
int c;
extern char *optarg;
2023-09-14 09:09:05 -06:00
char text[text_s];
int x, y, i, j;
unsigned int du;
XSetWindowAttributes swa;
Window w, dw, *dws;
XWindowAttributes wa;
XClassHint ch = {"dmenubar", "dmenubar"};
#ifdef XINERAMA
XineramaScreenInfo *info;
Window pw;
int a, di, n, area = 0;
#endif
2023-09-14 09:09:05 -06:00
for(i = 0; i < text_s; ++i) /* initialize text[] */
text[i] = '\0';
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:09:05 -06:00
colors[Scheme][ColFg] = argv[++i];
2023-09-14 09:09:21 -06:00
break;
}case 'm': if(optarg){
mon = atoi(argv[++i]);
break;
}case 'w': if(optarg){
2023-09-14 09:09:05 -06:00
embed = argv[++i];
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
/* init appearance */
for (j = 0; j < SchemeLast; j++)
scheme[j] = drw_scm_create(drw, colors[j], 2);
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);
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);
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';
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);
} while (fgets(text, text_s, stdin) != NULL || !BREAK_ON_EOF);
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);
return 0;
}