add scratch window support
[awesome.git] / awesome-message.c
blob982aefcd4191f4f5a5ab78c77a4054b2bcae385f
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 <unistd.h>
26 #include <stdlib.h>
28 #include <confuse.h>
30 #include <X11/Xlib.h>
32 #include "common/swindow.h"
33 #include "common/util.h"
34 #include "common/awesome-version.h"
35 #include "common/configopts.h"
37 #define PROGNAME "awesome-message"
39 /** Import awesome config file format */
40 extern cfg_opt_t awesome_opts[];
42 /** awesome-run global configuration structure */
43 typedef struct
45 /** Display ref */
46 Display *display;
47 /** Font to use */
48 XftFont *font;
49 /** Foreground color */
50 XColor fg;
51 /** Background color */
52 XColor bg;
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] <message> <icon>\n",
62 PROGNAME);
63 exit(exit_code);
66 static void
67 config_parse(const char *confpatharg)
69 int ret;
70 char *confpath;
71 cfg_t *cfg, *cfg_screen, *cfg_general, *cfg_colors;
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 exit(ret);
93 /* get global screen section */
94 cfg_screen = cfg_getsec(cfg, "screen");
96 if(!cfg_screen)
97 eprint("parsing configuration file failed, no screen section found\n");
99 /* get colors and general section */
100 cfg_general = cfg_getsec(cfg_screen, "general");
101 cfg_colors = cfg_getsec(cfg_screen, "colors");
103 /* colors */
104 globalconf.fg = draw_color_new(globalconf.display, DefaultScreen(globalconf.display),
105 cfg_getstr(cfg_colors, "normal_fg"));
106 globalconf.bg = draw_color_new(globalconf.display, DefaultScreen(globalconf.display),
107 cfg_getstr(cfg_colors, "normal_bg"));
109 /* font */
110 globalconf.font = XftFontOpenName(globalconf.display, DefaultScreen(globalconf.display),
111 cfg_getstr(cfg_general, "font"));
113 p_delete(&confpath);
117 main(int argc, char **argv)
119 Display *disp;
120 SimpleWindow *sw;
121 DrawCtx *ctx;
122 XEvent ev;
123 Bool running = True;
124 Area geometry = { 0, 0, 200, 50, NULL },
125 icon_geometry = { -1, -1, -1, -1, NULL };
126 int opt;
127 char *configfile = NULL;
128 static struct option long_options[] =
130 {"help", 0, NULL, 'h'},
131 {"version", 0, NULL, 'v'},
132 {NULL, 0, NULL, 0}
135 if(!(disp = XOpenDisplay(NULL)))
136 eprint("unable to open display");
138 globalconf.display = disp;
140 while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:",
141 long_options, NULL)) != -1)
142 switch(opt)
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 config_parse(configfile);
166 geometry.width = draw_textwidth(disp, globalconf.font, argv[optind]);
167 geometry.height = globalconf.font->height * 1.5;
169 if(argc - optind >= 2)
171 icon_geometry = draw_get_image_size(argv[optind + 1]);
172 if(icon_geometry.width <= 0 || icon_geometry.height <= 0)
173 eprint("invalid image\n");
174 else
175 geometry.width += icon_geometry.width
176 * ((double) globalconf.font->height / (double) icon_geometry.height);
179 sw = simplewindow_new(disp, DefaultScreen(disp),
180 geometry.x, geometry.y, geometry.width, geometry.height, 0);
182 XStoreName(disp, sw->window, "awmessage");
184 ctx = draw_context_new(disp, DefaultScreen(disp),
185 geometry.width, geometry.height, sw->drawable);
187 geometry.x = geometry.y = 0;
188 draw_text(ctx, geometry, AlignRight,
189 0, globalconf.font, argv[optind], globalconf.fg, globalconf.bg);
191 if(icon_geometry.width > 0 && icon_geometry.height > 0)
192 draw_image(ctx, 0, (geometry.height / 2) - (globalconf.font->height / 2),
193 globalconf.font->height,
194 argv[optind + 1]);
196 p_delete(&ctx);
198 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
200 XMapRaised(disp, sw->window);
201 XSync(disp, False);
203 while (running)
205 XNextEvent(disp, &ev);
206 switch (ev.type)
208 case ButtonPress:
209 case KeyPress:
210 running = False;
211 case Expose:
212 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
213 break;
214 default:
215 break;
219 simplewindow_delete(sw);
220 XCloseDisplay(disp);
222 return EXIT_SUCCESS;
225 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80