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