"Fossies" - the Fresh Open Source Software Archive

Member "xbindkeys-1.8.7/keys.c" (23 May 2020, 9646 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 "keys.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 "keys.h"
   19 #include <stdio.h>
   20 #include <stdlib.h>
   21 #include <unistd.h>
   22 #include <string.h>
   23 #include <sys/types.h>
   24 #include <sys/wait.h>
   25 #include <X11/keysym.h>
   26 #include "xbindkeys.h"
   27 #include "keys.h"
   28 #include "config.h"
   29 #include "options.h"
   30 #include "grab_key.h"
   31 
   32 
   33 #define STR_KEY_LEN 1000
   34 
   35 int nb_keys;
   36 Keys_t *keys;
   37 
   38 extern char rc_file[512];
   39 
   40 static char *modifier_string[] = { "Control", "Shift", "Alt", "Mod2",
   41   "Mod3", "Mod4", "Mod5"
   42 };
   43 
   44 
   45 int
   46 init_keys (void)
   47 {
   48   nb_keys = 0;
   49   keys = NULL;
   50 
   51   return 0;
   52 }
   53 
   54 void
   55 close_keys (void)
   56 {
   57   int i;
   58 
   59   for (i = 0; i < nb_keys; i++)
   60     free_key (&keys[i]);
   61 
   62   if (keys != NULL)
   63     {
   64       free (keys);
   65       keys = NULL;
   66     }
   67 
   68   nb_keys = 0;
   69 }
   70 
   71 
   72 
   73 int
   74 add_key (KeyType_t type, EventType_t event_type, KeySym keysym, KeyCode keycode,
   75      unsigned int button, unsigned int modifier, char *command, SCM function)
   76 {
   77   Keys_t *keys_bis = NULL;
   78   int i;
   79 
   80   if (keysym == 0 && keycode == 0 && button == 0)
   81     {
   82       fprintf (stderr, "Warning: unknown key in RC file : %s\n", rc_file);
   83       return (-1);
   84     }
   85 
   86   /* make new array keys_bis */
   87   keys_bis = (Keys_t *) malloc ((nb_keys + 1) * sizeof (Keys_t));
   88   if (keys_bis == NULL)
   89     return (-1);
   90 
   91   if (keys != NULL)
   92     {
   93       /* copy keys in keys_bis */
   94       for (i = 0; i < nb_keys; i++)
   95     {
   96       keys_bis[i] = keys[i];
   97     }
   98 
   99       /* delete old keys array */
  100       free (keys);
  101     }
  102 
  103   /* make keys_bis as keys */
  104   keys = keys_bis;
  105 
  106   modifier &= ~(numlock_mask | capslock_mask | scrolllock_mask);
  107 
  108 #ifdef GUILE_FLAG
  109   if(function) scm_gc_protect_object(function);
  110 #endif
  111   /* set key */
  112   if (type == SYM)
  113     {
  114       set_keysym (&keys[nb_keys], event_type, keysym, modifier, command, function);
  115     }
  116   else if (type == BUTTON)
  117     {
  118       set_button (&keys[nb_keys], event_type, button, modifier, command, function);
  119     }
  120   else
  121     {
  122       set_keycode (&keys[nb_keys], event_type, keycode, modifier, command, function);
  123     }
  124 
  125   /* new key */
  126   nb_keys += 1;
  127 
  128   return (0);
  129 }
  130 
  131 
  132 
  133 
  134 
  135 
  136 void
  137 show_key_binding (Display * d)
  138 {
  139   int i;
  140   int last_verbose = verbose;
  141 
  142   verbose = 1;
  143 
  144   for (i = 0; i < nb_keys; i++)
  145     {
  146       print_key (d, &keys[i]);
  147     }
  148 
  149   verbose = last_verbose;
  150 }
  151 
  152 
  153 
  154 void
  155 modifier_to_string (unsigned int modifier, char *str)
  156 {
  157   str[0] = '\0';
  158 
  159   if (modifier & ControlMask)
  160     {
  161       if (str[0])
  162     strncat (str, "+", STR_KEY_LEN);
  163       strncat (str, modifier_string[0], STR_KEY_LEN);
  164     }
  165 
  166   if (modifier & ShiftMask)
  167     {
  168       if (str[0])
  169     strncat (str, "+", STR_KEY_LEN);
  170       strncat (str, modifier_string[1], STR_KEY_LEN);
  171     }
  172 
  173 
  174   if (modifier & Mod1Mask)
  175     {
  176       if (str[0])
  177     strncat (str, "+", STR_KEY_LEN);
  178       strncat (str, modifier_string[2], STR_KEY_LEN);
  179     }
  180 
  181   if (modifier & Mod2Mask)
  182     {
  183       if (str[0])
  184     strncat (str, "+", STR_KEY_LEN);
  185       strncat (str, modifier_string[3], STR_KEY_LEN);
  186     }
  187 
  188   if (modifier & Mod3Mask)
  189     {
  190       if (str[0])
  191     strncat (str, "+", STR_KEY_LEN);
  192       strncat (str, modifier_string[4], STR_KEY_LEN);
  193     }
  194 
  195   if (modifier & Mod4Mask)
  196     {
  197       if (str[0])
  198     strncat (str, "+", STR_KEY_LEN);
  199       strncat (str, modifier_string[5], STR_KEY_LEN);
  200     }
  201 
  202   if (modifier & Mod5Mask)
  203     {
  204       if (str[0])
  205     strncat (str, "+", STR_KEY_LEN);
  206       strncat (str, modifier_string[6], STR_KEY_LEN);
  207     }
  208 }
  209 
  210 
  211 void
  212 print_key (Display * d, Keys_t * key)
  213 {
  214   char str[STR_KEY_LEN];
  215   int keysyms_per_keycode_return;
  216 
  217   if (verbose)
  218     {
  219       if (key->type == SYM)
  220     {
  221       modifier_to_string (key->modifier, str);
  222 
  223       printf ("\"%s\"\n    %s%s%s%s\n",
  224           key->command != NULL ? key->command : ( key->function != 0 ? "(Scheme function)" : "NoCommand" ),
  225           key->event_type == PRESS ? "" : "Release + ",
  226           str, str[0] ? " + " : "", XKeysymToString (key->key.sym));
  227     }
  228       else if (key->type == BUTTON)
  229     {
  230       printf ("\"%s\"\n    %sm:0x%x + b:%d   (mouse)\n",
  231           key->command != NULL ? key->command : ( key->function != 0 ? "(Scheme function)" : "NoCommand" ),
  232           key->event_type == PRESS ? "" : "Release + ",
  233           key->modifier, key->key.button);
  234     }
  235       else
  236     {
  237       printf ("\"%s\"\n    %sm:0x%x + c:%d\n",
  238           key->command != NULL ? key->command : ( key->function != 0 ? "(Scheme function)" : "NoCommand" ),
  239           key->event_type == PRESS ? "" : "Release + ",
  240           key->modifier, key->key.code);
  241       if (d != NULL)
  242         {
  243           modifier_to_string (key->modifier, str);
  244           printf ("    %s%s%s%s\n",
  245               str,
  246               str[0] ? " + " : "",
  247               key->event_type == PRESS ? "" : "Release + ",
  248               (XKeysymToString (*XGetKeyboardMapping(d, key->key.code, 1, &keysyms_per_keycode_return)) != NULL) ?
  249                       XKeysymToString (*XGetKeyboardMapping(d, key->key.code, 1, &keysyms_per_keycode_return)) : "NoSymbol");
  250         }
  251     }
  252     }
  253 }
  254 
  255 
  256 void
  257 set_keysym (Keys_t * key, EventType_t event_type, KeySym keysym,
  258         unsigned int modifier, char *command, SCM function)
  259 {
  260     int size;
  261 
  262   key->type = SYM;
  263   key->event_type = event_type;
  264   key->key.sym = keysym;
  265   key->modifier = modifier;
  266 
  267   if (command != NULL)
  268     {
  269       size = (strlen (command) + 1) * sizeof (char);
  270       key->command = (char *) malloc (size);
  271       if (key->command != NULL)
  272         strncpy (key->command, command, size);
  273     }
  274   else
  275     {
  276       key->command = NULL;
  277     }
  278 
  279   key->function = function;
  280 }
  281 
  282 void
  283 set_keycode (Keys_t * key, EventType_t event_type, KeyCode keycode,
  284          unsigned int modifier, char *command, SCM function)
  285 {
  286   int size;
  287 
  288   key->type = CODE;
  289   key->event_type = event_type;
  290   key->key.code = keycode;
  291   key->modifier = modifier;
  292 
  293   if (command != NULL)
  294       {
  295       size = (strlen (command) + 1) * sizeof (char);
  296       key->command = (char *) malloc (size);
  297       if (key->command != NULL)
  298         strncpy (key->command, command, size);
  299     }
  300   else
  301     {
  302       key->command = NULL;
  303     }
  304 
  305   key->function = function;
  306 }
  307 
  308 
  309 void
  310 set_button (Keys_t * key, EventType_t event_type, unsigned int button,
  311         unsigned int modifier, char *command, SCM function)
  312 {
  313   int size;
  314 
  315   key->type = BUTTON;
  316   key->event_type = event_type;
  317   key->key.button = button;
  318   key->modifier = modifier;
  319 
  320   if (command != NULL)
  321     {
  322       size = (strlen (command) + 1) * sizeof (char);
  323       key->command = (char *) malloc (size);
  324       if (key->command != NULL)
  325         strncpy (key->command, command, size);
  326     }
  327   else
  328     {
  329       key->command = NULL;
  330     }
  331 
  332   key->function = function;
  333 }
  334 
  335 
  336 
  337 
  338 void
  339 free_key (Keys_t * key)
  340 {
  341   key->type = SYM;
  342   key->event_type = PRESS;
  343   key->key.sym = 0;
  344   key->modifier = 0;
  345 
  346   if (key->command != NULL)
  347     free (key->command);
  348 
  349 #ifdef GUILE_FLAG
  350   if(key->function) scm_gc_unprotect_object(key->function);
  351 #endif
  352   key->function = 0;
  353 }
  354 
  355 
  356 
  357 
  358 int
  359 remove_key (KeyType_t type, EventType_t event_type, KeySym keysym, KeyCode keycode,
  360         unsigned int button, unsigned int modifier)
  361 {
  362   int i, found_index = -1;
  363   Keys_t *keys_bis = NULL;
  364 
  365   if (keys == NULL)
  366     {
  367       return (-1);
  368     }
  369 
  370   modifier &= ~(numlock_mask | capslock_mask | scrolllock_mask);
  371 
  372   for (i = 0; i < nb_keys; i++)
  373     {
  374       if (keys[i].type == type &&
  375       keys[i].event_type == event_type &&
  376       keys[i].modifier == modifier &&
  377       ((type == SYM && keys[i].key.sym == keysym) ||
  378        (type == CODE && keys[i].key.code == keycode) ||
  379        (type == BUTTON && keys[i].key.button == button)))
  380     found_index = i;
  381     }
  382 
  383   if (found_index != -1)
  384     {
  385       if (verbose)
  386     printf ("Removing key index %d\n", found_index);
  387 
  388       /* make new array keys_bis */
  389       keys_bis = (Keys_t *) malloc ((nb_keys - 1) * sizeof (Keys_t));
  390       if (keys_bis == NULL)
  391     return (-1);
  392 
  393       for (i = 0; i < found_index; i++)
  394     keys_bis[i] = keys[i];
  395 
  396       for (i = found_index + 1; i < nb_keys; i++)
  397     keys_bis[i - 1] = keys[i];
  398 
  399       free_key (&keys[found_index]);
  400       free (keys);
  401 
  402       /* make keys_bis as keys */
  403       keys = keys_bis;
  404 
  405       nb_keys -= 1;
  406     }
  407 
  408   return (0);
  409 }
  410 
  411 
  412 
  413 
  414 
  415 
  416 
  417 void
  418 run_command (char *command)
  419 {
  420 #ifdef FORK_FLAG
  421   pid_t pid;
  422 
  423   if (verbose)
  424     printf ("Start program with fork+exec call\n");
  425 
  426   //  if (fork() == 0)
  427   //  execlp ("sh", "sh", "-c", key->command, NULL);
  428   if (!(pid = fork()))
  429     {
  430       setsid();
  431       switch (fork())
  432     {
  433     case 0: execlp ("sh", "sh", "-c", command, (char *) NULL);
  434       break;
  435     default: _exit(0);
  436       break;
  437     }
  438     }
  439   if (pid > 0)
  440     wait(NULL);
  441 #else
  442   if (verbose)
  443     printf ("Start program with system call\n");
  444 
  445   system (command);
  446 #endif
  447 }
  448 
  449 
  450 void
  451 start_command_key (Keys_t * key)
  452 {
  453   if (key->command == NULL)
  454     {
  455 #ifdef GUILE_FLAG
  456       if (key->function != 0)
  457     {
  458       scm_call_0 (key->function);
  459     }
  460 #endif
  461       return;
  462     }
  463 
  464   run_command (key->command);
  465 }