]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
"Finish" cl_gecko.c (input handling is done now, too) for now. I need to update to...
authorblack <black@d7cf8633-e32d-0410-b094-e92efae38249>
Sun, 2 Dec 2007 23:03:04 +0000 (23:03 +0000)
committerblack <black@d7cf8633-e32d-0410-b094-e92efae38249>
Sun, 2 Dec 2007 23:03:04 +0000 (23:03 +0000)
I've also added a console command for text injection into a gecko browser.

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7754 d7cf8633-e32d-0410-b094-e92efae38249

cl_gecko.c
cl_gecko.h
keys.h

index 5885f8ab40e34f02a789e8b69cf47af54174ed23..46d94b9e14e28f569f4b5db69993d7d8ee04c733 100644 (file)
@@ -181,7 +181,7 @@ static void cl_gecko_destroy_f( void ) {
 \r
        if (Cmd_Argc() != 2)\r
        {\r
-               Con_Print("usage: gecko_destroy <name>\ndestroys a browser (full texture path " CLGECKOPREFIX "<name>)\n");\r
+               Con_Print("usage: gecko_destroy <name>\ndestroys a browser\n");\r
                return;\r
        }\r
 \r
@@ -196,7 +196,7 @@ static void cl_gecko_navigate_f( void ) {
 \r
        if (Cmd_Argc() != 3)\r
        {\r
-               Con_Print("usage: gecko_navigate <name> <URI>\nnavigates to a certain URI (full texture path " CLGECKOPREFIX "<name>)\n");\r
+               Con_Print("usage: gecko_navigate <name> <URI>\nnavigates to a certain URI\n");\r
                return;\r
        }\r
 \r
@@ -206,11 +206,57 @@ static void cl_gecko_navigate_f( void ) {
        CL_Gecko_NavigateToURI( CL_Gecko_FindBrowser( name ), URI );\r
 }\r
 \r
+static void cl_gecko_injecttext_f( void ) {\r
+       char name[MAX_QPATH];\r
+       const char *text;\r
+       clgecko_t *instance;\r
+       const char *p;\r
+\r
+       if (Cmd_Argc() < 3)\r
+       {\r
+               Con_Print("usage: gecko_injecttext <name> <text>\ninjects a certain text into the browser\n");\r
+               return;\r
+       }\r
+\r
+       // TODO: use snprintf instead\r
+       sprintf(name, CLGECKOPREFIX "%s", Cmd_Argv(1));\r
+       instance = CL_Gecko_FindBrowser( name );\r
+       if( !instance ) {\r
+               Con_Printf( "cl_gecko_injecttext_f: gecko instance '%s' couldn't be found!\n", name );\r
+               return;\r
+       }\r
+\r
+       text = Cmd_Argv( 2 );\r
+\r
+       for( p = text ; *p ; p++ ) {\r
+               unsigned key = *p;\r
+               switch( key ) {\r
+                       case ' ':\r
+                               key = K_SPACE;\r
+                               break;\r
+                       case '\\':\r
+                               key = *++p;\r
+                               switch( key ) {\r
+                               case 'n':\r
+                                       key = K_ENTER;\r
+                                       break;\r
+                               case '\0':\r
+                                       --p;\r
+                                       key = '\\';\r
+                                       break;\r
+                               }\r
+                               break;\r
+               }\r
+\r
+               CL_Gecko_Event_Key( instance, key, CLG_BET_PRESS );\r
+       }\r
+}\r
+\r
 void CL_Gecko_Init( void )\r
 {\r
        OSGK_EmbeddingOptions *options = osgk_embedding_options_create();\r
        osgk_embedding_options_add_search_path( options, "./xulrunner/" );\r
-   cl_geckoembedding = osgk_embedding_create_with_options( options, NULL );\r
+       cl_geckoembedding = osgk_embedding_create_with_options( options, NULL );\r
        osgk_release( options );\r
        \r
        if( cl_geckoembedding == NULL ) {\r
@@ -219,7 +265,8 @@ void CL_Gecko_Init( void )
        \r
        Cmd_AddCommand( "gecko_create", cl_gecko_create_f, "Create a gecko browser instance" );\r
        Cmd_AddCommand( "gecko_destroy", cl_gecko_destroy_f, "Destroy a gecko browser instance" );\r
-       Cmd_AddCommand( "gecko_navigate", cl_gecko_navigate_f, "Navigate a gecko browser to an URI" );\r
+       Cmd_AddCommand( "gecko_navigate", cl_gecko_navigate_f, "Navigate a gecko browser to a URI" );\r
+       Cmd_AddCommand( "gecko_injecttext", cl_gecko_injecttext_f, "Injects text into a browser" );\r
 \r
        R_RegisterModule( "CL_Gecko", cl_gecko_start, cl_gecko_shutdown, cl_gecko_newmap );\r
 }\r
@@ -230,8 +277,124 @@ void CL_Gecko_NavigateToURI( clgecko_t *instance, const char *URI ) {
        }\r
 }\r
 \r
-// TODO: write this function\r
-void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y );\r
-qboolean CL_Gecko_Event_Key( clgecko_t *instance, int key, clgecko_buttoneventtype_t eventtype );\r
+void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y ) {\r
+       // TODO: assert x, y \in [0.0, 1.0]\r
+       int mappedx, mappedy;\r
+\r
+       if( !instance || !instance->browser ) {\r
+               return;\r
+       }\r
+\r
+       mappedx = x * DEFAULT_GECKO_WIDTH;\r
+       mappedy = y * DEFAULT_GECKO_HEIGHT;\r
+       osgk_browser_event_mouse_move( instance->browser, mappedx, mappedy );\r
+}\r
+\r
+typedef struct geckokeymapping_s {\r
+       keynum_t keycode;\r
+       unsigned int geckokeycode;\r
+} geckokeymapping_t;\r
+\r
+static geckokeymapping_t geckokeymappingtable[] = {\r
+       { K_BACKSPACE, OSGKKey_Backspace },\r
+       { K_TAB, OSGKKey_Tab },\r
+       { K_ENTER, OSGKKey_Return },\r
+       { K_SHIFT, OSGKKey_Shift },\r
+       { K_CTRL, OSGKKey_Control },\r
+       { K_ALT, OSGKKey_Alt },\r
+       { K_CAPSLOCK, OSGKKey_CapsLock },\r
+       { K_ESCAPE, OSGKKey_Escape },\r
+       { K_SPACE, OSGKKey_Space },\r
+       { K_PGUP, OSGKKey_PageUp },\r
+       { K_PGDN, OSGKKey_PageDown },\r
+       { K_END, OSGKKey_End },\r
+       { K_HOME, OSGKKey_Home },\r
+       { K_LEFTARROW, OSGKKey_Left },\r
+       { K_UPARROW, OSGKKey_Up },\r
+       { K_RIGHTARROW, OSGKKey_Right },\r
+       { K_DOWNARROW, OSGKKey_Down },\r
+       { K_INS, OSGKKey_Insert },\r
+       { K_DEL, OSGKKey_Delete },\r
+       { K_F1, OSGKKey_F1 },\r
+       { K_F2, OSGKKey_F2 },\r
+       { K_F3, OSGKKey_F3 },\r
+       { K_F4, OSGKKey_F4 },\r
+       { K_F5, OSGKKey_F5 },\r
+       { K_F6, OSGKKey_F6 },\r
+       { K_F7, OSGKKey_F7 },\r
+       { K_F8, OSGKKey_F8 },\r
+       { K_F9, OSGKKey_F9 },\r
+       { K_F10, OSGKKey_F10 },\r
+       { K_F11, OSGKKey_F11 },\r
+       { K_F12, OSGKKey_F12 },\r
+       { K_NUMLOCK, OSGKKey_NumLock },\r
+       { K_SCROLLOCK, OSGKKey_ScrollLock }\r
+};\r
+\r
+qboolean CL_Gecko_Event_Key( clgecko_t *instance, int key, clgecko_buttoneventtype_t eventtype ) {\r
+       if( !instance || !instance->browser ) {\r
+               return false;\r
+       }\r
+\r
+       // determine whether its a keyboard event\r
+       if( key < K_OTHERDEVICESBEGIN ) {\r
+\r
+               OSGK_KeyboardEventType mappedtype;\r
+               unsigned int mappedkey = key;\r
+               \r
+\r
+               int i;\r
+               // yes! then convert it if necessary!\r
+               for( i = 0 ; i < sizeof( geckokeymappingtable ) / sizeof( *geckokeymappingtable ) ; i++ ) {\r
+                       const geckokeymapping_t * const mapping = &geckokeymappingtable[ i ];\r
+                       if( key == mapping->keycode ) {\r
+                               mappedkey = mapping->geckokeycode;\r
+                               break;\r
+                       }\r
+               }\r
+\r
+               // convert the eventtype\r
+               // map the type\r
+               switch( eventtype ) {\r
+               case CLG_BET_DOWN:\r
+                       mappedtype = keDown;\r
+                       break;\r
+               case CLG_BET_UP:\r
+                       mappedtype = keUp;\r
+                       break;\r
+               case CLG_BET_DOUBLECLICK:\r
+                       // TODO: error message\r
+                       break;\r
+               case CLG_BET_PRESS:\r
+                       mappedtype = kePress;\r
+               }\r
+\r
+               return osgk_browser_event_key( instance->browser, mappedkey, mappedtype ) != 0;\r
+       } else if( K_MOUSE1 <= key && key <= K_MOUSE3 ) {\r
+               OSGK_MouseButtonEventType mappedtype;\r
+               OSGK_MouseButton mappedbutton;\r
+\r
+               mappedbutton = (OSGK_MouseButton) (mbLeft + (key - K_MOUSE1));\r
+\r
+               switch( eventtype ) {\r
+               case CLG_BET_DOWN:\r
+                       mappedtype = meDown;\r
+                       break;\r
+               case CLG_BET_UP:\r
+                       mappedtype = meUp;\r
+                       break;\r
+               case CLG_BET_DOUBLECLICK:\r
+                       mappedtype = meDoubleClick;\r
+                       break;\r
+               case CLG_BET_PRESS:\r
+                       // TODO: error message\r
+                       break;\r
+               }\r
+\r
+               return true;\r
+       }\r
+       // TODO: error?\r
+       return false;\r
+}\r
 \r
 #endif
\ No newline at end of file
index ce380743f9ce76e8245d3ffb1a1cf7e0982aaed2..11c02902440773da8ef4ff9f0a6e275e4095c245 100644 (file)
@@ -30,6 +30,7 @@ clgecko_t * CL_Gecko_FindBrowser( const char *name );
 void CL_Gecko_DestroyBrowser( clgecko_t *instance );\r
 \r
 void CL_Gecko_NavigateToURI( clgecko_t *instance, const char *URI );\r
+// x and y between 0.0 and 1.0 (0.0 is top-left?)\r
 void CL_Gecko_Event_CursorMove( clgecko_t *instance, float x, float y );\r
 \r
 // returns whether the key/button event was handled or not\r
diff --git a/keys.h b/keys.h
index 3736ac4d561419af08bbf85197d1bf3a591b4fa3..7e3e9ef27b1ad8e614b2b9f19234fe422dae9a1c 100644 (file)
--- a/keys.h
+++ b/keys.h
@@ -107,6 +107,7 @@ typedef enum keynum_e
        // mouse buttons generate virtual keys
 
        K_MOUSE1 = 512,
+       K_OTHERDEVICESBEGIN = K_MOUSE1,
        K_MOUSE2,
        K_MOUSE3,
        K_MWHEELUP,