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

  1. /*
  2.  * xmodmap - program for loading keymap definitions into server
  3.  *
  4.  * $XConsortium: exec.c,v 1.12 91/07/18 10:25:57 rws Exp $
  5.  *
  6.  * Copyright 1988 Massachusetts Institute of Technology
  7.  * Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  8.  *
  9.  *                     All Rights Reserved
  10.  * 
  11.  * Permission  to  use,  copy,  modify,  and  distribute   this
  12.  * software  and  its documentation for any purpose and without
  13.  * fee is hereby granted, provided that the above copyright no-
  14.  * tice  appear  in all copies and that both that copyright no-
  15.  * tice and this permission notice appear in  supporting  docu-
  16.  * mentation,  and  that the names of Sun or MIT not be used in
  17.  * advertising or publicity pertaining to distribution  of  the
  18.  * software  without specific prior written permission. Sun and
  19.  * M.I.T. make no representations about the suitability of this
  20.  * software for any purpose. It is provided "as is" without any
  21.  * express or implied warranty.
  22.  * 
  23.  * SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  24.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  25.  * NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  26.  * ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  27.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  28.  * PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  29.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  30.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  31.  *
  32.  * Author:  Jim Fulton, MIT X Consortium; derived from parts of the
  33.  * original xmodmap, written by David Rosenthal, of Sun Microsystems.
  34.  */
  35.  
  36. #include <X11/Xos.h>
  37. #include <X11/Xlib.h>
  38. #include <stdio.h>
  39. #include "xmodmap.h"
  40. #include "wq.h"
  41.  
  42. static mapping_busy_key (timeout)
  43.     int timeout;
  44. {
  45.     int i;
  46.     unsigned char keymap[32];
  47.     static unsigned int masktable[8] = {
  48.     0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  49.  
  50.     XQueryKeymap (dpy, (char *) keymap);
  51.  
  52.     fprintf (stderr,
  53.          "%s:  please release the following keys within %d seconds:\n",
  54.          ProgramName, timeout);
  55.     for (i = 0; i < 256; i++) {
  56.     if (keymap[i >> 3] & masktable[i & 7]) {
  57.         KeySym ks = XKeycodeToKeysym (dpy, (KeyCode) i, 0);
  58.         char *cp = XKeysymToString (ks);
  59.         fprintf (stderr, "    %s (keysym 0x%x, keycode %d)\n",
  60.              cp ? cp : "UNNAMED", ks, i);
  61.     }
  62.     }
  63.     sleep (timeout);
  64.     return;
  65. }
  66.  
  67. static mapping_busy_pointer (timeout)
  68.     int timeout;
  69. {
  70.     int i;
  71.     Window root, child;            /* dummy variables */
  72.     int rx, ry, wx, wy;            /* dummy variables */
  73.     unsigned int mask;
  74.     static unsigned int masks[5] = {
  75.     Button1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask };
  76.  
  77.     if (!XQueryPointer (dpy, RootWindow(dpy,DefaultScreen(dpy)),
  78.             &root, &child, &rx, &ry, &wx, &wy, &mask))
  79.       mask = 0;
  80.  
  81.     fprintf (stderr,
  82.          "%s:  please release the following buttons within %d seconds:\n",
  83.          ProgramName, timeout);
  84.     for (i = 0; i < 5; i++) {
  85.     if (mask & masks[i]) 
  86.       fprintf (stderr, "    Button%d\n", i+1);
  87.     }
  88.     sleep (timeout);
  89.     return;
  90. }
  91.  
  92.  
  93. /*
  94.  * UpdateModifierMapping - this sends the modifier map to the server
  95.  * and deals with retransmissions due to the keyboard being busy.
  96.  */
  97.  
  98. int UpdateModifierMapping (map)
  99.     XModifierKeymap *map;
  100. {
  101.     int retries, timeout;
  102.  
  103.     for (retries = 5, timeout = 2; retries > 0; retries--, timeout *= 2) {
  104.     int result;
  105.  
  106.     result = XSetModifierMapping (dpy, map);
  107.     switch (result) {
  108.       case MappingSuccess:    /* Success */
  109.         return (0);
  110.       case MappingBusy:        /* Busy */
  111.         mapping_busy_key (timeout);
  112.         continue;
  113.       case MappingFailed:
  114.         fprintf (stderr, "%s: bad set modifier mapping.\n",
  115.              ProgramName);
  116.         return (-1);
  117.       default:
  118.         fprintf (stderr, "%s:  bad return %d from XSetModifierMapping\n",
  119.              ProgramName, result);
  120.         return (-1);
  121.     }
  122.     }
  123.     fprintf (stderr,
  124.          "%s:  unable to set modifier mapping, keyboard problem\n",
  125.          ProgramName);
  126.     return (-1);
  127. }
  128.  
  129.  
  130. /*
  131.  * AddModifier - this adds a keycode to the modifier list
  132.  */
  133.  
  134. int AddModifier (mapp, keycode, modifier)
  135.     XModifierKeymap **mapp;
  136.     KeyCode keycode;
  137.     int modifier;
  138. {
  139.     if (keycode) {
  140.     *mapp = XInsertModifiermapEntry (*mapp, keycode, modifier);
  141.     return (0);
  142.     } else {
  143.     return (-1);
  144.     }
  145.     /*NOTREACHED*/
  146. }
  147.  
  148.  
  149. /*
  150.  * DeleteModifier - this removes a keycode from the modifier list
  151.  */
  152.  
  153. int RemoveModifier (mapp, keycode, modifier)
  154.     XModifierKeymap **mapp;
  155.     KeyCode keycode;
  156.     int modifier;
  157. {
  158.     if (keycode) {
  159.     *mapp = XDeleteModifiermapEntry (*mapp, keycode, modifier);
  160.     return (0);
  161.     } else {
  162.     return (-1);
  163.     }
  164.     /*NOTREACHED*/
  165. }
  166.  
  167.  
  168. /*
  169.  * ClearModifier - this removes all entries from the modifier list
  170.  */
  171.  
  172. int ClearModifier (mapp, modifier)
  173.     XModifierKeymap **mapp;
  174.     int modifier;
  175. {
  176.     int i;
  177.     XModifierKeymap *map = *mapp;
  178.     KeyCode *kcp;
  179.  
  180.     kcp = &map->modifiermap[modifier * map->max_keypermod];
  181.     for (i = 0; i < map->max_keypermod; i++) {
  182.     *kcp++ = (KeyCode) 0;
  183.     }
  184.     return (0);
  185. }
  186.  
  187.  
  188. /*
  189.  * print the contents of the map
  190.  */
  191.  
  192. PrintModifierMapping (map, fp)
  193.     XModifierKeymap *map;
  194.     FILE *fp;
  195. {
  196.     int i, k = 0;
  197.  
  198.     fprintf (fp, 
  199.              "%s:  up to %d keys per modifier, (keycodes in parentheses):\n\n", 
  200.              ProgramName, map->max_keypermod);
  201.     for (i = 0; i < 8; i++) {
  202.     int j;
  203.  
  204.     fprintf(fp, "%-10s", modifier_table[i].name);
  205.     for (j = 0; j < map->max_keypermod; j++) {
  206.         if (map->modifiermap[k]) {
  207.         KeySym ks = XKeycodeToKeysym(dpy, map->modifiermap[k], 0);
  208.         char *nm = XKeysymToString(ks);
  209.  
  210.         fprintf (fp, "%s  %s (0x%0x)", (j > 0 ? "," : ""), 
  211.              (nm ? nm : "BadKey"), map->modifiermap[k]);
  212.         }
  213.         k++;
  214.     }
  215.     fprintf(fp, "\n");
  216.     }
  217.     fprintf (fp, "\n");
  218.     return;
  219. }
  220.  
  221.  
  222. PrintKeyTable (exprs, fp)
  223.     Bool exprs;
  224.     FILE *fp;
  225. {
  226.     int         i;
  227.     int min_keycode, max_keycode, keysyms_per_keycode;
  228.     KeySym *keymap, *origkeymap;
  229.  
  230.     XDisplayKeycodes (dpy, &min_keycode, &max_keycode);
  231.     origkeymap = XGetKeyboardMapping (dpy, min_keycode,
  232.                       (max_keycode - min_keycode + 1),
  233.                       &keysyms_per_keycode);
  234.  
  235.     if (!origkeymap) {
  236.     fprintf (stderr, "%s:  unable to get keyboard mapping table.\n",
  237.          ProgramName);
  238.     return;
  239.     }
  240.     if (!exprs) {
  241.     fprintf (fp, 
  242.          "There are %d KeySyms per KeyCode; KeyCodes range from %d to %d.\n\n", 
  243.          keysyms_per_keycode, min_keycode, max_keycode);
  244.     fprintf (fp, "    KeyCode\tKeysym (Keysym)\t...\n");
  245.     fprintf (fp, "    Value  \tValue   (Name) \t...\n\n");
  246.     }
  247.     keymap = origkeymap;
  248.     for (i = min_keycode; i <= max_keycode; i++) {
  249.     int  j, max;
  250.  
  251.     if (exprs)
  252.         fprintf(fp, "keycode %3d =", i);
  253.     else
  254.         fprintf(fp, "    %3d    \t", i);
  255.     max = keysyms_per_keycode - 1;
  256.     while ((max >= 0) && (keymap[max] == NoSymbol))
  257.         max--;
  258.     for (j = 0; j <= max; j++) {
  259.         register KeySym ks = keymap[j];
  260.         char *s;
  261.         if (ks != NoSymbol)
  262.         s = XKeysymToString (ks);
  263.         else
  264.         s = "NoSymbol";
  265.         if (!exprs)
  266.         fprintf (fp, "0x%04x (%s)\t", ks, s ? s : "no name");
  267.         else if (s)
  268.         fprintf (fp, " %s", s);
  269.         else
  270.         fprintf (fp, " 0x%04x", ks);
  271.     }
  272.     keymap += keysyms_per_keycode;
  273.     fprintf (fp, "\n");
  274.     }
  275.  
  276.     XFree ((char *) origkeymap);
  277.     return;
  278. }
  279.  
  280.  
  281. PrintPointerMap (fp)
  282.     FILE *fp;
  283. {
  284.     unsigned char pmap[256];        /* there are 8 bits of buttons */
  285.     int count, i;
  286.  
  287.     count = XGetPointerMapping (dpy, pmap, 256);
  288.  
  289.     fprintf (fp, "There are %d pointer buttons defined.\n\n", count);
  290.     fprintf (fp, "    Physical        Button\n");
  291.     fprintf (fp, "     Button          Code\n");
  292. /*               "      ###            ###\n"               */
  293.     for (i = 0; i < count; i++) {
  294.     fprintf (fp, "      %3u            %3u\n", 
  295.          i+1, (unsigned int) pmap[i]);
  296.     }
  297.     fprintf (fp, "\n");
  298.     return;
  299. }
  300.  
  301.  
  302. /*
  303.  * SetPointerMap - set the pointer map
  304.  */
  305.  
  306. int SetPointerMap (map, n)
  307.     unsigned char *map;
  308.     int n;
  309. {
  310.     unsigned char defmap[MAXBUTTONCODES];
  311.     int j;
  312.     int retries, timeout;
  313.  
  314.     if (n == 0) {                /* reset to default */
  315.     n = XGetPointerMapping (dpy, defmap, MAXBUTTONCODES);
  316.     for (j = 0; j < n; j++) defmap[j] = (unsigned char) (j + 1);
  317.     map = defmap;
  318.     }
  319.  
  320.     for (retries = 5, timeout = 2; retries > 0; retries--, timeout *= 2) {
  321.     int result;
  322.  
  323.     switch (result = XSetPointerMapping (dpy, map, n)) {
  324.       case MappingSuccess:
  325.         return 0;
  326.       case MappingBusy:
  327.         mapping_busy_pointer (timeout);
  328.         continue;
  329.       case MappingFailed:
  330.         fprintf (stderr, "%s:  bad pointer mapping\n", ProgramName);
  331.         return -1;
  332.       default:
  333.         fprintf (stderr, "%s:  bad return %d from XSetPointerMapping\n",
  334.              ProgramName, result);
  335.         return -1;
  336.     }
  337.     }
  338.     fprintf (stderr, "%s:  unable to set pointer mapping\n", ProgramName);
  339.     return -1;
  340. }
  341.