Mercurial > MadButterfly
annotate src/observer.h @ 123:9e2316dc6ecb
Program completion events
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 16 Sep 2008 14:19:26 +0800 |
parents | 17e97e92b76e |
children | 1c1f28c124c9 |
rev | line source |
---|---|
73 | 1 #ifndef __OBSERVER_H_ |
2 #define __OBSERVER_H_ | |
3 | |
4 #include "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 { | |
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 | 15 subject_t *tgt, *cur_tgt; |
16 }; | |
17 | |
78 | 18 /*! \brief Observer of observer pattern. |
19 * | |
20 * A target for receiving events. | |
21 */ | |
73 | 22 struct _observer { |
23 evt_handler hdr; | |
24 void *arg; | |
25 observer_t *next; | |
26 }; | |
27 | |
78 | 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 */ | |
73 | 34 struct _subject { |
78 | 35 int obj_type; /*!< type of object (a.k.a. OBJT_*). */ |
36 void *obj; /*!< the object this subject for. */ | |
73 | 37 int flags; |
38 STAILQ(observer_t) observers; | |
39 }; | |
78 | 40 /*! \brief Flag that make a subject to propagate events to parents. */ |
73 | 41 #define SUBF_STOP_PROPAGATE 0x1 |
42 | |
123
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
43 enum {OBJT_GEO, OBJT_COORD, OBJT_KB, OBJT_PROGM}; |
73 | 44 |
45 struct _mouse_event { | |
46 event_t event; | |
47 int x, y; | |
78 | 48 unsigned int but_state; |
49 unsigned int button; | |
73 | 50 }; |
51 | |
76 | 52 #define MOUSE_BUT1 0x1 |
53 #define MOUSE_BUT2 0x2 | |
54 #define MOUSE_BUT3 0x4 | |
55 | |
73 | 56 /*! \brief Observer factory. |
57 * | |
58 * It provides functions for allocation of subject and observer objects, | |
59 * and strategy function for getting the subject of parent coord object. | |
60 */ | |
61 struct _ob_factory { | |
62 subject_t *(*subject_alloc)(ob_factory_t *factory); | |
63 void (*subject_free)(ob_factory_t *factory, subject_t *subject); | |
64 observer_t *(*observer_alloc)(ob_factory_t *factory); | |
65 void (*observer_free)(ob_factory_t *factory, observer_t *observer); | |
66 /*! This is a strategy function to get subjects of parents. */ | |
67 subject_t *(*get_parent_subject)(ob_factory_t *factory, | |
68 subject_t *cur_subject); | |
69 }; | |
70 | |
71 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
|
72 EVT_MOUSE_BUT_PRESS, EVT_MOUSE_BUT_RELEASE, |
123
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
73 EVT_KB_PRESS, EVT_KB_RELEASE, EVT_PROGM_COMPLETE}; |
73 | 74 |
75 extern subject_t *subject_new(ob_factory_t *factory, | |
76 void *obj, int obj_type); | |
77 extern void subject_free(ob_factory_t *factory, subject_t *subject); | |
78 extern void subject_notify(ob_factory_t *factory, | |
79 subject_t *subject, event_t *evt); | |
80 extern observer_t *subject_add_observer(ob_factory_t *factory, | |
81 subject_t *subject, | |
82 evt_handler hdr, void *arg); | |
83 extern void subject_remove_observer(ob_factory_t *factory, | |
84 subject_t *subject, | |
85 observer_t *observer); | |
86 | |
87 | |
88 #endif /* __OBSERVER_H_ */ |