~profzoom/ubuntu/quantal/wmaker/bug-1079925

« back to all changes in this revision

Viewing changes to WINGs/notification.c

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2004-11-10 14:05:30 UTC
  • Revision ID: james.westby@ubuntu.com-20041110140530-qpd66b5lm38x7apk
Tags: upstream-0.91.0
ImportĀ upstreamĀ versionĀ 0.91.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <stdlib.h>
 
3
#include <assert.h>
 
4
#include <stdio.h>
 
5
#include <string.h>
 
6
 
 
7
#include "WUtil.h"
 
8
 
 
9
 
 
10
typedef struct W_Notification {
 
11
    const char *name;
 
12
    void *object;
 
13
    void *clientData;
 
14
    int refCount;
 
15
} Notification;
 
16
 
 
17
 
 
18
extern void W_FlushASAPNotificationQueue();
 
19
 
 
20
 
 
21
const char*
 
22
WMGetNotificationName(WMNotification *notification)
 
23
{
 
24
    return notification->name;
 
25
}
 
26
 
 
27
 
 
28
void*
 
29
WMGetNotificationObject(WMNotification *notification)
 
30
{
 
31
    return notification->object;
 
32
}
 
33
 
 
34
 
 
35
void*
 
36
WMGetNotificationClientData(WMNotification *notification)
 
37
{
 
38
    return notification->clientData;
 
39
}
 
40
 
 
41
 
 
42
WMNotification*
 
43
WMCreateNotification(const char *name, void *object, void *clientData)
 
44
{
 
45
    Notification *nPtr;
 
46
 
 
47
    nPtr = wmalloc(sizeof(Notification));
 
48
 
 
49
    nPtr->name = name;
 
50
    nPtr->object = object;
 
51
    nPtr->clientData = clientData;
 
52
 
 
53
    nPtr->refCount = 1;
 
54
 
 
55
    return nPtr;
 
56
}
 
57
 
 
58
 
 
59
void
 
60
WMReleaseNotification(WMNotification *notification)
 
61
{
 
62
    notification->refCount--;
 
63
 
 
64
    if (notification->refCount < 1) {
 
65
        wfree(notification);
 
66
    }
 
67
}
 
68
 
 
69
 
 
70
WMNotification*
 
71
WMRetainNotification(WMNotification *notification)
 
72
{
 
73
    notification->refCount++;
 
74
 
 
75
    return notification;
 
76
}
 
77
 
 
78
 
 
79
/***************** Notification Center *****************/
 
80
 
 
81
typedef struct NotificationObserver {
 
82
    WMNotificationObserverAction *observerAction;
 
83
    void *observer;
 
84
 
 
85
    const char *name;
 
86
    void *object;
 
87
 
 
88
    struct NotificationObserver *prev; /* for tables */
 
89
    struct NotificationObserver *next;
 
90
    struct NotificationObserver *nextAction;   /* for observerTable */
 
91
} NotificationObserver;
 
92
 
 
93
 
 
94
typedef struct W_NotificationCenter {
 
95
    WMHashTable *nameTable;            /* names -> observer lists */
 
96
    WMHashTable *objectTable;          /* object -> observer lists */
 
97
    NotificationObserver *nilList;     /* obervers that catch everything */
 
98
 
 
99
    WMHashTable *observerTable;        /* observer -> NotificationObserver */
 
100
} NotificationCenter;
 
101
 
 
102
 
 
103
/* default (and only) center */
 
104
static NotificationCenter *notificationCenter = NULL;
 
105
 
 
106
 
 
107
void
 
108
W_InitNotificationCenter(void)
 
109
{
 
110
    notificationCenter = wmalloc(sizeof(NotificationCenter));
 
111
 
 
112
    notificationCenter->nameTable = WMCreateHashTable(WMStringPointerHashCallbacks);
 
113
    notificationCenter->objectTable = WMCreateHashTable(WMIntHashCallbacks);
 
114
    notificationCenter->nilList = NULL;
 
115
 
 
116
    notificationCenter->observerTable = WMCreateHashTable(WMIntHashCallbacks);
 
117
}
 
118
 
 
119
 
 
120
void
 
121
WMAddNotificationObserver(WMNotificationObserverAction *observerAction,
 
122
                          void *observer, const char *name, void *object)
 
123
{
 
124
    NotificationObserver *oRec, *rec;
 
125
 
 
126
    oRec = wmalloc(sizeof(NotificationObserver));
 
127
    oRec->observerAction = observerAction;
 
128
    oRec->observer = observer;
 
129
    oRec->name = name;
 
130
    oRec->object = object;
 
131
    oRec->next = NULL;
 
132
    oRec->prev = NULL;
 
133
 
 
134
 
 
135
    /* put this action in the list of actions for this observer */
 
136
    rec = (NotificationObserver*)WMHashInsert(notificationCenter->observerTable,
 
137
                                              observer, oRec);
 
138
 
 
139
    if (rec) {
 
140
        /* if this is not the first action for the observer */
 
141
        oRec->nextAction = rec;
 
142
    } else {
 
143
        oRec->nextAction = NULL;
 
144
    }
 
145
 
 
146
    if (!name && !object) {
 
147
        /* catch-all */
 
148
        oRec->next = notificationCenter->nilList;
 
149
        if (notificationCenter->nilList) {
 
150
            notificationCenter->nilList->prev = oRec;
 
151
        }
 
152
        notificationCenter->nilList = oRec;
 
153
    } else if (!name) {
 
154
        /* any message coming from object */
 
155
        rec = (NotificationObserver*)WMHashInsert(notificationCenter->objectTable,
 
156
                                                  object, oRec);
 
157
        oRec->next = rec;
 
158
        if (rec) {
 
159
            rec->prev = oRec;
 
160
        }
 
161
    } else {
 
162
        /* name && (object || !object) */
 
163
        rec = (NotificationObserver*)WMHashInsert(notificationCenter->nameTable,
 
164
                                                  name, oRec);
 
165
        oRec->next = rec;
 
166
        if (rec) {
 
167
            rec->prev = oRec;
 
168
        }
 
169
    }
 
170
}
 
171
 
 
172
 
 
173
void
 
174
WMPostNotification(WMNotification *notification)
 
175
{
 
176
    NotificationObserver *orec, *tmp;
 
177
 
 
178
    WMRetainNotification(notification);
 
179
 
 
180
    /* tell the observers that want to know about a particular message */
 
181
    orec = (NotificationObserver*)WMHashGet(notificationCenter->nameTable,
 
182
                                            notification->name);
 
183
 
 
184
    while (orec) {
 
185
        tmp = orec->next;
 
186
 
 
187
        if (!orec->object || !notification->object
 
188
            || orec->object == notification->object) {
 
189
            /* tell the observer */
 
190
            if (orec->observerAction) {
 
191
                (*orec->observerAction)(orec->observer, notification);
 
192
            }
 
193
        }
 
194
 
 
195
        orec = tmp;
 
196
    }
 
197
 
 
198
    /* tell the observers that want to know about an object */
 
199
    orec = (NotificationObserver*)WMHashGet(notificationCenter->objectTable,
 
200
                                            notification->object);
 
201
 
 
202
    while (orec) {
 
203
        tmp = orec->next;
 
204
 
 
205
        /* tell the observer */
 
206
        if (orec->observerAction) {
 
207
            (*orec->observerAction)(orec->observer, notification);
 
208
        }
 
209
        orec = tmp;
 
210
    }
 
211
 
 
212
    /* tell the catch all observers */
 
213
    orec = notificationCenter->nilList;
 
214
    while (orec) {
 
215
        tmp = orec->next;
 
216
 
 
217
        /* tell the observer */
 
218
        if (orec->observerAction) {
 
219
            (*orec->observerAction)(orec->observer, notification);
 
220
        }
 
221
        orec = tmp;
 
222
    }
 
223
 
 
224
    WMReleaseNotification(notification);
 
225
}
 
226
 
 
227
 
 
228
void
 
229
WMRemoveNotificationObserver(void *observer)
 
230
{
 
231
    NotificationObserver *orec, *tmp, *rec;
 
232
 
 
233
    /* get the list of actions the observer is doing */
 
234
    orec = (NotificationObserver*)WMHashGet(notificationCenter->observerTable,
 
235
                                            observer);
 
236
 
 
237
    /*
 
238
     * FOREACH orec IN actionlist for observer
 
239
     * DO
 
240
     *   remove from respective lists/tables
 
241
     *   free
 
242
     * END
 
243
     */
 
244
    while (orec) {
 
245
        tmp = orec->nextAction;
 
246
 
 
247
        if (!orec->name && !orec->object) {
 
248
            /* catch-all */
 
249
            if (notificationCenter->nilList==orec)
 
250
                notificationCenter->nilList = orec->next;
 
251
        } else if (!orec->name) {
 
252
            /* any message coming from object */
 
253
            rec = (NotificationObserver*)WMHashGet(notificationCenter->objectTable,
 
254
                                                   orec->object);
 
255
            if (rec==orec) {
 
256
                /* replace table entry */
 
257
                if (orec->next) {
 
258
                    WMHashInsert(notificationCenter->objectTable, orec->object,
 
259
                                 orec->next);
 
260
                } else {
 
261
                    WMHashRemove(notificationCenter->objectTable, orec->object);
 
262
                }
 
263
            }
 
264
        } else {
 
265
            /* name && (object || !object) */
 
266
            rec = (NotificationObserver*)WMHashGet(notificationCenter->nameTable,
 
267
                                                   orec->name);
 
268
            if (rec==orec) {
 
269
                /* replace table entry */
 
270
                if (orec->next) {
 
271
                    WMHashInsert(notificationCenter->nameTable, orec->name,
 
272
                                 orec->next);
 
273
                } else {
 
274
                    WMHashRemove(notificationCenter->nameTable, orec->name);
 
275
                }
 
276
            }
 
277
        }
 
278
        if (orec->prev)
 
279
            orec->prev->next = orec->next;
 
280
        if (orec->next)
 
281
            orec->next->prev = orec->prev;
 
282
 
 
283
        wfree(orec);
 
284
 
 
285
        orec = tmp;
 
286
    }
 
287
 
 
288
    WMHashRemove(notificationCenter->observerTable, observer);
 
289
}
 
290
 
 
291
 
 
292
void
 
293
WMRemoveNotificationObserverWithName(void *observer, const char *name, void *object)
 
294
{
 
295
    NotificationObserver *orec, *tmp, *rec;
 
296
    NotificationObserver *newList = NULL;
 
297
 
 
298
    /* get the list of actions the observer is doing */
 
299
    orec = (NotificationObserver*)WMHashGet(notificationCenter->observerTable, observer);
 
300
 
 
301
    WMHashRemove(notificationCenter->observerTable, observer);
 
302
 
 
303
    /* rebuild the list of actions for the observer */
 
304
 
 
305
    while (orec) {
 
306
        tmp = orec->nextAction;
 
307
        if (orec->name == name && orec->object == object) {
 
308
            if (!name && !object) {
 
309
                if (notificationCenter->nilList == orec)
 
310
                    notificationCenter->nilList = orec->next;
 
311
            } else if (!name) {
 
312
                rec = (NotificationObserver*)WMHashGet(notificationCenter->objectTable, orec->object);
 
313
                if (rec==orec) {
 
314
                    assert(rec->prev==NULL);
 
315
                    /* replace table entry */
 
316
                    if (orec->next) {
 
317
                        WMHashInsert(notificationCenter->objectTable,
 
318
                                     orec->object, orec->next);
 
319
                    } else {
 
320
                        WMHashRemove(notificationCenter->objectTable,
 
321
                                     orec->object);
 
322
                    }
 
323
                }
 
324
            } else {
 
325
                rec = (NotificationObserver*)WMHashGet(notificationCenter->nameTable,
 
326
                                                       orec->name);
 
327
                if (rec==orec) {
 
328
                    assert(rec->prev==NULL);
 
329
                    /* replace table entry */
 
330
                    if (orec->next) {
 
331
                        WMHashInsert(notificationCenter->nameTable,
 
332
                                     orec->name, orec->next);
 
333
                    } else {
 
334
                        WMHashRemove(notificationCenter->nameTable,
 
335
                                     orec->name);
 
336
                    }
 
337
                }
 
338
            }
 
339
 
 
340
            if (orec->prev)
 
341
                orec->prev->next = orec->next;
 
342
            if (orec->next)
 
343
                orec->next->prev = orec->prev;
 
344
            wfree(orec);
 
345
        } else {
 
346
            /* append this action in the new action list */
 
347
            orec->nextAction = NULL;
 
348
            if (!newList) {
 
349
                newList = orec;
 
350
            } else {
 
351
                NotificationObserver *p;
 
352
 
 
353
                p = newList;
 
354
                while (p->nextAction) {
 
355
                    p = p->nextAction;
 
356
                }
 
357
                p->nextAction = orec;
 
358
            }
 
359
        }
 
360
        orec = tmp;
 
361
    }
 
362
 
 
363
    /* reinsert the list to the table */
 
364
    if (newList) {
 
365
        WMHashInsert(notificationCenter->observerTable, observer, newList);
 
366
    }
 
367
}
 
368
 
 
369
 
 
370
void
 
371
WMPostNotificationName(const char *name, void *object, void *clientData)
 
372
{
 
373
    WMNotification *notification;
 
374
 
 
375
    notification = WMCreateNotification(name, object, clientData);
 
376
 
 
377
    WMPostNotification(notification);
 
378
 
 
379
    WMReleaseNotification(notification);
 
380
}
 
381
 
 
382
 
 
383
 
 
384
/**************** Notification Queues ****************/
 
385
 
 
386
 
 
387
typedef struct W_NotificationQueue {
 
388
    WMArray *asapQueue;
 
389
    WMArray *idleQueue;
 
390
 
 
391
    struct W_NotificationQueue *next;
 
392
} NotificationQueue;
 
393
 
 
394
 
 
395
static WMNotificationQueue *notificationQueueList = NULL;
 
396
 
 
397
/* default queue */
 
398
static WMNotificationQueue *notificationQueue = NULL;
 
399
 
 
400
 
 
401
WMNotificationQueue*
 
402
WMGetDefaultNotificationQueue(void)
 
403
{
 
404
    if (!notificationQueue)
 
405
        notificationQueue = WMCreateNotificationQueue();
 
406
 
 
407
    return notificationQueue;
 
408
}
 
409
 
 
410
 
 
411
WMNotificationQueue*
 
412
WMCreateNotificationQueue(void)
 
413
{
 
414
    NotificationQueue *queue;
 
415
 
 
416
    queue = wmalloc(sizeof(NotificationQueue));
 
417
 
 
418
    queue->asapQueue =
 
419
        WMCreateArrayWithDestructor(8, (WMFreeDataProc*)WMReleaseNotification);
 
420
    queue->idleQueue =
 
421
        WMCreateArrayWithDestructor(8, (WMFreeDataProc*)WMReleaseNotification);
 
422
    queue->next = notificationQueueList;
 
423
 
 
424
    notificationQueueList = queue;
 
425
 
 
426
    return queue;
 
427
}
 
428
 
 
429
 
 
430
 
 
431
void
 
432
WMEnqueueNotification(WMNotificationQueue *queue, WMNotification *notification,
 
433
                      WMPostingStyle postingStyle)
 
434
{
 
435
    WMEnqueueCoalesceNotification(queue, notification, postingStyle,
 
436
                                  WNCOnName|WNCOnSender);
 
437
}
 
438
 
 
439
 
 
440
#define NOTIF ((WMNotification*)cdata)
 
441
#define ITEM  ((WMNotification*)item)
 
442
 
 
443
static int
 
444
matchSenderAndName(void *item, void *cdata)
 
445
{
 
446
    return (NOTIF->object==ITEM->object && strcmp(NOTIF->name, ITEM->name)==0);
 
447
}
 
448
 
 
449
 
 
450
static int
 
451
matchSender(void *item, void *cdata)
 
452
{
 
453
    return (NOTIF->object == ITEM->object);
 
454
}
 
455
 
 
456
 
 
457
static int
 
458
matchName(void *item, void *cdata)
 
459
{
 
460
    return (strcmp(NOTIF->name, ITEM->name)==0);
 
461
}
 
462
 
 
463
#undef NOTIF
 
464
#undef ITEM
 
465
 
 
466
 
 
467
void
 
468
WMDequeueNotificationMatching(WMNotificationQueue *queue,
 
469
                              WMNotification *notification, unsigned mask)
 
470
{
 
471
    WMMatchDataProc *matchFunc;
 
472
 
 
473
    if ((mask & WNCOnName) && (mask & WNCOnSender))
 
474
        matchFunc = matchSenderAndName;
 
475
    else if (mask & WNCOnName)
 
476
        matchFunc = matchName;
 
477
    else if (mask & WNCOnSender)
 
478
        matchFunc = matchSender;
 
479
    else
 
480
        return;
 
481
 
 
482
    WMRemoveFromArrayMatching(queue->asapQueue, matchFunc, notification);
 
483
    WMRemoveFromArrayMatching(queue->idleQueue, matchFunc, notification);
 
484
}
 
485
 
 
486
 
 
487
void
 
488
WMEnqueueCoalesceNotification(WMNotificationQueue *queue,
 
489
                              WMNotification *notification,
 
490
                              WMPostingStyle postingStyle,
 
491
                              unsigned coalesceMask)
 
492
{
 
493
    if (coalesceMask != WNCNone)
 
494
        WMDequeueNotificationMatching(queue, notification, coalesceMask);
 
495
 
 
496
    switch (postingStyle) {
 
497
    case WMPostNow:
 
498
        WMPostNotification(notification);
 
499
        WMReleaseNotification(notification);
 
500
        break;
 
501
 
 
502
    case WMPostASAP:
 
503
        WMAddToArray(queue->asapQueue, notification);
 
504
        break;
 
505
 
 
506
    case WMPostWhenIdle:
 
507
        WMAddToArray(queue->idleQueue, notification);
 
508
        break;
 
509
    }
 
510
}
 
511
 
 
512
 
 
513
void
 
514
W_FlushASAPNotificationQueue()
 
515
{
 
516
    WMNotificationQueue *queue = notificationQueueList;
 
517
 
 
518
    while (queue) {
 
519
        while (WMGetArrayItemCount(queue->asapQueue)) {
 
520
            WMPostNotification(WMGetFromArray(queue->asapQueue, 0));
 
521
            WMDeleteFromArray(queue->asapQueue, 0);
 
522
        }
 
523
 
 
524
        queue = queue->next;
 
525
    }
 
526
}
 
527
 
 
528
 
 
529
void
 
530
W_FlushIdleNotificationQueue()
 
531
{
 
532
    WMNotificationQueue *queue = notificationQueueList;
 
533
 
 
534
    while (queue) {
 
535
        while (WMGetArrayItemCount(queue->idleQueue)) {
 
536
            WMPostNotification(WMGetFromArray(queue->idleQueue, 0));
 
537
            WMDeleteFromArray(queue->idleQueue, 0);
 
538
        }
 
539
 
 
540
        queue = queue->next;
 
541
    }
 
542
}
 
543