Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions app/src/main/runtime/display/connector/XOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,25 @@ public void setAncillaryFd(int ancillaryFd) {
}

public void writeByte(byte value) {
if (buffer == null) return;
ensureSpaceIsAvailable(1);
buffer.put(value);
}

public void writeShort(short value) {
if (buffer == null) return;
ensureSpaceIsAvailable(2);
buffer.putShort(value);
}

public void writeInt(int value) {
if (buffer == null) return;
ensureSpaceIsAvailable(4);
buffer.putInt(value);
}

public void writeLong(long value) {
if (buffer == null) return;
ensureSpaceIsAvailable(8);
buffer.putLong(value);
}
Expand All @@ -57,6 +61,7 @@ public void writeFP3232(double value) {
}

public void writeString8(String str) {
if (buffer == null) return;
byte[] bytes = str.getBytes(XServer.LATIN1_CHARSET);
int length = -bytes.length & 3;
ensureSpaceIsAvailable(bytes.length + length);
Expand All @@ -69,11 +74,13 @@ public void write(byte[] data) {
}

public void write(byte[] data, int offset, int length) {
if (buffer == null) return;
ensureSpaceIsAvailable(length);
buffer.put(data, offset, length);
}

public void write(ByteBuffer data) {
if (buffer == null) return;
ensureSpaceIsAvailable(data.remaining());
buffer.put(data);
}
Expand All @@ -83,6 +90,7 @@ public void writePad(int length) {
}

private void flush() throws IOException {
if (buffer == null) return;
if (buffer.position() != 0) {
buffer.flip();

Expand Down Expand Up @@ -124,9 +132,19 @@ public void close() throws IOException {
}
}

// Writers hold `lock` for the whole event write; releasing under the same lock
// means the buffer can never be freed mid-write, and later writes see null and
// discard (dead connection).
public void release() {
XInputStream.freeDirectBuffer(buffer);
buffer = null;
lock.lock();
try {
if (buffer != null) {
XInputStream.freeDirectBuffer(buffer);
buffer = null;
}
} finally {
lock.unlock();
}
}

public void writeSuccessReply(int sequenceNumber, int replyLength) throws IOException {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/runtime/display/xserver/XClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.winlator.cmod.runtime.display.connector.XInputStream;
import com.winlator.cmod.runtime.display.connector.XOutputStream;
import com.winlator.cmod.runtime.display.xserver.events.Event;
import com.winlator.cmod.runtime.display.xserver.extensions.XInput2Extension;
import com.winlator.cmod.shared.android.RefreshRateUtils;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -92,6 +93,9 @@ public void freeResources() {
eventListeners.keyAt(i).removeEventListener(eventListeners.removeAt(i));
}

XInput2Extension xInput2Extension = xServer.getExtension(XInput2Extension.MAJOR_OPCODE);
if (xInput2Extension != null) xInput2Extension.onClientDisconnected(this);

xServer.windowManager.removeOnResourceLifecycleListener(this);
xServer.pixmapManager.removeOnResourceLifecycleListener(this);
xServer.graphicsContextManager.removeOnResourceLifecycleListener(this);
Expand Down