linux上面 實現 遊戲手柄 映射 鍵盤

main.c

#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include "joy.h"
#include "vkey.h"
#include <linux/input.h>

#define MASK_UP        1
#define MASK_DOWN    2
#define MASK_LEFT    4
#define MASK_RIGHT    8

int main(void)
{
    int key,value,mask=0;
    printf("Programe by QbasicJacky!\r\n");
    if(openjoy("/dev/input/js0")<0)
    {
        printf("no gamepad!");
        return 1;
    }
    #if 1
    if(openvkey()<0)
    {
        printf("can not open vkey!\r\n");
        closejoy();
        return 2;
    }
    #endif
    printf("Hello World\r\n");
    while(1)
    {
        if(readjoy(&key,&value)==0)
        {
            printf("key=%d value=%d\r\n",key,value);
            if(key==19)    break;
            if(key==25)
            {
                if(value==-32767)
                {
                    reportvkey(EV_KEY, KEY_UP, 1);
                    mask|=MASK_UP;
                }
                else if(value==32767)
                {
                    reportvkey(EV_KEY, KEY_DOWN, 1);
                    mask|=MASK_DOWN;
                }
                else if(value==0)
                {
                    if(mask&MASK_UP)
                    {
                        reportvkey(EV_KEY, KEY_UP, 0);
                        mask&=~MASK_UP;
                    }
                    if(mask&MASK_DOWN)
                    {
                        reportvkey(EV_KEY, KEY_DOWN, 0);
                        mask&=~MASK_DOWN;
                    }
                }
                reportvkey(EV_SYN, SYN_REPORT, 0);
            }
            else if(key==24)
            {
                if(value==-32767)
                {
                    reportvkey(EV_KEY, KEY_LEFT, 1);
                    mask|=MASK_LEFT;
                }
                else if(value==32767)
                {
                    reportvkey(EV_KEY, KEY_RIGHT, 1);
                    mask|=MASK_RIGHT;
                }
                else if(value==0)
                {
                    if(mask&MASK_LEFT)
                    {
                        reportvkey(EV_KEY, KEY_LEFT, 0);
                        mask&=~MASK_LEFT;
                    }
                    if(mask&MASK_RIGHT)
                    {
                        reportvkey(EV_KEY, KEY_RIGHT, 0);
                        mask&=~MASK_RIGHT;
                    }
                }
                reportvkey(EV_SYN, SYN_REPORT, 0);
            }
            else if(key==11)    
            {
                reportvkey(EV_KEY, KEY_SPACE, value);
                reportvkey(EV_SYN, SYN_REPORT, 0);
            }
            else if(key==13)
            {
                reportvkey(EV_KEY, KEY_ESC, value);
                reportvkey(EV_SYN, SYN_REPORT, 0);
            }

        }else usleep(1000);
    }
    closevkey();        
    closejoy();
    return 0;
}

 

vkey.h  vkey.c

#ifndef __VKEY_H__
#define __VKEY_H__
int reportvkey(uint16_t type, uint16_t keycode, int32_t value);
int openvkey(void);
void closevkey(void);
#endif

#include <linux/input.h>
#include <linux/uinput.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>

static int fd;
int reportvkey(uint16_t type, uint16_t keycode, int32_t value)
{
    int ret;
    struct input_event ev;
    memset(&ev, 0, sizeof(struct input_event));
    ev.type = type;
    ev.code = keycode;
    ev.value = value;
    ret = write(fd, &ev, sizeof(struct input_event));
    if (ret < 0) {
        printf("report key error!\n");
        return ret;
    }
    return 0;
}

 

int openvkey(void)
{
    struct uinput_user_dev uidev;
    int ret,i;
    //鎵撳紑uinput璁懼
    fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
    if (fd < 0) {
        printf("error:%d\r\n",fd);
        return fd;
    }

    //娉ㄥ唽浜嬩歡
    ioctl(fd, UI_SET_EVBIT, EV_SYN);    //鍚屾浜嬩歡
    ioctl(fd, UI_SET_EVBIT, EV_KEY);    //鎸夐敭浜嬩歡

    //娉ㄥ唽鎸夐敭
    for(i = 0; i < 128; i++){
        ioctl(fd, UI_SET_KEYBIT, i);
    }

    //鍒涘緩鉶氭嫙璁懼
    memset(&uidev, 0, sizeof(struct uinput_user_dev));
    snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
    uidev.id.bustype = BUS_USB;
    uidev.id.vendor = 0x1234;    //搴旇鏄殢渚垮啓鐨?    
    uidev.id.product = 0xfedc;    //搴旇鏄殢渚垮啓鐨?    
    uidev.id.version = 1;        //搴旇鏄殢渚垮啓鐨?    
    ret = write(fd, &uidev, sizeof(struct uinput_user_dev));
    ret = ioctl(fd, UI_DEV_CREATE);
    if (ret < 0) {
        printf("create dev error:%d\r\n",ret);
        close(fd);
        return ret;
    }
    return 0;
}

void closevkey(void)
{
    //鍒犻櫎涓嬭櫄鎷熻澶?    
    ioctl(fd, UI_DEV_DESTROY);
    close(fd);
}

joy.h joy.c

#define __JOY_H__
int openjoy(const char *dev);
int readjoy(int *key,int *value);
void closejoy(void);
#endif
#include <stdio.h>  
#include <unistd.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <errno.h>  
#include <linux/input.h>  
#include <linux/joystick.h>

static int fd;
int openjoy(const char *dev)
{
    printf("open:%s\r\n",dev);
    fd = open(dev, O_RDONLY);
    if (fd < 0)    return -1;
    return fd;
}

int readjoy(int *key,int *value)
{
    int len;
    struct js_event js;
    len = read(fd, &js, sizeof(struct js_event));
    if (len < 0)    return -1;
    *key=js.type*10+js.number;
    *value=js.value;
    return 0;
}

void closejoy(void)
{
    close(fd);
}


makefile

 

cc = gcc
prom = joy2key
deps = $(shell find ./ -name "*.h")
src = $(shell find ./ -name "*.c")
obj = $(src:%.c=%.o)

$(prom): $(obj)
    @echo $(src)
    $(cc) -o $(prom) $(obj)
%.o: %.c $(deps)
    $(cc) -c $< -o $@
clean:
    rm -rf $(obj) $(prom)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章