make draw_text multibyte UTF-8 aware
[awesome.git] / awesome-message.c
blobe995f30610ec4c03d135ee663d4ed4e55bf627f1
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 globalconf.fg = draw_color_new(globalconf.display, DefaultScreen(globalconf.display),
108 cfg_getstr(cfg_colors, "normal_fg"));
109 globalconf.bg = draw_color_new(globalconf.display, DefaultScreen(globalconf.display),
110 cfg_getstr(cfg_colors, "normal_bg"));
112 /* font */
113 globalconf.font = XftFontOpenName(globalconf.display, DefaultScreen(globalconf.display),
114 cfg_getstr(cfg_general, "font"));
116 p_delete(&confpath);
119 static void
120 exit_on_signal(int sig __attribute__ ((unused)))
122 running = False;
126 main(int argc, char **argv)
128 Display *disp;
129 SimpleWindow *sw;
130 DrawCtx *ctx;
131 XEvent ev;
132 Area geometry = { 0, 0, 200, 50, NULL },
133 icon_geometry = { -1, -1, -1, -1, NULL };
134 int opt;
135 int delay = 0;
136 char *configfile = NULL;
137 static struct option long_options[] =
139 {"help", 0, NULL, 'h'},
140 {"version", 0, NULL, 'v'},
141 {NULL, 0, NULL, 0}
144 if(!(disp = XOpenDisplay(NULL)))
145 eprint("unable to open display");
147 globalconf.display = disp;
149 while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:d:",
150 long_options, NULL)) != -1)
151 switch(opt)
153 case 'd':
154 delay = atoi(optarg);
155 break;
156 case 'v':
157 eprint_version(PROGNAME);
158 break;
159 case 'x':
160 geometry.x = atoi(optarg);
161 break;
162 case 'y':
163 geometry.y = atoi(optarg);
164 break;
165 case 'h':
166 exit_help(EXIT_SUCCESS);
167 break;
168 case 'c':
169 configfile = a_strdup(optarg);
170 break;
173 if(argc - optind < 1)
174 exit_help(EXIT_FAILURE);
176 config_parse(configfile);
178 geometry.width = draw_textwidth(disp, globalconf.font, argv[optind]);
179 geometry.height = globalconf.font->height * 1.5;
181 if(argc - optind >= 2)
183 icon_geometry = draw_get_image_size(argv[optind + 1]);
184 if(icon_geometry.width <= 0 || icon_geometry.height <= 0)
185 eprint("invalid image\n");
186 else
187 geometry.width += icon_geometry.width
188 * ((double) globalconf.font->height / (double) icon_geometry.height);
191 sw = simplewindow_new(disp, DefaultScreen(disp),
192 geometry.x, geometry.y, geometry.width, geometry.height, 0);
194 XStoreName(disp, sw->window, PROGNAME);
196 ctx = draw_context_new(disp, DefaultScreen(disp),
197 geometry.width, geometry.height, sw->drawable);
199 geometry.x = geometry.y = 0;
200 draw_text(ctx, geometry, AlignRight,
201 0, globalconf.font, argv[optind], globalconf.fg, globalconf.bg);
203 if(icon_geometry.width > 0 && icon_geometry.height > 0)
204 draw_image(ctx, 0, (geometry.height / 2) - (globalconf.font->height / 2),
205 globalconf.font->height,
206 argv[optind + 1]);
208 p_delete(&ctx);
210 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
212 XMapRaised(disp, sw->window);
213 XSync(disp, False);
215 signal(SIGALRM, &exit_on_signal);
216 alarm(delay);
218 while(running)
220 if(XPending(disp))
222 XNextEvent(disp, &ev);
223 switch(ev.type)
225 case ButtonPress:
226 case KeyPress:
227 running = False;
228 case Expose:
229 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
230 break;
231 default:
232 break;
235 sleep(0.1);
238 simplewindow_delete(sw);
239 XCloseDisplay(disp);
241 return EXIT_SUCCESS;
244 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80