-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMapper.cs
More file actions
1698 lines (1534 loc) · 64.4 KB
/
Copy pathMapper.cs
File metadata and controls
1698 lines (1534 loc) · 64.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Threading;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
namespace Mapper
{
[Flags]
public enum Type
{
Device = 0x01, //!< Devices only.
SignalIn = 0x02, //!< Input signals.
SignalOut = 0x04, //!< Output signals.
Signal = 0x06, //!< All signals.
MapIn = 0x08, //!< Incoming maps.
MapOut = 0x10, //!< Outgoing maps.
Map = 0x18, //!< All maps.
Object = 0x1F, //!< All objects: devices, signale, and maps
List = 0x40, //!< object query.
Graph = 0x41, //!< Graph.
Boolean = 'b', /* 0x62 */ //!< Boolean value.
Type = 'c', /* 0x63 */ //!< libmapper data type.
Double = 'd', /* 0x64 */ //!< 64-bit float.
Float = 'f', /* 0x66 */ //!< 32-bit float.
Int64 = 'h', /* 0x68 */ //!< 64-bit integer.
Int32 = 'i', /* 0x69 */ //!< 32-bit integer.
String = 's', /* 0x73 */ //!< String.
Time = 't', /* 0x74 */ //!< 64-bit NTP timestamp.
Pointer = 'v', /* 0x76 */ //!< pointer.
Null = 'N' /* 0x4E */ //!< NULL value.
}
public enum Operator
{
DoesNotExist = 0x01, //!< Property does not exist.
IsEqual = 0x02, //!< Property value == query value.
Exists = 0x03, //!< Property exists for this entity.
IsGreaterThan = 0x04, //!< Property value > query value.
IsGreaterThanOrEqual = 0x05, //!< Property value >= query value
IsLessThan = 0x06, //!< Property value < query value
IsLessThanOrEqual = 0x07, //!< Property value <= query value
IsNotEqual = 0x08, //!< Property value != query value
All = 0x10, //!< Applies to all elements of value
Any = 0x20 //!< Applies to any element of value
}
[StructLayout(LayoutKind.Explicit)]
public struct MprTime
{
[FieldOffset(0)]
internal long ntp;
[FieldOffset(0)]
internal UInt32 sec;
[FieldOffset(4)]
internal UInt32 frac;
}
public class Time
{
// internal long _time;
internal MprTime data;
public Time(long ntp)
{ data.ntp = ntp; }
public Time(Time time)
{ data.ntp = time.data.ntp; }
public Time()
{ data.sec = 0; data.frac = 1; }
public Time(double seconds)
{ this.SetDouble(seconds); }
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
unsafe private static extern int mpr_time_set(IntPtr l, long r);
unsafe public Time Set(Time time)
{
fixed (void* t = &data)
{
mpr_time_set((IntPtr)t, time.data.ntp);
}
return this;
}
public Time SetDouble(Double seconds)
{
if (seconds > 0.0)
{
data.sec = (UInt32)Math.Floor(seconds);
seconds -= data.sec;
data.frac = (UInt32) (((double)seconds) * 4294967296.0);
}
else
data.ntp = 0;
return this;
}
private static double as_dbl(Time time)
{
return (double)time.data.sec + (double)time.data.frac * 0.00000000023283064365;
}
public Time Add(Time addend)
{
data.sec += addend.data.sec;
data.frac += addend.data.frac;
if (data.frac < addend.data.frac) /* overflow */
++data.sec;
return this;
}
public Time Subtract(Time subtrahend)
{
if (data.sec > subtrahend.data.sec)
{
data.sec -= subtrahend.data.sec;
if (data.frac < subtrahend.data.frac) /* overflow */
--data.sec;
data.frac -= subtrahend.data.frac;
}
else
data.ntp = 0;
return this;
}
public Time Multiply(double multiplicand)
{
if (multiplicand > 0.0)
{
multiplicand *= Time.as_dbl(this);
data.sec = (UInt32) Math.Floor(multiplicand);
multiplicand -= data.sec;
data.frac = (UInt32) (multiplicand * 4294967296.0);
}
else
data.ntp = 0;
return this;
}
/* casting between Time and double */
public static implicit operator double(Time t) => Time.as_dbl(t);
public static explicit operator Time(double d) => new Time(d);
/* Overload some arithmetic operators */
public static Time operator +(Time a, Time b) => new Time(a).Add(b);
public static Time operator -(Time a, Time b) => new Time(a).Subtract(b);
public static Time operator *(Time a, double b) => new Time(a).Multiply(b);
public static bool operator ==(Time a, Time b) => a.data.ntp == b.data.ntp;
public static bool operator !=(Time a, Time b) => a.data.ntp != b.data.ntp;
public static bool operator >(Time a, Time b) => a.data.ntp > b.data.ntp;
public static bool operator <(Time a, Time b) => a.data.ntp < b.data.ntp;
public static bool operator >=(Time a, Time b) => a.data.ntp >= b.data.ntp;
public static bool operator <=(Time a, Time b) => a.data.ntp <= b.data.ntp;
public override bool Equals(object o)
{
if (o == null)
return false;
var second = o as Time;
return second != null && data.ntp == second.data.ntp;
}
public override int GetHashCode()
{
return (int)(data.sec ^ data.frac);
}
public override string ToString() => $"Mapper.Time:{this.data.sec}:{this.data.frac}";
}
public enum Property
{
Bundle = 0x0100,
Data = 0x0200,
Device = 0x0300,
Direction = 0x0400,
Ephemeral = 0x0500,
Expression = 0x0600,
Host = 0x0700,
Id = 0x0800,
IsLocal = 0x0900,
Jitter = 0x0A00,
Length = 0x0B00,
LibVersion = 0x0C00,
Linked = 0x0D00,
Max = 0x0E00,
Min = 0x0F00,
Muted = 0x1000,
Name = 0x1100,
NumInstances = 0x1200,
NumMaps = 0x1300,
NumMapsIn = 0x1400,
NumMapsOut = 0x1500,
NumSigsIn = 0x1600,
NumSigsOut = 0x1700,
Ordinal = 0x1800,
Period = 0x1900,
Port = 0x1A00,
ProcessingLocation = 0x1B00,
Protocol = 0x1C00,
Rate = 0x1D00,
Scope = 0x1E00,
Signal = 0x1F00,
Status = 0x2100,
Stealing = 0x2200,
Synced = 0x2300,
Type = 0x2400,
Unit = 0x2500,
UseInstances = 0x2600,
Version = 0x2700
}
public abstract class Object
{
public enum Status
{
Expired = 0x01,
Staged = 0x02,
Waiting = 0x0E,
Ready = 0x36,
Active = 0x7E,
Reserved = 0x80,
Any = 0xFF
}
public Object()
{
_obj = IntPtr.Zero;
_owned = false;
}
protected Object(IntPtr obj)
{
_obj = obj;
_owned = false;
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
protected static extern int mpr_obj_get_type(IntPtr obj);
public new Type GetType()
{ return (Type) mpr_obj_get_type(_obj); }
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int mpr_obj_get_num_props(IntPtr obj);
public int GetNumProperties()
{ return mpr_obj_get_num_props(_obj); }
unsafe internal object BuildValue(int len, int type, void *value, int property)
{
if (0 == len)
return null;
if (value == null)
return null;
switch (type)
{
case (int)Type.Int32:
if (1 == len)
{
int i = *(int*)value;
switch (property)
{
case (int)Property.Direction:
return (Signal.Direction)i;
case (int)Property.ProcessingLocation:
return (Map.Location)i;
case (int)Property.Protocol:
return (Map.Protocol)i;
case (int)Property.Status:
return (Object.Status)i;
case (int)Property.Stealing:
return (Signal.Stealing)i;
case (int)Property.Type:
return (Type)i;
default:
return i;
}
}
else
{
int[] arr = new int[len];
Marshal.Copy((IntPtr)value, arr, 0, len);
return arr;
}
case (int)Type.Float:
if (1 == len)
return *(float*)value;
else
{
float[] arr = new float[len];
Marshal.Copy((IntPtr)value, arr, 0, len);
return arr;
}
case (int)Type.Double:
if (1 == len)
return *(double*)value;
else
{
double[] arr = new double[len];
Marshal.Copy((IntPtr)value, arr, 0, len);
return arr;
}
case (int)Type.Boolean:
if (1 == len)
return *(int*)value != 0;
else
{
bool[] arr = new bool[len];
for (int i = 0; i < len; i++)
arr[i] = ((int*)value)[i] != 0;
return arr;
}
case (int)Type.Int64:
if (1 == len)
return *(long*)value;
else
{
long[] arr = new long[len];
Marshal.Copy((IntPtr)value, arr, 0, len);
return arr;
}
case (int)Type.Time:
if (1 == len)
return new Time(*(long*)value);
else
{
Time[] arr = new Time[len];
for (int i = 0; i < len; i++)
arr[0] = new Time(((long*)value)[i]);
return arr;
}
case (int)Type.String:
if (1 == len)
return new String(Marshal.PtrToStringAnsi((IntPtr)value));
else
{
String[] arr = new String[len];
for (int i = 0; i < len; i++)
arr[i] = new String(Marshal.PtrToStringAnsi((IntPtr)((char**)value)[i]));
return arr;
}
case (int)Type.Device:
return new Device((IntPtr)value);
case (int)Type.Map:
return new Map((IntPtr)value);
case (int)Type.Signal:
return new Signal((IntPtr)value);
case (int)Type.List:
if (1 == len)
{
switch (property)
{
case (int)Property.Device:
case (int)Property.Linked:
case (int)Property.Scope:
return new List<Device>((IntPtr)value, Type.Device);
case (int)Property.Signal:
return new List<Signal>((IntPtr)value, Type.Signal);
default:
Console.WriteLine("error: missing List type.");
return null;
}
}
else
{
Console.WriteLine("error: arrays of List are not currently supported.");
return null;
}
default:
Console.WriteLine("error: unhandled data type in Object.BuildValue().");
return null;
}
}
// TODO: implement IDictionary?
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
unsafe internal static extern int mpr_obj_get_prop_by_idx(IntPtr obj, int idx, ref char *key,
ref int len, ref int type,
ref void *value, ref int publish);
unsafe public (String, object) GetProperty(int index)
{
char *key = null;
int len = 0;
int type = 0;
void *val = null;
int pub = 0;
int idx = mpr_obj_get_prop_by_idx(this._obj, index, ref key, ref len,
ref type, ref val, ref pub);
if (0 == idx || 0 == len)
return (new String("unknown"), null);
return (new string(Marshal.PtrToStringAnsi((IntPtr)key)), BuildValue(len, type, val, idx));
}
unsafe public object GetProperty(Property property)
{
char *key = null;
int len = 0;
int type = 0;
void *val = null;
int pub = 0;
int idx = mpr_obj_get_prop_by_idx(this._obj, (int)property, ref key, ref len,
ref type, ref val, ref pub);
if (0 == idx || 0 == len)
return null;
return BuildValue(len, type, val, idx);
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
unsafe internal static extern int mpr_obj_get_prop_by_key(IntPtr obj,
[MarshalAs(UnmanagedType.LPStr)] string key,
ref int len, ref int type,
ref void *value, ref int publish);
unsafe public object GetProperty(string key)
{
int len = 0;
int type = 0;
void *val = null;
int pub = 0;
int idx;
idx = mpr_obj_get_prop_by_key(this._obj, key, ref len, ref type, ref val, ref pub);
if (0 == idx || 0 == len)
return null;
return BuildValue(len, type, val, idx);
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
unsafe internal static extern
int mpr_obj_get_prop_as_int32(IntPtr obj, int prop, [MarshalAs(UnmanagedType.LPStr)] string key);
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
unsafe internal static extern
int mpr_obj_set_prop(IntPtr obj, int prop, [MarshalAs(UnmanagedType.LPStr)] string key,
int len, int type, void* value, int publish);
unsafe public Object SetProperty<P, T>(P property, T value, bool publish)
{
int _prop = 0, _pub = Convert.ToInt32(publish);
string _key = null;
switch (property)
{
case Property p: _prop = (int)p; break;
case string s: _key = s; break;
default:
Console.WriteLine("error: unknown property specifier in method SetProperty().");
return this;
}
switch (value)
{
case int i:
mpr_obj_set_prop(_obj, _prop, _key, 1, (int)Type.Int32, (void*)&i, _pub);
break;
case float f:
mpr_obj_set_prop(_obj, _prop, _key, 1, (int)Type.Float, (void*)&f, _pub);
break;
case double d:
mpr_obj_set_prop(_obj, _prop, _key, 1, (int)Type.Double, (void*)&d, _pub);
break;
case bool b:
{
int i = Convert.ToInt32(b);
mpr_obj_set_prop(_obj, _prop, _key, 1, (int)Type.Boolean, (void*)&i, _pub);
}
break;
case string s:
mpr_obj_set_prop(_obj, _prop, _key, 1, (int)Type.String, (void*)Marshal.StringToHGlobalAnsi(s), _pub);
break;
case int[] i:
fixed(int *temp = &i[0])
{
IntPtr intPtr = new IntPtr((void*)temp);
mpr_obj_set_prop(_obj, _prop, _key, i.Length, (int)Type.Int32, (void*)intPtr, _pub);
}
break;
case float[] f:
fixed(float *temp = &f[0])
{
IntPtr intPtr = new IntPtr((void*)temp);
mpr_obj_set_prop(_obj, _prop, _key, f.Length, (int)Type.Float, (void*)intPtr, _pub);
}
break;
case double[] d:
fixed(double *temp = &d[0])
{
IntPtr intPtr = new IntPtr((void*)temp);
mpr_obj_set_prop(_obj, _prop, _key, d.Length, (int)Type.Double, (void*)intPtr, _pub);
}
break;
default:
Console.WriteLine("error: unhandled type in SetProperty().");
break;
}
return this;
}
public Object SetProperty<P, T>(P property, T value)
{
return SetProperty(property, value, true);
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int mpr_obj_remove_prop(IntPtr obj, int prop,
[MarshalAs(UnmanagedType.LPStr)] string key);
public bool RemoveProperty<P>(P property)
{
int _prop = 0;
string _key = null;
switch (property)
{
case Property p: _prop = (int)p; break;
case string s: _key = s; break;
default:
Console.WriteLine("error: unknown property specifier in method SetProperty().");
return false;
}
return 0 != mpr_obj_remove_prop(this._obj, _prop, _key);
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void mpr_obj_push(IntPtr obj);
public Object Push()
{
mpr_obj_push(_obj);
return this;
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_obj_get_graph(IntPtr obj);
public Graph GetGraph()
{
return new Graph(mpr_obj_get_graph(_obj));
}
internal IntPtr _obj;
internal bool _owned;
}
public abstract class _List {
protected Type _type;
private IntPtr _list;
private bool _started;
public _List()
{
_type = Type.Null;
_list = IntPtr.Zero;
_started = false;
}
public _List(IntPtr list, Type type)
{
_type = type;
_list = list;
_started = false;
}
/* copy constructor */
public _List(_List original)
{
_list = mpr_list_get_cpy(original._list);
_type = original._type;
_started = false;
}
// TODO: probably need refcounting to check if we should free the underlying mpr_list
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void mpr_list_free(IntPtr list);
public void Free()
{
mpr_list_free(_list);
_list = IntPtr.Zero;
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int mpr_list_get_size(IntPtr list);
public int Count()
{
return mpr_list_get_size(_list);
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_list_get_idx(IntPtr list, int index);
public IntPtr GetIdx(int index)
{
return mpr_list_get_idx(_list, index);
}
public IntPtr Deref()
{
unsafe {
return new IntPtr(_list != IntPtr.Zero ? *(void**)_list : null);
}
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_list_get_cpy(IntPtr list);
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_list_get_union(IntPtr list1, IntPtr list2);
public _List Join(_List rhs)
{
_list = mpr_list_get_union(_list, mpr_list_get_cpy(rhs._list));
return this;
}
public static IntPtr Union(_List a, _List b)
{
return mpr_list_get_union(mpr_list_get_cpy(a._list), mpr_list_get_cpy(b._list));
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_list_get_isect(IntPtr list1, IntPtr list2);
public _List Intersect(_List rhs)
{
_list = mpr_list_get_isect(_list, mpr_list_get_cpy(rhs._list));
return this;
}
public static IntPtr Intersection(_List a, _List b)
{
return mpr_list_get_isect(mpr_list_get_cpy(a._list), mpr_list_get_cpy(b._list));
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_list_get_diff(IntPtr list1, IntPtr list2);
public _List Subtract(_List rhs)
{
_list = mpr_list_get_diff(_list, mpr_list_get_cpy(rhs._list));
return this;
}
public static IntPtr Difference(_List a, _List b)
{
return mpr_list_get_diff(mpr_list_get_cpy(a._list), mpr_list_get_cpy(b._list));
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_list_get_next(IntPtr list);
public bool GetNext()
{
if (_started)
{
_list = mpr_list_get_next(_list);
}
else
_started = true;
return _list != IntPtr.Zero;
}
public override string ToString() => $"Mapper.List<{_type}>";
}
public class List<T> : _List, IEnumerator, IEnumerable, IDisposable
where T : Object, new()
{
internal List(IntPtr list, Type type) : base(list, type) {}
public T this[int index]
{
get { T t = new T(); t._obj = this.GetIdx(index); return t; }
}
/* Overload some arithmetic operators */
public static List<T> operator +(List<T> a, List<T> b)
=> new List<T>( Union(a, b), a._type );
public static List<T> operator *(List<T> a, List<T> b)
=> new List<T>( Intersection(a, b), a._type );
public static List<T> operator -(List<T> a, List<T> b)
=> new List<T>( Difference(a, b), a._type );
/* Methods for enumeration */
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
public void Reset()
{
// TODO: throw NotSupportedException;
}
public bool MoveNext()
{
return this.GetNext();
}
void IDisposable.Dispose()
{
this.Free();
}
public T Current
{
get
{
T t = new T();
t._obj = this.Deref();
return t;
}
}
object IEnumerator.Current
{
get
{
return Current;
}
}
}
public class Graph : Object
{
private delegate void HandlerDelegate(IntPtr graph, IntPtr obj, int evt, IntPtr data);
public enum Event
{
New, //!< New record has been added to the graph.
Modified, //!< The existing record has been modified.
Removed, //!< The existing record has been removed.
Expired //!< The graph has lost contact with the remote entity.
}
internal Graph(IntPtr obj) : base(obj) {}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_graph_new(int flags);
public Graph(Type flags) : base(mpr_graph_new((int)flags))
{ _owned = true; }
public Graph() : base(mpr_graph_new((int)Type.Object))
{ _owned = true; }
// TODO: check if Graph is used by a Device
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void mpr_graph_free(IntPtr graph);
~Graph()
{ if (_owned) mpr_graph_free(this._obj); }
public new Graph SetProperty<P, T>(P property, T value, bool publish)
{
base.SetProperty(property, value, publish);
return this;
}
public new Graph SetProperty<P, T>(P property, T value)
{
base.SetProperty(property, value, true);
return this;
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void mpr_graph_set_interface(IntPtr graph,
[MarshalAs(UnmanagedType.LPStr)] string iface);
public Graph SetInterface(string iface)
{
mpr_graph_set_interface(this._obj, iface);
return this;
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_graph_get_interface(IntPtr graph);
public string GetInterface()
{
IntPtr iface = mpr_graph_get_interface(this._obj);
return Marshal.PtrToStringAnsi(iface);
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void mpr_graph_set_address(IntPtr graph,
[MarshalAs(UnmanagedType.LPStr)] string group,
int port);
public Graph SetAddress(string group, int port)
{
mpr_graph_set_address(this._obj, group, port);
return this;
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_graph_get_address(IntPtr graph);
public string GetAddress()
{
IntPtr addr = mpr_graph_get_address(this._obj);
return Marshal.PtrToStringAnsi(addr);
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int mpr_graph_poll(IntPtr graph, int block_ms);
public int Poll(int block_ms = 0)
{ return mpr_graph_poll(this._obj, block_ms); }
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void mpr_graph_subscribe(IntPtr graph, IntPtr dev, int types, int timeout);
public Graph Subscribe(Device device, Type types, int timeout = -1)
{
mpr_graph_subscribe(this._obj, device._obj, (int)types, timeout);
return this;
}
public Graph Subscribe(Type types, int timeout = -1)
{
mpr_graph_subscribe(this._obj, IntPtr.Zero, (int)types, timeout);
return this;
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void mpr_graph_unsubscribe(IntPtr graph, IntPtr dev);
public Graph Unsubscribe(Device device)
{
mpr_graph_unsubscribe(this._obj, device._obj);
return this;
}
public Graph Unsubscribe()
{
mpr_graph_unsubscribe(this._obj, IntPtr.Zero);
return this;
}
private void _handler(IntPtr graph, IntPtr obj, int evt, IntPtr data)
{
Type type = (Type) mpr_obj_get_type(obj);
// Event event = (Event) evt;
Object o;
Event e = (Event)evt;
switch (type)
{
case Type.Device:
o = new Device(obj);
break;
case Type.Signal:
o = new Signal(obj);
break;
case Type.Map:
o = new Map(obj);
break;
default:
return;
}
handlers.ForEach(delegate(Handler h)
{
if ((h._types & type) != 0)
h._callback(o, e);
});
}
protected class Handler
{
public Action<Object, Graph.Event> _callback;
public Type _types;
public Handler(Action<Object, Graph.Event> callback, Type types)
{
_callback = callback;
_types = types;
}
}
private System.Collections.Generic.List<Handler> handlers = new System.Collections.Generic.List<Handler>();
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void mpr_graph_add_cb(IntPtr graph, IntPtr handler, int events, IntPtr data);
public Graph AddCallback(Action<Object, Graph.Event> callback, Type types = Type.Object)
{
// TODO: check if handler is already registered
if (handlers.Count == 0)
{
mpr_graph_add_cb(this._obj,
Marshal.GetFunctionPointerForDelegate(new HandlerDelegate(_handler)),
(int)Type.Object,
IntPtr.Zero);
}
handlers.Add(new Handler(callback, types));
return this;
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void mpr_graph_remove_cb(IntPtr graph, IntPtr cb, IntPtr data);
public Graph RemoveCallback(Action<Object, Graph.Event> callback)
{
int i = -1, found = -1;
handlers.ForEach(delegate(Handler h)
{
if (h._callback == callback)
found = i;
++i;
});
if (i >= 0)
{
handlers.RemoveAt(found);
if (handlers.Count == 0)
{
mpr_graph_remove_cb(this._obj,
Marshal.GetFunctionPointerForDelegate(new HandlerDelegate(_handler)),
IntPtr.Zero);
}
}
return this;
}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_graph_get_list(IntPtr graph, int type);
public List<Device> GetDevices()
{
return new List<Device>(mpr_graph_get_list(this._obj, (int)Type.Device), Type.Device);
}
public List<Signal> GetSignals()
{
return new List<Signal>(mpr_graph_get_list(this._obj, (int)Type.Signal), Type.Signal);
}
public List<Map> GetMaps()
{
return new List<Map>(mpr_graph_get_list(this._obj, (int)Type.Map), Type.Map);
}
}
public class Signal : Object
{
private delegate void HandlerDelegate(IntPtr sig, int evt, UInt64 instanceId, int length,
int type, IntPtr value, long time);
public enum Direction
{
Incoming = 0x01, //!< Signal is an input
Outgoing = 0x02, //!< Signal is an output
Any = 0x03, //!< Either incoming or outgoing
Both = 0x04 /*!< Both directions apply. Currently signals cannot be both inputs
* and outputs, so this value is only used for querying device maps
* that touch only local signals. */
}
[Flags]
public enum Event
{
New = 0x01, //!< New instance has been created.
UpstreamRelease = 0x02, //!< Instance was released upstream.
DownstreamRelease = 0x04, //!< Instance was released downstream.
Overflow = 0x08, //!< No local instances left.
Update = 0x10, //!< Instance value has been updated.
All = 0x1F
}
public enum Stealing
{
None, //!< No stealing will take place.
Oldest, //!< Steal the oldest instance.
Newest, //!< Steal the newest instance.
}
private enum HandlerType
{
None,
SingletonInt,
SingletonFloat,
SingletonDouble,
SingletonIntVector,
SingletonFloatVector,
SingletonDoubleVector,
InstancedInt,
InstancedFloat,
InstancedDouble,
InstancedIntVector,
InstancedFloatVector,
InstancedDoubleVector
}
public Signal()
: base() {}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr mpr_sig_get_dev(IntPtr sig);
public Device GetDevice()
{
return new Device(mpr_sig_get_dev(this._obj));
}
public override string ToString()
=> $"Mapper.Signal:{this.GetDevice().GetProperty(Property.Name)}:{this.GetProperty(Property.Name)}";
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void mpr_sig_set_cb(IntPtr sig, IntPtr handler, int events);
// construct from mpr_sig pointer
internal Signal(IntPtr sig) : base(sig) {}
[DllImport("mapper", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
unsafe private static extern void mpr_sig_set_value(IntPtr sig, UInt64 id, int len, int type, void* val);
unsafe private void _SetValue(int value, UInt64 instanceId)
{
mpr_sig_set_value(this._obj, instanceId, 1, (int)Type.Int32, (void*)&value);
}
unsafe private void _SetValue(float value, UInt64 instanceId)
{
mpr_sig_set_value(this._obj, instanceId, 1, (int)Type.Float, (void*)&value);
}
unsafe private void _SetValue(double value, UInt64 instanceId)
{
mpr_sig_set_value(this._obj, instanceId, 1, (int)Type.Double, (void*)&value);