Skip to content

Commit

Permalink
Statically link VCRuntime for MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
2e3s committed Dec 5, 2023
1 parent 8fcf0fd commit 2d63c1d
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 9 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@ jobs:
# 22.04 is the earliest version with OpenSSL 3
image: ubuntu-22.04
build_name: linux
rustflags:
- target: x86_64-pc-windows-msvc
image: windows-latest
build_name: windows
rustflags: -C target-feature=+crt-static
runs-on: ${{ matrix.image }}
env:
TARGET: ${{ matrix.target }}
RUSTFLAGS: ${{ matrix.rustflags }}
steps:
- uses: actions/checkout@v3
- name: Install dependencies for Ubuntu
Expand Down Expand Up @@ -59,3 +56,15 @@ jobs:
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: target/${{ matrix.target }}/release/aw-watcher-media-player-${{ matrix.build_name }}.zip

- run: cargo install cargo-deb
if: ${{ contains(matrix.image, 'ubuntu') }}
- run: cargo deb
if: ${{ contains(matrix.image, 'ubuntu') }}
- name: Upload deb to release
if: ${{ contains(matrix.image, 'ubuntu') }}
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file_glob: true
file: target/debian/aw-*.deb
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name = "aw-watcher-media-player"
authors = ["Demmie <[email protected]>"]
version = "1.0.0"
edition = "2021"
description = "A cross-platform watcher to send currently playing media to ActivityWatch."
description = "A cross-platform watcher to report currently playing media to ActivityWatch."
license-file = "LICENSE"

[dependencies]
aw-client-rust = { git = "https://github.com/ActivityWatch/aw-server-rust", rev = "448312d" }
Expand All @@ -21,6 +22,9 @@ clap-verbosity-flag = "2.1.0"
tokio = { version = "1.34.0", features = ["time", "macros", "signal"] }
dirs = "5.0.1"

[target.'cfg(target_env = "msvc")'.build-dependencies]
static_vcruntime = { version = "2.0" }

[target.'cfg(all(unix, not(target_os = "macos")))'.dependencies]
mpris = "2.0.1"

Expand All @@ -30,3 +34,6 @@ features = [
"Foundation",
"Media_Control",
]

[package.metadata.deb]
extended-description = "ActivityWatch must be available by the given address for this watcher to work."
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ such as Spotify, Foobar, browser-based players, and others. Most media players a
| Windows | :heavy_check_mark: Yes |
| MacOS | :hourglass: Not yet supported |

Example of report for Spotify in Linux:
<details open>
<summary>Examples of reported data</summary>

Spotify in Linux:
```json
{
"album": "How to Measure a Planet? (Deluxe Edition)",
Expand All @@ -29,6 +32,25 @@ Firefox in Linux (no plugins):
"title": "🇺🇦 🇵🇱 Гей, соколи! / Hej, sokoły! – Ukrainian/Polish folk song"
}
```
MS Edge in Windows:
```json
{
"artist": "Bel Canto Choir Vilnius",
"player": "MSEdge",
"title": "Shchedryk (Carol of the Bells) – Bel Canto Choir Vilnius"
}
```
Default Windows player
```json
{
"album": "Zemlya",
"artist": "Okean Elzy",
"player": "Microsoft.ZuneMusic_8wekyb3d8bbwe!Microsoft.ZuneMusic",
"title": "Obijmy"
}
```

</details>

## Configuration

Expand Down
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
#[cfg(target_env = "msvc")]
static_vcruntime::metabuild();
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ async fn main() -> anyhow::Result<()> {
if let Some(data) = data {
if config.report_player(&data.player) {
watcher.send_active_window(&data).await.unwrap();
} else {
trace!("Player \"{}\" is filtered out", data.player);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ fn mediadata(player_finder: &PlayerFinder) -> Option<MediaData> {
let player = player_finder.find_active().ok()?;

if player.get_playback_status().ok()? != PlaybackStatus::Playing {
trace!(
"Player {} is not playing with status {}",
player.bus_name(),
player
.get_playback_status()
.map(|status| format!("{status:?}"))
.unwrap_or("not found".to_string())
);

return None;
}

Expand Down
24 changes: 20 additions & 4 deletions src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use chrono::Utc;
use super::config::Config;

const BUCKET_NAME: &str = env!("CARGO_PKG_NAME");
const TCP_ERROR: &str = "tcp connect error: Connection refused";

pub struct Watcher {
client: AwClient,
Expand All @@ -27,10 +28,25 @@ impl Watcher {
}

pub async fn init(&self) -> anyhow::Result<()> {
self.client
.create_bucket_simple(&self.bucket_name, "currently-playing")
.await
.with_context(|| format!("Failed to create bucket {}", self.bucket_name))
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1));
let mut attempts = 0;
loop {
let f = self
.client
.create_bucket_simple(&self.bucket_name, "currently-playing");
match f.await {
Ok(val) => return Ok(val),
Err(e) if attempts < 3 && e.to_string().contains(TCP_ERROR) => {
warn!("Failed to connect, retrying: {}", e);

attempts += 1;
interval.tick().await;
}
Err(e) => {
return Err(e).context(format!("Failed to create bucket {}", self.bucket_name))
}
}
}
}

pub async fn send_active_window(&self, data: &MediaData) -> anyhow::Result<()> {
Expand Down

0 comments on commit 2d63c1d

Please sign in to comment.