chiark / gitweb /
debian: Fix maintainer email address.
[xtoys] / xshutdown.c
1 /* -*-c-*-
2  *
3  * $Id: xshutdown.c,v 1.9 2004/04/08 01:36:29 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 /*----- Header files ------------------------------------------------------*/
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <X11/Xlib.h>
36 #include <X11/Xutil.h>
37
38 #include <gtk/gtk.h>
39 #include <gdk/gdkprivate.h>
40 #include <gdk/gdkkeysyms.h>
41
42 #include <mLib/mdwopt.h>
43 #include <mLib/quis.h>
44 #include <mLib/report.h>
45
46 #include <mgLib/msg.h>
47
48 #include "xatom.h"
49 #include "xwait.h"
50
51 /*----- Main code ---------------------------------------------------------*/
52
53 /* --- @version@ --- */
54
55 static void version(FILE *fp)
56 {
57   fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
58 }
59
60 /* --- @usage@ --- */
61
62 static void usage(FILE *fp)
63 {
64   fprintf(fp, "Usage: %s [-p PROMPT] [-t TITLE] [ATOM:MSG]\n", QUIS);
65 }
66
67 /* --- @main@ --- *
68  *
69  * Main program.
70  */
71
72 int main(int argc, char *argv[])
73 {
74   const char *atom = XWAIT_DIE;
75   const char *xmsg = XWAIT_DIE_MSG;
76   Atom xa, xm;
77
78   const char *prompt = "Are you sure you want to shut down this session?";
79   const char *title;
80   ego(argv[0]);
81   gtk_init(&argc, &argv);
82
83   /* --- Parse options --- */
84
85   title = QUIS;
86   for (;;) {
87     static struct option opt[] = {
88       { "help",         0,              0,      'h' },
89       { "usage",        0,              0,      'u' },
90       { "version",      0,              0,      'v' },
91       { "atom",         OPTF_ARGREQ,    0,      'a' },
92       { "msg",          OPTF_ARGREQ,    0,      'm' },
93       { "prompt",       OPTF_ARGREQ,    0,      'p' },
94       { "title",        OPTF_ARGREQ,    0,      't' },
95       { 0,              0,              0,      0 }
96     };
97     int i;
98
99     i = getopt_long(argc, argv, "huv a:m:p:t:", opt, 0);
100
101     if (i < 0)
102       break;
103
104     switch (i) {
105       case 'h':
106         version(stdout);
107         fputs("\n", stdout);
108         usage(stdout);
109         fputs(
110 "\n"
111 "Kills a waiting `xwait' process.  Pops up a confirmation window first.\n"
112 "\n"
113 "Options available are:\n"
114 "\n"
115 "-h, --help             Display this help text\n"
116 "-u, --usage            Display a short usage summary\n"
117 "-v, --version          Display the program's version number\n"
118 "\n"
119 "-a, --atom=ATOM\t      Select the property name atom [deprecated]\n"
120 "-m, --msg=MSG          Select the message to send [deprecated]\n"
121 "-p, --prompt=PROMPT    Select the prompt string in the confirmation box\n"
122 "-t, --title=TITLE      Select the title string in the confirmation box\n",
123           stdout);
124         exit(0);
125         break;
126       case 'u':
127         usage(stdout);
128         exit(0);
129         break;
130       case 'v':
131         version(stdout);
132         exit(0);
133         break;
134         
135       case 'a':
136         atom = optarg;
137         break;
138       case 'm':
139         xmsg = optarg;
140         break;
141       case 'p':
142         prompt = optarg;
143         break;
144       case 't':
145         title = optarg;
146         break;
147       default:
148         usage(stderr);
149         exit(EXIT_FAILURE);
150     }
151   }
152
153   if (optind < argc) {
154     char *p;
155     if ((atom = strtok(argv[optind++], ":")) == 0)
156       die(EXIT_FAILURE, "missing atom name");
157     if ((p = strtok(0, ",")) != 0)
158       xmsg = p;
159   }
160
161   if (optind < argc) {
162     usage(stderr);
163     exit(EXIT_FAILURE);
164   }
165
166   xa = XInternAtom(gdk_display, atom, False);
167   xm = XInternAtom(gdk_display, xmsg, False);
168
169   /* --- Decide whether there's an xwait listening --- *
170    *
171    * If not, pop up an error box and quit.
172    */
173
174   if (xatom_get(gdk_display, DefaultRootWindow(gdk_display), xa) == None) {
175     msg(QUIS, "!:~OK", "no xwait listening for `%s'", atom);
176     exit(EXIT_FAILURE);
177   }
178
179   /* --- Main code --- */
180
181   if (msg(title, "!:OK,~Cancel", "%s", prompt) == 0)
182     xatom_set(gdk_display, DefaultRootWindow(gdk_display), xa, xm);
183
184   return (0);
185 }
186
187 /*----- That's all, folks -------------------------------------------------*/