[wiki] [sites] Added json patch for dmenu || C.J.Wagenius

From: <git_AT_suckless.org>
Date: Fri, 20 Sep 2019 22:14:11 +0200

commit 3a181b28b9df0db141025b2228af81f7d4accd24
Author: C.J.Wagenius <cjw_AT_voidptr.se>
Date: Fri Sep 20 22:14:01 2019 +0200

    Added json patch for dmenu

diff --git a/tools.suckless.org/dmenu/patches/json/dmenu-json-4.9.diff b/tools.suckless.org/dmenu/patches/json/dmenu-json-4.9.diff
new file mode 100644
index 00000000..94db0d52
--- /dev/null
+++ b/tools.suckless.org/dmenu/patches/json/dmenu-json-4.9.diff
_AT_@ -0,0 +1,225 @@
+diff --git a/config.mk b/config.mk
+index 0929b4a..6cb79e2 100644
+--- a/config.mk
++++ b/config.mk
+_AT_@ -18,13 +18,17 @@ FREETYPEINC = /usr/include/freetype2
+ # OpenBSD (uncomment)
+ #FREETYPEINC = $(X11INC)/freetype2
+
++# jansson
++JANSSONINC = `pkg-config --cflags jansson`
++JANSSONLIBS = `pkg-config --libs jansson`
++
+ # includes and libs
+-INCS = -I$(X11INC) -I$(FREETYPEINC)
+-LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS)
++INCS = -I$(X11INC) -I$(FREETYPEINC) $(JANSSONINC)
++LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) $(JANSSONLIBS)
+
+ # flags
+ CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" $(XINERAMAFLAGS)
+-CFLAGS = -std=c99 -pedantic -Wall -Os $(INCS) $(CPPFLAGS)
++CFLAGS = -std=c99 -pedantic -Wall -Os $(INCS) $(CPPFLAGS)
+ LDFLAGS = $(LIBS)
+
+ # compiler and linker
+diff --git a/dmenu.c b/dmenu.c
+index 65f25ce..7b4cd66 100644
+--- a/dmenu.c
++++ b/dmenu.c
+_AT_@ -15,6 +15,7 @@
+ #include <X11/extensions/Xinerama.h>
+ #endif
+ #include <X11/Xft/Xft.h>
++#include <jansson.h>
+
+ #include "drw.h"
+ #include "util.h"
+_AT_@ -32,6 +33,7 @@ struct item {
+ char *text;
+ struct item *left, *right;
+ int out;
++ json_t *json;
+ };
+
+ static char text[BUFSIZ] = "";
+_AT_@ -40,6 +42,8 @@ static int bh, mw, mh;
+ static int inputw = 0, promptw;
+ static int lrpad; /* sum of left and right padding */
+ static size_t cursor;
++static size_t items_sz = 0;
++static size_t items_ln = 0;
+ static struct item *items = NULL;
+ static struct item *matches, *matchend;
+ static struct item *prev, *curr, *next, *sel;
+_AT_@ -58,6 +62,18 @@ static Clr *scheme[SchemeLast];
+ static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
+ static char *(*fstrstr)(const char *, const char *) = strstr;
+
++static void listjson(json_t *obj);
++static json_t *json = NULL;
++
++static struct item *
++itemnew(void)
++{
++ if (items_ln + 1 >= (items_sz / sizeof *items))
++ if (!(items = realloc(items, (items_sz += BUFSIZ))))
++ die("cannot realloc %u bytes:", items_sz);
++ return &items[items_ln++];
++}
++
+ static void
+ appenditem(struct item *item, struct item **list, struct item **last)
+ {
+_AT_@ -221,6 +237,8 @@ match(void)
+ size_t len, textsize;
+ struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
+
++ if (json)
++ fstrstr = strcasestr;
+ strcpy(buf, text);
+ /* separate input text into tokens to be matched individually */
+ for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " "))
+_AT_@ -464,7 +482,19 @@ insert:
+ break;
+ case XK_Return:
+ case XK_KP_Enter:
+- puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
++ if (sel && sel->json) {
++ if (json_is_object(sel->json)) {
++ listjson(sel->json);
++ text[0] = '++ match();
++ drawmenu();
++ break;
++ } else {
++ puts(json_string_value(sel->json));
++ }
++ } else {
++ puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
++ }
+ if (!(ev->state & ControlMask)) {
+ cleanup();
+ exit(0);
+_AT_@ -518,33 +548,72 @@ paste(void)
+ drawmenu();
+ }
+
++static void
++readjson(const char *path)
++{
++ json_error_t jerr;
++
++ if (!(json = json_load_file(path, 0, &jerr)))
++ die("%s _AT_ line: %i - %s", jerr.text, jerr.line, path);
++}
++
++static void
++listjson(json_t *obj)
++{
++ void *iter;
++ unsigned tmpmax = 0;
++ struct item *item;
++ struct item *longest = items;
++
++
++ items_ln = 0;
++ iter = json_object_iter(obj);
++ while (iter) {
++ item = itemnew();
++ item->text = (char*) json_object_iter_key(iter);
++ item->json = json_object_iter_value(iter);
++ item->out = 0;
++ drw_font_getexts(drw->fonts, item->text, strlen(item->text),
++ &tmpmax, NULL);
++ if (tmpmax > inputw) {
++ inputw = tmpmax;
++ longest = item;
++ }
++ iter = json_object_iter_next(obj, iter);
++ }
++ if (items)
++ items[items_ln].text = NULL;
++ inputw = items ? TEXTW(longest->text) : 0;
++ lines = MIN(lines, items_ln - 1);
++}
++
+ static void
+ readstdin(void)
+ {
+ char buf[sizeof text], *p;
+- size_t i, imax = 0, size = 0;
++ size_t i;
+ unsigned int tmpmax = 0;
++ struct item *item, *longest;
+
+ /* 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);
++ item = itemnew();
+ if ((p = strchr(buf, '
')))
+ *p = '+- if (!(items[i].text = strdup(buf)))
++ if (!(item->text = strdup(buf)))
+ die("cannot strdup %u bytes:", strlen(buf) + 1);
+- items[i].out = 0;
++ item->json = NULL;
++ item->out = 0;
+ drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
+ if (tmpmax > inputw) {
+ inputw = tmpmax;
+- imax = i;
++ longest = item;
+ }
+ }
+ if (items)
+- items[i].text = NULL;
+- inputw = items ? TEXTW(items[imax].text) : 0;
+- lines = MIN(lines, i);
++ items[items_ln].text = NULL;
++ inputw = items ? TEXTW(longest->text) : 0;
++ lines = MIN(lines, items_ln);
+ }
+
+ static void
+_AT_@ -689,8 +758,9 @@ setup(void)
+ static void
+ usage(void)
+ {
+- fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]
"
+- " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]
", stderr);
++ fputs("usage: dmenu [-bfiv] [-j json-file] [-l lines] [-p prompt]
"
++ " [-fn font] [-m monitor] [-nb color] [-nf color]
"
++ " [-sb color] [-sf color] [-w windowid]
", stderr);
+ exit(1);
+ }
+
+_AT_@ -715,6 +785,8 @@ main(int argc, char *argv[])
+ } else if (i + 1 == argc)
+ usage();
+ /* these options take one argument */
++ else if (!strcmp(argv[i], "-j"))
++ readjson(argv[++i]);
+ else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */
+ lines = atoi(argv[++i]);
+ else if (!strcmp(argv[i], "-m"))
+_AT_@ -759,9 +831,15 @@ main(int argc, char *argv[])
+
+ if (fast && !isatty(0)) {
+ grabkeyboard();
+- readstdin();
++ if (json)
++ listjson(json);
++ else
++ readstdin();
+ } else {
+- readstdin();
++ if (json)
++ listjson(json);
++ else
++ readstdin();
+ grabkeyboard();
+ }
+ setup();
diff --git a/tools.suckless.org/dmenu/patches/json/index.md b/tools.suckless.org/dmenu/patches/json/index.md
new file mode 100644
index 00000000..09f878b7
--- /dev/null
+++ b/tools.suckless.org/dmenu/patches/json/index.md
_AT_@ -0,0 +1,27 @@
+json
+=======
+Adds support for json-files. Only use object and strings.
+
+Requires [jansson](http://www.digip.org/jansson/)
+
+`dmenu -j ~/.bookmarks`
+
+Usefull code for surf:
+ #define BOOKMARKS(u, p) { \
+ .v = (const char *[]){ "/bin/sh", "-c", \
+ "prop=$(dmenu -p \"$3\" -w $1 -j ~/.bookmarks) && " \
+ "xprop -id $1 -f $2 8s -set $2 \"$prop\"", \
+ "surf-bookmark", winid, u, p, NULL \
+ } \
+ }
+
+ { MODKEY, GDK_KEY_b, spawn, BOOKMARKS("_SURF_GO", PROMPT_GO) },
+
+
+Download
+--------
+* [dmenu-json-4.9.diff](dmenu-json-4.9.diff)
+
+Author
+------
+* C.J.Wagenius <cjw_AT_voidptr.se>
Received on Fri Sep 20 2019 - 22:14:11 CEST

This archive was generated by hypermail 2.3.0 : Fri Sep 20 2019 - 22:24:27 CEST