11//! Properties, AKA [Meta Data](https://jackaudio.org/api/group__Metadata.html)
22//!
3+ use std:: borrow:: Cow ;
34use std:: panic:: catch_unwind;
45
56use j:: jack_uuid_t as uuid;
67use jack_sys as j;
78
8- /// A description of a Metadata change describint a creation, change or deletion, its owner
9+ /// A description of a Metadata change describing a creation, change or deletion, its owner
910/// `subject` and `key`.
10- #[ derive( Debug , PartialEq , Eq ) ]
11+ ///
12+ /// `key` is `None` when JACK reports a change without a key. This happens for `Deleted` when all
13+ /// properties of `subject` were removed (see [`Client::property_remove_subject`]), or when all
14+ /// properties of all subjects were removed (see [`Client::property_remove_all`]), in which case
15+ /// `subject` is the empty UUID `0`.
16+ ///
17+ /// Non-UTF-8 keys are converted lossily, invalid bytes are replaced with `U+FFFD`.
18+ ///
19+ /// [`Client::property_remove_subject`]: crate::Client::property_remove_subject
20+ /// [`Client::property_remove_all`]: crate::Client::property_remove_all
21+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
1122pub enum PropertyChange < ' a > {
12- Created { subject : uuid , key : & ' a str } ,
13- Changed { subject : uuid , key : & ' a str } ,
14- Deleted { subject : uuid , key : & ' a str } ,
23+ Created {
24+ subject : uuid ,
25+ key : Option < Cow < ' a , str > > ,
26+ } ,
27+ Changed {
28+ subject : uuid ,
29+ key : Option < Cow < ' a , str > > ,
30+ } ,
31+ Deleted {
32+ subject : uuid ,
33+ key : Option < Cow < ' a , str > > ,
34+ } ,
1535}
1636
1737/// A trait for reacting to property changes.
@@ -34,8 +54,13 @@ pub(crate) unsafe extern "C" fn property_changed<P>(
3454 unsafe {
3555 let res = catch_unwind ( || {
3656 let h: & mut P = & mut * ( arg as * mut P ) ;
37- let key_c = std:: ffi:: CStr :: from_ptr ( key) ;
38- let key = key_c. to_str ( ) . expect ( "to convert key to valid str" ) ;
57+ // JACK passes a NULL key when all properties of a subject (or of all subjects) were
58+ // deleted.
59+ let key = if key. is_null ( ) {
60+ None
61+ } else {
62+ Some ( std:: ffi:: CStr :: from_ptr ( key) . to_string_lossy ( ) )
63+ } ;
3964 let c = match change {
4065 j:: PropertyCreated => PropertyChange :: Created { subject, key } ,
4166 j:: PropertyDeleted => PropertyChange :: Deleted { subject, key } ,
@@ -44,7 +69,12 @@ pub(crate) unsafe extern "C" fn property_changed<P>(
4469 h. property_changed ( & c) ;
4570 } ) ;
4671 if let Err ( err) = res {
47- eprintln ! ( "{err:?}" ) ;
72+ let msg = err
73+ . downcast_ref :: < & str > ( )
74+ . copied ( )
75+ . or_else ( || err. downcast_ref :: < String > ( ) . map ( String :: as_str) )
76+ . unwrap_or ( "unknown panic" ) ;
77+ eprintln ! ( "panic in property change handler: {msg}" ) ;
4878 std:: mem:: forget ( err) ;
4979 }
5080 }
@@ -65,11 +95,13 @@ mod metadata {
6595 use crate :: Client ;
6696
6797 /// A helper enum, allowing for sending changes between threads.
98+ ///
99+ /// See [`PropertyChange`] for the meaning of a `None` key.
68100 #[ derive( Debug , Clone , PartialEq , Eq ) ]
69101 pub enum PropertyChangeOwned {
70- Created { subject : uuid , key : String } ,
71- Changed { subject : uuid , key : String } ,
72- Deleted { subject : uuid , key : String } ,
102+ Created { subject : uuid , key : Option < String > } ,
103+ Changed { subject : uuid , key : Option < String > } ,
104+ Deleted { subject : uuid , key : Option < String > } ,
73105 }
74106
75107 /// A piece of Metadata on a Jack `subject`: either a port or a client.
@@ -350,15 +382,15 @@ mod metadata {
350382 match property {
351383 PropertyChange :: Created { subject, key } => Self :: Created {
352384 subject : * subject,
353- key : key. to_string ( ) ,
385+ key : key. as_deref ( ) . map ( str :: to_string ) ,
354386 } ,
355387 PropertyChange :: Changed { subject, key } => Self :: Changed {
356388 subject : * subject,
357- key : key. to_string ( ) ,
389+ key : key. as_deref ( ) . map ( str :: to_string ) ,
358390 } ,
359391 PropertyChange :: Deleted { subject, key } => Self :: Deleted {
360392 subject : * subject,
361- key : key. to_string ( ) ,
393+ key : key. as_deref ( ) . map ( str :: to_string ) ,
362394 } ,
363395 }
364396 }
@@ -561,7 +593,7 @@ mod metadata {
561593 assert_eq ! (
562594 Ok ( PropertyChangeOwned :: Created {
563595 subject: c2. uuid( ) ,
564- key: "blah" . into( )
596+ key: Some ( "blah" . into( ) )
565597 } ) ,
566598 r
567599 ) ;
@@ -575,7 +607,7 @@ mod metadata {
575607 assert_eq ! (
576608 Ok ( PropertyChangeOwned :: Changed {
577609 subject: c2. uuid( ) ,
578- key: "blah" . into( )
610+ key: Some ( "blah" . into( ) )
579611 } ) ,
580612 r
581613 ) ;
@@ -585,7 +617,7 @@ mod metadata {
585617 assert_eq ! (
586618 Ok ( PropertyChangeOwned :: Deleted {
587619 subject: c2. uuid( ) ,
588- key: "blah" . into( )
620+ key: Some ( "blah" . into( ) )
589621 } ) ,
590622 r
591623 ) ;
@@ -599,20 +631,108 @@ mod metadata {
599631 assert_eq ! (
600632 Ok ( PropertyChangeOwned :: Created {
601633 subject: c2. uuid( ) ,
602- key: "blah" . into( )
634+ key: Some ( "blah" . into( ) )
603635 } ) ,
604636 r
605637 ) ;
606638 let r = receiver. recv_timeout ( timeout) ;
607639 assert_eq ! (
608640 Ok ( PropertyChangeOwned :: Created {
609641 subject: ac. as_client( ) . uuid( ) ,
610- key: "mutant" . into( )
642+ key: Some ( "mutant" . into( ) )
611643 } ) ,
612644 r
613645 ) ;
614646 }
615647
648+ fn call_property_changed (
649+ key : * const :: libc:: c_char ,
650+ change : j:: jack_property_change_t ,
651+ ) -> Vec < PropertyChangeOwned > {
652+ let ( sender, receiver) = channel :: < PropertyChangeOwned > ( ) ;
653+ let mut handler = ClosurePropertyChangeHandler :: new ( move |c : & PropertyChange | {
654+ sender. send ( c. into ( ) ) . unwrap ( ) ;
655+ } ) ;
656+ fn call < P : PropertyChangeHandler > (
657+ handler : & mut P ,
658+ key : * const :: libc:: c_char ,
659+ change : j:: jack_property_change_t ,
660+ ) {
661+ unsafe {
662+ crate :: properties:: property_changed :: < P > (
663+ 42 ,
664+ key,
665+ change,
666+ handler as * mut P as * mut :: libc:: c_void ,
667+ )
668+ }
669+ }
670+ call ( & mut handler, key, change) ;
671+ drop ( handler) ;
672+ receiver. iter ( ) . collect ( )
673+ }
674+
675+ #[ test]
676+ fn property_changed_null_key ( ) {
677+ assert_eq ! (
678+ call_property_changed( ptr:: null( ) , j:: PropertyDeleted ) ,
679+ vec![ PropertyChangeOwned :: Deleted {
680+ subject: 42 ,
681+ key: None
682+ } ]
683+ ) ;
684+ }
685+
686+ #[ test]
687+ fn property_changed_non_utf8_key ( ) {
688+ let key = ffi:: CString :: new ( & b"key-\xff " [ ..] ) . unwrap ( ) ;
689+ assert_eq ! (
690+ call_property_changed( key. as_ptr( ) , j:: PropertyCreated ) ,
691+ vec![ PropertyChangeOwned :: Created {
692+ subject: 42 ,
693+ key: Some ( "key-\u{fffd} " . into( ) )
694+ } ]
695+ ) ;
696+ }
697+
698+ #[ test]
699+ #[ ignore = "unsupported on pipewire-jack; CI runs ignored tests with JACK2" ]
700+ fn client_callbacks_remove_subject_and_all ( ) {
701+ let timeout = std:: time:: Duration :: from_millis ( 50 ) ;
702+ let prop = Property :: new ( "foo" , None ) ;
703+ let ( mut c1, _) = Client :: new ( "client1" , ClientOptions :: default ( ) ) . unwrap ( ) ;
704+ let ( c2, _) = Client :: new ( "client2" , ClientOptions :: default ( ) ) . unwrap ( ) ;
705+ let ( sender, receiver) : ( Sender < PropertyChangeOwned > , _ ) = channel ( ) ;
706+ assert_eq ! (
707+ Ok ( ( ) ) ,
708+ c1. register_property_change_handler( ClosurePropertyChangeHandler :: new(
709+ move |change| {
710+ let _ = sender. send( change. into( ) ) ;
711+ }
712+ ) )
713+ ) ;
714+ let _ac = c1. activate_async ( ( ) , ( ) ) . unwrap ( ) ;
715+
716+ assert_eq ! ( c2. property_set( c2. uuid( ) , "blah" , & prop) , Ok ( ( ) ) ) ;
717+ assert ! ( receiver. recv_timeout( timeout) . is_ok( ) ) ;
718+ assert_eq ! ( c2. property_remove_subject( c2. uuid( ) ) , Ok ( ( ) ) ) ;
719+ assert_eq ! (
720+ receiver. recv_timeout( timeout) ,
721+ Ok ( PropertyChangeOwned :: Deleted {
722+ subject: c2. uuid( ) ,
723+ key: None
724+ } )
725+ ) ;
726+
727+ assert_eq ! ( c2. property_set( c2. uuid( ) , "blah" , & prop) , Ok ( ( ) ) ) ;
728+ assert ! ( receiver. recv_timeout( timeout) . is_ok( ) ) ;
729+ assert_eq ! ( c2. property_remove_all( ) , Ok ( ( ) ) ) ;
730+ assert ! ( matches!(
731+ receiver. recv_timeout( timeout) ,
732+ Ok ( PropertyChangeOwned :: Deleted { key: None , .. } )
733+ ) ) ;
734+ }
735+
616736 #[ test]
617737 #[ should_panic]
618738 fn double_register ( ) {
0 commit comments