Skip to content

Instantly share code, notes, and snippets.

@ghedo
Last active July 5, 2020 10:21

Revisions

  1. ghedo revised this gist Jan 13, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sleepd.c
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@
    * then create the group "input" and add your user to it. Be aware that this
    * may have security implications, particularly on shared boxes.
    *
    * Copyright (C) 2012 Alessandro Ghedini <[email protected]>
    * Copyright (C) 2012 Alessandro Ghedini <[email protected]>
    * --------------------------------------------------------------
    * "THE BEER-WARE LICENSE" (Revision 42):
    * Alessandro Ghedini wrote this file. As long as you retain this
  2. ghedo revised this gist Nov 20, 2012. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions sleepd.c
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,12 @@
    /*
    * Execute a command after some time of inactivity
    *
    * Compile:
    * $ cc -o sleepd sleepd.c
    *
    * Usage:
    * $ ./sleepd -t 600 -c "slock"
    *
    * NOTE: to run this as a normal (non-root) user you may need to change
    * /dev/input/event* devices permissions, e.g. using udev:
    *
    @@ -9,12 +15,6 @@
    * then create the group "input" and add your user to it. Be aware that this
    * may have security implications, particularly on shared boxes.
    *
    * Compile:
    * $ cc -o sleepd sleepd.c
    *
    * Usage:
    * $ ./sleepd -t 600 -c "slock"
    *
    * Copyright (C) 2012 Alessandro Ghedini <[email protected]>
    * --------------------------------------------------------------
    * "THE BEER-WARE LICENSE" (Revision 42):
    @@ -127,4 +127,4 @@ int main(int argc, char *argv[]) {
    }

    return 0;
    }
    }
  3. ghedo created this gist Nov 20, 2012.
    130 changes: 130 additions & 0 deletions sleepd.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,130 @@
    /*
    * Execute a command after some time of inactivity
    *
    * NOTE: to run this as a normal (non-root) user you may need to change
    * /dev/input/event* devices permissions, e.g. using udev:
    *
    * KERNEL=="event*", NAME="input/%k", MODE="644", GROUP="input"
    *
    * then create the group "input" and add your user to it. Be aware that this
    * may have security implications, particularly on shared boxes.
    *
    * Compile:
    * $ cc -o sleepd sleepd.c
    *
    * Usage:
    * $ ./sleepd -t 600 -c "slock"
    *
    * Copyright (C) 2012 Alessandro Ghedini <[email protected]>
    * --------------------------------------------------------------
    * "THE BEER-WARE LICENSE" (Revision 42):
    * Alessandro Ghedini wrote this file. As long as you retain this
    * notice you can do whatever you want with this stuff. If we
    * meet some day, and you think this stuff is worth it, you can
    * buy me a beer in return.
    * --------------------------------------------------------------
    */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #include <fcntl.h>
    #include <getopt.h>
    #include <unistd.h>

    #include <sys/select.h>
    #include <sys/types.h>

    fd_set ev;
    int devs[128];

    void init_ev() {
    int i = 0;

    FD_ZERO(&ev);

    do {
    int fd;
    char dev_name[128];

    snprintf(dev_name, 127, "/dev/input/event%d", i++);
    fd = open(dev_name, O_RDONLY);

    if (fd < 0) break;

    devs[i] = fd;
    FD_SET(fd, &ev);
    } while (1);
    }

    void clean_ev() {
    int i;

    for (i = 0; i < 128; i++)
    close(devs[i]);
    }

    void usage(char *name) {
    fprintf(stderr, "Usage: %s -t <timeout> -c <command>\n", name);
    exit(-1);
    }

    int main(int argc, char *argv[]) {
    char *cmd = NULL;
    int opt, timeout = 0, enabled = 1;

    struct timeval tv;

    while ((opt = getopt(argc, argv, "c:t:")) != -1) {
    switch (opt) {
    case 'c':
    cmd = strdup(optarg);
    break;
    case 't':
    timeout = atoi(optarg);
    break;
    default:
    usage(argv[0]);
    }
    }

    if ((timeout == 0) || (cmd == NULL))
    usage(argv[0]);

    while (1) {
    int rc;

    init_ev();

    tv.tv_sec = timeout;
    tv.tv_usec = 0;

    rc = select(FD_SETSIZE, &ev, NULL, NULL, &tv);

    switch (rc) {
    case -1: {
    perror("sleepd");
    return -1;
    }

    case 0: {
    if (enabled) {
    enabled = 0;
    system(cmd);
    }

    break;
    }

    default: {
    enabled = 1;
    break;
    }
    }

    clean_ev();
    }

    return 0;
    }