wmii/cmd/wmiimenu.c

409 lines
9.7 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"
2006-02-12 05:16:42 +03:00
static Bool done = False;
static int ret = 0;
static char text[4096];
static Color selcolor;
static Color normcolor;
2005-11-18 18:54:58 +03:00
static Display *dpy;
2005-12-05 01:45:59 +03:00
static Window win;
2005-11-18 18:54:58 +03:00
static XRectangle mrect;
static int screen;
2006-02-12 05:16:42 +03:00
static char **allitem = nil;
static size_t nallitem = 0;
static size_t allitemsz = 0;
static char **item = nil;
static size_t itemsz = 0;
static size_t nitem = 0;
static int sel = -1;
static size_t nextoff = 0;
static size_t prevoff = 0;
static size_t curroff = 0;
2005-11-18 18:54:58 +03:00
static unsigned int cmdw = 0;
2006-02-12 05:16:42 +03:00
static Draw draw = { 0 };
2005-12-21 18:18:11 +03:00
static const int seek = 30; /* 30px */
2005-12-05 01:45:59 +03:00
static void check_event(void);
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
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]\n");
2005-12-21 18:18:11 +03:00
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
{
2006-02-12 05:16:42 +03:00
size_t i;
2005-12-21 18:18:11 +03:00
unsigned int w = cmdw + 2 * seek;
2006-02-12 05:16:42 +03:00
if(!nitem)
2005-12-21 18:18:11 +03:00
return;
2006-02-12 05:16:42 +03:00
for(i = curroff; i < nitem; i++) {
w += XTextWidth(draw.font, item[i], strlen(item[i])) + mrect.height;
2005-12-21 18:18:11 +03:00
if(w > mrect.width)
break;
}
2006-02-12 05:16:42 +03:00
nextoff = i;
2005-12-21 18:18:11 +03:00
w = cmdw + 2 * seek;
2006-02-12 05:16:42 +03:00
for(i = curroff; i > 0; i--) {
w += XTextWidth(draw.font, item[i], strlen(item[i])) + mrect.height;
2005-12-21 18:18:11 +03:00
if(w > mrect.width)
break;
}
2006-02-12 05:16:42 +03:00
prevoff = i;
2005-11-18 18:54:58 +03:00
}
2006-02-12 05:16:42 +03:00
static size_t
2005-12-21 18:18:11 +03:00
update_items(char *pattern)
2005-11-18 18:54:58 +03:00
{
2006-02-12 05:16:42 +03:00
size_t plen = strlen(pattern);
int i, matched = pattern ? plen == 0 : 1;
2005-12-21 18:18:11 +03:00
cmdw = 0;
2006-02-12 05:16:42 +03:00
curroff = prevoff = nextoff = 0;
sel = -1;
for(i = 0; i < nitem; i++)
item[i] = nil; /* faster than cext_array_detach */
nitem = 0;
for(i = 0; i < nallitem; i++) {
if(matched|| !strncmp(pattern, allitem[i], plen)
|| (pattern && strstr(allitem[i], pattern)))
{
item = (char **)cext_array_attach((void **)item, allitem[i],
sizeof(char *), &itemsz);
nitem++;
}
2005-12-21 18:18:11 +03:00
}
2005-12-21 18:18:11 +03:00
update_offsets();
2006-02-12 05:16:42 +03:00
return nitem++;
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
{
2006-02-12 05:16:42 +03:00
unsigned int i, offx = 0;
draw.rect = mrect;
draw.rect.x = 0;
draw.rect.y = 0;
draw.color = normcolor;
blitz_drawlabelnoborder(dpy, &draw);
2005-12-21 18:18:11 +03:00
/* print command */
2006-02-12 05:16:42 +03:00
draw.align = WEST;
draw.data = text;
if(cmdw && sel >= 0)
draw.rect.width = cmdw;
offx += draw.rect.width;
blitz_drawlabelnoborder(dpy, &draw);
draw.align = CENTER;
if(sel >= 0) {
draw.color = normcolor;
draw.data = prevoff < curroff ? "<" : nil;
draw.rect.x = offx;
draw.rect.width = seek;
offx += draw.rect.width;
blitz_drawlabelnoborder(dpy, &draw);
2005-12-21 18:18:11 +03:00
/* determine maximum items */
2006-02-12 05:16:42 +03:00
for(i = curroff; i < nextoff; i++) {
draw.data = item[i];
draw.rect.x = offx;
draw.rect.width =
XTextWidth(draw.font, draw.data, strlen(draw.data)) + mrect.height;
offx += draw.rect.width;
2005-12-21 18:18:11 +03:00
if(sel == i) {
2006-02-12 05:16:42 +03:00
draw.color = selcolor;
blitz_drawlabel(dpy, &draw);
2005-12-21 18:18:11 +03:00
} else {
2006-02-12 05:16:42 +03:00
draw.color = normcolor;
blitz_drawlabelnoborder(dpy, &draw);
2005-12-21 18:18:11 +03:00
}
}
2006-02-12 05:16:42 +03:00
draw.color = normcolor;
draw.data = nitem > nextoff ? ">" : nil;
draw.rect.x = mrect.width - seek;
draw.rect.width = seek;
blitz_drawlabelnoborder(dpy, &draw);
2005-12-21 18:18:11 +03:00
}
2006-02-12 05:16:42 +03:00
XCopyArea(dpy, draw.drawable, win, draw.gc, 0, 0, mrect.width, mrect.height, 0, 0);
2005-12-21 18:18:11 +03:00
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
{
2005-12-21 18:18:11 +03:00
KeySym ksym;
char buf[32];
int num;
2006-02-12 05:16:42 +03:00
size_t len = strlen(text);
2005-12-21 18:18:11 +03:00
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 & ShiftMask) {
if(ksym == XK_ISO_Left_Tab)
ksym = XK_Left;
} else if(e->state & ControlMask) {
switch (ksym) {
case XK_E:
case XK_e:
ksym = XK_End;
break;
case XK_H:
case XK_h:
ksym = XK_BackSpace;
break;
case XK_J:
case XK_j:
ksym = XK_Return;
break;
case XK_U:
case XK_u:
2006-02-12 05:16:42 +03:00
text[0] = 0;
2005-12-21 18:18:11 +03:00
update_items(0);
draw_menu();
return;
break;
default: /* ignore other control sequences */
return;
break;
}
}
switch (ksym) {
case XK_Left:
2006-02-12 05:16:42 +03:00
if(sel <= 0)
2005-12-21 18:18:11 +03:00
return;
2006-02-12 05:16:42 +03:00
sel--;
2005-12-21 18:18:11 +03:00
break;
case XK_Tab:
2006-02-12 05:16:42 +03:00
if(sel < 0)
2006-01-19 19:29:20 +03:00
return;
2006-02-12 05:16:42 +03:00
cext_strlcpy(text, item[sel], sizeof(text));
update_items(text);
break;
2006-01-19 19:29:20 +03:00
case XK_Right:
2006-02-12 05:16:42 +03:00
if(sel < 0 || (sel + 1 == nitem))
2005-12-21 18:18:11 +03:00
return;
2006-02-12 05:16:42 +03:00
sel++;
2005-12-21 18:18:11 +03:00
break;
case XK_Return:
2006-02-12 05:16:42 +03:00
if(sel >= 0)
fprintf(stdout, "%s", item[sel]);
2005-12-21 18:18:11 +03:00
else if(text)
2006-02-12 05:16:42 +03:00
fprintf(stdout, "%s", text);
fflush(stdout);
done = True;
break;
2005-12-21 18:18:11 +03:00
case XK_Escape:
2006-02-12 05:16:42 +03:00
ret = 1;
done = True;
2005-12-21 18:18:11 +03:00
break;
case XK_BackSpace:
if(len) {
size_t i = len;
if(i) {
2006-02-12 05:16:42 +03:00
int prev_nitem;
do
2005-12-21 18:18:11 +03:00
text[--i] = 0;
2006-02-12 05:16:42 +03:00
while((prev_nitem = nitem) && i && prev_nitem == update_items(text));
2005-12-21 18:18:11 +03:00
}
2006-02-12 05:16:42 +03:00
update_items(text);
2005-12-21 18:18:11 +03:00
}
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));
2006-02-12 05:16:42 +03:00
update_items(text);
2005-12-21 18:18:11 +03:00
}
}
2006-02-12 05:16:42 +03:00
if(sel >= 0) {
if(sel == curroff - 1) {
sel = prevoff;
2005-12-21 18:18:11 +03:00
update_offsets();
2006-02-12 05:16:42 +03:00
} else if(sel == nextoff)
2005-12-21 18:18:11 +03:00
update_offsets();
}
draw_menu();
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
check_event()
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
XEvent ev;
while(XPending(dpy)) {
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;
}
}
2005-11-18 18:54:58 +03:00
}
void
2006-02-12 05:16:42 +03:00
read_allitems()
2005-11-18 18:54:58 +03:00
{
2006-02-12 05:16:42 +03:00
char *maxname = 0, *p, buf[1024];
size_t len = 0, max = 0;
while(fgets(buf, sizeof(buf), stdin)) {
p = strdup(buf);
len = strlen(p);
if(max < len) {
maxname = p;
max = len;
2005-12-21 18:18:11 +03:00
}
2006-02-12 05:16:42 +03:00
allitem = (char **)cext_array_attach((void **)allitem, p,
sizeof(char *), &allitemsz);
nallitem++;
}
if(maxname)
cmdw = XTextWidth(draw.font, maxname, max) + mrect.height;
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
{
2005-12-21 18:18:11 +03:00
int i;
XSetWindowAttributes wa;
2006-02-12 05:16:42 +03:00
char *fontstr, *selcolstr, *normcolstr;
2005-12-21 18:18:11 +03:00
/* command line args */
for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
switch (argv[i][1]) {
case 'v':
fprintf(stdout, "%s", version);
2005-12-21 18:18:11 +03:00
exit(0);
break;
default:
usage();
break;
}
}
dpy = XOpenDisplay(0);
if(!dpy) {
fprintf(stderr, "%s", "wmiimenu: cannot open display\n");
exit(1);
}
screen = DefaultScreen(dpy);
2005-12-21 18:18:11 +03:00
/* set font and colors */
fontstr = getenv("WMII_FONT");
if (!fontstr)
fontstr = strdup(BLITZ_FONT);
2006-02-12 05:16:42 +03:00
draw.font = blitz_getfont(dpy, fontstr);
normcolstr = getenv("WMII_NORMCOLORS");
if (!normcolstr || strlen(normcolstr) != 23)
normcolstr = strdup(BLITZ_NORMCOLORS);
2006-02-12 05:16:42 +03:00
blitz_loadcolor(dpy, screen, normcolstr, &normcolor);
selcolstr = getenv("WMII_SELCOLORS");
if (!selcolstr || strlen(selcolstr) != 23)
selcolstr = strdup(BLITZ_SELCOLORS);
2006-02-12 05:16:42 +03:00
blitz_loadcolor(dpy, screen, selcolstr, &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;
2006-02-12 05:16:42 +03:00
mrect.width = DisplayWidth(dpy, screen);
mrect.height = draw.font->ascent + draw.font->descent + 4;
mrect.y = DisplayHeight(dpy, screen) - mrect.height;
mrect.x = 0;
2005-12-21 18:18:11 +03:00
win = XCreateWindow(dpy, RootWindow(dpy, screen), mrect.x, mrect.y,
2006-02-12 05:16:42 +03:00
mrect.width, mrect.height, 0, DefaultDepth(dpy, screen),
CopyFromParent, DefaultVisual(dpy, screen),
2005-12-21 18:18:11 +03:00
CWOverrideRedirect | CWBackPixmap | CWEventMask,
&wa);
XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
XSync(dpy, False);
2006-02-12 05:16:42 +03:00
/* pixmap */
draw.gc = XCreateGC(dpy, win, 0, 0);
draw.drawable = XCreatePixmap(dpy, win, mrect.width, mrect.height,
DefaultDepth(dpy, screen));
2005-12-21 18:18:11 +03:00
2006-02-12 05:16:42 +03:00
XSync(dpy, False);
read_allitems();
2005-12-21 18:18:11 +03:00
2006-02-12 05:16:42 +03:00
text[0] = 0;
XMapRaised(dpy, win);
XSync(dpy, False);
update_items(text);
draw_menu();
while(XGrabKeyboard
(dpy, RootWindow(dpy, screen), True, GrabModeAsync,
GrabModeAsync, CurrentTime) != GrabSuccess)
usleep(1000);
2005-12-21 18:18:11 +03:00
/* main event loop */
while (!done) {
check_event();
usleep(1000);
}
2006-02-12 05:16:42 +03:00
XFreePixmap(dpy, draw.drawable);
XFreeGC(dpy, draw.gc);
XDestroyWindow(dpy, win);
2005-12-21 18:18:11 +03:00
XCloseDisplay(dpy);
2006-02-12 05:16:42 +03:00
return ret;
2005-11-18 18:54:58 +03:00
}