blob: 091b7080a23b0cbbb8c7253694352d37138ac046 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkMetaData.h"
12#include "SkString.h"
13
Hal Canary20de6152017-02-23 13:24:49 -050014class SkDOM;
15struct SkDOMNode;
16
halcanary4dbbd042016-06-07 17:21:10 -070017#include "../private/SkLeanWindows.h"
18
reed@android.com8a1c16f2008-12-17 15:59:43 +000019/** Unique 32bit id used to identify an instance of SkEventSink. When events are
20 posted, they are posted to a specific sinkID. When it is time to dispatch the
21 event, the sinkID is used to find the specific SkEventSink object. If it is found,
22 its doEvent() method is called with the event.
23*/
24typedef uint32_t SkEventSinkID;
25
reed@google.com87fac4a2011-08-04 13:50:17 +000026/**
27 * \class SkEvent
28 *
29 * When an event is dispatched from the event queue, it is either sent to
30 * the eventsink matching the target ID (if not 0), or the target proc is
31 * called (if not NULL).
32 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000033class SkEvent {
34public:
reed@google.com87fac4a2011-08-04 13:50:17 +000035 /**
36 * Function pointer that takes an event, returns true if it "handled" it.
37 */
38 typedef bool (*Proc)(const SkEvent& evt);
39
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 SkEvent();
reed@google.com87fac4a2011-08-04 13:50:17 +000041 explicit SkEvent(const SkString& type, SkEventSinkID = 0);
42 explicit SkEvent(const char type[], SkEventSinkID = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 SkEvent(const SkEvent& src);
44 ~SkEvent();
45
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 /** Copy the event's type into the specified SkString parameter */
reed@google.com87fac4a2011-08-04 13:50:17 +000047 void getType(SkString* str) const;
48
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 /** Returns true if the event's type matches exactly the specified type (case sensitive) */
reed@google.com87fac4a2011-08-04 13:50:17 +000050 bool isType(const SkString& str) const;
51
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 /** Returns true if the event's type matches exactly the specified type (case sensitive) */
reed@google.com87fac4a2011-08-04 13:50:17 +000053 bool isType(const char type[], size_t len = 0) const;
54
55 /**
56 * Set the event's type to the specified string.
57 */
58 void setType(const SkString&);
59
60 /**
61 * Set the event's type to the specified string.
62 */
63 void setType(const char type[], size_t len = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000064
reed@google.comc514dde2011-08-03 19:41:24 +000065 /**
66 * Return the target ID, or 0 if there is none.
reed@google.com87fac4a2011-08-04 13:50:17 +000067 *
68 * When an event is dispatched from the event queue, it is either sent to
69 * the eventsink matching the targetID (if not 0), or the target proc is
70 * called (if not NULL).
reed@google.comc514dde2011-08-03 19:41:24 +000071 */
72 SkEventSinkID getTargetID() const { return fTargetID; }
73
74 /**
reed@google.com87fac4a2011-08-04 13:50:17 +000075 * Set the target ID for this event. 0 means none. Calling this will
76 * automatically clear the targetProc to null.
77 *
78 * When an event is dispatched from the event queue, it is either sent to
79 * the eventsink matching the targetID (if not 0), or the target proc is
80 * called (if not NULL).
reed@google.comc514dde2011-08-03 19:41:24 +000081 */
reed@google.com87fac4a2011-08-04 13:50:17 +000082 SkEvent* setTargetID(SkEventSinkID targetID) {
Ben Wagnera93a14a2017-08-28 10:34:05 -040083 fTargetProc = nullptr;
reed@google.com87fac4a2011-08-04 13:50:17 +000084 fTargetID = targetID;
85 return this;
86 }
reed@google.comc514dde2011-08-03 19:41:24 +000087
reed@google.com87fac4a2011-08-04 13:50:17 +000088 /**
89 * Return the target proc, or NULL if it has none.
90 *
91 * When an event is dispatched from the event queue, it is either sent to
92 * the eventsink matching the targetID (if not 0), or the target proc is
93 * called (if not NULL).
94 */
95 Proc getTargetProc() const { return fTargetProc; }
96
97 /**
98 * Set the target ID for this event. NULL means none. Calling this will
99 * automatically clear the targetID to 0.
100 *
101 * When an event is dispatched from the event queue, it is either sent to
102 * the eventsink matching the targetID (if not 0), or the target proc is
103 * called (if not NULL).
104 */
105 SkEvent* setTargetProc(Proc proc) {
106 fTargetID = 0;
107 fTargetProc = proc;
108 return this;
109 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000110
reed@google.com87fac4a2011-08-04 13:50:17 +0000111 /**
112 * Return the event's unnamed 32bit field. Default value is 0
113 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 uint32_t getFast32() const { return f32; }
reed@google.com87fac4a2011-08-04 13:50:17 +0000115
116 /**
117 * Set the event's unnamed 32bit field.
118 */
119 void setFast32(uint32_t x) { f32 = x; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120
121 /** Return true if the event contains the named 32bit field, and return the field
122 in value (if value is non-null). If there is no matching named field, return false
123 and ignore the value parameter.
124 */
Ben Wagnera93a14a2017-08-28 10:34:05 -0400125 bool findS32(const char name[], int32_t* value = nullptr) const {
126 return fMeta.findS32(name, value);
127 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 /** Return true if the event contains the named SkScalar field, and return the field
129 in value (if value is non-null). If there is no matching named field, return false
130 and ignore the value parameter.
131 */
Ben Wagnera93a14a2017-08-28 10:34:05 -0400132 bool findScalar(const char name[], SkScalar* value = nullptr) const {
133 return fMeta.findScalar(name, value);
134 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 /** Return true if the event contains the named SkScalar field, and return the fields
136 in value[] (if value is non-null), and return the number of SkScalars in count (if count is non-null).
137 If there is no matching named field, return false and ignore the value and count parameters.
138 */
Ben Wagnera93a14a2017-08-28 10:34:05 -0400139 const SkScalar* findScalars(const char name[], int* count, SkScalar values[] = nullptr) const {
140 return fMeta.findScalars(name, count, values);
141 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 /** Return the value of the named string field, or if no matching named field exists, return null.
143 */
144 const char* findString(const char name[]) const { return fMeta.findString(name); }
145 /** Return true if the event contains the named pointer field, and return the field
146 in value (if value is non-null). If there is no matching named field, return false
147 and ignore the value parameter.
148 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000149 bool findPtr(const char name[], void** value) const { return fMeta.findPtr(name, value); }
150 bool findBool(const char name[], bool* value) const { return fMeta.findBool(name, value); }
Ben Wagnera93a14a2017-08-28 10:34:05 -0400151 const void* findData(const char name[], size_t* byteCount = nullptr) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000152 return fMeta.findData(name, byteCount);
153 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154
155 /** 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 +0000156 bool hasS32(const char name[], int32_t value) const { return fMeta.hasS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 /** 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 +0000158 bool hasScalar(const char name[], SkScalar value) const { return fMeta.hasScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 /** 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 +0000160 bool hasString(const char name[], const char value[]) const { return fMeta.hasString(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 /** 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 +0000162 bool hasPtr(const char name[], void* value) const { return fMeta.hasPtr(name, value); }
163 bool hasBool(const char name[], bool value) const { return fMeta.hasBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000164 bool hasData(const char name[], const void* data, size_t byteCount) const {
165 return fMeta.hasData(name, data, byteCount);
166 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167
168 /** 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 +0000169 void setS32(const char name[], int32_t value) { fMeta.setS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 /** 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 +0000171 void setScalar(const char name[], SkScalar value) { fMeta.setScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 /** Add/replace the named SkScalar[] field to the event. */
Ben Wagnera93a14a2017-08-28 10:34:05 -0400173 SkScalar* setScalars(const char name[], int count, const SkScalar values[] = nullptr) {
174 return fMeta.setScalars(name, count, values);
175 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 /** 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 +0000177 void setString(const char name[], const SkString& value) { fMeta.setString(name, value.c_str()); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 /** 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 +0000179 void setString(const char name[], const char value[]) { fMeta.setString(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 /** 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 +0000181 void setPtr(const char name[], void* value) { fMeta.setPtr(name, value); }
182 void setBool(const char name[], bool value) { fMeta.setBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000183 void setData(const char name[], const void* data, size_t byteCount) {
184 fMeta.setData(name, data, byteCount);
185 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186
187 /** Return the underlying metadata object */
reed@google.com87fac4a2011-08-04 13:50:17 +0000188 SkMetaData& getMetaData() { return fMeta; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 /** Return the underlying metadata object */
reed@google.com87fac4a2011-08-04 13:50:17 +0000190 const SkMetaData& getMetaData() const { return fMeta; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191
192 /** Call this to initialize the event from the specified XML node */
Hal Canary20de6152017-02-23 13:24:49 -0500193 void inflate(const SkDOM&, const SkDOMNode*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194
Ben Wagnera93a14a2017-08-28 10:34:05 -0400195 SkDEBUGCODE(void dump(const char title[] = nullptr);)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196
reed@google.com87fac4a2011-08-04 13:50:17 +0000197 ///////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198
reed@google.comc514dde2011-08-03 19:41:24 +0000199 /**
reed@google.com87fac4a2011-08-04 13:50:17 +0000200 * Post to the event queue using the event's targetID or target-proc.
201 *
202 * The event must be dynamically allocated, as ownership is transferred to
203 * the event queue. It cannot be allocated on the stack or in a global.
reed@google.comc514dde2011-08-03 19:41:24 +0000204 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000205 void post() {
reed@google.comc514dde2011-08-03 19:41:24 +0000206 return this->postDelay(0);
207 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000208
reed@google.comc514dde2011-08-03 19:41:24 +0000209 /**
reed@google.com87fac4a2011-08-04 13:50:17 +0000210 * Post to the event queue using the event's targetID or target-proc and
211 * the specifed millisecond delay.
212 *
213 * The event must be dynamically allocated, as ownership is transferred to
214 * the event queue. It cannot be allocated on the stack or in a global.
reed@google.comc514dde2011-08-03 19:41:24 +0000215 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000216 void postDelay(SkMSec delay);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000217
reed@google.comc514dde2011-08-03 19:41:24 +0000218 /**
reed@google.com87fac4a2011-08-04 13:50:17 +0000219 * Post to the event queue using the event's targetID or target-proc.
220 * The event will be delivered no sooner than the specified millisecond
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700221 * time, as measured by GetMSecsSinceStartup().
reed@google.com87fac4a2011-08-04 13:50:17 +0000222 *
223 * The event must be dynamically allocated, as ownership is transferred to
224 * the event queue. It cannot be allocated on the stack or in a global.
reed@google.comc514dde2011-08-03 19:41:24 +0000225 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000226 void postTime(SkMSec time);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700228 /**
229 * Returns ~zero the first time it's called, then returns the number of
230 * milliseconds since the first call. Behavior is undefined if the program
231 * runs more than ~25 days.
232 */
233 static SkMSec GetMSecsSinceStartup();
234
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 ///////////////////////////////////////////////
236 /** Porting layer must call these functions **/
237 ///////////////////////////////////////////////
238
239 /** Global initialization function for the SkEvent system. Should be called exactly
240 once before any other event method is called, and should be called after the
241 call to SkGraphics::Init().
242 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000243 static void Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244 /** Global cleanup function for the SkEvent system. Should be called exactly once after
mtkleinfe81e2d2015-09-09 07:35:42 -0700245 all event methods have been called.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000247 static void Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248
249 /** Call this to process one event from the queue. If it returns true, there are more events
250 to process.
251 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000252 static bool ProcessEvent();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253 /** Call this whenever the requested timer has expired (requested by a call to SetQueueTimer).
254 It will post any delayed events whose time as "expired" onto the event queue.
255 It may also call SignalQueueTimer() and SignalNonEmptyQueue().
256 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000257 static void ServiceQueueTimer();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258
reed@android.comf2b98d62010-12-20 18:26:13 +0000259 /** Return the number of queued events. note that this value may be obsolete
260 upon return, since another thread may have called ProcessEvent() or
261 Post() after the count was made.
262 */
263 static int CountEventsOnQueue();
264
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 ////////////////////////////////////////////////////
266 /** Porting layer must implement these functions **/
267 ////////////////////////////////////////////////////
268
269 /** Called whenever an SkEvent is posted to an empty queue, so that the OS
270 can be told to later call Dequeue().
271 */
272 static void SignalNonEmptyQueue();
273 /** Called whenever the delay until the next delayed event changes. If zero is
274 passed, then there are no more queued delay events.
275 */
276 static void SignalQueueTimer(SkMSec delay);
277
tfarina@chromium.org887760f2012-10-01 16:14:21 +0000278#if defined(SK_BUILD_FOR_WIN)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 static bool WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281
282private:
283 SkMetaData fMeta;
284 mutable char* fType; // may be characters with low bit set to know that it is not a pointer
285 uint32_t f32;
reed@google.com87fac4a2011-08-04 13:50:17 +0000286
287 // 'there can be only one' (non-zero) between target-id and target-proc
reed@google.comc514dde2011-08-03 19:41:24 +0000288 SkEventSinkID fTargetID;
reed@google.com87fac4a2011-08-04 13:50:17 +0000289 Proc fTargetProc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290
291 // these are for our implementation of the event queue
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 SkMSec fTime;
293 SkEvent* fNextEvent; // either in the delay or normal event queue
reed@google.com87fac4a2011-08-04 13:50:17 +0000294
295 void initialize(const char* type, size_t typeLen, SkEventSinkID);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296
297 static bool Enqueue(SkEvent* evt);
298 static SkMSec EnqueueTime(SkEvent* evt, SkMSec time);
reed@google.com87fac4a2011-08-04 13:50:17 +0000299 static SkEvent* Dequeue();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 static bool QHasEvents();
301};
302
303#endif