1 /* 2 * Copyright 2002-2009, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Matthijs Hollemans 7 */ 8 #ifndef MIDI_SERVER_APP_H 9 #define MIDI_SERVER_APP_H 10 11 #include <Application.h> 12 #include <List.h> 13 14 #include "DeviceWatcher.h" 15 16 struct app_t; 17 struct endpoint_t; 18 19 // The heart of the midi_server. This BApplication subclass 20 // keeps the roster of endpoints and applications, processes 21 // incoming messages from libmidi2.so, and notifies the apps 22 // when something interesting happens. 23 class MidiServerApp : public BApplication 24 { 25 public: 26 27 MidiServerApp(); 28 virtual ~MidiServerApp(); 29 30 virtual void AboutRequested(); 31 virtual void MessageReceived(BMessage* msg); 32 33 private: 34 35 typedef BApplication super; 36 37 void OnRegisterApp(BMessage* msg); 38 void OnCreateEndpoint(BMessage* msg); 39 void OnDeleteEndpoint(BMessage* msg); 40 void OnPurgeEndpoint(BMessage* msg); 41 void OnChangeEndpoint(BMessage* msg); 42 void OnConnectDisconnect(BMessage* msg); 43 44 // Sends an app MSG_ENDPOINT_CREATED notifications for 45 // all current endpoints. Used when the app registers. 46 bool SendAllEndpoints(app_t* app); 47 48 // Sends an app MSG_ENDPOINTS_CONNECTED notifications for 49 // all current connections. Used when the app registers. 50 bool SendAllConnections(app_t* app); 51 52 // Adds the specified endpoint to the roster, and notifies 53 // all other applications about this event. 54 void AddEndpoint(BMessage* msg, endpoint_t* endp); 55 56 // Removes an endpoint from the roster, and notifies all 57 // other apps about this event. "app" is the application 58 // that the endpoint belongs to; if it is NULL, the app 59 // no longer exists and we're purging the endpoint. 60 void RemoveEndpoint(app_t* app, endpoint_t* endp); 61 62 // Removes a consumer from the list of connections of 63 // all the producers it is connected to, just before 64 // we remove it from the roster. 65 void DisconnectDeadConsumer(endpoint_t* cons); 66 67 // Fills up a MSG_ENDPOINT_CREATED message. 68 void MakeCreatedNotification(BMessage* msg, endpoint_t* endp); 69 70 // Fills up a MSG_ENDPOINTS_(DIS)CONNECTED message. 71 void MakeConnectedNotification( 72 BMessage* msg, endpoint_t* prod, endpoint_t* cons, bool mustConnect); 73 74 // Figures out which application a message came from. 75 // Returns NULL if the application is not registered. 76 app_t* WhichApp(BMessage* msg); 77 78 // Looks at the "midi:id" field from a message, and returns 79 // the endpoint object that corresponds to that ID. It also 80 // checks whether the application specified by "app" really 81 // owns the endpoint. Returns NULL on error. 82 endpoint_t* WhichEndpoint(BMessage* msg, app_t* app); 83 84 // Returns the endpoint with the specified ID, or 85 // NULL if no such endpoint exists on the roster. 86 endpoint_t* FindEndpoint(int32 id); 87 88 // Sends notification messages to all registered apps, 89 // except to the application that triggered the event. 90 // The "except" app is allowed to be NULL. 91 void NotifyAll(BMessage* msg, app_t* except); 92 93 // Sends a notification message to an application, which is 94 // not necessarily registered yet. Applications never reply 95 // to such notification messages. 96 bool SendNotification(app_t* app, BMessage* msg); 97 98 // Sends a reply to a request made by an application. 99 // If "app" is NULL, the application is not registered 100 // (and the reply should contain an error code). 101 bool SendReply(app_t* app, BMessage* msg, BMessage* reply); 102 103 // Removes an app and all of its endpoints from the roster 104 // if a reply or notification message cannot be delivered. 105 // (Waiting for communications to fail is actually our only 106 // way to get rid of stale endpoints.) 107 void DeliveryError(app_t* app); 108 109 int32 CountApps(); 110 app_t* AppAt(int32 index); 111 112 int32 CountEndpoints(); 113 endpoint_t* EndpointAt(int32 index); 114 115 int32 CountConnections(endpoint_t* prod); 116 endpoint_t* ConnectionAt(endpoint_t* prod, int32 index); 117 118 // The registered applications. 119 BList apps; 120 121 // All the endpoints in the system. 122 BList endpoints; 123 124 // The ID we will assign to the next new endpoint. 125 int32 nextId; 126 127 // Watch endpoints from /dev/midi drivers. 128 DeviceWatcher* fDeviceWatcher; 129 130 #ifdef DEBUG 131 void DumpApps(); 132 void DumpEndpoints(); 133 #endif 134 }; 135 136 #endif // MIDI_SERVER_APP_H 137