Committed filthy mess of a working copy solely for moving between machines.
[ardour.git] / libs / midi++2 / midi++ / manager.h
1 /*
2     Copyright (C) 1998 Paul Barton-Davis
3     
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #ifndef __midi_manager_h__
22 #define __midi_manager_h__
23
24 #include <map>
25 #include <string>
26
27 #include <midi++/types.h>
28 #include <midi++/port.h>
29
30 namespace MIDI {
31
32 /** Creates, stores, and manages system MIDI ports.
33  */
34 class Manager {
35   public:
36         ~Manager ();
37         
38         void set_api_data(void* data) { api_data = data; }
39
40         /** Signal the start of an audio cycle.
41          * This MUST be called before any reading/writing for this cycle.
42          * Realtime safe.
43          */
44         void cycle_start(nframes_t nframes);
45
46         /** Signal the end of an audio cycle.
47          * This signifies that the cycle began with @ref cycle_start has ended.
48          * This MUST be called at the end of each cycle.
49          * Realtime safe.
50          */
51         void cycle_end();
52
53         Port *add_port (PortRequest &);
54         int   remove_port (std::string port);
55
56         Port *port (std::string name);
57         Port *port (size_t number);
58
59         size_t    nports () { return ports_by_device.size(); }
60
61         int foreach_port (int (*func)(const Port &, size_t n, void *), 
62                           void *arg);
63
64         typedef std::map<std::string, Port *> PortMap;
65
66         const PortMap& get_midi_ports() const { return ports_by_tag; } 
67
68         static Manager *instance () {
69                 if (theManager == 0) {
70                         theManager = new Manager;
71                 }
72                 return theManager;
73         }
74
75         static int parse_port_request (std::string str, Port::Type type);
76
77   private:
78         /* This is a SINGLETON pattern */
79         
80         Manager ();
81
82         static Manager *theManager;
83         PortMap         ports_by_device; /* canonical */
84         PortMap         ports_by_tag;    /* may contain duplicate Ports */
85
86         void *api_data;
87
88         void close_ports ();
89 };
90
91 }; /* namespace MIDI */
92
93 #endif  // __midi_manager_h__