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