Skip to content
Closed
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
54 changes: 44 additions & 10 deletions Documentation/Plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ timers for `schedule`.
| `LinuxWebKit` | `WebContent` over WPE WebKit, loaded at run time; the player's `WebView` view shows it (§8) |
| `LinuxCoreModel` | `LinuxModelStore`: the model store over CoreModel-SQLite, through AppRuntime's `HostModelStore` (§9) |
| `LinuxDevice` | `Device` from `/etc`, DMI, the device tree, and power and heat in sysfs; orientation and proximity from iio-sensor-proxy (§10) |
| `LinuxDBus` | `Bus`: the system and session buses, for the modules that talk to system services: method calls, properties and signals by match rule, delivered on the main loop (§11.1) |
| `LinuxDBus` | `Bus`: the system and session buses, for the modules that talk to system services: method calls, properties and signals by match rule, delivered on the main loop; and objects the host serves, with the names it owns (§11.1) |
| `LinuxLocation` | `LocationManager`, `Location` and `Heading` over GeoClue 2 (§11) |
| `LinuxDocuments` | `Documents`: AppRuntime's `HostDocuments` with the desktop portal's file chooser, and bookmarks (§12) |
| `LinuxMotion` | `MotionManager` and `Altimeter` over the kernel's IIO sensors in sysfs, and an unavailable `Pedometer` and `ActivityRecognizer` (§13) |
| `LinuxNFC` | `NFCReader`, `NFCTag`, `NDEFMessage` and `NDEFRecord` over neard (§14) |
| `LinuxMedia` | `MediaPlayback` over GStreamer's `playbin`, loaded at run time; the player's `VideoPlayer` view shows its pictures (§15) |
| `LinuxMedia` | `MediaPlayback` over GStreamer's `playbin`, loaded at run time; the player's `VideoPlayer` view shows its pictures, and MPRIS what's playing (§15) |
| `LinuxHostApp` | `main`: reads the app's keys, starts the host, and runs Cacao's SDL loop with one window showing the guest's first renderer |
| `LinuxPackager`, `linux-app` | The packager (§17, [Packager.md](Packager.md)) |

Expand Down Expand Up @@ -515,8 +515,8 @@ of its own, made when it first needs one:
### 11.1 D-Bus

`LinuxDBus` is what Bluetooth, Location, the device's orientation and
proximity (iio-sensor-proxy), NFC (neard) and the desktop portal
share. `Bus.system`
proximity (iio-sensor-proxy), NFC (neard), the desktop portal and what's
playing (MPRIS) share. `Bus.system`
and `Bus.session` are one connection each per process, made on first use
through PureSwift/DBus, which speaks D-Bus in Swift, without libdbus; its
values are `DBusMessageArgument`s, whose arrays and dictionaries carry
Expand All @@ -543,6 +543,15 @@ their element types.
`org.freedesktop.DBus.Local.Disconnected` signal; the next request
connects again, and the rules are added again. Without a bus, calls fail
with `NoServer`.
- **Serving**, for MPRIS: `export` serves an object's `BusInterface`s at a
path, through PureSwift/DBus's object server, which answers
`Introspectable`, `Peer` and `Properties` for it. A method call or a
property read or write comes in on the connection's threads, is posted to
the main loop, and its answer goes back, so handlers run on the main
thread like everything else. `requestName` asks for a well-known name
without queueing, and `releaseName` gives it up; `emit` and
`emitPropertiesChanged` send signals. Objects and names are served and
asked for again on a new connection.

## 12. Documents

Expand Down Expand Up @@ -746,11 +755,30 @@ gives it a player per `MediaPlayback`, `GStreamerEngine`.
no `VideoPlayer` case yet, so `HostNodeViews` draws it; DarwinUI's later
revisions draw `GLSurface`, `WebView` and `VideoPlayer` through
providers, which a sync will have to set instead.
- **`audio`** asks for nothing, as everywhere.
- **What's playing** (AppRuntime's `NowPlaying.md`) is an MPRIS player on
the session bus, `org.mpris.MediaPlayer2.<app id>` (characters a bus
name can't have become `_`), which the shell shows with its media
controls and media keys. The name is owned while the app describes a
player and given up when it stops; the object is served at
`/org/mpris/MediaPlayer2`:

| | MPRIS |
|---|---|
| `org.mpris.MediaPlayer2` | `Identity`, the app's name; `DesktopEntry`, its identifier, as the packager names the entry. No `Raise`, `Quit` or track list |
| `Metadata` | `xesam:title`, `xesam:artist`, `xesam:album`, `mpris:length` when the duration is known, `mpris:trackid` (one per player), `mpris:artUrl`: an artwork file's URL, or bytes written to a temporary file |
| `PlaybackStatus`, `Rate`, `Position` | Playing or Paused, from `HostMediaPlayback`'s reports; `Position` worked out when it's read from the last report and the rate, as the contract has the guest do. `Rate` is the last one played at, never 0 |
| Buttons | `Play`, `Pause`, `PlayPause`, `Stop` (a pause), `Seek` and `SetPosition` (for this track) are `HostMediaPlayback.remote`'s, answered without the guest; `Next` and `Previous` go to the guest when it offered them (`CanGoNext`, `CanGoPrevious`). `OpenUri` is `NotSupported` |
| Changes | `PropertiesChanged` at each report, with what changed; `Seeked` when the position jumps |

- **`audio`** asks for nothing, as everywhere. The desktop doesn't send an
app to the background, so a player plays on with its window hidden;
`.audio(background: true)` (`RTCapabilityOptions.audio.background`)
makes `canPlayInBackground` say so.
- **Not yet**:
- What's playing outside the window (NowPlaying): MPRIS on the session
bus. The desktop doesn't send an app to the background, so a player
plays on with its window hidden, and `setPlaysInBackground` is refused.
- `Volume` is 1 and read-only from the shell, and there's no
`LoopStatus` or `Shuffle`.
- Artwork as a file is named by its path, which a shell in another
sandbox may not read.
- Interruptions and unplugged headphones: the sound server doesn't tell
a client, short of PulseAudio's corking.
- Hardware decoding: GStreamer's VA-API and V4L2 decoders would need the
Expand Down Expand Up @@ -808,7 +836,9 @@ gives it a player per `MediaPlayback`, `GStreamerEngine`.
and, where `dbus-daemon` is installed, a daemon of the test's own: calls
waited for and on the main loop, properties, an error, a signal by match
rule from another connection, and connecting again, with the rules, once
the daemon's gone and another is in its place.
the daemon's gone and another is in its place; an object served, its
methods, an error, its properties read and written, introspection, its
signals, and a name owned and given up.
- `LinuxCoreModel`: where stores are; about 500 requests answered as
CoreModel's in-memory store answers them; every value there and back;
batches applied whole or not at all; a store kept between launches,
Expand Down Expand Up @@ -854,7 +884,11 @@ gives it a player per `MediaPlayback`, `GStreamerEngine`.
file; what isn't media, a missing file, and a URL without `network`; a
video's size, its preroll's picture, pictures while playing and after a
seek; what the registry plays. GStreamer's errors and MIME types as the
contract has them. `VideoPlayer` nodes lay out in `LinuxUI`'s tests,
contract has them. What's playing as an MPRIS player, against a bus
daemon of the test's own with another connection as the shell: the name,
the properties and metadata, the position moving on, play, seeking with
`Seeked`, a seek for another track ignored, next to the guest, a pause
that's the user's, `OpenUri` refused, and the name given up. `VideoPlayer` nodes lay out in `LinuxUI`'s tests,
with and without controls. The packager accepts the contracts with
`audio`.
- End to end: AppRuntime's `ExampleUI`, `ExampleGL` and `ExampleModel`
Expand Down
10 changes: 6 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,12 @@ let package = Package(

// MARK: - Media

// GStreamer's playbin, opened at run time.
// GStreamer's playbin, opened at run time, and MPRIS on the session
// bus.
.target(
name: "LinuxMedia",
dependencies: ["LinuxHost", runtime("DynamicMedia"), runtime("DynamicRuntime"), runtime("RuntimeMedia"), runtime("RuntimeHost"),
runtime("RuntimeCore"), runtime("RuntimeNative")],
dependencies: ["LinuxHost", "LinuxDBus", runtime("DynamicMedia"), runtime("DynamicRuntime"), runtime("RuntimeMedia"),
runtime("RuntimeHost"), runtime("RuntimeCore"), runtime("RuntimeNative"), .product(name: "DBus", package: "DBus")],
swiftSettings: host
),

Expand Down Expand Up @@ -299,7 +300,8 @@ let package = Package(
runtime("DynamicFiles"), runtime("RuntimeHost"), runtime("RuntimeNative"),
.product(name: "DBus", package: "DBus")],
swiftSettings: host),
.testTarget(name: "LinuxMediaTests", dependencies: ["LinuxMedia", runtime("DynamicMedia"), runtime("RuntimeMedia"), runtime("RuntimeHost")],
.testTarget(name: "LinuxMediaTests", dependencies: ["LinuxMedia", "LinuxDBus", runtime("DynamicMedia"), runtime("RuntimeMedia"),
runtime("RuntimeHost"), .product(name: "DBus", package: "DBus")],
swiftSettings: host),
.testTarget(name: "LinuxMotionTests", dependencies: ["LinuxMotion", runtime("DynamicMotion")], swiftSettings: host),
.testTarget(name: "LinuxWebKitTests", dependencies: ["LinuxWebKit", runtime("DynamicWebKit")], swiftSettings: host),
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ added to its database between launches:
| `LinuxWebKit` | `WebContent`: WPE WebKit pages, loaded at run time, drawn into shared memory and shown by the player's `WebView` view ([plan](Documentation/Plan.md) §8) |
| `LinuxCoreModel` | `LinuxModelStore`: the app's CoreModel stores in SQLite, through AppRuntime's `HostModelStore` and CoreModel-SQLite ([plan](Documentation/Plan.md) §9) |
| `LinuxDevice` | `Device`: the machine, its battery, power mode and heat, from `/etc`, DMI, the device tree and sysfs, and its orientation and proximity from iio-sensor-proxy ([plan](Documentation/Plan.md) §10) |
| `LinuxDBus` | `Bus`: the system and session buses for the modules that talk to system services, over PureSwift/DBus, with calls, properties and signals delivered on the main loop ([plan](Documentation/Plan.md) §11.1) |
| `LinuxDBus` | `Bus`: the system and session buses for the modules that talk to system services, over PureSwift/DBus, with calls, properties and signals delivered on the main loop, and objects the host serves ([plan](Documentation/Plan.md) §11.1) |
| `LinuxLocation` | `LocationManager`: the device's location from GeoClue 2 on the system bus, which its agent authorizes by the app's desktop ID; no heading ([plan](Documentation/Plan.md) §11) |
| `LinuxDocuments` | `Documents`: AppRuntime's `HostDocuments` with the desktop portal's file chooser on the session bus, for opening, folders and saving, and bookmarks in the app's data ([plan](Documentation/Plan.md) §12) |
| `LinuxMotion` | Motion: the accelerometer, gyroscope, magnetometer and barometer from the kernel's IIO sensors in sysfs, device motion fused from them, and a pedometer and activity recognizer that are unavailable ([plan](Documentation/Plan.md) §13) |
| `LinuxNFC` | `NFCReader`, `NFCTag`, `NDEFMessage`, `NDEFRecord`: reading and writing NDEF tags through neard on the system bus; no raw commands or locking ([plan](Documentation/Plan.md) §14) |
| `LinuxMedia` | `MediaPlayback`: songs and videos through GStreamer's `playbin`, opened at run time, from files, bytes or URLs; the player's `VideoPlayer` view shows the pictures, with play, pause and a scrubber ([plan](Documentation/Plan.md) §15) |
| `LinuxMedia` | `MediaPlayback`: songs and videos through GStreamer's `playbin`, opened at run time, from files, bytes or URLs; the player's `VideoPlayer` view shows the pictures, with play, pause and a scrubber; what's playing is an MPRIS player on the session bus, for the shell's media controls ([plan](Documentation/Plan.md) §15) |
| `LinuxHostApp`, `linux-app-host` | The app's `main`: the host, and Cacao's window and loop. It also registers AppRuntime's `RuntimeFormatting`, dates and numbers over Swift Foundation, and `RuntimeNetworking`, HTTP over `URLSession` and libcurl ([plan](Documentation/Plan.md) §3) |
| `LinuxPackager`, `linux-app` | The packager: validation, thinning a bundle to one target, the install tree, PortMaster ports |

Expand Down
20 changes: 15 additions & 5 deletions Sources/LinuxDBus/Bus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import LinuxHost

/// A D-Bus bus, for the modules that talk to system services (`Plan.md`
/// §11): method calls, property reads and writes, and signals by match
/// rule. Main loop only.
/// rule; and objects this process serves, with the names it owns
/// (`BusObjects.swift`). Main loop only.
///
/// The connection, PureSwift/DBus's, lives on Swift's own threads; every
/// reply and signal is posted to the main loop. It's made on first use
/// and, if it closes, made again on the next, with the subscriptions' match
/// rules added again; subscribers to `MatchRule.disconnected` hear when it
/// rules added again, and its objects served and its names asked for
/// again; subscribers to `MatchRule.disconnected` hear when it
/// closes. Requests are started in the order they're made, and one to the
/// bus itself, such as `AddMatch`, is answered before the next starts.
///
Expand All @@ -31,14 +33,21 @@ public final class Bus {
/// The system bus: BlueZ's, GeoClue's, iio-sensor-proxy's, neard's.
nonisolated(unsafe) public static let system = Bus(.system)

/// The session bus: the desktop portal's.
/// The session bus: the desktop portal's, and the shell's media
/// controls'.
nonisolated(unsafe) public static let session = Bus(.session)

public let kind: Kind

private let loop: MainLoop
let loop: MainLoop

private let link: BusLink
let link: BusLink

/// The objects this process serves, by path, and the names it asked
/// for.
var exports: [String: [BusInterface]] = [:]

var names: Set<String> = []

private var subscriptions: [UInt64: (rule: MatchRule, handler: (BusSignal) -> Void)] = [:]

Expand Down Expand Up @@ -273,6 +282,7 @@ public final class Bus {
for rule in Set(subscriptions.values.map(\.rule)) {
addMatch(rule)
}
serveAgain()
case .signal(let signal):
deliver(signal)
case .closed(let error):
Expand Down
33 changes: 26 additions & 7 deletions Sources/LinuxDBus/BusLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ final class BusLink: @unchecked Sendable {

typealias Completion = @Sendable (Result<[DBusMessageArgument], BusError>) -> Void

/// Work on the connection itself, such as exporting an object or
/// emitting a signal, in order with the requests.
typealias Action = @Sendable (DBusConnection) async -> Void

enum Event: Sendable {
/// Connected and said `Hello`; requests made while connecting are
/// on their way.
Expand All @@ -22,14 +26,15 @@ final class BusLink: @unchecked Sendable {
}

private struct Outgoing: Sendable {
let message: DBusMessage
let message: DBusMessage?
let completion: Completion
var action: Action?

/// A request to the bus itself (`AddMatch`, `RemoveMatch`): the
/// next isn't started until it's answered, so a match rule is in
/// place before what was asked after it.
var isBarrier: Bool {
message.destination == DBusWellKnown.busName
message?.destination == DBusWellKnown.busName
}
}

Expand Down Expand Up @@ -60,7 +65,17 @@ final class BusLink: @unchecked Sendable {
/// Sends `message`; `completion` gets its reply's values or its error,
/// on Swift's threads.
func send(_ message: DBusMessage, _ completion: @escaping Completion) {
let item = Outgoing(message: message, completion: completion)
enqueue(Outgoing(message: message, completion: completion))
}

/// Runs `action` on the connection, connecting first if need be, after
/// what was asked before it. Without a connection it's dropped.
func perform(_ action: @escaping Action) {
enqueue(Outgoing(message: nil, completion: { _ in }, action: action))
}

private func enqueue(_ item: Outgoing) {
let completion = item.completion
lock.lock()
switch status {
case .connected(let outgoing):
Expand Down Expand Up @@ -119,10 +134,14 @@ final class BusLink: @unchecked Sendable {
let (outgoing, continuation) = AsyncStream.makeStream(of: Outgoing.self)
let writer = Task {
for await item in outgoing {
if item.isBarrier {
item.completion(await Self.send(item.message, on: connection))
} else {
await connection.begin(item.message, item.completion)
if let action = item.action {
await action(connection)
} else if let message = item.message {
if item.isBarrier {
item.completion(await Self.send(message, on: connection))
} else {
await connection.begin(message, item.completion)
}
}
}
}
Expand Down
Loading
Loading