using System; using System.Runtime.InteropServices; using System.Collections.Generic; using System.Collections.ObjectModel; namespace DMX2 { public static partial class AlsaSeqLib { static SeqHandleWrapper seq_handle = null; static int clientId; //static int inport; //static int outport; static System.Threading.Thread eventthread=null; static MidiPort systemPort=null; static internal Dictionary openports = new Dictionary(); public class Client { int id; snd_seq_client_type_t type; string name; List ports = new List (); public int Id { get { return id; } } public snd_seq_client_type_t Type { get { return type; } } public string Name { get { return name; } } public ReadOnlyCollection Ports { get { return ports.AsReadOnly (); } } internal Client (PointerWrapper clientInfo) { id = Invoke.snd_seq_client_info_get_client (clientInfo.Pointer); IntPtr nameptr = Invoke.snd_seq_client_info_get_name (clientInfo.Pointer); name = nameptr.PtrToString (); type = (snd_seq_client_type_t)Invoke.snd_seq_client_info_get_type (clientInfo.Pointer); using (PointerWrapper portInfo = new PointerWrapper(GetPortInfoSize ())) { 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)); } } } } /*public static int ClientId { get { return clientId; } }*/ public class Port { int portId; int clientId; string name; uint caps; uint type; public int ClientId { get { return clientId; } } public int PortId { get { return portId; } } public string Name { get { return name; } } public uint Caps { get { return caps; } } public uint Type { get { return type; } } internal Port (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 (); } } public static void Init (string appname) { if (seq_handle != null) return; seq_handle = new SeqHandleWrapper (appname); clientId = Invoke.snd_seq_client_id (seq_handle.Handle); System.Threading.ThreadStart ts = new System.Threading.ThreadStart (EventLoop); eventthread = new System.Threading.Thread (ts); systemPort = new MidiPort ("system"); systemPort.ConnectTo (SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE); } public static void Close () { if (seq_handle == null) return; seq_handle.Dispose (); seq_handle = null; } public static void EventLoop(){ snd_seq_event_t ev = new snd_seq_event_t ();; while (seq_handle != null) { while (seq_handle != null && Invoke.snd_seq_event_input_pending (seq_handle.Handle, 1) > 0) { IntPtr evPtr; // Recup du pointeur vers l'ev Invoke.snd_seq_event_input (seq_handle.Handle, out evPtr); // Copie de la zone mémoire dans une structure managee ev = (snd_seq_event_t)Marshal.PtrToStructure (evPtr, typeof(snd_seq_event_t)); // liberation du pointeur Invoke.snd_seq_free_event (evPtr); if (ev.dest.port == systemPort.portid) { if (ev.type == AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_PORT_START) { } } else { // queue if (openports.ContainsKey (ev.dest.port)) { openports [ev.dest.port].eventqueue.Enqueue (ev); } } } System.Threading.Thread.Sleep (1); } } /*static bool GetEvent (out snd_seq_event_t ev) { if (seq_handle == null) throw new InvalidOperationException (); ev = new snd_seq_event_t (); if (Invoke.snd_seq_event_input_pending (seq_handle.Handle, 1) <= 0) return false; IntPtr evPtr; // Recup du pointeur vers l'ev Invoke.snd_seq_event_input (seq_handle.Handle, out evPtr); // Copie de la zone mémoire dans une structure managee ev = (snd_seq_event_t)Marshal.PtrToStructure (evPtr, typeof(snd_seq_event_t)); // liberation du pointeur Invoke.snd_seq_free_event (evPtr); // TODO : Manage System Events here return true; }*/ static PointerWrapper evOutPtr = new PointerWrapper (32); static IEnumerable EnumClients () { if (seq_handle == null) throw new InvalidOperationException (); using (PointerWrapper clientInfo = new PointerWrapper(GetClientInfoSize())) { Invoke.snd_seq_client_info_set_client (clientInfo.Pointer, -1); while (Invoke.snd_seq_query_next_client(seq_handle.Handle,clientInfo.Pointer)>=0) { yield return new Client (clientInfo); } } } static Client GetClientByID (int client) { if (seq_handle == null) throw new InvalidOperationException (); using (PointerWrapper clientInfo = new PointerWrapper(GetClientInfoSize())) { if (Invoke.snd_seq_get_any_client_info (seq_handle.Handle, client, clientInfo.Pointer) >= 0) { return new Client (clientInfo); } else return null; } } static Port GetPortByIDs (int client, int port) { 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); } else return null; } } } }