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