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