dtms
changeset 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 | 49df14292d38 |
files | .hgignore Makefile data/dtms.mp3 src/main.c |
diffstat | 4 files changed, 187 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Fri May 13 21:03:06 2016 +0300 1.3 @@ -0,0 +1,7 @@ 1.4 +syntax: glob 1.5 +*.au 1.6 +*.o 1.7 +*.d 1.8 +*.aup 1.9 +*.swp 1.10 +dtms
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/Makefile Fri May 13 21:03:06 2016 +0300 2.3 @@ -0,0 +1,24 @@ 2.4 +src = $(wildcard src/*.c) 2.5 +obj = $(src:.c=.o) 2.6 +dep = $(obj:.o=.d) 2.7 +bin = dtms 2.8 + 2.9 +dbg = -g 2.10 +opt = -O0 2.11 + 2.12 +CC = gcc 2.13 +INCLUDE = -I/usr/include 2.14 +CFLAGS = -pedantic -Wall -std=c99 -D_XOPEN_SOURCE=600 $(dbg) $(INCLUDE) 2.15 +LDFLAGS = -L/usr/lib $(libs) 2.16 + 2.17 +$(bin): $(obj) 2.18 + $(CC) -o $@ $(obj) $(LDFLAGS) 2.19 + 2.20 +-include $(dep) 2.21 + 2.22 +%.d: %.c 2.23 + @$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ 2.24 + 2.25 +.PHONY: clean 2.26 +clean: 2.27 + rm -f $(obj) $(bin) $(dep)
3.1 Binary file data/dtms.mp3 has changed
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/main.c Fri May 13 21:03:06 2016 +0300 4.3 @@ -0,0 +1,156 @@ 4.4 +#include <errno.h> 4.5 +#include <fcntl.h> 4.6 +#include <pwd.h> 4.7 +#include <stdio.h> 4.8 +#include <stdlib.h> 4.9 +#include <string.h> 4.10 +#include <sys/select.h> 4.11 +#include <sys/stat.h> 4.12 +#include <sys/types.h> 4.13 +#include <time.h> 4.14 +#include <unistd.h> 4.15 + 4.16 +static int parse_args(int argc, char **argv); 4.17 +static void print_help(); 4.18 +static void check_device(char *device_path); 4.19 +static char* concatenate(char *mp_path, char *data_path); 4.20 + 4.21 +static int uid; 4.22 +static char *path; 4.23 +static char *dev_path; 4.24 + 4.25 +int main(int argc, char **argv) 4.26 +{ 4.27 + time_t last_touch_time = 0; 4.28 + int fd; 4.29 + 4.30 + if(parse_args(argc, argv)) 4.31 + return 1; 4.32 + 4.33 + check_device(dev_path); 4.34 + 4.35 + if((fd = open(dev_path, O_RDONLY | O_NONBLOCK)) == -1) { 4.36 + fprintf(stderr, "Failed to open device: %s, error: %s\n", dev_path, strerror(errno)); 4.37 + return 1; 4.38 + } 4.39 + 4.40 + if(seteuid(uid) == -1) { 4.41 + perror("Set uid failed"); 4.42 + uid = 0; 4.43 + } 4.44 + 4.45 + if(!uid) { 4.46 + const char *st = "/"; 4.47 + if(strncmp(path, st, 1) != 0) { 4.48 + fprintf(stderr, "If you run this program as root you should pass the absolute path to a valid mp3 player.\n"); 4.49 + return 1; 4.50 + } 4.51 + } 4.52 + 4.53 + while(1) { 4.54 + fd_set read_set; 4.55 + FD_ZERO(&read_set); 4.56 + FD_SET(fd, &read_set); 4.57 + 4.58 + int res; 4.59 + while((res = select(fd + 1, &read_set, 0, 0, 0)) == -1 && errno == EINTR); 4.60 + if(res < 0) { 4.61 + perror("Select failed"); 4.62 + break; 4.63 + } 4.64 + if(res == 0) //nothing to read 4.65 + continue; 4.66 + 4.67 + if(FD_ISSET(fd, &read_set)) { 4.68 + char buf[1024]; 4.69 + time_t now = time(0); 4.70 + while(read(fd, buf, sizeof buf) > 0); 4.71 + if (now - last_touch_time > 2) { 4.72 + char *cmd = concatenate(path, "data/dtms.mp3"); 4.73 + system(cmd); 4.74 + last_touch_time = now; 4.75 + } 4.76 + } 4.77 + } 4.78 + close(fd); 4.79 +} 4.80 + 4.81 +static int parse_args(int argc, char **argv) 4.82 +{ 4.83 + for(int i=1; i<argc; i++) { 4.84 + if((strcmp(argv[i], "-h") == 0)) { 4.85 + print_help(); 4.86 + exit(0); 4.87 + } 4.88 + if((strcmp(argv[i], "-p") == 0)) { 4.89 + if(argv[i+1]) { 4.90 + path = argv[i+1]; 4.91 + } 4.92 + else { 4.93 + fprintf(stderr, "Invalid path. Please give the absolute path to an mp3 player.\n"); 4.94 + exit(1); 4.95 + } 4.96 + } 4.97 + 4.98 + if((strcmp(argv[i], "-u") == 0)) { 4.99 + if(argv[i+1]) { 4.100 + struct passwd *passwd = getpwnam(argv[i+1]); 4.101 + if(!passwd) { 4.102 + fprintf(stderr, "Failed to get uid for: %s : %s.\n", argv[i+1], strerror(errno)); 4.103 + exit(1); 4.104 + } 4.105 + uid = passwd->pw_uid; 4.106 + } 4.107 + else { 4.108 + fprintf(stderr, "Invalid username. Type -u `whoami`.\n"); 4.109 + exit(1); 4.110 + } 4.111 + } 4.112 + if((strcmp(argv[i], "-d") == 0)) { 4.113 + if(argv[i+1]) { 4.114 + dev_path = argv[i+1]; 4.115 + } 4.116 + else { 4.117 + fprintf(stderr, "Invalid device file.\n"); 4.118 + exit(1); 4.119 + } 4.120 + } 4.121 + } 4.122 + return 0; 4.123 +} 4.124 + 4.125 +static void print_help() 4.126 +{ 4.127 + printf("Options:\n"); 4.128 + printf("-h, prints this help\n"); 4.129 + printf("-d, path to the device\n"); 4.130 + printf("-p, path to the mp3 player\n"); 4.131 + printf("-u. username of the user that runs the program\n"); 4.132 + printf("--------\n"); 4.133 + printf("Examples:\n"); 4.134 + printf("--------\n"); 4.135 + printf("./dtms -d /dev/usb/hiddev0 -u eleni -p /usr/bin/mpv\n"); 4.136 +} 4.137 + 4.138 +static void check_device(char *device_path) 4.139 +{ 4.140 + struct stat sb; 4.141 + if(stat(dev_path, &sb) == -1) { 4.142 + perror("stat"); 4.143 + exit(0); 4.144 + } 4.145 + if(((sb.st_mode & S_IFMT) != S_IFBLK) && ((sb.st_mode & S_IFMT) != S_IFCHR)) { 4.146 + fprintf(stderr, "Invalid device file.\n"); 4.147 + exit(0); 4.148 + } 4.149 +} 4.150 + 4.151 +static char *concatenate(char *mp_path, char *data_path) 4.152 +{ 4.153 + char *res = malloc(strlen(mp_path) + 1 + strlen(data_path) + 1); 4.154 + strcpy(res, mp_path); 4.155 + strcat(res, " "); 4.156 + strcat(res, data_path); 4.157 + 4.158 + return res; 4.159 +}