"Fossies" - the Fresh Open Source Software Archive

Member "xbindkeys-1.8.7/grab_key.c" (1 Mar 2020, 8579 Bytes) of package /linux/privat/xbindkeys-1.8.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "grab_key.c" see the Fossies "Dox" file reference documentation.

    1 /***************************************************************************
    2         xbindkeys : a program to bind keys to commands under X11.
    3                            -------------------
    4     begin                : Sat Oct 13 14:11:34 CEST 2001
    5     copyright            : (C) 2001 by Philippe Brochard
    6     email                : hocwp@free.fr
    7  ***************************************************************************/
    8 
    9 /***************************************************************************
   10  *                                                                         *
   11  *   This program is free software; you can redistribute it and/or modify  *
   12  *   it under the terms of the GNU General Public License as published by  *
   13  *   the Free Software Foundation; either version 2 of the License, or     *
   14  *   (at your option) any later version.                                   *
   15  *                                                                         *
   16  ***************************************************************************/
   17 
   18 #include <X11/Xlib.h>
   19 #include <X11/keysym.h>
   20 #include <stdio.h>
   21 #include <stdlib.h>
   22 #include "xbindkeys.h"
   23 #include "grab_key.h"
   24 #include "options.h"
   25 #include "keys.h"
   26 
   27 
   28 unsigned int numlock_mask = 0;
   29 unsigned int scrolllock_mask = 0;
   30 unsigned int capslock_mask = 0;
   31 
   32 
   33 
   34 static void my_grab_key (Display * dpy, KeyCode keycode,
   35              unsigned int modifier, Window win);
   36 
   37 
   38 
   39 void
   40 get_offending_modifiers (Display * dpy)
   41 {
   42   int i;
   43   XModifierKeymap *modmap;
   44   KeyCode nlock, slock;
   45   static int mask_table[8] = {
   46     ShiftMask, LockMask, ControlMask, Mod1Mask,
   47     Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
   48   };
   49 
   50   nlock = XKeysymToKeycode (dpy, XK_Num_Lock);
   51   slock = XKeysymToKeycode (dpy, XK_Scroll_Lock);
   52 
   53   /*
   54    * Find out the masks for the NumLock and ScrollLock modifiers,
   55    * so that we can bind the grabs for when they are enabled too.
   56    */
   57   modmap = XGetModifierMapping (dpy);
   58 
   59   if (modmap != NULL && modmap->max_keypermod > 0)
   60     {
   61       for (i = 0; i < 8 * modmap->max_keypermod; i++)
   62     {
   63       if (modmap->modifiermap[i] == nlock && nlock != 0)
   64         numlock_mask = mask_table[i / modmap->max_keypermod];
   65       else if (modmap->modifiermap[i] == slock && slock != 0)
   66         scrolllock_mask = mask_table[i / modmap->max_keypermod];
   67     }
   68     }
   69 
   70   capslock_mask = LockMask;
   71 
   72   if (modmap)
   73     XFreeModifiermap (modmap);
   74 }
   75 
   76 
   77 static void
   78 my_grab_key (Display * dpy, KeyCode keycode, unsigned int modifier,
   79          Window win)
   80 {
   81   modifier &= ~(numlock_mask | capslock_mask | scrolllock_mask);
   82 
   83 
   84   XGrabKey (dpy, keycode, modifier, (win ? win : DefaultRootWindow (dpy)),
   85         False, GrabModeAsync, GrabModeAsync);
   86 
   87   if (modifier == AnyModifier)
   88     return;
   89 
   90   if (numlock_mask)
   91     XGrabKey (dpy, keycode, modifier | numlock_mask,
   92           (win ? win : DefaultRootWindow (dpy)),
   93           False, GrabModeAsync, GrabModeAsync);
   94 
   95   if (capslock_mask)
   96     XGrabKey (dpy, keycode, modifier | capslock_mask,
   97           (win ? win : DefaultRootWindow (dpy)),
   98           False, GrabModeAsync, GrabModeAsync);
   99 
  100   if (scrolllock_mask)
  101     XGrabKey (dpy, keycode, modifier | scrolllock_mask,
  102           (win ? win : DefaultRootWindow (dpy)),
  103           False, GrabModeAsync, GrabModeAsync);
  104 
  105   if (numlock_mask && capslock_mask)
  106     XGrabKey (dpy, keycode, modifier | numlock_mask | capslock_mask,
  107           (win ? win : DefaultRootWindow (dpy)),
  108           False, GrabModeAsync, GrabModeAsync);
  109 
  110   if (numlock_mask && scrolllock_mask)
  111     XGrabKey (dpy, keycode, modifier | numlock_mask | scrolllock_mask,
  112           (win ? win : DefaultRootWindow (dpy)),
  113           False, GrabModeAsync, GrabModeAsync);
  114 
  115   if (capslock_mask && scrolllock_mask)
  116     XGrabKey (dpy, keycode, modifier | capslock_mask | scrolllock_mask,
  117           (win ? win : DefaultRootWindow (dpy)),
  118           False, GrabModeAsync, GrabModeAsync);
  119 
  120   if (numlock_mask && capslock_mask && scrolllock_mask)
  121     XGrabKey (dpy, keycode,
  122           modifier | numlock_mask | capslock_mask | scrolllock_mask,
  123           (win ? win : DefaultRootWindow (dpy)), False, GrabModeAsync,
  124           GrabModeAsync);
  125 
  126 }
  127 
  128 
  129 static void
  130 my_grab_button (Display * dpy, unsigned int button, unsigned int modifier,
  131         Window win)
  132 {
  133   modifier &= ~(numlock_mask | capslock_mask | scrolllock_mask);
  134 
  135   XGrabButton (dpy, button, modifier, (win ? win : DefaultRootWindow (dpy)),
  136            False, ButtonPressMask | ButtonReleaseMask,
  137            GrabModeAsync, GrabModeAsync, None, None);
  138 
  139   if (modifier == AnyModifier)
  140     return;
  141 
  142   if (numlock_mask)
  143     XGrabButton (dpy, button, modifier | numlock_mask,
  144          (win ? win : DefaultRootWindow (dpy)),
  145          False, ButtonPressMask | ButtonReleaseMask,
  146          GrabModeAsync, GrabModeAsync, None, None);
  147 
  148 
  149   if (capslock_mask)
  150     XGrabButton (dpy, button, modifier | capslock_mask,
  151          (win ? win : DefaultRootWindow (dpy)),
  152          False, ButtonPressMask | ButtonReleaseMask,
  153          GrabModeAsync, GrabModeAsync, None, None);
  154 
  155   if (scrolllock_mask)
  156     XGrabButton (dpy, button, modifier | scrolllock_mask,
  157          (win ? win : DefaultRootWindow (dpy)),
  158          False, ButtonPressMask | ButtonReleaseMask,
  159          GrabModeAsync, GrabModeAsync, None, None);
  160 
  161   if (numlock_mask && capslock_mask)
  162     XGrabButton (dpy, button, modifier | numlock_mask | capslock_mask,
  163          (win ? win : DefaultRootWindow (dpy)),
  164          False, ButtonPressMask | ButtonReleaseMask,
  165          GrabModeAsync, GrabModeAsync, None, None);
  166 
  167   if (numlock_mask && scrolllock_mask)
  168     XGrabButton (dpy, button, modifier | numlock_mask | scrolllock_mask,
  169          (win ? win : DefaultRootWindow (dpy)),
  170          False, ButtonPressMask | ButtonReleaseMask,
  171          GrabModeAsync, GrabModeAsync, None, None);
  172 
  173   if (capslock_mask && scrolllock_mask)
  174     XGrabButton (dpy, button, modifier | capslock_mask | scrolllock_mask,
  175          (win ? win : DefaultRootWindow (dpy)),
  176          False, ButtonPressMask | ButtonReleaseMask,
  177          GrabModeAsync, GrabModeAsync, None, None);
  178 
  179   if (numlock_mask && capslock_mask && scrolllock_mask)
  180     XGrabButton (dpy, button,
  181          modifier | numlock_mask | capslock_mask | scrolllock_mask,
  182          (win ? win : DefaultRootWindow (dpy)), False,
  183          ButtonPressMask | ButtonReleaseMask, GrabModeAsync,
  184          GrabModeAsync, None, None);
  185 }
  186 
  187 
  188 
  189 void
  190 ungrab_all_keys (Display * dpy)
  191 {
  192   int screen;
  193 
  194   for (screen = 0; screen < ScreenCount (dpy); screen++)
  195     {
  196       XUngrabKey (dpy, AnyKey, AnyModifier, RootWindow (dpy, screen));
  197       XUngrabButton (dpy, AnyButton, AnyModifier, RootWindow (dpy, screen));
  198     }
  199 }
  200 
  201 
  202 void
  203 grab_keys (Display * dpy)
  204 {
  205   int i;
  206   int min, max;
  207   int screen;
  208 
  209   XDisplayKeycodes (dpy, &min, &max);
  210 
  211   if (verbose)
  212     {
  213       printf ("\n");
  214       printf ("min_keycode=%d     max_keycode=%d (ie: know keycodes)\n", min,
  215           max);
  216     }
  217 
  218   ungrab_all_keys (dpy);
  219 
  220 
  221   for (i = 0; i < nb_keys; i++)
  222     {
  223       print_key (dpy, &keys[i]);
  224 
  225       if (keys[i].type == SYM)
  226     {
  227       for (screen = 0; screen < ScreenCount (dpy); screen++)
  228         {
  229           KeyCode code = XKeysymToKeycode (dpy, keys[i].key.sym);
  230           if (code != 0)
  231             {
  232               my_grab_key (dpy, code,
  233                    keys[i].modifier, RootWindow (dpy, screen));
  234             }
  235           else
  236             {
  237               fprintf (stderr, "--- xbindkeys error ---\n");
  238               if (!verbose)
  239             {
  240               verbose = 1;
  241               print_key (dpy, &keys[i]);
  242               verbose = 0;
  243             }
  244           fprintf (stderr,
  245                            "  The key symbol '%s' cannot be used, as it's not mapped\n"
  246                            "  on your keyboard.\n"
  247                            "  xbindkeys will keep running, but will ignore this symbol.\n",
  248                            XKeysymToString (keys[i].key.sym));
  249             }
  250 
  251         }
  252     }
  253       else if (keys[i].type == BUTTON)
  254     {
  255       for (screen = 0; screen < ScreenCount (dpy); screen++)
  256         {
  257           my_grab_button (dpy, keys[i].key.button, keys[i].modifier,
  258                   RootWindow (dpy, screen));
  259         }
  260     }
  261       else
  262     {
  263       if (keys[i].key.code >= min && keys[i].key.code <= max)
  264         {
  265           for (screen = 0; screen < ScreenCount (dpy); screen++)
  266         {
  267           my_grab_key (dpy, keys[i].key.code, keys[i].modifier,
  268                    RootWindow (dpy, screen));
  269         }
  270         }
  271       else
  272         {
  273           fprintf (stderr, "--- xbindkeys error ---\n");
  274 
  275           if (!verbose)
  276         {
  277           verbose = 1;
  278           print_key (dpy, &keys[i]);
  279         }
  280 
  281           fprintf (stderr,
  282                "  The keycode %d cannot be used, as it's not between the\n"
  283                "  min(%d) and max(%d) keycode of your keyboard.\n"
  284                "  Please increase the 'maximum' value in\n"
  285                "    /usr/X11R6/lib/X11/xkb/keycodes/xfree86,\n"
  286                "  then restart X.\n", keys[i].key.code, min, max);
  287           end_it_all (dpy);
  288           exit (-1);
  289         }
  290     }
  291     }
  292 }