176bb8030Smahlzeit /* 2*5b2d7337SPhilippe Houdoin * Copyright 2002-2009, Haiku, Inc. All rights reserved. 3*5b2d7337SPhilippe Houdoin * Distributed under the terms of the MIT License. 4263f2c60Smahlzeit * 5*5b2d7337SPhilippe Houdoin * Authors: 6*5b2d7337SPhilippe Houdoin * Matthijs Hollemans 77c27cd32Smahlzeit */ 837192de9Smahlzeit #ifndef MIDI_SERVER_APP_H 937192de9Smahlzeit #define MIDI_SERVER_APP_H 1037192de9Smahlzeit 1137192de9Smahlzeit #include <Application.h> 1253284a9fSmahlzeit #include <List.h> 1337192de9Smahlzeit 1494d1c0eaSmahlzeit #include "DeviceWatcher.h" 1594d1c0eaSmahlzeit 1653284a9fSmahlzeit struct app_t; 1753284a9fSmahlzeit struct endpoint_t; 1827d74562Smahlzeit 1976bb8030Smahlzeit // The heart of the midi_server. This BApplication subclass 2076bb8030Smahlzeit // keeps the roster of endpoints and applications, processes 2176bb8030Smahlzeit // incoming messages from libmidi2.so, and notifies the apps 2276bb8030Smahlzeit // when something interesting happens. 2337192de9Smahlzeit class MidiServerApp : public BApplication 2437192de9Smahlzeit { 2537192de9Smahlzeit public: 2653284a9fSmahlzeit 2737192de9Smahlzeit MidiServerApp(); 2827d74562Smahlzeit virtual ~MidiServerApp(); 2927d74562Smahlzeit 3037192de9Smahlzeit virtual void AboutRequested(); 3127d74562Smahlzeit virtual void MessageReceived(BMessage* msg); 3296462df1Sjerl1 3396462df1Sjerl1 private: 3496462df1Sjerl1 3527d74562Smahlzeit typedef BApplication super; 3627d74562Smahlzeit 3753284a9fSmahlzeit void OnRegisterApp(BMessage* msg); 3853284a9fSmahlzeit void OnCreateEndpoint(BMessage* msg); 3953284a9fSmahlzeit void OnDeleteEndpoint(BMessage* msg); 4053284a9fSmahlzeit void OnPurgeEndpoint(BMessage* msg); 4153284a9fSmahlzeit void OnChangeEndpoint(BMessage* msg); 4253284a9fSmahlzeit void OnConnectDisconnect(BMessage* msg); 4353284a9fSmahlzeit 4476bb8030Smahlzeit // Sends an app MSG_ENDPOINT_CREATED notifications for 4576bb8030Smahlzeit // all current endpoints. Used when the app registers. 4653284a9fSmahlzeit bool SendAllEndpoints(app_t* app); 4753284a9fSmahlzeit 4876bb8030Smahlzeit // Sends an app MSG_ENDPOINTS_CONNECTED notifications for 4976bb8030Smahlzeit // all current connections. Used when the app registers. 5053284a9fSmahlzeit bool SendAllConnections(app_t* app); 5153284a9fSmahlzeit 5276bb8030Smahlzeit // Adds the specified endpoint to the roster, and notifies 5376bb8030Smahlzeit // all other applications about this event. 5453284a9fSmahlzeit void AddEndpoint(BMessage* msg, endpoint_t* endp); 5553284a9fSmahlzeit 5676bb8030Smahlzeit // Removes an endpoint from the roster, and notifies all 5776bb8030Smahlzeit // other apps about this event. "app" is the application 5876bb8030Smahlzeit // that the endpoint belongs to; if it is NULL, the app 5976bb8030Smahlzeit // no longer exists and we're purging the endpoint. 6053284a9fSmahlzeit void RemoveEndpoint(app_t* app, endpoint_t* endp); 6153284a9fSmahlzeit 6276bb8030Smahlzeit // Removes a consumer from the list of connections of 6376bb8030Smahlzeit // all the producers it is connected to, just before 6476bb8030Smahlzeit // we remove it from the roster. 6553284a9fSmahlzeit void DisconnectDeadConsumer(endpoint_t* cons); 6653284a9fSmahlzeit 6776bb8030Smahlzeit // Fills up a MSG_ENDPOINT_CREATED message. 6853284a9fSmahlzeit void MakeCreatedNotification(BMessage* msg, endpoint_t* endp); 6953284a9fSmahlzeit 7076bb8030Smahlzeit // Fills up a MSG_ENDPOINTS_(DIS)CONNECTED message. 7153284a9fSmahlzeit void MakeConnectedNotification( 7253284a9fSmahlzeit BMessage* msg, endpoint_t* prod, endpoint_t* cons, bool mustConnect); 7353284a9fSmahlzeit 7476bb8030Smahlzeit // Figures out which application a message came from. 7576bb8030Smahlzeit // Returns NULL if the application is not registered. 7653284a9fSmahlzeit app_t* WhichApp(BMessage* msg); 7753284a9fSmahlzeit 7876bb8030Smahlzeit // Looks at the "midi:id" field from a message, and returns 7976bb8030Smahlzeit // the endpoint object that corresponds to that ID. It also 8076bb8030Smahlzeit // checks whether the application specified by "app" really 8176bb8030Smahlzeit // owns the endpoint. Returns NULL on error. 8253284a9fSmahlzeit endpoint_t* WhichEndpoint(BMessage* msg, app_t* app); 8353284a9fSmahlzeit 8476bb8030Smahlzeit // Returns the endpoint with the specified ID, or 8576bb8030Smahlzeit // NULL if no such endpoint exists on the roster. 8653284a9fSmahlzeit endpoint_t* FindEndpoint(int32 id); 8753284a9fSmahlzeit 8876bb8030Smahlzeit // Sends notification messages to all registered apps, 8976bb8030Smahlzeit // except to the application that triggered the event. 9076bb8030Smahlzeit // The "except" app is allowed to be NULL. 9153284a9fSmahlzeit void NotifyAll(BMessage* msg, app_t* except); 9253284a9fSmahlzeit 9376bb8030Smahlzeit // Sends a notification message to an application, which is 9476bb8030Smahlzeit // not necessarily registered yet. Applications never reply 9576bb8030Smahlzeit // to such notification messages. 9653284a9fSmahlzeit bool SendNotification(app_t* app, BMessage* msg); 9753284a9fSmahlzeit 9876bb8030Smahlzeit // Sends a reply to a request made by an application. 9976bb8030Smahlzeit // If "app" is NULL, the application is not registered 10076bb8030Smahlzeit // (and the reply should contain an error code). 10153284a9fSmahlzeit bool SendReply(app_t* app, BMessage* msg, BMessage* reply); 10253284a9fSmahlzeit 10376bb8030Smahlzeit // Removes an app and all of its endpoints from the roster 10476bb8030Smahlzeit // if a reply or notification message cannot be delivered. 10576bb8030Smahlzeit // (Waiting for communications to fail is actually our only 10676bb8030Smahlzeit // way to get rid of stale endpoints.) 10753284a9fSmahlzeit void DeliveryError(app_t* app); 10853284a9fSmahlzeit 10953284a9fSmahlzeit int32 CountApps(); 11053284a9fSmahlzeit app_t* AppAt(int32 index); 11153284a9fSmahlzeit 11253284a9fSmahlzeit int32 CountEndpoints(); 11353284a9fSmahlzeit endpoint_t* EndpointAt(int32 index); 11453284a9fSmahlzeit 11553284a9fSmahlzeit int32 CountConnections(endpoint_t* prod); 11653284a9fSmahlzeit endpoint_t* ConnectionAt(endpoint_t* prod, int32 index); 11753284a9fSmahlzeit 11876bb8030Smahlzeit // The registered applications. 11953284a9fSmahlzeit BList apps; 12053284a9fSmahlzeit 12176bb8030Smahlzeit // All the endpoints in the system. 12253284a9fSmahlzeit BList endpoints; 12353284a9fSmahlzeit 12476bb8030Smahlzeit // The ID we will assign to the next new endpoint. 12553284a9fSmahlzeit int32 nextId; 12653284a9fSmahlzeit 127*5b2d7337SPhilippe Houdoin // Watch endpoints from /dev/midi drivers. 128*5b2d7337SPhilippe Houdoin DeviceWatcher* fDeviceWatcher; 12994d1c0eaSmahlzeit 13053284a9fSmahlzeit #ifdef DEBUG 13153284a9fSmahlzeit void DumpApps(); 13253284a9fSmahlzeit void DumpEndpoints(); 13353284a9fSmahlzeit #endif 13437192de9Smahlzeit }; 13537192de9Smahlzeit 13637192de9Smahlzeit #endif // MIDI_SERVER_APP_H 137