xref: /haiku/src/kits/midi2/MidiRosterLooper.h (revision 81f5654c124bf46fba0fd251f208e2d88d81e1ce)
1 /*
2  * Copyright (c) 2002-2004 Matthijs Hollemans
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef MIDI_ROSTER_LOOPER_H
24 #define MIDI_ROSTER_LOOPER_H
25 
26 #include <Looper.h>
27 #include <Message.h>
28 
29 class BMidiRoster;
30 
31 namespace BPrivate {
32 
33 // Receives messages from the midi_server on behalf of the
34 // BMidiRoster. Also keeps track of the list of endpoints.
35 class BMidiRosterLooper : public BLooper
36 {
37 public:
38 
39 	BMidiRosterLooper();
40 	virtual ~BMidiRosterLooper();
41 
42 	// Starts up the looper.
43 	bool Init(BMidiRoster* roster);
44 
45 	// Does the work for BMidiRoster::NextEndpoint().
46 	BMidiEndpoint* NextEndpoint(int32* id);
47 
48 	// Finds an endpoint in our list of endpoints.
49 	BMidiEndpoint* FindEndpoint(int32 id);
50 
51 	// Adds an endpoint to our list.
52 	void AddEndpoint(BMidiEndpoint* endp);
53 
54 	// Removes an endpoint from our list. Throws away
55 	// any connections that this endpoint is part of.
56 	void RemoveEndpoint(BMidiEndpoint* endp);
57 
58 	// Invoked when the client app wants to be kept informed
59 	// about changes in the roster. From now on, we will send
60  	// the messenger B_MIDI_EVENT notifications when something
61 	// interesting happens. But first, we send a whole bunch
62 	// of notifications about the registered remote endpoints,
63 	// and the connections between them.
64 	void StartWatching(const BMessenger* watcher);
65 
66 	// From now on, we will no longer send notifications to
67 	// the client when something interesting happens.
68 	void StopWatching();
69 
70 	virtual void MessageReceived(BMessage* msg);
71 
72 private:
73 
74 	friend class BMidiRoster;
75 	friend class BMidiProducer;
76 
77 	typedef BLooper super;
78 
79 	void OnAppRegistered(BMessage* msg);
80 	void OnEndpointCreated(BMessage* msg);
81 	void OnEndpointDeleted(BMessage* msg);
82 	void OnEndpointChanged(BMessage* msg);
83 	void OnConnectedDisconnected(BMessage* msg);
84 
85 	void ChangeRegistered(BMessage* msg, BMidiEndpoint* endp);
86 	void ChangeName(BMessage* msg, BMidiEndpoint* endp);
87 	void ChangeProperties(BMessage* msg, BMidiEndpoint* endp);
88 	void ChangeLatency(BMessage* msg, BMidiEndpoint* endp);
89 
90 	// Removes the consumer from the list of connections of
91 	// all the producers it is connected to. Also sends out
92 	// B_MIDI_EVENT "disconnected" notifications if the
93 	// consumer is remote and the client is watching.
94 	void DisconnectDeadConsumer(BMidiConsumer* cons);
95 
96 	// Sends out B_MIDI_EVENT "disconnected" notifications
97 	// if the producer is remote and the client is watching.
98 	void DisconnectDeadProducer(BMidiProducer* prod);
99 
100 	// Sends B_MIDI_EVENT notifications for all registered
101 	// remote endpoints to the watcher. Used when the client
102 	// calls StartWatching().
103 	void AllEndpoints();
104 
105 	// Sends B_MIDI_EVENT notifications for the connections
106 	// between all registered remote endpoints to the watcher.
107 	// Used when the client calls StartWatching().
108 	void AllConnections();
109 
110 	// Sends a B_MIDI_EVENT notification to the watcher
111 	// when another application changes the attributes
112 	// of one of its endpoints.
113 	void ChangeEvent(BMessage* msg, BMidiEndpoint* endp);
114 
115 	// Sends a B_MIDI_EVENT notification to the watcher
116 	// when another application connects or disconnects
117 	// the two endpoints.
118 	void ConnectionEvent(
119 		BMidiProducer* prod, BMidiConsumer* cons, bool mustConnect);
120 
121 	int32 CountEndpoints();
122 	BMidiEndpoint* EndpointAt(int32 index);
123 
124 	BMidiRoster* roster;
125 
126 	// Makes sure BMidiRoster::MidiRoster() does not return
127 	// until confirmation from the midi_server is received.
128 	sem_id initLock;
129 
130 	// The object we send B_MIDI_EVENT notifications to.
131 	BMessenger* watcher;
132 
133 	// All the endpoints in the system, local and remote.
134 	BList endpoints;
135 
136 	#ifdef DEBUG
137 	void DumpEndpoints();
138 	#endif
139 };
140 
141 } // namespace BPrivate
142 
143 #endif // MIDI_ROSTER_LOOPER_H
144