aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/menu/menu.c
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-06-08 14:58:18 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-06-08 14:58:18 -0700
commitac78d59c80b627a5332ac64a7d4359389c4251fa (patch)
treeeb7a1c77a688a703442d4bcf604e97aba1566105 /sys/cmd/menu/menu.c
parent75eece6dd52aa49705bf59a6f5b6197b7a14f5db (diff)
small fixes
Diffstat (limited to 'sys/cmd/menu/menu.c')
-rw-r--r--sys/cmd/menu/menu.c160
1 files changed, 73 insertions, 87 deletions
diff --git a/sys/cmd/menu/menu.c b/sys/cmd/menu/menu.c
index 666e531..f0d1276 100644
--- a/sys/cmd/menu/menu.c
+++ b/sys/cmd/menu/menu.c
@@ -1,36 +1,4 @@
-/* See LICENSE file for copyright and license details. */
-#include <u.h>
-#include <libn.h>
-#include <time.h>
-#include <locale.h>
-
-#include <X11/Xlib.h>
-#include <X11/Xatom.h>
-#include <X11/Xutil.h>
-#include <X11/extensions/Xinerama.h>
-#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 TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
-
-/* enums */
-enum {
- SchemeNorm,
- SchemeSel,
- SchemeOut,
- SchemeLast
-}; /* color schemes */
-
-struct item {
- char *text;
- struct item *left, *right;
- int out;
-};
+#include "menu.h"
static char text[BUFSIZ] = "";
static char *embed;
@@ -38,7 +6,7 @@ static int bh, mw, mh;
static int inputw = 0, promptw;
static int lrpad; /* sum of left and right padding */
static size_t cursor;
-static struct item *items = NULL;
+static struct item *items = nil;
static struct item *matches, *matchend;
static struct item *prev, *curr, *next, *sel;
static int mon = -1, screen;
@@ -56,7 +24,8 @@ static Clr *scheme[SchemeLast];
static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
static char *(*fstrstr)(const char *, const char *) = strstr;
-static void
+static
+void
appenditem(struct item *item, struct item **list, struct item **last)
{
if (*last)
@@ -65,11 +34,12 @@ appenditem(struct item *item, struct item **list, struct item **last)
*list = item;
item->left = *last;
- item->right = NULL;
+ item->right = nil;
*last = item;
}
-static void
+static
+void
calcoffsets(void)
{
int i, n;
@@ -87,7 +57,8 @@ calcoffsets(void)
break;
}
-static void
+static
+void
cleanup(void)
{
size_t i;
@@ -100,7 +71,8 @@ cleanup(void)
XCloseDisplay(dpy);
}
-static char *
+static
+char *
cistrstr(const char *s, const char *sub)
{
size_t len;
@@ -108,10 +80,11 @@ cistrstr(const char *s, const char *sub)
for (len = strlen(sub); *s; s++)
if (!strncasecmp(s, sub, len))
return (char *)s;
- return NULL;
+ return nil;
}
-static int
+static
+int
drawitem(struct item *item, int x, int y, int w)
{
if (item == sel)
@@ -124,10 +97,11 @@ drawitem(struct item *item, int x, int y, int w)
return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
}
-static void
+static
+void
drawmenu(void)
{
- unsigned int curpos;
+ uint curpos;
struct item *item;
int x = 0, y = 0, w;
@@ -173,7 +147,8 @@ drawmenu(void)
drw_map(drw, win, 0, 0, mw, mh);
}
-static void
+static
+void
grabfocus(void)
{
struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
@@ -185,12 +160,13 @@ grabfocus(void)
if (focuswin == win)
return;
XSetInputFocus(dpy, win, RevertToParent, CurrentTime);
- nanosleep(&ts, NULL);
+ nanosleep(&ts, nil);
}
- die("cannot grab focus");
+ fatal("cannot grab focus");
}
-static void
+static
+void
grabkeyboard(void)
{
struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
@@ -203,15 +179,16 @@ grabkeyboard(void)
if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, GrabModeAsync,
GrabModeAsync, CurrentTime) == GrabSuccess)
return;
- nanosleep(&ts, NULL);
+ nanosleep(&ts, nil);
}
- die("cannot grab keyboard");
+ fatal("cannot grab keyboard");
}
-static void
+static
+void
match(void)
{
- static char **tokv = NULL;
+ static char **tokv = nil;
static int tokn = 0;
char buf[sizeof text], *s;
@@ -221,12 +198,12 @@ match(void)
strcpy(buf, text);
/* separate input text into tokens to be matched individually */
- for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " "))
+ for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(nil, " "))
if (++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
- die("cannot realloc %u bytes:", tokn * sizeof *tokv);
+ fatal("cannot realloc %u bytes:", tokn * sizeof *tokv);
len = tokc ? strlen(tokv[0]) : 0;
- matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
+ matches = lprefix = lsubstr = matchend = prefixend = substrend = nil;
textsize = strlen(text) + 1;
for (item = items; item && item->text; item++) {
for (i = 0; i < tokc; i++)
@@ -262,7 +239,8 @@ match(void)
calcoffsets();
}
-static void
+static
+void
insert(const char *str, ssize_t n)
{
if (strlen(text) + n > sizeof text - 1)
@@ -275,7 +253,8 @@ insert(const char *str, ssize_t n)
match();
}
-static size_t
+static
+size_t
nextrune(int inc)
{
ssize_t n;
@@ -286,7 +265,8 @@ nextrune(int inc)
return n;
}
-static void
+static
+void
movewordedge(int dir)
{
if (dir < 0) { /* move cursor to the start of the word*/
@@ -302,7 +282,8 @@ movewordedge(int dir)
}
}
-static void
+static
+void
keypress(XKeyEvent *ev)
{
char buf[32];
@@ -344,13 +325,13 @@ keypress(XKeyEvent *ev)
match();
break;
case XK_u: /* delete left */
- insert(NULL, 0 - cursor);
+ insert(nil, 0 - cursor);
break;
case XK_w: /* delete word */
while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)]))
- insert(NULL, nextrune(-1) - cursor);
+ insert(nil, nextrune(-1) - cursor);
while (cursor > 0 && !strchr(worddelimiters, text[nextrune(-1)]))
- insert(NULL, nextrune(-1) - cursor);
+ insert(nil, nextrune(-1) - cursor);
break;
case XK_y: /* paste selection */
case XK_Y:
@@ -405,7 +386,7 @@ insert:
case XK_BackSpace:
if (cursor == 0)
return;
- insert(NULL, nextrune(-1) - cursor);
+ insert(nil, nextrune(-1) - cursor);
break;
case XK_End:
if (text[cursor] != '\0') {
@@ -498,7 +479,8 @@ draw:
drawmenu();
}
-static void
+static
+void
paste(void)
{
char *p, *q;
@@ -516,36 +498,38 @@ paste(void)
drawmenu();
}
-static void
+static
+void
readstdin(void)
{
char buf[sizeof text], *p;
size_t i, imax = 0, size = 0;
- unsigned int tmpmax = 0;
+ uint tmpmax = 0;
/* read each line from stdin and add it to the item list */
for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
if (i + 1 >= size / sizeof *items)
if (!(items = realloc(items, (size += BUFSIZ))))
- die("cannot realloc %u bytes:", size);
+ fatal("cannot realloc %u bytes:", size);
if ((p = strchr(buf, '\n')))
*p = '\0';
if (!(items[i].text = strdup(buf)))
- die("cannot strdup %u bytes:", strlen(buf) + 1);
+ fatal("cannot strdup %u bytes:", strlen(buf) + 1);
items[i].out = 0;
- drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
+ drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, nil);
if (tmpmax > inputw) {
inputw = tmpmax;
imax = i;
}
}
if (items)
- items[i].text = NULL;
+ items[i].text = nil;
inputw = items ? TEXTW(items[imax].text) : 0;
lines = MIN(lines, i);
}
-static void
+static
+void
run(void)
{
XEvent ev;
@@ -583,16 +567,17 @@ run(void)
}
}
-static void
+static
+void
setup(void)
{
int x, y, i, j;
- unsigned int du;
+ uint du;
XSetWindowAttributes swa;
XIM xim;
Window w, dw, *dws;
XWindowAttributes wa;
- XClassHint ch = {"dmenu", "dmenu"};
+ XClassHint ch = {"menu", "menu"};
XineramaScreenInfo *info;
Window pw;
int a, di, n, area = 0;
@@ -640,7 +625,7 @@ setup(void)
} else
{
if (!XGetWindowAttributes(dpy, parentwin, &wa))
- die("could not get embedding window attributes: 0x%lx",
+ fatal("could not get embedding window attributes: 0x%lx",
parentwin);
x = 0;
y = topbar ? 0 : wa.height - mh;
@@ -661,11 +646,11 @@ setup(void)
/* input methods */
- if ((xim = XOpenIM(dpy, NULL, NULL, NULL)) == NULL)
- die("XOpenIM failed: could not open input device");
+ if ((xim = XOpenIM(dpy, nil, nil, nil)) == nil)
+ fatal("XOpenIM failed: could not open input device");
xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
- XNClientWindow, win, XNFocusWindow, win, NULL);
+ XNClientWindow, win, XNFocusWindow, win, nil);
XMapRaised(dpy, win);
if (embed) {
@@ -681,10 +666,11 @@ setup(void)
drawmenu();
}
-static void
+static
+void
usage(void)
{
- fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
+ fputs("usage: menu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
exit(1);
}
@@ -698,7 +684,7 @@ main(int argc, char *argv[])
for (i = 1; i < argc; i++)
/* these options take no arguments */
if (!strcmp(argv[i], "-v")) { /* prints version information */
- puts("dmenu-"VERSION);
+ puts("menu-"VERSION);
exit(0);
} else if (!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */
topbar = 0;
@@ -733,23 +719,23 @@ main(int argc, char *argv[])
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
fputs("warning: no locale support\n", stderr);
- if (!(dpy = XOpenDisplay(NULL)))
- die("cannot open display");
+ if (!(dpy = XOpenDisplay(nil)))
+ fatal("cannot open display");
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
- if (!embed || !(parentwin = strtol(embed, NULL, 0)))
+ if (!embed || !(parentwin = strtol(embed, nil, 0)))
parentwin = root;
if (!XGetWindowAttributes(dpy, parentwin, &wa))
- die("could not get embedding window attributes: 0x%lx",
+ fatal("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, arrlen(fonts)))
- die("no fonts could be loaded.");
+ fatal("no fonts could be loaded.");
lrpad = drw->fonts->h;
#ifdef __OpenBSD__
- if (pledge("stdio rpath", NULL) == -1)
- die("pledge");
+ if (pledge("stdio rpath", nil) == -1)
+ fatal("pledge");
#endif
if (fast && !isatty(0)) {