Mercurial > MadButterfly
annotate include/mb_observer.h @ 1493:b0e113605382
Add comment
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Mon, 02 May 2011 23:26:32 +0800 |
parents | 1993e5ae60aa |
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 #ifndef __OBSERVER_H_ |
4 #define __OBSERVER_H_ | |
5 | |
186
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 |
8 typedef struct _event event_t; | |
9 typedef struct _observer observer_t; | |
10 typedef struct _subject subject_t; | |
11 typedef struct _mouse_event mouse_event_t; | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
12 typedef struct _monitor_event monitor_event_t; |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
13 typedef struct _observer_factory observer_factory_t; |
73 | 14 typedef void (*evt_handler)(event_t *event, void *arg); |
15 | |
16 struct _event { | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
17 int type; /*!< event type (a.k.a. EVT_*) */ |
73 | 18 subject_t *tgt, *cur_tgt; |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
19 int flags; |
73 | 20 }; |
21 | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
22 /*! \brief Observer mark event with EVTF_STOP_PROPAGATE flag |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
23 * to stop propagation. |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
24 */ |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
25 #define EVTF_STOP_PROPAGATE 0x1 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
26 /*! \brief Observer mark event with EVTF_STOP_NOTIFY flag to stop |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
27 * stop notification the event immediately. |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
28 */ |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
29 #define EVTF_STOP_NOTIFY 0x2 |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
30 |
78 | 31 /*! \brief Observer of observer pattern. |
32 * | |
33 * A target for receiving events. | |
34 */ | |
73 | 35 struct _observer { |
198
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
36 int type; |
73 | 37 evt_handler hdr; |
38 void *arg; | |
39 observer_t *next; | |
40 }; | |
41 | |
78 | 42 /*! \brief Subject of observer pattern. |
43 * | |
44 * Observer is a pattern to decouple caller and callee, | |
45 * especial for multiple callee. | |
46 * \see http://en.wikipedia.org/wiki/Observer_pattern | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
47 * |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
48 * This implementation add a monitor facility to monitor adding/removing |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
49 * observers from subjects. Monitor is another subject that monitor events |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
50 * will be sent to if it is existed. |
78 | 51 */ |
73 | 52 struct _subject { |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
53 int obj_type; /*!< \brief type of object (a.k.a. OBJT_*). */ |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
54 void *obj; /*!< \brief the object this subject for. */ |
73 | 55 int flags; |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
56 subject_t *monitor_sub; /*!< \brief Monitor adding/removing |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
57 * obervers on this subject. */ |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
58 observer_factory_t *factory; |
73 | 59 STAILQ(observer_t) observers; |
60 }; | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
61 /*! \brief Flag that make a subject to stop propagate events to parents. */ |
73 | 62 #define SUBF_STOP_PROPAGATE 0x1 |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
63 #define SUBF_BUSY 0x2 /*!< \brief in subject_notify() */ |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
64 #define SUBF_FREE 0x4 /*!< \brief in postponding subject_free() */ |
73 | 65 |
154 | 66 enum {OBJT_GEO, OBJT_COORD, OBJT_KB, OBJT_PROGM, OBJT_RDMAN}; |
73 | 67 |
68 struct _mouse_event { | |
69 event_t event; | |
70 int x, y; | |
78 | 71 unsigned int but_state; |
72 unsigned int button; | |
73 | 73 }; |
74 | |
76 | 75 #define MOUSE_BUT1 0x1 |
76 #define MOUSE_BUT2 0x2 | |
77 #define MOUSE_BUT3 0x4 | |
78 | |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
79 struct _monitor_event { |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
80 event_t event; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
81 subject_t *subject; /*!< \brief Subject been monitored. */ |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
82 observer_t *observer; /*!< \brief Observer been added or removed. */ |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
83 }; |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
84 |
1112
1993e5ae60aa
Rename X_kb_event_t to mb_kb_event_t
Thinker K.F. Li <thinker@codemud.net>
parents:
1060
diff
changeset
|
85 struct _mb_kb_event { |
1993e5ae60aa
Rename X_kb_event_t to mb_kb_event_t
Thinker K.F. Li <thinker@codemud.net>
parents:
1060
diff
changeset
|
86 event_t event; |
1993e5ae60aa
Rename X_kb_event_t to mb_kb_event_t
Thinker K.F. Li <thinker@codemud.net>
parents:
1060
diff
changeset
|
87 int keycode; |
1993e5ae60aa
Rename X_kb_event_t to mb_kb_event_t
Thinker K.F. Li <thinker@codemud.net>
parents:
1060
diff
changeset
|
88 int sym; |
1993e5ae60aa
Rename X_kb_event_t to mb_kb_event_t
Thinker K.F. Li <thinker@codemud.net>
parents:
1060
diff
changeset
|
89 }; |
1993e5ae60aa
Rename X_kb_event_t to mb_kb_event_t
Thinker K.F. Li <thinker@codemud.net>
parents:
1060
diff
changeset
|
90 typedef struct _mb_kb_event mb_kb_event_t; |
1993e5ae60aa
Rename X_kb_event_t to mb_kb_event_t
Thinker K.F. Li <thinker@codemud.net>
parents:
1060
diff
changeset
|
91 |
73 | 92 /*! \brief Observer factory. |
93 * | |
94 * It provides functions for allocation of subject and observer objects, | |
95 * and strategy function for getting the subject of parent coord object. | |
96 */ | |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
97 struct _observer_factory { |
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
98 subject_t *(*subject_alloc)(observer_factory_t *factory); |
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
99 void (*subject_free)(observer_factory_t *factory, subject_t *subject); |
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
100 observer_t *(*observer_alloc)(observer_factory_t *factory); |
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
101 void (*observer_free)(observer_factory_t *factory, observer_t *observer); |
73 | 102 /*! This is a strategy function to get subjects of parents. */ |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
103 subject_t *(*get_parent_subject)(observer_factory_t *factory, |
73 | 104 subject_t *cur_subject); |
105 }; | |
106 | |
198
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
107 enum {EVT_ANY,EVT_MOUSE_OVER, EVT_MOUSE_OUT, EVT_MOUSE_MOVE, |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
108 EVT_MOUSE_BUT_PRESS, EVT_MOUSE_BUT_RELEASE, |
154 | 109 EVT_KB_PRESS, EVT_KB_RELEASE, EVT_PROGM_COMPLETE, |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
110 EVT_RDMAN_REDRAW, |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
111 EVT_MONITOR_ADD, EVT_MONITOR_REMOVE, 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
|
112 EVT_MOUSE_MOVE_RAW |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
113 }; |
73 | 114 |
1060
e415c55b4a0d
Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
115 extern subject_t *subject_new(observer_factory_t *factory, |
73 | 116 void *obj, int obj_type); |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
117 extern void subject_free(subject_t *subject); |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
118 extern void subject_notify(subject_t *subject, event_t *evt); |
206
748896358da2
Export subject_add_event_observer() to rest of the system.
Thinker K.F. Li <thinker@branda.to>
parents:
198
diff
changeset
|
119 extern observer_t *subject_add_event_observer(subject_t *subject, int type, |
748896358da2
Export subject_add_event_observer() to rest of the system.
Thinker K.F. Li <thinker@branda.to>
parents:
198
diff
changeset
|
120 evt_handler hdr, void *arg); |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
121 /*! \brief Add an observer for any type of events. */ |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
122 #define subject_add_observer(s, h, a) \ |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
123 subject_add_event_observer(s, EVT_ANY, h, a) |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
124 extern observer_t *subject_add_event_observer_head(subject_t *subject, |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
125 int type, |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
126 evt_handler hdr, |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
127 void *arg); |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
128 /*! \brief Add an observer for any 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
|
129 #define subject_add_observer_head(s, h, a) \ |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
130 subject_add_event_observer_head(s, EVT_ANY, h, a) |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
131 extern void subject_remove_observer(subject_t *subject, |
73 | 132 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
|
133 #define subject_get_object(s) ((s)->obj) |
73 | 134 |
224
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
135 /*! \brief Set monitor for the subject. |
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 * Monitor of a subject is another subject that would be notified when |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
138 * add/remove a observer to/from the subject. It can be used to efficiently |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
139 * implement translator to translate events. |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
140 */ |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
141 #define 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
|
142 do { (subject)->monitor_sub = monitor; } while(0) |
29e1b2bffe4c
X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
206
diff
changeset
|
143 #define subject_monitor(subject) ((subject)->monitor_sub) |
73 | 144 |
145 #endif /* __OBSERVER_H_ */ |