Meh, stupid bug in cl_gecko. Sorry about that :-(
[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 static rtexturepool_t *cl_geckotexturepool;\r
16 static OSGK_Embedding *cl_geckoembedding;\r
17 \r
18 struct clgecko_s {\r
19         qboolean active;\r
20         char name[ MAX_QPATH + 32 ];\r
21 \r
22         OSGK_Browser *browser;\r
23         \r
24         rtexture_t *texture;\r
25 };\r
26 \r
27 static clgecko_t cl_geckoinstances[ MAX_GECKO_INSTANCES ];\r
28 \r
29 static clgecko_t * cl_gecko_findunusedinstance( void ) {\r
30         int i;\r
31         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
32                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
33                 if( !instance->active ) {\r
34                         return instance;\r
35                 }\r
36         }\r
37         return NULL;\r
38 }\r
39 \r
40 clgecko_t * CL_Gecko_FindBrowser( const char *name ) {\r
41         int i;\r
42 \r
43         if( !name || !*name || strncmp( name, CLGECKOPREFIX, sizeof( CLGECKOPREFIX ) - 1 ) != 0 ) {\r
44                 if( developer.integer > 0 ) {\r
45                         Con_Printf( "CL_Gecko_FindBrowser: Bad gecko texture name '%s'!\n", name );\r
46                 }\r
47                 return NULL;\r
48         }\r
49 \r
50         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
51                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
52                 if( instance->active && strcmp( instance->name, name ) == 0 ) {\r
53                         return instance;\r
54                 }\r
55         }\r
56 \r
57         return NULL;\r
58 }\r
59 \r
60 static void cl_gecko_updatecallback( rtexture_t *texture, clgecko_t *instance ) {\r
61         const unsigned char *data;\r
62         if( instance->browser && osgk_browser_query_dirty (instance->browser) ) {\r
63                 // TODO: OSGK only supports BGRA right now\r
64                 TIMING_TIMESTATEMENT(data = osgk_browser_lock_data( instance->browser, NULL ));\r
65                 R_UpdateTexture( texture, data, 0, 0, DEFAULT_GECKO_WIDTH, DEFAULT_GECKO_HEIGHT );\r
66                 osgk_browser_unlock_data( instance->browser, data );\r
67         }\r
68 }\r
69 \r
70 static void cl_gecko_linktexture( clgecko_t *instance ) {\r
71         // TODO: assert that instance->texture == NULL\r
72         instance->texture = R_LoadTexture2D( cl_geckotexturepool, instance->name, DEFAULT_GECKO_WIDTH, DEFAULT_GECKO_HEIGHT, NULL, TEXTYPE_BGRA, TEXF_ALPHA | TEXF_PERSISTENT, NULL );\r
73         R_MakeTextureDynamic( instance->texture, cl_gecko_updatecallback, instance );\r
74         CL_LinkDynTexture( instance->name, instance->texture );\r
75 }\r
76 \r
77 static void cl_gecko_unlinktexture( clgecko_t *instance ) {\r
78         if( instance->texture ) {\r
79                 CL_UnlinkDynTexture( instance->name );\r
80                 R_FreeTexture( instance->texture );\r
81                 instance->texture = NULL;\r
82         }\r
83 }\r
84 \r
85 clgecko_t * CL_Gecko_CreateBrowser( const char *name ) {\r
86         // TODO: verify that we dont use a name twice\r
87         clgecko_t *instance = cl_gecko_findunusedinstance();\r
88         // TODO: assert != NULL\r
89         \r
90         if( cl_geckoembedding == NULL ) {\r
91                 OSGK_EmbeddingOptions *options = osgk_embedding_options_create();\r
92                 osgk_embedding_options_add_search_path( options, "./xulrunner/" );\r
93                 cl_geckoembedding = osgk_embedding_create_with_options( options, NULL );\r
94                 osgk_release( options );\r
95                 \r
96                 if( cl_geckoembedding == NULL ) {\r
97                         Con_Printf( "CL_Gecko_CreateBrowser: Couldn't retrieve gecko embedding object!\n" );\r
98                         return NULL;\r
99                 }\r
100         }\r
101 \r
102         instance->active = true;\r
103         strlcpy( instance->name, name, sizeof( instance->name ) );\r
104         instance->browser = osgk_browser_create( cl_geckoembedding, DEFAULT_GECKO_WIDTH, DEFAULT_GECKO_HEIGHT );\r
105         // TODO: assert != NULL\r
106 \r
107         cl_gecko_linktexture( instance );\r
108 \r
109         return instance;\r
110 }\r
111 \r
112 void CL_Gecko_DestroyBrowser( clgecko_t *instance ) {\r
113    if( !instance || !instance->active ) {\r
114                 return;\r
115         }\r
116 \r
117         instance->active = false;\r
118         cl_gecko_unlinktexture( instance );\r
119 \r
120         osgk_release( instance->browser );\r
121         instance->browser = NULL;\r
122 }\r
123 \r
124 void CL_Gecko_Frame( void ) {\r
125         int i;\r
126         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
127                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
128                 if( instance->active ) {\r
129                         if( instance->browser && osgk_browser_query_dirty( instance->browser ) == 1 ) {\r
130                                 R_MarkDirtyTexture( instance->texture );\r
131                         }\r
132                 }\r
133         }\r
134 }\r
135 \r
136 static void cl_gecko_start( void )\r
137 {\r
138         int i;\r
139         cl_geckotexturepool = R_AllocTexturePool();\r
140 \r
141         // recreate textures on module start\r
142         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
143                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
144                 if( instance->active ) {\r
145                         cl_gecko_linktexture( instance );\r
146                 }\r
147         }\r
148 }\r
149 \r
150 static void cl_gecko_shutdown( void )\r
151 {\r
152         int i;\r
153         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
154                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
155                 if( instance->active ) {\r
156                         cl_gecko_unlinktexture( instance );\r
157                 }\r
158         }\r
159         R_FreeTexturePool( &cl_geckotexturepool );\r
160 }\r
161 \r
162 static void cl_gecko_newmap( void )\r
163 {\r
164         // DO NOTHING\r
165 }\r
166 \r
167 void CL_Gecko_Shutdown( void ) {\r
168         int i;\r
169         for( i = 0 ; i < MAX_GECKO_INSTANCES ; i++ ) {\r
170                 clgecko_t *instance = &cl_geckoinstances[ i ];\r
171                 if( instance->active ) {\r
172                         cl_gecko_unlinktexture( instance );\r
173                 }               \r
174         }\r
175 \r
176         if (cl_geckoembedding != NULL)\r
177         {\r
178             osgk_release( cl_geckoembedding );\r
179             cl_geckoembedding = NULL;\r
180         }\r
181 }\r
182 \r
183 static void cl_gecko_create_f( void ) {\r
184         char name[MAX_QPATH];\r
185 \r
186         if (Cmd_Argc() != 2)\r
187         {\r
188                 Con_Print("usage: gecko_create <name>\npcreates a browser (full texture path " CLGECKOPREFIX "<name>)\n");\r
189                 return;\r
190         }\r
191 \r
192         // TODO: use snprintf instead\r
193         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
194         CL_Gecko_CreateBrowser( name );\r
195 }\r
196 \r
197 static void cl_gecko_destroy_f( void ) {\r
198         char name[MAX_QPATH];\r
199 \r
200         if (Cmd_Argc() != 2)\r
201         {\r
202                 Con_Print("usage: gecko_destroy <name>\ndestroys a browser\n");\r
203                 return;\r
204         }\r
205 \r
206         // TODO: use snprintf instead\r
207         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
208         CL_Gecko_DestroyBrowser( CL_Gecko_FindBrowser( name ) );\r
209 }\r
210 \r
211 static void cl_gecko_navigate_f( void ) {\r
212         char name[MAX_QPATH];\r
213         const char *URI;\r
214 \r
215         if (Cmd_Argc() != 3)\r
216         {\r
217                 Con_Print("usage: gecko_navigate <name> <URI>\nnavigates to a certain URI\n");\r
218                 return;\r
219         }\r
220 \r
221         // TODO: use snprintf instead\r
222         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
223         URI = Cmd_Argv( 2 );\r
224         CL_Gecko_NavigateToURI( CL_Gecko_FindBrowser( name ), URI );\r
225 }\r
226 \r
227 static void cl_gecko_injecttext_f( void ) {\r
228         char name[MAX_QPATH];\r
229         const char *text;\r
230         clgecko_t *instance;\r
231         const char *p;\r
232 \r
233         if (Cmd_Argc() < 3)\r
234         {\r
235                 Con_Print("usage: gecko_injecttext <name> <text>\ninjects a certain text into the browser\n");\r
236                 return;\r
237         }\r
238 \r
239         // TODO: use snprintf instead\r
240         sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
241         instance = CL_Gecko_FindBrowser( name );\r
242         if( !instance ) {\r
243                 Con_Printf( "cl_gecko_injecttext_f: gecko instance '%s' couldn't be found!\n", name );\r
244                 return;\r
245         }\r
246 \r
247         text = Cmd_Argv( 2 );\r
248 \r
249         for( p = text ; *p ; p++ ) {\r
250                 unsigned key = *p;\r
251                 switch( key ) {\r
252                         case ' ':\r
253                                 key = K_SPACE;\r
254                                 break;\r
255                         case '\\':\r
256                                 key = *++p;\r
257                                 switch( key ) {\r
258                                 case 'n':\r
259                                         key = K_ENTER;\r
260                                         break;\r
261                                 case '\0':\r
262                                         --p;\r
263                                         key = '\\';\r
264                                         break;\r
265                                 }\r
266                                 break;\r
267                 }\r
268 \r
269                 CL_Gecko_Event_Key( instance, key, CLG_BET_PRESS );\r
270         }\r
271 }\r
272 \r
273 void CL_Gecko_Init( void )\r
274 {\r
275         Cmd_AddCommand( "gecko_create", cl_gecko_create_f, "Create a gecko browser instance" );\r
276         Cmd_AddCommand( "gecko_destroy", cl_gecko_destroy_f, "Destroy a gecko browser instance" );\r
277         Cmd_AddCommand( "gecko_navigate", cl_gecko_navigate_f, "Navigate a gecko browser to a URI" );\r
278         Cmd_AddCommand( "gecko_injecttext", cl_gecko_injecttext_f, "Injects text into a browser" );\r
279 \r
280         R_RegisterModule( "CL_Gecko", cl_gecko_start, cl_gecko_shutdown, cl_gecko_newmap );\r
281 }\r
282 \r
283 void CL_Gecko_NavigateToURI( clgecko_t *instance, const char *URI ) {\r
284         if( instance && instance->active ) {\r
285                 osgk_browser_navigate( instance->browser, URI );\r
286         }\r
287 }\r
288 \r
289 void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y ) {\r
290         // TODO: assert x, y \in [0.0, 1.0]\r
291         int mappedx, mappedy;\r
292 \r
293         if( !instance || !instance->browser ) {\r
294                 return;\r
295         }\r
296 \r
297         mappedx = x * DEFAULT_GECKO_WIDTH;\r
298         mappedy = y * DEFAULT_GECKO_HEIGHT;\r
299         osgk_browser_event_mouse_move( instance->browser, mappedx, mappedy );\r
300 }\r
301 \r
302 typedef struct geckokeymapping_s {\r
303         keynum_t keycode;\r
304         unsigned int geckokeycode;\r
305 } geckokeymapping_t;\r
306 \r
307 static geckokeymapping_t geckokeymappingtable[] = {\r
308         { K_BACKSPACE, OSGKKey_Backspace },\r
309         { K_TAB, OSGKKey_Tab },\r
310         { K_ENTER, OSGKKey_Return },\r
311         { K_SHIFT, OSGKKey_Shift },\r
312         { K_CTRL, OSGKKey_Control },\r
313         { K_ALT, OSGKKey_Alt },\r
314         { K_CAPSLOCK, OSGKKey_CapsLock },\r
315         { K_ESCAPE, OSGKKey_Escape },\r
316         { K_SPACE, OSGKKey_Space },\r
317         { K_PGUP, OSGKKey_PageUp },\r
318         { K_PGDN, OSGKKey_PageDown },\r
319         { K_END, OSGKKey_End },\r
320         { K_HOME, OSGKKey_Home },\r
321         { K_LEFTARROW, OSGKKey_Left },\r
322         { K_UPARROW, OSGKKey_Up },\r
323         { K_RIGHTARROW, OSGKKey_Right },\r
324         { K_DOWNARROW, OSGKKey_Down },\r
325         { K_INS, OSGKKey_Insert },\r
326         { K_DEL, OSGKKey_Delete },\r
327         { K_F1, OSGKKey_F1 },\r
328         { K_F2, OSGKKey_F2 },\r
329         { K_F3, OSGKKey_F3 },\r
330         { K_F4, OSGKKey_F4 },\r
331         { K_F5, OSGKKey_F5 },\r
332         { K_F6, OSGKKey_F6 },\r
333         { K_F7, OSGKKey_F7 },\r
334         { K_F8, OSGKKey_F8 },\r
335         { K_F9, OSGKKey_F9 },\r
336         { K_F10, OSGKKey_F10 },\r
337         { K_F11, OSGKKey_F11 },\r
338         { K_F12, OSGKKey_F12 },\r
339         { K_NUMLOCK, OSGKKey_NumLock },\r
340         { K_SCROLLOCK, OSGKKey_ScrollLock }\r
341 };\r
342 \r
343 qboolean CL_Gecko_Event_Key( clgecko_t *instance, int key, clgecko_buttoneventtype_t eventtype ) {\r
344         if( !instance || !instance->browser ) {\r
345                 return false;\r
346         }\r
347 \r
348         // determine whether its a keyboard event\r
349         if( key < K_OTHERDEVICESBEGIN ) {\r
350 \r
351                 OSGK_KeyboardEventType mappedtype;\r
352                 unsigned int mappedkey = key;\r
353                 \r
354                 int i;\r
355                 // yes! then convert it if necessary!\r
356                 for( i = 0 ; i < sizeof( geckokeymappingtable ) / sizeof( *geckokeymappingtable ) ; i++ ) {\r
357                         const geckokeymapping_t * const mapping = &geckokeymappingtable[ i ];\r
358                         if( key == mapping->keycode ) {\r
359                                 mappedkey = mapping->geckokeycode;\r
360                                 break;\r
361                         }\r
362                 }\r
363 \r
364                 // convert the eventtype\r
365                 // map the type\r
366                 switch( eventtype ) {\r
367                 case CLG_BET_DOWN:\r
368                         mappedtype = keDown;\r
369                         break;\r
370                 case CLG_BET_UP:\r
371                         mappedtype = keUp;\r
372                         break;\r
373                 case CLG_BET_DOUBLECLICK:\r
374                         // TODO: error message\r
375                         break;\r
376                 case CLG_BET_PRESS:\r
377                         mappedtype = kePress;\r
378                 }\r
379 \r
380                 return osgk_browser_event_key( instance->browser, mappedkey, mappedtype ) != 0;\r
381         } else if( K_MOUSE1 <= key && key <= K_MOUSE3 ) {\r
382                 OSGK_MouseButtonEventType mappedtype;\r
383                 OSGK_MouseButton mappedbutton;\r
384 \r
385                 mappedbutton = (OSGK_MouseButton) (mbLeft + (key - K_MOUSE1));\r
386 \r
387                 switch( eventtype ) {\r
388                 case CLG_BET_DOWN:\r
389                         mappedtype = meDown;\r
390                         break;\r
391                 case CLG_BET_UP:\r
392                         mappedtype = meUp;\r
393                         break;\r
394                 case CLG_BET_DOUBLECLICK:\r
395                         mappedtype = meDoubleClick;\r
396                         break;\r
397                 case CLG_BET_PRESS:\r
398                         // hihi, hacky hacky\r
399                         osgk_browser_event_mouse_button( instance->browser, mappedbutton, meDown );\r
400                         mappedtype = meUp;\r
401                         break;\r
402                 }\r
403 \r
404                 osgk_browser_event_mouse_button( instance->browser, mappedbutton, mappedtype );\r
405                 return true;\r
406         }\r
407         // TODO: error?\r
408         return false;\r
409 }\r
410 \r
411 #endif