2f4c6dab9a704bf5cf71216b0b4bb921e4378e63
[oweals/cde.git] / cde / lib / DtSvc / DtUtil1 / GetMwmW.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: GetMwmW.c /main/5 1996/05/20 16:07:08 drk $
24  *
25  * (c) Copyright 1996 Digital Equipment Corporation.
26  * (c) Copyright 1990,1993,1994,1996 Hewlett-Packard Company.
27  * (c) Copyright 1993,1994,1996 International Business Machines Corp.
28  * (c) Copyright 1993,1994,1996 Sun Microsystems, Inc.
29  * (c) Copyright 1993,1994,1996 Novell, Inc. 
30  * (c) Copyright 1996 FUJITSU LIMITED.
31  * (c) Copyright 1996 Hitachi.
32  */
33
34 /************************************<+>*************************************
35  ****************************************************************************
36  **
37  **   File:     GetMwmW.c
38  **
39  **   Project:  DT Workspace Manager
40  **
41  **   Description: Gets the mwm window id.
42  **
43  ****************************************************************************
44  ************************************<+>*************************************/
45 #include <stdio.h>
46 #include <X11/Xlib.h>
47 #include <X11/Xutil.h>
48 #include <Xm/MwmUtil.h>
49 #include <Xm/Xm.h>
50 #include <Xm/AtomMgr.h>
51
52
53
54 /********    Public Function Declarations    ********/
55
56 extern int _DtGetMwmWindow( 
57                         Display *display,
58                         Window root,
59                         Window *pMwmWindow) ;
60
61 /********    End Public Function Declarations    ********/
62
63 /********    Static Function Declarations    ********/
64
65 static int _GetMwmWindow( 
66                         Display *display,
67                         Window root,
68                         Window *pMwmWindow,
69                         Atom property) ;
70
71 /********    End Static Function Declarations    ********/
72
73 \f
74 /*************************************<->*************************************
75  *
76  *  int _GetMwmWindow (display, root, pMwmWindow, property)
77  *
78  *
79  *  Description:
80  *  -----------
81  *  Get the Motif Window manager window
82  *
83  *
84  *  Inputs:
85  *  ------
86  *  display             - display 
87  *  root                - root window of screen
88  *  pMwmWindow          - pointer to a window (to be returned)
89  *  property            - the property atom
90  *
91  *  Outputs:
92  *  --------
93  *  *pMwmWindow         - mwm window id, if successful
94  *  Return              - status from XGetWindowProperty
95  *
96  *  Comments:
97  *  --------
98  *  This can fail if mwm is not managing the screen for the root window
99  *  passed in.
100  * 
101  *************************************<->***********************************/
102 static int 
103 _GetMwmWindow(
104         Display *display,
105         Window root,
106         Window *pMwmWindow,
107         Atom property )
108 {
109     Atom actualType;
110     int actualFormat;
111     unsigned long nitems;
112     unsigned long leftover;
113     PropMotifWmInfo *pWmInfo = NULL;
114     int rcode;
115     Window wroot, wparent, *pchildren;
116     unsigned int nchildren;
117
118     *pMwmWindow = NULL;
119     if ((rcode=XGetWindowProperty(display,root,
120                              property,0L, PROP_MWM_INFO_ELEMENTS,
121                              False,property,
122                              &actualType,&actualFormat,
123                              &nitems,&leftover,(unsigned char **)&pWmInfo))==Success)
124     {
125
126         if (actualType != property)
127         {
128             /* wrong type, force failure */
129             rcode = BadValue;
130         }
131         else
132         {
133             rcode = BadWindow;  /* assume the worst */
134
135             /*
136              * The mwm window should be a direct child of root
137              */
138             if (XQueryTree (display, root, &wroot, &wparent,
139                             &pchildren, &nchildren))
140             {
141                 int i;
142
143                 for (i = 0; (i < nchildren) && (rcode != Success); i++)
144                 {
145                     if (pchildren[i] == pWmInfo->wmWindow)
146                     {
147                         rcode = Success;
148                     }
149                 }
150             }
151
152             if (rcode == Success);
153             {
154                 *pMwmWindow = pWmInfo->wmWindow;
155             }
156
157             if (pchildren)
158             {
159                 XFree ((char *)pchildren);
160             }
161
162         }
163
164         if (pWmInfo)
165         {
166             XFree ((char *)pWmInfo);
167         }
168     }
169         
170     return(rcode);
171
172 } /* END OF FUNCTION _GetMwmWindow */
173
174 \f
175 /*************************************<->*************************************
176  *
177  *  int _DtGetMwmWindow (display, root, pMwmWindow)
178  *
179  *
180  *  Description:
181  *  -----------
182  *  Get the Motif Window manager window
183  *
184  *
185  *  Inputs:
186  *  ------
187  *  display             - display 
188  *  root                - root window of screen
189  *  pMwmWindow          - pointer to a window (to be returned)
190  *
191  *  Outputs:
192  *  --------
193  *  *pMwmWindow         - mwm window id, if successful
194  *  Return              - status from XGetWindowProperty
195  *
196  *  Comments:
197  *  --------
198  *  This can fail if mwm is not managing the screen for the root window
199  *  passed in.
200  * 
201  *************************************<->***********************************/
202 int 
203 _DtGetMwmWindow(
204         Display *display,
205         Window root,
206         Window *pMwmWindow )
207 {
208     Atom xa_MWM_INFO;
209
210     xa_MWM_INFO = XmInternAtom (display, _XA_MWM_INFO, False);
211     return (_GetMwmWindow (display, root, pMwmWindow, xa_MWM_INFO));
212 }