Mercurial > MadButterfly
annotate src/observer.c @ 1517:31a21656c0a8
Describe designs of Scribboo
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Sat, 14 May 2011 01:07:23 +0800 |
parents | e415c55b4a0d |
children |
rev | line source |
---|---|
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
224
diff
changeset
|
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*- |
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
224
diff
changeset
|
2 // vim: sw=4:ts=8:sts=4 |
73 | 3 #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
|
4 #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
|
5 #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
|
6 #include "mb_tools.h" |
73 | 7 |
126 | 8 #ifndef ASSERT |
9 #define ASSERT(x) | |
10 #endif | |
11 | |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
12 subject_t *subject_new(observer_factory_t *factory, void *obj, int obj_type) { |
73 | 13 subject_t *subject; |
14 | |
15 subject = factory->subject_alloc(factory); | |
16 if(subject == NULL) | |
17 return NULL; | |
18 | |
19 subject->obj = obj; | |
20 subject->obj_type = obj_type; | |
21 subject->flags = 0; | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
22 subject->monitor_sub = NULL; |
73 | 23 STAILQ_INIT(subject->observers); |
24 | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
25 subject->factory = factory; |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
26 |
73 | 27 return subject; |
28 } | |
29 | |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
30 /*! |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
31 * \todo Keep observer_factory following subject objects. |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
32 */ |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
33 void subject_free(subject_t *subject) { |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
34 observer_factory_t *factory = subject->factory; |
73 | 35 observer_t *observer; |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
36 monitor_event_t mevt; |
73 | 37 |
126 | 38 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
|
39 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
|
40 /* 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
|
41 * 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
|
42 */ |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
43 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
|
44 return; |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
45 } |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
224
diff
changeset
|
46 |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
47 if(subject->monitor_sub) { |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
48 mevt.event.type = EVT_MONITOR_FREE; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
49 mevt.subject = subject; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
50 mevt.observer = NULL; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
51 subject_notify(subject->monitor_sub, (event_t *)&mevt); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
52 } |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
53 |
73 | 54 while((observer = STAILQ_HEAD(subject->observers))) { |
55 STAILQ_REMOVE(subject->observers, observer_t, next, observer); | |
56 factory->observer_free(factory, observer); | |
57 } | |
58 factory->subject_free(factory, subject); | |
59 } | |
60 | |
61 | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
62 void subject_notify(subject_t *subject, event_t *evt) { |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
63 observer_factory_t *factory = subject->factory; |
73 | 64 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
|
65 subject_t *old_subject; |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
66 int stop_propagate = 0; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
67 int old_busy; |
73 | 68 |
77 | 69 evt->tgt = subject; |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
70 evt->flags = 0; |
73 | 71 while(subject) { |
127
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
72 /*! |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
73 * \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
|
74 * 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
|
75 * 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
|
76 */ |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
77 old_busy = subject->flags & SUBF_BUSY; |
127
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
78 subject->flags |= SUBF_BUSY; |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
79 |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
80 evt->cur_tgt = subject; |
73 | 81 for(observer = STAILQ_HEAD(subject->observers); |
82 observer != NULL; | |
83 observer = STAILQ_NEXT(observer_t, next, observer)) { | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
84 if (observer->type == EVT_ANY || observer->type == evt->type) { |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
85 observer->hdr(evt, observer->arg); |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
224
diff
changeset
|
86 |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
87 if(evt->flags & EVTF_STOP_NOTIFY) { |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
88 stop_propagate = 1; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
89 break; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
90 } |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
91 if(evt->flags & EVTF_STOP_PROPAGATE) |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
92 stop_propagate = 1; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
93 } |
73 | 94 } |
95 | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
96 if(!old_busy) |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
97 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
|
98 |
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
99 old_subject = subject; |
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
100 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
|
101 |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
102 if(old_subject->flags & SUBF_STOP_PROPAGATE) |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
103 stop_propagate = 1; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
104 |
194
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
105 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
|
106 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
|
107 |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
108 if(stop_propagate) |
73 | 109 break; |
110 } | |
111 } | |
112 | |
206
748896358da2
Export subject_add_event_observer() to rest of the system.
Thinker K.F. Li <thinker@branda.to>
parents:
202
diff
changeset
|
113 /*! \brief Add an observer for specified type of events. |
748896358da2
Export subject_add_event_observer() to rest of the system.
Thinker K.F. Li <thinker@branda.to>
parents:
202
diff
changeset
|
114 */ |
198
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
115 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
|
116 evt_handler hdr, void *arg) { |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
117 observer_factory_t *factory = subject->factory; |
198
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
118 observer_t *observer; |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
119 monitor_event_t mevt; |
198
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
120 |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
121 observer = factory->observer_alloc(factory); |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
122 if(observer == NULL) |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
123 return NULL; |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
124 observer->hdr = hdr; |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
125 observer->arg = arg; |
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
126 observer->type = type; |
73 | 127 |
128 STAILQ_INS_TAIL(subject->observers, observer_t, next, observer); | |
129 | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
130 if(subject->monitor_sub) { |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
131 mevt.event.type = EVT_MONITOR_ADD; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
132 mevt.subject = subject; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
133 mevt.observer = observer; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
134 subject_notify(subject->monitor_sub, (event_t *)&mevt); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
135 } |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
136 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
137 return observer; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
138 } |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
139 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
140 /*! \brief Add an observer for specified type of events at head. |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
141 */ |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
142 observer_t *subject_add_event_observer_head(subject_t *subject, int type, |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
143 evt_handler hdr, void *arg) { |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
144 observer_factory_t *factory = subject->factory; |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
145 observer_t *observer; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
146 monitor_event_t mevt; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
147 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
148 observer = factory->observer_alloc(factory); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
149 if(observer == NULL) |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
150 return NULL; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
151 observer->hdr = hdr; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
152 observer->arg = arg; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
153 observer->type = type; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
154 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
155 STAILQ_INS(subject->observers, observer_t, next, observer); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
156 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
157 if(subject->monitor_sub) { |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
158 mevt.event.type = EVT_MONITOR_ADD; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
159 mevt.subject = subject; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
160 mevt.observer = observer; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
161 subject_notify(subject->monitor_sub, (event_t *)&mevt); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
162 } |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
163 |
73 | 164 return observer; |
165 } | |
166 | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
167 void subject_remove_observer(subject_t *subject, |
73 | 168 observer_t *observer) { |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
169 observer_factory_t *factory = subject->factory; |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
170 monitor_event_t mevt; |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
171 |
73 | 172 STAILQ_REMOVE(subject->observers, observer_t, next, observer); |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
224
diff
changeset
|
173 |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
174 if(subject->monitor_sub) { |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
175 mevt.event.type = EVT_MONITOR_REMOVE; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
176 mevt.subject = subject; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
177 mevt.observer = observer; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
178 subject_notify(subject->monitor_sub, (event_t *)&mevt); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
179 } |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
180 |
73 | 181 factory->observer_free(factory, observer); |
182 } | |
183 | |
184 #ifdef UNITTEST | |
185 | |
186 #include <CUnit/Basic.h> | |
187 #include <stdlib.h> | |
188 | |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
189 static subject_t *test_subject_alloc(observer_factory_t *factory) { |
73 | 190 subject_t *subject; |
191 | |
192 subject = (subject_t *)malloc(sizeof(subject_t)); | |
193 return subject; | |
194 } | |
195 | |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
196 static void |
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
197 test_subject_free(observer_factory_t *factory, subject_t *subject) { |
73 | 198 free(subject); |
199 } | |
200 | |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
201 static observer_t *test_observer_alloc(observer_factory_t *factory) { |
73 | 202 observer_t *observer; |
203 | |
204 observer = (observer_t *)malloc(sizeof(observer_t)); | |
205 return observer; | |
206 } | |
207 | |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
208 static void |
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
209 test_observer_free(observer_factory_t *factory, observer_t *observer) { |
73 | 210 free(observer); |
211 } | |
212 | |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
213 static subject_t *test_get_parent_subject(observer_factory_t *factory, |
73 | 214 subject_t *subject) { |
215 return NULL; | |
216 } | |
217 | |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
218 static observer_factory_t test_factory = { |
73 | 219 test_subject_alloc, |
220 test_subject_free, | |
221 test_observer_alloc, | |
222 test_observer_free, | |
223 test_get_parent_subject | |
224 }; | |
225 | |
226 static void handler(event_t *evt, void *arg) { | |
227 int *cnt = (int *)arg; | |
228 | |
74 | 229 CU_ASSERT(evt->type == EVT_MOUSE_OUT); |
73 | 230 (*cnt)++; |
231 } | |
232 | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
233 static void test_observer(void) { |
73 | 234 subject_t *subject; |
74 | 235 observer_t *observer[2]; |
73 | 236 event_t evt; |
237 int cnt = 0; | |
238 | |
239 subject = subject_new(&test_factory, NULL, 0); | |
240 subject->flags |= SUBF_STOP_PROPAGATE; | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
241 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
|
242 observer[1] = subject_add_observer(subject, handler, &cnt); |
73 | 243 |
74 | 244 evt.type = EVT_MOUSE_OUT; |
73 | 245 evt.tgt = NULL; |
246 evt.cur_tgt = NULL; | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
247 subject_notify(subject, &evt); |
74 | 248 CU_ASSERT(cnt == 2); |
73 | 249 |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
250 subject_remove_observer(subject, observer[0]); |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
251 subject_remove_observer(subject, observer[1]); |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
252 subject_free(subject); |
73 | 253 } |
254 | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
255 static void monitor_handler(event_t *evt, void *arg) { |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
256 int *cnt = (int *)arg; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
257 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
258 (*cnt)++; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
259 } |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
260 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
261 static void test_monitor(void) { |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
262 subject_t *subject, *monitor; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
263 observer_t *observer[2]; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
264 int cnt = 0; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
265 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
266 subject = subject_new(&test_factory, NULL, 0); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
267 monitor = subject_new(&test_factory, NULL, 0); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
268 subject_set_monitor(subject, monitor); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
269 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
270 observer[0] = subject_add_observer(monitor, monitor_handler, &cnt); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
271 observer[1] = subject_add_observer(subject, NULL, NULL); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
272 CU_ASSERT(cnt == 1); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
273 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
274 subject_remove_observer(subject, observer[1]); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
275 CU_ASSERT(cnt == 2); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
276 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
277 subject_free(subject); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
278 CU_ASSERT(cnt == 3); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
279 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
280 subject_remove_observer(monitor, observer[0]); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
281 subject_free(monitor); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
282 } |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
283 |
73 | 284 CU_pSuite get_observer_suite(void) { |
285 CU_pSuite suite; | |
286 | |
287 suite = CU_add_suite("Suite_observer", NULL, NULL); | |
288 CU_ADD_TEST(suite, test_observer); | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
289 CU_ADD_TEST(suite, test_monitor); |
73 | 290 |
291 return suite; | |
292 } | |
293 | |
294 #endif /* UNITTEST */ |