awesome-run use awesome config file

This commit is contained in:
Julien Danjou 2008-01-31 17:32:05 +01:00
parent 4b412d86ff
commit 395b39de78
2 changed files with 92 additions and 30 deletions

View File

@ -150,9 +150,10 @@ awesome_message_SOURCES = \
common/draw.c common/draw.h \ common/draw.c common/draw.h \
common/util.h common/util.c \ common/util.h common/util.c \
common/awesome-version.h common/awesome-version.c \ common/awesome-version.h common/awesome-version.c \
common/configopts.h common/configopts.c \
awesome-message.c awesome-message.c
awesome_message_LDADD = $(XFT_LIBS) $(X_LIBS) $(CAIRO_LIBS) awesome_message_LDADD = $(XFT_LIBS) $(X_LIBS) $(CAIRO_LIBS) $(CONFUSE_LIBS)
EXTRA_DIST += awesome.1.txt EXTRA_DIST += awesome.1.txt
man_MANS += awesome.1 man_MANS += awesome.1

View File

@ -23,40 +23,109 @@
#include <getopt.h> #include <getopt.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <confuse.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include "common/swindow.h" #include "common/swindow.h"
#include "common/util.h" #include "common/util.h"
#include "common/awesome-version.h" #include "common/awesome-version.h"
#include "common/configopts.h"
#define PROGNAME "awesome-message" #define PROGNAME "awesome-message"
/** Import awesome config file format */
extern cfg_opt_t awesome_opts[];
/** awesome-run global configuration structure */
typedef struct
{
/** Display ref */
Display *display;
/** Font to use */
XftFont *font;
/** Foreground color */
XColor fg;
/** Background color */
XColor bg;
} AwesomeMsgConf;
static AwesomeMsgConf globalconf;
static void __attribute__ ((noreturn)) static void __attribute__ ((noreturn))
exit_help(int exit_code) exit_help(int exit_code)
{ {
FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr; FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
fprintf(outfile, "Usage: %s [-x xcoord] [-y ycoord] [-f fgcolor] [-b bgcolor] <message> <icon>\n", fprintf(outfile, "Usage: %s [-x xcoord] [-y ycoord] <message> <icon>\n",
PROGNAME); PROGNAME);
exit(exit_code); exit(exit_code);
} }
static void
config_parse(const char *confpatharg)
{
int ret;
char *confpath;
cfg_t *cfg, *cfg_screen, *cfg_general, *cfg_colors;
if(!confpatharg)
confpath = config_file();
else
confpath = a_strdup(confpatharg);
cfg = cfg_init(awesome_opts, CFGF_NONE);
switch((ret = cfg_parse(cfg, confpath)))
{
case CFG_FILE_ERROR:
perror("awesome-message: parsing configuration file failed");
break;
case CFG_PARSE_ERROR:
cfg_error(cfg, "awesome: parsing configuration file %s failed.\n", confpath);
break;
}
if(ret)
exit(ret);
/* get global screen section */
cfg_screen = cfg_getsec(cfg, "screen");
if(!cfg_screen)
eprint("parsing configuration file failed, no screen section found\n");
/* get colors and general section */
cfg_general = cfg_getsec(cfg_screen, "general");
cfg_colors = cfg_getsec(cfg_screen, "colors");
/* colors */
globalconf.fg = draw_color_new(globalconf.display, DefaultScreen(globalconf.display),
cfg_getstr(cfg_colors, "normal_fg"));
globalconf.bg = draw_color_new(globalconf.display, DefaultScreen(globalconf.display),
cfg_getstr(cfg_colors, "normal_bg"));
/* font */
globalconf.font = XftFontOpenName(globalconf.display, DefaultScreen(globalconf.display),
cfg_getstr(cfg_general, "font"));
p_delete(&confpath);
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
Display *disp; Display *disp;
SimpleWindow *sw; SimpleWindow *sw;
DrawCtx *ctx; DrawCtx *ctx;
XColor fg, bg;
XEvent ev; XEvent ev;
Bool running = True; Bool running = True;
const char *fg_color = "#000000";
const char *bg_color = "#ffffff";
int opt;
Area geometry = { 0, 0, 200, 50, NULL }, Area geometry = { 0, 0, 200, 50, NULL },
icon_geometry = { -1, -1, -1, -1, NULL }; icon_geometry = { -1, -1, -1, -1, NULL };
XftFont *font = NULL; int opt, option_index = 0;
int option_index = 0; char *configfile = NULL;
static struct option long_options[] = { static struct option long_options[] =
{
{"help", 0, NULL, 'h'}, {"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'}, {"version", 0, NULL, 'v'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
@ -65,19 +134,15 @@ main(int argc, char **argv)
if(!(disp = XOpenDisplay(NULL))) if(!(disp = XOpenDisplay(NULL)))
eprint("unable to open display"); eprint("unable to open display");
while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:", globalconf.display = disp;
while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:",
long_options, &option_index)) != -1) long_options, &option_index)) != -1)
switch(opt) switch(opt)
{ {
case 'v': case 'v':
eprint_version(PROGNAME); eprint_version(PROGNAME);
break; break;
case 'f':
fg_color = a_strdup(optarg);
break;
case 'b':
bg_color = a_strdup(optarg);
break;
case 'x': case 'x':
geometry.x = atoi(optarg); geometry.x = atoi(optarg);
break; break;
@ -87,19 +152,18 @@ main(int argc, char **argv)
case 'h': case 'h':
exit_help(EXIT_SUCCESS); exit_help(EXIT_SUCCESS);
break; break;
case 'n': case 'c':
font = XftFontOpenName(disp, DefaultScreen(disp), optarg); configfile = a_strdup(optarg);
break; break;
} }
if(argc - optind < 1) if(argc - optind < 1)
exit_help(EXIT_FAILURE); exit_help(EXIT_FAILURE);
if(!font) config_parse(configfile);
font = XftFontOpenName(disp, DefaultScreen(disp), "vera-12");
geometry.width = draw_textwidth(disp, font, argv[optind]); geometry.width = draw_textwidth(disp, globalconf.font, argv[optind]);
geometry.height = font->height * 1.5; geometry.height = globalconf.font->height * 1.5;
if(argc - optind >= 2) if(argc - optind >= 2)
{ {
@ -107,7 +171,8 @@ main(int argc, char **argv)
if(icon_geometry.width <= 0 || icon_geometry.height <= 0) if(icon_geometry.width <= 0 || icon_geometry.height <= 0)
eprint("invalid image\n"); eprint("invalid image\n");
else else
geometry.width += icon_geometry.width * ((double) font->height / (double) icon_geometry.height); geometry.width += icon_geometry.width
* ((double) globalconf.font->height / (double) icon_geometry.height);
} }
sw = simplewindow_new(disp, DefaultScreen(disp), sw = simplewindow_new(disp, DefaultScreen(disp),
@ -118,17 +183,13 @@ main(int argc, char **argv)
ctx = draw_context_new(disp, DefaultScreen(disp), ctx = draw_context_new(disp, DefaultScreen(disp),
geometry.width, geometry.height, sw->drawable); geometry.width, geometry.height, sw->drawable);
bg = draw_color_new(disp, DefaultScreen(disp), bg_color);
fg = draw_color_new(disp, DefaultScreen(disp), fg_color);
geometry.x = geometry.y = 0; geometry.x = geometry.y = 0;
draw_text(ctx, geometry, AlignRight, draw_text(ctx, geometry, AlignRight,
0, font, argv[optind], fg, bg); 0, globalconf.font, argv[optind], globalconf.fg, globalconf.bg);
if(icon_geometry.width > 0 && icon_geometry.height > 0) if(icon_geometry.width > 0 && icon_geometry.height > 0)
draw_image(ctx, 0, (geometry.height / 2) - (font->height / 2), draw_image(ctx, 0, (geometry.height / 2) - (globalconf.font->height / 2),
font->height, globalconf.font->height,
argv[optind + 1]); argv[optind + 1]);
p_delete(&ctx); p_delete(&ctx);