µGFX  2.9
version 2.9
gevent.h
Go to the documentation of this file.
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.io/license.html
6  */
7 
8 /**
9  * @file src/gevent/gevent.h
10  *
11  * @addtogroup GEVENT
12  *
13  * @brief Module to build a complete many-to-many event system
14  *
15  * @details GEVENT provides a simple to use but yet powerful event
16  * system.
17  *
18  * @pre GFX_USE_GEVENT must be set to GFXON in your gfxconf.h
19  *
20  * @{
21  */
22 #ifndef _GEVENT_H
23 #define _GEVENT_H
24 
25 #include "../../gfx.h"
26 
27 #if GFX_USE_GEVENT || defined(__DOXYGEN__)
28 
29 /*===========================================================================*/
30 /* Type definitions */
31 /*===========================================================================*/
32 
33 typedef gU16 GEventType;
34  #define GEVENT_NULL 0x0000 // Null Event - Do nothing
35  #define GEVENT_EXIT 0x0001 // The listener is being forced to exit (someone is destroying the listener)
36 
37  /* Other event types are allocated in ranges in their respective include files */
38  #define GEVENT_GINPUT_FIRST 0x0100 // GINPUT events range from 0x0100 to 0x01FF
39  #define GEVENT_GWIN_FIRST 0x0200 // GWIN events range from 0x0200 to 0x02FF
40  #define GEVENT_GADC_FIRST 0x0300 // GADC events range from 0x0300 to 0x033F
41  #define GEVENT_GAUDIO_FIRST 0x0340 // GAUDIO events range from 0x0340 to 0x037F
42  #define GEVENT_USER_FIRST 0x8000 // Any application defined events start at 0x8000
43 
44 // This object can be typecast to any GEventXxxxx type to allow any sub-system (or the application) to create events.
45 // The prerequisite is that the new status structure type starts with a field named 'type' of type 'GEventType'.
46 // The total status structure also must not exceed GEVENT_MAXIMUM_SIZE bytes.
47 // For example, this is used by GWIN button events, GINPUT data streams etc.
48 typedef union GEvent_u {
49  GEventType type; // The type of this event
50  char pad[GEVENT_MAXIMUM_SIZE]; // This is here to allow static initialisation of GEventObject's in the application.
51 } GEvent;
52 
53 // A special callback function
54 typedef void (*GEventCallbackFn)(void *param, GEvent *pe);
55 
56 // The Listener Object
57 typedef struct GListener {
58  gSem waitqueue; // Private: Semaphore for the listener to wait on.
59  gU16 flags; // Private: Flags for operation
60  GEventCallbackFn callback; // Private: Call back Function
61  void *param; // Private: Parameter for the callback function.
62  GEvent event; // Public: The event object into which the event information is stored.
63  } GListener;
64 
65 // The Source Object
66 typedef struct GSource_t GSource, *GSourceHandle;
67 
68 // This structure is passed to a source to describe a contender listener for sending the current event.
69 typedef struct GSourceListener_t {
70  GListener *pListener; // The listener
71  GSource *pSource; // The source
72  gU32 listenflags; // The flags the listener passed when the source was assigned to it.
73  gU32 srcflags; // For the source's exclusive use. Initialised as 0 for a new listener source assignment.
74  } GSourceListener;
75 
76 /*===========================================================================*/
77 /* External declarations. */
78 /*===========================================================================*/
79 
80 /* How to listen for events (act as a Listener)...
81  1. Get handles for all the event sources you are interested in.
82  2. Initialise a listener
83  3. Attach sources to your listener.
84  - Sources can be attached or detached from a listener at any time.
85  - A source can be attached to more than one listener.
86  4. Loop on getting listener events
87  5. When finished detach all sources from the listener
88 
89  How to create events (act as a Source)...
90  1. Provide a funtion to the application that returns a GSourceHandle (which can be a pointer to whatever the source wants)
91  2. Whenever a possible event occurs call geventGetSourceListener() to get a pointer to a GSourceListener.
92  This will return NULL when there are no more listeners.
93  For each listener - check the flags to see if an event should be sent.
94  - use geventGetEventBuffer() to get the event buffer supplied by the listener
95  and then call geventSendEvent() to send the event.
96  - Note: geventGetEventBuffer() may return gFalse to indicate the listener is currently not listening and
97  therefore no event should be sent. This situation enables the source to (optionally) flag
98  to the listener on its next wait that there have been missed events.
99  - Note: The GSourceListener pointer (and the GEvent buffer) are only valid between
100  the geventGetSourceListener() call and either the geventSendEvent call or the next
101  geventGetSourceListener() call.
102  - Note: All listeners must be processed for this event before anything else is processed.
103 */
104 
105 /*---------- Listener Functions --------------------------------------------*/
106 
107 /**
108  * @brief Create a Listener
109  * @details If insufficient resources are available it will either assert or return NULL
110  * depending on the value of GEVENT_ASSERT_NO_RESOURCE.
111  *
112  * @param[in] pl A listener
113  */
114 void geventListenerInit(GListener *pl);
115 
116 /**
117  * @brief Attach a source to a listener
118  * @details Flags are interpreted by the source when generating events for each listener.
119  * If this source is already assigned to the listener it will update the flags.
120  * If insufficient resources are available it will either assert or return gFalse
121  * depending on the value of GEVENT_ASSERT_NO_RESOURCE.
122  *
123  * @param[in] pl The listener
124  * @param[in] gsh The source which has to be attached to the listener
125  * @param[in] flags The flags
126  *
127  * @return gTrue if succeeded, gFalse otherwise
128  */
129 gBool geventAttachSource(GListener *pl, GSourceHandle gsh, gU32 flags);
130 
131 /**
132  * @brief Detach a source from a listener
133  * @details If gsh is NULL detach all sources from this listener and if there is still
134  * a thread waiting for events on this listener, it is sent the exit event.
135  *
136  * @param[in] pl The listener
137  * @param[in] gsh The source
138  */
139 void geventDetachSource(GListener *pl, GSourceHandle gsh);
140 
141 /**
142  * @brief Wait for an event on a listener from an assigned source.
143  * @details The type of the event should be checked (pevent->type) and then pevent should
144  * be typecast to the actual event type if it needs to be processed.
145  * gDelayForever means no timeout - wait forever for an event.
146  * gDelayNone means return immediately
147  * @note The returned GEvent is released when this routine is called again
148  * or when optionally @p geventEventComplete() is called. Calling @p geventEventComplete()
149  * allows the GEvent object to be reused earlier which can reduce missed events. The GEvent
150  * object MUST NOT be used after this function is called (and is blocked waiting for the next
151  * event) or after geventEventComplete() is called.
152  * The one listener object should not be waited on using more than one thread simultanously
153  * because of the implicit geventEventComplete() that occurs when this function is called.
154  *
155  * @param[in] pl The listener
156  * @param[in] timeout The timeout in milliseconds
157  *
158  * @return NULL on timeout
159  */
160 GEvent *geventEventWait(GListener *pl, gDelay timeout);
161 
162 /**
163  * @brief Release the GEvent buffer associated with a listener.
164  * @details The GEvent returned by @p geventEventWait() is released.
165  * @note The GEvent pointer returned by @p geventEventWait() is released when @p geventEventWait()
166  * is called again or when this function is called. The GEvent
167  * object MUST NOT be used after this function is called.
168  *
169  * @param[in] pl The listener
170  */
171 void geventEventComplete(GListener *pl);
172 
173 /* @brief Register a callback for an event on a listener from an assigned source.
174  * @details The type of the event should be checked (pevent->type) and then pevent should be typecast to the
175  * actual event type if it needs to be processed.
176  *
177  * @params[in] pl The Listener
178  * @params[in] fn The function to call back
179  * @params[in] param A parameter to pass the callback function
180  *
181  * @note The GEvent buffer is valid only during the time of the callback. The callback MUST NOT save
182  * a pointer to the buffer for use outside the callback.
183  * @note An existing callback function is de-registered by passing a NULL for 'fn'. Any existing
184  * callback function is replaced. Any thread currently waiting using geventEventWait will be sent the exit event.
185  * @note Callbacks occur in a thread context but stack space must be kept to a minumum and
186  * the callback must process quickly as all other events are performed on a single thread.
187  * @note In the callback function you should never call ANY event functions using your own GListener handle
188  * as it WILL create a deadlock and lock the system up.
189  * @note Applications should not use this call - geventEventWait() is the preferred mechanism for an
190  * application. This call is provided for GUI objects that may not have their own thread.
191  */
192 void geventRegisterCallback(GListener *pl, GEventCallbackFn fn, void *param);
193 
194 /*---------- Source Functions --------------------------------------------*/
195 
196 /**
197  * @brief Called by a source with a possible event to get a listener record.
198  * @details @p lastlr should be NULL on the first call and thereafter the result of the previous call.
199  *
200  * @param[in] gsh The source handler
201  * @param[in] lastlr The source listener
202  *
203  * @return NULL when there are no more listeners for this source
204  */
205 GSourceListener *geventGetSourceListener(GSourceHandle gsh, GSourceListener *lastlr);
206 
207 /**
208  * @brief Get the event buffer from the GSourceListener.
209  * @details A NULL return allows the source to record (perhaps in glr->scrflags) that the listener
210  * has missed events. This can then be notified as part of the next event for the listener.
211  * The buffer can only be accessed untill the next call to @p geventGetSourceListener()
212  * or @p geventSendEvent()
213  *
214  * @param[in] psl The source listener
215  *
216  * @return NULL if the event buffer for this listener is currently in use.
217  */
218 GEvent *geventGetEventBuffer(GSourceListener *psl);
219 
220 /**
221  * @brief Called by a source to indicate the listener's event buffer has been filled.
222  * @details After calling this function the source must not reference in fields in the @p GSourceListener or the event buffer.
223  *
224  * @param[in] psl The source listener
225  */
226 void geventSendEvent(GSourceListener *psl);
227 
228 /**
229  * @brief Detach any listener that has this source attached
230  *
231  * @param[in] gsh The source handle
232  */
233 void geventDetachSourceListeners(GSourceHandle gsh);
234 
235 #endif /* GFX_USE_GEVENT */
236 
237 #endif /* _GEVENT_H */
238 /** @} */
239 
void geventListenerInit(GListener *pl)
Create a Listener.
Definition: gevent.c:64
GEvent * geventGetEventBuffer(GSourceListener *psl)
Get the event buffer from the GSourceListener.
Definition: gevent.c:187
void geventDetachSource(GListener *pl, GSourceHandle gsh)
Detach a source from a listener.
Definition: gevent.c:108
void geventSendEvent(GSourceListener *psl)
Called by a source to indicate the listener's event buffer has been filled.
Definition: gevent.c:200
GEvent * geventEventWait(GListener *pl, gDelay timeout)
Wait for an event on a listener from an assigned source.
Definition: gevent.c:118
gBool geventAttachSource(GListener *pl, GSourceHandle gsh, gU32 flags)
Attach a source to a listener.
Definition: gevent.c:71
GSourceListener * geventGetSourceListener(GSourceHandle gsh, GSourceListener *lastlr)
Called by a source with a possible event to get a listener record.
Definition: gevent.c:163
void geventDetachSourceListeners(GSourceHandle gsh)
Detach any listener that has this source attached.
Definition: gevent.c:220
#define GEVENT_MAXIMUM_SIZE
Defines the maximum size of an event status variable.
void geventEventComplete(GListener *pl)
Release the GEvent buffer associated with a listener.
Definition: gevent.c:146
A semaphore.
Definition: gos.h:104