model_server/em/src/em.cxx
/*_______________________________________________________________________
!
! Function:	em_enter_string
!
! Arguments:	Window win
!		char* theString
!		unsigned int modifiers 
!		     Bit wise OR the following modifiers for held
!		     down keys:
!	     		Shift	0x0001
!	     		Lock	0x0002	
!	     		Control	0x0004	
!	     		Mod1	0x0008	(Alt or Meta)	
!	     		Mod2	0x0010	
!	     		Mod3	0x0020	
!	     		Mod4	0x0040
!	     		Mod5	0x0080
!       
! Returns:	void
!
! Synopsis:	Passes each character in the string as an event to the 
!			passed window.		
!
_______________________________________________________________________*/


Window win
char* theString
unsigned int modifiers
Bit wise OR the following modifiers for held
down keys:
Shift   0x0001
Lock    0x0002
Control 0x0004
Mod1    0x0008  (Alt or Meta)
Mod2    0x0010
Mod3    0x0020
Mod4    0x0040
Mod5    0x0080

void

Passes each character in the string as an event to the
passed window.


Window win char * theString unsigned int modifiers Bit wise OR the following modifiers for held down keys : Shift 0x0001 Lock 0x0002 Control 0x0004 Mod1 0x0008 ( Alt or Meta ) Mod2 0x0010 Mod3 0x0020 Mod4 0x0040 Mod5 0x0080 void Passes each character in the string as an event to the passed window .
void em_enter_string(Window win, char* theString, unsigned int modifier_keys = 0) {
    Initialize(em_enter_string);
    if ((theString) && (win)){
	char current_char = '\0';
	XEvent ev;
	int row_size = 0;
	KeySym key_symbol = 0;
	int min_keycode = 0;
	int max_keycode = 0;
	XDisplayKeycodes(UxDisplay, &min_keycode, &max_keycode);
	KeySym * key_table = XGetKeyboardMapping(UxDisplay, min_keycode, 
						 max_keycode - min_keycode, &row_size);
	int length = strlen(theString);
	for(int i = 0; i < length; i++) {
	    current_char = theString[i];
	    if (current_char == '\\') {
		if (i +1 < length) {
		    switch (theString[i+1]) {
		      case 'r':
		      case 'n':
			current_char = '\n';
			i += 1;
			break;
		      case 't':
			current_char = '\t';
			i += 1;
			break;
		      case 'b':
			current_char = '\b';
			i += 1;
			break;
		    }
		}		
	    }
	    unsigned int key_code = XKeysymToKeycode(UxDisplay, (KeySym)current_char);
	    bool AnEscapeChar = FALSE;
	    switch (current_char) {
	      case '\a' :
		break;
	      case '\b' : 
		key_code = XKeysymToKeycode(UxDisplay, (KeySym)0xFF08);
		AnEscapeChar = TRUE;
		break;
	      case '\f' :
		break;
	      case '\n' :
	      case '\r' : 
		key_code = XKeysymToKeycode(UxDisplay, (KeySym)0xFF0D);
		AnEscapeChar = TRUE;
		break;
	      case '\t' : 
		key_code = XKeysymToKeycode(UxDisplay, (KeySym)0xFF09);
		AnEscapeChar = TRUE;
		break;
	      case '\v' :
		break;
	      case '\0' :
		break;
	      default:		
		break;
	    }
	    unsigned int additional_modifiers = 0;
	    if ((key_code >= min_keycode) && (key_code <= max_keycode)) {
		if(isalpha(current_char)) {
		    if(isupper(current_char))
			additional_modifiers = 1;
		} else if(!AnEscapeChar){
		    for(int column = 1; column < row_size; column++)  
			if(key_table[(key_code - min_keycode) * row_size + column] == current_char)
			    additional_modifiers |= 1<<(column - 1);
		}
		struct timeval time_values;
		struct timezone time_zones;
		OSapi_gettimeofday(&time_values, &time_zones);
		if (starttime == 0)
		    starttime = time_values.tv_sec - 10;
		unsigned long timestamp = (time_values.tv_sec - starttime) * 1000 +
		    (time_values.tv_usec / 1000);  
		Window root = NULL, child = NULL;
		int rootx = 0, rooty = 0, x= 0, y = 0;
		unsigned int but = 0;
		XQueryPointer (UxDisplay, win, &root, &child,
			       &rootx, &rooty, &x, &y, &but);
		ev.xkey.type = KeyPress; 
		ev.xkey.serial = 0; 
		ev.xkey.send_event = FALSE; 
		ev.xkey.display = UxDisplay; 
		ev.xkey.window = win;	     
		ev.xkey.root = win;	     
		ev.xkey.subwindow = child;     
		ev.xkey.time = timestamp;
		ev.xkey.x = x; 
		ev.xkey.y = y;	
		ev.xkey.x_root = rootx; 
		ev.xkey.y_root = rooty; 
		ev.xkey.state = (modifier_keys | additional_modifiers);
		ev.xkey.keycode = key_code; 
		ev.xkey.same_screen = True; 
       		XSendEvent (UxDisplay, child, True, 0, &ev);
		OSapi_usleep(1000); 
		OSapi_gettimeofday(&time_values, &time_zones);
		timestamp = (time_values.tv_sec - starttime) * 1000 +
		    (time_values.tv_usec / 1000);  
		ev.xkey.type = KeyRelease; 
		ev.xkey.serial = 0; 
		ev.xkey.send_event = FALSE; 
		ev.xkey.display = UxDisplay; 
		ev.xkey.window = win;	     
		ev.xkey.root = win;	     
		ev.xkey.subwindow = child;     
		ev.xkey.time = timestamp;
		ev.xkey.x = x; 
		ev.xkey.y = y;	
		ev.xkey.x_root = rootx; 
		ev.xkey.y_root = rooty; 
		ev.xkey.state = (modifier_keys | additional_modifiers);
		ev.xkey.keycode = key_code; 
		ev.xkey.same_screen = True; 
		XSendEvent (UxDisplay, win, True, 0, &ev);
	    }
	}
    }
}
void em_enter_string ( Window win , char * theString , unsigned int modifier_keys = 0 ) { Initialize ( em_enter_string ) ; if ( ( theString ) && ( win ) ) { char current_char = ' \0 ' ; XEvent ev ; int row_size = 0 ; KeySym key_symbol = 0 ; int min_keycode = 0 ; int max_keycode = 0 ; XDisplayKeycodes ( UxDisplay , & min_keycode , & max_keycode ) ; KeySym * key_table = XGetKeyboardMapping ( UxDisplay , min_keycode , max_keycode - min_keycode , & row_size ) ; int length = strlen ( theString ) ; for ( int i = 0 ; i < length ; i ++ ) { current_char = theString [ i ] ; if ( current_char == ' \\ ' ) { if ( i + 1 < length ) { switch ( theString [ i + 1 ] ) { case ' ' : case ' ' : current_char = ' \n ' ; i += 1 ; break ; case ' ' : current_char = ' \t ' ; i += 1 ; break ; case ' ' : current_char = ' \b ' ; i += 1 ; break ; } } } unsigned int key_code = XKeysymToKeycode ( UxDisplay , ( KeySym ) current_char ) ; bool AnEscapeChar = FALSE ; switch ( current_char ) { case ' \a ' : break ; case ' \b ' : key_code = XKeysymToKeycode ( UxDisplay , ( KeySym ) 0xFF08 ) ; AnEscapeChar = TRUE ; break ; case ' \f ' : break ; case ' \n ' : case ' \r ' : key_code = XKeysymToKeycode ( UxDisplay , ( KeySym ) 0xFF0D ) ; AnEscapeChar = TRUE ; break ; case ' \t ' : key_code = XKeysymToKeycode ( UxDisplay , ( KeySym ) 0xFF09 ) ; AnEscapeChar = TRUE ; break ; case ' \v ' : break ; case ' \0 ' : break ; default : break ; } unsigned int additional_modifiers = 0 ; if ( ( key_code >= min_keycode ) && ( key_code <= max_keycode ) ) { if ( isalpha ( current_char ) ) { if ( isupper ( current_char ) ) additional_modifiers = 1 ; } else if ( ! AnEscapeChar ) { for ( int column = 1 ; column < row_size ; column ++ ) if ( key_table [ ( key_code - min_keycode ) * row_size + column ] == current_char ) additional_modifiers |= 1 << ( column - 1 ) ; } struct timeval time_values ; struct timezone time_zones ; OSapi_gettimeofday ( & time_values , & time_zones ) ; if ( starttime == 0 ) starttime = time_values . tv_sec - 10 ; unsigned long timestamp = ( time_values . tv_sec - starttime ) * 1000 + ( time_values . tv_usec / 1000 ) ; Window root = NULL , child = NULL ; int rootx = 0 , rooty = 0 , x = 0 , y = 0 ; unsigned int but = 0 ; XQueryPointer ( UxDisplay , win , & root , & child , & rootx , & rooty , & x , & y , & but ) ; ev . xkey . type = KeyPress ; ev . xkey . serial = 0 ; ev . xkey . send_event = FALSE ; ev . xkey . display = UxDisplay ; ev . xkey . window = win ; ev . xkey . root = win ; ev . xkey . subwindow = child ; ev . xkey . time = timestamp ; ev . xkey . x = x ; ev . xkey . y = y ; ev . xkey . x_root = rootx ; ev . xkey . y_root = rooty ; ev . xkey . state = ( modifier_keys | additional_modifiers ) ; ev . xkey . keycode = key_code ; ev . xkey . same_screen = True ; XSendEvent ( UxDisplay , child , True , 0 , & ev ) ; OSapi_usleep ( 1000 ) ; OSapi_gettimeofday ( & time_values , & time_zones ) ; timestamp = ( time_values . tv_sec - starttime ) * 1000 + ( time_values . tv_usec / 1000 ) ; ev . xkey . type = KeyRelease ; ev . xkey . serial = 0 ; ev . xkey . send_event = FALSE ; ev . xkey . display = UxDisplay ; ev . xkey . window = win ; ev . xkey . root = win ; ev . xkey . subwindow = child ; ev . xkey . time = timestamp ; ev . xkey . x = x ; ev . xkey . y = y ; ev . xkey . x_root = rootx ; ev . xkey . y_root = rooty ; ev . xkey . state = ( modifier_keys | additional_modifiers ) ; ev . xkey . keycode = key_code ; ev . xkey . same_screen = True ; XSendEvent ( UxDisplay , win , True , 0 , & ev ) ; } } } }
_______________________________________________________________________
Function:      em_enter_string
_______________________________________________________________________ Function : em_enter_string
//	XSynchronize(UxDisplay, TRUE); /* send an event for each character. */ /* the auto_test parser recognizes a backslash and inserts */ /* a second backslash to negate it.  We must remove this. */ /* Get the correct keycode and any additional modifier keys, such as <SHIFT>. */ /* See X.h */ /* ShiftMask	(1<<0)	*/ /* LockMask		(1<<1)	*/ /* ControlMask	(1<<2)	*/ /* Mod1Mask		(1<<3)	*/ /* Mod2Mask		(1<<4)	*/ /* Mod3Mask		(1<<5)	*/ /* Mod4Mask		(1<<6)	*/ /* Mod5Mask		(1<<7)	*/ /* Important note: The key map does not contain lower case letters and the */ /* upper case letters are in the first (unmodified) column.  So letters must */ /* be handled differently.  Try executing xmodmap -pk in a shell. */ /* Backspace */ /* Return */ /* Tab */ /* Get the time of day in microseconds. */ /* timestamp in milliseconds */ /* Get the cursor position. */ /* Send the event. */ /* # of last request processed by server */ /* true if this came from a SendEvent request */ /* Display the event was read from */ /* "event" window it is reported relative to */ /* root window that the event occured on */ /* child window */ /* pointer x, y coordinates in event window */ /* coordinates relative to root */ /* detail */ /* same screen flag */ /* insure that the timestamp changes (just in case) */ /* timestamp in milliseconds */ /* # of last request processed by server */ /* true if this came from a SendEvent request */ /* Display the event was read from */ /* "event" window it is reported relative to */ /* root window that the event occured on */ /* child window */ /* pointer x, y coordinates in event window */ /* coordinates relative to root */ /* detail */ /* same screen flag */ //	XSynchronize(UxDisplay, FALSE);