comparison include/mb_observer.h @ 224:29e1b2bffe4c

X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly. - backend does not interpret mouse events (over/out/move), now. - redraw manager, now, interpret mouse events to make it backend independent. - The task (interpret mouse events) should be moved to somewhere in futhure. - backend only sent MotionNotify as EVT_MOUSE_MOVE_RAW. - EVT_MOUSE_MOVE_RAW is interpreted by backend independent code.
author Thinker K.F. Li <thinker@branda.to>
date Mon, 15 Dec 2008 10:13:03 +0800
parents 748896358da2
children 586e50f82c1f
comparison
equal deleted inserted replaced
223:8be36a0d4239 224:29e1b2bffe4c
5 5
6 typedef struct _event event_t; 6 typedef struct _event event_t;
7 typedef struct _observer observer_t; 7 typedef struct _observer observer_t;
8 typedef struct _subject subject_t; 8 typedef struct _subject subject_t;
9 typedef struct _mouse_event mouse_event_t; 9 typedef struct _mouse_event mouse_event_t;
10 typedef struct _monitor_event monitor_event_t;
10 typedef struct _ob_factory ob_factory_t; 11 typedef struct _ob_factory ob_factory_t;
11 typedef void (*evt_handler)(event_t *event, void *arg); 12 typedef void (*evt_handler)(event_t *event, void *arg);
12 13
13 struct _event { 14 struct _event {
14 int type; /*!< event type (a.k.a. EVT_* */ 15 int type; /*!< event type (a.k.a. EVT_*) */
15 subject_t *tgt, *cur_tgt; 16 subject_t *tgt, *cur_tgt;
17 int flags;
16 }; 18 };
19
20 /*! \brief Observer mark event with EVTF_STOP_PROPAGATE flag
21 * to stop propagation.
22 */
23 #define EVTF_STOP_PROPAGATE 0x1
24 /*! \brief Observer mark event with EVTF_STOP_NOTIFY flag to stop
25 * stop notification the event immediately.
26 */
27 #define EVTF_STOP_NOTIFY 0x2
17 28
18 /*! \brief Observer of observer pattern. 29 /*! \brief Observer of observer pattern.
19 * 30 *
20 * A target for receiving events. 31 * A target for receiving events.
21 */ 32 */
29 /*! \brief Subject of observer pattern. 40 /*! \brief Subject of observer pattern.
30 * 41 *
31 * Observer is a pattern to decouple caller and callee, 42 * Observer is a pattern to decouple caller and callee,
32 * especial for multiple callee. 43 * especial for multiple callee.
33 * \see http://en.wikipedia.org/wiki/Observer_pattern 44 * \see http://en.wikipedia.org/wiki/Observer_pattern
45 *
46 * This implementation add a monitor facility to monitor adding/removing
47 * observers from subjects. Monitor is another subject that monitor events
48 * will be sent to if it is existed.
34 */ 49 */
35 struct _subject { 50 struct _subject {
36 int obj_type; /*!< \brief type of object (a.k.a. OBJT_*). */ 51 int obj_type; /*!< \brief type of object (a.k.a. OBJT_*). */
37 void *obj; /*!< \brief the object this subject for. */ 52 void *obj; /*!< \brief the object this subject for. */
38 int flags; 53 int flags;
54 subject_t *monitor_sub; /*!< \brief Monitor adding/removing
55 * obervers on this subject. */
39 ob_factory_t *factory; 56 ob_factory_t *factory;
40 STAILQ(observer_t) observers; 57 STAILQ(observer_t) observers;
41 }; 58 };
42 /*! \brief Flag that make a subject to propagate events to parents. */ 59 /*! \brief Flag that make a subject to stop propagate events to parents. */
43 #define SUBF_STOP_PROPAGATE 0x1 60 #define SUBF_STOP_PROPAGATE 0x1
44 #define SUBF_BUSY 0x2 /*!< \brief in subject_notify() */ 61 #define SUBF_BUSY 0x2 /*!< \brief in subject_notify() */
45 #define SUBF_FREE 0x4 /*!< \brief in postponding subject_free() */ 62 #define SUBF_FREE 0x4 /*!< \brief in postponding subject_free() */
46 63
47 enum {OBJT_GEO, OBJT_COORD, OBJT_KB, OBJT_PROGM, OBJT_RDMAN}; 64 enum {OBJT_GEO, OBJT_COORD, OBJT_KB, OBJT_PROGM, OBJT_RDMAN};
54 }; 71 };
55 72
56 #define MOUSE_BUT1 0x1 73 #define MOUSE_BUT1 0x1
57 #define MOUSE_BUT2 0x2 74 #define MOUSE_BUT2 0x2
58 #define MOUSE_BUT3 0x4 75 #define MOUSE_BUT3 0x4
76
77 struct _monitor_event {
78 event_t event;
79 subject_t *subject; /*!< \brief Subject been monitored. */
80 observer_t *observer; /*!< \brief Observer been added or removed. */
81 };
59 82
60 /*! \brief Observer factory. 83 /*! \brief Observer factory.
61 * 84 *
62 * It provides functions for allocation of subject and observer objects, 85 * It provides functions for allocation of subject and observer objects,
63 * and strategy function for getting the subject of parent coord object. 86 * and strategy function for getting the subject of parent coord object.
73 }; 96 };
74 97
75 enum {EVT_ANY,EVT_MOUSE_OVER, EVT_MOUSE_OUT, EVT_MOUSE_MOVE, 98 enum {EVT_ANY,EVT_MOUSE_OVER, EVT_MOUSE_OUT, EVT_MOUSE_MOVE,
76 EVT_MOUSE_BUT_PRESS, EVT_MOUSE_BUT_RELEASE, 99 EVT_MOUSE_BUT_PRESS, EVT_MOUSE_BUT_RELEASE,
77 EVT_KB_PRESS, EVT_KB_RELEASE, EVT_PROGM_COMPLETE, 100 EVT_KB_PRESS, EVT_KB_RELEASE, EVT_PROGM_COMPLETE,
78 EVT_RDMAN_REDRAW }; 101 EVT_RDMAN_REDRAW,
102 EVT_MONITOR_ADD, EVT_MONITOR_REMOVE, EVT_MONITOR_FREE,
103 EVT_MOUSE_MOVE_RAW
104 };
79 105
80 extern subject_t *subject_new(ob_factory_t *factory, 106 extern subject_t *subject_new(ob_factory_t *factory,
81 void *obj, int obj_type); 107 void *obj, int obj_type);
82 extern void subject_free(subject_t *subject); 108 extern void subject_free(subject_t *subject);
83 extern void subject_notify(subject_t *subject, event_t *evt); 109 extern void subject_notify(subject_t *subject, event_t *evt);
84 extern observer_t *subject_add_observer(subject_t *subject,
85 evt_handler hdr, void *arg);
86 extern observer_t *subject_add_event_observer(subject_t *subject, int type, 110 extern observer_t *subject_add_event_observer(subject_t *subject, int type,
87 evt_handler hdr, void *arg); 111 evt_handler hdr, void *arg);
112 /*! \brief Add an observer for any type of events. */
113 #define subject_add_observer(s, h, a) \
114 subject_add_event_observer(s, EVT_ANY, h, a)
115 extern observer_t *subject_add_event_observer_head(subject_t *subject,
116 int type,
117 evt_handler hdr,
118 void *arg);
119 /*! \brief Add an observer for any type of events at head. */
120 #define subject_add_observer_head(s, h, a) \
121 subject_add_event_observer_head(s, EVT_ANY, h, a)
88 extern void subject_remove_observer(subject_t *subject, 122 extern void subject_remove_observer(subject_t *subject,
89 observer_t *observer); 123 observer_t *observer);
124 #define subject_get_object(s) ((s)->obj)
90 125
126 /*! \brief Set monitor for the subject.
127 *
128 * Monitor of a subject is another subject that would be notified when
129 * add/remove a observer to/from the subject. It can be used to efficiently
130 * implement translator to translate events.
131 */
132 #define subject_set_monitor(subject, monitor) \
133 do { (subject)->monitor_sub = monitor; } while(0)
134 #define subject_monitor(subject) ((subject)->monitor_sub)
91 135
92 #endif /* __OBSERVER_H_ */ 136 #endif /* __OBSERVER_H_ */