home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / awm2 / part09 / lockscreen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-20  |  7.3 KB  |  289 lines

  1. #ifndef lint
  2.      static char sccs_id[] = "%W%  %H%";
  3. #endif
  4.  
  5. /*
  6.  *                             XLOCK V1.4
  7.  *
  8.  *                      A Terminal Locker for X11
  9.  *
  10.  *              Copyright (c) 1988 by Patrick J. Naughton
  11.  *
  12.  *                         All Rights Reserved
  13.  *
  14.  * Permission to use, copy, modify, and distribute this software and its
  15.  * documentation for any purpose and without fee is hereby granted,
  16.  * provided that the above copyright notice appear in all copies and that
  17.  * both that copyright notice and this permission notice appear in
  18.  * supporting documentation.
  19.  *
  20.  * Original version posted to comp.windows.x by:
  21.  * Walter Milliken
  22.  * (milliken@heron.bbn.com)
  23.  * BBN Advanced Computers, Inc., Cambridge, MA
  24.  *
  25.  * Comments and additions may be sent the author at:
  26.  *
  27.  * naughton@sun.soe.clarkson.edu
  28.  *
  29.  * or by Snail Mail:
  30.  * Patrick J. Naughton
  31.  * 23 Pleasant Street, #3
  32.  * Potsdam, NY  13676
  33.  *         or
  34.  * (315) 265-2853 (voice)
  35.  *
  36.  *
  37.  * Revision History:
  38.  * 12-Apr-88: Added root password override.
  39.  *            Added screen saver override.
  40.  *            Removed XGrabServer/XUngrabServer (Bad idea Phil...)
  41.  *            Added access control handling instead.
  42.  * 01-Apr-88: Added XGrabServer/XUngrabServer for more security.
  43.  * 30-Mar-88: Removed startup password requirement (why did I add that?)
  44.  *            Removed cursor to avoid phosphor burn.
  45.  * 27-Mar-88: Rotate fractal by 45 degrees clockwise. (aesthetics)
  46.  * 23-Mar-88: Added HOPALONG routines from Scientific American Sept. 86 p. 14.
  47.  *            added password requirement for invokation
  48.  *            removed option for command line password
  49.  *            added requirement for display to be "unix:0".
  50.  * 22-Mar-88: Recieved Walter Milliken's comp.windows.x posting.
  51.  *
  52.  * 20-Dec-88: Incorporated into awm. -jkh
  53.  *
  54.  */
  55.  
  56. #ifdef LOCKSCR
  57. #include <pwd.h>
  58. #include <math.h>
  59. #include "awm.h"
  60. #include <X11/Xutil.h>
  61. char *crypt();
  62. void ReadXString(), lockscreen(), GrabHosts(), UngrabHosts();
  63. void iterate(), inithop();
  64.  
  65. static char no_bits[] = {0};
  66.  
  67. Window w;            /* window used to cover screen */
  68. GC gc;
  69.  
  70. unsigned int width;         /* width of screen */
  71. unsigned int height;        /* height of screen */
  72.  
  73. Window rootw;            /* root window */
  74. int screen;            /* current screen */
  75. Colormap cmap;            /* colormap of current screen */
  76. Cursor mycursor;                /* blank cursor */
  77.  
  78. unsigned long black_pixel;    /* pixel value for black */
  79. unsigned long white_pixel;    /* pixel value for white */
  80. XColor black_color;        /* color value for black */
  81. XColor white_color;        /* color value for white */
  82.  
  83. int centerx, centery, iter, maxiter, range, color;
  84. double a, b, c, i, j;
  85.  
  86. Display *dsp;
  87.  
  88.  
  89. void ReadXString(s, slen)
  90. char *s;
  91. int slen;
  92. {
  93.      int bp;
  94.      char c;
  95.      XEvent evt;
  96.      XKeyEvent *kpevt = (XKeyEvent *) &evt;
  97.      char keystr[20];
  98.      
  99.      Entry("ReadXString")
  100.       
  101.      bp = 0;
  102.      while (1) {
  103.       if (XPending(dsp)) {
  104.            XNextEvent(dsp, &evt);
  105.            if (evt.type == KeyPress) {
  106.             if (XLookupString(kpevt, keystr, 20, (KeySym *) NULL, 
  107.                       (XComposeStatus *) NULL) > 0) {
  108.              c = keystr[0];
  109.              switch (c) {
  110.              case 8:            /* ^H */
  111.                   if (bp > 0) bp--;
  112.                   break;
  113.              case 13:        /* ^M */
  114.                   s[bp] = '\0';
  115.                   Leave_void
  116.                   case 21:        /* ^U */
  117.                    bp = 0;
  118.                   break;
  119.              default:
  120.                   s[bp] = c;
  121.                   if (bp < slen-1) bp++;
  122.              }
  123.             }
  124.            }
  125.       }
  126.       else iterate();
  127.      }
  128.      Leave_void
  129. }
  130.  
  131.  
  132. void lockscreen(dpy)
  133. Display *dpy;
  134. {
  135.      char buf[10];
  136.      char rootpass[10];
  137.      XSetWindowAttributes attrs;
  138.      XGCValues xgcv;
  139.      struct passwd *pw;
  140.      Pixmap lockc, lockm;   
  141.      int timeout, interval, blanking, exposures; /* screen saver parameters */
  142.      extern GC XCreateGC();
  143.      
  144.      Entry("lockscreen")
  145.       
  146.      color = GetBoolRes("lock.useColor", FALSE);
  147.      dsp = dpy;
  148.      
  149.      pw = getpwuid(0);
  150.      strcpy(rootpass, pw->pw_passwd);
  151.      
  152.      rootw = DefaultRootWindow(dsp);
  153.      screen = DefaultScreen(dsp);
  154.      width = DisplayWidth(dsp, screen);
  155.      height = DisplayHeight(dsp, screen);
  156.      centerx = width / 2;
  157.      centery = height / 2;
  158.      range = (int) sqrt((double)centerx*centerx+(double)centery*centery);
  159.      cmap = DefaultColormap(dsp, screen);
  160.      
  161.      black_pixel = BlackPixel(dsp, screen);
  162.      black_color.pixel = black_pixel;
  163.      XQueryColor(dsp, cmap, &black_color);
  164.      
  165.      white_pixel = WhitePixel(dsp, screen);
  166.      white_color.pixel = white_pixel;
  167.      XQueryColor(dsp, cmap, &white_color);
  168.      
  169.      attrs.background_pixel = black_pixel;
  170.      attrs.override_redirect = True;
  171.      attrs.event_mask = KeyPressMask;
  172.      w = XCreateWindow(dsp, rootw, 0, 0, width, height, 0,
  173.                CopyFromParent, InputOutput, CopyFromParent,
  174.                CWOverrideRedirect | CWBackPixel | CWEventMask, &attrs);
  175.      
  176.      lockc = XCreateBitmapFromData(dsp, w, no_bits, 8, 1);
  177.      lockm = XCreateBitmapFromData(dsp, w, no_bits, 8, 1);
  178.      mycursor = XCreatePixmapCursor(dsp, lockc, lockm,
  179.                     &black_color, &black_color,
  180.                     0, 0);
  181.      XFreePixmap(dsp, lockc);
  182.      XFreePixmap(dsp, lockm);
  183.      
  184.      XMapWindow(dsp, w);
  185.      
  186.      xgcv.foreground = white_pixel;
  187.      xgcv.background = black_pixel;
  188.      gc = XCreateGC(dsp, w, GCForeground | GCBackground, &xgcv);
  189.      
  190.      XGetScreenSaver(dsp, &timeout, &interval, &blanking, &exposures);
  191.      XSetScreenSaver(dsp, 0, 0, 0, 0); /* disable screen saver */
  192.      
  193.      XGrabKeyboard(dsp, w, True, GrabModeAsync, GrabModeAsync, CurrentTime);
  194.      XGrabPointer(dsp, w, False, -1, GrabModeAsync, GrabModeAsync, None,
  195.           mycursor, CurrentTime);
  196.      
  197.      GrabHosts();
  198.      
  199.      pw = getpwuid(getuid());
  200.      
  201.      srandom(time(NULL));
  202.      do {
  203.       inithop();
  204.       ReadXString(buf, 10);
  205.      } while ((strcmp(crypt(buf, pw->pw_passwd), pw->pw_passwd))
  206.           && (strcmp(crypt(buf, rootpass), rootpass)));
  207.      
  208.      UngrabHosts();
  209.      
  210.      XUngrabPointer(dsp, CurrentTime);
  211.      XUngrabKeyboard(dsp, CurrentTime);
  212.      
  213.      XSetScreenSaver(dsp, timeout, interval, blanking, exposures);
  214.      XDestroyWindow(dsp, w);
  215.      XFlush(dsp);
  216.      Leave_void
  217. }
  218.  
  219. XHostAddress *hosts;
  220. int num_hosts, state;
  221.  
  222. void GrabHosts()
  223. {
  224.      Entry("GrabHosts")
  225.       
  226.      XDisableAccessControl(dsp);
  227.      hosts = XListHosts(dsp, &num_hosts, &state);
  228.      XRemoveHosts(dsp, hosts, num_hosts);
  229.      Leave_void
  230. }
  231.  
  232.  
  233. void UngrabHosts()
  234. {
  235.      Entry("UngrabHosts")
  236.       
  237.      XEnableAccessControl(dsp);
  238.      XAddHosts(dsp, hosts, num_hosts);
  239.      XFree(hosts);
  240.      Leave_void
  241. }
  242.  
  243.  
  244. void iterate()
  245. {
  246.      double oldj;
  247.      register int k;
  248.      
  249.      Entry("iterate")
  250.       
  251.      for (k=0;k<500;k++) {
  252.       oldj = j;
  253.       j = a - i;
  254.       i = oldj + ((i < 0) ? sqrt(fabs(b*i - c)) : -sqrt(fabs(b*i - c))); 
  255.       if (color)
  256.            XSetForeground(dsp, gc, (iter % 25) * 10 + 5); 
  257.  
  258.       XDrawPoint(dsp, w, gc,
  259.              centerx + (int)(i+j), /* sneaky way to rotate +45 deg. */
  260.              centery - (int)(i-j));
  261.       iter++;
  262.      }
  263.      if (iter > maxiter)
  264.       inithop();
  265.      Leave_void
  266. }
  267.  
  268. void inithop()
  269. {
  270.      Entry("inithop")
  271.       
  272.      a = random() % (range * 100) * (random()%2?-1.0:1.0) / 100.0;
  273.      b = random() % (range * 100) * (random()%2?-1.0:1.0) / 100.0;
  274.      c = random() % (range * 100) * (random()%2?-1.0:1.0) / 100.0;
  275.      
  276.      if (!(random()%3))
  277.       a /= 10.0;
  278.      if (!(random()%2))
  279.       b /= 100.0;
  280.      
  281.      maxiter = (color?10000+random()%20000:20000+random()%50000);
  282.      iter = 0;
  283.      i = j = 0.0;
  284.      XClearWindow(dsp, w);
  285.      Leave_void
  286. }
  287. #endif LOCKSCR
  288.  
  289.