xwininfo.c revision 853aa076
1863f95b1Smrg/*
2863f95b1Smrg * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
3863f95b1Smrg *
4863f95b1Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5863f95b1Smrg * copy of this software and associated documentation files (the "Software"),
6863f95b1Smrg * to deal in the Software without restriction, including without limitation
7863f95b1Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8863f95b1Smrg * and/or sell copies of the Software, and to permit persons to whom the
9863f95b1Smrg * Software is furnished to do so, subject to the following conditions:
10863f95b1Smrg *
11863f95b1Smrg * The above copyright notice and this permission notice (including the next
12863f95b1Smrg * paragraph) shall be included in all copies or substantial portions of the
13863f95b1Smrg * Software.
14863f95b1Smrg *
15863f95b1Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16863f95b1Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17863f95b1Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18863f95b1Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19863f95b1Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20863f95b1Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21863f95b1Smrg * DEALINGS IN THE SOFTWARE.
22863f95b1Smrg */
23ff7e0accSmrg/*
24ff7e0accSmrg
25ff7e0accSmrgCopyright 1987, 1998  The Open Group
26ff7e0accSmrg
27ff7e0accSmrgPermission to use, copy, modify, distribute, and sell this software and its
28ff7e0accSmrgdocumentation for any purpose is hereby granted without fee, provided that
29ff7e0accSmrgthe above copyright notice appear in all copies and that both that
30ff7e0accSmrgcopyright notice and this permission notice appear in supporting
31ff7e0accSmrgdocumentation.
32ff7e0accSmrg
33ff7e0accSmrgThe above copyright notice and this permission notice shall be included
34ff7e0accSmrgin all copies or substantial portions of the Software.
35ff7e0accSmrg
36ff7e0accSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
37ff7e0accSmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
38ff7e0accSmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
39ff7e0accSmrgOF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
40ff7e0accSmrgHOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41ff7e0accSmrgINDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42ff7e0accSmrgFROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43ff7e0accSmrgNEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44ff7e0accSmrgWITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45ff7e0accSmrg
46ff7e0accSmrgExcept as contained in this notice, the name of a copyright holder
47ff7e0accSmrgshall not be used in advertising or otherwise to promote the sale, use
48ff7e0accSmrgor other dealings in this Software without prior written authorization
49ff7e0accSmrgof the copyright holder.
50ff7e0accSmrg
51ff7e0accSmrg*/
52ff7e0accSmrg
53ff7e0accSmrg
54ff7e0accSmrg/*
55ff7e0accSmrg * xwininfo.c	- MIT Project Athena, X Window system window
56ff7e0accSmrg *		  information utility.
57ff7e0accSmrg *
58ff7e0accSmrg *
59ff7e0accSmrg *	This program will report all relevant information
60ff7e0accSmrg *	about a specific window.
61ff7e0accSmrg *
62ff7e0accSmrg *  Author:	Mark Lillibridge, MIT Project Athena
63ff7e0accSmrg *		16-Jun-87
64ff7e0accSmrg */
65ff7e0accSmrg
66ff7e0accSmrg#include "config.h"
67863f95b1Smrg
68863f95b1Smrg#include <xcb/xcb.h>
69863f95b1Smrg#include <xcb/xproto.h>
70863f95b1Smrg#ifdef USE_XCB_ICCCM
71863f95b1Smrg# include <xcb/xcb_icccm.h>
72ff7e0accSmrg#endif
73863f95b1Smrg#include <xcb/shape.h>
74863f95b1Smrg
75ff7e0accSmrg#include <stdio.h>
76ff7e0accSmrg#include <stdlib.h>
77863f95b1Smrg#include <string.h>
78863f95b1Smrg#include <locale.h>
79863f95b1Smrg#include <langinfo.h>
80863f95b1Smrg#ifdef HAVE_ICONV
81863f95b1Smrg# include <iconv.h>
82863f95b1Smrg#endif
83863f95b1Smrg#include <ctype.h>
84863f95b1Smrg#include <errno.h>
85863f95b1Smrg
86863f95b1Smrg#ifndef HAVE_STRNLEN
87863f95b1Smrg#include "strnlen.h"
88863f95b1Smrg#endif
89ff7e0accSmrg
90ff7e0accSmrg/* Include routines to handle parsing defaults */
91ff7e0accSmrg#include "dsimple.h"
92ff7e0accSmrg
93ff7e0accSmrgtypedef struct {
94863f95b1Smrg    long code;
95863f95b1Smrg    const char *name;
96ff7e0accSmrg} binding;
97ff7e0accSmrg
98863f95b1Smrg#ifndef USE_XCB_ICCCM
99863f95b1Smrg/* Once xcb-icccm's API is stable, this should be replaced by
100863f95b1Smrg   xcb_size_hints_t & xcb_size_hints_flags_t */
101863f95b1Smrgtypedef struct {
102863f95b1Smrg  /** User specified flags */
103863f95b1Smrg  uint32_t flags;
104863f95b1Smrg  /** User-specified position */
105863f95b1Smrg  int32_t x, y;
106863f95b1Smrg  /** User-specified size */
107863f95b1Smrg  int32_t width, height;
108863f95b1Smrg  /** Program-specified minimum size */
109863f95b1Smrg  int32_t min_width, min_height;
110863f95b1Smrg  /** Program-specified maximum size */
111863f95b1Smrg  int32_t max_width, max_height;
112863f95b1Smrg  /** Program-specified resize increments */
113863f95b1Smrg  int32_t width_inc, height_inc;
114863f95b1Smrg  /** Program-specified minimum aspect ratios */
115863f95b1Smrg  int32_t min_aspect_num, min_aspect_den;
116863f95b1Smrg  /** Program-specified maximum aspect ratios */
117863f95b1Smrg  int32_t max_aspect_num, max_aspect_den;
118863f95b1Smrg  /** Program-specified base size */
119863f95b1Smrg  int32_t base_width, base_height;
120863f95b1Smrg  /** Program-specified window gravity */
121863f95b1Smrg  uint32_t win_gravity;
122863f95b1Smrg} wm_size_hints_t;
123863f95b1Smrg
124863f95b1Smrg# define xcb_size_hints_t wm_size_hints_t
125863f95b1Smrg
126863f95b1Smrgtypedef struct {
127863f95b1Smrg  /** Marks which fields in this structure are defined */
128863f95b1Smrg  int32_t flags;
129863f95b1Smrg  /** Does this application rely on the window manager to get keyboard
130863f95b1Smrg      input? */
131863f95b1Smrg  uint32_t input;
132863f95b1Smrg  /** See below */
133863f95b1Smrg  int32_t initial_state;
134863f95b1Smrg  /** Pixmap to be used as icon */
135863f95b1Smrg  xcb_pixmap_t icon_pixmap;
136863f95b1Smrg  /** Window to be used as icon */
137863f95b1Smrg  xcb_window_t icon_window;
138863f95b1Smrg  /** Initial position of icon */
139863f95b1Smrg  int32_t icon_x, icon_y;
140863f95b1Smrg  /** Icon mask bitmap */
141863f95b1Smrg  xcb_pixmap_t icon_mask;
142863f95b1Smrg  /* Identifier of related window group */
143863f95b1Smrg  xcb_window_t window_group;
144863f95b1Smrg} wm_hints_t;
145863f95b1Smrg
146853aa076Smrg#define xcb_icccm_wm_hints_t wm_hints_t
147863f95b1Smrg
148863f95b1Smrgenum {
149863f95b1Smrg  /* xcb_size_hints_flags_t */
150853aa076Smrg  XCB_ICCCM_SIZE_HINT_US_POSITION = 1 << 0,
151853aa076Smrg  XCB_ICCCM_SIZE_HINT_US_SIZE = 1 << 1,
152853aa076Smrg  XCB_ICCCM_SIZE_HINT_P_POSITION = 1 << 2,
153853aa076Smrg  XCB_ICCCM_SIZE_HINT_P_SIZE = 1 << 3,
154853aa076Smrg  XCB_ICCCM_SIZE_HINT_P_MIN_SIZE = 1 << 4,
155853aa076Smrg  XCB_ICCCM_SIZE_HINT_P_MAX_SIZE = 1 << 5,
156853aa076Smrg  XCB_ICCCM_SIZE_HINT_P_RESIZE_INC = 1 << 6,
157853aa076Smrg  XCB_ICCCM_SIZE_HINT_P_ASPECT = 1 << 7,
158853aa076Smrg  XCB_ICCCM_SIZE_HINT_BASE_SIZE = 1 << 8,
159853aa076Smrg  XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY = 1 << 9,
160863f95b1Smrg  /* xcb_wm_state_t */
161853aa076Smrg  XCB_ICCCM_WM_STATE_WITHDRAWN = 0,
162853aa076Smrg  XCB_ICCCM_WM_STATE_NORMAL = 1,
163853aa076Smrg  XCB_ICCCM_WM_STATE_ICONIC = 3,
164863f95b1Smrg  /* xcb_wm_t */
165853aa076Smrg  XCB_ICCCM_WM_HINT_INPUT = (1L << 0),
166853aa076Smrg  XCB_ICCCM_WM_HINT_STATE = (1L << 1),
167853aa076Smrg  XCB_ICCCM_WM_HINT_ICON_PIXMAP = (1L << 2),
168853aa076Smrg  XCB_ICCCM_WM_HINT_ICON_WINDOW = (1L << 3),
169853aa076Smrg  XCB_ICCCM_WM_HINT_ICON_POSITION = (1L << 4),
170853aa076Smrg  XCB_ICCCM_WM_HINT_ICON_MASK = (1L << 5),
171853aa076Smrg  XCB_ICCCM_WM_HINT_WINDOW_GROUP = (1L << 6),
172853aa076Smrg  XCB_ICCCM_WM_HINT_X_URGENCY = (1L << 8)
173863f95b1Smrg};
174863f95b1Smrg
175863f95b1Smrg/* Once xcb-icccm's API is stable, these should be replaced by calls to it */
176863f95b1Smrg# define GET_TEXT_PROPERTY(Dpy, Win, Atom) \
177863f95b1Smrg    xcb_get_property (Dpy, False, Win, Atom, XCB_GET_PROPERTY_TYPE_ANY, 0, BUFSIZ)
178853aa076Smrg# define xcb_icccm_get_wm_name(Dpy, Win) \
179853aa076Smrg    GET_TEXT_PROPERTY(Dpy, Win, XCB_ATOM_WM_NAME)
180863f95b1Smrg
181853aa076Smrg# define xcb_icccm_get_wm_class(Dpy, Win) \
182863f95b1Smrg    xcb_get_property (Dpy, False, Win, XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, 0, BUFSIZ)
183853aa076Smrg# define xcb_icccm_get_wm_hints(Dpy, Win) \
184863f95b1Smrg    xcb_get_property(Dpy, False, Win, XCB_ATOM_WM_HINTS, XCB_ATOM_WM_HINTS, 0, 9)
185863f95b1Smrg
186853aa076Smrg# define xcb_icccm_get_wm_size_hints(Dpy, Win, Atom) \
187863f95b1Smrg    xcb_get_property (Dpy, False, Win, Atom, XCB_ATOM_WM_SIZE_HINTS, 0, 18)
188853aa076Smrg# define xcb_icccm_get_wm_normal_hints(Dpy, Win) \
189853aa076Smrg    xcb_icccm_get_wm_size_hints(Dpy, Win, XCB_ATOM_WM_NORMAL_HINTS)
190863f95b1Smrg#endif
191863f95b1Smrg
192863f95b1Smrg/* Possibly in xcb-emwh in the future? */
193863f95b1Smrgstatic xcb_atom_t atom_net_wm_name, atom_utf8_string;
194863f95b1Smrgstatic xcb_atom_t atom_net_wm_desktop, atom_net_wm_window_type,
195863f95b1Smrg    atom_net_wm_state, atom_net_wm_pid, atom_net_frame_extents;
196863f95b1Smrgstatic xcb_get_property_cookie_t get_net_wm_name (xcb_connection_t *,
197863f95b1Smrg						  xcb_window_t);
198863f95b1Smrg
199863f95b1Smrg/* Information we keep track of for each window to allow prefetching/reusing */
200863f95b1Smrgstruct wininfo {
201863f95b1Smrg    xcb_window_t			window;
202863f95b1Smrg
203863f95b1Smrg    /* cookies for requests we've sent */
204863f95b1Smrg    xcb_get_geometry_cookie_t		geometry_cookie;
205863f95b1Smrg    xcb_get_property_cookie_t		net_wm_name_cookie;
206863f95b1Smrg    xcb_get_property_cookie_t		wm_name_cookie;
207863f95b1Smrg    xcb_get_property_cookie_t		wm_class_cookie;
208863f95b1Smrg    xcb_translate_coordinates_cookie_t	trans_coords_cookie;
209863f95b1Smrg    xcb_query_tree_cookie_t		tree_cookie;
210863f95b1Smrg    xcb_get_window_attributes_cookie_t	attr_cookie;
211863f95b1Smrg    xcb_get_property_cookie_t		normal_hints_cookie;
212863f95b1Smrg    xcb_get_property_cookie_t		hints_cookie;
213863f95b1Smrg    xcb_get_property_cookie_t		wm_desktop_cookie;
214863f95b1Smrg    xcb_get_property_cookie_t		wm_window_type_cookie;
215863f95b1Smrg    xcb_get_property_cookie_t		wm_state_cookie;
216863f95b1Smrg    xcb_get_property_cookie_t		wm_pid_cookie;
217863f95b1Smrg    xcb_get_property_cookie_t		wm_client_machine_cookie;
218863f95b1Smrg    xcb_get_property_cookie_t		frame_extents_cookie;
219863f95b1Smrg    xcb_get_property_cookie_t		zoom_cookie;
220863f95b1Smrg
221863f95b1Smrg    /* cached results from previous requests */
222863f95b1Smrg    xcb_get_geometry_reply_t *		geometry;
223863f95b1Smrg    xcb_get_window_attributes_reply_t *	win_attributes;
224863f95b1Smrg    xcb_size_hints_t *			normal_hints;
225863f95b1Smrg};
226863f95b1Smrg
227863f95b1Smrgstatic void scale_init (xcb_screen_t *scrn);
228863f95b1Smrgstatic char *nscale (int, int, int, char *, size_t);
229863f95b1Smrgstatic char *xscale (int);
230863f95b1Smrgstatic char *yscale (int);
231863f95b1Smrgstatic char *bscale (int);
232863f95b1Smrgint main (int, char **);
233863f95b1Smrgstatic const char *LookupL (long, const binding *);
234863f95b1Smrgstatic const char *Lookup (int, const binding *);
235863f95b1Smrgstatic void Display_Window_Id (struct wininfo *, Bool);
236863f95b1Smrgstatic void Display_Stats_Info (struct wininfo *);
237863f95b1Smrgstatic void Display_Bits_Info (struct wininfo *);
238863f95b1Smrgstatic void Display_Event_Mask (long);
239863f95b1Smrgstatic void Display_Events_Info (struct wininfo *);
240863f95b1Smrgstatic void Display_Tree_Info (struct wininfo *, int);
241863f95b1Smrgstatic void display_tree_info_1 (struct wininfo *, int, int);
242863f95b1Smrgstatic void Display_Hints (xcb_size_hints_t *);
243863f95b1Smrgstatic void Display_Size_Hints (struct wininfo *);
244863f95b1Smrgstatic void Display_Window_Shape (xcb_window_t);
245863f95b1Smrgstatic void Display_WM_Info (struct wininfo *);
246863f95b1Smrgstatic void wininfo_wipe (struct wininfo *);
247863f95b1Smrg
248863f95b1Smrgstatic const char *window_id_format = "0x%lx";
249863f95b1Smrg
250863f95b1Smrg#ifdef HAVE_ICONV
251863f95b1Smrgstatic iconv_t iconv_from_utf8;
252863f95b1Smrg#endif
253863f95b1Smrgstatic const char *user_encoding;
254863f95b1Smrgstatic void print_utf8 (const char *, char *, size_t, const char *);
255863f95b1Smrgstatic void print_friendly_name (const char *, const char *, const char *);
256863f95b1Smrg
257863f95b1Smrgstatic xcb_connection_t *dpy;
258863f95b1Smrgstatic xcb_screen_t *screen;
259863f95b1Smrgstatic xcb_generic_error_t *err;
260ff7e0accSmrg
261ff7e0accSmrg#ifndef HAVE_STRLCAT
262863f95b1Smrgstatic size_t strlcat (char *dst, const char *src, size_t dstsize)
263ff7e0accSmrg{
264863f95b1Smrg    size_t sd = strlen (dst);
265863f95b1Smrg    size_t ss = strlen (src);
266ff7e0accSmrg    size_t s = sd + ss;
267863f95b1Smrg
268ff7e0accSmrg    if (s < dstsize) {
269863f95b1Smrg	strcpy (dst + sd, src);
270ff7e0accSmrg    } else {
271863f95b1Smrg	strncpy (dst + sd, src, dstsize-sd-1);
272ff7e0accSmrg	dst[dstsize] = '\0';
273ff7e0accSmrg    }
274ff7e0accSmrg    return s;
275ff7e0accSmrg}
276ff7e0accSmrg#endif
277ff7e0accSmrg
278ff7e0accSmrg/*
279ff7e0accSmrg * Report the syntax for calling xwininfo:
280ff7e0accSmrg */
281853aa076Smrgstatic void
282863f95b1Smrgusage (void)
283ff7e0accSmrg{
284ff7e0accSmrg    fprintf (stderr,
285863f95b1Smrg	     "usage:  %s [-options ...]\n\n"
286863f95b1Smrg	     "where options include:\n"
287863f95b1Smrg	     "    -help                print this message\n"
288863f95b1Smrg	     "    -display host:dpy    X server to contact\n"
289863f95b1Smrg	     "    -root                use the root window\n"
290863f95b1Smrg	     "    -id windowid         use the window with the specified id\n"
291863f95b1Smrg	     "    -name windowname     use the window with the specified name\n"
292863f95b1Smrg	     "    -int                 print window id in decimal\n"
293863f95b1Smrg	     "    -children            print parent and child identifiers\n"
294863f95b1Smrg	     "    -tree                print children identifiers recursively\n"
295863f95b1Smrg	     "    -stats               print window geometry [DEFAULT]\n"
296863f95b1Smrg	     "    -bits                print window pixel information\n"
297863f95b1Smrg	     "    -events              print events selected for on window\n"
298863f95b1Smrg	     "    -size                print size hints\n"
299863f95b1Smrg	     "    -wm                  print window manager hints\n"
300863f95b1Smrg	     "    -shape               print shape extents\n"
301863f95b1Smrg	     "    -frame               don't ignore window manager frames\n"
302863f95b1Smrg	     "    -english             print sizes in english units\n"
303863f95b1Smrg	     "    -metric              print sizes in metric units\n"
304863f95b1Smrg	     "    -all                 -tree, -stats, -bits, -events, -wm, -size, -shape\n"
305863f95b1Smrg	     "\n",
306863f95b1Smrg	     program_name);
307ff7e0accSmrg    exit (1);
308ff7e0accSmrg}
309ff7e0accSmrg
310ff7e0accSmrg/*
311ff7e0accSmrg * pixel to inch, metric converter.
312ff7e0accSmrg * Hacked in by Mark W. Eichin <eichin@athena> [eichin:19880619.1509EST]
313ff7e0accSmrg *
314ff7e0accSmrg * Simply put: replace the old numbers with string print calls.
315ff7e0accSmrg * Returning a local string is ok, since we only ever get called to
316ff7e0accSmrg * print one x and one y, so as long as they don't collide, they're
317ff7e0accSmrg * fine. This is not meant to be a general purpose routine.
318ff7e0accSmrg *
319ff7e0accSmrg */
320ff7e0accSmrg
321863f95b1Smrgstatic int xp = 0, xmm = 0;
322863f95b1Smrgstatic int yp = 0, ymm = 0;
323863f95b1Smrgstatic int bp = 0, bmm = 0;
324ff7e0accSmrgstatic int english = 0, metric = 0;
325ff7e0accSmrg
326ff7e0accSmrgstatic void
327863f95b1Smrgscale_init (xcb_screen_t *screen)
328ff7e0accSmrg{
329863f95b1Smrg    xp = screen->width_in_pixels;
330863f95b1Smrg    yp = screen->height_in_pixels;
331863f95b1Smrg    xmm = screen->width_in_millimeters;
332863f95b1Smrg    ymm = screen->height_in_millimeters;
333863f95b1Smrg    bp  = xp  + yp;
334863f95b1Smrg    bmm = xmm + ymm;
335ff7e0accSmrg}
336ff7e0accSmrg
337ff7e0accSmrg#define MILE (5280*12)
338ff7e0accSmrg#define YARD (3*12)
339ff7e0accSmrg#define FOOT (12)
340ff7e0accSmrg
341ff7e0accSmrgstatic char *
342863f95b1Smrgnscale (int n, int np, int nmm, char *nbuf, size_t nbufsize)
343ff7e0accSmrg{
344ff7e0accSmrg    int s;
345863f95b1Smrg    snprintf (nbuf, nbufsize, "%d", n);
346863f95b1Smrg
347ff7e0accSmrg    if (metric||english) {
348863f95b1Smrg	s = strlcat (nbuf, " (", nbufsize);
349ff7e0accSmrg
350ff7e0accSmrg	if (metric) {
351863f95b1Smrg	    snprintf (nbuf+s, nbufsize-s, "%.2f mm%s",
352863f95b1Smrg		      ((double) n) * nmm/np , english ? "; " : "");
353ff7e0accSmrg	}
354ff7e0accSmrg	if (english) {
355ff7e0accSmrg	    double inch_frac;
356ff7e0accSmrg	    Bool printed_anything = False;
357ff7e0accSmrg	    int mi, yar, ft, inr;
358ff7e0accSmrg
359ff7e0accSmrg	    inch_frac = ((double) n)*(nmm/25.4)/np;
360ff7e0accSmrg	    inr = (int)inch_frac;
361ff7e0accSmrg	    inch_frac -= (double)inr;
362ff7e0accSmrg	    if (inr >= MILE) {
363ff7e0accSmrg		mi = inr/MILE;
364ff7e0accSmrg		inr %= MILE;
365863f95b1Smrg		s = strlen (nbuf);
366863f95b1Smrg		snprintf (nbuf+s, nbufsize-s, "%d %s(?!?)",
367863f95b1Smrg			  mi, (mi == 1) ? "mile" : "miles");
368ff7e0accSmrg		printed_anything = True;
369ff7e0accSmrg	    }
370ff7e0accSmrg	    if (inr >= YARD) {
371ff7e0accSmrg		yar = inr/YARD;
372ff7e0accSmrg		inr %= YARD;
373ff7e0accSmrg		if (printed_anything)
374863f95b1Smrg		    strlcat (nbuf, ", ", nbufsize);
375863f95b1Smrg		s = strlen (nbuf);
376863f95b1Smrg		snprintf (nbuf+s, nbufsize-s, "%d %s",
377863f95b1Smrg			 yar, (yar==1) ? "yard" : "yards");
378ff7e0accSmrg		printed_anything = True;
379ff7e0accSmrg	    }
380ff7e0accSmrg	    if (inr >= FOOT) {
381ff7e0accSmrg		ft = inr/FOOT;
382ff7e0accSmrg		inr  %= FOOT;
383ff7e0accSmrg		if (printed_anything)
384863f95b1Smrg		    strlcat (nbuf, ", ", nbufsize);
385863f95b1Smrg		s = strlen (nbuf);
386863f95b1Smrg		snprintf (nbuf+s, nbufsize-s, "%d %s",
387863f95b1Smrg			 ft, (ft==1) ? "foot" : "feet");
388ff7e0accSmrg		printed_anything = True;
389ff7e0accSmrg	    }
390ff7e0accSmrg	    if (!printed_anything || inch_frac != 0.0 || inr != 0) {
391ff7e0accSmrg		if (printed_anything)
392863f95b1Smrg		    strlcat (nbuf, ", ", nbufsize);
393863f95b1Smrg		s = strlen (nbuf);
394863f95b1Smrg		snprintf (nbuf+s, nbufsize-s, "%.2f inches", inr+inch_frac);
395ff7e0accSmrg	    }
396ff7e0accSmrg	}
397ff7e0accSmrg	strlcat (nbuf, ")", nbufsize);
398ff7e0accSmrg    }
399863f95b1Smrg    return (nbuf);
400863f95b1Smrg}
401863f95b1Smrg
402ff7e0accSmrgstatic char xbuf[BUFSIZ];
403ff7e0accSmrgstatic char *
404863f95b1Smrgxscale (int x)
405ff7e0accSmrg{
406863f95b1Smrg    return (nscale (x, xp, xmm, xbuf, sizeof(xbuf)));
407ff7e0accSmrg}
408ff7e0accSmrg
409ff7e0accSmrgstatic char ybuf[BUFSIZ];
410ff7e0accSmrgstatic char *
411863f95b1Smrgyscale (int y)
412ff7e0accSmrg{
413863f95b1Smrg    return (nscale (y, yp, ymm, ybuf, sizeof(ybuf)));
414ff7e0accSmrg}
415ff7e0accSmrg
416ff7e0accSmrgstatic char bbuf[BUFSIZ];
417ff7e0accSmrgstatic char *
418863f95b1Smrgbscale (int b)
419ff7e0accSmrg{
420863f95b1Smrg    return (nscale (b, bp, bmm, bbuf, sizeof(bbuf)));
421ff7e0accSmrg}
422ff7e0accSmrg
423ff7e0accSmrg/* end of pixel to inch, metric converter */
424ff7e0accSmrg
425863f95b1Smrgint
426863f95b1Smrgmain (int argc, char **argv)
427ff7e0accSmrg{
428863f95b1Smrg    register int i;
429863f95b1Smrg    int tree = 0, stats = 0, bits = 0, events = 0, wm = 0, size = 0, shape = 0;
430863f95b1Smrg    int frame = 0, children = 0;
431863f95b1Smrg    int use_root = 0;
432863f95b1Smrg    xcb_window_t window = 0;
433863f95b1Smrg    char *display_name = NULL;
434863f95b1Smrg    const char *window_name = NULL;
435863f95b1Smrg    struct wininfo wininfo;
436863f95b1Smrg    struct wininfo *w = &wininfo;
437863f95b1Smrg
438863f95b1Smrg    program_name = argv[0];
439863f95b1Smrg
440863f95b1Smrg    if (!setlocale (LC_ALL, ""))
441863f95b1Smrg	fprintf (stderr, "%s: can not set locale properly\n", program_name);
442863f95b1Smrg    user_encoding = nl_langinfo (CODESET);
443863f95b1Smrg    if (user_encoding == NULL)
444863f95b1Smrg	user_encoding = "unknown encoding";
445863f95b1Smrg
446863f95b1Smrg    memset (w, 0, sizeof(struct wininfo));
447863f95b1Smrg
448863f95b1Smrg    /* Handle our command line arguments */
449863f95b1Smrg    for (i = 1; i < argc; i++) {
450863f95b1Smrg	if (!strcmp (argv[i], "-help"))
451863f95b1Smrg	    usage ();
452863f95b1Smrg	if (!strcmp (argv[i], "-display") || !strcmp (argv[i], "-d")) {
453863f95b1Smrg	    if (++i >= argc)
454863f95b1Smrg		Fatal_Error("-display requires argument");
455863f95b1Smrg	    display_name = argv[i];
456863f95b1Smrg	    continue;
457863f95b1Smrg	}
458863f95b1Smrg	if (!strcmp (argv[i], "-root")) {
459863f95b1Smrg	    use_root = 1;
460863f95b1Smrg	    continue;
461863f95b1Smrg	}
462863f95b1Smrg	if (!strcmp (argv[i], "-id")) {
463863f95b1Smrg	    if (++i >= argc)
464863f95b1Smrg		Fatal_Error("-id requires argument");
465863f95b1Smrg	    window = strtoul(argv[i], NULL, 0);
466863f95b1Smrg	    continue;
467863f95b1Smrg	}
468863f95b1Smrg	if (!strcmp (argv[i], "-name")) {
469863f95b1Smrg	    if (++i >= argc)
470863f95b1Smrg		Fatal_Error("-name requires argument");
471863f95b1Smrg	    window_name = argv[i];
472863f95b1Smrg	    continue;
473863f95b1Smrg	}
474863f95b1Smrg	if (!strcmp (argv[i], "-int")) {
475863f95b1Smrg	    window_id_format = "%ld";
476863f95b1Smrg	    continue;
477863f95b1Smrg	}
478863f95b1Smrg	if (!strcmp (argv[i], "-children")) {
479863f95b1Smrg	    children = 1;
480863f95b1Smrg	    continue;
481863f95b1Smrg	}
482863f95b1Smrg	if (!strcmp (argv[i], "-tree")) {
483863f95b1Smrg	    tree = 1;
484863f95b1Smrg	    continue;
485863f95b1Smrg	}
486863f95b1Smrg	if (!strcmp (argv[i], "-stats")) {
487863f95b1Smrg	    stats = 1;
488863f95b1Smrg	    continue;
489863f95b1Smrg	}
490863f95b1Smrg	if (!strcmp (argv[i], "-bits")) {
491863f95b1Smrg	    bits = 1;
492863f95b1Smrg	    continue;
493863f95b1Smrg	}
494863f95b1Smrg	if (!strcmp (argv[i], "-events")) {
495863f95b1Smrg	    events = 1;
496863f95b1Smrg	    continue;
497863f95b1Smrg	}
498863f95b1Smrg	if (!strcmp (argv[i], "-wm")) {
499863f95b1Smrg	    wm = 1;
500863f95b1Smrg	    continue;
501863f95b1Smrg	}
502863f95b1Smrg	if (!strcmp (argv[i], "-frame")) {
503863f95b1Smrg	    frame = 1;
504863f95b1Smrg	    continue;
505863f95b1Smrg	}
506863f95b1Smrg	if (!strcmp (argv[i], "-size")) {
507863f95b1Smrg	    size = 1;
508863f95b1Smrg	    continue;
509863f95b1Smrg	}
510863f95b1Smrg	if (!strcmp (argv[i], "-shape")) {
511863f95b1Smrg	    shape = 1;
512863f95b1Smrg	    continue;
513863f95b1Smrg	}
514863f95b1Smrg	if (!strcmp (argv[i], "-english")) {
515863f95b1Smrg	    english = 1;
516863f95b1Smrg	    continue;
517863f95b1Smrg	}
518863f95b1Smrg	if (!strcmp (argv[i], "-metric")) {
519863f95b1Smrg	    metric = 1;
520863f95b1Smrg	    continue;
521863f95b1Smrg	}
522863f95b1Smrg	if (!strcmp (argv[i], "-all")) {
523863f95b1Smrg	    tree = stats = bits = events = wm = size = shape = 1;
524863f95b1Smrg	    continue;
525863f95b1Smrg	}
526863f95b1Smrg	usage ();
527863f95b1Smrg    }
528ff7e0accSmrg
529863f95b1Smrg    Setup_Display_And_Screen (display_name, &dpy, &screen);
530863f95b1Smrg
531863f95b1Smrg    /* preload atoms we may need later */
532863f95b1Smrg    Intern_Atom (dpy, "_NET_WM_NAME");
533863f95b1Smrg    Intern_Atom (dpy, "UTF8_STRING");
534863f95b1Smrg    if (wm) {
535863f95b1Smrg	Intern_Atom (dpy, "_NET_WM_DESKTOP");
536863f95b1Smrg	Intern_Atom (dpy, "_NET_WM_WINDOW_TYPE");
537863f95b1Smrg	Intern_Atom (dpy, "_NET_WM_STATE");
538863f95b1Smrg	Intern_Atom (dpy, "_NET_WM_PID");
539863f95b1Smrg	Intern_Atom (dpy, "_NET_FRAME_EXTENTS");
540863f95b1Smrg    }
541863f95b1Smrg    /* initialize scaling data */
542863f95b1Smrg    scale_init(screen);
543863f95b1Smrg
544863f95b1Smrg    if (use_root)
545863f95b1Smrg	window = screen->root;
546863f95b1Smrg    else if (window_name) {
547863f95b1Smrg	window = Window_With_Name (dpy, screen->root, window_name);
548863f95b1Smrg	if (!window)
549863f95b1Smrg	    Fatal_Error ("No window with name \"%s\" exists!", window_name);
550863f95b1Smrg    }
551ff7e0accSmrg
552863f95b1Smrg    /* If no window selected on command line, let user pick one the hard way */
553863f95b1Smrg    if (!window) {
554863f95b1Smrg	printf ("\n"
555863f95b1Smrg		"xwininfo: Please select the window about which you\n"
556863f95b1Smrg		"          would like information by clicking the\n"
557863f95b1Smrg		"          mouse in that window.\n");
558863f95b1Smrg	Intern_Atom (dpy, "_NET_VIRTUAL_ROOTS");
559863f95b1Smrg	Intern_Atom (dpy, "WM_STATE");
560863f95b1Smrg	window = Select_Window (dpy, screen, !frame);
561863f95b1Smrg    }
562ff7e0accSmrg
563863f95b1Smrg    /*
564863f95b1Smrg     * Do the actual displaying as per parameters
565863f95b1Smrg     */
566863f95b1Smrg    if (!(children || tree || bits || events || wm || size))
567863f95b1Smrg	stats = 1;
568ff7e0accSmrg
569863f95b1Smrg    /*
570863f95b1Smrg     * make sure that the window is valid
571863f95b1Smrg     */
572863f95b1Smrg    {
573863f95b1Smrg	xcb_get_geometry_cookie_t gg_cookie =
574863f95b1Smrg	    xcb_get_geometry (dpy, window);
575ff7e0accSmrg
576863f95b1Smrg	w->geometry = xcb_get_geometry_reply(dpy, gg_cookie, &err);
577ff7e0accSmrg
578863f95b1Smrg	if (!w->geometry) {
579863f95b1Smrg	    char badid[20];
580863f95b1Smrg
581863f95b1Smrg	    if (err)
582863f95b1Smrg		Print_X_Error (dpy, err);
583863f95b1Smrg
584863f95b1Smrg	    snprintf (badid, sizeof(badid), window_id_format, window);
585863f95b1Smrg	    Fatal_Error ("No such window with id %s.", badid);
586863f95b1Smrg	}
587ff7e0accSmrg    }
588863f95b1Smrg
589863f95b1Smrg    /* Send requests to prefetch data we'll need */
590863f95b1Smrg    w->window = window;
591863f95b1Smrg    w->net_wm_name_cookie = get_net_wm_name (dpy, window);
592853aa076Smrg    w->wm_name_cookie = xcb_icccm_get_wm_name (dpy, window);
593863f95b1Smrg    if (children || tree)
594863f95b1Smrg	w->tree_cookie = xcb_query_tree (dpy, window);
595863f95b1Smrg    if (stats) {
596863f95b1Smrg	w->trans_coords_cookie =
597863f95b1Smrg	    xcb_translate_coordinates (dpy, window, w->geometry->root,
598863f95b1Smrg				       -(w->geometry->border_width),
599863f95b1Smrg				       -(w->geometry->border_width));
600ff7e0accSmrg    }
601863f95b1Smrg    if (stats || bits || events)
602863f95b1Smrg	w->attr_cookie = xcb_get_window_attributes (dpy, window);
603863f95b1Smrg    if (stats || size)
604853aa076Smrg	w->normal_hints_cookie = xcb_icccm_get_wm_normal_hints (dpy, window);
605863f95b1Smrg    if (wm) {
606853aa076Smrg	w->hints_cookie = xcb_icccm_get_wm_hints(dpy, window);
607863f95b1Smrg
608863f95b1Smrg	atom_net_wm_desktop = Get_Atom (dpy, "_NET_WM_DESKTOP");
609863f95b1Smrg	if (atom_net_wm_desktop) {
610863f95b1Smrg	    w->wm_desktop_cookie = xcb_get_property
611863f95b1Smrg		(dpy, False, window, atom_net_wm_desktop,
612863f95b1Smrg		 XCB_ATOM_CARDINAL, 0, 4);
613863f95b1Smrg	}
614863f95b1Smrg
615863f95b1Smrg	atom_net_wm_window_type	= Get_Atom (dpy, "_NET_WM_WINDOW_TYPE");
616863f95b1Smrg	if (atom_net_wm_window_type) {
617863f95b1Smrg	    w->wm_window_type_cookie = xcb_get_property
618863f95b1Smrg		(dpy, False, window, atom_net_wm_window_type,
619863f95b1Smrg		 XCB_ATOM_ATOM, 0, BUFSIZ);
620863f95b1Smrg	}
621863f95b1Smrg
622863f95b1Smrg	atom_net_wm_state = Get_Atom (dpy, "_NET_WM_STATE");
623863f95b1Smrg	if (atom_net_wm_state) {
624863f95b1Smrg	    w->wm_state_cookie = xcb_get_property
625863f95b1Smrg		(dpy, False, window, atom_net_wm_state,
626863f95b1Smrg		 XCB_ATOM_ATOM, 0, BUFSIZ);
627863f95b1Smrg	}
628863f95b1Smrg
629863f95b1Smrg	atom_net_wm_pid	= Get_Atom (dpy, "_NET_WM_PID");
630863f95b1Smrg	if (atom_net_wm_pid) {
631863f95b1Smrg	    w->wm_pid_cookie = xcb_get_property
632863f95b1Smrg		(dpy, False, window, atom_net_wm_pid,
633863f95b1Smrg		 XCB_ATOM_CARDINAL, 0, BUFSIZ);
634863f95b1Smrg	    w->wm_client_machine_cookie = xcb_get_property
635863f95b1Smrg		(dpy, False, window, XCB_ATOM_WM_CLIENT_MACHINE,
636863f95b1Smrg		 XCB_GET_PROPERTY_TYPE_ANY, 0, BUFSIZ);
637863f95b1Smrg	}
638863f95b1Smrg
639863f95b1Smrg	atom_net_frame_extents = Get_Atom (dpy, "_NET_FRAME_EXTENTS");
640863f95b1Smrg	if (atom_net_frame_extents) {
641863f95b1Smrg	    w->frame_extents_cookie = xcb_get_property
642863f95b1Smrg		(dpy, False, window, atom_net_frame_extents,
643863f95b1Smrg		 XCB_ATOM_CARDINAL, 0, 4 * 4);
644863f95b1Smrg	}
645ff7e0accSmrg    }
646863f95b1Smrg    if (size)
647853aa076Smrg	w->zoom_cookie = xcb_icccm_get_wm_size_hints (dpy, window,
648853aa076Smrg						      XCB_ATOM_WM_ZOOM_HINTS);
649863f95b1Smrg    xcb_flush (dpy);
650863f95b1Smrg
651863f95b1Smrg    printf ("\nxwininfo: Window id: ");
652863f95b1Smrg    Display_Window_Id (w, True);
653863f95b1Smrg    if (children || tree)
654863f95b1Smrg	Display_Tree_Info (w, tree);
655863f95b1Smrg    if (stats)
656863f95b1Smrg	Display_Stats_Info (w);
657863f95b1Smrg    if (bits)
658863f95b1Smrg	Display_Bits_Info (w);
659863f95b1Smrg    if (events)
660863f95b1Smrg	Display_Events_Info (w);
661863f95b1Smrg    if (wm)
662863f95b1Smrg	Display_WM_Info (w);
663863f95b1Smrg    if (size)
664863f95b1Smrg	Display_Size_Hints (w);
665863f95b1Smrg    if (shape)
666863f95b1Smrg	Display_Window_Shape (window);
667863f95b1Smrg    printf ("\n");
668863f95b1Smrg
669863f95b1Smrg    wininfo_wipe (w);
670863f95b1Smrg    xcb_disconnect (dpy);
671863f95b1Smrg#ifdef HAVE_ICONV
672863f95b1Smrg    if (iconv_from_utf8 && (iconv_from_utf8 != (iconv_t) -1)) {
673863f95b1Smrg	iconv_close (iconv_from_utf8);
674ff7e0accSmrg    }
675863f95b1Smrg#endif
676863f95b1Smrg    exit (0);
677863f95b1Smrg}
678863f95b1Smrg
679863f95b1Smrg/* Ensure win_attributes field is filled in */
680863f95b1Smrgstatic xcb_get_window_attributes_reply_t *
681863f95b1Smrgfetch_win_attributes (struct wininfo *w)
682863f95b1Smrg{
683863f95b1Smrg    if (!w->win_attributes) {
684863f95b1Smrg	w->win_attributes =
685863f95b1Smrg	    xcb_get_window_attributes_reply (dpy, w->attr_cookie, &err);
686863f95b1Smrg
687863f95b1Smrg	if (!w->win_attributes) {
688863f95b1Smrg	    Print_X_Error (dpy, err);
689863f95b1Smrg	    Fatal_Error ("Can't get window attributes.");
690863f95b1Smrg	}
691ff7e0accSmrg    }
692863f95b1Smrg    return w->win_attributes;
693863f95b1Smrg}
694863f95b1Smrg
695863f95b1Smrg#ifndef USE_XCB_ICCCM
696863f95b1Smrgstatic Bool
697863f95b1Smrgwm_size_hints_reply (xcb_connection_t *dpy, xcb_get_property_cookie_t cookie,
698863f95b1Smrg		     wm_size_hints_t *hints_return, xcb_generic_error_t **err)
699863f95b1Smrg{
700863f95b1Smrg    xcb_get_property_reply_t *prop = xcb_get_property_reply (dpy, cookie, err);
701863f95b1Smrg    int length;
702863f95b1Smrg
703863f95b1Smrg    if (!prop || (prop->type != XCB_ATOM_WM_SIZE_HINTS) ||
704863f95b1Smrg	(prop->format != 32)) {
705863f95b1Smrg	free (prop);
706863f95b1Smrg	return False;
707ff7e0accSmrg    }
708863f95b1Smrg
709863f95b1Smrg    memset (hints_return, 0, sizeof(wm_size_hints_t));
710863f95b1Smrg
711863f95b1Smrg    length = xcb_get_property_value_length(prop);
712863f95b1Smrg    if (length > sizeof(wm_size_hints_t))
713863f95b1Smrg	length = sizeof(wm_size_hints_t);
714863f95b1Smrg    memcpy (hints_return, xcb_get_property_value (prop), length);
715863f95b1Smrg
716863f95b1Smrg    free (prop);
717863f95b1Smrg    return True;
718863f95b1Smrg}
719863f95b1Smrg
720853aa076Smrg#define xcb_icccm_get_wm_normal_hints_reply wm_size_hints_reply
721853aa076Smrg#define xcb_icccm_get_wm_size_hints_reply wm_size_hints_reply
722863f95b1Smrg#endif
723863f95b1Smrg
724863f95b1Smrg
725863f95b1Smrg
726863f95b1Smrg/* Ensure normal_hints field is filled in */
727863f95b1Smrgstatic xcb_size_hints_t *
728863f95b1Smrgfetch_normal_hints (struct wininfo *w, xcb_size_hints_t *hints_return)
729863f95b1Smrg{
730863f95b1Smrg    xcb_size_hints_t hints;
731863f95b1Smrg
732863f95b1Smrg    if (!w->normal_hints) {
733853aa076Smrg	if (xcb_icccm_get_wm_normal_hints_reply (dpy, w->normal_hints_cookie,
734853aa076Smrg						 &hints, NULL)) {
735863f95b1Smrg	    w->normal_hints = malloc (sizeof(xcb_size_hints_t));
736863f95b1Smrg	    if (w->normal_hints)
737863f95b1Smrg		memcpy(w->normal_hints, &hints, sizeof(xcb_size_hints_t));
738863f95b1Smrg	}
739ff7e0accSmrg    }
740863f95b1Smrg    if (hints_return && w->normal_hints)
741863f95b1Smrg	memcpy(hints_return, w->normal_hints, sizeof(xcb_size_hints_t));
742863f95b1Smrg    return w->normal_hints;
743ff7e0accSmrg}
744ff7e0accSmrg
745ff7e0accSmrg
746ff7e0accSmrg/*
747ff7e0accSmrg * Lookup: lookup a code in a table.
748ff7e0accSmrg */
749ff7e0accSmrgstatic char _lookup_buffer[100];
750ff7e0accSmrg
751ff7e0accSmrgstatic const char *
752863f95b1SmrgLookupL (long code, const binding *table)
753ff7e0accSmrg{
754863f95b1Smrg    const char *name = NULL;
755ff7e0accSmrg
756863f95b1Smrg    while (table->name) {
757863f95b1Smrg	if (table->code == code) {
758863f95b1Smrg	    name = table->name;
759863f95b1Smrg	    break;
760ff7e0accSmrg	}
761863f95b1Smrg	table++;
762863f95b1Smrg    }
763ff7e0accSmrg
764863f95b1Smrg    if (name == NULL) {
765863f95b1Smrg	snprintf (_lookup_buffer, sizeof(_lookup_buffer),
766863f95b1Smrg		  "unknown (code = %ld. = 0x%lx)", code, code);
767863f95b1Smrg	name = _lookup_buffer;
768863f95b1Smrg    }
769863f95b1Smrg
770863f95b1Smrg    return (name);
771ff7e0accSmrg}
772ff7e0accSmrg
773ff7e0accSmrgstatic const char *
774863f95b1SmrgLookup (int code, const binding *table)
775ff7e0accSmrg{
776863f95b1Smrg    return LookupL ((long)code, table);
777ff7e0accSmrg}
778ff7e0accSmrg
779ff7e0accSmrg/*
780ff7e0accSmrg * Routine to display a window id in dec/hex with name if window has one
781863f95b1Smrg *
782863f95b1Smrg * Requires wininfo members initialized: window, wm_name_cookie
783ff7e0accSmrg */
784ff7e0accSmrg
785ff7e0accSmrgstatic void
786863f95b1SmrgDisplay_Window_Id (struct wininfo *w, Bool newline_wanted)
787ff7e0accSmrg{
788863f95b1Smrg#ifdef USE_XCB_ICCCM
789853aa076Smrg    xcb_icccm_get_text_property_reply_t wmn_reply;
790853aa076Smrg    uint8_t got_reply = False;
791ff7e0accSmrg#endif
792863f95b1Smrg    xcb_get_property_reply_t *prop;
793863f95b1Smrg    const char *wm_name = NULL;
794863f95b1Smrg    unsigned int wm_name_len = 0;
795863f95b1Smrg    xcb_atom_t wm_name_encoding = XCB_NONE;
796ff7e0accSmrg
797863f95b1Smrg    printf (window_id_format, w->window);      /* print id # in hex/dec */
798863f95b1Smrg
799863f95b1Smrg    if (!w->window) {
800863f95b1Smrg	printf (" (none)");
801ff7e0accSmrg    } else {
802863f95b1Smrg	if (w->window == screen->root) {
803863f95b1Smrg	    printf (" (the root window)");
804ff7e0accSmrg	}
805863f95b1Smrg	/* Get window name if any */
806863f95b1Smrg	prop = xcb_get_property_reply (dpy, w->net_wm_name_cookie, NULL);
807863f95b1Smrg	if (prop && (prop->type != XCB_NONE)) {
808863f95b1Smrg	    wm_name = xcb_get_property_value (prop);
809863f95b1Smrg	    wm_name_len = xcb_get_property_value_length (prop);
810863f95b1Smrg	    wm_name_encoding = prop->type;
811853aa076Smrg	} else { /* No _NET_WM_NAME, check WM_NAME */
812863f95b1Smrg#ifdef USE_XCB_ICCCM
813853aa076Smrg	    got_reply = xcb_icccm_get_wm_name_reply (dpy, w->wm_name_cookie,
814853aa076Smrg						     &wmn_reply, NULL);
815863f95b1Smrg	    if (got_reply) {
816863f95b1Smrg		wm_name = wmn_reply.name;
817863f95b1Smrg		wm_name_len = wmn_reply.name_len;
818863f95b1Smrg		wm_name_encoding = wmn_reply.encoding;
819863f95b1Smrg	    }
820ff7e0accSmrg#else
821863f95b1Smrg	    prop = xcb_get_property_reply (dpy, w->wm_name_cookie, NULL);
822863f95b1Smrg	    if (prop && (prop->type != XCB_NONE)) {
823863f95b1Smrg		wm_name = xcb_get_property_value (prop);
824863f95b1Smrg		wm_name_len = xcb_get_property_value_length (prop);
825863f95b1Smrg		wm_name_encoding = prop->type;
826863f95b1Smrg	    }
827863f95b1Smrg#endif
828ff7e0accSmrg	}
829853aa076Smrg	if (wm_name_len == 0) {
830863f95b1Smrg	    printf (" (has no name)");
831863f95b1Smrg        } else {
832863f95b1Smrg	    if (wm_name_encoding == XCB_ATOM_STRING) {
833863f95b1Smrg		printf (" \"%.*s\"", wm_name_len, wm_name);
834863f95b1Smrg	    } else if (wm_name_encoding == atom_utf8_string) {
835863f95b1Smrg		print_utf8 (" \"", (char *) wm_name, wm_name_len,  "\"");
836863f95b1Smrg	    } else {
837863f95b1Smrg		/* Encodings we don't support, including COMPOUND_TEXT */
838863f95b1Smrg		const char *enc_name = Get_Atom_Name (dpy, wm_name_encoding);
839863f95b1Smrg		if (enc_name) {
840863f95b1Smrg		    printf (" (name in unsupported encoding %s)", enc_name);
841863f95b1Smrg		} else {
842863f95b1Smrg		    printf (" (name in unsupported encoding ATOM 0x%x)",
843863f95b1Smrg			    wm_name_encoding);
844863f95b1Smrg		}
845863f95b1Smrg	    }
846863f95b1Smrg	}
847863f95b1Smrg#ifdef USE_XCB_ICCCM
848863f95b1Smrg	if (got_reply)
849853aa076Smrg	    xcb_icccm_get_text_property_reply_wipe (&wmn_reply);
850863f95b1Smrg#else
851863f95b1Smrg	free (prop);
852ff7e0accSmrg#endif
853ff7e0accSmrg    }
854ff7e0accSmrg
855ff7e0accSmrg    if (newline_wanted)
856863f95b1Smrg	printf ("\n");
857ff7e0accSmrg
858ff7e0accSmrg    return;
859ff7e0accSmrg}
860ff7e0accSmrg
861ff7e0accSmrg
862ff7e0accSmrg/*
863ff7e0accSmrg * Display Stats on window
864ff7e0accSmrg */
865ff7e0accSmrgstatic const binding _window_classes[] = {
866863f95b1Smrg	{ XCB_WINDOW_CLASS_INPUT_OUTPUT, "InputOutput" },
867863f95b1Smrg	{ XCB_WINDOW_CLASS_INPUT_ONLY, "InputOnly" },
86810998002Smrg        { 0, NULL } };
869ff7e0accSmrg
870ff7e0accSmrgstatic const binding _map_states[] = {
871863f95b1Smrg	{ XCB_MAP_STATE_UNMAPPED,	"IsUnMapped" },
872863f95b1Smrg	{ XCB_MAP_STATE_UNVIEWABLE,	"IsUnviewable" },
873863f95b1Smrg	{ XCB_MAP_STATE_VIEWABLE,	"IsViewable" },
87410998002Smrg	{ 0, NULL } };
875ff7e0accSmrg
876ff7e0accSmrgstatic const binding _backing_store_states[] = {
877863f95b1Smrg	{ XCB_BACKING_STORE_NOT_USEFUL, "NotUseful" },
878863f95b1Smrg	{ XCB_BACKING_STORE_WHEN_MAPPED,"WhenMapped" },
879863f95b1Smrg	{ XCB_BACKING_STORE_ALWAYS,	"Always" },
88010998002Smrg	{ 0, NULL } };
881ff7e0accSmrg
882ff7e0accSmrgstatic const binding _bit_gravity_states[] = {
883863f95b1Smrg	{ XCB_GRAVITY_BIT_FORGET,	"ForgetGravity" },
884863f95b1Smrg	{ XCB_GRAVITY_NORTH_WEST,	"NorthWestGravity" },
885863f95b1Smrg	{ XCB_GRAVITY_NORTH,		"NorthGravity" },
886863f95b1Smrg	{ XCB_GRAVITY_NORTH_EAST,	"NorthEastGravity" },
887863f95b1Smrg	{ XCB_GRAVITY_WEST,		"WestGravity" },
888863f95b1Smrg	{ XCB_GRAVITY_CENTER,		"CenterGravity" },
889863f95b1Smrg	{ XCB_GRAVITY_EAST,		"EastGravity" },
890863f95b1Smrg	{ XCB_GRAVITY_SOUTH_WEST,	"SouthWestGravity" },
891863f95b1Smrg	{ XCB_GRAVITY_SOUTH,		"SouthGravity" },
892863f95b1Smrg	{ XCB_GRAVITY_SOUTH_EAST,	"SouthEastGravity" },
893863f95b1Smrg	{ XCB_GRAVITY_STATIC,		"StaticGravity" },
89410998002Smrg	{ 0, NULL }};
895ff7e0accSmrg
896ff7e0accSmrgstatic const binding _window_gravity_states[] = {
897863f95b1Smrg	{ XCB_GRAVITY_WIN_UNMAP,	"UnmapGravity" },
898863f95b1Smrg	{ XCB_GRAVITY_NORTH_WEST,	"NorthWestGravity" },
899863f95b1Smrg	{ XCB_GRAVITY_NORTH,		"NorthGravity" },
900863f95b1Smrg	{ XCB_GRAVITY_NORTH_EAST,	"NorthEastGravity" },
901863f95b1Smrg	{ XCB_GRAVITY_WEST,		"WestGravity" },
902863f95b1Smrg	{ XCB_GRAVITY_CENTER,		"CenterGravity" },
903863f95b1Smrg	{ XCB_GRAVITY_EAST,		"EastGravity" },
904863f95b1Smrg	{ XCB_GRAVITY_SOUTH_WEST,	"SouthWestGravity" },
905863f95b1Smrg	{ XCB_GRAVITY_SOUTH,		"SouthGravity" },
906863f95b1Smrg	{ XCB_GRAVITY_SOUTH_EAST,	"SouthEastGravity" },
907863f95b1Smrg	{ XCB_GRAVITY_STATIC,		"StaticGravity" },
90810998002Smrg	{ 0, NULL }};
909ff7e0accSmrg
910ff7e0accSmrgstatic const binding _visual_classes[] = {
911863f95b1Smrg	{ XCB_VISUAL_CLASS_STATIC_GRAY,	"StaticGray" },
912863f95b1Smrg	{ XCB_VISUAL_CLASS_GRAY_SCALE,	"GrayScale" },
913863f95b1Smrg	{ XCB_VISUAL_CLASS_STATIC_COLOR,"StaticColor" },
914863f95b1Smrg	{ XCB_VISUAL_CLASS_PSEUDO_COLOR,"PseudoColor" },
915863f95b1Smrg	{ XCB_VISUAL_CLASS_TRUE_COLOR,	"TrueColor" },
916863f95b1Smrg	{ XCB_VISUAL_CLASS_DIRECT_COLOR,"DirectColor" },
91710998002Smrg	{ 0, NULL }};
918ff7e0accSmrg
919863f95b1Smrg/*
920863f95b1Smrg * Requires wininfo members initialized:
921863f95b1Smrg *   window, geometry, attr_cookie, trans_coords_cookie, normal_hints_cookie
922863f95b1Smrg */
923ff7e0accSmrgstatic void
924863f95b1SmrgDisplay_Stats_Info (struct wininfo *w)
925ff7e0accSmrg{
926863f95b1Smrg    xcb_translate_coordinates_reply_t *trans_coords;
927863f95b1Smrg    xcb_get_window_attributes_reply_t *win_attributes;
928863f95b1Smrg    xcb_size_hints_t hints;
929863f95b1Smrg
930863f95b1Smrg    int dw = screen->width_in_pixels, dh = screen->height_in_pixels;
931863f95b1Smrg    int rx, ry, xright, ybelow;
932863f95b1Smrg    int showright = 0, showbelow = 0;
933863f95b1Smrg    xcb_window_t wmframe, parent;
934863f95b1Smrg
935863f95b1Smrg    trans_coords =
936863f95b1Smrg	xcb_translate_coordinates_reply (dpy, w->trans_coords_cookie, NULL);
937863f95b1Smrg    if (!trans_coords)
938863f95b1Smrg	Fatal_Error ("Can't get translated coordinates.");
939863f95b1Smrg
940863f95b1Smrg    rx = (int16_t)trans_coords->dst_x;
941863f95b1Smrg    ry = (int16_t)trans_coords->dst_y;
942863f95b1Smrg    free (trans_coords);
943863f95b1Smrg
944863f95b1Smrg    xright = (dw - rx - w->geometry->border_width * 2 -
945863f95b1Smrg	      w->geometry->width);
946863f95b1Smrg    ybelow = (dh - ry - w->geometry->border_width * 2 -
947863f95b1Smrg	      w->geometry->height);
948863f95b1Smrg
949863f95b1Smrg
950863f95b1Smrg    printf ("\n");
951863f95b1Smrg    printf ("  Absolute upper-left X:  %s\n", xscale (rx));
952863f95b1Smrg    printf ("  Absolute upper-left Y:  %s\n", yscale (ry));
953863f95b1Smrg    printf ("  Relative upper-left X:  %s\n", xscale (w->geometry->x));
954863f95b1Smrg    printf ("  Relative upper-left Y:  %s\n", yscale (w->geometry->y));
955863f95b1Smrg    printf ("  Width: %s\n", xscale (w->geometry->width));
956863f95b1Smrg    printf ("  Height: %s\n", yscale (w->geometry->height));
957863f95b1Smrg    printf ("  Depth: %d\n", w->geometry->depth);
958863f95b1Smrg
959863f95b1Smrg    win_attributes = fetch_win_attributes (w);
960863f95b1Smrg
961863f95b1Smrg    printf ("  Visual: 0x%lx\n", (unsigned long) win_attributes->visual);
962863f95b1Smrg    if (screen)
963863f95b1Smrg    {
964863f95b1Smrg	xcb_depth_iterator_t depth_iter;
965863f95b1Smrg	xcb_visualtype_t  *visual_type = NULL;
966863f95b1Smrg
967863f95b1Smrg	depth_iter = xcb_screen_allowed_depths_iterator (screen);
968863f95b1Smrg	for (; depth_iter.rem; xcb_depth_next (&depth_iter)) {
969863f95b1Smrg	    xcb_visualtype_iterator_t visual_iter;
970863f95b1Smrg
971863f95b1Smrg	    visual_iter = xcb_depth_visuals_iterator (depth_iter.data);
972863f95b1Smrg	    for (; visual_iter.rem; xcb_visualtype_next (&visual_iter)) {
973863f95b1Smrg		if (screen->root_visual == visual_iter.data->visual_id) {
974863f95b1Smrg		    visual_type = visual_iter.data;
975863f95b1Smrg		    break;
976863f95b1Smrg		}
977863f95b1Smrg	    }
978863f95b1Smrg	}
979863f95b1Smrg	if (visual_type)
980863f95b1Smrg	    printf ("  Visual Class: %s\n", Lookup (visual_type->_class,
981863f95b1Smrg						    _visual_classes));
982863f95b1Smrg    }
983863f95b1Smrg
984863f95b1Smrg    printf ("  Border width: %s\n", bscale (w->geometry->border_width));
985863f95b1Smrg    printf ("  Class: %s\n",
986863f95b1Smrg	    Lookup (win_attributes->_class, _window_classes));
987863f95b1Smrg    printf ("  Colormap: 0x%lx (%sinstalled)\n",
988863f95b1Smrg	    (unsigned long) win_attributes->colormap,
989863f95b1Smrg	    win_attributes->map_is_installed ? "" : "not ");
990863f95b1Smrg    printf ("  Bit Gravity State: %s\n",
991863f95b1Smrg	    Lookup (win_attributes->bit_gravity, _bit_gravity_states));
992863f95b1Smrg    printf ("  Window Gravity State: %s\n",
993863f95b1Smrg	    Lookup (win_attributes->win_gravity, _window_gravity_states));
994863f95b1Smrg    printf ("  Backing Store State: %s\n",
995863f95b1Smrg	    Lookup (win_attributes->backing_store, _backing_store_states));
996863f95b1Smrg    printf ("  Save Under State: %s\n",
997863f95b1Smrg	    win_attributes->save_under ? "yes" : "no");
998863f95b1Smrg    printf ("  Map State: %s\n",
999863f95b1Smrg	    Lookup (win_attributes->map_state, _map_states));
1000863f95b1Smrg    printf ("  Override Redirect State: %s\n",
1001863f95b1Smrg	    win_attributes->override_redirect ? "yes" : "no");
1002863f95b1Smrg    printf ("  Corners:  +%d+%d  -%d+%d  -%d-%d  +%d-%d\n",
1003863f95b1Smrg	    rx, ry, xright, ry, xright, ybelow, rx, ybelow);
1004863f95b1Smrg
1005863f95b1Smrg    /*
1006863f95b1Smrg     * compute geometry string that would recreate window
1007863f95b1Smrg     */
1008863f95b1Smrg    printf ("  -geometry ");
1009863f95b1Smrg
1010863f95b1Smrg    /* compute size in appropriate units */
1011863f95b1Smrg    if (!fetch_normal_hints (w, &hints))
1012863f95b1Smrg	hints.flags = 0;
1013863f95b1Smrg
1014853aa076Smrg    if ((hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)  &&
1015863f95b1Smrg	(hints.width_inc != 0)  && (hints.height_inc != 0)) {
1016853aa076Smrg	if (hints.flags &
1017853aa076Smrg	    (XCB_ICCCM_SIZE_HINT_BASE_SIZE|XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)) {
1018853aa076Smrg	    if (hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
1019863f95b1Smrg		w->geometry->width -= hints.base_width;
1020863f95b1Smrg		w->geometry->height -= hints.base_height;
1021863f95b1Smrg	    } else {
1022863f95b1Smrg		/* ICCCM says MinSize is default for BaseSize */
1023863f95b1Smrg		w->geometry->width -= hints.min_width;
1024863f95b1Smrg		w->geometry->height -= hints.min_height;
1025863f95b1Smrg	    }
1026863f95b1Smrg	}
1027863f95b1Smrg	printf ("%dx%d", w->geometry->width/hints.width_inc,
1028863f95b1Smrg		w->geometry->height/hints.height_inc);
1029863f95b1Smrg    } else
1030863f95b1Smrg	printf ("%dx%d", w->geometry->width, w->geometry->height);
1031863f95b1Smrg
1032853aa076Smrg    if (!(hints.flags & XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY))
1033863f95b1Smrg	hints.win_gravity = XCB_GRAVITY_NORTH_WEST; /* per ICCCM */
1034863f95b1Smrg    /* find our window manager frame, if any */
1035863f95b1Smrg    for (wmframe = parent = w->window; parent != 0 ; wmframe = parent) {
1036863f95b1Smrg	xcb_query_tree_cookie_t qt_cookie;
1037863f95b1Smrg	xcb_query_tree_reply_t *tree;
1038863f95b1Smrg
1039863f95b1Smrg	qt_cookie = xcb_query_tree (dpy, wmframe);
1040863f95b1Smrg	tree = xcb_query_tree_reply (dpy, qt_cookie, &err);
1041863f95b1Smrg	if (!tree) {
1042863f95b1Smrg	    Print_X_Error (dpy, err);
1043863f95b1Smrg	    Fatal_Error ("Can't query window tree.");
1044863f95b1Smrg	}
1045863f95b1Smrg	parent = tree->parent;
1046863f95b1Smrg	free (tree);
1047863f95b1Smrg	if (parent == w->geometry->root || !parent)
1048863f95b1Smrg	    break;
1049863f95b1Smrg    }
1050863f95b1Smrg    if (wmframe != w->window) {
1051863f95b1Smrg	/* WM reparented, so find edges of the frame */
1052863f95b1Smrg	/* Only works for ICCCM-compliant WMs, and then only if the
1053863f95b1Smrg	   window has corner gravity.  We would need to know the original width
1054863f95b1Smrg	   of the window to correctly handle the other gravities. */
1055863f95b1Smrg	xcb_get_geometry_cookie_t geom_cookie;
1056863f95b1Smrg	xcb_get_geometry_reply_t *frame_geometry;
1057863f95b1Smrg
1058863f95b1Smrg	geom_cookie = xcb_get_geometry (dpy, wmframe);
1059863f95b1Smrg	frame_geometry = xcb_get_geometry_reply (dpy, geom_cookie, &err);
1060863f95b1Smrg
1061863f95b1Smrg	if (!frame_geometry) {
1062863f95b1Smrg	    Print_X_Error (dpy, err);
1063863f95b1Smrg	    Fatal_Error ("Can't get frame geometry.");
1064863f95b1Smrg	}
1065863f95b1Smrg	switch (hints.win_gravity) {
1066863f95b1Smrg	    case XCB_GRAVITY_NORTH_WEST: case XCB_GRAVITY_SOUTH_WEST:
1067863f95b1Smrg	    case XCB_GRAVITY_NORTH_EAST: case XCB_GRAVITY_SOUTH_EAST:
1068863f95b1Smrg	    case XCB_GRAVITY_WEST:
1069863f95b1Smrg		rx = frame_geometry->x;
1070863f95b1Smrg	}
1071863f95b1Smrg	switch (hints.win_gravity) {
1072863f95b1Smrg	    case XCB_GRAVITY_NORTH_WEST: case XCB_GRAVITY_SOUTH_WEST:
1073863f95b1Smrg	    case XCB_GRAVITY_NORTH_EAST: case XCB_GRAVITY_SOUTH_EAST:
1074863f95b1Smrg	    case XCB_GRAVITY_EAST:
1075863f95b1Smrg		xright = dw - frame_geometry->x - frame_geometry->width -
1076863f95b1Smrg		    (2 * frame_geometry->border_width);
1077863f95b1Smrg	}
1078863f95b1Smrg	switch (hints.win_gravity) {
1079863f95b1Smrg	    case XCB_GRAVITY_NORTH_WEST: case XCB_GRAVITY_SOUTH_WEST:
1080863f95b1Smrg	    case XCB_GRAVITY_NORTH_EAST: case XCB_GRAVITY_SOUTH_EAST:
1081863f95b1Smrg	    case XCB_GRAVITY_NORTH:
1082863f95b1Smrg		ry = frame_geometry->y;
1083863f95b1Smrg	}
1084863f95b1Smrg	switch (hints.win_gravity) {
1085863f95b1Smrg	    case XCB_GRAVITY_NORTH_WEST: case XCB_GRAVITY_SOUTH_WEST:
1086863f95b1Smrg	    case XCB_GRAVITY_NORTH_EAST: case XCB_GRAVITY_SOUTH_EAST:
1087863f95b1Smrg	    case XCB_GRAVITY_SOUTH:
1088863f95b1Smrg		ybelow = dh - frame_geometry->y - frame_geometry->height -
1089863f95b1Smrg		    (2 * frame_geometry->border_width);
1090863f95b1Smrg	}
1091863f95b1Smrg	free (frame_geometry);
1092863f95b1Smrg    }
1093863f95b1Smrg    /* If edge gravity, offer a corner on that edge (because the application
1094863f95b1Smrg       programmer cares about that edge), otherwise offer upper left unless
1095863f95b1Smrg       some other corner is close to an edge of the screen.
1096863f95b1Smrg       (For corner gravity, assume gravity was set by XWMGeometry.
1097863f95b1Smrg       For CenterGravity, it doesn't matter.) */
1098863f95b1Smrg    if (hints.win_gravity == XCB_GRAVITY_EAST  ||
1099863f95b1Smrg	(abs (xright) <= 100  &&  abs (xright) < abs (rx)
1100863f95b1Smrg	 &&  hints.win_gravity != XCB_GRAVITY_WEST))
1101863f95b1Smrg	showright = 1;
1102863f95b1Smrg    if (hints.win_gravity == XCB_GRAVITY_SOUTH  ||
1103863f95b1Smrg	(abs (ybelow) <= 100  &&  abs (ybelow) < abs (ry)
1104863f95b1Smrg	 &&  hints.win_gravity != XCB_GRAVITY_NORTH))
1105863f95b1Smrg	showbelow = 1;
1106863f95b1Smrg
1107863f95b1Smrg    if (showright)
1108863f95b1Smrg	printf ("-%d", xright);
1109863f95b1Smrg    else
1110863f95b1Smrg	printf ("+%d", rx);
1111863f95b1Smrg    if (showbelow)
1112863f95b1Smrg	printf ("-%d", ybelow);
1113863f95b1Smrg    else
1114863f95b1Smrg	printf ("+%d", ry);
1115863f95b1Smrg    printf ("\n");
1116ff7e0accSmrg}
1117ff7e0accSmrg
1118ff7e0accSmrg
1119ff7e0accSmrg/*
1120ff7e0accSmrg * Display bits info:
1121ff7e0accSmrg */
1122ff7e0accSmrgstatic const binding _gravities[] = {
1123863f95b1Smrg    /* WARNING: the first two of these have the same value - see code */
1124863f95b1Smrg	{ XCB_GRAVITY_WIN_UNMAP,	"UnMapGravity" },
1125863f95b1Smrg	{ XCB_GRAVITY_BIT_FORGET,	"ForgetGravity" },
1126863f95b1Smrg	{ XCB_GRAVITY_NORTH_WEST,	"NorthWestGravity" },
1127863f95b1Smrg	{ XCB_GRAVITY_NORTH,		"NorthGravity" },
1128863f95b1Smrg	{ XCB_GRAVITY_NORTH_EAST,	"NorthEastGravity" },
1129863f95b1Smrg	{ XCB_GRAVITY_WEST,		"WestGravity" },
1130863f95b1Smrg	{ XCB_GRAVITY_CENTER,		"CenterGravity" },
1131863f95b1Smrg	{ XCB_GRAVITY_EAST,		"EastGravity" },
1132863f95b1Smrg	{ XCB_GRAVITY_SOUTH_WEST,	"SouthWestGravity" },
1133863f95b1Smrg	{ XCB_GRAVITY_SOUTH,		"SouthGravity" },
1134863f95b1Smrg	{ XCB_GRAVITY_SOUTH_EAST,	"SouthEastGravity" },
1135863f95b1Smrg	{ XCB_GRAVITY_STATIC,		"StaticGravity" },
113610998002Smrg	{ 0, NULL } };
1137ff7e0accSmrg
1138ff7e0accSmrgstatic const binding _backing_store_hint[] = {
1139863f95b1Smrg	{ XCB_BACKING_STORE_NOT_USEFUL, "NotUseful" },
1140863f95b1Smrg	{ XCB_BACKING_STORE_WHEN_MAPPED,"WhenMapped" },
1141863f95b1Smrg	{ XCB_BACKING_STORE_ALWAYS,	"Always" },
114210998002Smrg	{ 0, NULL } };
1143ff7e0accSmrg
1144ff7e0accSmrgstatic const binding _bool[] = {
1145ff7e0accSmrg	{ 0, "No" },
1146ff7e0accSmrg	{ 1, "Yes" },
114710998002Smrg	{ 0, NULL } };
1148ff7e0accSmrg
1149863f95b1Smrg/*
1150863f95b1Smrg * Requires wininfo members initialized:
1151863f95b1Smrg *   window, attr_cookie (or win_attributes)
1152863f95b1Smrg */
1153ff7e0accSmrgstatic void
1154863f95b1SmrgDisplay_Bits_Info (struct wininfo * w)
1155ff7e0accSmrg{
1156863f95b1Smrg    xcb_get_window_attributes_reply_t *win_attributes
1157863f95b1Smrg	= fetch_win_attributes (w);
1158863f95b1Smrg
1159863f95b1Smrg    printf ("\n");
1160863f95b1Smrg    printf ("  Bit gravity: %s\n",
1161863f95b1Smrg	    Lookup (win_attributes->bit_gravity, _gravities+1));
1162863f95b1Smrg    printf ("  Window gravity: %s\n",
1163863f95b1Smrg	    Lookup (win_attributes->win_gravity, _gravities));
1164863f95b1Smrg    printf ("  Backing-store hint: %s\n",
1165863f95b1Smrg	    Lookup (win_attributes->backing_store, _backing_store_hint));
1166863f95b1Smrg    printf ("  Backing-planes to be preserved: 0x%lx\n",
1167863f95b1Smrg	    (unsigned long) win_attributes->backing_planes);
1168863f95b1Smrg    printf ("  Backing pixel: %ld\n",
1169863f95b1Smrg	    (unsigned long) win_attributes->backing_pixel);
1170863f95b1Smrg    printf ("  Save-unders: %s\n",
1171863f95b1Smrg	    Lookup (win_attributes->save_under, _bool));
1172ff7e0accSmrg}
1173ff7e0accSmrg
1174ff7e0accSmrg
1175ff7e0accSmrg/*
1176ff7e0accSmrg * Routine to display all events in an event mask
1177ff7e0accSmrg */
1178ff7e0accSmrgstatic const binding _event_mask_names[] = {
1179863f95b1Smrg	{ XCB_EVENT_MASK_KEY_PRESS,		"KeyPress" },
1180863f95b1Smrg	{ XCB_EVENT_MASK_KEY_RELEASE,		"KeyRelease" },
1181863f95b1Smrg	{ XCB_EVENT_MASK_BUTTON_PRESS,		"ButtonPress" },
1182863f95b1Smrg	{ XCB_EVENT_MASK_BUTTON_RELEASE,	"ButtonRelease" },
1183863f95b1Smrg	{ XCB_EVENT_MASK_ENTER_WINDOW,		"EnterWindow" },
1184863f95b1Smrg	{ XCB_EVENT_MASK_LEAVE_WINDOW,		"LeaveWindow" },
1185863f95b1Smrg	{ XCB_EVENT_MASK_POINTER_MOTION,	"PointerMotion" },
1186863f95b1Smrg	{ XCB_EVENT_MASK_POINTER_MOTION_HINT,	"PointerMotionHint" },
1187863f95b1Smrg	{ XCB_EVENT_MASK_BUTTON_1_MOTION,	"Button1Motion" },
1188863f95b1Smrg	{ XCB_EVENT_MASK_BUTTON_2_MOTION,	"Button2Motion" },
1189863f95b1Smrg	{ XCB_EVENT_MASK_BUTTON_3_MOTION,	"Button3Motion" },
1190863f95b1Smrg	{ XCB_EVENT_MASK_BUTTON_4_MOTION,	"Button4Motion" },
1191863f95b1Smrg	{ XCB_EVENT_MASK_BUTTON_5_MOTION,	"Button5Motion" },
1192863f95b1Smrg	{ XCB_EVENT_MASK_BUTTON_MOTION,		"ButtonMotion" },
1193863f95b1Smrg	{ XCB_EVENT_MASK_KEYMAP_STATE,		"KeymapState" },
1194863f95b1Smrg	{ XCB_EVENT_MASK_EXPOSURE,		"Exposure" },
1195863f95b1Smrg	{ XCB_EVENT_MASK_VISIBILITY_CHANGE,	"VisibilityChange" },
1196863f95b1Smrg	{ XCB_EVENT_MASK_STRUCTURE_NOTIFY,	"StructureNotify" },
1197863f95b1Smrg	{ XCB_EVENT_MASK_RESIZE_REDIRECT,	"ResizeRedirect" },
1198863f95b1Smrg	{ XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY,	"SubstructureNotify" },
1199863f95b1Smrg	{ XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,	"SubstructureRedirect" },
1200863f95b1Smrg	{ XCB_EVENT_MASK_FOCUS_CHANGE,		"FocusChange" },
1201863f95b1Smrg	{ XCB_EVENT_MASK_PROPERTY_CHANGE,	"PropertyChange" },
1202863f95b1Smrg	{ XCB_EVENT_MASK_COLOR_MAP_CHANGE,	"ColormapChange" },
1203863f95b1Smrg	{ XCB_EVENT_MASK_OWNER_GRAB_BUTTON,	"OwnerGrabButton" },
120410998002Smrg	{ 0, NULL } };
1205ff7e0accSmrg
1206ff7e0accSmrgstatic void
1207863f95b1SmrgDisplay_Event_Mask (long mask)
1208ff7e0accSmrg{
1209863f95b1Smrg    long bit, bit_mask;
1210ff7e0accSmrg
1211863f95b1Smrg    for (bit=0, bit_mask=1; bit < sizeof(long)*8; bit++, bit_mask <<= 1)
1212863f95b1Smrg	if (mask & bit_mask)
1213863f95b1Smrg	    printf ("      %s\n",
1214863f95b1Smrg		    LookupL (bit_mask, _event_mask_names));
1215ff7e0accSmrg}
1216ff7e0accSmrg
1217ff7e0accSmrg
1218ff7e0accSmrg/*
1219ff7e0accSmrg * Display info on events
1220863f95b1Smrg *
1221863f95b1Smrg * Requires wininfo members initialized:
1222863f95b1Smrg *   window, attr_cookie (or win_attributes)
1223ff7e0accSmrg */
1224ff7e0accSmrgstatic void
1225863f95b1SmrgDisplay_Events_Info (struct wininfo *w)
1226ff7e0accSmrg{
1227863f95b1Smrg    xcb_get_window_attributes_reply_t *win_attributes
1228863f95b1Smrg	= fetch_win_attributes (w);
1229ff7e0accSmrg
1230863f95b1Smrg    printf ("\n");
1231863f95b1Smrg    printf ("  Someone wants these events:\n");
1232863f95b1Smrg    Display_Event_Mask (win_attributes->all_event_masks);
1233ff7e0accSmrg
1234863f95b1Smrg    printf ("  Do not propagate these events:\n");
1235863f95b1Smrg    Display_Event_Mask (win_attributes->do_not_propagate_mask);
1236ff7e0accSmrg
1237863f95b1Smrg    printf ("  Override redirection?: %s\n",
1238863f95b1Smrg	    Lookup (win_attributes->override_redirect, _bool));
1239ff7e0accSmrg}
1240ff7e0accSmrg
1241ff7e0accSmrg
1242ff7e0accSmrg  /* left out visual stuff */
1243ff7e0accSmrg  /* left out colormap */
1244ff7e0accSmrg  /* left out map_installed */
1245ff7e0accSmrg
1246ff7e0accSmrg
1247ff7e0accSmrg/*
1248ff7e0accSmrg * Display root, parent, and (recursively) children information
1249ff7e0accSmrg * recurse - true to show children information
1250863f95b1Smrg *
1251863f95b1Smrg * Requires wininfo members initialized: window, tree_cookie
1252ff7e0accSmrg */
1253ff7e0accSmrgstatic void
1254863f95b1SmrgDisplay_Tree_Info (struct wininfo *w, int recurse)
1255ff7e0accSmrg{
1256863f95b1Smrg    display_tree_info_1 (w, recurse, 0);
1257ff7e0accSmrg}
1258ff7e0accSmrg
1259ff7e0accSmrg/*
1260ff7e0accSmrg * level - recursion level
1261ff7e0accSmrg */
1262ff7e0accSmrgstatic void
1263863f95b1Smrgdisplay_tree_info_1 (struct wininfo *w, int recurse, int level)
1264ff7e0accSmrg{
1265863f95b1Smrg    int i, j;
1266863f95b1Smrg    unsigned int num_children;
1267863f95b1Smrg    xcb_query_tree_reply_t *tree;
1268863f95b1Smrg
1269863f95b1Smrg    tree = xcb_query_tree_reply (dpy, w->tree_cookie, &err);
1270863f95b1Smrg    if (!tree) {
1271863f95b1Smrg	Print_X_Error (dpy, err);
1272863f95b1Smrg	Fatal_Error ("Can't query window tree.");
1273863f95b1Smrg    }
1274ff7e0accSmrg
1275863f95b1Smrg    if (level == 0) {
1276863f95b1Smrg	struct wininfo rw, pw;
1277863f95b1Smrg	rw.window = tree->root;
1278863f95b1Smrg	rw.net_wm_name_cookie = get_net_wm_name (dpy, rw.window);
1279853aa076Smrg	rw.wm_name_cookie = xcb_icccm_get_wm_name (dpy, rw.window);
1280863f95b1Smrg	pw.window = tree->parent;
1281863f95b1Smrg	pw.net_wm_name_cookie = get_net_wm_name (dpy, pw.window);
1282853aa076Smrg	pw.wm_name_cookie = xcb_icccm_get_wm_name (dpy, pw.window);
1283863f95b1Smrg	xcb_flush (dpy);
1284863f95b1Smrg
1285863f95b1Smrg	printf ("\n");
1286863f95b1Smrg	printf ("  Root window id: ");
1287863f95b1Smrg	Display_Window_Id (&rw, True);
1288863f95b1Smrg	printf ("  Parent window id: ");
1289863f95b1Smrg	Display_Window_Id (&pw, True);
1290863f95b1Smrg    }
1291863f95b1Smrg
1292863f95b1Smrg    num_children = xcb_query_tree_children_length (tree);
1293ff7e0accSmrg
1294863f95b1Smrg    if (level == 0  ||  num_children > 0) {
1295863f95b1Smrg	printf ("     ");
1296863f95b1Smrg	for (j = 0; j < level; j++) printf ("   ");
1297863f95b1Smrg	printf ("%d child%s%s\n", num_children, num_children == 1 ? "" : "ren",
1298863f95b1Smrg		num_children ? ":" : ".");
1299863f95b1Smrg    }
1300863f95b1Smrg
1301863f95b1Smrg    if (num_children > 0) {
1302863f95b1Smrg	xcb_window_t *child_list = xcb_query_tree_children (tree);
1303863f95b1Smrg	struct wininfo *children
1304863f95b1Smrg	    = calloc (num_children, sizeof(struct wininfo));
1305863f95b1Smrg
1306863f95b1Smrg	if (children == NULL)
1307863f95b1Smrg	    Fatal_Error ("Failed to allocate memory in display_tree_info");
1308863f95b1Smrg
1309863f95b1Smrg	for (i = (int)num_children - 1; i >= 0; i--) {
1310863f95b1Smrg	    struct wininfo *cw = &children[i];
1311863f95b1Smrg
1312863f95b1Smrg	    cw->window = child_list[i];
1313863f95b1Smrg	    cw->net_wm_name_cookie = get_net_wm_name (dpy, child_list[i]);
1314853aa076Smrg	    cw->wm_name_cookie = xcb_icccm_get_wm_name (dpy, child_list[i]);
1315853aa076Smrg	    cw->wm_class_cookie = xcb_icccm_get_wm_class (dpy, child_list[i]);
1316863f95b1Smrg	    cw->geometry_cookie = xcb_get_geometry (dpy, child_list[i]);
1317863f95b1Smrg	    cw->trans_coords_cookie = xcb_translate_coordinates
1318863f95b1Smrg		(dpy, child_list[i], tree->root, 0, 0);
1319863f95b1Smrg	    if (recurse)
1320863f95b1Smrg		cw->tree_cookie = xcb_query_tree (dpy, child_list[i]);
1321ff7e0accSmrg	}
1322863f95b1Smrg	xcb_flush (dpy);
1323863f95b1Smrg
1324863f95b1Smrg	for (i = (int)num_children - 1; i >= 0; i--) {
1325863f95b1Smrg	    struct wininfo *cw = &children[i];
1326863f95b1Smrg	    Bool got_wm_class = False;
1327863f95b1Smrg	    char *instance_name = NULL, *class_name = NULL;
1328863f95b1Smrg	    int instance_name_len, class_name_len;
1329863f95b1Smrg#ifdef USE_XCB_ICCCM
1330853aa076Smrg	    xcb_icccm_get_wm_class_reply_t classhint;
1331863f95b1Smrg#else
1332863f95b1Smrg	    xcb_get_property_reply_t *classprop;
1333863f95b1Smrg#endif
1334863f95b1Smrg	    xcb_get_geometry_reply_t *geometry;
1335863f95b1Smrg
1336863f95b1Smrg	    printf ("     ");
1337863f95b1Smrg	    for (j = 0; j < level; j++) printf ("   ");
1338863f95b1Smrg	    Display_Window_Id (cw, False);
1339863f95b1Smrg	    printf (": (");
1340863f95b1Smrg
1341863f95b1Smrg#ifdef USE_XCB_ICCCM
1342853aa076Smrg	    if (xcb_icccm_get_wm_class_reply (dpy, cw->wm_class_cookie,
1343863f95b1Smrg					&classhint, NULL)) {
1344863f95b1Smrg		got_wm_class = True;
1345863f95b1Smrg		instance_name = classhint.instance_name;
1346863f95b1Smrg		class_name = classhint.class_name;
1347863f95b1Smrg		instance_name_len = strlen(instance_name);
1348863f95b1Smrg		class_name_len = strlen(class_name);
1349863f95b1Smrg	    }
1350863f95b1Smrg#else
1351863f95b1Smrg	    classprop = xcb_get_property_reply
1352863f95b1Smrg		(dpy, cw->wm_class_cookie, NULL);
1353863f95b1Smrg	    if (classprop) {
1354863f95b1Smrg		if (classprop->type == XCB_ATOM_STRING &&
1355863f95b1Smrg		    classprop->format == 8) {
1356863f95b1Smrg		    int proplen = xcb_get_property_value_length (classprop);
1357863f95b1Smrg
1358863f95b1Smrg		    instance_name = xcb_get_property_value (classprop);
1359863f95b1Smrg		    instance_name_len = strnlen (instance_name, proplen);
1360863f95b1Smrg		    if (instance_name_len < proplen) {
1361863f95b1Smrg			class_name = instance_name + instance_name_len + 1;
1362863f95b1Smrg			class_name_len = strnlen
1363863f95b1Smrg			    (class_name, proplen - (instance_name_len + 1));
1364863f95b1Smrg		    } else
1365863f95b1Smrg			class_name_len = 0;
1366863f95b1Smrg		    got_wm_class = True;
1367863f95b1Smrg		}
1368863f95b1Smrg		else
1369863f95b1Smrg		    free (classprop);
1370863f95b1Smrg	    }
1371863f95b1Smrg#endif
1372863f95b1Smrg
1373863f95b1Smrg	    if (got_wm_class) {
1374863f95b1Smrg		if (instance_name)
1375863f95b1Smrg		    printf ("\"%.*s\" ", instance_name_len, instance_name);
1376863f95b1Smrg		else
1377863f95b1Smrg		    printf ("(none) ");
1378863f95b1Smrg
1379863f95b1Smrg		if (class_name)
1380863f95b1Smrg		    printf ("\"%.*s\") ",  class_name_len, class_name);
1381863f95b1Smrg		else
1382863f95b1Smrg		    printf ("(none)) ");
1383863f95b1Smrg
1384863f95b1Smrg#ifdef USE_XCB_ICCCM
1385853aa076Smrg		xcb_icccm_get_wm_class_reply_wipe (&classhint);
1386863f95b1Smrg#else
1387863f95b1Smrg		free (classprop);
1388863f95b1Smrg#endif
1389863f95b1Smrg	    } else
1390863f95b1Smrg		printf (") ");
1391863f95b1Smrg
1392863f95b1Smrg	    geometry = xcb_get_geometry_reply(dpy, cw->geometry_cookie, &err);
1393863f95b1Smrg	    if (geometry) {
1394863f95b1Smrg		xcb_translate_coordinates_reply_t *trans_coords;
1395863f95b1Smrg
1396863f95b1Smrg		printf (" %ux%u+%d+%d", geometry->width, geometry->height,
1397863f95b1Smrg					geometry->x, geometry->y);
1398863f95b1Smrg
1399863f95b1Smrg		trans_coords = xcb_translate_coordinates_reply
1400863f95b1Smrg		    (dpy, cw->trans_coords_cookie, &err);
1401863f95b1Smrg
1402863f95b1Smrg		if (trans_coords) {
1403863f95b1Smrg		    int16_t abs_x = (int16_t) trans_coords->dst_x;
1404863f95b1Smrg		    int16_t abs_y = (int16_t) trans_coords->dst_y;
1405863f95b1Smrg		    int border = geometry->border_width;
1406863f95b1Smrg
1407863f95b1Smrg		    printf ("  +%d+%d", abs_x - border, abs_y - border);
1408863f95b1Smrg		    free (trans_coords);
1409863f95b1Smrg		} else if (err) {
1410863f95b1Smrg		    Print_X_Error (dpy, err);
1411863f95b1Smrg		}
1412863f95b1Smrg
1413863f95b1Smrg		free (geometry);
1414863f95b1Smrg	    } else if (err) {
1415863f95b1Smrg		Print_X_Error (dpy, err);
1416863f95b1Smrg	    }
1417863f95b1Smrg	    printf ("\n");
1418863f95b1Smrg
1419863f95b1Smrg	    if (recurse)
1420863f95b1Smrg		display_tree_info_1 (cw, 1, level+1);
1421863f95b1Smrg
1422863f95b1Smrg	    wininfo_wipe (cw);
1423863f95b1Smrg	}
1424863f95b1Smrg	free (children);
1425ff7e0accSmrg    }
1426ff7e0accSmrg
1427863f95b1Smrg    free (tree); /* includes storage for child_list[] */
1428ff7e0accSmrg}
1429ff7e0accSmrg
1430ff7e0accSmrg
1431ff7e0accSmrg/*
1432ff7e0accSmrg * Display a set of size hints
1433ff7e0accSmrg */
1434ff7e0accSmrgstatic void
1435863f95b1SmrgDisplay_Hints (xcb_size_hints_t *hints)
1436ff7e0accSmrg{
1437863f95b1Smrg    long flags;
1438ff7e0accSmrg
1439863f95b1Smrg    flags = hints->flags;
1440ff7e0accSmrg
1441853aa076Smrg    if (flags & XCB_ICCCM_SIZE_HINT_US_POSITION)
1442863f95b1Smrg	printf ("      User supplied location: %s, %s\n",
1443863f95b1Smrg		xscale (hints->x), yscale (hints->y));
1444ff7e0accSmrg
1445853aa076Smrg    if (flags & XCB_ICCCM_SIZE_HINT_P_POSITION)
1446863f95b1Smrg	printf ("      Program supplied location: %s, %s\n",
1447863f95b1Smrg		xscale (hints->x), yscale (hints->y));
1448ff7e0accSmrg
1449853aa076Smrg    if (flags & XCB_ICCCM_SIZE_HINT_US_SIZE) {
1450863f95b1Smrg	printf ("      User supplied size: %s by %s\n",
1451863f95b1Smrg		xscale (hints->width), yscale (hints->height));
1452863f95b1Smrg    }
1453ff7e0accSmrg
1454853aa076Smrg    if (flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
1455863f95b1Smrg	printf ("      Program supplied size: %s by %s\n",
1456863f95b1Smrg		xscale (hints->width), yscale (hints->height));
1457ff7e0accSmrg
1458853aa076Smrg    if (flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
1459863f95b1Smrg	printf ("      Program supplied minimum size: %s by %s\n",
1460863f95b1Smrg		xscale (hints->min_width), yscale (hints->min_height));
1461ff7e0accSmrg
1462853aa076Smrg    if (flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
1463863f95b1Smrg	printf ("      Program supplied maximum size: %s by %s\n",
1464863f95b1Smrg		xscale (hints->max_width), yscale (hints->max_height));
1465ff7e0accSmrg
1466853aa076Smrg    if (flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
1467863f95b1Smrg	printf ("      Program supplied base size: %s by %s\n",
1468863f95b1Smrg		xscale (hints->base_width), yscale (hints->base_height));
1469863f95b1Smrg    }
1470863f95b1Smrg
1471853aa076Smrg    if (flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC) {
1472863f95b1Smrg	printf ("      Program supplied x resize increment: %s\n",
1473863f95b1Smrg		xscale (hints->width_inc));
1474863f95b1Smrg	printf ("      Program supplied y resize increment: %s\n",
1475863f95b1Smrg		yscale (hints->height_inc));
1476863f95b1Smrg	if (hints->width_inc != 0 && hints->height_inc != 0) {
1477853aa076Smrg	    if (flags & XCB_ICCCM_SIZE_HINT_US_SIZE)
1478863f95b1Smrg		printf ("      User supplied size in resize increments:  %s by %s\n",
1479863f95b1Smrg			(xscale (hints->width / hints->width_inc)),
1480863f95b1Smrg			(yscale (hints->height / hints->height_inc)));
1481853aa076Smrg	    if (flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
1482863f95b1Smrg		printf ("      Program supplied size in resize increments:  %s by %s\n",
1483863f95b1Smrg			(xscale (hints->width / hints->width_inc)),
1484863f95b1Smrg			(yscale (hints->height / hints->height_inc)));
1485853aa076Smrg	    if (flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
1486863f95b1Smrg		printf ("      Program supplied minimum size in resize increments: %s by %s\n",
1487863f95b1Smrg			xscale (hints->min_width / hints->width_inc), yscale (hints->min_height / hints->height_inc));
1488853aa076Smrg	    if (flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE)
1489863f95b1Smrg		printf ("      Program supplied base size in resize increments:  %s by %s\n",
1490863f95b1Smrg			(xscale (hints->base_width / hints->width_inc)),
1491863f95b1Smrg			(yscale (hints->base_height / hints->height_inc)));
1492ff7e0accSmrg	}
1493863f95b1Smrg    }
1494863f95b1Smrg
1495853aa076Smrg    if (flags & XCB_ICCCM_SIZE_HINT_P_ASPECT) {
1496863f95b1Smrg	printf ("      Program supplied min aspect ratio: %s/%s\n",
1497863f95b1Smrg		xscale (hints->min_aspect_num), yscale (hints->min_aspect_den));
1498863f95b1Smrg	printf ("      Program supplied max aspect ratio: %s/%s\n",
1499863f95b1Smrg		xscale (hints->max_aspect_num), yscale (hints->max_aspect_den));
1500863f95b1Smrg    }
1501863f95b1Smrg
1502853aa076Smrg    if (flags & XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY) {
1503863f95b1Smrg	printf ("      Program supplied window gravity: %s\n",
1504863f95b1Smrg		Lookup (hints->win_gravity, _gravities));
1505863f95b1Smrg    }
1506ff7e0accSmrg}
1507ff7e0accSmrg
1508ff7e0accSmrg
1509ff7e0accSmrg/*
1510ff7e0accSmrg * Display Size Hints info
1511ff7e0accSmrg */
1512ff7e0accSmrgstatic void
1513863f95b1SmrgDisplay_Size_Hints (struct wininfo *w)
1514ff7e0accSmrg{
1515863f95b1Smrg    xcb_size_hints_t hints;
1516ff7e0accSmrg
1517863f95b1Smrg    printf ("\n");
1518863f95b1Smrg    if (!fetch_normal_hints (w, &hints))
1519863f95b1Smrg	printf ("  No normal window size hints defined\n");
1520863f95b1Smrg    else {
1521863f95b1Smrg	printf ("  Normal window size hints:\n");
1522863f95b1Smrg	Display_Hints (&hints);
1523863f95b1Smrg    }
1524863f95b1Smrg
1525853aa076Smrg    if (!xcb_icccm_get_wm_size_hints_reply (dpy, w->zoom_cookie, &hints, NULL))
1526863f95b1Smrg	printf ("  No zoom window size hints defined\n");
1527863f95b1Smrg    else {
1528863f95b1Smrg	printf ("  Zoom window size hints:\n");
1529863f95b1Smrg	Display_Hints (&hints);
1530863f95b1Smrg    }
1531ff7e0accSmrg}
1532ff7e0accSmrg
1533ff7e0accSmrg
1534ff7e0accSmrgstatic void
1535863f95b1SmrgDisplay_Window_Shape (xcb_window_t window)
1536ff7e0accSmrg{
1537863f95b1Smrg    const xcb_query_extension_reply_t *shape_query;
1538863f95b1Smrg    xcb_shape_query_extents_cookie_t extents_cookie;
1539863f95b1Smrg    xcb_shape_query_extents_reply_t *extents;
1540ff7e0accSmrg
1541863f95b1Smrg    shape_query = xcb_get_extension_data (dpy, &xcb_shape_id);
1542863f95b1Smrg    if (!shape_query->present)
1543ff7e0accSmrg	return;
1544ff7e0accSmrg
1545863f95b1Smrg    printf ("\n");
1546863f95b1Smrg
1547863f95b1Smrg    extents_cookie = xcb_shape_query_extents (dpy, window);
1548863f95b1Smrg    extents = xcb_shape_query_extents_reply (dpy, extents_cookie, &err);
1549863f95b1Smrg
1550863f95b1Smrg    if (!extents) {
1551863f95b1Smrg	if (err)
1552863f95b1Smrg	    Print_X_Error (dpy, err);
1553863f95b1Smrg	else
1554863f95b1Smrg	{
1555863f95b1Smrg	    printf ("  No window shape defined\n");
1556863f95b1Smrg	    printf ("  No border shape defined\n");
1557863f95b1Smrg	}
1558863f95b1Smrg	return;
1559863f95b1Smrg    }
1560863f95b1Smrg
1561863f95b1Smrg    if (!extents->bounding_shaped)
1562863f95b1Smrg	printf ("  No window shape defined\n");
1563ff7e0accSmrg    else {
1564863f95b1Smrg	printf ("  Window shape extents:  %sx%s",
1565863f95b1Smrg		xscale (extents->bounding_shape_extents_width),
1566863f95b1Smrg		yscale (extents->bounding_shape_extents_height));
1567863f95b1Smrg	printf ("+%s+%s\n",
1568863f95b1Smrg		xscale (extents->bounding_shape_extents_x),
1569863f95b1Smrg		yscale (extents->bounding_shape_extents_y));
1570ff7e0accSmrg    }
1571863f95b1Smrg    if (!extents->clip_shaped)
1572863f95b1Smrg	printf ("  No border shape defined\n");
1573ff7e0accSmrg    else {
1574863f95b1Smrg	printf ("  Border shape extents:  %sx%s",
1575863f95b1Smrg		xscale (extents->clip_shape_extents_width),
1576863f95b1Smrg		yscale (extents->clip_shape_extents_height));
1577863f95b1Smrg	printf ("+%s+%s\n",
1578863f95b1Smrg		xscale (extents->clip_shape_extents_x),
1579863f95b1Smrg		yscale (extents->clip_shape_extents_y));
1580ff7e0accSmrg    }
1581863f95b1Smrg
1582863f95b1Smrg    free (extents);
1583ff7e0accSmrg}
1584ff7e0accSmrg
1585ff7e0accSmrg/*
1586ff7e0accSmrg * Display Window Manager Info
1587863f95b1Smrg *
1588863f95b1Smrg * Requires wininfo members initialized:
1589863f95b1Smrg *   window, hints_cookie
1590ff7e0accSmrg */
1591ff7e0accSmrgstatic const binding _state_hints[] = {
1592853aa076Smrg	{ XCB_ICCCM_WM_STATE_WITHDRAWN, "Withdrawn State" },
1593853aa076Smrg	{ XCB_ICCCM_WM_STATE_NORMAL, "Normal State" },
1594853aa076Smrg	{ XCB_ICCCM_WM_STATE_ICONIC, "Iconic State" },
1595863f95b1Smrg/* xwininfo previously also reported the ZoomState & InactiveState,
1596863f95b1Smrg   but ICCCM declared those obsolete long ago */
159710998002Smrg	{ 0, NULL } };
1598ff7e0accSmrg
1599863f95b1Smrg#ifndef USE_XCB_ICCCM
1600863f95b1Smrgstatic Bool
1601863f95b1Smrgwm_hints_reply (xcb_connection_t *dpy, xcb_get_property_cookie_t cookie,
1602863f95b1Smrg		wm_hints_t *hints_return, xcb_generic_error_t **err)
1603863f95b1Smrg{
1604863f95b1Smrg    xcb_get_property_reply_t *prop = xcb_get_property_reply (dpy, cookie, err);
1605863f95b1Smrg    int length;
1606863f95b1Smrg
1607863f95b1Smrg    if (!prop || (prop->type != XCB_ATOM_WM_HINTS) || (prop->format != 32)) {
1608863f95b1Smrg	free (prop);
1609863f95b1Smrg	return False;
1610863f95b1Smrg    }
1611863f95b1Smrg
1612863f95b1Smrg    memset (hints_return, 0, sizeof(wm_hints_t));
1613863f95b1Smrg
1614863f95b1Smrg    length = xcb_get_property_value_length(prop);
1615863f95b1Smrg    if (length > sizeof(wm_hints_t))
1616863f95b1Smrg	length = sizeof(wm_hints_t);
1617863f95b1Smrg    memcpy (hints_return, xcb_get_property_value (prop), length);
1618863f95b1Smrg
1619863f95b1Smrg    free (prop);
1620863f95b1Smrg    return True;
1621863f95b1Smrg}
1622863f95b1Smrg
1623853aa076Smrg#define xcb_icccm_get_wm_hints_reply wm_hints_reply
1624863f95b1Smrg#endif
1625863f95b1Smrg
1626ff7e0accSmrgstatic void
1627863f95b1SmrgDisplay_WM_Info (struct wininfo *w)
1628ff7e0accSmrg{
1629853aa076Smrg    xcb_icccm_wm_hints_t wmhints;
1630863f95b1Smrg    long flags;
1631863f95b1Smrg    xcb_get_property_reply_t *prop;
1632863f95b1Smrg    int i;
1633863f95b1Smrg
1634863f95b1Smrg    printf ("\n");
1635853aa076Smrg    if (!xcb_icccm_get_wm_hints_reply(dpy, w->hints_cookie, &wmhints, &err))
1636863f95b1Smrg    {
1637863f95b1Smrg	printf ("  No window manager hints defined\n");
1638863f95b1Smrg	if (err)
1639863f95b1Smrg	    Print_X_Error (dpy, err);
1640863f95b1Smrg	flags = 0;
1641863f95b1Smrg    } else
1642863f95b1Smrg	flags = wmhints.flags;
1643863f95b1Smrg
1644863f95b1Smrg    printf ("  Window manager hints:\n");
1645863f95b1Smrg
1646853aa076Smrg    if (flags & XCB_ICCCM_WM_HINT_INPUT)
1647863f95b1Smrg	printf ("      Client accepts input or input focus: %s\n",
1648863f95b1Smrg		Lookup (wmhints.input, _bool));
1649863f95b1Smrg
1650853aa076Smrg    if (flags & XCB_ICCCM_WM_HINT_ICON_WINDOW) {
1651863f95b1Smrg	struct wininfo iw;
1652863f95b1Smrg	iw.window = wmhints.icon_window;
1653863f95b1Smrg	iw.net_wm_name_cookie = get_net_wm_name (dpy, iw.window);
1654853aa076Smrg	iw.wm_name_cookie = xcb_icccm_get_wm_name (dpy, iw.window);
1655863f95b1Smrg
1656863f95b1Smrg	printf ("      Icon window id: ");
1657863f95b1Smrg	Display_Window_Id (&iw, True);
1658863f95b1Smrg    }
1659863f95b1Smrg
1660853aa076Smrg    if (flags & XCB_ICCCM_WM_HINT_ICON_POSITION)
1661863f95b1Smrg	printf ("      Initial icon position: %s, %s\n",
1662863f95b1Smrg		xscale (wmhints.icon_x), yscale (wmhints.icon_y));
1663863f95b1Smrg
1664853aa076Smrg    if (flags & XCB_ICCCM_WM_HINT_STATE)
1665863f95b1Smrg	printf ("      Initial state is %s\n",
1666863f95b1Smrg		Lookup (wmhints.initial_state, _state_hints));
1667863f95b1Smrg
1668863f95b1Smrg    if (atom_net_wm_desktop) {
1669863f95b1Smrg	prop = xcb_get_property_reply (dpy, w->wm_desktop_cookie, NULL);
1670863f95b1Smrg	if (prop && (prop->type != XCB_NONE)) {
1671863f95b1Smrg	    uint32_t *desktop = xcb_get_property_value (prop);
1672863f95b1Smrg	    if (*desktop == 0xFFFFFFFF) {
1673863f95b1Smrg		printf ("      Displayed on all desktops\n");
1674863f95b1Smrg	    } else {
1675863f95b1Smrg		printf ("      Displayed on desktop %d\n", *desktop);
1676863f95b1Smrg	    }
1677ff7e0accSmrg	}
1678863f95b1Smrg	free (prop);
1679863f95b1Smrg    }
1680ff7e0accSmrg
1681863f95b1Smrg    if (atom_net_wm_window_type) {
1682863f95b1Smrg	prop = xcb_get_property_reply (dpy, w->wm_window_type_cookie,
1683863f95b1Smrg				       NULL);
1684863f95b1Smrg	if (prop && (prop->type != XCB_NONE) && (prop->value_len > 0)) {
1685863f95b1Smrg	    xcb_atom_t *atoms = xcb_get_property_value (prop);
1686863f95b1Smrg	    int atom_count = prop->value_len;
1687863f95b1Smrg
1688863f95b1Smrg	    if (atom_count > 0) {
1689863f95b1Smrg		printf ("      Window type:\n");
1690863f95b1Smrg		for (i = 0; i < atom_count; i++) {
1691863f95b1Smrg		    const char *atom_name = Get_Atom_Name (dpy, atoms[i]);
1692863f95b1Smrg
1693863f95b1Smrg		    if (atom_name) {
1694863f95b1Smrg			print_friendly_name ("          %s\n", atom_name,
1695863f95b1Smrg					     "_NET_WM_WINDOW_TYPE_");
1696863f95b1Smrg		    } else {
1697863f95b1Smrg			printf ("          (unresolvable ATOM 0x%x)\n",
1698863f95b1Smrg				atoms[i]);
1699863f95b1Smrg		    }
1700863f95b1Smrg		}
1701863f95b1Smrg	    }
1702863f95b1Smrg	}
1703863f95b1Smrg	free (prop);
1704863f95b1Smrg    }
1705863f95b1Smrg
1706863f95b1Smrg    if (atom_net_wm_state) {
1707863f95b1Smrg	prop = xcb_get_property_reply (dpy, w->wm_state_cookie, NULL);
1708863f95b1Smrg	if (prop && (prop->type != XCB_NONE) && (prop->value_len > 0)) {
1709863f95b1Smrg	    xcb_atom_t *atoms = xcb_get_property_value (prop);
1710863f95b1Smrg	    int atom_count = prop->value_len;
1711863f95b1Smrg
1712863f95b1Smrg	    if (atom_count > 0) {
1713863f95b1Smrg		printf ("      Window state:\n");
1714863f95b1Smrg		for (i = 0; i < atom_count; i++) {
1715863f95b1Smrg		    const char *atom_name = Get_Atom_Name (dpy, atoms[i]);
1716863f95b1Smrg
1717863f95b1Smrg		    if (atom_name) {
1718863f95b1Smrg			print_friendly_name ("          %s\n", atom_name,
1719863f95b1Smrg					     "_NET_WM_STATE_");
1720863f95b1Smrg		    } else {
1721863f95b1Smrg			printf ("          (unresolvable ATOM 0x%x)\n",
1722863f95b1Smrg				atoms[i]);
1723863f95b1Smrg		    }
1724863f95b1Smrg		}
1725863f95b1Smrg	    }
1726863f95b1Smrg	}
1727863f95b1Smrg	free (prop);
1728863f95b1Smrg    }
1729ff7e0accSmrg
1730863f95b1Smrg    if (atom_net_wm_pid) {
1731863f95b1Smrg	printf ("      Process id: ");
1732863f95b1Smrg	prop = xcb_get_property_reply (dpy, w->wm_pid_cookie, NULL);
1733863f95b1Smrg	if (prop && (prop->type == XCB_ATOM_CARDINAL)) {
1734863f95b1Smrg	    uint32_t *pid = xcb_get_property_value (prop);
1735863f95b1Smrg	    printf ("%d", *pid);
1736863f95b1Smrg	} else {
1737863f95b1Smrg	    printf ("(unknown)");
1738863f95b1Smrg	}
1739863f95b1Smrg	free (prop);
1740ff7e0accSmrg
1741863f95b1Smrg	prop = xcb_get_property_reply (dpy, w->wm_client_machine_cookie, NULL);
1742863f95b1Smrg	if (prop && (prop->type == XCB_ATOM_STRING)) {
1743863f95b1Smrg	    const char *hostname = xcb_get_property_value (prop);
1744863f95b1Smrg	    int hostname_len = xcb_get_property_value_length (prop);
1745863f95b1Smrg	    printf (" on host %.*s", hostname_len, hostname);
1746ff7e0accSmrg	}
1747863f95b1Smrg	printf ("\n");
1748863f95b1Smrg	free (prop);
1749863f95b1Smrg    }
1750863f95b1Smrg
1751863f95b1Smrg    if (atom_net_frame_extents) {
1752863f95b1Smrg	prop = xcb_get_property_reply (dpy, w->frame_extents_cookie, NULL);
1753863f95b1Smrg	if (prop && (prop->type == XCB_ATOM_CARDINAL)
1754863f95b1Smrg	    && (prop->value_len == 4)) {
1755863f95b1Smrg	    uint32_t *extents = xcb_get_property_value (prop);
1756863f95b1Smrg
1757863f95b1Smrg	    printf ("      Frame extents: %d, %d, %d, %d\n",
1758863f95b1Smrg		    extents[0], extents[1], extents[2], extents[3]);
1759863f95b1Smrg	}
1760863f95b1Smrg	free (prop);
1761863f95b1Smrg    }
1762863f95b1Smrg}
1763863f95b1Smrg
1764863f95b1Smrg/* Frees all members of a wininfo struct, but not the struct itself */
1765863f95b1Smrgstatic void
1766863f95b1Smrgwininfo_wipe (struct wininfo *w)
1767863f95b1Smrg{
1768863f95b1Smrg    free (w->geometry);
1769863f95b1Smrg    free (w->win_attributes);
1770863f95b1Smrg    free (w->normal_hints);
1771863f95b1Smrg}
1772863f95b1Smrg
1773863f95b1Smrg/* Gets UTF-8 encoded EMWH property _NET_WM_NAME for a window */
1774863f95b1Smrgstatic xcb_get_property_cookie_t
1775863f95b1Smrgget_net_wm_name (xcb_connection_t *dpy, xcb_window_t win)
1776863f95b1Smrg{
1777863f95b1Smrg    if (!atom_net_wm_name)
1778863f95b1Smrg	atom_net_wm_name = Get_Atom (dpy, "_NET_WM_NAME");
1779863f95b1Smrg
1780863f95b1Smrg    if (!atom_utf8_string)
1781863f95b1Smrg	atom_utf8_string = Get_Atom (dpy, "UTF8_STRING");
1782863f95b1Smrg
1783863f95b1Smrg    if (atom_net_wm_name && atom_utf8_string)
1784863f95b1Smrg	return xcb_get_property (dpy, False, win, atom_net_wm_name,
1785863f95b1Smrg				 atom_utf8_string, 0, BUFSIZ);
1786863f95b1Smrg    else {
1787863f95b1Smrg	xcb_get_property_cookie_t dummy = { 0 };
1788863f95b1Smrg	return dummy;
1789863f95b1Smrg    }
1790863f95b1Smrg}
1791863f95b1Smrg
1792863f95b1Smrg/* [Copied from code added by Yang Zhao to xprop/xprop.c]
1793863f95b1Smrg *
1794863f95b1Smrg * Validate a string as UTF-8 encoded according to RFC 3629
1795863f95b1Smrg *
1796863f95b1Smrg * Simply, a unicode code point (up to 21-bits long) is encoded as follows:
1797863f95b1Smrg *
1798863f95b1Smrg *    Char. number range  |        UTF-8 octet sequence
1799863f95b1Smrg *       (hexadecimal)    |              (binary)
1800863f95b1Smrg *    --------------------+---------------------------------------------
1801863f95b1Smrg *    0000 0000-0000 007F | 0xxxxxxx
1802863f95b1Smrg *    0000 0080-0000 07FF | 110xxxxx 10xxxxxx
1803863f95b1Smrg *    0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
1804863f95b1Smrg *    0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1805863f95b1Smrg *
1806863f95b1Smrg * Validation is done left-to-right, and an error condition, if any, refers to
1807863f95b1Smrg * only the left-most problem in the string.
1808863f95b1Smrg *
1809863f95b1Smrg * Return values:
1810863f95b1Smrg *   UTF8_VALID: Valid UTF-8 encoded string
1811863f95b1Smrg *   UTF8_OVERLONG: Using more bytes than needed for a code point
1812863f95b1Smrg *   UTF8_SHORT_TAIL: Not enough bytes in a multi-byte sequence
1813863f95b1Smrg *   UTF8_LONG_TAIL: Too many bytes in a multi-byte sequence
1814863f95b1Smrg *   UTF8_FORBIDDEN_VALUE: Forbidden prefix or code point outside 0x10FFFF
1815863f95b1Smrg */
1816863f95b1Smrg#define UTF8_VALID 0
1817863f95b1Smrg#define UTF8_FORBIDDEN_VALUE 1
1818863f95b1Smrg#define UTF8_OVERLONG 2
1819863f95b1Smrg#define UTF8_SHORT_TAIL 3
1820863f95b1Smrg#define UTF8_LONG_TAIL 4
1821863f95b1Smrgstatic int
1822863f95b1Smrgis_valid_utf8 (const char *string, int len)
1823863f95b1Smrg{
1824863f95b1Smrg    unsigned long codepoint;
1825863f95b1Smrg    int rem, i;
1826863f95b1Smrg    unsigned char c;
1827863f95b1Smrg
1828863f95b1Smrg    rem = 0;
1829863f95b1Smrg    for (i = 0; i < len; i++) {
1830863f95b1Smrg	c = (unsigned char) string[i];
1831863f95b1Smrg
1832863f95b1Smrg	/* Order of type check:
1833863f95b1Smrg	 *   - Single byte code point
1834863f95b1Smrg	 *   - Non-starting byte of multi-byte sequence
1835863f95b1Smrg	 *   - Start of 2-byte sequence
1836863f95b1Smrg	 *   - Start of 3-byte sequence
1837863f95b1Smrg	 *   - Start of 4-byte sequence
1838863f95b1Smrg	 */
1839863f95b1Smrg	if (!(c & 0x80)) {
1840863f95b1Smrg	    if (rem > 0) return UTF8_SHORT_TAIL;
1841863f95b1Smrg	    rem = 0;
1842863f95b1Smrg	    codepoint = c;
1843863f95b1Smrg	} else if ((c & 0xC0) == 0x80) {
1844863f95b1Smrg	    if (rem == 0) return UTF8_LONG_TAIL;
1845863f95b1Smrg	    rem--;
1846863f95b1Smrg	    codepoint |= (c & 0x3F) << (rem * 6);
1847863f95b1Smrg	    if (codepoint == 0) return UTF8_OVERLONG;
1848863f95b1Smrg	} else if ((c & 0xE0) == 0xC0) {
1849863f95b1Smrg	    if (rem > 0) return UTF8_SHORT_TAIL;
1850863f95b1Smrg	    rem = 1;
1851863f95b1Smrg	    codepoint = (c & 0x1F) << 6;
1852863f95b1Smrg	    if (codepoint == 0) return UTF8_OVERLONG;
1853863f95b1Smrg	} else if ((c & 0xF0) == 0xE0) {
1854863f95b1Smrg	    if (rem > 0) return UTF8_SHORT_TAIL;
1855863f95b1Smrg	    rem = 2;
1856863f95b1Smrg	    codepoint = (c & 0x0F) << 12;
1857863f95b1Smrg	} else if ((c & 0xF8) == 0xF0) {
1858863f95b1Smrg	    if (rem > 0) return UTF8_SHORT_TAIL;
1859863f95b1Smrg	    rem = 3;
1860863f95b1Smrg	    codepoint = (c & 0x07) << 18;
1861863f95b1Smrg	    if (codepoint > 0x10FFFF) return UTF8_FORBIDDEN_VALUE;
1862863f95b1Smrg	} else
1863863f95b1Smrg	    return UTF8_FORBIDDEN_VALUE;
1864863f95b1Smrg    }
1865ff7e0accSmrg
1866863f95b1Smrg    return UTF8_VALID;
1867863f95b1Smrg}
1868863f95b1Smrg
1869863f95b1Smrg/*
1870863f95b1Smrg * Converts a UTF-8 encoded string to the current locale encoding,
1871863f95b1Smrg * if possible, and prints it, with prefix before and suffix after.
1872863f95b1Smrg * Length of the string is specified in bytes, or -1 for going until '\0'
1873863f95b1Smrg */
1874863f95b1Smrgstatic void
1875863f95b1Smrgprint_utf8 (const char *prefix, char *u8str, size_t length, const char *suffix)
1876863f95b1Smrg{
1877863f95b1Smrg    size_t inlen = length;
1878863f95b1Smrg
1879863f95b1Smrg    if (inlen < 0) {
1880863f95b1Smrg	inlen = strlen (u8str);
1881863f95b1Smrg    }
1882863f95b1Smrg
1883863f95b1Smrg    if (is_valid_utf8 (u8str, inlen) != UTF8_VALID) {
1884863f95b1Smrg	printf (" (invalid UTF8_STRING)");
1885863f95b1Smrg	return;
1886863f95b1Smrg    }
1887863f95b1Smrg
1888863f95b1Smrg    if (strcmp (user_encoding, "UTF-8") == 0) {
1889863f95b1Smrg	/* Don't need to convert */
1890863f95b1Smrg	printf ("%s", prefix);
1891863f95b1Smrg	fwrite (u8str, 1, inlen, stdout);
1892863f95b1Smrg	printf ("%s", suffix);
1893863f95b1Smrg	return;
1894863f95b1Smrg    }
1895863f95b1Smrg
1896863f95b1Smrg#ifdef HAVE_ICONV
1897863f95b1Smrg    if (!iconv_from_utf8) {
1898863f95b1Smrg	iconv_from_utf8 = iconv_open (user_encoding, "UTF-8");
1899863f95b1Smrg    }
1900863f95b1Smrg
1901863f95b1Smrg    if (iconv_from_utf8 != (iconv_t) -1) {
1902863f95b1Smrg	Bool done = True;
1903863f95b1Smrg	char *inp = u8str;
1904863f95b1Smrg	char convbuf[BUFSIZ];
1905863f95b1Smrg	int convres;
1906863f95b1Smrg
1907863f95b1Smrg	printf ("%s", prefix);
1908863f95b1Smrg	do {
1909863f95b1Smrg	    char *outp = convbuf;
1910863f95b1Smrg	    size_t outlen = sizeof(convbuf);
1911863f95b1Smrg
1912b419652fSmrg	    convres = iconv (iconv_from_utf8, (const char **)&inp, &inlen, &outp, &outlen);
1913ff7e0accSmrg
1914863f95b1Smrg	    if ((convres == -1) && (errno == E2BIG)) {
1915863f95b1Smrg		done = False;
1916863f95b1Smrg		convres = 0;
1917863f95b1Smrg	    }
1918863f95b1Smrg
1919863f95b1Smrg	    if (convres == 0) {
1920863f95b1Smrg		fwrite (convbuf, 1, sizeof(convbuf) - outlen, stdout);
1921863f95b1Smrg	    } else {
1922863f95b1Smrg		printf (" (failure in conversion from UTF8_STRING to %s)",
1923863f95b1Smrg			user_encoding);
1924863f95b1Smrg	    }
1925863f95b1Smrg	} while (!done);
1926863f95b1Smrg	printf ("%s", suffix);
1927863f95b1Smrg    } else {
1928863f95b1Smrg	printf (" (can't load iconv conversion for UTF8_STRING to %s)",
1929863f95b1Smrg		user_encoding);
1930863f95b1Smrg    }
1931863f95b1Smrg#else
1932863f95b1Smrg    printf (" (can't convert UTF8_STRING to %s)", user_encoding);
1933863f95b1Smrg#endif
1934863f95b1Smrg}
1935863f95b1Smrg
1936863f95b1Smrg/*
1937863f95b1Smrg * Takes a string such as an atom name, strips the prefix, converts
1938863f95b1Smrg * underscores to spaces, lowercases all but the first letter of each word,
1939863f95b1Smrg * and prints it.
1940863f95b1Smrg */
1941863f95b1Smrgstatic void
1942863f95b1Smrgprint_friendly_name (const char *format, const char *string,
1943863f95b1Smrg		     const char *prefix)
1944863f95b1Smrg{
1945863f95b1Smrg    const char *name_start = string;
1946863f95b1Smrg    char *lowered_name, *n;
1947863f95b1Smrg    int prefix_len = strlen (prefix);
1948863f95b1Smrg
1949863f95b1Smrg    if (strncmp (name_start, prefix, prefix_len) == 0) {
1950863f95b1Smrg	name_start += prefix_len;
1951863f95b1Smrg    }
1952863f95b1Smrg
1953863f95b1Smrg    lowered_name = strdup (name_start);
1954863f95b1Smrg    if (lowered_name) {
1955863f95b1Smrg	Bool first = True;
1956863f95b1Smrg
1957863f95b1Smrg	for (n = lowered_name ; *n != 0 ; n++) {
1958863f95b1Smrg	    if (*n == '_') {
1959863f95b1Smrg		*n = ' ';
1960863f95b1Smrg		first = True;
1961863f95b1Smrg	    } else if (first) {
1962863f95b1Smrg		first = False;
1963863f95b1Smrg	    } else {
1964863f95b1Smrg		*n = tolower(*n);
1965863f95b1Smrg	    }
1966863f95b1Smrg	}
1967863f95b1Smrg	name_start = lowered_name;
1968863f95b1Smrg    }
1969ff7e0accSmrg
1970863f95b1Smrg    printf (format, name_start);
1971863f95b1Smrg    free (lowered_name);
1972ff7e0accSmrg}
1973