- clgecko_t *instance = callbackData;\r
- const unsigned char *data;\r
- if( instance->browser ) {\r
- // TODO: OSGK only supports BGRA right now\r
- TIMING_TIMESTATEMENT(data = osgk_browser_lock_data( instance->browser, NULL ));\r
- R_UpdateTexture( texture, data, 0, 0, instance->width, instance->height );\r
- osgk_browser_unlock_data( instance->browser, data );\r
- }\r
-}\r
-\r
-static void cl_gecko_linktexture( clgecko_t *instance ) {\r
- // TODO: assert that instance->texture == NULL\r
- instance->texture = R_LoadTexture2D( cl_geckotexturepool, instance->name, \r
- instance->texWidth, instance->texHeight, NULL, TEXTYPE_BGRA, TEXF_ALPHA | TEXF_PERSISTENT, NULL );\r
- R_MakeTextureDynamic( instance->texture, cl_gecko_updatecallback, instance );\r
- CL_LinkDynTexture( instance->name, instance->texture );\r
-}\r
-\r
-static void cl_gecko_unlinktexture( clgecko_t *instance ) {\r
- if( instance->texture ) {\r
- CL_UnlinkDynTexture( instance->name );\r
- R_FreeTexture( instance->texture );\r
- instance->texture = NULL;\r
- }\r
-}\r
-\r
-void CL_Gecko_Resize( clgecko_t *instance, int width, int height ) {\r
- int newWidth, newHeight;\r
-\r
- // early out if bad parameters are passed (no resize to a texture size bigger than the original texture size)\r
- if( !instance || !instance->browser) {\r
- return;\r
- }\r
-\r
- newWidth = CeilPowerOf2( width );\r
- newHeight = CeilPowerOf2( height );\r
- if ((newWidth != instance->texWidth) || (newHeight != instance->texHeight))\r
- {\r
- cl_gecko_unlinktexture( instance );\r
- instance->texWidth = newWidth;\r
- instance->texHeight = newHeight;\r
- cl_gecko_linktexture( instance );\r
- }\r
- else\r
- {\r
- /* The gecko area will only cover a part of the texture; to avoid\r
- 'old' pixels bleeding in at the border clear the texture. */\r
- R_ClearTexture( instance->texture );\r
- }\r
-\r
- osgk_browser_resize( instance->browser, width, height);\r
- instance->width = width;\r
- instance->height = height;\r
-}\r
-\r
-void CL_Gecko_GetTextureExtent( clgecko_t *instance, float* pwidth, float* pheight )\r
-{\r
- if( !instance || !instance->browser ) {\r
- return;\r
- }\r
-\r
- *pwidth = (float)instance->width / instance->texWidth;\r
- *pheight = (float)instance->height / instance->texHeight;\r
-}\r
-\r
-\r
-clgecko_t * CL_Gecko_CreateBrowser( const char *name ) {\r
- clgecko_t *instance;\r
-\r
- if (!osgk_dll) return NULL;\r
-\r
- // TODO: verify that we dont use a name twice\r
- instance = cl_gecko_findunusedinstance();\r
- // TODO: assert != NULL\r
- \r
- if( cl_geckoembedding == NULL ) {\r
- char profile_path [MAX_OSPATH];\r
- OSGK_GeckoResult grc;\r
- OSGK_EmbeddingOptions *options;\r
-\r
- if( developer.integer > 0 ) {\r
- Con_Printf( "CL_Gecko_CreateBrowser: setting up gecko embedding\n" );\r
- }\r
-\r
- options = osgk_embedding_options_create();\r
- osgk_embedding_options_add_search_path( options, "./xulrunner/" );\r
- dpsnprintf (profile_path, sizeof (profile_path), "%s/xulrunner_profile/", fs_gamedir);\r
- osgk_embedding_options_set_profile_dir( options, profile_path, 0 );\r
- cl_geckoembedding = osgk_embedding_create_with_options( options, &grc );\r
- osgk_release( options );\r
- \r
- if( cl_geckoembedding == NULL ) {\r
- Con_Printf( "CL_Gecko_CreateBrowser: Couldn't retrieve gecko embedding object (%.8x)!\n", grc );\r
- return NULL;\r
- } else if( developer.integer > 0 ) {\r
- Con_Printf( "CL_Gecko_CreateBrowser: Embedding set up correctly\n" );\r
- }\r
- }\r
-\r
- instance->active = true;\r
- strlcpy( instance->name, name, sizeof( instance->name ) );\r
- instance->browser = osgk_browser_create( cl_geckoembedding, DEFAULT_GECKO_SIZE, DEFAULT_GECKO_SIZE );\r
- if( instance->browser == NULL ) {\r
- Con_Printf( "CL_Gecko_CreateBrowser: Browser object creation failed!\n" );\r
- }\r
- // TODO: assert != NULL\r
-\r
- instance->width = instance->texWidth = DEFAULT_GECKO_SIZE;\r
- instance->height = instance->texHeight = DEFAULT_GECKO_SIZE;\r
- cl_gecko_linktexture( instance );\r
-\r
- return instance;\r
-}\r
-\r
-void CL_Gecko_DestroyBrowser( clgecko_t *instance ) {\r
- if( !instance || !instance->active ) {\r
- return;\r
- }\r
-\r
- instance->active = false;\r
- cl_gecko_unlinktexture( instance );\r
-\r
- osgk_release( instance->browser );\r
- instance->browser = NULL;\r
-}\r
-\r
-void CL_Gecko_Frame( void ) {\r
- int i;\r
- // FIXME: track cl_numgeckoinstances to avoid scanning the entire array?\r
- for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
- clgecko_t *instance = &cl_geckoinstances[ i ];\r
- if( instance->active ) {\r
- if( instance->browser && osgk_browser_query_dirty( instance->browser ) == 1 ) {\r
- R_MarkDirtyTexture( instance->texture );\r
- }\r
- }\r
- }\r
-}\r
-\r
-static void cl_gecko_start( void )\r
-{\r
- int i;\r
- cl_geckotexturepool = R_AllocTexturePool();\r
-\r
- // recreate textures on module start\r
- for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
- clgecko_t *instance = &cl_geckoinstances[ i ];\r
- if( instance->active ) {\r
- cl_gecko_linktexture( instance );\r
- }\r
- }\r
-}\r
-\r
-static void cl_gecko_shutdown( void )\r
-{\r
- int i;\r
- for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
- clgecko_t *instance = &cl_geckoinstances[ i ];\r
- if( instance->active ) {\r
- cl_gecko_unlinktexture( instance );\r
- }\r
- }\r
- R_FreeTexturePool( &cl_geckotexturepool );\r
-}\r
-\r
-static void cl_gecko_newmap( void )\r
-{\r
- // DO NOTHING\r
-}\r
-\r
-void CL_Gecko_Shutdown( void ) {\r
- int i;\r
- for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
- clgecko_t *instance = &cl_geckoinstances[ i ];\r
- if( instance->active ) {\r
- cl_gecko_unlinktexture( instance );\r
- } \r
- }\r
-\r
- if (cl_geckoembedding != NULL)\r
- {\r
- osgk_release( cl_geckoembedding );\r
- cl_geckoembedding = NULL;\r
- }\r
-\r
- if (osgk_dll != NULL)\r
- {\r
- Sys_UnloadLibrary (&osgk_dll);\r
- }\r
-}\r
-\r
-static void cl_gecko_create_f( void ) {\r
- char name[MAX_QPATH];\r
-\r
- if (Cmd_Argc() != 2)\r
- {\r
- Con_Print("usage: gecko_create <name>\npcreates a browser (full texture path " CLGECKOPREFIX "<name>)\n");\r
- return;\r
- }\r
-\r
- // TODO: use snprintf instead\r
- sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
- CL_Gecko_CreateBrowser( name );\r
-}\r
-\r
-static void cl_gecko_destroy_f( void ) {\r
- char name[MAX_QPATH];\r
-\r
- if (Cmd_Argc() != 2)\r
- {\r
- Con_Print("usage: gecko_destroy <name>\ndestroys a browser\n");\r
- return;\r
- }\r
-\r
- // TODO: use snprintf instead\r
- sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
- CL_Gecko_DestroyBrowser( CL_Gecko_FindBrowser( name ) );\r
-}\r
-\r
-static void cl_gecko_navigate_f( void ) {\r
- char name[MAX_QPATH];\r
- const char *URI;\r
-\r
- if (Cmd_Argc() != 3)\r
- {\r
- Con_Print("usage: gecko_navigate <name> <URI>\nnavigates to a certain URI\n");\r
- return;\r
- }\r
-\r
- // TODO: use snprintf instead\r
- sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
- URI = Cmd_Argv( 2 );\r
- CL_Gecko_NavigateToURI( CL_Gecko_FindBrowser( name ), URI );\r
-}\r
-\r
-static void cl_gecko_injecttext_f( void ) {\r
- char name[MAX_QPATH];\r
- const char *text;\r
- clgecko_t *instance;\r
- const char *p;\r
-\r
- if (Cmd_Argc() < 3)\r
- {\r
- Con_Print("usage: gecko_injecttext <name> <text>\ninjects a certain text into the browser\n");\r
- return;\r
- }\r
-\r
- // TODO: use snprintf instead\r
- sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
- instance = CL_Gecko_FindBrowser( name );\r
- if( !instance ) {\r
- Con_Printf( "cl_gecko_injecttext_f: gecko instance '%s' couldn't be found!\n", name );\r
- return;\r
- }\r
-\r
- text = Cmd_Argv( 2 );\r
-\r
- for( p = text ; *p ; p++ ) {\r
- unsigned key = *p;\r
- switch( key ) {\r
- case ' ':\r
- key = K_SPACE;\r
- break;\r
- case '\\':\r
- key = *++p;\r
- switch( key ) {\r
- case 'n':\r
- key = K_ENTER;\r
- break;\r
- case '\0':\r
- --p;\r
- key = '\\';\r
- break;\r
- }\r
- break;\r
- }\r
-\r
- CL_Gecko_Event_Key( instance, key, CLG_BET_PRESS );\r
- }\r
-}\r
-\r
-static void gl_gecko_movecursor_f( void ) {\r
- char name[MAX_QPATH];\r
- float x, y;\r
-\r
- if (Cmd_Argc() != 4)\r
- {\r
- Con_Print("usage: gecko_movecursor <name> <x> <y>\nmove the cursor to a certain position\n");\r
- return;\r
- }\r
-\r
- // TODO: use snprintf instead\r
- sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
- x = atof( Cmd_Argv( 2 ) );\r
- y = atof( Cmd_Argv( 3 ) );\r
-\r
- CL_Gecko_Event_CursorMove( CL_Gecko_FindBrowser( name ), x, y );\r
-}\r
-\r
-#undef osgk_addref\r
-#undef osgk_release\r
-\r
-static const dllfunction_t osgkFuncs[] =\r
-{\r
- {"osgk_addref", (void **) &osgk_addref},\r
- {"osgk_release", (void **) &osgk_release},\r
- {"osgk_embedding_create2", (void **) &osgk_embedding_create2},\r
- {"osgk_embedding_options_create", (void **) &osgk_embedding_options_create},\r
- {"osgk_embedding_options_add_search_path", (void **) &osgk_embedding_options_add_search_path},\r
- {"osgk_embedding_options_set_profile_dir", (void **) &osgk_embedding_options_set_profile_dir},\r
- {"osgk_browser_create", (void **) &osgk_browser_create},\r
- {"osgk_browser_query_dirty", (void **) &osgk_browser_query_dirty},\r
- {"osgk_browser_navigate", (void **) &osgk_browser_navigate},\r
- {"osgk_browser_lock_data", (void **) &osgk_browser_lock_data},\r
- {"osgk_browser_unlock_data", (void **) &osgk_browser_unlock_data},\r
- {"osgk_browser_resize", (void **) &osgk_browser_resize},\r
- {"osgk_browser_event_mouse_move", (void **) &osgk_browser_event_mouse_move},\r
- {"osgk_browser_event_mouse_button", (void **) &osgk_browser_event_mouse_button},\r
- {"osgk_browser_event_mouse_wheel", (void **) &osgk_browser_event_mouse_wheel},\r
- {"osgk_browser_event_key", (void **) &osgk_browser_event_key},\r
- {NULL, NULL}\r
-};\r
-\r
-void CL_Gecko_Init( void )\r
-{\r
- const char* dllnames [] =\r
- {\r
- #if defined(WIN64)\r
- "OffscreenGecko64.dll",\r
- #elif defined(WIN32)\r
- "OffscreenGecko.dll",\r
- #elif defined(MACOSX)\r
- "OffscreenGecko.dylib",\r
- #else\r
- "OffscreenGecko.so",\r
- #endif\r
- NULL\r
- };\r
- \r
- if (!osgk_dll)\r
- {\r
- if (! Sys_LoadLibrary (dllnames, &osgk_dll, osgkFuncs))\r
- {\r
- Con_Printf ("Could not load OffscreenGecko, Gecko support unavailable\n");\r
- }\r
- }\r
-\r
- Cmd_AddCommand( "gecko_create", cl_gecko_create_f, "Create a gecko browser instance" );\r
- Cmd_AddCommand( "gecko_destroy", cl_gecko_destroy_f, "Destroy a gecko browser instance" );\r
- Cmd_AddCommand( "gecko_navigate", cl_gecko_navigate_f, "Navigate a gecko browser to a URI" );\r
- Cmd_AddCommand( "gecko_injecttext", cl_gecko_injecttext_f, "Injects text into a browser" );\r
- Cmd_AddCommand( "gecko_movecursor", gl_gecko_movecursor_f, "Move the cursor to a certain position" );\r
-\r
- R_RegisterModule( "CL_Gecko", cl_gecko_start, cl_gecko_shutdown, cl_gecko_newmap );\r
-}\r
-\r
-void CL_Gecko_NavigateToURI( clgecko_t *instance, const char *URI ) {\r
- if( !instance || !instance->browser ) {\r
- return;\r
- }\r
-\r
- if( instance->active ) {\r
- osgk_browser_navigate( instance->browser, URI );\r
- }\r
-}\r
-\r
-void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y ) {\r
- // TODO: assert x, y \in [0.0, 1.0]\r
- int mappedx, mappedy;\r
-\r
- if( !instance || !instance->browser ) {\r
- return;\r
- }\r
-\r
- mappedx = x * instance->width;\r
- mappedy = y * instance->height;\r
- osgk_browser_event_mouse_move( instance->browser, mappedx, mappedy );\r
-}\r
-\r
-typedef struct geckokeymapping_s {\r
- keynum_t keycode;\r
- unsigned int geckokeycode;\r
-} geckokeymapping_t;\r
-\r
-static geckokeymapping_t geckokeymappingtable[] = {\r
- { K_BACKSPACE, OSGKKey_Backspace },\r
- { K_TAB, OSGKKey_Tab },\r
- { K_ENTER, OSGKKey_Return },\r
- { K_SHIFT, OSGKKey_Shift },\r
- { K_CTRL, OSGKKey_Control },\r
- { K_ALT, OSGKKey_Alt },\r
- { K_CAPSLOCK, OSGKKey_CapsLock },\r
- { K_ESCAPE, OSGKKey_Escape },\r
- { K_SPACE, OSGKKey_Space },\r
- { K_PGUP, OSGKKey_PageUp },\r
- { K_PGDN, OSGKKey_PageDown },\r
- { K_END, OSGKKey_End },\r
- { K_HOME, OSGKKey_Home },\r
- { K_LEFTARROW, OSGKKey_Left },\r
- { K_UPARROW, OSGKKey_Up },\r
- { K_RIGHTARROW, OSGKKey_Right },\r
- { K_DOWNARROW, OSGKKey_Down },\r
- { K_INS, OSGKKey_Insert },\r
- { K_DEL, OSGKKey_Delete },\r
- { K_F1, OSGKKey_F1 },\r
- { K_F2, OSGKKey_F2 },\r
- { K_F3, OSGKKey_F3 },\r
- { K_F4, OSGKKey_F4 },\r
- { K_F5, OSGKKey_F5 },\r
- { K_F6, OSGKKey_F6 },\r
- { K_F7, OSGKKey_F7 },\r
- { K_F8, OSGKKey_F8 },\r
- { K_F9, OSGKKey_F9 },\r
- { K_F10, OSGKKey_F10 },\r
- { K_F11, OSGKKey_F11 },\r
- { K_F12, OSGKKey_F12 },\r
- { K_NUMLOCK, OSGKKey_NumLock },\r
- { K_SCROLLOCK, OSGKKey_ScrollLock }\r
-};\r
-\r
-qboolean CL_Gecko_Event_Key( clgecko_t *instance, keynum_t key, clgecko_buttoneventtype_t eventtype ) {\r
- if( !instance || !instance->browser ) {\r
- return false;\r
- }\r
-\r
- // determine whether its a keyboard event\r
- if( key < K_OTHERDEVICESBEGIN ) {\r
-\r
- OSGK_KeyboardEventType mappedtype;\r
- unsigned int mappedkey = key;\r
- \r
- unsigned int i;\r
- // yes! then convert it if necessary!\r
- for( i = 0 ; i < sizeof( geckokeymappingtable ) / sizeof( *geckokeymappingtable ) ; i++ ) {\r
- const geckokeymapping_t * const mapping = &geckokeymappingtable[ i ];\r
- if( key == mapping->keycode ) {\r
- mappedkey = mapping->geckokeycode;\r
- break;\r
- }\r
- }\r
-\r
- // convert the eventtype\r
- // map the type\r
- switch( eventtype ) {\r
- case CLG_BET_DOWN:\r
- mappedtype = keDown;\r
- break;\r
- case CLG_BET_UP:\r
- mappedtype = keUp;\r
- break;\r
- case CLG_BET_DOUBLECLICK:\r
- // TODO: error message\r
- break;\r
- case CLG_BET_PRESS:\r
- mappedtype = kePress;\r
- }\r
-\r
- return osgk_browser_event_key( instance->browser, mappedkey, mappedtype ) != 0;\r
- } else if( K_MOUSE1 <= key && key <= K_MOUSE3 ) {\r
- OSGK_MouseButtonEventType mappedtype;\r
- OSGK_MouseButton mappedbutton;\r
-\r
- mappedbutton = (OSGK_MouseButton) (mbLeft + (key - K_MOUSE1));\r
-\r
- switch( eventtype ) {\r
- case CLG_BET_DOWN:\r
- mappedtype = meDown;\r
- break;\r
- case CLG_BET_UP:\r
- mappedtype = meUp;\r
- break;\r
- case CLG_BET_DOUBLECLICK:\r
- mappedtype = meDoubleClick;\r
- break;\r
- case CLG_BET_PRESS:\r
- // hihi, hacky hacky\r
- osgk_browser_event_mouse_button( instance->browser, mappedbutton, meDown );\r
- mappedtype = meUp;\r
- break;\r
- }\r
-\r
- osgk_browser_event_mouse_button( instance->browser, mappedbutton, mappedtype );\r
- return true;\r
- } else if( K_MWHEELUP <= key && key <= K_MWHEELDOWN ) {\r
- if( eventtype == CLG_BET_DOWN )\r
- osgk_browser_event_mouse_wheel( instance->browser, \r
- waVertical, (key == K_MWHEELUP) ? wdNegative : wdPositive );\r
- return true;\r
- }\r
- // TODO: error?\r
- return false;\r
-}\r
+ clgecko_t *instance = (clgecko_t *) callbackData;
+ const unsigned char *data;
+ if( instance->browser ) {
+ // TODO: OSGK only supports BGRA right now
+ TIMING_TIMESTATEMENT(data = osgk_browser_lock_data( instance->browser, NULL ));
+ R_UpdateTexture( texture, data, 0, 0, 0, instance->width, instance->height, 1 );
+ osgk_browser_unlock_data( instance->browser, data );
+ }
+}
+
+static void cl_gecko_linktexture( clgecko_t *instance ) {
+ // TODO: assert that instance->texture == NULL
+ instance->texture = R_LoadTexture2D( cl_geckotexturepool, instance->name,
+ instance->texWidth, instance->texHeight, NULL, TEXTYPE_BGRA, TEXF_ALPHA | TEXF_PERSISTENT, -1, NULL );
+ R_MakeTextureDynamic( instance->texture, cl_gecko_updatecallback, instance );
+ CL_LinkDynTexture( instance->name, instance->texture );
+}
+
+static void cl_gecko_unlinktexture( clgecko_t *instance ) {
+ if( instance->texture ) {
+ CL_UnlinkDynTexture( instance->name );
+ R_FreeTexture( instance->texture );
+ instance->texture = NULL;
+ }
+}
+
+void CL_Gecko_Resize( clgecko_t *instance, int width, int height ) {
+ int newWidth, newHeight;
+
+ // early out if bad parameters are passed (no resize to a texture size bigger than the original texture size)
+ if( !instance || !instance->browser) {
+ return;
+ }
+
+ newWidth = CeilPowerOf2( width );
+ newHeight = CeilPowerOf2( height );
+ if ((newWidth != instance->texWidth) || (newHeight != instance->texHeight))
+ {
+ cl_gecko_unlinktexture( instance );
+ instance->texWidth = newWidth;
+ instance->texHeight = newHeight;
+ cl_gecko_linktexture( instance );
+ }
+ else
+ {
+ /* The gecko area will only cover a part of the texture; to avoid
+ 'old' pixels bleeding in at the border clear the texture. */
+ R_ClearTexture( instance->texture );
+ }
+
+ osgk_browser_resize( instance->browser, width, height);
+ instance->width = width;
+ instance->height = height;
+}
+
+void CL_Gecko_GetTextureExtent( clgecko_t *instance, float* pwidth, float* pheight )
+{
+ if( !instance || !instance->browser ) {
+ return;
+ }
+
+ *pwidth = (float)instance->width / instance->texWidth;
+ *pheight = (float)instance->height / instance->texHeight;
+}
+
+static OSGK_ScriptResult dpGlobal_create (OSGK_ScriptObjectCreateParams* params,
+ void** objTag)
+{
+ if (!osgk_browser_get_user_data (params->browser, USERDATAKEY_CL_GECKO_T, objTag))
+ return srFailed;
+ return srSuccess;
+}
+
+static OSGK_ScriptResult dpGlobal_query (void* objTag, void* methTag,
+ size_t numParams,
+ OSGK_ScriptVariant** params,
+ OSGK_ScriptVariant** returnVal)
+{
+ clgecko_t *instance = (clgecko_t *) objTag;
+ OSGK_ScriptVariant* strVal;
+ OSGK_ScriptResult result = srFailed;
+ prvm_prog_t * saveProg;
+
+ /* Can happen when created from console */
+ if (instance->ownerProg < 0) return srFailed;
+
+ /* Require exactly one param, for now */
+ if (numParams != 1) return srFailed;
+
+ strVal = osgk_variant_convert (params[0], svtString);
+ if (strVal == 0) return srFailed;
+
+ saveProg = prog;
+ PRVM_SetProg(instance->ownerProg);
+
+ if (PRVM_clientfunction(Gecko_Query))
+ {
+ OSGK_String* paramStr, *resultStr;
+
+ if (!osgk_variant_get_string (strVal, ¶mStr)) return srFailed;
+ PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString (instance->name);
+ PRVM_G_INT(OFS_PARM1) = PRVM_SetTempString (osgk_string_get (paramStr));
+ PRVM_ExecuteProgram(PRVM_clientfunction(Gecko_Query),"Gecko_Query() required");
+ resultStr = osgk_string_create (PRVM_G_STRING (OFS_RETURN));
+ *returnVal = osgk_variant_create_string (cl_geckoembedding, resultStr);
+ osgk_release (resultStr);
+
+ result = srSuccess;
+ }
+
+ prog = saveProg;
+
+ return result;
+}
+
+#if defined(_WIN64)
+# define XULRUNNER_DIR_SUFFIX "win64"
+#elif defined(WIN32)
+# define XULRUNNER_DIR_SUFFIX "win32"
+#elif defined(DP_OS_STR) && defined(DP_ARCH_STR)
+# define XULRUNNER_DIR_SUFFIX DP_OS_STR "-" DP_ARCH_STR
+#endif
+
+static qboolean CL_Gecko_Embedding_Init (void)
+{
+ char profile_path [MAX_OSPATH];
+ OSGK_GeckoResult grc;
+ OSGK_EmbeddingOptions *options;
+ OSGK_ScriptObjectTemplate* dpGlobalTemplate;
+
+ if (!osgk_dll) return false;
+
+ if( cl_geckoembedding != NULL ) return true;
+
+ Con_DPrintf( "CL_Gecko_Embedding_Init: setting up gecko embedding\n" );
+
+ options = osgk_embedding_options_create();
+#ifdef XULRUNNER_DIR_SUFFIX
+ osgk_embedding_options_add_search_path( options, "./xulrunner-" XULRUNNER_DIR_SUFFIX "/" );
+#endif
+ osgk_embedding_options_add_search_path( options, "./xulrunner/" );
+ dpsnprintf (profile_path, sizeof (profile_path), "%s/xulrunner_profile/", fs_gamedir);
+ osgk_embedding_options_set_profile_dir( options, profile_path, 0 );
+ cl_geckoembedding = osgk_embedding_create_with_options( options, &grc );
+ osgk_release( options );
+
+ if( cl_geckoembedding == NULL ) {
+ Con_Printf( "CL_Gecko_Embedding_Init: Couldn't retrieve gecko embedding object (%.8x)!\n", grc );
+ return false;
+ }
+
+ Con_DPrintf( "CL_Gecko_Embedding_Init: Embedding set up correctly\n" );
+
+ dpGlobalTemplate = osgk_sot_create( cl_geckoembedding, dpGlobal_create, NULL, NULL );
+
+ osgk_sot_add_function (dpGlobalTemplate, "query", 0, dpGlobal_query);
+
+ osgk_sot_register (dpGlobalTemplate, cl_geckoembedding, "Darkplaces", 0);
+ osgk_release( dpGlobalTemplate );
+
+ return true;
+}
+
+clgecko_t * CL_Gecko_CreateBrowser( const char *name, int ownerProg ) {
+ clgecko_t *instance;
+
+ if (!CL_Gecko_Embedding_Init ()) return NULL;
+
+ // TODO: verify that we dont use a name twice
+ instance = cl_gecko_findunusedinstance();
+ // TODO: assert != NULL
+
+ instance->active = true;
+ strlcpy( instance->name, name, sizeof( instance->name ) );
+ instance->browser = osgk_browser_create( cl_geckoembedding, DEFAULT_GECKO_SIZE, DEFAULT_GECKO_SIZE );
+ if( instance->browser == NULL ) {
+ Con_Printf( "CL_Gecko_CreateBrowser: Browser object creation failed!\n" );
+ }
+ // TODO: assert != NULL
+ osgk_browser_set_user_data (instance->browser, USERDATAKEY_CL_GECKO_T,
+ instance, 0);
+ instance->ownerProg = ownerProg;
+
+ instance->width = instance->texWidth = DEFAULT_GECKO_SIZE;
+ instance->height = instance->texHeight = DEFAULT_GECKO_SIZE;
+ cl_gecko_linktexture( instance );
+
+ return instance;
+}
+
+void CL_Gecko_DestroyBrowser( clgecko_t *instance ) {
+ if( !instance || !instance->active ) {
+ return;
+ }
+
+ instance->active = false;
+ cl_gecko_unlinktexture( instance );
+
+ osgk_release( instance->browser );
+ instance->browser = NULL;
+}
+
+void CL_Gecko_Frame( void ) {
+ int i;
+ // FIXME: track cl_numgeckoinstances to avoid scanning the entire array?
+ for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
+ clgecko_t *instance = &cl_geckoinstances[ i ];
+ if( instance->active ) {
+ if( instance->browser && osgk_browser_query_dirty( instance->browser ) == 1 ) {
+ R_MarkDirtyTexture( instance->texture );
+ }
+ }
+ }
+}
+
+static void cl_gecko_start( void )
+{
+ int i;
+ cl_geckotexturepool = R_AllocTexturePool();
+
+ // recreate textures on module start
+ for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
+ clgecko_t *instance = &cl_geckoinstances[ i ];
+ if( instance->active ) {
+ cl_gecko_linktexture( instance );
+ }
+ }
+}
+
+static void cl_gecko_shutdown( void )
+{
+ int i;
+ for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
+ clgecko_t *instance = &cl_geckoinstances[ i ];
+ if( instance->active ) {
+ cl_gecko_unlinktexture( instance );
+ }
+ }
+ R_FreeTexturePool( &cl_geckotexturepool );
+}
+
+static void cl_gecko_newmap( void )
+{
+ // DO NOTHING
+}
+
+void CL_Gecko_Shutdown( void ) {
+ int i;
+ for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
+ clgecko_t *instance = &cl_geckoinstances[ i ];
+ if( instance->active ) {
+ cl_gecko_unlinktexture( instance );
+ }
+ }
+
+ if (cl_geckoembedding != NULL)
+ {
+ osgk_release( cl_geckoembedding );
+ cl_geckoembedding = NULL;
+ }
+
+ if (osgk_dll != NULL)
+ {
+ Sys_UnloadLibrary (&osgk_dll);
+ }
+}
+
+static void cl_gecko_create_f( void ) {
+ char name[MAX_QPATH];
+
+ if (Cmd_Argc() != 2)
+ {
+ Con_Print("usage: gecko_create <name>\npcreates a browser (full texture path " CLGECKOPREFIX "<name>)\n");
+ return;
+ }
+
+ dpsnprintf(name, sizeof(name), CLGECKOPREFIX "%s", Cmd_Argv(1));
+ CL_Gecko_CreateBrowser( name, -1 );
+}
+
+static void cl_gecko_destroy_f( void ) {
+ char name[MAX_QPATH];
+
+ if (Cmd_Argc() != 2)
+ {
+ Con_Print("usage: gecko_destroy <name>\ndestroys a browser\n");
+ return;
+ }
+
+ dpsnprintf(name, sizeof(name), CLGECKOPREFIX "%s", Cmd_Argv(1));
+ CL_Gecko_DestroyBrowser( CL_Gecko_FindBrowser( name ) );
+}
+
+static void cl_gecko_navigate_f( void ) {
+ char name[MAX_QPATH];
+ const char *URI;
+
+ if (Cmd_Argc() != 3)
+ {
+ Con_Print("usage: gecko_navigate <name> <URI>\nnavigates to a certain URI\n");
+ return;
+ }
+
+ dpsnprintf(name, sizeof(name), CLGECKOPREFIX "%s", Cmd_Argv(1));
+ URI = Cmd_Argv( 2 );
+ CL_Gecko_NavigateToURI( CL_Gecko_FindBrowser( name ), URI );
+}
+
+static void cl_gecko_injecttext_f( void ) {
+ char name[MAX_QPATH];
+ const char *text;
+ clgecko_t *instance;
+ const char *p;
+
+ if (Cmd_Argc() < 3)
+ {
+ Con_Print("usage: gecko_injecttext <name> <text>\ninjects a certain text into the browser\n");
+ return;
+ }
+
+ dpsnprintf(name, sizeof(name), CLGECKOPREFIX "%s", Cmd_Argv(1));
+ instance = CL_Gecko_FindBrowser( name );
+ if( !instance ) {
+ Con_Printf( "cl_gecko_injecttext_f: gecko instance '%s' couldn't be found!\n", name );
+ return;
+ }
+
+ text = Cmd_Argv( 2 );
+
+ for( p = text ; *p ; p++ ) {
+ unsigned key = *p;
+ switch( key ) {
+ case ' ':
+ key = K_SPACE;
+ break;
+ case '\\':
+ key = *++p;
+ switch( key ) {
+ case 'n':
+ key = K_ENTER;
+ break;
+ case '\0':
+ --p;
+ key = '\\';
+ break;
+ }
+ break;
+ }
+
+ CL_Gecko_Event_Key( instance, (keynum_t) key, CLG_BET_PRESS );
+ }
+}
+
+static void gl_gecko_movecursor_f( void ) {
+ char name[MAX_QPATH];
+ float x, y;
+
+ if (Cmd_Argc() != 4)
+ {
+ Con_Print("usage: gecko_movecursor <name> <x> <y>\nmove the cursor to a certain position\n");
+ return;
+ }
+
+ dpsnprintf(name, sizeof(name), CLGECKOPREFIX "%s", Cmd_Argv(1));
+ x = atof( Cmd_Argv( 2 ) );
+ y = atof( Cmd_Argv( 3 ) );
+
+ CL_Gecko_Event_CursorMove( CL_Gecko_FindBrowser( name ), x, y );
+}
+
+#undef osgk_addref
+#undef osgk_release
+
+static const dllfunction_t osgkFuncs[] =
+{
+ {"osgk_addref", (void **) &osgk_addref},
+ {"osgk_release", (void **) &osgk_release},
+ {"osgk_embedding_create2", (void **) &osgk_embedding_create2},
+ {"osgk_embedding_options_create", (void **) &osgk_embedding_options_create},
+ {"osgk_embedding_options_add_search_path", (void **) &osgk_embedding_options_add_search_path},
+ {"osgk_embedding_options_set_profile_dir", (void **) &osgk_embedding_options_set_profile_dir},
+ {"osgk_browser_create", (void **) &osgk_browser_create},
+ {"osgk_browser_query_dirty", (void **) &osgk_browser_query_dirty},
+ {"osgk_browser_navigate", (void **) &osgk_browser_navigate},
+ {"osgk_browser_lock_data", (void **) &osgk_browser_lock_data},
+ {"osgk_browser_unlock_data", (void **) &osgk_browser_unlock_data},
+ {"osgk_browser_resize", (void **) &osgk_browser_resize},
+ {"osgk_browser_event_mouse_move", (void **) &osgk_browser_event_mouse_move},
+ {"osgk_browser_event_mouse_button", (void **) &osgk_browser_event_mouse_button},
+ {"osgk_browser_event_mouse_wheel", (void **) &osgk_browser_event_mouse_wheel},
+ {"osgk_browser_event_key", (void **) &osgk_browser_event_key},
+ {"osgk_browser_set_user_data", (void **) &osgk_browser_set_user_data},
+ {"osgk_browser_get_user_data", (void **) &osgk_browser_get_user_data},
+ {"osgk_sot_create", (void **) &osgk_sot_create},
+ {"osgk_sot_register", (void **) &osgk_sot_register},
+ {"osgk_sot_add_function", (void **) &osgk_sot_add_function},
+ {"osgk_string_get", (void **) &osgk_string_get},
+ {"osgk_string_create", (void **) &osgk_string_create},
+ {"osgk_variant_convert", (void **) &osgk_variant_convert},
+ {"osgk_variant_get_string", (void **) &osgk_variant_get_string},
+ {"osgk_variant_create_string", (void **) &osgk_variant_create_string},
+ {NULL, NULL}
+};
+
+qboolean CL_Gecko_OpenLibrary (void)
+{
+ const char* dllnames_gecko [] =
+ {
+#if defined(WIN32)
+ "OffscreenGecko.dll",
+#elif defined(MACOSX)
+ "OffscreenGecko.dylib",
+#else
+ "libOffscreenGecko.so",
+#endif
+ NULL
+ };
+
+ // Already loaded?
+ if (osgk_dll)
+ return true;
+
+// COMMANDLINEOPTION: Sound: -nogecko disables gecko support (web browser support for menu and computer terminals)
+ if (COM_CheckParm("-nogecko"))
+ return false;
+
+ return Sys_LoadLibrary (dllnames_gecko, &osgk_dll, osgkFuncs);
+}
+
+void CL_Gecko_Init( void )
+{
+ CL_Gecko_OpenLibrary();
+
+ Cmd_AddCommand( "gecko_create", cl_gecko_create_f, "Create a gecko browser instance" );
+ Cmd_AddCommand( "gecko_destroy", cl_gecko_destroy_f, "Destroy a gecko browser instance" );
+ Cmd_AddCommand( "gecko_navigate", cl_gecko_navigate_f, "Navigate a gecko browser to a URI" );
+ Cmd_AddCommand( "gecko_injecttext", cl_gecko_injecttext_f, "Injects text into a browser" );
+ Cmd_AddCommand( "gecko_movecursor", gl_gecko_movecursor_f, "Move the cursor to a certain position" );
+
+ R_RegisterModule( "CL_Gecko", cl_gecko_start, cl_gecko_shutdown, cl_gecko_newmap, NULL, NULL );
+}
+
+void CL_Gecko_NavigateToURI( clgecko_t *instance, const char *URI ) {
+ if( !instance || !instance->browser ) {
+ return;
+ }
+
+ if( instance->active ) {
+ osgk_browser_navigate( instance->browser, URI );
+ }
+}
+
+void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y ) {
+ // TODO: assert x, y \in [0.0, 1.0]
+ int mappedx, mappedy;
+
+ if( !instance || !instance->browser ) {
+ return;
+ }
+
+ mappedx = (int) (x * instance->width);
+ mappedy = (int) (y * instance->height);
+ osgk_browser_event_mouse_move( instance->browser, mappedx, mappedy );
+}
+
+typedef struct geckokeymapping_s {
+ keynum_t keycode;
+ unsigned int geckokeycode;
+} geckokeymapping_t;
+
+static geckokeymapping_t geckokeymappingtable[] = {
+ { K_BACKSPACE, OSGKKey_Backspace },
+ { K_TAB, OSGKKey_Tab },
+ { K_ENTER, OSGKKey_Return },
+ { K_SHIFT, OSGKKey_Shift },
+ { K_CTRL, OSGKKey_Control },
+ { K_ALT, OSGKKey_Alt },
+ { K_CAPSLOCK, OSGKKey_CapsLock },
+ { K_ESCAPE, OSGKKey_Escape },
+ { K_SPACE, OSGKKey_Space },
+ { K_PGUP, OSGKKey_PageUp },
+ { K_PGDN, OSGKKey_PageDown },
+ { K_END, OSGKKey_End },
+ { K_HOME, OSGKKey_Home },
+ { K_LEFTARROW, OSGKKey_Left },
+ { K_UPARROW, OSGKKey_Up },
+ { K_RIGHTARROW, OSGKKey_Right },
+ { K_DOWNARROW, OSGKKey_Down },
+ { K_INS, OSGKKey_Insert },
+ { K_DEL, OSGKKey_Delete },
+ { K_F1, OSGKKey_F1 },
+ { K_F2, OSGKKey_F2 },
+ { K_F3, OSGKKey_F3 },
+ { K_F4, OSGKKey_F4 },
+ { K_F5, OSGKKey_F5 },
+ { K_F6, OSGKKey_F6 },
+ { K_F7, OSGKKey_F7 },
+ { K_F8, OSGKKey_F8 },
+ { K_F9, OSGKKey_F9 },
+ { K_F10, OSGKKey_F10 },
+ { K_F11, OSGKKey_F11 },
+ { K_F12, OSGKKey_F12 },
+ { K_NUMLOCK, OSGKKey_NumLock },
+ { K_SCROLLOCK, OSGKKey_ScrollLock }
+};
+
+qboolean CL_Gecko_Event_Key( clgecko_t *instance, keynum_t key, clgecko_buttoneventtype_t eventtype ) {
+ if( !instance || !instance->browser ) {
+ return false;
+ }
+
+ // determine whether its a keyboard event
+ if( key < K_OTHERDEVICESBEGIN ) {
+
+ OSGK_KeyboardEventType mappedtype = kePress;
+ unsigned int mappedkey = key;
+
+ unsigned int i;
+ // yes! then convert it if necessary!
+ for( i = 0 ; i < sizeof( geckokeymappingtable ) / sizeof( *geckokeymappingtable ) ; i++ ) {
+ const geckokeymapping_t * const mapping = &geckokeymappingtable[ i ];
+ if( key == mapping->keycode ) {
+ mappedkey = mapping->geckokeycode;
+ break;
+ }
+ }
+
+ // convert the eventtype
+ // map the type
+ switch( eventtype ) {
+ case CLG_BET_DOWN:
+ mappedtype = keDown;
+ break;
+ case CLG_BET_UP:
+ mappedtype = keUp;
+ break;
+ case CLG_BET_DOUBLECLICK:
+ // TODO: error message
+ break;
+ case CLG_BET_PRESS:
+ mappedtype = kePress;
+ }
+
+ return osgk_browser_event_key( instance->browser, mappedkey, mappedtype ) != 0;
+ } else if( K_MOUSE1 <= key && key <= K_MOUSE3 ) {
+ OSGK_MouseButtonEventType mappedtype = meDoubleClick;
+ OSGK_MouseButton mappedbutton;
+
+ mappedbutton = (OSGK_MouseButton) (mbLeft + (key - K_MOUSE1));
+
+ switch( eventtype ) {
+ case CLG_BET_DOWN:
+ mappedtype = meDown;
+ break;
+ case CLG_BET_UP:
+ mappedtype = meUp;
+ break;
+ case CLG_BET_DOUBLECLICK:
+ mappedtype = meDoubleClick;
+ break;
+ case CLG_BET_PRESS:
+ // hihi, hacky hacky
+ osgk_browser_event_mouse_button( instance->browser, mappedbutton, meDown );
+ mappedtype = meUp;
+ break;
+ }
+
+ osgk_browser_event_mouse_button( instance->browser, mappedbutton, mappedtype );
+ return true;
+ } else if( K_MWHEELUP <= key && key <= K_MWHEELDOWN ) {
+ if( eventtype == CLG_BET_DOWN )
+ osgk_browser_event_mouse_wheel( instance->browser,
+ waVertical, (key == K_MWHEELUP) ? wdNegative : wdPositive );
+ return true;
+ }
+ // TODO: error?
+ return false;
+}