📄 wm.c
字号:
/* * Authors: Tyler Vodak and Will Faught * CPE 454 * Project * * Name: WiiDriver.c * Description: The driver code that will manipulate the mouse on the * X windows system with coordinates sent from the Wiimote * layer. * Usage: none * * Modified: * 5/9/07 TCV and WF Original Write * *//*module header files*/#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h> #include <linux/types.h>#include <linux/miscdevice.h>#include <linux/string.h>#include <linux/input.h>#include <linux/time.h>#include <asm/bitops.h>#include <linux/slab.h>#include <asm/uaccess.h>#include "WiimoteData.h"#include "wm.h"#define WII_MODULE /*flag for making this code a module*/#ifdef WII_MODULEMODULE_AUTHOR("Tyler Vodak and Will Faught");MODULE_DESCRIPTION("Wiimote driver for CPE 454 project");MODULE_LICENSE("GPL");module_init(wiidriver_init);module_exit(wiidriver_exit);#endif/* * wiidev_set * * sets the wiimote device buttons and movement */static int wiidev_set (struct input_dev *input_wii){ input_wii->name = WII_MOD_NAME; input_wii->id.bustype = BUS_BLUETOOTH; input_wii->id.vendor = ID_VENDOR; input_wii->id.product = ID_PRODUCT; input_wii->id.version = ID_VERSION; input_wii->open = wiidev_open; input_wii->close = wiidev_close; set_bit(EV_KEY,input_wii->evbit); set_bit(EV_REL,input_wii->evbit); set_bit(REL_X,input_wii->relbit); set_bit(REL_Y,input_wii->relbit); set_bit(REL_WHEEL,input_wii->relbit); set_bit(BTN_LEFT,input_wii->keybit); set_bit(BTN_RIGHT,input_wii->keybit); set_bit(BTN_MIDDLE,input_wii->keybit); set_bit(KEY_LEFT,input_wii->keybit); set_bit(KEY_RIGHT,input_wii->keybit); return 0;}/* * wiidev_open * * @param n_dev * * used to open the wiimote device */static int wiidev_open (struct input_dev *n_dev){ return 0;}/* * wiidev_close * * @param n_dev * * used to close the wiimote device */static void wiidev_close (struct input_dev *n_dev) {}/* * wiidriver_open * @param inode - inode for the wii device * @param file - file struct for wii device * * Opens the wii device for input */static int wiidriver_open(struct inode *inode,struct file *file){ struct input_dev *input_wii; /*represents a new wiimote*/ WiimoteData *data; input_wii = input_allocate_device(); /*create new input device*/ if(!input_wii) return -ENOMEM; wiidev_set(input_wii); /*set the input device*/ if(input_register_device(input_wii)) { input_free_device(input_wii); return -ENODEV; } file->private_data = input_wii; /*save input device to wiimote*/ data = (WiimoteData*)kmalloc(sizeof(struct WiimoteData),GFP_KERNEL); input_wii->private = data; try_module_get(THIS_MODULE); /*increment use count*/ return 0;}/* * wiidriver_release * @param inode - inode for the wii device * @param file - file struct for wii device * * releases/closes the wii device */static int wiidriver_release(struct inode *inode,struct file *file){ struct input_dev *input_wii = file->private_data; kfree(input_wii->private); input_unregister_device(input_wii); module_put(THIS_MODULE); /*decrement use count*/ return 0;}/* * wiidriver_read * @param fp - file struct to wii device * @param buf - character buffer containing data * @param count - number of bytes to read * @param pos - file position offset * * reads data from wii device and returns current WiimoteData structure */static ssize_t wiidriver_read(struct file *fp,char *buf,size_t count, loff_t *pos){ struct input_dev *input_wii = (struct input_dev *)fp->private_data; WiimoteData *n_data = (WiimoteData *)input_wii->private; if(count != sizeof(WiimoteData)) return -EFAULT; if(copy_to_user(buf,n_data,count)) return -EINVAL; return count;}/* * wiidriver_write * @param fp - file struct to wii device * @param buf - character buffer containing data * @param count - number of bytes to write * @param pos - file position offset * * writes data to the wii device */static ssize_t wiidriver_write(struct file *fp,const char *buf,size_t count, loff_t *pos){ struct input_dev *input_wii = (struct input_dev *)fp->private_data; WiimoteData *n_data = (WiimoteData *)input_wii->private; if(count != sizeof(struct WiimoteData)) return -EFAULT; if(copy_from_user(n_data,buf,count)) return -EFAULT; n_data->LEDField = 0; mouse_move(input_wii,n_data->x,n_data->y); if(n_data->ButtonField & BUTTON_A) { mouse_click(input_wii,BTN_LEFT,BTN_DOWN); n_data->LEDField |= (LED_ONE | LED_TWO); } else if(!(n_data->ButtonField & BUTTON_A)) mouse_click(input_wii,BTN_LEFT,BTN_UP); if(n_data->ButtonField & BUTTON_B) { mouse_click(input_wii,BTN_RIGHT,BTN_DOWN); n_data->LEDField |= (LED_THREE | LED_FOUR); } else if(!(n_data->ButtonField & BUTTON_B)) mouse_click(input_wii,BTN_RIGHT,BTN_UP); if(n_data->ButtonField & BUTTON_HOME) { mouse_click(input_wii,BTN_MIDDLE,BTN_DOWN); n_data->LEDField |= (LED_TWO | LED_THREE); } else if(!(n_data->ButtonField & BUTTON_HOME)) mouse_click(input_wii,BTN_MIDDLE,BTN_UP); /*new wheel code*/ if(n_data->ButtonField & BUTTON_DOWN) { mouse_wheel(input_wii,-SCROLL_LEN); n_data->LEDField |= LED_TWO; } if(n_data->ButtonField & BUTTON_UP) { mouse_wheel(input_wii,SCROLL_LEN); n_data->LEDField |= LED_THREE; } if(n_data->ButtonField & BUTTON_LEFT) { mouse_click(input_wii,KEY_LEFT,BTN_DOWN); n_data->LEDField |= LED_ONE; } else if(!(n_data->ButtonField & BUTTON_LEFT)) mouse_click(input_wii,KEY_LEFT,BTN_UP); if(n_data->ButtonField & BUTTON_RIGHT) { mouse_click(input_wii,KEY_RIGHT,BTN_DOWN); n_data->LEDField |= LED_FOUR; } else if(!(n_data->ButtonField & BUTTON_RIGHT)) mouse_click(input_wii,KEY_RIGHT,BTN_UP); return 0;}/* * mouse_wheel * * @param val - value to scroll * * scrolls the mouse */static void mouse_wheel(struct input_dev *i_wii,int val){ struct input_event ev; memset(&ev,0,sizeof(struct input_event)); do_gettimeofday(&ev.time); ev.type = EV_REL; ev.code = REL_WHEEL; ev.value = val; input_event(i_wii,ev.type,ev.code,ev.value); input_sync(i_wii);}/* * mouse_move * * @param x - x coordinate of new position * @param y - y coordinate of new position * * moves the mouse to new position */static void mouse_move(struct input_dev *i_wii,int x,int y){ struct input_event ev; memset(&ev,0,sizeof(struct input_event)); do_gettimeofday(&ev.time); if(Y_AXIS_INV) y = -y; ev.type = EV_REL; ev.code = REL_X; ev.value = x; input_event(i_wii,ev.type,ev.code,ev.value); ev.type = EV_REL; ev.code = REL_Y; ev.value = y; input_event(i_wii,ev.type,ev.code,ev.value); input_sync(i_wii);}/* * mouse_click * * takes care of a mouse click */static void mouse_click(struct input_dev *i_wii,unsigned int button,int value){ struct input_event ev; memset(&ev,0,sizeof(struct input_event)); do_gettimeofday(&ev.time); ev.type = EV_KEY; ev.code = button; ev.value = value; input_event(i_wii,ev.type,ev.code,ev.value); input_sync(i_wii);}/* * wiidriver_init * * initializes the wiimote driver module */static int __init wiidriver_init(void){ return misc_register(&wii_dev);}/* * * removes the wiimote driver module */static void __exit wiidriver_exit(void){ misc_deregister(&wii_dev);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -