aboutsummaryrefslogtreecommitdiff
path: root/patches/dmenu-json-4.9-r2.diff
blob: 7a5a710e2fd3ecb5a5ca10a489f6d8f936216afb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
diff --git a/config.mk b/config.mk
index 0929b4a..4627988 100644
--- a/config.mk
+++ b/config.mk
@@ -18,13 +18,19 @@ FREETYPEINC = /usr/include/freetype2
 # OpenBSD (uncomment)
 #FREETYPEINC = $(X11INC)/freetype2
 
+# jansson
+JANSSONINC = `pkg-config --cflags jansson`
+JANSSONLIBS = `pkg-config --libs jansson`
+# uncomment on RHEL for strcasecmp
+#EXTRAFLAGS=-D_GNU_SOURCE
+
 # 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)
+CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" $(XINERAMAFLAGS) $(EXTRAFLAGS)
+CFLAGS  = -std=c99 -pedantic -Wall -Os $(INCS) $(CPPFLAGS)
 LDFLAGS  = $(LIBS)
 
 # compiler and linker
diff --git a/dmenu.c b/dmenu.c
index 65f25ce..58c1e23 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -15,6 +15,7 @@
 #include <X11/extensions/Xinerama.h>
 #endif
 #include <X11/Xft/Xft.h>
+#include <jansson.h>
 
 #include "drw.h"
 #include "util.h"
@@ -32,6 +33,7 @@ struct item {
 	char *text;
 	struct item *left, *right;
 	int out;
+	json_t *json;
 };
 
 static char text[BUFSIZ] = "";
@@ -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;
@@ -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)
 {
@@ -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, " "))
@@ -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] = '\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);
@@ -519,32 +549,71 @@ paste(void)
 }
 
 static void
+readjson(const char *path)
+{
+	json_error_t jerr;
+
+	if (!(json = json_load_file(path, 0, &jerr)))
+		die("%s @ line: %i - %s", jerr.text, jerr.line, path);
+}
+
+static void
+listjson(json_t *obj)
+{
+	void *iter;
+	unsigned imax = 0;
+	unsigned tmpmax = 0;
+	struct item *item;
+
+	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;
+			imax = items_ln - 1;
+		}
+		iter = json_object_iter_next(obj, iter);
+	}
+	if (items)
+		items[items_ln].text = NULL;
+	inputw = items ? TEXTW(items[imax].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 imax = 0;
 	unsigned int tmpmax = 0;
+	struct item *item;
 
 	/* 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, '\n')))
 			*p = '\0';
-		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;
+			imax = items_ln - 1;
 		}
 	}
 	if (items)
-		items[i].text = NULL;
+		items[items_ln].text = NULL;
 	inputw = items ? TEXTW(items[imax].text) : 0;
-	lines = MIN(lines, i);
+	lines = MIN(lines, items_ln);
 }
 
 static void
@@ -689,8 +758,9 @@ setup(void)
 static void
 usage(void)
 {
-	fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
-	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
+	fputs("usage: dmenu [-bfiv] [-j json-file] [-l lines] [-p prompt]\n"
+	      "             [-fn font] [-m monitor] [-nb color] [-nf color]\n"
+	      "             [-sb color] [-sf color] [-w windowid]\n", stderr);
 	exit(1);
 }
 
@@ -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"))
@@ -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();