wmii/cmd/wmii9menu.c

410 lines
8.5 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>
#include <util.h>
2007-02-26 13:17:10 +03:00
2007-03-23 19:30:50 +03:00
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-04-07 09:28:49 +04:00
ulong selfg, selbg;
ulong normfg, normbg;
ulong border;
char *sfgname, *sbgname;
char *nfgname, *nbgname;
char *brcname;
2007-03-23 19:30:50 +03:00
/* 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
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 **labels; /* list of labels and commands */
char **commands;
int numitems;
void usage();
void run_menu();
void create_window();
void redraw();
void warpmouse();
void memory();
int args();
2007-02-26 13:17:10 +03:00
/* args --- go through the argument list, set options */
struct {
char *name, **var;
} argtab[] = {
{"display", &displayname},
{"initial", &initial},
{"font", &fontname},
{"nb", &nbgname},
{"nf", &nfgname},
{"sb", &sbgname},
{"sf", &sfgname},
{"br", &brcname},
{0, },
}, *ap;
2007-04-07 09:28:49 +04:00
ulong
getcolor(char *name, ulong def) {
if((name != nil)
2007-04-06 09:44:50 +04:00
&& (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
{
2007-04-07 09:28:49 +04:00
int i, n;
2007-02-26 13:17:10 +03:00
char *cp;
XGCValues gv;
2007-04-07 09:28:49 +04:00
ulong mask;
2007-02-26 13:17:10 +03:00
g_argc = argc;
g_argv = argv;
/* set default label name */
2007-04-07 09:28:49 +04:00
if((cp = strrchr(argv[0], '/')) != nil)
progname = ++cp;
2007-04-07 09:28:49 +04:00
else
progname = argv[0];
2007-02-26 13:17:10 +03:00
2007-04-07 09:28:49 +04:00
for(i = 1; i < argc && argv[i][0] == '-'; i++) {
if(strcmp(argv[i], "-version") == 0) {
printf("%s\n", version);
exit(0);
}
2007-02-26 13:17:10 +03:00
2007-04-07 09:28:49 +04:00
for(ap = argtab; ap->name; ap++) {
n = strlen(ap->name);
if(strncmp(ap->name, argv[i]+1, n) == 0)
break;
2007-02-26 13:17:10 +03:00
}
2007-04-07 09:28:49 +04:00
if(ap->name == 0)
usage();
if(argv[i][n+1] != '\0')
*ap->var = &argv[i][n+1];
else {
if(argc <= i+1)
usage();
*ap->var = argv[++i];
2007-02-26 13:17:10 +03:00
}
}
2007-04-07 09:28:49 +04:00
argc -= i, argv += i;
2007-02-26 13:17:10 +03:00
2007-04-07 09:28:49 +04:00
if(argc == 0)
usage();
2007-02-26 13:17:10 +03:00
2007-04-07 09:28:49 +04:00
numitems = argc;
2007-02-26 13:17:10 +03:00
2007-04-07 09:28:49 +04:00
labels = emalloc(numitems * sizeof(*labels));
commands = emalloc(numitems * sizeof(*labels));
for(i = 0; i < numitems; i++) {
labels[i] = argv[i];
if((cp = strchr(labels[i], ':')) != nil) {
2007-02-26 13:17:10 +03:00
*cp++ = '\0';
2007-04-07 09:28:49 +04:00
commands[i] = cp;
2007-02-26 13:17:10 +03:00
} else
2007-04-07 09:28:49 +04:00
commands[i] = labels[i];
if(strcmp(labels[i], initial) == 0)
cur = i;
2007-02-26 13:17:10 +03:00
}
dpy = XOpenDisplay(displayname);
2007-04-07 09:28:49 +04:00
if(dpy == nil) {
2007-02-26 13:17:10 +03:00
fprintf(stderr, "%s: cannot open display", progname);
2007-04-07 09:28:49 +04: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);
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-04-07 09:28:49 +04:00
if(fontname != nil) {
2007-02-26 13:17:10 +03:00
font = XLoadQueryFont(dpy, fontname);
2007-04-07 09:28:49 +04: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-04-07 09:28:49 +04: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-04-07 09:28:49 +04:00
if(font != nil)
2007-02-26 13:17:10 +03:00
break;
}
}
2007-04-07 09:28:49 +04: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, "[-{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;
2007-04-07 09:28:49 +04:00
for(i = 0; i < numitems; i++) {
2007-02-26 13:17:10 +03:00
wide = XTextWidth(font, labels[i], strlen(labels[i])) + 4;
2007-04-07 09:28:49 +04:00
if(wide > dx)
2007-02-26 13:17:10 +03:00
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 */
2007-04-07 09:28:49 +04:00
for(;;) {
2007-02-26 13:17:10 +03:00
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;
2007-04-07 09:28:49 +04:00
if(ev.xbutton.x < 0 || ev.xbutton.x > wide)
return;
2007-04-07 09:28:49 +04: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;
2007-04-07 09:28:49 +04:00
if(ev.xbutton.x < 0 || ev.xbutton.x > wide)
cur = ~0;
2007-04-07 09:28:49 +04:00
if(cur == old)
2007-02-26 13:17:10 +03:00
break;
redraw(high, wide);
2007-02-26 13:17:10 +03:00
break;
case MapNotify:
redraw(high, wide);
if(XGrabPointer(dpy, menuwin,
False, MouseMask,
GrabModeAsync, GrabModeAsync,
0, None, CurrentTime) != GrabSuccess)
2007-02-26 13:17:10 +03:00
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 };
2007-04-07 09:28:49 +04:00
uint h;
int x, y, dummy;
Window wdummy;
h = high * numitems;
XQueryPointer(dpy, root, &wdummy, &wdummy, &x, &y,
&dummy, &dummy, (uint*)&dummy);
x -= wide / 2;
2007-04-07 09:28:49 +04:00
if(x < 0)
x = 0;
2007-04-07 09:28:49 +04:00
else if(x + wide > DisplayWidth(dpy, screen))
x = DisplayWidth(dpy, screen) - wide;
y -= cur * high + high / 2;
2007-04-07 09:28:49 +04:00
if(y < 0)
y = 0;
2007-04-07 09:28:49 +04:00
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;
2007-04-07 09:28:49 +04:00
for(i = 0; i < numitems; i++) {
2007-02-26 13:17:10 +03:00
tx = (wide - XTextWidth(font, labels[i], strlen(labels[i]))) / 2;
ty = i*high + font->ascent + 1;
2007-04-07 09:28:49 +04:00
if(cur == i)
2007-02-26 13:17:10 +03:00
XSetForeground(dpy, gc, selbg);
else
XSetForeground(dpy, gc, normbg);
XFillRectangle(dpy, menuwin, gc, 0, i*high, wide, high);
2007-04-07 09:28:49 +04:00
if(cur == i)
2007-02-26 13:17:10 +03:00
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);
}