comparison include/mb_observer.h @ 186:530bb7728546 include_mb_test

Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'. This is the solution that I dicussed with FourDollars, last night.
author Thinker K.F. Li <thinker@branda.to>
date Wed, 05 Nov 2008 15:24:01 +0800
parents include/mb/observer.h@c7e5b8779bb5
children 54fdc2a65242
comparison
equal deleted inserted replaced
185:c7e5b8779bb5 186:530bb7728546
1 #ifndef __OBSERVER_H_
2 #define __OBSERVER_H_
3
4 #include "mb_tools.h"
5
6 typedef struct _event event_t;
7 typedef struct _observer observer_t;
8 typedef struct _subject subject_t;
9 typedef struct _mouse_event mouse_event_t;
10 typedef struct _ob_factory ob_factory_t;
11 typedef void (*evt_handler)(event_t *event, void *arg);
12
13 struct _event {
14 int type; /*!< event type (a.k.a. EVT_* */
15 subject_t *tgt, *cur_tgt;
16 };
17
18 /*! \brief Observer of observer pattern.
19 *
20 * A target for receiving events.
21 */
22 struct _observer {
23 evt_handler hdr;
24 void *arg;
25 observer_t *next;
26 };
27
28 /*! \brief Subject of observer pattern.
29 *
30 * Observer is a pattern to decouple caller and callee,
31 * especial for multiple callee.
32 * \see http://en.wikipedia.org/wiki/Observer_pattern
33 */
34 struct _subject {
35 int obj_type; /*!< \brief type of object (a.k.a. OBJT_*). */
36 void *obj; /*!< \brief the object this subject for. */
37 int flags;
38 STAILQ(observer_t) observers;
39 };
40 /*! \brief Flag that make a subject to propagate events to parents. */
41 #define SUBF_STOP_PROPAGATE 0x1
42 #define SUBF_BUSY 0x2 /*!< \brief in subject_notify() */
43 #define SUBF_FREE 0x4 /*!< \brief in postponding subject_free() */
44
45 enum {OBJT_GEO, OBJT_COORD, OBJT_KB, OBJT_PROGM, OBJT_RDMAN};
46
47 struct _mouse_event {
48 event_t event;
49 int x, y;
50 unsigned int but_state;
51 unsigned int button;
52 };
53
54 #define MOUSE_BUT1 0x1
55 #define MOUSE_BUT2 0x2
56 #define MOUSE_BUT3 0x4
57
58 /*! \brief Observer factory.
59 *
60 * It provides functions for allocation of subject and observer objects,
61 * and strategy function for getting the subject of parent coord object.
62 */
63 struct _ob_factory {
64 subject_t *(*subject_alloc)(ob_factory_t *factory);
65 void (*subject_free)(ob_factory_t *factory, subject_t *subject);
66 observer_t *(*observer_alloc)(ob_factory_t *factory);
67 void (*observer_free)(ob_factory_t *factory, observer_t *observer);
68 /*! This is a strategy function to get subjects of parents. */
69 subject_t *(*get_parent_subject)(ob_factory_t *factory,
70 subject_t *cur_subject);
71 };
72
73 enum {EVT_MOUSE_OVER, EVT_MOUSE_OUT, EVT_MOUSE_MOVE,
74 EVT_MOUSE_BUT_PRESS, EVT_MOUSE_BUT_RELEASE,
75 EVT_KB_PRESS, EVT_KB_RELEASE, EVT_PROGM_COMPLETE,
76 EVT_RDMAN_REDRAW };
77
78 extern subject_t *subject_new(ob_factory_t *factory,
79 void *obj, int obj_type);
80 extern void subject_free(ob_factory_t *factory, subject_t *subject);
81 extern void subject_notify(ob_factory_t *factory,
82 subject_t *subject, event_t *evt);
83 extern observer_t *subject_add_observer(ob_factory_t *factory,
84 subject_t *subject,
85 evt_handler hdr, void *arg);
86 extern void subject_remove_observer(ob_factory_t *factory,
87 subject_t *subject,
88 observer_t *observer);
89
90
91 #endif /* __OBSERVER_H_ */