Remove useless include
[awesome.git] / awesome-message.c
blob56083909e4c88c37a47e6e5e7c2d112b68bb7694
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"
37 #include "common/xscreen.h"
39 #define PROGNAME "awesome-message"
41 static Bool running = True;
43 /** Import awesome config file format */
44 extern cfg_opt_t awesome_opts[];
46 /** awesome-run global configuration structure */
47 typedef struct
49 /** Display ref */
50 Display *display;
51 /** Style */
52 style_t style;
53 } AwesomeMsgConf;
55 static AwesomeMsgConf globalconf;
57 static void __attribute__ ((noreturn))
58 exit_help(int exit_code)
60 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
61 fprintf(outfile, "Usage: %s [-x xcoord] [-y ycoord] [-d delay] <message> <icon>\n",
62 PROGNAME);
63 exit(exit_code);
66 static int
67 config_parse(int screen, const char *confpatharg)
69 int ret;
70 char *confpath;
71 cfg_t *cfg, *cfg_screen;
73 if(!confpatharg)
74 confpath = config_file();
75 else
76 confpath = a_strdup(confpatharg);
78 cfg = cfg_init(awesome_opts, CFGF_NONE);
80 switch((ret = cfg_parse(cfg, confpath)))
82 case CFG_FILE_ERROR:
83 perror("awesome-message: parsing configuration file failed");
84 break;
85 case CFG_PARSE_ERROR:
86 cfg_error(cfg, "awesome: parsing configuration file %s failed.\n", confpath);
87 break;
90 if(ret)
91 return ret;
93 /* get global screen section */
94 if(!(cfg_screen = cfg_getnsec(cfg, "screen", screen)))
95 if(!(cfg_screen = cfg_getsec(cfg, "screen")))
96 eprint("parsing configuration file failed, no screen section found\n");
98 /* style */
99 draw_style_init(globalconf.display, DefaultScreen(globalconf.display),
100 cfg_getsec(cfg_getsec(cfg_screen, "styles"), "normal"),
101 &globalconf.style, NULL);
103 if(!globalconf.style.font)
104 eprint("no default font available\n");
106 p_delete(&confpath);
108 return ret;
111 static void
112 exit_on_signal(int sig __attribute__ ((unused)))
114 running = False;
118 main(int argc, char **argv)
120 Display *disp;
121 SimpleWindow *sw;
122 DrawCtx *ctx;
123 XEvent ev;
124 area_t geometry = { 0, 0, 200, 50, NULL, NULL },
125 icon_geometry = { -1, -1, -1, -1, NULL, NULL };
126 int opt, i, x, y, ret, screen = 0, delay = 0;
127 unsigned int ui;
128 char *configfile = NULL;
129 ScreensInfo *si;
130 Window dummy;
131 static struct option long_options[] =
133 {"help", 0, NULL, 'h'},
134 {"version", 0, NULL, 'v'},
135 {NULL, 0, NULL, 0}
138 if(!(disp = XOpenDisplay(NULL)))
139 eprint("unable to open display");
141 globalconf.display = disp;
143 while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:d:",
144 long_options, NULL)) != -1)
145 switch(opt)
147 case 'd':
148 delay = atoi(optarg);
149 break;
150 case 'v':
151 eprint_version(PROGNAME);
152 break;
153 case 'x':
154 geometry.x = atoi(optarg);
155 break;
156 case 'y':
157 geometry.y = atoi(optarg);
158 break;
159 case 'h':
160 exit_help(EXIT_SUCCESS);
161 break;
162 case 'c':
163 configfile = a_strdup(optarg);
164 break;
167 if(argc - optind < 1)
168 exit_help(EXIT_FAILURE);
170 si = screensinfo_new(disp);
171 if(si->xinerama_is_active)
173 if(XQueryPointer(disp, RootWindow(disp, DefaultScreen(disp)),
174 &dummy, &dummy, &x, &y, &i, &i, &ui))
175 screen = screen_get_bycoord(si, 0, x, y);
177 else
178 screen = DefaultScreen(disp);
180 if((ret = config_parse(screen, configfile)))
181 return ret;
183 geometry.width = draw_textwidth(disp, globalconf.style.font, argv[optind]);
184 geometry.height = globalconf.style.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.style.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, 0,argv[optind], globalconf.style);
207 if(icon_geometry.width > 0 && icon_geometry.height > 0)
208 draw_image(ctx, 0, (geometry.height / 2) - (globalconf.style.font->height / 2),
209 globalconf.style.font->height, argv[optind + 1]);
211 p_delete(&ctx);
213 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
215 XMapRaised(disp, sw->window);
216 XSync(disp, False);
218 signal(SIGALRM, &exit_on_signal);
219 alarm(delay);
221 while(running)
223 if(XPending(disp))
225 XNextEvent(disp, &ev);
226 switch(ev.type)
228 case ButtonPress:
229 case KeyPress:
230 running = False;
231 case Expose:
232 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
233 break;
234 default:
235 break;
238 usleep(100000);
241 simplewindow_delete(sw);
242 XCloseDisplay(disp);
244 return EXIT_SUCCESS;
247 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80