Change build structure a bit: put common code in a library.
[xtoys] / xshutdown.c
1 /* -*-c-*-
2 *
3 * $Id: xshutdown.c,v 1.1 1998/11/16 23:00: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.1 1998/11/16 23:00:49 mdw
33 * Initial versions.
34 *
35 */
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include <X11/Xlib.h>
44 #include <X11/Xutil.h>
45
46 #include <gtk/gtk.h>
47 #include <gdk/gdkprivate.h>
48
49 #include "mdwopt.h"
50 #include "xwait.h"
51
52 /*----- Static variables --------------------------------------------------*/
53
54 static char *atom = XWAIT_DIE;
55 static char *msg = XWAIT_DIE_MSG;
56
57 static Atom xwait_die;
58
59 /*----- Main code ---------------------------------------------------------*/
60
61 /* --- @cancel@ --- *
62 *
63 * Just end the main loop.
64 */
65
66 static void cancel(GtkWidget *w, gpointer *p)
67 {
68 gtk_main_quit();
69 }
70
71 /* --- @ok@ --- *
72 *
73 * Send the xwait process a message.
74 */
75
76 static void ok(GtkWidget *w, gpointer *p)
77 {
78 XTextProperty prop;
79 XStringListToTextProperty(&msg, 1, &prop);
80 XSetTextProperty(gdk_display, DefaultRootWindow(gdk_display),
81 &prop, xwait_die);
82 gtk_main_quit();
83 }
84
85 /* --- @main@ --- *
86 *
87 * Main program.
88 */
89
90 int main(int argc, char *argv[])
91 {
92 char *prompt = "Are you sure you want to shut down this session?";
93 char *title = "xshutdown";
94 gtk_init(&argc, &argv);
95
96 /* --- Parse options --- */
97
98 for (;;) {
99 static struct option opt[] = {
100 { "atom", required_argument, 0, 'a' },
101 { "msg", required_argument, 0, 'm' },
102 { "prompt", required_argument, 0, 'p' },
103 { "title", required_argument, 0, 't' },
104 { 0, 0, 0, 0 }
105 };
106 int i;
107
108 i = getopt_long(argc, argv, "a:m:p:t:", opt, 0);
109
110 if (i < 0)
111 break;
112
113 switch (i) {
114 case 'a':
115 atom = optarg;
116 break;
117 case 'm':
118 msg = optarg;
119 break;
120 case 'p':
121 prompt = optarg;
122 break;
123 case 't':
124 title = optarg;
125 break;
126 default:
127 exit(EXIT_FAILURE);
128 }
129 }
130
131 xwait_die = XInternAtom(gdk_display, atom, False);
132
133 /* --- Decide whether there's an xwait listening --- *
134 *
135 * If not, pop up an error box and quit.
136 */
137
138 {
139 XTextProperty prop;
140
141 if (!XGetTextProperty(gdk_display, DefaultRootWindow(gdk_display),
142 &prop, xwait_die)) {
143 char buf[64];
144 GtkWidget *win = gtk_dialog_new();
145 GtkWidget *w;
146
147 /* --- Make the main window --- */
148
149 gtk_window_set_title(GTK_WINDOW(win), "xshutdown");
150 gtk_signal_connect(GTK_OBJECT(win), "destroy",
151 GTK_SIGNAL_FUNC(cancel), 0);
152
153 gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(win)->action_area), 0);
154
155 /* --- Make the label --- */
156
157 sprintf(buf, "no xwait listening for `%s'\n", atom);
158 w = gtk_label_new(buf);
159 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(win)->vbox), w, 1, 1, 0);
160 gtk_misc_set_padding(GTK_MISC(w), 8, 8);
161 gtk_widget_show(w);
162
163 /* --- Make the little button --- */
164
165 w = gtk_button_new_with_label("OK");
166 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win)->action_area), w, 0, 0, 0);
167 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
168 gtk_widget_grab_default(w);
169 gtk_signal_connect(GTK_OBJECT(w), "clicked",
170 GTK_SIGNAL_FUNC(cancel), 0);
171 gtk_widget_show(w);
172
173 /* --- Make everything work --- */
174
175 gtk_widget_show(win);
176 gtk_main();
177 exit(EXIT_FAILURE);
178 }
179 }
180
181 /* --- Main code --- */
182
183 {
184 GtkWidget *win;
185 GtkWidget *w;
186
187 /* --- Make the main dialogue box --- */
188
189 win = gtk_dialog_new();
190 gtk_window_set_title(GTK_WINDOW(win), title);
191 gtk_signal_connect(GTK_OBJECT(win), "destroy",
192 GTK_SIGNAL_FUNC(cancel), 0);
193
194 gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(win)->action_area), 0);
195
196 /* --- Make the prompt label --- */
197
198 w = gtk_label_new(prompt);
199 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(win)->vbox), w, 1, 1, 0);
200 gtk_misc_set_padding(GTK_MISC(w), 8, 8);
201 gtk_widget_show(w);
202
203 /* --- Make the OK button --- */
204
205 w = gtk_button_new_with_label("OK");
206 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win)->action_area), w, 0, 0, 0);
207 gtk_signal_connect(GTK_OBJECT(w), "clicked", GTK_SIGNAL_FUNC(ok), 0);
208 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
209 gtk_widget_show(w);
210 gtk_widget_grab_default(w);
211
212 /* --- And the cancel button --- */
213
214 w = gtk_button_new_with_label("Cancel");
215 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win)->action_area), w, 0, 0, 0);
216 gtk_signal_connect(GTK_OBJECT(w), "clicked", GTK_SIGNAL_FUNC(cancel), 0);
217 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
218 gtk_widget_show(w);
219
220 /* --- Show the completed window --- */
221
222 gtk_widget_show(win);
223 }
224
225 /* --- Let rip --- */
226
227 gtk_main();
228 return (0);
229 }
230
231 /*----- That's all, folks -------------------------------------------------*/