@@ -4,7 +4,7 @@ use std::ffi::{c_char, c_void, CStr};
4
4
use std:: iter:: zip;
5
5
use std:: ptr:: NonNull ;
6
6
use std:: sync:: Arc ;
7
- use std:: { io, ptr, slice} ;
7
+ use std:: { io, mem , ptr, slice} ;
8
8
9
9
use clap_sys:: ext:: { audio_ports:: * , audio_ports_config:: * , gui:: * , params:: * , state:: * } ;
10
10
use clap_sys:: { events:: * , host:: * , id:: * , plugin:: * , process:: * , stream:: * } ;
@@ -18,6 +18,7 @@ use crate::host::Host;
18
18
use crate :: params:: { ParamId , ParamInfo , ParamValue } ;
19
19
use crate :: plugin:: { Plugin , PluginInfo } ;
20
20
use crate :: process:: { Config , Processor } ;
21
+ use crate :: sync:: param_gestures:: { GestureStates , GestureUpdate , ParamGestures } ;
21
22
use crate :: sync:: params:: ParamValues ;
22
23
use crate :: util:: { copy_cstring, slice_from_raw_parts_checked, DisplayParam } ;
23
24
@@ -45,12 +46,14 @@ fn map_param_out(param: &ParamInfo, value: ParamValue) -> f64 {
45
46
}
46
47
47
48
pub struct MainThreadState < P : Plugin > {
49
+ pub host_params : Option < * const clap_host_params > ,
48
50
pub layout_index : usize ,
49
51
pub plugin : P ,
50
52
pub editor : Option < P :: Editor > ,
51
53
}
52
54
53
55
pub struct ProcessState < P : Plugin > {
56
+ gesture_states : GestureStates ,
54
57
buffer_data : Vec < BufferData > ,
55
58
buffer_ptrs : Vec < * mut f32 > ,
56
59
events : Vec < Event > ,
@@ -65,11 +68,12 @@ pub struct Instance<P: Plugin> {
65
68
pub info : Arc < PluginInfo > ,
66
69
pub input_bus_map : Vec < usize > ,
67
70
pub output_bus_map : Vec < usize > ,
68
- pub param_map : HashMap < ParamId , usize > ,
71
+ pub param_map : Arc < HashMap < ParamId , usize > > ,
69
72
// Processor -> plugin parameter changes
70
73
pub plugin_params : ParamValues ,
71
74
// Plugin -> processor parameter changes
72
75
pub processor_params : ParamValues ,
76
+ pub param_gestures : Arc < ParamGestures > ,
73
77
pub main_thread_state : UnsafeCell < MainThreadState < P > > ,
74
78
pub process_state : UnsafeCell < ProcessState < P > > ,
75
79
}
@@ -119,15 +123,18 @@ impl<P: Plugin> Instance<P> {
119
123
info : info. clone ( ) ,
120
124
input_bus_map,
121
125
output_bus_map,
122
- param_map,
126
+ param_map : Arc :: new ( param_map ) ,
123
127
plugin_params : ParamValues :: with_count ( info. params . len ( ) ) ,
124
128
processor_params : ParamValues :: with_count ( info. params . len ( ) ) ,
129
+ param_gestures : Arc :: new ( ParamGestures :: with_count ( info. params . len ( ) ) ) ,
125
130
main_thread_state : UnsafeCell :: new ( MainThreadState {
131
+ host_params : None ,
126
132
layout_index : 0 ,
127
133
plugin : P :: new ( Host :: from_inner ( Arc :: new ( ClapHost { } ) ) ) ,
128
134
editor : None ,
129
135
} ) ,
130
136
process_state : UnsafeCell :: new ( ProcessState {
137
+ gesture_states : GestureStates :: with_count ( info. params . len ( ) ) ,
131
138
buffer_data : Vec :: new ( ) ,
132
139
buffer_ptrs : Vec :: new ( ) ,
133
140
events : Vec :: with_capacity ( 4096 ) ,
@@ -197,10 +204,114 @@ impl<P: Plugin> Instance<P> {
197
204
( * self . host ) . request_callback . unwrap ( ) ( self . host ) ;
198
205
}
199
206
}
207
+
208
+ unsafe fn process_gestures (
209
+ & self ,
210
+ gesture_states : & mut GestureStates ,
211
+ events : & mut Vec < Event > ,
212
+ out_events : * const clap_output_events ,
213
+ time : u32 ,
214
+ ) {
215
+ for update in self . param_gestures . poll ( gesture_states) {
216
+ let param = & self . info . params [ update. index ] ;
217
+
218
+ if let Some ( value) = update. set_value {
219
+ events. push ( Event {
220
+ time : time as i64 ,
221
+ data : Data :: ParamChange {
222
+ id : param. id ,
223
+ value,
224
+ } ,
225
+ } ) ;
226
+
227
+ self . plugin_params . set ( update. index , value) ;
228
+ }
229
+
230
+ self . send_gesture_events ( & update, out_events, time) ;
231
+ }
232
+ }
233
+
234
+ unsafe fn send_gesture_events (
235
+ & self ,
236
+ update : & GestureUpdate ,
237
+ out_events : * const clap_output_events ,
238
+ time : u32 ,
239
+ ) {
240
+ let param = & self . info . params [ update. index ] ;
241
+
242
+ if update. begin_gesture {
243
+ let event = clap_event_param_gesture {
244
+ header : clap_event_header {
245
+ size : mem:: size_of :: < clap_event_param_gesture > ( ) as u32 ,
246
+ time,
247
+ space_id : CLAP_CORE_EVENT_SPACE_ID ,
248
+ type_ : CLAP_EVENT_PARAM_GESTURE_BEGIN ,
249
+ flags : CLAP_EVENT_IS_LIVE ,
250
+ } ,
251
+ param_id : param. id ,
252
+ } ;
253
+
254
+ ( * out_events) . try_push . unwrap ( ) (
255
+ out_events,
256
+ & event as * const clap_event_param_gesture as * const clap_event_header ,
257
+ ) ;
258
+ }
259
+
260
+ if let Some ( value) = update. set_value {
261
+ let event = clap_event_param_value {
262
+ header : clap_event_header {
263
+ size : mem:: size_of :: < clap_event_param_value > ( ) as u32 ,
264
+ time,
265
+ space_id : CLAP_CORE_EVENT_SPACE_ID ,
266
+ type_ : CLAP_EVENT_PARAM_VALUE ,
267
+ flags : CLAP_EVENT_IS_LIVE ,
268
+ } ,
269
+ param_id : param. id ,
270
+ cookie : ptr:: null_mut ( ) ,
271
+ note_id : -1 ,
272
+ port_index : -1 ,
273
+ channel : -1 ,
274
+ key : -1 ,
275
+ value : map_param_out ( param, value) ,
276
+ } ;
277
+
278
+ ( * out_events) . try_push . unwrap ( ) (
279
+ out_events,
280
+ & event as * const clap_event_param_value as * const clap_event_header ,
281
+ ) ;
282
+ }
283
+
284
+ if update. end_gesture {
285
+ let event = clap_event_param_gesture {
286
+ header : clap_event_header {
287
+ size : mem:: size_of :: < clap_event_param_gesture > ( ) as u32 ,
288
+ time,
289
+ space_id : CLAP_CORE_EVENT_SPACE_ID ,
290
+ type_ : CLAP_EVENT_PARAM_GESTURE_END ,
291
+ flags : CLAP_EVENT_IS_LIVE ,
292
+ } ,
293
+ param_id : param. id ,
294
+ } ;
295
+
296
+ ( * out_events) . try_push . unwrap ( ) (
297
+ out_events,
298
+ & event as * const clap_event_param_gesture as * const clap_event_header ,
299
+ ) ;
300
+ }
301
+ }
200
302
}
201
303
202
304
impl < P : Plugin > Instance < P > {
203
- unsafe extern "C" fn init ( _plugin : * const clap_plugin ) -> bool {
305
+ unsafe extern "C" fn init ( plugin : * const clap_plugin ) -> bool {
306
+ let instance = & * ( plugin as * const Self ) ;
307
+ let main_thread_state = & mut * instance. main_thread_state . get ( ) ;
308
+
309
+ let host_params =
310
+ ( * instance. host ) . get_extension . unwrap ( ) ( instance. host , CLAP_EXT_PARAMS . as_ptr ( ) ) ;
311
+ if !host_params. is_null ( ) {
312
+ main_thread_state. host_params = Some ( host_params as * const clap_host_params ) ;
313
+ }
314
+
204
315
true
205
316
}
206
317
@@ -372,6 +483,14 @@ impl<P: Plugin> Instance<P> {
372
483
instance. sync_processor ( & mut process_state. events ) ;
373
484
instance. process_param_events ( process. in_events , & mut process_state. events ) ;
374
485
486
+ let last_sample = process. frames_count . saturating_sub ( 1 ) ;
487
+ instance. process_gestures (
488
+ & mut process_state. gesture_states ,
489
+ & mut process_state. events ,
490
+ process. out_events ,
491
+ last_sample,
492
+ ) ;
493
+
375
494
processor. process (
376
495
Buffers :: from_raw_parts (
377
496
& process_state. buffer_data ,
@@ -684,7 +803,7 @@ impl<P: Plugin> Instance<P> {
684
803
unsafe extern "C" fn params_flush (
685
804
plugin : * const clap_plugin ,
686
805
in_ : * const clap_input_events ,
687
- _out : * const clap_output_events ,
806
+ out : * const clap_output_events ,
688
807
) {
689
808
let instance = & * ( plugin as * const Self ) ;
690
809
let process_state = & mut * instance. process_state . get ( ) ;
@@ -696,6 +815,12 @@ impl<P: Plugin> Instance<P> {
696
815
process_state. events . clear ( ) ;
697
816
instance. sync_processor ( & mut process_state. events ) ;
698
817
instance. process_param_events ( in_, & mut process_state. events ) ;
818
+ instance. process_gestures (
819
+ & mut process_state. gesture_states ,
820
+ & mut process_state. events ,
821
+ out,
822
+ 0 ,
823
+ ) ;
699
824
700
825
processor. process (
701
826
Buffers :: from_raw_parts (
@@ -730,6 +855,20 @@ impl<P: Plugin> Instance<P> {
730
855
}
731
856
}
732
857
}
858
+
859
+ for update in instance. param_gestures . poll ( & mut process_state. gesture_states ) {
860
+ let param = & instance. info . params [ update. index ] ;
861
+
862
+ if let Some ( value) = update. set_value {
863
+ main_thread_state. plugin . set_param ( param. id , value) ;
864
+
865
+ if let Some ( editor) = & mut main_thread_state. editor {
866
+ editor. param_changed ( param. id , value) ;
867
+ }
868
+ }
869
+
870
+ instance. send_gesture_events ( & update, out, 0 ) ;
871
+ }
733
872
}
734
873
}
735
874
}
0 commit comments