wmii/cmd/wmii9menu.c

519 lines
11 KiB
C
Raw Normal View History

2007-02-26 13:17:10 +03:00
/* Licence
* =======
*
* 9menu is free software, and is Copyright (c) 1994 by David Hogan and
* Arnold Robbins. Permission is granted to all sentient beings to use
* this software, to make copies of it, and to distribute those copies,
* provided that:
*
* (1) the copyright and licence notices are left intact
* (2) the recipients are aware that it is free software
* (3) any unapproved changes in functionality are either
* (i) only distributed as patches
* or (ii) distributed as a new program which is not called 9menu
* and whose documentation gives credit where it is due
* (4) the authors are not held responsible for any defects
* or shortcomings in the software, or damages caused by it.
*
* There is no warranty for this software. Have a nice day.
*
* --
* Arnold Robbins
* arnold@skeeve.com
*
* 9menu.c
*
* This program puts up a window that is just a menu, and executes
* commands that correspond to the items selected.
*
* Initial idea: Arnold Robbins
* Version using libXg: Matty Farrow (some ideas borrowed)
* This code by: David Hogan and Arnold Robbins
*/
/*
* Heavily modified by Kris Maglione for use with wmii.
*/
2007-02-26 13:17:10 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xutil.h>
2007-03-23 19:30:50 +03:00
#define nil ((void*)0)
char version[] = "@(#) wmii9menu version 1.8";
2007-02-26 13:17:10 +03:00
2007-03-23 19:30:50 +03:00
/* lovely X stuff */
Display *dpy;
2007-02-26 13:17:10 +03:00
int screen;
Window root;
Window menuwin;
2007-03-23 19:30:50 +03:00
Colormap defcmap;
XColor color;
XFontStruct *font;
2007-02-26 13:17:10 +03:00
GC gc;
2007-03-23 19:30:50 +03:00
2007-02-26 13:17:10 +03:00
unsigned long selbg;
unsigned long selfg;
unsigned long normbg;
unsigned long normfg;
unsigned long border;
2007-03-23 19:30:50 +03:00
char *sfgname = nil;
char *sbgname = nil;
char *nfgname = nil;
char *nbgname = nil;
char *brcname = nil;
/* for XSetWMProperties to use */
int g_argc;
2007-02-26 13:17:10 +03:00
char **g_argv;
2007-03-23 19:30:50 +03:00
/* for labels read from files */
int f_argc;
2007-02-26 13:17:10 +03:00
char **f_argv;
2007-03-23 19:30:50 +03:00
char *initial = "";
int cur;
2007-02-26 13:17:10 +03:00
char *fontlist[] = { /* default font list if no -font */
"pelm.latin1.9",
"lucm.latin1.9",
"blit",
"9x15bold",
"9x15",
"lucidasanstypewriter-12",
"fixed",
2007-03-23 19:30:50 +03:00
nil
2007-02-26 13:17:10 +03:00
};
char *progname; /* my name */
char *displayname; /* X display */
char *fontname; /* font */
char *filename; /* file to read options or labels from */
char **labels; /* list of labels and commands */
char **commands;
int numitems;
void usage(), run_menu();
void create_window();
void redraw(), warpmouse();
void memory();
int args();
2007-02-26 13:17:10 +03:00
/* memory --- print the out of memory message and die */
void
memory(s)
char *s;
{
fprintf(stderr, "%s: couldn't allocate memory for %s\n", progname, s);
exit(1);
}
/* args --- go through the argument list, set options */
struct {
char *name, **var;
} argtab[] = {
{"display", &displayname},
{"file", &filename},
{"initial", &initial},
{"font", &fontname},
{"nb", &nbgname},
{"nf", &nfgname},
{"sb", &sbgname},
{"sf", &sfgname},
{"br", &brcname},
{0, },
}, *ap;
2007-02-26 13:17:10 +03:00
int
args(int argc, char **argv)
2007-02-26 13:17:10 +03:00
{
int i, n;
for (i = 0; i < argc && argv[i][0] == '-'; i++) {
if(strcmp(argv[i], "-version") == 0) {
2007-02-26 13:17:10 +03:00
printf("%s\n", version);
exit(0);
}
if(i+1 >= argc)
usage();
for(ap = argtab; ap->name; ap++) {
n = strlen(ap->name);
if(strncmp(ap->name, &argv[i][1], n) == 0) {
*ap->var = argv[i][n+1] ? &argv[i][n+1] : argv[++i];
break;
}
}
if(ap->name == 0)
2007-02-26 13:17:10 +03:00
usage();
}
return i;
}
2007-04-06 09:44:50 +04:00
unsigned long
getcolor(char *name, unsigned long def) {
if ((name != nil)
&& (XParseColor(dpy, defcmap, name, &color) != 0)
&& (XAllocColor(dpy, defcmap, &color) != 0))
return color.pixel;
else
return def;
}
2007-02-26 13:17:10 +03:00
/* main --- crack arguments, set up X stuff, run the main menu loop */
int
main(int argc, char **argv)
2007-02-26 13:17:10 +03:00
{
int i, j;
char *cp;
XGCValues gv;
unsigned long mask;
int nlabels = 0;
g_argc = argc;
g_argv = argv;
/* set default label name */
2007-03-23 19:30:50 +03:00
if ((cp = strrchr(argv[0], '/')) == nil)
progname = argv[0];
2007-02-26 13:17:10 +03:00
else
progname = ++cp;
2007-02-26 13:17:10 +03:00
++argv;
--argc;
i = args(argc, argv);
numitems = argc - i;
2007-03-23 19:30:50 +03:00
if (numitems <= 0 && filename == nil)
2007-02-26 13:17:10 +03:00
usage();
if (filename) {
/* Read options and labels from file */
char fbuf[1024];
FILE *fp;
fp = fopen(filename, "r");
2007-03-23 19:30:50 +03:00
if (fp == nil) {
2007-02-26 13:17:10 +03:00
fprintf(stderr, "%s: couldn't open '%s'\n", progname,
filename);
exit(1);
}
while (fgets(fbuf, sizeof fbuf, fp)) {
char *s = fbuf;
strtok(s, "\n");
if (s[0] == '-') {
char *temp[3];
temp[0] = s;
temp[1] = strchr(s, ' ');
if (temp[1]) {
*(temp[1]++) = '\0';
s = malloc(strlen(temp[1]) + 1);
2007-03-23 19:30:50 +03:00
if (s == nil)
2007-02-26 13:17:10 +03:00
memory("temporary argument");
strcpy(s, temp[1]);
temp[1] = s;
}
temp[2] = 0;
args(temp[1] ? 2 : 1, temp);
continue;
}
if (s[0] == '#')
continue;
/* allow - in menu items to be escaped */
if (s[0] == '\\')
++s;
/* allocate space */
if (f_argc < nlabels + 1) {
int k;
char **temp = malloc(sizeof(char *) * (f_argc + 5));
if (temp == 0)
memory("temporary item");
for (k = 0; k < nlabels; k++)
temp[k] = f_argv[k];
free(f_argv);
f_argv = temp;
f_argc += 5;
}
f_argv[nlabels] = malloc(strlen(s) + 1);
2007-03-23 19:30:50 +03:00
if (f_argv[nlabels] == nil)
2007-02-26 13:17:10 +03:00
memory("temporary text");
strcpy(f_argv[nlabels], s);
++nlabels;
}
}
labels = (char **) malloc((numitems + nlabels) * sizeof(char *));
commands = (char **) malloc((numitems + nlabels) * sizeof(char *));
2007-03-23 19:30:50 +03:00
if (commands == nil || labels == nil)
2007-02-26 13:17:10 +03:00
memory("command and label arrays");
for (j = 0; j < numitems; j++) {
labels[j] = argv[i + j];
2007-03-23 19:30:50 +03:00
if ((cp = strchr(labels[j], ':')) != nil) {
2007-02-26 13:17:10 +03:00
*cp++ = '\0';
commands[j] = cp;
} else
commands[j] = labels[j];
if(strcmp(labels[j], initial) == 0)
cur = j;
2007-02-26 13:17:10 +03:00
}
/*
* Now we no longer need i (our offset into argv) so we recycle it,
* while keeping the old value of j!
*/
for (i = 0; i < nlabels; i++) {
labels[j] = f_argv[i];
2007-03-23 19:30:50 +03:00
if ((cp = strchr(labels[j], ':')) != nil) {
2007-02-26 13:17:10 +03:00
*cp++ = '\0';
commands[j] = cp;
} else
commands[j] = labels[j];
++j;
}
/* And now we merge the totals */
numitems += nlabels;
dpy = XOpenDisplay(displayname);
2007-03-23 19:30:50 +03:00
if (dpy == nil) {
2007-02-26 13:17:10 +03:00
fprintf(stderr, "%s: cannot open display", progname);
2007-03-23 19:30:50 +03:00
if (displayname != nil)
2007-02-26 13:17:10 +03:00
fprintf(stderr, " %s", displayname);
fprintf(stderr, "\n");
exit(1);
}
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
/*
* This used to be
* black = BlackPixel(dpy, screen);
* white = WhitePixel(dpy, screen);
*/
defcmap = DefaultColormap(dpy, screen);
2007-04-06 09:44:50 +04:00
selbg = getcolor(sbgname, BlackPixel(dpy, screen));
selfg = getcolor(sfgname, WhitePixel(dpy, screen));
normbg = getcolor(nbgname, selfg);
normfg = getcolor(nfgname, selbg);
border = getcolor(brcname, selbg);
2007-02-26 13:17:10 +03:00
/* try user's font first */
2007-03-23 19:30:50 +03:00
if (fontname != nil) {
2007-02-26 13:17:10 +03:00
font = XLoadQueryFont(dpy, fontname);
2007-03-23 19:30:50 +03:00
if (font == nil)
2007-02-26 13:17:10 +03:00
fprintf(stderr, "%s: warning: can't load font %s\n",
progname, fontname);
}
/* if no user font, try one of our default fonts */
2007-03-23 19:30:50 +03:00
if (font == nil) {
for (i = 0; fontlist[i] != nil; i++) {
2007-02-26 13:17:10 +03:00
font = XLoadQueryFont(dpy, fontlist[i]);
2007-03-23 19:30:50 +03:00
if (font != nil)
2007-02-26 13:17:10 +03:00
break;
}
}
2007-03-23 19:30:50 +03:00
if (font == nil) {
2007-02-26 13:17:10 +03:00
fprintf(stderr, "%s: fatal: cannot load a font\n", progname);
exit(1);
}
gv.foreground = normfg;
gv.background = normbg;
gv.font = font->fid;
gv.line_width = 0;
mask = GCForeground | GCBackground | GCFont | GCLineWidth;
gc = XCreateGC(dpy, root, mask, &gv);
run_menu();
XCloseDisplay(dpy);
exit(0);
}
/* usage --- print a usage message and die */
void
usage()
{
fprintf(stderr, "usage: %s [-display <displayname>] [-font <fontname>] ", progname);
fprintf(stderr, "[-file filename] ");
fprintf(stderr, "[-{n,s}{f,b} <color>] [-br <color>] ");
fprintf(stderr, "[-version] menitem[:command] ...\n");
2007-02-26 13:17:10 +03:00
exit(0);
}
/* run_menu --- put up the window, execute selected commands */
void
run_menu()
{
XEvent ev;
int i, old, wide, high, dx, dy;
2007-02-26 13:17:10 +03:00
dx = 0;
for (i = 0; i < numitems; i++) {
wide = XTextWidth(font, labels[i], strlen(labels[i])) + 4;
if (wide > dx)
dx = wide;
}
wide = dx;
high = font->ascent + font->descent + 1;
dy = numitems * high;
enum {
MouseMask =
2007-02-26 13:17:10 +03:00
ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
2007-02-26 13:17:10 +03:00
| PointerMotionMask,
MenuMask =
2007-02-26 13:17:10 +03:00
MouseMask
| StructureNotifyMask
| ExposureMask
};
create_window(wide, high);
warpmouse(wide, high);
2007-02-26 13:17:10 +03:00
XSelectInput(dpy, menuwin, MenuMask);
XMapWindow(dpy, menuwin);
i = 0; /* save menu Item position */
for (;;) {
XNextEvent(dpy, &ev);
switch (ev.type) {
default:
fprintf(stderr, "%s: unknown ev.type %d\n",
progname, ev.type);
break;
case ButtonRelease:
i = ev.xbutton.y/high;
if (ev.xbutton.x < 0 || ev.xbutton.x > wide)
return;
2007-02-26 13:17:10 +03:00
else if (i < 0 || i >= numitems)
return;
2007-02-26 13:17:10 +03:00
printf("%s\n", commands[i]);
return;
case ButtonPress:
2007-02-26 13:17:10 +03:00
case MotionNotify:
old = cur;
cur = ev.xbutton.y/high;
if (ev.xbutton.x < 0 || ev.xbutton.x > wide)
cur = ~0;
2007-02-26 13:17:10 +03:00
if (cur == old)
break;
redraw(high, wide);
2007-02-26 13:17:10 +03:00
break;
case MapNotify:
redraw(high, wide);
2007-02-26 13:17:10 +03:00
if(XGrabPointer(dpy, menuwin, False, MouseMask,
GrabModeAsync, GrabModeAsync,
0, None, CurrentTime
2007-02-26 13:17:10 +03:00
) != GrabSuccess) {
fprintf(stderr, "Failed to grab the mouse\n");
}
break;
case Expose:
redraw(high, wide);
2007-02-26 13:17:10 +03:00
break;
case MappingNotify: /* why do we get this? */
break;
}
}
}
/* set_wm_hints --- set all the window manager hints */
void
create_window(int wide, int high)
2007-02-26 13:17:10 +03:00
{
XSetWindowAttributes wa = { 0 };
unsigned int h;
int x, y, dummy;
Window wdummy;
h = high * numitems;
XQueryPointer(dpy, root, &wdummy, &wdummy, &x, &y,
&dummy, &dummy, (uint*)&dummy);
x -= wide / 2;
if (x < 0)
x = 0;
else if (x + wide > DisplayWidth(dpy, screen))
x = DisplayWidth(dpy, screen) - wide;
y -= cur * high + high / 2;
if (y < 0)
y = 0;
else if (y + h > DisplayHeight(dpy, screen))
y = DisplayHeight(dpy, screen) - h;
2007-02-26 13:17:10 +03:00
wa.override_redirect = True;
wa.border_pixel = border;
wa.background_pixel = normbg;
menuwin = XCreateWindow(dpy, root, x, y, wide, h,
2007-02-26 13:17:10 +03:00
1, DefaultDepth(dpy, screen), CopyFromParent,
DefaultVisual(dpy, screen),
CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
| CWEventMask,
&wa);
XSetCommand(dpy, menuwin, g_argv, g_argc);
}
/* redraw --- actually redraw the menu */
void
redraw(int high, int wide)
2007-02-26 13:17:10 +03:00
{
int tx, ty, i;
for (i = 0; i < numitems; i++) {
tx = (wide - XTextWidth(font, labels[i], strlen(labels[i]))) / 2;
ty = i*high + font->ascent + 1;
if (cur == i)
XSetForeground(dpy, gc, selbg);
else
XSetForeground(dpy, gc, normbg);
XFillRectangle(dpy, menuwin, gc, 0, i*high, wide, high);
if (cur == i)
XSetForeground(dpy, gc, selfg);
else
XSetForeground(dpy, gc, normfg);
XDrawString(dpy, menuwin, gc, tx, ty, labels[i], strlen(labels[i]));
}
}
/* warpmouse --- bring the mouse to the menu */
void
warpmouse(int wide, int high)
2007-02-26 13:17:10 +03:00
{
int offset;
/* move tip of pointer into middle of menu item */
offset = (font->ascent + font->descent + 1) / 2;
offset += 6; /* fudge factor */
XWarpPointer(dpy, None, menuwin, 0, 0, 0, 0,
2007-02-26 13:17:10 +03:00
wide/2, cur*high-high/2+offset);
}