trailing whitespaces removed
[awesome.git] / awesome-message.c
blob77fe0866fd12cabab7ee49bdff5cf30b5c870d8b
1 /*
2 * awesome-message.c - message window for awesome
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #define _GNU_SOURCE
23 #include <getopt.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <stdlib.h>
29 #include <confuse.h>
31 #include <X11/Xlib.h>
33 #include "common/swindow.h"
34 #include "common/util.h"
35 #include "common/awesome-version.h"
36 #include "common/configopts.h"
38 #define PROGNAME "awesome-message"
40 static Bool running = True;
42 /** Import awesome config file format */
43 extern cfg_opt_t awesome_opts[];
45 /** awesome-run global configuration structure */
46 typedef struct
48 /** Display ref */
49 Display *display;
50 /** Font to use */
51 XftFont *font;
52 /** Foreground color */
53 XColor fg;
54 /** Background color */
55 XColor bg;
56 } AwesomeMsgConf;
58 static AwesomeMsgConf globalconf;
60 static void __attribute__ ((noreturn))
61 exit_help(int exit_code)
63 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
64 fprintf(outfile, "Usage: %s [-x xcoord] [-y ycoord] [-d delay] <message> <icon>\n",
65 PROGNAME);
66 exit(exit_code);
69 static void
70 config_parse(const char *confpatharg)
72 int ret;
73 char *confpath;
74 cfg_t *cfg, *cfg_screen, *cfg_general, *cfg_colors;
76 if(!confpatharg)
77 confpath = config_file();
78 else
79 confpath = a_strdup(confpatharg);
81 cfg = cfg_init(awesome_opts, CFGF_NONE);
83 switch((ret = cfg_parse(cfg, confpath)))
85 case CFG_FILE_ERROR:
86 perror("awesome-message: parsing configuration file failed");
87 break;
88 case CFG_PARSE_ERROR:
89 cfg_error(cfg, "awesome: parsing configuration file %s failed.\n", confpath);
90 break;
93 if(ret)
94 exit(ret);
96 /* get global screen section */
97 cfg_screen = cfg_getsec(cfg, "screen");
99 if(!cfg_screen)
100 eprint("parsing configuration file failed, no screen section found\n");
102 /* get colors and general section */
103 cfg_general = cfg_getsec(cfg_screen, "general");
104 cfg_colors = cfg_getsec(cfg_screen, "colors");
106 /* colors */
107 draw_color_new(globalconf.display, DefaultScreen(globalconf.display),
108 cfg_getstr(cfg_colors, "normal_fg"),
109 &globalconf.fg);
110 draw_color_new(globalconf.display, DefaultScreen(globalconf.display),
111 cfg_getstr(cfg_colors, "normal_bg"),
112 &globalconf.bg);
114 /* font */
115 globalconf.font = XftFontOpenName(globalconf.display, DefaultScreen(globalconf.display),
116 cfg_getstr(cfg_general, "font"));
118 p_delete(&confpath);
121 static void
122 exit_on_signal(int sig __attribute__ ((unused)))
124 running = False;
128 main(int argc, char **argv)
130 Display *disp;
131 SimpleWindow *sw;
132 DrawCtx *ctx;
133 XEvent ev;
134 Area geometry = { 0, 0, 200, 50, NULL },
135 icon_geometry = { -1, -1, -1, -1, NULL };
136 int opt;
137 int delay = 0;
138 char *configfile = NULL;
139 static struct option long_options[] =
141 {"help", 0, NULL, 'h'},
142 {"version", 0, NULL, 'v'},
143 {NULL, 0, NULL, 0}
146 if(!(disp = XOpenDisplay(NULL)))
147 eprint("unable to open display");
149 globalconf.display = disp;
151 while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:d:",
152 long_options, NULL)) != -1)
153 switch(opt)
155 case 'd':
156 delay = atoi(optarg);
157 break;
158 case 'v':
159 eprint_version(PROGNAME);
160 break;
161 case 'x':
162 geometry.x = atoi(optarg);
163 break;
164 case 'y':
165 geometry.y = atoi(optarg);
166 break;
167 case 'h':
168 exit_help(EXIT_SUCCESS);
169 break;
170 case 'c':
171 configfile = a_strdup(optarg);
172 break;
175 if(argc - optind < 1)
176 exit_help(EXIT_FAILURE);
178 config_parse(configfile);
180 geometry.width = draw_textwidth(disp, globalconf.font, argv[optind]);
181 geometry.height = globalconf.font->height * 1.5;
183 if(argc - optind >= 2)
185 icon_geometry = draw_get_image_size(argv[optind + 1]);
186 if(icon_geometry.width <= 0 || icon_geometry.height <= 0)
187 eprint("invalid image\n");
188 else
189 geometry.width += icon_geometry.width
190 * ((double) globalconf.font->height / (double) icon_geometry.height);
193 sw = simplewindow_new(disp, DefaultScreen(disp),
194 geometry.x, geometry.y, geometry.width, geometry.height, 0);
196 XStoreName(disp, sw->window, PROGNAME);
198 ctx = draw_context_new(disp, DefaultScreen(disp),
199 geometry.width, geometry.height, sw->drawable);
201 geometry.x = geometry.y = 0;
202 draw_text(ctx, geometry, AlignRight,
203 0, globalconf.font, argv[optind], globalconf.fg, globalconf.bg);
205 if(icon_geometry.width > 0 && icon_geometry.height > 0)
206 draw_image(ctx, 0, (geometry.height / 2) - (globalconf.font->height / 2),
207 globalconf.font->height,
208 argv[optind + 1]);
210 p_delete(&ctx);
212 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
214 XMapRaised(disp, sw->window);
215 XSync(disp, False);
217 signal(SIGALRM, &exit_on_signal);
218 alarm(delay);
220 while(running)
222 if(XPending(disp))
224 XNextEvent(disp, &ev);
225 switch(ev.type)
227 case ButtonPress:
228 case KeyPress:
229 running = False;
230 case Expose:
231 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
232 break;
233 default:
234 break;
237 sleep(0.1);
240 simplewindow_delete(sw);
241 XCloseDisplay(disp);
243 return EXIT_SUCCESS;
246 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80