home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume15 / twm / part01 / twm.c < prev   
C/C++ Source or Header  |  1988-06-12  |  11KB  |  371 lines

  1. /*****************************************************************************/
  2. /**       Copyright 1988 by Evans & Sutherland Computer Corporation,        **/
  3. /**                          Salt Lake City, Utah                           **/
  4. /**                                                                         **/
  5. /**                           All Rights Reserved                           **/
  6. /**                                                                         **/
  7. /**    Permission to use, copy, modify, and distribute this software and    **/
  8. /**    its documentation  for  any  purpose  and  without  fee is hereby    **/
  9. /**    granted, provided that the above copyright notice appear  in  all    **/
  10. /**    copies and that both  that  copyright  notice  and  this  permis-    **/
  11. /**    sion  notice appear in supporting  documentation,  and  that  the    **/
  12. /**    name  of Evans & Sutherland  not be used in advertising or publi-    **/
  13. /**    city pertaining to distribution  of the software without  specif-    **/
  14. /**    ic, written prior permission.                                        **/
  15. /**                                                                         **/
  16. /**    EVANS  & SUTHERLAND  DISCLAIMS  ALL  WARRANTIES  WITH  REGARD  TO    **/
  17. /**    THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILI-    **/
  18. /**    TY AND FITNESS, IN NO EVENT SHALL EVANS &  SUTHERLAND  BE  LIABLE    **/
  19. /**    FOR  ANY  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY  DAM-    **/
  20. /**    AGES  WHATSOEVER RESULTING FROM  LOSS OF USE,  DATA  OR  PROFITS,    **/
  21. /**    WHETHER   IN  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS    **/
  22. /**    ACTION, ARISING OUT OF OR IN  CONNECTION  WITH  THE  USE  OR PER-    **/
  23. /**    FORMANCE OF THIS SOFTWARE.                                           **/
  24. /*****************************************************************************/
  25.  
  26. /***********************************************************************
  27.  *
  28.  * $Header: twm.c,v 1.23 88/04/15 07:09:30 tlastran Exp $
  29.  *
  30.  * twm - "Tom's Window Manager"
  31.  *
  32.  * 27-Oct-87 Thomas E. LaStrange    File created
  33.  *
  34.  ***********************************************************************/
  35.  
  36. #ifndef lint
  37. static char RCSinfo[] =
  38. "$Header: twm.c,v 1.23 88/04/15 07:09:30 tlastran Exp $";
  39. #endif
  40.  
  41. #include <stdio.h>
  42. #include <signal.h>
  43. #include <fcntl.h>
  44. #include "twm.h"
  45. #include "add_window.h"
  46. #include "gc.h"
  47. #include "parse.h"
  48. #include "version.h"
  49. #include "menus.h"
  50. #include "events.h"
  51. #include "util.h"
  52.  
  53. #include "twm.bm"
  54.  
  55. TwmWindow TwmRoot;        /* the head of the twm window list */
  56.  
  57. Display *dpy;            /* which display are we talking to */
  58.  
  59. Window Root;            /* the root window */
  60. Window VersionWindow;        /* the twm version window */
  61. Window SizeWindow;        /* the resize dimensions window */
  62. Window ResizeWindow;        /* the window we are resizing */
  63.  
  64. XFontStruct *TitleBarFont;    /* title bar font structure */
  65. XFontStruct *MenuFont;        /* menu font structure */
  66. XFontStruct *IconFont;        /* icon font structure */
  67. XFontStruct *SizeFont;        /* resize font structure */
  68. XFontStruct *VersionFont;    /* version font structure */
  69.  
  70. char *TitleBarFontName = "8x13";/* default title bar font */
  71. char *MenuFontName = "8x13";    /* default menu font */
  72. char *IconFontName = "8x13";    /* default icon font */
  73. char *SizeFontName = "fg-22";    /* default resize font */
  74. char *VersionFontName = "8x13";    /* default version font */
  75.  
  76. int TitleBarFontHeight;        /* height of title bar font */
  77. int MenuFontHeight;        /* height of menu font */
  78. int IconFontHeight;        /* height of icon font */
  79. int SizeFontHeight;        /* height of resize font */
  80. int VersionFontHeight;        /* height of version font */
  81.  
  82. Cursor ArrowCursor;        /* title bar cursor */
  83. Cursor ButtonCursor;        /* title bar button cursor */
  84. Cursor MoveCursor;        /* move and resize cursor */
  85. Cursor ClockCursor;        /* wait a while cursor */
  86. Cursor RightArrowCursor;    /* menu cursor */
  87.  
  88. GC TitleNormalGC;        /* GC for title bar */
  89. GC MenuNormalGC;        /* normal GC for menus */
  90. GC MenuReverseGC;        /* reverse video GC for menus */
  91. GC MenuXorGC;            /* XOR GC for menus */
  92. GC IconNormalGC;        /* GC for icons */
  93. GC VersionNormalGC;        /* GC for the version window */
  94. GC SizeNormalGC;        /* GC for the resize window */
  95. GC DrawGC;            /* GC to draw lines for move and resize */
  96.  
  97. XContext TwmContext;        /* context for twm windows */
  98. XContext MenuContext;        /* context for all menu windows */
  99.  
  100. int BorderWidth = BW;        /* border width of twm windows */
  101. unsigned long Foreground;    /* foreground color for all windows */
  102. unsigned long Background;    /* background color for all windows */
  103.  
  104. char Version[100];        /* place to build the version string */
  105. Pixmap UnknownPm = NULL;    /* the unknown icon pixmap */
  106. int UnknownWidth = 0;        /* width of the unknown icon */
  107. int UnknownHeight = 0;        /* height of the unknown icon */
  108. int FirstTime = TRUE;        /* first time we've read .twmrc */
  109. int ReverseVideo = FALSE;    /* flag to do reverse video */
  110. int FocusRoot = TRUE;        /* is the input focus on the root ? */
  111. TwmWindow *Focus = NULL;    /* the twm window that has focus */
  112. int WarpCursor = FALSE;        /* warp cursor on de-iconify ? */
  113. int ParseError;            /* error parsing the .twmrc file */
  114.  
  115. int TitleButton[MAX_BUTTONS + 1];    /* title button functions */
  116.  
  117. int TitleBarY;            /* y coordinate to start text in the title */
  118. int TitleBarX = TITLE_BAR_HEIGHT + 4;    /* x coordinate ditto */
  119. int MenuY;            /* y coordiante for text in menus */
  120.  
  121. Window JunkRoot;        /* junk window */
  122. Window JunkChild;        /* junk window */
  123. int JunkX;            /* junk variable */
  124. int JunkY;            /* junk variable */
  125. int JunkWidth;            /* junk variable */
  126. int JunkHeight;            /* junk variable */
  127. int JunkDepth;            /* junk variable */
  128. int JunkBW;            /* junk variable */
  129. int JunkMask;            /* junk variable */
  130.  
  131. /***********************************************************************
  132.  *
  133.  *  Procedure:
  134.  *    main - start of twm
  135.  *
  136.  ***********************************************************************
  137.  */
  138.  
  139. main(argc, argv)
  140.     int argc;
  141.     char *argv[];
  142. {
  143.     Window root, parent, *children;
  144.     int nchildren, i;
  145.     int m, d, y;
  146.     char *display_name;
  147.     unsigned long valuemask;    /* mask for create windows */
  148.     XSetWindowAttributes attributes;    /* attributes for create windows */
  149.  
  150.     display_name = NULL;
  151.  
  152.     if (argc != 1 && argc != 3)
  153.     {
  154.     fprintf(stderr, "Usage: twm [-display display]\n");
  155.     exit(1);
  156.     }
  157.  
  158.     if (argc == 3)
  159.     {
  160.     if (strncmp(argv[1], "-d", 2) == 0)
  161.         display_name = argv[2];
  162.     else
  163.     {
  164.         fprintf(stderr, "Usage: twm [-display display]\n");
  165.         exit(1);
  166.     }
  167.     }
  168.  
  169.     signal(SIGINT, Done);
  170.     signal(SIGQUIT, Done);
  171.     signal(SIGHUP, Done);
  172.     signal(SIGTERM, Done);
  173.  
  174.     TwmRoot.next = NULL;
  175.     TwmRoot.prev = NULL;
  176.     TwmRoot.w = NULL;
  177.     TwmRoot.title_w = NULL;
  178.     TwmRoot.iconify_w = NULL;
  179.     TwmRoot.resize_w = NULL;
  180.  
  181.     if ((dpy = XOpenDisplay(display_name)) == NULL)
  182.     {
  183.     fprintf(stderr, "twm: can't open the display\n");
  184.     exit(1);
  185.     }
  186.  
  187.  
  188.     if (fcntl(ConnectionNumber(dpy), F_SETFD, 1) == -1)
  189.     {
  190.     fprintf(stderr, "twm: child cannot disinherit TCP fd\n");
  191.     exit(1);
  192.     }
  193.  
  194.     Root = RootWindow(dpy, DefaultScreen(dpy));
  195.  
  196.     XSetErrorHandler(Other);
  197.     XSelectInput(dpy, Root,
  198.     SubstructureRedirectMask |
  199.     ButtonPressMask | ButtonReleaseMask |
  200.     ExposureMask | ButtonMotionMask);
  201.     XSync(dpy, 0);
  202.  
  203.     XSetErrorHandler(Error);
  204.     XSetInputFocus(dpy, Root, RevertToPointerRoot, CurrentTime);
  205.  
  206.     TwmContext = XUniqueContext();
  207.     MenuContext = XUniqueContext();
  208.  
  209.     /* define cursors */
  210.  
  211.     ArrowCursor = XCreateFontCursor(dpy, XC_top_left_arrow);
  212.     MoveCursor = XCreateFontCursor(dpy, XC_fleur);
  213.     RightArrowCursor = XCreateFontCursor(dpy, XC_sb_right_arrow);
  214.     ButtonCursor = XCreateFontCursor(dpy, XC_center_ptr);
  215.     ClockCursor = XCreateFontCursor(dpy, XC_watch);
  216.  
  217.     XGrabServer(dpy);
  218.     XSync(dpy, 0);
  219.     XQueryTree(dpy, Root, &root, &parent, &children, &nchildren);
  220.  
  221.     ParseTwmrc(NULL);
  222.     FirstTime = FALSE;
  223.     CreateGCs();
  224.  
  225.     for (i = 0; i < nchildren; i++)
  226.     {
  227.     if (MappedNotOverride(children[i]))
  228.     {
  229.         AddWindow(children[i]);
  230.     }
  231.     }
  232.  
  233.     /* contruct the version string */
  234.     sprintf(Version, "%s", &Revision[1]);
  235.     Version[strlen(Version) - 1] = '\0';
  236.     sscanf(&Date[7], "%d/%d/%d", &y, &m, &d);
  237.     sprintf(Version, "%s  Date: %d/%d/%d %s", Version, m, d, y, &Date[16]);
  238.     Version[strlen(Version) - 2] = '\0';
  239.  
  240.     VersionWindow = XCreateSimpleWindow(dpy, Root,
  241.     0, 0,
  242.     twm_width + XTextWidth(VersionFont, Version, strlen(Version)) + 20,
  243.     VersionFontHeight + 4,
  244.     BW,
  245.     Foreground, Background);
  246.  
  247.     valuemask = CWBackPixmap;
  248.     attributes.background_pixmap = MakePixmap(VersionWindow, TitleNormalGC,
  249.     twm_bits, twm_width, twm_height);
  250.  
  251.     XCreateWindow(dpy, VersionWindow,
  252.     4, 1,
  253.     twm_width, twm_height,
  254.     0, DefaultDepth(dpy, 0), CopyFromParent, DefaultVisual(dpy, 0),
  255.     valuemask, &attributes);
  256.  
  257.     XSelectInput(dpy, VersionWindow, ExposureMask);
  258.     XMapSubwindows(dpy, VersionWindow);
  259.     XMapWindow(dpy, VersionWindow);
  260.  
  261.     SizeWindow = XCreateSimpleWindow(dpy, Root,
  262.     0, 0,
  263.     100,
  264.     SizeFontHeight + 4,
  265.     BW,
  266.     Foreground, Background);
  267.  
  268.  
  269.     XUngrabServer(dpy);
  270.  
  271.     HandleEvents();
  272. }
  273.  
  274. /***********************************************************************
  275.  *
  276.  *  Procedure:
  277.  *    Done - cleanup and exit twm
  278.  *
  279.  *  Returned Value:
  280.  *    none
  281.  *
  282.  *  Inputs:
  283.  *    none
  284.  *
  285.  *  Outputs:
  286.  *    none
  287.  *
  288.  *  Special Considerations:
  289.  *    none
  290.  *
  291.  ***********************************************************************
  292.  */
  293.  
  294. void
  295. Done()
  296. {
  297.     TwmWindow *tmp;            /* temp twm window structure */
  298.     unsigned x, y;
  299.     XWindowChanges xwc;        /* change window structure */
  300.     unsigned int xwcm;        /* change window mask */
  301.  
  302.     /* put a border back around all windows */
  303.  
  304.     for (tmp = TwmRoot.next; tmp != NULL; tmp = tmp->next)
  305.     {
  306.     XGetGeometry(dpy, tmp->w, &JunkRoot, &x, &y, &JunkWidth, &JunkHeight,
  307.         &JunkBW, &JunkDepth);
  308.  
  309.     xwcm = CWX | CWY | CWBorderWidth;
  310.  
  311.     xwc.x = x - (2 * BorderWidth);
  312.     xwc.y = y - (2 * BorderWidth);
  313.     xwc.border_width = BorderWidth;
  314.  
  315.     XConfigureWindow(dpy, tmp->w, xwcm, &xwc);
  316.     }
  317.  
  318.     XSetInputFocus(dpy, Root, RevertToPointerRoot, CurrentTime);
  319.     XCloseDisplay(dpy);
  320.     exit(0);
  321. }
  322.  
  323. /***********************************************************************
  324.  *
  325.  *  Procedure:
  326.  *    Error - X error handler.  Print the error and exit.
  327.  *
  328.  *  Inputs:
  329.  *    dpy    - the connection to the X server
  330.  *    event    - the error event structure
  331.  *
  332.  ***********************************************************************
  333.  */
  334.  
  335. void
  336. Error(dpy, event)
  337. Display *dpy;
  338. XErrorEvent *event;
  339. {
  340.     char buffer[BUFSIZ];
  341.  
  342.     XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
  343.     (void) fprintf(stderr, "X Error: %s\n", buffer);
  344.     (void) fprintf(stderr, "  Request Major code: %d\n", event->request_code);
  345.     (void) fprintf(stderr, "  Request Minor code: %d\n", event->minor_code);
  346.     (void) fprintf(stderr, "  ResourceId 0x%x\n", event->resourceid);
  347.     (void) fprintf(stderr, "  Error Serial #%d\n", event->serial);
  348.     (void) fprintf(stderr, "  Current Serial #%d\n", dpy->request);
  349.  
  350.     Done();
  351. }
  352.  
  353. /***********************************************************************
  354.  *
  355.  *  Procedure:
  356.  *    Other - error handler called if something else has set 
  357.  *        the attributes on the root window.  Typically
  358.  *        another window manager.
  359.  *
  360.  ***********************************************************************
  361.  */
  362.  
  363. void
  364. Other(dpy, event)
  365. Display *dpy;
  366. XErrorEvent *event;
  367. {
  368.     fprintf(stderr, "twm: Are you running another window manager?\n");
  369.     exit(1);
  370. }
  371.