home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xcmstest / TestInit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-31  |  6.8 KB  |  326 lines

  1. /* $XConsortium: TestInit.c,v 1.5 91/07/31 11:40:28 rws Exp $ */
  2.  
  3. /*
  4.  * (c) Copyright 1988, Tektronix Inc.
  5.  *     All Rights Reserved
  6.  *
  7.  * Permission to use, copy, modify, and distribute this software and its
  8.  * documentation for any purpose and without fee is hereby granted,
  9.  * provided that the above copyright notice appear in all copies and that
  10.  * both that copyright notice and this permission notice appear in
  11.  * supporting documentation, and that the name of Tektronix not be used
  12.  * in advertising or publicity pertaining to distribution of the software
  13.  * without specific, written prior permission.
  14.  *
  15.  * Tektronix disclaims all warranties with regard to this software, including
  16.  * all implied warranties of merchantability and fitness, in no event shall
  17.  * Tektronix be liable for any special, indirect or consequential damages or
  18.  * any damages whatsoever resulting from loss of use, data or profits,
  19.  * whether in an action of contract, negligence or other tortious action,
  20.  * arising out of or in connection with the use or performance of this
  21.  * software.
  22.  *
  23.  *    NAME
  24.  *        testinit.c -- TekCMS LibTest Utilities
  25.  *
  26.  *    DESCRIPTION
  27.  */
  28.  
  29. /*
  30.  *    EXTERNAL INCLUDES
  31.  *              Include files that must be exported to any package or
  32.  *              program using this package.
  33.  */
  34. #include "LibTest.h"
  35. #include <X11/Xlib.h>
  36.  
  37.  
  38. /*
  39.  *    INTERNAL INCLUDES
  40.  *              Include files that need NOT be exported to any package or
  41.  *              program using this package.
  42.  */
  43. #include <stdio.h>
  44. #include <sys/stat.h>
  45. #include <X11/Xutil.h>
  46. #include <X11/Xatom.h>
  47.  
  48. #ifdef AUTOHEADER
  49. #  include "TekCMS_TCI.ah"
  50. #else
  51. #  include "TekCMS_TCI.h"
  52. #endif
  53.  
  54. /*
  55.  *    EXTERNALS
  56.  *        Declarations that are needed by calling modules.
  57.  *        When using 'autohdr', these declaration will be placed
  58.  *        in the resulting header file.
  59.  */
  60. #ifndef    GLOBAL
  61. #  define    GLOBAL
  62. #endif
  63.  
  64. GLOBAL Display    *pDpy;
  65. GLOBAL GC    gc;
  66.  
  67.  
  68. /*
  69.  *    INTERNALS
  70.  *        Declarations that are local to this module.
  71.  *        (ignored by 'autohdr').
  72.  */
  73.     /*
  74.      *      DEFINES
  75.      */
  76.  
  77. #define W_WIDTH     260
  78. #define W_HEIGHT 450
  79. #define W_BORDERWIDTH 0
  80. #define W_BORDER_COLOR        "black"
  81. #define W_BACKGROUND_COLOR    "black"
  82.  
  83. #define GC_FOREGROUND_COLOR    "white"
  84. #define GC_BACKGROUND_COLOR    "black"
  85.  
  86. #define EVENT_MASK (\
  87.         ExposureMask    |\
  88.         StructureNotifyMask    |\
  89.         ButtonPressMask )
  90.  
  91. /* xColor defines */
  92. #define    DOALL    (DoRed | DoGreen | DoBlue)
  93. #define PAD    0
  94.  
  95.     /*
  96.      *      EXTERNS
  97.      */
  98. extern int _XDefaultError();
  99. extern int LtErrorHandler();
  100.  
  101.  
  102.  
  103. /* include one entry for every window, add more members if desired */
  104. static
  105. struct _winfo{
  106.     XPoint pt;        /* window origin */
  107. } winfo[] = {
  108.     0,    0
  109. };
  110.  
  111. #define N_WINDOWS ( sizeof(winfo)/sizeof(XPoint) )
  112. static Window    w[N_WINDOWS];
  113. static XImage   *wImage[N_WINDOWS];
  114.  
  115. /*
  116.  * Include any special fonts that you will want to use, two examples given
  117.  * Access X font id as font[0..n]
  118.  */
  119. static
  120. char * fontname[] = {
  121.     "vtbold",
  122.     "9x15"
  123. };
  124. #define N_FONTS ( sizeof(fontname)/sizeof(char *) )
  125. static Font    font[N_FONTS];
  126.  
  127.  
  128.  
  129. /*
  130.  *    NAME
  131.  *        TestInit - Connect to X Server and init X globals
  132.  *
  133.  *    SYNOPSIS
  134.  */
  135. void
  136. TestInit()
  137. /*
  138.  *    DESCRIPTION
  139.  *        Set the X Error Handler.
  140.  *        Open the display using command line arguments.
  141.  *
  142.  *        Init all x global variables which includes:
  143.  *
  144.  *        cmap
  145.  *        w[0..n]
  146.  *        gc
  147.  *        font[0..n]
  148.  *
  149.  *    RETURNS
  150.  *        void
  151.  *
  152.  */
  153. {
  154.  
  155.     int        i;
  156.     XSetWindowAttributes xswa;
  157.     XEvent    event;
  158.     Visual    visual;
  159.     Colormap    cmap;
  160.     XColor    xColor,xColorExact;
  161.     XGCValues    xgcValues;
  162.     unsigned long xgcValueMask;
  163.     Status    status;
  164.     struct stat statbuf;
  165.     char *idir = "./IDIR";
  166.     char *rdir = "./RDIR";
  167.     char *vdir = "./VDIR";
  168.     char *displayname = NULL;
  169.  
  170.     /*
  171.      * Set up our special error handler
  172.      */
  173.  
  174.      XSetErrorHandler(LtErrorHandler);
  175.  
  176.     for (i = 1; i < CommandArgc; i++) {
  177.     if ((strcmp(CommandArgv[i], "-display") == 0)
  178.       && ((i+1) < CommandArgc) && (*CommandArgv[i+1] != '-')) {
  179.         displayname = CommandArgv[i+1];
  180.         break;
  181.     }
  182.     }
  183.     if (!(pDpy = XOpenDisplay(displayname))) {
  184.     perror("Cannot open display\n");
  185.     exit(0);
  186.     }
  187.  
  188. #ifdef XDEBUG
  189.     XSynchronize(pDpy,1);
  190. #endif /* XDEBUG */
  191.  
  192. #ifdef NEED_COLORMAP
  193.     /*
  194.      * Color Map Initialization
  195.      */
  196.     cmap = XDefaultColormap(pDpy,XDefaultScreen(pDpy));
  197. #endif /* NEED_COLORMAP */
  198.  
  199. #ifdef NEED_WINDOW
  200.     /*
  201.      * Window Initialization
  202.      */
  203.     status = XAllocNamedColor(pDpy,cmap,
  204.         W_BACKGROUND_COLOR,&xColor,&xColorExact);
  205.     if(!status){
  206.     printf("Error: XAllocNamedColor status: %d\n",status);
  207.     }
  208.     xswa.background_pixel = xColor.pixel;
  209.  
  210.     status = XAllocNamedColor(pDpy,cmap,
  211.         W_BORDER_COLOR,&xColor,&xColorExact);
  212.     if(!status){
  213.     printf("Error: XAllocNamedColor status: %d\n",status);
  214.     }
  215.     xswa.border_pixel = xColor.pixel;
  216.  
  217.     xswa.event_mask= EVENT_MASK;
  218.     xswa.override_redirect = True;
  219.     visual.visualid = CopyFromParent;
  220.     for(i=0; i<N_WINDOWS; i++){
  221.     /*
  222.      * Create and map the global windows to be used in all tests
  223.      */
  224.     w[i] = XCreateWindow(pDpy, RootWindow(pDpy, 0),
  225.         winfo[i].pt.x, winfo[i].pt.y, W_WIDTH, W_HEIGHT, W_BORDERWIDTH,
  226.         DefaultDepth(pDpy, 0), InputOutput, &visual,
  227.         (CWEventMask | CWBackPixel | CWBorderPixel | CWOverrideRedirect),
  228.         &xswa);
  229.  
  230.     XMapRaised(pDpy, w[i]);
  231.  
  232.     }
  233. #endif /* NEED_WINDOW */
  234.  
  235.  
  236. #ifdef NEED_GC
  237.     /* 
  238.      * Graphics Context Initialization
  239.      */
  240.     status = XAllocNamedColor(pDpy,cmap,
  241.         GC_BACKGROUND_COLOR,&xColor,&xColorExact);
  242.     if(!status){
  243.     printf("Error: XAllocNamedColor status: %d\n",status);
  244.     }
  245.     xgcValues.background = xColor.pixel;
  246.  
  247.     status = XAllocNamedColor(pDpy,cmap,
  248.         GC_FOREGROUND_COLOR,&xColor,&xColorExact);
  249.     if(!status){
  250.     printf("Error: XAllocNamedColor status: %d\n",status);
  251.     }
  252.     xgcValues.foreground = xColor.pixel;
  253.  
  254.     xgcValueMask = (GCForeground | GCBackground);
  255.     gc = XCreateGC(pDpy, w[0], xgcValueMask, &xgcValues);
  256. #endif /* NEED_GC */
  257.  
  258.  
  259. #ifdef NEED_FONTS
  260.     /*
  261.      * Font Initialization
  262.      */
  263.     for( i = 0; i < N_FONTS; i++) {
  264.     font[i] = XLoadFont(pDpy,fontname[i]);
  265.     }
  266. #endif /* NEED_FONTS */
  267.  
  268.     /* check for the IDIR, RDIR and VDIR directories */
  269.     if (stat(idir, &statbuf) == 0) {
  270.     if (statbuf.st_mode & S_IFDIR) {
  271.         strcpy(TekCMS_idir, idir);
  272.     }
  273.     }
  274.     if (stat(rdir, &statbuf) == 0) {
  275.     if (statbuf.st_mode & S_IFDIR) {
  276.         strcpy(TekCMS_rdir, rdir);
  277.     }
  278.     }
  279.     if (stat(vdir, &statbuf) == 0) {
  280.     if (statbuf.st_mode & S_IFDIR) {
  281.         strcpy(TekCMS_vdir, vdir);
  282.     }
  283.     }
  284. }
  285.  
  286.  
  287. /*
  288.  *    NAME
  289.  *        TestCleanup - Clean up X resources
  290.  *
  291.  *    SYNOPSIS
  292.  */
  293. void
  294. TestCleanup()
  295. /*
  296.  *    DESCRIPTION
  297.  *        Cleanup X resources
  298.  *
  299.  *    RETURNS
  300.  *        void
  301.  *
  302.  */
  303. {
  304.     int i;
  305.  
  306.     /*
  307.      * Clean up X resources
  308.      */
  309. #ifdef NEED_GC
  310.     XFreeGC(pDpy,gc);
  311. #endif /* NEED_GC */
  312.  
  313. #ifdef NEED_WINDOW
  314.     for( i = 0; i < N_WINDOWS; i++) {
  315.     XDestroyWindow(pDpy,w[i]);
  316.     }
  317. #endif /* NEED_WINDOW */
  318.  
  319. #ifdef NEED_FONTS
  320.     for( i = 0; i < N_FONTS; i++) {
  321.     XUnloadFont(pDpy,font[i]);
  322.     }
  323. #endif /* NEED_FONTS */
  324.  
  325. }
  326.