/* 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 /* setlocale(3), LC_CTYPE */ #include #include #include /* strlen(3) */ #include /* EX_USAGE */ #include /* getopt(3) */ #include #include #ifdef XINERAMA #include #endif #include #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 */ 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. */ static char *embed; static int bh, mw, mh; static int inputw = 0; static int lrpad; /* sum of left and right padding */ static int mon = -1, screen; static Display *dpy; static Window root, parentwin, win; static Drw *drw; static Clr *scheme[SchemeLast]; #include "config.h" int main(int argc, char *argv[]){ int c; extern char *optarg; 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 for(i = 0; i < text_s; ++i) /* initialize text[] */ text[i] = '\0'; 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] = argv[++i]; break; }case 'm': if(optarg){ mon = atoi(argv[++i]); break; }case 'w': if(optarg){ embed = argv[++i]; 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); do { /* fgets adds a newline which will be printable in some fonts */ if (strlen(text) > 0) text[strlen(text)-1] = '\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, text_s, stdin) != NULL || !BREAK_ON_EOF); for (i = 0; i < SchemeLast; i++) free(scheme[i]); drw_free(drw); XSync(dpy, False); XCloseDisplay(dpy); return 0; }