11ab64890Smrg
21ab64890Smrg/***********************************************************
31ab64890Smrg
41ab64890SmrgCopyright 1987, 1998  The Open Group
51ab64890Smrg
61ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
71ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
81ab64890Smrgthe above copyright notice appear in all copies and that both that
91ab64890Smrgcopyright notice and this permission notice appear in supporting
101ab64890Smrgdocumentation.
111ab64890Smrg
121ab64890SmrgThe above copyright notice and this permission notice shall be included in
131ab64890Smrgall copies or substantial portions of the Software.
141ab64890Smrg
151ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
181ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
191ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
201ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
211ab64890Smrg
221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
231ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
241ab64890Smrgin this Software without prior written authorization from The Open Group.
251ab64890Smrg
261ab64890Smrg
271ab64890SmrgCopyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
281ab64890Smrg
291ab64890Smrg                        All Rights Reserved
301ab64890Smrg
3161b2299dSmrgPermission to use, copy, modify, and distribute this software and its
3261b2299dSmrgdocumentation for any purpose and without fee is hereby granted,
331ab64890Smrgprovided that the above copyright notice appear in all copies and that
3461b2299dSmrgboth that copyright notice and this permission notice appear in
351ab64890Smrgsupporting documentation, and that the name of Digital not be
361ab64890Smrgused in advertising or publicity pertaining to distribution of the
3761b2299dSmrgsoftware without specific, written prior permission.
381ab64890Smrg
391ab64890SmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
401ab64890SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
411ab64890SmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
421ab64890SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
431ab64890SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
441ab64890SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
451ab64890SmrgSOFTWARE.
461ab64890Smrg
471ab64890Smrg******************************************************************/
481ab64890Smrg
491ab64890Smrg#ifdef HAVE_CONFIG_H
501ab64890Smrg#include <config.h>
511ab64890Smrg#endif
521ab64890Smrg#include <X11/Xlibint.h>
531ab64890Smrg#include <X11/Xos.h>
541ab64890Smrg#include <X11/Xutil.h>
551ab64890Smrg#include "Xatomtype.h"
561ab64890Smrg#include <X11/Xatom.h>
571ab64890Smrg#include <stdio.h>
581ab64890Smrg
591ab64890SmrgStatus XGetSizeHints (
601ab64890Smrg	Display *dpy,
611ab64890Smrg	Window w,
621ab64890Smrg	XSizeHints *hints,
631ab64890Smrg        Atom property)
641ab64890Smrg{
651ab64890Smrg	xPropSizeHints *prop = NULL;
661ab64890Smrg        Atom actual_type;
671ab64890Smrg        int actual_format;
681ab64890Smrg        unsigned long leftover;
691ab64890Smrg        unsigned long nitems;
701ab64890Smrg	if (XGetWindowProperty(dpy, w, property, 0L,
711ab64890Smrg			       (long) OldNumPropSizeElements,
721ab64890Smrg	    False, XA_WM_SIZE_HINTS, &actual_type, &actual_format,
731ab64890Smrg            &nitems, &leftover, (unsigned char **)&prop)
741ab64890Smrg            != Success) return (0);
751ab64890Smrg
761ab64890Smrg        if ((actual_type != XA_WM_SIZE_HINTS) ||
771ab64890Smrg	    (nitems < OldNumPropSizeElements) || (actual_format != 32)) {
780f8248bfSmrg		Xfree (prop);
791ab64890Smrg                return(0);
801ab64890Smrg		}
811ab64890Smrg	hints->flags	  = (prop->flags & (USPosition|USSize|PAllHints));
821ab64890Smrg	hints->x 	  = cvtINT32toInt (prop->x);
831ab64890Smrg	hints->y 	  = cvtINT32toInt (prop->y);
841ab64890Smrg	hints->width      = cvtINT32toInt (prop->width);
851ab64890Smrg	hints->height     = cvtINT32toInt (prop->height);
861ab64890Smrg	hints->min_width  = cvtINT32toInt (prop->minWidth);
871ab64890Smrg	hints->min_height = cvtINT32toInt (prop->minHeight);
881ab64890Smrg	hints->max_width  = cvtINT32toInt (prop->maxWidth);
891ab64890Smrg	hints->max_height = cvtINT32toInt (prop->maxHeight);
901ab64890Smrg	hints->width_inc  = cvtINT32toInt (prop->widthInc);
911ab64890Smrg	hints->height_inc = cvtINT32toInt (prop->heightInc);
921ab64890Smrg	hints->min_aspect.x = cvtINT32toInt (prop->minAspectX);
931ab64890Smrg	hints->min_aspect.y = cvtINT32toInt (prop->minAspectY);
941ab64890Smrg	hints->max_aspect.x = cvtINT32toInt (prop->maxAspectX);
951ab64890Smrg	hints->max_aspect.y = cvtINT32toInt (prop->maxAspectY);
96818534a1Smrg	Xfree(prop);
971ab64890Smrg	return(1);
981ab64890Smrg}
991ab64890Smrg
10061b2299dSmrg/*
1011ab64890Smrg * must return a pointer to the hint, in malloc'd memory, or routine is not
1021ab64890Smrg * extensible; any use of the caller's memory would cause things to be stepped
1031ab64890Smrg * on.
1041ab64890Smrg */
1051ab64890Smrg
1061ab64890SmrgXWMHints *XGetWMHints (
1071ab64890Smrg	Display *dpy,
1081ab64890Smrg	Window w)
1091ab64890Smrg{
1101ab64890Smrg	xPropWMHints *prop = NULL;
1111ab64890Smrg	register XWMHints *hints;
1121ab64890Smrg        Atom actual_type;
1131ab64890Smrg        int actual_format;
1141ab64890Smrg        unsigned long leftover;
1151ab64890Smrg        unsigned long nitems;
11661b2299dSmrg	if (XGetWindowProperty(dpy, w, XA_WM_HINTS,
1171ab64890Smrg	    0L, (long)NumPropWMHintsElements,
1181ab64890Smrg	    False, XA_WM_HINTS, &actual_type, &actual_format,
1191ab64890Smrg            &nitems, &leftover, (unsigned char **)&prop)
1201ab64890Smrg            != Success) return (NULL);
1211ab64890Smrg
1221ab64890Smrg	/* If the property is undefined on the window, return null pointer. */
1231ab64890Smrg	/* pre-R3 bogusly truncated window_group, don't fail on them */
1241ab64890Smrg
1251ab64890Smrg        if ((actual_type != XA_WM_HINTS) ||
1261ab64890Smrg	    (nitems < (NumPropWMHintsElements - 1)) || (actual_format != 32)) {
1270f8248bfSmrg		Xfree (prop);
1281ab64890Smrg                return(NULL);
1291ab64890Smrg		}
1301ab64890Smrg	/* static copies not allowed in library, due to reentrancy constraint*/
131eb411b4bSmrg	if ((hints = Xcalloc (1, sizeof(XWMHints)))) {
1321ab64890Smrg	    hints->flags = prop->flags;
1331ab64890Smrg	    hints->input = (prop->input ? True : False);
1341ab64890Smrg	    hints->initial_state = cvtINT32toInt (prop->initialState);
1351ab64890Smrg	    hints->icon_pixmap = prop->iconPixmap;
1361ab64890Smrg	    hints->icon_window = prop->iconWindow;
1371ab64890Smrg	    hints->icon_x = cvtINT32toInt (prop->iconX);
1381ab64890Smrg	    hints->icon_y = cvtINT32toInt (prop->iconY);
1391ab64890Smrg	    hints->icon_mask = prop->iconMask;
1401ab64890Smrg	    if (nitems >= NumPropWMHintsElements)
1411ab64890Smrg		hints->window_group = prop->windowGroup;
1421ab64890Smrg	    else
1431ab64890Smrg		hints->window_group = 0;
1441ab64890Smrg	}
145818534a1Smrg	Xfree (prop);
1461ab64890Smrg	return(hints);
1471ab64890Smrg}
1481ab64890Smrg
1491ab64890SmrgStatus
1501ab64890SmrgXGetZoomHints (
1511ab64890Smrg	Display *dpy,
1521ab64890Smrg	Window w,
1531ab64890Smrg	XSizeHints *zhints)
1541ab64890Smrg{
1551ab64890Smrg	return (XGetSizeHints(dpy, w, zhints, XA_WM_ZOOM_HINTS));
1561ab64890Smrg}
1571ab64890Smrg
1581ab64890SmrgStatus
1591ab64890SmrgXGetNormalHints (
1601ab64890Smrg	Display *dpy,
1611ab64890Smrg	Window w,
1621ab64890Smrg	XSizeHints *hints)
1631ab64890Smrg{
1641ab64890Smrg	return (XGetSizeHints(dpy, w, hints, XA_WM_NORMAL_HINTS));
1651ab64890Smrg}
1661ab64890Smrg
16761b2299dSmrg
1681ab64890Smrg/*
16961b2299dSmrg * XGetIconSizes reads the property
1701ab64890Smrg *	ICONSIZE_ATOM	type: ICONSIZE_ATOM format: 32
1711ab64890Smrg */
1721ab64890Smrg
1731ab64890SmrgStatus XGetIconSizes (
1741ab64890Smrg	Display *dpy,
1751ab64890Smrg	Window w,	/* typically, root */
1761ab64890Smrg	XIconSize **size_list,	/* RETURN */
1771ab64890Smrg	int *count) 		/* RETURN number of items on the list */
1781ab64890Smrg{
1791ab64890Smrg	xPropIconSize *prop = NULL;
1801ab64890Smrg	register xPropIconSize *pp;
1811ab64890Smrg	register XIconSize *hp, *hints;
1821ab64890Smrg        Atom actual_type;
1831ab64890Smrg        int actual_format;
1841ab64890Smrg        unsigned long leftover;
1851ab64890Smrg        unsigned long nitems;
1861ab64890Smrg	register int i;
1871ab64890Smrg
1881ab64890Smrg	if (XGetWindowProperty(dpy, w, XA_WM_ICON_SIZE, 0L, 60L,
1891ab64890Smrg	    False, XA_WM_ICON_SIZE, &actual_type, &actual_format,
1901ab64890Smrg            &nitems, &leftover, (unsigned char **)&prop)
1911ab64890Smrg            != Success) return (0);
1921ab64890Smrg
1931ab64890Smrg	pp = prop;
1941ab64890Smrg
1951ab64890Smrg        if ((actual_type != XA_WM_ICON_SIZE) ||
1961ab64890Smrg	    (nitems < NumPropIconSizeElements) ||
1971ab64890Smrg	    (nitems % NumPropIconSizeElements != 0) ||
1981ab64890Smrg	    (actual_format != 32)) {
1990f8248bfSmrg		Xfree (prop);
2001ab64890Smrg                return(0);
2011ab64890Smrg		}
2021ab64890Smrg
2031ab64890Smrg	/* static copies not allowed in library, due to reentrancy constraint*/
2041ab64890Smrg
2051ab64890Smrg	nitems /= NumPropIconSizeElements;
206eb411b4bSmrg	if (! (hp = hints = Xcalloc (nitems, sizeof(XIconSize)))) {
2070f8248bfSmrg	    Xfree (prop);
2081ab64890Smrg	    return 0;
2091ab64890Smrg	}
2101ab64890Smrg
2111ab64890Smrg	/* march down array putting things into native form */
2121ab64890Smrg	for (i = 0; i < nitems; i++) {
2131ab64890Smrg	    hp->min_width  = cvtINT32toInt (pp->minWidth);
2141ab64890Smrg	    hp->min_height = cvtINT32toInt (pp->minHeight);
2151ab64890Smrg	    hp->max_width  = cvtINT32toInt (pp->maxWidth);
2161ab64890Smrg	    hp->max_height = cvtINT32toInt (pp->maxHeight);
2171ab64890Smrg	    hp->width_inc  = cvtINT32toInt (pp->widthInc);
2181ab64890Smrg	    hp->height_inc = cvtINT32toInt (pp->heightInc);
2191ab64890Smrg	    hp += 1;
2201ab64890Smrg	    pp += 1;
2211ab64890Smrg	}
2221ab64890Smrg	*count = nitems;
2231ab64890Smrg	*size_list = hints;
224818534a1Smrg	Xfree (prop);
2251ab64890Smrg	return(1);
2261ab64890Smrg}
2271ab64890Smrg
2281ab64890Smrg
2291ab64890SmrgStatus XGetCommand (
2301ab64890Smrg    Display *dpy,
2311ab64890Smrg    Window w,
2321ab64890Smrg    char ***argvp,
2331ab64890Smrg    int *argcp)
2341ab64890Smrg{
2351ab64890Smrg    XTextProperty tp;
2361ab64890Smrg    int argc;
2371ab64890Smrg    char **argv;
2381ab64890Smrg
2391ab64890Smrg    if (!XGetTextProperty (dpy, w, &tp, XA_WM_COMMAND)) return 0;
2401ab64890Smrg
2411ab64890Smrg    if (tp.encoding != XA_STRING || tp.format != 8) {
2420f8248bfSmrg	Xfree (tp.value);
2431ab64890Smrg	return 0;
2441ab64890Smrg    }
2451ab64890Smrg
2461ab64890Smrg
2471ab64890Smrg    /*
2481ab64890Smrg     * ignore final <NUL> if present since UNIX WM_COMMAND is nul-terminated
2491ab64890Smrg     */
2501ab64890Smrg    if (tp.nitems && (tp.value[tp.nitems - 1] == '\0')) tp.nitems--;
2511ab64890Smrg
2521ab64890Smrg
2531ab64890Smrg    /*
2541ab64890Smrg     * create a string list and return if successful
2551ab64890Smrg     */
2561ab64890Smrg    if (!XTextPropertyToStringList (&tp, &argv, &argc)) {
2570f8248bfSmrg	Xfree (tp.value);
2581ab64890Smrg	return (0);
2591ab64890Smrg    }
2601ab64890Smrg
2610f8248bfSmrg    Xfree (tp.value);
2621ab64890Smrg    *argvp = argv;
2631ab64890Smrg    *argcp = argc;
2641ab64890Smrg    return 1;
2651ab64890Smrg}
2661ab64890Smrg
2671ab64890Smrg
2681ab64890SmrgStatus
2691ab64890SmrgXGetTransientForHint(
2701ab64890Smrg	Display *dpy,
2711ab64890Smrg	Window w,
2721ab64890Smrg	Window *propWindow)
2731ab64890Smrg{
2741ab64890Smrg    Atom actual_type;
2751ab64890Smrg    int actual_format;
2761ab64890Smrg    unsigned long nitems;
2771ab64890Smrg    unsigned long leftover;
2781ab64890Smrg    Window *data = NULL;
2791ab64890Smrg    if (XGetWindowProperty(dpy, w, XA_WM_TRANSIENT_FOR, 0L, 1L, False,
28061b2299dSmrg        XA_WINDOW,
2811ab64890Smrg	&actual_type,
2821ab64890Smrg	&actual_format, &nitems, &leftover, (unsigned char **) &data)
2831ab64890Smrg	!= Success) {
2841ab64890Smrg	*propWindow = None;
2851ab64890Smrg	return (0);
2861ab64890Smrg	}
2871ab64890Smrg    if ( (actual_type == XA_WINDOW) && (actual_format == 32) &&
2881ab64890Smrg	 (nitems != 0) ) {
2891ab64890Smrg	*propWindow = *data;
2901ab64890Smrg	Xfree( (char *) data);
2911ab64890Smrg	return (1);
2921ab64890Smrg	}
2931ab64890Smrg    *propWindow = None;
2940f8248bfSmrg    Xfree( (char *) data);
2951ab64890Smrg    return(0);
2961ab64890Smrg}
2971ab64890Smrg
2981ab64890SmrgStatus
2991ab64890SmrgXGetClassHint(
3001ab64890Smrg	Display *dpy,
3011ab64890Smrg	Window w,
3021ab64890Smrg	XClassHint *classhint)	/* RETURN */
3031ab64890Smrg{
3041ab64890Smrg    int len_name, len_class;
3051ab64890Smrg
3061ab64890Smrg    Atom actual_type;
3071ab64890Smrg    int actual_format;
3081ab64890Smrg    unsigned long nitems;
3091ab64890Smrg    unsigned long leftover;
3101ab64890Smrg    unsigned char *data = NULL;
3111ab64890Smrg    if (XGetWindowProperty(dpy, w, XA_WM_CLASS, 0L, (long)BUFSIZ, False,
31261b2299dSmrg        XA_STRING,
3131ab64890Smrg	&actual_type,
3141ab64890Smrg	&actual_format, &nitems, &leftover, &data) != Success)
3151ab64890Smrg           return (0);
31661b2299dSmrg
3171ab64890Smrg   if ( (actual_type == XA_STRING) && (actual_format == 8) ) {
3189c019ec5Smaya	len_name = (int) strlen((char *) data);
319eb411b4bSmrg	if (! (classhint->res_name = Xmalloc(len_name + 1))) {
320818534a1Smrg	    Xfree(data);
3211ab64890Smrg	    return (0);
3221ab64890Smrg	}
3231ab64890Smrg	strcpy(classhint->res_name, (char *) data);
3241ab64890Smrg	if (len_name == nitems) len_name--;
3259c019ec5Smaya	len_class = (int) strlen((char *) (data+len_name+1));
326eb411b4bSmrg	if (! (classhint->res_class = Xmalloc(len_class + 1))) {
3271ab64890Smrg	    Xfree(classhint->res_name);
3281ab64890Smrg	    classhint->res_name = (char *) NULL;
329818534a1Smrg	    Xfree(data);
3301ab64890Smrg	    return (0);
3311ab64890Smrg	}
3321ab64890Smrg	strcpy(classhint->res_class, (char *) (data+len_name+1));
3331ab64890Smrg	Xfree( (char *) data);
3341ab64890Smrg	return(1);
3351ab64890Smrg	}
3360f8248bfSmrg    Xfree( (char *) data);
3371ab64890Smrg    return(0);
3381ab64890Smrg}
339