windows/src/Him.cpp

00001 /* Copyright (c) 2008, Florent FAYOLLE
00002 
00003 
00004 This file is part of Zenith
00005 
00006 Zenith is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
00018 
00019 /* Implementation file */
00020 #include "Him.h"
00021 #define   MIN(x,y)  (((x)<(y))?(x):(y))
00022 #define   MAX(x,y)  (((x)>(y))?(x):(y))
00023 #define HIM_NORMAL  0
00024 #define HIM_CANCEL_ACTION     1
00025 #define MAX_MOVE    2
00026 
00027 
00028 
00029 NS_IMPL_ISUPPORTS1(Him, IHim)
00030 Him::Him()
00031 {
00032      
00033      regX = regY = -1;
00034      
00035   /* member initializers and constructor code */
00036 }
00037 
00038 Him::~Him()
00039 {
00040   /* destructor code */
00041 }
00042 
00043 /* void MoveCursorTo (in unsigned short posX, in unsigned short posY); */
00044 NS_IMETHODIMP Him::MouseMove(PRInt16 posX, PRInt16 posY, PRInt16 *status)
00045 {
00046      int min = MIN(abs(posX),abs(posY)), i, a=((posX>0)?1:-1), b=((posY>0)?1:-1);
00047 
00048      
00049      GetCursorPos(&pos);
00050      // we see where the pointer is in the screen
00051      for(i=0; i<min;i++){
00052           SetCursorPos(pos.x+a,pos.y+b);
00053           
00054           
00055           if(detectMouseMove()){
00056                *status = HIM_CANCEL_ACTION;
00057                //XCloseDisplay(display);
00058                return NS_OK;
00059           }
00060           
00061           Msleep(4);
00062      }
00063      a*=(abs(posX)>abs(posY)); b*=(abs(posX)<abs(posY));//abs(a)=1 && b=0 if x>y, abs(b)=1 && a=0 if y>x
00064      for(i=0; i<MAX(abs(posX),abs(posY))-min;i++){
00065           if(detectMouseMove()){
00066                *status = HIM_CANCEL_ACTION;
00067                //XCloseDisplay(display);
00068                return NS_OK;
00069           }
00070           SetCursorPos(pos.x+a,pos.y+b);
00071           Msleep(4);
00072           
00073      }
00074      regX = regY = -1;
00075      *status = HIM_NORMAL;
00076      //XCloseDisplay(display);
00077      return NS_OK;
00078 }
00079 NS_IMETHODIMP Him::DetectMouseMove(int* res){
00080      *res = detectMouseMove();
00081      return NS_OK;
00082 }
00083 bool Him::detectMouseMove(){
00084      GetCursorPos(&pos);
00085      int curX = pos.x, curY = pos.y;
00086      if(regX>0 && regY>0 && (abs(curX-regX) >MAX_MOVE || abs(curY-regY)>MAX_MOVE)){
00087           //printf("la souris a bougé");
00088           regX = regY = -1;
00089           return true;
00090      }
00091      regX = curX;
00092      regY = curY;
00093      return false;
00094 }
00095 NS_IMETHODIMP Him::ResetReg(){
00096      regX = regY = -1;
00097      return NS_OK;
00098 }
00099 NS_IMETHODIMP Him::Msleep(PRInt16 time){
00100   Sleep(time);
00101   return NS_OK;
00102 }
00103 
00104 NS_IMETHODIMP Him::Msleep2(PRInt16 time, PRInt16* status){
00105      int i;
00106      for(i=0; i<time/100; i++){
00107           Msleep(100);
00108           if(detectMouseMove()){
00109                *status =  HIM_CANCEL_ACTION; 
00110                return NS_OK;
00111           }
00112      }
00113      Msleep(time-100*((int)time/100));
00114      return NS_OK;
00115 }
00116 
00117 NS_IMETHODIMP Him::GetMousePosition(PRInt16* posX, PRInt16* posY){
00118      //XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
00119      GetCursorPos(&pos);
00120      *posX = pos.x;
00121      *posY = pos.y;
00122      return NS_OK;
00123 }
00124 NS_IMETHODIMP Him::SetCursorTo(PRInt16 x, PRInt16 y){
00125      SetCursorPos(x,y);
00126      return NS_OK;
00127 }
00128 
00129 NS_IMETHODIMP Him::Click(PRInt16 button){                                                                      
00130         //Display *display = XOpenDisplay(NULL);     
00131      switch(button){
00132           case 1: // button left
00133                mouse_event(MOUSEEVENTF_LEFTDOWN, 0,0,0,0);
00134                Sleep(100);
00135                mouse_event(MOUSEEVENTF_LEFTUP, 0,0,0,0);
00136                break;
00137           case 2:
00138                mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0,0,0,0);
00139                Sleep(100);
00140                mouse_event(MOUSEEVENTF_MIDDLEUP, 0,0,0,0);
00141                break;
00142           case 3:
00143                mouse_event(MOUSEEVENTF_RIGHTDOWN, 0,0,0,0);
00144                Sleep(100);
00145                mouse_event(MOUSEEVENTF_RIGHTUP, 0,0,0,0);
00146                break;
00147      }
00148 
00149   return NS_OK;
00150 }

Generated on Sat Dec 5 17:45:36 2009 for zenith by  doxygen 1.6.1