ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

ResidualVMEvents.java

Go to the documentation of this file.
00001 package org.residualvm.residualvm;
00002 
00003 import android.os.Handler;
00004 import android.os.Message;
00005 import android.util.DisplayMetrics;
00006 import android.content.Context;
00007 import android.view.InputDevice;
00008 import android.view.KeyEvent;
00009 import android.view.KeyCharacterMap;
00010 import android.view.MotionEvent;
00011 import android.view.View;
00012 import android.view.ViewConfiguration;
00013 import android.view.GestureDetector;
00014 import android.view.InputDevice;
00015 import android.view.inputmethod.InputMethodManager;
00016 
00017 public class ResidualVMEvents implements
00018         android.view.View.OnKeyListener,
00019         android.view.View.OnTouchListener,
00020         android.view.GestureDetector.OnGestureListener,
00021         android.view.GestureDetector.OnDoubleTapListener {
00022 
00023     public static final int JE_SYS_KEY = 0;
00024     public static final int JE_KEY = 1;
00025     public static final int JE_DPAD = 2;
00026     public static final int JE_DOWN = 3;
00027     public static final int JE_SCROLL = 4;
00028     public static final int JE_TAP = 5;
00029     public static final int JE_DOUBLE_TAP = 6;
00030     public static final int JE_MULTI = 7;
00031     public static final int JE_BALL = 8;
00032     public static final int JE_LMB_DOWN = 9;
00033     public static final int JE_LMB_UP = 10;
00034     public static final int JE_RMB_DOWN = 11;
00035     public static final int JE_RMB_UP = 12;
00036     public static final int JE_MOUSE_MOVE = 13;
00037     public static final int JE_GAMEPAD = 14;
00038     public static final int JE_JOYSTICK = 15;
00039     public static final int JE_MMB_DOWN = 16;
00040     public static final int JE_MMB_UP = 17;
00041     public static final int JE_TOUCH = 18;
00042     public static final int JE_LONG = 19;
00043     public static final int JE_FLING = 20;
00044     public static final int JE_QUIT = 0x1000;
00045 
00046     final protected Context _context;
00047     final protected ResidualVM _residualvm;
00048     final protected GestureDetector _gd;
00049     final protected int _longPress;
00050     final protected MouseHelper _mouseHelper;
00051 
00052     public ResidualVMEvents(Context context, ResidualVM residualvm, MouseHelper mouseHelper) {
00053         _context = context;
00054         _residualvm = residualvm;
00055         _mouseHelper = mouseHelper;
00056 
00057         _gd = new GestureDetector(context, this);
00058         _gd.setOnDoubleTapListener(this);
00059         _gd.setIsLongpressEnabled(false);
00060 
00061         _longPress = ViewConfiguration.getLongPressTimeout();
00062     }
00063 
00064     final public void sendQuitEvent() {
00065         _residualvm.pushEvent(JE_QUIT, 0, 0, 0, 0, 0, 0);
00066     }
00067 
00068     public boolean onTrackballEvent(MotionEvent e) {
00069         _residualvm.pushEvent(JE_BALL, e.getAction(),
00070                             (int)(e.getX() * e.getXPrecision() * 100),
00071                             (int)(e.getY() * e.getYPrecision() * 100),
00072                             0, 0, 0);
00073         return true;
00074     }
00075 
00076     public boolean onGenericMotionEvent(MotionEvent e) {
00077         return false;
00078     }
00079 
00080     final static int MSG_MENU_LONG_PRESS = 1;
00081 
00082     final private Handler keyHandler = new Handler() {
00083         @Override
00084         public void handleMessage(Message msg) {
00085             if (msg.what == MSG_MENU_LONG_PRESS) {
00086                 InputMethodManager imm = (InputMethodManager)
00087                     _context.getSystemService(Context.INPUT_METHOD_SERVICE);
00088 
00089                 if (imm != null)
00090                     imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
00091             }
00092         }
00093     };
00094 
00095     // OnKeyListener
00096     @Override
00097     final public boolean onKey(View v, int keyCode, KeyEvent e) {
00098         final int action = e.getAction();
00099 
00100         if (keyCode == 238) {
00101             // this (undocumented) event is sent when ACTION_HOVER_ENTER or ACTION_HOVER_EXIT occurs
00102             return false;
00103         }
00104 
00105         if (keyCode == KeyEvent.KEYCODE_BACK) {
00106             if (action != KeyEvent.ACTION_UP) {
00107                 // only send event from back button on up event, since down event is sent on right mouse click and
00108                 // cannot be caught (thus rmb click would send escape key first)
00109                 return true;
00110             }
00111 
00112             if (_mouseHelper != null) {
00113                 if (_mouseHelper.getRmbGuard()) {
00114                     // right mouse button was just clicked which sends an extra back button press
00115                     return true;
00116                 }
00117             }
00118         }
00119 
00120         if (e.isSystem()) {
00121             // filter what we handle
00122             switch (keyCode) {
00123             case KeyEvent.KEYCODE_BACK:
00124             case KeyEvent.KEYCODE_MENU:
00125             case KeyEvent.KEYCODE_CAMERA:
00126             case KeyEvent.KEYCODE_SEARCH:
00127             case KeyEvent.KEYCODE_MEDIA_PLAY:
00128             case KeyEvent.KEYCODE_MEDIA_PAUSE:
00129                 break;
00130 
00131             default:
00132                 return false;
00133             }
00134 
00135             // no repeats for system keys
00136             if (e.getRepeatCount() > 0)
00137                 return false;
00138 
00139             // Have to reimplement hold-down-menu-brings-up-softkeybd
00140             // ourselves, since we are otherwise hijacking the menu key :(
00141             // See com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel()
00142             // for the usual Android implementation of this feature.
00143             if (keyCode == KeyEvent.KEYCODE_MENU) {
00144                 final boolean fired =
00145                     !keyHandler.hasMessages(MSG_MENU_LONG_PRESS);
00146 
00147                 keyHandler.removeMessages(MSG_MENU_LONG_PRESS);
00148 
00149                 if (action == KeyEvent.ACTION_DOWN) {
00150                     keyHandler.sendMessageDelayed(keyHandler.obtainMessage(
00151                                     MSG_MENU_LONG_PRESS), _longPress);
00152                     return true;
00153                 }
00154 
00155                 if (fired)
00156                     return true;
00157 
00158                 // only send up events of the menu button to the native side
00159                 if (action != KeyEvent.ACTION_UP)
00160                     return true;
00161             }
00162 
00163             _residualvm.pushEvent(JE_SYS_KEY, action, keyCode, 0, 0, 0, 0);
00164 
00165             return true;
00166         }
00167 
00168         // sequence of characters
00169         if (action == KeyEvent.ACTION_MULTIPLE &&
00170                 keyCode == KeyEvent.KEYCODE_UNKNOWN) {
00171             final KeyCharacterMap m = KeyCharacterMap.load(e.getDeviceId());
00172             final KeyEvent[] es = m.getEvents(e.getCharacters().toCharArray());
00173 
00174             if (es == null)
00175                 return true;
00176 
00177             for (KeyEvent s : es) {
00178                 _residualvm.pushEvent(JE_KEY, s.getAction(), s.getKeyCode(),
00179                     s.getUnicodeChar() & KeyCharacterMap.COMBINING_ACCENT_MASK,
00180                     s.getMetaState(), s.getRepeatCount(), 0);
00181             }
00182 
00183             return true;
00184         }
00185 
00186         switch (keyCode) {
00187         case KeyEvent.KEYCODE_DPAD_UP:
00188         case KeyEvent.KEYCODE_DPAD_DOWN:
00189         case KeyEvent.KEYCODE_DPAD_LEFT:
00190         case KeyEvent.KEYCODE_DPAD_RIGHT:
00191         case KeyEvent.KEYCODE_DPAD_CENTER:
00192             _residualvm.pushEvent(JE_DPAD, action, keyCode,
00193                                 (int)(e.getEventTime() - e.getDownTime()),
00194                                 e.getRepeatCount(), 0, 0);
00195             return true;
00196         case KeyEvent.KEYCODE_BUTTON_A:
00197         case KeyEvent.KEYCODE_BUTTON_B:
00198         case KeyEvent.KEYCODE_BUTTON_C:
00199         case KeyEvent.KEYCODE_BUTTON_X:
00200         case KeyEvent.KEYCODE_BUTTON_Y:
00201         case KeyEvent.KEYCODE_BUTTON_Z:
00202         case KeyEvent.KEYCODE_BUTTON_L1:
00203         case KeyEvent.KEYCODE_BUTTON_R1:
00204         case KeyEvent.KEYCODE_BUTTON_L2:
00205         case KeyEvent.KEYCODE_BUTTON_R2:
00206         case KeyEvent.KEYCODE_BUTTON_THUMBL:
00207         case KeyEvent.KEYCODE_BUTTON_THUMBR:
00208         case KeyEvent.KEYCODE_BUTTON_START:
00209         case KeyEvent.KEYCODE_BUTTON_SELECT:
00210         case KeyEvent.KEYCODE_BUTTON_MODE:
00211             _residualvm.pushEvent(JE_GAMEPAD, action, keyCode,
00212                                 (int)(e.getEventTime() - e.getDownTime()),
00213                                 e.getRepeatCount(), 0, 0);
00214             return true;
00215         }
00216 
00217         _residualvm.pushEvent(JE_KEY, action, keyCode,
00218                     e.getUnicodeChar() & KeyCharacterMap.COMBINING_ACCENT_MASK,
00219                     e.getMetaState(), e.getRepeatCount(), 0);
00220 
00221         return true;
00222     }
00223 
00224     // OnTouchListener
00225     @Override
00226     final public boolean onTouch(View v, MotionEvent e) {
00227         if (_mouseHelper != null) {
00228             boolean isMouse = MouseHelper.isMouse(e);
00229             if (isMouse) {
00230                 // mouse button is pressed
00231                 return _mouseHelper.onMouseEvent(e, false);
00232             }
00233         }
00234 
00235         final int action = e.getAction();
00236 
00237         // constants from APIv5:
00238         // (action & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT
00239         final int pointer = (action & 0xff00) >> 8;
00240 
00241         if (pointer > 0) {
00242             _residualvm.pushEvent(JE_MULTI, pointer, action & 0xff, // ACTION_MASK
00243                                 (int)e.getX(), (int)e.getY(), 0, 0);
00244             return true;
00245         }
00246 
00247         return _gd.onTouchEvent(e);
00248     }
00249 
00250     // OnGestureListener
00251     @Override
00252     final public boolean onDown(MotionEvent e) {
00253         _residualvm.pushEvent(JE_DOWN, (int)e.getX(), (int)e.getY(), 0, 0, 0, 0);
00254         return true;
00255     }
00256 
00257     @Override
00258     final public boolean onFling(MotionEvent e1, MotionEvent e2,
00259                                     float velocityX, float velocityY) {
00260         return false;
00261     }
00262 
00263     @Override
00264     final public void onLongPress(MotionEvent e) {
00265         // disabled, interferes with drag&drop
00266     }
00267 
00268     @Override
00269     final public boolean onScroll(MotionEvent e1, MotionEvent e2,
00270                                     float distanceX, float distanceY) {
00271         _residualvm.pushEvent(JE_SCROLL, (int)e1.getX(), (int)e1.getY(),
00272                             (int)e2.getX(), (int)e2.getY(), 0, 0);
00273 
00274         return true;
00275     }
00276 
00277     @Override
00278     final public void onShowPress(MotionEvent e) {
00279     }
00280 
00281     @Override
00282     final public boolean onSingleTapUp(MotionEvent e) {
00283         return false;
00284     }
00285 
00286     // OnDoubleTapListener
00287     @Override
00288     final public boolean onDoubleTap(MotionEvent e) {
00289         return false;
00290     }
00291 
00292     @Override
00293     final public boolean onDoubleTapEvent(MotionEvent e) {
00294         _residualvm.pushEvent(JE_DOUBLE_TAP, (int)e.getX(), (int)e.getY(),
00295                             e.getAction(), 0, 0, 0);
00296 
00297         return true;
00298     }
00299 
00300     @Override
00301     final public boolean onSingleTapConfirmed(MotionEvent e) {
00302         _residualvm.pushEvent(JE_TAP, (int)e.getX(), (int)e.getY(),
00303                 (int)(e.getEventTime() - e.getDownTime()), 0, 0, 0);
00304         return true;
00305     }
00306 
00307 }


Generated on Sat Jun 27 2020 05:00:31 for ResidualVM by doxygen 1.7.1
curved edge   curved edge