1 /* ------------------------------------------------------------------------- 
   2  * Project: Mac Notifier Support 
   4  * Author:  Stefan CSomor 
   5  * Purpose: Mac Notifier main file 
   7  * ------------------------------------------------------------------------- 
  12 #include "wx/mac/private.h" 
  14 #include "wx/mac/macnotfy.h" 
  16 const short kMaxEvents 
= 1000 ; 
  18 struct wxMacNotificationEvents
 
  23     wxMacNotificationProcPtr proc
[kMaxEvents
] ; 
  24     unsigned long events
[kMaxEvents
] ; 
  25     void* data
[kMaxEvents
] ; 
  28 typedef struct wxMacNotificationEvents wxMacNotificationEvents 
; 
  29 static wxMacNotificationEvents gMacNotificationEvents 
; 
  31 static ProcessSerialNumber gAppProcess 
; 
  35     ProcessSerialNumber psn 
; 
  37     psn
.highLongOfPSN 
= 0 ; 
  38     psn
.lowLongOfPSN 
= kCurrentProcess 
; 
  39     SameProcess( &gAppProcess 
, &psn 
, &isSame 
) ; 
  44         OSStatus err 
= MacCreateEvent(nil
, 'WXMC', 'WXMC', GetCurrentEventTime(), 
  45                         kEventAttributeNone
, &dummyEvent
); 
  48             err 
= PostEventToQueue(GetMainEventQueue(), dummyEvent
, 
  52         PostEvent( nullEvent 
, 0 ) ; 
  57         WakeUpProcess( &gAppProcess 
) ; 
  61 void wxMacCreateNotifierTable() 
  63       GetCurrentProcess(&gAppProcess
); 
  64     gMacNotificationEvents
.top 
= 0 ; 
  65     gMacNotificationEvents
.bottom 
= 0 ; 
  66     for ( int i 
= 0 ; i 
< kMaxEvents 
; ++i 
) 
  68         gMacNotificationEvents
.proc
[i
] = NULL 
; 
  69         gMacNotificationEvents
.events
[i
] = NULL 
; 
  70         gMacNotificationEvents
.data
[i
] = NULL 
; 
  74 void wxMacDestroyNotifierTable()  
  78 wxMacNotifierTableRef 
wxMacGetNotifierTable()  
  80     return (wxMacNotifierTableRef
) &gMacNotificationEvents 
; 
  84     wxMacNotifierTableRef table 
,  
  85     wxMacNotificationProcPtr handler 
,  
  90     wxMacNotificationEvents 
*e 
= (wxMacNotificationEvents 
*) table 
; 
  91     wxASSERT_MSG( handler 
!= NULL 
, wxT("illegal notification proc ptr") ) ; 
  92     /* this should be protected eventually */ 
  93     short index 
= e
->top
++ ; 
  95     if ( e
->top 
== kMaxEvents 
) 
  98     e
->proc
[index
] = handler 
; 
  99     e
->events
[index
] = event 
; 
 100     e
->data
[index
] = data 
; 
 105 bool gInProcessing 
= false ; 
 107 void wxMacRemoveAllNotifiersForData( wxMacNotifierTableRef table 
, void* data 
)  
 109     wxMacNotificationEvents 
*e 
= (wxMacNotificationEvents 
*) table 
; 
 110     /* this should be protected eventually */ 
 111     short index 
= e
->bottom 
; 
 113     while ( e
->top 
!= index 
) 
 115         if ( e
->data
[index
] == data 
) 
 116             e
->data
[index
] = NULL 
; 
 118         if ( index 
== kMaxEvents 
) 
 123 void wxMacProcessNotifierEvents() 
 125     //  if ( gInProcessing ) 
 128     gInProcessing 
= true ; 
 129     if ( gMacNotificationEvents
.top 
!= gMacNotificationEvents
.bottom 
) 
 131         // we only should process the notifiers that were here when we entered it 
 132         // otherwise we might never get out...   
 133         short count 
= gMacNotificationEvents
.top 
- gMacNotificationEvents
.bottom 
; 
 135             count 
+= kMaxEvents 
; 
 139             // consume event at bottom 
 140             short index 
= gMacNotificationEvents
.bottom
++ ; 
 141             if ( gMacNotificationEvents
.bottom 
== kMaxEvents 
) 
 142                 gMacNotificationEvents
.bottom 
= 0 ; 
 143             void* data 
= gMacNotificationEvents
.data
[index
] ; 
 144             unsigned long event 
= gMacNotificationEvents
.events
[index
] ; 
 145             wxMacNotificationProcPtr handler 
=  gMacNotificationEvents
.proc
[index
] ; 
 147             gMacNotificationEvents
.data
[index
] = NULL 
; 
 148             gMacNotificationEvents
.events
[index
] = NULL 
; 
 149             gMacNotificationEvents
.proc
[index
]  = NULL 
; 
 152                 handler( event 
, data  
) ; 
 155     gInProcessing 
= false ; 
 158 void wxMacProcessNotifierAndPendingEvents()  
 160     wxMacProcessNotifierEvents() ; 
 161     wxTheApp
->ProcessPendingEvents() ;