2010-10-03 23:04:50 +02:00
|
|
|
// Copyright (c) 2009 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "cefclient/uiplugin.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
#if defined(OS_WIN)
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include <windows.h>
|
|
|
|
#include <gl/gl.h>
|
|
|
|
#include <sstream>
|
|
|
|
#include "include/cef_browser.h"
|
|
|
|
#include "include/cef_frame.h"
|
|
|
|
#include "cefclient/cefclient.h"
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Initialized in NP_Initialize.
|
|
|
|
NPNetscapeFuncs* g_uibrowser = NULL;
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
namespace {
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Global values.
|
|
|
|
float g_rotationspeed = 0.0f;
|
|
|
|
float g_theta = 0.0f;
|
|
|
|
|
|
|
|
// Class holding pointers for the client plugin window.
|
2012-01-10 00:46:23 +01:00
|
|
|
class ClientPlugin {
|
|
|
|
public:
|
|
|
|
ClientPlugin() {
|
2010-10-03 23:04:50 +02:00
|
|
|
hWnd = NULL;
|
|
|
|
hDC = NULL;
|
|
|
|
hRC = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HWND hWnd;
|
|
|
|
HDC hDC;
|
|
|
|
HGLRC hRC;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Forward declarations of functions included in this code module:
|
|
|
|
LRESULT CALLBACK PluginWndProc(HWND hWnd, UINT message, WPARAM wParam,
|
|
|
|
LPARAM lParam);
|
|
|
|
void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC);
|
|
|
|
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
NPError NPP_NewImpl(NPMIMEType plugin_type, NPP instance, uint16 mode,
|
|
|
|
int16 argc, char* argn[], char* argv[],
|
|
|
|
NPSavedData* saved) {
|
2010-10-03 23:04:50 +02:00
|
|
|
if (instance == NULL)
|
|
|
|
return NPERR_INVALID_INSTANCE_ERROR;
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
ClientPlugin* plugin = new ClientPlugin;
|
2010-10-03 23:04:50 +02:00
|
|
|
instance->pdata = reinterpret_cast<void*>(plugin);
|
|
|
|
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
NPError NPP_DestroyImpl(NPP instance, NPSavedData** save) {
|
2012-01-10 00:46:23 +01:00
|
|
|
ClientPlugin* plugin = reinterpret_cast<ClientPlugin*>(instance->pdata);
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
if (plugin) {
|
2012-01-10 00:46:23 +01:00
|
|
|
if (plugin->hWnd) {
|
2010-10-03 23:04:50 +02:00
|
|
|
DestroyWindow(plugin->hWnd);
|
|
|
|
DisableOpenGL(plugin->hWnd, plugin->hDC, plugin->hRC);
|
|
|
|
}
|
|
|
|
delete plugin;
|
|
|
|
g_rotationspeed = 0.0f;
|
|
|
|
g_theta = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
NPError NPP_SetWindowImpl(NPP instance, NPWindow* window_info) {
|
2010-10-03 23:04:50 +02:00
|
|
|
if (instance == NULL)
|
|
|
|
return NPERR_INVALID_INSTANCE_ERROR;
|
|
|
|
|
|
|
|
if (window_info == NULL)
|
|
|
|
return NPERR_GENERIC_ERROR;
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
ClientPlugin* plugin = reinterpret_cast<ClientPlugin*>(instance->pdata);
|
2010-10-03 23:04:50 +02:00
|
|
|
HWND parent_hwnd = reinterpret_cast<HWND>(window_info->window);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
|
|
|
if (plugin->hWnd == NULL) {
|
2010-10-03 23:04:50 +02:00
|
|
|
WNDCLASS wc;
|
|
|
|
HINSTANCE hInstance = GetModuleHandle(NULL);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Register the window class.
|
|
|
|
wc.style = CS_OWNDC;
|
|
|
|
wc.lpfnWndProc = PluginWndProc;
|
|
|
|
wc.cbClsExtra = 0;
|
|
|
|
wc.cbWndExtra = 0;
|
|
|
|
wc.hInstance = hInstance;
|
|
|
|
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
|
|
|
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|
|
|
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
|
|
|
|
wc.lpszMenuName = NULL;
|
|
|
|
wc.lpszClassName = L"ClientUIPlugin";
|
|
|
|
RegisterClass(&wc);
|
2011-03-24 21:36:47 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Create the main window.
|
|
|
|
plugin->hWnd = CreateWindow(L"ClientUIPlugin", L"Client UI Plugin",
|
|
|
|
WS_CHILD, 0, 0, 0, 0, parent_hwnd, NULL, hInstance, NULL);
|
2011-03-24 21:36:47 +01:00
|
|
|
|
|
|
|
SetWindowLongPtr(plugin->hWnd, GWLP_USERDATA,
|
|
|
|
reinterpret_cast<LONG_PTR>(plugin));
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Enable OpenGL drawing for the window.
|
|
|
|
EnableOpenGL(plugin->hWnd, &(plugin->hDC), &(plugin->hRC));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position the window and make sure it's visible.
|
|
|
|
RECT parent_rect;
|
|
|
|
GetClientRect(parent_hwnd, &parent_rect);
|
|
|
|
SetWindowPos(plugin->hWnd, NULL, parent_rect.left, parent_rect.top,
|
|
|
|
parent_rect.right - parent_rect.left,
|
|
|
|
parent_rect.bottom - parent_rect.top, SWP_SHOWWINDOW);
|
|
|
|
|
|
|
|
UpdateWindow(plugin->hWnd);
|
|
|
|
ShowWindow(plugin->hWnd, SW_SHOW);
|
|
|
|
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the notification to the browser as a JavaScript function call.
|
2012-01-10 00:46:23 +01:00
|
|
|
static void NotifyNewRotation(float value) {
|
2010-11-22 18:49:46 +01:00
|
|
|
std::stringstream buf;
|
|
|
|
buf << "notifyNewRotation(" << value << ");";
|
|
|
|
AppGetBrowser()->GetMainFrame()->ExecuteJavaScript(buf.str(), CefString(),
|
2010-10-03 23:04:50 +02:00
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nice little fly polygon borrowed from the OpenGL Red Book.
|
|
|
|
const GLubyte fly[] = {
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2012-01-10 00:46:23 +01:00
|
|
|
0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,
|
|
|
|
0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,
|
2010-10-03 23:04:50 +02:00
|
|
|
0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
|
2012-01-10 00:46:23 +01:00
|
|
|
0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22,
|
|
|
|
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
|
|
|
|
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
|
2010-10-03 23:04:50 +02:00
|
|
|
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
|
2012-01-10 00:46:23 +01:00
|
|
|
0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC,
|
2010-10-03 23:04:50 +02:00
|
|
|
0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
|
2012-01-10 00:46:23 +01:00
|
|
|
0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
|
|
|
|
0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
|
2010-10-03 23:04:50 +02:00
|
|
|
0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
|
2012-01-10 00:46:23 +01:00
|
|
|
0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
|
|
|
|
0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
|
2010-10-03 23:04:50 +02:00
|
|
|
0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08};
|
|
|
|
|
|
|
|
|
|
|
|
// Plugin window procedure.
|
|
|
|
LRESULT CALLBACK PluginWndProc(HWND hWnd, UINT message, WPARAM wParam,
|
2012-01-10 00:46:23 +01:00
|
|
|
LPARAM lParam) {
|
2011-03-24 21:36:47 +01:00
|
|
|
ClientPlugin* plugin =
|
|
|
|
reinterpret_cast<ClientPlugin*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
switch (message) {
|
2010-10-03 23:04:50 +02:00
|
|
|
case WM_CREATE:
|
|
|
|
// Start the timer that's used for redrawing.
|
|
|
|
SetTimer(hWnd, 1, 1, NULL);
|
|
|
|
return 0;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
case WM_DESTROY:
|
|
|
|
// Stop the timer that's used for redrawing.
|
|
|
|
KillTimer(hWnd, 1);
|
|
|
|
return 0;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
case WM_LBUTTONDOWN:
|
|
|
|
// Decrement rotation speed.
|
|
|
|
ModifyRotation(-2.0f);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WM_RBUTTONDOWN:
|
|
|
|
// Increment rotation speed.
|
|
|
|
ModifyRotation(2.0f);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WM_SIZE:
|
2011-03-24 21:36:47 +01:00
|
|
|
if (plugin) {
|
|
|
|
// Resize the OpenGL viewport to match the window size.
|
|
|
|
int width = LOWORD(lParam);
|
|
|
|
int height = HIWORD(lParam);
|
|
|
|
|
|
|
|
wglMakeCurrent(plugin->hDC, plugin->hRC);
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_ERASEBKGND:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WM_TIMER:
|
2011-03-24 21:36:47 +01:00
|
|
|
wglMakeCurrent(plugin->hDC, plugin->hRC);
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Adjust the theta value and redraw the display when the timer fires.
|
|
|
|
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
glPushMatrix();
|
|
|
|
glEnable(GL_POLYGON_STIPPLE);
|
|
|
|
glPolygonStipple(fly);
|
|
|
|
|
|
|
|
glRotatef(g_theta, 0.0f, 0.0f, 1.0f);
|
|
|
|
glBegin(GL_QUADS);
|
2012-01-10 00:46:23 +01:00
|
|
|
glColor3f(1.0f, 0.0f, 0.0f);
|
|
|
|
glVertex2f(0.7f, 0.7f);
|
|
|
|
glColor3f(0.0f, 1.0f, 0.0f);
|
|
|
|
glVertex2f(0.7f, -0.7f);
|
|
|
|
glColor3f(0.0f, 0.0f, 1.0f);
|
|
|
|
glVertex2f(-0.7f, -0.7f);
|
|
|
|
glColor3f(1.0f, 0.0f, 1.0f);
|
|
|
|
glVertex2f(-0.7f, 0.7f);
|
2010-10-03 23:04:50 +02:00
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glDisable(GL_POLYGON_STIPPLE);
|
|
|
|
glPopMatrix();
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
SwapBuffers(plugin->hDC);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
g_theta -= g_rotationspeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DefWindowProc(hWnd, message, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable OpenGL.
|
2012-01-10 00:46:23 +01:00
|
|
|
void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC) {
|
2010-10-03 23:04:50 +02:00
|
|
|
PIXELFORMATDESCRIPTOR pfd;
|
|
|
|
int format;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Get the device context.
|
|
|
|
*hDC = GetDC(hWnd);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Set the pixel format for the DC.
|
|
|
|
ZeroMemory(&pfd, sizeof(pfd));
|
|
|
|
pfd.nSize = sizeof(pfd);
|
|
|
|
pfd.nVersion = 1;
|
|
|
|
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
|
|
|
pfd.iPixelType = PFD_TYPE_RGBA;
|
|
|
|
pfd.cColorBits = 24;
|
|
|
|
pfd.cDepthBits = 16;
|
|
|
|
pfd.iLayerType = PFD_MAIN_PLANE;
|
|
|
|
format = ChoosePixelFormat(*hDC, &pfd);
|
|
|
|
SetPixelFormat(*hDC, format, &pfd);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Create and enable the render contex.
|
|
|
|
*hRC = wglCreateContext(*hDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable OpenGL.
|
2012-01-10 00:46:23 +01:00
|
|
|
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC) {
|
2010-10-03 23:04:50 +02:00
|
|
|
wglMakeCurrent(NULL, NULL);
|
|
|
|
wglDeleteContext(hRC);
|
|
|
|
ReleaseDC(hWnd, hDC);
|
|
|
|
}
|
2010-10-28 22:38:27 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
} // namespace
|
2011-03-24 21:36:47 +01:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
NPError API_CALL NP_UIGetEntryPoints(NPPluginFuncs* pFuncs) {
|
2011-03-24 21:36:47 +01:00
|
|
|
pFuncs->newp = NPP_NewImpl;
|
|
|
|
pFuncs->destroy = NPP_DestroyImpl;
|
|
|
|
pFuncs->setwindow = NPP_SetWindowImpl;
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
NPError API_CALL NP_UIInitialize(NPNetscapeFuncs* pFuncs) {
|
2011-03-24 21:36:47 +01:00
|
|
|
g_uibrowser = pFuncs;
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
NPError API_CALL NP_UIShutdown(void) {
|
2011-03-24 21:36:47 +01:00
|
|
|
g_uibrowser = NULL;
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void ModifyRotation(float value) {
|
2011-03-24 21:36:47 +01:00
|
|
|
g_rotationspeed += value;
|
|
|
|
NotifyNewRotation(g_rotationspeed);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void ResetRotation() {
|
2011-03-24 21:36:47 +01:00
|
|
|
g_rotationspeed = 0.0;
|
|
|
|
NotifyNewRotation(g_rotationspeed);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif // OS_WIN
|