annotate include/mb_observer.h @ 192:54fdc2a65242

Remove factory from observer APIs. - Save factory that used to new a subject as subject's attribute. - Get factory from subject himself instead of passing as an argument. - It make API of observer more simple and clean.
author Thinker K.F. Li <thinker@branda.to>
date Tue, 18 Nov 2008 21:42:30 +0800
parents 530bb7728546
children f9d507a3e1d9
rev   line source
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #ifndef __OBSERVER_H_
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #define __OBSERVER_H_
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3
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_tools.h"
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 typedef struct _event event_t;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 typedef struct _observer observer_t;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 typedef struct _subject subject_t;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 typedef struct _mouse_event mouse_event_t;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 typedef struct _ob_factory ob_factory_t;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 typedef void (*evt_handler)(event_t *event, void *arg);
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 struct _event {
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
14 int type; /*!< event type (a.k.a. EVT_* */
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 subject_t *tgt, *cur_tgt;
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
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
18 /*! \brief Observer of observer pattern.
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
19 *
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
20 * A target for receiving events.
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
21 */
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 struct _observer {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 evt_handler hdr;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 void *arg;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 observer_t *next;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 };
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
28 /*! \brief Subject of observer pattern.
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
29 *
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
30 * Observer is a pattern to decouple caller and callee,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
31 * especial for multiple callee.
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
32 * \see http://en.wikipedia.org/wiki/Observer_pattern
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
33 */
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 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
35 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
36 void *obj; /*!< \brief the object this subject for. */
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 int flags;
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
38 ob_factory_t *factory;
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 STAILQ(observer_t) observers;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 };
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
41 /*! \brief Flag that make a subject to propagate events to parents. */
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 #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
43 #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
44 #define SUBF_FREE 0x4 /*!< \brief in postponding subject_free() */
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 125
diff changeset
46 enum {OBJT_GEO, OBJT_COORD, OBJT_KB, OBJT_PROGM, OBJT_RDMAN};
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 struct _mouse_event {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 event_t event;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 int x, y;
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
51 unsigned int but_state;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 76
diff changeset
52 unsigned int button;
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 };
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54
76
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
55 #define MOUSE_BUT1 0x1
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
56 #define MOUSE_BUT2 0x2
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
57 #define MOUSE_BUT3 0x4
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
58
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 /*! \brief Observer factory.
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 *
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 * It provides functions for allocation of subject and observer objects,
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 * and strategy function for getting the subject of parent coord object.
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 */
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 struct _ob_factory {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 subject_t *(*subject_alloc)(ob_factory_t *factory);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 void (*subject_free)(ob_factory_t *factory, subject_t *subject);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 observer_t *(*observer_alloc)(ob_factory_t *factory);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 void (*observer_free)(ob_factory_t *factory, observer_t *observer);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 /*! This is a strategy function to get subjects of parents. */
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 subject_t *(*get_parent_subject)(ob_factory_t *factory,
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 subject_t *cur_subject);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 };
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 enum {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
75 EVT_MOUSE_BUT_PRESS, EVT_MOUSE_BUT_RELEASE,
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 125
diff changeset
76 EVT_KB_PRESS, EVT_KB_RELEASE, EVT_PROGM_COMPLETE,
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 125
diff changeset
77 EVT_RDMAN_REDRAW };
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 extern subject_t *subject_new(ob_factory_t *factory,
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 void *obj, int obj_type);
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
81 extern void subject_free(subject_t *subject);
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
82 extern 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
83 extern observer_t *subject_add_observer(subject_t *subject,
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 evt_handler hdr, void *arg);
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
85 extern void subject_remove_observer(subject_t *subject,
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 observer_t *observer);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 #endif /* __OBSERVER_H_ */