18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkEvent_DEFINED
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkEvent_DEFINED
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDOM.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkMetaData.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkString.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Unique 32bit id used to identify an instance of SkEventSink. When events are
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    posted, they are posted to a specific sinkID. When it is time to dispatch the
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    event, the sinkID is used to find the specific SkEventSink object. If it is found,
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    its doEvent() method is called with the event.
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkEventSinkID;
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2287fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com/**
2387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com *  \class SkEvent
2487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com *
2587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com *  When an event is dispatched from the event queue, it is either sent to
2687fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com *  the eventsink matching the target ID (if not 0), or the target proc is
2787fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com *  called (if not NULL).
2887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com */
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkEvent {
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
3187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    /**
3287fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  Function pointer that takes an event, returns true if it "handled" it.
3387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     */
3487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    typedef bool (*Proc)(const SkEvent& evt);
3587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkEvent();
3787fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    explicit SkEvent(const SkString& type, SkEventSinkID = 0);
3887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    explicit SkEvent(const char type[], SkEventSinkID = 0);
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkEvent(const SkEvent& src);
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkEvent();
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Copy the event's type into the specified SkString parameter */
4387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void getType(SkString* str) const;
4487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if the event's type matches exactly the specified type (case sensitive) */
4687fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    bool isType(const SkString& str) const;
4787fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if the event's type matches exactly the specified type (case sensitive) */
4987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    bool isType(const char type[], size_t len = 0) const;
5087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com
5187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    /**
5287fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  Set the event's type to the specified string.
5387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     */
5487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void setType(const SkString&);
5587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com
5687fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    /**
5787fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  Set the event's type to the specified string.
5887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     */
5987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void setType(const char type[], size_t len = 0);
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
61c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com    /**
62c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com     *  Return the target ID, or 0 if there is none.
6387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *
6487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  When an event is dispatched from the event queue, it is either sent to
6587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  the eventsink matching the targetID (if not 0), or the target proc is
6687fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  called (if not NULL).
67c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com     */
68c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com    SkEventSinkID getTargetID() const { return fTargetID; }
69c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com
70c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com    /**
7187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  Set the target ID for this event. 0 means none. Calling this will
7287fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  automatically clear the targetProc to null.
7387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *
7487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  When an event is dispatched from the event queue, it is either sent to
7587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  the eventsink matching the targetID (if not 0), or the target proc is
7687fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  called (if not NULL).
77c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com     */
7887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    SkEvent* setTargetID(SkEventSinkID targetID) {
7987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com        fTargetProc = NULL;
8087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com        fTargetID = targetID;
8187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com        return this;
8287fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    }
83c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com
8487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    /**
8587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  Return the target proc, or NULL if it has none.
8687fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *
8787fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  When an event is dispatched from the event queue, it is either sent to
8887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  the eventsink matching the targetID (if not 0), or the target proc is
8987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  called (if not NULL).
9087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     */
9187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    Proc getTargetProc() const { return fTargetProc; }
9287fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com
9387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    /**
9487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  Set the target ID for this event. NULL means none. Calling this will
9587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  automatically clear the targetID to 0.
9687fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *
9787fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  When an event is dispatched from the event queue, it is either sent to
9887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  the eventsink matching the targetID (if not 0), or the target proc is
9987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  called (if not NULL).
10087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     */
10187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    SkEvent* setTargetProc(Proc proc) {
10287fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com        fTargetID = 0;
10387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com        fTargetProc = proc;
10487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com        return this;
10587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    }
106fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
10787fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    /**
10887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  Return the event's unnamed 32bit field. Default value is 0
10987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     */
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t getFast32() const { return f32; }
11187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com
11287fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    /**
11387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  Set the event's unnamed 32bit field.
11487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     */
11587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void setFast32(uint32_t x) { f32 = x; }
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return true if the event contains the named 32bit field, and return the field
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        in value (if value is non-null). If there is no matching named field, return false
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        and ignore the value parameter.
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
12187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    bool findS32(const char name[], int32_t* value = NULL) const { return fMeta.findS32(name, value); }
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return true if the event contains the named SkScalar field, and return the field
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        in value (if value is non-null). If there is no matching named field, return false
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        and ignore the value parameter.
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
12687fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    bool findScalar(const char name[], SkScalar* value = NULL) const { return fMeta.findScalar(name, value); }
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return true if the event contains the named SkScalar field, and return the fields
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        in value[] (if value is non-null), and return the number of SkScalars in count (if count is non-null).
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If there is no matching named field, return false and ignore the value and count parameters.
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkScalar* findScalars(const char name[], int* count, SkScalar values[] = NULL) const { return fMeta.findScalars(name, count, values); }
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the value of the named string field, or if no matching named field exists, return null.
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* findString(const char name[]) const { return fMeta.findString(name); }
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return true if the event contains the named pointer field, and return the field
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        in value (if value is non-null). If there is no matching named field, return false
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        and ignore the value parameter.
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
13987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    bool findPtr(const char name[], void** value) const { return fMeta.findPtr(name, value); }
14087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    bool findBool(const char name[], bool* value) const { return fMeta.findBool(name, value); }
141f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const void* findData(const char name[], size_t* byteCount = NULL) const {
142f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        return fMeta.findData(name, byteCount);
143f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if ethe event contains the named 32bit field, and if it equals the specified value */
14687fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    bool hasS32(const char name[], int32_t value) const { return fMeta.hasS32(name, value); }
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if ethe event contains the named SkScalar field, and if it equals the specified value */
14887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    bool hasScalar(const char name[], SkScalar value) const { return fMeta.hasScalar(name, value); }
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if ethe event contains the named string field, and if it equals (using strcmp) the specified value */
15087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    bool hasString(const char name[], const char value[]) const { return fMeta.hasString(name, value); }
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if ethe event contains the named pointer field, and if it equals the specified value */
15287fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    bool hasPtr(const char name[], void* value) const { return fMeta.hasPtr(name, value); }
15387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    bool hasBool(const char name[], bool value) const { return fMeta.hasBool(name, value); }
154f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    bool hasData(const char name[], const void* data, size_t byteCount) const {
155f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        return fMeta.hasData(name, data, byteCount);
156f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Add/replace the named 32bit field to the event. In XML use the subelement <data name=... s32=... /> */
15987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void setS32(const char name[], int32_t value) { fMeta.setS32(name, value); }
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Add/replace the named SkScalar field to the event. In XML use the subelement <data name=... scalar=... /> */
16187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void setScalar(const char name[], SkScalar value) { fMeta.setScalar(name, value); }
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Add/replace the named SkScalar[] field to the event. */
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar* setScalars(const char name[], int count, const SkScalar values[] = NULL) { return fMeta.setScalars(name, count, values); }
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */
16587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void setString(const char name[], const SkString& value) { fMeta.setString(name, value.c_str()); }
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */
16787fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void setString(const char name[], const char value[]) { fMeta.setString(name, value); }
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Add/replace the named pointer field to the event. There is no XML equivalent for this call */
16987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void setPtr(const char name[], void* value) { fMeta.setPtr(name, value); }
17087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void setBool(const char name[], bool value) { fMeta.setBool(name, value); }
171f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    void setData(const char name[], const void* data, size_t byteCount) {
172f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fMeta.setData(name, data, byteCount);
173f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the underlying metadata object */
17687fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    SkMetaData& getMetaData() { return fMeta; }
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the underlying metadata object */
17887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    const SkMetaData& getMetaData() const { return fMeta; }
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Call this to initialize the event from the specified XML node */
18187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void inflate(const SkDOM&, const SkDOM::Node*);
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDEBUGCODE(void dump(const char title[] = NULL);)
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    ///////////////////////////////////////////////////////////////////////////
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
187c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com    /**
18887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  Post to the event queue using the event's targetID or target-proc.
18987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *
19087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  The event must be dynamically allocated, as ownership is transferred to
19187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  the event queue. It cannot be allocated on the stack or in a global.
192c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com     */
19387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void post() {
194c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com        return this->postDelay(0);
195c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com    }
196fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
197c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com    /**
19887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  Post to the event queue using the event's targetID or target-proc and
19987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  the specifed millisecond delay.
20087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *
20187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  The event must be dynamically allocated, as ownership is transferred to
20287fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  the event queue. It cannot be allocated on the stack or in a global.
203c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com     */
20487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void postDelay(SkMSec delay);
205fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
206c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com    /**
20787fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  Post to the event queue using the event's targetID or target-proc.
20887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  The event will be delivered no sooner than the specified millisecond
20987fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  time, as measured by SkTime::GetMSecs().
21087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *
21187fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  The event must be dynamically allocated, as ownership is transferred to
21287fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com     *  the event queue. It cannot be allocated on the stack or in a global.
213c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com     */
21487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void postTime(SkMSec time);
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ///////////////////////////////////////////////
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Porting layer must call these functions **/
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ///////////////////////////////////////////////
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Global initialization function for the SkEvent system. Should be called exactly
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        once before any other event method is called, and should be called after the
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        call to SkGraphics::Init().
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
22487fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    static void Init();
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Global cleanup function for the SkEvent system. Should be called exactly once after
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        all event methods have been called, and should be called before calling SkGraphics::Term().
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
22887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    static void Term();
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Call this to process one event from the queue. If it returns true, there are more events
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        to process.
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
23387fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    static bool ProcessEvent();
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Call this whenever the requested timer has expired (requested by a call to SetQueueTimer).
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        It will post any delayed events whose time as "expired" onto the event queue.
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        It may also call SignalQueueTimer() and SignalNonEmptyQueue().
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
23887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    static void ServiceQueueTimer();
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
240f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    /** Return the number of queued events. note that this value may be obsolete
241f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        upon return, since another thread may have called ProcessEvent() or
242f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        Post() after the count was made.
243f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
244f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    static int CountEventsOnQueue();
245f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ////////////////////////////////////////////////////
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Porting layer must implement these functions **/
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ////////////////////////////////////////////////////
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Called whenever an SkEvent is posted to an empty queue, so that the OS
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        can be told to later call Dequeue().
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void SignalNonEmptyQueue();
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Called whenever the delay until the next delayed event changes. If zero is
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        passed, then there are no more queued delay events.
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void SignalQueueTimer(SkMSec delay);
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
259887760f60be146fdefa6b386f80677b540a9f279tfarina@chromium.org#if defined(SK_BUILD_FOR_WIN)
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMetaData      fMeta;
2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mutable char*   fType;  // may be characters with low bit set to know that it is not a pointer
2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t        f32;
26787fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com
26887fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    // 'there can be only one' (non-zero) between target-id and target-proc
269c514dde99e10fb3dbfff5a15537211a7eb094365reed@google.com    SkEventSinkID   fTargetID;
27087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    Proc            fTargetProc;
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // these are for our implementation of the event queue
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMSec          fTime;
2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkEvent*        fNextEvent; // either in the delay or normal event queue
27587fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com
27687fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    void initialize(const char* type, size_t typeLen, SkEventSinkID);
2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool Enqueue(SkEvent* evt);
2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkMSec EnqueueTime(SkEvent* evt, SkMSec time);
28087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    static SkEvent* Dequeue();
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool     QHasEvents();
2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
285