dtms
view src/main.c @ 3:bf6dda0e9daf
added GPL License text
author | Eleni Maria Stea <eleni@mutantstargoat.com> |
---|---|
date | Fri, 13 May 2016 23:02:26 +0300 |
parents | 50af08b83a1f |
children | db4bb4b5905a |
line source
1 /*
2 * DTMS Copyright (C) 2016 Eleni Maria Stea <elene.mst@gmail.com>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * */
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <pwd.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/select.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <time.h>
28 #include <unistd.h>
30 static int parse_args(int argc, char **argv);
31 static void print_help();
32 static void check_device(char *device_path);
33 static char* concatenate(char *mp_path, char *data_path);
35 static int uid;
36 static char *path;
37 static char *dev_path;
39 int main(int argc, char **argv)
40 {
41 time_t last_touch_time = 0;
42 int fd;
44 if(parse_args(argc, argv))
45 return 1;
47 check_device(dev_path);
49 if((fd = open(dev_path, O_RDONLY | O_NONBLOCK)) == -1) {
50 fprintf(stderr, "Failed to open device: %s, error: %s\n", dev_path, strerror(errno));
51 return 1;
52 }
54 if(seteuid(uid) == -1) {
55 perror("Set uid failed");
56 uid = 0;
57 }
59 if(!uid) {
60 const char *st = "/";
61 if(strncmp(path, st, 1) != 0) {
62 fprintf(stderr, "If you run this program as root you should pass the absolute path to a valid mp3 player.\n");
63 return 1;
64 }
65 }
67 while(1) {
68 fd_set read_set;
69 FD_ZERO(&read_set);
70 FD_SET(fd, &read_set);
72 int res;
73 while((res = select(fd + 1, &read_set, 0, 0, 0)) == -1 && errno == EINTR);
74 if(res < 0) {
75 perror("Select failed");
76 break;
77 }
78 if(res == 0) //nothing to read
79 continue;
81 if(FD_ISSET(fd, &read_set)) {
82 char buf[1024];
83 time_t now = time(0);
84 while(read(fd, buf, sizeof buf) > 0);
85 if (now - last_touch_time > 2) {
86 char *cmd = concatenate(path, "data/dtms.mp3");
87 system(cmd);
88 last_touch_time = now;
89 }
90 }
91 }
92 close(fd);
93 }
95 static int parse_args(int argc, char **argv)
96 {
97 for(int i=1; i<argc; i++) {
98 if((strcmp(argv[i], "-h") == 0)) {
99 print_help();
100 exit(0);
101 }
102 if((strcmp(argv[i], "-p") == 0)) {
103 if(argv[i+1]) {
104 path = argv[i+1];
105 }
106 else {
107 fprintf(stderr, "Invalid path. Please give the absolute path to an mp3 player.\n");
108 exit(1);
109 }
110 }
112 if((strcmp(argv[i], "-u") == 0)) {
113 if(argv[i+1]) {
114 struct passwd *passwd = getpwnam(argv[i+1]);
115 if(!passwd) {
116 fprintf(stderr, "Failed to get uid for: %s : %s.\n", argv[i+1], strerror(errno));
117 exit(1);
118 }
119 uid = passwd->pw_uid;
120 }
121 else {
122 fprintf(stderr, "Invalid username. Type -u `whoami`.\n");
123 exit(1);
124 }
125 }
126 if((strcmp(argv[i], "-d") == 0)) {
127 if(argv[i+1]) {
128 dev_path = argv[i+1];
129 }
130 else {
131 fprintf(stderr, "Invalid device file.\n");
132 exit(1);
133 }
134 }
135 }
136 return 0;
137 }
139 static void print_help()
140 {
141 printf("Options:\n");
142 printf("-h, prints this help\n");
143 printf("-d, path to the device\n");
144 printf("-p, path to the mp3 player\n");
145 printf("-u. username of the user that runs the program\n");
146 printf("--------\n");
147 printf("Examples:\n");
148 printf("--------\n");
149 printf("./dtms -d /dev/usb/hiddev0 -u eleni -p /usr/bin/mpv\n");
150 }
152 static void check_device(char *device_path)
153 {
154 struct stat sb;
155 if(stat(dev_path, &sb) == -1) {
156 perror("stat");
157 exit(0);
158 }
159 if(((sb.st_mode & S_IFMT) != S_IFBLK) && ((sb.st_mode & S_IFMT) != S_IFCHR)) {
160 fprintf(stderr, "Invalid device file.\n");
161 exit(0);
162 }
163 }
165 static char *concatenate(char *mp_path, char *data_path)
166 {
167 char *res = malloc(strlen(mp_path) + 1 + strlen(data_path) + 1);
168 strcpy(res, mp_path);
169 strcat(res, " ");
170 strcat(res, data_path);
172 return res;
173 }