[uicb] uicb_client_toggletag() with NULL use as sticky toggle
[awesome.git] / awesome-message.c
blob5cc36faee4821cd26ab0a071230d6c23c94bfc65
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>
28 #include <errno.h>
30 #include <X11/Xlib.h>
32 #include "common/swindow.h"
33 #include "common/util.h"
34 #include "common/version.h"
35 #include "common/configopts.h"
36 #include "common/xscreen.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 /** Style */
51 style_t style;
52 } AwesomeMsgConf;
54 static AwesomeMsgConf globalconf;
56 static void __attribute__ ((noreturn))
57 exit_help(int exit_code)
59 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
60 fprintf(outfile, "Usage: %s [-x xcoord] [-y ycoord] [-d delay] <message> <icon>\n",
61 PROGNAME);
62 exit(exit_code);
65 static int
66 config_parse(int screen, const char *confpatharg)
68 int ret;
69 char *confpath;
70 cfg_t *cfg, *cfg_screen;
72 if(!confpatharg)
73 confpath = config_file();
74 else
75 confpath = a_strdup(confpatharg);
77 cfg = cfg_new();
79 switch((ret = cfg_parse(cfg, confpath)))
81 case CFG_FILE_ERROR:
82 warn("parsing configuration file failed: %s\n", strerror(errno));
83 break;
84 case CFG_PARSE_ERROR:
85 cfg_error(cfg, "awesome: parsing configuration file %s failed.\n", confpath);
86 break;
89 if(ret)
90 return ret;
92 /* get global screen section */
93 if(!(cfg_screen = cfg_getnsec(cfg, "screen", screen)))
94 if(!(cfg_screen = cfg_getsec(cfg, "screen")))
95 eprint("parsing configuration file failed, no screen section found\n");
97 /* style */
98 draw_style_init(globalconf.display, DefaultScreen(globalconf.display),
99 cfg_getsec(cfg_getsec(cfg_screen, "styles"), "normal"),
100 &globalconf.style, NULL);
102 if(!globalconf.style.font)
103 eprint("no default font available\n");
105 p_delete(&confpath);
107 return ret;
110 static void
111 exit_on_signal(int sig __attribute__ ((unused)))
113 running = False;
117 main(int argc, char **argv)
119 SimpleWindow *sw;
120 DrawCtx *ctx;
121 XEvent ev;
122 area_t geometry = { 0, 0, 200, 50, NULL, NULL },
123 icon_geometry = { -1, -1, -1, -1, NULL, NULL };
124 int opt, i, x, y, ret, screen = 0, delay = 0;
125 unsigned int ui;
126 char *configfile = NULL;
127 ScreensInfo *si;
128 Window dummy;
129 static struct option long_options[] =
131 {"help", 0, NULL, 'h'},
132 {"version", 0, NULL, 'v'},
133 {NULL, 0, NULL, 0}
136 while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:d:",
137 long_options, NULL)) != -1)
138 switch(opt)
140 case 'd':
141 if((delay = atoi(optarg)) <= 0)
142 delay = 1;
143 break;
144 case 'v':
145 eprint_version(PROGNAME);
146 break;
147 case 'x':
148 geometry.x = atoi(optarg);
149 break;
150 case 'y':
151 geometry.y = atoi(optarg);
152 break;
153 case 'h':
154 exit_help(EXIT_SUCCESS);
155 break;
156 case 'c':
157 configfile = a_strdup(optarg);
158 break;
161 if(argc - optind < 1)
162 exit_help(EXIT_FAILURE);
164 if(!(globalconf.display = XOpenDisplay(NULL)))
165 eprint("unable to open display");
167 si = screensinfo_new(globalconf.display);
168 if(si->xinerama_is_active)
170 if(XQueryPointer(globalconf.display, RootWindow(globalconf.display, DefaultScreen(globalconf.display)),
171 &dummy, &dummy, &x, &y, &i, &i, &ui))
172 screen = screen_get_bycoord(si, 0, x, y);
174 else
175 screen = DefaultScreen(globalconf.display);
177 if((ret = config_parse(screen, configfile)))
178 return ret;
180 geometry.width = draw_textwidth(globalconf.display, globalconf.style.font, argv[optind]);
181 geometry.height = globalconf.style.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.style.font->height / (double) icon_geometry.height);
193 sw = simplewindow_new(globalconf.display, DefaultScreen(globalconf.display),
194 geometry.x, geometry.y, geometry.width, geometry.height, 0);
196 XStoreName(globalconf.display, sw->window, PROGNAME);
198 ctx = draw_context_new(globalconf.display, DefaultScreen(globalconf.display),
199 geometry.width, geometry.height, sw->drawable);
201 geometry.x = geometry.y = 0;
202 draw_text(ctx, geometry, AlignRight, 0,argv[optind], globalconf.style);
204 if(icon_geometry.width > 0 && icon_geometry.height > 0)
205 draw_image(ctx, 0, (geometry.height / 2) - (globalconf.style.font->height / 2),
206 globalconf.style.font->height, argv[optind + 1]);
208 p_delete(&ctx);
210 simplewindow_refresh_drawable(sw, DefaultScreen(globalconf.display));
212 XMapRaised(globalconf.display, sw->window);
213 XSync(globalconf.display, False);
215 signal(SIGALRM, &exit_on_signal);
216 alarm(delay);
218 while(running)
220 if(XPending(globalconf.display))
222 XNextEvent(globalconf.display, &ev);
223 switch(ev.type)
225 case ButtonPress:
226 case KeyPress:
227 running = False;
228 case Expose:
229 simplewindow_refresh_drawable(sw, DefaultScreen(globalconf.display));
230 break;
231 default:
232 break;
235 usleep(100000);
238 simplewindow_delete(&sw);
239 XCloseDisplay(globalconf.display);
241 return EXIT_SUCCESS;
244 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80