gonzui


Format: Advanced Search

tkernel_2/driver/tef_em1d/kbpd/src/pdsim.cbare sourcepermlink (0.02 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T-Kernel 2.0 Software Package
    4:  *
    5:  *    Copyright 2011 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17.
   10:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   11:  *
   12:  *----------------------------------------------------------------------
   13:  */
   14: 
   15: /*
   16:  *      pdsim.c
   17:  *
   18:  *       KB/PD device manager
   19:  *       PD simulation
   20:  */
   21: 
   22: #include "kbpd.h"
   23: 
   24: /*
   25:  * start and stop PD simulation
   26:  */
   27: LOCAL void pdSimToggle( PdSimMode mode )
   28: {
   29:         if ( kpMgrInfo.kpState.stat.pdsim == PdSim_Off ) {
   30:                 /* start PD simulation */
   31:                 kpMgrInfo.kpState.stat.pdsim = mode;
   32:         } else {
   33:                 /* end PD simulation */
   34:                 kpMgrInfo.kpState.stat.pdsim = PdSim_Off;
   35:         }
   36: 
   37:         /* notify PD event */
   38:         kpNotifyPdEvent(TDE_PDSTATE, 0);
   39: }
   40: 
   41: /*
   42:  * main button press
   43:  */
   44: LOCAL void simButPress( void )
   45: {
   46:         /* main button press */
   47:         kpMgrInfo.kpState.stat.main = 1;
   48: 
   49:         /* notify PD event */
   50:         kpNotifyPdEvent(TDE_PDBUT, 0);
   51: }
   52: 
   53: /*
   54:  * main button release
   55:  */
   56: LOCAL void simButRelease( void )
   57: {
   58:         /* main button release */
   59:         kpMgrInfo.kpState.stat.main = 0;
   60: 
   61:         /* notify PD event */
   62:         kpNotifyPdEvent(TDE_PDBUT, 0);
   63: }
   64: 
   65: /*
   66:  * main button click presse
   67:  */
   68: LOCAL void simButClickPress( void )
   69: {
   70:         /* press */
   71:         kpMgrInfo.kpState.stat.main = 1;
   72:         kpNotifyPdEvent(TDE_PDBUT, 0);
   73: 
   74:         /* release */
   75:         kpMgrInfo.kpState.stat.main = 0;
   76:         kpNotifyPdEvent(TDE_PDBUT, 0);
   77: 
   78:         /* press */
   79:         kpMgrInfo.kpState.stat.main = 1;
   80:         kpNotifyPdEvent(TDE_PDBUT, 0);
   81: }
   82: 
   83: /*
   84:  * PD displacement
   85:  */
   86: LOCAL W simMoveUnit( BOOL start )
   87: {
   88:         const W        N = 10;
   89:         W      n;
   90: 
   91:         if ( start ) {
   92:                 /* initialize repeat count */
   93:                 kpMgrInfo.pdSimState.rep = 0;
   94:         }
   95: 
   96:         n = kpMgrInfo.pdSimState.rep;
   97:         if ( n < 20 ) kpMgrInfo.pdSimState.rep++;
   98: 
   99:         return ( kpMgrInfo.pd.pdSimSpeed < N )?
  100:                 (n / (N - kpMgrInfo.pd.pdSimSpeed + 1) + 1):
  101:                 (n * (kpMgrInfo.pd.pdSimSpeed - N + 1) + 1);
  102: }
  103: 
  104: /*
  105:  * move PD
  106:  */
  107: LOCAL void simMove( BOOL start )
  108: {
  109:         W      unit = simMoveUnit(start);
  110:         BOOL   move;
  111: 
  112:         /* relative movement */
  113:         move = kpMovePointer(
  114:                 kpMgrInfo.kpState.xpos + kpMgrInfo.pdSimState.x * unit,
  115:                 kpMgrInfo.kpState.ypos + kpMgrInfo.pdSimState.y * unit);
  116: 
  117:         if ( move ) {
  118:                 /* notify PD movement event */
  119:                 kpNotifyPdEvent(TDE_PDMOVE, 0);
  120:         }
  121: }
  122: 
  123: /*
  124:  * start PD movement
  125:  */
  126: LOCAL void simMoveStart( UH code, TMO *tmout )
  127: {
  128:         /* set movement direction */
  129:         switch ( code ) {
  130:           case KC_CC_U:                /* main CC UP */
  131:                 kpMgrInfo.pdSimState.y = -1;
  132:                 break;
  133:           case KC_CC_D:                /* main CC DOWN */
  134:                 kpMgrInfo.pdSimState.y = +1;
  135:                 break;
  136:           case KC_CC_R:                /* main CC RIGHT */
  137:                 kpMgrInfo.pdSimState.x = +1;
  138:                 break;
  139:           case KC_CC_L:                /* main CC LEFT */
  140:                 kpMgrInfo.pdSimState.x = -1;
  141:                 break;
  142:           default:
  143:                 return;
  144:         }
  145: 
  146:         /* period for location update */
  147:         *tmout = simStartTime();
  148: 
  149:         /* move */
  150:         simMove(TRUE);
  151: }
  152: 
  153: /*
  154:  * stop PD movement
  155:  */
  156: LOCAL void simMoveStop( UH code, TMO *tmout )
  157: {
  158:         switch ( code ) {
  159:           case KC_CC_U:                /* main CC UP */
  160:           case KC_CC_D:                /* main CC DOWN */
  161:                 kpMgrInfo.pdSimState.y = 0;
  162:                 if ( kpMgrInfo.pdSimState.x == 0 ) *tmout = TMO_FEVR;
  163:                 break;
  164:           case KC_CC_R:                /* main CC RIGHT */
  165:           case KC_CC_L:                /* main CC LEFT */
  166:                 kpMgrInfo.pdSimState.x = 0;
  167:                 if ( kpMgrInfo.pdSimState.y == 0 ) *tmout = TMO_FEVR;
  168:                 break;
  169:           default:
  170:                 kpMgrInfo.pdSimState.x = 0;
  171:                 kpMgrInfo.pdSimState.y = 0;
  172:                 *tmout = TMO_FEVR;
  173:         }
  174: }
  175: 
  176: /*
  177:  * check whether PD simulation is in effect
  178:  *       if PD simulation is on, return TRUE
  179:  */
  180: Inline BOOL isPdSimMode( void )
  181: {
  182:         return ( kpMgrInfo.kpState.stat.pdsim != 0 && !kpMgrInfo.pd.pdSimInh );
  183: }
  184: 
  185: /*
  186:  * check whether we can enter PD simulation mode
  187:  *       if entering PD simulation mode is not permitted, return TRUE
  188:  */
  189: Inline BOOL isPdSimBlock( void )
  190: {
  191:         return ( kpMgrInfo.pd.pdSimSpeed == 0 || kpMgrInfo.pd.pdSimInh );
  192: }
  193: 
  194: /*
  195:  * check for key to enter PD simulation mode
  196:  */
  197: LOCAL PdSimMode isPdSimModeKey( InnerEvent *evt, UH code )
  198: {
  199:         static /*const*/ KpMetaBut meta = {{0}};       /* no shift status */
  200: 
  201:         if ( kpMgrInfo.kpState.stat.rsh == 0
  202:           || kpMgrInfo.kpState.stat.lsh == 0 ) return 0;
  203: 
  204:         if ( evt->i.key.keytop.u.tenkey != 0 ) {
  205:                 if ( isMainCC(code) ) return PdSim_TenKey;
  206:                 code = kpToKeycode(evt->i.key.keytop, &meta);
  207:                 if ( isMainCC(code) ) return PdSim_TenKey;
  208:                 return 0;
  209:         }
  210: 
  211:         if ( isSubCC(code)
  212:           || code == KC_PG_U || code == KC_PG_D
  213:           || code == KC_PGUP || code == KC_PGDN
  214:           || code == KC_HOME || code == KC_END ) {
  215:                 return PdSim_MainBut;
  216:         }
  217: 
  218:         if ( isMainCC(code) ) return PdSim_Std;
  219: 
  220:         return 0;
  221: }
  222: 
  223: /*
  224:  * key down
  225:  *       if keys are related to PD simulation, do the processing, and return TRUE
  226:  */
  227: EXPORT BOOL kpExecPdSimKeyDown( InnerEvent *evt, UH code, TMO *tmout )
  228: {
  229:         PdSimMode      mode;
  230: 
  231:         if ( isPdSimBlock() ) return FALSE; /* PD simulation is prohibited */
  232: 
  233:         mode = isPdSimModeKey(evt, code);
  234:         if ( mode != 0 ) {
  235:                 /* enter or exit PD simulation mode */
  236:                 pdSimToggle(mode);
  237:                 return TRUE;
  238:         }
  239: 
  240:         if ( !isPdSimMode() ) {
  241:                 /* PD simulation mode is not on */
  242:                 return FALSE;
  243:         }
  244: 
  245:         mode = kpMgrInfo.kpState.stat.pdsim;
  246: 
  247:         if ( mode == PdSim_TenKey ) {
  248:                 if ( evt->i.key.keytop.u.tenkey == 0
  249:                   && !isSubCC(code) ) return FALSE;
  250:         }
  251: 
  252:         switch ( code ) {
  253:           case KC_PGUP:                /* PageUp */
  254:           case KC_PG_U:                /* PageUp */
  255:           case KC_SC_R:                /* sub CC key RIGHT */
  256:                 /* main button off */
  257:                 simButRelease();
  258:                 break;
  259: 
  260:           case KC_PGDN:                /* PageDown */
  261:           case KC_PG_D:                /* PageDown */
  262:           case KC_SC_L:                /* sub CC key LEFT */
  263:                 /* main button on */
  264:           case KC_END:         /* End */
  265:           case KC_SC_D:                /* sub CC key DOWN */
  266:                 /* main button press */
  267:                 simButPress();
  268:                 break;
  269: 
  270:           case KC_HOME:                /* Home */
  271:           case KC_SC_U:                /* sub CC key UP */
  272:                 /* main button click presse */
  273:                 simButClickPress();
  274:                 break;
  275: 
  276:           case KC_CC_U:                /* main CC UP */
  277:           case KC_CC_D:                /* main CC DOWN */
  278:           case KC_CC_R:                /* main CC RIGHT */
  279:           case KC_CC_L:                /* main CC LEFT */
  280:                 if ( mode == PdSim_MainBut ) return FALSE;
  281:                 /* start PD movement */
  282:                 simMoveStart(code, tmout);
  283:                 break;
  284: 
  285:           default:
  286:                 /* keys unrelated to PD simulation */
  287:                 return FALSE;
  288:         }
  289: 
  290:         return TRUE;
  291: }
  292: 
  293: /*
  294:  * key up
  295:  *       if keys are related to PD simulation, do the processing, and return TRUE
  296:  */
  297: EXPORT BOOL kpExecPdSimKeyUp( InnerEvent *evt, UH code, TMO *tmout )
  298: {
  299:         PdSimMode      mode;
  300: 
  301:         if ( isPdSimBlock() ) return FALSE; /* PD simulation is prohibited */
  302: 
  303:         if ( isPdSimModeKey(evt, code) != 0 ) {
  304:                 /* ON / OFF PD simulation mode */
  305:                 return TRUE;
  306:         }
  307: 
  308:         if ( !isPdSimMode() ) {
  309:                 /* PD simulation mode is not on */
  310:                 return FALSE;
  311:         }
  312: 
  313:         mode = kpMgrInfo.kpState.stat.pdsim;
  314: 
  315:         if ( mode == PdSim_TenKey ) {
  316:                 if ( evt->i.key.keytop.u.tenkey == 0
  317:                   && !isSubCC(code) ) return FALSE;
  318:         }
  319: 
  320:         switch ( code ) {
  321:           case KC_PGUP:                /* PageUp */
  322:           case KC_PG_U:                /* PageUp */
  323:           case KC_SC_R:                /* sub CC key RIGHT */
  324:           case KC_PGDN:                /* PageDown */
  325:           case KC_PG_D:                /* PageDown */
  326:           case KC_SC_L:                /* sub CC key LEFT */
  327:                 /* keep the state (do nothing) */
  328:                 break;
  329: 
  330:           case KC_END:         /* End */
  331:           case KC_SC_D:                /* sub CC key DOWN */
  332:           case KC_HOME:                /* Home */
  333:           case KC_SC_U:                /* sub CC key UP */
  334:                 /* main button release */
  335:                 simButRelease();
  336:                 break;
  337: 
  338:           case KC_CC_U:                /* main CC UP */
  339:           case KC_CC_D:                /* main CC DOWN */
  340:           case KC_CC_R:                /* main CC RIGHT */
  341:           case KC_CC_L:                /* main CC LEFT */
  342:                 if ( mode == PdSim_MainBut ) return FALSE;
  343:                 /* stop PD movement */
  344:                 simMoveStop(code, tmout);
  345:                 break;
  346: 
  347:           default:
  348:                 /* keys unrelated to PD simulation */
  349:                 return FALSE;
  350:         }
  351: 
  352:         return TRUE;
  353: }
  354: 
  355: /*
  356:  * repeat
  357:  */
  358: EXPORT void kpExecPdSimRepeat( TMO *tmout )
  359: {
  360:         if ( isPdSimMode() ) {
  361:                 /* location update period */
  362:                 *tmout = simIntervalTime();
  363: 
  364:                 /* move */
  365:                 simMove(FALSE);
  366:         } else {
  367:                 /* halt PD simulation */
  368:                 simMoveStop(0, tmout);
  369:         }
  370: }