This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 144
/
termux-x11.c
90 lines (75 loc) · 1.54 KB
/
termux-x11.c
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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <wayland-client.h>
#ifndef TERMUX_PREFIX
# define TERMUX_PREFIX "/data/data/com.termux/files/usr"
#endif
#ifndef TERMUX_X11_DIR
# define TERMUX_X11_DIR "/data/data/com.termux.x11/"
#endif
int dir_exists(const char *dir) {
struct stat sb;
if (stat(dir, &sb) == 0 && S_ISDIR(sb.st_mode))
return 1;
return 0;
}
int connection_exists(void) {
struct wl_display *d = wl_display_connect(NULL);
if (d != NULL) {
wl_display_disconnect(d);
return 1;
}
return 0;
}
int start_server(char *argv[]) {
switch(fork()) {
case -1:
// Error
perror("fork");
return -1;
break;
case 0:
// Child
execv(argv[0], argv);
perror("execv");
exit(1);
return -1;
default:
//Parent
return 0;
}
// Should never reach this
return -1;
}
void start_xwayland(char *argv[]) {
argv[0] = TERMUX_PREFIX "/bin/Xwayland";
execv(argv[0], argv);
perror("execv");
}
int main(int argc, char *argv[]) {
if (!dir_exists(TERMUX_X11_DIR)) {
printf("Termux:X11 is not installed\n");
return 1;
}
char *server_argv[] = {TERMUX_PREFIX "/bin/am", "start", "-n", "com.termux.x11/.MainActivity", NULL};
if (!connection_exists()) {
if (start_server(server_argv) == -1) {
printf("Error starting Termux:X11\n");
return 1;
}
}
int e = 0, i = 0;
while(i++ < 10) {
if ((e = connection_exists())) {
start_xwayland(argv);
break;
}
sleep(1);
}
printf("Failed starting Termux:X11: timeout\n");
return 1;
}