Ignore:
Timestamp:
Dec 15, 2011, 11:24:47 AM (14 years ago)
Author:
dmik
Message:

Fix crashes in GUI apps when pressing modifier keys.

This was due the fact that BYTE is signed in the toolkit and converting it
to an unsigned int causes the sign bit to be extended in the result while
the code assumes that the extension is made with zeroes (this is how
VAC converts signed integers of smaller precisions to unsigned ones).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/gcc-kmk/src/user32/winkeyboard.cpp

    r21808 r21885  
    5555
    5656
    57 BYTE abPMScanToWinVKey[256][2] =
     57static UCHAR auchPMScanToWinVKey[256][2] =
    5858/****************************************************************************/
    5959/* PM Scancode              *    Win32 vkey                 Extended Key     */
     
    320320{
    321321  /* index is the VKey value */
    322   BYTE  bPMScanCode;
     322  UCHAR  uchPMScanCode;
    323323  LPCSTR lpstrName;
    324324} WINVKEYTOPMSCAN, *PWINVKEYTOPMSCAN;
    325325
    326 static WINVKEYTOPMSCAN abWinVKeyToPMScan[256] =
     326static WINVKEYTOPMSCAN auchWinVKeyToPMScan[256] =
    327327/**********************************************************************/
    328328/* Vkey                   *    Scancode               * Name          */
     
    592592// Note: windows uses different scancodes if numlock is pressed
    593593// This is not (yet) reflected here!
    594 static BYTE abPMScanToWinScan[256][2] =
     594static BYTE auchPMScanToWinScan[256][2] =
    595595/****************************************************************************/
    596596/* PM Scancode              *    Win32 Scancode             Extended Key     */
     
    858858//******************************************************************************
    859859
    860 VOID WIN32API KeyTranslatePMToWinBuf(BYTE *pmkey, BYTE *winkey,
     860VOID WIN32API KeyTranslatePMToWinBuf(PUCHAR pmkey, PUCHAR winkey,
    861861                                     int nrkeys)
    862862{
     
    871871
    872872   for(int i=1;i<nrkeys;i++) {
    873        if(abWinVKeyToPMScan[i].bPMScanCode) {
    874             pmvkey = OSLibWinTranslateChar(abWinVKeyToPMScan[i].bPMScanCode, TC_SCANCODETOVIRTUALKEY, 0);
     873       if(auchWinVKeyToPMScan[i].uchPMScanCode) {
     874            pmvkey = OSLibWinTranslateChar(auchWinVKeyToPMScan[i].uchPMScanCode, TC_SCANCODETOVIRTUALKEY, 0);
    875875            if(pmvkey == 0) {
    876                 dprintf2(("WinTranslateChar %x (%x) FAILED!!", i, abWinVKeyToPMScan[i].bPMScanCode));
     876                dprintf2(("WinTranslateChar %x (%x) FAILED!!", i, auchWinVKeyToPMScan[i].uchPMScanCode));
    877877            }
    878878            winkey[i] = pmkey[pmvkey];
     
    886886//******************************************************************************
    887887//******************************************************************************
    888 BYTE KeyTranslateWinVKeyToPMScan(BYTE bWinVKey, BOOL fExtended)
     888UCHAR KeyTranslateWinVKeyToPMScan(UCHAR uchWinVKey, BOOL fExtended)
    889889{
    890890  // if the extended bit didn't match, this is
    891891  // the closest hit
    892   BYTE bAlmost = 0;
     892  UCHAR uchAlmost = 0;
    893893 
    894894  // experiment
    895895
    896896#if 0
    897   bAlmost = abWinVKeyToPMScan[bWinVKey].bPMScanCode;
     897  bAlmost = auchWinVKeyToPMScan[bWinVKey].uchPMScanCode;
    898898#else
    899899  for(int i = 0;
    900       i < 256; // sizeof(row of abPMScanToWinVKey)
     900      i < 256; // sizeof(row of auchPMScanToWinVKey)
    901901      i++)
    902902  {
    903     if (abPMScanToWinVKey[i][0] == bWinVKey)
     903    if (auchPMScanToWinVKey[i][0] == uchWinVKey)
    904904    {
    905905      // this represents the PMScan code which is used as index     
    906       bAlmost = i;
     906      uchAlmost = i;
    907907     
    908908      // exact match!
    909       if (abPMScanToWinVKey[i][1] == fExtended)
     909      if (auchPMScanToWinVKey[i][1] == fExtended)
    910910        break;
    911911    }
     
    914914 
    915915  dprintf(("KeyTranslateWinVKeyToPMScan(%02xh,%d) = %02xh",
    916            bWinVKey,
     916           uchWinVKey,
    917917           fExtended,
    918            bAlmost));
     918           uchAlmost));
    919919 
    920920  // almost a match or no match at all.
    921   return bAlmost;
    922 }
    923 //******************************************************************************
    924 //******************************************************************************
    925 void KeyTranslatePMScanToWinVKey(BYTE bPMScan,
     921  return uchAlmost;
     922}
     923//******************************************************************************
     924//******************************************************************************
     925void KeyTranslatePMScanToWinVKey(UCHAR uchPMScan,
    926926                                 BOOL bNumLock,
    927                                  PBYTE pbWinVKey,
    928                                  WORD* pwWinScan,
     927                                 PUCHAR puchWinVKey,
     928                                 WORD *pwWinScan,
    929929                                 PBOOL pfExtended)
    930930{
    931931  // @@@PH numlock is currently ignored
    932   if (pbWinVKey)
    933     *pbWinVKey = abPMScanToWinVKey[bPMScan][0];
     932  if (puchWinVKey)
     933    *puchWinVKey = auchPMScanToWinVKey[uchPMScan][0];
    934934 
    935935  if (pfExtended)
    936     *pfExtended = abPMScanToWinVKey[bPMScan][1];
     936    *pfExtended = auchPMScanToWinVKey[uchPMScan][1];
    937937 
    938938  if (pwWinScan)
    939     *pwWinScan = abPMScanToWinScan[bPMScan][0];
    940 }
    941 //******************************************************************************
    942 //******************************************************************************
    943 BYTE KeyTranslateWinScanToPMScan(BYTE bWinScan, BOOL fExtended)
     939    *pwWinScan = auchPMScanToWinScan[uchPMScan][0];
     940}
     941//******************************************************************************
     942//******************************************************************************
     943UCHAR KeyTranslateWinScanToPMScan(UCHAR uchWinScan, BOOL fExtended)
    944944{
    945945  // Note:
     
    949949  // if the extended bit didn't match, this is
    950950  // the closest hit
    951   BYTE bAlmost = 0;
     951  UCHAR uchAlmost = 0;
    952952 
    953953  for(int i = 0;
    954       i < 256; // sizeof(row of abPMScanToWinVKey)
     954      i < 256; // sizeof(row of auchPMScanToWinVKey)
    955955      i++)
    956956  {
    957     if (abPMScanToWinScan[i][0] == bWinScan)
     957    if (auchPMScanToWinScan[i][0] == uchWinScan)
    958958    {
    959959      // this represents the PMScan code which is used as index     
    960       bAlmost = i;
     960      uchAlmost = i;
    961961     
    962962      // exact match!
    963       if (abPMScanToWinVKey[i][1] == fExtended)
     963      if (auchPMScanToWinVKey[i][1] == fExtended)
    964964        break;
    965965    }
     
    967967 
    968968  dprintf(("KeyTranslateWinScanToPMScan(%02xh,%d) = %02xh",
    969            bWinScan,
     969           uchWinScan,
    970970           fExtended,
    971            bAlmost));
     971           uchAlmost));
    972972 
    973973  // almost a match or no match at all.
    974   return bAlmost;
    975  
    976 }
    977 //******************************************************************************
    978 //******************************************************************************
    979 BYTE KeyTranslatePMScanToWinScan(BYTE bPMScan)
     974  return uchAlmost;
     975 
     976}
     977//******************************************************************************
     978//******************************************************************************
     979UCHAR KeyTranslatePMScanToWinScan(UCHAR bPMScan)
    980980{
    981981  // Note:
     
    983983  // O32_MapVirtualKeyA uses PM Scancodes only!
    984984 
    985   return abPMScanToWinScan[bPMScan][0];
     985  return auchPMScanToWinScan[bPMScan][0];
    986986}
    987987//******************************************************************************
     
    10261026   }
    10271027   for(int i=0;i<256;i++) {
    1028        if(abWinVKeyToPMScan[i].bPMScanCode) {
    1029            lpKeyState[i] = PMScanState[abWinVKeyToPMScan[i].bPMScanCode];
     1028       if(auchWinVKeyToPMScan[i].uchPMScanCode) {
     1029           lpKeyState[i] = PMScanState[auchWinVKeyToPMScan[i].uchPMScanCode];
    10301030       }
    10311031       if(lpKeyState[i] & 0x80) {
     
    13721372  BOOL  fDontCare = (lParam & WIN_KEY_DONTCARE) != 0;
    13731373  BOOL  fExtended = (lParam & WIN_KEY_EXTENDED) != 0,PMExtended;
    1374   UCHAR ucWinScan = (lParam & 0x00ff0000) >> 16;
    1375   UCHAR ucWinVKey;
    1376   UCHAR ucPMScan;
     1374  UCHAR uchWinScan = (lParam & 0x00ff0000) >> 16;
     1375  UCHAR uchWinVKey;
     1376  UCHAR uchPMScan;
    13771377  int   result;
    13781378 
     
    13891389  // Note: Open32 expects PM Scancodes, NOT Winscancodes.
    13901390  // Note: this is the only way of loss-less conversion!
    1391   ucPMScan = KeyTranslateWinScanToPMScan(ucWinScan, fExtended);
     1391  uchPMScan = KeyTranslateWinScanToPMScan(uchWinScan, fExtended);
    13921392  dprintf(("ucWinScan=%02xh, fExtended=%d translated to PMscan=%02xh\n",
    1393            ucWinScan,
     1393           uchWinScan,
    13941394           fExtended,
    1395            ucPMScan));
    1396   ucWinVKey = abPMScanToWinVKey[ucPMScan][0];
    1397   PMExtended = abPMScanToWinVKey[ucPMScan][1];
     1395           uchPMScan));
     1396  uchWinVKey = auchPMScanToWinVKey[uchPMScan][0];
     1397  PMExtended = auchPMScanToWinVKey[uchPMScan][1];
    13981398
    13991399  dprintf(("ucPMScan=%02xh translated to ucWinVKey=%02xh PMExtended=%d\n",
    1400            ucPMScan,
    1401            ucWinVKey,PMExtended));
     1400           uchPMScan,
     1401           uchWinVKey,PMExtended));
    14021402
    14031403  // Bug in Open32:
     
    14101410 
    14111411  LPCSTR lpstrKey;
    1412   lpstrKey = abWinVKeyToPMScan[ucWinVKey].lpstrName;
     1412  lpstrKey = auchWinVKeyToPMScan[uchWinVKey].lpstrName;
    14131413
    14141414  // handle Enter on Numeric Keypad here
    14151415  if (PMExtended)
    14161416  {
    1417    if (ucWinVKey==VK_RETURN) lpstrKey = lpstrNumEnter;
     1417   if (uchWinVKey==VK_RETURN) lpstrKey = lpstrNumEnter;
    14181418  }
    14191419
     
    14211421  {
    14221422    dprintf(("ERROR: keyname for winscan=%02xh winvkey=%02xh, fExtended=%d not found.\n",
    1423              ucWinScan, ucWinVKey, fExtended & !fDontCare));
     1423             uchWinScan, uchWinVKey, fExtended & !fDontCare));
    14241424   
    14251425    // build artificial name
     
    14281428    sprintf(szName,
    14291429            "VKey%02x%c",
    1430             ucWinScan,
     1430            uchWinScan,
    14311431            fExtended ? '+' : 0);
    14321432    memcpy(lpString, szName, nSize);
     
    15051505  //O32_GetKeyState converts windows virtual keys to PM virtual keys and there
    15061506  //are far fewer PM vkeys. (e.g. 0-9, A-Z will fail)
    1507   if(nVirtKey < 256 && abWinVKeyToPMScan[nVirtKey].bPMScanCode)
     1507  if(nVirtKey < 256 && auchWinVKeyToPMScan[nVirtKey].uchPMScanCode)
    15081508  {
    15091509      INT  nVirtKey2 = 0;
     
    15121512      if (nVirtKey == VK_CONTROL || nVirtKey == VK_LCONTROL)
    15131513      {//if AltGr is down, then pretend VK_LCONTROL is down too
    1514           result = OSLibWinGetScanState(abWinVKeyToPMScan[VK_RMENU].bPMScanCode);
     1514          result = OSLibWinGetScanState(auchWinVKeyToPMScan[VK_RMENU].uchPMScanCode);
    15151515          if(result & 0x8000) {
    15161516              return result;
     
    15211521      if ((nVirtKey >= VK_PRIOR) && (nVirtKey <= VK_DELETE))
    15221522      {
    1523          WORD numState = OSLibWinGetScanState(abWinVKeyToPMScan[VK_NUMLOCK].bPMScanCode) & 1;
     1523         WORD numState = OSLibWinGetScanState(auchWinVKeyToPMScan[VK_NUMLOCK].uchPMScanCode) & 1;
    15241524
    15251525         if(!numState) {
     
    15451545          nVirtKey2 = VK_RSHIFT;
    15461546      }
    1547       result = OSLibWinGetScanState(abWinVKeyToPMScan[nVirtKey].bPMScanCode);
     1547      result = OSLibWinGetScanState(auchWinVKeyToPMScan[nVirtKey].uchPMScanCode);
    15481548      if(nVirtKey2) {
    1549           result |= OSLibWinGetScanState(abWinVKeyToPMScan[nVirtKey2].bPMScanCode);
     1549          result |= OSLibWinGetScanState(auchWinVKeyToPMScan[nVirtKey2].uchPMScanCode);
    15501550      }
    15511551      return result;
     
    15771577  //O32_GetAsyncKeyState converts windows virtual keys to PM virtual keys and there
    15781578  //are far fewer PM vkeys. (e.g. 0-9, A-Z will fail)
    1579   if(nVirtKey < 256 && abWinVKeyToPMScan[nVirtKey].bPMScanCode)
     1579  if(nVirtKey < 256 && auchWinVKeyToPMScan[nVirtKey].uchPMScanCode)
    15801580  {
    15811581      INT  nVirtKey2 = 0;
     
    15841584      if (nVirtKey == VK_CONTROL || nVirtKey == VK_LCONTROL)
    15851585      {//if AltGr is down, then pretend VK_LCONTROL is down too
    1586           result = OSLibWinGetPhysKeyState(abWinVKeyToPMScan[VK_RMENU].bPMScanCode);
     1586          result = OSLibWinGetPhysKeyState(auchWinVKeyToPMScan[VK_RMENU].uchPMScanCode);
    15871587          if(result & 0x8000) {
    15881588              return result;
     
    15931593      if ((nVirtKey >= VK_PRIOR) && (nVirtKey <= VK_DELETE))
    15941594      {
    1595          WORD numState = OSLibWinGetScanState(abWinVKeyToPMScan[VK_NUMLOCK].bPMScanCode) & 1;
     1595         WORD numState = OSLibWinGetScanState(auchWinVKeyToPMScan[VK_NUMLOCK].uchPMScanCode) & 1;
    15961596
    15971597         if(!numState) {
     
    16181618      }
    16191619
    1620       result = OSLibWinGetPhysKeyState(abWinVKeyToPMScan[nVirtKey].bPMScanCode);
     1620      result = OSLibWinGetPhysKeyState(auchWinVKeyToPMScan[nVirtKey].uchPMScanCode);
    16211621      if(nVirtKey2) {
    1622           result |= OSLibWinGetPhysKeyState(abWinVKeyToPMScan[nVirtKey2].bPMScanCode);
     1622          result |= OSLibWinGetPhysKeyState(auchWinVKeyToPMScan[nVirtKey2].uchPMScanCode);
    16231623      }
    16241624      return result;
Note: See TracChangeset for help on using the changeset viewer.