Connecting a Task to the Event Broker


Before a task can work with the event broker, it must connect to the event broker by sending a message that requests connection as a listener. To do so, the task sends a message of the flavor EB_Configure, using Portfolio's standard message-sending procedure. The message also specifies input focus, and indicates the event types in which the task is interested.

Creating a Message Port

Before creating a configuration message, a listener task must create its own message port to use as a reply port where the event broker will send messages. To do so, the listener uses the CreateMsgPort()call, which returns the item number of the new message port.

Creating a Configuration Message

After a listener task creates a message port, it must create a message using CreateMsg() or CreateBufferedMsg(). Both calls accept a string that names the message, supplies an optional priority number for the message, and provides the item number of the listener message port. CreateBufferedMsg() also takes the size of the buffer to allocate with the message. Both calls return the item number of the newly created message.

Creating a Configuration Block

To turn the newly created message into a configuration request, the listener task must create a configuration data block, which consists of the ConfigurationRequest structure, shown in Example 1.

Example 1: ConfigurationRequest structure.

typedef struct ConfigurationRequest
{
  EventBrokerHeader                            cr_Header;                     /* { EB_Configure } */
  enum ListenerCategory                            cr_Category;                      /* focus, monitor, or 
                                                 /* hybrid */
  uint32                            cr_TriggerMask[8]; /* events to trigger on */
  uint32                            cr_CaptureMask[8]; /* events to capture */
  int32                            cr_QueueMax;                       /* max # events in 

                                                 /* transit */
  uint32                            rfu[8];                           /* must be 0 for now */
} ConfigurationRequest;

The listener task must fill in the following fields to set the parameters of its configuration request:


Event Types

The 256 different event types available through the event broker are divided into two main bodies:

Both system events and custom events are divided into two types that are defined by the way the event broker reports them to focus-interested tasks:

Table 1 shows how the 256 different bit values in a bit mask are divided into system and custom events, and then UI and non-UI events. Custom event types are not supported in this release of Portfolio.

Table 1:   Flag Bit constants defined in event.h.
-----------------------------------------------------------------
System UI Event Type Constants  |Event Definition                
-----------------------------------------------------------------
EVENTBIT0_ControlButtonPressed  |A controller pad button was     
                                |pressed.                        
-----------------------------------------------------------------
EVENTBIT0_ControlButtonReleased |A controller pad button was     
                                |released.                       
-----------------------------------------------------------------
EVENTBIT0_ControlButtonUpdate   |A controller pad button was     
                                |pressed or released, or control 
                                |button information may be lost  
                                |in an event queue overflow.     
-----------------------------------------------------------------
EVENTBIT0_ControlButtonArrived  |Data from a controller pad      
                                |button has arrived (it arrives  
                                |every field).                   
-----------------------------------------------------------------
EVENTBIT0_MouseButtonPressed    |A mouse button was pressed.     
-----------------------------------------------------------------
EVENTBIT0_MouseButtonReleased   |A mouse button was released.    
-----------------------------------------------------------------
EVENTBIT0_MouseUpdate           |A mouse button was pressed or   
                                |released, the mouse has moved,  
                                |or mouse info may be lost in an 
                                |event queue overflow.           
-----------------------------------------------------------------
EVENTBIT0_MouseMoved            |A mouse has moved.              
-----------------------------------------------------------------
EVENTBIT0_MouseDataArrived      |Data from a mouse has arrived   
                                |(it arrives every field).       
-----------------------------------------------------------------
EVENTBIT0_GunButtonPressed      |A gun button was pressed. This  
                                |is not currently implemented.   
-----------------------------------------------------------------
EVENTBIT0_GunButtonReleased     |A gun button was released. This 
                                |is not currently implemented.   
-----------------------------------------------------------------
EVENTBIT0_GunUpdate             |A gun button was pressed or     
                                |released, or gun button         
                                |information may be lost in an   
                                |event queue overflow. This is   
                                |not currently implemented.      
-----------------------------------------------------------------
EVENTBIT0_GunDataArrived        |Data from a gun has arrived (it 
                                |arrives every field). This is   
                                |not currently implemented.      
-----------------------------------------------------------------
EVENTBIT0_KeyboardKeyPressed    |A keyboard key was pressed. This
                                |is not currently implemented.   
-----------------------------------------------------------------
EVENTBIT0_KeyboardKeyReleased   |A keyboard key was released.    
                                |This is not currently           
                                |implemented.                    
-----------------------------------------------------------------
EVENTBIT0_KeyboardUpdate        |A keyboard key was pressed or   
                                |released, or keyboard           
                                |information may be lost in an   
                                |event queue overflow. This is   
                                |not currently implemented.      
-----------------------------------------------------------------
EVENTBIT0_KeyboardDataArrived   |Data from a keyboard has arrived
                                |(it arrives every field). This  
                                |is not currently implemented.   
-----------------------------------------------------------------
EVENTBIT0_CharacterEntered      |A character was entered. This is
                                |not currently implemented.      
-----------------------------------------------------------------
EVENTBIT0_GivingFocus           |This task was given focus.      
-----------------------------------------------------------------
EVENTBIT0_LosingFocus           |This task lost focus.           
-----------------------------------------------------------------
EVENTBIT0_LightGunButtonPressed |A light gun button was pressed. 
-----------------------------------------------------------------
EVENTBIT0_LightGunButtonReleased|A light gun button was released.
-----------------------------------------------------------------
EVENTBIT0_LightGunUpdate        |A light gun button was pressed  
                                |or released, or light gun button
                                |information may be lost in an   
                                |event queue overflow.           
-----------------------------------------------------------------
EVENTBIT0_LightGunFireTracking  |Data from a light gun is being  
                                |tracked.                        
-----------------------------------------------------------------
EVENTBIT0_LightGunDataArrived   |Data from a light gun has       
                                |arrived (it arrives every       
                                |field).                         
-----------------------------------------------------------------
EVENTBIT0_StickButtonPressed    |A joystick button was pressed.  
-----------------------------------------------------------------
EVENTBIT0_StickButtonReleased   |A joystick button was released. 
-----------------------------------------------------------------
EVENTBIT0_StickUpdate           |A joystick button was pressed or
                                |released, or joystick button    
                                |information may be lost in an   
                                |event queue overflow.           
-----------------------------------------------------------------
EVENTBIT0_StickMoved            |A joystick has moved.           
-----------------------------------------------------------------
EVENTBIT0_StickDataArrived      |Data from a joystick has arrived
                                |(it arrives every field).       
-----------------------------------------------------------------
EVENTBIT0_IRKeyPressed          |An IR key was pressed and a     
                                |button code is returned. This is
                                |device specific. This is not    
                                |currently implemented.          
-----------------------------------------------------------------
EVENTBIT0_IRKeyReleased         |An IR key was released and a    
                                |button code is returned. This is
                                |device specific. This is not    
                                |currently implemented.          
-----------------------------------------------------------------

Table 2 shows the flag bit constants defined in event.h for the system events, along with the meaning for each of the event types. These event type constants define flags for system-defined events, each of which can occur during a single control port polling.

Table 2:  Event type constants that define flags for system-defined events. 
---------------------------------------------------------------
System Non-UI Event Type      |Event Definition                
Constants                     |                                
---------------------------------------------------------------
EVENTBIT2_DeviceOnline        |Media was inserted in a device. 
---------------------------------------------------------------
EVENTBIT2_DeviceOffline       |A device was removed.           
---------------------------------------------------------------
EVENTBIT2_FilesystemMounted   |A file system was mounted.      
---------------------------------------------------------------
EVENTBIT2_FilesystemOffline   |A file system went off-line.    
---------------------------------------------------------------
EVENTBIT2_FilesystemDismounted|A file system was dismounted.   
---------------------------------------------------------------
EVENTBIT2_ControlPortChange   |A new device was plugged into or
                              |disconnected from the control   
                              |port.                           
---------------------------------------------------------------
EVENTBIT2_PleaseSaveAndExit   |This task was requested to save 
                              |current status and exit.        
---------------------------------------------------------------
EVENTBIT2_PleaseExitImmediatel|This task was requested to exit 
y                             |immediately.                    
---------------------------------------------------------------
EVENTBIT2_EventQueueOverflow  |This task's event queue has     
                              |overflowed and the task has lost
                              |events.                         
---------------------------------------------------------------

Setting the Trigger and Capture Masks

Each of the masks in the ConfigurationRequest data structure is an 8-element array of 32-bit words that provides 256 bits, 1 bit for each event type. To set a mask, the task logically ORs the desired constants defined for each word of the array, and stores the results in the appropriate word. The constant name identifies the appropriate word for storage. For example, all the desired flag constants starting with EVENTBIT0 must be logically ORed and then stored in word 0 of the array. All the desired flag constants starting with EVENTBIT2 must be logically ORed and then stored in the second word of the array.

Sending the Configuration Message

Once the ConfigurationRequest data structure has been created and filled in, the listener task must send the message to the event broker. To do this, the task first must find the event broker message port using the FindMsgPort() call, as shown in Example 2.

Example 2: FindMsgPort().

{
Item ebPortItem;

    ebPortItem = FindMsgPort(EventPortName);
    if (ebPortItem < 0)
    {
        /* big trouble, the event broker can't be found! */
    }
}

After the listener task has the event broker message port item number, the task sends its configuration message using SendMsg():

SendMsg(ebPortItem, msg, &configuration, sizeof(configuration));
The SendMsg() call accepts four arguments: the item number of the event broker port, the item number of the message the task has created, a pointer to the ConfigurationRequest data structure the task has initialized, and the size of that data structure.

When the event broker receives the configuration message, it adds the task as a listener. The event broker reads the reply port contained in the message and uses that port whenever it needs to communicate with the listening task. The event broker also reads the ConfigurationRequest data structure and sets up the listener's configuration accordingly.

Receiving the Configuration Reply Message

When the event broker finishes processing the configuration message, it changes the message to a configuration reply message. That message contains a pointer to a single data structure: an EventBrokerHeader with the value EB_ConfigureReply. The event broker returns the message to the task requesting configuration. The receiving task checks the configuration message's result field (msg_Result, which is cast to an Err type) to see if the operation was successful or not. If the value is a negative number, it is an error code, and the configuration was not successful (the task was not connected). If the value is 0 or a positive number, the configuration was successful and the task is connected.