summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorDavid Blajda <blajda@hotmail.com>2020-01-16 01:49:03 +0000
committerDavid Blajda <blajda@hotmail.com>2020-01-16 01:49:03 +0000
commit888ef30c1dcecca20a67d72df37ec58727f437b4 (patch)
treed1e622a0835a69c90aab40fa53d9d9e8374ba1c4 /main.c
First attempt
Diffstat (limited to 'main.c')
-rw-r--r--main.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..850e6c3
--- /dev/null
+++ b/main.c
@@ -0,0 +1,86 @@
+#include <libudev.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <stdio.h>
+#include <sys/epoll.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <signal.h>
+#include <errno.h>
+
+
+#define EPOLL_EVENTS 8
+
+/*Open a connection with the current wayland compositor if it exists
+ * A fd is provided which can be polled. When polling the fd returns an error
+ * send a signal to any children and terminate.
+ */
+
+bool TERMINATE = false;
+
+int fork_and_wait() {
+ pid_t pid = fork();
+ assert(pid != -1);
+ if(pid == 0) {
+ /*There's a race condition with adb...*/
+ sleep(2);
+ return execlp("scrcpy", "scrcpy", (char *) 0);
+ }
+ int status;
+ pid_t _pid;
+ do {
+ _pid = waitpid(pid, &status, WNOHANG);
+ } while (pid != _pid && !TERMINATE);
+ int exit_status = WEXITSTATUS(status);
+ return exit_status;
+}
+
+void handle_hup(int sig) {
+ printf("poll-phone: %d\n", sig);
+ signal(sig, SIG_IGN);
+ kill(0, SIGHUP);
+ TERMINATE = true;
+}
+
+/*Refactor this so we poll the signal handlers*/
+
+int main(int argc, char **argv) {
+
+ struct epoll_event events[EPOLL_EVENTS];
+ int epfd = epoll_create(EPOLL_EVENTS);
+ struct udev *_udev = udev_new();
+ assert(_udev != NULL);
+ struct udev_monitor *_monitor = udev_monitor_new_from_netlink(_udev, "udev");
+ assert(_monitor != NULL);
+ udev_monitor_enable_receiving(_monitor);
+ int fd = udev_monitor_get_fd(_monitor);
+
+ struct epoll_event event;
+ event.events = EPOLLIN;
+ epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
+
+ signal(SIGTERM, handle_hup);
+ signal(SIGINT, handle_hup);
+
+ fork_and_wait();
+ while(!TERMINATE) {
+ int state = epoll_wait(epfd, &events[0], EPOLL_EVENTS, -1);
+ if(state == EINTR) { break; }
+ else { exit(-1); }
+
+ struct udev_device *device = udev_monitor_receive_device(_monitor);
+ const char *action = udev_device_get_action(device);
+ udev_device_unref(device);
+
+ if(!strcmp(action, "bind")) {
+ fork_and_wait();
+ }
+ }
+
+ udev_monitor_unref(_monitor);
+ udev_unref(_udev);
+ return 0;
+}