home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xset / xset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-22  |  25.0 KB  |  944 lines

  1. /* 
  2.  * $XConsortium: xset.c,v 1.59 91/07/22 18:32:33 keith Exp $ 
  3.  */
  4.  
  5. /* Copyright    Massachusetts Institute of Technology    1985    */
  6.  
  7. /*
  8. Permission to use, copy, modify, distribute, and sell this software and its
  9. documentation for any purpose is hereby granted without fee, provided that
  10. the above copyright notice appear in all copies and that both that
  11. copyright notice and this permission notice appear in supporting
  12. documentation, and that the name of M.I.T. not be used in advertising or
  13. publicity pertaining to distribution of the software without specific,
  14. written prior permission.  M.I.T. makes no representations about the
  15. suitability of this software for any purpose.  It is provided "as is"
  16. without express or implied warranty.
  17. */
  18.  
  19. /* $XConsortium: xset.c,v 1.59 91/07/22 18:32:33 keith Exp $ */
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23.  
  24. #include <X11/Xos.h>
  25. #include <X11/Xfuncs.h>
  26. #include <X11/Xlib.h>
  27. /*  #include <X11/Xlibwm.h>  [Doesn't exist yet  5-14-87]  %*/
  28. #include <X11/keysym.h>
  29. #include <X11/Xproto.h>
  30. #include <X11/Xutil.h>
  31. #include <X11/Xmu/Error.h>
  32. #ifdef MITMISC
  33. #include <X11/extensions/MITMisc.h>
  34. #endif
  35.  
  36. #define ON 1
  37. #define OFF 0
  38.  
  39. #define SERVER_DEFAULT (-1)
  40. #define DONT_CHANGE -2
  41.  
  42. #define ALL -1
  43. #define TIMEOUT 1
  44. #define INTERVAL 2
  45. #define PREFER_BLANK 3
  46. #define ALLOW_EXP 4
  47.  
  48. #define    nextarg(i, argv) \
  49.     argv[i]; \
  50.     if (i >= argc) \
  51.         break; \
  52.  
  53. char *progName;
  54.  
  55. int local_xerror();
  56.  
  57. main(argc, argv)
  58. int argc;
  59. char **argv;
  60. {
  61. register char *arg;
  62. register int i;
  63. int percent;
  64. int acc_num, acc_denom, threshold;
  65. int key, auto_repeat_mode;
  66. XKeyboardControl values;
  67. #define MAX_PIXEL_COUNT 512
  68. unsigned long pixels[MAX_PIXEL_COUNT];
  69. caddr_t colors[MAX_PIXEL_COUNT];
  70. int numpixels = 0;
  71. char *disp = NULL;
  72. Display *dpy;
  73. Bool hasargs = False;
  74.  
  75. progName = argv[0];
  76. for (i = 1; i < argc; i++) {
  77.   arg = argv[i];
  78.   if (strcmp (arg, "-display") == 0 || strcmp (arg, "-d") == 0) {
  79.     if (++i >= argc) usage ("missing argument to -display", NULL);
  80.     disp = argv[i];
  81.   } else {
  82.     hasargs = True;
  83.   }
  84. }
  85. if (!hasargs) {
  86.     usage (NULL, NULL);            /* replace with window interface */
  87. }
  88.  
  89. dpy = XOpenDisplay(disp);  /*  Open display and check for success */
  90. if (dpy == NULL) {
  91.   fprintf(stderr, "%s:  unable to open display \"%s\"\n",
  92.       argv[0], XDisplayName (disp));
  93.   exit(1);
  94. }
  95. XSetErrorHandler (local_xerror);
  96. for (i = 1; i < argc; ) {
  97.   arg = argv[i++];
  98.   if (strcmp (arg, "-display") == 0 || strcmp (arg, "-d") == 0) {
  99.     ++i;                    /* already dealt with */
  100.     continue;
  101.   } else if (*arg == '-' && *(arg + 1) == 'c'){ /* Does arg start with "-c"? */
  102.     set_click(dpy, 0);           /* If so, turn click off and  */
  103.   } 
  104.   else if (*arg == 'c') {         /* Well, does it start with "c", then? */
  105.     percent = SERVER_DEFAULT;        /* Default click volume. */
  106.     if (i >= argc) {
  107.     set_click (dpy, percent);    /* set click to default */
  108.     break;
  109.     }
  110.     arg = nextarg(i, argv);
  111.     if (strcmp(arg, "on") == 0) {               /* Let click be default. */
  112.       i++;
  113.     } 
  114.     else if (strcmp(arg, "off") == 0) {  
  115.       percent = 0;       /* Turn it off.          */
  116.       i++;
  117.     } 
  118.     else if (isnumber(arg, 100)) {
  119.       percent = atoi(arg);  /* Set to spec. volume */
  120.       i++;
  121.     }
  122.     set_click(dpy, percent);
  123.   } 
  124.   else if (strcmp(arg, "-b") == 0) {
  125.     set_bell_vol(dpy, 0);           /* Then turn off bell.    */
  126.   } 
  127.   else if (strcmp(arg, "b") == 0) {
  128.     percent = SERVER_DEFAULT;        /* Set bell to default. */
  129.     if (i >= argc) {
  130.     set_bell_vol (dpy, percent);    /* set bell to default */
  131.     break;
  132.     }
  133.     arg = nextarg(i, argv);
  134.     if (strcmp(arg, "on") == 0) {               /* Let it stay that way.  */
  135.       set_bell_vol(dpy, percent);
  136.       i++;
  137.     } 
  138.     else if (strcmp(arg, "off") == 0) {
  139.       percent = 0;            /* Turn the bell off.     */
  140.       set_bell_vol(dpy, percent);
  141.       i++;
  142.     } 
  143.     else if (isnumber(arg, 100)) {              /* If volume is given:    */
  144.       percent = atoi(arg);    /* set bell appropriately.*/
  145.       set_bell_vol(dpy, percent);
  146.       i++;
  147.       arg = nextarg(i, argv);
  148.  
  149.       if (isnumber(arg, 20000)) {               /* If pitch is given:     */
  150.     set_bell_pitch(dpy, atoi(arg));    /* set the bell.           */
  151.     i++;
  152.  
  153.     arg = nextarg(i, argv);
  154.     if (isnumber(arg, 1000)) {              /* If duration is given:  */
  155.       set_bell_dur(dpy, atoi(arg));  /*  set the bell.      */
  156.       i++;
  157.     }
  158.       }
  159.     }
  160.     else
  161.       set_bell_vol (dpy, percent);        /* set bell to default */
  162.   }
  163. #ifdef MITMISC
  164.   else if (strcmp(arg, "bc") == 0) {
  165.       int dummy;
  166.       if (XMITMiscQueryExtension(dpy, &dummy, &dummy))
  167.       XMITMiscSetBugMode(dpy, True);
  168.       else
  169.       fprintf(stderr, "server does not have extension for bc option\n");
  170.   }
  171.   else if (strcmp(arg, "-bc") == 0) {
  172.       int dummy;
  173.       if (XMITMiscQueryExtension(dpy, &dummy, &dummy))
  174.       XMITMiscSetBugMode(dpy, False);
  175.       else
  176.       fprintf(stderr, "server does not have extension for -bc option\n");
  177.   }
  178. #endif
  179.   else if (strcmp(arg, "fp") == 0) {           /* set font path */
  180.     if (i >= argc) {
  181.     arg = "default";
  182.     } else {
  183.     arg = nextarg(i, argv);
  184.     }
  185.     set_font_path(dpy, arg, 1, 0, 0);    /* special argument */
  186.     i++;
  187.   }
  188.   else if (strcmp(arg, "fp=") == 0) {    /* unconditionally set */
  189.     if (i >= argc) {
  190.     usage ("missing fp= argument", NULL);
  191.     } else {
  192.     arg = nextarg(i, argv);
  193.     }
  194.     set_font_path(dpy, arg, 0, 0, 0);    /* not special, set */
  195.     i++;
  196.   }
  197.   else if (strcmp(arg, "+fp") == 0) {           /* set font path */
  198.     if (i >= argc) usage ("missing +fp argument", NULL);
  199.     arg = nextarg(i, argv);
  200.     set_font_path(dpy, arg, 0, 1, 0);    /* not special, prepend */
  201.     i++;
  202.   }
  203.   else if (strcmp(arg, "fp+") == 0) {           /* set font path */
  204.     if (i >= argc) usage ("missing fp+ argument", NULL);
  205.     arg = nextarg(i, argv);
  206.     set_font_path(dpy, arg, 0, 0, 1);    /* not special, append */
  207.     i++;
  208.   }
  209.   else if (strcmp(arg, "-fp") == 0) {           /* set font path */
  210.     if (i >= argc) usage ("missing -fp argument", NULL);
  211.     arg = nextarg(i, argv);
  212.     set_font_path(dpy, arg, 0, -1, 0);    /* not special, preremove */
  213.     i++;
  214.   }
  215.   else if (strcmp(arg, "fp-") == 0) {           /* set font path */
  216.     if (i >= argc) usage ("missing fp- argument", NULL);
  217.     arg = nextarg(i, argv);
  218.     set_font_path(dpy, arg, 0, 0, -1);    /* not special, postremove */
  219.     i++;
  220.   }
  221.   else if (strcmp(arg, "-led") == 0) {         /* Turn off one or all LEDs */
  222.     values.led_mode = OFF;
  223.     values.led = ALL;        /* None specified */
  224.     arg = nextarg(i, argv);
  225.     if (isnumber(arg, 32) && atoi(arg) > 0) {
  226.       values.led = atoi(arg);
  227.       i++;
  228.     }
  229.     set_led(dpy, values.led, values.led_mode);
  230.   } 
  231.   else if (strcmp(arg, "led") == 0) {         /* Turn on one or all LEDs  */
  232.     values.led_mode = ON;
  233.     values.led = ALL;
  234.     if (i >= argc) {
  235.     set_led (dpy, values.led, values.led_mode);    /* set led to def */
  236.     break;
  237.     }
  238.     arg = nextarg(i, argv);
  239.     if (strcmp(arg, "on") == 0) {
  240.       i++;
  241.     } 
  242.     else if (strcmp(arg, "off") == 0) {       /*  ...except in this case. */
  243.        values.led_mode = OFF;
  244.       i++;
  245.     }
  246.     else if (isnumber(arg, 32) && atoi(arg) > 0) {
  247.       values.led = atoi(arg);
  248.       i++;
  249.     }
  250.     set_led(dpy, values.led, values.led_mode);
  251.   }
  252. /*  Set pointer (mouse) settings:  Acceleration and Threshold. */
  253.   else if (strcmp(arg, "m") == 0 || strcmp(arg, "mouse") == 0) {
  254.     acc_num = SERVER_DEFAULT;        /* restore server defaults */
  255.     acc_denom = SERVER_DEFAULT;
  256.     threshold = SERVER_DEFAULT;
  257.     if (i >= argc){
  258.       set_mouse(dpy, acc_num, acc_denom, threshold);
  259.       break;
  260.     }
  261.     arg = argv[i];
  262.     if (strcmp(arg, "default") == 0) {
  263.       i++;
  264.     } 
  265.     else if (*arg >= '0' && *arg <= '9') {
  266.       acc_denom = 1;
  267.       sscanf(arg, "%d/%d", &acc_num, &acc_denom);
  268.       i++;
  269.       if (i >= argc) {
  270.     set_mouse(dpy, acc_num, acc_denom, threshold);
  271.     break;
  272.       }
  273.       arg = argv[i];
  274.       if (*arg >= '0' && *arg <= '9') {
  275.     threshold = atoi(arg);  /* Set threshold as user specified.  */
  276.     i++;
  277.       }
  278.     }
  279.     set_mouse(dpy, acc_num, acc_denom, threshold);
  280.   } 
  281.   else if (strcmp(arg, "s") == 0) {
  282.     if (i >= argc) {
  283.       set_saver(dpy, ALL, 0);  /* Set everything to default  */
  284.       break;
  285.     }
  286.     arg = argv[i];
  287.     if (strcmp(arg, "blank") == 0) {       /* Alter blanking preference. */
  288.       set_saver(dpy, PREFER_BLANK, PreferBlanking);
  289.       i++;
  290.     }
  291.     else if (strcmp(arg, "noblank") == 0) {     /*  Ditto.  */
  292.       set_saver(dpy, PREFER_BLANK, DontPreferBlanking);
  293.       i++;
  294.     }
  295.     else if (strcmp(arg, "expose") == 0) {       /* Alter exposure preference. */
  296.       set_saver(dpy, ALLOW_EXP, AllowExposures);
  297.       i++;
  298.     }
  299.     else if (strcmp(arg, "noexpose") == 0) {     /*  Ditto.  */
  300.       set_saver(dpy, ALLOW_EXP, DontAllowExposures);
  301.       i++;
  302.     }
  303.     else if (strcmp(arg, "off") == 0) {
  304.       set_saver(dpy, TIMEOUT, 0);   /*  Turn off screen saver.  */
  305.       i++;
  306.       if (i >= argc)
  307.     break;
  308.       arg = argv[i];
  309.       if (strcmp(arg, "off") == 0) {
  310.     set_saver(dpy, INTERVAL, 0);
  311.     i++;
  312.       }
  313.     }
  314.     else if (strcmp(arg, "default") == 0 ||
  315.          strcmp(arg, "on") == 0) {    /*  Leave as default.       */
  316.       set_saver(dpy, ALL, 0);
  317.       i++;
  318.     } 
  319.     else if (*arg >= '0' && *arg <= '9') {  /*  Set as user wishes.   */
  320.       set_saver(dpy, TIMEOUT, atoi(arg));
  321.       i++;
  322.       if (i >= argc)
  323.     break;
  324.       arg = argv[i];
  325.       if (*arg >= '0' && *arg <= '9') {
  326.     set_saver(dpy, INTERVAL, atoi(arg));
  327.     i++;
  328.       }
  329.     }
  330.   } 
  331.   else if (strcmp(arg, "-r") == 0) {        /* Turn off one or all autorepeats */
  332.     auto_repeat_mode = OFF;
  333.     key = ALL;          /* None specified */
  334.     arg = argv[i];
  335.     if (i < argc)
  336.     if (isnumber(arg, 255)) {
  337.       key = atoi(arg);
  338.       i++;
  339.   } 
  340.     set_repeat(dpy, key, auto_repeat_mode);
  341.     }
  342.   else if (strcmp(arg, "r") == 0) {         /* Turn on one or all autorepeats */
  343.     auto_repeat_mode = ON;
  344.     key = ALL;          /* None specified */
  345.     arg = argv[i];
  346.     if (i < argc)
  347.     if (strcmp(arg, "on") == 0) {
  348.       i++;
  349.     } 
  350.     else if (strcmp(arg, "off") == 0) {       /*  ...except in this case. */
  351.       auto_repeat_mode = OFF;
  352.       i++;
  353.     }
  354.     else if (isnumber(arg, 255)) {
  355.       key = atoi(arg);
  356.       i++;
  357.   } 
  358.     set_repeat(dpy, key, auto_repeat_mode);
  359.   } 
  360.   else if (strcmp(arg, "p") == 0) {
  361.     if (i + 1 >= argc)
  362.       usage ("missing argument to p", NULL);
  363.     arg = argv[i];
  364.     if (numpixels >= MAX_PIXEL_COUNT)
  365.     usage ("more than %d pixels specified", MAX_PIXEL_COUNT);
  366.     if (*arg >= '0' && *arg <= '9')
  367.       pixels[numpixels] = atoi(arg);
  368.     else
  369.       usage ("invalid pixel number %s", arg);
  370.     i++;
  371.     colors[numpixels] = argv[i];
  372.     i++;
  373.     numpixels++;
  374.   }
  375.   else if (strcmp(arg, "-k") == 0) {
  376.     set_lock(dpy, OFF);
  377.   }
  378.   else if (strcmp(arg, "k") == 0) {
  379.     set_lock(dpy, ON);
  380.   }
  381.   else if (strcmp(arg, "q") == 0 || strcmp(arg, "-q") == 0) {
  382.     query(dpy);
  383.   }
  384.   else
  385.     usage ("unknown option %s", arg);
  386. }
  387.  
  388. if (numpixels)
  389.     set_pixels(dpy, pixels, colors, numpixels);
  390.  
  391. XCloseDisplay (dpy);
  392.  
  393. exit(0);    /*  Done.  We can go home now.  */
  394. }
  395.  
  396.  
  397. isnumber(arg, maximum)
  398.     char *arg;
  399.     int maximum;
  400. {
  401.     register char *p;
  402.  
  403.     if (arg[0] == '-' && arg[1] == '1' && arg[2] == '\0')
  404.         return(1);
  405.     for (p=arg; isdigit(*p); p++);
  406.     if (*p || atoi(arg) > maximum)
  407.         return(0); 
  408.     return(1);
  409. }
  410.  
  411. /*  These next few functions do the real work (xsetting things).
  412.  */
  413. set_click(dpy, percent)
  414. Display *dpy;
  415. int percent;
  416. {
  417. XKeyboardControl values;
  418. values.key_click_percent = percent;
  419. XChangeKeyboardControl(dpy, KBKeyClickPercent, &values);
  420. return;
  421. }
  422.  
  423. set_bell_vol(dpy, percent)
  424. Display *dpy;
  425. int percent;
  426. {
  427. XKeyboardControl values;
  428. values.bell_percent = percent;
  429. XChangeKeyboardControl(dpy, KBBellPercent, &values);
  430. return;
  431. }
  432.  
  433. set_bell_pitch(dpy, pitch)
  434. Display *dpy;
  435. int pitch;
  436. {
  437. XKeyboardControl values;
  438. values.bell_pitch = pitch;
  439. XChangeKeyboardControl(dpy, KBBellPitch, &values);
  440. return;
  441. }
  442.  
  443. set_bell_dur(dpy, duration)
  444. Display *dpy;
  445. int duration;
  446. {
  447. XKeyboardControl values;
  448. values.bell_duration = duration;
  449. XChangeKeyboardControl(dpy, KBBellDuration, &values);
  450. return;
  451. }
  452.  
  453. /*
  454.  * Set, add, or subtract the path according to before and after flags:
  455.  *
  456.  *    before    after    action
  457.  *
  458.  *       0      0    FontPath := path
  459.  *      -1      0    FontPath := current - path
  460.  *       0     -1    FontPath := current - path
  461.  *       1      0    FontPath := path + current
  462.  *       0      1    FontPath := current + path
  463.  */
  464.  
  465. set_font_path(dpy, path, special, before, after)
  466.     Display *dpy;
  467.     char *path;
  468.     int special, before, after;
  469. {
  470.     char **directoryList = NULL; int ndirs = 0;
  471.     char **currentList = NULL; int ncurrent = 0;
  472.  
  473.     if (special) {
  474.     if (strcmp (path, "default") == 0) {
  475.         XSetFontPath (dpy, NULL, 0);
  476.         return;
  477.     }
  478.     if (strcmp (path, "rehash") == 0) {
  479.         currentList = XGetFontPath (dpy, &ncurrent);
  480.         if (!currentList) {
  481.         fprintf (stderr, "%s:  unable to get current font path.\n",
  482.              progName);
  483.         return;
  484.         }
  485.         XSetFontPath (dpy, currentList, ncurrent);
  486.         XFreeFontPath (currentList);
  487.         return;
  488.     } 
  489.     /*
  490.      * for now, fall though and process keyword and directory list for
  491.      * compatibility with previous versions.
  492.      */
  493.     }
  494.  
  495.     /*
  496.      * parse the path list.  If before or after is non-zero, we'll need 
  497.      * the current value.
  498.      */
  499.  
  500.     if (before != 0 || after != 0) {
  501.     currentList = XGetFontPath (dpy, &ncurrent);
  502.     if (!currentList) {
  503.         fprintf (stderr, "%s:  unable to get old font path.\n",
  504.              progName);
  505.         before = after = 0;
  506.     }
  507.     }
  508.  
  509.     {
  510.     /* count the number of directories in path */
  511.     register char *cp = path;
  512.  
  513.     ndirs = 1;
  514.     while ((cp = index (cp, ',')) != NULL) {
  515.         ndirs++;
  516.         cp++;
  517.     }
  518.     }
  519.  
  520.     directoryList = (char **) malloc (ndirs*sizeof (char *));
  521.     if (!directoryList) error ("out of memory for font path directory list");
  522.  
  523.     {
  524.     /* mung the path and set directoryList pointers */
  525.     int i = 0;
  526.     char *cp = path;
  527.  
  528.     directoryList[i++] = cp;
  529.     while ((cp = index (cp, ',')) != NULL) {
  530.         directoryList[i++] = cp + 1;
  531.         *cp++ = '\0';
  532.     }
  533.     if (i != ndirs) {
  534.         fprintf (stderr, 
  535.              "%s: internal error, only parsed %d of %d directories.\n",
  536.              progName, i, ndirs);
  537.         exit (1);
  538.     }
  539.     }
  540.         
  541.     /*
  542.      * now we have have parsed the input path, so we can set it
  543.      */
  544.  
  545.     if (before == 0 && after == 0) {
  546.     XSetFontPath (dpy, directoryList, ndirs);
  547.     }
  548.  
  549.     /* if adding to list, build a superset */
  550.     if (before > 0 || after > 0) {
  551.     int nnew = ndirs + ncurrent;
  552.     char **newList = (char **) malloc (nnew * sizeof (char *));
  553.     
  554.     if (!newList) error ("out of memory");
  555.     if (before > 0) {        /* new + current */
  556.         bcopy ((char *) directoryList, (char *) newList, 
  557.            (unsigned) (ndirs*sizeof (char *)));
  558.         bcopy ((char *) currentList, (char *) (newList + ndirs),
  559.             (unsigned) (ncurrent*sizeof (char *)));
  560.         XSetFontPath (dpy, newList, nnew);
  561.     } else if (after > 0) {
  562.         bcopy ((char *) currentList, (char *) newList,
  563.            (unsigned) (ncurrent*sizeof (char *)));
  564.         bcopy ((char *) directoryList, (char *) (newList + ncurrent),
  565.            (unsigned) (ndirs*sizeof (char *)));
  566.         XSetFontPath (dpy, newList, nnew);
  567.     } 
  568.     free ((char *) newList);
  569.     }
  570.  
  571.     /* if deleting from list, build one the same size */
  572.     if (before < 0 || after < 0) {
  573.     int i, j;
  574.     int nnew = 0;
  575.     char **newList = (char **) malloc (ncurrent * sizeof (char *));
  576.     
  577.     if (!newList) error ("out of memory");
  578.     for (i = 0; i < ncurrent; i++) {
  579.         for (j = 0; j < ndirs; j++) {
  580.         if (strcmp (currentList[i], directoryList[j]) == 0)
  581.           break;
  582.         }
  583.         /* if we ran out, then insert into new list */
  584.         if (j == ndirs) newList[nnew++] = currentList[i];
  585.     }
  586.     if (nnew == ncurrent) {
  587.         fprintf (stderr,
  588.              "%s:  warning, no entries deleted from font path.\n",
  589.              progName);
  590.     }
  591.     XSetFontPath (dpy, newList, nnew);
  592.     free ((char *) newList);
  593.     }
  594.  
  595.     if (directoryList) free ((char *) directoryList);
  596.     if (currentList) XFreeFontPath (currentList);
  597.  
  598.     return;
  599. }
  600.  
  601.  
  602. set_led(dpy, led, led_mode)
  603. Display *dpy;
  604. int led, led_mode;
  605. {
  606.   XKeyboardControl values;
  607.   values.led_mode = led_mode;
  608.   if (led != ALL) {
  609.     values.led = led;
  610.     XChangeKeyboardControl(dpy, KBLed | KBLedMode, &values);
  611.   }
  612.   else {
  613.     XChangeKeyboardControl(dpy, KBLedMode, &values);
  614.   }
  615.   return;
  616. }
  617.  
  618. set_mouse(dpy, acc_num, acc_denom, threshold)
  619. Display *dpy;
  620. int acc_num, acc_denom, threshold;
  621. {
  622. int do_accel = True, do_threshold = True;
  623.  
  624. if (acc_num == DONT_CHANGE)        /* what an incredible crock... */
  625.   do_accel = False;
  626. if (threshold == DONT_CHANGE)
  627.   do_threshold = False;
  628. if (acc_num < 0)             /* shouldn't happen */
  629.   acc_num = SERVER_DEFAULT;
  630. if (acc_denom <= 0)            /* prevent divide by zero */
  631.   acc_denom = SERVER_DEFAULT;
  632. if (threshold < 0) threshold = SERVER_DEFAULT;
  633. XChangePointerControl(dpy, do_accel, do_threshold, acc_num,
  634.               acc_denom, threshold);
  635. return;
  636. }
  637.  
  638. set_saver(dpy, mask, value)
  639. Display *dpy;
  640. int mask, value;
  641. {
  642.   int timeout, interval, prefer_blank, allow_exp;
  643.   XGetScreenSaver(dpy, &timeout, &interval, &prefer_blank, 
  644.           &allow_exp);
  645.   if (mask == TIMEOUT) timeout = value;
  646.   if (mask == INTERVAL) interval = value;
  647.   if (mask == PREFER_BLANK) prefer_blank = value;
  648.   if (mask == ALLOW_EXP) allow_exp = value;
  649.   if (mask == ALL) {  /* "value" is ignored in this case.  (defaults) */
  650.     timeout = SERVER_DEFAULT;
  651.     interval = SERVER_DEFAULT;
  652.     prefer_blank = DefaultBlanking;
  653.     allow_exp = DefaultExposures;
  654.   }
  655.       XSetScreenSaver(dpy, timeout, interval, prefer_blank, 
  656.               allow_exp);
  657.   return;
  658. }
  659.  
  660. set_repeat(dpy, key, auto_repeat_mode)
  661. Display *dpy;
  662. int key, auto_repeat_mode;
  663. {
  664.   XKeyboardControl values;
  665.   values.auto_repeat_mode = auto_repeat_mode;
  666.   if (key != ALL) {
  667.     values.key = key;
  668.     XChangeKeyboardControl(dpy, KBKey | KBAutoRepeatMode, &values);
  669.   }
  670.   else {
  671.     XChangeKeyboardControl(dpy, KBAutoRepeatMode, &values);
  672.   }
  673.   return;
  674. }
  675.  
  676. set_pixels(dpy, pixels, colors, numpixels)
  677. Display *dpy;
  678. unsigned long *pixels;
  679. caddr_t *colors;
  680. int numpixels;
  681. {
  682.   XColor def;
  683.   int scr = DefaultScreen (dpy);
  684.   Visual *visual = DefaultVisual (dpy, scr);
  685.   Colormap cmap = DefaultColormap (dpy, scr);
  686.   unsigned long max_cells = DisplayCells(dpy, scr);
  687.   XVisualInfo viproto, *vip;
  688.   int nvisuals = 0;
  689.   char *visual_type = NULL;
  690.   int i;
  691.  
  692.   viproto.visualid = XVisualIDFromVisual (visual);
  693.   vip = XGetVisualInfo (dpy, VisualIDMask, &viproto, &nvisuals);
  694.   if (!vip) {
  695.       fprintf (stderr, "%s: Can't get visual for visualID 0x%x\n",
  696.            progName, viproto.visualid);
  697.       return;
  698.   }
  699.  
  700.   switch (vip->class) {
  701.       case GrayScale:
  702.       case PseudoColor:
  703.     break;
  704.       case TrueColor:
  705.     visual_type = "TrueColor";
  706.     /* fall through */
  707.       case DirectColor:
  708.     max_cells *= max_cells * max_cells;
  709.     break;
  710.       case StaticGray:
  711.     visual_type = "StaticGray";
  712.     break;
  713.       case StaticColor:
  714.     visual_type = "StaticColor";
  715.     break;
  716.       default:
  717.     fprintf (stderr, "%s:  unknown visual class type %d\n",
  718.          progName, vip->class);
  719.     numpixels = 0;
  720.   }
  721.  
  722.   if (visual_type) {
  723.     fprintf (stderr, "%s:  cannot set pixel values in read-only %s visuals\n",
  724.          progName, visual_type);
  725.   } else {
  726.     for (i = 0; i < numpixels; i++) {
  727.       def.pixel = pixels[i];
  728.       if (def.pixel >= max_cells)
  729.     fprintf(stderr, 
  730.         "%s:  pixel value %d out of colormap range 0 through %d\n",
  731.         progName, def.pixel, max_cells - 1);
  732.       else {
  733.         if (XParseColor (dpy, cmap, colors[i], &def))
  734.       XStoreColor(dpy, cmap, &def);
  735.         else
  736.       fprintf (stderr, "%s:  invalid color \"%s\"\n", progName, colors[i]);
  737.       }
  738.     }
  739.   }
  740.  
  741.   XFree ((char *) vip);
  742.  
  743.   return;
  744. }
  745.  
  746. set_lock(dpy, onoff)
  747. Display *dpy;
  748. Bool onoff;
  749. {
  750.   XModifierKeymap *mods;
  751.   mods = XGetModifierMapping(dpy);
  752.  
  753.   if (onoff)
  754.     mods = XInsertModifiermapEntry(mods, XK_Caps_Lock, LockMapIndex);
  755.   else
  756.     mods = XDeleteModifiermapEntry(mods, XK_Caps_Lock, LockMapIndex);
  757.   XSetModifierMapping(dpy, mods);
  758.   return;
  759. }
  760.  
  761. char *on_or_off (val, onval, onstr, offval, offstr, buf)
  762.     int val, onval, offval;
  763.     char *onstr, *offstr;
  764.     char buf[];
  765. {
  766.     if (val == onval) 
  767.       return onstr;
  768.     else if (val == offval)
  769.       return offstr;
  770.  
  771.     buf[0] = '\0';
  772.     sprintf (buf, "<%d>", val);
  773.     return buf;
  774. }
  775.  
  776.  
  777. /*  This is the information-getting function for telling the user what the
  778.  *  current "xsettings" are.
  779.  */
  780. query(dpy)
  781. Display *dpy;
  782. {
  783. int scr = DefaultScreen (dpy);
  784. XKeyboardState values;
  785. int acc_num, acc_denom, threshold;
  786. int timeout, interval, prefer_blank, allow_exp;
  787. char **font_path; int npaths;
  788. int i, j;
  789. char buf[20];                /* big enough for 16 bit number */
  790.  
  791. XGetKeyboardControl(dpy, &values);
  792. XGetPointerControl(dpy, &acc_num, &acc_denom, &threshold);
  793. XGetScreenSaver(dpy, &timeout, &interval, &prefer_blank, &allow_exp);
  794. font_path = XGetFontPath(dpy, &npaths);
  795.  
  796. printf ("Keyboard Control:\n");
  797. printf ("  auto repeat:  %s    key click percent:  %d    LED mask:  %08lx\n", 
  798.     on_or_off (values.global_auto_repeat,
  799.            AutoRepeatModeOn, "on", AutoRepeatModeOff, "off", buf),
  800.     values.key_click_percent, values.led_mask);
  801. printf ("  auto repeating keys:  ");
  802. for (i = 0; i < 4; i++) {
  803.     if (i) printf ("                        ");
  804.     for (j = 0; j < 8; j++) {
  805.     printf ("%02x", (unsigned char)values.auto_repeats[i*8 + j]);
  806.     }
  807.     printf ("\n");
  808. }
  809. printf ("  bell percent:  %d    bell pitch:  %d    bell duration:  %d\n",
  810.     values.bell_percent, values.bell_pitch, values.bell_duration);
  811.  
  812. printf ("Pointer Control:\n");
  813. printf ("  acceleration:  %d/%d    threshold:  %d\n",
  814.     acc_num, acc_denom, threshold);
  815.  
  816. printf ("Screen Saver:\n");
  817. printf ("  prefer blanking:  %s    ",
  818.     on_or_off (prefer_blank, PreferBlanking, "yes",
  819.            DontPreferBlanking, "no", buf));
  820. printf ("allow exposures:  %s\n",
  821.     on_or_off (allow_exp, AllowExposures, "yes",
  822.            DontAllowExposures, "no", buf));
  823. printf ("  timeout:  %d    cycle:  %d\n", timeout, interval);
  824.  
  825. printf ("Colors:\n");
  826. printf ("  default colormap:  0x%lx    BlackPixel:  %d    WhitePixel:  %d\n",
  827.     DefaultColormap (dpy, scr), 
  828.     BlackPixel (dpy, scr), WhitePixel (dpy, scr));
  829.  
  830. printf ("Font Path:\n");
  831. if (npaths) {
  832.     printf( "  %s", *font_path++ );
  833.     for( --npaths; npaths; npaths-- )
  834.         printf( ",%s", *font_path++ );
  835.     printf( "\n" );
  836. } else {
  837.     printf ("  (empty)\n");
  838. }
  839.  
  840. #ifdef MITMISC
  841. {
  842.     int dummy;
  843.     if (XMITMiscQueryExtension(dpy, &dummy, &dummy)) {
  844.     if (XMITMiscGetBugMode(dpy))
  845.         printf ("Bug Mode: compatibility mode is enabled\n");
  846.     else
  847.         printf ("Bug Mode: compatibility mode is disabled\n");
  848.     }
  849. }
  850. #endif
  851.  
  852. return;
  853. }
  854.  
  855.  
  856. /*  This is the usage function */
  857.  
  858. usage (fmt, arg)
  859.     char *fmt;
  860.     char *arg;
  861. {
  862.     if (fmt) {
  863.     fprintf (stderr, "%s:  ", progName);
  864.     fprintf (stderr, fmt, arg);
  865.     fprintf (stderr, "\n\n");
  866.     }
  867.  
  868.     fprintf (stderr, "usage:  %s [-display host:dpy] option ...\n", progName);
  869.     fprintf (stderr, "    To turn bell off:\n");
  870.     fprintf (stderr, "\t-b                b off               b 0\n");
  871.     fprintf (stderr, "    To set bell volume, pitch and duration:\n");
  872.     fprintf (stderr, "\t b [vol [pitch [dur]]]          b on\n");
  873. #ifdef MITMISC
  874.     fprintf (stderr, "    To disable bug compatibility mode:\n");
  875.     fprintf (stderr, "\t-bc\n");
  876.     fprintf (stderr, "    To enable bug compatibility mode:\n");
  877.     fprintf (stderr, "\tbc\n");
  878. #endif
  879.     fprintf (stderr, "    To turn keyclick off:\n");
  880.     fprintf (stderr, "\t-c                c off               c 0\n");
  881.     fprintf (stderr, "    To set keyclick volume:\n");
  882.     fprintf (stderr, "\t c [0-100]        c on\n");
  883.     fprintf (stderr, "    To set the font path:\n" );
  884.     fprintf (stderr, "\t fp= path[,path...]\n" );
  885.     fprintf (stderr, "    To restore the default font path:\n");
  886.     fprintf (stderr, "\t fp default\n");
  887.     fprintf (stderr, "    To have the server reread font databases:\n");
  888.     fprintf (stderr, "\t fp rehash\n");
  889.     fprintf (stderr, "    To remove elements from font path:\n");
  890.     fprintf (stderr, "\t-fp path[,path...]  fp- path[,path...]\n");
  891.     fprintf (stderr, "    To prepend or append elements to font path:\n");
  892.     fprintf (stderr, "\t+fp path[,path...]  fp+ path[,path...]\n");
  893.     fprintf (stderr, "    To set LED states off or on:\n");
  894.     fprintf (stderr, "\t-led [1-32]         led off\n");
  895.     fprintf (stderr, "\t led [1-32]         led on\n");
  896.     fprintf (stderr, "    To set mouse acceleration and threshold:\n");
  897.     fprintf (stderr, "\t m [acc_mult[/acc_div] [thr]]    m default\n");
  898.     fprintf (stderr, "    To set pixel colors:\n");
  899.     fprintf (stderr, "\t p pixel_value color_name\n");
  900.     fprintf (stderr, "    To turn auto-repeat off or on:\n");
  901.     fprintf (stderr, "\t-r [keycode]        r off\n");
  902.     fprintf (stderr, "\t r [keycode]        r on\n");
  903.     fprintf (stderr, "    For screen-saver control:\n");
  904.     fprintf (stderr, "\t s [timeout [cycle]]  s default    s on\n");
  905.     fprintf (stderr, "\t s blank              s noblank    s off\n");
  906.     fprintf (stderr, "\t s expose             s noexpose\n");
  907.     fprintf (stderr, "    For status information:  q\n");
  908.     exit(0);
  909. }
  910.  
  911. error( message )
  912.     char *message;
  913. {
  914.     fprintf( stderr, "%s: %s\n", progName, message );
  915.     exit( 1 );
  916. }
  917.  
  918.  
  919. int local_xerror (dpy, rep)
  920.     Display *dpy;
  921.     XErrorEvent *rep;
  922. {
  923.     char *errname = NULL;
  924.  
  925.     if (rep->request_code == X_StoreColors) {
  926.     switch (rep->error_code) {
  927.       case BadAccess:
  928.         errname = "BadAccess, pixel not allocated read/write";
  929.         break;
  930.       case BadValue:
  931.         errname = "BadValue, invalid pixel number";
  932.         break;
  933.     }
  934.     }
  935.  
  936.     if (errname) {
  937.     fprintf (stderr, "%s:  error in storing color:  %s\n",
  938.          progName, errname);
  939.     } else {
  940.     XmuPrintDefaultErrorMessage (dpy, rep, stderr);
  941.     }
  942.     return (0);
  943. }
  944.