added an optimization note (should be tracking number of active gecko
[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         // FIXME: track cl_numgeckoinstances to avoid scanning the entire array?\r
196         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
197                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
198                 if( instance->active ) {\r
199                         if( instance->browser && osgk_browser_query_dirty( instance->browser ) == 1 ) {\r
200                                 R_MarkDirtyTexture( instance->texture );\r
201                         }\r
202                 }\r
203         }\r
204 }\r
205 \r
206 static void cl_gecko_start( void )\r
207 {\r
208         int i;\r
209         cl_geckotexturepool = R_AllocTexturePool();\r
210 \r
211         // recreate textures on module start\r
212         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
213                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
214                 if( instance->active ) {\r
215                         cl_gecko_linktexture( instance );\r
216                 }\r
217         }\r
218 }\r
219 \r
220 static void cl_gecko_shutdown( void )\r
221 {\r
222         int i;\r
223         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
224                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
225                 if( instance->active ) {\r
226                         cl_gecko_unlinktexture( instance );\r
227                 }\r
228         }\r
229         R_FreeTexturePool( &cl_geckotexturepool );\r
230 }\r
231 \r
232 static void cl_gecko_newmap( void )\r
233 {\r
234         // DO NOTHING\r
235 }\r
236 \r
237 void CL_Gecko_Shutdown( void ) {\r
238         int i;\r
239         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
240                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
241                 if( instance->active ) {\r
242                         cl_gecko_unlinktexture( instance );\r
243                 }               \r
244         }\r
245 \r
246         if (cl_geckoembedding != NULL)\r
247         {\r
248             osgk_release( cl_geckoembedding );\r
249             cl_geckoembedding = NULL;\r
250         }\r
251 }\r
252 \r
253 static void cl_gecko_create_f( void ) {\r
254         char name[MAX_QPATH];\r
255 \r
256         if (Cmd_Argc() != 2)\r
257         {\r
258                 Con_Print("usage: gecko_create <name>\npcreates a browser (full texture path " CLGECKOPREFIX "<name>)\n");\r
259                 return;\r
260         }\r
261 \r
262         // TODO: use snprintf instead\r
263         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
264         CL_Gecko_CreateBrowser( name );\r
265 }\r
266 \r
267 static void cl_gecko_destroy_f( void ) {\r
268         char name[MAX_QPATH];\r
269 \r
270         if (Cmd_Argc() != 2)\r
271         {\r
272                 Con_Print("usage: gecko_destroy <name>\ndestroys a browser\n");\r
273                 return;\r
274         }\r
275 \r
276         // TODO: use snprintf instead\r
277         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
278         CL_Gecko_DestroyBrowser( CL_Gecko_FindBrowser( name ) );\r
279 }\r
280 \r
281 static void cl_gecko_navigate_f( void ) {\r
282         char name[MAX_QPATH];\r
283         const char *URI;\r
284 \r
285         if (Cmd_Argc() != 3)\r
286         {\r
287                 Con_Print("usage: gecko_navigate <name> <URI>\nnavigates to a certain URI\n");\r
288                 return;\r
289         }\r
290 \r
291         // TODO: use snprintf instead\r
292         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
293         URI = Cmd_Argv( 2 );\r
294         CL_Gecko_NavigateToURI( CL_Gecko_FindBrowser( name ), URI );\r
295 }\r
296 \r
297 static void cl_gecko_injecttext_f( void ) {\r
298         char name[MAX_QPATH];\r
299         const char *text;\r
300         clgecko_t *instance;\r
301         const char *p;\r
302 \r
303         if (Cmd_Argc() < 3)\r
304         {\r
305                 Con_Print("usage: gecko_injecttext <name> <text>\ninjects a certain text into the browser\n");\r
306                 return;\r
307         }\r
308 \r
309         // TODO: use snprintf instead\r
310         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
311         instance = CL_Gecko_FindBrowser( name );\r
312         if( !instance ) {\r
313                 Con_Printf( "cl_gecko_injecttext_f: gecko instance '%s' couldn't be found!\n", name );\r
314                 return;\r
315         }\r
316 \r
317         text = Cmd_Argv( 2 );\r
318 \r
319         for( p = text ; *p ; p++ ) {\r
320                 unsigned key = *p;\r
321                 switch( key ) {\r
322                         case ' ':\r
323                                 key = K_SPACE;\r
324                                 break;\r
325                         case '\\':\r
326                                 key = *++p;\r
327                                 switch( key ) {\r
328                                 case 'n':\r
329                                         key = K_ENTER;\r
330                                         break;\r
331                                 case '\0':\r
332                                         --p;\r
333                                         key = '\\';\r
334                                         break;\r
335                                 }\r
336                                 break;\r
337                 }\r
338 \r
339                 CL_Gecko_Event_Key( instance, key, CLG_BET_PRESS );\r
340         }\r
341 }\r
342 \r
343 static void gl_gecko_movecursor_f( void ) {\r
344         char name[MAX_QPATH];\r
345         float x, y;\r
346 \r
347         if (Cmd_Argc() != 4)\r
348         {\r
349                 Con_Print("usage: gecko_movecursor <name> <x> <y>\nmove the cursor to a certain position\n");\r
350                 return;\r
351         }\r
352 \r
353         // TODO: use snprintf instead\r
354         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
355         x = atof( Cmd_Argv( 2 ) );\r
356         y = atof( Cmd_Argv( 3 ) );\r
357 \r
358         CL_Gecko_Event_CursorMove( CL_Gecko_FindBrowser( name ), x, y );\r
359 }\r
360 \r
361 void CL_Gecko_Init( void )\r
362 {\r
363         Cmd_AddCommand( "gecko_create", cl_gecko_create_f, "Create a gecko browser instance" );\r
364         Cmd_AddCommand( "gecko_destroy", cl_gecko_destroy_f, "Destroy a gecko browser instance" );\r
365         Cmd_AddCommand( "gecko_navigate", cl_gecko_navigate_f, "Navigate a gecko browser to a URI" );\r
366         Cmd_AddCommand( "gecko_injecttext", cl_gecko_injecttext_f, "Injects text into a browser" );\r
367         Cmd_AddCommand( "gecko_movecursor", gl_gecko_movecursor_f, "Move the cursor to a certain position" );\r
368 \r
369         R_RegisterModule( "CL_Gecko", cl_gecko_start, cl_gecko_shutdown, cl_gecko_newmap );\r
370 }\r
371 \r
372 void CL_Gecko_NavigateToURI( clgecko_t *instance, const char *URI ) {\r
373         if( !instance || !instance->browser ) {\r
374                 return;\r
375         }\r
376 \r
377         if( instance->active ) {\r
378                 osgk_browser_navigate( instance->browser, URI );\r
379         }\r
380 }\r
381 \r
382 void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y ) {\r
383         // TODO: assert x, y \in [0.0, 1.0]\r
384         int mappedx, mappedy;\r
385 \r
386         if( !instance || !instance->browser ) {\r
387                 return;\r
388         }\r
389 \r
390         mappedx = x * instance->width;\r
391         mappedy = y * instance->height;\r
392         osgk_browser_event_mouse_move( instance->browser, mappedx, mappedy );\r
393 }\r
394 \r
395 typedef struct geckokeymapping_s {\r
396         keynum_t keycode;\r
397         unsigned int geckokeycode;\r
398 } geckokeymapping_t;\r
399 \r
400 static geckokeymapping_t geckokeymappingtable[] = {\r
401         { K_BACKSPACE, OSGKKey_Backspace },\r
402         { K_TAB, OSGKKey_Tab },\r
403         { K_ENTER, OSGKKey_Return },\r
404         { K_SHIFT, OSGKKey_Shift },\r
405         { K_CTRL, OSGKKey_Control },\r
406         { K_ALT, OSGKKey_Alt },\r
407         { K_CAPSLOCK, OSGKKey_CapsLock },\r
408         { K_ESCAPE, OSGKKey_Escape },\r
409         { K_SPACE, OSGKKey_Space },\r
410         { K_PGUP, OSGKKey_PageUp },\r
411         { K_PGDN, OSGKKey_PageDown },\r
412         { K_END, OSGKKey_End },\r
413         { K_HOME, OSGKKey_Home },\r
414         { K_LEFTARROW, OSGKKey_Left },\r
415         { K_UPARROW, OSGKKey_Up },\r
416         { K_RIGHTARROW, OSGKKey_Right },\r
417         { K_DOWNARROW, OSGKKey_Down },\r
418         { K_INS, OSGKKey_Insert },\r
419         { K_DEL, OSGKKey_Delete },\r
420         { K_F1, OSGKKey_F1 },\r
421         { K_F2, OSGKKey_F2 },\r
422         { K_F3, OSGKKey_F3 },\r
423         { K_F4, OSGKKey_F4 },\r
424         { K_F5, OSGKKey_F5 },\r
425         { K_F6, OSGKKey_F6 },\r
426         { K_F7, OSGKKey_F7 },\r
427         { K_F8, OSGKKey_F8 },\r
428         { K_F9, OSGKKey_F9 },\r
429         { K_F10, OSGKKey_F10 },\r
430         { K_F11, OSGKKey_F11 },\r
431         { K_F12, OSGKKey_F12 },\r
432         { K_NUMLOCK, OSGKKey_NumLock },\r
433         { K_SCROLLOCK, OSGKKey_ScrollLock }\r
434 };\r
435 \r
436 qboolean CL_Gecko_Event_Key( clgecko_t *instance, int key, clgecko_buttoneventtype_t eventtype ) {\r
437         if( !instance || !instance->browser ) {\r
438                 return false;\r
439         }\r
440 \r
441         // determine whether its a keyboard event\r
442         if( key < K_OTHERDEVICESBEGIN ) {\r
443 \r
444                 OSGK_KeyboardEventType mappedtype;\r
445                 unsigned int mappedkey = key;\r
446                 \r
447                 int i;\r
448                 // yes! then convert it if necessary!\r
449                 for( i = 0 ; i < sizeof( geckokeymappingtable ) / sizeof( *geckokeymappingtable ) ; i++ ) {\r
450                         const geckokeymapping_t * const mapping = &geckokeymappingtable[ i ];\r
451                         if( key == mapping->keycode ) {\r
452                                 mappedkey = mapping->geckokeycode;\r
453                                 break;\r
454                         }\r
455                 }\r
456 \r
457                 // convert the eventtype\r
458                 // map the type\r
459                 switch( eventtype ) {\r
460                 case CLG_BET_DOWN:\r
461                         mappedtype = keDown;\r
462                         break;\r
463                 case CLG_BET_UP:\r
464                         mappedtype = keUp;\r
465                         break;\r
466                 case CLG_BET_DOUBLECLICK:\r
467                         // TODO: error message\r
468                         break;\r
469                 case CLG_BET_PRESS:\r
470                         mappedtype = kePress;\r
471                 }\r
472 \r
473                 return osgk_browser_event_key( instance->browser, mappedkey, mappedtype ) != 0;\r
474         } else if( K_MOUSE1 <= key && key <= K_MOUSE3 ) {\r
475                 OSGK_MouseButtonEventType mappedtype;\r
476                 OSGK_MouseButton mappedbutton;\r
477 \r
478                 mappedbutton = (OSGK_MouseButton) (mbLeft + (key - K_MOUSE1));\r
479 \r
480                 switch( eventtype ) {\r
481                 case CLG_BET_DOWN:\r
482                         mappedtype = meDown;\r
483                         break;\r
484                 case CLG_BET_UP:\r
485                         mappedtype = meUp;\r
486                         break;\r
487                 case CLG_BET_DOUBLECLICK:\r
488                         mappedtype = meDoubleClick;\r
489                         break;\r
490                 case CLG_BET_PRESS:\r
491                         // hihi, hacky hacky\r
492                         osgk_browser_event_mouse_button( instance->browser, mappedbutton, meDown );\r
493                         mappedtype = meUp;\r
494                         break;\r
495                 }\r
496 \r
497                 osgk_browser_event_mouse_button( instance->browser, mappedbutton, mappedtype );\r
498                 return true;\r
499         } else if( K_MWHEELUP <= key && key <= K_MWHEELDOWN ) {\r
500                 if( eventtype == CLG_BET_DOWN )\r
501                         osgk_browser_event_mouse_wheel( instance->browser, \r
502                                 waVertical, (key == K_MWHEELUP) ? wdNegative : wdPositive );\r
503                 return true;\r
504         }\r
505         // TODO: error?\r
506         return false;\r
507 }\r
508 \r
509 #endif\r