blob: 0efc19a6ea8f302b5936344c7e7386d8263aad0c [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();
Brian Osman4f99e582017-11-22 13:23:35 -050041 explicit SkEvent(const SkString& type);
42 explicit SkEvent(const char type[]);
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 /**
reed@google.com87fac4a2011-08-04 13:50:17 +000066 * Return the event's unnamed 32bit field. Default value is 0
67 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 uint32_t getFast32() const { return f32; }
reed@google.com87fac4a2011-08-04 13:50:17 +000069
70 /**
71 * Set the event's unnamed 32bit field.
72 */
73 void setFast32(uint32_t x) { f32 = x; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
75 /** Return true if the event contains the named 32bit 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 */
Ben Wagnera93a14a2017-08-28 10:34:05 -040079 bool findS32(const char name[], int32_t* value = nullptr) const {
80 return fMeta.findS32(name, value);
81 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 /** Return true if the event contains the named SkScalar field, and return the field
83 in value (if value is non-null). If there is no matching named field, return false
84 and ignore the value parameter.
85 */
Ben Wagnera93a14a2017-08-28 10:34:05 -040086 bool findScalar(const char name[], SkScalar* value = nullptr) const {
87 return fMeta.findScalar(name, value);
88 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 /** 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 */
Ben Wagnera93a14a2017-08-28 10:34:05 -040093 const SkScalar* findScalars(const char name[], int* count, SkScalar values[] = nullptr) const {
94 return fMeta.findScalars(name, count, values);
95 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 /** Return the value of the named string field, or if no matching named field exists, return null.
97 */
98 const char* findString(const char name[]) const { return fMeta.findString(name); }
99 /** Return true if the event contains the named pointer field, and return the field
100 in value (if value is non-null). If there is no matching named field, return false
101 and ignore the value parameter.
102 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000103 bool findPtr(const char name[], void** value) const { return fMeta.findPtr(name, value); }
104 bool findBool(const char name[], bool* value) const { return fMeta.findBool(name, value); }
Ben Wagnera93a14a2017-08-28 10:34:05 -0400105 const void* findData(const char name[], size_t* byteCount = nullptr) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000106 return fMeta.findData(name, byteCount);
107 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108
109 /** 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 +0000110 bool hasS32(const char name[], int32_t value) const { return fMeta.hasS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 /** 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 +0000112 bool hasScalar(const char name[], SkScalar value) const { return fMeta.hasScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 /** 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 +0000114 bool hasString(const char name[], const char value[]) const { return fMeta.hasString(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 /** 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 +0000116 bool hasPtr(const char name[], void* value) const { return fMeta.hasPtr(name, value); }
117 bool hasBool(const char name[], bool value) const { return fMeta.hasBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000118 bool hasData(const char name[], const void* data, size_t byteCount) const {
119 return fMeta.hasData(name, data, byteCount);
120 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121
122 /** 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 +0000123 void setS32(const char name[], int32_t value) { fMeta.setS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 /** 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 +0000125 void setScalar(const char name[], SkScalar value) { fMeta.setScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 /** Add/replace the named SkScalar[] field to the event. */
Ben Wagnera93a14a2017-08-28 10:34:05 -0400127 SkScalar* setScalars(const char name[], int count, const SkScalar values[] = nullptr) {
128 return fMeta.setScalars(name, count, values);
129 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 /** 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 +0000131 void setString(const char name[], const SkString& value) { fMeta.setString(name, value.c_str()); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 /** 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 +0000133 void setString(const char name[], const char value[]) { fMeta.setString(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 /** 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 +0000135 void setPtr(const char name[], void* value) { fMeta.setPtr(name, value); }
136 void setBool(const char name[], bool value) { fMeta.setBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000137 void setData(const char name[], const void* data, size_t byteCount) {
138 fMeta.setData(name, data, byteCount);
139 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140
141 /** Return the underlying metadata object */
reed@google.com87fac4a2011-08-04 13:50:17 +0000142 SkMetaData& getMetaData() { return fMeta; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 /** Return the underlying metadata object */
reed@google.com87fac4a2011-08-04 13:50:17 +0000144 const SkMetaData& getMetaData() const { return fMeta; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145
146 /** Call this to initialize the event from the specified XML node */
Hal Canary20de6152017-02-23 13:24:49 -0500147 void inflate(const SkDOM&, const SkDOMNode*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148
Ben Wagnera93a14a2017-08-28 10:34:05 -0400149 SkDEBUGCODE(void dump(const char title[] = nullptr);)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150
reed@google.com87fac4a2011-08-04 13:50:17 +0000151 ///////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153private:
154 SkMetaData fMeta;
155 mutable char* fType; // may be characters with low bit set to know that it is not a pointer
156 uint32_t f32;
reed@google.com87fac4a2011-08-04 13:50:17 +0000157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 // these are for our implementation of the event queue
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 SkMSec fTime;
160 SkEvent* fNextEvent; // either in the delay or normal event queue
reed@google.com87fac4a2011-08-04 13:50:17 +0000161
Brian Osman4f99e582017-11-22 13:23:35 -0500162 void initialize(const char* type, size_t typeLen);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163};
164
165#endif