Made gecko bindings a bit more chatty in developer mode.
[divverent/darkplaces.git] / cl_gecko.c
1 #ifdef SUPPORT_GECKO\r
2 \r
3 // includes everything!\r
4 #include <OffscreenGecko/browser.h>\r
5 \r
6 #ifdef _MSC_VER\r
7 #       pragma comment( lib, "OffscreenGecko" )\r
8 #endif\r
9 \r
10 #include "quakedef.h"\r
11 #include "cl_dyntexture.h"\r
12 #include "cl_gecko.h"\r
13 #include "timing.h"\r
14 \r
15 #define DEFAULT_GECKO_SIZE        512\r
16 \r
17 static rtexturepool_t *cl_geckotexturepool;\r
18 static OSGK_Embedding *cl_geckoembedding;\r
19 \r
20 struct clgecko_s {\r
21         qboolean active;\r
22         char name[ MAX_QPATH + 32 ];\r
23 \r
24         OSGK_Browser *browser;\r
25         int width, height;\r
26         int texWidth, texHeight;\r
27         \r
28         rtexture_t *texture;\r
29 };\r
30 \r
31 static clgecko_t cl_geckoinstances[ MAX_GECKO_INSTANCES ];\r
32 \r
33 static clgecko_t * cl_gecko_findunusedinstance( void ) {\r
34         int i;\r
35         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
36                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
37                 if( !instance->active ) {\r
38                         return instance;\r
39                 }\r
40         }\r
41         if( developer.integer > 0 ) {\r
42                 Con_Printf( "cl_gecko_findunusedinstance: out of geckos\n" );\r
43         }\r
44         return NULL;\r
45 }\r
46 \r
47 clgecko_t * CL_Gecko_FindBrowser( const char *name ) {\r
48         int i;\r
49 \r
50         if( !name || !*name || strncmp( name, CLGECKOPREFIX, sizeof( CLGECKOPREFIX ) - 1 ) != 0 ) {\r
51                 if( developer.integer > 0 ) {\r
52                         Con_Printf( "CL_Gecko_FindBrowser: Bad gecko texture name '%s'!\n", name );\r
53                 }\r
54                 return NULL;\r
55         }\r
56 \r
57         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
58                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
59                 if( instance->active && strcmp( instance->name, name ) == 0 ) {\r
60                         return instance;\r
61                 }\r
62         }\r
63 \r
64         if( developer.integer > 0 ) {\r
65                 Con_Printf( "CL_Gecko_FindBrowser: No browser named '%s'!\n", name );\r
66         }\r
67 \r
68         return NULL;\r
69 }\r
70 \r
71 static void cl_gecko_updatecallback( rtexture_t *texture, clgecko_t *instance ) {\r
72         const unsigned char *data;\r
73         if( instance->browser ) {\r
74                 // TODO: OSGK only supports BGRA right now\r
75                 TIMING_TIMESTATEMENT(data = osgk_browser_lock_data( instance->browser, NULL ));\r
76                 R_UpdateTexture( texture, data, 0, 0, instance->width, instance->height );\r
77                 osgk_browser_unlock_data( instance->browser, data );\r
78         }\r
79 }\r
80 \r
81 static void cl_gecko_linktexture( clgecko_t *instance ) {\r
82         // TODO: assert that instance->texture == NULL\r
83         instance->texture = R_LoadTexture2D( cl_geckotexturepool, instance->name, \r
84                 instance->texWidth, instance->texHeight, NULL, TEXTYPE_BGRA, TEXF_ALPHA | TEXF_PERSISTENT, NULL );\r
85         R_MakeTextureDynamic( instance->texture, cl_gecko_updatecallback, instance );\r
86         CL_LinkDynTexture( instance->name, instance->texture );\r
87 }\r
88 \r
89 static void cl_gecko_unlinktexture( clgecko_t *instance ) {\r
90         if( instance->texture ) {\r
91                 CL_UnlinkDynTexture( instance->name );\r
92                 R_FreeTexture( instance->texture );\r
93                 instance->texture = NULL;\r
94         }\r
95 }\r
96 \r
97 void CL_Gecko_Resize( clgecko_t *instance, int width, int height ) {\r
98         int newWidth, newHeight;\r
99 \r
100         // early out if bad parameters are passed (no resize to a texture size bigger than the original texture size)\r
101         if( !instance || !instance->browser) {\r
102                 return;\r
103         }\r
104 \r
105         newWidth = CeilPowerOf2( width );\r
106         newHeight = CeilPowerOf2( height );\r
107         if ((newWidth != instance->texWidth) || (newHeight != instance->texHeight))\r
108         {\r
109                 cl_gecko_unlinktexture( instance );\r
110                 instance->texWidth = newWidth;\r
111                 instance->texHeight = newHeight;\r
112                 cl_gecko_linktexture( instance );\r
113         }\r
114         else\r
115         {\r
116                 /* The gecko area will only cover a part of the texture; to avoid\r
117                 'old' pixels bleeding in at the border clear the texture. */\r
118                 R_ClearTexture( instance->texture );\r
119         }\r
120 \r
121         osgk_browser_resize( instance->browser, width, height);\r
122         instance->width = width;\r
123         instance->height = height;\r
124 }\r
125 \r
126 void CL_Gecko_GetTextureExtent( clgecko_t *instance, float* pwidth, float* pheight )\r
127 {\r
128         if( !instance || !instance->browser ) {\r
129                 return;\r
130         }\r
131 \r
132         *pwidth = (float)instance->width / instance->texWidth;\r
133         *pheight = (float)instance->height / instance->texHeight;\r
134 }\r
135 \r
136 \r
137 clgecko_t * CL_Gecko_CreateBrowser( const char *name ) {\r
138         // TODO: verify that we dont use a name twice\r
139         clgecko_t *instance = cl_gecko_findunusedinstance();\r
140         // TODO: assert != NULL\r
141         \r
142         if( cl_geckoembedding == NULL ) {\r
143                 char profile_path [MAX_OSPATH];\r
144                 OSGK_GeckoResult grc;\r
145                 OSGK_EmbeddingOptions *options;\r
146 \r
147                 if( developer.integer > 0 ) {\r
148                         Con_Printf( "CL_Gecko_CreateBrowser: setting up gecko embedding\n" );\r
149                 }\r
150 \r
151                 options = osgk_embedding_options_create();\r
152                 osgk_embedding_options_add_search_path( options, "./xulrunner/" );\r
153                 dpsnprintf (profile_path, sizeof (profile_path), "%s/xulrunner_profile/", fs_gamedir);\r
154                 osgk_embedding_options_set_profile_dir( options, profile_path, 0 );\r
155                 cl_geckoembedding = osgk_embedding_create_with_options( options, &grc );\r
156                 osgk_release( options );\r
157                 \r
158                 if( cl_geckoembedding == NULL ) {\r
159                         Con_Printf( "CL_Gecko_CreateBrowser: Couldn't retrieve gecko embedding object (%.8x)!\n", grc );\r
160                         return NULL;\r
161                 } else if( developer.integer > 0 ) {\r
162                         Con_Printf( "CL_Gecko_CreateBrowser: Embedding set up correctly\n" );\r
163                 }\r
164         }\r
165 \r
166         instance->active = true;\r
167         strlcpy( instance->name, name, sizeof( instance->name ) );\r
168         instance->browser = osgk_browser_create( cl_geckoembedding, DEFAULT_GECKO_SIZE, DEFAULT_GECKO_SIZE );\r
169         if( instance->browser == NULL ) {\r
170                 Con_Printf( "CL_Gecko_CreateBrowser: Browser object creation failed!\n" );\r
171         }\r
172         // TODO: assert != NULL\r
173 \r
174         instance->width = instance->texWidth = DEFAULT_GECKO_SIZE;\r
175         instance->height = instance->texHeight = DEFAULT_GECKO_SIZE;\r
176         cl_gecko_linktexture( instance );\r
177 \r
178         return instance;\r
179 }\r
180 \r
181 void CL_Gecko_DestroyBrowser( clgecko_t *instance ) {\r
182    if( !instance || !instance->active ) {\r
183                 return;\r
184         }\r
185 \r
186         instance->active = false;\r
187         cl_gecko_unlinktexture( instance );\r
188 \r
189         osgk_release( instance->browser );\r
190         instance->browser = NULL;\r
191 }\r
192 \r
193 void CL_Gecko_Frame( void ) {\r
194         int i;\r
195         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
196                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
197                 if( instance->active ) {\r
198                         if( instance->browser && osgk_browser_query_dirty( instance->browser ) == 1 ) {\r
199                                 R_MarkDirtyTexture( instance->texture );\r
200                         }\r
201                 }\r
202         }\r
203 }\r
204 \r
205 static void cl_gecko_start( void )\r
206 {\r
207         int i;\r
208         cl_geckotexturepool = R_AllocTexturePool();\r
209 \r
210         // recreate textures on module start\r
211         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
212                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
213                 if( instance->active ) {\r
214                         cl_gecko_linktexture( instance );\r
215                 }\r
216         }\r
217 }\r
218 \r
219 static void cl_gecko_shutdown( void )\r
220 {\r
221         int i;\r
222         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
223                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
224                 if( instance->active ) {\r
225                         cl_gecko_unlinktexture( instance );\r
226                 }\r
227         }\r
228         R_FreeTexturePool( &cl_geckotexturepool );\r
229 }\r
230 \r
231 static void cl_gecko_newmap( void )\r
232 {\r
233         // DO NOTHING\r
234 }\r
235 \r
236 void CL_Gecko_Shutdown( void ) {\r
237         int i;\r
238         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
239                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
240                 if( instance->active ) {\r
241                         cl_gecko_unlinktexture( instance );\r
242                 }               \r
243         }\r
244 \r
245         if (cl_geckoembedding != NULL)\r
246         {\r
247             osgk_release( cl_geckoembedding );\r
248             cl_geckoembedding = NULL;\r
249         }\r
250 }\r
251 \r
252 static void cl_gecko_create_f( void ) {\r
253         char name[MAX_QPATH];\r
254 \r
255         if (Cmd_Argc() != 2)\r
256         {\r
257                 Con_Print("usage: gecko_create <name>\npcreates a browser (full texture path " CLGECKOPREFIX "<name>)\n");\r
258                 return;\r
259         }\r
260 \r
261         // TODO: use snprintf instead\r
262         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
263         CL_Gecko_CreateBrowser( name );\r
264 }\r
265 \r
266 static void cl_gecko_destroy_f( void ) {\r
267         char name[MAX_QPATH];\r
268 \r
269         if (Cmd_Argc() != 2)\r
270         {\r
271                 Con_Print("usage: gecko_destroy <name>\ndestroys a browser\n");\r
272                 return;\r
273         }\r
274 \r
275         // TODO: use snprintf instead\r
276         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
277         CL_Gecko_DestroyBrowser( CL_Gecko_FindBrowser( name ) );\r
278 }\r
279 \r
280 static void cl_gecko_navigate_f( void ) {\r
281         char name[MAX_QPATH];\r
282         const char *URI;\r
283 \r
284         if (Cmd_Argc() != 3)\r
285         {\r
286                 Con_Print("usage: gecko_navigate <name> <URI>\nnavigates to a certain URI\n");\r
287                 return;\r
288         }\r
289 \r
290         // TODO: use snprintf instead\r
291         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
292         URI = Cmd_Argv( 2 );\r
293         CL_Gecko_NavigateToURI( CL_Gecko_FindBrowser( name ), URI );\r
294 }\r
295 \r
296 static void cl_gecko_injecttext_f( void ) {\r
297         char name[MAX_QPATH];\r
298         const char *text;\r
299         clgecko_t *instance;\r
300         const char *p;\r
301 \r
302         if (Cmd_Argc() < 3)\r
303         {\r
304                 Con_Print("usage: gecko_injecttext <name> <text>\ninjects a certain text into the browser\n");\r
305                 return;\r
306         }\r
307 \r
308         // TODO: use snprintf instead\r
309         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
310         instance = CL_Gecko_FindBrowser( name );\r
311         if( !instance ) {\r
312                 Con_Printf( "cl_gecko_injecttext_f: gecko instance '%s' couldn't be found!\n", name );\r
313                 return;\r
314         }\r
315 \r
316         text = Cmd_Argv( 2 );\r
317 \r
318         for( p = text ; *p ; p++ ) {\r
319                 unsigned key = *p;\r
320                 switch( key ) {\r
321                         case ' ':\r
322                                 key = K_SPACE;\r
323                                 break;\r
324                         case '\\':\r
325                                 key = *++p;\r
326                                 switch( key ) {\r
327                                 case 'n':\r
328                                         key = K_ENTER;\r
329                                         break;\r
330                                 case '\0':\r
331                                         --p;\r
332                                         key = '\\';\r
333                                         break;\r
334                                 }\r
335                                 break;\r
336                 }\r
337 \r
338                 CL_Gecko_Event_Key( instance, key, CLG_BET_PRESS );\r
339         }\r
340 }\r
341 \r
342 static void gl_gecko_movecursor_f( void ) {\r
343         char name[MAX_QPATH];\r
344         float x, y;\r
345 \r
346         if (Cmd_Argc() != 4)\r
347         {\r
348                 Con_Print("usage: gecko_movecursor <name> <x> <y>\nmove the cursor to a certain position\n");\r
349                 return;\r
350         }\r
351 \r
352         // TODO: use snprintf instead\r
353         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
354         x = atof( Cmd_Argv( 2 ) );\r
355         y = atof( Cmd_Argv( 3 ) );\r
356 \r
357         CL_Gecko_Event_CursorMove( CL_Gecko_FindBrowser( name ), x, y );\r
358 }\r
359 \r
360 void CL_Gecko_Init( void )\r
361 {\r
362         Cmd_AddCommand( "gecko_create", cl_gecko_create_f, "Create a gecko browser instance" );\r
363         Cmd_AddCommand( "gecko_destroy", cl_gecko_destroy_f, "Destroy a gecko browser instance" );\r
364         Cmd_AddCommand( "gecko_navigate", cl_gecko_navigate_f, "Navigate a gecko browser to a URI" );\r
365         Cmd_AddCommand( "gecko_injecttext", cl_gecko_injecttext_f, "Injects text into a browser" );\r
366         Cmd_AddCommand( "gecko_movecursor", gl_gecko_movecursor_f, "Move the cursor to a certain position" );\r
367 \r
368         R_RegisterModule( "CL_Gecko", cl_gecko_start, cl_gecko_shutdown, cl_gecko_newmap );\r
369 }\r
370 \r
371 void CL_Gecko_NavigateToURI( clgecko_t *instance, const char *URI ) {\r
372         if( !instance || !instance->browser ) {\r
373                 return;\r
374         }\r
375 \r
376         if( instance->active ) {\r
377                 osgk_browser_navigate( instance->browser, URI );\r
378         }\r
379 }\r
380 \r
381 void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y ) {\r
382         // TODO: assert x, y \in [0.0, 1.0]\r
383         int mappedx, mappedy;\r
384 \r
385         if( !instance || !instance->browser ) {\r
386                 return;\r
387         }\r
388 \r
389         mappedx = x * instance->width;\r
390         mappedy = y * instance->height;\r
391         osgk_browser_event_mouse_move( instance->browser, mappedx, mappedy );\r
392 }\r
393 \r
394 typedef struct geckokeymapping_s {\r
395         keynum_t keycode;\r
396         unsigned int geckokeycode;\r
397 } geckokeymapping_t;\r
398 \r
399 static geckokeymapping_t geckokeymappingtable[] = {\r
400         { K_BACKSPACE, OSGKKey_Backspace },\r
401         { K_TAB, OSGKKey_Tab },\r
402         { K_ENTER, OSGKKey_Return },\r
403         { K_SHIFT, OSGKKey_Shift },\r
404         { K_CTRL, OSGKKey_Control },\r
405         { K_ALT, OSGKKey_Alt },\r
406         { K_CAPSLOCK, OSGKKey_CapsLock },\r
407         { K_ESCAPE, OSGKKey_Escape },\r
408         { K_SPACE, OSGKKey_Space },\r
409         { K_PGUP, OSGKKey_PageUp },\r
410         { K_PGDN, OSGKKey_PageDown },\r
411         { K_END, OSGKKey_End },\r
412         { K_HOME, OSGKKey_Home },\r
413         { K_LEFTARROW, OSGKKey_Left },\r
414         { K_UPARROW, OSGKKey_Up },\r
415         { K_RIGHTARROW, OSGKKey_Right },\r
416         { K_DOWNARROW, OSGKKey_Down },\r
417         { K_INS, OSGKKey_Insert },\r
418         { K_DEL, OSGKKey_Delete },\r
419         { K_F1, OSGKKey_F1 },\r
420         { K_F2, OSGKKey_F2 },\r
421         { K_F3, OSGKKey_F3 },\r
422         { K_F4, OSGKKey_F4 },\r
423         { K_F5, OSGKKey_F5 },\r
424         { K_F6, OSGKKey_F6 },\r
425         { K_F7, OSGKKey_F7 },\r
426         { K_F8, OSGKKey_F8 },\r
427         { K_F9, OSGKKey_F9 },\r
428         { K_F10, OSGKKey_F10 },\r
429         { K_F11, OSGKKey_F11 },\r
430         { K_F12, OSGKKey_F12 },\r
431         { K_NUMLOCK, OSGKKey_NumLock },\r
432         { K_SCROLLOCK, OSGKKey_ScrollLock }\r
433 };\r
434 \r
435 qboolean CL_Gecko_Event_Key( clgecko_t *instance, int key, clgecko_buttoneventtype_t eventtype ) {\r
436         if( !instance || !instance->browser ) {\r
437                 return false;\r
438         }\r
439 \r
440         // determine whether its a keyboard event\r
441         if( key < K_OTHERDEVICESBEGIN ) {\r
442 \r
443                 OSGK_KeyboardEventType mappedtype;\r
444                 unsigned int mappedkey = key;\r
445                 \r
446                 int i;\r
447                 // yes! then convert it if necessary!\r
448                 for( i = 0 ; i < sizeof( geckokeymappingtable ) / sizeof( *geckokeymappingtable ) ; i++ ) {\r
449                         const geckokeymapping_t * const mapping = &geckokeymappingtable[ i ];\r
450                         if( key == mapping->keycode ) {\r
451                                 mappedkey = mapping->geckokeycode;\r
452                                 break;\r
453                         }\r
454                 }\r
455 \r
456                 // convert the eventtype\r
457                 // map the type\r
458                 switch( eventtype ) {\r
459                 case CLG_BET_DOWN:\r
460                         mappedtype = keDown;\r
461                         break;\r
462                 case CLG_BET_UP:\r
463                         mappedtype = keUp;\r
464                         break;\r
465                 case CLG_BET_DOUBLECLICK:\r
466                         // TODO: error message\r
467                         break;\r
468                 case CLG_BET_PRESS:\r
469                         mappedtype = kePress;\r
470                 }\r
471 \r
472                 return osgk_browser_event_key( instance->browser, mappedkey, mappedtype ) != 0;\r
473         } else if( K_MOUSE1 <= key && key <= K_MOUSE3 ) {\r
474                 OSGK_MouseButtonEventType mappedtype;\r
475                 OSGK_MouseButton mappedbutton;\r
476 \r
477                 mappedbutton = (OSGK_MouseButton) (mbLeft + (key - K_MOUSE1));\r
478 \r
479                 switch( eventtype ) {\r
480                 case CLG_BET_DOWN:\r
481                         mappedtype = meDown;\r
482                         break;\r
483                 case CLG_BET_UP:\r
484                         mappedtype = meUp;\r
485                         break;\r
486                 case CLG_BET_DOUBLECLICK:\r
487                         mappedtype = meDoubleClick;\r
488                         break;\r
489                 case CLG_BET_PRESS:\r
490                         // hihi, hacky hacky\r
491                         osgk_browser_event_mouse_button( instance->browser, mappedbutton, meDown );\r
492                         mappedtype = meUp;\r
493                         break;\r
494                 }\r
495 \r
496                 osgk_browser_event_mouse_button( instance->browser, mappedbutton, mappedtype );\r
497                 return true;\r
498         } else if( K_MWHEELUP <= key && key <= K_MWHEELDOWN ) {\r
499                 if( eventtype == CLG_BET_DOWN )\r
500                         osgk_browser_event_mouse_wheel( instance->browser, \r
501                                 waVertical, (key == K_MWHEELUP) ? wdNegative : wdPositive );\r
502                 return true;\r
503         }\r
504         // TODO: error?\r
505         return false;\r
506 }\r
507 \r
508 #endif\r