Compare commits
10 Commits
ba1a347dca
...
d11991d997
| Author | SHA1 | Date |
|---|---|---|
|
|
d11991d997 | |
|
|
8e5365c96c | |
|
|
efe9765517 | |
|
|
b476bf037b | |
|
|
8331a88407 | |
|
|
1b31202e91 | |
|
|
31e972d7c1 | |
|
|
7ab0cb5ef0 | |
|
|
0fe460dbd4 | |
|
|
dfbbf7f6e1 |
56
config.def.h
56
config.def.h
|
|
@ -1,23 +1,63 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
/* Default settings; can be overriden by command line. */
|
||||
|
||||
static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */
|
||||
static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */
|
||||
static int centered = 0; /* -c option; centers dmenu on screen */
|
||||
static int min_width = 500; /* -cw option; minimum width when centered */
|
||||
static int fuzzy = 1; /* -F option; if 0, dmenu doesn't use fuzzy matching */
|
||||
|
||||
/* -fn option overrides fonts[0]; default X11 font or font set */
|
||||
static char font[] = "monospace:size=10";
|
||||
static const char *fonts[] = {
|
||||
"monospace:size=10"
|
||||
font,
|
||||
"monospace:size=10",
|
||||
};
|
||||
static const char *prompt = NULL; /* -p option; prompt to the left of input field */
|
||||
static const char *colors[SchemeLast][2] = {
|
||||
|
||||
static char *prompt = NULL; /* -p option; prompt to the left of input field */
|
||||
|
||||
static char normfgcolor[] = "#bbbbbb";
|
||||
static char normbgcolor[] = "#222222";
|
||||
static char normhlcolor[] = "#ffc978";
|
||||
static char selfgcolor[] = "#eeeeee";
|
||||
static char selbgcolor[] = "#005577";
|
||||
static char selhlcolor[] = "#ffc978";
|
||||
static char *colors[SchemeLast][2] = {
|
||||
/* fg bg */
|
||||
[SchemeNorm] = { "#bbbbbb", "#222222" },
|
||||
[SchemeSel] = { "#eeeeee", "#005577" },
|
||||
[SchemeOut] = { "#000000", "#00ffff" },
|
||||
[SchemeNorm] = { normfgcolor, normbgcolor },
|
||||
[SchemeNormHighlight] = { normhlcolor, normbgcolor },
|
||||
[SchemeSel] = { selfgcolor, selbgcolor },
|
||||
[SchemeSelHighlight] = { normhlcolor, selbgcolor },
|
||||
[SchemeOut] = { "#000000", "#00ffff" },
|
||||
};
|
||||
/* -l option; if nonzero, dmenu uses vertical list with given number of lines */
|
||||
/* -l and -g options; controls number of lines and columns in grid if > 0 */
|
||||
static unsigned int lines = 0;
|
||||
static unsigned int columns = 0;
|
||||
|
||||
/*
|
||||
* Characters not considered part of a word while deleting words
|
||||
* for example: " /?\"&[]"
|
||||
*/
|
||||
static const char worddelimiters[] = " ";
|
||||
|
||||
/* Size of the window border */
|
||||
static unsigned int borderpx = 0;
|
||||
|
||||
/*
|
||||
* Xresources preferences to load at startup
|
||||
*/
|
||||
ResourcePref resources[] = {
|
||||
{ "font", STRING, &font },
|
||||
{ "normfgcolor", STRING, &normfgcolor },
|
||||
{ "normbgcolor", STRING, &normbgcolor },
|
||||
{ "normhlcolor", STRING, &normhlcolor },
|
||||
{ "selfgcolor", STRING, &selfgcolor },
|
||||
{ "selbgcolor", STRING, &selbgcolor },
|
||||
{ "selhlcolor", STRING, &selhlcolor },
|
||||
{ "prompt", STRING, &prompt },
|
||||
{ "centered", INTEGER, ¢ered },
|
||||
{ "min_width", INTEGER, &min_width },
|
||||
{ "fuzzy", INTEGER, &fuzzy },
|
||||
{ "lines", INTEGER, &lines },
|
||||
{ "columns", INTEGER, &columns },
|
||||
{ "borderpx", INTEGER, &borderpx },
|
||||
};
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ FREETYPEINC = /usr/include/freetype2
|
|||
|
||||
# includes and libs
|
||||
INCS = -I$(X11INC) -I$(FREETYPEINC)
|
||||
LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS)
|
||||
LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) -lm
|
||||
|
||||
# flags
|
||||
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" $(XINERAMAFLAGS)
|
||||
|
|
|
|||
30
dmenu.1
30
dmenu.1
|
|
@ -4,6 +4,8 @@ dmenu \- dynamic menu
|
|||
.SH SYNOPSIS
|
||||
.B dmenu
|
||||
.RB [ \-bfiv ]
|
||||
.RB [ \-g
|
||||
.IR columns ]
|
||||
.RB [ \-l
|
||||
.IR lines ]
|
||||
.RB [ \-m
|
||||
|
|
@ -20,6 +22,14 @@ dmenu \- dynamic menu
|
|||
.IR color ]
|
||||
.RB [ \-sf
|
||||
.IR color ]
|
||||
.RB [ \-nhb
|
||||
.IR color ]
|
||||
.RB [ \-nhf
|
||||
.IR color ]
|
||||
.RB [ \-shb
|
||||
.IR color ]
|
||||
.RB [ \-shf
|
||||
.IR color ]
|
||||
.RB [ \-w
|
||||
.IR windowid ]
|
||||
.P
|
||||
|
|
@ -40,6 +50,9 @@ which lists programs in the user's $PATH and runs the result in their $SHELL.
|
|||
.B \-b
|
||||
dmenu appears at the bottom of the screen.
|
||||
.TP
|
||||
.B \-c
|
||||
dmenu appears centered on the screen.
|
||||
.TP
|
||||
.B \-f
|
||||
dmenu grabs the keyboard before reading stdin if not reading from a tty. This
|
||||
is faster, but will lock up X until stdin reaches end\-of\-file.
|
||||
|
|
@ -47,8 +60,11 @@ is faster, but will lock up X until stdin reaches end\-of\-file.
|
|||
.B \-i
|
||||
dmenu matches menu items case insensitively.
|
||||
.TP
|
||||
.BI \-g " columns"
|
||||
dmenu lists items in a grid with the given number of columns.
|
||||
.TP
|
||||
.BI \-l " lines"
|
||||
dmenu lists items vertically, with the given number of lines.
|
||||
dmenu lists items in a grid with the given number of lines.
|
||||
.TP
|
||||
.BI \-m " monitor"
|
||||
dmenu is displayed on the monitor number supplied. Monitor numbers are starting
|
||||
|
|
@ -75,6 +91,18 @@ defines the selected background color.
|
|||
.BI \-sf " color"
|
||||
defines the selected foreground color.
|
||||
.TP
|
||||
.BI \-nhb " color"
|
||||
defines the normal highlight background color.
|
||||
.TP
|
||||
.BI \-nhf " color"
|
||||
defines the normal highlight foreground color.
|
||||
.TP
|
||||
.BI \-shb " color"
|
||||
defines the selected highlight background color.
|
||||
.TP
|
||||
.BI \-shf " color"
|
||||
defines the selected highlight foreground color.
|
||||
.TP
|
||||
.B \-v
|
||||
prints version information to stdout, then exits.
|
||||
.TP
|
||||
|
|
|
|||
340
dmenu.c
340
dmenu.c
|
|
@ -1,6 +1,7 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
#include <ctype.h>
|
||||
#include <locale.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
|
@ -11,6 +12,7 @@
|
|||
#include <X11/Xlib.h>
|
||||
#include <X11/Xatom.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <X11/Xresource.h>
|
||||
#ifdef XINERAMA
|
||||
#include <X11/extensions/Xinerama.h>
|
||||
#endif
|
||||
|
|
@ -22,16 +24,18 @@
|
|||
/* 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 */
|
||||
enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
|
||||
enum { SchemeNorm, SchemeSel, SchemeNormHighlight, SchemeSelHighlight,
|
||||
SchemeOut, SchemeLast }; /* color schemes */
|
||||
|
||||
|
||||
struct item {
|
||||
char *text;
|
||||
struct item *left, *right;
|
||||
int out;
|
||||
double distance;
|
||||
};
|
||||
|
||||
static char text[BUFSIZ] = "";
|
||||
|
|
@ -53,6 +57,21 @@ static XIC xic;
|
|||
static Drw *drw;
|
||||
static Clr *scheme[SchemeLast];
|
||||
|
||||
/* Xresources preferences */
|
||||
enum resource_type {
|
||||
STRING = 0,
|
||||
INTEGER = 1,
|
||||
FLOAT = 2
|
||||
};
|
||||
typedef struct {
|
||||
char *name;
|
||||
enum resource_type type;
|
||||
void *dst;
|
||||
} ResourcePref;
|
||||
|
||||
static void load_xresources(void);
|
||||
static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst);
|
||||
|
||||
#include "config.h"
|
||||
|
||||
static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
|
||||
|
|
@ -84,7 +103,7 @@ calcoffsets(void)
|
|||
int i, n;
|
||||
|
||||
if (lines > 0)
|
||||
n = lines * bh;
|
||||
n = lines * columns * bh;
|
||||
else
|
||||
n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">"));
|
||||
/* calculate which items will begin the next page and previous page */
|
||||
|
|
@ -96,6 +115,15 @@ calcoffsets(void)
|
|||
break;
|
||||
}
|
||||
|
||||
static int
|
||||
max_textw(void)
|
||||
{
|
||||
int len = 0;
|
||||
for (struct item *item = items; item && item->text; item++)
|
||||
len = MAX(TEXTW(item->text), len);
|
||||
return len;
|
||||
}
|
||||
|
||||
static void
|
||||
cleanup(void)
|
||||
{
|
||||
|
|
@ -130,9 +158,49 @@ cistrstr(const char *h, const char *n)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
drawhighlights(struct item *item, int x, int y, int maxw)
|
||||
{
|
||||
int i, indent;
|
||||
char *highlight;
|
||||
char c;
|
||||
|
||||
if (!(strlen(item->text) && strlen(text)))
|
||||
return;
|
||||
|
||||
drw_setscheme(drw, scheme[item == sel
|
||||
? SchemeSelHighlight
|
||||
: SchemeNormHighlight]);
|
||||
for (i = 0, highlight = item->text; *highlight && text[i];) {
|
||||
if (*highlight == text[i]) {
|
||||
/* get indentation */
|
||||
c = *highlight;
|
||||
*highlight = '\0';
|
||||
indent = TEXTW(item->text);
|
||||
*highlight = c;
|
||||
|
||||
/* highlight character */
|
||||
c = highlight[1];
|
||||
highlight[1] = '\0';
|
||||
drw_text(
|
||||
drw,
|
||||
x + indent - (lrpad / 2),
|
||||
y,
|
||||
MIN(maxw - indent, TEXTW(highlight) - lrpad),
|
||||
bh, 0, highlight, 0
|
||||
);
|
||||
highlight[1] = c;
|
||||
i++;
|
||||
}
|
||||
highlight++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
drawitem(struct item *item, int x, int y, int w)
|
||||
{
|
||||
int r;
|
||||
if (item == sel)
|
||||
drw_setscheme(drw, scheme[SchemeSel]);
|
||||
else if (item->out)
|
||||
|
|
@ -140,7 +208,9 @@ drawitem(struct item *item, int x, int y, int w)
|
|||
else
|
||||
drw_setscheme(drw, scheme[SchemeNorm]);
|
||||
|
||||
return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
|
||||
r = drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
|
||||
drawhighlights(item, x, y, w);
|
||||
return r;
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -169,9 +239,15 @@ drawmenu(void)
|
|||
}
|
||||
|
||||
if (lines > 0) {
|
||||
/* draw vertical list */
|
||||
for (item = curr; item != next; item = item->right)
|
||||
drawitem(item, x, y += bh, mw - x);
|
||||
/* draw grid */
|
||||
int i = 0;
|
||||
for (item = curr; item != next; item = item->right, i++)
|
||||
drawitem(
|
||||
item,
|
||||
x + ((i / lines) * ((mw - x) / columns)),
|
||||
y + (((i % lines) + 1) * bh),
|
||||
(mw - x) / columns
|
||||
);
|
||||
} else if (matches) {
|
||||
/* draw horizontal list */
|
||||
x += inputw;
|
||||
|
|
@ -227,9 +303,94 @@ grabkeyboard(void)
|
|||
die("cannot grab keyboard");
|
||||
}
|
||||
|
||||
int
|
||||
compare_distance(const void *a, const void *b)
|
||||
{
|
||||
struct item *da = *(struct item **) a;
|
||||
struct item *db = *(struct item **) b;
|
||||
|
||||
if (!db)
|
||||
return 1;
|
||||
if (!da)
|
||||
return -1;
|
||||
|
||||
return da->distance == db->distance ? 0 : da->distance < db->distance ? -1 : 1;
|
||||
}
|
||||
|
||||
void
|
||||
fuzzymatch(void)
|
||||
{
|
||||
/* bang - we have so much memory */
|
||||
struct item *it;
|
||||
struct item **fuzzymatches = NULL;
|
||||
char c;
|
||||
int number_of_matches = 0, i, pidx, sidx, eidx;
|
||||
int text_len = strlen(text), itext_len;
|
||||
|
||||
matches = matchend = NULL;
|
||||
|
||||
/* walk through all items */
|
||||
for (it = items; it && it->text; it++) {
|
||||
if (text_len) {
|
||||
itext_len = strlen(it->text);
|
||||
pidx = 0; /* pointer */
|
||||
sidx = eidx = -1; /* start of match, end of match */
|
||||
/* walk through item text */
|
||||
for (i = 0; i < itext_len && (c = it->text[i]); i++) {
|
||||
/* fuzzy match pattern */
|
||||
if (!fstrncmp(&text[pidx], &c, 1)) {
|
||||
if(sidx == -1)
|
||||
sidx = i;
|
||||
pidx++;
|
||||
if (pidx == text_len) {
|
||||
eidx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* build list of matches */
|
||||
if (eidx != -1) {
|
||||
/* compute distance */
|
||||
/* add penalty if match starts late (log(sidx+2))
|
||||
* add penalty for long a match without many matching characters */
|
||||
it->distance = log(sidx + 2) + (double)(eidx - sidx - text_len);
|
||||
/* fprintf(stderr, "distance %s %f\n", it->text, it->distance); */
|
||||
appenditem(it, &matches, &matchend);
|
||||
number_of_matches++;
|
||||
}
|
||||
} else {
|
||||
appenditem(it, &matches, &matchend);
|
||||
}
|
||||
}
|
||||
|
||||
if (number_of_matches) {
|
||||
/* initialize array with matches */
|
||||
if (!(fuzzymatches = realloc(fuzzymatches, number_of_matches * sizeof(struct item*))))
|
||||
die("cannot realloc %u bytes:", number_of_matches * sizeof(struct item*));
|
||||
for (i = 0, it = matches; it && i < number_of_matches; i++, it = it->right) {
|
||||
fuzzymatches[i] = it;
|
||||
}
|
||||
/* sort matches according to distance */
|
||||
qsort(fuzzymatches, number_of_matches, sizeof(struct item*), compare_distance);
|
||||
/* rebuild list of matches */
|
||||
matches = matchend = NULL;
|
||||
for (i = 0, it = fuzzymatches[i]; i < number_of_matches && it && \
|
||||
it->text; i++, it = fuzzymatches[i]) {
|
||||
appenditem(it, &matches, &matchend);
|
||||
}
|
||||
free(fuzzymatches);
|
||||
}
|
||||
curr = sel = matches;
|
||||
calcoffsets();
|
||||
}
|
||||
|
||||
static void
|
||||
match(void)
|
||||
{
|
||||
if (fuzzy) {
|
||||
fuzzymatch();
|
||||
return;
|
||||
}
|
||||
static char **tokv = NULL;
|
||||
static int tokn = 0;
|
||||
|
||||
|
|
@ -328,6 +489,8 @@ keypress(XKeyEvent *ev)
|
|||
int len;
|
||||
KeySym ksym = NoSymbol;
|
||||
Status status;
|
||||
int i, offscreen = 0;
|
||||
struct item *tmpsel;
|
||||
|
||||
len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
|
||||
switch (status) {
|
||||
|
|
@ -414,7 +577,7 @@ keypress(XKeyEvent *ev)
|
|||
|
||||
switch(ksym) {
|
||||
default:
|
||||
insert:
|
||||
insert:
|
||||
if (!iscntrl((unsigned char)*buf))
|
||||
insert(buf, len);
|
||||
break;
|
||||
|
|
@ -459,6 +622,27 @@ insert:
|
|||
calcoffsets();
|
||||
break;
|
||||
case XK_Left:
|
||||
if (columns > 1) {
|
||||
if (!sel)
|
||||
return;
|
||||
tmpsel = sel;
|
||||
for (i = 0; i < lines; i++) {
|
||||
if (!tmpsel->left || tmpsel->left->right != tmpsel) {
|
||||
if (offscreen)
|
||||
break;
|
||||
return;
|
||||
}
|
||||
if (tmpsel == curr)
|
||||
offscreen = 1;
|
||||
tmpsel = tmpsel->left;
|
||||
}
|
||||
sel = tmpsel;
|
||||
if (offscreen) {
|
||||
curr = prev;
|
||||
calcoffsets();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case XK_KP_Left:
|
||||
if (cursor > 0 && (!sel || !sel->left || lines > 0)) {
|
||||
cursor = nextrune(-1);
|
||||
|
|
@ -499,6 +683,27 @@ insert:
|
|||
sel->out = 1;
|
||||
break;
|
||||
case XK_Right:
|
||||
if (columns > 1) {
|
||||
if (!sel)
|
||||
return;
|
||||
tmpsel = sel;
|
||||
for (i = 0; i < lines; i++) {
|
||||
if (!tmpsel->right || tmpsel->right->left != tmpsel) {
|
||||
if (offscreen)
|
||||
break;
|
||||
return;
|
||||
}
|
||||
tmpsel = tmpsel->right;
|
||||
if (tmpsel == next)
|
||||
offscreen = 1;
|
||||
}
|
||||
sel = tmpsel;
|
||||
if (offscreen) {
|
||||
curr = next;
|
||||
calcoffsets();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case XK_KP_Right:
|
||||
if (text[cursor] != '\0') {
|
||||
cursor = nextrune(+1);
|
||||
|
|
@ -550,11 +755,11 @@ static void
|
|||
readstdin(void)
|
||||
{
|
||||
char *line = NULL;
|
||||
size_t i, junk, itemsiz = 0;
|
||||
size_t i, itemsiz = 0, linesiz = 0;
|
||||
ssize_t len;
|
||||
|
||||
/* read each line from stdin and add it to the item list */
|
||||
for (i = 0; (len = getline(&line, &junk, stdin)) != -1; i++) {
|
||||
for (i = 0; (len = getline(&line, &linesiz, stdin)) != -1; i++) {
|
||||
if (i + 1 >= itemsiz) {
|
||||
itemsiz += 256;
|
||||
if (!(items = realloc(items, itemsiz * sizeof(*items))))
|
||||
|
|
@ -562,9 +767,10 @@ readstdin(void)
|
|||
}
|
||||
if (line[len - 1] == '\n')
|
||||
line[len - 1] = '\0';
|
||||
items[i].text = line;
|
||||
if (!(items[i].text = strdup(line)))
|
||||
die("strdup:");
|
||||
|
||||
items[i].out = 0;
|
||||
line = NULL; /* next call of getline() allocates a new line */
|
||||
}
|
||||
free(line);
|
||||
if (items)
|
||||
|
|
@ -572,6 +778,54 @@ readstdin(void)
|
|||
lines = MIN(lines, i);
|
||||
}
|
||||
|
||||
void
|
||||
resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
|
||||
{
|
||||
char *sdst = NULL;
|
||||
int *idst = NULL;
|
||||
float *fdst = NULL;
|
||||
sdst = dst;
|
||||
idst = dst;
|
||||
fdst = dst;
|
||||
char fullname[256];
|
||||
char *type;
|
||||
XrmValue ret;
|
||||
snprintf(fullname, sizeof(fullname), "%s.%s", "dmenu", name);
|
||||
fullname[sizeof(fullname) - 1] = '\0';
|
||||
XrmGetResource(db, fullname, "*", &type, &ret);
|
||||
if (!(ret.addr == NULL || strncmp("String", type, 64)))
|
||||
{
|
||||
switch (rtype) {
|
||||
case STRING:
|
||||
strcpy(sdst, ret.addr);
|
||||
break;
|
||||
case INTEGER:
|
||||
*idst = strtoul(ret.addr, NULL, 10);
|
||||
break;
|
||||
case FLOAT:
|
||||
*fdst = strtof(ret.addr, NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
load_xresources(void)
|
||||
{
|
||||
Display *display;
|
||||
char *resm;
|
||||
XrmDatabase db;
|
||||
ResourcePref *p;
|
||||
display = XOpenDisplay(NULL);
|
||||
resm = XResourceManagerString(display);
|
||||
if (!resm)
|
||||
return;
|
||||
db = XrmGetStringDatabase(resm);
|
||||
for (p = resources; p < resources + LENGTH(resources); p++)
|
||||
resource_load(db, p->name, p->type, p->dst);
|
||||
XCloseDisplay(display);
|
||||
}
|
||||
|
||||
static void
|
||||
run(void)
|
||||
{
|
||||
|
|
@ -636,6 +890,7 @@ setup(void)
|
|||
bh = drw->fonts->h + 2;
|
||||
lines = MAX(lines, 0);
|
||||
mh = (lines + 1) * bh;
|
||||
promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
|
||||
#ifdef XINERAMA
|
||||
i = 0;
|
||||
if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) {
|
||||
|
|
@ -662,9 +917,16 @@ setup(void)
|
|||
if (INTERSECT(x, y, 1, 1, info[i]) != 0)
|
||||
break;
|
||||
|
||||
x = info[i].x_org;
|
||||
y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
|
||||
mw = info[i].width;
|
||||
if (centered) {
|
||||
mw = MIN(MAX(max_textw() + promptw, min_width), info[i].width);
|
||||
x = info[i].x_org + ((info[i].width - mw) / 2);
|
||||
y = info[i].y_org + ((info[i].height - mh) / 2);
|
||||
} else {
|
||||
x = info[i].x_org;
|
||||
y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
|
||||
mw = info[i].width;
|
||||
}
|
||||
|
||||
XFree(info);
|
||||
} else
|
||||
#endif
|
||||
|
|
@ -672,9 +934,16 @@ setup(void)
|
|||
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;
|
||||
|
||||
if (centered) {
|
||||
mw = MIN(MAX(max_textw() + promptw, min_width), wa.width);
|
||||
x = (wa.width - mw) / 2;
|
||||
y = (wa.height - mh) / 2;
|
||||
} else {
|
||||
x = 0;
|
||||
y = topbar ? 0 : wa.height - mh;
|
||||
mw = wa.width;
|
||||
}
|
||||
}
|
||||
promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
|
||||
inputw = mw / 3; /* input width: ~33% of monitor width */
|
||||
|
|
@ -684,9 +953,11 @@ setup(void)
|
|||
swa.override_redirect = True;
|
||||
swa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
|
||||
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
|
||||
win = XCreateWindow(dpy, parentwin, x, y, mw, mh, 0,
|
||||
win = XCreateWindow(dpy, root, x, y, mw, mh, borderpx,
|
||||
CopyFromParent, CopyFromParent, CopyFromParent,
|
||||
CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
|
||||
if (borderpx)
|
||||
XSetWindowBorder(dpy, win, scheme[SchemeSel][ColBg].pixel);
|
||||
XSetClassHint(dpy, win, &ch);
|
||||
|
||||
|
||||
|
|
@ -699,6 +970,7 @@ setup(void)
|
|||
|
||||
XMapRaised(dpy, win);
|
||||
if (embed) {
|
||||
XReparentWindow(dpy, win, parentwin, x, y);
|
||||
XSelectInput(dpy, parentwin, FocusChangeMask | SubstructureNotifyMask);
|
||||
if (XQueryTree(dpy, parentwin, &dw, &w, &dws, &du) && dws) {
|
||||
for (i = 0; i < du && dws[i] != win; ++i)
|
||||
|
|
@ -715,7 +987,8 @@ static void
|
|||
usage(void)
|
||||
{
|
||||
die("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
|
||||
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]");
|
||||
" [-nb color] [-nf color] [-sb color] [-sf color]\n"
|
||||
" [-nhb color] [-nhf color] [-shb color] [-shf color] [-w windowid]\n");
|
||||
}
|
||||
|
||||
int
|
||||
|
|
@ -724,6 +997,9 @@ main(int argc, char *argv[])
|
|||
XWindowAttributes wa;
|
||||
int i, fast = 0;
|
||||
|
||||
XrmInitialize();
|
||||
load_xresources();
|
||||
|
||||
for (i = 1; i < argc; i++)
|
||||
/* these options take no arguments */
|
||||
if (!strcmp(argv[i], "-v")) { /* prints version information */
|
||||
|
|
@ -733,15 +1009,25 @@ main(int argc, char *argv[])
|
|||
topbar = 0;
|
||||
else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */
|
||||
fast = 1;
|
||||
else if (!strcmp(argv[i], "-c")) /* centers dmenu on screen */
|
||||
centered = 1;
|
||||
else if (!strcmp(argv[i], "-cw")) /* centered min width on screen */
|
||||
min_width = atoi(argv[++i]);
|
||||
else if (!strcmp(argv[i], "-F")) /* grabs keyboard before reading stdin */
|
||||
fuzzy = 0;
|
||||
else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
|
||||
fstrncmp = strncasecmp;
|
||||
fstrstr = cistrstr;
|
||||
} else if (i + 1 == argc)
|
||||
usage();
|
||||
/* these options take one argument */
|
||||
else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */
|
||||
else if (!strcmp(argv[i], "-g")) { /* number of columns in grid */
|
||||
columns = atoi(argv[++i]);
|
||||
if (lines == 0) lines = 1;
|
||||
} else if (!strcmp(argv[i], "-l")) { /* number of lines in grid */
|
||||
lines = atoi(argv[++i]);
|
||||
else if (!strcmp(argv[i], "-m"))
|
||||
if (columns == 0) columns = 1;
|
||||
} else if (!strcmp(argv[i], "-m"))
|
||||
mon = atoi(argv[++i]);
|
||||
else if (!strcmp(argv[i], "-p")) /* adds prompt to left of input field */
|
||||
prompt = argv[++i];
|
||||
|
|
@ -755,8 +1041,18 @@ main(int argc, char *argv[])
|
|||
colors[SchemeSel][ColBg] = argv[++i];
|
||||
else if (!strcmp(argv[i], "-sf")) /* selected foreground color */
|
||||
colors[SchemeSel][ColFg] = argv[++i];
|
||||
else if (!strcmp(argv[i], "-nhb")) /* normal hi background color */
|
||||
colors[SchemeNormHighlight][ColBg] = argv[++i];
|
||||
else if (!strcmp(argv[i], "-nhf")) /* normal hi foreground color */
|
||||
colors[SchemeNormHighlight][ColFg] = argv[++i];
|
||||
else if (!strcmp(argv[i], "-shb")) /* selected hi background color */
|
||||
colors[SchemeSelHighlight][ColBg] = argv[++i];
|
||||
else if (!strcmp(argv[i], "-shf")) /* selected hi foreground color */
|
||||
colors[SchemeSelHighlight][ColFg] = argv[++i];
|
||||
else if (!strcmp(argv[i], "-w")) /* embedding window id */
|
||||
embed = argv[++i];
|
||||
else if (!strcmp(argv[i], "-bw"))
|
||||
borderpx = atoi(argv[++i]); /* border width */
|
||||
else
|
||||
usage();
|
||||
|
||||
|
|
|
|||
25
drw.c
25
drw.c
|
|
@ -195,7 +195,7 @@ drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
|
|||
/* Wrapper to create color schemes. The caller has to call free(3) on the
|
||||
* returned color scheme when done using it. */
|
||||
Clr *
|
||||
drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
|
||||
drw_scm_create(Drw *drw, char *clrnames[], size_t clrcount)
|
||||
{
|
||||
size_t i;
|
||||
Clr *ret;
|
||||
|
|
@ -238,8 +238,8 @@ drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int
|
|||
int
|
||||
drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
|
||||
{
|
||||
int i, ty, ellipsis_x = 0;
|
||||
unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len;
|
||||
int ty, ellipsis_x = 0;
|
||||
unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len, hash, h0, h1;
|
||||
XftDraw *d = NULL;
|
||||
Fnt *usedfont, *curfont, *nextfont;
|
||||
int utf8strlen, utf8charlen, render = x || y || w || h;
|
||||
|
|
@ -251,9 +251,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
|||
XftResult result;
|
||||
int charexists = 0, overflow = 0;
|
||||
/* keep track of a couple codepoints for which we have no match. */
|
||||
enum { nomatches_len = 64 };
|
||||
static struct { long codepoint[nomatches_len]; unsigned int idx; } nomatches;
|
||||
static unsigned int ellipsis_width = 0;
|
||||
static unsigned int nomatches[128], ellipsis_width;
|
||||
|
||||
if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts)
|
||||
return 0;
|
||||
|
|
@ -338,11 +336,14 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
|||
* character must be drawn. */
|
||||
charexists = 1;
|
||||
|
||||
for (i = 0; i < nomatches_len; ++i) {
|
||||
/* avoid calling XftFontMatch if we know we won't find a match */
|
||||
if (utf8codepoint == nomatches.codepoint[i])
|
||||
goto no_match;
|
||||
}
|
||||
hash = (unsigned int)utf8codepoint;
|
||||
hash = ((hash >> 16) ^ hash) * 0x21F0AAAD;
|
||||
hash = ((hash >> 15) ^ hash) * 0xD35A2D97;
|
||||
h0 = ((hash >> 15) ^ hash) % LENGTH(nomatches);
|
||||
h1 = (hash >> 17) % LENGTH(nomatches);
|
||||
/* avoid expensive XftFontMatch call when we know we won't find a match */
|
||||
if (nomatches[h0] == utf8codepoint || nomatches[h1] == utf8codepoint)
|
||||
goto no_match;
|
||||
|
||||
fccharset = FcCharSetCreate();
|
||||
FcCharSetAddChar(fccharset, utf8codepoint);
|
||||
|
|
@ -371,7 +372,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
|||
curfont->next = usedfont;
|
||||
} else {
|
||||
xfont_free(usedfont);
|
||||
nomatches.codepoint[++nomatches.idx % nomatches_len] = utf8codepoint;
|
||||
nomatches[nomatches[h0] ? h1 : h0] = utf8codepoint;
|
||||
no_match:
|
||||
usedfont = drw->fonts;
|
||||
}
|
||||
|
|
|
|||
2
drw.h
2
drw.h
|
|
@ -40,7 +40,7 @@ void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned in
|
|||
|
||||
/* Colorscheme abstraction */
|
||||
void drw_clr_create(Drw *drw, Clr *dest, const char *clrname);
|
||||
Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount);
|
||||
Clr *drw_scm_create(Drw *drw, char *clrnames[], size_t clrcount);
|
||||
|
||||
/* Cursor abstraction */
|
||||
Cur *drw_cur_create(Drw *drw, int shape);
|
||||
|
|
|
|||
Loading…
Reference in New Issue