1 /* --- 8< --- 8< --- OffscreenGecko headers --- >8 --- >8 --- */
2 /* NOTE: Documentation comments have been omitted. Documentation is
3 available in the OffscreenGecko SDK download. */
5 /* OffscreenGecko/defs.h */
7 #define OSGK_CLASSTYPE_DEF struct
8 #define OSGK_CLASSTYPE_REF struct
12 #define OSGK_ASSERT(x) assert(x)
14 typedef unsigned int OSGK_GeckoResult;
16 #if defined(__cplusplus) || defined(__GNUC__)
17 # define OSGK_INLINE inline
18 #elif defined(_MSC_VER)
19 # define OSGK_INLINE __inline
24 /* OffscreenGecko/baseobj.h */
26 struct OSGK_BaseObject_s
30 typedef struct OSGK_BaseObject_s OSGK_BaseObject;
32 #define OSGK_DERIVEDTYPE(T) \
33 typedef struct T ## _s { \
34 OSGK_BaseObject baseobj; \
37 static int (*osgk_addref) (OSGK_BaseObject* obj);
38 static int (*osgk_release) (OSGK_BaseObject* obj);
40 /*static OSGK_INLINE int osgk_addref_real (OSGK_BaseObject* obj)
42 return osgk_addref (obj);
45 static OSGK_INLINE int osgk_release_real (OSGK_BaseObject* obj)
47 return osgk_release (obj);
50 #define osgk_addref(obj) osgk_addref_real (&((obj)->baseobj))
51 #define osgk_release(obj) osgk_release_real (&((obj)->baseobj))
53 /* OffscreenGecko/embedding.h */
55 OSGK_DERIVEDTYPE(OSGK_EmbeddingOptions);
57 static OSGK_EmbeddingOptions* (*osgk_embedding_options_create) (void);
58 static void (*osgk_embedding_options_add_search_path) (
59 OSGK_EmbeddingOptions* options, const char* path);
60 /*static void (*osgk_embedding_options_add_components_path) (
61 OSGK_EmbeddingOptions* options, const char* path);*/
62 static void (*osgk_embedding_options_set_profile_dir) (
63 OSGK_EmbeddingOptions* options, const char* profileDir,
64 const char* localProfileDir);
66 OSGK_DERIVEDTYPE(OSGK_Embedding);
68 #define OSGK_API_VERSION 1
70 static OSGK_Embedding* (*osgk_embedding_create2) (
71 unsigned int apiVer, OSGK_EmbeddingOptions* options,
72 OSGK_GeckoResult* geckoResult);
74 /*static OSGK_INLINE OSGK_Embedding* osgk_embedding_create (
75 OSGK_GeckoResult* geckoResult)
77 return osgk_embedding_create2 (OSGK_API_VERSION, 0, geckoResult);
80 static OSGK_INLINE OSGK_Embedding* osgk_embedding_create_with_options (
81 OSGK_EmbeddingOptions* options, OSGK_GeckoResult* geckoResult)
83 return osgk_embedding_create2 (OSGK_API_VERSION, options, geckoResult);
86 /*static OSGK_GeckoMem* (*osgk_embedding_get_gecko_mem) (
87 OSGK_Embedding* embedding);*/
89 /*static OSGK_ComponentMgr* (*osgk_embedding_get_component_mgr) (
90 OSGK_Embedding* embedding);*/
92 /*OSGK_CLASSTYPE_DEF nsIComponentManager;
93 OSGK_CLASSTYPE_DEF nsIComponentRegistrar;
94 OSGK_CLASSTYPE_DEF nsIServiceManager;*/
96 /*static OSGK_CLASSTYPE_REF nsIComponentManager*
97 (*osgk_embedding_get_gecko_component_manager) (OSGK_Embedding* embedding);*/
98 /*static OSGK_CLASSTYPE_REF nsIComponentRegistrar*
99 (*osgk_embedding_get_gecko_component_registrar) (OSGK_Embedding* embedding);*/
100 /*static OSGK_CLASSTYPE_REF nsIServiceManager*
101 (*osgk_embedding_get_gecko_service_manager) (OSGK_Embedding* embedding);*/
107 /*static int (*osgk_embedding_register_js_global) (
108 OSGK_Embedding* embedding, const char* name, const char* contractID,
109 unsigned int flags, OSGK_String** previousContract,
110 OSGK_GeckoResult* geckoResult);*/
112 /*static void (*osgk_embedding_clear_focus*) (OSGK_Embedding* embedding);*/
113 /*void (*osgk_embedding_set_auto_focus) (OSGK_Embedding* embedding, int autoFocus);*/
114 /*static int (*osgk_embedding_get_auto_focus) (OSGK_Embedding* embedding);*/
116 /* OffscreenGecko/browser.h */
117 OSGK_DERIVEDTYPE(OSGK_Browser);
119 static OSGK_Browser* (*osgk_browser_create) (
120 OSGK_Embedding* embedding, int width, int height);
121 static void (*osgk_browser_navigate) (OSGK_Browser* browser,
124 static int (*osgk_browser_query_dirty) (OSGK_Browser* browser);
125 static const unsigned char* (*osgk_browser_lock_data) (
126 OSGK_Browser* browser, int* isDirty);
127 static void (*osgk_browser_unlock_data) (OSGK_Browser* browser,
128 const unsigned char* data);
130 typedef enum OSGK_MouseButton
137 typedef enum OSGK_MouseButtonEventType
142 } OSGK_MouseButtonEventType;
144 static void (*osgk_browser_event_mouse_move) (
145 OSGK_Browser* browser, int x, int y);
146 static void (*osgk_browser_event_mouse_button) (
147 OSGK_Browser* browser, OSGK_MouseButton button,
148 OSGK_MouseButtonEventType eventType);
150 typedef enum OSGK_WheelAxis
156 typedef enum OSGK_WheelDirection
162 } OSGK_WheelDirection;
164 static void (*osgk_browser_event_mouse_wheel) (
165 OSGK_Browser* browser, OSGK_WheelAxis axis,
166 OSGK_WheelDirection direction);
168 typedef enum OSGK_KeyboardEventType
173 } OSGK_KeyboardEventType;
177 OSGKKey_First = 0x110000,
179 OSGKKey_Backspace = OSGKKey_First,
215 static int (*osgk_browser_event_key) (
216 OSGK_Browser* browser, unsigned int key,
217 OSGK_KeyboardEventType eventType);
219 typedef enum OSGK_AntiAliasType
224 } OSGK_AntiAliasType;
226 /*static void (*osgk_browser_set_antialias) (
227 OSGK_Browser* browser, OSGK_AntiAliasType aaType);*/
228 /*static OSGK_AntiAliasType (*osgk_browser_get_antialias) (OSGK_Browser* browser);*/
230 /*static void (*osgk_browser_focus) (OSGK_Browser* browser);*/
232 static void (*osgk_browser_resize) (OSGK_Browser* browser,
233 int width, int height);
235 static int (*osgk_browser_set_user_data) (OSGK_Browser* browser,
236 unsigned int key, void* data, int overrideData);
237 static int (*osgk_browser_get_user_data) (OSGK_Browser* browser,
238 unsigned int key, void** data);
240 /* OffscreenGecko/string.h */
241 OSGK_DERIVEDTYPE(OSGK_String);
243 static const char* (*osgk_string_get) (OSGK_String* str);
245 static OSGK_String* (*osgk_string_create) (const char* str);
247 /* OffscreenGecko/scriptvariant.h */
249 OSGK_CLASSTYPE_DEF nsISupports;
251 OSGK_DERIVEDTYPE(OSGK_ScriptVariant);
253 typedef enum OSGK_ScriptVariantType
266 } OSGK_ScriptVariantType;
268 /*static OSGK_ScriptVariantType (*osgk_variant_get_type) (
269 OSGK_ScriptVariant* variant);*/
270 static OSGK_ScriptVariant* (*osgk_variant_convert) (
271 OSGK_ScriptVariant* variant, OSGK_ScriptVariantType newType);
273 /*static int (*osgk_variant_get_int) (OSGK_ScriptVariant* variant,
275 /*static int (*osgk_variant_get_uint) (OSGK_ScriptVariant* variant,
276 unsigned int* val);*/
277 /*static int (*osgk_variant_get_float) (OSGK_ScriptVariant* variant,
279 /*static int (*osgk_variant_get_double) (OSGK_ScriptVariant* variant,
281 /*static int (*osgk_variant_get_bool) (OSGK_ScriptVariant* variant,
283 /*static int (*osgk_variant_get_char) (OSGK_ScriptVariant* variant,
284 unsigned int* val);*/
285 // Does not increase ref count
286 static int (*osgk_variant_get_string) (OSGK_ScriptVariant* variant,
288 /*// Does not increase ref count
289 static int (*osgk_variant_get_isupports) (OSGK_ScriptVariant* variant,
290 OSGK_CLASSTYPE_REF nsISupports** val);*/
291 /*static int (*osgk_variant_get_script_object) (OSGK_ScriptVariant* variant,
293 /*static int (*osgk_variant_get_array_size) (OSGK_ScriptVariant* variant,
295 /*static int (*osgk_variant_get_array_item) (OSGK_ScriptVariant* variant,
296 OSGK_ScriptVariant** val);*/
298 /*static OSGK_ScriptVariant* (*osgk_variant_create_empty) (
299 OSGK_Embedding* embedding);*/
300 /*static OSGK_ScriptVariant* (*osgk_variant_create_int) (
301 OSGK_Embedding* embedding, int val);*/
302 /*static OSGK_ScriptVariant* (*osgk_variant_create_uint) (
303 OSGK_Embedding* embedding, unsigned int val);*/
304 /*static OSGK_ScriptVariant* (*osgk_variant_create_float) (
305 OSGK_Embedding* embedding, float val);*/
306 /*static OSGK_ScriptVariant* (*osgk_variant_create_double) (
307 OSGK_Embedding* embedding, double val);*/
308 /*OSGK_ScriptVariant* (*osgk_variant_create_bool) (
309 OSGK_Embedding* embedding, int val);*/
310 /*static OSGK_ScriptVariant* (*osgk_variant_create_char) (
311 OSGK_Embedding* embedding, unsigned int val);*/
312 static OSGK_ScriptVariant* (*osgk_variant_create_string) (
313 OSGK_Embedding* embedding, OSGK_String* val);
314 /*static OSGK_ScriptVariant* (*osgk_variant_create_isupports) (
315 OSGK_Embedding* embedding, OSGK_CLASSTYPE_REF nsISupports* val);*/
316 /*static OSGK_ScriptVariant* (*osgk_variant_create_script_object) (
317 OSGK_Embedding* embedding, void* tag);*/
318 /*static OSGK_ScriptVariant* (*osgk_variant_create_array) (
319 OSGK_Embedding* embedding, size_t numItems, OSGK_ScriptVariant** items);*/
321 /* OffscreenGecko/scriptobjecttemplate.h */
323 OSGK_DERIVEDTYPE(OSGK_ScriptObjectTemplate);
325 typedef enum OSGK_ScriptResult
328 srFailed = (int)0x80004005L /* actually NS_ERROR_FAILURE */
331 typedef struct OSGK_ScriptObjectCreateParams_s
334 OSGK_Browser* browser;
335 } OSGK_ScriptObjectCreateParams;
337 typedef OSGK_ScriptResult (*OSGK_CreateObjFunc) (
338 OSGK_ScriptObjectCreateParams* params, void** objTag);
339 typedef void (*OSGK_DestroyObjFunc) (void* objTag);
341 static OSGK_ScriptObjectTemplate* (*osgk_sot_create) (
342 OSGK_Embedding* embedding,
343 OSGK_CreateObjFunc createFunc, OSGK_DestroyObjFunc destroyFunc,
346 typedef OSGK_ScriptVariant* (*OSGK_GetPropertyFunc) (void* objTag,
348 typedef OSGK_ScriptResult (*OSGK_SetPropertyFunc) (void* objTag,
349 void* propTag, OSGK_ScriptVariant* val);
351 /*static int (*osgk_sot_add_property) (
352 OSGK_ScriptObjectTemplate* templ, const char* propName, void* propTag,
353 OSGK_GetPropertyFunc getter, OSGK_SetPropertyFunc setter);*/
355 typedef OSGK_ScriptResult (*OSGK_FunctionCallFunc) (void* objTag,
356 void* methTag, size_t numParams, OSGK_ScriptVariant** params,
357 OSGK_ScriptVariant** returnVal);
359 static int (*osgk_sot_add_function) (
360 OSGK_ScriptObjectTemplate* templ, const char* funcName, void* funcTag,
361 OSGK_FunctionCallFunc callFunc);
363 /*static OSGK_ScriptVariant* (*osgk_sot_instantiate) (
364 OSGK_ScriptObjectTemplate* templ, void** objTag); */
366 static int (*osgk_sot_register) (
367 OSGK_ScriptObjectTemplate* templ, OSGK_Embedding* embedding,
368 const char* name, unsigned int flags);
370 /* --- >8 --- >8 --- End OffscreenGecko headers --- 8< --- 8< --- */
372 #include "quakedef.h"
373 #include "cl_dyntexture.h"
374 #include "cl_gecko.h"
377 #define DEFAULT_GECKO_SIZE 512
379 static rtexturepool_t *cl_geckotexturepool;
380 static OSGK_Embedding *cl_geckoembedding;
384 char name[ MAX_QPATH + 32 ];
387 OSGK_Browser *browser;
389 int texWidth, texHeight;
394 #define USERDATAKEY_CL_GECKO_T 0
396 static dllhandle_t osgk_dll = NULL;
398 static clgecko_t cl_geckoinstances[ MAX_GECKO_INSTANCES ];
400 static clgecko_t * cl_gecko_findunusedinstance( void ) {
402 for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
403 clgecko_t *instance = &cl_geckoinstances[ i ];
404 if( !instance->active ) {
408 Con_DPrintf( "cl_gecko_findunusedinstance: out of geckos\n" );
412 clgecko_t * CL_Gecko_FindBrowser( const char *name ) {
415 if( !name || !*name || strncmp( name, CLGECKOPREFIX, sizeof( CLGECKOPREFIX ) - 1 ) != 0 ) {
416 Con_DPrintf( "CL_Gecko_FindBrowser: Bad gecko texture name '%s'!\n", name );
420 for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
421 clgecko_t *instance = &cl_geckoinstances[ i ];
422 if( instance->active && strcmp( instance->name, name ) == 0 ) {
427 Con_DPrintf( "CL_Gecko_FindBrowser: No browser named '%s'!\n", name );
432 static void cl_gecko_updatecallback( rtexture_t *texture, void* callbackData ) {
433 clgecko_t *instance = (clgecko_t *) callbackData;
434 const unsigned char *data;
435 if( instance->browser ) {
436 // TODO: OSGK only supports BGRA right now
437 TIMING_TIMESTATEMENT(data = osgk_browser_lock_data( instance->browser, NULL ));
438 R_UpdateTexture( texture, data, 0, 0, 0, instance->width, instance->height, 1 );
439 osgk_browser_unlock_data( instance->browser, data );
443 static void cl_gecko_linktexture( clgecko_t *instance ) {
444 // TODO: assert that instance->texture == NULL
445 instance->texture = R_LoadTexture2D( cl_geckotexturepool, instance->name,
446 instance->texWidth, instance->texHeight, NULL, TEXTYPE_BGRA, TEXF_ALPHA | TEXF_PERSISTENT, -1, NULL );
447 R_MakeTextureDynamic( instance->texture, cl_gecko_updatecallback, instance );
448 CL_LinkDynTexture( instance->name, instance->texture );
451 static void cl_gecko_unlinktexture( clgecko_t *instance ) {
452 if( instance->texture ) {
453 CL_UnlinkDynTexture( instance->name );
454 R_FreeTexture( instance->texture );
455 instance->texture = NULL;
459 void CL_Gecko_Resize( clgecko_t *instance, int width, int height ) {
460 int newWidth, newHeight;
462 // early out if bad parameters are passed (no resize to a texture size bigger than the original texture size)
463 if( !instance || !instance->browser) {
467 newWidth = CeilPowerOf2( width );
468 newHeight = CeilPowerOf2( height );
469 if ((newWidth != instance->texWidth) || (newHeight != instance->texHeight))
471 cl_gecko_unlinktexture( instance );
472 instance->texWidth = newWidth;
473 instance->texHeight = newHeight;
474 cl_gecko_linktexture( instance );
478 /* The gecko area will only cover a part of the texture; to avoid
479 'old' pixels bleeding in at the border clear the texture. */
480 R_ClearTexture( instance->texture );
483 osgk_browser_resize( instance->browser, width, height);
484 instance->width = width;
485 instance->height = height;
488 void CL_Gecko_GetTextureExtent( clgecko_t *instance, float* pwidth, float* pheight )
490 if( !instance || !instance->browser ) {
494 *pwidth = (float)instance->width / instance->texWidth;
495 *pheight = (float)instance->height / instance->texHeight;
498 static OSGK_ScriptResult dpGlobal_create (OSGK_ScriptObjectCreateParams* params,
501 if (!osgk_browser_get_user_data (params->browser, USERDATAKEY_CL_GECKO_T, objTag))
506 static OSGK_ScriptResult dpGlobal_query (void* objTag, void* methTag,
508 OSGK_ScriptVariant** params,
509 OSGK_ScriptVariant** returnVal)
511 clgecko_t *instance = (clgecko_t *) objTag;
512 OSGK_ScriptVariant* strVal;
513 OSGK_ScriptResult result = srFailed;
514 prvm_prog_t * saveProg;
516 /* Can happen when created from console */
517 if (instance->ownerProg < 0) return srFailed;
519 /* Require exactly one param, for now */
520 if (numParams != 1) return srFailed;
522 strVal = osgk_variant_convert (params[0], svtString);
523 if (strVal == 0) return srFailed;
526 PRVM_SetProg(instance->ownerProg);
528 if (prog->funcoffsets.Gecko_Query)
530 OSGK_String* paramStr, *resultStr;
532 if (!osgk_variant_get_string (strVal, ¶mStr)) return srFailed;
533 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString (instance->name);
534 PRVM_G_INT(OFS_PARM1) = PRVM_SetTempString (osgk_string_get (paramStr));
535 PRVM_ExecuteProgram(prog->funcoffsets.Gecko_Query,"Gecko_Query() required");
536 resultStr = osgk_string_create (PRVM_G_STRING (OFS_RETURN));
537 *returnVal = osgk_variant_create_string (cl_geckoembedding, resultStr);
538 osgk_release (resultStr);
549 # define XULRUNNER_DIR_SUFFIX "win64"
551 # define XULRUNNER_DIR_SUFFIX "win32"
552 #elif defined(DP_OS_STR) && defined(DP_ARCH_STR)
553 # define XULRUNNER_DIR_SUFFIX DP_OS_STR "-" DP_ARCH_STR
556 static qboolean CL_Gecko_Embedding_Init (void)
558 char profile_path [MAX_OSPATH];
559 OSGK_GeckoResult grc;
560 OSGK_EmbeddingOptions *options;
561 OSGK_ScriptObjectTemplate* dpGlobalTemplate;
563 if (!osgk_dll) return false;
565 if( cl_geckoembedding != NULL ) return true;
567 Con_DPrintf( "CL_Gecko_Embedding_Init: setting up gecko embedding\n" );
569 options = osgk_embedding_options_create();
570 #ifdef XULRUNNER_DIR_SUFFIX
571 osgk_embedding_options_add_search_path( options, "./xulrunner-" XULRUNNER_DIR_SUFFIX "/" );
573 osgk_embedding_options_add_search_path( options, "./xulrunner/" );
574 dpsnprintf (profile_path, sizeof (profile_path), "%s/xulrunner_profile/", fs_gamedir);
575 osgk_embedding_options_set_profile_dir( options, profile_path, 0 );
576 cl_geckoembedding = osgk_embedding_create_with_options( options, &grc );
577 osgk_release( options );
579 if( cl_geckoembedding == NULL ) {
580 Con_Printf( "CL_Gecko_Embedding_Init: Couldn't retrieve gecko embedding object (%.8x)!\n", grc );
584 Con_DPrintf( "CL_Gecko_Embedding_Init: Embedding set up correctly\n" );
586 dpGlobalTemplate = osgk_sot_create( cl_geckoembedding, dpGlobal_create, NULL, NULL );
588 osgk_sot_add_function (dpGlobalTemplate, "query", 0, dpGlobal_query);
590 osgk_sot_register (dpGlobalTemplate, cl_geckoembedding, "Darkplaces", 0);
591 osgk_release( dpGlobalTemplate );
596 clgecko_t * CL_Gecko_CreateBrowser( const char *name, int ownerProg ) {
599 if (!CL_Gecko_Embedding_Init ()) return NULL;
601 // TODO: verify that we dont use a name twice
602 instance = cl_gecko_findunusedinstance();
603 // TODO: assert != NULL
605 instance->active = true;
606 strlcpy( instance->name, name, sizeof( instance->name ) );
607 instance->browser = osgk_browser_create( cl_geckoembedding, DEFAULT_GECKO_SIZE, DEFAULT_GECKO_SIZE );
608 if( instance->browser == NULL ) {
609 Con_Printf( "CL_Gecko_CreateBrowser: Browser object creation failed!\n" );
611 // TODO: assert != NULL
612 osgk_browser_set_user_data (instance->browser, USERDATAKEY_CL_GECKO_T,
614 instance->ownerProg = ownerProg;
616 instance->width = instance->texWidth = DEFAULT_GECKO_SIZE;
617 instance->height = instance->texHeight = DEFAULT_GECKO_SIZE;
618 cl_gecko_linktexture( instance );
623 void CL_Gecko_DestroyBrowser( clgecko_t *instance ) {
624 if( !instance || !instance->active ) {
628 instance->active = false;
629 cl_gecko_unlinktexture( instance );
631 osgk_release( instance->browser );
632 instance->browser = NULL;
635 void CL_Gecko_Frame( void ) {
637 // FIXME: track cl_numgeckoinstances to avoid scanning the entire array?
638 for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
639 clgecko_t *instance = &cl_geckoinstances[ i ];
640 if( instance->active ) {
641 if( instance->browser && osgk_browser_query_dirty( instance->browser ) == 1 ) {
642 R_MarkDirtyTexture( instance->texture );
648 static void cl_gecko_start( void )
651 cl_geckotexturepool = R_AllocTexturePool();
653 // recreate textures on module start
654 for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
655 clgecko_t *instance = &cl_geckoinstances[ i ];
656 if( instance->active ) {
657 cl_gecko_linktexture( instance );
662 static void cl_gecko_shutdown( void )
665 for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
666 clgecko_t *instance = &cl_geckoinstances[ i ];
667 if( instance->active ) {
668 cl_gecko_unlinktexture( instance );
671 R_FreeTexturePool( &cl_geckotexturepool );
674 static void cl_gecko_newmap( void )
679 void CL_Gecko_Shutdown( void ) {
681 for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
682 clgecko_t *instance = &cl_geckoinstances[ i ];
683 if( instance->active ) {
684 cl_gecko_unlinktexture( instance );
688 if (cl_geckoembedding != NULL)
690 osgk_release( cl_geckoembedding );
691 cl_geckoembedding = NULL;
694 if (osgk_dll != NULL)
696 Sys_UnloadLibrary (&osgk_dll);
700 static void cl_gecko_create_f( void ) {
701 char name[MAX_QPATH];
705 Con_Print("usage: gecko_create <name>\npcreates a browser (full texture path " CLGECKOPREFIX "<name>)\n");
709 dpsnprintf(name, sizeof(name), CLGECKOPREFIX "%s", Cmd_Argv(1));
710 CL_Gecko_CreateBrowser( name, -1 );
713 static void cl_gecko_destroy_f( void ) {
714 char name[MAX_QPATH];
718 Con_Print("usage: gecko_destroy <name>\ndestroys a browser\n");
722 dpsnprintf(name, sizeof(name), CLGECKOPREFIX "%s", Cmd_Argv(1));
723 CL_Gecko_DestroyBrowser( CL_Gecko_FindBrowser( name ) );
726 static void cl_gecko_navigate_f( void ) {
727 char name[MAX_QPATH];
732 Con_Print("usage: gecko_navigate <name> <URI>\nnavigates to a certain URI\n");
736 dpsnprintf(name, sizeof(name), CLGECKOPREFIX "%s", Cmd_Argv(1));
738 CL_Gecko_NavigateToURI( CL_Gecko_FindBrowser( name ), URI );
741 static void cl_gecko_injecttext_f( void ) {
742 char name[MAX_QPATH];
749 Con_Print("usage: gecko_injecttext <name> <text>\ninjects a certain text into the browser\n");
753 dpsnprintf(name, sizeof(name), CLGECKOPREFIX "%s", Cmd_Argv(1));
754 instance = CL_Gecko_FindBrowser( name );
756 Con_Printf( "cl_gecko_injecttext_f: gecko instance '%s' couldn't be found!\n", name );
760 text = Cmd_Argv( 2 );
762 for( p = text ; *p ; p++ ) {
782 CL_Gecko_Event_Key( instance, (keynum_t) key, CLG_BET_PRESS );
786 static void gl_gecko_movecursor_f( void ) {
787 char name[MAX_QPATH];
792 Con_Print("usage: gecko_movecursor <name> <x> <y>\nmove the cursor to a certain position\n");
796 dpsnprintf(name, sizeof(name), CLGECKOPREFIX "%s", Cmd_Argv(1));
797 x = atof( Cmd_Argv( 2 ) );
798 y = atof( Cmd_Argv( 3 ) );
800 CL_Gecko_Event_CursorMove( CL_Gecko_FindBrowser( name ), x, y );
806 static const dllfunction_t osgkFuncs[] =
808 {"osgk_addref", (void **) &osgk_addref},
809 {"osgk_release", (void **) &osgk_release},
810 {"osgk_embedding_create2", (void **) &osgk_embedding_create2},
811 {"osgk_embedding_options_create", (void **) &osgk_embedding_options_create},
812 {"osgk_embedding_options_add_search_path", (void **) &osgk_embedding_options_add_search_path},
813 {"osgk_embedding_options_set_profile_dir", (void **) &osgk_embedding_options_set_profile_dir},
814 {"osgk_browser_create", (void **) &osgk_browser_create},
815 {"osgk_browser_query_dirty", (void **) &osgk_browser_query_dirty},
816 {"osgk_browser_navigate", (void **) &osgk_browser_navigate},
817 {"osgk_browser_lock_data", (void **) &osgk_browser_lock_data},
818 {"osgk_browser_unlock_data", (void **) &osgk_browser_unlock_data},
819 {"osgk_browser_resize", (void **) &osgk_browser_resize},
820 {"osgk_browser_event_mouse_move", (void **) &osgk_browser_event_mouse_move},
821 {"osgk_browser_event_mouse_button", (void **) &osgk_browser_event_mouse_button},
822 {"osgk_browser_event_mouse_wheel", (void **) &osgk_browser_event_mouse_wheel},
823 {"osgk_browser_event_key", (void **) &osgk_browser_event_key},
824 {"osgk_browser_set_user_data", (void **) &osgk_browser_set_user_data},
825 {"osgk_browser_get_user_data", (void **) &osgk_browser_get_user_data},
826 {"osgk_sot_create", (void **) &osgk_sot_create},
827 {"osgk_sot_register", (void **) &osgk_sot_register},
828 {"osgk_sot_add_function", (void **) &osgk_sot_add_function},
829 {"osgk_string_get", (void **) &osgk_string_get},
830 {"osgk_string_create", (void **) &osgk_string_create},
831 {"osgk_variant_convert", (void **) &osgk_variant_convert},
832 {"osgk_variant_get_string", (void **) &osgk_variant_get_string},
833 {"osgk_variant_create_string", (void **) &osgk_variant_create_string},
837 qboolean CL_Gecko_OpenLibrary (void)
839 const char* dllnames_gecko [] =
842 "OffscreenGecko.dll",
843 #elif defined(MACOSX)
844 "OffscreenGecko.dylib",
846 "libOffscreenGecko.so",
855 // COMMANDLINEOPTION: Sound: -nogecko disables gecko support (web browser support for menu and computer terminals)
856 if (COM_CheckParm("-nogecko"))
859 return Sys_LoadLibrary (dllnames_gecko, &osgk_dll, osgkFuncs);
862 void CL_Gecko_Init( void )
864 CL_Gecko_OpenLibrary();
866 Cmd_AddCommand( "gecko_create", cl_gecko_create_f, "Create a gecko browser instance" );
867 Cmd_AddCommand( "gecko_destroy", cl_gecko_destroy_f, "Destroy a gecko browser instance" );
868 Cmd_AddCommand( "gecko_navigate", cl_gecko_navigate_f, "Navigate a gecko browser to a URI" );
869 Cmd_AddCommand( "gecko_injecttext", cl_gecko_injecttext_f, "Injects text into a browser" );
870 Cmd_AddCommand( "gecko_movecursor", gl_gecko_movecursor_f, "Move the cursor to a certain position" );
872 R_RegisterModule( "CL_Gecko", cl_gecko_start, cl_gecko_shutdown, cl_gecko_newmap, NULL, NULL );
875 void CL_Gecko_NavigateToURI( clgecko_t *instance, const char *URI ) {
876 if( !instance || !instance->browser ) {
880 if( instance->active ) {
881 osgk_browser_navigate( instance->browser, URI );
885 void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y ) {
886 // TODO: assert x, y \in [0.0, 1.0]
887 int mappedx, mappedy;
889 if( !instance || !instance->browser ) {
893 mappedx = (int) (x * instance->width);
894 mappedy = (int) (y * instance->height);
895 osgk_browser_event_mouse_move( instance->browser, mappedx, mappedy );
898 typedef struct geckokeymapping_s {
900 unsigned int geckokeycode;
903 static geckokeymapping_t geckokeymappingtable[] = {
904 { K_BACKSPACE, OSGKKey_Backspace },
905 { K_TAB, OSGKKey_Tab },
906 { K_ENTER, OSGKKey_Return },
907 { K_SHIFT, OSGKKey_Shift },
908 { K_CTRL, OSGKKey_Control },
909 { K_ALT, OSGKKey_Alt },
910 { K_CAPSLOCK, OSGKKey_CapsLock },
911 { K_ESCAPE, OSGKKey_Escape },
912 { K_SPACE, OSGKKey_Space },
913 { K_PGUP, OSGKKey_PageUp },
914 { K_PGDN, OSGKKey_PageDown },
915 { K_END, OSGKKey_End },
916 { K_HOME, OSGKKey_Home },
917 { K_LEFTARROW, OSGKKey_Left },
918 { K_UPARROW, OSGKKey_Up },
919 { K_RIGHTARROW, OSGKKey_Right },
920 { K_DOWNARROW, OSGKKey_Down },
921 { K_INS, OSGKKey_Insert },
922 { K_DEL, OSGKKey_Delete },
923 { K_F1, OSGKKey_F1 },
924 { K_F2, OSGKKey_F2 },
925 { K_F3, OSGKKey_F3 },
926 { K_F4, OSGKKey_F4 },
927 { K_F5, OSGKKey_F5 },
928 { K_F6, OSGKKey_F6 },
929 { K_F7, OSGKKey_F7 },
930 { K_F8, OSGKKey_F8 },
931 { K_F9, OSGKKey_F9 },
932 { K_F10, OSGKKey_F10 },
933 { K_F11, OSGKKey_F11 },
934 { K_F12, OSGKKey_F12 },
935 { K_NUMLOCK, OSGKKey_NumLock },
936 { K_SCROLLOCK, OSGKKey_ScrollLock }
939 qboolean CL_Gecko_Event_Key( clgecko_t *instance, keynum_t key, clgecko_buttoneventtype_t eventtype ) {
940 if( !instance || !instance->browser ) {
944 // determine whether its a keyboard event
945 if( key < K_OTHERDEVICESBEGIN ) {
947 OSGK_KeyboardEventType mappedtype = kePress;
948 unsigned int mappedkey = key;
951 // yes! then convert it if necessary!
952 for( i = 0 ; i < sizeof( geckokeymappingtable ) / sizeof( *geckokeymappingtable ) ; i++ ) {
953 const geckokeymapping_t * const mapping = &geckokeymappingtable[ i ];
954 if( key == mapping->keycode ) {
955 mappedkey = mapping->geckokeycode;
960 // convert the eventtype
962 switch( eventtype ) {
969 case CLG_BET_DOUBLECLICK:
970 // TODO: error message
973 mappedtype = kePress;
976 return osgk_browser_event_key( instance->browser, mappedkey, mappedtype ) != 0;
977 } else if( K_MOUSE1 <= key && key <= K_MOUSE3 ) {
978 OSGK_MouseButtonEventType mappedtype = meDoubleClick;
979 OSGK_MouseButton mappedbutton;
981 mappedbutton = (OSGK_MouseButton) (mbLeft + (key - K_MOUSE1));
983 switch( eventtype ) {
990 case CLG_BET_DOUBLECLICK:
991 mappedtype = meDoubleClick;
995 osgk_browser_event_mouse_button( instance->browser, mappedbutton, meDown );
1000 osgk_browser_event_mouse_button( instance->browser, mappedbutton, mappedtype );
1002 } else if( K_MWHEELUP <= key && key <= K_MWHEELDOWN ) {
1003 if( eventtype == CLG_BET_DOWN )
1004 osgk_browser_event_mouse_wheel( instance->browser,
1005 waVertical, (key == K_MWHEELUP) ? wdNegative : wdPositive );