linux wireless mouse

1、下面是写的一个无线或是有线鼠标测试程序

注:有线鼠标插入linux系统中是会在/dev目录下创建一个event0文件,但是无线鼠标插上后会有两个设备文件,一个是event0,另一个是event1;

/*********************************************************************************
 *      Copyright:  (C) 2014 fulinux<fulinux@sina.com> 
 *                  All rights reserved.
 *
 *       Filename:  mice_test.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(01/23/2014~)
 *         Author:  fulinux <fulinux@sina.com>
 *      ChangeLog:  1, Release initial version on "01/23/2014 09:43:04 PM"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/input.h>

#define WIRELESS_MOUSE

#ifdef WIRELESS_MOUSE
    #define DEV_MOUSE "/dev/event1"
#else
    #define DEV_MOUSE "/dev/event0"
#endif

struct mousedev_motion{
    int dx, dy, dz;
    unsigned long buttons;
    unsigned short abs_x;
    unsigned short abs_y;
}event;

typedef struct mouse_data
{
    unsigned char key;
    signed short x;
    signed short y;                                                                                                                    
    signed short z;

    unsigned short ax;
    unsigned short ay;
}MOUSE_DATA;

/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    int fd;
    MOUSE_DATA pdata;

    if((fd = open(DEV_MOUSE, O_RDONLY)) < 0)
    {

        printf ("open mice device failed!\n");
        exit(1);
    }

    while(1)
    {
        int ret;
        ret = read(fd, &event, sizeof(struct mousedev_motion));
        if(ret < 0)
        {

            printf ("read mouse failed\n");
            exit(1);
        }
        else
        {
            pdata.key = (event.buttons & 0x7);
            pdata.x = (short)event.dx;
            pdata.y = (short)-event.dy;
            pdata.z = (short)event.dz;

            /* absolute coordinates */
            pdata.ax = event.abs_x;
            pdata.ay = event.abs_y;

            printf ("key = 0x%02x x = 0x%02x, y = 0x%02x, z = 0x%02x\n",
                    (char)pdata.key, (char)pdata.x, (char)pdata.y, (char)pdata.z);
        }
    }
    return 0;
} /* ----- End of main() ----- */

2、下面是一个makefile文件:

注:如果你的ARCH不是arn,还有CROSS_COMPILE不是下面的那个,请修改。

#*********************************************************************************
#      Copyright:  (C) 2011 China Undergraduate Embedded League(CUEL)
#                  All rights reserved.
#
#       Filename:  Makefile
#    Description:  This Makefile used to compile all the C source code file in current 
#                  folder to respective excutable binary files.
#                      
#        Version:  1.0.0(1/24/2014)
#                  Author:   fulinux<fulinux@gmail.com>
#      ChangeLog:  1, Release initial version on "1/24/2014 12:29:33 PM"
#                       
#********************************************************************************/

PWD=$(shell pwd)
INSTPATH=/mnt/nfs

#ARCH?=i386
ARCH?=arm

LINK_MODE=STATIC
MODE=PRODUCTION
DEBUG=1

#LDFLAGS+=-lpthread
CFLAGS+=-Wall -Werror

ifeq ("${MODE}", "PRODUCTION")
    CFLAGS+=-DPRODUCTION_MODE
endif
ifdef DEBUG
    CFLAGS+=-g -DDEBUG
endif

COMPILE_DATE=$(shell date -u +"%Y-%m-%d %H:%M")
VPATH= .
SRCS = $(wildcard ${VPATH}/*.c)
OBJS = $(patsubst %.c,%.o,$(SRCS))

TMP=$(shell echo $(ARCH) | tr "[A-Z]" "[a-z]")
ifneq (,$(filter i386,$(TMP)))
    CROSS_COMPILE=
else
    CROSS_COMPILE=arm-hisiv100nptl-linux-
endif

CFLAGS+=-I`dirname ${PWD}`

export CC=${CROSS_COMPILE}gcc
export CXX=${CROSS_COMPILE}gcc
export AR=${CROSS_COMPILE}ar
export AS=${CROSS_COMPILE}as
export RANLIB=${CROSS_COMPILE}ranlib
export STRIP=${CROSS_COMPILE}strip
export CFLAGS
export LDFLAGS
export ARCH                                                                                                                         
export LINK_MODE

ifeq ("${LINK_MODE}", "STATIC")
    CFLAGS+=--static
    LDFLAGS+=-static
else
    LDFLAGS+=-ldl
endif

SRCFILES = $(wildcard *.c)
BINARIES=$(SRCFILES:%.c=%)

all: entry version binaries install
entry: 
    @echo " ";
    @echo " =========================================================";
    @echo " **        Compile \"${BINARIES}\" for ${ARCH}         ";
    @echo " =========================================================";

version:
    @echo "/* Generated by makefile, don‘t Edit it by hand */" > version.h 
    @echo "#define DATE \"$(COMPILE_DATE)\"" >> version.h 
    @echo "#define MAJOR 1" >>version.h 
    @echo "#define MINOR 0" >>version.h 
    @echo "#define REVER 0" >>version.h 
    @if [ -f .svn/entries ] ; then         echo "#define SVNVER `sed -n -e 11p .svn/entries`" >>version.h;     else         echo "#define SVNVER 0" >>version.h;     fi;

binaries:  ${BINARIES}
    @echo " Compile over"

%:  %.c
    $(CC) -o $@ $< $(CFLAGS)

tag: 
    @ctags --c-kinds=+defglmnstuvx --langmap=c:.c.h.ho.hem.het.hec.hev.him.hit.hic.hiv -R .  
    @cscope -Rbq

install:
    cp $(BINARIES) ${INSTPATH}

clean: 
    @rm -f version.h 
    @rm -f *.o $(BINARIES) 
    @rm -rf *.gdb *.a *.so *.elf*

distclean: clean
    @rm -f  tags cscope*

.PHONY: clean entry

3、下面是一个测试效果:

# ./mice_test 
key = 0x01 x = 0x3d, y = 0xff, z = 0x02
key = 0x00 x = 0x3d, y = 0xf8, z = 0x00
key = 0x05 x = 0x3e, y = 0x61, z = 0x02
key = 0x00 x = 0x3e, y = 0x59, z = 0x00
key = 0x07 x = 0x3e, y = 0x2f, z = 0x02
key = 0x00 x = 0x3e, y = 0x28, z = 0x00
key = 0x04 x = 0x3e, y = 0xf1, z = 0x02
key = 0x07 x = 0x3e, y = 0xed, z = 0x02
key = 0x00 x = 0x3e, y = 0xe7, z = 0x00
key = 0x05 x = 0x3e, y = 0xb2, z = 0x02
key = 0x02 x = 0x3e, y = 0xaf, z = 0x02
key = 0x00 x = 0x3e, y = 0xa9, z = 0x00
key = 0x06 x = 0x3e, y = 0x70, z = 0x02
key = 0x02 x = 0x3e, y = 0x6c, z = 0x02




linux wireless mouse

上一篇:UDX对多网络设备支持,视频会议中单兵设备的多3G网卡支持


下一篇:参加ShineScrum CSPO认证课的个人体会