Mercurial > MadButterfly
annotate src/observer.c @ 199:55533146efdf
Add drag example
author | wycc@wycc-desktop |
---|---|
date | Sat, 06 Dec 2008 15:40:23 +0800 |
parents | f9d507a3e1d9 |
children | 75ec0124202a |
rev | line source |
---|---|
73 | 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 | 5 |
126 | 6 #ifndef ASSERT |
7 #define ASSERT(x) | |
8 #endif | |
9 | |
73 | 10 subject_t *subject_new(ob_factory_t *factory, void *obj, int obj_type) { |
11 subject_t *subject; | |
12 | |
13 subject = factory->subject_alloc(factory); | |
14 if(subject == NULL) | |
15 return NULL; | |
16 | |
17 subject->obj = obj; | |
18 subject->obj_type = obj_type; | |
19 subject->flags = 0; | |
20 STAILQ_INIT(subject->observers); | |
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 | 24 return subject; |
25 } | |
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 | 32 observer_t *observer; |
33 | |
126 | 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 | 43 while((observer = STAILQ_HEAD(subject->observers))) { |
44 STAILQ_REMOVE(subject->observers, observer_t, next, observer); | |
45 factory->observer_free(factory, observer); | |
46 } | |
47 factory->subject_free(factory, subject); | |
48 } | |
49 | |
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 | 53 observer_t *observer; |
54 | |
77 | 55 evt->tgt = subject; |
73 | 56 while(subject) { |
127
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
57 /*! |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
58 * \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
|
59 * 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
|
60 * 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
|
61 */ |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
62 subject->flags |= SUBF_BUSY; |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
63 |
77 | 64 evt->cur_tgt = subject->obj; |
73 | 65 for(observer = STAILQ_HEAD(subject->observers); |
66 observer != NULL; | |
67 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
|
68 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
|
69 observer->hdr(evt, observer->arg); |
73 | 70 } |
71 | |
127
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
72 subject->flags &= ~SUBF_BUSY; |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
73 if(subject->flags & SUBF_FREE) |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
74 subject_free(subject); |
127
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
75 |
73 | 76 if(subject->flags & SUBF_STOP_PROPAGATE) |
77 break; | |
78 | |
79 subject = factory->get_parent_subject(factory, subject); | |
80 } | |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
81 |
73 | 82 } |
83 | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
84 observer_t *subject_add_observer(subject_t *subject, |
73 | 85 evt_handler hdr, void *arg) { |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
86 ob_factory_t *factory = subject->factory; |
73 | 87 observer_t *observer; |
88 | |
89 observer = factory->observer_alloc(factory); | |
90 if(observer == NULL) | |
91 return NULL; | |
92 observer->hdr = hdr; | |
93 observer->arg = arg; | |
198
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
94 observer->type = EVT_ANY; |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
95 |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
96 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
|
97 |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
98 return 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 |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
101 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
|
102 evt_handler hdr, void *arg) { |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
103 ob_factory_t *factory = subject->factory; |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
104 observer_t *observer; |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
105 |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
106 observer = factory->observer_alloc(factory); |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
107 if(observer == NULL) |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
108 return NULL; |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
109 observer->hdr = hdr; |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
110 observer->arg = arg; |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
111 observer->type = type; |
73 | 112 |
113 STAILQ_INS_TAIL(subject->observers, observer_t, next, observer); | |
114 | |
115 return observer; | |
116 } | |
117 | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
118 void subject_remove_observer(subject_t *subject, |
73 | 119 observer_t *observer) { |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
120 ob_factory_t *factory = subject->factory; |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
121 |
73 | 122 STAILQ_REMOVE(subject->observers, observer_t, next, observer); |
123 factory->observer_free(factory, observer); | |
124 } | |
125 | |
126 #ifdef UNITTEST | |
127 | |
128 #include <CUnit/Basic.h> | |
129 #include <stdlib.h> | |
130 | |
131 static subject_t *test_subject_alloc(ob_factory_t *factory) { | |
132 subject_t *subject; | |
133 | |
134 subject = (subject_t *)malloc(sizeof(subject_t)); | |
135 return subject; | |
136 } | |
137 | |
138 static void test_subject_free(ob_factory_t *factory, subject_t *subject) { | |
139 free(subject); | |
140 } | |
141 | |
142 static observer_t *test_observer_alloc(ob_factory_t *factory) { | |
143 observer_t *observer; | |
144 | |
145 observer = (observer_t *)malloc(sizeof(observer_t)); | |
146 return observer; | |
147 } | |
148 | |
149 static void test_observer_free(ob_factory_t *factory, observer_t *observer) { | |
150 free(observer); | |
151 } | |
152 | |
153 static subject_t *test_get_parent_subject(ob_factory_t *factory, | |
154 subject_t *subject) { | |
155 return NULL; | |
156 } | |
157 | |
158 static ob_factory_t test_factory = { | |
159 test_subject_alloc, | |
160 test_subject_free, | |
161 test_observer_alloc, | |
162 test_observer_free, | |
163 test_get_parent_subject | |
164 }; | |
165 | |
166 static void handler(event_t *evt, void *arg) { | |
167 int *cnt = (int *)arg; | |
168 | |
74 | 169 CU_ASSERT(evt->type == EVT_MOUSE_OUT); |
73 | 170 (*cnt)++; |
171 } | |
172 | |
173 void test_observer(void) { | |
174 subject_t *subject; | |
74 | 175 observer_t *observer[2]; |
73 | 176 event_t evt; |
177 int cnt = 0; | |
178 | |
179 subject = subject_new(&test_factory, NULL, 0); | |
180 subject->flags |= SUBF_STOP_PROPAGATE; | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
181 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
|
182 observer[1] = subject_add_observer(subject, handler, &cnt); |
73 | 183 |
74 | 184 evt.type = EVT_MOUSE_OUT; |
73 | 185 evt.tgt = NULL; |
186 evt.cur_tgt = NULL; | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
187 subject_notify(subject, &evt); |
74 | 188 CU_ASSERT(cnt == 2); |
73 | 189 |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
190 subject_remove_observer(subject, observer[0]); |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
191 subject_remove_observer(subject, observer[1]); |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
192 subject_free(subject); |
73 | 193 } |
194 | |
195 CU_pSuite get_observer_suite(void) { | |
196 CU_pSuite suite; | |
197 | |
198 suite = CU_add_suite("Suite_observer", NULL, NULL); | |
199 CU_ADD_TEST(suite, test_observer); | |
200 | |
201 return suite; | |
202 } | |
203 | |
204 #endif /* UNITTEST */ |