awesome/awesome-message.c

245 lines
6.7 KiB
C
Raw Normal View History

2008-01-26 21:41:34 +01:00
/*
2008-01-27 19:10:43 +01:00
* awesome-message.c - message window for awesome
2008-01-26 21:41:34 +01:00
*
* Copyright © 2008 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#define _GNU_SOURCE
#include <getopt.h>
2008-02-08 11:51:12 +01:00
#include <signal.h>
2008-01-26 21:41:34 +01:00
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
2008-01-31 17:32:05 +01:00
2008-01-26 21:41:34 +01:00
#include <X11/Xlib.h>
#include "common/swindow.h"
#include "common/util.h"
#include "common/version.h"
2008-01-31 17:32:05 +01:00
#include "common/configopts.h"
#include "common/xscreen.h"
2008-01-26 21:41:34 +01:00
#define PROGNAME "awesome-message"
2008-02-08 11:51:12 +01:00
static Bool running = True;
2008-01-31 17:32:05 +01:00
/** Import awesome config file format */
extern cfg_opt_t awesome_opts[];
/** awesome-run global configuration structure */
typedef struct
{
/** Display ref */
Display *display;
/** Style */
style_t style;
2008-01-31 17:32:05 +01:00
} AwesomeMsgConf;
static AwesomeMsgConf globalconf;
2008-01-26 21:41:34 +01:00
static void __attribute__ ((noreturn))
exit_help(int exit_code)
{
FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
2008-02-08 11:51:12 +01:00
fprintf(outfile, "Usage: %s [-x xcoord] [-y ycoord] [-d delay] <message> <icon>\n",
PROGNAME);
2008-01-26 21:41:34 +01:00
exit(exit_code);
}
static int
config_parse(int screen, const char *confpatharg)
2008-01-31 17:32:05 +01:00
{
int ret;
char *confpath;
cfg_t *cfg, *cfg_screen;
2008-01-31 17:32:05 +01:00
if(!confpatharg)
confpath = config_file();
else
confpath = a_strdup(confpatharg);
cfg = cfg_new();
2008-01-31 17:32:05 +01:00
switch((ret = cfg_parse(cfg, confpath)))
{
case CFG_FILE_ERROR:
warn("parsing configuration file failed: %s\n", strerror(errno));
2008-01-31 17:32:05 +01:00
break;
case CFG_PARSE_ERROR:
cfg_error(cfg, "awesome: parsing configuration file %s failed.\n", confpath);
break;
}
if(ret)
return ret;
2008-01-31 17:32:05 +01:00
/* get global screen section */
if(!(cfg_screen = cfg_getnsec(cfg, "screen", screen)))
if(!(cfg_screen = cfg_getsec(cfg, "screen")))
eprint("parsing configuration file failed, no screen section found\n");
2008-01-31 17:32:05 +01:00
/* style */
draw_style_init(globalconf.display, DefaultScreen(globalconf.display),
cfg_getsec(cfg_getsec(cfg_screen, "styles"), "normal"),
&globalconf.style, NULL);
2008-01-31 17:32:05 +01:00
if(!globalconf.style.font)
eprint("no default font available\n");
2008-01-31 17:32:05 +01:00
p_delete(&confpath);
return ret;
2008-01-31 17:32:05 +01:00
}
2008-02-08 11:51:12 +01:00
static void
exit_on_signal(int sig __attribute__ ((unused)))
{
running = False;
}
2008-01-26 21:41:34 +01:00
int
main(int argc, char **argv)
{
SimpleWindow *sw;
DrawCtx *ctx;
XEvent ev;
2008-03-14 09:37:25 +01:00
area_t geometry = { 0, 0, 200, 50, NULL, NULL },
icon_geometry = { -1, -1, -1, -1, NULL, NULL };
int opt, i, x, y, ret, screen = 0, delay = 0;
unsigned int ui;
2008-01-31 17:32:05 +01:00
char *configfile = NULL;
ScreensInfo *si;
Window dummy;
2008-01-31 17:32:05 +01:00
static struct option long_options[] =
{
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{NULL, 0, NULL, 0}
};
2008-01-26 21:41:34 +01:00
2008-02-08 11:51:12 +01:00
while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:d:",
long_options, NULL)) != -1)
2008-01-26 21:41:34 +01:00
switch(opt)
{
2008-02-08 11:51:12 +01:00
case 'd':
if((delay = atoi(optarg)) <= 0)
delay = 1;
2008-02-08 11:51:12 +01:00
break;
2008-01-26 21:41:34 +01:00
case 'v':
eprint_version(PROGNAME);
2008-01-26 21:41:34 +01:00
break;
case 'x':
geometry.x = atoi(optarg);
break;
case 'y':
geometry.y = atoi(optarg);
break;
case 'h':
exit_help(EXIT_SUCCESS);
break;
2008-01-31 17:32:05 +01:00
case 'c':
configfile = a_strdup(optarg);
2008-01-26 21:41:34 +01:00
break;
}
if(argc - optind < 1)
exit_help(EXIT_FAILURE);
if(!(globalconf.display = XOpenDisplay(NULL)))
eprint("unable to open display");
si = screensinfo_new(globalconf.display);
if(si->xinerama_is_active)
{
if(XQueryPointer(globalconf.display, RootWindow(globalconf.display, DefaultScreen(globalconf.display)),
&dummy, &dummy, &x, &y, &i, &i, &ui))
screen = screen_get_bycoord(si, 0, x, y);
}
else
screen = DefaultScreen(globalconf.display);
if((ret = config_parse(screen, configfile)))
return ret;
2008-01-26 21:41:34 +01:00
geometry.width = draw_textwidth(globalconf.display, globalconf.style.font, argv[optind]);
geometry.height = globalconf.style.font->height * 1.5;
2008-01-26 21:41:34 +01:00
if(argc - optind >= 2)
{
icon_geometry = draw_get_image_size(argv[optind + 1]);
2008-01-26 21:41:34 +01:00
if(icon_geometry.width <= 0 || icon_geometry.height <= 0)
eprint("invalid image\n");
else
2008-01-31 17:32:05 +01:00
geometry.width += icon_geometry.width
* ((double) globalconf.style.font->height / (double) icon_geometry.height);
2008-01-26 21:41:34 +01:00
}
sw = simplewindow_new(globalconf.display, DefaultScreen(globalconf.display),
2008-01-26 21:41:34 +01:00
geometry.x, geometry.y, geometry.width, geometry.height, 0);
XStoreName(globalconf.display, sw->window, PROGNAME);
2008-01-26 21:41:34 +01:00
ctx = draw_context_new(globalconf.display, DefaultScreen(globalconf.display),
2008-01-26 21:41:34 +01:00
geometry.width, geometry.height, sw->drawable);
geometry.x = geometry.y = 0;
draw_text(ctx, geometry, AlignRight, 0,argv[optind], globalconf.style);
2008-01-26 21:41:34 +01:00
if(icon_geometry.width > 0 && icon_geometry.height > 0)
draw_image(ctx, 0, (geometry.height / 2) - (globalconf.style.font->height / 2),
globalconf.style.font->height, argv[optind + 1]);
2008-01-26 21:41:34 +01:00
p_delete(&ctx);
simplewindow_refresh_drawable(sw, DefaultScreen(globalconf.display));
2008-01-26 21:41:34 +01:00
XMapRaised(globalconf.display, sw->window);
XSync(globalconf.display, False);
2008-01-26 21:41:34 +01:00
2008-02-08 11:51:12 +01:00
signal(SIGALRM, &exit_on_signal);
alarm(delay);
while(running)
2008-01-26 21:41:34 +01:00
{
if(XPending(globalconf.display))
2008-01-26 21:41:34 +01:00
{
XNextEvent(globalconf.display, &ev);
2008-02-08 11:51:12 +01:00
switch(ev.type)
{
case ButtonPress:
case KeyPress:
running = False;
case Expose:
simplewindow_refresh_drawable(sw, DefaultScreen(globalconf.display));
2008-02-08 11:51:12 +01:00
break;
default:
break;
}
2008-01-26 21:41:34 +01:00
}
usleep(100000);
2008-01-26 21:41:34 +01:00
}
simplewindow_delete(&sw);
XCloseDisplay(globalconf.display);
2008-01-26 21:41:34 +01:00
return EXIT_SUCCESS;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80