diff --git a/src/input/mouse.c b/src/input/mouse.c index 942fcaf1..68b1b94c 100755 --- a/src/input/mouse.c +++ b/src/input/mouse.c @@ -1,43 +1,47 @@ -/* *************************************************************************** - * mouse.c -- mouse support - * - * Copyright (C) 2012-2013 by - * Liu Chao - * - * This file is part of the LCUI project, and may only be used, modified, and - * distributed under the terms of the GPLv2. - * - * (GPLv2 is abbreviation of GNU General Public License Version 2) - * - * By continuing to use, modify, or distribute this file you indicate that you - * have read the license and understand and accept it fully. - * - * The LCUI project is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GPL v2 for more details. - * - * You should have received a copy of the GPLv2 along with this file. It is - * usually in the LICENSE.TXT file, If not, see . - * ****************************************************************************/ - -/* **************************************************************************** - * mouse.c -- 鼠标支持 - * - * 版权所有 (C) 2013 归属于 - * 刘超 - * - * 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。 - * - * (GPLv2 是 GNU通用公共许可证第二版 的英文缩写) - * - * 继续使用、修改或发布本文件,表明您已经阅读并完全理解和接受这个许可协议。 - * - * LCUI 项目是基于使用目的而加以散布的,但不负任何担保责任,甚至没有适销性或特 - * 定用途的隐含担保,详情请参照GPLv2许可协议。 - * - * 您应已收到附随于本文件的GPLv2许可协议的副本,它通常在LICENSE.TXT文件中,如果 - * 没有,请查看:. - * ****************************************************************************/ +/** ****************************************************************************** + * @file mouse.c + * @brief mouse input support. + * @author Liu Chao + * @warning + * Copyright (C) 2012-2013 by \n + * Liu Chao \n + * \n + * This file is part of the LCUI project, and may only be used, modified, and \n + * distributed under the terms of the GPLv2. \n + * \n + * (GPLv2 is abbreviation of GNU General Public License Version 2) \n + * \n + * By continuing to use, modify, or distribute this file you indicate that you \n + * have read the license and understand and accept it fully. \n + * \n + * The LCUI project is distributed in the hope that it will be useful, but \n + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY \n + * or FITNESS FOR A PARTICULAR PURPOSE. See the GPL v2 for more details. \n + * \n + * You should have received a copy of the GPLv2 along with this file. It is \n + * usually in the LICENSE.TXT file, If not, see . \n + * ******************************************************************************/ + +/** ****************************************************************************** + * @file mouse.c + * @brief 鼠标输入支持。 + * @author 刘超 + * @warning + * 版权所有 (C) 2012-2013 归属于 \n + * 刘超 \n + * \n + * 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。 \n + * \n + * (GPLv2 是 GNU通用公共许可证第二版 的英文缩写) \n + * \n + * 继续使用、修改或发布本文件,表明您已经阅读并完全理解和接受这个许可协议。 \n + * \n + * LCUI 项目是基于使用目的而加以散布的,但不负任何担保责任,甚至没有适销性或特定\n + 用途的隐含担保,详情请参照GPLv2许可协议。 \n + * \n + * 您应已收到附随于本文件的GPLv2许可协议的副本,它通常在LICENSE.TXT文件中,如果 \n + * 没有,请查看:. \n + * ******************************************************************************/ #include #include LC_LCUI_H @@ -52,86 +56,29 @@ #include #endif -/********************** 鼠标相关信息 ***************************/ -typedef struct _LCUI_Mouse -{ #ifdef LCUI_MOUSE_DRIVER_LINUX - int fd, state; /* 句柄,状态 */ +static int mouse_fd; /**< 鼠标设备标示符 */ +static int mouse_state; /**< 鼠标设备的状态 */ #endif - float move_speed; /* 鼠标移动速度,1.0为正常速度 */ -} -LCUI_Mouse; -/*************************************************************/ -static LCUI_Mouse mouse_data; +/** 鼠标左右键的状态 */ +static int button_state[2]={ + LCUIKEYSTATE_RELEASE, + LCUIKEYSTATE_RELEASE +}; -/****************************** Mouse *********************************/ -/* - * 功能:检测鼠标事件中鼠标左键的状态 - * 说明:该函数只适用于响应鼠标按键状态发生改变时,判断按键状态。 - * 返回值: - * -2 事件无效 - * -1 键值不是鼠标左键的键值 - * 0 鼠标左键已经释放 - * 1 鼠标左键处于按下状态 - **/ -LCUI_API int -Mouse_LeftButton( LCUI_MouseButtonEvent *event ) -{ - if( !event ) { - return -2; - } - else if(event->button == LCUIKEY_LEFTBUTTON) { - return event->state; - } - return -1; -} - -/* - * 功能:检测鼠标事件中鼠标右键的状态 - * 说明:该函数只适用于响应鼠标按键状态发生改变时,判断按键状态。 - * 返回值: - * -2 事件指针为NULL - * -1 键值不是鼠标右键的键值 - * 0 鼠标右键已经释放 - * 1 鼠标右键处于按下状态 - **/ -LCUI_API int -Mouse_RightButton( LCUI_MouseButtonEvent *event ) -{ - if(NULL == event) { - return -2; - } - else if(event->button == LCUIKEY_RIGHTBUTTON) { - return event->state; - } - return -1; -} - -LCUI_API int -Click_LeftButton (LCUI_MouseButtonEvent *event) -/* 功能:检测是否是按鼠标左键 */ -{ - /* 如果按下的是鼠标左键,并且之前没有按住它 */ - if (Mouse_LeftButton(event) == LCUIKEYSTATE_PRESSED - && !LCUIKey_IsHit(event->button)) { - return 1; - } - return 0; -} - -/* 记录被按下的指定键的键值,并添加LCUI_MOUSEBUTTONDOWN事件 */ -LCUI_API int -LCUIMouse_ButtonDown( LCUI_Pos pos, int key_code ) +/** 按下指定鼠标按键 */ +LCUI_API int LCUIMouse_ButtonDown( int key_code ) { LCUI_Event event; + LCUI_Pos pos; - /* 若该键已经按下,就不需要再添加至队列了 */ - if( LCUIKey_IsHit( key_code ) ) { + if( button_state[key_code] == LCUIKEYSTATE_PRESSED ) { return -1; } - LCUIKey_Hit( key_code ); + button_state[key_code] = LCUIKEYSTATE_PRESSED; event.type = LCUI_MOUSEBUTTONDOWN; + pos = LCUICursor_GetPos(); event.button.x = pos.x; event.button.y = pos.y; event.button.button = key_code; @@ -140,17 +87,18 @@ LCUIMouse_ButtonDown( LCUI_Pos pos, int key_code ) return 0; } -/* 记录被释放的指定键的键值,并添加LCUI_MOUSEBUTTONUP事件 */ -LCUI_API int -LCUIMouse_ButtonUp( LCUI_Pos pos, int key_code ) +/** 释放指定鼠标按键 */ +LCUI_API int LCUIMouse_ButtonUp( int key_code ) { LCUI_Event event; - - if( !LCUIKey_IsHit( key_code ) ) { + LCUI_Pos pos; + + if( button_state[key_code] == LCUIKEYSTATE_PRESSED ) { return -1; } LCUIKey_Free( key_code ); event.type = LCUI_MOUSEBUTTONUP; + pos = LCUICursor_GetPos(); event.button.x = pos.x; event.button.y = pos.y; event.button.button = key_code; @@ -159,52 +107,30 @@ LCUIMouse_ButtonUp( LCUI_Pos pos, int key_code ) return 0; } -LCUI_API void -LCUI_PushMouseMotionEvent( LCUI_Pos new_pos, int key_state ) +/** 添加鼠标移动事件 */ +LCUI_API void LCUI_PushMouseMotionEvent( LCUI_Pos new_pos ) { + LCUI_Event event; static LCUI_Pos old_pos = {0,0}; - /* 如果鼠标位置有改变 */ - if(Pos_Cmp( new_pos, old_pos ) != 0) { - LCUI_Event event; - event.type = LCUI_MOUSEMOTION; - event.motion.x = new_pos.x; - event.motion.y = new_pos.y; - event.motion.xrel = new_pos.x - old_pos.x; - event.motion.yrel = new_pos.y - old_pos.y; - event.motion.state = key_state; - LCUI_PushEvent( &event ); - old_pos = new_pos; - } -} -/* 处理鼠标产生的事件 */ -LCUI_API void -LCUI_PushMouseEvent( LCUI_Pos new_pos, int button_type ) -{ - int key_state = LCUIKEYSTATE_RELEASE; - switch (button_type) { - case 1: /* 鼠标左键被按下 */ - LCUIMouse_ButtonDown( new_pos, LCUIKEY_LEFTBUTTON ); - key_state = LCUIKEYSTATE_PRESSED; - LCUIMouse_ButtonUp( new_pos, LCUIKEY_RIGHTBUTTON ); - break; - case 2: /* 鼠标右键被按下 */ - LCUIMouse_ButtonDown( new_pos, LCUIKEY_RIGHTBUTTON ); - LCUIMouse_ButtonUp( new_pos, LCUIKEY_LEFTBUTTON ); - key_state = LCUIKEYSTATE_RELEASE; - break; - case 3: /* 鼠标左右键被按下 */ - LCUIMouse_ButtonDown( new_pos, LCUIKEY_RIGHTBUTTON ); - LCUIMouse_ButtonDown( new_pos, LCUIKEY_LEFTBUTTON ); - key_state = LCUIKEYSTATE_PRESSED; - break; - default: /* 默认是释放的 */ - LCUIMouse_ButtonUp( new_pos, LCUIKEY_RIGHTBUTTON ); - LCUIMouse_ButtonUp( new_pos, LCUIKEY_LEFTBUTTON ); - key_state = LCUIKEYSTATE_RELEASE; - break; + if( new_pos.x == old_pos.x + && new_pos.y == old_pos.y ) { + return; } - LCUI_PushMouseMotionEvent( new_pos, key_state ); + + event.type = LCUI_MOUSEMOTION; + event.motion.x = new_pos.x; + event.motion.y = new_pos.y; + event.motion.xrel = new_pos.x - old_pos.x; + event.motion.yrel = new_pos.y - old_pos.y; + if( button_state[0] == LCUIKEYSTATE_PRESSED + || button_state[1] == LCUIKEYSTATE_PRESSED ) { + event.motion.state = LCUIKEYSTATE_PRESSED; + } else { + event.motion.state = LCUIKEYSTATE_RELEASE; + } + LCUI_PushEvent( &event ); + old_pos = new_pos; } #ifdef LCUI_MOUSE_DRIVER_LINUX @@ -243,55 +169,41 @@ static LCUI_BOOL proc_mouse( void ) } pos = LCUICursor_GetNewPos(); - pos.x += (buf[1] * mouse_data.move_speed); - pos.y -= (buf[2] * mouse_data.move_speed); - - if (pos.x > LCUIScreen_GetWidth ()) { - pos.x = LCUIScreen_GetWidth (); + pos.x += buf[1]; + pos.y -= buf[2]; + if( pos.x > LCUIScreen_GetWidth() ) { + pos.x = LCUIScreen_GetWidth(); } - if (pos.y > LCUIScreen_GetHeight ()) { - pos.y = LCUIScreen_GetHeight (); + if( pos.y > LCUIScreen_GetHeight() ) { + pos.y = LCUIScreen_GetHeight(); } pos.x = pos.x<0 ? 0:pos.x; pos.y = pos.y<0 ? 0:pos.y; /* 应用鼠标游标的位置变更 */ LCUICursor_SetPos( pos ); - /* 处理鼠标事件 */ - LCUI_PushMouseEvent( pos, buf[0] & 0x07 ); + switch ( buf[0]&0x07 ) { + case 1: /* 鼠标左键被按下 */ + LCUIMouse_ButtonDown( new_pos, LCUIKEY_LEFTBUTTON ); + LCUIMouse_ButtonUp( new_pos, LCUIKEY_RIGHTBUTTON ); + break; + case 2: /* 鼠标右键被按下 */ + LCUIMouse_ButtonDown( new_pos, LCUIKEY_RIGHTBUTTON ); + LCUIMouse_ButtonUp( new_pos, LCUIKEY_LEFTBUTTON ); + break; + case 3: /* 鼠标左右键被按下 */ + LCUIMouse_ButtonDown( new_pos, LCUIKEY_RIGHTBUTTON ); + LCUIMouse_ButtonDown( new_pos, LCUIKEY_LEFTBUTTON ); + break; + default: /* 默认是释放的 */ + LCUIMouse_ButtonUp( new_pos, LCUIKEY_RIGHTBUTTON ); + LCUIMouse_ButtonUp( new_pos, LCUIKEY_LEFTBUTTON ); + break; + } + LCUI_PushMouseMotionEvent( new_pos ); return TRUE; } #else -LCUI_API int -Win32_LCUIMouse_ButtonDown( int key_code ) -{ - LCUI_Pos pos; - pos = LCUICursor_GetPos(); - DEBUG_MSG("pos: %d,%d\n", pos.x, pos.y); - return LCUIMouse_ButtonDown( pos, key_code ); -} - -LCUI_API int -Win32_LCUIMouse_ButtonUp( int key_code ) -{ - LCUI_Pos pos; - pos = LCUICursor_GetPos(); - DEBUG_MSG("pos: %d,%d\n", pos.x, pos.y); - return LCUIMouse_ButtonUp( pos, key_code ); -} - -static void Win32_LCUIMouse_PushMotionEvent( LCUI_Pos pos ) -{ - int key_state; - if( LCUIKey_IsHit( LCUIKEY_LEFTBUTTON ) - || LCUIKey_IsHit( LCUIKEY_RIGHTBUTTON ) ) { - key_state = LCUIKEYSTATE_PRESSED; - } else { - key_state = LCUIKEYSTATE_RELEASE; - } - LCUI_PushMouseMotionEvent( pos, key_state ); -} - static LCUI_BOOL proc_mouse( void ) { LCUI_Pos pos; @@ -315,17 +227,16 @@ static LCUI_BOOL proc_mouse( void ) } pos.x = new_pos.x; pos.y = new_pos.y; - /* 更新鼠标位置 */ + /* 更新鼠标游标的位置 */ LCUICursor_SetPos( pos ); - Win32_LCUIMouse_PushMotionEvent( pos ); + LCUI_PushMouseMotionEvent( pos ); DEBUG_MSG("new pos: %d,%d\n", pos.x, pos.y); return TRUE; } #endif -LCUI_API LCUI_BOOL -Enable_Mouse_Input(void) -/* 功能:启用鼠标输入处理 */ +/** 启用鼠标输入处理 */ +LCUI_API LCUI_BOOL Enable_Mouse_Input(void) { #ifdef LCUI_MOUSE_DRIVER_LINUX char *msdev; @@ -352,9 +263,8 @@ Enable_Mouse_Input(void) return TRUE; } -LCUI_API LCUI_BOOL -Disable_Mouse_Input(void) -/* 功能:禁用鼠标输入处理 */ +/** 禁用鼠标输入处理 */ +LCUI_API LCUI_BOOL Disable_Mouse_Input(void) { #ifdef LCUI_MOUSE_DRIVER_LINUX if(mouse_data.state != INSIDE) { @@ -367,11 +277,9 @@ Disable_Mouse_Input(void) return TRUE; } -/* 初始化鼠标输入模块 */ -LCUI_API int -LCUIModule_Mouse_Init( void ) +/** 初始化鼠标输入模块 */ +LCUI_API int LCUIModule_Mouse_Init( void ) { - mouse_data.move_speed = 1; /* 移动数度为1 */ #ifdef LCUI_MOUSE_DRIVER_LINUX mouse_data.fd = -1; mouse_data.state = REMOVE; /* 鼠标为移除状态 */ @@ -382,4 +290,3 @@ LCUIModule_Mouse_Init( void ) return LCUIDevice_Add( Enable_Mouse_Input, proc_mouse, Disable_Mouse_Input ); } -/**************************** Mouse End *******************************/