annotate src/observer.c @ 202:75ec0124202a

Commit merged between 200 and 201
author wycc@wycc-desktop
date Tue, 09 Dec 2008 23:34:05 +0800
parents f9d507a3e1d9 45d9a1e2764d
children 748896358da2
rev   line source
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <stdio.h>
186
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
2 #include "mb_redraw_man.h"
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
3 #include "mb_observer.h"
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
4 #include "mb_tools.h"
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5
126
Thinker K.F. Li <thinker@branda.to>
parents: 125
diff changeset
6 #ifndef ASSERT
Thinker K.F. Li <thinker@branda.to>
parents: 125
diff changeset
7 #define ASSERT(x)
Thinker K.F. Li <thinker@branda.to>
parents: 125
diff changeset
8 #endif
Thinker K.F. Li <thinker@branda.to>
parents: 125
diff changeset
9
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 subject_t *subject_new(ob_factory_t *factory, void *obj, int obj_type) {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 subject_t *subject;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 subject = factory->subject_alloc(factory);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 if(subject == NULL)
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 return NULL;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 subject->obj = obj;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 subject->obj_type = obj_type;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 subject->flags = 0;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 STAILQ_INIT(subject->observers);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
22 subject->factory = factory;
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
23
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 return subject;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26
125
1c1f28c124c9 Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
27 /*!
1c1f28c124c9 Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
28 * \todo Keep ob_factory following subject objects.
1c1f28c124c9 Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
29 */
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
30 void subject_free(subject_t *subject) {
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
31 ob_factory_t *factory = subject->factory;
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 observer_t *observer;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33
126
Thinker K.F. Li <thinker@branda.to>
parents: 125
diff changeset
34 ASSERT(!(subject->flags & SUBF_FREE));
125
1c1f28c124c9 Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
35 if(subject->flags & SUBF_BUSY) {
1c1f28c124c9 Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
36 /* Postpond the request until busy status been stoped.
1c1f28c124c9 Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
37 * SUBF_BUSY means in subject_notify().
1c1f28c124c9 Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
38 */
1c1f28c124c9 Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
39 subject->flags |= SUBF_FREE;
1c1f28c124c9 Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
40 return;
1c1f28c124c9 Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
41 }
1c1f28c124c9 Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
42
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43 while((observer = STAILQ_HEAD(subject->observers))) {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 STAILQ_REMOVE(subject->observers, observer_t, next, observer);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 factory->observer_free(factory, observer);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 factory->subject_free(factory, subject);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
51 void subject_notify(subject_t *subject, event_t *evt) {
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
52 ob_factory_t *factory = subject->factory;
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 observer_t *observer;
194
45d9a1e2764d Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
54 subject_t *old_subject;
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55
77
Thinker K.F. Li <thinker@branda.to>
parents: 74
diff changeset
56 evt->tgt = subject;
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 while(subject) {
127
d2cc7400c971 Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents: 126
diff changeset
58 /*!
d2cc7400c971 Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents: 126
diff changeset
59 * \note What is happend when the subject is freed by observer?
d2cc7400c971 Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents: 126
diff changeset
60 * Postponding the request of free until notification
d2cc7400c971 Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents: 126
diff changeset
61 * been finished. (\ref SUBF_BUSY / \ref SUBF_FREE)
d2cc7400c971 Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents: 126
diff changeset
62 */
d2cc7400c971 Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents: 126
diff changeset
63 subject->flags |= SUBF_BUSY;
d2cc7400c971 Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents: 126
diff changeset
64
77
Thinker K.F. Li <thinker@branda.to>
parents: 74
diff changeset
65 evt->cur_tgt = subject->obj;
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 for(observer = STAILQ_HEAD(subject->observers);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 observer != NULL;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 observer = STAILQ_NEXT(observer_t, next, observer)) {
198
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
69 if (observer->type == EVT_ANY || observer->type == evt->type)
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
70 observer->hdr(evt, observer->arg);
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72
127
d2cc7400c971 Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents: 126
diff changeset
73 subject->flags &= ~SUBF_BUSY;
194
45d9a1e2764d Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
74
45d9a1e2764d Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
75 old_subject = subject;
45d9a1e2764d Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
76 subject = factory->get_parent_subject(factory, subject);
127
d2cc7400c971 Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents: 126
diff changeset
77
194
45d9a1e2764d Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
78 if(old_subject->flags & SUBF_FREE)
45d9a1e2764d Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
79 subject_free(old_subject);
45d9a1e2764d Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
80
45d9a1e2764d Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
81 if(old_subject->flags & SUBF_STOP_PROPAGATE)
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 break;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
86 observer_t *subject_add_observer(subject_t *subject,
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 evt_handler hdr, void *arg) {
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
88 ob_factory_t *factory = subject->factory;
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 observer_t *observer;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 observer = factory->observer_alloc(factory);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 if(observer == NULL)
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 return NULL;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 observer->hdr = hdr;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 observer->arg = arg;
198
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
96 observer->type = EVT_ANY;
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
97
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
98 STAILQ_INS_TAIL(subject->observers, observer_t, next, observer);
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
99
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
100 return observer;
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
101 }
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
102
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
103 observer_t *subject_add_event_observer(subject_t *subject, int type,
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
104 evt_handler hdr, void *arg) {
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
105 ob_factory_t *factory = subject->factory;
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
106 observer_t *observer;
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
107
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
108 observer = factory->observer_alloc(factory);
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
109 if(observer == NULL)
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
110 return NULL;
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
111 observer->hdr = hdr;
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
112 observer->arg = arg;
f9d507a3e1d9 Add event observer which listen to one event type only.
wycc@wycc-desktop
parents: 192
diff changeset
113 observer->type = type;
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115 STAILQ_INS_TAIL(subject->observers, observer_t, next, observer);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 return observer;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
120 void subject_remove_observer(subject_t *subject,
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 observer_t *observer) {
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
122 ob_factory_t *factory = subject->factory;
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
123
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 STAILQ_REMOVE(subject->observers, observer_t, next, observer);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125 factory->observer_free(factory, observer);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
126 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
127
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
128 #ifdef UNITTEST
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
129
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
130 #include <CUnit/Basic.h>
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 #include <stdlib.h>
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
132
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
133 static subject_t *test_subject_alloc(ob_factory_t *factory) {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
134 subject_t *subject;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
135
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
136 subject = (subject_t *)malloc(sizeof(subject_t));
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
137 return subject;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
138 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
139
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
140 static void test_subject_free(ob_factory_t *factory, subject_t *subject) {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
141 free(subject);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
142 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
143
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
144 static observer_t *test_observer_alloc(ob_factory_t *factory) {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
145 observer_t *observer;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
146
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
147 observer = (observer_t *)malloc(sizeof(observer_t));
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
148 return observer;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
149 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
150
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
151 static void test_observer_free(ob_factory_t *factory, observer_t *observer) {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
152 free(observer);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
153 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
154
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
155 static subject_t *test_get_parent_subject(ob_factory_t *factory,
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
156 subject_t *subject) {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
157 return NULL;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
158 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
159
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
160 static ob_factory_t test_factory = {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
161 test_subject_alloc,
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
162 test_subject_free,
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
163 test_observer_alloc,
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
164 test_observer_free,
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
165 test_get_parent_subject
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
166 };
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
167
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
168 static void handler(event_t *evt, void *arg) {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
169 int *cnt = (int *)arg;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
170
74
3e3e074120a7 test case for observer
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
171 CU_ASSERT(evt->type == EVT_MOUSE_OUT);
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
172 (*cnt)++;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
173 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
174
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
175 void test_observer(void) {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
176 subject_t *subject;
74
3e3e074120a7 test case for observer
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
177 observer_t *observer[2];
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
178 event_t evt;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
179 int cnt = 0;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
180
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
181 subject = subject_new(&test_factory, NULL, 0);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
182 subject->flags |= SUBF_STOP_PROPAGATE;
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
183 observer[0] = subject_add_observer(subject, handler, &cnt);
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
184 observer[1] = subject_add_observer(subject, handler, &cnt);
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
185
74
3e3e074120a7 test case for observer
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
186 evt.type = EVT_MOUSE_OUT;
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
187 evt.tgt = NULL;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
188 evt.cur_tgt = NULL;
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
189 subject_notify(subject, &evt);
74
3e3e074120a7 test case for observer
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
190 CU_ASSERT(cnt == 2);
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
191
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
192 subject_remove_observer(subject, observer[0]);
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
193 subject_remove_observer(subject, observer[1]);
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
194 subject_free(subject);
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
195 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
196
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
197 CU_pSuite get_observer_suite(void) {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
198 CU_pSuite suite;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
199
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
200 suite = CU_add_suite("Suite_observer", NULL, NULL);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
201 CU_ADD_TEST(suite, test_observer);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
202
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
203 return suite;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
204 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
205
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
206 #endif /* UNITTEST */