3 // includes everything!
\r
4 #include <OffscreenGecko/browser.h>
\r
7 # pragma comment( lib, "OffscreenGecko" )
\r
10 #include "quakedef.h"
\r
11 #include "cl_dyntexture.h"
\r
12 #include "cl_gecko.h"
\r
15 #define DEFAULT_GECKO_SIZE 512
\r
17 static rtexturepool_t *cl_geckotexturepool;
\r
18 static OSGK_Embedding *cl_geckoembedding;
\r
22 char name[ MAX_QPATH + 32 ];
\r
24 OSGK_Browser *browser;
\r
26 int texWidth, texHeight;
\r
28 rtexture_t *texture;
\r
31 static clgecko_t cl_geckoinstances[ MAX_GECKO_INSTANCES ];
\r
33 static clgecko_t * cl_gecko_findunusedinstance( void ) {
\r
35 for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
\r
36 clgecko_t *instance = &cl_geckoinstances[ i ];
\r
37 if( !instance->active ) {
\r
44 clgecko_t * CL_Gecko_FindBrowser( const char *name ) {
\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
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
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
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
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
90 void CL_Gecko_Resize( clgecko_t *instance, int width, int height ) {
\r
91 int newWidth, newHeight;
\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
98 newWidth = CeilPowerOf2( width );
\r
99 newHeight = CeilPowerOf2( height );
\r
100 if ((newWidth != instance->texWidth) || (newHeight != instance->texHeight))
\r
102 cl_gecko_unlinktexture( instance );
\r
103 instance->texWidth = newWidth;
\r
104 instance->texHeight = newHeight;
\r
105 cl_gecko_linktexture( instance );
\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
114 osgk_browser_resize( instance->browser, width, height);
\r
115 instance->width = width;
\r
116 instance->height = height;
\r
119 void CL_Gecko_GetTextureExtent( clgecko_t *instance, float* pwidth, float* pheight )
\r
121 if( !instance || !instance->browser ) {
\r
125 *pwidth = (float)instance->width / instance->texWidth;
\r
126 *pheight = (float)instance->height / instance->texHeight;
\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
135 if( cl_geckoembedding == NULL ) {
\r
136 OSGK_EmbeddingOptions *options = osgk_embedding_options_create();
\r
137 osgk_embedding_options_add_search_path( options, "./xulrunner/" );
\r
138 cl_geckoembedding = osgk_embedding_create_with_options( options, NULL );
\r
139 osgk_release( options );
\r
141 if( cl_geckoembedding == NULL ) {
\r
142 Con_Printf( "CL_Gecko_CreateBrowser: Couldn't retrieve gecko embedding object!\n" );
\r
147 instance->active = true;
\r
148 strlcpy( instance->name, name, sizeof( instance->name ) );
\r
149 instance->browser = osgk_browser_create( cl_geckoembedding, DEFAULT_GECKO_SIZE, DEFAULT_GECKO_SIZE );
\r
150 // TODO: assert != NULL
\r
152 instance->width = instance->texWidth = DEFAULT_GECKO_SIZE;
\r
153 instance->height = instance->texHeight = DEFAULT_GECKO_SIZE;
\r
154 cl_gecko_linktexture( instance );
\r
159 void CL_Gecko_DestroyBrowser( clgecko_t *instance ) {
\r
160 if( !instance || !instance->active ) {
\r
164 instance->active = false;
\r
165 cl_gecko_unlinktexture( instance );
\r
167 osgk_release( instance->browser );
\r
168 instance->browser = NULL;
\r
171 void CL_Gecko_Frame( void ) {
\r
173 for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
\r
174 clgecko_t *instance = &cl_geckoinstances[ i ];
\r
175 if( instance->active ) {
\r
176 if( instance->browser && osgk_browser_query_dirty( instance->browser ) == 1 ) {
\r
177 R_MarkDirtyTexture( instance->texture );
\r
183 static void cl_gecko_start( void )
\r
186 cl_geckotexturepool = R_AllocTexturePool();
\r
188 // recreate textures on module start
\r
189 for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
\r
190 clgecko_t *instance = &cl_geckoinstances[ i ];
\r
191 if( instance->active ) {
\r
192 cl_gecko_linktexture( instance );
\r
197 static void cl_gecko_shutdown( void )
\r
200 for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
\r
201 clgecko_t *instance = &cl_geckoinstances[ i ];
\r
202 if( instance->active ) {
\r
203 cl_gecko_unlinktexture( instance );
\r
206 R_FreeTexturePool( &cl_geckotexturepool );
\r
209 static void cl_gecko_newmap( void )
\r
214 void CL_Gecko_Shutdown( void ) {
\r
216 for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {
\r
217 clgecko_t *instance = &cl_geckoinstances[ i ];
\r
218 if( instance->active ) {
\r
219 cl_gecko_unlinktexture( instance );
\r
223 if (cl_geckoembedding != NULL)
\r
225 osgk_release( cl_geckoembedding );
\r
226 cl_geckoembedding = NULL;
\r
230 static void cl_gecko_create_f( void ) {
\r
231 char name[MAX_QPATH];
\r
233 if (Cmd_Argc() != 2)
\r
235 Con_Print("usage: gecko_create <name>\npcreates a browser (full texture path " CLGECKOPREFIX "<name>)\n");
\r
239 // TODO: use snprintf instead
\r
240 sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));
\r
241 CL_Gecko_CreateBrowser( name );
\r
244 static void cl_gecko_destroy_f( void ) {
\r
245 char name[MAX_QPATH];
\r
247 if (Cmd_Argc() != 2)
\r
249 Con_Print("usage: gecko_destroy <name>\ndestroys a browser\n");
\r
253 // TODO: use snprintf instead
\r
254 sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));
\r
255 CL_Gecko_DestroyBrowser( CL_Gecko_FindBrowser( name ) );
\r
258 static void cl_gecko_navigate_f( void ) {
\r
259 char name[MAX_QPATH];
\r
262 if (Cmd_Argc() != 3)
\r
264 Con_Print("usage: gecko_navigate <name> <URI>\nnavigates to a certain URI\n");
\r
268 // TODO: use snprintf instead
\r
269 sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));
\r
270 URI = Cmd_Argv( 2 );
\r
271 CL_Gecko_NavigateToURI( CL_Gecko_FindBrowser( name ), URI );
\r
274 static void cl_gecko_injecttext_f( void ) {
\r
275 char name[MAX_QPATH];
\r
277 clgecko_t *instance;
\r
280 if (Cmd_Argc() < 3)
\r
282 Con_Print("usage: gecko_injecttext <name> <text>\ninjects a certain text into the browser\n");
\r
286 // TODO: use snprintf instead
\r
287 sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));
\r
288 instance = CL_Gecko_FindBrowser( name );
\r
290 Con_Printf( "cl_gecko_injecttext_f: gecko instance '%s' couldn't be found!\n", name );
\r
294 text = Cmd_Argv( 2 );
\r
296 for( p = text ; *p ; p++ ) {
\r
316 CL_Gecko_Event_Key( instance, key, CLG_BET_PRESS );
\r
320 static void gl_gecko_movecursor_f( void ) {
\r
321 char name[MAX_QPATH];
\r
324 if (Cmd_Argc() != 4)
\r
326 Con_Print("usage: gecko_movecursor <name> <x> <y>\nmove the cursor to a certain position\n");
\r
330 // TODO: use snprintf instead
\r
331 sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));
\r
332 x = atof( Cmd_Argv( 2 ) );
\r
333 y = atof( Cmd_Argv( 3 ) );
\r
335 CL_Gecko_Event_CursorMove( CL_Gecko_FindBrowser( name ), x, y );
\r
338 void CL_Gecko_Init( void )
\r
340 Cmd_AddCommand( "gecko_create", cl_gecko_create_f, "Create a gecko browser instance" );
\r
341 Cmd_AddCommand( "gecko_destroy", cl_gecko_destroy_f, "Destroy a gecko browser instance" );
\r
342 Cmd_AddCommand( "gecko_navigate", cl_gecko_navigate_f, "Navigate a gecko browser to a URI" );
\r
343 Cmd_AddCommand( "gecko_injecttext", cl_gecko_injecttext_f, "Injects text into a browser" );
\r
344 Cmd_AddCommand( "gecko_movecursor", gl_gecko_movecursor_f, "Move the cursor to a certain position" );
\r
346 R_RegisterModule( "CL_Gecko", cl_gecko_start, cl_gecko_shutdown, cl_gecko_newmap );
\r
349 void CL_Gecko_NavigateToURI( clgecko_t *instance, const char *URI ) {
\r
350 if( instance && instance->active ) {
\r
351 osgk_browser_navigate( instance->browser, URI );
\r
355 void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y ) {
\r
356 // TODO: assert x, y \in [0.0, 1.0]
\r
357 int mappedx, mappedy;
\r
359 if( !instance || !instance->browser ) {
\r
363 mappedx = x * instance->width;
\r
364 mappedy = y * instance->height;
\r
365 osgk_browser_event_mouse_move( instance->browser, mappedx, mappedy );
\r
368 typedef struct geckokeymapping_s {
\r
370 unsigned int geckokeycode;
\r
371 } geckokeymapping_t;
\r
373 static geckokeymapping_t geckokeymappingtable[] = {
\r
374 { K_BACKSPACE, OSGKKey_Backspace },
\r
375 { K_TAB, OSGKKey_Tab },
\r
376 { K_ENTER, OSGKKey_Return },
\r
377 { K_SHIFT, OSGKKey_Shift },
\r
378 { K_CTRL, OSGKKey_Control },
\r
379 { K_ALT, OSGKKey_Alt },
\r
380 { K_CAPSLOCK, OSGKKey_CapsLock },
\r
381 { K_ESCAPE, OSGKKey_Escape },
\r
382 { K_SPACE, OSGKKey_Space },
\r
383 { K_PGUP, OSGKKey_PageUp },
\r
384 { K_PGDN, OSGKKey_PageDown },
\r
385 { K_END, OSGKKey_End },
\r
386 { K_HOME, OSGKKey_Home },
\r
387 { K_LEFTARROW, OSGKKey_Left },
\r
388 { K_UPARROW, OSGKKey_Up },
\r
389 { K_RIGHTARROW, OSGKKey_Right },
\r
390 { K_DOWNARROW, OSGKKey_Down },
\r
391 { K_INS, OSGKKey_Insert },
\r
392 { K_DEL, OSGKKey_Delete },
\r
393 { K_F1, OSGKKey_F1 },
\r
394 { K_F2, OSGKKey_F2 },
\r
395 { K_F3, OSGKKey_F3 },
\r
396 { K_F4, OSGKKey_F4 },
\r
397 { K_F5, OSGKKey_F5 },
\r
398 { K_F6, OSGKKey_F6 },
\r
399 { K_F7, OSGKKey_F7 },
\r
400 { K_F8, OSGKKey_F8 },
\r
401 { K_F9, OSGKKey_F9 },
\r
402 { K_F10, OSGKKey_F10 },
\r
403 { K_F11, OSGKKey_F11 },
\r
404 { K_F12, OSGKKey_F12 },
\r
405 { K_NUMLOCK, OSGKKey_NumLock },
\r
406 { K_SCROLLOCK, OSGKKey_ScrollLock }
\r
409 qboolean CL_Gecko_Event_Key( clgecko_t *instance, int key, clgecko_buttoneventtype_t eventtype ) {
\r
410 if( !instance || !instance->browser ) {
\r
414 // determine whether its a keyboard event
\r
415 if( key < K_OTHERDEVICESBEGIN ) {
\r
417 OSGK_KeyboardEventType mappedtype;
\r
418 unsigned int mappedkey = key;
\r
421 // yes! then convert it if necessary!
\r
422 for( i = 0 ; i < sizeof( geckokeymappingtable ) / sizeof( *geckokeymappingtable ) ; i++ ) {
\r
423 const geckokeymapping_t * const mapping = &geckokeymappingtable[ i ];
\r
424 if( key == mapping->keycode ) {
\r
425 mappedkey = mapping->geckokeycode;
\r
430 // convert the eventtype
\r
432 switch( eventtype ) {
\r
434 mappedtype = keDown;
\r
439 case CLG_BET_DOUBLECLICK:
\r
440 // TODO: error message
\r
442 case CLG_BET_PRESS:
\r
443 mappedtype = kePress;
\r
446 return osgk_browser_event_key( instance->browser, mappedkey, mappedtype ) != 0;
\r
447 } else if( K_MOUSE1 <= key && key <= K_MOUSE3 ) {
\r
448 OSGK_MouseButtonEventType mappedtype;
\r
449 OSGK_MouseButton mappedbutton;
\r
451 mappedbutton = (OSGK_MouseButton) (mbLeft + (key - K_MOUSE1));
\r
453 switch( eventtype ) {
\r
455 mappedtype = meDown;
\r
460 case CLG_BET_DOUBLECLICK:
\r
461 mappedtype = meDoubleClick;
\r
463 case CLG_BET_PRESS:
\r
464 // hihi, hacky hacky
\r
465 osgk_browser_event_mouse_button( instance->browser, mappedbutton, meDown );
\r
470 osgk_browser_event_mouse_button( instance->browser, mappedbutton, mappedtype );
\r
472 } else if( K_MWHEELUP <= key && key <= K_MWHEELDOWN ) {
\r
473 if( eventtype == CLG_BET_DOWN )
\r
474 osgk_browser_event_mouse_wheel( instance->browser,
\r
475 waVertical, (key == K_MWHEELUP) ? wdNegative : wdPositive );
\r