File: vtkTDxUnixDevice.cxx

package info (click to toggle)
vtk6 6.3.0%2Bdfsg2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,972 kB
  • sloc: cpp: 1,442,790; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,119; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 154; makefile: 68; objc: 17
file content (270 lines) | stat: -rw-r--r-- 8,650 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkTDxUnixDevice.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkTDxUnixDevice.h"

#include <cassert>

#include <X11/Xlib.h> // Needed for X types used in the public interface
// Display *DisplayId; // Actually a "Display *" but we cannot include Xlib.h
//  Window WindowId; // Actually a "Window" but we cannot include Xlib.h


#define SGI // Used in xdrvlib.h to define ParameterCheck

// xdrvlib.h does not have the usual __cplusplus extern "C" guard
extern "C" {
#include "xdrvlib.h" // Magellan X-Window driver API.
}

#include "vtkTDxMotionEventInfo.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkMath.h"

vtkStandardNewMacro(vtkTDxUnixDevice);

// ----------------------------------------------------------------------------
// Description:
// Default constructor. Just set initial values for
// DisplayId (0), WindowId (0)), TranslationScale (1.0),
// RotationScale (1.0).
vtkTDxUnixDevice::vtkTDxUnixDevice()
{
  this->DisplayId=0;
  this->WindowId=0;
  this->TranslationScale=1.0;
  this->RotationScale=1.0;
  this->Interactor=0;
//  this->DebugOn();
}

// ----------------------------------------------------------------------------
// Description:
// Destructor. If the device is not initialized, do nothing. If the device
// is initialized, close the device.
vtkTDxUnixDevice::~vtkTDxUnixDevice()
{
  if(this->Initialized)
    {
    this->Close();
    }
}

// ----------------------------------------------------------------------------
// Description:
// Get the ID of the X Display. Initial value is 0.
vtkTDxUnixDeviceDisplay *vtkTDxUnixDevice::GetDisplayId() const
{
  return this->DisplayId;
}

// ----------------------------------------------------------------------------
// Description:
// Get the ID of the X Window. Initial value is 0.
vtkTDxUnixDeviceWindow vtkTDxUnixDevice::GetWindowId() const
{
  return this->WindowId;
}

// ----------------------------------------------------------------------------
// Description:
// Set the ID of the X Display.
// \pre not_yet_initialized: !GetInitialized()
void vtkTDxUnixDevice::SetDisplayId(vtkTDxUnixDeviceDisplay *id)
{
  assert("pre: not_yet_initialized" && !this->GetInitialized());
  if(this->DisplayId!=id)
    {
    this->DisplayId=id;
    this->Modified();
    }
}

// ----------------------------------------------------------------------------
// Description:
// Set the ID of the X Window.
// \pre not_yet_initialized: !GetInitialized()
void vtkTDxUnixDevice::SetWindowId(vtkTDxUnixDeviceWindow id)
{
  assert("pre: not_yet_initialized" && !this->GetInitialized());
  if(this->WindowId!=id)
    {
    this->WindowId=id;
    this->Modified();
    }
}

// ----------------------------------------------------------------------------
// Description:
// Initialize the device with the current display and window ids.
// It updates the value of GetInitialized().
// Initialization can fail. You must look for the value of
// GetInitialized() before processing further.
// \pre not_yet_initialized: !GetInitialized()
// \pre valid_display: GetDisplayId()!=0
// \pre valid_window: GetWindowId()!=0
// \pre valid_interactor: GetInteractor()!=0
void vtkTDxUnixDevice::Initialize()
{
  assert("pre: not_yet_initialized" && !this->GetInitialized());
  assert("pre: valid_display" && this->GetDisplayId()!=0);
  assert("pre: valid_window" && this->GetWindowId()!=0);

  int status=MagellanInit(static_cast<Display *>(this->DisplayId),
                          static_cast<Window>(this->WindowId));
  this->Initialized=status==1;
}

// ----------------------------------------------------------------------------
// Description:
// Close the device. This is called by the destructor.
// You don't have to close the device explicitly, as the destructor do it
// automatically, but you can.
// \pre initialized: GetInitialized().
// \post restored: !GetInitialized()
void vtkTDxUnixDevice::Close()
{
  assert("pre: initialized" && this->GetInitialized());

  vtkDebugMacro(<< "Close()" );
  MagellanClose(static_cast<Display *>(this->DisplayId));
  this->Initialized=false;

  assert("post: restored" && !this->GetInitialized());
}

// ----------------------------------------------------------------------------
// Description:
// Translate the X11 event by invoking a VTK event, if the event came from
// the device.
// Return true if the event passed in argument was effectively an event from
// the device, return false otherwise.
// \pre initialized: GetInitialized()
// \pre e_exists: e!=0
// \pre e_is_client_message: e->type==ClientMessage
bool vtkTDxUnixDevice::ProcessEvent(const vtkTDxUnixDeviceXEvent *e)
{
  assert("pre: initialized" && this->GetInitialized());
  assert("pre: e_exists" && e!=0);
  assert("pre: e_is_client_message" &&
         static_cast<const XEvent *>(e)->type==ClientMessage);

  MagellanFloatEvent info;

  const XEvent *event=static_cast<const XEvent *>(e);

  int deviceEvent=MagellanTranslateEvent(
    static_cast<Display *>(this->DisplayId),
    const_cast<XEvent *>(event),
    &info,
    this->TranslationScale,
    this->RotationScale);

  vtkDebugMacro(<< "deviceEvent=" << deviceEvent);

  vtkTDxMotionEventInfo motionInfo;
  int buttonInfo;
  double axis[3];

  bool result;
  switch(deviceEvent)
    {
    case MagellanInputMotionEvent:
      vtkDebugMacro(<< "it is MagellanInputMotionEvent");
      MagellanRemoveMotionEvents(static_cast<Display *>(this->DisplayId));
      motionInfo.X=info.MagellanData[MagellanX];
      motionInfo.Y=info.MagellanData[MagellanY];

      // On Unix, the Z axis is reversed (wrong). We want to have a
      // right-handed coordinate system, so positive Z has to come towards us,
      // as on Windows.
      motionInfo.Z=-info.MagellanData[MagellanZ];

      axis[0]=info.MagellanData[MagellanA];
      axis[1]=info.MagellanData[MagellanB];

      // On Unix, the Z axis is reserved (wrong).
      axis[2]=-info.MagellanData[MagellanC];

      motionInfo.Angle=vtkMath::Norm(axis);
      if(motionInfo.Angle==0.0)
        {
        motionInfo.AxisX=0.0;
        motionInfo.AxisY=0.0;
        motionInfo.AxisZ=1.0;
        }
      else
        {
        motionInfo.AxisX=axis[0]/motionInfo.Angle;
        motionInfo.AxisY=axis[1]/motionInfo.Angle;
        motionInfo.AxisZ=axis[2]/motionInfo.Angle;
        }
      if(this->Interactor!=0)
        {
        this->Interactor->InvokeEvent(vtkCommand::TDxMotionEvent,&motionInfo);
        }
      result=true;
      break;
    case MagellanInputButtonPressEvent:
      vtkDebugMacro(<< "it is  MagellanInputButtonPressEvent");
      buttonInfo=info.MagellanButton;
      if(this->Interactor!=0)
        {
        this->Interactor->InvokeEvent(vtkCommand::TDxButtonPressEvent,
                                      &buttonInfo);
        }
      result=true;
      break;
    case MagellanInputButtonReleaseEvent:
      vtkDebugMacro(<< "it is  MagellanInputButtonReleaseEvent");
      buttonInfo=info.MagellanButton;
      if(this->Interactor!=0)
        {
        this->Interactor->InvokeEvent(vtkCommand::TDxButtonReleaseEvent,
                                      &buttonInfo);
        }
      result=true;
      break;
    default:
      vtkDebugMacro(<< "it is not a Magellan event");
      result=false;
      break;
    }

  return result;
}

// ----------------------------------------------------------------------------
// Description:
// Set the sensitivity of the device for the current application.
// A neutral value is 1.0.
// \pre initialized: GetInitialized()
void vtkTDxUnixDevice::SetSensitivity(double sensitivity)
{
  assert("pre: initialized" && this->GetInitialized());

  MagellanApplicationSensitivity(static_cast<Display *>(this->DisplayId),
                                 sensitivity);
}

// ----------------------------------------------------------------------------
void vtkTDxUnixDevice::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "RotationScale: " << this->RotationScale << endl;
  os << indent << "TranslationScale: " << this->TranslationScale << endl;
}