ADR-0013: Desktop data layer for Milestone 2: SQLite cache in tetherd, MPRIS, clipboard model
- Status: accepted
- Date: 2026-11-08
- Deciders: maintainer
- Tags:
linux,architecture,messages,media,clipboard
Context
Milestone 2 brings data that must outlive a window: message threads (thousands
of rows), contacts, a photo index, transfer history. ADR-0006 made tetherd the
only process that talks to the phone and D-Bus the only interface to UIs. The
questions are where the cache lives, how large datasets cross D-Bus, how phone
media reaches desktop media keys, and how clipboard sync can work on both X11
and Wayland.
Facts:
- A phone with years of SMS has tens of thousands of messages; the first sync must survive daemon restarts and be resumable per thread.
- D-Bus is fine for paged method calls of a few hundred dicts and for change signals; it is wrong for streaming megabytes (attachments, photos).
- MPRIS (
org.mpris.MediaPlayer2.*) is how every Linux desktop exposes media keys, lock-screen and panel controls. Exporting one player per phone makes phone playback controllable from existing applets without any Tether UI. - On X11 any client can watch and set the selection without focus (GDK uses XFixes). On Wayland a client reads the clipboard only when focused and can set it only with a recent input serial; no portal for a daemon exists on GNOME today.
Decision
- Cache in the daemon.
tetherdkeeps a per-device SQLite database (rusqlitewith the bundled SQLite) at$XDG_DATA_HOME/tether/devices/<id>/cache.dbwith tables for threads, messages, attachments, contacts, media items and transfers, plus desktop-local read state. Binary payloads (attachments, thumbnails, fetched media, contact photos) are files under$XDG_CACHE_HOME/tether/devices/<id>/…, referenced by path. The schema is versioned withPRAGMA user_versionand migrated on open. - Sync strategy. On connect the daemon requests conversations since its
newest known message, then pages missing threads oldest-newest in the
background (a few requests in flight), persisting as it goes; live
MessageEvents are applied immediately. Contacts are a delta sync keyed by(uid, version). The photo index is filled on demand when a client asks for items and refreshed onMediaChanged. - D-Bus shapes. Paged getters return
aa{sv}; changes are signals carrying the changed row (messages) or a hint to re-query (threads, contacts, media). Bulk bytes never cross D-Bus: methods return file paths in the daemon cache and clients copy them. - MPRIS in the daemon. For every connected phone with at least one media
session,
tetherdexportsorg.mpris.MediaPlayer2.tether_<id8>mirroring the active session and forwarding commands asMediaCommand;Volumemaps to themusicstream. - Clipboard. Phone → desktop: the daemon stores the text, emits
ClipboardReceivedand exposesLastClipboard; the GTK app (long-lived thanks to the tray) sets the desktop clipboard. On Wayland that succeeds only when the app can obtain a serial; otherwise the app posts a notification whose “Copy” action and the device page’s “Copy phone clipboard” button do it on user interaction. Desktop → phone: the GTK app watches the GDK clipboard (changedsignal; on X11 this works unfocused, on Wayland while focused) and callsSendClipboard; the app also offers an explicit “Send clipboard” action and the CLItether-cli clipboard send. Direction toggles live in the daemon config (ClipboardSync). Automatic phone → desktop sync remains an enhanced tier feature (M4). - Desktop notifications for messages. The daemon posts a freedesktop
notification for each new incoming message (sender name, snippet, “Reply”
action →
ReplyRequested-style signal handled by the GTK app) unless the thread is open in the app orMessageNotificationsis off. Incoming SMS also arrive as mirrored phone notifications; the daemon suppresses the mirror for the default SMS app’s package whilemessagesis active to avoid duplicates.
Consequences
Positive
- Messages, contacts and photos survive app restarts and are searchable offline; the first sync is incremental and restartable.
- Media keys, GNOME/Cinnamon media applets and
playerctlcontrol the phone. - One place (the daemon) holds state; the GTK app stays a thin client and a future Shell extension gets the same data.
Negative / risks
- SQLite adds a native dependency to
tetherd(bundled, no system package). - Wayland clipboard sync is best-effort and documented as such until a portal exists.
- Two copies of large media (daemon cache and wherever the user saves it); mitigated by a cache size cap (default 2 GiB, LRU) in a later milestone.
Alternatives considered
- Cache in the GTK app: loses data when the app is closed, duplicates work for every client. Rejected.
- Stream data over D-Bus (
hfile descriptors): workable but complex; paths in a daemon-owned cache are simpler and match how icons already work. - MPRIS in the GTK app: dies with the app and would require the app for media keys. Rejected.
- wl-clipboard / xclip subprocesses from the daemon: fragile across compositors and absent on minimal installs. Rejected as the primary path.
Amendment (2026-11-11): automatic two-way clipboard sync
User feedback during 0.2.0 testing asked for the clipboard to stay in sync both ways without pressing “Send”. Changes to the clipboard decision above:
- Phone → desktop is automatic when the user grants two permissions once
over adb:
READ_LOGSand theSYSTEM_ALERT_WINDOWapp-op. Android logsClipboardService: Denied/Denying clipboard access to <pkg>when it skips a background app’s clip listener; the app follows logcat for that line and starts a translucent activity that gains focus, reads the clip, sends it and finishes (the technique KDE Connect uses). Without the grants the Quick Settings tile, share sheet and notification action remain. This moves the “Clipboard phone to PC (automatic)” parity row from M4 to M2 as an enhanced-tier feature that needs only a one-time grant, not the shell helper (ADR-0007). - The daemon covers for a closed GTK app: when
org.tether.Tetheris not on the bus,tetherdsets the desktop clipboard withwl-copy/xclipand, on X11, polls the CLIPBOARD selection once a second to send desktop changes. Wayland still requires the focused app for desktop → phone. - The manual “Send clipboard” button and tray entry were removed from the GTK app.
Known costs: Android 13+ may show a “Tether pasted from your clipboard” toast for
each background read; the log line wording differs between AOSP and One UI, so
the watcher matches the common substring clipboard access to <pkg>.