f1ec9d89f9692387ad6af500180d51ad08501451
[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_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 w, h;\r
26         int texW, texH;\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->w, instance->h);\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->texW, instance->texH, 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 clgecko_t * CL_Gecko_CreateBrowser( const char *name ) {\r
91         // TODO: verify that we dont use a name twice\r
92         clgecko_t *instance = cl_gecko_findunusedinstance();\r
93         // TODO: assert != NULL\r
94         \r
95         if( cl_geckoembedding == NULL ) {\r
96                 OSGK_EmbeddingOptions *options = osgk_embedding_options_create();\r
97                 osgk_embedding_options_add_search_path( options, "./xulrunner/" );\r
98                 cl_geckoembedding = osgk_embedding_create_with_options( options, NULL );\r
99                 osgk_release( options );\r
100                 \r
101                 if( cl_geckoembedding == NULL ) {\r
102                         Con_Printf( "CL_Gecko_CreateBrowser: Couldn't retrieve gecko embedding object!\n" );\r
103                         return NULL;\r
104                 }\r
105         }\r
106 \r
107         instance->active = true;\r
108         strlcpy( instance->name, name, sizeof( instance->name ) );\r
109         instance->browser = osgk_browser_create( cl_geckoembedding, DEFAULT_SIZE, DEFAULT_SIZE );\r
110         // TODO: assert != NULL\r
111 \r
112         instance->texW = DEFAULT_SIZE;\r
113         instance->texH = DEFAULT_SIZE;\r
114         instance->w = DEFAULT_SIZE;\r
115         instance->h = DEFAULT_SIZE;\r
116         cl_gecko_linktexture( instance );\r
117 \r
118         return instance;\r
119 }\r
120 \r
121 void CL_Gecko_DestroyBrowser( clgecko_t *instance ) {\r
122    if( !instance || !instance->active ) {\r
123                 return;\r
124         }\r
125 \r
126         instance->active = false;\r
127         cl_gecko_unlinktexture( instance );\r
128 \r
129         osgk_release( instance->browser );\r
130         instance->browser = NULL;\r
131 }\r
132 \r
133 void CL_Gecko_Frame( void ) {\r
134         int i;\r
135         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
136                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
137                 if( instance->active ) {\r
138                         if( instance->browser && osgk_browser_query_dirty( instance->browser ) == 1 ) {\r
139                                 R_MarkDirtyTexture( instance->texture );\r
140                         }\r
141                 }\r
142         }\r
143 }\r
144 \r
145 static void cl_gecko_start( void )\r
146 {\r
147         int i;\r
148         cl_geckotexturepool = R_AllocTexturePool();\r
149 \r
150         // recreate textures on module start\r
151         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
152                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
153                 if( instance->active ) {\r
154                         cl_gecko_linktexture( instance );\r
155                 }\r
156         }\r
157 }\r
158 \r
159 static void cl_gecko_shutdown( void )\r
160 {\r
161         int i;\r
162         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
163                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
164                 if( instance->active ) {\r
165                         cl_gecko_unlinktexture( instance );\r
166                 }\r
167         }\r
168         R_FreeTexturePool( &cl_geckotexturepool );\r
169 }\r
170 \r
171 static void cl_gecko_newmap( void )\r
172 {\r
173         // DO NOTHING\r
174 }\r
175 \r
176 void CL_Gecko_Shutdown( void ) {\r
177         int i;\r
178         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
179                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
180                 if( instance->active ) {\r
181                         cl_gecko_unlinktexture( instance );\r
182                 }               \r
183         }\r
184 \r
185         if (cl_geckoembedding != NULL)\r
186         {\r
187             osgk_release( cl_geckoembedding );\r
188             cl_geckoembedding = NULL;\r
189         }\r
190 }\r
191 \r
192 static void cl_gecko_create_f( void ) {\r
193         char name[MAX_QPATH];\r
194 \r
195         if (Cmd_Argc() != 2)\r
196         {\r
197                 Con_Print("usage: gecko_create <name>\npcreates a browser (full texture path " CLGECKOPREFIX "<name>)\n");\r
198                 return;\r
199         }\r
200 \r
201         // TODO: use snprintf instead\r
202         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
203         CL_Gecko_CreateBrowser( name );\r
204 }\r
205 \r
206 static void cl_gecko_destroy_f( void ) {\r
207         char name[MAX_QPATH];\r
208 \r
209         if (Cmd_Argc() != 2)\r
210         {\r
211                 Con_Print("usage: gecko_destroy <name>\ndestroys a browser\n");\r
212                 return;\r
213         }\r
214 \r
215         // TODO: use snprintf instead\r
216         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
217         CL_Gecko_DestroyBrowser( CL_Gecko_FindBrowser( name ) );\r
218 }\r
219 \r
220 static void cl_gecko_navigate_f( void ) {\r
221         char name[MAX_QPATH];\r
222         const char *URI;\r
223 \r
224         if (Cmd_Argc() != 3)\r
225         {\r
226                 Con_Print("usage: gecko_navigate <name> <URI>\nnavigates to a certain URI\n");\r
227                 return;\r
228         }\r
229 \r
230         // TODO: use snprintf instead\r
231         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
232         URI = Cmd_Argv( 2 );\r
233         CL_Gecko_NavigateToURI( CL_Gecko_FindBrowser( name ), URI );\r
234 }\r
235 \r
236 static void cl_gecko_injecttext_f( void ) {\r
237         char name[MAX_QPATH];\r
238         const char *text;\r
239         clgecko_t *instance;\r
240         const char *p;\r
241 \r
242         if (Cmd_Argc() < 3)\r
243         {\r
244                 Con_Print("usage: gecko_injecttext <name> <text>\ninjects a certain text into the browser\n");\r
245                 return;\r
246         }\r
247 \r
248         // TODO: use snprintf instead\r
249         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
250         instance = CL_Gecko_FindBrowser( name );\r
251         if( !instance ) {\r
252                 Con_Printf( "cl_gecko_injecttext_f: gecko instance '%s' couldn't be found!\n", name );\r
253                 return;\r
254         }\r
255 \r
256         text = Cmd_Argv( 2 );\r
257 \r
258         for( p = text ; *p ; p++ ) {\r
259                 unsigned key = *p;\r
260                 switch( key ) {\r
261                         case ' ':\r
262                                 key = K_SPACE;\r
263                                 break;\r
264                         case '\\':\r
265                                 key = *++p;\r
266                                 switch( key ) {\r
267                                 case 'n':\r
268                                         key = K_ENTER;\r
269                                         break;\r
270                                 case '\0':\r
271                                         --p;\r
272                                         key = '\\';\r
273                                         break;\r
274                                 }\r
275                                 break;\r
276                 }\r
277 \r
278                 CL_Gecko_Event_Key( instance, key, CLG_BET_PRESS );\r
279         }\r
280 }\r
281 \r
282 void CL_Gecko_Init( void )\r
283 {\r
284         Cmd_AddCommand( "gecko_create", cl_gecko_create_f, "Create a gecko browser instance" );\r
285         Cmd_AddCommand( "gecko_destroy", cl_gecko_destroy_f, "Destroy a gecko browser instance" );\r
286         Cmd_AddCommand( "gecko_navigate", cl_gecko_navigate_f, "Navigate a gecko browser to a URI" );\r
287         Cmd_AddCommand( "gecko_injecttext", cl_gecko_injecttext_f, "Injects text into a browser" );\r
288 \r
289         R_RegisterModule( "CL_Gecko", cl_gecko_start, cl_gecko_shutdown, cl_gecko_newmap );\r
290 }\r
291 \r
292 void CL_Gecko_NavigateToURI( clgecko_t *instance, const char *URI ) {\r
293         if( instance && instance->active ) {\r
294                 osgk_browser_navigate( instance->browser, URI );\r
295         }\r
296 }\r
297 \r
298 void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y ) {\r
299         // TODO: assert x, y \in [0.0, 1.0]\r
300         int mappedx, mappedy;\r
301 \r
302         if( !instance || !instance->browser ) {\r
303                 return;\r
304         }\r
305 \r
306         mappedx = x * instance->w;\r
307         mappedy = y * instance->h;\r
308         osgk_browser_event_mouse_move( instance->browser, mappedx, mappedy );\r
309 }\r
310 \r
311 typedef struct geckokeymapping_s {\r
312         keynum_t keycode;\r
313         unsigned int geckokeycode;\r
314 } geckokeymapping_t;\r
315 \r
316 static geckokeymapping_t geckokeymappingtable[] = {\r
317         { K_BACKSPACE, OSGKKey_Backspace },\r
318         { K_TAB, OSGKKey_Tab },\r
319         { K_ENTER, OSGKKey_Return },\r
320         { K_SHIFT, OSGKKey_Shift },\r
321         { K_CTRL, OSGKKey_Control },\r
322         { K_ALT, OSGKKey_Alt },\r
323         { K_CAPSLOCK, OSGKKey_CapsLock },\r
324         { K_ESCAPE, OSGKKey_Escape },\r
325         { K_SPACE, OSGKKey_Space },\r
326         { K_PGUP, OSGKKey_PageUp },\r
327         { K_PGDN, OSGKKey_PageDown },\r
328         { K_END, OSGKKey_End },\r
329         { K_HOME, OSGKKey_Home },\r
330         { K_LEFTARROW, OSGKKey_Left },\r
331         { K_UPARROW, OSGKKey_Up },\r
332         { K_RIGHTARROW, OSGKKey_Right },\r
333         { K_DOWNARROW, OSGKKey_Down },\r
334         { K_INS, OSGKKey_Insert },\r
335         { K_DEL, OSGKKey_Delete },\r
336         { K_F1, OSGKKey_F1 },\r
337         { K_F2, OSGKKey_F2 },\r
338         { K_F3, OSGKKey_F3 },\r
339         { K_F4, OSGKKey_F4 },\r
340         { K_F5, OSGKKey_F5 },\r
341         { K_F6, OSGKKey_F6 },\r
342         { K_F7, OSGKKey_F7 },\r
343         { K_F8, OSGKKey_F8 },\r
344         { K_F9, OSGKKey_F9 },\r
345         { K_F10, OSGKKey_F10 },\r
346         { K_F11, OSGKKey_F11 },\r
347         { K_F12, OSGKKey_F12 },\r
348         { K_NUMLOCK, OSGKKey_NumLock },\r
349         { K_SCROLLOCK, OSGKKey_ScrollLock }\r
350 };\r
351 \r
352 qboolean CL_Gecko_Event_Key( clgecko_t *instance, int key, clgecko_buttoneventtype_t eventtype ) {\r
353         if( !instance || !instance->browser ) {\r
354                 return false;\r
355         }\r
356 \r
357         // determine whether its a keyboard event\r
358         if( key < K_OTHERDEVICESBEGIN ) {\r
359 \r
360                 OSGK_KeyboardEventType mappedtype;\r
361                 unsigned int mappedkey = key;\r
362                 \r
363                 int i;\r
364                 // yes! then convert it if necessary!\r
365                 for( i = 0 ; i < sizeof( geckokeymappingtable ) / sizeof( *geckokeymappingtable ) ; i++ ) {\r
366                         const geckokeymapping_t * const mapping = &geckokeymappingtable[ i ];\r
367                         if( key == mapping->keycode ) {\r
368                                 mappedkey = mapping->geckokeycode;\r
369                                 break;\r
370                         }\r
371                 }\r
372 \r
373                 // convert the eventtype\r
374                 // map the type\r
375                 switch( eventtype ) {\r
376                 case CLG_BET_DOWN:\r
377                         mappedtype = keDown;\r
378                         break;\r
379                 case CLG_BET_UP:\r
380                         mappedtype = keUp;\r
381                         break;\r
382                 case CLG_BET_DOUBLECLICK:\r
383                         // TODO: error message\r
384                         break;\r
385                 case CLG_BET_PRESS:\r
386                         mappedtype = kePress;\r
387                 }\r
388 \r
389                 return osgk_browser_event_key( instance->browser, mappedkey, mappedtype ) != 0;\r
390         } else if( K_MOUSE1 <= key && key <= K_MOUSE3 ) {\r
391                 OSGK_MouseButtonEventType mappedtype;\r
392                 OSGK_MouseButton mappedbutton;\r
393 \r
394                 mappedbutton = (OSGK_MouseButton) (mbLeft + (key - K_MOUSE1));\r
395 \r
396                 switch( eventtype ) {\r
397                 case CLG_BET_DOWN:\r
398                         mappedtype = meDown;\r
399                         break;\r
400                 case CLG_BET_UP:\r
401                         mappedtype = meUp;\r
402                         break;\r
403                 case CLG_BET_DOUBLECLICK:\r
404                         mappedtype = meDoubleClick;\r
405                         break;\r
406                 case CLG_BET_PRESS:\r
407                         // hihi, hacky hacky\r
408                         osgk_browser_event_mouse_button( instance->browser, mappedbutton, meDown );\r
409                         mappedtype = meUp;\r
410                         break;\r
411                 }\r
412 \r
413                 osgk_browser_event_mouse_button( instance->browser, mappedbutton, mappedtype );\r
414                 return true;\r
415         } else if( K_MWHEELUP <= key && key <= K_MWHEELDOWN ) {\r
416                 if( eventtype == CLG_BET_DOWN )\r
417                         osgk_browser_event_mouse_wheel( instance->browser, \r
418                                 waVertical, (key == K_MWHEELUP) ? wdNegative : wdPositive );\r
419                 return true;\r
420         }\r
421         // TODO: error?\r
422         return false;\r
423 }\r
424 \r
425 void CL_Gecko_Resize( clgecko_t *instance, int w, int h ) {\r
426         int newW, newH;\r
427 \r
428         if( !instance || !instance->browser ) {\r
429                 return;\r
430         }\r
431 \r
432         newW = 1; while( newW < w ) newW *= 2;\r
433         newH = 1; while( newH < h ) newH *= 2;\r
434         if ((newW != instance->texW) || (newH != instance->texH))\r
435         {\r
436           cl_gecko_unlinktexture( instance );\r
437           instance->texW = newW;\r
438           instance->texH = newH;\r
439           cl_gecko_linktexture( instance );\r
440         }\r
441         else\r
442         {\r
443           /* The gecko area will only cover a part of the texture; to avoid\r
444              'old' pixels bleeding in at the border clear the texture. */\r
445           R_ClearTexture( instance->texture );\r
446         }\r
447 \r
448         osgk_browser_resize( instance->browser, w, h);\r
449         instance->w = w;\r
450         instance->h = h;\r
451 }\r
452 \r
453 void CL_Gecko_GetTextureExtent( clgecko_t *instance, float* u, float* v )\r
454 {\r
455         if( !instance || !instance->browser ) {\r
456                 return;\r
457         }\r
458 \r
459         *u = (float)instance->w / instance->texW;\r
460         *v = (float)instance->h / instance->texH;\r
461 }\r
462 \r
463 #endif\r