1/*
2 * Copyright 2001,2002 Red Hat Inc., Durham, North Carolina.
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation on the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28/*
29 * Authors:
30 *   Rickard E. (Rik) Faith <faith@redhat.com>
31 *
32 */
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <X11/Xlib.h>
37#include <X11/extensions/dmxext.h>
38
39static void indent(int level)
40{
41    int i;
42    for (i = 0; i < level; i++) printf("    ");
43}
44
45static void print_window_id(const char *displayName, Display *display,
46                            Window window, int level, int child)
47{
48    char                 *name;
49
50    if (!XFetchName(display, window, &name)) name = NULL;
51    indent(level);
52    if (child) printf("(%d) ", child);
53    printf("%s window 0x%08lx: %s%s\n",
54           displayName,
55           (long unsigned)window,
56           name ? name : "",
57           (window == DefaultRootWindow(display))
58           ? " (DMX root window)" : "");
59    if (name) XFree(name);
60}
61
62static void print_info(Display *display, Window window, int level, int child)
63{
64    DMXWindowAttributes winfo[128];
65    int                 count;
66    int                 i;
67
68    if (!DMXGetWindowAttributes(display, window, &count, 128, winfo)) {
69        printf("Could not get window information for 0x%08lx\n",
70               (long unsigned)window);
71        exit(-2);
72    }
73    printf("\n");
74    print_window_id("DMX", display, window, level, child);
75    for (i = 0; i < count; i++) {
76        DMXScreenAttributes  sinfo;
77        Display              *backend;
78
79        /* This could also be cached -- the information doesn't change. */
80        if (!DMXGetScreenAttributes(display, winfo[i].screen, &sinfo)) {
81            printf("Could not get screen information for screen %d\n", i);
82            exit(-2);
83        }
84        if (!(backend = XOpenDisplay(sinfo.displayName))) {
85            printf("Cannot open backend display %s\n", sinfo.displayName);
86            exit(-2);
87        }
88        XCloseDisplay(backend);
89
90        indent(level+1);
91        printf("%s window 0x%08lx: %dx%d%+d%+d",
92               sinfo.displayName,
93               (long unsigned)winfo[i].window,
94               winfo[i].pos.width, winfo[i].pos.height,
95               winfo[i].pos.x, winfo[i].pos.y);
96        if (!winfo[i].vis.width
97            && !winfo[i].vis.height
98            && !winfo[i].vis.x
99            && !winfo[i].vis.y) printf(" not visible\n");
100        else if (winfo[i].vis.width == winfo[i].pos.width
101                 && winfo[i].vis.height == winfo[i].pos.height) {
102            printf( " %+d%+d\n", winfo[i].vis.x, winfo[i].vis.y);
103        } else {
104            printf( " %dx%d%+d%+d\n",
105                    winfo[i].vis.width, winfo[i].vis.height,
106                    winfo[i].vis.x, winfo[i].vis.y);
107        }
108    }
109}
110
111static void print_tree(Display *display, Window window, int level, int child)
112{
113    Window       root, parent;
114    Window       *list;
115    unsigned int count;
116    unsigned int i;
117
118    print_info(display, window, level, child);
119
120    if (!XQueryTree(display, window, &root, &parent, &list, &count)) {
121        printf("Cannot query window tree for 0x%08lx\n",
122               (long unsigned)window);
123        exit(-3);
124    }
125
126    if (count) {
127        indent(level+1);
128        printf("%d child%s:\n", count, count > 1 ? "ren" : "");
129        for (i = 0; i < count; i++) {
130            print_tree(display, list[i], level+1, i+1);
131        }
132    }
133}
134
135static const char *core(DMXInputAttributes *iinfo)
136{
137    if (iinfo->isCore)         return "core";
138    else if (iinfo->sendsCore) return "extension (sends core)";
139    else                       return "extension";
140}
141
142int main(int argc, char **argv)
143{
144    Display              *display = NULL;
145    Window               window   = 0;
146    int                  event_base;
147    int                  error_base;
148    int                  major_version, minor_version, patch_version;
149    DMXScreenAttributes  sinfo;
150    DMXInputAttributes   iinfo;
151    int                  count;
152    int                  i;
153
154    if (argc == 2 || argc == 3) {
155        if (!(display = XOpenDisplay(argv[1]))) {
156            printf("Cannot open display %s\n", argv[1]);
157            return -1;
158        }
159        if (argc == 3) window = strtol(argv[2], NULL, 0);
160    } else {
161        printf("Usage: %s display [windowid]\n", argv[0]);
162        return -1;
163    }
164
165    if (!display && !(display = XOpenDisplay(NULL))) {
166        printf("Cannot open default display\n");
167        return -1;
168    }
169
170    if (!DMXQueryExtension(display, &event_base, &error_base)) {
171        printf("DMX extension not present\n");
172        return -1;
173    }
174    printf("DMX extension present: event_base = %d, error_base = %d\n",
175           event_base, error_base);
176
177    if (!DMXQueryVersion(display,
178                         &major_version, &minor_version, &patch_version)) {
179        printf("Could not get extension version\n");
180        return -1;
181    }
182    printf("Extension version: %d.%d patch %d\n",
183           major_version, minor_version, patch_version);
184
185    if (!DMXGetScreenCount(display, &count)) {
186        printf("Could not get screen count\n");
187        return -1;
188    }
189    printf("Screen count = %d\n", count);
190
191    for (i = 0; i < count; i++) {
192        if (!DMXGetScreenAttributes(display, i, &sinfo)) {
193            printf("Could not get screen information for %d\n", i);
194            return -1;
195        }
196        printf("%d: %s %ux%u+%d+%d %d @%dx%d (root: %dx%d%+d%+d)\n",
197               i, sinfo.displayName,
198               sinfo.screenWindowWidth, sinfo.screenWindowHeight,
199               sinfo.screenWindowXoffset, sinfo.screenWindowYoffset,
200               sinfo.logicalScreen,
201               sinfo.rootWindowXorigin, sinfo.rootWindowYorigin,
202               sinfo.rootWindowWidth, sinfo.rootWindowHeight,
203               sinfo.rootWindowXoffset, sinfo.rootWindowYoffset);
204    }
205
206    if (major_version == 1 && minor_version >= 1) {
207        if (!DMXGetInputCount(display, &count)) {
208            printf("Could not get input count\n");
209            return -1;
210        }
211        printf("Input count = %d\n", count);
212        for (i = 0; i < count; i++) {
213            if (!DMXGetInputAttributes(display, i, &iinfo)) {
214                printf("Could not get input information for id %d\n", i);
215                return -1;
216            }
217            switch (iinfo.inputType) {
218            case DMXLocalInputType:
219                printf("  %2d local   %-20.20s %s\n", i, "", core(&iinfo));
220                break;
221            case DMXConsoleInputType:
222                printf("  %2d console %-20.20s %s\n",
223                       i, iinfo.name, core(&iinfo));
224                break;
225            case DMXBackendInputType:
226                printf("  %2d backend %-20.20s id=%2d screen=%2d %s\n",
227                       i, iinfo.name, iinfo.physicalId, iinfo.physicalScreen,
228                       core(&iinfo));
229                break;
230            }
231        }
232    }
233
234    if (window) print_info(display, window, 0, 0);
235    else        print_tree(display, DefaultRootWindow(display), 0, 0);
236
237    XCloseDisplay(display);
238    return 0;
239}
240