-
Notifications
You must be signed in to change notification settings - Fork 2
/
dmenu.patch
306 lines (298 loc) · 8.19 KB
/
dmenu.patch
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
diff --git a/dmenu.c b/dmenu.c
index d605ab4..cc689a9 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -24,17 +24,21 @@
#define LENGTH(X) (sizeof X / sizeof X[0])
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
+#define SKEY(x) (x->alias ? x->alias : x->maintext)
+
/* enums */
enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
struct item {
- char *text;
+ char *alias;
+ char *maintext;
struct item *left, *right;
int out;
};
static char text[BUFSIZ] = "";
static char *embed;
+static int aliased = 0;
static int bh, mw, mh;
static int inputw = 0, promptw;
static int lrpad; /* sum of left and right padding */
@@ -81,10 +85,10 @@ calcoffsets(void)
n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">"));
/* calculate which items will begin the next page and previous page */
for (i = 0, next = curr; next; next = next->right)
- if ((i += (lines > 0) ? bh : MIN(TEXTW(next->text), n)) > n)
+ if ((i += (lines > 0) ? bh : MIN(TEXTW(SKEY(next)), n)) > n)
break;
for (i = 0, prev = curr; prev && prev->left; prev = prev->left)
- if ((i += (lines > 0) ? bh : MIN(TEXTW(prev->left->text), n)) > n)
+ if ((i += (lines > 0) ? bh : MIN(TEXTW(SKEY(prev->left)), n)) > n)
break;
}
@@ -122,7 +126,7 @@ 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);
+ return drw_text(drw, x, y, w, bh, lrpad / 2, SKEY(item), 0);
}
static void
@@ -164,7 +168,7 @@ drawmenu(void)
}
x += w;
for (item = curr; item != next; item = item->right)
- x = drawitem(item, x, 0, MIN(TEXTW(item->text), mw - x - TEXTW(">")));
+ x = drawitem(item, x, 0, MIN(TEXTW(SKEY(item)), mw - x - TEXTW(">")));
if (next) {
w = TEXTW(">");
drw_setscheme(drw, scheme[SchemeNorm]);
@@ -229,16 +233,16 @@ match(void)
matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
textsize = strlen(text) + 1;
- for (item = items; item && item->text; item++) {
+ for (item = items; item && SKEY(item); item++) {
for (i = 0; i < tokc; i++)
- if (!fstrstr(item->text, tokv[i]))
+ if (!fstrstr(SKEY(item), tokv[i]))
break;
if (i != tokc) /* not all tokens match */
continue;
/* exact matches go first, then prefixes, then substrings */
- if (!tokc || !fstrncmp(text, item->text, textsize))
+ if (!tokc || !fstrncmp(text, SKEY(item), textsize))
appenditem(item, &matches, &matchend);
- else if (!fstrncmp(tokv[0], item->text, len))
+ else if (!fstrncmp(tokv[0], SKEY(item), len))
appenditem(item, &lprefix, &prefixend);
else
appenditem(item, &lsubstr, &substrend);
@@ -268,6 +272,7 @@ insert(const char *str, ssize_t n)
{
if (strlen(text) + n > sizeof text - 1)
return;
+
/* move existing text out of the way, insert new text, and update cursor */
memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
if (n > 0)
@@ -424,7 +429,7 @@ keypress(XKeyEvent *ev)
break;
case XK_Return:
case XK_KP_Enter:
- puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
+ puts((sel && !(ev->state & ShiftMask)) ? sel->maintext : text);
if (!(ev->state & ControlMask)) {
cleanup();
exit(0);
@@ -449,7 +454,7 @@ keypress(XKeyEvent *ev)
case XK_Tab:
if (!sel)
return;
- strncpy(text, sel->text, sizeof text - 1);
+ strncpy(text, SKEY(sel), sizeof text - 1);
text[sizeof text - 1] = '\0';
cursor = strlen(text);
match();
@@ -488,8 +493,18 @@ readstdin(void)
die("cannot realloc %u bytes:", size);
if ((p = strchr(buf, '\n')))
*p = '\0';
- if (!(items[i].text = strdup(buf)))
+
+ if (aliased && (p = strchr(buf, '\t'))) {
+ *p = '\0';
+ if (!(items[i].alias = strdup(p + 1)))
+ die("cannot strdup %u bytes:", strlen(p + 1) + 1);
+ } else {
+ items[i].alias = NULL;
+ }
+
+ if (!(items[i].maintext = strdup(buf)))
die("cannot strdup %u bytes:", strlen(buf) + 1);
+
items[i].out = 0;
drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
if (tmpmax > inputw) {
@@ -497,9 +512,11 @@ readstdin(void)
imax = i;
}
}
- if (items)
- items[i].text = NULL;
- inputw = items ? TEXTW(items[imax].text) : 0;
+ if (items) {
+ items[i].maintext = NULL;
+ items[i].alias = NULL;
+ }
+ inputw = items ? TEXTW(SKEY((&items[imax]))) : 0;
lines = MIN(lines, i);
}
@@ -636,7 +653,7 @@ setup(void)
static void
usage(void)
{
- fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
+ fputs("usage: dmenu [-bfiva] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
exit(1);
}
@@ -659,6 +676,8 @@ main(int argc, char *argv[])
else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
fstrncmp = strncasecmp;
fstrstr = cistrstr;
+ } else if (!strcmp(argv[i], "-a")) { /* aliased items */
+ aliased = 1;
} else if (i + 1 == argc)
usage();
/* these options take one argument */
diff --git a/dmenu_path b/dmenu_path
old mode 100644
new mode 100755
index 338bac4..c79e559
--- a/dmenu_path
+++ b/dmenu_path
@@ -1,13 +1,85 @@
#!/bin/sh
cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
-if [ -d "$cachedir" ]; then
- cache=$cachedir/dmenu_run
-else
- cache=$HOME/.dmenu_cache # if no xdg dir, fall back to dotfile in ~
-fi
-IFS=:
-if stest -dqr -n "$cache" $PATH; then
- stest -flx $PATH | sort -u | tee "$cache"
-else
- cat "$cache"
+
+function dom0_cache
+{
+ if [ -d "$cachedir" ]; then
+ cache=$cachedir/dmenu_dom0
+ else
+ cache=$HOME/.dmenu_dom0_cache # if no xdg dir, fall back to dotfile in ~
+ fi
+
+ IFS=:
+ if stest -dqr -n "$cache" $PATH; then
+ stest -flx $PATH | sort -u | tee "$cache"
+ else
+ cat "$cache"
+ fi
+}
+
+function vms_cache
+{
+ if [ -d "$cachedir" ]; then
+ cache=$cachedir/dmenu_vms
+ else
+ cache=$HOME/.dmenu_vms_cache # if no xdg dir, fall back to dotfile in ~
+ fi
+
+ APPDIR=$HOME/.local/share/applications
+ if stest -dqr -n "$cache" $APPDIR; then
+ for file in $(stest -flr $APPDIR | sort -u); do
+ cmd=$(grep '^Exec=' "$APPDIR/$file")
+ cmd=${cmd:5}
+
+ name=$(grep '^Name=' "$APPDIR/$file")
+ name=${name:5}
+ echo -e "$cmd\t$name"
+ done | tee "$cache"
+ else
+ cat "$cache"
+ fi
+}
+
+function disp_handler
+{
+ if [[ "$1" == "disp"* ]]; then
+ template_vm_name=`qvm-prefs $1 template`
+ echo $template_vm_name
+ else
+ echo $1
+ fi
+}
+
+function usage
+{
+ echo "usage: $0 dom0 | vms | vm <vm_name>" >&2
+}
+
+if [ $# -eq 0 ]; then
+ usage
+ exit
fi
+
+case $1 in
+ dom0)
+ dom0_cache
+ ;;
+
+ vms)
+ vms_cache
+ ;;
+
+ vm)
+ shift;
+ vmname=$(disp_handler $1)
+ cmds=$(vms_cache | grep -P "\t$vmname:")
+ if [[ "$vmname" != "$1" ]]; then
+ cmds=$(echo "$cmds" | sed -e "s/--dispvm=$vmname/$1/g" | sed -e "s/$vmname/$1/g" | sed -e "s/\t[^:]*:/\t/g" | grep -v "Qube Settings" )
+ fi
+ echo "$cmds" | sed -e "s/\t[^:]*:/\t/g"
+ ;;
+
+ *)
+ usage
+ ;;
+esac
diff --git a/dmenu_run b/dmenu_run
index 834ede5..3845cfb 100755
--- a/dmenu_run
+++ b/dmenu_run
@@ -1,2 +1,40 @@
#!/bin/sh
-dmenu_path | dmenu "$@" | ${SHELL:-"/bin/sh"} &
+
+function usage
+{
+ echo "usage: $0 dom0 | vms | vm <vm_name> [dmenu arguments]" >&2
+}
+
+get_id() {
+ local id=$(xprop -root _NET_ACTIVE_WINDOW)
+ echo ${id##* } # extract id
+}
+
+get_color() {
+ local id=$(get_id)
+ local vm=$(xprop -id $id | grep '_QUBES_LABEL_COLOR(CARDINAL)')
+ local color=$(echo $vm | sed -e "s/[^=]* = //") # extract color
+ if [[ "$color" -eq "0" ]]; then
+ echo "000";
+ else
+ echo $(printf '%x' $color)
+ fi
+}
+
+if [ $# -eq 0 ]; then
+ usage
+ exit
+fi
+
+case $1 in
+ dom0) shift; dmenu_path dom0 | dmenu "$@" -i -nb "#3874D8" -nf "#FFF" -p "[Dom0] Run:" | ${SHELL:-"/bin/sh"} & ;;
+ vms) shift; dmenu_path vms | dmenu -i -a "$@" -nf "#FFF" -p "[VMs] Run:" | ${SHELL:-"/bin/sh"} & ;;
+ vm)
+ shift;
+ vm=$1;
+ shift;
+ nb=$(get_color)
+ dmenu_path vm "$vm" | dmenu -i -a "$@" -nb "#$nb" -nf "#FFF" -p "[$vm] Run:" | ${SHELL:-"/bin/sh"} &
+ ;;
+ *) usage ;;
+esac