Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / lib / util / tt_Xlib.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 //%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                  
24 //%%  (c) Copyright 1993, 1994 International Business Machines Corp.    
25 //%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                   
26 //%%  (c) Copyright 1993, 1994 Novell, Inc.                             
27 //%%  $XConsortium: tt_Xlib.C /main/3 1995/10/23 10:36:22 rswiston $                                                    
28 /*
29  * @(#)tt_Xlib.C        1.3 93/09/07
30  *
31  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
32  */
33
34 #include "util/tt_Xlib.h"
35 #if defined(OPT_DLOPEN_X11)
36 #include <dlfcn.h>
37 #endif
38 #include "util/tt_string.h"
39 #include "util/tt_port.h"
40
41
42 #ifdef OPT_DLOPEN_X11
43
44 /************************************************************************** 
45  * This section contains functions for dynamically loading in the
46  * necessary X11 functions. This avoids the need for applications to link
47  * in libX11. Note that all of this section is ifdefd with OPT_DLOPEN_X11 so
48  * only functions relevant to dynamic loading of X11 should be included.
49  **************************************************************************/
50 _Tt_xlib _tt_xlib;
51 _Tt_xt _tt_xt;
52
53 // 
54 // Dlopens the X11 library and attempts to dlsym the needed functions
55 // from it. 
56 // 
57 int
58 _tt_load_xlib()
59 {
60         static int      loaded = 0;
61
62         if (loaded > 0) {
63                 return 1;
64         }
65
66         int             load_xlib_fns_from_handle(void *xlib_handle);
67         int             full_ldpath(_Tt_string libname, _Tt_string &path);
68
69         // open libX11.so and fill in _tt_xlib with pointers to
70         // functions
71         void            *xlib_handle;
72         _Tt_string      xlib_path;
73
74         // first try opening the current executable to see if the
75         // functions have already been defined
76         xlib_handle = dlopen((char *)0, 1);
77         if (load_xlib_fns_from_handle(xlib_handle)) {
78                 loaded++;
79                 return(1);
80         }
81
82 #if defined(OPT_BUG_SUNOS_4)
83         // While it's not strictly a bug that SunOS 4 doesn't do path
84         // searching right, it's certainly a pain special to 4.x.
85         // have to dlopen libX11.so from somewhere
86         if (! tt_ldpath("libX11.so", xlib_path)) {
87                 return(0);
88         }
89 #else
90         // SVR4 version of dlopen does the path searching for us
91         // (yay!). 
92         xlib_path = "libX11.so";
93 #endif                          // !SVR4
94
95         xlib_handle = dlopen((char *)xlib_path, 1);
96
97         if (xlib_handle == (void *)0) {
98                 _tt_syslog( 0, LOG_ERR, "dlopen( \"%s\", 1 ): %s",
99                             xlib_path.operator const char *(),
100                             dlerror() );
101                 return(0);
102         }
103
104         loaded = load_xlib_fns_from_handle(xlib_handle);
105         if (! loaded) {
106                 _tt_syslog( 0, LOG_ERR, "dlsym(): %s", dlerror() );
107         }
108         return loaded;
109 }
110
111 int
112 _tt_load_xt()
113 {
114         static int      loaded = 0;
115
116         if (loaded > 0) {
117                 return 1;
118         }
119
120         int             load_xt_fns_from_handle(void *xlib_handle);
121         int             full_ldpath(_Tt_string libname, _Tt_string &path);
122
123         // open libXt.so and fill in _tt_xt with pointers to
124         // functions
125         void            *xt_handle;
126         _Tt_string      xt_path;
127
128         // first try opening the current executable to see if the
129         // functions have already been defined
130         xt_handle = dlopen((char *)0, 1);
131         if (load_xt_fns_from_handle(xt_handle)) {
132                 loaded++;
133                 return(1);
134         }
135
136 #if defined(OPT_BUG_SUNOS_4)
137         // While it's not strictly a bug that SunOS 4 doesn't do path
138         // searching right, it's certainly a pain special to 4.x.
139         // have to dlopen libXt.so from somewhere
140         if (! tt_ldpath("libXt.so", xt_path)) {
141                 return(0);
142         }
143 #else
144         // SVR4 version of dlopen does the path searching for us
145         // (yay!). 
146         xt_path = "libXt.so";
147 #endif                          // !SVR4
148
149         xt_handle = dlopen((char *)xt_path, 1);
150
151         if (xt_handle == (void *)0) {
152                 _tt_syslog( 0, LOG_ERR, "dlopen( \"%s\", 1 ): %s",
153                             xt_path.operator const char *(),
154                             dlerror() );
155                 return(0);
156         }
157
158         loaded = load_xt_fns_from_handle(xt_handle);
159         if (! loaded) {
160                 _tt_syslog( 0, LOG_ERR, "dlsym(): %s", dlerror() );
161         }
162         return loaded;
163 }
164
165
166 // 
167 // Dlsym the needed functions from the handle. This is done by way of a
168 // macro which will just return 0 if it fails. Note that if a new
169 // function is needed then it should be added to the _tt_xlib struct and
170 // also added below as well.
171 // 
172 static int
173 load_xlib_fns_from_handle(void *xlib_handle)
174 {
175 #if defined(__STDC__)
176 #define DLINK_FN(fn)\
177         _tt_xlib . fn = (_Tt_xfn_ptr)dlsym(xlib_handle, #fn);           \
178         if ( _tt_xlib . fn  == (_Tt_xfn_ptr)0) {                        \
179                 return(0);                                              \
180         }                                                               
181 #else /* __STDC__ */
182 #define DLINK_FN(fn)\
183         _tt_xlib . fn = (_Tt_xfn_ptr)dlsym(xlib_handle, "fn");          \
184         if ( _tt_xlib . fn  == (_Tt_xfn_ptr)0) {                        \
185                 return(0);                                              \
186         }                                                               
187 #endif /* __STDC__ */
188
189         DLINK_FN(XOpenDisplay)
190         DLINK_FN(XCloseDisplay)
191         DLINK_FN(XFlush)
192         DLINK_FN(XChangeProperty)
193         DLINK_FN(XDeleteProperty)
194         DLINK_FN(XGetWindowProperty)
195         DLINK_FN(XSetIOErrorHandler)
196         DLINK_FN(XInternAtom)
197         DLINK_FN(XNextEvent)
198         DLINK_FN(XRefreshKeyboardMapping)
199         DLINK_FN(XGrabServer)
200         DLINK_FN(XUngrabServer)
201         DLINK_FN(XGetWindowAttributes)
202         DLINK_FN(XTranslateCoordinates)
203         DLINK_FN(XMoveResizeWindow)
204         DLINK_FN(XRaiseWindow)
205         DLINK_FN(XLowerWindow)
206         DLINK_FN(XIconifyWindow)
207         DLINK_FN(XMapWindow)
208         DLINK_FN(XWithdrawWindow)
209         DLINK_FN(XSendEvent)
210         DLINK_FN(XScreenNumberOfScreen)
211         DLINK_FN(XFree)
212
213         return(1);
214 #undef DLINK_FN
215 }
216
217 // 
218 // Dlsym the needed functions from the handle. This is done by way of a
219 // macro which will just return 0 if it fails. Note that if a new
220 // function is needed then it should be added to the _tt_xt struct and
221 // also added below as well.
222 // 
223 static int
224 load_xt_fns_from_handle(void *xt_handle)
225 {
226 #if defined(__STDC__)
227 #define DLINK_FN(fn)\
228         _tt_xt . fn = (_Tt_xfn_ptr)dlsym(xt_handle, #fn);               \
229         if ( _tt_xt . fn  == (_Tt_xfn_ptr)0) {                          \
230                 return(0);                                              \
231         }                                                               
232 #else /* __STDC__ */
233 #define DLINK_FN(fn)\
234         _tt_xt . fn = (_Tt_xfn_ptr)dlsym(xt_handle, "fn");              \
235         if ( _tt_xt . fn  == (_Tt_xfn_ptr)0) {                          \
236                 return(0);                                              \
237         }                                                               
238 #endif /* __STDC__ */
239
240         DLINK_FN(XtVaGetValues)
241         DLINK_FN(XtVaSetValues)
242         DLINK_FN(XtRemoveInput)
243         DLINK_FN(XtDisplay)
244         DLINK_FN(XtWindow)
245         DLINK_FN(XtIsRealized)
246         DLINK_FN(XtIsSubclass)
247         DLINK_FN(XtIsSensitive)
248         DLINK_FN(XtSetSensitive)
249         DLINK_FN(XtAppPending)
250         DLINK_FN(XtAppAddTimeOut)
251         DLINK_FN(XtRemoveTimeOut)
252         DLINK_FN(XtAppProcessEvent)
253         DLINK_FN(applicationShellWidgetClass)
254         return(1);
255 }
256
257 #else                           // OPT_DLOPEN_X11
258 /*
259  * Instead of putting ifdefs around every call to _tt_load_xlib and
260  * _tt_load_xt, we simply provide null versions that always return success
261  * if OPT_DLOPEN_X11 is turned off.
262  */
263
264 int
265 _tt_load_xlib()
266 {
267         return 1;
268 }
269
270 int
271 _tt_load_xt()
272 {
273         return 1;
274 }
275
276 #endif                          // OPT_DLOPEN_X11