File: root.c

package info (click to toggle)
xli 1.17.0+20061110-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,540 kB
  • sloc: ansic: 25,840; makefile: 11
file content (228 lines) | stat: -rw-r--r-- 7,001 bytes parent folder | download
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
/* root.c:
 *
 * this loads an image onto the root window.  changes to allow proper
 * freeing of previously allocated resources made by Deron Dann Johnson
 * (dj@eng.sun.com).
 *
 * jim frost 10.03.89
 *
 * Copyright 1989, 1990, 1991 Jim Frost.
 * See included file "copyright.h" for complete copyright information.
 */

#include "copyright.h"
#include "xli.h"
#include <string.h>

#define RETAIN_PROP_NAME	"_XSETROOT_ID"

/* Sets the close-down mode of the client to 'RetainPermanent'
 * so all client resources will be preserved after the client
 * exits.  Puts a property on the default root window containing
 * an XID of the client so that the resources can later be killed.
 */

static void preserveResource(Display * dpy, Window w)
{
	/* create dummy resource */
	Pixmap pm = XCreatePixmap(dpy, w, 1, 1, 1);
        unsigned char *data = (unsigned char *) &pm;

        /* intern the property name */
        char *name = RETAIN_PROP_NAME;
        Atom atom = XInternAtom(dpy, name, 0);

        /* create or replace the property */
        XChangeProperty(dpy, w, atom, XA_PIXMAP, 32, PropModeReplace,
                        data, sizeof(Pixmap)/4);

	/* retain all client resources until explicitly killed */
	XSetCloseDownMode(dpy, RetainPermanent);
}


/* Flushes any resources previously retained by the client,
 * if any exist.
 */

static void freePrevious(Display * dpy, Window w)
{
	Pixmap *pm;
        unsigned char *charpm;
        Atom actual_type;
	int format;
        unsigned long nitems;
        unsigned long bytes_after;
        int returncode;

        /* intern the property name */
	Atom atom = XInternAtom(dpy, RETAIN_PROP_NAME, 0);

	/* look for existing resource allocation */
        nitems = sizeof(Pixmap)/4;
        returncode = XGetWindowProperty(dpy, w, atom,
                                        0, nitems, 1/*delete*/,
                                        XA_PIXMAP, &actual_type,
                                        &format, &nitems,
                                        &bytes_after, &charpm);
        if (returncode != Success) {
            if (globals.verbose)
                fprintf(stderr, "failed to look for %s with return code %i.\n",
                        RETAIN_PROP_NAME, returncode);
            return;
        }

        /* Check if the property was found */
        if (actual_type == None) {
            if (globals.verbose)
                fprintf(stderr, "didn't find evidence of prior run.\n");
            return;
        }

        /* Make sure the dummy value is still present */
        if (actual_type != XA_PIXMAP) {
            if (globals.verbose)
                fprintf(stderr, "found wrong data type - skipped.\n");
            return;
        }

        /* Check size, in case we're a different architecture */
        if ((nitems != sizeof(Pixmap)/4) ||
            (format != 32) ||
            (bytes_after != 0)) {
            if (globals.verbose)
                fprintf(stderr, "saw wrong %li / word size %i / architecture %li.\n",
                        bytes_after, format, nitems);
            return;
        }

        /* blast it away */
        pm = (Pixmap*) charpm;
        XKillClient(dpy, (XID) *pm);
        XFree(charpm);
}

/* this returns the root window for DECWindows servers.  this is right
 * out of xsetroot with minor formatting changes.
 * (Later DEC OSF/1 releases don't need this stuff)
 */

static Window getWmRootWindow(Display * dpy, Window root)
{
	Window parent, retv;
	Window *child;
	unsigned int nchildren;
	XWindowAttributes rootatt, childatt;

	retv = None;
	if (!XGetWindowAttributes(dpy, root, &rootatt)) {
		fprintf(stderr, "XGetWindowAttributes on root failed.\n");
		exit(1);
	}
	if (XQueryTree(dpy, root, &root, &parent, &child, &nchildren)) {
		int i;
		for (i = 0; i < nchildren; i++) {
			if (!XGetWindowAttributes(dpy, child[i], &childatt)) {
				XFree((caddr_t) child);
				fprintf(stderr, "XGetWindowAttributes on child failed.\n");
				exit(1);
			}
			if ((rootatt.width == childatt.width) &&
					(rootatt.height == childatt.height))
				retv = child[i];
		}
		XFree((caddr_t) child);
		return retv;
	} else {
		fprintf(stderr, "XQueryTree failed (window doesn't exist).\n");
		exit(1);
	}
}

static Window getDECRootWindow(Display * dpy, Window root)
{
	Window temporary_rootW;

	temporary_rootW = getWmRootWindow(dpy, root);
	return (getWmRootWindow(dpy, temporary_rootW));
}

void imageOnRoot(DisplayInfo * dinfo, Image * image, ImageOptions * options)
{
	Display *disp = dinfo->disp;
	int scrn = dinfo->scrn;
	Pixmap pixmap;
	XImageInfo *ximageinfo;
	Atom __SWM_VROOT = None;
	Window root, rootReturn, parentReturn, *children;
	unsigned int numChildren;
	int i;
	char *s;

	if (globals.dest_window)
		root = globals.dest_window;
	else {
		root = RootWindow(disp, scrn);

		/* look for DECWindows servers because they do strange stuff with the
		 * root window.  they should be shot for that one.
		 * (Later DEC OSF/1 releases don't need this stuff)
		 */

		for (s = ServerVendor(disp); *s; s++)
			if (!strncmp(s, "DECWINDOWS", 10) &&
					!xlistrstr(s, "OSF/1"))
				root = getDECRootWindow(disp, root);

		/* Added for window managers like swm and tvtwm that follow
		 * solbourne's virtual root window concept.
		 */

		__SWM_VROOT = XInternAtom(disp, "__SWM_VROOT", FALSE);
		XQueryTree(disp, root, &rootReturn, &parentReturn, &children,
			&numChildren);
		for (i = 0; i < numChildren; i++) {
			Atom actual_type;
			int actual_format;
                        unsigned long nitems, bytesafter;
                        unsigned char *newRoot = 0;

                        if ((XGetWindowProperty (disp, children[i], __SWM_VROOT,0,1,
                                                 False, XA_WINDOW,
                                                 &actual_type, &actual_format,
                                                 &nitems, &bytesafter, &newRoot)
                             == Success) &&
                            newRoot) {
                            root = *((Window*) newRoot);
                            break;
			}
		}
		XFree((caddr_t) children);
	}
        freePrevious(disp, root);

	if (!(ximageinfo = imageToXImage(disp, scrn,
			DefaultVisual(disp, scrn),
			DefaultDepth(disp, scrn),
			image, FALSE, TRUE, options))) {
		fprintf(stderr, "Cannot convert Image to XImage\n");
		exit(1);
	}
	if ((pixmap = ximageToPixmap(disp, root, ximageinfo)) == None) {
		printf("Cannot create background (not enough resources, sorry)\n");
		exit(1);
	}

	/* changing the root colormap is A Bad Thing, so deny it. */
	if (ximageinfo->cmap != DefaultColormap(disp, scrn)) {
		printf("Loading image onto root would change default colormap (sorry)\n");
		XFreePixmap(disp, pixmap);
		exit(1);
	}
	XSetWindowBackgroundPixmap(disp, root, pixmap);
	XClearWindow(disp, root);
	XFreePixmap(disp, pixmap);
	ximageinfo->rootimage = TRUE;	/* make sure colors arn't freed */
	freeXImage(image, ximageinfo);
	preserveResource(disp, root);
}