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

    Posté par . En réponse au message Lancer une commande lors de l'appui sur une touche du clavier. Évalué à 3.

    Voici le code sans zombies :

    #include <fcntl.h>
    #include <stdio.h>
    #include <sys/time.h>
    #include <sys/wait.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 char *sleep_9[] = { "sleep", "9", NULL };
    static struct config myconf[] = {
     /* { key code, cmd } */
     {16, echo_q},
     {17, echo_w},
     {18, sleep_9},
     };
    int main (int argc, char *argv[])
    {
     int kbd, pid;
     struct config *p;
     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 (p = myconf; p < myconf + sizeof (myconf) / sizeof (struct config); p++)
     if (p->key == ev.code) {
     pid = fork ();
     if (0 == pid) {
     if (0 == fork ())
     /* execlp pour exclure la recherche dans $PATH */
     execvp (p->cmd[0], p->cmd);
     return 0;
     } else {
     if (pid > 0)
     waitpid (pid, NULL, 0);
     }
     goto loop_forever;
     }
     /* pour avoir le codekey, sinon commenter: */
     printf ("%d:%d:%d\n", ev.type, ev.code, ev.value);
     }
     goto loop_forever;
    }

    Mort aux cons !