• [^] # Re: Côté Xorg...

    Posté par . En réponse au message Lancer une commande lors de l'appui sur une touche du clavier. Évalué à 2. Dernière modification le 25 novembre 2022 à 13:05.

    #include <fcntl.h>
    #include <stdio.h>
    #include <sys/time.h>
    #include <unistd.h>
    /* config, avec un qwerty en exemple */
    struct config {
     int key; char **cmd;
     };
    static char *echo_q[] = { "echo", "touche q active", NULL };
    static char *echo_w[] = { "echo", "touche", "w", "active", NULL };
    static struct config myconf[] = {
     /* { key code, cmd } */
     {16, echo_q},
     {17, echo_w},
     };
    int main (int argc, char *argv[])
    {
     int kbd, i;
     struct {
     struct timeval time;
     unsigned short type;
     unsigned short code;
     unsigned int value;
     } ev;
     if (argc != 2)
     return 2;
     kbd = open (argv[1], O_RDONLY);
     if (kbd < 0)
     return 1;
     loop_forever:
     read (kbd, &ev, sizeof(ev));
     if (1 == ev.type && 1 == ev.value) {
     for (i = 0; i < sizeof (myconf) / sizeof (struct config); i++)
     if (myconf[i].key == ev.code) {
     if (0 == fork ())
     execvp (myconf[i].cmd[0], myconf[i].cmd);
     goto loop_forever;
     }
     /* pour avoir le codekey, sinon commenter: */
     printf ("%d:%d:%d\n", ev.type, ev.code, ev.value);
     }
     goto loop_forever;
    }

    Enregistrer le code dans un fichier inex.c, modifier le myconf comme souhaité, puis un simple :

    gcc inex.c -o inex
    ./inex /dev/inputN

    C-c ou kill pour arrêter le programme.

    Edit : j’ai oublié la récolte des codes retour des fils, je corrigerai d’ici ce soir... sauf si un autre contributeur s’en charge.

    Mort aux cons !