[awesome-menu] Remove a bunch of useless keybindings and add support for ISO_Left_Tab
[awesome.git] / awesome-message.c
blob3e97ca6431aad0db4320736d59f316110d5663b7
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 /** Draw shadow_offset under text */
57 Bool shadow_offset;
58 } AwesomeMsgConf;
60 static AwesomeMsgConf globalconf;
62 static void __attribute__ ((noreturn))
63 exit_help(int exit_code)
65 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
66 fprintf(outfile, "Usage: %s [-x xcoord] [-y ycoord] [-d delay] <message> <icon>\n",
67 PROGNAME);
68 exit(exit_code);
71 static int
72 config_parse(const char *confpatharg)
74 int ret;
75 char *confpath;
76 cfg_t *cfg, *cfg_screen, *cfg_general, *cfg_colors;
78 if(!confpatharg)
79 confpath = config_file();
80 else
81 confpath = a_strdup(confpatharg);
83 cfg = cfg_init(awesome_opts, CFGF_NONE);
85 switch((ret = cfg_parse(cfg, confpath)))
87 case CFG_FILE_ERROR:
88 perror("awesome-message: parsing configuration file failed");
89 break;
90 case CFG_PARSE_ERROR:
91 cfg_error(cfg, "awesome: parsing configuration file %s failed.\n", confpath);
92 break;
95 if(ret)
96 return ret;
98 /* get global screen section */
99 cfg_screen = cfg_getsec(cfg, "screen");
101 if(!cfg_screen)
102 eprint("parsing configuration file failed, no screen section found\n");
104 /* get colors and general section */
105 cfg_general = cfg_getsec(cfg_screen, "general");
106 cfg_colors = cfg_getsec(cfg_screen, "colors");
108 /* colors */
109 draw_color_new(globalconf.display, DefaultScreen(globalconf.display),
110 cfg_getstr(cfg_colors, "normal_fg"),
111 &globalconf.fg);
112 draw_color_new(globalconf.display, DefaultScreen(globalconf.display),
113 cfg_getstr(cfg_colors, "normal_bg"),
114 &globalconf.bg);
116 globalconf.shadow_offset = cfg_getint(cfg_general, "text_shadow_offset");
118 /* font */
119 globalconf.font = XftFontOpenName(globalconf.display, DefaultScreen(globalconf.display),
120 cfg_getstr(cfg_general, "font"));
122 p_delete(&confpath);
124 return ret;
127 static void
128 exit_on_signal(int sig __attribute__ ((unused)))
130 running = False;
134 main(int argc, char **argv)
136 Display *disp;
137 SimpleWindow *sw;
138 DrawCtx *ctx;
139 XEvent ev;
140 Area geometry = { 0, 0, 200, 50, NULL },
141 icon_geometry = { -1, -1, -1, -1, NULL };
142 int opt, ret;
143 int delay = 0;
144 char *configfile = NULL;
145 static struct option long_options[] =
147 {"help", 0, NULL, 'h'},
148 {"version", 0, NULL, 'v'},
149 {NULL, 0, NULL, 0}
152 if(!(disp = XOpenDisplay(NULL)))
153 eprint("unable to open display");
155 globalconf.display = disp;
157 while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:d:",
158 long_options, NULL)) != -1)
159 switch(opt)
161 case 'd':
162 delay = atoi(optarg);
163 break;
164 case 'v':
165 eprint_version(PROGNAME);
166 break;
167 case 'x':
168 geometry.x = atoi(optarg);
169 break;
170 case 'y':
171 geometry.y = atoi(optarg);
172 break;
173 case 'h':
174 exit_help(EXIT_SUCCESS);
175 break;
176 case 'c':
177 configfile = a_strdup(optarg);
178 break;
181 if(argc - optind < 1)
182 exit_help(EXIT_FAILURE);
184 if((ret = config_parse(configfile)))
185 return ret;
187 geometry.width = draw_textwidth(disp, globalconf.font, argv[optind]);
188 geometry.height = globalconf.font->height * 1.5;
190 if(argc - optind >= 2)
192 icon_geometry = draw_get_image_size(argv[optind + 1]);
193 if(icon_geometry.width <= 0 || icon_geometry.height <= 0)
194 eprint("invalid image\n");
195 else
196 geometry.width += icon_geometry.width
197 * ((double) globalconf.font->height / (double) icon_geometry.height);
200 sw = simplewindow_new(disp, DefaultScreen(disp),
201 geometry.x, geometry.y, geometry.width, geometry.height, 0);
203 XStoreName(disp, sw->window, PROGNAME);
205 ctx = draw_context_new(disp, DefaultScreen(disp),
206 geometry.width, geometry.height, sw->drawable);
208 geometry.x = geometry.y = 0;
209 draw_text(ctx, geometry, AlignRight,
210 0, globalconf.font, argv[optind],
211 globalconf.shadow_offset, globalconf.fg, globalconf.bg);
213 if(icon_geometry.width > 0 && icon_geometry.height > 0)
214 draw_image(ctx, 0, (geometry.height / 2) - (globalconf.font->height / 2),
215 globalconf.font->height,
216 argv[optind + 1]);
218 p_delete(&ctx);
220 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
222 XMapRaised(disp, sw->window);
223 XSync(disp, False);
225 signal(SIGALRM, &exit_on_signal);
226 alarm(delay);
228 while(running)
230 if(XPending(disp))
232 XNextEvent(disp, &ev);
233 switch(ev.type)
235 case ButtonPress:
236 case KeyPress:
237 running = False;
238 case Expose:
239 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
240 break;
241 default:
242 break;
245 usleep(100000);
248 simplewindow_delete(sw);
249 XCloseDisplay(disp);
251 return EXIT_SUCCESS;
254 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80