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