Use mgLib's `msg' box rather than doing things the hard way.
[xtoys] / xshutdown.c
1 /* -*-c-*-
2 *
3 * $Id: xshutdown.c,v 1.6 1998/12/11 09:51:51 mdw Exp $
4 *
5 * Pretty GTK interface to waking up an xwait
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the Edgeware X tools collection.
13 *
14 * X tools is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * X tools is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with X tools; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: xshutdown.c,v $
32 * Revision 1.6 1998/12/11 09:51:51 mdw
33 * Use mgLib's `msg' box rather than doing things the hard way.
34 *
35 * Revision 1.5 1998/12/03 01:00:19 mdw
36 * Honour escape presses in the dialogue boxes.
37 *
38 * Revision 1.4 1998/12/03 00:39:45 mdw
39 * Force focus when starting up.
40 *
41 * Revision 1.3 1998/11/30 22:36:49 mdw
42 * Tidy up tabbing in help texts very slightly.
43 *
44 * Revision 1.2 1998/11/21 22:30:23 mdw
45 * Support GNU-style long options throughout, and introduce proper help
46 * text to all programs. Update manual pages to match.
47 *
48 * Revision 1.1 1998/11/16 23:00:49 mdw
49 * Initial versions.
50 *
51 */
52
53 /*----- Header files ------------------------------------------------------*/
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58
59 #include <X11/Xlib.h>
60 #include <X11/Xutil.h>
61
62 #include <gtk/gtk.h>
63 #include <gdk/gdkprivate.h>
64 #include <gdk/gdkkeysyms.h>
65
66 #include <mLib/mdwopt.h>
67 #include <mLib/quis.h>
68
69 #include <mgLib/msg.h>
70
71 #include "xwait.h"
72
73 /*----- Static variables --------------------------------------------------*/
74
75 static char *atom = XWAIT_DIE;
76 static char *xmsg = XWAIT_DIE_MSG;
77
78 static Atom xwait_die;
79
80 /*----- Main code ---------------------------------------------------------*/
81
82 /* --- @version@ --- */
83
84 static void version(FILE *fp)
85 {
86 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
87 }
88
89 /* --- @usage@ --- */
90
91 static void usage(FILE *fp)
92 {
93 fprintf(fp, "Usage: %s [-a ATOM] [-m MSG] [-p PROMPT] [-t TITLE]\n", QUIS);
94 }
95
96 /* --- @main@ --- *
97 *
98 * Main program.
99 */
100
101 int main(int argc, char *argv[])
102 {
103 char *prompt = "Are you sure you want to shut down this session?";
104 char *title = "xshutdown";
105 ego(argv[0]);
106 gtk_init(&argc, &argv);
107
108 /* --- Parse options --- */
109
110 for (;;) {
111 static struct option opt[] = {
112 { "help", 0, 0, 'h' },
113 { "usage", 0, 0, 'u' },
114 { "version", 0, 0, 'v' },
115 { "atom", required_argument, 0, 'a' },
116 { "msg", required_argument, 0, 'm' },
117 { "prompt", required_argument, 0, 'p' },
118 { "title", required_argument, 0, 't' },
119 { 0, 0, 0, 0 }
120 };
121 int i;
122
123 i = getopt_long(argc, argv, "huv a:m:p:t:", opt, 0);
124
125 if (i < 0)
126 break;
127
128 switch (i) {
129 case 'h':
130 version(stdout);
131 fputs("\n", stdout);
132 usage(stdout);
133 fputs(
134 "\n"
135 "Kills a waiting `xwait' process. Pops up a confirmation window first.\n"
136 "\n"
137 "Options available are:\n"
138 "\n"
139 "-h, --help Display this help text\n"
140 "-u, --usage Display a short usage summary\n"
141 "-v, --version Display the program's version number\n"
142 "\n"
143 "-a, --atom=ATOM\t Select the atom that `xwait' is waiting for\n"
144 "-m, --msg=MSG Select the message to send to `xwait'\n"
145 "-p, --prompt=PROMPT Select the prompt string in the confirmation box\n"
146 "-t, --title=TITLE Select the title string in the confirmation box\n",
147 stdout);
148 exit(0);
149 break;
150 case 'u':
151 usage(stdout);
152 exit(0);
153 break;
154 case 'v':
155 version(stdout);
156 exit(0);
157 break;
158
159 case 'a':
160 atom = optarg;
161 break;
162 case 'm':
163 xmsg = optarg;
164 break;
165 case 'p':
166 prompt = optarg;
167 break;
168 case 't':
169 title = optarg;
170 break;
171 default:
172 usage(stderr);
173 exit(EXIT_FAILURE);
174 }
175 }
176
177 xwait_die = XInternAtom(gdk_display, atom, False);
178
179 /* --- Decide whether there's an xwait listening --- *
180 *
181 * If not, pop up an error box and quit.
182 */
183
184 {
185 XTextProperty prop;
186
187 if (!XGetTextProperty(gdk_display, DefaultRootWindow(gdk_display),
188 &prop, xwait_die)) {
189 msg("!:~OK", "no xwait listening for `%s'", atom);
190 exit(EXIT_FAILURE);
191 }
192 }
193
194 /* --- Main code --- */
195
196 if (msg("!:OK,~Cancel", "%s", prompt) == 0) {
197 XTextProperty prop;
198 XStringListToTextProperty(&xmsg, 1, &prop);
199 XSetTextProperty(gdk_display, DefaultRootWindow(gdk_display),
200 &prop, xwait_die);
201 }
202
203 return (0);
204 }
205
206 /*----- That's all, folks -------------------------------------------------*/