dtms

diff src/main.c @ 0:50af08b83a1f

a program that shouts "Don't touch my screen, bro" whenever someone touches the screen
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Fri, 13 May 2016 21:03:06 +0300
parents
children bf6dda0e9daf
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main.c	Fri May 13 21:03:06 2016 +0300
     1.3 @@ -0,0 +1,156 @@
     1.4 +#include <errno.h>
     1.5 +#include <fcntl.h>
     1.6 +#include <pwd.h>
     1.7 +#include <stdio.h>
     1.8 +#include <stdlib.h>
     1.9 +#include <string.h>
    1.10 +#include <sys/select.h>
    1.11 +#include <sys/stat.h>
    1.12 +#include <sys/types.h>
    1.13 +#include <time.h>
    1.14 +#include <unistd.h>
    1.15 +
    1.16 +static int parse_args(int argc, char **argv);
    1.17 +static void print_help();
    1.18 +static void check_device(char *device_path);
    1.19 +static char* concatenate(char *mp_path, char *data_path);
    1.20 +
    1.21 +static int uid;
    1.22 +static char *path;
    1.23 +static char *dev_path;
    1.24 +
    1.25 +int main(int argc, char **argv)
    1.26 +{
    1.27 +    time_t last_touch_time = 0;
    1.28 +    int fd;
    1.29 +
    1.30 +    if(parse_args(argc, argv))
    1.31 +	return 1;
    1.32 +
    1.33 +    check_device(dev_path);
    1.34 +
    1.35 +    if((fd = open(dev_path, O_RDONLY | O_NONBLOCK)) == -1) {
    1.36 +	fprintf(stderr, "Failed to open device: %s, error: %s\n", dev_path, strerror(errno));
    1.37 +	return 1;
    1.38 +    }
    1.39 +
    1.40 +    if(seteuid(uid) == -1) {
    1.41 +	perror("Set uid failed");
    1.42 +	uid = 0;
    1.43 +    }
    1.44 +
    1.45 +    if(!uid) {
    1.46 +	const char *st = "/";
    1.47 +	if(strncmp(path, st, 1) != 0) {
    1.48 +	    fprintf(stderr, "If you run this program as root you should pass the absolute path to a valid mp3 player.\n");
    1.49 +	    return 1;
    1.50 +	}
    1.51 +    }
    1.52 +
    1.53 +    while(1) {
    1.54 +	fd_set read_set;
    1.55 +	FD_ZERO(&read_set);
    1.56 +	FD_SET(fd, &read_set);
    1.57 +
    1.58 +	int res;
    1.59 +	while((res = select(fd + 1, &read_set, 0, 0, 0)) == -1 && errno == EINTR);
    1.60 +	if(res < 0) {
    1.61 +	    perror("Select failed");
    1.62 +	    break;
    1.63 +	}
    1.64 +	if(res == 0) //nothing to read
    1.65 +	    continue;
    1.66 +
    1.67 +	if(FD_ISSET(fd, &read_set)) {
    1.68 +	    char buf[1024];
    1.69 +	    time_t now = time(0);
    1.70 +	    while(read(fd, buf, sizeof buf) > 0);
    1.71 +	    if (now - last_touch_time > 2) {
    1.72 +		char *cmd = concatenate(path, "data/dtms.mp3");
    1.73 +		system(cmd);
    1.74 +		last_touch_time = now;
    1.75 +	    }
    1.76 +	}
    1.77 +    }
    1.78 +    close(fd);
    1.79 +}
    1.80 +
    1.81 +static int parse_args(int argc, char **argv)
    1.82 +{
    1.83 +    for(int i=1; i<argc; i++) {
    1.84 +	if((strcmp(argv[i], "-h") == 0)) {
    1.85 +	    print_help();
    1.86 +	    exit(0);
    1.87 +	}
    1.88 +	if((strcmp(argv[i], "-p") == 0)) {
    1.89 +	    if(argv[i+1]) {
    1.90 +		path = argv[i+1];
    1.91 +	    }
    1.92 +	    else {
    1.93 +		fprintf(stderr, "Invalid path. Please give the absolute path to an mp3 player.\n");
    1.94 +		exit(1);
    1.95 +	    }
    1.96 +	}
    1.97 +
    1.98 +	if((strcmp(argv[i], "-u") == 0)) {
    1.99 +	    if(argv[i+1]) {
   1.100 +		struct passwd *passwd = getpwnam(argv[i+1]);
   1.101 +		if(!passwd) {
   1.102 +		    fprintf(stderr, "Failed to get uid for: %s : %s.\n", argv[i+1], strerror(errno));
   1.103 +		    exit(1);
   1.104 +		}
   1.105 +		uid = passwd->pw_uid;
   1.106 +	    }
   1.107 +	    else {
   1.108 +		fprintf(stderr, "Invalid username. Type -u `whoami`.\n");
   1.109 +		exit(1);
   1.110 +	    }
   1.111 +	}
   1.112 +	if((strcmp(argv[i], "-d") == 0)) {
   1.113 +	    if(argv[i+1]) {
   1.114 +		dev_path = argv[i+1];
   1.115 +	    }
   1.116 +	    else {
   1.117 +		fprintf(stderr, "Invalid device file.\n");
   1.118 +		exit(1);
   1.119 +	    }
   1.120 +	}
   1.121 +    }
   1.122 +    return 0;
   1.123 +}
   1.124 +
   1.125 +static void print_help()
   1.126 +{
   1.127 +    printf("Options:\n");
   1.128 +    printf("-h, prints this help\n");
   1.129 +    printf("-d, path to the device\n");
   1.130 +    printf("-p, path to the mp3 player\n");
   1.131 +    printf("-u. username of the user that runs the program\n");
   1.132 +    printf("--------\n");
   1.133 +    printf("Examples:\n");
   1.134 +    printf("--------\n");
   1.135 +    printf("./dtms -d /dev/usb/hiddev0 -u eleni -p /usr/bin/mpv\n");
   1.136 +}
   1.137 +
   1.138 +static void check_device(char *device_path)
   1.139 +{
   1.140 +    struct stat sb;
   1.141 +    if(stat(dev_path, &sb) == -1) {
   1.142 +	perror("stat");
   1.143 +	exit(0);
   1.144 +    }
   1.145 +    if(((sb.st_mode & S_IFMT) != S_IFBLK) && ((sb.st_mode & S_IFMT) != S_IFCHR)) {
   1.146 +	fprintf(stderr, "Invalid device file.\n");
   1.147 +	exit(0);
   1.148 +    }
   1.149 +}
   1.150 +
   1.151 +static char *concatenate(char *mp_path, char *data_path)
   1.152 +{
   1.153 +    char *res = malloc(strlen(mp_path) + 1 + strlen(data_path) + 1);
   1.154 +    strcpy(res, mp_path);
   1.155 +    strcat(res, " ");
   1.156 +    strcat(res, data_path);
   1.157 +
   1.158 +    return res;
   1.159 +}