summaryrefslogtreecommitdiff
path: root/main.c
blob: 960ee009c2e0f8454b293e73afd6e97f0f5f96e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#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>
#include <wayland-client.h>


#define EPOLL_EVENTS 8

enum app_event {
    EVENT_UDEV = 0,
    EVENT_WAYLAND = 1,
    EVENT_CHILD = 2,
};

enum app_state {
    STATE_POLLING,
    STATE_RUNNING,
    STATE_FINISHED,
};

/*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.
 */

struct poll_phone {
    enum app_state state;
    pid_t child;
    int pipe;
};

void app_abort(char *str) {
    fprintf(stderr, "%s\n", str); 
    exit(-1);
}

static struct poll_phone app;

void run_scrcpy() {
    assert(app.state != STATE_RUNNING);

    pid_t pid = fork();
    if(pid == 0) {
        signal(SIGTERM, SIG_DFL);
        signal(SIGINT, SIG_DFL);
        /*There's a race condition with adb...*/
        sleep(2);
        execlp("scrcpy", "scrcpy", (char *) 0);
        exit(-1);
    } else if(pid == -1) {
        app_abort("fork() error");
    }
    app.state = STATE_RUNNING;
    app.child = pid;
}

void handle_term(int sig) {
    signal(sig, SIG_IGN);
    app.state = STATE_FINISHED;
}

void handle_child(int sig) {
    char data[1] = {0};
    app.state = STATE_POLLING;
    if(write(app.pipe, data, 1) == -1) {
        app_abort("write failed");
    }
}

/*Refactor this so we poll the signal handlers*/

int main(int argc, char **argv) {

    int fileds[2];
    char buffer[128];
    if(pipe(fileds) == -1) {
        app_abort("Failled to create pipe.");
    }
    app.state = STATE_POLLING;
    app.pipe = fileds[1];
    app.child = -1;
    signal(SIGTERM, handle_term);
    signal(SIGINT, handle_term);
    signal(SIGCHLD, handle_child);

    struct epoll_event events[EPOLL_EVENTS];
    struct epoll_event event;

    struct wl_display *display = wl_display_connect(NULL);
    if(!display) {
        app_abort("Failled to connect to Wayland display.");
    }

    struct udev *_udev = udev_new();
    if(!_udev) {
        app_abort("Failed to allocate udev");
    }

    struct udev_monitor *_monitor = udev_monitor_new_from_netlink(_udev, "udev");
    if(!_monitor) {
        app_abort("Failed to allocate udev monitor");
    }
    udev_monitor_enable_receiving(_monitor);

    int epfd = epoll_create(EPOLL_EVENTS);
    if(epfd == -1) {
        app_abort("Failed to create epoll");
    }

    event.events = EPOLLIN;
    event.data.u32 = EVENT_UDEV;
    epoll_ctl(epfd, EPOLL_CTL_ADD, udev_monitor_get_fd(_monitor), &event);
    event.data.u32 = EVENT_WAYLAND;
    epoll_ctl(epfd, EPOLL_CTL_ADD, wl_display_get_fd(display), &event);
    event.data.u32 = EVENT_CHILD;
    epoll_ctl(epfd, EPOLL_CTL_ADD, fileds[0], &event);

    run_scrcpy();
    while(app.state != STATE_FINISHED) {
        int ready = epoll_wait(epfd, &events[0], EPOLL_EVENTS, -1);
        if(ready > 0) {
            for(int i = 0; i < ready; i++) {
                struct epoll_event *event = &events[i];
                switch(event->data.u32) {
                case EVENT_UDEV: {
                    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") && app.state == STATE_POLLING) {
                        run_scrcpy();
                    } 
                    break;
                    }
                case EVENT_WAYLAND:
                    if(wl_display_dispatch(display) == -1) {
                        app.state = STATE_FINISHED;
                    }
                    break;
                case EVENT_CHILD:
                    {
                    if(read(fileds[0], buffer, 128) == -1) {
                        app_abort("Failed to read");
                    }
                    if(app.state != STATE_FINISHED) {
                        app.state = STATE_POLLING;
                    }
                    wait(NULL);
                    app.child = -1;
                    }
                    break;
                }
            }
        }
    }

    kill(0, SIGTERM);
    if(app.child != -1) {
        pid_t r;
        do {
            r = wait(NULL);
        } while (r != app.child && r > 0);
    }

    close(fileds[0]);
    close(fileds[1]);
    udev_monitor_unref(_monitor);
    udev_unref(_udev);
    return 0;
}