wmii/cmd/wmiibar2.c

471 lines
9.4 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
* (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include "../libixp2/ixp.h"
#include "blitz.h"
/*
* filesystem specification
* / Droot
* /display Fdisplay 'top', 'bottom', 'none'
* /font Ffont <xlib font name>
* /new Fnew returns id of new item
* /default/ Ditem
* /default/bNpress Fevent <command, gets executed>
* /default/bgcolor Fcolor <#RRGGBB, #RGB>
* /default/fgcolor Fcolor <#RRGGBB, #RGB>
* /default/bordercolor Fcolor <#RRGGBB, #RGB>
* /1/ Ditem
* /1/data Fdata <arbitrary data which gets displayed>
* /1/bNpress Fevent <command, gets executed>
* /1/bgcolor Fcolor <#RRGGBB, #RGB>
* /1/fgcolor Fcolor <#RRGGBB, #RGB>
* /1/bordercolor Fcolor <#RRGGBB, #RGB> ...
*/
2005-12-05 01:45:59 +03:00
enum { /* 8-bit qid.path.type */
2005-11-18 18:54:58 +03:00
Droot,
Ditem,
Fdisplay,
Fnew,
2005-12-05 01:45:59 +03:00
Fdata, /* data to display */
2005-11-18 18:54:58 +03:00
Fevent,
Fcolor,
Ffont
};
#define NONE (u16)0xffff
typedef struct {
2005-12-05 01:45:59 +03:00
char *name;
u8 type;
} QFile;
2005-11-18 18:54:58 +03:00
2005-12-05 01:45:59 +03:00
static QFile qfilelist[] = {
2005-11-18 18:54:58 +03:00
{"display", Fdisplay},
{"font", Ffont},
{"new", Fnew},
{"data", Fdata},
{"bgcolor", Fcolor},
{"fgcolor", Fcolor},
{"bordercolor", Fcolor},
{"b1press", Fevent},
{"b2press", Fevent},
{"b3press", Fevent},
{"b4press", Fevent},
{"b5press", Fevent},
{0, 0},
};
typedef struct Map Map;
struct Map {
2005-12-05 01:45:59 +03:00
u32 fid;
Qid qid;
Map *next;
};
2005-11-18 18:54:58 +03:00
typedef struct {
2005-12-05 01:45:59 +03:00
int id;
char text[256];
int value;
unsigned long bg;
unsigned long fg;
unsigned long border[4];
XFontStruct *font;
char event[5][256];
} Item;
static size_t nitems = 0;
static char *sockfile = nil;
2005-12-05 01:45:59 +03:00
static pid_t mypid = 0;
static IXPServer srv = { 0 };
static Qid root_qid;
2005-11-18 18:54:58 +03:00
static Display *dpy;
2005-12-05 01:45:59 +03:00
static int screen_num;
static char *align = nil;
static char *font = nil;
2005-11-18 18:54:58 +03:00
/*
static GC gc;
static Window win;
static XRectangle geom;
static int mapped = 0;
static Pixmap pmap;
static Draw zero_draw = { 0 };
*/
2005-12-05 01:45:59 +03:00
static char *version[] = {
"wmiibar - window manager improved bar - " VERSION "\n"
2005-12-05 01:45:59 +03:00
" (C)opyright MMIV-MMV Anselm R. Garbe\n", 0
2005-11-18 18:54:58 +03:00
};
2005-12-05 01:45:59 +03:00
static void usage()
2005-11-18 18:54:58 +03:00
{
fprintf(stderr, "%s %d",
"usage: wmiibar -a <server address> [-v]\n"
" -a server address \n"
" -v version info\n",
2005-12-05 01:45:59 +03:00
NONE);
2005-11-18 18:54:58 +03:00
exit(1);
}
2005-12-05 01:45:59 +03:00
static int dummy_error_handler(Display * dpy, XErrorEvent * err)
2005-11-18 18:54:58 +03:00
{
return 0;
}
2005-12-05 01:45:59 +03:00
static void exit_cleanup()
2005-11-18 18:54:58 +03:00
{
if (mypid == getpid())
unlink(sockfile);
}
2005-12-05 01:45:59 +03:00
static u64 make_qpath(u8 type, u16 item, u16 file)
2005-11-18 18:54:58 +03:00
{
return ((u64) file << 24) | ((u64) item << 8) | (u64) type;
}
2005-12-05 01:45:59 +03:00
static u8 qpath_type(u64 path)
2005-11-18 18:54:58 +03:00
{
return path & 0xff;
}
2005-12-05 01:45:59 +03:00
static u16 qpath_item(u64 path)
2005-11-18 18:54:58 +03:00
{
return (path >> 8) & 0xffff;
}
/*
static u16
qpath_file(u64 path)
{
return (path >> 24) & 0xffff;
}
*/
static Map *fid_to_map(Map *maps, u32 fid)
{
Map *m;
for (m = maps; m; m = m->next)
if (m->fid == fid)
return m;
return nil;
}
static Bool qfile_index(char *name, u16 * index)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
2005-11-18 18:54:58 +03:00
for (i = 0; qfilelist[i].name; i++)
if (!strncmp(name, qfilelist[i].name, strlen(qfilelist[i].name))) {
*index = i;
return True;
2005-11-18 18:54:58 +03:00
}
return False;
2005-11-18 18:54:58 +03:00
}
static Bool make_qid(Qid * dir, char *wname, Qid * new)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
u16 idx;
const char *errstr;
2005-11-18 18:54:58 +03:00
if (dir->type != IXP_QTDIR)
return False;
new->version = nil;
2005-11-18 18:54:58 +03:00
if (!qfile_index(wname, &idx)) {
new->type = IXP_QTDIR;
if (!strncmp(wname, "..", 3)) {
*new = root_qid;
return True;
2005-11-18 18:54:58 +03:00
} else if (!strncmp(wname, "default", 8)) {
new->path = make_qpath(Ditem, 0, NONE);
return True;
2005-11-18 18:54:58 +03:00
}
/* check if wname is a number, otherwise file not found */
idx = (u16) cext_strtonum(wname, 1, 0xffff, &errstr);
if (errstr || nitems < idx)
return False;
2005-11-18 18:54:58 +03:00
/* found */
new->path = make_qpath(Ditem, idx, NONE);
} else {
new->type = IXP_QTFILE;
new->path = make_qpath(qfilelist[idx].type, qpath_item(dir->path), idx);
2005-11-18 18:54:58 +03:00
}
return True;
2005-11-18 18:54:58 +03:00
}
static int attach(IXPServer *s, IXPConn *c)
2005-11-18 18:54:58 +03:00
{
Map *maps = c->aux;
Map *m, *new = cext_emallocz(sizeof(Map));
if (!maps)
c->aux = new;
else {
for (m = maps; m && m->next; m = m->next);
m->next = new;
}
2005-12-20 18:32:42 +03:00
fprintf(stderr, "attaching %u %u %s %s\n", s->fcall.fid, s->fcall.afid, s->fcall.uname, s->fcall.aname);
new->qid = root_qid;
new->fid = s->fcall.fid;
2005-11-18 18:54:58 +03:00
s->fcall.id = RATTACH;
s->fcall.qid = root_qid;
return TRUE;
}
2005-12-05 01:45:59 +03:00
static int walk(IXPServer * s, IXPConn * c)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
u16 nwqid = 0;
Qid qid;
Map *map;
2005-11-18 18:54:58 +03:00
fprintf(stderr, "%s", "walking\n");
if (!(map = fid_to_map(c->aux, s->fcall.fid))) {
s->errstr = "no directory associated with fid";
return FALSE;
}
if (s->fcall.fid != s->fcall.newfid
2005-12-05 01:45:59 +03:00
&& (fid_to_map(c->aux, s->fcall.newfid))) {
2005-11-18 18:54:58 +03:00
s->errstr = "fid alreay in use";
return FALSE;
}
if (s->fcall.nwname) {
qid = map->qid;
for (nwqid = 0; (nwqid < s->fcall.nwname) && make_qid(&qid, s->fcall.wname[nwqid], &s->fcall.wqid[nwqid]); nwqid++)
2005-11-18 18:54:58 +03:00
qid = s->fcall.wqid[nwqid];
if (!nwqid) {
s->errstr = "file not found";
return FALSE;
}
}
/*
* following condition is required by 9P, a fid will only be valid if
* the walk was complete
*/
if (nwqid == s->fcall.nwname) {
Map *m, *maps = c->aux;
2005-11-18 18:54:58 +03:00
if (s->fcall.fid == s->fcall.newfid) {
if (maps == map)
maps = maps->next;
else {
for (m = maps; m && m->next != map; m = m->next);
m->next = map->next;
}
2005-11-18 18:54:58 +03:00
free(map);
}
map = cext_emallocz(sizeof(Map));
2005-11-18 18:54:58 +03:00
map->qid = qid;
map->fid = s->fcall.newfid;
for (m = maps; m && m->next; m = m->next);
if (!m)
maps = map;
else
m->next = map;
2005-11-18 18:54:58 +03:00
}
s->fcall.id = RWALK;
s->fcall.nwqid = nwqid;
return TRUE;
}
2005-12-05 01:45:59 +03:00
static int _open(IXPServer * s, IXPConn * c)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
Map *map = fid_to_map(c->aux, s->fcall.fid);
2005-11-18 18:54:58 +03:00
fprintf(stderr, "%s", "opening\n");
if (!map) {
s->errstr = "invalid fid";
return FALSE;
}
if ((s->fcall.mode != IXP_OREAD) && (s->fcall.mode != IXP_OWRITE)) {
s->errstr = "mode not supported";
return FALSE;
}
s->fcall.id = ROPEN;
s->fcall.qid = map->qid;
s->fcall.iounit =
s->fcall.maxmsg - (sizeof(u8) + sizeof(u16) + 2 * sizeof(u32));
return TRUE;
}
2005-12-05 01:45:59 +03:00
static int _read(IXPServer * s, IXPConn * c)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
Map *map = fid_to_map(c->aux, s->fcall.fid);
Stat stat = { 0 };
u8 *p;
2005-11-18 18:54:58 +03:00
fprintf(stderr, "%s", "reading\n");
if (!map) {
s->errstr = "invalid fid";
return FALSE;
}
stat.mode = 0xff;
stat.atime = stat.mtime = time(0);
cext_strlcpy(stat.uid, getenv("USER"), sizeof(stat.uid));
cext_strlcpy(stat.gid, getenv("USER"), sizeof(stat.gid));
cext_strlcpy(stat.muid, getenv("USER"), sizeof(stat.muid));
2005-11-18 18:54:58 +03:00
fprintf(stderr, "%d\n", qpath_item(map->qid.path));
switch (qpath_type(map->qid.path)) {
default:
case Droot:
p = s->fcall.data;
cext_strlcpy(stat.name, "display", sizeof(stat.name));
2005-11-18 18:54:58 +03:00
stat.length = strlen(align);
make_qid(&root_qid, "display", &stat.qid);
stat.size = ixp_sizeof_stat(&stat);
s->fcall.count = stat.size;
p = ixp_enc_stat(p, &stat);
cext_strlcpy(stat.name, "font", sizeof(stat.name));
2005-11-18 18:54:58 +03:00
stat.length = strlen(font);
make_qid(&root_qid, "font", &stat.qid);
stat.size = ixp_sizeof_stat(&stat);;
s->fcall.count += stat.size;
p = ixp_enc_stat(p, &stat);
cext_strlcpy(stat.name, "new", sizeof(stat.name));
2005-11-18 18:54:58 +03:00
stat.length = 0;
make_qid(&root_qid, "new", &stat.qid);
stat.size = ixp_sizeof_stat(&stat);;
s->fcall.count += stat.size;
p = ixp_enc_stat(p, &stat);
s->fcall.id = RREAD;
fprintf(stderr, "%d msize\n", s->fcall.count);
break;
case Ditem:
break;
case Fdisplay:
break;
case Fnew:
break;
case Fdata:
break;
case Fevent:
break;
case Fcolor:
break;
case Ffont:
break;
}
return TRUE;
}
static int _write(IXPServer *s, IXPConn *c)
2005-11-18 18:54:58 +03:00
{
return FALSE;
}
static int clunk(IXPServer *s, IXPConn *c)
2005-11-18 18:54:58 +03:00
{
Map *maps = c->aux;
Map *m, *map = fid_to_map(maps, s->fcall.fid);
2005-11-18 18:54:58 +03:00
if (!map) {
s->errstr = "invalid fid";
return FALSE;
}
if (maps == map)
maps = maps->next;
else {
for (m = maps; m && m->next != map; m = m->next);
m->next = map->next;
}
2005-11-18 18:54:58 +03:00
free(map);
s->fcall.id = RCLUNK;
return TRUE;
}
2005-12-05 01:45:59 +03:00
static void freeconn(IXPServer * s, IXPConn * c)
2005-11-18 18:54:58 +03:00
{
Map *m, *maps = c->aux;
while ((m = maps)) {
maps = maps->next;
free(m);
2005-11-18 18:54:58 +03:00
}
c->aux = nil;
2005-11-18 18:54:58 +03:00
}
static IXPTFunc funcs[] = {
{TVERSION, ixp_server_tversion},
{TATTACH, attach},
{TWALK, walk},
{TOPEN, _open},
{TREAD, _read},
{TWRITE, _write},
{TCLUNK, clunk},
{0, 0}
};
2005-12-05 01:45:59 +03:00
int main(int argc, char *argv[])
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
Item *item;
2005-11-18 18:54:58 +03:00
/* command line args */
for (i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
switch (argv[i][1]) {
case 'v':
fprintf(stdout, "%s", version[0]);
exit(0);
break;
case 'a':
2005-11-18 18:54:58 +03:00
if (i + 1 < argc)
sockfile = argv[++i];
else
usage();
break;
default:
usage();
break;
}
}
dpy = XOpenDisplay(0);
if (!dpy) {
fprintf(stderr, "%s", "wmiibar: cannot open display\n");
2005-11-18 18:54:58 +03:00
exit(1);
}
XSetErrorHandler(dummy_error_handler);
screen_num = DefaultScreen(dpy);
if (!ixp_server_init(&srv, sockfile, funcs, freeconn)) {
fprintf(stderr, "wmiibar: fatal: %s\n", srv.errstr);
2005-11-18 18:54:58 +03:00
exit(1);
}
root_qid.type = IXP_QTDIR;
root_qid.version = 0;
root_qid.path = make_qpath(Droot, NONE, NONE);
mypid = getpid();
atexit(exit_cleanup);
/* default item settings */
item = cext_emallocz(sizeof(Item));
2005-11-18 18:54:58 +03:00
align = "bottom";
font = "fixed";
ixp_server_loop(&srv);
if (srv.errstr) {
fprintf(stderr, "wmiibar: fatal: %s\n", srv.errstr);
2005-11-18 18:54:58 +03:00
ixp_server_deinit(&srv);
exit(1);
}
ixp_server_deinit(&srv);
return 0;
}