gonzui


Format: Advanced Search

tkernel_2/driver/tef_em1d/kbpd/src/etc.cbare sourcepermlink (0.06 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 T-Engine Forum at 2014/09/10.
   11:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   12:  *
   13:  *----------------------------------------------------------------------
   14:  */
   15: 
   16: /*
   17:  *      etc.c
   18:  *
   19:  *       KB/PD device manager
   20:  *       miscellaneous functions
   21:  */
   22: 
   23: #include "kbpd.h"
   24: 
   25: /* ===== event notification ===================================================== */
   26: 
   27: /*
   28:  * event notification
   29:  */
   30: EXPORT ER kpNotifyEvent( void *evt, W size )
   31: {
   32:         ER     ercd;
   33: 
   34:         /* unnecessary to notify event if message buffer for event notification is unspecified */
   35:         if ( kpMgrInfo.eventMbf == InvalidID ) return E_OK;
   36: 
   37:         /* event notification */
   38:         ercd = tk_snd_mbf(kpMgrInfo.eventMbf, evt, size, TMO_FEVR);
   39:         if ( ercd != E_OK ) {
   40:                 DEBUG_PRINT(("kpNotifyEvent, tk_snd_mbf err = %d\n", ercd));
   41:                 return ercd;
   42:         }
   43: 
   44:         return E_OK;
   45: }
   46: 
   47: /*
   48:  * notify meta key status change event
   49:  */
   50: EXPORT ER kpNotifyMetaEvent( void )
   51: {
   52:         KeyEvt keyEvt;
   53:         ER     err;
   54: 
   55:         /* set meta key status change event */
   56:         keyEvt.h.evttyp = TDE_KEYMETA;
   57:         keyEvt.keytop = 0;
   58:         keyEvt.code = 0;
   59:         keyEvt.stat = kpMgrInfo.kpState.stat;
   60: 
   61:         /* event notification */
   62:         err = kpNotifyEvent(&keyEvt, sizeof(keyEvt));
   63:         if ( err != E_OK ) {
   64:                 DEBUG_PRINT(("kpNotifyMetaEvent, err = %d\n", err));
   65:                 return err;
   66:         }
   67: 
   68:         return E_OK;
   69: }
   70: 
   71: /*
   72:  * notify PD event
   73:  */
   74: EXPORT ER kpNotifyPdEvent( TDEvtTyp devEvt, UW nodsp )
   75: {
   76:         PdEvt  pdEvt;
   77:         ER     err;
   78: 
   79:         /* change pointer invisible status */
   80:         kpMgrInfo.kpState.stat.nodsp = nodsp;
   81: 
   82:         /* set PD event */
   83:         pdEvt.h.evttyp = devEvt;
   84:         pdEvt.stat = kpMgrInfo.kpState;
   85: 
   86:         /* event notification */
   87:         err = kpNotifyEvent(&pdEvt, sizeof(pdEvt));
   88:         if ( err != E_OK ) {
   89:                 DEBUG_PRINT(("kpNotifyPdEvent, err = %d\n", err));
   90:                 return err;
   91:         }
   92: 
   93:         return E_OK;
   94: }
   95: 
   96: /* ===== KB ========================================================== */
   97: 
   98: /*
   99:  * conversion from key top code to key code
  100:  */
  101: EXPORT UH kpToKeycode( KeyTop keytop, KpMetaBut *meta )
  102: {
  103: #define keyTab  kbdef->keyDef.keytab
  104: 
  105:         KbDef  *kbdef;
  106:         W      sel;
  107:         UH     code;
  108: 
  109:         kbdef = GetKbDef(meta->o.kbsel, keytop.u.kid);
  110:         if ( kbdef == NULL ) {
  111:                 /* if keytable for kbsel does not exist, then
  112:                    use keyboard definition 1 (kbsel=0) */
  113:                 kbdef = GetKbDef(0, keytop.u.kid);
  114:                 if ( kbdef == NULL ) return InvalidKeycode;
  115:         }
  116: 
  117:         if ( keytop.u.kcode >= keyTab.keymax ) return InvalidKeycode;
  118: 
  119:         sel = (*(UW*)meta & 0xfc) >> 2;
  120:         code = keyTab.kct[keyTab.keymax * keyTab.kctsel[sel] + keytop.u.kcode];
  121: 
  122:         return code;
  123: 
  124: #undef keyTab
  125: }
  126: 
  127: /*
  128:  * find out key type
  129:  *     return InKeyKind if it is direct key input
  130:  *       if it is shiftkey, then return ShiftKeyKind
  131:  */
  132: EXPORT W kpGetKeyKind( KeyTop keytop )
  133: {
  134:         UH                     code;
  135: 
  136:         /* evaluate keycode obtained from the keymap corresponding to the current shift status. */
  137:         code = kpToKeycode(keytop, (KpMetaBut*)&kpMgrInfo.kpState.stat);
  138: 
  139:         switch ( code ) {
  140:           /* shift key */
  141:           case KC_SHT_R:       return SK_SHIFT_R;    /* shift right */
  142:           case KC_SHT_L:       return SK_SHIFT_L;    /* shift Left */
  143:           case KC_EXP:         return SK_EXPANSION;   /* expansion */
  144:           case KC_CMD:         return SK_COMMAND;     /* command */
  145: 
  146:           /* direct key input */
  147:           case KC_CC_U:                return IK_CC_U;               /* main CC */
  148:           case KC_CC_D:                return IK_CC_D;               /* main CC */
  149:           case KC_CC_R:                return IK_CC_R;               /* main CC */
  150:           case KC_CC_L:                return IK_CC_L;               /* main CC */
  151:         }
  152:         return NormalKey;
  153: }
  154: 
  155: /*
  156:  * switch temporary shift or simple lock modes
  157:  *       returns TRUE if meta key status changes
  158:  */
  159: EXPORT BOOL kpChangeShiftLock( InnEvtType type, ShiftKeyKind skind )
  160: {
  161:         KpMetaBut      meta;
  162:         KpMetaBut      *stat = (KpMetaBut*)&kpMgrInfo.kpState.stat;
  163: 
  164:         meta.o = kpMgrInfo.kpState.stat;
  165: 
  166:         if ( (type & IE_BUTUP) != 0 ) type = IE_BUTUP;
  167: 
  168:         switch ( type ) {
  169:           case IE_KEYUP:       /* key up */
  170:           case IE_BUTUP:       /* PD button up */
  171:                 meta.u.tmpShift = 0;
  172:                 break;
  173: 
  174:           case IE_S_REL:       /* shift release */
  175:                 meta.u.tmpShift &= ~skind;
  176:                 meta.u.shiftLock &= ~skind;
  177:                 break;
  178: 
  179:           case IE_S_SCLK:      /* shift single click */
  180:                 if ( kpMgrInfo.kb.keyMode.tslock ) {
  181:                         /* temporary shift */
  182:                         if ( (meta.u.shiftLock & skind) != 0 ) {
  183:                                 meta.u.shiftLock &= ~skind;
  184:                                 break;
  185:                         }
  186:                         if ( (meta.u.tmpShift & skind) != 0 ) {
  187:                                 meta.u.tmpShift &= ~skind;
  188:                                 meta.u.shiftLock |= skind;
  189:                                 break;
  190:                         }
  191:                         meta.u.tmpShift |= skind;
  192:                 } else {
  193:                         if ( (meta.u.shiftLock & skind) == 0 ) {
  194:                                 meta.u.tmpShift ^= skind;
  195:                         } else {
  196:                                 meta.u.shiftLock &= ~skind;
  197:                         }
  198:                 }
  199:                 break;
  200: 
  201:           case IE_S_DCLK:      /* shift double click */
  202:                 meta.u.tmpShift &= ~skind;
  203:                 meta.u.shiftLock |= skind;
  204:                 break;
  205: 
  206:           default:
  207:                 return FALSE;
  208:         }
  209: 
  210:         if ( meta.u.tmpShift == stat->u.tmpShift
  211:           && meta.u.shiftLock == stat->u.shiftLock ) return FALSE;
  212: 
  213:         /* change meta key status */
  214:         stat->u.tmpShift = meta.u.tmpShift;
  215:         stat->u.shiftLock = meta.u.shiftLock;
  216:         stat->u.shift = meta.u.tmpShift | meta.u.shiftLock | kpMgrInfo.spress;
  217: 
  218:         return TRUE;
  219: }
  220: 
  221: /*
  222:  * key top code after addition of offset
  223:  */
  224: EXPORT W kpKeyTopCode( KeyTop keytop, UW kbsel )
  225: {
  226:         KbDef  *kbdef;
  227:         W      keytopcode, keytopofs;
  228: 
  229:         kbdef = GetKbDef(kbsel, keytop.u.kid);
  230:         if ( kbdef == NULL ) {
  231:                 /* if keytable for kbsel does not exist, then
  232:                    use keyboard definition 1 (kbsel=0) */
  233:                 kbdef = GetKbDef(0, keytop.u.kid);
  234:         }
  235:         keytopofs = ( kbdef == NULL )? 0: kbdef->keyDef.keytopofs;
  236: 
  237:         keytopcode = keytop.u.kcode + keytopofs;
  238:         if ( keytopcode >= KEYMAX ) return InvalidKeytop;
  239: 
  240:         return keytopcode;
  241: }
  242: 
  243: /*
  244:  * change keymap
  245:  */
  246: EXPORT void kpSetOrResetKeyMap( KeyTop keytop, UW kbsel, UW press )
  247: {
  248:         W      i;
  249:         UB     *idx;
  250: 
  251:         if ( (i = kpKeyTopCode(keytop, kbsel)) != InvalidKeytop ) {
  252:                 idx = &kpMgrInfo.kb.keyMap[i >> 3];
  253:                 if ( press )  *idx |=  0x80 >> (i & 7);
  254:                 else          *idx &= ~0x80 >> (i & 7);
  255:         }
  256: }
  257: 
  258: /*
  259:  * reset all keymaps
  260:  */
  261: EXPORT void kpAllResetKeyMap( void )
  262: {
  263:         MEMSET(kpMgrInfo.kb.keyMap, 0, sizeof(KeyMap));
  264: }
  265: 
  266: /* ===== PD ========================================================== */
  267: 
  268: /*
  269:  * pre-processing of PD events from real I/O drivers
  270:  *       Left-handed processing (buttons and coordinates are modified accordingly)
  271:  *       One button operation
  272:  */
  273: EXPORT void kpPdPreProcess( PdInput *msg )
  274: {
  275:         if ( kpMgrInfo.pd.pdMode.attr.reverse ) {
  276:                 /* left-handed mode */
  277:                 if ( msg->stat.xyrev && !msg->stat.abs ) {
  278:                         /* reverse coordinate */
  279:                         msg->xpos = - msg->xpos;
  280:                         msg->ypos = - msg->ypos;
  281:                 }
  282:                 if ( msg->stat.butrev ) {
  283:                         /* swap buttons */
  284:                         SWAP(UW, msg->stat.main, msg->stat.sub);
  285:                 }
  286:         }
  287: 
  288:         if ( kpMgrInfo.pd.pdMode.attr.qpress ) {
  289:                 if ( msg->stat.onebut && msg->stat.qpress ) {
  290:                         /* one button quick press */
  291:                         msg->stat.main = 1;
  292:                 }
  293:         }
  294: }
  295: 
  296: /*
  297:  * normalize pointer position (X coordinate)
  298:  */
  299: LOCAL H normalizePositionX( W xpos )
  300: {
  301: #define xmax    (kpMgrInfo.pd.pdRange.xmax)
  302: 
  303:         if ( xpos < 0 ) return 0;
  304:         if ( xpos >= xmax ) return xmax - 1;
  305:         return xpos;
  306: 
  307: #undef xmax
  308: }
  309: 
  310: /*
  311:  * normalize pointer position (Y coordinate)
  312:  */
  313: LOCAL H normalizePositionY( W ypos )
  314: {
  315: #define ymax    (kpMgrInfo.pd.pdRange.ymax)
  316: 
  317:         if ( ypos < 0 ) return 0;
  318:         if ( ypos >= ymax ) return ymax - 1;
  319:         return ypos;
  320: 
  321: #undef ymax
  322: }
  323: 
  324: /*
  325:  * move pointer
  326:  *       call after locking manager information
  327:  *       if there is a movement, TRUE is returned
  328:  */
  329: EXPORT BOOL kpMovePointer( W xpos, W ypos )
  330: {
  331:         xpos = normalizePositionX(xpos);
  332:         ypos = normalizePositionY(ypos);
  333: 
  334:         if ( kpMgrInfo.kpState.xpos == xpos
  335:           && kpMgrInfo.kpState.ypos == ypos ) return FALSE;
  336: 
  337:         /* move pointer */
  338:         kpMgrInfo.kpState.xpos = xpos;
  339:         kpMgrInfo.kpState.ypos = ypos;
  340: 
  341:         return TRUE;
  342: }
  343: 
  344: /*
  345:  * move pointer and notify event
  346:  */
  347: EXPORT ER kpMovePointerAndNotify( H xpos, H ypos )
  348: {
  349:         BOOL   move;
  350:         ER     err = E_OK;
  351: 
  352:         /* move PD */
  353:         move = kpMovePointer(xpos, ypos);
  354: 
  355:         if ( move ) {
  356:                 /* notify PD movement event */
  357:                 err = kpNotifyPdEvent(TDE_PDMOVE, 0);
  358:         }
  359: 
  360:         return err;
  361: }
  362: 
  363: /*
  364:  * move pointer
  365:  *       process internal event, and move pointer
  366:  */
  367: EXPORT TDEvtTyp kpPdMoveEvent( InnerEvent *evt )
  368: {
  369:         W              x, y;
  370:         TDEvtTyp       devEvt = 0;
  371: 
  372:         x = evt->i.pd.x;
  373:         y = evt->i.pd.y;
  374: 
  375:         if ( evt->i.pd.stat.o.abs ) {
  376:                 /* absolute coordinate device */
  377:                 PdRange *range = &kpMgrInfo.pd.pdRange;
  378: 
  379:                 x = range->xmax * x / PDIN_XMAX;
  380:                 y = range->ymax * y / PDIN_YMAX;
  381: 
  382: #if     0   /* we no longer support relative movement with absolute coordinate device. */
  383:                 if ( !kpMgrInfo.pd.pdMode.attr.absolute
  384:                   && !evt->i.pd.stat.o.norel ) {
  385:                         /* relative coordinate mode */
  386:                         if ( evt->i.pd.stat.o.vst ) {
  387:                                 /* save the origin */
  388:                                 kpMgrInfo.firstXpos = x;
  389:                                 kpMgrInfo.firstYpos = y;
  390:                         }
  391:                         /* displacement */
  392:                         x = x - kpMgrInfo.firstXpos;
  393:                         y = y - kpMgrInfo.firstYpos;
  394:                         /* move */
  395:                         kpMgrInfo.firstXpos += x;    /* move the origin */
  396:                         kpMgrInfo.firstYpos += y;
  397:                         x += kpMgrInfo.kpState.xpos; /* move PD position */
  398:                         y += kpMgrInfo.kpState.ypos;
  399:                 }
  400: #endif
  401:         } else {
  402:                 /* relative coordinate device */
  403:                 x += kpMgrInfo.kpState.xpos;
  404:                 y += kpMgrInfo.kpState.ypos;
  405:         }
  406: 
  407:         /* move PD */
  408:         if ( kpMovePointer(x, y) ) {
  409:                 devEvt = TDE_PDMOVE;
  410:         }
  411: 
  412:         return devEvt;
  413: }
  414: 
  415: /*
  416:  * change button status
  417:  *           process internal event, and change button status.
  418:  */
  419: EXPORT TDEvtTyp kpPdButEvent( InnerEvent *evt )
  420: {
  421:         if ( (evt->type & IE_MBUTDOWN) != 0 ) {        /* PD main button down */
  422:                 kpMgrInfo.kpState.stat.main = 1;
  423:         }
  424:         if ( (evt->type & IE_MBUTUP) != 0 ) {  /* PD main button up */
  425:                 kpMgrInfo.kpState.stat.main = 0;
  426:         }
  427:         if ( (evt->type & IE_SBUTDOWN) != 0 ) {        /* PD subbutton down */
  428:                 kpMgrInfo.kpState.stat.sub = 1;
  429:         }
  430:         if ( (evt->type & IE_SBUTUP) != 0 ) {  /* PD subbutton up */
  431:                 kpMgrInfo.kpState.stat.sub = 0;
  432:         }
  433: 
  434:         return TDE_PDBUT;
  435: }