Home | History | Annotate | Download | only in plugins
      1 /*
      2  * Copyright 2009, The Android Open Source Project
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 /*  Defines the android-specific types and functions as part of npapi
     27 
     28     In particular, defines the window and event types that are passed to
     29     NPN_GetValue, NPP_SetWindow and NPP_HandleEvent.
     30 
     31     To minimize what native libraries the plugin links against, some
     32     functionality is provided via function-ptrs (e.g. time, sound)
     33  */
     34 
     35 #ifndef android_npapi_h
     36 #define android_npapi_h
     37 
     38 #include <stdint.h>
     39 
     40 #include "npapi.h"
     41 
     42 ///////////////////////////////////////////////////////////////////////////////
     43 // General types
     44 
     45 enum ANPBitmapFormats {
     46     kUnknown_ANPBitmapFormat    = 0,
     47     kRGBA_8888_ANPBitmapFormat  = 1,
     48     kRGB_565_ANPBitmapFormat    = 2
     49 };
     50 typedef int32_t ANPBitmapFormat;
     51 
     52 struct ANPPixelPacking {
     53     uint8_t AShift;
     54     uint8_t ABits;
     55     uint8_t RShift;
     56     uint8_t RBits;
     57     uint8_t GShift;
     58     uint8_t GBits;
     59     uint8_t BShift;
     60     uint8_t BBits;
     61 };
     62 
     63 struct ANPBitmap {
     64     void*           baseAddr;
     65     ANPBitmapFormat format;
     66     int32_t         width;
     67     int32_t         height;
     68     int32_t         rowBytes;
     69 };
     70 
     71 struct ANPRectF {
     72     float   left;
     73     float   top;
     74     float   right;
     75     float   bottom;
     76 };
     77 
     78 struct ANPRectI {
     79     int32_t left;
     80     int32_t top;
     81     int32_t right;
     82     int32_t bottom;
     83 };
     84 
     85 struct ANPCanvas;
     86 struct ANPMatrix;
     87 struct ANPPaint;
     88 struct ANPPath;
     89 struct ANPRegion;
     90 struct ANPTypeface;
     91 
     92 enum ANPMatrixFlags {
     93     kIdentity_ANPMatrixFlag     = 0,
     94     kTranslate_ANPMatrixFlag    = 0x01,
     95     kScale_ANPMatrixFlag        = 0x02,
     96     kAffine_ANPMatrixFlag       = 0x04,
     97     kPerspective_ANPMatrixFlag  = 0x08,
     98 };
     99 typedef uint32_t ANPMatrixFlag;
    100 
    101 ///////////////////////////////////////////////////////////////////////////////
    102 // NPN_GetValue
    103 
    104 /** queries for a specific ANPInterface.
    105 
    106     Maybe called with NULL for the NPP instance
    107 
    108     NPN_GetValue(inst, interface_enum, ANPInterface*)
    109  */
    110 #define kLogInterfaceV0_ANPGetValue         ((NPNVariable)1000)
    111 #define kAudioTrackInterfaceV0_ANPGetValue  ((NPNVariable)1001)
    112 #define kCanvasInterfaceV0_ANPGetValue      ((NPNVariable)1002)
    113 #define kMatrixInterfaceV0_ANPGetValue      ((NPNVariable)1003)
    114 #define kPaintInterfaceV0_ANPGetValue       ((NPNVariable)1004)
    115 #define kPathInterfaceV0_ANPGetValue        ((NPNVariable)1005)
    116 #define kTypefaceInterfaceV0_ANPGetValue    ((NPNVariable)1006)
    117 #define kWindowInterfaceV0_ANPGetValue      ((NPNVariable)1007)
    118 #define kBitmapInterfaceV0_ANPGetValue      ((NPNVariable)1008)
    119 #define kSurfaceInterfaceV0_ANPGetValue     ((NPNVariable)1009)
    120 #define kSystemInterfaceV0_ANPGetValue      ((NPNVariable)1010)
    121 #define kEventInterfaceV0_ANPGetValue       ((NPNVariable)1011)
    122 
    123 #define kAudioTrackInterfaceV1_ANPGetValue  ((NPNVariable)1012)
    124 #define kOpenGLInterfaceV0_ANPGetValue      ((NPNVariable)1013)
    125 #define kWindowInterfaceV1_ANPGetValue      ((NPNVariable)1014)
    126 #define kVideoInterfaceV0_ANPGetValue       ((NPNVariable)1015)
    127 #define kSystemInterfaceV1_ANPGetValue      ((NPNVariable)1016)
    128 
    129 #define kSystemInterfaceV2_ANPGetValue      ((NPNVariable)1017)
    130 #define kWindowInterfaceV2_ANPGetValue      ((NPNVariable)1018)
    131 
    132 #define kNativeWindowInterfaceV0_ANPGetValue ((NPNVariable)1019)
    133 #define kVideoInterfaceV1_ANPGetValue       ((NPNVariable)1020)
    134 
    135 /** queries for the drawing models supported on this device.
    136 
    137     NPN_GetValue(inst, kSupportedDrawingModel_ANPGetValue, uint32_t* bits)
    138  */
    139 #define kSupportedDrawingModel_ANPGetValue  ((NPNVariable)2000)
    140 
    141 /** queries for the context (android.content.Context) of the plugin. If no
    142     instance is specified the application's context is returned. If the instance
    143     is given then the context returned is identical to the context used to
    144     create the webview in which that instance resides.
    145 
    146     NOTE: Holding onto a non-application context after your instance has been
    147     destroyed will cause a memory leak.  Refer to the android documentation to
    148     determine what context is best suited for your particular scenario.
    149 
    150     NPN_GetValue(inst, kJavaContext_ANPGetValue, jobject context)
    151  */
    152 #define kJavaContext_ANPGetValue            ((NPNVariable)2001)
    153 
    154 ///////////////////////////////////////////////////////////////////////////////
    155 // NPN_SetValue
    156 
    157 /** Request to set the drawing model. SetValue will return false if the drawing
    158     model is not supported or has insufficient information for configuration.
    159 
    160     NPN_SetValue(inst, kRequestDrawingModel_ANPSetValue, (void*)foo_ANPDrawingModel)
    161  */
    162 #define kRequestDrawingModel_ANPSetValue    ((NPPVariable)1000)
    163 
    164 /** These are used as bitfields in ANPSupportedDrawingModels_EnumValue,
    165     and as-is in ANPRequestDrawingModel_EnumValue. The drawing model determines
    166     how to interpret the ANPDrawingContext provided in the Draw event and how
    167     to interpret the NPWindow->window field.
    168  */
    169 enum ANPDrawingModels {
    170     /** Draw into a bitmap from the browser thread in response to a Draw event.
    171         NPWindow->window is reserved (ignore)
    172      */
    173     kBitmap_ANPDrawingModel  = 1 << 0,
    174     /** Draw into a surface (e.g. raster, openGL, etc.) using the Java surface
    175         interface. When this model is used the browser will invoke the Java
    176         class specified in the plugin's apk manifest. From that class the browser
    177         will invoke the appropriate method to return an an instance of a android
    178         Java View. The instance is then embedded in the html. The plugin can then
    179         manipulate the view as it would any normal Java View in android.
    180 
    181         Unlike the bitmap model, a surface model is opaque so no html content
    182         behind the plugin will be  visible. Unless the plugin needs to be
    183         transparent the surface model should be chosen over the bitmap model as
    184         it will have better performance.
    185 
    186         Further, a plugin can manipulate some surfaces in native code using the
    187         ANPSurfaceInterface.  This interface can be used to manipulate Java
    188         objects that extend Surface.class by allowing them to access the
    189         surface's underlying bitmap in native code.  For instance, if a raster
    190         surface is used the plugin can lock, draw directly into the bitmap, and
    191         unlock the surface in native code without making JNI calls to the Java
    192         surface object.
    193      */
    194     kSurface_ANPDrawingModel = 1 << 1,
    195     kOpenGL_ANPDrawingModel  = 1 << 2,
    196 };
    197 typedef int32_t ANPDrawingModel;
    198 
    199 /** Request to receive/disable events. If the pointer is NULL then all flags will
    200     be disabled. Otherwise, the event type will be enabled iff its corresponding
    201     bit in the EventFlags bit field is set.
    202 
    203     NPN_SetValue(inst, ANPAcceptEvents, (void*)EventFlags)
    204  */
    205 #define kAcceptEvents_ANPSetValue           ((NPPVariable)1001)
    206 
    207 /** The EventFlags are a set of bits used to determine which types of events the
    208     plugin wishes to receive. For example, if the value is 0x03 then both key
    209     and touch events will be provided to the plugin.
    210  */
    211 enum ANPEventFlag {
    212     kKey_ANPEventFlag               = 0x01,
    213     kTouch_ANPEventFlag             = 0x02,
    214 };
    215 typedef uint32_t ANPEventFlags;
    216 
    217 ///////////////////////////////////////////////////////////////////////////////
    218 // NPP_GetValue
    219 
    220 /** Requests that the plugin return a java surface to be displayed. This will
    221     only be used if the plugin has choosen the kSurface_ANPDrawingModel.
    222 
    223     NPP_GetValue(inst, kJavaSurface_ANPGetValue, jobject surface)
    224  */
    225 #define kJavaSurface_ANPGetValue            ((NPPVariable)2000)
    226 
    227 
    228 ///////////////////////////////////////////////////////////////////////////////
    229 // ANDROID INTERFACE DEFINITIONS
    230 
    231 /** Interfaces provide additional functionality to the plugin via function ptrs.
    232     Once an interface is retrieved, it is valid for the lifetime of the plugin
    233     (just like browserfuncs).
    234 
    235     All ANPInterfaces begin with an inSize field, which must be set by the
    236     caller (plugin) with the number of bytes allocated for the interface.
    237     e.g. SomeInterface si; si.inSize = sizeof(si); browser->getvalue(..., &si);
    238  */
    239 struct ANPInterface {
    240     uint32_t    inSize;     // size (in bytes) of this struct
    241 };
    242 
    243 enum ANPLogTypes {
    244     kError_ANPLogType   = 0,    // error
    245     kWarning_ANPLogType = 1,    // warning
    246     kDebug_ANPLogType   = 2     // debug only (informational)
    247 };
    248 typedef int32_t ANPLogType;
    249 
    250 struct ANPLogInterfaceV0 : ANPInterface {
    251     /** dumps printf messages to the log file
    252         e.g. interface->log(instance, kWarning_ANPLogType, "value is %d", value);
    253      */
    254     void (*log)(ANPLogType, const char format[], ...);
    255 };
    256 
    257 struct ANPBitmapInterfaceV0 : ANPInterface {
    258     /** Returns true if the specified bitmap format is supported, and if packing
    259         is non-null, sets it to the packing info for that format.
    260      */
    261     bool (*getPixelPacking)(ANPBitmapFormat, ANPPixelPacking* packing);
    262 };
    263 
    264 struct ANPMatrixInterfaceV0 : ANPInterface {
    265     /** Return a new identity matrix
    266      */
    267     ANPMatrix*  (*newMatrix)();
    268     /** Delete a matrix previously allocated by newMatrix()
    269      */
    270     void        (*deleteMatrix)(ANPMatrix*);
    271 
    272     ANPMatrixFlag (*getFlags)(const ANPMatrix*);
    273 
    274     void        (*copy)(ANPMatrix* dst, const ANPMatrix* src);
    275 
    276     /** Return the matrix values in a float array (allcoated by the caller),
    277         where the values are treated as follows:
    278         w  = x * [6] + y * [7] + [8];
    279         x' = (x * [0] + y * [1] + [2]) / w;
    280         y' = (x * [3] + y * [4] + [5]) / w;
    281      */
    282     void        (*get3x3)(const ANPMatrix*, float[9]);
    283     /** Initialize the matrix from values in a float array,
    284         where the values are treated as follows:
    285          w  = x * [6] + y * [7] + [8];
    286          x' = (x * [0] + y * [1] + [2]) / w;
    287          y' = (x * [3] + y * [4] + [5]) / w;
    288      */
    289     void        (*set3x3)(ANPMatrix*, const float[9]);
    290 
    291     void        (*setIdentity)(ANPMatrix*);
    292     void        (*preTranslate)(ANPMatrix*, float tx, float ty);
    293     void        (*postTranslate)(ANPMatrix*, float tx, float ty);
    294     void        (*preScale)(ANPMatrix*, float sx, float sy);
    295     void        (*postScale)(ANPMatrix*, float sx, float sy);
    296     void        (*preSkew)(ANPMatrix*, float kx, float ky);
    297     void        (*postSkew)(ANPMatrix*, float kx, float ky);
    298     void        (*preRotate)(ANPMatrix*, float degrees);
    299     void        (*postRotate)(ANPMatrix*, float degrees);
    300     void        (*preConcat)(ANPMatrix*, const ANPMatrix*);
    301     void        (*postConcat)(ANPMatrix*, const ANPMatrix*);
    302 
    303     /** Return true if src is invertible, and if so, return its inverse in dst.
    304         If src is not invertible, return false and ignore dst.
    305      */
    306     bool        (*invert)(ANPMatrix* dst, const ANPMatrix* src);
    307 
    308     /** Transform the x,y pairs in src[] by this matrix, and store the results
    309         in dst[]. The count parameter is treated as the number of pairs in the
    310         array. It is legal for src and dst to point to the same memory, but
    311         illegal for the two arrays to partially overlap.
    312      */
    313     void        (*mapPoints)(ANPMatrix*, float dst[], const float src[],
    314                              int32_t count);
    315 };
    316 
    317 struct ANPPathInterfaceV0 : ANPInterface {
    318     /** Return a new path */
    319     ANPPath* (*newPath)();
    320 
    321     /** Delete a path previously allocated by ANPPath() */
    322     void (*deletePath)(ANPPath*);
    323 
    324     /** Make a deep copy of the src path, into the dst path (already allocated
    325         by the caller).
    326      */
    327     void (*copy)(ANPPath* dst, const ANPPath* src);
    328 
    329     /** Returns true if the two paths are the same (i.e. have the same points)
    330      */
    331     bool (*equal)(const ANPPath* path0, const ANPPath* path1);
    332 
    333     /** Remove any previous points, initializing the path back to empty. */
    334     void (*reset)(ANPPath*);
    335 
    336     /** Return true if the path is empty (has no lines, quads or cubics). */
    337     bool (*isEmpty)(const ANPPath*);
    338 
    339     /** Return the path's bounds in bounds. */
    340     void (*getBounds)(const ANPPath*, ANPRectF* bounds);
    341 
    342     void (*moveTo)(ANPPath*, float x, float y);
    343     void (*lineTo)(ANPPath*, float x, float y);
    344     void (*quadTo)(ANPPath*, float x0, float y0, float x1, float y1);
    345     void (*cubicTo)(ANPPath*, float x0, float y0, float x1, float y1,
    346                     float x2, float y2);
    347     void (*close)(ANPPath*);
    348 
    349     /** Offset the src path by [dx, dy]. If dst is null, apply the
    350         change directly to the src path. If dst is not null, write the
    351         changed path into dst, and leave the src path unchanged. In that case
    352         dst must have been previously allocated by the caller.
    353      */
    354     void (*offset)(ANPPath* src, float dx, float dy, ANPPath* dst);
    355 
    356     /** Transform the path by the matrix. If dst is null, apply the
    357         change directly to the src path. If dst is not null, write the
    358         changed path into dst, and leave the src path unchanged. In that case
    359         dst must have been previously allocated by the caller.
    360      */
    361     void (*transform)(ANPPath* src, const ANPMatrix*, ANPPath* dst);
    362 };
    363 
    364 /** ANPColor is always defined to have the same packing on all platforms, and
    365     it is always unpremultiplied.
    366 
    367     This is in contrast to 32bit format(s) in bitmaps, which are premultiplied,
    368     and their packing may vary depending on the platform, hence the need for
    369     ANPBitmapInterface::getPixelPacking()
    370  */
    371 typedef uint32_t ANPColor;
    372 #define ANPColor_ASHIFT     24
    373 #define ANPColor_RSHIFT     16
    374 #define ANPColor_GSHIFT     8
    375 #define ANPColor_BSHIFT     0
    376 #define ANP_MAKE_COLOR(a, r, g, b)  \
    377                    (((a) << ANPColor_ASHIFT) |  \
    378                     ((r) << ANPColor_RSHIFT) |  \
    379                     ((g) << ANPColor_GSHIFT) |  \
    380                     ((b) << ANPColor_BSHIFT))
    381 
    382 enum ANPPaintFlag {
    383     kAntiAlias_ANPPaintFlag         = 1 << 0,
    384     kFilterBitmap_ANPPaintFlag      = 1 << 1,
    385     kDither_ANPPaintFlag            = 1 << 2,
    386     kUnderlineText_ANPPaintFlag     = 1 << 3,
    387     kStrikeThruText_ANPPaintFlag    = 1 << 4,
    388     kFakeBoldText_ANPPaintFlag      = 1 << 5,
    389 };
    390 typedef uint32_t ANPPaintFlags;
    391 
    392 enum ANPPaintStyles {
    393     kFill_ANPPaintStyle             = 0,
    394     kStroke_ANPPaintStyle           = 1,
    395     kFillAndStroke_ANPPaintStyle    = 2
    396 };
    397 typedef int32_t ANPPaintStyle;
    398 
    399 enum ANPPaintCaps {
    400     kButt_ANPPaintCap   = 0,
    401     kRound_ANPPaintCap  = 1,
    402     kSquare_ANPPaintCap = 2
    403 };
    404 typedef int32_t ANPPaintCap;
    405 
    406 enum ANPPaintJoins {
    407     kMiter_ANPPaintJoin = 0,
    408     kRound_ANPPaintJoin = 1,
    409     kBevel_ANPPaintJoin = 2
    410 };
    411 typedef int32_t ANPPaintJoin;
    412 
    413 enum ANPPaintAligns {
    414     kLeft_ANPPaintAlign     = 0,
    415     kCenter_ANPPaintAlign   = 1,
    416     kRight_ANPPaintAlign    = 2
    417 };
    418 typedef int32_t ANPPaintAlign;
    419 
    420 enum ANPTextEncodings {
    421     kUTF8_ANPTextEncoding   = 0,
    422     kUTF16_ANPTextEncoding  = 1,
    423 };
    424 typedef int32_t ANPTextEncoding;
    425 
    426 enum ANPTypefaceStyles {
    427     kBold_ANPTypefaceStyle      = 1 << 0,
    428     kItalic_ANPTypefaceStyle    = 1 << 1
    429 };
    430 typedef uint32_t ANPTypefaceStyle;
    431 
    432 typedef uint32_t ANPFontTableTag;
    433 
    434 struct ANPFontMetrics {
    435     /** The greatest distance above the baseline for any glyph (will be <= 0) */
    436     float   fTop;
    437     /** The recommended distance above the baseline (will be <= 0) */
    438     float   fAscent;
    439     /** The recommended distance below the baseline (will be >= 0) */
    440     float   fDescent;
    441     /** The greatest distance below the baseline for any glyph (will be >= 0) */
    442     float   fBottom;
    443     /** The recommended distance to add between lines of text (will be >= 0) */
    444     float   fLeading;
    445 };
    446 
    447 struct ANPTypefaceInterfaceV0 : ANPInterface {
    448     /** Return a new reference to the typeface that most closely matches the
    449         requested name and style. Pass null as the name to return
    450         the default font for the requested style. Will never return null
    451 
    452         The 5 generic font names "serif", "sans-serif", "monospace", "cursive",
    453         "fantasy" are recognized, and will be mapped to their logical font
    454         automatically by this call.
    455 
    456         @param name     May be NULL. The name of the font family.
    457         @param style    The style (normal, bold, italic) of the typeface.
    458         @return reference to the closest-matching typeface. Caller must call
    459                 unref() when they are done with the typeface.
    460      */
    461     ANPTypeface* (*createFromName)(const char name[], ANPTypefaceStyle);
    462 
    463     /** Return a new reference to the typeface that most closely matches the
    464         requested typeface and specified Style. Use this call if you want to
    465         pick a new style from the same family of the existing typeface.
    466         If family is NULL, this selects from the default font's family.
    467 
    468         @param family  May be NULL. The name of the existing type face.
    469         @param s       The style (normal, bold, italic) of the type face.
    470         @return reference to the closest-matching typeface. Call must call
    471                 unref() when they are done.
    472      */
    473     ANPTypeface* (*createFromTypeface)(const ANPTypeface* family,
    474                                        ANPTypefaceStyle);
    475 
    476     /** Return the owner count of the typeface. A newly created typeface has an
    477         owner count of 1. When the owner count is reaches 0, the typeface is
    478         deleted.
    479      */
    480     int32_t (*getRefCount)(const ANPTypeface*);
    481 
    482     /** Increment the owner count on the typeface
    483      */
    484     void (*ref)(ANPTypeface*);
    485 
    486     /** Decrement the owner count on the typeface. When the count goes to 0,
    487         the typeface is deleted.
    488      */
    489     void (*unref)(ANPTypeface*);
    490 
    491     /** Return the style bits for the specified typeface
    492      */
    493     ANPTypefaceStyle (*getStyle)(const ANPTypeface*);
    494 
    495     /** Some fonts are stored in files. If that is true for the fontID, then
    496         this returns the byte length of the full file path. If path is not null,
    497         then the full path is copied into path (allocated by the caller), up to
    498         length bytes. If index is not null, then it is set to the truetype
    499         collection index for this font, or 0 if the font is not in a collection.
    500 
    501         Note: getFontPath does not assume that path is a null-terminated string,
    502         so when it succeeds, it only copies the bytes of the file name and
    503         nothing else (i.e. it copies exactly the number of bytes returned by the
    504         function. If the caller wants to treat path[] as a C string, it must be
    505         sure that it is allocated at least 1 byte larger than the returned size,
    506         and it must copy in the terminating 0.
    507 
    508         If the fontID does not correspond to a file, then the function returns
    509         0, and the path and index parameters are ignored.
    510 
    511         @param fontID  The font whose file name is being queried
    512         @param path    Either NULL, or storage for receiving up to length bytes
    513                        of the font's file name. Allocated by the caller.
    514         @param length  The maximum space allocated in path (by the caller).
    515                        Ignored if path is NULL.
    516         @param index   Either NULL, or receives the TTC index for this font.
    517                        If the font is not a TTC, then will be set to 0.
    518         @return The byte length of th font's file name, or 0 if the font is not
    519                 baked by a file.
    520      */
    521     int32_t (*getFontPath)(const ANPTypeface*, char path[], int32_t length,
    522                            int32_t* index);
    523 
    524     /** Return a UTF8 encoded path name for the font directory, or NULL if not
    525         supported. If returned, this string address will be valid for the life
    526         of the plugin instance. It will always end with a '/' character.
    527      */
    528     const char* (*getFontDirectoryPath)();
    529 };
    530 
    531 struct ANPPaintInterfaceV0 : ANPInterface {
    532     /** Return a new paint object, which holds all of the color and style
    533         attributes that affect how things (geometry, text, bitmaps) are drawn
    534         in a ANPCanvas.
    535 
    536         The paint that is returned is not tied to any particular plugin
    537         instance, but it must only be accessed from one thread at a time.
    538      */
    539     ANPPaint*   (*newPaint)();
    540     void        (*deletePaint)(ANPPaint*);
    541 
    542     ANPPaintFlags (*getFlags)(const ANPPaint*);
    543     void        (*setFlags)(ANPPaint*, ANPPaintFlags);
    544 
    545     ANPColor    (*getColor)(const ANPPaint*);
    546     void        (*setColor)(ANPPaint*, ANPColor);
    547 
    548     ANPPaintStyle (*getStyle)(const ANPPaint*);
    549     void        (*setStyle)(ANPPaint*, ANPPaintStyle);
    550 
    551     float       (*getStrokeWidth)(const ANPPaint*);
    552     float       (*getStrokeMiter)(const ANPPaint*);
    553     ANPPaintCap (*getStrokeCap)(const ANPPaint*);
    554     ANPPaintJoin (*getStrokeJoin)(const ANPPaint*);
    555     void        (*setStrokeWidth)(ANPPaint*, float);
    556     void        (*setStrokeMiter)(ANPPaint*, float);
    557     void        (*setStrokeCap)(ANPPaint*, ANPPaintCap);
    558     void        (*setStrokeJoin)(ANPPaint*, ANPPaintJoin);
    559 
    560     ANPTextEncoding (*getTextEncoding)(const ANPPaint*);
    561     ANPPaintAlign (*getTextAlign)(const ANPPaint*);
    562     float       (*getTextSize)(const ANPPaint*);
    563     float       (*getTextScaleX)(const ANPPaint*);
    564     float       (*getTextSkewX)(const ANPPaint*);
    565     void        (*setTextEncoding)(ANPPaint*, ANPTextEncoding);
    566     void        (*setTextAlign)(ANPPaint*, ANPPaintAlign);
    567     void        (*setTextSize)(ANPPaint*, float);
    568     void        (*setTextScaleX)(ANPPaint*, float);
    569     void        (*setTextSkewX)(ANPPaint*, float);
    570 
    571     /** Return the typeface ine paint, or null if there is none. This does not
    572         modify the owner count of the returned typeface.
    573      */
    574     ANPTypeface* (*getTypeface)(const ANPPaint*);
    575 
    576     /** Set the paint's typeface. If the paint already had a non-null typeface,
    577         its owner count is decremented. If the new typeface is non-null, its
    578         owner count is incremented.
    579      */
    580     void (*setTypeface)(ANPPaint*, ANPTypeface*);
    581 
    582     /** Return the width of the text. If bounds is not null, return the bounds
    583         of the text in that rectangle.
    584      */
    585     float (*measureText)(ANPPaint*, const void* text, uint32_t byteLength,
    586                          ANPRectF* bounds);
    587 
    588     /** Return the number of unichars specifed by the text.
    589         If widths is not null, returns the array of advance widths for each
    590             unichar.
    591         If bounds is not null, returns the array of bounds for each unichar.
    592      */
    593     int (*getTextWidths)(ANPPaint*, const void* text, uint32_t byteLength,
    594                          float widths[], ANPRectF bounds[]);
    595 
    596     /** Return in metrics the spacing values for text, respecting the paint's
    597         typeface and pointsize, and return the spacing between lines
    598         (descent - ascent + leading). If metrics is NULL, it will be ignored.
    599      */
    600     float (*getFontMetrics)(ANPPaint*, ANPFontMetrics* metrics);
    601 };
    602 
    603 struct ANPCanvasInterfaceV0 : ANPInterface {
    604     /** Return a canvas that will draw into the specified bitmap. Note: the
    605         canvas copies the fields of the bitmap, so it need not persist after
    606         this call, but the canvas DOES point to the same pixel memory that the
    607         bitmap did, so the canvas should not be used after that pixel memory
    608         goes out of scope. In the case of creating a canvas to draw into the
    609         pixels provided by kDraw_ANPEventType, those pixels are only while
    610         handling that event.
    611 
    612         The canvas that is returned is not tied to any particular plugin
    613         instance, but it must only be accessed from one thread at a time.
    614      */
    615     ANPCanvas*  (*newCanvas)(const ANPBitmap*);
    616     void        (*deleteCanvas)(ANPCanvas*);
    617 
    618     void        (*save)(ANPCanvas*);
    619     void        (*restore)(ANPCanvas*);
    620     void        (*translate)(ANPCanvas*, float tx, float ty);
    621     void        (*scale)(ANPCanvas*, float sx, float sy);
    622     void        (*rotate)(ANPCanvas*, float degrees);
    623     void        (*skew)(ANPCanvas*, float kx, float ky);
    624     void        (*concat)(ANPCanvas*, const ANPMatrix*);
    625     void        (*clipRect)(ANPCanvas*, const ANPRectF*);
    626     void        (*clipPath)(ANPCanvas*, const ANPPath*);
    627 
    628     /** Return the current matrix on the canvas
    629      */
    630     void        (*getTotalMatrix)(ANPCanvas*, ANPMatrix*);
    631     /** Return the current clip bounds in local coordinates, expanding it to
    632         account for antialiasing edge effects if aa is true. If the
    633         current clip is empty, return false and ignore the bounds argument.
    634      */
    635     bool        (*getLocalClipBounds)(ANPCanvas*, ANPRectF* bounds, bool aa);
    636     /** Return the current clip bounds in device coordinates in bounds. If the
    637         current clip is empty, return false and ignore the bounds argument.
    638      */
    639     bool        (*getDeviceClipBounds)(ANPCanvas*, ANPRectI* bounds);
    640 
    641     void        (*drawColor)(ANPCanvas*, ANPColor);
    642     void        (*drawPaint)(ANPCanvas*, const ANPPaint*);
    643     void        (*drawLine)(ANPCanvas*, float x0, float y0, float x1, float y1,
    644                             const ANPPaint*);
    645     void        (*drawRect)(ANPCanvas*, const ANPRectF*, const ANPPaint*);
    646     void        (*drawOval)(ANPCanvas*, const ANPRectF*, const ANPPaint*);
    647     void        (*drawPath)(ANPCanvas*, const ANPPath*, const ANPPaint*);
    648     void        (*drawText)(ANPCanvas*, const void* text, uint32_t byteLength,
    649                             float x, float y, const ANPPaint*);
    650     void       (*drawPosText)(ANPCanvas*, const void* text, uint32_t byteLength,
    651                                const float xy[], const ANPPaint*);
    652     void        (*drawBitmap)(ANPCanvas*, const ANPBitmap*, float x, float y,
    653                               const ANPPaint*);
    654     void        (*drawBitmapRect)(ANPCanvas*, const ANPBitmap*,
    655                                   const ANPRectI* src, const ANPRectF* dst,
    656                                   const ANPPaint*);
    657 };
    658 
    659 struct ANPWindowInterfaceV0 : ANPInterface {
    660     /** Registers a set of rectangles that the plugin would like to keep on
    661         screen. The rectangles are listed in order of priority with the highest
    662         priority rectangle in location rects[0].  The browser will attempt to keep
    663         as many of the rectangles on screen as possible and will scroll them into
    664         view in response to the invocation of this method and other various events.
    665         The count specifies how many rectangles are in the array. If the count is
    666         zero it signals the browser that any existing rectangles should be cleared
    667         and no rectangles will be tracked.
    668      */
    669     void (*setVisibleRects)(NPP instance, const ANPRectI rects[], int32_t count);
    670     /** Clears any rectangles that are being tracked as a result of a call to
    671         setVisibleRects. This call is equivalent to setVisibleRect(inst, NULL, 0).
    672      */
    673     void    (*clearVisibleRects)(NPP instance);
    674     /** Given a boolean value of true the device will be requested to provide
    675         a keyboard. A value of false will result in a request to hide the
    676         keyboard. Further, the on-screen keyboard will not be displayed if a
    677         physical keyboard is active.
    678      */
    679     void    (*showKeyboard)(NPP instance, bool value);
    680     /** Called when a plugin wishes to enter into full screen mode. The plugin's
    681         Java class (defined in the plugin's apk manifest) will be called
    682         asynchronously to provide a View object to be displayed full screen.
    683      */
    684     void    (*requestFullScreen)(NPP instance);
    685     /** Called when a plugin wishes to exit from full screen mode. As a result,
    686         the plugin's full screen view will be discarded by the view system.
    687      */
    688     void    (*exitFullScreen)(NPP instance);
    689     /** Called when a plugin wishes to be zoomed and centered in the current view.
    690      */
    691     void    (*requestCenterFitZoom)(NPP instance);
    692 };
    693 
    694 struct ANPWindowInterfaceV1 : ANPWindowInterfaceV0 {
    695     /** Returns a rectangle representing the visible area of the plugin on
    696         screen. The coordinates are relative to the size of the plugin in the
    697         document and therefore will never be negative or exceed the plugin's size.
    698      */
    699     ANPRectI (*visibleRect)(NPP instance);
    700 };
    701 
    702 enum ANPScreenOrientations {
    703     /** No preference specified: let the system decide the best orientation.
    704      */
    705     kDefault_ANPScreenOrientation        = 0,
    706     /** Would like to have the screen in a landscape orientation, but it will
    707         not allow for 180 degree rotations.
    708      */
    709     kFixedLandscape_ANPScreenOrientation = 1,
    710     /** Would like to have the screen in a portrait orientation, but it will
    711         not allow for 180 degree rotations.
    712      */
    713     kFixedPortrait_ANPScreenOrientation  = 2,
    714     /** Would like to have the screen in landscape orientation, but can use the
    715         sensor to change which direction the screen is facing.
    716      */
    717     kLandscape_ANPScreenOrientation      = 3,
    718     /** Would like to have the screen in portrait orientation, but can use the
    719         sensor to change which direction the screen is facing.
    720      */
    721     kPortrait_ANPScreenOrientation       = 4
    722 };
    723 typedef int32_t ANPScreenOrientation;
    724 
    725 struct ANPWindowInterfaceV2 : ANPWindowInterfaceV1 {
    726     /** Called when the plugin wants to specify a particular screen orientation
    727         when entering into full screen mode. The orientation must be set prior
    728         to entering into full screen.  After entering full screen any subsequent
    729         changes will be updated the next time the plugin goes full screen.
    730      */
    731     void (*requestFullScreenOrientation)(NPP instance, ANPScreenOrientation orientation);
    732 };
    733 
    734 ///////////////////////////////////////////////////////////////////////////////
    735 
    736 enum ANPSampleFormats {
    737     kUnknown_ANPSamleFormat     = 0,
    738     kPCM16Bit_ANPSampleFormat   = 1,
    739     kPCM8Bit_ANPSampleFormat    = 2
    740 };
    741 typedef int32_t ANPSampleFormat;
    742 
    743 /** The audio buffer is passed to the callback proc to request more samples.
    744     It is owned by the system, and the callback may read it, but should not
    745     maintain a pointer to it outside of the scope of the callback proc.
    746  */
    747 struct ANPAudioBuffer {
    748     // RO - repeat what was specified in newTrack()
    749     int32_t     channelCount;
    750     // RO - repeat what was specified in newTrack()
    751     ANPSampleFormat  format;
    752     /** This buffer is owned by the caller. Inside the callback proc, up to
    753         "size" bytes of sample data should be written into this buffer. The
    754         address is only valid for the scope of a single invocation of the
    755         callback proc.
    756      */
    757     void*       bufferData;
    758     /** On input, specifies the maximum number of bytes that can be written
    759         to "bufferData". On output, specifies the actual number of bytes that
    760         the callback proc wrote into "bufferData".
    761      */
    762     uint32_t    size;
    763 };
    764 
    765 enum ANPAudioEvents {
    766     /** This event is passed to the callback proc when the audio-track needs
    767         more sample data written to the provided buffer parameter.
    768      */
    769     kMoreData_ANPAudioEvent = 0,
    770     /** This event is passed to the callback proc if the audio system runs out
    771         of sample data. In this event, no buffer parameter will be specified
    772         (i.e. NULL will be passed to the 3rd parameter).
    773      */
    774     kUnderRun_ANPAudioEvent = 1
    775 };
    776 typedef int32_t ANPAudioEvent;
    777 
    778 /** Called to feed sample data to the track. This will be called in a separate
    779     thread. However, you may call trackStop() from the callback (but you
    780     cannot delete the track).
    781 
    782     For example, when you have written the last chunk of sample data, you can
    783     immediately call trackStop(). This will take effect after the current
    784     buffer has been played.
    785 
    786     The "user" parameter is the same value that was passed to newTrack()
    787  */
    788 typedef void (*ANPAudioCallbackProc)(ANPAudioEvent event, void* user,
    789                                      ANPAudioBuffer* buffer);
    790 
    791 struct ANPAudioTrack;   // abstract type for audio tracks
    792 
    793 struct ANPAudioTrackInterfaceV0 : ANPInterface {
    794     /** Create a new audio track, or NULL on failure. The track is initially in
    795         the stopped state and therefore ANPAudioCallbackProc will not be called
    796         until the track is started.
    797      */
    798     ANPAudioTrack*  (*newTrack)(uint32_t sampleRate,    // sampling rate in Hz
    799                                 ANPSampleFormat,
    800                                 int channelCount,       // MONO=1, STEREO=2
    801                                 ANPAudioCallbackProc,
    802                                 void* user);
    803     /** Deletes a track that was created using newTrack.  The track can be
    804         deleted in any state and it waits for the ANPAudioCallbackProc thread
    805         to exit before returning.
    806      */
    807     void (*deleteTrack)(ANPAudioTrack*);
    808 
    809     void (*start)(ANPAudioTrack*);
    810     void (*pause)(ANPAudioTrack*);
    811     void (*stop)(ANPAudioTrack*);
    812     /** Returns true if the track is not playing (e.g. pause or stop was called,
    813         or start was never called.
    814      */
    815     bool (*isStopped)(ANPAudioTrack*);
    816 };
    817 
    818 struct ANPAudioTrackInterfaceV1 : ANPAudioTrackInterfaceV0 {
    819     /** Returns the track's latency in milliseconds. */
    820     uint32_t (*trackLatency)(ANPAudioTrack*);
    821 };
    822 
    823 ///////////////////////////////////////////////////////////////////////////////
    824 // DEFINITION OF VALUES PASSED THROUGH NPP_HandleEvent
    825 
    826 enum ANPEventTypes {
    827     kNull_ANPEventType          = 0,
    828     kKey_ANPEventType           = 1,
    829     /** Mouse events are triggered by either clicking with the navigational pad
    830         or by tapping the touchscreen (if the kDown_ANPTouchAction is handled by
    831         the plugin then no mouse event is generated).  The kKey_ANPEventFlag has
    832         to be set to true in order to receive these events.
    833      */
    834     kMouse_ANPEventType         = 2,
    835     /** Touch events are generated when the user touches on the screen. The
    836         kTouch_ANPEventFlag has to be set to true in order to receive these
    837         events.
    838      */
    839     kTouch_ANPEventType         = 3,
    840     /** Only triggered by a plugin using the kBitmap_ANPDrawingModel. This event
    841         signals that the plugin needs to redraw itself into the provided bitmap.
    842      */
    843     kDraw_ANPEventType          = 4,
    844     kLifecycle_ANPEventType     = 5,
    845 
    846     /** This event type is completely defined by the plugin.
    847         When creating an event, the caller must always set the first
    848         two fields, the remaining data is optional.
    849             ANPEvent evt;
    850             evt.inSize = sizeof(ANPEvent);
    851             evt.eventType = kCustom_ANPEventType
    852             // other data slots are optional
    853             evt.other[] = ...;
    854         To post a copy of the event, call
    855             eventInterface->postEvent(myNPPInstance, &evt);
    856         That call makes a copy of the event struct, and post that on the event
    857         queue for the plugin.
    858      */
    859     kCustom_ANPEventType        = 6,
    860     /** MultiTouch events are generated when the user touches on the screen. The
    861         kTouch_ANPEventFlag has to be set to true in order to receive these
    862         events. This type is a replacement for the older kTouch_ANPEventType.
    863      */
    864     kMultiTouch_ANPEventType    = 7,
    865 };
    866 typedef int32_t ANPEventType;
    867 
    868 enum ANPKeyActions {
    869     kDown_ANPKeyAction  = 0,
    870     kUp_ANPKeyAction    = 1,
    871 };
    872 typedef int32_t ANPKeyAction;
    873 
    874 #include "ANPKeyCodes.h"
    875 typedef int32_t ANPKeyCode;
    876 
    877 enum ANPKeyModifiers {
    878     kAlt_ANPKeyModifier     = 1 << 0,
    879     kShift_ANPKeyModifier   = 1 << 1,
    880 };
    881 // bit-field containing some number of ANPKeyModifier bits
    882 typedef uint32_t ANPKeyModifier;
    883 
    884 enum ANPMouseActions {
    885     kDown_ANPMouseAction  = 0,
    886     kUp_ANPMouseAction    = 1,
    887 };
    888 typedef int32_t ANPMouseAction;
    889 
    890 enum ANPTouchActions {
    891     /** This occurs when the user first touches on the screen. As such, this
    892         action will always occur prior to any of the other touch actions. If
    893         the plugin chooses to not handle this action then no other events
    894         related to that particular touch gesture will be generated.
    895      */
    896     kDown_ANPTouchAction        = 0,
    897     kUp_ANPTouchAction          = 1,
    898     kMove_ANPTouchAction        = 2,
    899     kCancel_ANPTouchAction      = 3,
    900     // The web view will ignore the return value from the following actions
    901     kLongPress_ANPTouchAction   = 4,
    902     kDoubleTap_ANPTouchAction   = 5,
    903 };
    904 typedef int32_t ANPTouchAction;
    905 
    906 enum ANPLifecycleActions {
    907     /** The web view containing this plugin has been paused.  See documentation
    908         on the android activity lifecycle for more information.
    909      */
    910     kPause_ANPLifecycleAction           = 0,
    911     /** The web view containing this plugin has been resumed. See documentation
    912         on the android activity lifecycle for more information.
    913      */
    914     kResume_ANPLifecycleAction          = 1,
    915     /** The plugin has focus and is now the recipient of input events (e.g. key,
    916         touch, etc.)
    917      */
    918     kGainFocus_ANPLifecycleAction       = 2,
    919     /** The plugin has lost focus and will not receive any input events until it
    920         regains focus. This event is always preceded by a GainFocus action.
    921      */
    922     kLoseFocus_ANPLifecycleAction       = 3,
    923     /** The browser is running low on available memory and is requesting that
    924         the plugin free any unused/inactive resources to prevent a performance
    925         degradation.
    926      */
    927     kFreeMemory_ANPLifecycleAction      = 4,
    928     /** The page has finished loading. This happens when the page's top level
    929         frame reports that it has completed loading.
    930      */
    931     kOnLoad_ANPLifecycleAction          = 5,
    932     /** The browser is honoring the plugin's request to go full screen. Upon
    933         returning from this event the browser will resize the plugin's java
    934         surface to full-screen coordinates.
    935      */
    936     kEnterFullScreen_ANPLifecycleAction = 6,
    937     /** The browser has exited from full screen mode. Immediately prior to
    938         sending this event the browser has resized the plugin's java surface to
    939         its original coordinates.
    940      */
    941     kExitFullScreen_ANPLifecycleAction  = 7,
    942     /** The plugin is visible to the user on the screen. This event will always
    943         occur after a kOffScreen_ANPLifecycleAction event.
    944      */
    945     kOnScreen_ANPLifecycleAction        = 8,
    946     /** The plugin is no longer visible to the user on the screen. This event
    947         will always occur prior to an kOnScreen_ANPLifecycleAction event.
    948      */
    949     kOffScreen_ANPLifecycleAction       = 9,
    950 };
    951 typedef uint32_t ANPLifecycleAction;
    952 
    953 struct TouchPoint {
    954     int32_t         id;
    955     float           x;  // relative to your "window" (0...width)
    956     float           y;  // relative to your "window" (0...height)
    957     float           pressure;
    958     float           size; // normalized to a value between 0...1
    959 };
    960 
    961 /* This is what is passed to NPP_HandleEvent() */
    962 struct ANPEvent {
    963     uint32_t        inSize;  // size of this struct in bytes
    964     ANPEventType    eventType;
    965     // use based on the value in eventType
    966     union {
    967         struct {
    968             ANPKeyAction    action;
    969             ANPKeyCode      nativeCode;
    970             int32_t         virtualCode;    // windows virtual key code
    971             ANPKeyModifier  modifiers;
    972             int32_t         repeatCount;    // 0 for initial down (or up)
    973             int32_t         unichar;        // 0 if there is no value
    974         } key;
    975         struct {
    976             ANPMouseAction  action;
    977             int32_t         x;  // relative to your "window" (0...width)
    978             int32_t         y;  // relative to your "window" (0...height)
    979         } mouse;
    980         struct {
    981             ANPTouchAction  action;
    982             ANPKeyModifier  modifiers;
    983             int32_t         x;  // relative to your "window" (0...width)
    984             int32_t         y;  // relative to your "window" (0...height)
    985         } touch;
    986         struct {
    987             ANPLifecycleAction  action;
    988         } lifecycle;
    989         struct {
    990             ANPDrawingModel model;
    991             // relative to (0,0) in top-left of your plugin
    992             ANPRectI        clip;
    993             // use based on the value in model
    994             union {
    995                 ANPBitmap   bitmap;
    996                 struct {
    997                     int32_t width;
    998                     int32_t height;
    999                 } surface;
   1000             } data;
   1001         } draw;
   1002         struct {
   1003             int64_t         timestamp;
   1004             int32_t         id;
   1005             ANPTouchAction  action;
   1006             int32_t         pointerCount;
   1007             TouchPoint*     touchPoint;
   1008         } multiTouch;
   1009         int32_t     other[8];
   1010     } data;
   1011 };
   1012 
   1013 struct ANPEventInterfaceV0 : ANPInterface {
   1014     /** Post a copy of the specified event to the plugin. The event will be
   1015         delivered to the plugin in its main thread (the thread that receives
   1016         other ANPEvents). If, after posting before delivery, the NPP instance
   1017         is torn down, the event will be discarded.
   1018      */
   1019     void (*postEvent)(NPP inst, const ANPEvent* event);
   1020 };
   1021 
   1022 
   1023 #endif
   1024