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