mouse 代码

来源:互联网 发布:软件开发企业认证 编辑:程序博客网 时间:2024/05/21 06:22
mouse.c 代码 
/* *  2010.2.25 *  mouse.c */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <fcntl.h>#include <unistd.h>#include "common.h"/* ********** .h */typedef struct {int x;int y;int button;}mouse_event_t;extern int mouse_open(char *device_name, int *fd);extern int mouse_parse(const u8_t *buf, mouse_event_t* mevent);extern int fb_restorecursor(fb_info fb_inf, int x, int y);extern int fb_drawcursor(fb_info fb_inf, int x, int y);extern int test_mouse(fb_info fb_inf);/* **************** end .h */#define C_WIDTH  10#define C_HEIGHT 17#define T___     0XFFFFFFFF#define BORD     0x0#define X___     0xFFFFstatic unsigned long cursor_pixel[C_WIDTH * C_HEIGHT] = {  BORD, T___, T___, T___, T___, T___, T___, T___, T___, T___,  BORD, BORD, T___, T___, T___, T___, T___, T___, T___, T___,  BORD, X___, BORD, T___, T___, T___, T___, T___, T___, T___,  BORD, X___, X___, BORD, T___, T___, T___, T___, T___, T___,  BORD, X___, X___, X___, BORD, T___, T___, T___, T___, T___,  BORD, X___, X___, X___, X___, BORD, T___, T___, T___, T___,  BORD, X___, X___, X___, X___, X___, BORD, T___, T___, T___,  BORD, X___, X___, X___, X___, X___, X___, BORD, T___, T___,  BORD, X___, X___, X___, X___, X___, X___, X___, BORD, T___,  BORD, X___, X___, X___, X___, X___, X___, X___, X___, BORD,  BORD, X___, X___, X___, X___, X___, BORD, BORD, BORD, BORD,  BORD, X___, X___, BORD, X___, X___, BORD, T___, T___, T___,  BORD, X___, BORD, T___, BORD, X___, X___, BORD, T___, T___,  BORD, BORD, T___, T___, BORD, X___, X___, BORD, T___, T___,  T___, T___, T___, T___, T___, BORD, X___, X___, BORD, T___,  T___, T___, T___, T___, T___, BORD, X___, X___, BORD, T___,  T___, T___, T___, T___, T___, T___, BORD, BORD, T___, T___};static unsigned long save_cursor[C_WIDTH * C_HEIGHT];int test_mouse(fb_info fb_inf){int mfd;mouse_open(NULL, &mfd);int m_x = fb_inf.w / 2;int m_y = fb_inf.h / 2;fb_drawcursor(fb_inf, m_x, m_y);u8_t buf[8];mouse_event_t mevent;int i;for(i = 0; i < 1000; ++i){int n = read(mfd, buf, 8);if (n != -1){mouse_parse(buf,&mevent);printf("dx:%d\tdy:%d\n", mevent.x, mevent.y);printf("mx:%d\tmy:%d\n", m_x, m_y);fb_restorecursor(fb_inf, m_x, m_y);m_x += mevent.x;m_y += mevent.y;fb_drawcursor(fb_inf, m_x, m_y);switch (mevent.button){case 1:printf("left\n");break;case 2:printf("right\n");break;case 3:printf("middle\n");break;default:break;}}sleep(1);}return 0;}int mouse_open(char *device_name, int *fd){if (NULL == device_name)device_name = "/dev/input/mice";*fd = open(device_name, O_RDONLY | O_NONBLOCK);if(*fd < 0){fprintf(stderr, "Open %s:%s\n", device_name, strerror(errno));exit(1);}return 0;}int mouse_parse(const u8_t *buf, mouse_event_t* mevent){switch(buf[0] & 0x7){case 1:/* left */mevent->button = 1;break;case 2:mevent->button = 2;/* right */break;case 4:mevent->button = 3;/* middle */break;default:break;}#if 0mevent->x = buf[1];mevent->y = buf[2];#elsemevent->x =  buf[1] - ((buf[0] & 0x10) ? 256 : 0);mevent->y = -buf[2] + ((buf[0] & 0x20) ? 256 : 0); #endifreturn 0;}/* cursor */static int fb_pixel_m(fb_info fb_inf, int x, int y, u32_t color){/* 16 bits */u8_t *pos = (u8_t*)fb_inf.fbmem + (fb_inf.w * y + x) * fb_inf.bpp/8;switch (fb_inf.bpp){case 32:  /* 32 bits */*(pos + 3) = color >> 24;case 24:  /* 24 bits */*(pos + 2) = color >> 16;case 16:         /* 16 bits */*(pos + 1) = color >> 8;case 8:      /* 8 bits */*pos = color;return 0;default:return -1;}return -1;}int fb_restorecursor(fb_info fb_inf, int x, int y){int i, j;u32_t color;for (j = 0;j<C_HEIGHT;j++){for (i = 0;i<C_WIDTH;i++){color = save_cursor[i + j * C_WIDTH];fb_pixel_m(fb_inf,x+i,y+j,color);}}return 0;}static int  fb_savecursor (fb_info fb_inf,int x,int y){int i,j;u32_t color;for (j = 0;j<C_HEIGHT;j++){for (i = 0;i<C_WIDTH;i++){  color = *(u32_t*)(fb_inf.fbmem + ((y + j) * fb_inf.w + x + i) * fb_inf.bpp/8);save_cursor[i + j * C_WIDTH] = color;}}return 0;}int  fb_drawcursor(fb_info fb_inf, int x, int y){int i,j;unsigned int color;fb_savecursor (fb_inf,x,y);for (j = 0;j<C_HEIGHT;j++){for (i = 0;i<C_WIDTH;i++){color = cursor_pixel[i + j * C_WIDTH];if (color != T___)fb_pixel_m(fb_inf,x+i,y+j,color);}}return 0;}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 2021 33111112222255555233333444444555556666699999777778 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友。本题就要求你找出那些帅到没有朋友 Kushsays 醉扶归 千里马论坛之独孤求败 牛牛机器人 红包牛牛机器人 牛牛财务 牛牛算账机器人 钙资源 _判断素数Description输入一个正整数r(0 输入一个正整数r(0 全排列 office激活 office2013激活 g_tk C#网络通信 C语言笔试 ucos qt包含文件 科大讯飞java 1,定义一个接口Assaultable(可攻击的),该接口有一个抽象方法attack()。2,定义一 FlyAudioVoice 2023 编写一个应用程序绘制一个如下的操作菜单并实现功能(定义一个类学生表示学生,有成员变量姓名name和年 Minecraft 1204:剔除相关数 平坦衰落信道2FSK差错性能分析 平坦衰落信道 编写一个应用程序绘制一个如下的操作菜单并实现功能(定义一个类学生表示学生,有成员变量姓名name和年 手机是现在人们必不可少、最重要的通讯工具,拥有一款简单、实用、易用的手机通讯录,将会使你的手机更加好 手机是现在人们必不可少、最重要的通讯工具,拥有一款简单、实用、易用的手机通讯录,将会使你的手机更加好 编写一个应用程序绘制一个如下的操作菜单并实现功能(定义一个类学生表示学生,有成员变量姓名name和年 61858 61850 全国市级城市拼音 pycr rpyc pry 安德地产公司