@@ -7,40 +7,27 @@ use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
7
7
8
8
#[ derive( Debug ) ]
9
9
struct Opt {
10
- #[ cfg( all(
11
- any( target_os = "linux" , target_os = "dragonfly" , target_os = "freebsd" ) ,
12
- feature = "jack"
13
- ) ) ]
10
+ #[ allow( dead_code) ]
14
11
jack : bool ,
15
-
12
+ #[ allow( dead_code) ]
13
+ pipewire : bool ,
16
14
device : String ,
17
15
}
18
16
19
17
impl Opt {
20
18
fn from_args ( ) -> Self {
21
19
let app = clap:: Command :: new ( "beep" ) . arg ( arg ! ( [ DEVICE ] "The audio device to use" ) ) ;
22
- #[ cfg( all(
23
- any( target_os = "linux" , target_os = "dragonfly" , target_os = "freebsd" ) ,
24
- feature = "jack"
25
- ) ) ]
26
20
let app = app. arg ( arg ! ( -j --jack "Use the JACK host" ) ) ;
21
+ let app = app. arg ( arg ! ( -p --pipewire "Use the PipeWire host" ) ) ;
22
+
27
23
let matches = app. get_matches ( ) ;
28
24
let device = matches. value_of ( "DEVICE" ) . unwrap_or ( "default" ) . to_string ( ) ;
29
25
30
- #[ cfg( all(
31
- any( target_os = "linux" , target_os = "dragonfly" , target_os = "freebsd" ) ,
32
- feature = "jack"
33
- ) ) ]
34
- return Opt {
26
+ Opt {
35
27
jack : matches. is_present ( "jack" ) ,
28
+ pipewire : matches. is_present ( "pipewire" ) ,
36
29
device,
37
- } ;
38
-
39
- #[ cfg( any(
40
- not( any( target_os = "linux" , target_os = "dragonfly" , target_os = "freebsd" ) ) ,
41
- not( feature = "jack" )
42
- ) ) ]
43
- Opt { device }
30
+ }
44
31
}
45
32
}
46
33
@@ -65,10 +52,22 @@ fn main() -> anyhow::Result<()> {
65
52
cpal:: default_host ( )
66
53
} ;
67
54
68
- #[ cfg( any(
69
- not( any( target_os = "linux" , target_os = "dragonfly" , target_os = "freebsd" ) ) ,
70
- not( feature = "jack" )
71
- ) ) ]
55
+ // Conditionally compile with PipeWire if the feature is specified.
56
+ #[ cfg( all( target_os = "linux" , feature = "pipewire" ) ) ]
57
+ // Manually check for flags. Can be passed through cargo with -- e.g.
58
+ // cargo run --release --example beep --features pipewire -- --pipewire
59
+ let host = if opt. pipewire {
60
+ cpal:: host_from_id ( cpal:: available_hosts ( )
61
+ . into_iter ( )
62
+ . find ( |id| * id == cpal:: HostId :: PipeWire )
63
+ . expect (
64
+ "make sure --features pipewire is specified. only works on OSes where PipeWire is available" ,
65
+ ) ) . expect ( "PipeWire host unavailable" )
66
+ } else {
67
+ cpal:: default_host ( )
68
+ } ;
69
+
70
+ #[ cfg( not( any( feature = "jack" , feature = "pipewire" ) ) ) ]
72
71
let host = cpal:: default_host ( ) ;
73
72
74
73
let device = if opt. device == "default" {
0 commit comments