Skip to content

Commit bd4259a

Browse files
committed
WLProxy : connect to real wayland on demand
1 parent 8493a03 commit bd4259a

1 file changed

Lines changed: 57 additions & 39 deletions

File tree

‎src/javaforce/linux/wl/WLProxy.java‎

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,20 @@
1616
public class WLProxy {
1717
public static boolean debug = false;
1818

19-
private String real_socket_addr;
20-
private UnixSocket real_socket; //wayland-0
2119
private String proxy_socket_addr;
2220
private UnixSocket proxy_socket; //wayland-99
2321
private boolean active;
2422
private Server server;
23+
private String real_wayland_display;
2524

2625
private static int log = 99;
2726

2827
public boolean start(String real_wayland_display) {
2928
if (debug) {
3029
JFLog.init(log, System.getenv("HOME") + "/wlproxy.log", true);
3130
}
32-
String real_path = System.getenv("XDG_RUNTIME_DIR");
33-
if (real_path == null || real_wayland_display == null) {
34-
JFLog.log(log, "WLProxy:socket not found");
35-
return false;
36-
}
37-
if (!real_path.endsWith("/")) {
38-
real_path += "/";
39-
}
40-
String temp_path = real_path + "wayland-90";
41-
real_path += real_wayland_display;
42-
JFLog.log(log, "WLProxy:server.socket=" + real_path);
4331

44-
try {
45-
real_socket_addr = real_path;
46-
real_socket = new UnixSocket();
47-
if (!real_socket.open()) throw new Exception("Unable to alloc unix socket");
48-
real_socket.bind(temp_path); //maybe not be necessary
49-
if (!real_socket.connect(real_path)) throw new Exception("Unable to connect to real wayland socket");
50-
} catch (Exception e) {
51-
JFLog.log(log, e);
52-
if (real_socket != null) {
53-
try {real_socket.close();} catch (Exception e2) {}
54-
}
55-
real_socket = null;
56-
return false;
57-
}
32+
this.real_wayland_display = real_wayland_display;
5833

5934
String proxy_path = System.getenv("XDG_RUNTIME_DIR");
6035
String proxy_wayland_display = "wayland-99";
@@ -93,10 +68,6 @@ public boolean start(String real_wayland_display) {
9368

9469
public void stop() {
9570
active = false;
96-
if (real_socket != null) {
97-
try { real_socket.close(); } catch (Exception e) {}
98-
real_socket = null;
99-
}
10071
if (proxy_socket != null) {
10172
try { proxy_socket.close(); } catch (Exception e) {}
10273
proxy_socket = null;
@@ -109,28 +80,75 @@ public void setLog(int log) {
10980
}
11081

11182
private ArrayList<Session> sessions = new ArrayList<>();
83+
private Object lock = new Object();
84+
85+
private class Session extends Thread {
86+
87+
public String real_socket_addr;
88+
public UnixSocket real_socket; //wayland-0
89+
90+
public UnixSocket client;
11291

113-
private class Session {
114-
UnixSocket client;
115-
Reader client_proxy;
116-
Reader proxy_client;
92+
public Reader client_proxy;
93+
public Reader proxy_client;
94+
95+
public void run() {
96+
try { client_proxy.join(); } catch (Exception e) {}
97+
try { proxy_client.join(); } catch (Exception e) {}
98+
synchronized (lock) {
99+
sessions.remove(this);
100+
}
101+
}
117102
}
118103

119104
public class Server extends Thread {
120105
public void run() {
106+
int session_counter = 100;
121107
while (active) {
122108
try {
123109
UnixSocket client = proxy_socket.accept();
124110
if (client == null) {
125111
continue;
126112
}
113+
127114
Session session = new Session();
115+
116+
String real_path = System.getenv("XDG_RUNTIME_DIR");
117+
if (real_path == null || real_wayland_display == null) {
118+
JFLog.log(log, "WLProxy:socket not found");
119+
continue;
120+
}
121+
if (!real_path.endsWith("/")) {
122+
real_path += "/";
123+
}
124+
String temp_path = real_path + "wayland-" + (session_counter++);
125+
real_path += real_wayland_display;
126+
JFLog.log(log, "WLProxy:server.socket=" + real_path);
127+
128+
try {
129+
session.real_socket_addr = real_path;
130+
session.real_socket = new UnixSocket();
131+
if (!session.real_socket.open()) throw new Exception("Unable to alloc unix socket");
132+
session.real_socket.bind(temp_path); //maybe not be necessary
133+
if (!session.real_socket.connect(real_path)) throw new Exception("Unable to connect to real wayland socket");
134+
} catch (Exception e) {
135+
JFLog.log(log, e);
136+
if (session.real_socket != null) {
137+
try {session.real_socket.close();} catch (Exception e2) {}
138+
}
139+
session.real_socket = null;
140+
continue;
141+
}
142+
128143
session.client = client;
129-
session.client_proxy = new Reader('>', client, real_socket);
144+
session.client_proxy = new Reader('>', session.client, session.real_socket);
130145
session.client_proxy.start();
131-
session.proxy_client = new Reader('<', real_socket, client);
146+
session.proxy_client = new Reader('<', session.real_socket, session.client);
132147
session.proxy_client.start();
133-
sessions.add(session);
148+
synchronized (lock) {
149+
sessions.add(session);
150+
}
151+
session.start();
134152
} catch (Exception e) {
135153
JFLog.log(log, e);
136154
}
@@ -217,7 +235,7 @@ public void run() {
217235
JFLog.log(log, dir + ":write:" + data_len[0] + "," + fds_len[0]);
218236
}
219237
if (!write) {
220-
JFLog.log(log, "WLProxy:write() failed");
238+
throw new Exception(dir + ":WLProxy:write() failed");
221239
}
222240
if (fds_len[0] > 0) {
223241
//close fds received after they have been transferred

0 commit comments

Comments
 (0)