npmac.cpp 8.44 KB
Newer Older
1 2 3
/*****************************************************************************
 * npmac.cpp: Safari/Mozilla/Firefox plugin for VLC
 *****************************************************************************
4
 * Copyright (C) 2009, Jean-Paul Saman <jpsaman@videolan.org>
5
 * Copyright (C) 2012-2015 Felix Paul Kühne <fkuehne # videolan # org>
6
 * $Id:$
7
 *
8 9
 * Authors: Jean-Paul Saman <jpsaman@videolan.org>
 *          Felix Paul Kühne <fkuehne # videolan # org>
10
 *
11 12 13 14 15 16
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
20
 *
21 22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
25

26 27 28
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
29

30
#include <string.h>
31
#include <stddef.h>
32
#include <cstring>
33

34
#include "../common.h"
35
#include "../vlcplugin.h"
36 37
#include "../vlcshell.h"

38 39 40 41
#include <npapi.h>
#include "npfunctions.h"
#define CALL_NPN(unused, FN, ...) ((*FN)(__VA_ARGS__))

42

43

44
#pragma mark -
45
#pragma mark Globals
46

47
NPNetscapeFuncs  *gNetscapeFuncs;    /* Netscape Function table */
48
short               gResFile;           // Refnum of the plugin’s resource file
49
static inline int getMinorVersion() { return gNetscapeFuncs->version & 0xFF; }
50 51


52 53
#pragma mark -
#pragma mark Wrapper Functions
54

55 56 57 58 59 60

void NPN_PluginThreadAsyncCall(NPP instance, void (*func)(void *), void *userData)
{
    CALL_NPN(CallNPN_PluginThreadAsyncCallProc, gNetscapeFuncs->pluginthreadasynccall, instance, func, userData);
}

61
#pragma mark -
62
#pragma mark Private Functions
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77
/***********************************************************************
 *
 * Wrapper functions : Netscape Navigator -> plugin
 *
 * These functions let the plugin developer just create the APIs
 * as documented and defined in npapi.h, without needing to 
 * install those functions in the function table or worry about
 * setting up globals for 68K plugins.
 *
 ***********************************************************************/

/* Function prototypes */
NPError Private_New(NPMIMEType pluginType, NPP instance, uint16_t mode,
        int16_t argc, char* argn[], char* argv[], NPSavedData* saved);
78

79 80 81 82 83 84
static bool boolValue(const char *value) {
    return ( !strcmp(value, "1") ||
            !strcasecmp(value, "true") ||
            !strcasecmp(value, "yes") );
}

85
/* function implementations */
86
NPError
87 88
Private_New(NPMIMEType pluginType, NPP instance, uint16_t mode,
        int16_t argc, char* argn[], char* argv[], NPSavedData* saved)
89
{
90 91 92 93 94 95 96 97 98 99
    /* find out, if the plugin should run in windowless mode.
     * if yes, choose the CoreGraphics drawing model */
    bool windowless = false;
    for( int i = 0; i < argc; i++ )
    {
        if( !strcmp( argn[i], "windowless" ) )
        {
            windowless = boolValue(argv[i]);
            break;
        }
100 101
    }

102 103
    NPError err;
    if (windowless) {
104
        fprintf(stderr, "running in windowless\n");
105 106 107
        NPBool supportsCoreGraphics = FALSE;
        err = NPN_GetValue(instance, NPNVsupportsCoreGraphicsBool, &supportsCoreGraphics);
        if (err != NPERR_NO_ERROR || !supportsCoreGraphics) {
108
            fprintf(stderr, "Error in New: browser doesn't support CoreGraphics drawing model\n");
109 110 111 112 113
            return NPERR_INCOMPATIBLE_VERSION_ERROR;
        }

        err = NPN_SetValue(instance, NPPVpluginDrawingModel, (void*)NPDrawingModelCoreGraphics);
        if (err != NPERR_NO_ERROR) {
114
            fprintf(stderr, "Error in New: couldn't activate CoreGraphics drawing model\n");
115 116 117
            return NPERR_INCOMPATIBLE_VERSION_ERROR;
        }
    } else {
118
        fprintf(stderr, "running windowed\n");
119 120 121
        NPBool supportsCoreAnimation = FALSE;
        err = NPN_GetValue(instance, NPNVsupportsCoreAnimationBool, &supportsCoreAnimation);
        if (err != NPERR_NO_ERROR || !supportsCoreAnimation) {
122
            fprintf(stderr, "Error in New: browser doesn't support CoreAnimation drawing model\n");
123 124 125
            return NPERR_INCOMPATIBLE_VERSION_ERROR;
        }

126 127 128 129 130
        err = NPN_SetValue(instance, NPPVpluginDrawingModel, (void*)NPDrawingModelCoreAnimation);
        if (err != NPERR_NO_ERROR) {
            fprintf(stderr, "Error in New: couldn't activate CoreAnimation drawing model\n");
            return NPERR_INCOMPATIBLE_VERSION_ERROR;
        }
131 132 133 134 135
    }

    NPBool supportsCocoaEvents = FALSE;
    err = NPN_GetValue(instance, NPNVsupportsCocoaBool, &supportsCocoaEvents);
    if (err != NPERR_NO_ERROR || !supportsCocoaEvents) {
136
        fprintf(stderr, "Error in New: browser doesn't support Cocoa event model\n");
137 138
        return NPERR_INCOMPATIBLE_VERSION_ERROR;
    }
139 140 141

    err = NPN_SetValue(instance, NPPVpluginEventModel, (void*)NPEventModelCocoa);
    if (err != NPERR_NO_ERROR) {
142
        fprintf(stderr, "Error in New: couldn't activate Cocoa event model\n");
143
        return NPERR_INCOMPATIBLE_VERSION_ERROR;
144 145 146
    }

    NPError ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
147

148
    return ret;
149 150
}

151 152
#pragma mark -
#pragma mark Initialization & Run
153

154 155 156 157 158 159 160 161 162 163 164 165 166 167
/*
** netscape plugins functions when building Mach-O binary
*/

extern "C" {
    NPError NP_Initialize(NPNetscapeFuncs* nsTable);
    NPError NP_GetEntryPoints(NPPluginFuncs* pluginFuncs);
    NPError NP_Shutdown(void);
}

/*
** netscape plugins functions when using Mach-O binary
*/

168 169
NPError
NP_Initialize(NPNetscapeFuncs* nsTable)
170 171 172
{
    /* validate input parameters */

173
    if (NULL == nsTable) {
174
        fprintf(stderr, "NP_Initialize error: NPERR_INVALID_FUNCTABLE_ERROR: table is null\n");
175
        return NPERR_INVALID_FUNCTABLE_ERROR;
176
    }
177

178 179 180 181 182 183 184 185 186
    /*
     * Check the major version passed in Netscape's function table.
     * We won't load if the major version is newer than what we expect.
     * Also check that the function tables passed in are big enough for
     * all the functions we need (they could be bigger, if Netscape added
     * new APIs, but that's OK with us -- we'll just ignore them).
     *
     */

187
    if ((nsTable->version >> 8) > NP_VERSION_MAJOR) {
188
        fprintf(stderr, "NP_Initialize error: NPERR_INCOMPATIBLE_VERSION_ERROR\n");
189
        return NPERR_INCOMPATIBLE_VERSION_ERROR;
190
    }
191

192

193
    // We use all functions of the nsTable up to and including pluginthreadasynccall. We therefore check that
194
    // reaches at least till that function.
195
    if (nsTable->size < (offsetof(NPNetscapeFuncs, pluginthreadasynccall) + sizeof(NPN_PluginThreadAsyncCallProcPtr))) {
196
        fprintf(stderr, "NP_Initialize error: NPERR_INVALID_FUNCTABLE_ERROR: table too small\n");
197
        return NPERR_INVALID_FUNCTABLE_ERROR;
198
    }
199

200
    gNetscapeFuncs = nsTable;
201 202 203 204

    return NPP_Initialize();
}

205 206
NPError
NP_GetEntryPoints(NPPluginFuncs* pluginFuncs)
207
{
208
    int minor = getMinorVersion();
209

210
    if (pluginFuncs == NULL)
211 212
        return NPERR_INVALID_FUNCTABLE_ERROR;

213
    /*if (pluginFuncs->size < sizeof(NPPluginFuncs))
214 215 216 217 218 219 220 221 222
    return NPERR_INVALID_FUNCTABLE_ERROR;*/

    /*
     * Set up the plugin function table that Netscape will use to
     * call us.  Netscape needs to know about our version and size
     * and have a UniversalProcPointer for every function we
     * implement.
     */

223 224 225 226 227 228 229 230 231 232 233 234 235
    pluginFuncs->version       = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
    pluginFuncs->newp          = (NPP_NewProcPtr)(Private_New);
    pluginFuncs->destroy       = NPP_Destroy;
    pluginFuncs->setwindow     = NPP_SetWindow;
    pluginFuncs->newstream     = NPP_NewStream;
    pluginFuncs->destroystream = NPP_DestroyStream;
    pluginFuncs->asfile        = NPP_StreamAsFile;
    pluginFuncs->writeready    = NPP_WriteReady;
    pluginFuncs->write         = NPP_Write;
    pluginFuncs->print         = NPP_Print;
    pluginFuncs->event         = NPP_HandleEvent;
    pluginFuncs->getvalue      = NPP_GetValue;
    pluginFuncs->setvalue      = NPP_SetValue;
236

237
    if (minor >= NPVERS_HAS_NOTIFICATION)
238
        pluginFuncs->urlnotify = NPP_URLNotify;
239

240 241 242 243 244
    pluginFuncs->javaClass = NULL;

    return NPERR_NO_ERROR;
}

245 246
NPError
NP_Shutdown(void)
247
{
248
    PLUGINDEBUGSTR("NP_Shutdown");
249 250 251
    NPP_Shutdown();
    return NPERR_NO_ERROR;
}