Notify of pokes and private messages on the desktop
While none of the client's windows has the focus, a poke or a private message shows a desktop notification, which the desktop's notification centre keeps. A private conversation keeps one notification, updated as messages come; clicking it brings the window forward with that chat open, and they all go once a window of the client is in front again. On Linux they go to the freedesktop.org notification service, spoken to over the session bus by a small D-Bus client of our own on Java's Unix sockets, so no library or native code is involved; Plasma also takes a reply typed into the notification. Elsewhere they are AWT tray messages, which Windows and macOS put in their notification centres. The options can turn them off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,215 @@
|
||||
package com.ts3client.notify.desktop;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.net.StandardProtocolFamily;
|
||||
import java.net.UnixDomainSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* A connection to the user's D-Bus session bus, spoken directly over its Unix socket:
|
||||
* method calls and the signals a match rule lets through, which is all a client of a
|
||||
* desktop service needs. Signals and replies are read on a thread of its own.
|
||||
*/
|
||||
final class DBusConnection implements AutoCloseable {
|
||||
|
||||
private static final String BUS = "org.freedesktop.DBus";
|
||||
private static final long CALL_TIMEOUT_MS = 3_000;
|
||||
|
||||
/** A signal from the bus. */
|
||||
interface SignalHandler {
|
||||
void signal(String iface, String member, List<Object> args);
|
||||
}
|
||||
|
||||
private final SocketChannel channel;
|
||||
private final AtomicLong serials = new AtomicLong();
|
||||
private final Map<Long, CompletableFuture<List<Object>>> pending = new ConcurrentHashMap<>();
|
||||
private volatile SignalHandler signals = (iface, member, args) -> { };
|
||||
private volatile boolean closed;
|
||||
|
||||
private DBusConnection(SocketChannel channel) {
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
/** Connects to the session bus named by {@code DBUS_SESSION_BUS_ADDRESS}, or the usual socket. */
|
||||
static DBusConnection session() throws IOException {
|
||||
Path socket = sessionSocket();
|
||||
if (socket == null) throw new IOException("No D-Bus session bus socket");
|
||||
SocketChannel channel = SocketChannel.open(StandardProtocolFamily.UNIX);
|
||||
try {
|
||||
channel.connect(UnixDomainSocketAddress.of(socket));
|
||||
authenticate(channel);
|
||||
DBusConnection bus = new DBusConnection(channel);
|
||||
Thread reader = new Thread(bus::readLoop, "dbus-reader");
|
||||
reader.setDaemon(true);
|
||||
reader.start();
|
||||
bus.call(BUS, "/org/freedesktop/DBus", BUS, "Hello", "");
|
||||
return bus;
|
||||
} catch (IOException | RuntimeException e) {
|
||||
channel.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private static Path sessionSocket() {
|
||||
String address = System.getenv("DBUS_SESSION_BUS_ADDRESS");
|
||||
if (address != null) {
|
||||
for (String entry : address.split(";")) {
|
||||
if (!entry.startsWith("unix:")) continue;
|
||||
for (String pair : entry.substring(5).split(",")) {
|
||||
// An abstract socket (unix:abstract=) is out of reach of Java's Unix sockets.
|
||||
if (pair.startsWith("path=")) return Path.of(unescape(pair.substring(5)));
|
||||
}
|
||||
}
|
||||
}
|
||||
String runtime = System.getenv("XDG_RUNTIME_DIR");
|
||||
if (runtime == null) return null;
|
||||
Path socket = Path.of(runtime, "bus");
|
||||
return Files.exists(socket) ? socket : null;
|
||||
}
|
||||
|
||||
/** Undoes the %xx escaping of D-Bus addresses. */
|
||||
private static String unescape(String value) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
char c = value.charAt(i);
|
||||
if (c == '%' && i + 2 < value.length()) {
|
||||
out.append((char) Integer.parseInt(value.substring(i + 1, i + 3), 16));
|
||||
i += 2;
|
||||
} else {
|
||||
out.append(c);
|
||||
}
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
/** The EXTERNAL mechanism: the bus checks the socket's peer credentials against our uid. */
|
||||
private static void authenticate(SocketChannel channel) throws IOException {
|
||||
Object uid = Files.getAttribute(Path.of("/proc/self"), "unix:uid");
|
||||
String hexUid = HexFormat.of().formatHex(uid.toString().getBytes(StandardCharsets.US_ASCII));
|
||||
write(channel, "\0AUTH EXTERNAL " + hexUid + "\r\n");
|
||||
String reply = readLine(channel);
|
||||
if (!reply.startsWith("OK ")) throw new IOException("D-Bus refused authentication: " + reply);
|
||||
write(channel, "BEGIN\r\n");
|
||||
}
|
||||
|
||||
private static void write(SocketChannel channel, String line) throws IOException {
|
||||
ByteBuffer b = ByteBuffer.wrap(line.getBytes(StandardCharsets.US_ASCII));
|
||||
while (b.hasRemaining()) channel.write(b);
|
||||
}
|
||||
|
||||
private static String readLine(SocketChannel channel) throws IOException {
|
||||
StringBuilder line = new StringBuilder();
|
||||
ByteBuffer one = ByteBuffer.allocate(1);
|
||||
while (!line.toString().endsWith("\r\n")) {
|
||||
one.clear();
|
||||
if (channel.read(one) < 0) throw new EOFException("D-Bus closed during authentication");
|
||||
line.append((char) one.get(0));
|
||||
}
|
||||
return line.substring(0, line.length() - 2);
|
||||
}
|
||||
|
||||
void onSignal(SignalHandler handler) {
|
||||
this.signals = handler;
|
||||
}
|
||||
|
||||
/** Lets the bus route the signals a match rule describes to this connection. */
|
||||
void addMatch(String rule) throws IOException {
|
||||
call(BUS, "/org/freedesktop/DBus", BUS, "AddMatch", "s", rule);
|
||||
}
|
||||
|
||||
/** Calls a method and waits for its reply's arguments. */
|
||||
List<Object> call(String destination, String path, String iface, String member, String signature,
|
||||
Object... args) throws IOException {
|
||||
long serial = serials.incrementAndGet();
|
||||
CompletableFuture<List<Object>> reply = new CompletableFuture<>();
|
||||
pending.put(serial, reply);
|
||||
try {
|
||||
byte[] message = DBusWire.methodCall(serial, destination, path, iface, member, signature, args);
|
||||
synchronized (channel) {
|
||||
ByteBuffer b = ByteBuffer.wrap(message);
|
||||
while (b.hasRemaining()) channel.write(b);
|
||||
}
|
||||
return reply.get(CALL_TIMEOUT_MS, TimeUnit.MILLISECONDS);
|
||||
} catch (ExecutionException e) {
|
||||
throw new IOException(e.getCause().getMessage(), e.getCause());
|
||||
} catch (TimeoutException e) {
|
||||
throw new IOException(member + " got no reply", e);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IOException("Interrupted", e);
|
||||
} finally {
|
||||
pending.remove(serial);
|
||||
}
|
||||
}
|
||||
|
||||
private void readLoop() {
|
||||
try {
|
||||
ByteBuffer header = ByteBuffer.allocate(DBusWire.FIXED_HEADER);
|
||||
while (!closed) {
|
||||
header.clear();
|
||||
readFully(header);
|
||||
ByteBuffer message = ByteBuffer.allocate(DBusWire.messageLength(header));
|
||||
message.put(header.flip());
|
||||
readFully(message);
|
||||
dispatch(DBusWire.parse(message.array()));
|
||||
}
|
||||
} catch (IOException | RuntimeException e) {
|
||||
if (!closed) System.err.println("D-Bus connection lost: " + e);
|
||||
} finally {
|
||||
closed = true;
|
||||
pending.values().forEach(f -> f.completeExceptionally(new IOException("D-Bus connection closed")));
|
||||
}
|
||||
}
|
||||
|
||||
private void readFully(ByteBuffer b) throws IOException {
|
||||
while (b.hasRemaining()) {
|
||||
if (channel.read(b) < 0) throw new EOFException("D-Bus closed the connection");
|
||||
}
|
||||
}
|
||||
|
||||
private void dispatch(DBusWire.Message m) {
|
||||
switch (m.type()) {
|
||||
case DBusWire.METHOD_RETURN -> {
|
||||
CompletableFuture<List<Object>> reply = pending.get(m.replySerial());
|
||||
if (reply != null) reply.complete(m.body());
|
||||
}
|
||||
case DBusWire.ERROR -> {
|
||||
CompletableFuture<List<Object>> reply = pending.get(m.replySerial());
|
||||
String detail = m.body().isEmpty() ? "" : ": " + m.body().getFirst();
|
||||
if (reply != null) reply.completeExceptionally(new IOException(m.string(DBusWire.FIELD_ERROR_NAME) + detail));
|
||||
}
|
||||
case DBusWire.SIGNAL -> {
|
||||
try {
|
||||
signals.signal(m.string(DBusWire.FIELD_INTERFACE), m.string(DBusWire.FIELD_MEMBER), m.body());
|
||||
} catch (RuntimeException e) {
|
||||
System.err.println("D-Bus signal handler failed: " + e);
|
||||
}
|
||||
}
|
||||
default -> { }
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
closed = true;
|
||||
try {
|
||||
channel.close();
|
||||
} catch (IOException ignored) {
|
||||
// Nothing left to release.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
package com.ts3client.notify.desktop;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The D-Bus wire format, for the handful of types a notification service speaks:
|
||||
* basic types, arrays, structs, dictionaries and variants.
|
||||
*
|
||||
* <p>Values map to Java as: {@code y} Integer, {@code b} Boolean, {@code i} Integer,
|
||||
* {@code u} Long, {@code s o g} String, {@code a} List (or Map for a dictionary),
|
||||
* a struct Object[], a variant the value it holds.
|
||||
*/
|
||||
final class DBusWire {
|
||||
|
||||
static final int METHOD_CALL = 1;
|
||||
static final int METHOD_RETURN = 2;
|
||||
static final int ERROR = 3;
|
||||
static final int SIGNAL = 4;
|
||||
|
||||
static final int FIELD_PATH = 1;
|
||||
static final int FIELD_INTERFACE = 2;
|
||||
static final int FIELD_MEMBER = 3;
|
||||
static final int FIELD_ERROR_NAME = 4;
|
||||
static final int FIELD_REPLY_SERIAL = 5;
|
||||
static final int FIELD_DESTINATION = 6;
|
||||
static final int FIELD_SIGNATURE = 8;
|
||||
|
||||
/** Everything before the header fields: byte order, type, flags, version and three lengths. */
|
||||
static final int FIXED_HEADER = 16;
|
||||
|
||||
private DBusWire() {
|
||||
}
|
||||
|
||||
/** A message as read off the bus. */
|
||||
record Message(int type, long serial, Map<Integer, Object> fields, List<Object> body) {
|
||||
|
||||
String string(int field) {
|
||||
Object value = fields.get(field);
|
||||
return value instanceof String s ? s : null;
|
||||
}
|
||||
|
||||
long replySerial() {
|
||||
return fields.get(FIELD_REPLY_SERIAL) instanceof Long l ? l : -1;
|
||||
}
|
||||
}
|
||||
|
||||
/** Builds a method call; the body is written after the header, aligned to 8 as the format wants. */
|
||||
static byte[] methodCall(long serial, String destination, String path, String iface, String member,
|
||||
String signature, Object... args) {
|
||||
Writer body = new Writer();
|
||||
body.values(signature, args);
|
||||
|
||||
Writer out = new Writer();
|
||||
out.put('l');
|
||||
out.put(METHOD_CALL);
|
||||
out.put(0);
|
||||
out.put(1);
|
||||
out.u32(body.size());
|
||||
out.u32(serial);
|
||||
out.array(8, () -> {
|
||||
out.field(FIELD_PATH, "o", path);
|
||||
out.field(FIELD_DESTINATION, "s", destination);
|
||||
out.field(FIELD_INTERFACE, "s", iface);
|
||||
out.field(FIELD_MEMBER, "s", member);
|
||||
if (!signature.isEmpty()) out.field(FIELD_SIGNATURE, "g", signature);
|
||||
});
|
||||
out.align(8);
|
||||
out.bytes(body.toBytes());
|
||||
return out.toBytes();
|
||||
}
|
||||
|
||||
/** @return the length of the whole message whose fixed header this is */
|
||||
static int messageLength(ByteBuffer fixedHeader) {
|
||||
ByteBuffer b = fixedHeader.duplicate().order(order(fixedHeader.get(0)));
|
||||
int body = b.getInt(4);
|
||||
int fields = b.getInt(12);
|
||||
return pad8(FIXED_HEADER + fields) + body;
|
||||
}
|
||||
|
||||
static Message parse(byte[] data) {
|
||||
Reader r = new Reader(ByteBuffer.wrap(data).order(order(data[0])));
|
||||
r.skip(1);
|
||||
int type = r.buf.get() & 0xff;
|
||||
r.skip(2);
|
||||
int bodyLength = r.buf.getInt();
|
||||
long serial = Integer.toUnsignedLong(r.buf.getInt());
|
||||
|
||||
Map<Integer, Object> fields = new java.util.HashMap<>();
|
||||
for (Object field : (List<?>) r.read("a(yv)")) {
|
||||
Object[] pair = (Object[]) field;
|
||||
fields.put((Integer) pair[0], pair[1]);
|
||||
}
|
||||
r.align(8);
|
||||
String signature = fields.get(FIELD_SIGNATURE) instanceof String s ? s : "";
|
||||
List<Object> body = new ArrayList<>();
|
||||
int end = r.buf.position() + bodyLength;
|
||||
for (int i = 0; i < signature.length() && r.buf.position() < end; ) {
|
||||
int next = typeEnd(signature, i);
|
||||
body.add(r.read(signature.substring(i, next)));
|
||||
i = next;
|
||||
}
|
||||
return new Message(type, serial, fields, body);
|
||||
}
|
||||
|
||||
private static ByteOrder order(byte marker) {
|
||||
return marker == 'B' ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
|
||||
}
|
||||
|
||||
private static int pad8(int n) {
|
||||
return (n + 7) & ~7;
|
||||
}
|
||||
|
||||
/** @return the index just past the single complete type starting at {@code start} */
|
||||
static int typeEnd(String signature, int start) {
|
||||
char c = signature.charAt(start);
|
||||
if (c == 'a') return typeEnd(signature, start + 1);
|
||||
if (c == '(' || c == '{') {
|
||||
char close = c == '(' ? ')' : '}';
|
||||
int i = start + 1;
|
||||
while (signature.charAt(i) != close) i = typeEnd(signature, i);
|
||||
return i + 1;
|
||||
}
|
||||
return start + 1;
|
||||
}
|
||||
|
||||
static int alignment(char type) {
|
||||
return switch (type) {
|
||||
case 'y', 'g', 'v' -> 1;
|
||||
case 'n', 'q' -> 2;
|
||||
case 'x', 't', 'd', '(', '{' -> 8;
|
||||
default -> 4;
|
||||
};
|
||||
}
|
||||
|
||||
/** Marshals values; positions count from the start of what it writes. */
|
||||
static final class Writer {
|
||||
private byte[] buf = new byte[256];
|
||||
private int pos;
|
||||
|
||||
int size() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
byte[] toBytes() {
|
||||
return Arrays.copyOf(buf, pos);
|
||||
}
|
||||
|
||||
void put(int b) {
|
||||
if (pos == buf.length) buf = Arrays.copyOf(buf, buf.length * 2);
|
||||
buf[pos++] = (byte) b;
|
||||
}
|
||||
|
||||
void bytes(byte[] b) {
|
||||
for (byte x : b) put(x);
|
||||
}
|
||||
|
||||
void align(int n) {
|
||||
while (pos % n != 0) put(0);
|
||||
}
|
||||
|
||||
void u32(long v) {
|
||||
align(4);
|
||||
for (int i = 0; i < 4; i++) put((int) (v >>> (8 * i)));
|
||||
}
|
||||
|
||||
private void setU32(int at, long v) {
|
||||
for (int i = 0; i < 4; i++) buf[at + i] = (byte) (v >>> (8 * i));
|
||||
}
|
||||
|
||||
void string(String s) {
|
||||
byte[] b = s.getBytes(StandardCharsets.UTF_8);
|
||||
u32(b.length);
|
||||
bytes(b);
|
||||
put(0);
|
||||
}
|
||||
|
||||
void signature(String s) {
|
||||
byte[] b = s.getBytes(StandardCharsets.US_ASCII);
|
||||
put(b.length);
|
||||
bytes(b);
|
||||
put(0);
|
||||
}
|
||||
|
||||
/** An array; the length leaves out the padding before its first element. */
|
||||
void array(int elementAlignment, Runnable elements) {
|
||||
u32(0);
|
||||
int lengthAt = pos - 4;
|
||||
align(elementAlignment);
|
||||
int start = pos;
|
||||
elements.run();
|
||||
setU32(lengthAt, pos - start);
|
||||
}
|
||||
|
||||
void field(int code, String signature, Object value) {
|
||||
align(8);
|
||||
put(code);
|
||||
signature(signature);
|
||||
value(signature, value);
|
||||
}
|
||||
|
||||
void values(String signature, Object... values) {
|
||||
int v = 0;
|
||||
for (int i = 0; i < signature.length(); v++) {
|
||||
int next = typeEnd(signature, i);
|
||||
value(signature.substring(i, next), values[v]);
|
||||
i = next;
|
||||
}
|
||||
}
|
||||
|
||||
void value(String type, Object value) {
|
||||
char c = type.charAt(0);
|
||||
align(alignment(c));
|
||||
switch (c) {
|
||||
case 'y' -> put(((Number) value).intValue());
|
||||
case 'b' -> u32((Boolean) value ? 1 : 0);
|
||||
case 'i' -> u32(((Number) value).intValue() & 0xffffffffL);
|
||||
case 'u' -> u32(((Number) value).longValue());
|
||||
case 's', 'o' -> string((String) value);
|
||||
case 'g' -> signature((String) value);
|
||||
case 'v' -> {
|
||||
String inner = variantType(value);
|
||||
signature(inner);
|
||||
value(inner, value);
|
||||
}
|
||||
case 'a' -> {
|
||||
String element = type.substring(1);
|
||||
array(alignment(element.charAt(0)), () -> {
|
||||
if (element.charAt(0) == '{') {
|
||||
String key = element.substring(1, typeEnd(element, 1));
|
||||
String val = element.substring(1 + key.length(), element.length() - 1);
|
||||
for (Map.Entry<?, ?> e : ((Map<?, ?>) value).entrySet()) {
|
||||
align(8);
|
||||
value(key, e.getKey());
|
||||
value(val, e.getValue());
|
||||
}
|
||||
} else {
|
||||
for (Object item : (List<?>) value) value(element, item);
|
||||
}
|
||||
});
|
||||
}
|
||||
default -> throw new IllegalArgumentException("Cannot write D-Bus type " + type);
|
||||
}
|
||||
}
|
||||
|
||||
private static String variantType(Object value) {
|
||||
if (value instanceof String) return "s";
|
||||
if (value instanceof Boolean) return "b";
|
||||
if (value instanceof Byte) return "y";
|
||||
if (value instanceof Integer) return "i";
|
||||
if (value instanceof Long) return "u";
|
||||
throw new IllegalArgumentException("No D-Bus type for " + value.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
/** Unmarshals values; alignment counts from the start of the message. */
|
||||
static final class Reader {
|
||||
final ByteBuffer buf;
|
||||
|
||||
Reader(ByteBuffer buf) {
|
||||
this.buf = buf;
|
||||
}
|
||||
|
||||
void skip(int n) {
|
||||
buf.position(buf.position() + n);
|
||||
}
|
||||
|
||||
void align(int n) {
|
||||
int p = buf.position();
|
||||
buf.position((p + n - 1) / n * n);
|
||||
}
|
||||
|
||||
Object read(String type) {
|
||||
char c = type.charAt(0);
|
||||
align(alignment(c));
|
||||
return switch (c) {
|
||||
case 'y' -> buf.get() & 0xff;
|
||||
case 'b' -> buf.getInt() != 0;
|
||||
case 'n' -> (int) buf.getShort();
|
||||
case 'q' -> buf.getShort() & 0xffff;
|
||||
case 'i' -> buf.getInt();
|
||||
case 'u' -> Integer.toUnsignedLong(buf.getInt());
|
||||
case 'x', 't' -> buf.getLong();
|
||||
case 'd' -> buf.getDouble();
|
||||
case 's', 'o' -> text(buf.getInt());
|
||||
case 'g' -> text(buf.get() & 0xff);
|
||||
case 'v' -> read((String) read("g"));
|
||||
case 'a' -> {
|
||||
int length = buf.getInt();
|
||||
String element = type.substring(1, typeEnd(type, 1));
|
||||
align(alignment(element.charAt(0)));
|
||||
int end = buf.position() + length;
|
||||
List<Object> items = new ArrayList<>();
|
||||
while (buf.position() < end) items.add(read(element));
|
||||
yield items;
|
||||
}
|
||||
case '(', '{' -> {
|
||||
List<Object> members = new ArrayList<>();
|
||||
for (int i = 1; i < type.length() - 1; ) {
|
||||
int next = typeEnd(type, i);
|
||||
members.add(read(type.substring(i, next)));
|
||||
i = next;
|
||||
}
|
||||
yield members.toArray();
|
||||
}
|
||||
default -> throw new IllegalArgumentException("Cannot read D-Bus type " + type);
|
||||
};
|
||||
}
|
||||
|
||||
private String text(int length) {
|
||||
byte[] b = new byte[length];
|
||||
buf.get(b);
|
||||
buf.get(); // the terminating NUL
|
||||
return new String(b, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.ts3client.notify.desktop;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* The desktop's own notifications — the ones its notification centre collects. Calls
|
||||
* may block on the desktop, so they belong off the event thread; the callbacks come
|
||||
* on a thread of the implementation's.
|
||||
*/
|
||||
public interface DesktopNotifications {
|
||||
|
||||
/**
|
||||
* @param title the headline, such as who a message is from
|
||||
* @param body plain text
|
||||
* @param onOpen run when the user clicks the notification
|
||||
* @param onReply given the text typed into the notification, where {@link #canReply()}; may be {@code null}
|
||||
*/
|
||||
record Notice(String title, String body, Runnable onOpen, Consumer<String> onReply) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a notice, in place of an earlier one still on screen.
|
||||
*
|
||||
* @param replaces what an earlier call returned, or 0
|
||||
* @return the notice's id for {@link #close} and later replacement, or 0 if it could not be shown
|
||||
*/
|
||||
int show(Notice notice, int replaces);
|
||||
|
||||
/** Takes a notice away, from the notification centre too. */
|
||||
void close(int id);
|
||||
|
||||
/** Whether a notice can take a reply typed into it. */
|
||||
boolean canReply();
|
||||
|
||||
void shutdown();
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.ts3client.notify.desktop;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Notifications through the freedesktop.org notification service, which every Linux
|
||||
* desktop (Plasma, GNOME, XFCE, a standalone daemon such as dunst) provides on the
|
||||
* session bus. Plasma also takes a reply typed into the notification.
|
||||
*/
|
||||
public final class FreedesktopNotifications implements DesktopNotifications {
|
||||
|
||||
private static final String SERVICE = "org.freedesktop.Notifications";
|
||||
private static final String PATH = "/org/freedesktop/Notifications";
|
||||
private static final String DEFAULT_ACTION = "default";
|
||||
/** Plasma's extension for a reply field in the notification. */
|
||||
private static final String REPLY_ACTION = "inline-reply";
|
||||
|
||||
private final DBusConnection bus;
|
||||
private final String appName;
|
||||
private final String desktopEntry;
|
||||
private final String icon;
|
||||
private final boolean markup;
|
||||
private final boolean reply;
|
||||
/** The notices on screen, by the id the service gave them. */
|
||||
private final Map<Long, Notice> shown = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* @param desktopEntry the name of the application's {@code .desktop} file, without the
|
||||
* extension; the desktop takes the application's name and icon from it
|
||||
* @param icon an icon file, for services that do not look at the desktop entry
|
||||
* @return the service, or {@code null} where there is none to reach
|
||||
*/
|
||||
public static FreedesktopNotifications connect(String appName, String desktopEntry, String icon) {
|
||||
DBusConnection bus = null;
|
||||
try {
|
||||
bus = DBusConnection.session();
|
||||
List<?> capabilities = (List<?>) bus.call(SERVICE, PATH, SERVICE, "GetCapabilities", "").getFirst();
|
||||
bus.addMatch("type='signal',interface='" + SERVICE + "'");
|
||||
return new FreedesktopNotifications(bus, appName, desktopEntry, icon, capabilities);
|
||||
} catch (IOException | RuntimeException e) {
|
||||
System.err.println("Desktop notifications unavailable: " + e.getMessage());
|
||||
if (bus != null) bus.close();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private FreedesktopNotifications(DBusConnection bus, String appName, String desktopEntry, String icon,
|
||||
List<?> capabilities) {
|
||||
this.bus = bus;
|
||||
this.appName = appName;
|
||||
this.desktopEntry = desktopEntry;
|
||||
this.icon = icon == null ? "" : icon;
|
||||
this.markup = capabilities.contains("body-markup");
|
||||
this.reply = capabilities.contains(REPLY_ACTION);
|
||||
bus.onSignal(this::signal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canReply() {
|
||||
return reply;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int show(Notice notice, int replaces) {
|
||||
List<String> actions = new ArrayList<>(List.of(DEFAULT_ACTION, "Open"));
|
||||
if (reply && notice.onReply() != null) actions.addAll(List.of(REPLY_ACTION, "Reply"));
|
||||
Map<String, Object> hints = new LinkedHashMap<>();
|
||||
hints.put("desktop-entry", desktopEntry);
|
||||
hints.put("category", "im.received");
|
||||
// The client plays the sound pack's own sound for whatever it notifies of.
|
||||
hints.put("suppress-sound", true);
|
||||
try {
|
||||
long id = (Long) bus.call(SERVICE, PATH, SERVICE, "Notify", "susssasa{sv}i",
|
||||
appName, (long) replaces, icon, notice.title(),
|
||||
markup ? escape(notice.body()) : notice.body(), actions, hints, -1).getFirst();
|
||||
shown.remove((long) replaces);
|
||||
shown.put(id, notice);
|
||||
return (int) id;
|
||||
} catch (IOException | RuntimeException e) {
|
||||
System.err.println("Cannot show a notification: " + e.getMessage());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(int id) {
|
||||
if (shown.remove((long) id) == null) return;
|
||||
try {
|
||||
bus.call(SERVICE, PATH, SERVICE, "CloseNotification", "u", (long) id);
|
||||
} catch (IOException e) {
|
||||
// Gone with the service; nothing to take away.
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
for (Long id : List.copyOf(shown.keySet())) close(id.intValue());
|
||||
bus.close();
|
||||
}
|
||||
|
||||
private void signal(String iface, String member, List<Object> args) {
|
||||
if (!SERVICE.equals(iface) || args.isEmpty() || !(args.getFirst() instanceof Long id)) return;
|
||||
switch (member) {
|
||||
case "ActionInvoked" -> {
|
||||
Notice notice = shown.get(id);
|
||||
if (notice != null && DEFAULT_ACTION.equals(args.get(1))) notice.onOpen().run();
|
||||
}
|
||||
case "NotificationReplied" -> {
|
||||
Notice notice = shown.get(id);
|
||||
if (notice != null && notice.onReply() != null) notice.onReply().accept((String) args.get(1));
|
||||
}
|
||||
case "NotificationClosed" -> shown.remove(id);
|
||||
default -> { }
|
||||
}
|
||||
}
|
||||
|
||||
/** The body is markup where the service says so; message text must not turn into any. */
|
||||
private static String escape(String text) {
|
||||
return text.replace("&", "&").replace("<", "<").replace(">", ">");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.ts3client.notify.desktop;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class DBusWireTest {
|
||||
|
||||
@Test
|
||||
void methodCallReadsBackAsWritten() {
|
||||
Map<String, Object> hints = new LinkedHashMap<>();
|
||||
hints.put("desktop-entry", "ts3j-client");
|
||||
hints.put("suppress-sound", true);
|
||||
byte[] message = DBusWire.methodCall(7, "org.freedesktop.Notifications", "/org/freedesktop/Notifications",
|
||||
"org.freedesktop.Notifications", "Notify", "susssasa{sv}i",
|
||||
"TS3J", 3L, "", "Bob", "Hi ž", List.of("default", "Open"), hints, -1);
|
||||
|
||||
assertEquals(message.length, DBusWire.messageLength(ByteBuffer.wrap(message, 0, DBusWire.FIXED_HEADER)));
|
||||
DBusWire.Message m = DBusWire.parse(message);
|
||||
assertEquals(DBusWire.METHOD_CALL, m.type());
|
||||
assertEquals(7, m.serial());
|
||||
assertEquals("Notify", m.string(DBusWire.FIELD_MEMBER));
|
||||
assertEquals("/org/freedesktop/Notifications", m.string(DBusWire.FIELD_PATH));
|
||||
assertEquals("TS3J", m.body().get(0));
|
||||
assertEquals(3L, m.body().get(1));
|
||||
assertEquals("Hi ž", m.body().get(4));
|
||||
assertEquals(List.of("default", "Open"), m.body().get(5));
|
||||
List<?> entries = (List<?>) m.body().get(6);
|
||||
assertArrayEquals(new Object[]{"desktop-entry", "ts3j-client"}, (Object[]) entries.get(0));
|
||||
assertArrayEquals(new Object[]{"suppress-sound", true}, (Object[]) entries.get(1));
|
||||
assertEquals(-1, m.body().get(7));
|
||||
}
|
||||
|
||||
@Test
|
||||
void completeTypesAreDelimited() {
|
||||
assertEquals(1, DBusWire.typeEnd("su", 0));
|
||||
assertEquals(5, DBusWire.typeEnd("a{sv}i", 0));
|
||||
assertEquals(5, DBusWire.typeEnd("a(yv)s", 0));
|
||||
assertEquals(3, DBusWire.typeEnd("aasu", 1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user