Salome HOME
620e97ec073ea73f0ce900bf31b96b515bd924b9
[modules/gui.git] / src / SVTK / SVTK_SpaceMouse.cxx
1 //  SALOME VTKViewer : build VTK viewer into Salome desktop
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SVTK_SpaceMouse.cxx
25 //  Author : Alexander SLADKOV
26 //  Module : SALOME
27 //  $Header$
28
29 #include <string.h>
30 #include <math.h>
31 #include <stdio.h>
32
33 #include <X11/X.h>
34 #include <X11/Xlib.h>
35 #include <X11/Xutil.h>
36 #include <X11/Xatom.h>
37 #include <X11/keysym.h>
38
39 #include "SVTK_SpaceMouse.h"
40
41 //---------------------------------------------
42 SVTK_SpaceMouse* SVTK_SpaceMouse::myInstance = 0;
43
44 //---------------------------------------------
45 SVTK_SpaceMouse* SVTK_SpaceMouse::getInstance()
46 {
47   if ( !myInstance )
48     myInstance = new SVTK_SpaceMouse();
49   return myInstance;
50 }
51
52 //---------------------------------------------
53 SVTK_SpaceMouse::SVTK_SpaceMouse()
54 {
55   win = InputFocus;
56   spaceMouseOn = 0;
57 }
58
59 //---------------------------------------------
60 int SVTK_SpaceMouse::initialize( Display *display, Window window )
61 {
62  XMotionEvent        = XInternAtom( display, "MotionEvent",        1 );
63  XButtonPressEvent   = XInternAtom( display, "ButtonPressEvent",   1 );
64  XButtonReleaseEvent = XInternAtom( display, "ButtonReleaseEvent", 1 );
65  XCommandEvent       = XInternAtom( display, "CommandEvent",       1 );
66
67  spaceMouseOn = (XMotionEvent        != 0) && 
68                 (XButtonPressEvent   != 0) && 
69                 (XButtonReleaseEvent != 0) && 
70                 (XCommandEvent       != 0);
71  if ( !spaceMouseOn )
72   return 0;
73
74  spaceMouseOn = setWindow( display, window );
75  if ( !spaceMouseOn )
76   return 0;
77  
78  return spaceMouseOn; 
79 }
80
81 //---------------------------------------------
82 static int errorCallback( Display *display, XErrorEvent *Error )
83 {
84   char msg[ 128 ];
85   if ( Error->error_code != BadWindow ) {
86     XGetErrorText( display,Error->error_code,msg,sizeof( msg ) );
87     fprintf( stderr, "SpaceMouse reported error = %s. Exit ... \n", msg );
88   }
89   return 0;
90 }
91
92 //---------------------------------------------
93 int SVTK_SpaceMouse::setWindow( Display *display, Window window )
94 {
95   XTextProperty winName;
96   XEvent xEvent;
97   Atom type;
98   int format;
99   unsigned long NItems, BytesReturn;
100   unsigned char *PropReturn;
101   Window root;
102   int result;
103   int (*errorHandler)(Display *,XErrorEvent *);
104
105   result = 1;
106   errorHandler = XSetErrorHandler( errorCallback );
107  
108   root = RootWindow( display, DefaultScreen(display) );
109
110   PropReturn = NULL;
111   XGetWindowProperty( display, root, XCommandEvent, 0,1, 0,
112                       AnyPropertyType, &type, &format, &NItems,
113                       &BytesReturn, &PropReturn );
114
115   win = InputFocus;
116   if ( PropReturn != NULL ) {
117     win = *(Window *) PropReturn;
118     XFree( PropReturn );
119   }
120   else
121     return result = 0;
122
123   if ( XGetWMName( display, win, &winName ) == 0 )
124     return result = 0;
125
126   if ( strcmp( (char *) "Magellan Window", (char *) winName.value) != 0 )
127     return result = 0;
128
129   xEvent.type = ClientMessage;
130   xEvent.xclient.format = 16;
131   xEvent.xclient.send_event = 0;
132   xEvent.xclient.display = display;
133   xEvent.xclient.window = win;
134   xEvent.xclient.message_type = XCommandEvent;
135   
136   xEvent.xclient.data.s[0] = (short) ((window>>16)&0x0000FFFF);
137   xEvent.xclient.data.s[1] = (short)  (window&0x0000FFFF);
138   xEvent.xclient.data.s[2] = 27695;
139
140   if ( XSendEvent( display, win, 0, 0x0000, &xEvent ) == 0 )
141     return result = 0;
142
143   XFlush( display );
144
145   XSetErrorHandler( errorHandler );
146   return result;
147 }
148
149 //---------------------------------------------
150 int SVTK_SpaceMouse::close(Display *display)
151 {
152   initialize( display, (Window)InputFocus );
153   spaceMouseOn = 0;
154   
155   return 1;
156 }
157
158 //---------------------------------------------
159 int SVTK_SpaceMouse::translateEvent( Display* display, XEvent* xEvent, MoveEvent* spaceMouseEvent,
160                     double scale, double rScale )
161 {
162   if ( !spaceMouseOn )
163     return 0;
164
165   if ( xEvent->type == ClientMessage ) {
166     if ( xEvent->xclient.message_type == XMotionEvent ) {
167       spaceMouseEvent->type = SpaceMouseMove;
168       spaceMouseEvent->data[ x ] =
169         xEvent->xclient.data.s[2] * scale;
170       spaceMouseEvent->data[ y ] =
171         xEvent->xclient.data.s[3] * scale;
172       spaceMouseEvent->data[ z ] =
173         xEvent->xclient.data.s[4] * scale;
174       spaceMouseEvent->data[ a ] =
175         xEvent->xclient.data.s[5] * rScale;
176       spaceMouseEvent->data[ b ] =
177         xEvent->xclient.data.s[6] * rScale;
178       spaceMouseEvent->data[ c ] =
179         xEvent->xclient.data.s[7] * rScale;
180       spaceMouseEvent->period = xEvent->xclient.data.s[8];
181       return 1;
182     }
183     else if ( xEvent->xclient.message_type == XButtonPressEvent ) {
184       spaceMouseEvent->type = SpaceButtonPress;
185       spaceMouseEvent->button = xEvent->xclient.data.s[2];
186       return 2;
187     }
188     else if ( xEvent->xclient.message_type == XButtonReleaseEvent ) {
189       spaceMouseEvent->type = SpaceButtonRelease;
190       spaceMouseEvent->button = xEvent->xclient.data.s[2];
191       return 3;
192     }
193   }
194   return (!display);
195 }
196