Fix a bug in uicb_spawn when using IP address in DISPLAY
[awesome.git] / awesome-message.c
blob043ee8ef1f0029cabae60b73740ecd88ae94c529
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/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 int
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 return 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);
120 return ret;
123 static void
124 exit_on_signal(int sig __attribute__ ((unused)))
126 running = False;
130 main(int argc, char **argv)
132 Display *disp;
133 SimpleWindow *sw;
134 DrawCtx *ctx;
135 XEvent ev;
136 Area geometry = { 0, 0, 200, 50, NULL },
137 icon_geometry = { -1, -1, -1, -1, NULL };
138 int opt, ret;
139 int delay = 0;
140 char *configfile = NULL;
141 static struct option long_options[] =
143 {"help", 0, NULL, 'h'},
144 {"version", 0, NULL, 'v'},
145 {NULL, 0, NULL, 0}
148 if(!(disp = XOpenDisplay(NULL)))
149 eprint("unable to open display");
151 globalconf.display = disp;
153 while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:d:",
154 long_options, NULL)) != -1)
155 switch(opt)
157 case 'd':
158 delay = atoi(optarg);
159 break;
160 case 'v':
161 eprint_version(PROGNAME);
162 break;
163 case 'x':
164 geometry.x = atoi(optarg);
165 break;
166 case 'y':
167 geometry.y = atoi(optarg);
168 break;
169 case 'h':
170 exit_help(EXIT_SUCCESS);
171 break;
172 case 'c':
173 configfile = a_strdup(optarg);
174 break;
177 if(argc - optind < 1)
178 exit_help(EXIT_FAILURE);
180 if((ret = config_parse(configfile)))
181 return ret;
183 geometry.width = draw_textwidth(disp, globalconf.font, argv[optind]);
184 geometry.height = globalconf.font->height * 1.5;
186 if(argc - optind >= 2)
188 icon_geometry = draw_get_image_size(argv[optind + 1]);
189 if(icon_geometry.width <= 0 || icon_geometry.height <= 0)
190 eprint("invalid image\n");
191 else
192 geometry.width += icon_geometry.width
193 * ((double) globalconf.font->height / (double) icon_geometry.height);
196 sw = simplewindow_new(disp, DefaultScreen(disp),
197 geometry.x, geometry.y, geometry.width, geometry.height, 0);
199 XStoreName(disp, sw->window, PROGNAME);
201 ctx = draw_context_new(disp, DefaultScreen(disp),
202 geometry.width, geometry.height, sw->drawable);
204 geometry.x = geometry.y = 0;
205 draw_text(ctx, geometry, AlignRight,
206 0, globalconf.font, argv[optind], globalconf.fg, globalconf.bg);
208 if(icon_geometry.width > 0 && icon_geometry.height > 0)
209 draw_image(ctx, 0, (geometry.height / 2) - (globalconf.font->height / 2),
210 globalconf.font->height,
211 argv[optind + 1]);
213 p_delete(&ctx);
215 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
217 XMapRaised(disp, sw->window);
218 XSync(disp, False);
220 signal(SIGALRM, &exit_on_signal);
221 alarm(delay);
223 while(running)
225 if(XPending(disp))
227 XNextEvent(disp, &ev);
228 switch(ev.type)
230 case ButtonPress:
231 case KeyPress:
232 running = False;
233 case Expose:
234 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
235 break;
236 default:
237 break;
240 usleep(100000);
243 simplewindow_delete(sw);
244 XCloseDisplay(disp);
246 return EXIT_SUCCESS;
249 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80