Usb鼠标驱动使用LIBUSB
Usb mouse driver using LIBUSB
我想在Ubuntu上尝试"userspace"驱动程序(它不是我的代码)。我有这样的代码:
#include <stdio.h>
#include <string.h>
#include <usb.h>
#include <usbpp.h>
#define DEBUG_LEVEL 0
struct usb_bus *busses,*bus,*dbus;
int main(int argc, char *argv[])
{
struct usb_device *dev;
char *buf;
int n,x,r, ret;
char *ibus, *idev;
char string[64];
int found = 0;
usb_dev_handle *udev;
usb_set_debug(DEBUG_LEVEL);
usb_init(); //initilize the usb library
usb_find_busses();
usb_find_devices();
busses=usb_get_busses();
//use the command line arguments for Bus and Device
if (argc >= 3) {
ibus = argv[1];
idev = argv[2];
}
else { printf("Usage: usbtest <Bus> <Device> [<numBytes>])n"); exit(1); }
printf("Bus = %s Device = %sn", ibus, idev);
for (bus=busses; bus && (found == 0); bus=bus->next) { // busses loop
if (strcmp(bus->dirname, ibus) == 0) {
for (dev=bus->devices; dev; dev=dev->next) { // devices loop
if (strcmp(dev->filename, idev) == 0) { dbus=bus; found=1; break; }
}
}
}
if (found == 0) { printf("Unable to find the required device !nexitingn"); exit(1); }
printf("Found devicen");
printf("Now we are dealing with device from vendor ID : %d (%x) n",dev->descriptor.idVendor,dev->descriptor.idVendor);
printf("Trying to open the device...n");
if (udev=usb_open(dev)) printf("Device opened successfully.n");
else { printf("Operation failed :-("); exit(1);}
buf=(char*)calloc(1,100);
if (usb_get_driver_np(udev,0,buf,100)) printf("Could not read the driver name :-( %sn",buf);
else printf("Kernel Using Driver :n");
// detach the driver from the kernel , seems to be just like rmmod?
if (usb_detach_kernel_driver_np(udev,0)) printf("Error detaching the device :-(n");
else printf("Device detached successfully from the kernel.n");
// reserving the device interface for our application, if another driver/software
//is using the device , it will return 'interface busy'
if (r=usb_claim_interface(udev,0)) printf("Interface Claimed !!n");
printf("Interface Claim Status : %dn",r);
printf("Device Protocol : %dn",dev->descriptor.bDeviceProtocol);
printf("Report Length : %dn",dev->descriptor.bLength);
printf("Decriptor Type : %dn",dev->descriptor.bDescriptorType);
printf("End Points : %dn",dev->config->interface->altsetting->bNumEndpoints);
printf("Interface Class : %dn",dev->config->interface->altsetting->bInterfaceClass);
printf("Protocol : %dn",dev->config->interface->altsetting->bInterfaceProtocol);
printf("Interface Number: %dn",dev->config->interface->altsetting->bInterfaceNumber);
printf("Device Filename : %sn",dev->filename);
printf("Bus Dir Name : %sn",dbus->dirname);
usb_get_string_simple(udev,dev->descriptor.iManufacturer,string,sizeof(string));
printf("Device Manfucaturer : %sn",string);
usb_get_string_simple(udev,dev->descriptor.iProduct,string,sizeof(string));
printf("Product Name : %sn",string);
usb_get_string_simple(udev,dev->descriptor.iSerialNumber,string,sizeof(string));
printf("Device Serial Number: %sn",string);
printf("End point addresses : 0x%xn",dev->config->interface->altsetting->endpoint->bEndpointAddress);
int numBytes = 4;
int errCount = 0;
unsigned char endPoint = 0x81;
int i;
int tmp1 = 5;
int tmp2 = 5;
// reset usb device if command line numBytes = 0 and exit
if (argc == 4 && atoi(argv[3]) == 0) { usb_reset(udev); exit(0); }
// use command line value for numBytes if supplied
if (argc == 4) numBytes = atoi(argv[3]);
if (numBytes > 64) numBytes = 64;
while (1) {
for (x=0; x<numBytes; x++) string[x]=0;
// read numBytes bytes using interrupt_read,
r = usb_interrupt_read(udev, endPoint, string, numBytes, 0);
system("clear");
tmp1+=string[1];
tmp2+=string[2];
if (tmp1>=0 && tmp2>=0 && tmp1<=150 && tmp2<=50){
for (i=0;i<tmp2;i++) printf("n");
for (i=0;i<tmp1;i++) printf(".");
if (string[0]==0) printf ("X");
else if (string[0]==1) printf("L");
else if(string[0]==2) printf("R");
else if(string[0]==3) printf("LR");
else if(string[0]==4) printf("M");
}
else {
if(tmp1<0) tmp1=0;
else if (tmp1>150) tmp1=150;
if (tmp2<0) tmp2=0;
else if (tmp2>50) tmp2=50;
}
if (string[3]==1) printf ("nSCROLL UP");
if (string[3]==-1) printf ("nSCROLL DOWN");
printf ("n(%d, %d)",tmp1,tmp2);
if ( r < 0 ) errCount++;
if (errCount >= 100) break;
usb_clear_halt(udev,endPoint);
}
printf("Closing Device.n");
usb_release_interface(udev,0);
usb_close(udev);
return EXIT_SUCCESS;
}
在这段代码使用API从libusb-0.1 -但我不知道它的确切版本。可能-它是libusb-0.1.12(它有两个头"usb.h"answers"usbpp.h",在这段代码中使用)。我已经在ubuntu上下载了这个版本,使用
安装它./configure && make && make install && sudo ldconfig
之后我试着做
gcc -c mouse.c -w
来编译此代码。但它不能编译。它在"usbpp.h"(在标准中包括"string"或"list")或其他史密斯中有不同的错误。谁能告诉我,我到底做错了什么?我怎么能尝试这个代码?PS -我试着在ubuntu 9.04和ubuntu 12上测试这段代码,两者都是虚拟机。这是我大学的任务。
usbpp.h是一个c++头文件,但是你运行的GCC是一个c编译器。
我找到了另一种解决这个问题的方法:
gcc -o mouse ./mouse.cpp -lstdc++ -lusb
相关文章:
- WinAPI挂钩鼠标
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- 跟踪滚动条上的鼠标事件
- SendInput()鼠标移动计算
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 如何在鼠标挂钩过程中检测拖动
- USB传输的LibUSB C++格式不同
- Win32 发送输入鼠标移动滞后并冻结
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 有没有办法区分Qt小部件是通过鼠标单击还是通过按表键获得焦点?
- 在 macOS 中使用 C/C++ 设置鼠标光标图像
- 如何通过拖动鼠标QT检查按钮?鼠标移动事件
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- 如何在QSplashScreen中禁用鼠标单击
- C++鼠标单击时的屏幕截图不起作用
- 如何将鼠标坐标转换为"mm"
- 标签检测鼠标单击 c++
- 使用 SFML 在矩形的矢量中查找鼠标单击量
- 在 OpenGL 中使用透视与正交投影时的光线投射(鼠标拾取)
- Usb鼠标驱动使用LIBUSB