CvtStdSel.c

Go to the documentation of this file.
00001 /* $Xorg: CvtStdSel.c,v 1.4 2001/02/09 02:03:52 xorgcvs Exp $ */
00002 
00003 /*
00004  
00005 Copyright 1988, 1998  The Open Group
00006 
00007 Permission to use, copy, modify, distribute, and sell this software and its
00008 documentation for any purpose is hereby granted without fee, provided that
00009 the above copyright notice appear in all copies and that both that
00010 copyright notice and this permission notice appear in supporting
00011 documentation.
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 
00023 Except as contained in this notice, the name of The Open Group shall not be
00024 used in advertising or otherwise to promote the sale, use or other dealings
00025 in this Software without prior written authorization from The Open Group.
00026 
00027 */
00028 /* $XFree86: xc/lib/Xmu/CvtStdSel.c,v 3.19 2001/11/21 16:22:59 tsi Exp $ */
00029 
00030 /*
00031  * This file contains routines to handle common selection targets.
00032  *
00033  * Public entry points:
00034  *
00035  *      XmuConvertStandardSelection()   return a known selection
00036  */
00037 
00038 #ifdef SYSVNET
00039 #include <interlan/il_types.h>
00040 #define __TYPES__               /* prevent #include <sys/types.h> in Xlib.h */
00041 #include <interlan/netdb.h>
00042 #include <interlan/socket.h>
00043 #endif /* SYSVNET */
00044 
00045 #include <X11/IntrinsicP.h>
00046 #include <X11/Xatom.h>
00047 #include <X11/ShellP.h>
00048 #ifdef XTHREADS
00049 #include <X11/Xthreads.h>
00050 #endif
00051 #include <stdio.h>
00052 
00053 #ifndef SYSVNET
00054 #ifdef WIN32
00055 #include <X11/Xwinsock.h>
00056 #define XOS_USE_MTSAFE_NETDBAPI
00057 #else
00058 #ifndef Lynx
00059 #include <sys/socket.h>
00060 #else
00061 #include <sys/types.h>
00062 #include <socket.h>
00063 #endif
00064 #define XOS_USE_XT_LOCKING
00065 #endif
00066 #define X_INCLUDE_NETDB_H
00067 #include <X11/Xos_r.h>
00068 #endif
00069 
00070 #include <X11/Xos.h>
00071 #include <stdlib.h>
00072 #include "Atoms.h"
00073 #include "StdSel.h"
00074 #include "SysUtil.h"
00075 #include <X11/Xfuncs.h>
00076 
00077 #ifndef OS_NAME
00078 #ifndef X_OS_FILE
00079 #ifdef SYSV                     /* keep separate until makedepend fixed */
00080 #define USE_UNAME
00081 #endif
00082 #ifdef SVR4
00083 #define USE_UNAME
00084 #endif
00085 #ifdef ultrix
00086 #define USE_UNAME
00087 #endif
00088 #ifdef CSRG_BASED
00089 #define USE_UNAME
00090 #endif
00091 #endif /*X_OS_FILE*/
00092 #ifdef USE_UNAME
00093 #include <sys/utsname.h>
00094 #endif
00095 #endif
00096 
00097 /*
00098  * Prototypes
00099  */
00100 static char *get_os_name(void);
00101 static Bool isApplicationShell(Widget);
00102 
00103 /*
00104  * Implementation
00105  */
00106 static char *
00107 get_os_name(void)
00108 {
00109 #ifdef OS_NAME
00110         return XtNewString(OS_NAME);
00111 #else
00112         FILE *f = NULL;
00113 
00114 #ifdef USE_UNAME
00115         struct utsname utss;
00116 
00117         if (uname (&utss) == 0) {
00118             char *os_name;
00119             int len = strlen(utss.sysname) + 1;
00120 #ifndef hpux                            /* because of hostname length crock */
00121             len += 2 + strlen(utss.release);
00122 #endif
00123             os_name = XtMalloc (len);
00124             strcpy (os_name, utss.sysname);
00125 #ifndef hpux
00126             strcat (os_name, " ");
00127             strcat (os_name, utss.release);
00128 #endif
00129             return os_name;
00130         }
00131 #endif
00132 
00133 #ifdef X_OS_FILE
00134         f = fopen(X_OS_FILE, "r");
00135         if (!f)
00136 #endif
00137 #ifdef MOTD_FILE
00138                f = fopen(MOTD_FILE, "r");
00139 #endif
00140         if (f) {
00141             char motd[512];
00142             motd[0] = '\0';
00143             (void) fgets(motd, 511, f);
00144             fclose(f);
00145             motd[511] = '\0';
00146             if (motd[0] != '\0') {
00147                 int len = strlen(motd);
00148                 if (motd[len - 1] == '\n')
00149                     motd[len - 1] = '\0';
00150                 return XtNewString(motd);
00151             }
00152         }
00153 
00154 #ifdef sun
00155         return XtNewString("SunOS");
00156 #else
00157 # if !defined(SYSV) && (defined(CSRG_BASED) || defined(unix))
00158         return XtNewString("BSD");
00159 # else
00160         return NULL;
00161 # endif
00162 #endif
00163 
00164 #endif /*OS_NAME*/
00165 }
00166 
00167 /* This is a trick/kludge.  To make shared libraries happier (linking
00168  * against Xmu but not linking against Xt, and apparently even work
00169  * as we desire on SVR4, we need to avoid an explicit data reference
00170  * to applicationShellWidgetClass.  XtIsTopLevelShell is known
00171  * (implementation dependent assumption!) to use a bit flag.  So we
00172  * go that far.  Then, we test whether it is an applicationShellWidget
00173  * class by looking for an explicit class name.  Seems pretty safe.
00174  */
00175 static Bool
00176 isApplicationShell(Widget w)
00177 {
00178     register WidgetClass c;
00179 
00180     if (!XtIsTopLevelShell(w))
00181         return False;
00182     for (c = XtClass(w); c; c = c->core_class.superclass) {
00183         if (!strcmp(c->core_class.class_name, "ApplicationShell"))
00184             return True;
00185     }
00186     return False;
00187 }
00188 
00189 Boolean
00190 XmuConvertStandardSelection(Widget w, Time time, Atom *selection, Atom *target,
00191                             Atom *type, XPointer *value,
00192                             unsigned long *length, int *format)
00193 {
00194     Display *d = XtDisplay(w);
00195     if (*target == XA_TIMESTAMP(d)) {
00196         *value = XtMalloc(4);
00197         if (sizeof(long) == 4)
00198             *(long*)*value = time;
00199         else {
00200             long temp = time;
00201             (void) memmove((char*)*value, ((char*)&temp)+sizeof(long)-4, 4);
00202         }
00203         *type = XA_INTEGER;
00204         *length = 1;
00205         *format = 32;
00206         return True;
00207     }
00208     if (*target == XA_HOSTNAME(d)) {
00209         char hostname[1024];
00210         hostname[0] = '\0';
00211         *length = XmuGetHostname (hostname, sizeof hostname);
00212         *value = XtNewString(hostname);
00213         *type = XA_STRING;
00214         *format = 8;
00215         return True;
00216     }
00217 #if defined(TCPCONN)
00218     if (*target == XA_IP_ADDRESS(d)) {
00219         char hostname[1024];
00220 #ifdef XTHREADS_NEEDS_BYNAMEPARAMS
00221         _Xgethostbynameparams hparams;
00222 #endif
00223         struct hostent *hostp;
00224 
00225         hostname[0] = '\0';
00226         (void) XmuGetHostname (hostname, sizeof hostname);
00227 
00228         if ((hostp = _XGethostbyname (hostname,hparams)) == NULL)
00229             return False;
00230 
00231         if (hostp->h_addrtype != AF_INET) return False;
00232         *length = hostp->h_length;
00233         *value = XtMalloc(*length);
00234         (void) memmove (*value, hostp->h_addr, *length);
00235         *type = XA_NET_ADDRESS(d);
00236         *format = 8;
00237         return True;
00238     }
00239 #endif
00240 #ifdef DNETCONN
00241     if (*target == XA_DECNET_ADDRESS(d)) {
00242         return False;           /* XXX niy */
00243     }
00244 #endif
00245     if (*target == XA_USER(d)) {
00246         char *name = (char*)getenv("USER");
00247         if (name == NULL) return False;
00248         *value = XtNewString(name);
00249         *type = XA_STRING;
00250         *length = strlen(name);
00251         *format = 8;
00252         return True;
00253     }
00254     if (*target == XA_CLASS(d)) {
00255         Widget parent = XtParent(w);
00256         char *class;
00257         int len;
00258         while (parent != NULL && !isApplicationShell(w)) {
00259             w = parent;
00260             parent = XtParent(w);
00261         }
00262         if (isApplicationShell(w))
00263             class = ((ApplicationShellWidget) w)->application.class;
00264         else
00265             class = XtClass(w)->core_class.class_name;
00266         *length = (len=strlen(w->core.name)) + strlen(class) + 2;
00267         *value = XtMalloc(*length);
00268         strcpy( (char*)*value, w->core.name );
00269         strcpy( (char*)*value+len+1, class );
00270         *type = XA_STRING;
00271         *format = 8;
00272         return True;
00273     }
00274     if (*target == XA_NAME(d)) {
00275         Widget parent = XtParent(w);
00276 
00277         while (parent != NULL && !XtIsWMShell(w)) {
00278             w = parent;
00279             parent = XtParent(w);
00280         }
00281         if (!XtIsWMShell(w)) return False;
00282         *value = XtNewString( ((WMShellWidget) w)->wm.title );
00283         *length = strlen(*value);
00284         *type = XA_STRING;
00285         *format = 8;
00286         return True;
00287     }
00288     if (*target == XA_CLIENT_WINDOW(d)) {
00289         Widget parent = XtParent(w);
00290         while (parent != NULL) {
00291             w = parent;
00292             parent = XtParent(w);
00293         }
00294         *value = XtMalloc(sizeof(Window));
00295         *(Window*)*value = w->core.window;
00296         *type = XA_WINDOW;
00297         *length = 1;
00298         *format = 32;
00299         return True;
00300     }
00301     if (*target == XA_OWNER_OS(d)) {
00302         *value = get_os_name();
00303         if (*value == NULL) return False;
00304         *type = XA_STRING;
00305         *length = strlen(*value);
00306         *format = 8;
00307         return True;
00308     }
00309     if (*target == XA_TARGETS(d)) {
00310 #if defined(unix) && defined(DNETCONN)
00311 #  define NUM_TARGETS 9
00312 #else
00313 #  if defined(unix) || defined(DNETCONN)
00314 #    define NUM_TARGETS 8
00315 #  else
00316 #    define NUM_TARGETS 7
00317 #  endif
00318 #endif
00319         Atom* std_targets = (Atom*)XtMalloc(NUM_TARGETS*sizeof(Atom));
00320         int i = 0;
00321         std_targets[i++] = XA_TIMESTAMP(d);
00322         std_targets[i++] = XA_HOSTNAME(d);
00323         std_targets[i++] = XA_IP_ADDRESS(d);
00324         std_targets[i++] = XA_USER(d);
00325         std_targets[i++] = XA_CLASS(d);
00326         std_targets[i++] = XA_NAME(d);
00327         std_targets[i++] = XA_CLIENT_WINDOW(d);
00328 #ifdef unix
00329         std_targets[i++] = XA_OWNER_OS(d);
00330 #endif
00331 #ifdef DNETCONN
00332         std_targets[i++] = XA_DECNET_ADDRESS(d);
00333 #endif
00334         *value = (XPointer)std_targets;
00335         *type = XA_ATOM;
00336         *length = NUM_TARGETS;
00337         *format = 32;
00338         return True;
00339     }
00340     /* else */
00341     return False;
00342 }

Generated on Sun Mar 4 15:03:56 2007 for Scilab [trunk] by  doxygen 1.5.1