blob: f2726894f05c63191e88c466c8acbe93d7eb1236 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkEvent_DEFINED
11#define SkEvent_DEFINED
12
13#include "SkDOM.h"
14#include "SkMetaData.h"
15#include "SkString.h"
16
reed@android.com8a1c16f2008-12-17 15:59:43 +000017/** Unique 32bit id used to identify an instance of SkEventSink. When events are
18 posted, they are posted to a specific sinkID. When it is time to dispatch the
19 event, the sinkID is used to find the specific SkEventSink object. If it is found,
20 its doEvent() method is called with the event.
21*/
22typedef uint32_t SkEventSinkID;
23
24/** \class SkEvent
25
26 SkEvents are used to communicate type-safe information to SkEventSinks.
27 SkEventSinks (including SkViews) each have a unique ID, which is stored
28 in an event. This ID is used to target the event once it has been "posted".
29*/
30class SkEvent {
31public:
32 /** Default construct, creating an empty event.
33 */
34 SkEvent();
35 /** Construct a new event with the specified type.
36 */
37 explicit SkEvent(const SkString& type);
38 /** Construct a new event with the specified type.
39 */
40 explicit SkEvent(const char type[]);
41 /** Construct a new event by copying the fields from the src event.
42 */
43 SkEvent(const SkEvent& src);
44 ~SkEvent();
45
46// /** Return the event's type (will never be null) */
47// const char* getType() const;
48 /** Copy the event's type into the specified SkString parameter */
49 void getType(SkString* str) const;
50 /** Returns true if the event's type matches exactly the specified type (case sensitive) */
51 bool isType(const SkString& str) const;
52 /** Returns true if the event's type matches exactly the specified type (case sensitive) */
53 bool isType(const char type[], size_t len = 0) const;
54 /** Set the event's type to the specified string.
55 In XML, use the "type" attribute.
56 */
57 void setType(const SkString&);
58 /** Set the event's type to the specified string.
59 In XML, use the "type" attribute.
60 */
61 void setType(const char type[], size_t len = 0);
62
63 /** Return the event's unnamed 32bit field. Default value is 0 */
64 uint32_t getFast32() const { return f32; }
65 /** Set the event's unnamed 32bit field. In XML, use
66 the subelement <data fast32=... />
67 */
68 void setFast32(uint32_t x) { f32 = x; }
69
70 /** Return true if the event contains the named 32bit field, and return the field
71 in value (if value is non-null). If there is no matching named field, return false
72 and ignore the value parameter.
73 */
74 bool findS32(const char name[], int32_t* value = NULL) const { return fMeta.findS32(name, value); }
75 /** Return true if the event contains the named SkScalar field, and return the field
76 in value (if value is non-null). If there is no matching named field, return false
77 and ignore the value parameter.
78 */
79 bool findScalar(const char name[], SkScalar* value = NULL) const { return fMeta.findScalar(name, value); }
80 /** Return true if the event contains the named SkScalar field, and return the fields
81 in value[] (if value is non-null), and return the number of SkScalars in count (if count is non-null).
82 If there is no matching named field, return false and ignore the value and count parameters.
83 */
84 const SkScalar* findScalars(const char name[], int* count, SkScalar values[] = NULL) const { return fMeta.findScalars(name, count, values); }
85 /** Return the value of the named string field, or if no matching named field exists, return null.
86 */
87 const char* findString(const char name[]) const { return fMeta.findString(name); }
88 /** Return true if the event contains the named pointer field, and return the field
89 in value (if value is non-null). If there is no matching named field, return false
90 and ignore the value parameter.
91 */
92 bool findPtr(const char name[], void** value) const { return fMeta.findPtr(name, value); }
93 bool findBool(const char name[], bool* value) const { return fMeta.findBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +000094 const void* findData(const char name[], size_t* byteCount = NULL) const {
95 return fMeta.findData(name, byteCount);
96 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000097
98 /** Returns true if ethe event contains the named 32bit field, and if it equals the specified value */
99 bool hasS32(const char name[], int32_t value) const { return fMeta.hasS32(name, value); }
100 /** Returns true if ethe event contains the named SkScalar field, and if it equals the specified value */
101 bool hasScalar(const char name[], SkScalar value) const { return fMeta.hasScalar(name, value); }
102 /** Returns true if ethe event contains the named string field, and if it equals (using strcmp) the specified value */
103 bool hasString(const char name[], const char value[]) const { return fMeta.hasString(name, value); }
104 /** Returns true if ethe event contains the named pointer field, and if it equals the specified value */
105 bool hasPtr(const char name[], void* value) const { return fMeta.hasPtr(name, value); }
106 bool hasBool(const char name[], bool value) const { return fMeta.hasBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000107 bool hasData(const char name[], const void* data, size_t byteCount) const {
108 return fMeta.hasData(name, data, byteCount);
109 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110
111 /** Add/replace the named 32bit field to the event. In XML use the subelement <data name=... s32=... /> */
112 void setS32(const char name[], int32_t value) { fMeta.setS32(name, value); }
113 /** Add/replace the named SkScalar field to the event. In XML use the subelement <data name=... scalar=... /> */
114 void setScalar(const char name[], SkScalar value) { fMeta.setScalar(name, value); }
115 /** Add/replace the named SkScalar[] field to the event. */
116 SkScalar* setScalars(const char name[], int count, const SkScalar values[] = NULL) { return fMeta.setScalars(name, count, values); }
117 /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */
118 void setString(const char name[], const SkString& value) { fMeta.setString(name, value.c_str()); }
119 /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */
120 void setString(const char name[], const char value[]) { fMeta.setString(name, value); }
121 /** Add/replace the named pointer field to the event. There is no XML equivalent for this call */
122 void setPtr(const char name[], void* value) { fMeta.setPtr(name, value); }
123 void setBool(const char name[], bool value) { fMeta.setBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000124 void setData(const char name[], const void* data, size_t byteCount) {
125 fMeta.setData(name, data, byteCount);
126 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127
128 /** Return the underlying metadata object */
129 SkMetaData& getMetaData() { return fMeta; }
130 /** Return the underlying metadata object */
131 const SkMetaData& getMetaData() const { return fMeta; }
132
133 void tron() { SkDEBUGCODE(fDebugTrace = true;) }
134 void troff() { SkDEBUGCODE(fDebugTrace = false;) }
135 bool isDebugTrace() const
136 {
137#ifdef SK_DEBUG
138 return fDebugTrace;
139#else
140 return false;
141#endif
142 }
143
144 /** Call this to initialize the event from the specified XML node */
145 void inflate(const SkDOM&, const SkDOM::Node*);
146
147 SkDEBUGCODE(void dump(const char title[] = NULL);)
148
149 /** Post the specified event to the event queue, targeting the specified eventsink, with an optional
150 delay. The event must be dynamically allocated for this. It cannot be a global or on the stack.
151 After this call, ownership is transfered to the system, so the caller must not retain
152 the event's ptr. Returns false if the event could not be posted (which means it will have been deleted).
153 */
154 static bool Post(SkEvent* evt, SkEventSinkID targetID, SkMSec delay = 0);
155 /** Post the specified event to the event queue, targeting the specified eventsink, to be delivered on/after the
156 specified millisecond time. The event must be dynamically allocated for this. It cannot be a global or on the stack.
157 After this call, ownership is transfered to the system, so the caller must not retain
158 the event's ptr. Returns false if the event could not be posted (which means it will have been deleted).
159 */
160 static bool PostTime(SkEvent* evt, SkEventSinkID targetID, SkMSec time);
161
162 /** Helper method for calling SkEvent::PostTime(this, ...), where the caller specifies a delay.
163 The real "time" will be computed automatically by sampling the clock and adding its value
164 to delay.
165 */
166 bool post(SkEventSinkID sinkID, SkMSec delay = 0)
167 {
168 return SkEvent::Post(this, sinkID, delay);
169 }
170
171 void postTime(SkEventSinkID sinkID, SkMSec time)
172 {
173 SkEvent::PostTime(this, sinkID, time);
174 }
175
176 ///////////////////////////////////////////////
177 /** Porting layer must call these functions **/
178 ///////////////////////////////////////////////
179
180 /** Global initialization function for the SkEvent system. Should be called exactly
181 once before any other event method is called, and should be called after the
182 call to SkGraphics::Init().
183 */
184 static void Init();
185 /** Global cleanup function for the SkEvent system. Should be called exactly once after
186 all event methods have been called, and should be called before calling SkGraphics::Term().
187 */
188 static void Term();
189
190 /** Call this to process one event from the queue. If it returns true, there are more events
191 to process.
192 */
193 static bool ProcessEvent();
194 /** Call this whenever the requested timer has expired (requested by a call to SetQueueTimer).
195 It will post any delayed events whose time as "expired" onto the event queue.
196 It may also call SignalQueueTimer() and SignalNonEmptyQueue().
197 */
198 static void ServiceQueueTimer();
199
reed@android.comf2b98d62010-12-20 18:26:13 +0000200 /** Return the number of queued events. note that this value may be obsolete
201 upon return, since another thread may have called ProcessEvent() or
202 Post() after the count was made.
203 */
204 static int CountEventsOnQueue();
205
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206 ////////////////////////////////////////////////////
207 /** Porting layer must implement these functions **/
208 ////////////////////////////////////////////////////
209
210 /** Called whenever an SkEvent is posted to an empty queue, so that the OS
211 can be told to later call Dequeue().
212 */
213 static void SignalNonEmptyQueue();
214 /** Called whenever the delay until the next delayed event changes. If zero is
215 passed, then there are no more queued delay events.
216 */
217 static void SignalQueueTimer(SkMSec delay);
218
219#ifndef SK_USE_WXWIDGETS
220#ifdef SK_BUILD_FOR_WIN
221 static bool WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
222#elif defined(SK_BUILD_FOR_UNIXx)
223 static uint32_t HandleTimer(uint32_t, void*);
224 static bool WndProc(Display*, Window, XEvent&);
225#endif
226#else
227 // Don't know yet what this will be
228 //static bool CustomEvent();
229#endif
230
231private:
232 SkMetaData fMeta;
233 mutable char* fType; // may be characters with low bit set to know that it is not a pointer
234 uint32_t f32;
235 SkDEBUGCODE(bool fDebugTrace;)
236
237 // these are for our implementation of the event queue
238 SkEventSinkID fTargetID;
239 SkMSec fTime;
240 SkEvent* fNextEvent; // either in the delay or normal event queue
241 void initialize(const char* type, size_t typeLen);
242
243 static bool Enqueue(SkEvent* evt);
244 static SkMSec EnqueueTime(SkEvent* evt, SkMSec time);
245 static SkEvent* Dequeue(SkEventSinkID* targetID);
246 static bool QHasEvents();
247};
248
249#endif
250