diff --git a/DMX-2.0/AlsaSeqLib.cs b/DMX-2.0/AlsaSeqLib.cs index c3fa9e1..1c096d0 100644 --- a/DMX-2.0/AlsaSeqLib.cs +++ b/DMX-2.0/AlsaSeqLib.cs @@ -12,10 +12,16 @@ namespace DMX2 //static int inport; //static int outport; - static System.Threading.Thread eventthread=null; - static MidiPort systemPort=null; + static System.Threading.Thread eventthread = null; + static MidiPort systemPort = null; - static internal Dictionary openports = new Dictionary(); + static internal Dictionary openports = new Dictionary(); + + public static int ClientId{ + get { + return clientId; + } + } public class Client { @@ -59,7 +65,7 @@ namespace DMX2 Invoke.snd_seq_port_info_set_client (portInfo.Pointer, id); Invoke.snd_seq_port_info_set_port (portInfo.Pointer, -1); while (Invoke.snd_seq_query_next_port(seq_handle.Handle, portInfo.Pointer) >= 0) { - ports.Add (new Port (portInfo)); + ports.Add (Port.GetPort(portInfo)); } } } @@ -78,6 +84,7 @@ namespace DMX2 string name; uint caps; uint type; + int srcid; public int ClientId { get { @@ -109,15 +116,53 @@ namespace DMX2 } } - internal Port (PointerWrapper portInfo) + public int SrcId + { + get + { + return srcid; + } + } + + private Port(int _clientId, int _portId) + { + clientId = _clientId; + portId = _portId; + srcid = clientId << 8 + portId; + } + + private void Updateinfo (PointerWrapper portInfo) { - clientId = Invoke.snd_seq_port_info_get_client (portInfo.Pointer); - portId = Invoke.snd_seq_port_info_get_port (portInfo.Pointer); IntPtr namePtr = Invoke.snd_seq_port_info_get_name (portInfo.Pointer); caps = Invoke.snd_seq_port_info_get_capability (portInfo.Pointer); type = Invoke.snd_seq_port_info_get_type (portInfo.Pointer); name = namePtr.PtrToString (); } + + internal static Port GetPort(PointerWrapper portInfo) + { + Port p; + int clientId = Invoke.snd_seq_port_info_get_client(portInfo.Pointer); + int portId = Invoke.snd_seq_port_info_get_port(portInfo.Pointer); + int srcid = clientId << 8 + portId; + + if (ports.ContainsKey(srcid)) + p = ports[srcid]; + else + p = new Port(clientId, portId); + + p.Updateinfo(portInfo); + return p; + } + internal static Port GetPort(int clientId, int portId) + { + int srcid = clientId << 8 + portId; + if (ports.ContainsKey(srcid)) + return ports[srcid]; + return null; + } + + static Dictionary ports = new Dictionary(); } public static void Init (string appname) @@ -131,7 +176,7 @@ namespace DMX2 System.Threading.ThreadStart ts = new System.Threading.ThreadStart (EventLoop); eventthread = new System.Threading.Thread (ts); - + eventthread.Start(); systemPort = new MidiPort ("system"); systemPort.ConnectTo (SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE); } @@ -198,7 +243,7 @@ namespace DMX2 static PointerWrapper evOutPtr = new PointerWrapper (32); - static IEnumerable EnumClients () + public static IEnumerable EnumClients () { if (seq_handle == null) throw new InvalidOperationException (); @@ -210,7 +255,7 @@ namespace DMX2 } } - static Client GetClientByID (int client) + public static Client GetClientByID (int client) { if (seq_handle == null) throw new InvalidOperationException (); @@ -222,15 +267,15 @@ namespace DMX2 } } - static Port GetPortByIDs (int client, int port) + public static Port GetPortByIDs (int clientid, int portid) { if (seq_handle == null) throw new InvalidOperationException (); using (PointerWrapper portInfo = new PointerWrapper(GetPortInfoSize ())) { - if (Invoke.snd_seq_get_any_port_info (seq_handle.Handle, client, port, portInfo.Pointer) >= 0) { - return new Port (portInfo); + if (Invoke.snd_seq_get_any_port_info (seq_handle.Handle, clientid, portid, portInfo.Pointer) >= 0) { + return Port.GetPort (portInfo); } else - return null; + return Port.GetPort (clientid,portid); } } diff --git a/DMX-2.0/MidiEventProvider.cs b/DMX-2.0/MidiEventProvider.cs index ba0ee8e..5c83a39 100644 --- a/DMX-2.0/MidiEventProvider.cs +++ b/DMX-2.0/MidiEventProvider.cs @@ -38,7 +38,7 @@ namespace DMX2 /// /// Liste des ports connectés avec feedback /// - readonly List feedbacksources = new List (); + readonly List feedbacksources = new List (); //static readonly Dictionary srcidToDev = new Dictionary(); readonly List unpaginatedchannels = new List (); @@ -139,7 +139,7 @@ namespace DMX2 public void RefreshFeedback (string name) { - foreach (int port in knowndevices[name].ConnectedPorts) { + foreach (AlsaSeqLib.Port port in knowndevices[name].ConnectedPorts) { if (knowndevices [name].HasFeedback) { if (!feedbacksources.Contains (port)) feedbacksources.Add (port); @@ -156,10 +156,8 @@ namespace DMX2 return; knowndevices.Remove (dev.Name); - foreach (int connectedport in dev.ConnectedPorts) { - int client = connectedport >> 8; - int port = connectedport & 0xFF; - AlsaSeqLib.Deconnecte (eventmidiport, client, port); + foreach (AlsaSeqLib.Port connectedport in dev.ConnectedPorts) { + midiport.Deconnecte ( connectedport); } } @@ -212,13 +210,14 @@ namespace DMX2 string fullportname = cli.Name + ':' + p.Name; if (knowndevices.ContainsKey (fullportname)) { int srcid = p.ClientId << 8 + p.PortId; - if (knowndevices [fullportname].ConnectedPorts.Contains (srcid)) + if (knowndevices [fullportname].ConnectedPorts.Contains (p)) return; - AlsaSeqLib.Connect (eventmidiport,p); + midiport.ConnectFrom(p); + midiport.ConnectTo(p); } } - + void PortConnect (AlsaSeqLib.snd_seq_connect_t cn, bool connect) { int clientId, portId; @@ -230,16 +229,18 @@ namespace DMX2 portId = cn.dest.port; } - int srcid = clientId << 8 + portId; + AlsaSeqLib.Port p = AlsaSeqLib.GetPortByIDs(clientId, portId); + if (connect) { - string fpname = AlsaSeqLib.GetClientByID (clientId).Name + ":" + AlsaSeqLib.GetPortByIDs (clientId, portId).Name; + AlsaSeqLib.Client c = AlsaSeqLib.GetClientByID(clientId); + string fpname = c.Name + ":" + p.Name; if (!knowndevices.ContainsKey (fpname)) return; - if (knowndevices [fpname].ConnectedPorts.Contains (srcid)) + if (knowndevices [fpname].ConnectedPorts.Contains (p)) return; - knowndevices [fpname].ConnectedPorts.Add (srcid); + knowndevices [fpname].ConnectedPorts.Add (p); if (knowndevices [fpname].HasFeedback) - feedbacksources.Add (srcid); + feedbacksources.Add (p); guirefreshflag = true; //srcidToDev[srcid] = knowndevices [fpname]; @@ -248,10 +249,8 @@ namespace DMX2 } foreach (var dev in knowndevices.Values) { - if (dev.ConnectedPorts.Contains (srcid)) { - /*if(srcidToDev.ContainsKey(srcid)) - srcidToDev.Remove(srcid);*/ - dev.ConnectedPorts.Remove (srcid); + if (dev.ConnectedPorts.Contains (p)) { + dev.ConnectedPorts.Remove (p); guirefreshflag = true; return; } @@ -266,7 +265,7 @@ namespace DMX2 } } - protected bool HasFeedback (int source) + protected bool HasFeedback (AlsaSeqLib.Port source) { return feedbacksources.Contains (source); } @@ -275,14 +274,14 @@ namespace DMX2 { midiport.SendEvent (ev); } - + public void Refresh () { foreach (var ievent in eventlist.Values) { if (ievent.Page == page) { ievent.SendFeedback(); - foreach (int src in feedbacksources) { - int lnvk = CombineHash (src, ievent.MidiEvCode); + foreach (AlsaSeqLib.Port src in feedbacksources) { + int lnvk = CombineHash (src.SrcId, ievent.MidiEvCode); if (ievent.LastKnownValue != -1) lastValueOfSrc [lnvk] = (byte)ievent.LastKnownValue; } @@ -789,13 +788,13 @@ namespace DMX2 #region IFeedbackInfo implementation bool IFeedbackInfo.FeedBack (byte data) { - + iev.LastKnownValue = data; if (prov.CurrentPage == iev.Page || iev.Page == 0) { iev.SendFeedback(); - foreach (int src in prov.feedbacksources) { - int lnvk = CombineHash (src, iev.MidiEvCode); + foreach (AlsaSeqLib.Port src in prov.feedbacksources) { + int lnvk = CombineHash (src.SrcId, iev.MidiEvCode); if (iev.LastKnownValue != -1) prov.lastValueOfSrc [lnvk] = (byte)iev.LastKnownValue; } @@ -814,9 +813,9 @@ namespace DMX2 public bool HasFeedback { get; set; } - readonly List connected = new List (); + readonly List connected = new List (); - public List ConnectedPorts { + public List ConnectedPorts { get{ return connected;} }