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

📄 mouhid.cpp

📁 YLP270的Windows CE5.0 bsp源码。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// 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.
//
//
//

#include "mouhid.h"
#include <windev.h>
#include "MouHidLib.h"


// Global variables
static BOOL g_fUseMouseHook = FALSE;

#ifdef DEBUG

// Debug Zones
#define DBG_ERROR              0x0001
#define DBG_WARNING            0x0002
#define DBG_INIT               0x0004
#define DBG_FUNCTION           0x0008

#define DBG_USAGES             0x0010

DBGPARAM dpCurSettings = {
        TEXT("MouHid"), {
        TEXT("Errors"), TEXT("Warnings"), TEXT("Init"), TEXT("Function"),
        TEXT("Usages"), TEXT(""), TEXT(""), TEXT(""),
        TEXT(""), TEXT(""), TEXT(""), TEXT(""),
        TEXT(""), TEXT(""), TEXT(""), TEXT("") },
        DBG_ERROR | DBG_WARNING };
        
#endif // DEBUG


// Conversions from button usages to mouse_event flags
static const DWORD g_rgdwUsageToDownButton[] = {
    0, // Undefined
    MOUSEEVENTF_LEFTDOWN,
    MOUSEEVENTF_RIGHTDOWN,
    MOUSEEVENTF_MIDDLEDOWN,
};

static const DWORD g_rgdwUsageToUpButton[] = {
    0, // Undefined
    MOUSEEVENTF_LEFTUP,
    MOUSEEVENTF_RIGHTUP,
    MOUSEEVENTF_MIDDLEUP,
};


#ifdef DEBUG

static
void
ValidateHidMouse(
    PHID_MOUSE pHidMouse
    );

#else

#define ValidateHidMouse(ptr)

#endif // DEBUG


BOOL
AllocateUsageLists(
    PHID_MOUSE pHidMouse,
    size_t cbUsages
    );

void
DetermineWheelUsage(
    PHID_MOUSE pHidMouse
    );

void
SetButtonFlags(
    PDWORD pdwFlags,
    PUSAGE pUsages,
    DWORD dwMaxUsages,
    const DWORD *pdwUsageMappings,
    DWORD cdwUsageMappings
    );

void
ProcessMouseReport(
    PHID_MOUSE pHidMouse,
    PCHAR pbHidPacket,
    DWORD cbHidPacket
    );

void
MouseEvent(
    DWORD dwFlags,
    DWORD dx,
    DWORD dy,
    DWORD dwData
    );

VOID
FreeHidMouse(
    PHID_MOUSE pHidMouse
    );


void MouseCursorOn();

void MouseCursorOff();


void MouseCursorOn()
{
    HANDLE s_hFileMap=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x30,TEXT("MMFShareData"));

    if(s_hFileMap !=NULL)
    {
        PINT lpView=(PINT)MapViewOfFile(s_hFileMap,FILE_MAP_READ | FILE_MAP_WRITE,0,0,0);

        if(lpView !=NULL)
        {
            lpView[0]=1;
            UnmapViewOfFile(lpView);
        }
        else
        {
            NKDbgPrintfW(TEXT("CursorOn Can't map view of file.!\r\n"));
        }
    }

    NKDbgPrintfW(TEXT("CursorOn Handle %x.!\r\n"),s_hFileMap);
    
    NKDbgPrintfW(TEXT("CursorOn Success.!\r\n"));
    
//    CloseHandle(s_hFileMap);
}

void MouseCursorOff()
{
    HANDLE s_hFileMap=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x30,TEXT("MMFShareData"));

    if(s_hFileMap !=NULL)
    {
        PINT lpView=(PINT)MapViewOfFile(s_hFileMap,FILE_MAP_READ | FILE_MAP_WRITE,0,0,0);

        if(lpView !=NULL)
        {
            lpView[0]=0;
            UnmapViewOfFile(lpView);
        }
        else
        {
            NKDbgPrintfW(TEXT("CursorOff Can't map view of file.!\r\n"));
        }
    }

    NKDbgPrintfW(TEXT("CursorOff Handle %x.!\r\n"),s_hFileMap);
    
    NKDbgPrintfW(TEXT("CursorOff Success.!\r\n"));
    
//    CloseHandle(s_hFileMap);
}

// Dll entry function.
extern "C" 
BOOL
DllEntry(
    HANDLE hDllHandle,
    DWORD dwReason, 
    LPVOID lpReserved
    )
{
    SETFNAME(_T("MOUHID DllEntry"));
    
    UNREFERENCED_PARAMETER(lpReserved);
    
    switch (dwReason) {
        case DLL_PROCESS_ATTACH:
            DEBUGREGISTER((HINSTANCE)hDllHandle);

            DEBUGMSG(ZONE_INIT, (_T("%s: Attach\r\n"), pszFname));
            DisableThreadLibraryCalls((HMODULE) hDllHandle);
            RETAILMSG(1,(TEXT("------------------------\r\n")));
            RETAILMSG(1,(TEXT("mouse===>>DLL_PROCESS_ATTACH r\n")));
            MouseCursorOn();
            g_fUseMouseHook = DriverMouseHookInitialize(hDllHandle);
            break;
            
        case DLL_PROCESS_DETACH:
            DEBUGMSG(ZONE_INIT, (_T("%s: Detach\r\n"), pszFname));
            break;
            
        default:
            break;
    }
    
    return TRUE ;
}


// Get interrupt reports from the device. Thread exits when the device has
// been removed.
static
DWORD
WINAPI
MouseThreadProc(
    LPVOID lpParameter
    )
{
    SETFNAME(_T("MouseThreadProc"));

    PHID_MOUSE pHidMouse = (PHID_MOUSE) lpParameter;
    
    PREFAST_DEBUGCHK(pHidMouse != NULL);

	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
    
    const DWORD cbMaxHidPacket = pHidMouse->hidpCaps.InputReportByteLength;
    PCHAR pbHidPacket = (PCHAR) LocalAlloc(LMEM_FIXED, cbMaxHidPacket);
    if (pbHidPacket == NULL) {
        DEBUGMSG(ZONE_ERROR, (TEXT("%s: LocalAlloc error:%d\r\n"), pszFname, GetLastError()));
        goto EXIT;
    }

    while (TRUE) {
        ValidateHidMouse(pHidMouse);

        DWORD cbHidPacket;
    
        // Get an interrupt report from the device.
        DWORD dwErr = pHidMouse->pHidFuncs->lpGetInterruptReport(
            pHidMouse->hDevice,
            pbHidPacket,
            cbMaxHidPacket,
            &cbHidPacket,
            NULL,
            INFINITE);

        if (dwErr != ERROR_SUCCESS) {
            if ( (dwErr != ERROR_DEVICE_REMOVED) && (dwErr != ERROR_CANCELLED) ) {
                DEBUGMSG(ZONE_ERROR, 
                    (_T("%s: GetInterruptReport returned unexpected error %u\r\n"),
                    pszFname, dwErr));
            }

            // Exit thread
            break;
        }
        else {
            DEBUGCHK(cbHidPacket <= cbMaxHidPacket);
            ProcessMouseReport(pHidMouse, pbHidPacket, cbHidPacket);
        }
    }

EXIT:
    if (pbHidPacket != NULL) LocalFree(pbHidPacket);
    DEBUGMSG(ZONE_FUNCTION, (_T("%s: Exiting thread\r\n"), pszFname));
    
    return 0;
}


// Entry point for the HID driver. Initializes the structures for this 
// keyboard and starts the thread that will receive interrupt reports.
extern "C"
BOOL
HIDDeviceAttach(
    HID_HANDLE                 hDevice, 
    PCHID_FUNCS                pHidFuncs,
    const HID_DRIVER_SETTINGS *pDriverSettings,
    PHIDP_PREPARSED_DATA       phidpPreparsedData,
    PVOID                     *ppvNotifyParameter,
    DWORD                      dwUnused
    )
{
    SETFNAME(_T("HIDDeviceAttach"));

    BOOL fRet = FALSE;
    PHID_MOUSE pHidMouse;
    size_t cbUsages;

    DEBUGCHK(hDevice != NULL);
    DEBUGCHK(pHidFuncs != NULL);
    DEBUGCHK(phidpPreparsedData != NULL);
    PREFAST_DEBUGCHK(ppvNotifyParameter != NULL);

    // Allocate this keyboard's data structure and fill it.
    pHidMouse = (PHID_MOUSE) LocalAlloc(LPTR, sizeof(HID_MOUSE));
    if (pHidMouse == NULL) {
        DEBUGMSG(ZONE_ERROR, (TEXT("%s: LocalAlloc error:%d\r\n"), pszFname, GetLastError()));
        goto EXIT;
    }

    pHidMouse->dwSig = HID_MOUSE_SIG;
    pHidMouse->hDevice = hDevice;
    pHidMouse->pHidFuncs = pHidFuncs;
    pHidMouse->phidpPreparsedData = phidpPreparsedData;
    HidP_GetCaps(pHidMouse->phidpPreparsedData, &pHidMouse->hidpCaps);

    // Get the total number of usages that can be returned in an input packet.
    // This could be zero, like if the device was just a scroll wheel.
    pHidMouse->dwMaxUsages = HidP_MaxUsageListLength(HidP_Input, 
        HID_USAGE_PAGE_BUTTON, phidpPreparsedData);
    cbUsages = pHidMouse->dwMaxUsages * sizeof(USAGE);

    if (AllocateUsageLists(pHidMouse, cbUsages) == FALSE) {
        goto EXIT;
    }

    // Do we have a mouse wheel?
    DetermineWheelUsage(pHidMouse);

    // Create the thread that will receive reports from this device
    pHidMouse->hThread = CreateThread(NULL, 0, MouseThreadProc, pHidMouse, 0, NULL);
    if (pHidMouse->hThread == NULL) {
        DEBUGMSG(ZONE_ERROR, (_T("%s: Failed creating mouse thread\r\n"), 
            pszFname));
        goto EXIT;
    }
#ifdef DEBUG
    pHidMouse->fhThreadInited = TRUE;
#endif

    *ppvNotifyParameter = pHidMouse;
    ValidateHidMouse(pHidMouse);
    fRet = TRUE;

EXIT:
    if ((fRet == FALSE) && (pHidMouse != NULL)) {
        FreeHidMouse(pHidMouse);
    }
    
    return fRet;
}
#ifdef DEBUG
// Match function with typedef.
static LPHID_CLIENT_ATTACH g_pfnDeviceAttach = HIDDeviceAttach;
#endif


// Entry point for the HID driver to give us notifications.
extern "C" 
BOOL 
WINAPI
HIDDeviceNotifications(
    DWORD  dwMsg,
    WPARAM wParam, // Message parameter
    PVOID  pvNotifyParameter
    )
{
    SETFNAME(_T("HIDDeviceNotifications"));

    BOOL fRet = FALSE;
    PHID_MOUSE pHidMouse = (PHID_MOUSE) pvNotifyParameter;
    DWORD dwFlags = 0;

    UNREFERENCED_PARAMETER(wParam);

    if (VALID_HID_MOUSE(pHidMouse) == FALSE) {
        DEBUGMSG(ZONE_ERROR, (_T("%s: Received invalid structure pointer\r\n"), pszFname));
        goto EXIT;

⌨️ 快捷键说明

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