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