00001 // Filename: vrpnButton.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 "vrpnButton.h" 00020 #include "vrpnButtonDevice.h" 00021 #include "vrpnClient.h" 00022 #include "config_vrpn.h" 00023 00024 #include <indent.h> 00025 00026 #include <algorithm> 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: VrpnButton::Constructor 00030 // Access: Public 00031 // Description: 00032 //////////////////////////////////////////////////////////////////// 00033 VrpnButton:: 00034 VrpnButton(const string &button_name, vrpn_Connection *connection) : 00035 _button_name(button_name) 00036 { 00037 _button = new vrpn_Button_Remote(_button_name.c_str(), connection); 00038 00039 _button->register_change_handler((void*)this, &vrpn_button_callback); 00040 } 00041 00042 //////////////////////////////////////////////////////////////////// 00043 // Function: VrpnButton::Destructor 00044 // Access: Public 00045 // Description: 00046 //////////////////////////////////////////////////////////////////// 00047 VrpnButton:: 00048 ~VrpnButton() { 00049 delete _button; 00050 } 00051 00052 //////////////////////////////////////////////////////////////////// 00053 // Function: VrpnButton::mark 00054 // Access: Public 00055 // Description: Adds the indicated VrpnButtonDevice to the list of 00056 // devices that are sharing this VrpnButton. 00057 //////////////////////////////////////////////////////////////////// 00058 void VrpnButton:: 00059 mark(VrpnButtonDevice *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: VrpnButton::unmark 00068 // Access: Public 00069 // Description: Removes the indicated VrpnButtonDevice from the list 00070 // of devices that are sharing this VrpnButton. 00071 //////////////////////////////////////////////////////////////////// 00072 void VrpnButton:: 00073 unmark(VrpnButtonDevice *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: VrpnButton::output 00088 // Access: Public 00089 // Description: 00090 //////////////////////////////////////////////////////////////////// 00091 void VrpnButton:: 00092 output(ostream &out) const { 00093 out << _button_name; 00094 } 00095 00096 //////////////////////////////////////////////////////////////////// 00097 // Function: VrpnButton::write 00098 // Access: Public 00099 // Description: 00100 //////////////////////////////////////////////////////////////////// 00101 void VrpnButton:: 00102 write(ostream &out, int indent_level) const { 00103 indent(out, indent_level) 00104 << get_button_name() << " (" 00105 << _devices.size() << " devices)\n"; 00106 } 00107 00108 //////////////////////////////////////////////////////////////////// 00109 // Function: VrpnButton::vrpn_button_callback 00110 // Access: Private, Static 00111 // Description: Receives the button event data from the VRPN 00112 // code and sends it to any interested 00113 // VrpnButtonDevices. 00114 //////////////////////////////////////////////////////////////////// 00115 void VrpnButton:: 00116 vrpn_button_callback(void *userdata, const vrpn_BUTTONCB info) { 00117 VrpnButton *self = (VrpnButton *)userdata; 00118 if (vrpn_cat.is_debug()) { 00119 vrpn_cat.debug() 00120 << *self << " got button " << info.button << " = " << info.state << "\n"; 00121 } 00122 00123 Devices::iterator di; 00124 for (di = self->_devices.begin(); di != self->_devices.end(); ++di) { 00125 VrpnButtonDevice *device = (*di); 00126 device->lock(); 00127 device->set_button_state(info.button, info.state != 0); 00128 device->unlock(); 00129 } 00130 }