use a boolean value
[awesome.git] / awesome-message.c
blob2e0e4757a1b3a09edf623e74db1e4a46d9dffbb5
1 /*
2 * awmessage.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 #include <unistd.h>
23 #include <stdlib.h>
24 #include <X11/Xlib.h>
26 #include "common/swindow.h"
27 #include "common/util.h"
28 #include "common/awesome-version.h"
30 static void __attribute__ ((noreturn))
31 exit_help(int exit_code)
33 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
34 fprintf(outfile, "Usage: awmessage [-x xcoord] [-y ycoord] [-f fgcolor] [-b bgcolor] <message> <icon>\n");
35 exit(exit_code);
38 int
39 main(int argc, char **argv)
41 Display *disp;
42 SimpleWindow *sw;
43 DrawCtx *ctx;
44 XColor fg, bg, c;
45 XEvent ev;
46 Bool running = True;
47 const char *fg_color = "#000000";
48 const char *bg_color = "#ffffff";
49 int opt;
50 Area geometry = { 0, 0, 200, 50 }, icon_geometry = { -1, -1, -1, -1 };
51 XftFont *font = NULL;
53 if(!(disp = XOpenDisplay(NULL)))
54 eprint("unable to open display");
56 while((opt = getopt(argc, argv, "vhf:b:x:y:n:")) != -1)
57 switch(opt)
59 case 'v':
60 eprint_version("awmessage");
61 break;
62 case 'f':
63 fg_color = a_strdup(optarg);
64 break;
65 case 'b':
66 bg_color = a_strdup(optarg);
67 break;
68 case 'x':
69 geometry.x = atoi(optarg);
70 break;
71 case 'y':
72 geometry.y = atoi(optarg);
73 break;
74 case 'h':
75 exit_help(EXIT_SUCCESS);
76 break;
77 case 'n':
78 font = XftFontOpenName(disp, DefaultScreen(disp), optarg);
79 break;
82 if(argc - optind < 1)
83 exit_help(EXIT_FAILURE);
85 if(!font)
86 font = XftFontOpenName(disp, DefaultScreen(disp), "vera-12");
88 geometry.width = draw_textwidth(disp, font, argv[optind]);
89 geometry.height = font->height * 1.5;
91 if(argc - optind >= 2)
93 icon_geometry = draw_get_image_size(argv[optind + 1]);
94 if(icon_geometry.width <= 0 || icon_geometry.height <= 0)
95 eprint("invalid image\n");
96 else
97 geometry.width += icon_geometry.width * ((double) font->height / (double) icon_geometry.height);
100 sw = simplewindow_new(disp, DefaultScreen(disp),
101 geometry.x, geometry.y, geometry.width, geometry.height, 0);
103 XStoreName(disp, sw->window, "awmessage");
105 ctx = draw_get_context(disp, DefaultScreen(disp),
106 geometry.width, geometry.height, sw->drawable);
108 /* XXX replace by initxcolor() */
109 XAllocNamedColor(disp, DefaultColormap(disp, DefaultScreen(disp)),
110 bg_color, &bg, &c);
112 XAllocNamedColor(disp, DefaultColormap(disp, DefaultScreen(disp)),
113 fg_color, &fg, &c);
115 geometry.x = geometry.y = 0;
116 draw_text(ctx, geometry, AlignRight,
117 0, font, argv[optind], fg, bg);
119 if(icon_geometry.width > 0 && icon_geometry.height > 0)
120 draw_image(ctx, 0, (geometry.height / 2) - (font->height / 2),
121 font->height,
122 argv[optind + 1]);
124 p_delete(&ctx);
126 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
128 XMapRaised(disp, sw->window);
129 XSync(disp, False);
131 while (running)
133 XNextEvent(disp, &ev);
134 switch (ev.type)
136 case ButtonPress:
137 case KeyPress:
138 running = False;
139 case Expose:
140 simplewindow_refresh_drawable(sw, DefaultScreen(disp));
141 break;
142 default:
143 break;
147 simplewindow_delete(sw);
148 XCloseDisplay(disp);
150 return EXIT_SUCCESS;
153 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80