wmii/cmd/wmiimenu.c

479 lines
9.8 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
2006-01-20 17:20:24 +03:00
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
* (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
2005-11-18 18:54:58 +03:00
* See LICENSE file for license details.
*/
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
2005-11-18 18:54:58 +03:00
#include <X11/cursorfont.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <blitz.h>
#include <cext.h>
2006-04-06 12:39:49 +04:00
VECTOR(ItemVector, char *);
static char *title = nil;
2006-02-12 05:16:42 +03:00
static Bool done = False;
static int ret = 0;
static char text[4096];
static BlitzColor selcolor;
static BlitzColor normcolor;
2005-11-18 18:54:58 +03:00
static Display *dpy;
2005-12-05 01:45:59 +03:00
static Window win;
static XRectangle irect;
static int screen;
static ItemVector allitem = {0};
static ItemVector item = {0};
2006-02-12 05:16:42 +03:00
static int sel = -1;
static unsigned int nextoff = 0;
static unsigned int prevoff = 0;
static unsigned int curroff = 0;
2005-11-18 18:54:58 +03:00
static unsigned int cmdw = 0;
static unsigned int twidth = 0;
static unsigned int cwidth = 0;
2006-06-20 17:47:08 +04:00
static BlitzWidget *draw = nil;
2006-04-06 10:09:22 +04:00
static const int seek = 30; /* 30px */
2005-12-05 01:45:59 +03:00
static void draw_menu(void);
static void handle_kpress(XKeyEvent * e);
static char version[] = "wmiimenu - " VERSION ", (C)opyright MMIV-MMVI Anselm R. Garbe\n";
2005-11-18 18:54:58 +03:00
static Vector *
item2vector(ItemVector *iv)
{
return (Vector *) iv;
}
2005-12-21 18:18:11 +03:00
static void
usage()
2005-11-18 18:54:58 +03:00
{
fprintf(stderr, "%s", "usage: wmiimenu [-v] [-t <title>]\n");
exit(1);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
update_offsets()
2005-11-18 18:54:58 +03:00
{
unsigned int i;
2006-05-13 23:21:32 +04:00
unsigned int tw, w = cmdw + 2 * seek;
2005-12-21 18:18:11 +03:00
if(!item.size)
return;
2005-12-21 18:18:11 +03:00
for(i = curroff; i < item.size; i++) {
2006-06-20 17:47:08 +04:00
tw = blitz_textwidth(draw->font, item.data[i]);
if(tw > irect.width / 3)
tw = irect.width / 3;
w += tw + irect.height;
if(w > irect.width)
break;
}
2006-02-12 05:16:42 +03:00
nextoff = i;
2005-12-21 18:18:11 +03:00
w = cmdw + 2 * seek;
for(i = curroff; i > 0; i--) {
2006-06-20 17:47:08 +04:00
tw = blitz_textwidth(draw->font, item.data[i]);
if(tw > irect.width / 3)
tw = irect.width / 3;
w += tw + irect.height;
if(w > irect.width)
break;
}
2006-02-12 05:16:42 +03:00
prevoff = i;
2005-11-18 18:54:58 +03:00
}
static unsigned int
2005-12-21 18:18:11 +03:00
update_items(char *pattern)
2005-11-18 18:54:58 +03:00
{
unsigned int plen = strlen(pattern);
int i;
2005-12-21 18:18:11 +03:00
if(*pattern)
cmdw = cwidth;
else
cmdw = twidth;
2006-02-12 05:16:42 +03:00
curroff = prevoff = nextoff = 0;
sel = -1;
while(item.size)
cext_vdetach(item2vector(&item), item.data[0]);
for(i = 0; i < allitem.size; i++)
if(!plen || !strncmp(pattern, allitem.data[i], plen))
cext_vattach(item2vector(&item), allitem.data[i]);
for(i = 0; i < allitem.size; i++)
if(plen && strncmp(pattern, allitem.data[i], plen)
&& strstr(allitem.data[i], pattern))
cext_vattach(item2vector(&item), allitem.data[i]);
if(item.size)
2006-02-12 14:00:44 +03:00
sel = 0;
update_offsets();
return item.size;
2005-11-18 18:54:58 +03:00
}
/* creates draw structs for menu mode drawing */
2005-12-21 18:18:11 +03:00
static void
draw_menu()
2005-11-18 18:54:58 +03:00
{
unsigned int i, offx = 0;
2006-02-12 05:16:42 +03:00
2006-06-20 17:47:08 +04:00
draw->align = WEST;
2006-06-20 17:47:08 +04:00
draw->rect = irect;
draw->rect.x = 0;
draw->rect.y = 0;
draw->color = normcolor;
draw->text = nil;
draw->draw(draw);
/* print command */
if(!title || text[0]) {
2006-06-20 17:47:08 +04:00
draw->text = text;
draw->color = normcolor;
cmdw = cwidth;
if(cmdw && item.size)
2006-06-20 17:47:08 +04:00
draw->rect.width = cmdw;
draw->draw(draw);
}
else {
cmdw = twidth;
2006-06-20 17:47:08 +04:00
draw->text = title;
draw->color = selcolor;
draw->rect.width = cmdw;
draw->draw(draw);
}
2006-06-20 17:47:08 +04:00
offx += draw->rect.width;
2006-06-20 17:47:08 +04:00
draw->align = CENTER;
if(item.size) {
2006-06-20 17:47:08 +04:00
draw->color = normcolor;
draw->text = prevoff < curroff ? "<" : nil;
draw->rect.x = offx;
draw->rect.width = seek;
offx += draw->rect.width;
draw->draw(draw);
/* determine maximum items */
for(i = curroff; i < nextoff; i++) {
2006-06-20 17:47:08 +04:00
draw->text = item.data[i];
draw->rect.x = offx;
draw->rect.width = blitz_textwidth(draw->font, draw->text);
if(draw->rect.width > irect.width / 3)
draw->rect.width = irect.width / 3;
draw->rect.width += irect.height;
if(sel == i) {
2006-06-20 17:47:08 +04:00
draw->color = selcolor;
draw->draw(draw);
} else {
2006-06-20 17:47:08 +04:00
draw->color = normcolor;
draw->draw(draw);
}
2006-06-20 17:47:08 +04:00
offx += draw->rect.width;
}
2006-06-20 17:47:08 +04:00
draw->color = normcolor;
draw->text = item.size > nextoff ? ">" : nil;
draw->rect.x = irect.width - seek;
draw->rect.width = seek;
draw->draw(draw);
}
2006-06-20 17:47:08 +04:00
XCopyArea(dpy, draw->drawable, win, draw->gc, 0, 0, irect.width,
irect.height, 0, 0);
XSync(dpy, False);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
handle_kpress(XKeyEvent * e)
2005-11-18 18:54:58 +03:00
{
KeySym ksym;
char buf[32];
int num;
unsigned int len = strlen(text);
buf[0] = 0;
num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
|| IsMiscFunctionKey(ksym) || IsPFKey(ksym)
|| IsPrivateKeypadKey(ksym))
return;
/* first check if a control mask is omitted */
if(e->state & ControlMask) {
switch (ksym) {
case XK_H:
case XK_h:
ksym = XK_BackSpace;
break;
case XK_I:
case XK_i:
ksym = XK_Tab;
break;
case XK_J:
case XK_j:
ksym = XK_Return;
break;
case XK_N:
case XK_n:
ksym = XK_Right;
break;
case XK_P:
case XK_p:
ksym = XK_Left;
break;
case XK_U:
case XK_u:
text[0] = 0;
update_items(text);
draw_menu();
return;
break;
case XK_bracketleft:
ksym = XK_Escape;
break;
default: /* ignore other control sequences */
return;
break;
}
}
switch (ksym) {
case XK_Left:
if(sel <= 0)
return;
2006-02-12 05:16:42 +03:00
sel--;
break;
case XK_Tab:
if(!item.size)
return;
cext_strlcpy(text, item.data[sel], sizeof(text));
update_items(text);
break;
case XK_Right:
if(sel < 0 || (sel + 1 == item.size))
return;
2006-02-12 05:16:42 +03:00
sel++;
break;
case XK_Return:
2006-04-20 03:19:23 +04:00
if(e->state & ShiftMask) {
if(text)
fprintf(stdout, "%s", text);
}
else if(sel >= 0)
fprintf(stdout, "%s", item.data[sel]);
else if(text)
fprintf(stdout, "%s", text);
2006-02-12 05:16:42 +03:00
fflush(stdout);
done = True;
break;
case XK_Escape:
2006-02-12 05:16:42 +03:00
ret = 1;
done = True;
break;
case XK_BackSpace:
if(len) {
unsigned int i = len;
if(i) {
int prev_nitem;
do
text[--i] = 0;
while((prev_nitem = item.size) && i &&
prev_nitem == update_items(text));
}
update_items(text);
}
break;
default:
if((num == 1) && !iscntrl((int) buf[0])) {
buf[num] = 0;
if(len > 0)
cext_strlcat(text, buf, sizeof(text));
else
cext_strlcpy(text, buf, sizeof(text));
update_items(text);
}
}
if(sel >= 0) {
if(sel == curroff - 1) {
2006-02-12 14:00:44 +03:00
curroff = prevoff;
update_offsets();
} else if((sel == nextoff) && (item.size > nextoff)) {
2006-02-12 14:00:44 +03:00
curroff = nextoff;
update_offsets();
2006-02-12 14:00:44 +03:00
}
}
draw_menu();
2005-11-18 18:54:58 +03:00
}
static char *
2006-02-12 05:16:42 +03:00
read_allitems()
2005-11-18 18:54:58 +03:00
{
static char *maxname = nil;
char *p, buf[1024];
unsigned int len = 0, max = 0;
2006-02-12 05:16:42 +03:00
while(fgets(buf, sizeof(buf), stdin)) {
2006-02-12 14:00:44 +03:00
len = strlen(buf);
if (buf[len - 1] == '\n') /* there might be no \n after the last item */
buf[len - 1] = 0; /* removing \n */
2006-02-12 05:16:42 +03:00
p = strdup(buf);
if(max < len) {
2006-02-12 05:16:42 +03:00
maxname = p;
max = len;
}
cext_vattach(item2vector(&allitem), p);
2006-02-12 05:16:42 +03:00
}
return maxname;
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
int
main(int argc, char *argv[])
2005-11-18 18:54:58 +03:00
{
int i;
XSetWindowAttributes wa;
char *maxname, *p;
2006-06-20 17:47:08 +04:00
BlitzFont font;
GC gc;
Drawable pmap;
XEvent ev;
/* command line args */
for(i = 1; i < argc; i++) {
if (argv[i][0] == '-')
switch (argv[i][1]) {
case 'v':
fprintf(stdout, "%s", version);
exit(0);
break;
case 't':
if(++i < argc)
title = argv[i];
else
usage();
break;
default:
usage();
break;
}
else
usage();
}
dpy = XOpenDisplay(0);
if(!dpy) {
fprintf(stderr, "%s", "wmiimenu: cannot open display\n");
exit(1);
}
screen = DefaultScreen(dpy);
maxname = read_allitems();
/* grab as early as possible, but after reading all items!!! */
2006-05-03 15:07:14 +04:00
while(XGrabKeyboard
(dpy, RootWindow(dpy, screen), True, GrabModeAsync,
GrabModeAsync, CurrentTime) != GrabSuccess)
usleep(1000);
/* set font and colors */
blitz_x11_init(dpy);
2006-06-20 17:47:08 +04:00
font.fontstr = getenv("WMII_FONT");
if (!font.fontstr)
font.fontstr = strdup(BLITZ_FONT);
blitz_loadfont(&font);
if((p = getenv("WMII_NORMCOLORS")))
cext_strlcpy(normcolor.colstr, p, sizeof(normcolor.colstr));
if(strlen(normcolor.colstr) != 23)
cext_strlcpy(normcolor.colstr, BLITZ_NORMCOLORS, sizeof(normcolor.colstr));
blitz_loadcolor(&normcolor);
if((p = getenv("WMII_SELCOLORS")))
cext_strlcpy(selcolor.colstr, p, sizeof(selcolor.colstr));
if(strlen(selcolor.colstr) != 23)
cext_strlcpy(selcolor.colstr, BLITZ_SELCOLORS, sizeof(selcolor.colstr));
blitz_loadcolor(&selcolor);
2005-12-21 18:18:11 +03:00
wa.override_redirect = 1;
wa.background_pixmap = ParentRelative;
wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask
| SubstructureRedirectMask | SubstructureNotifyMask;
2005-12-21 18:18:11 +03:00
irect.width = DisplayWidth(dpy, screen);
2006-06-20 17:47:08 +04:00
irect.height = font.ascent + font.descent + 4;
irect.y = DisplayHeight(dpy, screen) - irect.height;
irect.x = 0;
win = XCreateWindow(dpy, RootWindow(dpy, screen), irect.x, irect.y,
irect.width, irect.height, 0, DefaultDepth(dpy, screen),
CopyFromParent, DefaultVisual(dpy, screen),
CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
XSync(dpy, False);
/* pixmap */
2006-06-20 17:47:08 +04:00
gc = XCreateGC(dpy, win, 0, 0);
pmap = XCreatePixmap(dpy, win, irect.width, irect.height,
DefaultDepth(dpy, screen));
XSync(dpy, False);
2006-06-20 17:47:08 +04:00
draw = blitz_create_input(pmap, gc, &font);
if(maxname)
2006-06-20 17:47:08 +04:00
cwidth = blitz_textwidth(draw->font, maxname) + irect.height;
if(cwidth > irect.width / 3)
cwidth = irect.width / 3;
if(title) {
2006-06-20 17:47:08 +04:00
twidth = blitz_textwidth(draw->font, title) + irect.height;
if(twidth > irect.width / 3)
twidth = irect.width / 3;
}
cmdw = title ? twidth : cwidth;
text[0] = 0;
update_items(text);
XMapRaised(dpy, win);
draw_menu();
XSync(dpy, False);
/* main event loop */
while(!XNextEvent(dpy, &ev)) {
switch (ev.type) {
case KeyPress:
handle_kpress(&ev.xkey);
break;
case Expose:
if(ev.xexpose.count == 0) {
draw_menu();
}
break;
default:
break;
}
if(done)
break;
}
XUngrabKeyboard(dpy, CurrentTime);
2006-06-20 17:47:08 +04:00
XFreePixmap(dpy, pmap);
XFreeGC(dpy, gc);
2006-02-12 05:16:42 +03:00
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
2005-12-21 18:18:11 +03:00
return ret;
2005-11-18 18:54:58 +03:00
}