00001 // Filename: vrpnDial.cxx 00002 // Created by: drose (26Jan01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "vrpnDial.h" 00020 #include "vrpnDialDevice.h" 00021 #include "vrpnClient.h" 00022 #include "config_vrpn.h" 00023 00024 #include <indent.h> 00025 00026 #include <algorithm> 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: VrpnDial::Constructor 00030 // Access: Public 00031 // Description: 00032 //////////////////////////////////////////////////////////////////// 00033 VrpnDial:: 00034 VrpnDial(const string &dial_name, vrpn_Connection *connection) : 00035 _dial_name(dial_name) 00036 { 00037 _dial = new vrpn_Dial_Remote(_dial_name.c_str(), connection); 00038 00039 _dial->register_change_handler((void*)this, &vrpn_dial_callback); 00040 } 00041 00042 //////////////////////////////////////////////////////////////////// 00043 // Function: VrpnDial::Destructor 00044 // Access: Public 00045 // Description: 00046 //////////////////////////////////////////////////////////////////// 00047 VrpnDial:: 00048 ~VrpnDial() { 00049 delete _dial; 00050 } 00051 00052 //////////////////////////////////////////////////////////////////// 00053 // Function: VrpnDial::mark 00054 // Access: Public 00055 // Description: Adds the indicated VrpnDialDevice to the list of 00056 // devices that are sharing this VrpnDial. 00057 //////////////////////////////////////////////////////////////////// 00058 void VrpnDial:: 00059 mark(VrpnDialDevice *device) { 00060 if (vrpn_cat.is_debug()) { 00061 vrpn_cat.debug() << *this << " marking " << *device << "\n"; 00062 } 00063 _devices.push_back(device); 00064 } 00065 00066 //////////////////////////////////////////////////////////////////// 00067 // Function: VrpnDial::unmark 00068 // Access: Public 00069 // Description: Removes the indicated VrpnDialDevice from the list 00070 // of devices that are sharing this VrpnDial. 00071 //////////////////////////////////////////////////////////////////// 00072 void VrpnDial:: 00073 unmark(VrpnDialDevice *device) { 00074 if (vrpn_cat.is_debug()) { 00075 vrpn_cat.debug() << *this << " unmarking " << *device << "\n"; 00076 } 00077 00078 Devices::iterator di = 00079 find(_devices.begin(), _devices.end(), device); 00080 00081 if (di != _devices.end()) { 00082 _devices.erase(di); 00083 } 00084 } 00085 00086 //////////////////////////////////////////////////////////////////// 00087 // Function: VrpnDial::output 00088 // Access: Public 00089 // Description: 00090 //////////////////////////////////////////////////////////////////// 00091 void VrpnDial:: 00092 output(ostream &out) const { 00093 out << _dial_name; 00094 } 00095 00096 //////////////////////////////////////////////////////////////////// 00097 // Function: VrpnDial::write 00098 // Access: Public 00099 // Description: 00100 //////////////////////////////////////////////////////////////////// 00101 void VrpnDial:: 00102 write(ostream &out, int indent_level) const { 00103 indent(out, indent_level) 00104 << get_dial_name() << " (" 00105 << _devices.size() << " devices)\n"; 00106 } 00107 00108 //////////////////////////////////////////////////////////////////// 00109 // Function: VrpnDial::vrpn_dial_callback 00110 // Access: Private, Static 00111 // Description: Receives the dial event data from the VRPN 00112 // code and sends it to any interested 00113 // VrpnDialDevices. 00114 //////////////////////////////////////////////////////////////////// 00115 void VrpnDial:: 00116 vrpn_dial_callback(void *userdata, const vrpn_DIALCB info) { 00117 VrpnDial *self = (VrpnDial *)userdata; 00118 00119 if (vrpn_cat.is_debug()) { 00120 vrpn_cat.debug() 00121 << *self << " got dial " << info.dial << " = " << info.change << "\n"; 00122 } 00123 00124 Devices::iterator di; 00125 for (di = self->_devices.begin(); di != self->_devices.end(); ++di) { 00126 VrpnDialDevice *device = (*di); 00127 device->lock(); 00128 device->push_dial(info.dial, info.change); 00129 device->unlock(); 00130 } 00131 }