⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jst.cpp

📁 ce5下测试游戏手柄/摇杆的驱动程序和测试代码以及一个笔记. 硬件是S3C2440. 对写CE的USB HID驱动的同志有帮助.
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//
//由CE下的键盘驱动修改: coke

#define ttt_ KBD_
#include "jsthid.h"
#include <devload.h>

static
PHID_KBD
GetContextFromReg(
    LPCTSTR  pszActivePath
    )
{
	RETAILMSG( 1, (TEXT(">GetContextFromReg--joystick\r\n")));

    PHID_KBD pHidKbd = NULL;
    HKEY hKey;
    LONG lStatus;

    if (pszActivePath != NULL) 
    {
        //
        // open the registry and read out our context pointer
        // since Dev Mgr doesn't pass it in.
        //
        lStatus = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                                pszActivePath,
                                0,
                                0,
                                &hKey);

        if (lStatus == ERROR_SUCCESS) 
        {
            DWORD dwVal;
            DWORD dwType = DEVLOAD_CLIENTINFO_VALTYPE;
            DWORD dwValLen = sizeof(dwVal);
            lStatus = RegQueryValueEx( hKey,
                                       DEVLOAD_CLIENTINFO_VALNAME,
                                       NULL,
                                       &dwType,
                                       (LPBYTE)(&dwVal),
                                       &dwValLen);

            if ((lStatus == ERROR_SUCCESS) && (dwType == REG_DWORD))
            {
                // check the signature
                pHidKbd = (PHID_KBD) dwVal;
                if (HID_KBD_SIG != pHidKbd->dwSig ) {
                    DEBUGMSG(ZONE_ERROR, (_T("%s: Invalid signature!!\r\n"), pszFname));
                    DEBUGCHK(FALSE);
                    pHidKbd = NULL;
                } else {
                    DEBUGMSG(ZONE_INIT, (_T("%s: ActivePath: %s\r\n"), pszFname, pszActivePath));
                }
            }

            RegCloseKey(hKey);

        }
        else {
            //DEBUGMSG(ZONE_ERROR, (_T("%s: Open ActivePath failed\n"), pszFname));
			RETAILMSG( 1, (TEXT(">Open ActivePath failed--joystick\r\n")));
        }
    }

   return pHidKbd;
}



//extern "C" 
DWORD
JST_Init (
    DWORD dwCtx
    )
{
    RETAILMSG( 1, (TEXT(">JST_Init--joystick\r\n")));

    LPCTSTR pszActivePath = (LPCTSTR) dwCtx; // HKLM\Drivers\Active\xx
    PHID_KBD pHidKbd = NULL;
    DWORD dwRet = 0;

    DEBUGCHK(pszActivePath != NULL);

    DEBUGMSG(ZONE_FUNCTION, (_T("+%s"), pszFname));

    pHidKbd = GetContextFromReg(pszActivePath);

    if (VALID_HID_KBD(pHidKbd) == TRUE) {
        ValidateHidKbd(pHidKbd);
        dwRet = (DWORD) pHidKbd;
    }
    else {
		RETAILMSG( 1, (TEXT(">ActivateDevice with a bad value--joystick\r\n")));
        DEBUGCHK(FALSE); // Who called ActivateDevice with a bad value?
    }

    DEBUGMSG(ZONE_FUNCTION, (_T("-%s"), pszFname));
    
    return dwRet;
}


extern "C" 
BOOL
JST_PreDeinit (DWORD dwCtx)
{
	RETAILMSG( 1, (TEXT(">JKBD_PreDeinit--joystick\r\n")));

    // This function is necessary so that KBD_Deinit is called only 
    // after no threads are in KBD_IOControl.

    DEBUGMSG(ZONE_FUNCTION, (_T("+%s"), pszFname));
    DEBUGMSG(ZONE_FUNCTION, (_T("-%s"), pszFname));

    return TRUE;
}


//extern "C" 
BOOL
JST_Deinit (DWORD dwCtx)
{
    RETAILMSG( 1, (TEXT(">JST_Deinit--joystick\r\n")));

    BOOL fRet = TRUE;

    PHID_KBD pHidKbd = (PHID_KBD) dwCtx;

    DEBUGMSG(ZONE_FUNCTION, (_T("+%s"), pszFname));

    if (VALID_HID_KBD(pHidKbd)) {
        SetEvent(pHidKbd->hevClosing);
    }
    else {
        fRet = FALSE;
    }

    DEBUGMSG(ZONE_FUNCTION, (_T("-%s"), pszFname));

    return fRet;
}


//extern "C" 
DWORD
JST_Open (DWORD dwCtx, DWORD dwAccMode, DWORD dwShrMode)
{
    SETFNAME(_T("KBD_Open"));
    RETAILMSG( 1, (TEXT(">JST_Open--joystick\r\n")));

    PHID_KBD pHidKbd = (PHID_KBD) dwCtx;    
    DWORD dwRet = dwCtx;

    DEBUGMSG(ZONE_FUNCTION, (_T("+%s"), pszFname));

    if (VALID_HID_KBD(pHidKbd) == FALSE) {
        DEBUGMSG(ZONE_ERROR, (_T("%s: ERROR_FILE_NOT_FOUND\r\n"), pszFname));
        SetLastError(ERROR_FILE_NOT_FOUND);
        dwRet = 0;
		RETAILMSG( 1, (TEXT(">ERROR_FILE_NOT_FOUND--joystick\r\n")));
    }
    
    DEBUGMSG(ZONE_FUNCTION, (_T("-%s"), pszFname));
    return dwRet;
}

extern "C" 
BOOL
JST_Close (DWORD dwCtx)
{
	RETAILMSG( 1, (TEXT(">KBD_Close--joystick\r\n")));

    PHID_KBD pHidKbd = (PHID_KBD) dwCtx;    
    BOOL fRet = FALSE;

    DEBUGMSG(ZONE_FUNCTION, (_T("+%s"), pszFname));

    if (VALID_HID_KBD(pHidKbd) == FALSE) {
        DEBUGMSG(ZONE_ERROR, (_T("%s: ERROR_INVALID_HANDLE\r\n"), pszFname));
        SetLastError(ERROR_INVALID_HANDLE);
		RETAILMSG( 1, (TEXT(">ERROR_INVALID_HANDLE--joystick\r\n")));
        goto EXIT;
    }

    fRet = TRUE;

EXIT:
    DEBUGMSG(ZONE_FUNCTION, (_T("-%s"), pszFname));
    return fRet;
}

DWORD
JST_Write(
   //PUSBPRN_CONTEXT pUsbPrn,
   DWORD dwCtx,
   PUCHAR pBuffer,
   ULONG  BufferLength
   )
{
 //   RETAILMSG( 1, (TEXT(">JST_Write--joystick\r\n")));

    PHID_KBD pHidKbd = NULL;
    DWORD dwRet = 0;
	pHidKbd = (PHID_KBD)dwCtx;

    PREFAST_DEBUGCHK(pHidKbd != NULL);
    DEBUGCHK(pHidKbd->hevClosing != NULL); // Needed to signal exit

	BufferLength = (pHidKbd->cbOutputBuffer > BufferLength)? BufferLength : pHidKbd->cbOutputBuffer;

    // Now send the report.
    DWORD dwErr = pHidKbd->pHidFuncs->lpSetReport(pHidKbd->hDevice, HidP_Output, 
        (char*)pBuffer, BufferLength, INFINITE);

	if(dwErr!=ERROR_SUCCESS)
		RETAILMSG( 1, (TEXT(">JST_Write fail--joystick\r\n")));

	return 0;
}

DWORD
JST_Read(
   DWORD dwCtx,
   PUCHAR pBuffer,
   DWORD  BufferLength
   )
{
 //   RETAILMSG( 1, (TEXT(">JST_Read--joystick\r\n")));

    PHID_KBD pHidKbd = NULL;
    DWORD dwRet = 0;
	pHidKbd = (PHID_KBD)dwCtx;

	DWORD i=(BufferLength > pHidKbd->hidpCaps.InputReportByteLength)? pHidKbd->hidpCaps.InputReportByteLength : BufferLength;
	BufferLength = i;
	for(DWORD j=0; j<i; j++)
	{
		pBuffer[j]=pHidKbd->pbInputBuffer[j+1];
	}
    return 0;
}

//extern "C" 
BOOL
JST_IOControl(
    PHID_KBD pHidKbd,
    DWORD dwCode,
    PBYTE pBufIn,
    DWORD dwLenIn,
    PBYTE pBufOut,
    DWORD dwLenOut,
    PDWORD pdwActualOut
    )
{
    RETAILMSG( 1, (TEXT(">JST_IOControl--joystick\r\n")));
    
    DWORD dwErr = ERROR_SUCCESS;

    DEBUGMSG(ZONE_FUNCTION, (_T("+%s"), pszFname));

    if (VALID_HID_KBD(pHidKbd) == FALSE) {
        //DEBUGMSG(ZONE_ERROR, (_T("%s: ERROR_INVALID_HANDLE\r\n"), pszFname));
		RETAILMSG( 1, (TEXT(">ERROR_INVALID_HANDLE--joystick\r\n")));
        dwErr = ERROR_INVALID_HANDLE;
        goto EXIT;
    }

    switch(dwCode) {
    case IOCTL_KBD_SET_MODIFIERS: {
        RETAILMSG(1, (_T("IOCTL_HID_SET_MODIFIERS\r\n")));

        if ( (dwLenIn != sizeof(KEY_STATE_FLAGS)) || (pBufIn == NULL) ) {
            RETAILMSG(1, (_T("%s: ERROR_INVALID_PARAMETER\r\n")));
            dwErr = ERROR_INVALID_PARAMETER;
        }
        else {
            // Only take the values of the following flags, since we keep
            // our own per-keyboard shift, ctrl, and alt states.
            DWORD dwFlags = KeyShiftCapitalFlag | KeyShiftNumLockFlag |  KeyShiftScrollLockFlag;
            pHidKbd->KeyStateFlags &= ~dwFlags;
            pHidKbd->KeyStateFlags |= (*((KEY_STATE_FLAGS*) pBufIn) & dwFlags);
            SetLEDs(pHidKbd, pHidKbd->KeyStateFlags);
            DEBUGMSG(ZONE_FUNCTION, (_T("%s: New modifier set = 0x%08x\r\n"), 
                pszFname, pHidKbd->KeyStateFlags));
        }
        break;
    }

    case IOCTL_KBD_SET_AUTOREPEAT: {
        DEBUGMSG(ZONE_FUNCTION, (_T("%s: IOCTL_HID_SET_AUTOREPEAT\r\n"), 
            pszFname));

        if ( (dwLenIn != sizeof(KBDI_AUTOREPEAT_INFO)) ||
             (pBufIn == NULL) ) {
            DEBUGMSG(ZONE_ERROR, (_T("%s: ERROR_INVALID_PARAMETER\r\n"),
                pszFname));
            dwErr = ERROR_INVALID_PARAMETER;
        }
        else {
            // These global writes are not protected since the Layout Manager
            // serializes these IOCTLs.
            KBDI_AUTOREPEAT_INFO *pAutoRepeat = (KBDI_AUTOREPEAT_INFO*) pBufIn;
            
            INT32 iInitialDelay = pAutoRepeat->CurrentInitialDelay;
            INT32 iRepeatRate = pAutoRepeat->CurrentRepeatRate;
            
            iInitialDelay = max(KBD_AUTO_REPEAT_INITIAL_DELAY_MIN, iInitialDelay);
            iInitialDelay = min(KBD_AUTO_REPEAT_INITIAL_DELAY_MAX, iInitialDelay);
          
            if (iRepeatRate) { // Do not alter 0
                iRepeatRate = max(KBD_AUTO_REPEAT_KEYS_PER_SEC_MIN, iRepeatRate);
                iRepeatRate = min(KBD_AUTO_REPEAT_KEYS_PER_SEC_MAX, iRepeatRate);
            }
            
            g_dwAutoRepeatInitialDelay = iInitialDelay;
            g_dwAutoRepeatKeysPerSec   = iRepeatRate;

            DEBUGMSG(ZONE_FUNCTION, (_T("%s: AutoRepeat intial delay = %u\r\n"), 
                pszFname, iInitialDelay));
            DEBUGMSG(ZONE_FUNCTION, (_T("%s: AutoRepeat keys/sec = %u\r\n"), 
                pszFname, iRepeatRate));
        }
        break;
    }

    case IOCTL_KBD_SET_LOCALE_FLAGS: {
        DEBUGMSG(ZONE_FUNCTION, (_T("%s: IOCTL_HID_SET_LOCALE_FLAGS\r\n"), 
            pszFname));

        if ( (dwLenIn != sizeof(DWORD)) || (pBufIn == NULL) ) {
            DEBUGMSG(ZONE_ERROR, (_T("%s: ERROR_INVALID_PARAMETER\r\n"),
                pszFname));
            dwErr = ERROR_INVALID_PARAMETER;
        }
        else {
            PDWORD pdwLocaleFlags = (PDWORD) pBufIn;
            g_dwLocaleFlags = *pdwLocaleFlags;
            DEBUGMSG(ZONE_FUNCTION, (_T("%s: Local flags = 0x%02x\r\n"), 
                pszFname, *pdwLocaleFlags));
        }
        break;
    }

    default:
        RETAILMSG(1, (_T("(0x%x) ERROR_NOT_SUPPORTED\r\n"), 
            dwCode));
        dwErr = ERROR_NOT_SUPPORTED;
    }

EXIT:
    if (dwErr != ERROR_SUCCESS) {
        SetLastError(dwErr);
    }
    return (dwErr == ERROR_SUCCESS);
}


// Set the keyboard's LEDs.
BOOL
SetLEDs(
    PHID_KBD pHidKbd,
    KEY_STATE_FLAGS KeyStateFlags
    )
{
    RETAILMSG( 1, (TEXT(">SetLEDs--joystick\r\n")));

    struct SHIFT_FLAG_TO_USAGE {
        DWORD dwShiftFlag;
        USAGE usage;
    };

    static const SHIFT_FLAG_TO_USAGE rgShiftToUsage[] = {
        { KeyShiftCapitalFlag, HID_USAGE_LED_CAPS_LOCK },
        { KeyShiftNumLockFlag, HID_USAGE_LED_NUM_LOCK },
        { KeyShiftScrollLockFlag, HID_USAGE_LED_SCROLL_LOCK },
    };

    USAGE rgUsages[dim(rgShiftToUsage)];
    DWORD cUsages = 0;
    DWORD dwShift;
    DWORD dwErr;
    BOOL fRet;

    NTSTATUS (__stdcall *lpSetUnsetUsages) (
       IN       HIDP_REPORT_TYPE      ReportType,
       IN       USAGE                 UsagePage,
       IN       USHORT                LinkCollection,
       IN       PUSAGE                UsageList,
       IN OUT   PULONG                UsageLength,
       IN       PHIDP_PREPARSED_DATA  PreparsedData,
       IN OUT   PCHAR                 Report,
       IN       ULONG                 ReportLength
       );    

    PREFAST_DEBUGCHK(pHidKbd != NULL);
    DEBUGCHK(pHidKbd->pbOutputBuffer != NULL);

    if (pHidKbd->cbOutputBuffer == 0) {
        // This device does not support output reports.
        return TRUE;
    }

    for (dwShift = 0; dwShift < dim(rgShiftToUsage); ++dwShift) 
    {
        const SHIFT_FLAG_TO_USAGE *pShiftToUsage = &rgShiftToUsage[dwShift];
        
        if ((pShiftToUsage->dwShiftFlag & KeyStateFlags) != 0) {
            rgUsages[cUsages++] = pShiftToUsage->usage;
        }
    }
    
    // Clear the output report
    DEBUGCHK(pHidKbd->cbOutputBuffer != 0);
    ZeroMemory(pHidKbd->pbOutputBuffer, pHidKbd->cbOutputBuffer);

    // Set up the report.
    if (cUsages == 0) {
        // Set up the report for the proper report ID.
        // In order to do this, we unset a usage.
        rgUsages[0] = HID_USAGE_LED_CAPS_LOCK;
        cUsages = 1;
        lpSetUnsetUsages = HidP_UnsetUsages;
    }
    else {
        lpSetUnsetUsages = HidP_SetUsages;
    }

    (*lpSetUnsetUsages)(
        HidP_Output,
        HID_USAGE_PAGE_LED,
        0,
        rgUsages,
        &cUsages,
        pHidKbd->phidpPreparsedData,
        pHidKbd->pbOutputBuffer,
        pHidKbd->cbOutputBuffer
        );

    // Now send the report.
    dwErr = pHidKbd->pHidFuncs->lpSetReport(pHidKbd->hDevice, HidP_Output, 
        pHidKbd->pbOutputBuffer, pHidKbd->cbOutputBuffer, INFINITE);
    fRet = (dwErr == ERROR_SUCCESS);

    return fRet;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -