-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmenu.c
More file actions
278 lines (266 loc) · 6.77 KB
/
tmenu.c
File metadata and controls
278 lines (266 loc) · 6.77 KB
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
#define _POSIX_C_SOURCE 200809L
#define FONT "-*-fixed-medium-r-*-*-13-*-*-*-*-*-*-*"
#define FGNORM "#bbbbbb"
#define BGNORM "#222222"
#define FGSEL "#eeeeee"
#define BGSEL "#005577"
#define TOP 1
#define LINES 0
#define PROMPT NULL
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#define MAXITEMS 65536
#define MAXTEXT 256
#define BUFSZ 4096
typedef struct { char *s; } It;
static Display *dp;
static Window w, root;
static int scr, sh, wh, ww;
static GC gc;
static Pixmap pm;
static XFontStruct *fs;
static int fa, fh;
static It its[MAXITEMS];
static int ni = 0;
static It *mt[MAXITEMS];
static int nm = 0, sel = 0, hoff = 0;
static char tb[MAXTEXT];
static int tl = 0;
static int top = TOP;
static int ln = LINES;
static const char *pr = PROMPT;
static unsigned long col[4];
static unsigned long xc(const char *s) {
XColor c;
Colormap cm = DefaultColormap(dp, scr);
if(!XParseColor(dp, cm, s, &c) || !XAllocColor(dp, cm, &c)) exit(1);
return c.pixel;
}
static void rd(void) {
char buf[BUFSZ];
while(fgets(buf, sizeof buf, stdin)) {
int l = strlen(buf);
if(l && buf[l-1] == '\n') buf[--l] = 0;
if(!l) continue;
if(!(its[ni].s = strdup(buf))) exit(1);
if(++ni >= MAXITEMS) break;
}
}
static void fi(void) {
int i, j, p, q;
char lo[MAXTEXT], tmp[BUFSZ];
sel = 0; hoff = 0;
for(i = 0; (lo[i] = tolower((unsigned char)tb[i])); i++);
if(!tl) {
for(nm = 0; nm < ni; nm++) mt[nm] = &its[nm];
return;
}
for(p = 0, i = 0; i < ni; i++) {
for(j = 0; (tmp[j] = tolower((unsigned char)its[i].s[j])); j++);
if(strncmp(tmp, lo, tl) == 0) mt[p++] = &its[i];
}
for(q = 0, i = 0; i < ni; i++) {
for(j = 0; (tmp[j] = tolower((unsigned char)its[i].s[j])); j++);
if(strncmp(tmp, lo, tl) != 0 && strstr(tmp, lo)) mt[p + q++] = &its[i];
}
nm = p + q;
}
static void dr(void) {
int x = 0, y, i, mx, cx;
XSetForeground(dp, gc, col[1]);
XFillRectangle(dp, pm, gc, 0, 0, ww, wh);
if(pr && *pr) {
int pl = strlen(pr);
int pw = XTextWidth(fs, pr, pl) + 8;
XSetForeground(dp, gc, col[3]);
XFillRectangle(dp, pm, gc, 0, 0, pw, fh);
XSetForeground(dp, gc, col[2]);
XDrawString(dp, pm, gc, 4, fa, pr, pl);
x = pw;
}
XSetForeground(dp, gc, col[0]);
XDrawString(dp, pm, gc, x + 4, fa, tb, tl);
cx = x + 4 + XTextWidth(fs, tb, tl);
XDrawLine(dp, pm, gc, cx, 2, cx, fh - 2);
if(ln > 0) {
for(i = 0; i < nm && i < ln; i++) {
int sl = strlen(mt[i]->s);
y = fh * (i + 1);
if(i == sel) {
XSetForeground(dp, gc, col[3]);
XFillRectangle(dp, pm, gc, 0, y, ww, fh);
XSetForeground(dp, gc, col[2]);
} else {
XSetForeground(dp, gc, col[0]);
}
XDrawString(dp, pm, gc, 4, y + fa, mt[i]->s, sl);
}
} else {
mx = cx + 12;
for(i = hoff; i < nm && mx < ww; i++) {
int sl = strlen(mt[i]->s);
int iw = XTextWidth(fs, mt[i]->s, sl) + 8;
if(i == sel) {
XSetForeground(dp, gc, col[3]);
XFillRectangle(dp, pm, gc, mx, 0, iw, fh);
XSetForeground(dp, gc, col[2]);
} else {
XSetForeground(dp, gc, col[0]);
}
XDrawString(dp, pm, gc, mx + 4, fa, mt[i]->s, sl);
mx += iw;
}
}
XCopyArea(dp, pm, w, gc, 0, 0, ww, wh, 0, 0);
XFlush(dp);
}
static void run(const char *cmd) {
pid_t pid;
if(!*cmd) exit(1);
pid = fork();
if(pid < 0) exit(1);
if(pid == 0) {
XCloseDisplay(dp);
execl("/bin/sh", "sh", "-c", cmd, NULL);
_exit(1);
}
exit(0);
}
static void scroll_to(int s) {
int ox, mx, i;
if(ln > 0 || nm == 0) { sel = s; return; }
if(s < 0) s = 0;
if(s >= nm) s = nm - 1;
if(s < hoff) hoff = s;
ox = (pr && *pr ? XTextWidth(fs, pr, strlen(pr)) + 8 : 0)
+ 4 + XTextWidth(fs, tb, tl) + 12;
while(1) {
mx = ox;
for(i = hoff; i < nm; i++) {
int sl = strlen(mt[i]->s);
int iw = XTextWidth(fs, mt[i]->s, sl) + 8;
if(mx + iw > ww) break;
mx += iw;
}
if(s < i) break;
hoff++;
}
sel = s;
}
static void kp(XKeyEvent *e) {
char buf[32];
KeySym ks;
XLookupString(e, buf, sizeof buf, &ks, NULL);
if(e->state & ControlMask) {
switch(ks) {
case XK_u: tl = 0; tb[0] = 0; fi(); dr(); return;
case XK_w:
while(tl && tb[tl-1] == ' ') tb[--tl] = 0;
while(tl && tb[tl-1] != ' ') tb[--tl] = 0;
fi(); dr(); return;
case XK_k:
if(sel > 0) { scroll_to(sel - 1); dr(); }
return;
case XK_j:
if(sel < nm - 1) { scroll_to(sel + 1); dr(); }
return;
}
}
switch(ks) {
case XK_Escape: exit(1);
case XK_Return:
case XK_KP_Enter:
run(nm > 0 ? mt[sel]->s : tb);
return;
case XK_BackSpace:
if(tl) { tb[--tl] = 0; fi(); dr(); }
return;
case XK_Up:
case XK_Left:
if(sel > 0) { scroll_to(sel - 1); dr(); }
return;
case XK_Down:
case XK_Right:
if(sel < nm - 1) { scroll_to(sel + 1); dr(); }
return;
default:
if(!iscntrl((unsigned char)buf[0]) && tl < MAXTEXT - 1) {
tb[tl++] = buf[0];
tb[tl] = 0;
fi(); dr();
}
}
}
static void grabfocus(void) {
struct timespec ts = {0, 10000000};
Window fw;
int di, i;
for(i = 0; i < 100; i++) {
XGetInputFocus(dp, &fw, &di);
if(fw == w) return;
XSetInputFocus(dp, w, RevertToParent, CurrentTime);
XSync(dp, 0);
nanosleep(&ts, NULL);
}
}
int main(int argc, char *argv[]) {
int i;
XSetWindowAttributes wa;
for(i = 1; i < argc; i++) {
if(!strcmp(argv[i], "-b")) top = 0;
else if(!strcmp(argv[i], "-l") && i+1 < argc) ln = atoi(argv[++i]);
else if(!strcmp(argv[i], "-p") && i+1 < argc) pr = argv[++i];
}
rd();
if(!(dp = XOpenDisplay(NULL))) return 1;
scr = DefaultScreen(dp);
root = RootWindow(dp, scr);
sh = DisplayHeight(dp, scr);
if(!(fs = XLoadQueryFont(dp, FONT)))
if(!(fs = XLoadQueryFont(dp, "fixed"))) return 1;
fa = fs->ascent;
fh = fs->ascent + fs->descent + 2;
ww = DisplayWidth(dp, scr);
wh = ln > 0 ? fh * (ln + 1) : fh;
col[0] = xc(FGNORM); col[1] = xc(BGNORM);
col[2] = xc(FGSEL); col[3] = xc(BGSEL);
wa.override_redirect = 1;
wa.background_pixel = col[1];
wa.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask;
w = XCreateWindow(dp, root, 0, top ? 0 : sh - wh, ww, wh, 0,
DefaultDepth(dp, scr), CopyFromParent, DefaultVisual(dp, scr),
CWOverrideRedirect | CWBackPixel | CWEventMask, &wa);
pm = XCreatePixmap(dp, w, ww, wh, DefaultDepth(dp, scr));
if(pm == None) return 1;
gc = XCreateGC(dp, w, 0, NULL);
if(!gc) return 1;
XSetFont(dp, gc, fs->fid);
fi();
XMapRaised(dp, w);
XSync(dp, 0);
{
struct timespec ts = {0, 1000000};
for(i = 0; i < 1000; i++) {
if(XGrabKeyboard(dp, root, 1, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) break;
nanosleep(&ts, NULL);
}
if(i == 1000) return 1;
}
grabfocus();
dr();
{
XEvent ev;
while(!XNextEvent(dp, &ev))
if(ev.type == KeyPress) kp(&ev.xkey);
else if(ev.type == Expose && !ev.xexpose.count) dr();
else if(ev.type == DestroyNotify && ev.xdestroywindow.window == w) exit(1);
}
return 0;
}