blob: f4df4482bf7b997141c26746460da30894eef774 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkEvent_DEFINED
9#define SkEvent_DEFINED
10
11#include "SkDOM.h"
12#include "SkMetaData.h"
13#include "SkString.h"
14
reed@android.com8a1c16f2008-12-17 15:59:43 +000015/** Unique 32bit id used to identify an instance of SkEventSink. When events are
16 posted, they are posted to a specific sinkID. When it is time to dispatch the
17 event, the sinkID is used to find the specific SkEventSink object. If it is found,
18 its doEvent() method is called with the event.
19*/
20typedef uint32_t SkEventSinkID;
21
reed@google.com87fac4a2011-08-04 13:50:17 +000022/**
23 * \class SkEvent
24 *
25 * When an event is dispatched from the event queue, it is either sent to
26 * the eventsink matching the target ID (if not 0), or the target proc is
27 * called (if not NULL).
28 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000029class SkEvent {
30public:
reed@google.com87fac4a2011-08-04 13:50:17 +000031 /**
32 * Function pointer that takes an event, returns true if it "handled" it.
33 */
34 typedef bool (*Proc)(const SkEvent& evt);
35
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 SkEvent();
reed@google.com87fac4a2011-08-04 13:50:17 +000037 explicit SkEvent(const SkString& type, SkEventSinkID = 0);
38 explicit SkEvent(const char type[], SkEventSinkID = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 SkEvent(const SkEvent& src);
40 ~SkEvent();
41
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 /** Copy the event's type into the specified SkString parameter */
reed@google.com87fac4a2011-08-04 13:50:17 +000043 void getType(SkString* str) const;
44
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 /** Returns true if the event's type matches exactly the specified type (case sensitive) */
reed@google.com87fac4a2011-08-04 13:50:17 +000046 bool isType(const SkString& str) const;
47
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 /** Returns true if the event's type matches exactly the specified type (case sensitive) */
reed@google.com87fac4a2011-08-04 13:50:17 +000049 bool isType(const char type[], size_t len = 0) const;
50
51 /**
52 * Set the event's type to the specified string.
53 */
54 void setType(const SkString&);
55
56 /**
57 * Set the event's type to the specified string.
58 */
59 void setType(const char type[], size_t len = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000060
reed@google.comc514dde2011-08-03 19:41:24 +000061 /**
62 * Return the target ID, or 0 if there is none.
reed@google.com87fac4a2011-08-04 13:50:17 +000063 *
64 * When an event is dispatched from the event queue, it is either sent to
65 * the eventsink matching the targetID (if not 0), or the target proc is
66 * called (if not NULL).
reed@google.comc514dde2011-08-03 19:41:24 +000067 */
68 SkEventSinkID getTargetID() const { return fTargetID; }
69
70 /**
reed@google.com87fac4a2011-08-04 13:50:17 +000071 * Set the target ID for this event. 0 means none. Calling this will
72 * automatically clear the targetProc to null.
73 *
74 * When an event is dispatched from the event queue, it is either sent to
75 * the eventsink matching the targetID (if not 0), or the target proc is
76 * called (if not NULL).
reed@google.comc514dde2011-08-03 19:41:24 +000077 */
reed@google.com87fac4a2011-08-04 13:50:17 +000078 SkEvent* setTargetID(SkEventSinkID targetID) {
79 fTargetProc = NULL;
80 fTargetID = targetID;
81 return this;
82 }
reed@google.comc514dde2011-08-03 19:41:24 +000083
reed@google.com87fac4a2011-08-04 13:50:17 +000084 /**
85 * Return the target proc, or NULL if it has none.
86 *
87 * When an event is dispatched from the event queue, it is either sent to
88 * the eventsink matching the targetID (if not 0), or the target proc is
89 * called (if not NULL).
90 */
91 Proc getTargetProc() const { return fTargetProc; }
92
93 /**
94 * Set the target ID for this event. NULL means none. Calling this will
95 * automatically clear the targetID to 0.
96 *
97 * When an event is dispatched from the event queue, it is either sent to
98 * the eventsink matching the targetID (if not 0), or the target proc is
99 * called (if not NULL).
100 */
101 SkEvent* setTargetProc(Proc proc) {
102 fTargetID = 0;
103 fTargetProc = proc;
104 return this;
105 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000106
reed@google.com87fac4a2011-08-04 13:50:17 +0000107 /**
108 * Return the event's unnamed 32bit field. Default value is 0
109 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 uint32_t getFast32() const { return f32; }
reed@google.com87fac4a2011-08-04 13:50:17 +0000111
112 /**
113 * Set the event's unnamed 32bit field.
114 */
115 void setFast32(uint32_t x) { f32 = x; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116
117 /** Return true if the event contains the named 32bit field, and return the field
118 in value (if value is non-null). If there is no matching named field, return false
119 and ignore the value parameter.
120 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000121 bool findS32(const char name[], int32_t* value = NULL) const { return fMeta.findS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 /** Return true if the event contains the named SkScalar field, and return the field
123 in value (if value is non-null). If there is no matching named field, return false
124 and ignore the value parameter.
125 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000126 bool findScalar(const char name[], SkScalar* value = NULL) const { return fMeta.findScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 /** Return true if the event contains the named SkScalar field, and return the fields
128 in value[] (if value is non-null), and return the number of SkScalars in count (if count is non-null).
129 If there is no matching named field, return false and ignore the value and count parameters.
130 */
131 const SkScalar* findScalars(const char name[], int* count, SkScalar values[] = NULL) const { return fMeta.findScalars(name, count, values); }
132 /** Return the value of the named string field, or if no matching named field exists, return null.
133 */
134 const char* findString(const char name[]) const { return fMeta.findString(name); }
135 /** Return true if the event contains the named pointer field, and return the field
136 in value (if value is non-null). If there is no matching named field, return false
137 and ignore the value parameter.
138 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000139 bool findPtr(const char name[], void** value) const { return fMeta.findPtr(name, value); }
140 bool findBool(const char name[], bool* value) const { return fMeta.findBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000141 const void* findData(const char name[], size_t* byteCount = NULL) const {
142 return fMeta.findData(name, byteCount);
143 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144
145 /** Returns true if ethe event contains the named 32bit field, and if it equals the specified value */
reed@google.com87fac4a2011-08-04 13:50:17 +0000146 bool hasS32(const char name[], int32_t value) const { return fMeta.hasS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 /** Returns true if ethe event contains the named SkScalar field, and if it equals the specified value */
reed@google.com87fac4a2011-08-04 13:50:17 +0000148 bool hasScalar(const char name[], SkScalar value) const { return fMeta.hasScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 /** Returns true if ethe event contains the named string field, and if it equals (using strcmp) the specified value */
reed@google.com87fac4a2011-08-04 13:50:17 +0000150 bool hasString(const char name[], const char value[]) const { return fMeta.hasString(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 /** Returns true if ethe event contains the named pointer field, and if it equals the specified value */
reed@google.com87fac4a2011-08-04 13:50:17 +0000152 bool hasPtr(const char name[], void* value) const { return fMeta.hasPtr(name, value); }
153 bool hasBool(const char name[], bool value) const { return fMeta.hasBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000154 bool hasData(const char name[], const void* data, size_t byteCount) const {
155 return fMeta.hasData(name, data, byteCount);
156 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157
158 /** Add/replace the named 32bit field to the event. In XML use the subelement <data name=... s32=... /> */
reed@google.com87fac4a2011-08-04 13:50:17 +0000159 void setS32(const char name[], int32_t value) { fMeta.setS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 /** Add/replace the named SkScalar field to the event. In XML use the subelement <data name=... scalar=... /> */
reed@google.com87fac4a2011-08-04 13:50:17 +0000161 void setScalar(const char name[], SkScalar value) { fMeta.setScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 /** Add/replace the named SkScalar[] field to the event. */
163 SkScalar* setScalars(const char name[], int count, const SkScalar values[] = NULL) { return fMeta.setScalars(name, count, values); }
164 /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */
reed@google.com87fac4a2011-08-04 13:50:17 +0000165 void setString(const char name[], const SkString& value) { fMeta.setString(name, value.c_str()); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */
reed@google.com87fac4a2011-08-04 13:50:17 +0000167 void setString(const char name[], const char value[]) { fMeta.setString(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 /** Add/replace the named pointer field to the event. There is no XML equivalent for this call */
reed@google.com87fac4a2011-08-04 13:50:17 +0000169 void setPtr(const char name[], void* value) { fMeta.setPtr(name, value); }
170 void setBool(const char name[], bool value) { fMeta.setBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000171 void setData(const char name[], const void* data, size_t byteCount) {
172 fMeta.setData(name, data, byteCount);
173 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174
175 /** Return the underlying metadata object */
reed@google.com87fac4a2011-08-04 13:50:17 +0000176 SkMetaData& getMetaData() { return fMeta; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 /** Return the underlying metadata object */
reed@google.com87fac4a2011-08-04 13:50:17 +0000178 const SkMetaData& getMetaData() const { return fMeta; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179
180 /** Call this to initialize the event from the specified XML node */
reed@google.com87fac4a2011-08-04 13:50:17 +0000181 void inflate(const SkDOM&, const SkDOM::Node*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182
183 SkDEBUGCODE(void dump(const char title[] = NULL);)
184
reed@google.com87fac4a2011-08-04 13:50:17 +0000185 ///////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186
reed@google.comc514dde2011-08-03 19:41:24 +0000187 /**
reed@google.com87fac4a2011-08-04 13:50:17 +0000188 * Post to the event queue using the event's targetID or target-proc.
189 *
190 * The event must be dynamically allocated, as ownership is transferred to
191 * the event queue. It cannot be allocated on the stack or in a global.
reed@google.comc514dde2011-08-03 19:41:24 +0000192 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000193 void post() {
reed@google.comc514dde2011-08-03 19:41:24 +0000194 return this->postDelay(0);
195 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000196
reed@google.comc514dde2011-08-03 19:41:24 +0000197 /**
reed@google.com87fac4a2011-08-04 13:50:17 +0000198 * Post to the event queue using the event's targetID or target-proc and
199 * the specifed millisecond delay.
200 *
201 * The event must be dynamically allocated, as ownership is transferred to
202 * the event queue. It cannot be allocated on the stack or in a global.
reed@google.comc514dde2011-08-03 19:41:24 +0000203 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000204 void postDelay(SkMSec delay);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000205
reed@google.comc514dde2011-08-03 19:41:24 +0000206 /**
reed@google.com87fac4a2011-08-04 13:50:17 +0000207 * Post to the event queue using the event's targetID or target-proc.
208 * The event will be delivered no sooner than the specified millisecond
209 * time, as measured by SkTime::GetMSecs().
210 *
211 * The event must be dynamically allocated, as ownership is transferred to
212 * the event queue. It cannot be allocated on the stack or in a global.
reed@google.comc514dde2011-08-03 19:41:24 +0000213 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000214 void postTime(SkMSec time);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215
216 ///////////////////////////////////////////////
217 /** Porting layer must call these functions **/
218 ///////////////////////////////////////////////
219
220 /** Global initialization function for the SkEvent system. Should be called exactly
221 once before any other event method is called, and should be called after the
222 call to SkGraphics::Init().
223 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000224 static void Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225 /** Global cleanup function for the SkEvent system. Should be called exactly once after
226 all event methods have been called, and should be called before calling SkGraphics::Term().
227 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000228 static void Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229
230 /** Call this to process one event from the queue. If it returns true, there are more events
231 to process.
232 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000233 static bool ProcessEvent();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 /** Call this whenever the requested timer has expired (requested by a call to SetQueueTimer).
235 It will post any delayed events whose time as "expired" onto the event queue.
236 It may also call SignalQueueTimer() and SignalNonEmptyQueue().
237 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000238 static void ServiceQueueTimer();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239
reed@android.comf2b98d62010-12-20 18:26:13 +0000240 /** Return the number of queued events. note that this value may be obsolete
241 upon return, since another thread may have called ProcessEvent() or
242 Post() after the count was made.
243 */
244 static int CountEventsOnQueue();
245
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 ////////////////////////////////////////////////////
247 /** Porting layer must implement these functions **/
248 ////////////////////////////////////////////////////
249
250 /** Called whenever an SkEvent is posted to an empty queue, so that the OS
251 can be told to later call Dequeue().
252 */
253 static void SignalNonEmptyQueue();
254 /** Called whenever the delay until the next delayed event changes. If zero is
255 passed, then there are no more queued delay events.
256 */
257 static void SignalQueueTimer(SkMSec delay);
258
tfarina@chromium.org887760f2012-10-01 16:14:21 +0000259#if defined(SK_BUILD_FOR_WIN)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 static bool WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262
263private:
264 SkMetaData fMeta;
265 mutable char* fType; // may be characters with low bit set to know that it is not a pointer
266 uint32_t f32;
reed@google.com87fac4a2011-08-04 13:50:17 +0000267
268 // 'there can be only one' (non-zero) between target-id and target-proc
reed@google.comc514dde2011-08-03 19:41:24 +0000269 SkEventSinkID fTargetID;
reed@google.com87fac4a2011-08-04 13:50:17 +0000270 Proc fTargetProc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271
272 // these are for our implementation of the event queue
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 SkMSec fTime;
274 SkEvent* fNextEvent; // either in the delay or normal event queue
reed@google.com87fac4a2011-08-04 13:50:17 +0000275
276 void initialize(const char* type, size_t typeLen, SkEventSinkID);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277
278 static bool Enqueue(SkEvent* evt);
279 static SkMSec EnqueueTime(SkEvent* evt, SkMSec time);
reed@google.com87fac4a2011-08-04 13:50:17 +0000280 static SkEvent* Dequeue();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281 static bool QHasEvents();
282};
283
284#endif