43ba96e1327b815bd0c5dde08490ec5c7a335770
[divverent/darkplaces.git] / cl_gecko.c
1 /* --- 8< --- 8< ---   OffscreenGecko headers   --- >8 --- >8 --- */
2 /* NOTE: Documentation comments have been omitted. Documentation is
3    available in the OffscreenGecko SDK download. */
4
5 /* OffscreenGecko/defs.h */
6
7 #define OSGK_CLASSTYPE_DEF      struct
8 #define OSGK_CLASSTYPE_REF      struct
9
10 #include <assert.h>
11 #include <stdlib.h>
12 #define OSGK_ASSERT(x)  assert(x)
13
14 typedef unsigned int OSGK_GeckoResult;
15
16 #if defined(__cplusplus) || defined(__GNUC__)
17 #  define OSGK_INLINE   inline
18 #elif defined(_MSC_VER)
19 #  define OSGK_INLINE   __inline
20 #else
21 #  define OSGK_INLINE
22 #endif
23
24 /* OffscreenGecko/baseobj.h */
25
26 struct OSGK_BaseObject_s
27 {
28   int reserved;
29 };
30 typedef struct OSGK_BaseObject_s OSGK_BaseObject;
31
32 #define OSGK_DERIVEDTYPE(T)           \
33   typedef struct T ## _s {            \
34     OSGK_BaseObject baseobj;          \
35   } T
36
37 static int (*osgk_addref) (OSGK_BaseObject* obj);
38 static int (*osgk_release) (OSGK_BaseObject* obj);
39
40 static OSGK_INLINE int osgk_addref_real (OSGK_BaseObject* obj)
41 {
42   return osgk_addref (obj);
43 }
44
45 static OSGK_INLINE int osgk_release_real (OSGK_BaseObject* obj)
46 {
47   return osgk_release (obj);
48 }
49
50 #define osgk_addref(obj)    osgk_addref_real (&((obj)->baseobj))
51 #define osgk_release(obj)   osgk_release_real (&((obj)->baseobj))
52
53 /* OffscreenGecko/embedding.h */
54
55 OSGK_DERIVEDTYPE(OSGK_EmbeddingOptions);
56
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);
65
66 OSGK_DERIVEDTYPE(OSGK_Embedding);
67
68 #define OSGK_API_VERSION    1
69
70 static OSGK_Embedding* (*osgk_embedding_create2) (
71   unsigned int apiVer, OSGK_EmbeddingOptions* options, 
72   OSGK_GeckoResult* geckoResult);
73
74 static OSGK_INLINE OSGK_Embedding* osgk_embedding_create (
75   OSGK_GeckoResult* geckoResult)
76 {
77   return osgk_embedding_create2 (OSGK_API_VERSION, 0, geckoResult);
78 }
79
80 static OSGK_INLINE OSGK_Embedding* osgk_embedding_create_with_options (
81   OSGK_EmbeddingOptions* options, OSGK_GeckoResult* geckoResult)
82 {
83   return osgk_embedding_create2 (OSGK_API_VERSION, options, geckoResult);
84 }
85
86 /*static OSGK_GeckoMem* (*osgk_embedding_get_gecko_mem) (
87   OSGK_Embedding* embedding);*/
88
89 /*static OSGK_ComponentMgr* (*osgk_embedding_get_component_mgr) (
90   OSGK_Embedding* embedding);*/
91
92 /*OSGK_CLASSTYPE_DEF nsIComponentManager;
93 OSGK_CLASSTYPE_DEF nsIComponentRegistrar;
94 OSGK_CLASSTYPE_DEF nsIServiceManager;*/
95
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);*/
102
103 enum
104 {
105   jsgPrivileged = 1
106 };
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);*/
111
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);*/
115
116 /* OffscreenGecko/browser.h */
117 OSGK_DERIVEDTYPE(OSGK_Browser);
118
119 static OSGK_Browser* (*osgk_browser_create) (
120   OSGK_Embedding* embedding, int width, int height);
121 static void (*osgk_browser_navigate) (OSGK_Browser* browser,
122   const char* uri);
123
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);
129
130 typedef enum OSGK_MouseButton
131 {
132   mbLeft, 
133   mbRight, 
134   mbMiddle
135 } OSGK_MouseButton;
136
137 typedef enum OSGK_MouseButtonEventType
138 {
139   meDown,
140   meUp,
141   meDoubleClick
142 } OSGK_MouseButtonEventType;
143
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);
149
150 typedef enum OSGK_WheelAxis
151 {
152   waVertical,
153   waHorizontal
154 } OSGK_WheelAxis;
155
156 typedef enum OSGK_WheelDirection
157 {
158   wdPositive,
159   wdNegative,
160   wdPositivePage,
161   wdNegativePage
162 } OSGK_WheelDirection;
163
164 static void (*osgk_browser_event_mouse_wheel) (
165   OSGK_Browser* browser, OSGK_WheelAxis axis, 
166   OSGK_WheelDirection direction);
167
168 typedef enum OSGK_KeyboardEventType
169 {
170   keDown,
171   keUp,
172   kePress
173 } OSGK_KeyboardEventType;
174
175 enum
176 {
177   OSGKKey_First = 0x110000,
178
179   OSGKKey_Backspace = OSGKKey_First,
180   OSGKKey_Tab,
181   OSGKKey_Return,
182   OSGKKey_Shift,
183   OSGKKey_Control,
184   OSGKKey_Alt,
185   OSGKKey_CapsLock,
186   OSGKKey_Escape,
187   OSGKKey_Space,
188   OSGKKey_PageUp,
189   OSGKKey_PageDown,
190   OSGKKey_End,
191   OSGKKey_Home,
192   OSGKKey_Left,
193   OSGKKey_Up,
194   OSGKKey_Right,
195   OSGKKey_Down,
196   OSGKKey_Insert,
197   OSGKKey_Delete,
198   OSGKKey_F1,
199   OSGKKey_F2,
200   OSGKKey_F3,
201   OSGKKey_F4,
202   OSGKKey_F5,
203   OSGKKey_F6,
204   OSGKKey_F7,
205   OSGKKey_F8,
206   OSGKKey_F9,
207   OSGKKey_F10,
208   OSGKKey_F11,
209   OSGKKey_F12,
210   OSGKKey_NumLock,
211   OSGKKey_ScrollLock,
212   OSGKKey_Meta
213 };
214
215 static int (*osgk_browser_event_key) (
216   OSGK_Browser* browser, unsigned int key,
217   OSGK_KeyboardEventType eventType);
218
219 typedef enum OSGK_AntiAliasType
220 {
221   aaNone,
222   aaGray,
223   aaSubpixel
224 } OSGK_AntiAliasType;
225
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);*/
229
230 /*static void (*osgk_browser_focus) (OSGK_Browser* browser);*/
231
232 static void (*osgk_browser_resize) (OSGK_Browser* browser,
233   int width, int height);
234
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);
239
240 /* OffscreenGecko/string.h */
241 OSGK_DERIVEDTYPE(OSGK_String);
242
243 static const char* (*osgk_string_get) (OSGK_String* str);
244
245 static OSGK_String* (*osgk_string_create) (const char* str);
246
247 /* OffscreenGecko/scriptvariant.h */
248
249 OSGK_CLASSTYPE_DEF nsISupports;
250
251 OSGK_DERIVEDTYPE(OSGK_ScriptVariant);
252
253 typedef enum OSGK_ScriptVariantType
254 {
255   svtEmpty,
256   svtInt,
257   svtUint,
258   svtFloat,
259   svtDouble,
260   svtBool,
261   svtChar,
262   svtString,
263   svtISupports,
264   svtScriptObject,
265   svtArray
266 } OSGK_ScriptVariantType;
267
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);
272
273 /*static int (*osgk_variant_get_int) (OSGK_ScriptVariant* variant,
274   int* val);*/
275 /*static int (*osgk_variant_get_uint) (OSGK_ScriptVariant* variant,
276   unsigned int* val);*/
277 /*static int (*osgk_variant_get_float) (OSGK_ScriptVariant* variant,
278   float* val);*/
279 /*static int (*osgk_variant_get_double) (OSGK_ScriptVariant* variant,
280   double* val);*/
281 /*static int (*osgk_variant_get_bool) (OSGK_ScriptVariant* variant,
282   int* val);*/
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,
287   OSGK_String** val);
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,
292   void** tag);*/
293 /*static int (*osgk_variant_get_array_size) (OSGK_ScriptVariant* variant,
294   size_t* size);*/
295 /*static int (*osgk_variant_get_array_item) (OSGK_ScriptVariant* variant,
296   OSGK_ScriptVariant** val);*/
297
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);*/
320
321 /* OffscreenGecko/scriptobjecttemplate.h */
322
323 OSGK_DERIVEDTYPE(OSGK_ScriptObjectTemplate);
324
325 typedef enum OSGK_ScriptResult
326 {
327   srSuccess = 0,
328   srFailed = 0x80004005L /* actually NS_ERROR_FAILURE */
329 } OSGK_ScriptResult;
330
331 typedef struct OSGK_ScriptObjectCreateParams_s
332 {
333   void* createParam;
334   OSGK_Browser* browser;
335 } OSGK_ScriptObjectCreateParams;
336
337 typedef OSGK_ScriptResult (*OSGK_CreateObjFunc) (
338   OSGK_ScriptObjectCreateParams* params, void** objTag);
339 typedef void (*OSGK_DestroyObjFunc) (void* objTag);
340
341 static OSGK_ScriptObjectTemplate* (*osgk_sot_create) (
342   OSGK_Embedding* embedding,
343   OSGK_CreateObjFunc createFunc, OSGK_DestroyObjFunc destroyFunc,
344   void* createParam);
345
346 typedef OSGK_ScriptVariant* (*OSGK_GetPropertyFunc) (void* objTag,
347   void* propTag);
348 typedef OSGK_ScriptResult (*OSGK_SetPropertyFunc) (void* objTag,
349   void* propTag, OSGK_ScriptVariant* val);
350   
351 /*static int (*osgk_sot_add_property) (
352   OSGK_ScriptObjectTemplate* templ, const char* propName, void* propTag,
353   OSGK_GetPropertyFunc getter, OSGK_SetPropertyFunc setter);*/
354
355 typedef OSGK_ScriptResult (*OSGK_FunctionCallFunc) (void* objTag,
356   void* methTag, size_t numParams, OSGK_ScriptVariant** params,
357   OSGK_ScriptVariant** returnVal);
358
359 static int (*osgk_sot_add_function) (
360   OSGK_ScriptObjectTemplate* templ, const char* funcName, void* funcTag,
361   OSGK_FunctionCallFunc callFunc);
362
363 /*static OSGK_ScriptVariant* (*osgk_sot_instantiate) (
364   OSGK_ScriptObjectTemplate* templ, void** objTag);    */
365
366 static int (*osgk_sot_register) (
367   OSGK_ScriptObjectTemplate* templ, OSGK_Embedding* embedding, 
368   const char* name, unsigned int flags);
369
370 /* --- >8 --- >8 --- End OffscreenGecko headers --- 8< --- 8< --- */
371
372 #include "quakedef.h"
373 #include "cl_dyntexture.h"
374 #include "cl_gecko.h"
375 #include "timing.h"
376
377 #define DEFAULT_GECKO_SIZE        512
378
379 static rtexturepool_t *cl_geckotexturepool;
380 static OSGK_Embedding *cl_geckoembedding;
381
382 struct clgecko_s {
383         qboolean active;
384         char name[ MAX_QPATH + 32 ];
385         int ownerProg;
386
387         OSGK_Browser *browser;
388         int width, height;
389         int texWidth, texHeight;
390         
391         rtexture_t *texture;
392 };
393
394 #define USERDATAKEY_CL_GECKO_T        0
395
396 static dllhandle_t osgk_dll = NULL;
397
398 static clgecko_t cl_geckoinstances[ MAX_GECKO_INSTANCES ];
399
400 static clgecko_t * cl_gecko_findunusedinstance( void ) {
401         int i;
402         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
403                 clgecko_t *instance = &cl_geckoinstances[ i ];
404                 if( !instance->active ) {
405                         return instance;
406                 }
407         }
408         if( developer.integer > 0 ) {
409                 Con_Printf( "cl_gecko_findunusedinstance: out of geckos\n" );
410         }
411         return NULL;
412 }
413
414 clgecko_t * CL_Gecko_FindBrowser( const char *name ) {
415         int i;
416
417         if( !name || !*name || strncmp( name, CLGECKOPREFIX, sizeof( CLGECKOPREFIX ) - 1 ) != 0 ) {
418                 if( developer.integer > 0 ) {
419                         Con_Printf( "CL_Gecko_FindBrowser: Bad gecko texture name '%s'!\n", name );
420                 }
421                 return NULL;
422         }
423
424         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
425                 clgecko_t *instance = &cl_geckoinstances[ i ];
426                 if( instance->active && strcmp( instance->name, name ) == 0 ) {
427                         return instance;
428                 }
429         }
430
431         if( developer.integer > 0 ) {
432                 Con_Printf( "CL_Gecko_FindBrowser: No browser named '%s'!\n", name );
433         }
434
435         return NULL;
436 }
437
438 static void cl_gecko_updatecallback( rtexture_t *texture, void* callbackData ) {
439         clgecko_t *instance = callbackData;
440         const unsigned char *data;
441         if( instance->browser ) {
442                 // TODO: OSGK only supports BGRA right now
443                 TIMING_TIMESTATEMENT(data = osgk_browser_lock_data( instance->browser, NULL ));
444                 R_UpdateTexture( texture, data, 0, 0, instance->width, instance->height );
445                 osgk_browser_unlock_data( instance->browser, data );
446         }
447 }
448
449 static void cl_gecko_linktexture( clgecko_t *instance ) {
450         // TODO: assert that instance->texture == NULL
451         instance->texture = R_LoadTexture2D( cl_geckotexturepool, instance->name, 
452                 instance->texWidth, instance->texHeight, NULL, TEXTYPE_BGRA, TEXF_ALPHA | TEXF_PERSISTENT, NULL );
453         R_MakeTextureDynamic( instance->texture, cl_gecko_updatecallback, instance );
454         CL_LinkDynTexture( instance->name, instance->texture );
455 }
456
457 static void cl_gecko_unlinktexture( clgecko_t *instance ) {
458         if( instance->texture ) {
459                 CL_UnlinkDynTexture( instance->name );
460                 R_FreeTexture( instance->texture );
461                 instance->texture = NULL;
462         }
463 }
464
465 void CL_Gecko_Resize( clgecko_t *instance, int width, int height ) {
466         int newWidth, newHeight;
467
468         // early out if bad parameters are passed (no resize to a texture size bigger than the original texture size)
469         if( !instance || !instance->browser) {
470                 return;
471         }
472
473         newWidth = CeilPowerOf2( width );
474         newHeight = CeilPowerOf2( height );
475         if ((newWidth != instance->texWidth) || (newHeight != instance->texHeight))
476         {
477                 cl_gecko_unlinktexture( instance );
478                 instance->texWidth = newWidth;
479                 instance->texHeight = newHeight;
480                 cl_gecko_linktexture( instance );
481         }
482         else
483         {
484                 /* The gecko area will only cover a part of the texture; to avoid
485                 'old' pixels bleeding in at the border clear the texture. */
486                 R_ClearTexture( instance->texture );
487         }
488
489         osgk_browser_resize( instance->browser, width, height);
490         instance->width = width;
491         instance->height = height;
492 }
493
494 void CL_Gecko_GetTextureExtent( clgecko_t *instance, float* pwidth, float* pheight )
495 {
496         if( !instance || !instance->browser ) {
497                 return;
498         }
499
500         *pwidth = (float)instance->width / instance->texWidth;
501         *pheight = (float)instance->height / instance->texHeight;
502 }
503
504 static OSGK_ScriptResult dpGlobal_create (OSGK_ScriptObjectCreateParams* params, 
505                                           void** objTag)
506 {
507   if (!osgk_browser_get_user_data (params->browser, USERDATAKEY_CL_GECKO_T, objTag))
508     return srFailed;
509   return srSuccess;
510 }
511
512 static OSGK_ScriptResult dpGlobal_query (void* objTag, void* methTag, 
513                                          size_t numParams, 
514                                          OSGK_ScriptVariant** params,
515                                          OSGK_ScriptVariant** returnVal)
516 {
517   clgecko_t *instance = objTag;
518   OSGK_ScriptVariant* strVal;
519   OSGK_ScriptResult result = srFailed;
520   prvm_prog_t * saveProg;
521
522   /* Can happen when created from console */
523   if (instance->ownerProg < 0) return srFailed;
524
525   /* Require exactly one param, for now */
526   if (numParams != 1) return srFailed;
527
528   strVal = osgk_variant_convert (params[0], svtString);
529   if (strVal == 0) return srFailed;
530
531   saveProg = prog;
532   PRVM_SetProg(instance->ownerProg);
533
534   if (prog->funcoffsets.Gecko_Query)
535   {
536     OSGK_String* paramStr, *resultStr;
537
538     if (!osgk_variant_get_string (strVal, &paramStr)) return srFailed;
539     *((string_t*)&prog->globals.generic[OFS_PARM0]) = PRVM_SetTempString (osgk_string_get (paramStr));
540     PRVM_ExecuteProgram(prog->funcoffsets.Gecko_Query,"Gecko_Query() required");
541     resultStr = osgk_string_create (PRVM_G_STRING (OFS_RETURN));
542     *returnVal = osgk_variant_create_string (cl_geckoembedding, resultStr);
543     osgk_release (resultStr);
544     
545     result = srSuccess;
546   }
547
548   prog = saveProg;
549   
550   return result;
551 }
552
553 #if defined(WIN64)
554 # define XULRUNNER_DIR_SUFFIX   "win64"
555 #elif defined(WIN32)
556 # define XULRUNNER_DIR_SUFFIX   "win32"
557 #elif defined(DP_OS_STR) && defined(DP_ARCH_STR)
558 # define XULRUNNER_DIR_SUFFIX   DP_OS_STR "-" DP_ARCH_STR
559 #endif
560
561 static qboolean CL_Gecko_Embedding_Init (void)
562 {
563         char profile_path [MAX_OSPATH];
564         OSGK_GeckoResult grc;
565         OSGK_EmbeddingOptions *options;
566         OSGK_ScriptObjectTemplate* dpGlobalTemplate;
567
568         if (!osgk_dll) return false;
569
570         if( cl_geckoembedding != NULL ) return true;
571
572         if( developer.integer > 0 ) {
573                 Con_Printf( "CL_Gecko_Embedding_Init: setting up gecko embedding\n" );
574         }
575
576         options = osgk_embedding_options_create();
577 #ifdef XULRUNNER_DIR_SUFFIX
578         osgk_embedding_options_add_search_path( options, "./xulrunner-" XULRUNNER_DIR_SUFFIX "/" );
579 #endif
580         osgk_embedding_options_add_search_path( options, "./xulrunner/" );
581         dpsnprintf (profile_path, sizeof (profile_path), "%s/xulrunner_profile/", fs_gamedir);
582         osgk_embedding_options_set_profile_dir( options, profile_path, 0 );
583         cl_geckoembedding = osgk_embedding_create_with_options( options, &grc );
584         osgk_release( options );
585                 
586         if( cl_geckoembedding == NULL ) {
587                 Con_Printf( "CL_Gecko_Embedding_Init: Couldn't retrieve gecko embedding object (%.8x)!\n", grc );
588                 return false;
589         } 
590         
591         if( developer.integer > 0 ) {
592                 Con_Printf( "CL_Gecko_Embedding_Init: Embedding set up correctly\n" );
593         }
594
595         dpGlobalTemplate = osgk_sot_create( cl_geckoembedding, dpGlobal_create, NULL, NULL );
596
597         osgk_sot_add_function (dpGlobalTemplate, "query", 0, dpGlobal_query);
598
599         osgk_sot_register (dpGlobalTemplate, cl_geckoembedding, "Darkplaces", 0);
600         osgk_release( dpGlobalTemplate );
601
602         return true;
603 }
604
605 clgecko_t * CL_Gecko_CreateBrowser( const char *name, int ownerProg ) {
606         clgecko_t *instance;
607
608         if (!CL_Gecko_Embedding_Init ()) return NULL;
609
610         // TODO: verify that we dont use a name twice
611         instance = cl_gecko_findunusedinstance();
612         // TODO: assert != NULL
613
614         instance->active = true;
615         strlcpy( instance->name, name, sizeof( instance->name ) );
616         instance->browser = osgk_browser_create( cl_geckoembedding, DEFAULT_GECKO_SIZE, DEFAULT_GECKO_SIZE );
617         if( instance->browser == NULL ) {
618                 Con_Printf( "CL_Gecko_CreateBrowser: Browser object creation failed!\n" );
619         }
620         // TODO: assert != NULL
621         osgk_browser_set_user_data (instance->browser, USERDATAKEY_CL_GECKO_T,
622           instance, 0);
623         instance->ownerProg = ownerProg;
624
625         instance->width = instance->texWidth = DEFAULT_GECKO_SIZE;
626         instance->height = instance->texHeight = DEFAULT_GECKO_SIZE;
627         cl_gecko_linktexture( instance );
628
629         return instance;
630 }
631
632 void CL_Gecko_DestroyBrowser( clgecko_t *instance ) {
633    if( !instance || !instance->active ) {
634                 return;
635         }
636
637         instance->active = false;
638         cl_gecko_unlinktexture( instance );
639
640         osgk_release( instance->browser );
641         instance->browser = NULL;
642 }
643
644 void CL_Gecko_Frame( void ) {
645         int i;
646         // FIXME: track cl_numgeckoinstances to avoid scanning the entire array?
647         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
648                 clgecko_t *instance = &cl_geckoinstances[ i ];
649                 if( instance->active ) {
650                         if( instance->browser && osgk_browser_query_dirty( instance->browser ) == 1 ) {
651                                 R_MarkDirtyTexture( instance->texture );
652                         }
653                 }
654         }
655 }
656
657 static void cl_gecko_start( void )
658 {
659         int i;
660         cl_geckotexturepool = R_AllocTexturePool();
661
662         // recreate textures on module start
663         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
664                 clgecko_t *instance = &cl_geckoinstances[ i ];
665                 if( instance->active ) {
666                         cl_gecko_linktexture( instance );
667                 }
668         }
669 }
670
671 static void cl_gecko_shutdown( void )
672 {
673         int i;
674         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
675                 clgecko_t *instance = &cl_geckoinstances[ i ];
676                 if( instance->active ) {
677                         cl_gecko_unlinktexture( instance );
678                 }
679         }
680         R_FreeTexturePool( &cl_geckotexturepool );
681 }
682
683 static void cl_gecko_newmap( void )
684 {
685         // DO NOTHING
686 }
687
688 void CL_Gecko_Shutdown( void ) {
689         int i;
690         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
691                 clgecko_t *instance = &cl_geckoinstances[ i ];
692                 if( instance->active ) {
693                         cl_gecko_unlinktexture( instance );
694                 }               
695         }
696
697         if (cl_geckoembedding != NULL)
698         {
699             osgk_release( cl_geckoembedding );
700             cl_geckoembedding = NULL;
701         }
702
703         if (osgk_dll != NULL)
704         {
705             Sys_UnloadLibrary (&osgk_dll);
706         }
707 }
708
709 static void cl_gecko_create_f( void ) {
710         char name[MAX_QPATH];
711
712         if (Cmd_Argc() != 2)
713         {
714                 Con_Print("usage: gecko_create <name>\npcreates a browser (full texture path " CLGECKOPREFIX "<name>)\n");
715                 return;
716         }
717
718         // TODO: use snprintf instead
719         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));
720         CL_Gecko_CreateBrowser( name, -1 );
721 }
722
723 static void cl_gecko_destroy_f( void ) {
724         char name[MAX_QPATH];
725
726         if (Cmd_Argc() != 2)
727         {
728                 Con_Print("usage: gecko_destroy <name>\ndestroys a browser\n");
729                 return;
730         }
731
732         // TODO: use snprintf instead
733         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));
734         CL_Gecko_DestroyBrowser( CL_Gecko_FindBrowser( name ) );
735 }
736
737 static void cl_gecko_navigate_f( void ) {
738         char name[MAX_QPATH];
739         const char *URI;
740
741         if (Cmd_Argc() != 3)
742         {
743                 Con_Print("usage: gecko_navigate <name> <URI>\nnavigates to a certain URI\n");
744                 return;
745         }
746
747         // TODO: use snprintf instead
748         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));
749         URI = Cmd_Argv( 2 );
750         CL_Gecko_NavigateToURI( CL_Gecko_FindBrowser( name ), URI );
751 }
752
753 static void cl_gecko_injecttext_f( void ) {
754         char name[MAX_QPATH];
755         const char *text;
756         clgecko_t *instance;
757         const char *p;
758
759         if (Cmd_Argc() < 3)
760         {
761                 Con_Print("usage: gecko_injecttext <name> <text>\ninjects a certain text into the browser\n");
762                 return;
763         }
764
765         // TODO: use snprintf instead
766         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));
767         instance = CL_Gecko_FindBrowser( name );
768         if( !instance ) {
769                 Con_Printf( "cl_gecko_injecttext_f: gecko instance '%s' couldn't be found!\n", name );
770                 return;
771         }
772
773         text = Cmd_Argv( 2 );
774
775         for( p = text ; *p ; p++ ) {
776                 unsigned key = *p;
777                 switch( key ) {
778                         case ' ':
779                                 key = K_SPACE;
780                                 break;
781                         case '\\':
782                                 key = *++p;
783                                 switch( key ) {
784                                 case 'n':
785                                         key = K_ENTER;
786                                         break;
787                                 case '\0':
788                                         --p;
789                                         key = '\\';
790                                         break;
791                                 }
792                                 break;
793                 }
794
795                 CL_Gecko_Event_Key( instance, key, CLG_BET_PRESS );
796         }
797 }
798
799 static void gl_gecko_movecursor_f( void ) {
800         char name[MAX_QPATH];
801         float x, y;
802
803         if (Cmd_Argc() != 4)
804         {
805                 Con_Print("usage: gecko_movecursor <name> <x> <y>\nmove the cursor to a certain position\n");
806                 return;
807         }
808
809         // TODO: use snprintf instead
810         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));
811         x = atof( Cmd_Argv( 2 ) );
812         y = atof( Cmd_Argv( 3 ) );
813
814         CL_Gecko_Event_CursorMove( CL_Gecko_FindBrowser( name ), x, y );
815 }
816
817 #undef osgk_addref
818 #undef osgk_release
819
820 static const dllfunction_t osgkFuncs[] =
821 {
822         {"osgk_addref",                             (void **) &osgk_addref},
823         {"osgk_release",                            (void **) &osgk_release},
824         {"osgk_embedding_create2",                  (void **) &osgk_embedding_create2},
825         {"osgk_embedding_options_create",           (void **) &osgk_embedding_options_create},
826         {"osgk_embedding_options_add_search_path",  (void **) &osgk_embedding_options_add_search_path},
827         {"osgk_embedding_options_set_profile_dir",  (void **) &osgk_embedding_options_set_profile_dir},
828         {"osgk_browser_create",                     (void **) &osgk_browser_create},
829         {"osgk_browser_query_dirty",                (void **) &osgk_browser_query_dirty},
830         {"osgk_browser_navigate",                   (void **) &osgk_browser_navigate},
831         {"osgk_browser_lock_data",                  (void **) &osgk_browser_lock_data},
832         {"osgk_browser_unlock_data",                (void **) &osgk_browser_unlock_data},
833         {"osgk_browser_resize",                     (void **) &osgk_browser_resize},
834         {"osgk_browser_event_mouse_move",           (void **) &osgk_browser_event_mouse_move},
835         {"osgk_browser_event_mouse_button",         (void **) &osgk_browser_event_mouse_button},
836         {"osgk_browser_event_mouse_wheel",          (void **) &osgk_browser_event_mouse_wheel},
837         {"osgk_browser_event_key",                  (void **) &osgk_browser_event_key},
838         {"osgk_browser_set_user_data",              (void **) &osgk_browser_set_user_data},
839         {"osgk_browser_get_user_data",              (void **) &osgk_browser_get_user_data},
840         {"osgk_sot_create",                         (void **) &osgk_sot_create},
841         {"osgk_sot_register",                       (void **) &osgk_sot_register},
842         {"osgk_sot_add_function",                   (void **) &osgk_sot_add_function},
843         {"osgk_string_get",                         (void **) &osgk_string_get},
844         {"osgk_string_create",                      (void **) &osgk_string_create},
845         {"osgk_variant_convert",                    (void **) &osgk_variant_convert},
846         {"osgk_variant_get_string",                 (void **) &osgk_variant_get_string},
847         {"osgk_variant_create_string",              (void **) &osgk_variant_create_string},
848         {NULL, NULL}
849 };
850
851 void CL_Gecko_Init( void )
852 {
853         const char* dllnames [] =
854         {
855         #if defined(WIN64)
856                 "OffscreenGecko64.dll",
857         #elif defined(WIN32)
858                 "OffscreenGecko.dll",
859         #elif defined(MACOSX)
860                 "OffscreenGecko.dylib",
861         #else
862                 "libOffscreenGecko.so",
863         #endif
864                 NULL
865         };
866         
867         if (!osgk_dll)
868         {
869                 if (! Sys_LoadLibrary (dllnames, &osgk_dll, osgkFuncs))
870                 {
871                         Con_Printf ("Could not load OffscreenGecko, Gecko support unavailable\n");
872                 }
873         }
874
875         Cmd_AddCommand( "gecko_create", cl_gecko_create_f, "Create a gecko browser instance" );
876         Cmd_AddCommand( "gecko_destroy", cl_gecko_destroy_f, "Destroy a gecko browser instance" );
877         Cmd_AddCommand( "gecko_navigate", cl_gecko_navigate_f, "Navigate a gecko browser to a URI" );
878         Cmd_AddCommand( "gecko_injecttext", cl_gecko_injecttext_f, "Injects text into a browser" );
879         Cmd_AddCommand( "gecko_movecursor", gl_gecko_movecursor_f, "Move the cursor to a certain position" );
880
881         R_RegisterModule( "CL_Gecko", cl_gecko_start, cl_gecko_shutdown, cl_gecko_newmap );
882 }
883
884 void CL_Gecko_NavigateToURI( clgecko_t *instance, const char *URI ) {
885         if( !instance || !instance->browser ) {
886                 return;
887         }
888
889         if( instance->active ) {
890                 osgk_browser_navigate( instance->browser, URI );
891         }
892 }
893
894 void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y ) {
895         // TODO: assert x, y \in [0.0, 1.0]
896         int mappedx, mappedy;
897
898         if( !instance || !instance->browser ) {
899                 return;
900         }
901
902         mappedx = x * instance->width;
903         mappedy = y * instance->height;
904         osgk_browser_event_mouse_move( instance->browser, mappedx, mappedy );
905 }
906
907 typedef struct geckokeymapping_s {
908         keynum_t keycode;
909         unsigned int geckokeycode;
910 } geckokeymapping_t;
911
912 static geckokeymapping_t geckokeymappingtable[] = {
913         { K_BACKSPACE, OSGKKey_Backspace },
914         { K_TAB, OSGKKey_Tab },
915         { K_ENTER, OSGKKey_Return },
916         { K_SHIFT, OSGKKey_Shift },
917         { K_CTRL, OSGKKey_Control },
918         { K_ALT, OSGKKey_Alt },
919         { K_CAPSLOCK, OSGKKey_CapsLock },
920         { K_ESCAPE, OSGKKey_Escape },
921         { K_SPACE, OSGKKey_Space },
922         { K_PGUP, OSGKKey_PageUp },
923         { K_PGDN, OSGKKey_PageDown },
924         { K_END, OSGKKey_End },
925         { K_HOME, OSGKKey_Home },
926         { K_LEFTARROW, OSGKKey_Left },
927         { K_UPARROW, OSGKKey_Up },
928         { K_RIGHTARROW, OSGKKey_Right },
929         { K_DOWNARROW, OSGKKey_Down },
930         { K_INS, OSGKKey_Insert },
931         { K_DEL, OSGKKey_Delete },
932         { K_F1, OSGKKey_F1 },
933         { K_F2, OSGKKey_F2 },
934         { K_F3, OSGKKey_F3 },
935         { K_F4, OSGKKey_F4 },
936         { K_F5, OSGKKey_F5 },
937         { K_F6, OSGKKey_F6 },
938         { K_F7, OSGKKey_F7 },
939         { K_F8, OSGKKey_F8 },
940         { K_F9, OSGKKey_F9 },
941         { K_F10, OSGKKey_F10 },
942         { K_F11, OSGKKey_F11 },
943         { K_F12, OSGKKey_F12 },
944         { K_NUMLOCK, OSGKKey_NumLock },
945         { K_SCROLLOCK, OSGKKey_ScrollLock }
946 };
947
948 qboolean CL_Gecko_Event_Key( clgecko_t *instance, keynum_t key, clgecko_buttoneventtype_t eventtype ) {
949         if( !instance || !instance->browser ) {
950                 return false;
951         }
952
953         // determine whether its a keyboard event
954         if( key < K_OTHERDEVICESBEGIN ) {
955
956                 OSGK_KeyboardEventType mappedtype = kePress;
957                 unsigned int mappedkey = key;
958                 
959                 unsigned int i;
960                 // yes! then convert it if necessary!
961                 for( i = 0 ; i < sizeof( geckokeymappingtable ) / sizeof( *geckokeymappingtable ) ; i++ ) {
962                         const geckokeymapping_t * const mapping = &geckokeymappingtable[ i ];
963                         if( key == mapping->keycode ) {
964                                 mappedkey = mapping->geckokeycode;
965                                 break;
966                         }
967                 }
968
969                 // convert the eventtype
970                 // map the type
971                 switch( eventtype ) {
972                 case CLG_BET_DOWN:
973                         mappedtype = keDown;
974                         break;
975                 case CLG_BET_UP:
976                         mappedtype = keUp;
977                         break;
978                 case CLG_BET_DOUBLECLICK:
979                         // TODO: error message
980                         break;
981                 case CLG_BET_PRESS:
982                         mappedtype = kePress;
983                 }
984
985                 return osgk_browser_event_key( instance->browser, mappedkey, mappedtype ) != 0;
986         } else if( K_MOUSE1 <= key && key <= K_MOUSE3 ) {
987                 OSGK_MouseButtonEventType mappedtype = meDoubleClick;
988                 OSGK_MouseButton mappedbutton;
989
990                 mappedbutton = (OSGK_MouseButton) (mbLeft + (key - K_MOUSE1));
991
992                 switch( eventtype ) {
993                 case CLG_BET_DOWN:
994                         mappedtype = meDown;
995                         break;
996                 case CLG_BET_UP:
997                         mappedtype = meUp;
998                         break;
999                 case CLG_BET_DOUBLECLICK:
1000                         mappedtype = meDoubleClick;
1001                         break;
1002                 case CLG_BET_PRESS:
1003                         // hihi, hacky hacky
1004                         osgk_browser_event_mouse_button( instance->browser, mappedbutton, meDown );
1005                         mappedtype = meUp;
1006                         break;
1007                 }
1008
1009                 osgk_browser_event_mouse_button( instance->browser, mappedbutton, mappedtype );
1010                 return true;
1011         } else if( K_MWHEELUP <= key && key <= K_MWHEELDOWN ) {
1012                 if( eventtype == CLG_BET_DOWN )
1013                         osgk_browser_event_mouse_wheel( instance->browser, 
1014                                 waVertical, (key == K_MWHEELUP) ? wdNegative : wdPositive );
1015                 return true;
1016         }
1017         // TODO: error?
1018         return false;
1019 }