plugin.cpp
上传用户:q202440
上传日期:2019-04-12
资源大小:95k
文件大小:7k
源码类别:

PlugIns编程

开发平台:

C/C++

  1. /*OpenSceneGraph Firefox-plugin
  2. *License: GPL 2.0
  3. *Based on Work by Mozilla.org and OpenSceneGraph osgviewerGLUT-sample by Robert Osfield
  4. *(c) Andreas Goebel 2008
  5. */
  6. #include "plugin.h"
  7. #include <gl/gl.h> 
  8. #include <stdlib.h>
  9. #include <Psapi.h>
  10. #include <osgDB/FileUtils>
  11. /*
  12. //Global Variables
  13. osg::ref_ptr<osgViewer::Viewer> viewer;
  14. osg::observer_ptr<osgViewer::GraphicsWindow> window;
  15. char* filename;
  16. */
  17. //Functions that translate the Windows Mouse-Events to osg-events
  18. void nsPluginInstance::mousebutton( int button, int state, int x, int y )
  19. {
  20. if (window.valid())
  21. {
  22. if (state==0) window->getEventQueue()->mouseButtonPress( x, y, button+1 );
  23. else window->getEventQueue()->mouseButtonRelease( x, y, button+1 );
  24. }
  25. }
  26. void nsPluginInstance::mousemove( int x, int y )
  27. {
  28. if (window.valid())
  29. {
  30. window->getEventQueue()->mouseMotion( x, y );
  31. }
  32. }
  33. //////////////////////////////////////
  34. //
  35. // general initialization and shutdown
  36. //
  37. NPError NS_PluginInitialize()
  38. {
  39. return NPERR_NO_ERROR;
  40. }
  41. void NS_PluginShutdown()
  42. {
  43. }
  44. /////////////////////////////////////////////////////////////
  45. //
  46. // construction and destruction of our plugin instance object
  47. //
  48. nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
  49. {
  50. if(!aCreateDataStruct)
  51. return NULL;
  52. nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
  53. for (int i = 0; i<aCreateDataStruct->argc; ++i){
  54. if ( strcmp(aCreateDataStruct->argn[i] , "src") == 0)
  55. plugin->filename = aCreateDataStruct->argv[i]; //This structs holds tha value passed to the plugin with embed
  56. if ( strcmp(aCreateDataStruct->argn[i] , "width") == 0)
  57. plugin->width = atoi(aCreateDataStruct->argv[i]) ; 
  58. if ( strcmp(aCreateDataStruct->argn[i] , "height") == 0)
  59. plugin->height= atoi(aCreateDataStruct->argv[i]) ; 
  60. //MessageBox(NULL,aCreateDataStruct->argn[i],aCreateDataStruct->argv[i],MB_OK);
  61. }
  62. DWORD   cchCurDir;
  63.     LPTSTR  lpszCurDir;
  64.     TCHAR   tchBuffer[MAX_PATH + 1];
  65.     //DWORD   nSize;
  66.     lpszCurDir = tchBuffer;
  67.     cchCurDir = MAX_PATH;
  68.     
  69. HINSTANCE handle =LoadLibrary( "nposgviewer.dll" );
  70. //std::string dlldir;
  71. if (handle)
  72. {
  73. GetModuleFileName(handle, lpszCurDir, cchCurDir);
  74. //PathRemoveFileSpec(lpszCurDir);
  75. FreeLibrary(handle);
  76. //std::string dir=lpszCurDir;
  77. //int pos = dir.rfind("\");
  78. //dlldir = dir.substr(0,pos);*/
  79. char* lastSlash = strrchr(lpszCurDir, '\');
  80.     if (lastSlash)
  81.    *(lastSlash + 1) = ''; 
  82. }
  83. //std::string dlldir;
  84. //MessageBox(NULL, "Verzeichnis", lpszCurDir, MB_OK);
  85. //std::string bla = lpszCurDir; //Copy?
  86. //std::deque<std::string>& list2 = osgDB::getLibraryFilePathList();
  87. osgDB::getLibraryFilePathList().push_front(lpszCurDir);
  88. //list2.push_front(dlldir);
  89. //std::deque<std::string> list;
  90. //list.push_front(dlldir);
  91. //osgDB::appendPlatformSpecificLibraryFilePaths(list);
  92. //osgDB::
  93. //FreeLibrary(handle);
  94. return plugin;
  95. }
  96. void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
  97. {
  98. if(aPlugin)
  99. delete (nsPluginInstance *)aPlugin;
  100. }
  101. ////////////////////////////////////////
  102. //
  103. // nsPluginInstance class implementation
  104. //
  105. nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
  106. mInstance(aInstance),
  107. mInitialized(FALSE)
  108. {
  109. mhWnd = NULL; width = 400; height = 400; filename=NULL;
  110. }
  111. nsPluginInstance::~nsPluginInstance()
  112. {
  113. }
  114. static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM);
  115. static WNDPROC lpOldProc = NULL;
  116. void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC); 
  117. void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);
  118. /*
  119. HDC hDC;
  120. HGLRC hRC;
  121. */
  122. NPBool nsPluginInstance::init(NPWindow* aWindow)
  123. {
  124. if(aWindow == NULL)
  125. return FALSE;
  126. mhWnd = (HWND)aWindow->window;
  127. if(mhWnd == NULL)
  128. return FALSE;
  129. // subclass window so we can intercept window messages and
  130. // do our drawing to it
  131. lpOldProc = SubclassWindow(mhWnd, (WNDPROC)PluginWinProc);
  132. // associate window with our nsPluginInstance object so we can access 
  133. // it in the window procedure
  134. SetWindowLong(mhWnd, GWL_USERDATA, (LONG)this);
  135. //we add EnableOpenGL and SetTimer
  136. EnableOpenGL( mhWnd, &hDC, &hRC );
  137. SetTimer(mhWnd, 0,  1,  (TIMERPROC) NULL); // no timer callback
  138. //osg-related code:
  139. //Node-file is read via http by libcurl:
  140. std::string s = filename;
  141. osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(s);
  142. viewer = new osgViewer::Viewer;
  143. float w = (float)width; float h = (float)height; w*=1.1; h*=1.1; width=(int)w; height=(int)h;
  144. window = viewer->setUpViewerAsEmbeddedInWindow(0,0,width,height);
  145. viewer->setSceneData(loadedModel.get());
  146. osg::ref_ptr<osgGA::TrackballManipulator> tbm = new osgGA::TrackballManipulator();
  147. viewer->setCameraManipulator(tbm.get() );
  148. osg::ref_ptr<osgViewer::StatsHandler> stats = new osgViewer::StatsHandler();
  149. viewer->addEventHandler(stats);
  150. viewer->realize();
  151. mInitialized = TRUE;
  152. return TRUE;
  153. }
  154. void nsPluginInstance::shut()
  155. {
  156. // We add DisableOpenGL
  157. DisableOpenGL( mhWnd, hDC, hRC );
  158. // subclass it back
  159. SubclassWindow(mhWnd, lpOldProc);
  160. mhWnd = NULL;
  161. mInitialized = FALSE;
  162. }
  163. NPBool nsPluginInstance::isInitialized()
  164. {
  165. return mInitialized;
  166. }
  167. const char * nsPluginInstance::getVersion()
  168. {
  169. return NPN_UserAgent(mInstance);
  170. }
  171. static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  172. {
  173. nsPluginInstance* inst = (nsPluginInstance*)GetWindowLong(hWnd, GWL_USERDATA);
  174. HDC hDC = inst->hDC;
  175. HGLRC hRC = inst->hRC;
  176. switch (msg) {
  177. case WM_PAINT:
  178. {
  179. wglMakeCurrent( hDC, hRC );
  180. if (inst->viewer.valid()) inst->viewer->frame();
  181. SwapBuffers( hDC );
  182. }
  183. break;
  184. case WM_TIMER:
  185. PostMessage( hWnd, WM_PAINT, NULL, NULL );
  186. break;    
  187. case WM_LBUTTONDOWN:
  188. {
  189. short xp = LOWORD(lParam);
  190. short yp = HIWORD(lParam);
  191. inst->mousebutton(0,0,xp,yp);
  192. break;
  193. }
  194. case WM_LBUTTONUP:
  195. {
  196. short xp = LOWORD(lParam);
  197. short yp = HIWORD(lParam);
  198. inst->mousebutton(0,1,xp,yp);
  199. break;
  200. }
  201. case WM_RBUTTONDOWN:
  202. {
  203. short xp = LOWORD(lParam);
  204. short yp = HIWORD(lParam);
  205. inst->mousebutton(2,0,xp,yp);
  206. }
  207. break;
  208. case WM_RBUTTONUP:
  209. {
  210. short xp = LOWORD(lParam);
  211. short yp = HIWORD(lParam);
  212. inst->mousebutton(2,1,xp,yp);
  213. break;
  214. }
  215. case WM_MOUSEMOVE:
  216. {
  217. short xp = LOWORD(lParam);
  218. short yp = HIWORD(lParam);
  219. inst->mousemove(xp,yp);
  220. break;
  221. }
  222. default:
  223. break;
  224. }
  225. return DefWindowProc(hWnd, msg, wParam, lParam);
  226. }
  227. void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
  228. {
  229. PIXELFORMATDESCRIPTOR pfd;
  230. int format;
  231. // get the device context (DC)
  232. *hDC = GetDC( hWnd ); // set the pixel format for the DC
  233. ZeroMemory( &pfd, sizeof( pfd ) );
  234. pfd.nSize = sizeof( pfd );
  235. pfd.nVersion = 1; 
  236. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | 
  237. PFD_DOUBLEBUFFER;
  238. pfd.iPixelType = PFD_TYPE_RGBA;
  239. pfd.cColorBits = 24;
  240. pfd.cDepthBits = 16;
  241. pfd.iLayerType = PFD_MAIN_PLANE;
  242. format = ChoosePixelFormat( *hDC, &pfd );
  243. SetPixelFormat( *hDC, format, &pfd );
  244. // create and enable the render context (RC)
  245. *hRC = wglCreateContext( *hDC ); 
  246. wglMakeCurrent( *hDC, *hRC );
  247. }
  248. // Disable OpenGL
  249. void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
  250. {
  251. wglMakeCurrent( NULL, NULL );
  252. wglDeleteContext( hRC );
  253. ReleaseDC( hWnd, hDC );
  254. }