linux/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 HIM_CONTINUE     2
00026 #define MAX_MOVE    5 // maximum of pixel that a mouse can be moved
00027 
00028 
00029 //using namespace nsIProxyObjectManager;
00030 NS_IMPL_ISUPPORTS1(Him, IHim)
00031 Him::Him()
00032 {
00033      
00034      regX = regY = -1;
00035      display = XOpenDisplay(NULL);
00036      
00037   /* member initializers and constructor code */
00038 }
00039 
00040 Him::~Him()
00041 {
00042      XCloseDisplay(display);
00043   /* destructor code */
00044 }
00045 
00046 
00047 
00048 /* void MoveCursorTo (in unsigned short posX, in unsigned short posY); */
00049 NS_IMETHODIMP Him::MouseMove(PRInt16 posX, PRInt16 posY, PRInt16 *status)
00050 {
00051      if (display == NULL){
00052          perror("display == NULL (Function : Him.MoveCursorTo)");
00053      }
00054      int min = MIN(abs(posX),abs(posY)), i, a=((posX>0)?1:-1), b=((posY>0)?1:-1), steps = 0;
00055 
00056      // we see where the pointer is in the screen
00057      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);
00058      for(i=0; i<min;i++){
00059           
00060           SetCursorTo(a,b);
00061           
00062           if(detectMouseMove()){
00063                regX = -1;
00064                regY = -1;
00065                *status = HIM_CANCEL_ACTION;
00066                return NS_OK;
00067           }
00068           Msleep(4);
00069                
00070      }
00071      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
00072      for(i=0; i<MAX(abs(posX),abs(posY))-min;i++){
00073           SetCursorTo(a,b);
00074           
00075           if(detectMouseMove()){
00076                regX = -1;
00077                regY = -1;
00078                *status = HIM_CANCEL_ACTION;
00079                return NS_OK;
00080           }
00081           Msleep(4);
00082      }
00083      regX = regY = -1;
00084      *status = HIM_NORMAL;
00085      XSendEvent(display, PointerWindow, True, 0xfff, &event);
00086      return NS_OK;
00087 }
00088 
00089 NS_IMETHODIMP Him::SetCursorTo(PRInt16 x, PRInt16 y){
00090      //Display *displayMain = XOpenDisplay(NULL);
00091      //XFlush(display);
00092      if(display == NULL)
00093      {
00094           perror("Erreur SetCursorTo\n");
00095           return NS_ERROR_FAILURE;
00096      }
00097 
00098      XWarpPointer(display, RootWindow(display, DefaultScreen(display)), None, 0, 0, 0, 0, (int)x, (int)y);
00099      return NS_OK;
00100 
00101      //XCloseDisplay(display);
00102 }
00103 
00104 NS_IMETHODIMP Him::DetectMouseMove(int * res){
00105      *res = detectMouseMove();
00106      return NS_OK;
00107 }
00108 
00109 bool Him::detectMouseMove(){
00110      int curX, curY;
00111      XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &curX, &curY, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
00112      if(regX>0 && regY>0 && (abs(curX-regX) >MAX_MOVE || abs(curY-regY)>MAX_MOVE)){
00113           regY = regX = -1;
00114           return true;
00115      }
00116      regX = curX;
00117      regY = curY;
00118      return false;
00119 }
00120 NS_IMETHODIMP Him::ResetReg(){
00121      regX = regY = -1;
00122      return NS_OK;
00123 }
00124 NS_IMETHODIMP Him::Msleep(PRInt16 time){
00125   usleep(time*1000);
00126   return NS_OK;
00127 }
00128 
00129 NS_IMETHODIMP Him::Msleep2(PRInt16 time, PRInt16* status){
00130      int i;
00131      for(i=0; i<time/100; i++){
00132           Msleep(100);
00133           if(detectMouseMove()){
00134                *status =  HIM_CANCEL_ACTION; 
00135                return NS_OK;
00136           }
00137      }
00138      Msleep(time- 100*((int)time/100));
00139      return NS_OK;
00140 }
00141 
00142 NS_IMETHODIMP Him::GetMousePosition(PRInt16* posX, PRInt16* posY){
00143      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);
00144      *posX = event.xbutton.x_root;
00145      *posY = event.xbutton.y_root;
00146      return NS_OK;
00147 }
00148 
00149 NS_IMETHODIMP Him::Click(PRInt16 button){                                                                      
00150         //Display *display = XOpenDisplay(NULL);     
00151         fflush(stdout);
00152      //XEvent event;
00153         if(display == NULL)
00154         {                  
00155                 fprintf(stderr, "Erreur mouseClick !!!\n");
00156                 return NS_ERROR_FAILURE;                  
00157         }                                                  
00158 
00159         memset(&event, 0x00, sizeof(event));
00160 
00161         event.type = ButtonPress;
00162         event.xbutton.button = button;
00163         event.xbutton.same_screen = True;
00164 
00165         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);
00166 
00167         event.xbutton.subwindow = event.xbutton.window;
00168 
00169         while(event.xbutton.subwindow)
00170         {
00171                 event.xbutton.window = event.xbutton.subwindow;
00172                 XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
00173         }
00174      XSendEvent(display, PointerWindow, True, 0xfff, &event);
00175         XFlush(display);
00176 
00177         usleep(10000);
00178 
00179         event.type = ButtonRelease;
00180         event.xbutton.state = 0x100;
00181      XSendEvent(display, PointerWindow, True, 0xfff, &event);
00182         XFlush(display);
00183 
00184         //XCloseDisplay(display);
00185 
00186      return NS_OK;
00187 }

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