10 typedef struct W_Notification
{
18 extern void W_FlushASAPNotificationQueue();
22 WMGetNotificationName(WMNotification
*notification
)
24 return notification
->name
;
29 WMGetNotificationObject(WMNotification
*notification
)
31 return notification
->object
;
36 WMGetNotificationClientData(WMNotification
*notification
)
38 return notification
->clientData
;
43 WMCreateNotification(char *name
, void *object
, void *clientData
)
47 nPtr
= wmalloc(sizeof(Notification
));
50 nPtr
->object
= object
;
51 nPtr
->clientData
= clientData
;
60 WMReleaseNotification(WMNotification
*notification
)
62 notification
->refCount
--;
64 if (notification
->refCount
< 1) {
71 WMRetainNotification(WMNotification
*notification
)
73 notification
->refCount
++;
79 /***************** Notification Center *****************/
81 typedef struct NotificationObserver
{
82 WMNotificationObserverAction
*observerAction
;
88 struct NotificationObserver
*prev
; /* for tables */
89 struct NotificationObserver
*next
;
90 struct NotificationObserver
*nextAction
; /* for observerTable */
91 } NotificationObserver
;
94 typedef struct W_NotificationCenter
{
95 WMHashTable
*nameTable
; /* names -> observer lists */
96 WMHashTable
*objectTable
; /* object -> observer lists */
97 NotificationObserver
*nilList
; /* obervers that catch everything */
99 WMHashTable
*observerTable
; /* observer -> NotificationObserver */
100 } NotificationCenter
;
103 /* default (and only) center */
104 static NotificationCenter
*notificationCenter
= NULL
;
108 W_InitNotificationCenter(void)
110 notificationCenter
= wmalloc(sizeof(NotificationCenter
));
112 notificationCenter
->nameTable
= WMCreateHashTable(WMStringPointerHashCallbacks
);
113 notificationCenter
->objectTable
= WMCreateHashTable(WMIntHashCallbacks
);
114 notificationCenter
->nilList
= NULL
;
116 notificationCenter
->observerTable
= WMCreateHashTable(WMIntHashCallbacks
);
121 WMAddNotificationObserver(WMNotificationObserverAction
*observerAction
,
122 void *observer
, char *name
, void *object
)
124 NotificationObserver
*oRec
, *rec
;
126 oRec
= wmalloc(sizeof(NotificationObserver
));
127 oRec
->observerAction
= observerAction
;
128 oRec
->observer
= observer
;
130 oRec
->object
= object
;
135 /* put this action in the list of actions for this observer */
136 rec
= WMHashInsert(notificationCenter
->observerTable
, observer
, oRec
);
139 /* if this is not the first action for the observer */
140 oRec
->nextAction
= rec
;
142 oRec
->nextAction
= NULL
;
145 if (!name
&& !object
) {
147 oRec
->next
= notificationCenter
->nilList
;
148 if (notificationCenter
->nilList
) {
149 notificationCenter
->nilList
->prev
= oRec
;
151 notificationCenter
->nilList
= oRec
;
153 /* any message coming from object */
154 rec
= WMHashInsert(notificationCenter
->objectTable
, object
, oRec
);
160 /* name && (object || !object) */
161 rec
= WMHashInsert(notificationCenter
->nameTable
, name
, oRec
);
171 WMPostNotification(WMNotification
*notification
)
173 NotificationObserver
*orec
, *tmp
;
175 WMRetainNotification(notification
);
177 /* tell the observers that want to know about a particular message */
178 orec
= WMHashGet(notificationCenter
->nameTable
, notification
->name
);
183 if (!orec
->object
|| !notification
->object
184 || orec
->object
== notification
->object
) {
185 /* tell the observer */
186 if (orec
->observerAction
) {
187 (*orec
->observerAction
)(orec
->observer
, notification
);
194 /* tell the observers that want to know about an object */
195 orec
= WMHashGet(notificationCenter
->objectTable
, notification
->object
);
200 /* tell the observer */
201 if (orec
->observerAction
) {
202 (*orec
->observerAction
)(orec
->observer
, notification
);
207 /* tell the catch all observers */
208 orec
= notificationCenter
->nilList
;
212 /* tell the observer */
213 if (orec
->observerAction
) {
214 (*orec
->observerAction
)(orec
->observer
, notification
);
219 WMReleaseNotification(notification
);
221 W_FlushASAPNotificationQueue();
226 WMRemoveNotificationObserver(void *observer
)
228 NotificationObserver
*orec
, *tmp
, *rec
;
230 /* get the list of actions the observer is doing */
231 orec
= WMHashGet(notificationCenter
->observerTable
, observer
);
234 * FOREACH orec IN actionlist for observer
236 * remove from respective lists/tables
241 tmp
= orec
->nextAction
;
243 if (!orec
->name
&& !orec
->object
) {
245 if (notificationCenter
->nilList
==orec
)
246 notificationCenter
->nilList
= orec
->next
;
247 } else if (!orec
->name
) {
248 /* any message coming from object */
249 rec
= WMHashGet(notificationCenter
->objectTable
, orec
->object
);
251 /* replace table entry */
253 WMHashInsert(notificationCenter
->objectTable
, orec
->object
,
256 WMHashRemove(notificationCenter
->objectTable
, orec
->object
);
260 /* name && (object || !object) */
261 rec
= WMHashGet(notificationCenter
->nameTable
, orec
->name
);
263 /* replace table entry */
265 WMHashInsert(notificationCenter
->nameTable
, orec
->name
,
268 WMHashRemove(notificationCenter
->nameTable
, orec
->name
);
273 orec
->prev
->next
= orec
->next
;
275 orec
->next
->prev
= orec
->prev
;
282 WMHashRemove(notificationCenter
->observerTable
, observer
);
287 WMRemoveNotificationObserverWithName(void *observer
, char *name
, void *object
)
289 NotificationObserver
*orec
, *tmp
, *rec
;
290 NotificationObserver
*newList
= NULL
;
292 /* get the list of actions the observer is doing */
293 orec
= WMHashGet(notificationCenter
->observerTable
, observer
);
295 WMHashRemove(notificationCenter
->observerTable
, observer
);
297 /* rebuild the list of actions for the observer */
300 tmp
= orec
->nextAction
;
301 if (orec
->name
== name
&& orec
->object
== object
) {
302 if (!name
&& !object
) {
303 if (notificationCenter
->nilList
== orec
)
304 notificationCenter
->nilList
= orec
->next
;
306 rec
= WMHashGet(notificationCenter
->objectTable
, orec
->object
);
308 assert(rec
->prev
==NULL
);
309 /* replace table entry */
311 WMHashInsert(notificationCenter
->objectTable
,
312 orec
->object
, orec
->next
);
314 WMHashRemove(notificationCenter
->objectTable
,
319 rec
= WMHashGet(notificationCenter
->nameTable
, orec
->name
);
321 assert(rec
->prev
==NULL
);
322 /* replace table entry */
324 WMHashInsert(notificationCenter
->nameTable
,
325 orec
->name
, orec
->next
);
327 WMHashRemove(notificationCenter
->nameTable
,
334 orec
->prev
->next
= orec
->next
;
336 orec
->next
->prev
= orec
->prev
;
339 /* append this action in the new action list */
340 orec
->nextAction
= NULL
;
344 NotificationObserver
*p
;
347 while (p
->nextAction
) {
350 p
->nextAction
= orec
;
356 /* reinsert the list to the table */
358 WMHashInsert(notificationCenter
->observerTable
, observer
, newList
);
364 WMPostNotificationName(char *name
, void *object
, void *clientData
)
366 WMNotification
*notification
;
368 notification
= WMCreateNotification(name
, object
, clientData
);
370 WMPostNotification(notification
);
372 WMReleaseNotification(notification
);
377 /**************** Notification Queues ****************/
380 typedef struct W_NotificationQueue
{
384 struct W_NotificationQueue
*next
;
388 static WMNotificationQueue
*notificationQueueList
= NULL
;
391 static WMNotificationQueue
*notificationQueue
= NULL
;
395 WMGetDefaultNotificationQueue(void)
397 if (!notificationQueue
)
398 notificationQueue
= WMCreateNotificationQueue();
400 return notificationQueue
;
405 WMCreateNotificationQueue(void)
407 NotificationQueue
*queue
;
409 queue
= wmalloc(sizeof(NotificationQueue
));
411 queue
->asapQueue
= WMCreateBag(8);
412 queue
->idleQueue
= WMCreateBag(8);
413 queue
->next
= notificationQueueList
;
415 notificationQueueList
= queue
;
423 WMEnqueueNotification(WMNotificationQueue
*queue
, WMNotification
*notification
,
424 WMPostingStyle postingStyle
)
426 WMEnqueueCoalesceNotification(queue
, notification
, postingStyle
,
427 WNCOnName
|WNCOnSender
);
433 WMDequeueNotificationMatching(WMNotificationQueue
*queue
,
434 WMNotification
*notification
, unsigned mask
)
439 if (mask
& WNCOnName
) {
440 for (i
= 0; i
< WMGetBagItemCount(queue
->asapQueue
); i
++) {
441 tmp
= WMGetFromBag(queue
->asapQueue
, i
);
443 if (strcmp(notification
->name
, tmp
->name
) == 0) {
444 WMRemoveFromBag(queue
->asapQueue
, tmp
);
445 WMReleaseNotification(tmp
);
449 for (i
= 0; i
< WMGetBagItemCount(queue
->idleQueue
); i
++) {
450 tmp
= WMGetFromBag(queue
->idleQueue
, i
);
452 if (strcmp(notification
->name
, tmp
->name
) == 0) {
453 WMRemoveFromBag(queue
->idleQueue
, tmp
);
454 WMReleaseNotification(tmp
);
459 if (mask
& WNCOnSender
) {
460 for (i
= 0; i
< WMGetBagItemCount(queue
->asapQueue
); i
++) {
461 tmp
= WMGetFromBag(queue
->asapQueue
, i
);
463 if (notification
->object
== tmp
->object
) {
464 WMRemoveFromBag(queue
->asapQueue
, tmp
);
465 WMReleaseNotification(tmp
);
469 for (i
= 0; i
< WMGetBagItemCount(queue
->idleQueue
); i
++) {
470 tmp
= WMGetFromBag(queue
->idleQueue
, i
);
472 if (notification
->object
== tmp
->object
) {
473 WMRemoveFromBag(queue
->idleQueue
, tmp
);
474 WMReleaseNotification(tmp
);
483 WMEnqueueCoalesceNotification(WMNotificationQueue
*queue
,
484 WMNotification
*notification
,
485 WMPostingStyle postingStyle
,
486 unsigned coalesceMask
)
488 if (coalesceMask
!= WNCNone
)
489 WMDequeueNotificationMatching(queue
, notification
, coalesceMask
);
491 switch (postingStyle
) {
493 WMPostNotification(notification
);
497 WMPutInBag(queue
->asapQueue
, notification
);
501 WMPutInBag(queue
->idleQueue
, notification
);
508 W_FlushASAPNotificationQueue()
510 WMNotificationQueue
*queue
= notificationQueueList
;
513 while (WMGetBagItemCount(queue
->asapQueue
)) {
514 WMNotification
*tmp
= WMGetFromBag(queue
->asapQueue
, 0);
516 WMPostNotification(tmp
);
517 WMDeleteFromBag(queue
->asapQueue
, 0);
526 W_FlushIdleNotificationQueue()
528 WMNotificationQueue
*queue
= notificationQueueList
;
531 while (WMGetBagItemCount(queue
->idleQueue
)) {
532 WMNotification
*tmp
= WMGetFromBag(queue
->idleQueue
, 0);
534 WMPostNotification(tmp
);
535 WMDeleteFromBag(queue
->idleQueue
, 0);