/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import java.lang.reflect.*;
import java.util.*;
import sun.awt.*;
import org.havi.ui.event.*;
import com.adb.util.*;

public class UserInput {
 public static final int PRESS_RELEASE_DELAY = 200;

 private static Robot robot;
 private static StringIntHashtable keymap;

 static {
  keymap=new StringIntHashtable();

  try {
   Class c=Class.forName("tv.osmosys.dvb.event.util.EventLookup");
   Field f=c.getDeclaredField("codes");
   f.setAccessible(true);

   IntHashtable codes=(IntHashtable)f.get(null);

   Enumeration e=codes.keys();

   while(e.hasMoreElements()) {
    int keyid=((Integer)e.nextElement()).intValue();
    String name=(String)codes.get(keyid);

    if (name.startsWith("VK_")) {
     name=(name.substring(3)).toLowerCase();
     keymap.add(name,keyid);
    }
   }
  } catch(Throwable t) {}
 };

 static {
  try {
   robot=new Robot();
  } catch(Throwable t) {}
 } 

 public static int[] get_keytab(String keyseq) {
  Vector v=new Vector();

  int pos=0;

  while(pos<keyseq.length()) {
   char c=keyseq.charAt(pos);

   int keyid=-1;

   if (c=='<') {
    String keyname=null;
    
    int nextpos=keyseq.indexOf('>',pos);    

    if (nextpos>0) {
     keyname=keyseq.substring(pos+1,nextpos);

     keyid=keymap.get(keyname);

     pos=nextpos+1;
    } else return null;

   } else {
    keyid=(int)c;
    pos++;
   }

   if (keyid>0) {
    v.addElement(new Integer(keyid));
   }
  }

  if (v.size()==0) return null;

  int keytab[]=new int[v.size()];  

  for(int i=0;i<v.size();i++) {
   keytab[i]=((Integer)v.elementAt(i)).intValue();
  }

  return keytab;
 }

 public static void press_key(int keyid) {
  robot.keyPress(keyid);
  robot.delay(PRESS_RELEASE_DELAY);
 }

 private static void key_sequence(int keys[]) {
  for(int i=0;i<keys.length;i++) {
   press_key(keys[i]);
  } 
 }

 public static boolean key_sequence(String keyseq) {
  boolean res=false;

  int keytab[]=UserInput.get_keytab(keyseq);

  if (keytab!=null) {
   UserInput.key_sequence(keytab);
   res=true;
  }

  return res;
 }
}
