]>
Commit | Line | Data |
---|---|---|
ee6b1d97 SC |
1 | /* ------------------------------------------------------------------------- |
2 | * Project: Mac Notifier Support | |
3 | * Name: macnotfy.c | |
4 | * Author: Stefan CSomor | |
5 | * Purpose: Mac Notifier main file | |
6 | * CVSID: $Id$ | |
7 | * ------------------------------------------------------------------------- | |
8 | */ | |
9 | ||
03e11df5 | 10 | #include "wx/wx.h" |
76a5e5d2 SC |
11 | |
12 | #include "wx/mac/private.h" | |
13 | ||
ee6b1d97 SC |
14 | #include "wx/mac/macnotfy.h" |
15 | ||
16 | const short kMaxEvents = 1000 ; | |
17 | ||
18 | struct wxMacNotificationEvents | |
19 | { | |
20 | short top ; | |
21 | short bottom ; | |
22 | ||
23 | wxMacNotificationProcPtr proc[kMaxEvents] ; | |
24 | unsigned long events[kMaxEvents] ; | |
25 | void* data[kMaxEvents] ; | |
26 | } ; | |
27 | ||
28 | typedef struct wxMacNotificationEvents wxMacNotificationEvents ; | |
29 | wxMacNotificationEvents gMacNotificationEvents ; | |
30 | ||
a959b088 | 31 | ProcessSerialNumber gAppProcess ; |
ee6b1d97 SC |
32 | |
33 | void wxMacWakeUp() | |
34 | { | |
35 | ProcessSerialNumber psn ; | |
36 | Boolean isSame ; | |
37 | psn.highLongOfPSN = 0 ; | |
38 | psn.lowLongOfPSN = kCurrentProcess ; | |
a959b088 | 39 | SameProcess( &gAppProcess , &psn , &isSame ) ; |
ee6b1d97 SC |
40 | if ( isSame ) |
41 | { | |
04f2e990 SC |
42 | #if TARGET_CARBON |
43 | EventRef dummyEvent ; | |
44 | OSStatus err = MacCreateEvent(nil, 'WXMC', 'WXMC', GetCurrentEventTime(), | |
45 | kEventAttributeNone, &dummyEvent); | |
46 | if (err == noErr) | |
47 | { | |
48 | err = PostEventToQueue(GetMainEventQueue(), dummyEvent, | |
49 | kEventPriorityHigh); | |
50 | } | |
51 | #else | |
ee6b1d97 | 52 | PostEvent( nullEvent , 0 ) ; |
04f2e990 | 53 | #endif |
ee6b1d97 SC |
54 | } |
55 | else | |
56 | { | |
a959b088 | 57 | WakeUpProcess( &gAppProcess ) ; |
ee6b1d97 SC |
58 | } |
59 | } | |
60 | ||
61 | void wxMacCreateNotifierTable() | |
62 | { | |
a959b088 | 63 | GetCurrentProcess(&gAppProcess); |
ee6b1d97 SC |
64 | gMacNotificationEvents.top = 0 ; |
65 | gMacNotificationEvents.bottom = 0 ; | |
66 | for ( int i = 0 ; i < kMaxEvents ; ++i ) | |
67 | { | |
68 | gMacNotificationEvents.proc[i] = NULL ; | |
69 | gMacNotificationEvents.events[i] = NULL ; | |
70 | gMacNotificationEvents.data[i] = NULL ; | |
71 | } | |
72 | } | |
73 | ||
74 | void wxMacDestroyNotifierTable() | |
75 | { | |
ee6b1d97 SC |
76 | } |
77 | ||
78 | wxMacNotifierTableRef wxMacGetNotifierTable() | |
79 | { | |
80 | return (wxMacNotifierTableRef) &gMacNotificationEvents ; | |
81 | } | |
82 | ||
83 | void wxMacAddEvent( | |
84 | wxMacNotifierTableRef table , | |
85 | wxMacNotificationProcPtr handler , | |
86 | unsigned long event , | |
87 | void* data , | |
88 | short wakeUp ) | |
89 | { | |
90 | wxMacNotificationEvents *e = (wxMacNotificationEvents *) table ; | |
76a5e5d2 | 91 | wxASSERT_MSG( handler != NULL , "illegal notification proc ptr" ) ; |
ee6b1d97 SC |
92 | /* this should be protected eventually */ |
93 | short index = e->top++ ; | |
94 | ||
95 | if ( e->top == kMaxEvents ) | |
96 | e->top = 0 ; | |
97 | ||
98 | e->proc[index] = handler ; | |
99 | e->events[index] = event ; | |
100 | e->data[index] = data ; | |
101 | if ( wakeUp ) | |
102 | wxMacWakeUp() ; | |
103 | } | |
104 | ||
105 | bool gInProcessing = false ; | |
106 | ||
107 | void wxMacRemoveAllNotifiersForData( wxMacNotifierTableRef table , void* data ) | |
108 | { | |
109 | wxMacNotificationEvents *e = (wxMacNotificationEvents *) table ; | |
110 | /* this should be protected eventually */ | |
111 | short index = e->bottom ; | |
112 | ||
113 | while ( e->top != index ) | |
114 | { | |
115 | if ( index == kMaxEvents ) | |
116 | index = 0 ; | |
117 | if ( e->data[index] == data ) | |
118 | e->data[index] = NULL ; | |
119 | index++ ; | |
120 | } | |
121 | } | |
122 | ||
123 | void wxMacProcessNotifierEvents() | |
124 | { | |
125 | // if ( gInProcessing ) | |
126 | // return ; | |
127 | ||
128 | gInProcessing = true ; | |
2c724d25 | 129 | if ( gMacNotificationEvents.top != gMacNotificationEvents.bottom ) |
ee6b1d97 | 130 | { |
2c724d25 SC |
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 ; | |
134 | if ( count < 0 ) | |
135 | count += kMaxEvents ; | |
136 | ||
137 | while ( count-- ) | |
138 | { | |
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] ; | |
146 | ||
147 | gMacNotificationEvents.data[index] = NULL ; | |
148 | gMacNotificationEvents.events[index] = NULL ; | |
149 | gMacNotificationEvents.proc[index] = NULL ; | |
150 | ||
151 | if ( handler ) | |
152 | handler( event , data ) ; | |
153 | } | |
ee6b1d97 SC |
154 | } |
155 | gInProcessing = false ; | |
156 | } | |
157 | ||
158 | void wxMacProcessNotifierAndPendingEvents() | |
159 | { | |
160 | wxMacProcessNotifierEvents() ; | |
161 | wxTheApp->ProcessPendingEvents() ; | |
162 | } |