home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / twm / twm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-08  |  23.6 KB  |  835 lines

  1. /*****************************************************************************/
  2. /**       Copyright 1988 by Evans & Sutherland Computer Corporation,        **/
  3. /**                          Salt Lake City, Utah                           **/
  4. /**  Portions Copyright 1989 by the Massachusetts Institute of Technology   **/
  5. /**                        Cambridge, Massachusetts                         **/
  6. /**                                                                         **/
  7. /**                           All Rights Reserved                           **/
  8. /**                                                                         **/
  9. /**    Permission to use, copy, modify, and distribute this software and    **/
  10. /**    its documentation  for  any  purpose  and  without  fee is hereby    **/
  11. /**    granted, provided that the above copyright notice appear  in  all    **/
  12. /**    copies and that both  that  copyright  notice  and  this  permis-    **/
  13. /**    sion  notice appear in supporting  documentation,  and  that  the    **/
  14. /**    names of Evans & Sutherland and M.I.T. not be used in advertising    **/
  15. /**    in publicity pertaining to distribution of the  software  without    **/
  16. /**    specific, written prior permission.                                  **/
  17. /**                                                                         **/
  18. /**    EVANS & SUTHERLAND AND M.I.T. DISCLAIM ALL WARRANTIES WITH REGARD    **/
  19. /**    TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES  OF  MERCHANT-    **/
  20. /**    ABILITY  AND  FITNESS,  IN  NO  EVENT SHALL EVANS & SUTHERLAND OR    **/
  21. /**    M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL  DAM-    **/
  22. /**    AGES OR  ANY DAMAGES WHATSOEVER  RESULTING FROM LOSS OF USE, DATA    **/
  23. /**    OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER    **/
  24. /**    TORTIOUS ACTION, ARISING OUT OF OR IN  CONNECTION  WITH  THE  USE    **/
  25. /**    OR PERFORMANCE OF THIS SOFTWARE.                                     **/
  26. /*****************************************************************************/
  27.  
  28.  
  29. /***********************************************************************
  30.  *
  31.  * $XConsortium: twm.c,v 1.124 91/05/08 11:01:54 dave Exp $
  32.  *
  33.  * twm - "Tom's Window Manager"
  34.  *
  35.  * 27-Oct-87 Thomas E. LaStrange    File created
  36.  * 10-Oct-90 David M. Sternlicht        Storing saved colors on root
  37.  ***********************************************************************/
  38.  
  39. #include <stdio.h>
  40. #include <signal.h>
  41. #include <fcntl.h>
  42. #include "twm.h"
  43. #include "add_window.h"
  44. #include "gc.h"
  45. #include "parse.h"
  46. #include "version.h"
  47. #include "menus.h"
  48. #include "events.h"
  49. #include "util.h"
  50. #include "gram.h"
  51. #include "screen.h"
  52. #include "iconmgr.h"
  53. #include <X11/Xproto.h>
  54. #include <X11/Xatom.h>
  55.  
  56. Display *dpy;            /* which display are we talking to */
  57. Window ResizeWindow;        /* the window we are resizing */
  58.  
  59. int MultiScreen = TRUE;        /* try for more than one screen? */
  60. int NumScreens;            /* number of screens in ScreenList */
  61. int HasShape;            /* server supports shape extension? */
  62. int ShapeEventBase, ShapeErrorBase;
  63. ScreenInfo **ScreenList;    /* structures for each screen */
  64. ScreenInfo *Scr = NULL;        /* the cur and prev screens */
  65. int PreviousScreen;        /* last screen that we were on */
  66. int FirstScreen;        /* TRUE ==> first screen of display */
  67. Bool PrintErrorMessages = False;    /* controls error messages */
  68. static int RedirectError;    /* TRUE ==> another window manager running */
  69. static int CatchRedirectError();    /* for settting RedirectError */
  70. static int TwmErrorHandler();    /* for everything else */
  71. char Info[INFO_LINES][INFO_SIZE];        /* info strings to print */
  72. int InfoLines;
  73. char *InitFile = NULL;
  74.  
  75. Cursor UpperLeftCursor;        /* upper Left corner cursor */
  76. Cursor RightButt;
  77. Cursor MiddleButt;
  78. Cursor LeftButt;
  79.  
  80. XContext TwmContext;        /* context for twm windows */
  81. XContext MenuContext;        /* context for all menu windows */
  82. XContext IconManagerContext;    /* context for all window list windows */
  83. XContext ScreenContext;        /* context to get screen data */
  84. XContext ColormapContext;    /* context for colormap operations */
  85.  
  86. XClassHint NoClass;        /* for applications with no class */
  87.  
  88. XGCValues Gcv;
  89.  
  90. char *Home;            /* the HOME environment variable */
  91. int HomeLen;            /* length of Home */
  92. int ParseError;            /* error parsing the .twmrc file */
  93.  
  94. int HandlingEvents = FALSE;    /* are we handling events yet? */
  95.  
  96. Window JunkRoot;        /* junk window */
  97. Window JunkChild;        /* junk window */
  98. int JunkX;            /* junk variable */
  99. int JunkY;            /* junk variable */
  100. unsigned int JunkWidth, JunkHeight, JunkBW, JunkDepth, JunkMask;
  101.  
  102. char *ProgramName;
  103. int Argc;
  104. char **Argv;
  105. char **Environ;
  106.  
  107. Bool RestartPreviousState = False;    /* try to restart in previous state */
  108.  
  109. unsigned long black, white;
  110.  
  111. extern void assign_var_savecolor();
  112.  
  113. /***********************************************************************
  114.  *
  115.  *  Procedure:
  116.  *    main - start of twm
  117.  *
  118.  ***********************************************************************
  119.  */
  120.  
  121. main(argc, argv, environ)
  122.     int argc;
  123.     char **argv;
  124.     char **environ;
  125. {
  126.     Window root, parent, *children;
  127.     unsigned int nchildren;
  128.     int i, j;
  129.     char *display_name = NULL;
  130.     unsigned long valuemask;    /* mask for create windows */
  131.     XSetWindowAttributes attributes;    /* attributes for create windows */
  132.     int numManaged, firstscrn, lastscrn, scrnum;
  133.     extern ColormapWindow *CreateColormapWindow();
  134.  
  135.     ProgramName = argv[0];
  136.     Argc = argc;
  137.     Argv = argv;
  138.     Environ = environ;
  139.  
  140.     for (i = 1; i < argc; i++) {
  141.     if (argv[i][0] == '-') {
  142.         switch (argv[i][1]) {
  143.           case 'd':                /* -display dpy */
  144.         if (++i >= argc) goto usage;
  145.         display_name = argv[i];
  146.         continue;
  147.           case 's':                /* -single */
  148.         MultiScreen = FALSE;
  149.         continue;
  150.           case 'f':                /* -file twmrcfilename */
  151.         if (++i >= argc) goto usage;
  152.         InitFile = argv[i];
  153.         continue;
  154.           case 'v':                /* -verbose */
  155.         PrintErrorMessages = True;
  156.         continue;
  157.           case 'q':                /* -quiet */
  158.         PrintErrorMessages = False;
  159.         continue;
  160.         }
  161.     }
  162.       usage:
  163.     fprintf (stderr,
  164.          "usage:  %s [-display dpy] [-f file] [-s] [-q] [-v]\n",
  165.          ProgramName);
  166.     exit (1);
  167.     }
  168.  
  169. #define newhandler(sig) \
  170.     if (signal (sig, SIG_IGN) != SIG_IGN) (void) signal (sig, Done)
  171.  
  172.     newhandler (SIGINT);
  173.     newhandler (SIGHUP);
  174.     newhandler (SIGQUIT);
  175.     newhandler (SIGTERM);
  176.  
  177. #undef newhandler
  178.  
  179.     Home = getenv("HOME");
  180.     if (Home == NULL)
  181.     Home = "./";
  182.  
  183.     HomeLen = strlen(Home);
  184.  
  185.     NoClass.res_name = NoName;
  186.     NoClass.res_class = NoName;
  187.  
  188.     if (!(dpy = XOpenDisplay(display_name))) {
  189.     fprintf (stderr, "%s:  unable to open display \"%s\"\n",
  190.          ProgramName, XDisplayName(display_name));
  191.     exit (1);
  192.     }
  193.  
  194.     if (fcntl(ConnectionNumber(dpy), F_SETFD, 1) == -1) {
  195.     fprintf (stderr, 
  196.          "%s:  unable to mark display connection as close-on-exec\n",
  197.          ProgramName);
  198.     exit (1);
  199.     }
  200.  
  201.     HasShape = XShapeQueryExtension (dpy, &ShapeEventBase, &ShapeErrorBase);
  202.     TwmContext = XUniqueContext();
  203.     MenuContext = XUniqueContext();
  204.     IconManagerContext = XUniqueContext();
  205.     ScreenContext = XUniqueContext();
  206.     ColormapContext = XUniqueContext();
  207.  
  208.     InternUsefulAtoms ();
  209.  
  210.  
  211.     /* Set up the per-screen global information. */
  212.  
  213.     NumScreens = ScreenCount(dpy);
  214.  
  215.     if (MultiScreen)
  216.     {
  217.     firstscrn = 0;
  218.     lastscrn = NumScreens - 1;
  219.     }
  220.     else
  221.     {
  222.     firstscrn = lastscrn = DefaultScreen(dpy);
  223.     }
  224.  
  225.     InfoLines = 0;
  226.  
  227.     /* for simplicity, always allocate NumScreens ScreenInfo struct pointers */
  228.     ScreenList = (ScreenInfo **) calloc (NumScreens, sizeof (ScreenInfo *));
  229.     if (ScreenList == NULL)
  230.     {
  231.     fprintf (stderr, "%s: Unable to allocate memory for screen list, exiting.\n",
  232.          ProgramName);
  233.     exit (1);
  234.     }
  235.     numManaged = 0;
  236.     PreviousScreen = DefaultScreen(dpy);
  237.     FirstScreen = TRUE;
  238.     for (scrnum = firstscrn ; scrnum <= lastscrn; scrnum++)
  239.     {
  240.         /* Make sure property priority colors is empty */
  241.         XChangeProperty (dpy, RootWindow(dpy, scrnum), _XA_MIT_PRIORITY_COLORS,
  242.              XA_CARDINAL, 32, PropModeReplace, NULL, 0);
  243.     RedirectError = FALSE;
  244.     XSetErrorHandler(CatchRedirectError);
  245.     XSelectInput(dpy, RootWindow (dpy, scrnum),
  246.         ColormapChangeMask | EnterWindowMask | PropertyChangeMask | 
  247.         SubstructureRedirectMask | KeyPressMask |
  248.         ButtonPressMask | ButtonReleaseMask);
  249.     XSync(dpy, 0);
  250.     XSetErrorHandler(TwmErrorHandler);
  251.  
  252.     if (RedirectError)
  253.     {
  254.         fprintf (stderr, "%s:  another window manager is already running",
  255.              ProgramName);
  256.         if (MultiScreen && NumScreens > 0)
  257.         fprintf(stderr, " on screen %d?\n", scrnum);
  258.         else
  259.         fprintf(stderr, "?\n");
  260.         continue;
  261.     }
  262.  
  263.     numManaged ++;
  264.  
  265.     /* Note:  ScreenInfo struct is calloc'ed to initialize to zero. */
  266.     Scr = ScreenList[scrnum] = 
  267.         (ScreenInfo *) calloc(1, sizeof(ScreenInfo));
  268.       if (Scr == NULL)
  269.       {
  270.           fprintf (stderr, "%s: unable to allocate memory for ScreenInfo structure for screen %d.\n",
  271.                ProgramName, scrnum);
  272.           continue;
  273.       }
  274.  
  275.     /* initialize list pointers, remember to put an initialization
  276.      * in InitVariables also
  277.      */
  278.     Scr->BorderColorL = NULL;
  279.     Scr->IconBorderColorL = NULL;
  280.     Scr->BorderTileForegroundL = NULL;
  281.     Scr->BorderTileBackgroundL = NULL;
  282.     Scr->TitleForegroundL = NULL;
  283.     Scr->TitleBackgroundL = NULL;
  284.     Scr->IconForegroundL = NULL;
  285.     Scr->IconBackgroundL = NULL;
  286.     Scr->NoTitle = NULL;
  287.     Scr->MakeTitle = NULL;
  288.     Scr->AutoRaise = NULL;
  289.     Scr->IconNames = NULL;
  290.     Scr->NoHighlight = NULL;
  291.     Scr->NoStackModeL = NULL;
  292.     Scr->NoTitleHighlight = NULL;
  293.     Scr->DontIconify = NULL;
  294.     Scr->IconMgrNoShow = NULL;
  295.     Scr->IconMgrShow = NULL;
  296.     Scr->IconifyByUn = NULL;
  297.     Scr->IconManagerFL = NULL;
  298.     Scr->IconManagerBL = NULL;
  299.     Scr->IconMgrs = NULL;
  300.     Scr->StartIconified = NULL;
  301.     Scr->SqueezeTitleL = NULL;
  302.     Scr->DontSqueezeTitleL = NULL;
  303.     Scr->WindowRingL = NULL;
  304.     Scr->WarpCursorL = NULL;
  305.     /* remember to put an initialization in InitVariables also
  306.      */
  307.  
  308.     Scr->screen = scrnum;
  309.     Scr->d_depth = DefaultDepth(dpy, scrnum);
  310.     Scr->d_visual = DefaultVisual(dpy, scrnum);
  311.     Scr->Root = RootWindow(dpy, scrnum);
  312.     XSaveContext (dpy, Scr->Root, ScreenContext, (caddr_t) Scr);
  313.  
  314.     Scr->TwmRoot.cmaps.number_cwins = 1;
  315.     Scr->TwmRoot.cmaps.cwins =
  316.         (ColormapWindow **) malloc(sizeof(ColormapWindow *));
  317.     Scr->TwmRoot.cmaps.cwins[0] =
  318.         CreateColormapWindow(Scr->Root, True, False);
  319.     Scr->TwmRoot.cmaps.cwins[0]->visibility = VisibilityPartiallyObscured;
  320.  
  321.     Scr->cmapInfo.cmaps = NULL;
  322.     Scr->cmapInfo.maxCmaps =
  323.         MaxCmapsOfScreen(ScreenOfDisplay(dpy, Scr->screen));
  324.     Scr->cmapInfo.root_pushes = 0;
  325.     InstallWindowColormaps(0, &Scr->TwmRoot);
  326.  
  327.     Scr->StdCmapInfo.head = Scr->StdCmapInfo.tail = 
  328.       Scr->StdCmapInfo.mru = NULL;
  329.     Scr->StdCmapInfo.mruindex = 0;
  330.     LocateStandardColormaps();
  331.  
  332.     Scr->TBInfo.nleft = Scr->TBInfo.nright = 0;
  333.     Scr->TBInfo.head = NULL;
  334.     Scr->TBInfo.border = 1;
  335.     Scr->TBInfo.width = 0;
  336.     Scr->TBInfo.leftx = 0;
  337.     Scr->TBInfo.titlex = 0;
  338.  
  339.     Scr->MyDisplayWidth = DisplayWidth(dpy, scrnum);
  340.     Scr->MyDisplayHeight = DisplayHeight(dpy, scrnum);
  341.     Scr->MaxWindowWidth = 32767 - Scr->MyDisplayWidth;
  342.     Scr->MaxWindowHeight = 32767 - Scr->MyDisplayHeight;
  343.  
  344.     Scr->XORvalue = (((unsigned long) 1) << Scr->d_depth) - 1;
  345.  
  346.     if (DisplayCells(dpy, scrnum) < 3)
  347.         Scr->Monochrome = MONOCHROME;
  348.     else
  349.         Scr->Monochrome = COLOR;
  350.  
  351.     /* setup default colors */
  352.     Scr->FirstTime = TRUE;
  353.     GetColor(Scr->Monochrome, &black, "black");
  354.     Scr->Black = black;
  355.     GetColor(Scr->Monochrome, &white, "white");
  356.     Scr->White = white;
  357.  
  358.     if (FirstScreen)
  359.     {
  360.         SetFocus ((TwmWindow *)NULL, CurrentTime);
  361.  
  362.         /* define cursors */
  363.  
  364.         NewFontCursor(&UpperLeftCursor, "top_left_corner");
  365.         NewFontCursor(&RightButt, "rightbutton");
  366.         NewFontCursor(&LeftButt, "leftbutton");
  367.         NewFontCursor(&MiddleButt, "middlebutton");
  368.     }
  369.  
  370.     Scr->iconmgr.x = 0;
  371.     Scr->iconmgr.y = 0;
  372.     Scr->iconmgr.width = 150;
  373.     Scr->iconmgr.height = 5;
  374.     Scr->iconmgr.next = NULL;
  375.     Scr->iconmgr.prev = NULL;
  376.     Scr->iconmgr.lasti = &(Scr->iconmgr);
  377.     Scr->iconmgr.first = NULL;
  378.     Scr->iconmgr.last = NULL;
  379.     Scr->iconmgr.active = NULL;
  380.     Scr->iconmgr.scr = Scr;
  381.     Scr->iconmgr.columns = 1;
  382.     Scr->iconmgr.count = 0;
  383.     Scr->iconmgr.name = "TWM";
  384.     Scr->iconmgr.icon_name = "Icons";
  385.  
  386.     Scr->IconDirectory = NULL;
  387.  
  388.     Scr->siconifyPm = None;
  389.     Scr->pullPm = None;
  390.     Scr->hilitePm = None;
  391.     Scr->tbpm.xlogo = None;
  392.     Scr->tbpm.resize = None;
  393.     Scr->tbpm.question = None;
  394.     Scr->tbpm.menu = None;
  395.     Scr->tbpm.delete = None;
  396.  
  397.     InitVariables();
  398.     InitMenus();
  399.  
  400.     /* Parse it once for each screen. */
  401.     ParseTwmrc(InitFile);
  402.     assign_var_savecolor(); /* storeing pixels for twmrc "entities" */
  403.     if (Scr->SqueezeTitle == -1) Scr->SqueezeTitle = FALSE;
  404.     if (!Scr->HaveFonts) CreateFonts();
  405.     CreateGCs();
  406.     MakeMenus();
  407.  
  408.     Scr->TitleBarFont.y += Scr->FramePadding;
  409.     Scr->TitleHeight = Scr->TitleBarFont.height + Scr->FramePadding * 2;
  410.     /* make title height be odd so buttons look nice and centered */
  411.     if (!(Scr->TitleHeight & 1)) Scr->TitleHeight++;
  412.  
  413.     InitTitlebarButtons ();        /* menus are now loaded! */
  414.  
  415.     XGrabServer(dpy);
  416.     XSync(dpy, 0);
  417.  
  418.     JunkX = 0;
  419.     JunkY = 0;
  420.  
  421.     XQueryTree(dpy, Scr->Root, &root, &parent, &children, &nchildren);
  422.     CreateIconManagers();
  423.     if (!Scr->NoIconManagers)
  424.         Scr->iconmgr.twm_win->icon = TRUE;
  425.  
  426.     /*
  427.      * weed out icon windows
  428.      */
  429.     for (i = 0; i < nchildren; i++) {
  430.         if (children[i]) {
  431.         XWMHints *wmhintsp = XGetWMHints (dpy, children[i]);
  432.  
  433.         if (wmhintsp) {
  434.             if (wmhintsp->flags & IconWindowHint) {
  435.             for (j = 0; j < nchildren; j++) {
  436.                 if (children[j] == wmhintsp->icon_window) {
  437.                 children[j] = None;
  438.                 break;
  439.                 }
  440.             }
  441.             }
  442.             XFree ((char *) wmhintsp);
  443.         }
  444.         }
  445.     }
  446.  
  447.     /*
  448.      * map all of the non-override windows
  449.      */
  450.     for (i = 0; i < nchildren; i++)
  451.     {
  452.         if (children[i] && MappedNotOverride(children[i]))
  453.         {
  454.         XUnmapWindow(dpy, children[i]);
  455.         SimulateMapRequest(children[i]);
  456.         }
  457.     }
  458.  
  459.     if (Scr->ShowIconManager && !Scr->NoIconManagers)
  460.     {
  461.         Scr->iconmgr.twm_win->icon = FALSE;
  462.         if (Scr->iconmgr.count)
  463.         {
  464.         SetMapStateProp (Scr->iconmgr.twm_win, NormalState);
  465.         XMapWindow(dpy, Scr->iconmgr.w);
  466.         XMapWindow(dpy, Scr->iconmgr.twm_win->frame);
  467.         }
  468.     }
  469.  
  470.     
  471.     attributes.border_pixel = Scr->DefaultC.fore;
  472.     attributes.background_pixel = Scr->DefaultC.back;
  473.     attributes.event_mask = (ExposureMask | ButtonPressMask |
  474.                  KeyPressMask | ButtonReleaseMask);
  475.     attributes.backing_store = NotUseful;
  476.     attributes.cursor = XCreateFontCursor (dpy, XC_hand2);
  477.     valuemask = (CWBorderPixel | CWBackPixel | CWEventMask | 
  478.              CWBackingStore | CWCursor);
  479.     Scr->InfoWindow = XCreateWindow (dpy, Scr->Root, 0, 0, 
  480.                      (unsigned int) 5, (unsigned int) 5,
  481.                      (unsigned int) BW, 0,
  482.                      (unsigned int) CopyFromParent,
  483.                      (Visual *) CopyFromParent,
  484.                      valuemask, &attributes);
  485.  
  486.     Scr->SizeStringWidth = XTextWidth (Scr->SizeFont.font,
  487.                        " 8888 x 8888 ", 13);
  488.     valuemask = (CWBorderPixel | CWBackPixel | CWBitGravity);
  489.     attributes.bit_gravity = NorthWestGravity;
  490.     Scr->SizeWindow = XCreateWindow (dpy, Scr->Root, 0, 0, 
  491.                      (unsigned int) Scr->SizeStringWidth,
  492.                      (unsigned int) (Scr->SizeFont.height +
  493.                              SIZE_VINDENT*2),
  494.                      (unsigned int) BW, 0,
  495.                      (unsigned int) CopyFromParent,
  496.                      (Visual *) CopyFromParent,
  497.                      valuemask, &attributes);
  498.  
  499.     XUngrabServer(dpy);
  500.  
  501.     FirstScreen = FALSE;
  502.         Scr->FirstTime = FALSE;
  503.     } /* for */
  504.  
  505.     if (numManaged == 0) {
  506.     if (MultiScreen && NumScreens > 0)
  507.       fprintf (stderr, "%s:  unable to find any unmanaged screens\n",
  508.            ProgramName);
  509.     exit (1);
  510.     }
  511.  
  512.     RestartPreviousState = False;
  513.     HandlingEvents = TRUE;
  514.     InitEvents();
  515.     HandleEvents();
  516. }
  517.  
  518. /***********************************************************************
  519.  *
  520.  *  Procedure:
  521.  *    InitVariables - initialize twm variables
  522.  *
  523.  ***********************************************************************
  524.  */
  525.  
  526. InitVariables()
  527. {
  528.     FreeList(&Scr->BorderColorL);
  529.     FreeList(&Scr->IconBorderColorL);
  530.     FreeList(&Scr->BorderTileForegroundL);
  531.     FreeList(&Scr->BorderTileBackgroundL);
  532.     FreeList(&Scr->TitleForegroundL);
  533.     FreeList(&Scr->TitleBackgroundL);
  534.     FreeList(&Scr->IconForegroundL);
  535.     FreeList(&Scr->IconBackgroundL);
  536.     FreeList(&Scr->IconManagerFL);
  537.     FreeList(&Scr->IconManagerBL);
  538.     FreeList(&Scr->IconMgrs);
  539.     FreeList(&Scr->NoTitle);
  540.     FreeList(&Scr->MakeTitle);
  541.     FreeList(&Scr->AutoRaise);
  542.     FreeList(&Scr->IconNames);
  543.     FreeList(&Scr->NoHighlight);
  544.     FreeList(&Scr->NoStackModeL);
  545.     FreeList(&Scr->NoTitleHighlight);
  546.     FreeList(&Scr->DontIconify);
  547.     FreeList(&Scr->IconMgrNoShow);
  548.     FreeList(&Scr->IconMgrShow);
  549.     FreeList(&Scr->IconifyByUn);
  550.     FreeList(&Scr->StartIconified);
  551.     FreeList(&Scr->IconManagerHighlightL);
  552.     FreeList(&Scr->SqueezeTitleL);
  553.     FreeList(&Scr->DontSqueezeTitleL);
  554.     FreeList(&Scr->WindowRingL);
  555.     FreeList(&Scr->WarpCursorL);
  556.  
  557.     NewFontCursor(&Scr->FrameCursor, "top_left_arrow");
  558.     NewFontCursor(&Scr->TitleCursor, "top_left_arrow");
  559.     NewFontCursor(&Scr->IconCursor, "top_left_arrow");
  560.     NewFontCursor(&Scr->IconMgrCursor, "top_left_arrow");
  561.     NewFontCursor(&Scr->MoveCursor, "fleur");
  562.     NewFontCursor(&Scr->ResizeCursor, "fleur");
  563.     NewFontCursor(&Scr->MenuCursor, "sb_left_arrow");
  564.     NewFontCursor(&Scr->ButtonCursor, "hand2");
  565.     NewFontCursor(&Scr->WaitCursor, "watch");
  566.     NewFontCursor(&Scr->SelectCursor, "dot");
  567.     NewFontCursor(&Scr->DestroyCursor, "pirate");
  568.  
  569.     Scr->Ring = NULL;
  570.     Scr->RingLeader = NULL;
  571.  
  572.     Scr->DefaultC.fore = black;
  573.     Scr->DefaultC.back = white;
  574.     Scr->BorderColor = black;
  575.     Scr->BorderTileC.fore = black;
  576.     Scr->BorderTileC.back = white;
  577.     Scr->TitleC.fore = black;
  578.     Scr->TitleC.back = white;
  579.     Scr->MenuC.fore = black;
  580.     Scr->MenuC.back = white;
  581.     Scr->MenuTitleC.fore = black;
  582.     Scr->MenuTitleC.back = white;
  583.     Scr->MenuShadowColor = black;
  584.     Scr->IconC.fore = black;
  585.     Scr->IconC.back = white;
  586.     Scr->IconBorderColor = black;
  587.     Scr->IconManagerC.fore = black;
  588.     Scr->IconManagerC.back = white;
  589.     Scr->IconManagerHighlight = black;
  590.  
  591.     Scr->FramePadding = 2;        /* values that look "nice" on */
  592.     Scr->TitlePadding = 8;        /* 75 and 100dpi displays */
  593.     Scr->ButtonIndent = 1;
  594.     Scr->SizeStringOffset = 0;
  595.     Scr->BorderWidth = BW;
  596.     Scr->IconBorderWidth = BW;
  597.     Scr->UnknownWidth = 0;
  598.     Scr->UnknownHeight = 0;
  599.     Scr->NumAutoRaises = 0;
  600.     Scr->NoDefaults = FALSE;
  601.     Scr->UsePPosition = PPOS_OFF;
  602.     Scr->FocusRoot = TRUE;
  603.     Scr->Focus = NULL;
  604.     Scr->WarpCursor = FALSE;
  605.     Scr->ForceIcon = FALSE;
  606.     Scr->NoGrabServer = FALSE;
  607.     Scr->NoRaiseMove = FALSE;
  608.     Scr->NoRaiseResize = FALSE;
  609.     Scr->NoRaiseDeicon = FALSE;
  610.     Scr->NoRaiseWarp = FALSE;
  611.     Scr->DontMoveOff = FALSE;
  612.     Scr->DoZoom = FALSE;
  613.     Scr->TitleFocus = TRUE;
  614.     Scr->NoTitlebar = FALSE;
  615.     Scr->DecorateTransients = FALSE;
  616.     Scr->IconifyByUnmapping = FALSE;
  617.     Scr->ShowIconManager = FALSE;
  618.     Scr->IconManagerDontShow =FALSE;
  619.     Scr->BackingStore = TRUE;
  620.     Scr->SaveUnder = TRUE;
  621.     Scr->RandomPlacement = FALSE;
  622.     Scr->OpaqueMove = FALSE;
  623.     Scr->Highlight = TRUE;
  624.     Scr->StackMode = TRUE;
  625.     Scr->TitleHighlight = TRUE;
  626.     Scr->MoveDelta = 1;        /* so that f.deltastop will work */
  627.     Scr->ZoomCount = 8;
  628.     Scr->SortIconMgr = FALSE;
  629.     Scr->Shadow = TRUE;
  630.     Scr->InterpolateMenuColors = FALSE;
  631.     Scr->NoIconManagers = FALSE;
  632.     Scr->ClientBorderWidth = FALSE;
  633.     Scr->SqueezeTitle = -1;
  634.     Scr->FirstRegion = NULL;
  635.     Scr->LastRegion = NULL;
  636.     Scr->FirstTime = TRUE;
  637.     Scr->HaveFonts = FALSE;        /* i.e. not loaded yet */
  638.     Scr->CaseSensitive = TRUE;
  639.     Scr->WarpUnmapped = FALSE;
  640.  
  641.     /* setup default fonts; overridden by defaults from system.twmrc */
  642. #define DEFAULT_NICE_FONT "variable"
  643. #define DEFAULT_FAST_FONT "fixed"
  644.  
  645.     Scr->TitleBarFont.font = NULL;
  646.     Scr->TitleBarFont.name = DEFAULT_NICE_FONT;
  647.     Scr->MenuFont.font = NULL;
  648.     Scr->MenuFont.name = DEFAULT_NICE_FONT;
  649.     Scr->IconFont.font = NULL;
  650.     Scr->IconFont.name = DEFAULT_NICE_FONT;
  651.     Scr->SizeFont.font = NULL;
  652.     Scr->SizeFont.name = DEFAULT_FAST_FONT;
  653.     Scr->IconManagerFont.font = NULL;
  654.     Scr->IconManagerFont.name = DEFAULT_NICE_FONT;
  655.     Scr->DefaultFont.font = NULL;
  656.     Scr->DefaultFont.name = DEFAULT_FAST_FONT;
  657.  
  658. }
  659.  
  660.  
  661. CreateFonts ()
  662. {
  663.     GetFont(&Scr->TitleBarFont);
  664.     GetFont(&Scr->MenuFont);
  665.     GetFont(&Scr->IconFont);
  666.     GetFont(&Scr->SizeFont);
  667.     GetFont(&Scr->IconManagerFont);
  668.     GetFont(&Scr->DefaultFont);
  669.     Scr->HaveFonts = TRUE;
  670. }
  671.  
  672.  
  673. RestoreWithdrawnLocation (tmp)
  674.     TwmWindow *tmp;
  675. {
  676.     int gravx, gravy;
  677.     unsigned int bw, mask;
  678.     XWindowChanges xwc;
  679.  
  680.     if (XGetGeometry (dpy, tmp->w, &JunkRoot, &xwc.x, &xwc.y, 
  681.               &JunkWidth, &JunkHeight, &bw, &JunkDepth)) {
  682.  
  683.     GetGravityOffsets (tmp, &gravx, &gravy);
  684.     if (gravy < 0) xwc.y -= tmp->title_height;
  685.  
  686.     if (bw != tmp->old_bw) {
  687.         int xoff, yoff;
  688.  
  689.         if (!Scr->ClientBorderWidth) {
  690.         xoff = gravx;
  691.         yoff = gravy;
  692.         } else {
  693.         xoff = 0;
  694.         yoff = 0;
  695.         }
  696.  
  697.         xwc.x -= (xoff + 1) * tmp->old_bw;
  698.         xwc.y -= (yoff + 1) * tmp->old_bw;
  699.     }
  700.     if (!Scr->ClientBorderWidth) {
  701.         xwc.x += gravx * tmp->frame_bw;
  702.         xwc.y += gravy * tmp->frame_bw;
  703.     }
  704.  
  705.     mask = (CWX | CWY);
  706.     if (bw != tmp->old_bw) {
  707.         xwc.border_width = tmp->old_bw;
  708.         mask |= CWBorderWidth;
  709.     }
  710.  
  711.     XConfigureWindow (dpy, tmp->w, mask, &xwc);
  712.  
  713.     if (tmp->wmhints && (tmp->wmhints->flags & IconWindowHint)) {
  714.         XUnmapWindow (dpy, tmp->wmhints->icon_window);
  715.     }
  716.  
  717.     }
  718. }
  719.  
  720.  
  721. /***********************************************************************
  722.  *
  723.  *  Procedure:
  724.  *    Done - cleanup and exit twm
  725.  *
  726.  *  Returned Value:
  727.  *    none
  728.  *
  729.  *  Inputs:
  730.  *    none
  731.  *
  732.  *  Outputs:
  733.  *    none
  734.  *
  735.  *  Special Considerations:
  736.  *    none
  737.  *
  738.  ***********************************************************************
  739.  */
  740.  
  741. void Reborder (time)
  742. Time time;
  743. {
  744.     TwmWindow *tmp;            /* temp twm window structure */
  745.     int scrnum;
  746.  
  747.     /* put a border back around all windows */
  748.  
  749.     XGrabServer (dpy);
  750.     for (scrnum = 0; scrnum < NumScreens; scrnum++)
  751.     {
  752.     if ((Scr = ScreenList[scrnum]) == NULL)
  753.         continue;
  754.  
  755.     InstallWindowColormaps (0, &Scr->TwmRoot);    /* force reinstall */
  756.     for (tmp = Scr->TwmRoot.next; tmp != NULL; tmp = tmp->next)
  757.     {
  758.         RestoreWithdrawnLocation (tmp);
  759.         XMapWindow (dpy, tmp->w);
  760.     }
  761.     }
  762.  
  763.     XUngrabServer (dpy);
  764.     SetFocus ((TwmWindow*)NULL, time);
  765. }
  766.  
  767. SIGNAL_T Done()
  768. {
  769.     Reborder (CurrentTime);
  770.     XCloseDisplay(dpy);
  771.     exit(0);
  772.     SIGNAL_RETURN;
  773. }
  774.  
  775.  
  776. /*
  777.  * Error Handlers.  If a client dies, we'll get a BadWindow error (except for
  778.  * GetGeometry which returns BadDrawable) for most operations that we do before
  779.  * manipulating the client's window.
  780.  */
  781.  
  782. Bool ErrorOccurred = False;
  783. XErrorEvent LastErrorEvent;
  784.  
  785. static int TwmErrorHandler(dpy, event)
  786.     Display *dpy;
  787.     XErrorEvent *event;
  788. {
  789.     LastErrorEvent = *event;
  790.     ErrorOccurred = True;
  791.  
  792.     if (PrintErrorMessages &&             /* don't be too obnoxious */
  793.     event->error_code != BadWindow &&    /* watch for dead puppies */
  794.     (event->request_code != X_GetGeometry &&     /* of all styles */
  795.      event->error_code != BadDrawable))
  796.       XmuPrintDefaultErrorMessage (dpy, event, stderr);
  797.     return 0;
  798. }
  799.  
  800.  
  801. /* ARGSUSED*/
  802. static int CatchRedirectError(dpy, event)
  803.     Display *dpy;
  804.     XErrorEvent *event;
  805. {
  806.     RedirectError = TRUE;
  807.     LastErrorEvent = *event;
  808.     ErrorOccurred = True;
  809.     return 0;
  810. }
  811.  
  812. Atom _XA_MIT_PRIORITY_COLORS;
  813. Atom _XA_WM_CHANGE_STATE;
  814. Atom _XA_WM_STATE;
  815. Atom _XA_WM_COLORMAP_WINDOWS;
  816. Atom _XA_WM_PROTOCOLS;
  817. Atom _XA_WM_TAKE_FOCUS;
  818. Atom _XA_WM_SAVE_YOURSELF;
  819. Atom _XA_WM_DELETE_WINDOW;
  820.  
  821. InternUsefulAtoms ()
  822. {
  823.     /* 
  824.      * Create priority colors if necessary.
  825.      */
  826.     _XA_MIT_PRIORITY_COLORS = XInternAtom(dpy, "_MIT_PRIORITY_COLORS", False);   
  827.     _XA_WM_CHANGE_STATE = XInternAtom (dpy, "WM_CHANGE_STATE", False);
  828.     _XA_WM_STATE = XInternAtom (dpy, "WM_STATE", False);
  829.     _XA_WM_COLORMAP_WINDOWS = XInternAtom (dpy, "WM_COLORMAP_WINDOWS", False);
  830.     _XA_WM_PROTOCOLS = XInternAtom (dpy, "WM_PROTOCOLS", False);
  831.     _XA_WM_TAKE_FOCUS = XInternAtom (dpy, "WM_TAKE_FOCUS", False);
  832.     _XA_WM_SAVE_YOURSELF = XInternAtom (dpy, "WM_SAVE_YOURSELF", False);
  833.     _XA_WM_DELETE_WINDOW = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
  834. }
  835.