File: xtoolwait.c

package info (click to toggle)
xtoolwait 1.3-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 132 kB
  • ctags: 573
  • sloc: makefile: 706; ansic: 258
file content (353 lines) | stat: -rw-r--r-- 10,867 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 *  Xtoolwait - wait for X client to map a window
 *  Copyright (C) 1995-1999  Richard Huveneers <richard@hekkihek.hacom.nl>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>    /* for XA_WM_STATE */
#include <X11/Xutil.h>    /* for definitions for initial window state */
#include <X11/Xmd.h>      /* for CARD32 */

#ifdef DEBUG
#   define DPRINTF(args) ((void) printf args)
#else
#   define DPRINTF(args) ((void) 0)
#endif

#define DEFAULT_TIMEOUT		15L	/* default timeout for command startup */
#define DEFAULT_MAPPINGS	1L	/* default number of mappings to wait for */
#define USAGE			"\
Usage: %s [options] command\n\
options:\n\
   -display display-name\n\
   -timeout nseconds\n\
   -mappings nwindows\n\
   -withdrawn\n\
   -pid\n\
   -help\n\
   -version\n"
#define MAINTAINER		"richard@hekkihek.hacom.nl"
#define VERSION_MAJOR		1
#define VERSION_MINOR		3

Display *dpy;
char *programname, *childname;
Atom xa_wm_state;
int (*prevxerrhandler)(Display *, XErrorEvent *);
int withdrawnok = 0;

void
timeout(signo)
    int signo;
{
    (void) fprintf(stderr, "%s: warning: timeout launching %s\n", programname, childname);
    XCloseDisplay(dpy);
    exit(1);
}

void
child_terminated(signo)
    int signo;
{
    int status;

    (void) fprintf(stderr, "%s: warning: child (%s) died prematurely\n", programname, childname);
    XCloseDisplay(dpy);
    (void) wait(&status);
    exit(WIFEXITED(status) ? WEXITSTATUS(status) : 1);
}

int
is_mapped(window)
    Window window;
{
    Atom actual_type;
    int actual_format;
    unsigned long nitems;
    unsigned long bytes_after;
    unsigned char *prop;
    int retv;
    CARD32 state;

    retv = XGetWindowProperty(dpy, window, xa_wm_state, 0L, 1L, False, xa_wm_state,
                              &actual_type, &actual_format, &nitems, &bytes_after, &prop);

    if (retv != Success)            { DPRINTF(("XGetWindowProperty failed")); return 0; }
    if (actual_type == None)        { DPRINTF(("WM_STATE undefined"));        return 0; }
    if (actual_type != xa_wm_state) { DPRINTF(("invalid WM_STATE type"));     return 0; }
    if (nitems != 1)                { DPRINTF(("invalid WM_STATE length"));   return 0; }
    if (actual_format != 32)        { DPRINTF(("invalid WM_STATE format"));   return 0; }

    state = *((CARD32 *) prop);

    switch ((int) state) {
        case WithdrawnState: DPRINTF(("WithdrawnState")); return withdrawnok;
        case NormalState:    DPRINTF(("NormalState"));    return 1;
        case IconicState:    DPRINTF(("IconicState"));    return 1;
    }

    /*
    ** We assume that the window manager knows what it is doing...
    */

    DPRINTF(("unknown WM_STATE value (%d)", (int) state));
    return 1;
}

int
xerrhandler(d, e)
    Display *d;
    XErrorEvent *e;
{
    DPRINTF(("received X error (error code %d, major opcode %d)\n", (int) e->error_code, (int) e->request_code));

    if ((int) e->error_code == BadWindow)
    {
        /*
        ** This error occurs when a window is destroyed before we got a
        ** chance to examine it.
        */
        DPRINTF(("  BadWindow error: ignored\n"));
        return 0;
    }

    return prevxerrhandler(d, e);
}

int
main(argc, argv)
    int argc;
    char **argv;
{
    int pid;
    int arg = 1;
    int printpid = 0;
    char *endptr;
    char *displayname = NULL;
    unsigned long timeouttime = DEFAULT_TIMEOUT;
    unsigned long nummappings = 1;
    XEvent event;

    programname = strrchr(argv[0], '/');
    if (programname) programname++; else programname = argv[0];

    while (1) {

        if (arg >= argc) {
            (void) fprintf(stderr, "%s: too few arguments\n", programname);
            (void) fprintf(stderr, USAGE, programname);
            return 1;
        }

        if (!strcmp(argv[arg], "-help")) {
            (void) fprintf(stdout, USAGE, programname);
            return 0;
        }

        if (!strcmp(argv[arg], "-version")) {
            (void) fprintf(stdout, "%s version %d.%d\n", programname, VERSION_MAJOR, VERSION_MINOR);
            return 0;
        }

	if (!strcmp(argv[arg], "-pid")) {
	    printpid = 1;
	    arg += 1;
	    continue;
	}

        /* the remaining options need at least one argument */
        if (arg+1 == argc) break;

        if (!strcmp(argv[arg], "-display")) {
            displayname = argv[arg+1];
            arg += 2;
            continue;
        }

        if (!strcmp(argv[arg], "-timeout")) {
            timeouttime = (unsigned long) strtol(argv[arg+1], &endptr, 0);
            if (*endptr || (endptr == argv[arg+1])) {
                (void) fprintf(stderr, "%s: invalid timeout, using default of %ld\n",
                    programname, DEFAULT_TIMEOUT);
                timeouttime = DEFAULT_TIMEOUT;
            }
            arg += 2;
            continue;
        }

        if (!strcmp(argv[arg], "-mappings")) {
            nummappings = (unsigned long) strtol(argv[arg+1], &endptr, 0);
            if (*endptr || (endptr == argv[arg+1])) {
                (void) fprintf(stderr, "%s: invalid number of mappings, using default of %ld\n",
                    programname, DEFAULT_MAPPINGS);
                nummappings = DEFAULT_MAPPINGS;
            }
            arg += 2;
            continue;
        }

	if (!strcmp(argv[arg], "-withdrawn")) {
	    withdrawnok = 1;
	    arg += 1;
	    continue;
	}
        break;
    }

    childname = argv[arg];

    prevxerrhandler = XSetErrorHandler(xerrhandler);

    dpy = XOpenDisplay(displayname);
    if (!dpy) {
        (void) fprintf(stderr, "%s: unable to open display (%s), NOT executing %s\n",
        	programname, XDisplayName(displayname), childname);
        return 1;
    }

    /*
    ** Make sure that the file descriptor is not passed to the client.
    */

    if (fcntl(ConnectionNumber(dpy), F_SETFD, 1L) == -1) {
        (void) fprintf(stderr, "%s: warning: one file descriptor unusable for ", programname);
        perror(childname);
    }
    DPRINTF(("Close on exec flag: %d\n", fcntl(ConnectionNumber(dpy), F_GETFD)));

    /*
    ** We assume that the client is not smart enough to locate the virtual root window,
    ** if there is one, so it will create its main window on the default root window.
    */

    XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureNotifyMask);

    /*
    ** We assume that the window manager provides the WM_STATE property on top-level
    ** windows, as required by ICCCM 2.0.
    ** If the window manager has not yet completed its initialisation, the WM_STATE atom
    ** might not exist, in which case we create it.
    */

#ifdef XA_WM_STATE    /* probably in X11R7 :-) */
    xa_wm_state = XA_WM_STATE;
#else
    xa_wm_state = XInternAtom(dpy, "WM_STATE", False);
#endif
#if 0
    if (xa_wm_state == None) {
        (void) fprintf(stderr, "%s: your window manager does not provide WM_STATE information.\n", programname);
        (void) fprintf(stderr, "Please report the name and version number of your window manager to %s\n", MAINTAINER);
        XCloseDisplay(dpy);
        return 1;
    }
#endif

    (void) signal(SIGALRM, timeout);
    (void) signal(SIGCHLD, child_terminated);
    (void) alarm(timeouttime);

    switch (pid = fork())
    {
    case -1:
        (void) fprintf(stderr, "%s: error forking ", programname);
        perror(childname);
	XCloseDisplay(dpy);
	return 1;
    case 0:
	if (printpid) {
	    (void) fprintf(stdout, "%u\n", getpid());
	    (void) fflush(stdout);
	    /* redirect stdout of child to stderr */
	    if (dup2(2, 1) == -1) {
		(void) fprintf(stderr, "%s: error ", programname);
		perror("redirecting stdout of child to stderr");
		return 1;
	    }
	}
	(void) execvp(argv[arg], argv + arg);
	(void) fprintf(stderr, "%s: error executing ", programname);
        perror(childname);
	return 1;
    }

    (void) signal(SIGINT, SIG_IGN);

    while (nummappings) {
        XNextEvent(dpy, &event);
        switch (event.type) {

            case CreateNotify:
                DPRINTF(("CreateNotify: %ld %ld: ", event.xcreatewindow.window, event.xcreatewindow.parent));
                if (event.xcreatewindow.send_event) {
                    DPRINTF(("send event: ignored\n"));
                    break;
                }
                if (event.xcreatewindow.override_redirect) {
                    DPRINTF(("override redirect: ignored\n"));
                    break;
                }
                XSelectInput(dpy, event.xcreatewindow.window, PropertyChangeMask);
                DPRINTF(("requested property change events\n"));

                /*
                ** BUG: PropertyNotify events generated between the CreateNotify event
                **      and the XSelectInput call are lost. It is very unlikely
                **      that the WM_STATE PropertyNotify event is generated so fast.
                **      Calling is_mapped at this point would solve this, but is
                **      not a good idea, since it delays the client startup significantly.
                */

                break;

            case PropertyNotify:
                DPRINTF(("PropertyNotify: %ld %s: ", event.xproperty.window, XGetAtomName(dpy, event.xproperty.atom)));
                if (event.xproperty.send_event) {
                    DPRINTF(("send event: ignored\n"));
                    break;
                }
                if (event.xproperty.atom != xa_wm_state) {
                    DPRINTF(("uninteresting property: ignored\n"));
                    break;
                }
                if (!is_mapped(event.xproperty.window)) {
                    DPRINTF((": ignored\n"));
                    break;
                }
                nummappings--;
                DPRINTF((": accepted\n"));
                break;

            default:
                DPRINTF(("Received uninteresting event: %d %ld: ignored\n", event.type, event.xany.window));
                break;
        }
    }

    XCloseDisplay(dpy);
    return 0;
}