Home | History | Annotate | Download | only in views
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SkEvent_DEFINED
     18 #define SkEvent_DEFINED
     19 
     20 #include "SkDOM.h"
     21 #include "SkMetaData.h"
     22 #include "SkString.h"
     23 
     24 /** Unique 32bit id used to identify an instance of SkEventSink. When events are
     25     posted, they are posted to a specific sinkID. When it is time to dispatch the
     26     event, the sinkID is used to find the specific SkEventSink object. If it is found,
     27     its doEvent() method is called with the event.
     28 */
     29 typedef uint32_t SkEventSinkID;
     30 
     31 /** \class SkEvent
     32 
     33     SkEvents are used to communicate type-safe information to SkEventSinks.
     34     SkEventSinks (including SkViews) each have a unique ID, which is stored
     35     in an event. This ID is used to target the event once it has been "posted".
     36 */
     37 class SkEvent {
     38 public:
     39     /** Default construct, creating an empty event.
     40     */
     41     SkEvent();
     42     /** Construct a new event with the specified type.
     43     */
     44     explicit SkEvent(const SkString& type);
     45     /** Construct a new event with the specified type.
     46     */
     47     explicit SkEvent(const char type[]);
     48     /** Construct a new event by copying the fields from the src event.
     49     */
     50     SkEvent(const SkEvent& src);
     51     ~SkEvent();
     52 
     53 //  /** Return the event's type (will never be null) */
     54 //  const char* getType() const;
     55     /** Copy the event's type into the specified SkString parameter */
     56     void    getType(SkString* str) const;
     57     /** Returns true if the event's type matches exactly the specified type (case sensitive) */
     58     bool    isType(const SkString& str) const;
     59     /** Returns true if the event's type matches exactly the specified type (case sensitive) */
     60     bool    isType(const char type[], size_t len = 0) const;
     61     /** Set the event's type to the specified string.
     62         In XML, use the "type" attribute.
     63     */
     64     void    setType(const SkString&);
     65     /** Set the event's type to the specified string.
     66         In XML, use the "type" attribute.
     67     */
     68     void    setType(const char type[], size_t len = 0);
     69 
     70     /** Return the event's unnamed 32bit field. Default value is 0 */
     71     uint32_t getFast32() const { return f32; }
     72     /** Set the event's unnamed 32bit field. In XML, use
     73         the subelement <data fast32=... />
     74     */
     75     void    setFast32(uint32_t x) { f32 = x; }
     76 
     77     /** Return true if the event contains the named 32bit 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     */
     81     bool    findS32(const char name[], int32_t* value = NULL) const { return fMeta.findS32(name, value); }
     82     /** 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     */
     86     bool    findScalar(const char name[], SkScalar* value = NULL) const { return fMeta.findScalar(name, value); }
     87     /** Return true if the event contains the named SkScalar field, and return the fields
     88         in value[] (if value is non-null), and return the number of SkScalars in count (if count is non-null).
     89         If there is no matching named field, return false and ignore the value and count parameters.
     90     */
     91     const SkScalar* findScalars(const char name[], int* count, SkScalar values[] = NULL) const { return fMeta.findScalars(name, count, values); }
     92     /** Return the value of the named string field, or if no matching named field exists, return null.
     93     */
     94     const char* findString(const char name[]) const { return fMeta.findString(name); }
     95     /** Return true if the event contains the named pointer field, and return the field
     96         in value (if value is non-null). If there is no matching named field, return false
     97         and ignore the value parameter.
     98     */
     99     bool    findPtr(const char name[], void** value) const { return fMeta.findPtr(name, value); }
    100     bool    findBool(const char name[], bool* value) const { return fMeta.findBool(name, value); }
    101     const void* findData(const char name[], size_t* byteCount = NULL) const {
    102         return fMeta.findData(name, byteCount);
    103     }
    104 
    105     /** Returns true if ethe event contains the named 32bit field, and if it equals the specified value */
    106     bool    hasS32(const char name[], int32_t value) const { return fMeta.hasS32(name, value); }
    107     /** Returns true if ethe event contains the named SkScalar field, and if it equals the specified value */
    108     bool    hasScalar(const char name[], SkScalar value) const { return fMeta.hasScalar(name, value); }
    109     /** Returns true if ethe event contains the named string field, and if it equals (using strcmp) the specified value */
    110     bool    hasString(const char name[], const char value[]) const { return fMeta.hasString(name, value); }
    111     /** Returns true if ethe event contains the named pointer field, and if it equals the specified value */
    112     bool    hasPtr(const char name[], void* value) const { return fMeta.hasPtr(name, value); }
    113     bool    hasBool(const char name[], bool value) const { return fMeta.hasBool(name, value); }
    114     bool hasData(const char name[], const void* data, size_t byteCount) const {
    115         return fMeta.hasData(name, data, byteCount);
    116     }
    117 
    118     /** Add/replace the named 32bit field to the event. In XML use the subelement <data name=... s32=... /> */
    119     void    setS32(const char name[], int32_t value) { fMeta.setS32(name, value); }
    120     /** Add/replace the named SkScalar field to the event. In XML use the subelement <data name=... scalar=... /> */
    121     void    setScalar(const char name[], SkScalar value) { fMeta.setScalar(name, value); }
    122     /** Add/replace the named SkScalar[] field to the event. */
    123     SkScalar* setScalars(const char name[], int count, const SkScalar values[] = NULL) { return fMeta.setScalars(name, count, values); }
    124     /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */
    125     void    setString(const char name[], const SkString& value) { fMeta.setString(name, value.c_str()); }
    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 char value[]) { fMeta.setString(name, value); }
    128     /** Add/replace the named pointer field to the event. There is no XML equivalent for this call */
    129     void    setPtr(const char name[], void* value) { fMeta.setPtr(name, value); }
    130     void    setBool(const char name[], bool value) { fMeta.setBool(name, value); }
    131     void setData(const char name[], const void* data, size_t byteCount) {
    132         fMeta.setData(name, data, byteCount);
    133     }
    134 
    135     /** Return the underlying metadata object */
    136     SkMetaData&         getMetaData() { return fMeta; }
    137     /** Return the underlying metadata object */
    138     const SkMetaData&   getMetaData() const { return fMeta; }
    139 
    140     void tron() { SkDEBUGCODE(fDebugTrace = true;) }
    141     void troff() { SkDEBUGCODE(fDebugTrace = false;) }
    142     bool isDebugTrace() const
    143     {
    144 #ifdef SK_DEBUG
    145         return fDebugTrace;
    146 #else
    147         return false;
    148 #endif
    149     }
    150 
    151     /** Call this to initialize the event from the specified XML node */
    152     void    inflate(const SkDOM&, const SkDOM::Node*);
    153 
    154     SkDEBUGCODE(void dump(const char title[] = NULL);)
    155 
    156     /** Post the specified event to the event queue, targeting the specified eventsink, with an optional
    157         delay. The event must be dynamically allocated for this. It cannot be a global or on the stack.
    158         After this call, ownership is transfered to the system, so the caller must not retain
    159         the event's ptr. Returns false if the event could not be posted (which means it will have been deleted).
    160     */
    161     static bool Post(SkEvent* evt, SkEventSinkID targetID, SkMSec delay = 0);
    162     /** Post the specified event to the event queue, targeting the specified eventsink, to be delivered on/after the
    163         specified millisecond time. The event must be dynamically allocated for this. It cannot be a global or on the stack.
    164         After this call, ownership is transfered to the system, so the caller must not retain
    165         the event's ptr. Returns false if the event could not be posted (which means it will have been deleted).
    166     */
    167     static bool PostTime(SkEvent* evt, SkEventSinkID targetID, SkMSec time);
    168 
    169     /** Helper method for calling SkEvent::PostTime(this, ...), where the caller specifies a delay.
    170         The real "time" will be computed automatically by sampling the clock and adding its value
    171         to delay.
    172     */
    173     bool post(SkEventSinkID sinkID, SkMSec delay = 0)
    174     {
    175         return SkEvent::Post(this, sinkID, delay);
    176     }
    177 
    178     void postTime(SkEventSinkID sinkID, SkMSec time)
    179     {
    180         SkEvent::PostTime(this, sinkID, time);
    181     }
    182 
    183     ///////////////////////////////////////////////
    184     /** Porting layer must call these functions **/
    185     ///////////////////////////////////////////////
    186 
    187     /** Global initialization function for the SkEvent system. Should be called exactly
    188         once before any other event method is called, and should be called after the
    189         call to SkGraphics::Init().
    190     */
    191     static void     Init();
    192     /** Global cleanup function for the SkEvent system. Should be called exactly once after
    193         all event methods have been called, and should be called before calling SkGraphics::Term().
    194     */
    195     static void     Term();
    196 
    197     /** Call this to process one event from the queue. If it returns true, there are more events
    198         to process.
    199     */
    200     static bool     ProcessEvent();
    201     /** Call this whenever the requested timer has expired (requested by a call to SetQueueTimer).
    202         It will post any delayed events whose time as "expired" onto the event queue.
    203         It may also call SignalQueueTimer() and SignalNonEmptyQueue().
    204     */
    205     static void     ServiceQueueTimer();
    206 
    207     /** Return the number of queued events. note that this value may be obsolete
    208         upon return, since another thread may have called ProcessEvent() or
    209         Post() after the count was made.
    210      */
    211     static int CountEventsOnQueue();
    212 
    213     ////////////////////////////////////////////////////
    214     /** Porting layer must implement these functions **/
    215     ////////////////////////////////////////////////////
    216 
    217     /** Called whenever an SkEvent is posted to an empty queue, so that the OS
    218         can be told to later call Dequeue().
    219     */
    220     static void SignalNonEmptyQueue();
    221     /** Called whenever the delay until the next delayed event changes. If zero is
    222         passed, then there are no more queued delay events.
    223     */
    224     static void SignalQueueTimer(SkMSec delay);
    225 
    226 #ifndef SK_USE_WXWIDGETS
    227 #ifdef SK_BUILD_FOR_WIN
    228     static bool WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    229 #elif defined(SK_BUILD_FOR_UNIXx)
    230   static uint32_t HandleTimer(uint32_t, void*);
    231   static bool WndProc(Display*, Window, XEvent&);
    232 #endif
    233 #else
    234     // Don't know yet what this will be
    235     //static bool CustomEvent();
    236 #endif
    237 
    238 private:
    239     SkMetaData      fMeta;
    240     mutable char*   fType;  // may be characters with low bit set to know that it is not a pointer
    241     uint32_t        f32;
    242     SkDEBUGCODE(bool fDebugTrace;)
    243 
    244     // these are for our implementation of the event queue
    245     SkEventSinkID   fTargetID;
    246     SkMSec          fTime;
    247     SkEvent*        fNextEvent; // either in the delay or normal event queue
    248     void initialize(const char* type, size_t typeLen);
    249 
    250     static bool Enqueue(SkEvent* evt);
    251     static SkMSec EnqueueTime(SkEvent* evt, SkMSec time);
    252     static SkEvent* Dequeue(SkEventSinkID* targetID);
    253     static bool     QHasEvents();
    254 };
    255 
    256 #endif
    257 
    258