Force focus when starting up.
[xtoys] / xshutdown.c
1 /* -*-c-*-
2 *
3 * $Id: xshutdown.c,v 1.4 1998/12/03 00:39:45 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.4 1998/12/03 00:39:45 mdw
33 * Force focus when starting up.
34 *
35 * Revision 1.3 1998/11/30 22:36:49 mdw
36 * Tidy up tabbing in help texts very slightly.
37 *
38 * Revision 1.2 1998/11/21 22:30:23 mdw
39 * Support GNU-style long options throughout, and introduce proper help
40 * text to all programs. Update manual pages to match.
41 *
42 * Revision 1.1 1998/11/16 23:00:49 mdw
43 * Initial versions.
44 *
45 */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include <X11/Xlib.h>
54 #include <X11/Xutil.h>
55
56 #include <gtk/gtk.h>
57 #include <gdk/gdkprivate.h>
58
59 #include "mdwfocus.h"
60 #include "mdwopt.h"
61 #include "quis.h"
62 #include "xwait.h"
63
64 /*----- Static variables --------------------------------------------------*/
65
66 static char *atom = XWAIT_DIE;
67 static char *msg = XWAIT_DIE_MSG;
68
69 static Atom xwait_die;
70
71 /*----- Main code ---------------------------------------------------------*/
72
73 /* --- @cancel@ --- *
74 *
75 * Just end the main loop.
76 */
77
78 static void cancel(GtkWidget *w, gpointer *p)
79 {
80 gtk_main_quit();
81 }
82
83 /* --- @ok@ --- *
84 *
85 * Send the xwait process a message.
86 */
87
88 static void ok(GtkWidget *w, gpointer *p)
89 {
90 XTextProperty prop;
91 XStringListToTextProperty(&msg, 1, &prop);
92 XSetTextProperty(gdk_display, DefaultRootWindow(gdk_display),
93 &prop, xwait_die);
94 gtk_main_quit();
95 }
96
97 /* --- @version@ --- */
98
99 static void version(FILE *fp)
100 {
101 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
102 }
103
104 /* --- @usage@ --- */
105
106 static void usage(FILE *fp)
107 {
108 fprintf(fp, "Usage: %s [-a ATOM] [-m MSG] [-p PROMPT] [-t TITLE]\n", QUIS);
109 }
110
111 /* --- @main@ --- *
112 *
113 * Main program.
114 */
115
116 int main(int argc, char *argv[])
117 {
118 char *prompt = "Are you sure you want to shut down this session?";
119 char *title = "xshutdown";
120 ego(argv[0]);
121 gtk_init(&argc, &argv);
122
123 /* --- Parse options --- */
124
125 for (;;) {
126 static struct option opt[] = {
127 { "help", 0, 0, 'h' },
128 { "usage", 0, 0, 'u' },
129 { "version", 0, 0, 'v' },
130 { "atom", required_argument, 0, 'a' },
131 { "msg", required_argument, 0, 'm' },
132 { "prompt", required_argument, 0, 'p' },
133 { "title", required_argument, 0, 't' },
134 { 0, 0, 0, 0 }
135 };
136 int i;
137
138 i = getopt_long(argc, argv, "huv a:m:p:t:", opt, 0);
139
140 if (i < 0)
141 break;
142
143 switch (i) {
144 case 'h':
145 version(stdout);
146 fputs("\n", stdout);
147 usage(stdout);
148 fputs(
149 "\n"
150 "Kills a waiting `xwait' process. Pops up a confirmation window first.\n"
151 "\n"
152 "Options available are:\n"
153 "\n"
154 "-h, --help Display this help text\n"
155 "-u, --usage Display a short usage summary\n"
156 "-v, --version Display the program's version number\n"
157 "\n"
158 "-a, --atom=ATOM\t Select the atom that `xwait' is waiting for\n"
159 "-m, --msg=MSG Select the message to send to `xwait'\n"
160 "-p, --prompt=PROMPT Select the prompt string in the confirmation box\n"
161 "-t, --title=TITLE Select the title string in the confirmation box\n",
162 stdout);
163 exit(0);
164 break;
165 case 'u':
166 usage(stdout);
167 exit(0);
168 break;
169 case 'v':
170 version(stdout);
171 exit(0);
172 break;
173
174 case 'a':
175 atom = optarg;
176 break;
177 case 'm':
178 msg = optarg;
179 break;
180 case 'p':
181 prompt = optarg;
182 break;
183 case 't':
184 title = optarg;
185 break;
186 default:
187 usage(stderr);
188 exit(EXIT_FAILURE);
189 }
190 }
191
192 xwait_die = XInternAtom(gdk_display, atom, False);
193
194 /* --- Decide whether there's an xwait listening --- *
195 *
196 * If not, pop up an error box and quit.
197 */
198
199 {
200 XTextProperty prop;
201
202 if (!XGetTextProperty(gdk_display, DefaultRootWindow(gdk_display),
203 &prop, xwait_die)) {
204 char buf[64];
205 GtkWidget *win = gtk_dialog_new();
206 GtkWidget *w;
207
208 /* --- Make the main window --- */
209
210 gtk_window_set_title(GTK_WINDOW(win), "xshutdown");
211 gtk_signal_connect(GTK_OBJECT(win), "destroy",
212 GTK_SIGNAL_FUNC(cancel), 0);
213
214 gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(win)->action_area), 0);
215
216 /* --- Make the label --- */
217
218 sprintf(buf, "no xwait listening for `%s'\n", atom);
219 w = gtk_label_new(buf);
220 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(win)->vbox), w, 1, 1, 0);
221 gtk_misc_set_padding(GTK_MISC(w), 8, 8);
222 gtk_widget_show(w);
223
224 /* --- Make the little button --- */
225
226 w = gtk_button_new_with_label("OK");
227 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win)->action_area), w, 0, 0, 0);
228 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
229 gtk_widget_grab_default(w);
230 gtk_signal_connect(GTK_OBJECT(w), "clicked",
231 GTK_SIGNAL_FUNC(cancel), 0);
232 gtk_widget_show(w);
233
234 /* --- Make everything work --- */
235
236 gtk_widget_realize(win);
237 mdwfocus(win);
238 gtk_widget_show(win);
239 gtk_main();
240 exit(EXIT_FAILURE);
241 }
242 }
243
244 /* --- Main code --- */
245
246 {
247 GtkWidget *win;
248 GtkWidget *w;
249
250 /* --- Make the main dialogue box --- */
251
252 win = gtk_dialog_new();
253 gtk_window_set_title(GTK_WINDOW(win), title);
254 gtk_signal_connect(GTK_OBJECT(win), "destroy",
255 GTK_SIGNAL_FUNC(cancel), 0);
256
257 gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(win)->action_area), 0);
258
259 /* --- Make the prompt label --- */
260
261 w = gtk_label_new(prompt);
262 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(win)->vbox), w, 1, 1, 0);
263 gtk_misc_set_padding(GTK_MISC(w), 8, 8);
264 gtk_widget_show(w);
265
266 /* --- Make the OK button --- */
267
268 w = gtk_button_new_with_label("OK");
269 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win)->action_area), w, 0, 0, 0);
270 gtk_signal_connect(GTK_OBJECT(w), "clicked", GTK_SIGNAL_FUNC(ok), 0);
271 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
272 gtk_widget_show(w);
273 gtk_widget_grab_default(w);
274
275 /* --- And the cancel button --- */
276
277 w = gtk_button_new_with_label("Cancel");
278 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win)->action_area), w, 0, 0, 0);
279 gtk_signal_connect(GTK_OBJECT(w), "clicked", GTK_SIGNAL_FUNC(cancel), 0);
280 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
281 gtk_widget_show(w);
282
283 /* --- Show the completed window --- */
284
285 gtk_widget_realize(win);
286 mdwfocus(win);
287 gtk_widget_show(win);
288 }
289
290 /* --- Let rip --- */
291
292 gtk_main();
293 return (0);
294 }
295
296 /*----- That's all, folks -------------------------------------------------*/