blob: aad3f58f3abe1539fc3ef8002f97f2b00830a66d [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000014/** Unique 32bit id used to identify an instance of SkEventSink. When events are
15 posted, they are posted to a specific sinkID. When it is time to dispatch the
16 event, the sinkID is used to find the specific SkEventSink object. If it is found,
17 its doEvent() method is called with the event.
18*/
19typedef uint32_t SkEventSinkID;
20
reed@google.com87fac4a2011-08-04 13:50:17 +000021/**
22 * \class SkEvent
23 *
24 * When an event is dispatched from the event queue, it is either sent to
25 * the eventsink matching the target ID (if not 0), or the target proc is
26 * called (if not NULL).
27 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000028class SkEvent {
29public:
reed@google.com87fac4a2011-08-04 13:50:17 +000030 /**
31 * Function pointer that takes an event, returns true if it "handled" it.
32 */
33 typedef bool (*Proc)(const SkEvent& evt);
34
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 SkEvent();
Brian Osman4f99e582017-11-22 13:23:35 -050036 explicit SkEvent(const SkString& type);
37 explicit SkEvent(const char type[]);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 SkEvent(const SkEvent& src);
39 ~SkEvent();
40
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 /** Copy the event's type into the specified SkString parameter */
reed@google.com87fac4a2011-08-04 13:50:17 +000042 void getType(SkString* str) const;
43
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 /** Returns true if the event's type matches exactly the specified type (case sensitive) */
reed@google.com87fac4a2011-08-04 13:50:17 +000045 bool isType(const SkString& str) const;
46
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 /** Returns true if the event's type matches exactly the specified type (case sensitive) */
reed@google.com87fac4a2011-08-04 13:50:17 +000048 bool isType(const char type[], size_t len = 0) const;
49
50 /**
51 * Set the event's type to the specified string.
52 */
53 void setType(const SkString&);
54
55 /**
56 * Set the event's type to the specified string.
57 */
58 void setType(const char type[], size_t len = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000059
reed@google.comc514dde2011-08-03 19:41:24 +000060 /**
reed@google.com87fac4a2011-08-04 13:50:17 +000061 * Return the event's unnamed 32bit field. Default value is 0
62 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 uint32_t getFast32() const { return f32; }
reed@google.com87fac4a2011-08-04 13:50:17 +000064
65 /**
66 * Set the event's unnamed 32bit field.
67 */
68 void setFast32(uint32_t x) { f32 = x; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000069
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 */
Ben Wagnera93a14a2017-08-28 10:34:05 -040074 bool findS32(const char name[], int32_t* value = nullptr) const {
75 return fMeta.findS32(name, value);
76 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 /** Return true if the event contains the named SkScalar field, and return the field
78 in value (if value is non-null). If there is no matching named field, return false
79 and ignore the value parameter.
80 */
Ben Wagnera93a14a2017-08-28 10:34:05 -040081 bool findScalar(const char name[], SkScalar* value = nullptr) const {
82 return fMeta.findScalar(name, value);
83 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 /** Return true if the event contains the named SkScalar field, and return the fields
85 in value[] (if value is non-null), and return the number of SkScalars in count (if count is non-null).
86 If there is no matching named field, return false and ignore the value and count parameters.
87 */
Ben Wagnera93a14a2017-08-28 10:34:05 -040088 const SkScalar* findScalars(const char name[], int* count, SkScalar values[] = nullptr) const {
89 return fMeta.findScalars(name, count, values);
90 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 /** Return the value of the named string field, or if no matching named field exists, return null.
92 */
93 const char* findString(const char name[]) const { return fMeta.findString(name); }
94 /** Return true if the event contains the named pointer field, and return the field
95 in value (if value is non-null). If there is no matching named field, return false
96 and ignore the value parameter.
97 */
reed@google.com87fac4a2011-08-04 13:50:17 +000098 bool findPtr(const char name[], void** value) const { return fMeta.findPtr(name, value); }
99 bool findBool(const char name[], bool* value) const { return fMeta.findBool(name, value); }
Ben Wagnera93a14a2017-08-28 10:34:05 -0400100 const void* findData(const char name[], size_t* byteCount = nullptr) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000101 return fMeta.findData(name, byteCount);
102 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103
104 /** 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 +0000105 bool hasS32(const char name[], int32_t value) const { return fMeta.hasS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 /** 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 +0000107 bool hasScalar(const char name[], SkScalar value) const { return fMeta.hasScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 /** 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 +0000109 bool hasString(const char name[], const char value[]) const { return fMeta.hasString(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 /** 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 +0000111 bool hasPtr(const char name[], void* value) const { return fMeta.hasPtr(name, value); }
112 bool hasBool(const char name[], bool value) const { return fMeta.hasBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000113 bool hasData(const char name[], const void* data, size_t byteCount) const {
114 return fMeta.hasData(name, data, byteCount);
115 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116
117 /** 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 +0000118 void setS32(const char name[], int32_t value) { fMeta.setS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 /** 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 +0000120 void setScalar(const char name[], SkScalar value) { fMeta.setScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 /** Add/replace the named SkScalar[] field to the event. */
Ben Wagnera93a14a2017-08-28 10:34:05 -0400122 SkScalar* setScalars(const char name[], int count, const SkScalar values[] = nullptr) {
123 return fMeta.setScalars(name, count, values);
124 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 /** 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 +0000126 void setString(const char name[], const SkString& value) { fMeta.setString(name, value.c_str()); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 /** 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 +0000128 void setString(const char name[], const char value[]) { fMeta.setString(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 /** 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 +0000130 void setPtr(const char name[], void* value) { fMeta.setPtr(name, value); }
131 void setBool(const char name[], bool value) { fMeta.setBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000132 void setData(const char name[], const void* data, size_t byteCount) {
133 fMeta.setData(name, data, byteCount);
134 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135
136 /** Return the underlying metadata object */
reed@google.com87fac4a2011-08-04 13:50:17 +0000137 SkMetaData& getMetaData() { return fMeta; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 /** Return the underlying metadata object */
reed@google.com87fac4a2011-08-04 13:50:17 +0000139 const SkMetaData& getMetaData() const { return fMeta; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140
reed@google.com87fac4a2011-08-04 13:50:17 +0000141 ///////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143private:
144 SkMetaData fMeta;
145 mutable char* fType; // may be characters with low bit set to know that it is not a pointer
146 uint32_t f32;
reed@google.com87fac4a2011-08-04 13:50:17 +0000147
Brian Osman4f99e582017-11-22 13:23:35 -0500148 void initialize(const char* type, size_t typeLen);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149};
150
151#endif