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

  1. /*
  2.  * xmodmap - program for loading keymap definitions into server
  3.  *
  4.  * $XConsortium: xmodmap.c,v 1.21 91/07/18 10:25:49 rws Exp $
  5.  *
  6.  * Copyright 1988 Massachusetts Institute of Technology
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that 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.  * Author:  Jim Fulton, MIT X Consortium
  19.  */
  20.  
  21. #include <X11/Xos.h>
  22. #include <X11/Xlib.h>
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include "xmodmap.h"
  26.  
  27. char *ProgramName;
  28. Display *dpy = NULL;
  29. int min_keycode, max_keycode;
  30. Bool verbose = False;
  31. Bool dontExecute = False;
  32.  
  33. void Exit (status)
  34.     int status;
  35. {
  36.     if (dpy) {
  37.     XCloseDisplay (dpy);
  38.     dpy = NULL;
  39.     }
  40.     exit (status);
  41. }
  42.  
  43.  
  44. static char *help_message[] = {
  45. "\nwhere options include:",
  46. "    -display host:dpy            X server to use",
  47. "    -verbose, -quiet             turn logging on or off",
  48. "    -n                           don't execute changes, just show like make",
  49. "    -e expression                execute string",
  50. "    -pm                          print modifier map",
  51. "    -pk                          print keymap table",
  52. "    -pke                         print keymap table as expressions",
  53. "    -pp                          print pointer map",
  54. "    -grammar                     print out short help on allowable input",
  55. "    -                            read standard input",
  56. "",
  57. NULL};
  58.  
  59.  
  60. void usage ()
  61. {
  62.     char **cpp;
  63.  
  64.     fprintf (stderr, "usage:  %s [-options ...] [filename]\n", ProgramName);
  65.     for (cpp = help_message; *cpp; cpp++) {
  66.     fprintf (stderr, "%s\n", *cpp);
  67.     }
  68.     Exit (1);
  69. }
  70.  
  71. static char *grammar_message[] = {
  72. "    pointer = default              reset pointer buttons to default",
  73. "    pointer = NUMBER ...           set pointer button codes",
  74. "    keycode NUMBER = [KEYSYM ...]  map keycode to given keysyms",
  75. "    keysym KEYSYM = [KEYSYM ...]   look up keysym and do a keycode operation",
  76. "    clear MODIFIER                 remove all keys for this modifier",
  77. "    add MODIFIER = KEYSYM ...      add the keysyms to the modifier",
  78. "    remove MODIFIER = KEYSYM ...   remove the keysyms from the modifier",
  79. "",
  80. "where NUMBER is a decimal, octal, or hex constant; KEYSYM is a valid",
  81. "Key Symbol name; and MODIFIER is one of the eight modifier names:  Shift,",
  82. "Lock, Control, Mod1, Mod2, Mod3, Mod4, or Mod5.  Lines beginning with",
  83. "an exclamation mark (!) are taken as comments.  Case is significant except",
  84. "for MODIFIER names.",
  85. "",
  86. "Keysyms on the left hand side of the = sign are looked up before any changes",
  87. "are made; keysyms on the right are looked up after all of those on the left",
  88. "have been resolved.  This makes it possible to swap modifier keys.",
  89. "",
  90. NULL };
  91.  
  92.  
  93. void grammar_usage ()
  94. {
  95.     char **cpp;
  96.  
  97.     fprintf (stderr, "%s accepts the following input expressions:\n\n",
  98.          ProgramName);
  99.     for (cpp = grammar_message; *cpp; cpp++) {
  100.     fprintf (stderr, "%s\n", *cpp);
  101.     }
  102.     Exit (0);
  103. }
  104.  
  105. int parse_errors = 0;
  106.  
  107. main (argc, argv)
  108.     int argc;
  109.     char **argv;
  110. {
  111.     int i;
  112.     char *displayname = NULL;
  113.     int status;
  114.     Bool printMap = False;
  115.     Bool printKeyTable = False;
  116.     Bool printKeyTableExprs = False;
  117.     Bool printPointerMap = False;
  118.     Bool didAnything = False;
  119.  
  120.     ProgramName = argv[0];
  121.  
  122.     /*
  123.      * scan the arg list once to find out which display to use
  124.      */
  125.  
  126.     for (i = 1; i < argc; i++) {
  127.     if (strncmp (argv[i], "-d", 2) == 0) {
  128.         if (++i >= argc) usage ();
  129.         displayname = argv[i];
  130.     }
  131.     }
  132.  
  133.     dpy = XOpenDisplay (displayname);
  134.     if (!dpy) {
  135.     fprintf (stderr, "%s:  unable to open display '%s'\n",
  136.          ProgramName, XDisplayName (displayname));
  137.     Exit (1);
  138.     }
  139.  
  140.     XDisplayKeycodes (dpy, &min_keycode, &max_keycode);
  141.  
  142.     initialize_map ();
  143.  
  144.     /*
  145.      * scan the arg list again to do the actual work (since it requires
  146.      * the display being open.
  147.      */
  148.  
  149.     status = 0;
  150.     for (i = 1; i < argc; i++) {
  151.     char *arg = argv[i];
  152.  
  153.     if (arg[0] == '-') {
  154.         switch (arg[1]) {
  155.           case 'd':            /* -display host:dpy */
  156.         ++i;            /* handled above */
  157.         continue;
  158.           case 'v':            /* -verbose */
  159.         verbose = True;
  160.         continue;
  161.           case 'q':            /* -quiet */
  162.         verbose = False;
  163.         continue;
  164.           case 'n':            /* -n (like make) */
  165.         dontExecute = True;
  166.         continue;
  167.           case 'e':            /* -e expression */
  168.         didAnything = True;
  169.         if (++i >= argc) usage ();
  170.         process_line (argv[i]);
  171.         continue;
  172.           case 'p':            /* -p... */
  173.         switch (arg[2]) {
  174.           case '\0':
  175.           case 'm':        /* -pm */
  176.             printMap = True;
  177.             break;
  178.           case 'k':        /* -pk, -pke */
  179.             switch (arg[3]) {
  180.             case '\0':
  181.             printKeyTable = True;
  182.             break;
  183.             case 'e':
  184.             printKeyTableExprs = True;
  185.             break;
  186.             default:
  187.             usage ();
  188.             }
  189.             break;
  190.           case 'p':        /* -pp */
  191.             printPointerMap = True;
  192.             break;
  193.           default:
  194.             usage ();
  195.             /* NOTREACHED */
  196.         }
  197.         didAnything = True;
  198.         continue;
  199.           case 'g':            /* -grammar */
  200.         grammar_usage ();
  201.         /*NOTREACHED*/
  202.           case '\0':        /* - (use standard input) */
  203.         didAnything = True;
  204.         process_file (NULL);
  205.         continue;
  206.  
  207.           /*
  208.            * provide old xmodmap args
  209.            */
  210.           case 'S':
  211.         didAnything = True;
  212.         process_line ("clear shift");
  213.         continue;
  214.           case 'L':
  215.         didAnything = True;
  216.         process_line ("clear lock");
  217.         continue;
  218.           case 'C':
  219.         didAnything = True;
  220.         process_line ("clear control");
  221.         continue;
  222.           case '1':
  223.           case '2':
  224.           case '3':
  225.           case '4':
  226.           case '5': {
  227.           char *cmd = "clear modX";
  228.           cmd[9] = arg[1];
  229.           process_line (cmd);
  230.           continue;
  231.           }
  232.           case 's':
  233.           case 'l':
  234.           case 'c': {
  235.           char cmd[80];        /* big enough to hold line */
  236.           didAnything = True;
  237.           if (++i >= argc) usage ();
  238.           (void) sprintf (cmd, "remove %s = %s",
  239.                   ((arg[1] == 's') ? "shift" :
  240.                    ((arg[1] == 'l') ? "lock" :
  241.                     "control")), argv[i]);
  242.           process_line (cmd);
  243.           continue;
  244.           }
  245.           default:
  246.         usage ();
  247.         /*NOTREACHED*/
  248.         }
  249.     } else if (arg[0] == '+') {    /* old xmodmap args */
  250.         switch (arg[1]) {
  251.           case '1':
  252.           case '2':
  253.           case '3':
  254.           case '4':
  255.           case '5': {
  256.           char cmd[80];        /* big enough to hold line */
  257.           didAnything = True;
  258.           if (++i >= argc) usage ();
  259.  
  260.           (void) sprintf (cmd, "add mod%c = %s", arg[1], argv[i]);
  261.           process_line (cmd);
  262.           continue;
  263.           }
  264.           case 'S':
  265.           case 'L':
  266.           case 'C':
  267.         arg[1] = tolower (arg[1]);
  268.         /* fall through to handler below */
  269.           case 's':
  270.           case 'l':
  271.           case 'c': {
  272.           char cmd[80];        /* big enough to hold line */
  273.           didAnything = True;
  274.           if (++i >= argc) usage ();
  275.           (void) sprintf (cmd, "add %s = %s",
  276.                   ((arg[1] == 's') ? "shift" :
  277.                    ((arg[1] == 'l') ? "lock" :
  278.                     "control")), argv[i]);
  279.           process_line (cmd);
  280.           continue;
  281.           }
  282.           default:
  283.         usage ();
  284.         }
  285.     } else {
  286.         didAnything = True;
  287.         process_file (arg);
  288.         continue;
  289.     }
  290.     }                    /* end for loop */
  291.  
  292.     /* for compatibility */
  293.     if (!didAnything) printMap = True;
  294.  
  295.     /*
  296.      * at this point, the work list has been built and we can view it or
  297.      * execute it
  298.      */
  299.  
  300.     if (dontExecute) {
  301.     print_work_queue ();
  302.     Exit (0);
  303.     }
  304.  
  305.     if (parse_errors != 0) {
  306.     fprintf (stderr, "%s:  %d errors encountered, aborting.\n",
  307.          ProgramName, parse_errors);
  308.     } else {
  309.     status = execute_work_queue ();
  310.     }
  311.  
  312.     if (printMap) {
  313.     print_modifier_map ();
  314.     }
  315.  
  316.     if (printKeyTable) {
  317.     print_key_table (False);
  318.     }
  319.  
  320.     if (printKeyTableExprs) {
  321.     print_key_table (True);
  322.     }
  323.  
  324.     if (printPointerMap) {
  325.     print_pointer_map ();
  326.     }
  327.  
  328.     Exit (status < 0 ? 1 : 0);
  329. }
  330.  
  331.