/*--------------------------------------------------------------------------

			S I C S    C A L L B A C K

  Functions needed to deal with the SICSCallback interface. Description is
  in file interface.h, interface.w and interface.w.
  
  Mark Koennecke, Juli 1997
  
       Copyright:

       Labor fuer Neutronenstreuung
       Paul Scherrer Institut
       CH-5423 Villigen-PSI


      The authors hereby grant permission to use, copy, modify, distribute,
      and license this software and its documentation for any purpose, provided
      that existing copyright notices are retained in all copies and that this
      notice is included verbatim in any distributions. No written agreement,
      license, or royalty fee is required for any of the authorized uses.
      Modifications to this software may be copyrighted by their authors
      and need not follow the licensing terms described here, provided that
      the new terms are clearly indicated on the first page of each file where
      they apply.

      IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
      FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
      ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
      DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
      POSSIBILITY OF SUCH DAMAGE.

      THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
      INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
      FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
      IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
      NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
      MODIFICATIONS.
----------------------------------------------------------------------------*/
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "fortify.h"
#include "lld.h"
#include "conman.h"
#include "interface.h" 

#define CALLBACK 17777


/*--------------------- The interface datastructure ---------------------*/
  typedef struct __ICallBack {
                                 int iID;
                                 int iList;
                             } ICallBack;     		
                             
/*-------------- The data stored for a single callback ------------------*/
  typedef struct {
                   long iID;
                   SICSCallBack pFunc;
                   void *pUserData;
                   KillFuncIT pKill;
                   int iEvent;
                 } CallBackItem, *pCallBackItem;
/*------------------------------------------------------------------------*/
  static int CheckPointer(pICallBack self)
  {
     if(self == NULL) return 0;
     if(self->iID != CALLBACK)
     {
       return 0;
     }
     return 1;
  }   
/*-------------------------------------------------------------------------*/

  pICallBack CreateCallBackInterface(void)
  {
    pICallBack pNew = NULL;
    
    pNew = (pICallBack)malloc(sizeof(ICallBack));
    if(!pNew)
    {
      return 0;
    }
    
    pNew->iID = CALLBACK;
    pNew->iList = LLDcreate(sizeof(CallBackItem));
    if(pNew->iList < 0)
    {
      free(pNew);
      return NULL;
    }
    return pNew;
  }
/*--------------------------------------------------------------------------*/
  void DeleteCallBackInterface(pICallBack self)
  {
     int iRet;
     CallBackItem sItem;
     
     if(!CheckPointer(self))
     {
       return;
     }
     
     /* kill all userdata associated with callbacks */
     iRet = LLDnodePtr2First(self->iList);
     while(iRet != 0)
     {
       LLDnodeDataTo(self->iList,&sItem);
       if(sItem.pKill != NULL)
       {
         sItem.pKill(sItem.pUserData);
       }
       iRet = LLDnodePtr2Next(self->iList);
     }
     
     LLDdelete(self->iList);
     free(self);
  } 
/*--------------------------------------------------------------------------*/
  int InvokeCallBack(pICallBack self, int iEvent, void *pEventData)
  {
     CallBackItem sItem;
     int iCurrent, iRet;
     int iResult = 1;
     
     if(!CheckPointer(self))
     {
       return 0;
     }
     
     iCurrent = LLDnodePtr2First(self->iList);
     while(iCurrent != 0)
     {
       LLDnodeDataTo(self->iList,&sItem);
       if(sItem.iEvent == iEvent)
       {
         iRet = sItem.pFunc(iEvent, pEventData,sItem.pUserData);
         if(!iRet)
         {
           iResult = 0;
         } 
       }
       iCurrent = LLDnodePtr2Next(self->iList);
     }
     return iResult;
  }  
/*--------------------------------------------------------------------------*/
  static long lCount = 1L;
  
  long RegisterCallback(pICallBack self, int iEvent,
                                 SICSCallBack pFunc, 
                                 void *pUserData, KillFunc pKFunc)
  {
      CallBackItem sItem;
      
      if(!CheckPointer(self))
      {
        return 0;
      }
      
      sItem.iID = lCount++;
      assert(pFunc);  
      sItem.pFunc = pFunc;
      sItem.iEvent = iEvent;
      sItem.pUserData = pUserData;
      sItem.pKill = pKFunc;
      
      LLDnodeAppendFrom(self->iList,&sItem);
      return sItem.iID;
  }        
/*-------------------------------------------------------------------------*/
  int RemoveCallback(pICallBack self, long lID)
  {
     CallBackItem sItem;
     int iCurrent;
     
     if(!CheckPointer(self))
     {
       return 0;
     }
  
     iCurrent = LLDnodePtr2First(self->iList);
     while(iCurrent != 0)
     {
        LLDnodeDataTo(self->iList,&sItem);
        if(sItem.iID == lID)
        {
           if(sItem.pKill != NULL)
           {
             sItem.pKill(sItem.pUserData);
           }
           LLDnodeDelete(self->iList);
           return 1;
        }
        iCurrent = LLDnodePtr2Next(self->iList);
     }
     return 0;
  }  
/*--------------------------------------------------------------------------*/
  int RemoveCallback2(pICallBack self, void *pUserData)
  {
     CallBackItem sItem;
     int iCurrent;
     
     if(!CheckPointer(self))
     {
       return 0;
     }
  
     iCurrent = LLDnodePtr2First(self->iList);
     while(iCurrent != 0)
     {
        LLDnodeDataTo(self->iList,&sItem);
        if(sItem.pUserData  == pUserData)
        {
           if(sItem.pKill != NULL)
           {
             sItem.pKill(sItem.pUserData);
           }
           LLDnodeDelete(self->iList);
        }
        iCurrent = LLDnodePtr2Next(self->iList);
     }
     return 1;
  }  
