epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkEvent_DEFINED |
| 11 | #define SkEvent_DEFINED |
| 12 | |
| 13 | #include "SkDOM.h" |
| 14 | #include "SkMetaData.h" |
| 15 | #include "SkString.h" |
| 16 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | /** 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 | */ |
| 22 | typedef 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 | */ |
| 30 | class SkEvent { |
| 31 | public: |
| 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | /** Copy the event's type into the specified SkString parameter */ |
| 47 | void getType(SkString* str) const; |
| 48 | /** Returns true if the event's type matches exactly the specified type (case sensitive) */ |
| 49 | bool isType(const SkString& str) const; |
| 50 | /** Returns true if the event's type matches exactly the specified type (case sensitive) */ |
| 51 | bool isType(const char type[], size_t len = 0) const; |
| 52 | /** Set the event's type to the specified string. |
| 53 | In XML, use the "type" attribute. |
| 54 | */ |
| 55 | void setType(const SkString&); |
| 56 | /** Set the event's type to the specified string. |
| 57 | In XML, use the "type" attribute. |
| 58 | */ |
| 59 | void setType(const char type[], size_t len = 0); |
| 60 | |
reed@google.com | c514dde | 2011-08-03 19:41:24 +0000 | [diff] [blame^] | 61 | /** |
| 62 | * Return the target ID, or 0 if there is none. |
| 63 | */ |
| 64 | SkEventSinkID getTargetID() const { return fTargetID; } |
| 65 | |
| 66 | /** |
| 67 | * Set the target ID for this event. 0 means none. Can be specified when |
| 68 | * the event is posted or sent. |
| 69 | */ |
| 70 | void setTargetID(SkEventSinkID targetID) { fTargetID = targetID; } |
| 71 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | /** Return the event's unnamed 32bit field. Default value is 0 */ |
| 73 | uint32_t getFast32() const { return f32; } |
| 74 | /** Set the event's unnamed 32bit field. In XML, use |
| 75 | the subelement <data fast32=... /> |
| 76 | */ |
| 77 | void setFast32(uint32_t x) { f32 = x; } |
| 78 | |
| 79 | /** Return true if the event contains the named 32bit field, and return the field |
| 80 | in value (if value is non-null). If there is no matching named field, return false |
| 81 | and ignore the value parameter. |
| 82 | */ |
| 83 | bool findS32(const char name[], int32_t* value = NULL) const { return fMeta.findS32(name, value); } |
| 84 | /** Return true if the event contains the named SkScalar field, and return the field |
| 85 | in value (if value is non-null). If there is no matching named field, return false |
| 86 | and ignore the value parameter. |
| 87 | */ |
| 88 | bool findScalar(const char name[], SkScalar* value = NULL) const { return fMeta.findScalar(name, value); } |
| 89 | /** Return true if the event contains the named SkScalar field, and return the fields |
| 90 | in value[] (if value is non-null), and return the number of SkScalars in count (if count is non-null). |
| 91 | If there is no matching named field, return false and ignore the value and count parameters. |
| 92 | */ |
| 93 | const SkScalar* findScalars(const char name[], int* count, SkScalar values[] = NULL) const { return fMeta.findScalars(name, count, values); } |
| 94 | /** Return the value of the named string field, or if no matching named field exists, return null. |
| 95 | */ |
| 96 | const char* findString(const char name[]) const { return fMeta.findString(name); } |
| 97 | /** Return true if the event contains the named pointer field, and return the field |
| 98 | in value (if value is non-null). If there is no matching named field, return false |
| 99 | and ignore the value parameter. |
| 100 | */ |
| 101 | bool findPtr(const char name[], void** value) const { return fMeta.findPtr(name, value); } |
| 102 | bool findBool(const char name[], bool* value) const { return fMeta.findBool(name, value); } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 103 | const void* findData(const char name[], size_t* byteCount = NULL) const { |
| 104 | return fMeta.findData(name, byteCount); |
| 105 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | |
| 107 | /** Returns true if ethe event contains the named 32bit field, and if it equals the specified value */ |
| 108 | bool hasS32(const char name[], int32_t value) const { return fMeta.hasS32(name, value); } |
| 109 | /** Returns true if ethe event contains the named SkScalar field, and if it equals the specified value */ |
| 110 | bool hasScalar(const char name[], SkScalar value) const { return fMeta.hasScalar(name, value); } |
| 111 | /** Returns true if ethe event contains the named string field, and if it equals (using strcmp) the specified value */ |
| 112 | bool hasString(const char name[], const char value[]) const { return fMeta.hasString(name, value); } |
| 113 | /** Returns true if ethe event contains the named pointer field, and if it equals the specified value */ |
| 114 | bool hasPtr(const char name[], void* value) const { return fMeta.hasPtr(name, value); } |
| 115 | bool hasBool(const char name[], bool value) const { return fMeta.hasBool(name, value); } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 116 | bool hasData(const char name[], const void* data, size_t byteCount) const { |
| 117 | return fMeta.hasData(name, data, byteCount); |
| 118 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | |
| 120 | /** Add/replace the named 32bit field to the event. In XML use the subelement <data name=... s32=... /> */ |
| 121 | void setS32(const char name[], int32_t value) { fMeta.setS32(name, value); } |
| 122 | /** Add/replace the named SkScalar field to the event. In XML use the subelement <data name=... scalar=... /> */ |
| 123 | void setScalar(const char name[], SkScalar value) { fMeta.setScalar(name, value); } |
| 124 | /** Add/replace the named SkScalar[] field to the event. */ |
| 125 | SkScalar* setScalars(const char name[], int count, const SkScalar values[] = NULL) { return fMeta.setScalars(name, count, values); } |
| 126 | /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */ |
| 127 | void setString(const char name[], const SkString& value) { fMeta.setString(name, value.c_str()); } |
| 128 | /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */ |
| 129 | void setString(const char name[], const char value[]) { fMeta.setString(name, value); } |
| 130 | /** Add/replace the named pointer field to the event. There is no XML equivalent for this call */ |
| 131 | void setPtr(const char name[], void* value) { fMeta.setPtr(name, value); } |
| 132 | void setBool(const char name[], bool value) { fMeta.setBool(name, value); } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 133 | void setData(const char name[], const void* data, size_t byteCount) { |
| 134 | fMeta.setData(name, data, byteCount); |
| 135 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | |
| 137 | /** Return the underlying metadata object */ |
| 138 | SkMetaData& getMetaData() { return fMeta; } |
| 139 | /** Return the underlying metadata object */ |
| 140 | const SkMetaData& getMetaData() const { return fMeta; } |
| 141 | |
| 142 | void tron() { SkDEBUGCODE(fDebugTrace = true;) } |
| 143 | void troff() { SkDEBUGCODE(fDebugTrace = false;) } |
| 144 | bool isDebugTrace() const |
| 145 | { |
| 146 | #ifdef SK_DEBUG |
| 147 | return fDebugTrace; |
| 148 | #else |
| 149 | return false; |
| 150 | #endif |
| 151 | } |
| 152 | |
| 153 | /** Call this to initialize the event from the specified XML node */ |
| 154 | void inflate(const SkDOM&, const SkDOM::Node*); |
| 155 | |
| 156 | SkDEBUGCODE(void dump(const char title[] = NULL);) |
| 157 | |
| 158 | /** Post the specified event to the event queue, targeting the specified eventsink, with an optional |
| 159 | delay. The event must be dynamically allocated for this. It cannot be a global or on the stack. |
| 160 | After this call, ownership is transfered to the system, so the caller must not retain |
| 161 | the event's ptr. Returns false if the event could not be posted (which means it will have been deleted). |
| 162 | */ |
| 163 | static bool Post(SkEvent* evt, SkEventSinkID targetID, SkMSec delay = 0); |
| 164 | /** Post the specified event to the event queue, targeting the specified eventsink, to be delivered on/after the |
| 165 | specified millisecond time. The event must be dynamically allocated for this. It cannot be a global or on the stack. |
| 166 | After this call, ownership is transfered to the system, so the caller must not retain |
| 167 | the event's ptr. Returns false if the event could not be posted (which means it will have been deleted). |
| 168 | */ |
| 169 | static bool PostTime(SkEvent* evt, SkEventSinkID targetID, SkMSec time); |
| 170 | |
reed@google.com | c514dde | 2011-08-03 19:41:24 +0000 | [diff] [blame^] | 171 | /** |
| 172 | * Post to the event queue using the event's targetID. If this is 0, then |
| 173 | * false is returned and the event is deleted, otherwise true is returned |
| 174 | * and ownership of the event passes to the event queue. |
| 175 | */ |
| 176 | bool post() { |
| 177 | return this->postDelay(0); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Post to the event queue using the event's targetID and the specifed |
| 182 | * millisecond delay. If the event's targetID is 0, then false is returned |
| 183 | * and the event is deleted, otherwise true is returned and ownership of |
| 184 | * the event passes to the event queue. |
| 185 | */ |
| 186 | bool postDelay(SkMSec delay); |
| 187 | |
| 188 | /** |
| 189 | * Post to the event queue using the event's targetID and the specifed |
| 190 | * millisecond time. If the event's targetID is 0, then false is returned |
| 191 | * and the event is deleted, otherwise true is returned and ownership of |
| 192 | * the event passes to the event queue. |
| 193 | */ |
| 194 | bool postTime(SkMSec time); |
| 195 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 196 | /** Helper method for calling SkEvent::PostTime(this, ...), where the caller specifies a delay. |
| 197 | The real "time" will be computed automatically by sampling the clock and adding its value |
| 198 | to delay. |
| 199 | */ |
reed@google.com | c514dde | 2011-08-03 19:41:24 +0000 | [diff] [blame^] | 200 | bool post(SkEventSinkID sinkID, SkMSec delay = 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | return SkEvent::Post(this, sinkID, delay); |
| 202 | } |
| 203 | |
reed@google.com | c514dde | 2011-08-03 19:41:24 +0000 | [diff] [blame^] | 204 | void postTime(SkEventSinkID sinkID, SkMSec time) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | SkEvent::PostTime(this, sinkID, time); |
| 206 | } |
| 207 | |
| 208 | /////////////////////////////////////////////// |
| 209 | /** Porting layer must call these functions **/ |
| 210 | /////////////////////////////////////////////// |
| 211 | |
| 212 | /** Global initialization function for the SkEvent system. Should be called exactly |
| 213 | once before any other event method is called, and should be called after the |
| 214 | call to SkGraphics::Init(). |
| 215 | */ |
| 216 | static void Init(); |
| 217 | /** Global cleanup function for the SkEvent system. Should be called exactly once after |
| 218 | all event methods have been called, and should be called before calling SkGraphics::Term(). |
| 219 | */ |
| 220 | static void Term(); |
| 221 | |
| 222 | /** Call this to process one event from the queue. If it returns true, there are more events |
| 223 | to process. |
| 224 | */ |
| 225 | static bool ProcessEvent(); |
| 226 | /** Call this whenever the requested timer has expired (requested by a call to SetQueueTimer). |
| 227 | It will post any delayed events whose time as "expired" onto the event queue. |
| 228 | It may also call SignalQueueTimer() and SignalNonEmptyQueue(). |
| 229 | */ |
| 230 | static void ServiceQueueTimer(); |
| 231 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 232 | /** Return the number of queued events. note that this value may be obsolete |
| 233 | upon return, since another thread may have called ProcessEvent() or |
| 234 | Post() after the count was made. |
| 235 | */ |
| 236 | static int CountEventsOnQueue(); |
| 237 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | //////////////////////////////////////////////////// |
| 239 | /** Porting layer must implement these functions **/ |
| 240 | //////////////////////////////////////////////////// |
| 241 | |
| 242 | /** Called whenever an SkEvent is posted to an empty queue, so that the OS |
| 243 | can be told to later call Dequeue(). |
| 244 | */ |
| 245 | static void SignalNonEmptyQueue(); |
| 246 | /** Called whenever the delay until the next delayed event changes. If zero is |
| 247 | passed, then there are no more queued delay events. |
| 248 | */ |
| 249 | static void SignalQueueTimer(SkMSec delay); |
| 250 | |
| 251 | #ifndef SK_USE_WXWIDGETS |
| 252 | #ifdef SK_BUILD_FOR_WIN |
| 253 | static bool WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); |
| 254 | #elif defined(SK_BUILD_FOR_UNIXx) |
| 255 | static uint32_t HandleTimer(uint32_t, void*); |
| 256 | static bool WndProc(Display*, Window, XEvent&); |
| 257 | #endif |
| 258 | #else |
| 259 | // Don't know yet what this will be |
| 260 | //static bool CustomEvent(); |
| 261 | #endif |
| 262 | |
| 263 | private: |
| 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.com | c514dde | 2011-08-03 19:41:24 +0000 | [diff] [blame^] | 267 | SkEventSinkID fTargetID; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | SkDEBUGCODE(bool fDebugTrace;) |
| 269 | |
| 270 | // these are for our implementation of the event queue |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | SkMSec fTime; |
| 272 | SkEvent* fNextEvent; // either in the delay or normal event queue |
| 273 | void initialize(const char* type, size_t typeLen); |
| 274 | |
| 275 | static bool Enqueue(SkEvent* evt); |
| 276 | static SkMSec EnqueueTime(SkEvent* evt, SkMSec time); |
| 277 | static SkEvent* Dequeue(SkEventSinkID* targetID); |
| 278 | static bool QHasEvents(); |
| 279 | }; |
| 280 | |
| 281 | #endif |
| 282 | |