// vmoudev.cpp - virtual mouse device for HID example
//=============================================================================
//
// Compuware Corporation
// NuMega Lab
// 9 Townsend West
// Nashua, NH 03060 USA
//
// Copyright (c) 1998 Compuware Corporation. All Rights Reserved.
// Unpublished - rights reserved under the Copyright laws of the
// United States.
//
//=============================================================================
// This module implements the device class of the virtual HID
// mouse minidriver.
#include <khid.h>
#include "vmoudev.h"
#include "hidmouse.h"
KTrace T("",TRACE_MONITOR, TraceAlways, BreakNever, KUstring(L"HidMouse"));
#define SCALEX 3
#define SCALEY 3
// The HID report descriptor for this device
// (taken from USB/HID specification)
HID_REPORT_DESCRIPTOR MouseHidReportDesc[] = {
0x05, 0x01, // Usage Page (Generic Desktop),
0x09, 0x02, // Usage (Mouse),
0xA1, 0x01, // Collection (Application),
0x09, 0x01, // Usage (Pointer),
0xA1, 0x00, // Collection (Physical),
0x05, 0x09, // Usage Page (Buttons),
0x19, 0x01, // Usage Minimum (01),
0x29, 0x03, // Usage Maximun (03),
0x15, 0x00, // Logical Minimum (0),
0x25, 0x01, // Logical Maximum (1),
0x95, 0x03, // Report Count (3),
0x75, 0x01, // Report Size (1),
0x81, 0x02, // Input (Data, Variable, Absolute), ;3 button bits
0x95, 0x01, // Report Count (1),
0x75, 0x05, // Report Size (5),
0x81, 0x01, // Input (Constant), ;5 bit padding
0x05, 0x01, // Usage Page (Generic Desktop),
0x09, 0x30, // Usage (X),
0x09, 0x31, // Usage (Y),
0x15, 0x81, // Logical Minimum (-127),
0x25, 0x7F, // Logical Maximum (127),
0x75, 0x08, // Report Size (8),
0x95, 0x02, // Report Count (2),
0x81, 0x06, // Input (Data, Variable, Relative), ;2 position bytes (X & Y)
0xC0, // End Collection,
0xC0 // End Collection
};
// HardwareID for the virtual mouse.
WCHAR HardwareID[]={L"ROOT\\NUMEGA_VIRTUAL_HID_MOUSE\0"};
WCHAR DeviceID[] ={L"ROOT\\NUMEGA_VIRTUAL_HID_MOUSE\0"};
HID_DEVICE_ATTRIBUTES DeviceAttributes = {
sizeof(HID_DEVICE_ATTRIBUTES),
MY_VENDOR_ID,
MY_PRODUCT_ID,
VERSION_NUMBER
};
// Device String
struct
{
HID_STRING_DESCRIPTOR Sd1;
WCHAR Str1[14];
HID_STRING_DESCRIPTOR Sd2;
WCHAR Str2[9];
} TheDeviceString = {
{ 30, 3}, {'V','i','r','e','o',' ','S','o','f','t','w','a','r','e'},
{ 20, 3}, {'H','i','d',' ','M','o','u','s','e'}
};
// We keep a pointer to an instance of the device class for use
// by the exported functions which are called from other drivers
VirtualHidMouse* DeviceInstance=NULL;
// Constructor for the virtual mouse device. It is derived from
// KHidDevice.
VirtualHidMouse::VirtualHidMouse(PDEVICE_OBJECT Fdo) :
KHidDevice(
Fdo,
MouseHidReportDesc,
sizeof MouseHidReportDesc,
DeviceID,
HardwareID,
NULL,
NULL,
&DeviceAttributes,
&TheDeviceString.Sd1,
sizeof TheDeviceString,
0
)
{
DeviceInstance = this;
m_DeltaX = m_DeltaY = 0;
m_OldButtonState = m_NewButtonState = 0;
// Set up the PDO connection
m_Pdo.Initialize(PDO(), TopOfStack());
SetLowerDevice(&m_Pdo);
// Set standard policies
SetPnpPolicy();
// Customize the policy for canceling the current IRP
m_Policies.m_QueryRemovePolicy.m_CancelCurrentIrp = TRUE;
// Set up the VxD interface
m_Vxd.Initialize("VHIDMSE", VxdControlMessageHandler, this);
}
// HIDCLASS calls this routine when it wants a report. We
// serialize this IRP so that when we get input, we know
// which IRP it is intended for. Note that HIDCLASS actually
// created the device object, so we're assuming that HIDCLASS
// is not using the device queue.
NTSTATUS VirtualHidMouse::ReadReport(KIrp I)
{
return QueueIrp(I, LinkTo(CancelQueuedIrp)); // queue to device queue.
// Note: A driver that relies on a bus driver or some other
// lower level device to process the IRP should generally not queue
// the IRP. Queueing is primarily used by lowest level drivers.
// If this rule is not followed, then the lower device's dispatch
// routines will be called at raised IRQL, and this is generally
// not good.
}
VOID VirtualHidMouse::CancelQueuedIrp(KIrp I)
{
KDeviceQueue dq(DeviceQueue());
if ( (PIRP)I == CurrentIrp() )
{
CurrentIrp() = NULL;
CancelSpinLock::Release(I.CancelIrql());
T << "Read IRP canceled " << I << "\n";
I.Information() = 0;
I.Status() = STATUS_CANCELLED;
PnpNextIrp(I);
}
else if (dq.RemoveSpecificEntry(I))
{
CancelSpinLock::Release(I.CancelIrql());
T << "Read IRP canceled " << I << "\n";
I.Information() = 0;
I.PnpComplete(this, STATUS_CANCELLED);
}
else
{
CancelSpinLock::Release(I.CancelIrql());
}
}
// StartIo
VOID VirtualHidMouse::StartIo(KIrp I)
{
ASSERT (I.MajorFunction() == IRP_MJ_INTERNAL_DEVICE_CONTROL);
ASSERT (I.IoctlCode() == IOCTL_HID_READ_REPORT);
// See if there is any input to report
UpdateState();
}
// UpdateState
//
// Completes the current IRP if there is anything to report
//
VOID VirtualHidMouse::UpdateState(void)
{
KIrp I=CurrentIrp();
if ( !I.IsNull() )
{
m_Lock.Lock();
if ( (m_DeltaX != 0) ||
(m_DeltaY != 0) ||
(m_NewButtonState != m_OldButtonState)
)
{
// Before processing the Irp, we need to check to see if it has been
// canceled. We also want to set the Irp into an non-cancelable
// state (cancel routine set to NULL) so we can process it. When
// performing these operations, it is necessary to hold the global
// cancel spin lock and take special precautions to ensure the Irp
// is still valid. This is accomplished using the routine
// KIrp::TestAndSetCancelRoutine().
if ( !I.TestAndSetCancelRoutine(
LinkTo(CancelQueuedIrp),
NULL,
CurrentIrp()) )
{
// The Irp has been canceled we stop processing and exit. Since
// it was in a cancelable state previously, it will be completed by
// the cancel routine that had been set on it.
return;
}
MouseReport* pReport = (MouseReport*)I.UserBuffer();
pReport->buttons = m_NewButtonState;
pReport->deltaX = m_DeltaX;
pReport->deltaY = m_DeltaY;
m_DeltaX = m_DeltaY = 0;
m_OldButtonState = m_NewButtonState;
I.Information() = sizeof(MouseReport);
I.Status() = STATUS_SUCCESS;
m_Lock.Unlock();
PnpNextIrp(I);
}
else
m_Lock.Unlock();
}
}
// Called indirectly by other drivers when the virtual mouse
// moves.
VOID VirtualHidMouse::Move(CHAR DeltaX, CHAR DeltaY)
{
m_Lock.Lock();
m_DeltaX += DeltaX*SCALEX;
m_DeltaY += DeltaY*SCALEY;
m_Lock.Unlock();
UpdateState();
}
// Called indirectly by other drivers when a virtual mouse
// button is clicked.
//
// Input:
// LeftOrRight: 1=Left, 0=Right
// DownOrUp: 1=Down, 0=Up
//
VOID VirtualHidMouse::Click(ULONG LeftOrRight, ULONG DownOrUp)
{
m_Lock.Lock();
if (DownOrUp != 0) // down
{
if (LeftOrRight != 0) // left
m_NewButtonState = (m_OldButtonState | LEFT_BUTTON);
else // right
m_NewButtonState = (m_OldButtonState | RIGHT_BUTTON);
}
else // up
{
if (LeftOrRight != 0) // left
m_NewButtonState = (m_OldButtonState & ~LEFT_BUTTON);
else // right
m_NewButtonState = (m_OldButtonState & ~RIGHT_BUTTON);
}
m_Lock.Unlock();
UpdateState();
}
NTSTATUS VirtualHidMouse::DefaultHidRequestHandler(KIrp I)
{
T << "Unhandled HID request\n";
I.ForceReuseOfCurrentStackLocationInCalldown();
return m_Pdo.PnpCall(this, I);
}
//////////////////////////////////////////////////////////////////
// VirtualHidMouse::DefaultPnp
//
// This handler is the override of KPnpDdevice::Default. It provides
// default functionality for IRP_MJ_PNP. The
// system invokes this function whenever the d
鼠标驱动源码鼠标驱动源码
4星 · 超过85%的资源 需积分: 44 138 浏览量
2011-08-02
10:24:08
上传
评论 1
收藏 14KB RAR 举报
rufanchen_
- 粉丝: 30
- 资源: 1
最新资源
- Unity适用于Windows与移动平台双端的轮盘交互式提示菜单
- UE5/UE4超详细教程接入科大讯飞语音唤醒SDK并初始持久监听(10102错误码解决)
- 靠搬运古装剧解说视频,日收益200+,作品原创度90%以上,超详细教程-教程网盘链接提取码下载 .txt
- 2024最强数学建模之美赛攻略.md
- STM32单片机FPGA毕设电路原理论文报告一种机器人寻线控制系统
- STM32单片机FPGA毕设电路原理论文报告一种光电编码器位置检测系统研究与应用
- linux常用命令大全
- ACM题库,分类整理.md
- STM32单片机FPGA毕设电路原理论文报告一种高速折刀运动控制系统的设计与实现
- ZYWIFI0939C循迹、红外避障、遥控综合程序.plg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈