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