mpcinputd.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright 2017 Markus Hennecke <markus-hennecke@markus-hennecke.de>
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  25. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #define _GNU_SOURCE
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <dirent.h>
  35. #include <fcntl.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <unistd.h>
  40. #include <err.h>
  41. #include <errno.h>
  42. #include <ctype.h>
  43. #include <signal.h>
  44. #include <pwd.h>
  45. #include <poll.h>
  46. #include <linux/input.h>
  47. #define PIDFILE "/run/mpcinputd.pid"
  48. volatile sig_atomic_t quit = 0;
  49. void *
  50. read_file(char *_file)
  51. {
  52. struct stat st;
  53. void *result;
  54. int fd;
  55. ssize_t n, s, done = 0;
  56. if (lstat(_file, &st) != 0) {
  57. warn("stat");
  58. return NULL;
  59. }
  60. if (! (st.st_mode & S_IFREG)) {
  61. warnx("%s is no regular file", _file);
  62. return NULL;
  63. }
  64. s = st.st_size;
  65. if ((fd = open(_file, O_RDONLY)) == -1)
  66. err(1, "open");
  67. if ((result = calloc(1, s)) == NULL)
  68. err(1, "malloc");
  69. while (done < s) {
  70. n = read(fd, (char *)result + done, s - done);
  71. if (n < 0)
  72. err(1, "read");
  73. if (n == 0) /* sysfs won't give us the actual length... */
  74. break;
  75. done += n;
  76. }
  77. close(fd);
  78. return result;
  79. }
  80. void
  81. chomp(char *_s)
  82. {
  83. size_t idx = strlen(_s);
  84. while (idx && !isprint(_s[idx])) {
  85. _s[idx] = '\0';
  86. --idx;
  87. }
  88. }
  89. char *
  90. find_input_device(char *_name)
  91. {
  92. DIR *d;
  93. char *result = NULL;
  94. struct dirent *ent;
  95. int dev_num = -1;
  96. if ((d = opendir("/sys/class/input")) == NULL) {
  97. warn("opendir");
  98. return NULL;
  99. }
  100. while ((ent = readdir(d)) != NULL && dev_num == -1) {
  101. char *dev_name;
  102. if (strncmp("input", ent->d_name, 5 /* strlen("input") */)) {
  103. continue;
  104. }
  105. if (asprintf(&dev_name, "/sys/class/input/%s/name",
  106. ent->d_name) < 0) {
  107. warn("asprintf");
  108. continue;
  109. }
  110. char *name = read_file(dev_name);
  111. if (name) {
  112. chomp(name);
  113. if (strcmp(name, _name) == 0) {
  114. char *fst_digit = ent->d_name;
  115. char *end_digit = NULL;
  116. while (*fst_digit && !isdigit(*fst_digit))
  117. ++fst_digit;
  118. dev_num = strtol(fst_digit, &end_digit, 10);
  119. if (*end_digit != '\0') {
  120. warnx("Unable to parse device number");
  121. dev_num = -1;
  122. }
  123. }
  124. }
  125. free(name);
  126. free(dev_name);
  127. }
  128. if (dev_num >= 0) {
  129. if (asprintf(&result, "/dev/input/event%d", dev_num) == -1)
  130. err(1, "asprintf");
  131. }
  132. closedir(d);
  133. return result;
  134. }
  135. void
  136. sig_handler(int _sig)
  137. {
  138. switch (_sig) {
  139. case SIGINT:
  140. case SIGTERM:
  141. quit = 1;
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. int
  148. mpc_command(char *_cmd)
  149. {
  150. int rc;
  151. char *cmd;
  152. if (asprintf(&cmd, "mpc %s >/dev/null 2>&1", _cmd) == -1) {
  153. warn("asprintf");
  154. return -1;
  155. }
  156. rc = system(cmd);
  157. if (rc != 0) {
  158. warnx("cmd '%s' returned with exit code %d", cmd, rc);
  159. }
  160. free(cmd);
  161. return rc;
  162. }
  163. void
  164. drop_privileges(char *_user)
  165. {
  166. struct passwd *ent;
  167. while ((ent = getpwent()) != NULL) {
  168. if (strcmp(ent->pw_name, _user) == 0) {
  169. if (setgid(ent->pw_gid) == -1)
  170. err(1, "setgid");
  171. if (setuid(ent->pw_uid) == -1)
  172. err(1, "setuid");
  173. break;
  174. }
  175. }
  176. endpwent();
  177. }
  178. void
  179. write_pid_file(char *_pidfile)
  180. {
  181. int fd;
  182. struct stat st;
  183. if (stat(_pidfile, &st) == -1) {
  184. if (errno != ENOENT)
  185. err(1, "stat");
  186. } else {
  187. errx(1, "pid file already exists");
  188. }
  189. if ((fd = creat(_pidfile, 0755)) == -1)
  190. err(1, "creat");
  191. FILE *f = fdopen(fd, "a");
  192. pid_t pid = getpid();
  193. fprintf(f, "%d\n", pid);
  194. fclose(f);
  195. }
  196. void
  197. handle_input(char *_device)
  198. {
  199. int flags;
  200. struct pollfd poll_fd;
  201. int fd = open(_device, O_RDONLY);
  202. if (fd == -1)
  203. err(1, "open");
  204. flags = fcntl(fd, F_GETFL, 0);
  205. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
  206. err(1, "fcntl");
  207. if (daemon(0, 0) != 0)
  208. err(1, "daemon");
  209. write_pid_file(PIDFILE);
  210. drop_privileges("nobody");
  211. //signal(SIGINT, sig_handler);
  212. signal(SIGTERM, sig_handler);
  213. poll_fd.fd = fd;
  214. poll_fd.events = POLLIN;
  215. while (!quit) {
  216. struct input_event ev;
  217. ssize_t n;
  218. int res;
  219. res = poll(&poll_fd, 1, -1);
  220. if (res < 0)
  221. err(1, "poll");
  222. if (res == 0)
  223. continue;
  224. n = read(fd, &ev, sizeof(ev));
  225. if (n < 0)
  226. err(1, "read");
  227. if (n == 0)
  228. continue;
  229. if (n != sizeof(ev))
  230. errx(1, "read: invalid size");
  231. if (ev.type != EV_MSC || ev.code != EV_MSC)
  232. continue;
  233. switch (ev.value) {
  234. case 0x826f15:
  235. printf("Play button pressed\n");
  236. mpc_command("toggle");
  237. break;
  238. case 0x826f14:
  239. printf("Record button pressed\n");
  240. break;
  241. case 0x826f09:
  242. printf("Stop button pressed\n");
  243. mpc_command("stop");
  244. break;
  245. case 0x826f04:
  246. printf("Previous button pressed\n");
  247. mpc_command("prev");
  248. break;
  249. case 0x826f0a:
  250. printf("Pause button pressed\n");
  251. mpc_command("toggle");
  252. break;
  253. case 0x826f06:
  254. printf("Next button pressed\n");
  255. mpc_command("next");
  256. break;
  257. case 0x826f0e:
  258. printf("mute button pressed\n");
  259. mpc_command("pause");
  260. break;
  261. default:
  262. printf("Key: %08x\n", ev.value);
  263. break;
  264. }
  265. }
  266. close(fd);
  267. }
  268. void
  269. usage(void)
  270. {
  271. extern char *__progname;
  272. fprintf(stderr, "usage: %s input_name\n", __progname);
  273. exit(1);
  274. }
  275. int
  276. main(int argc, char **argv)
  277. {
  278. if (argc != 2)
  279. usage();
  280. if (geteuid())
  281. errx(1, "need root privileges");
  282. char *device_name = argv[1];
  283. char *dev = find_input_device(device_name);
  284. if (dev == NULL)
  285. errx(1, "Unable to find input device for %s", device_name);
  286. handle_input(dev);
  287. return 0;
  288. }