home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / CvtStdSel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-10  |  7.5 KB  |  309 lines

  1. /* $XConsortium: CvtStdSel.c,v 1.23 91/04/11 09:04:08 rws Exp $
  2.  *
  3.  * Copyright 1988 by the Massachusetts Institute of Technology
  4.  *
  5.  * Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of M.I.T. not be used in advertising or
  10.  * publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.  M.I.T. makes no representations about the
  12.  * suitability of this software for any purpose.  It is provided "as is"
  13.  * without express or implied warranty.
  14.  *
  15.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  17.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  20.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  *
  23.  * This file contains routines to handle common selection targets.
  24.  *
  25.  * Public entry points:
  26.  *
  27.  *    XmuConvertStandardSelection()    return a known selection
  28.  */
  29.  
  30. #ifdef SYSVNET
  31. #include <interlan/il_types.h>
  32. #define __TYPES__        /* prevent #include <sys/types.h> in Xlib.h */
  33. #include <interlan/netdb.h>
  34. #include <interlan/socket.h>
  35. #endif /* SYSVNET */
  36.  
  37. #include <X11/IntrinsicP.h>
  38. #include <X11/Xatom.h>
  39. #include <X11/ShellP.h>
  40. #include <stdio.h>
  41.  
  42. #ifndef SYSVNET
  43. #include <netdb.h>
  44. #include <sys/socket.h>
  45. #endif
  46.  
  47. #include "Atoms.h"
  48. #include "StdSel.h"
  49. #include "SysUtil.h"
  50. #include <X11/Xfuncs.h>
  51. #include <X11/Xos.h>
  52.  
  53. #ifndef OS_NAME
  54. #ifndef X_OS_FILE
  55. #ifdef SYSV            /* keep separate until makedepend fixed */
  56. #define USE_UNAME
  57. #endif
  58. #ifdef SVR4
  59. #define USE_UNAME
  60. #endif
  61. #ifdef ultrix
  62. #define USE_UNAME
  63. #endif
  64. #endif /*X_OS_FILE*/
  65. #ifdef USE_UNAME
  66. #ifdef ultrix
  67. #ifndef __STDC__
  68. #include <limits.h>        /* fixed in Ultrix 3.0 */
  69. #endif
  70. #endif
  71. #include <sys/utsname.h>
  72. #endif
  73. #endif
  74.  
  75. static char *get_os_name ()
  76. {
  77. #ifdef OS_NAME
  78.     return XtNewString(OS_NAME);
  79. #else
  80.     FILE *f = NULL;
  81.  
  82. #ifdef USE_UNAME
  83.     struct utsname uts;
  84.  
  85.     if (uname (&uts) == 0) {
  86.         char *os_name;
  87.         int len = strlen(uts.sysname);
  88. #ifndef hpux                /* because of hostname length crock */
  89.         len += 1 + strlen(uts.release);
  90. #endif
  91.         os_name = XtMalloc (len);
  92.         strcpy (os_name, uts.sysname);
  93. #ifndef hpux
  94.         strcat (os_name, " ");
  95.         strcat (os_name, uts.release);
  96. #endif
  97.         return os_name;
  98.     }
  99. #endif
  100.  
  101. #ifdef X_OS_FILE
  102.     f = fopen(X_OS_FILE, "r");
  103.     if (!f)
  104. #endif
  105. #ifdef MOTD_FILE
  106.            f = fopen(MOTD_FILE, "r");
  107. #endif
  108.     if (f) {
  109.         char motd[512];
  110.         motd[0] = '\0';
  111.         (void) fgets(motd, 511, f);
  112.         fclose(f);
  113.         motd[511] = '\0';
  114.         if (motd[0] != '\0') {
  115.         int len = strlen(motd);
  116.         if (motd[len - 1] == '\n')
  117.             motd[len - 1] = '\0';
  118.         return XtNewString(motd);
  119.         }
  120.     }
  121.  
  122. #ifdef sun
  123.     return XtNewString("SunOS");
  124. #else
  125. # if !defined(SYSV) && defined(unix)
  126.     return XtNewString("BSD");
  127. # else
  128.     return NULL;
  129. # endif
  130. #endif
  131.  
  132. #endif /*OS_NAME*/
  133. }
  134.  
  135. /* This is a trick/kludge.  To make shared libraries happier (linking
  136.  * against Xmu but not linking against Xt, and apparently even work
  137.  * as we desire on SVR4, we need to avoid an explicit data reference
  138.  * to applicationShellWidgetClass.  XtIsTopLevelShell is known
  139.  * (implementation dependent assumption!) to use a bit flag.  So we
  140.  * go that far.  Then, we test whether it is an applicationShellWidget
  141.  * class by looking for an explicit class name.  Seems pretty safe.
  142.  */
  143. static Bool isApplicationShell(w)
  144.     Widget w;
  145. {
  146.     register WidgetClass c;
  147.  
  148.     if (!XtIsTopLevelShell(w))
  149.     return False;
  150.     for (c = XtClass(w); c; c = c->core_class.superclass) {
  151.     if (!strcmp(c->core_class.class_name, "ApplicationShell"))
  152.         return True;
  153.     }
  154.     return False;
  155. }
  156.  
  157. Boolean XmuConvertStandardSelection(w, time, selection, target,
  158.                     type, value, length, format)
  159.     Widget w;
  160.     Time time;
  161.     Atom *selection, *target, *type;
  162.     caddr_t *value;
  163.     unsigned long *length;
  164.     int *format;
  165. {
  166.     Display *d = XtDisplay(w);
  167.     if (*target == XA_TIMESTAMP(d)) {
  168.     *value = XtMalloc(4);
  169.     if (sizeof(long) == 4)
  170.         *(long*)*value = time;
  171.     else {
  172.         long temp = time;
  173.         bcopy( ((char*)&temp)+sizeof(long)-4, (char*)*value, 4);
  174.     }
  175.     *type = XA_INTEGER;
  176.     *length = 1;
  177.     *format = 32;
  178.     return True;
  179.     }
  180.     if (*target == XA_HOSTNAME(d)) {
  181.     char hostname[1024];
  182.     hostname[0] = '\0';
  183.     *length = XmuGetHostname (hostname, sizeof hostname);
  184.     *value = XtNewString(hostname);
  185.     *type = XA_STRING;
  186.     *format = 8;
  187.     return True;
  188.     }
  189. #ifdef TCPCONN
  190.     if (*target == XA_IP_ADDRESS(d)) {
  191.     char hostname[1024];
  192.  
  193.     struct hostent *hostent;
  194.     hostname[0] = '\0';
  195.     (void) XmuGetHostname (hostname, sizeof hostname);
  196.     hostent = gethostbyname (hostname);
  197.     if (hostent->h_addrtype != AF_INET) return False;
  198.     *length = hostent->h_length;
  199.     *value = XtMalloc(*length);
  200.     bcopy (hostent->h_addr, *value, *length);
  201.     *type = XA_NET_ADDRESS(d);
  202.     *format = 8;
  203.     return True;
  204.     }
  205. #endif
  206. #ifdef DNETCONN
  207.     if (*target == XA_DECNET_ADDRESS(d)) {
  208.     return False;        /* XXX niy */
  209.     }
  210. #endif
  211.     if (*target == XA_USER(d)) {
  212.     char *name = (char*)getenv("USER");
  213.     if (name == NULL) return False;
  214.     *value = XtNewString(name);
  215.     *type = XA_STRING;
  216.     *length = strlen(name);
  217.     *format = 8;
  218.     return True;
  219.     }
  220.     if (*target == XA_CLASS(d)) {
  221.     Widget parent = XtParent(w);
  222.     char *class;
  223.     int len;
  224.     while (parent != NULL && !isApplicationShell(w)) {
  225.         w = parent;
  226.         parent = XtParent(w);
  227.     }
  228.     if (isApplicationShell(w))
  229.         class = ((ApplicationShellWidget) w)->application.class;
  230.     else
  231.         class = XtClass(w)->core_class.class_name;
  232.     *length = (len=strlen(w->core.name)) + strlen(class) + 2;
  233.     *value = XtMalloc(*length);
  234.     strcpy( (char*)*value, w->core.name );
  235.     strcpy( (char*)*value+len+1, class );
  236.     *type = XA_STRING;
  237.     *format = 8;
  238.     return True;
  239.     }
  240.     if (*target == XA_NAME(d)) {
  241.     Widget parent = XtParent(w);
  242.  
  243.     while (parent != NULL && !XtIsWMShell(w)) {
  244.         w = parent;
  245.         parent = XtParent(w);
  246.     }
  247.     if (!XtIsWMShell(w)) return False;
  248.     *value = XtNewString( ((WMShellWidget) w)->wm.title );
  249.     *length = strlen(*value);
  250.     *type = XA_STRING;
  251.     *format = 8;
  252.     return True;
  253.     }
  254.     if (*target == XA_CLIENT_WINDOW(d)) {
  255.     Widget parent = XtParent(w);
  256.     while (parent != NULL) {
  257.         w = parent;
  258.         parent = XtParent(w);
  259.     }
  260.     *value = XtMalloc(sizeof(Window));
  261.     *(Window*)*value = w->core.window;
  262.     *type = XA_WINDOW;
  263.     *length = 1;
  264.     *format = 32;
  265.     return True;
  266.     }
  267.     if (*target == XA_OWNER_OS(d)) {
  268.     *value = get_os_name();
  269.     if (*value == NULL) return False;
  270.     *type = XA_STRING;
  271.     *length = strlen(*value);
  272.     *format = 8;
  273.     return True;
  274.     }
  275.     if (*target == XA_TARGETS(d)) {
  276. #if defined(unix) && defined(DNETCONN)
  277. #  define NUM_TARGETS 9
  278. #else
  279. #  if defined(unix) || defined(DNETCONN)
  280. #    define NUM_TARGETS 8
  281. #  else
  282. #    define NUM_TARGETS 7
  283. #  endif
  284. #endif
  285.     Atom* std_targets = (Atom*)XtMalloc(NUM_TARGETS*sizeof(Atom));
  286.     int i = 0;
  287.     std_targets[i++] = XA_TIMESTAMP(d);
  288.     std_targets[i++] = XA_HOSTNAME(d);
  289.     std_targets[i++] = XA_IP_ADDRESS(d);
  290.     std_targets[i++] = XA_USER(d);
  291.     std_targets[i++] = XA_CLASS(d);
  292.     std_targets[i++] = XA_NAME(d);
  293.     std_targets[i++] = XA_CLIENT_WINDOW(d);
  294. #ifdef unix
  295.     std_targets[i++] = XA_OWNER_OS(d);
  296. #endif
  297. #ifdef DNETCONN
  298.     std_targets[i++] = XA_DECNET_ADDRESS(d);
  299. #endif
  300.     *value = (caddr_t)std_targets;
  301.     *type = XA_ATOM;
  302.     *length = NUM_TARGETS;
  303.     *format = 32;
  304.     return True;
  305.     }
  306.     /* else */
  307.     return False;
  308. }
  309.