linux如何使用鼠标数据的,浅析linux中鼠标数据读取

001

#include

002

#include

003

#include

004

#include

005

#include

006

// 以下代码源自

007

// [需要在文本控制台下运行,才能获取到数据,在ubuntu 8.10 GNOME界面只能收到1个字节数据0xfa][luther.gliethttp]

008

// libminigui-1.6.10/src/ial/native/native.c

009

// InitIAL==>__mg_cur_input = inputs{"console", InitNativeInput, TermNativeInput},

010

// mousedev = mousedev_IMPS2;

011

// input->update_mouse = mouse_update;

012

/* Mouse button bits*/

013

#define WHEEL_UP 0x10

014

#define WHEEL_DOWN 0x08

015

#define BUTTON_L 0x04

016

#define BUTTON_M 0x02

017

#define BUTTON_R 0x01

018

#define SCALE 3

/* default scaling factor for acceleration */

019

#define THRESH 5

/* default threshhold for acceleration */

020

static

int

xpos;

/* current x position of mouse */

021

static

int

ypos;

/* current y position of mouse */

022

static

int

minx;

/* minimum allowed x position */

023

static

int

maxx;

/* maximum allowed x position */

024

static

int

miny;

/* minimum allowed y position */

025

static

int

maxy;

/* maximum allowed y position */

026

static

int

buttons;

/* current state of buttons */

027

static

int

scale

=

SCALE;

/* acceleration scale factor */

028

static

int

thresh

=

THRESH;

/* acceleration threshhold */

029

static

int

mouse_update(

int

dx

,

int

dy

,

int

dz);

030

static

int

IMPS2_Read (

int

*

dx

,

int

*

dy

,

int

*

dz

,

int

*bp);

031

static

void

mouse_setposition (

int

newx

,

int

newy);

032

static

void

mouse_setrange (

int

newminx

,

int

newminy

,

int

newmaxx

,

int

newmaxy);

033

int

mouse_fd;

034

int

main(

void)

035

{

036

int

dx

,

dy

,

dz;

037

static

unsigned

char

imps2_param

[]

=

{

243

,

200

,

243

,

100

,

243

,

80

};

//,242};

038

// 来自vnc4的xc/programs/Xserver/hw/xfree86/input/mouse/mouse.c==>PROT_IMPS2

039

const

char

*

mdev

=

"/dev/input/mice";

040

mouse_fd

=

open (

mdev

,

O_RDWR);

// | O_NONBLOCK);

041

if (

mouse_fd

<

0)

{

042

printf(

"[luther.gliethttp]: RW error [please use root user]: %s

/n

"

,

mdev);

043

mouse_fd

=

open (

mdev

,

O_RDONLY);

// | O_NONBLOCK);

044

if (

mouse_fd

<

0)

045

return

-

1;

046

}

else

{

047

write (

mouse_fd

,

imps2_param

,

sizeof (

imps2_param));

// 初始化序列, 这样可以读取4个字节数据

048

// 0x80用来表示滚轮向上还是向下滚动.de>

049

de

>

// 0xa0表示滚轮向上滚动的同时中键按下

050

de

>

051

de

>

printf(

"[luther.gliethttp]: imps2_param ok!

/n

");

052

}

053

mouse_setrange(

0

,

0

,

1024

,

768);

054

for (;;)

{

055

IMPS2_Read(

&

dx

,

&

dy

,

&

dz

,

&

buttons);

056

mouse_update(

dx

,

dy

,

dz);

057

mouse_setposition(

xpos

,

ypos);

058

printf(

"[%04d,%04d,0x%04x]

/n

"

,

xpos

,

ypos

,

buttons);

059

}

060

return

0;

061

}

062

static

int

IMPS2_Read (

int

*

dx

,

int

*

dy

,

int

*

dz

,

int

*bp)

063

{

064

static

unsigned

char

buf

[

5

];

065

static

int

buttons

[

7

]

=

{

0

,

1

,

3

,

0

,

2

,

0

,

0

};

// 1左键,2中键,3右键

066

static

int

nbytes;

067

int n;

068

while ((n

=

read (

mouse_fd

,

&

buf

[

nbytes

],

4

-

nbytes)))

{

069

if (n

<

0)

{

070

if (

errno

==

EINTR)

071

continue;

072

else

073

return

-

1;

074

}

075

nbytes

+= n;

076

if (

nbytes

==

4)

{

077

int

wheel;

078

// printf("[luther.gliethttp]: %02x %02x %02x %02x/n", buf[0], buf[1], buf[2], buf[3]);

079

if ((

buf

[

0

]

&

0xc0)

!=

0)

{

080

buf

[

0

]

=

buf

[

1

];

081

buf

[

1

]

=

buf

[

2

];

082

buf

[

2

]

=

buf

[

3

];

083

nbytes

=

3;

084

return

-

1;

085

}

086

/* FORM XFree86 4.0.1 */

087

*bp

=

buttons

[(

buf

[

0

]

&

0x07

)];

088

*

dx

= (

buf

[

0

]

&

0x10)

?

buf

[

1

]

-

256

:

buf

[

1

];

089

*

dy

= (

buf

[

0

]

&

0x20)

?

-(

buf

[

2

]

-

256)

:

-

buf

[

2

];

090

/* Is a wheel event? */

091

if ((

wheel

=

buf

[

3

])

!=

0)

{

092

if(

wheel

>

0x7f)

{

093

*bp

|=

WHEEL_UP;

094

}

095

else

{

096

*bp

|=

WHEEL_DOWN;

097

}

098

}

099

*

dz

=

0;

100

nbytes

=

0;

101

return

1;

102

}

103

}

104

return

0;

105

}

106

static

int

mouse_update(

int

dx

,

int

dy

,

int

dz)

107

{

108

int

r;

109

int

sign;

110

sign

=

1;

111

if (

dx

<

0)

{

112

sign

=

-

1;

113

dx

=

-

dx;

114

}

115

if (

dx

>

thresh)

116

dx

=

thresh

+ (

dx

-

thresh)

*

scale;

117

dx

*=

sign;

118

xpos

+=

dx;

119

if(

xpos

<

minx )

120

xpos

=

minx;

121

if(

xpos

>

maxx )

122

xpos

=

maxx;

123

sign

=

1;

124

if (

dy

<

0)

{

125

sign

=

-

1;

126

dy

=

-

dy;

127

}

128

if (

dy

>

thresh)

129

dy

=

thresh

+ (

dy

-

thresh)

*

scale;

130

dy

*=

sign;

131

ypos

+=

dy;

132

if (

ypos

<

miny )

133

ypos

=

miny;

134

if (

ypos

>

maxy )

135

ypos

=

maxy;

136

return

1;

137

}

138

static

void

mouse_setposition (

int

newx

,

int

newy)

139

{

140

if (

newx

<

minx)

141

newx

=

minx;

142

if (

newx

>

maxx)

143

newx

=

maxx;

144

if (

newy

<

miny)

145

newy

=

miny;

146

if (

newy

>

maxy)

147

newy

=

maxy;

148

if (

newx

==

xpos

&&

newy

==

ypos)

149

return;

150

xpos

=

newx;

151

ypos

=

newy;

152

}

153

static

void

mouse_setrange (

int

newminx

,

int

newminy

,

int

newmaxx

,

int

newmaxy)

154

{

155

minx

=

newminx;

156

miny

=

newminy;

157

maxx

=

newmaxx;

158

maxy

=

newmaxy;

159

mouse_setposition ((

newminx

+

newmaxx)

/

2

, (

newminy

+

newmaxy)

/

2);

160

}

161

static

int

mouse_getbutton (

void)

162

{

163

return

buttons;

164

}

165

static

void

mouse_getxy (

int

*

x

,

int

*

y)

166

{

167

*

x

=

xpos;

168

*

y

=

ypos;

169

}

170

171

172

本文来自

CSDN

博客,转载请标明出处:

http

:

//blog.csdn.net/linucos/archive/2010/03/31/5436767.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nohup和screen是在Linux系统常用的运行后台任务的工具,它们在功能和使用方式上有一些区别。 首先,nohup是一个可以运行命令或脚本的命令。使用nohup命令可以让命令在后台运行,并且不会受到终端退出的影响。使用nohup的示例如下: ``` nohup command & ``` 其,command是要运行的命令或脚本,&表示在后台运行。例如,要在后台运行一个名为task.sh的脚本,可以使用以下命令: ``` nohup ./task.sh & ``` nohup命令会将命令的输出信息重定向到nohup.out文件,可以通过查看该文件来获取命令的输出。 相比之下,screen是一个用于多路复用命令行界面的工具。使用screen可以在一个终端窗口同时运行多个会话,并且可以在不同的会话之间切换。使用screen的示例如下: ``` screen -S session_name ``` 其,session_name是会话的名称,可以根据需要指定。进入会话后,可以在其运行命令或脚本。 要在screen会话运行后台任务,可以先进入会话,然后使用nohup命令来运行命令或脚本。例如,要在一个名为my_session的会话运行任务,可以按照以下步骤进行: 1. 进入my_session会话:`screen -r my_session` 2. 运行需要后台运行的命令或脚本:`nohup command &` nohup命令将任务放在后台运行,并且不受终端退出的影响。可以通过查看nohup.out文件来获取命令的输出。 总结来说,nohup适用于简单的后台任务,它可以让命令在后台运行并且不受终端退出的影响。而screen适用于需要同时运行多个会话,并且能够在会话之间切换的情况下使用nohup来运行后台任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值